Skip to content

Commit 9d0425f

Browse files
authored
[AUDIO_WORKLET] Fix emscripten_audio_worklet_post_function_sig (#23108)
Fix emscripten_audio_worklet_post_function_sig, which was previously not worked, and enhances test to cover all emscripten_audio_worklet_post_function_*.
1 parent 10257ae commit 9d0425f

File tree

2 files changed

+179
-14
lines changed

2 files changed

+179
-14
lines changed

src/lib/libwebaudio.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ let LibraryWebAudio = {
378378
emscripten_audio_worklet_post_function_3(audioContext, funcPtr, arg0, arg1, arg2);
379379
},
380380

381-
emscripten_audio_worklet_post_function_sig__deps: ['$readAsmConstArgs'],
381+
emscripten_audio_worklet_post_function_sig__deps: ['$readEmAsmArgs'],
382382
emscripten_audio_worklet_post_function_sig: (audioContext, funcPtr, sigPtr, varargs) => {
383383
#if ASSERTIONS
384384
assert(audioContext >= 0);
@@ -387,7 +387,7 @@ let LibraryWebAudio = {
387387
assert(UTF8ToString(sigPtr)[0] != 'v', 'Do NOT specify the return argument in the signature string for a call to emscripten_audio_worklet_post_function_sig(), just pass the function arguments.');
388388
assert(varargs);
389389
#endif
390-
(audioContext ? EmAudio[audioContext].audioWorklet.bootstrapMessage.port : globalThis['messagePort']).postMessage({'_wsc': funcPtr, 'x': readAsmConstArgs(sigPtr, varargs) });
390+
(audioContext ? EmAudio[audioContext].audioWorklet.bootstrapMessage.port : globalThis['messagePort']).postMessage({'_wsc': funcPtr, 'x': readEmAsmArgs(sigPtr, varargs) });
391391
}
392392
};
393393

test/webaudio/audioworklet_post_function.c

Lines changed: 177 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,193 @@
55
// and the Audio Worklet thread using the
66
// emscripten_audio_worklet_post_function_*() API.
77

8-
// This event will fire on the main thread.
9-
void MessageReceivedOnMainThread(int d, int e, int f) {
10-
emscripten_outf("MessageReceivedOnMainThread: d=%d, e=%d, f=%d", d, e, f);
11-
assert(!emscripten_current_thread_is_audio_worklet());
12-
assert(d == 1 && e == 2 && f == 3);
8+
// This test consists of two steps
9+
// 1. post invocation from main thread to audio worklet thread
10+
// 2. post invocation from audio worklet thread to main thread
11+
_Atomic uint32_t callbackCount = 0;
1312

14-
// test succeeded, were able to post a message from main thread to audio thread and back!
13+
void do_exit() {
14+
emscripten_out("do_exit");
15+
assert(callbackCount == 16);
1516
emscripten_force_exit(0);
1617
}
1718

18-
// This event will fire on the audio worklet thread.
19-
void MessageReceivedInAudioWorkletThread(int a, int b) {
20-
emscripten_outf("MessageReceivedInAudioWorkletThread: a=%d, b=%d", a, b);
19+
// [v, vi, vii, viii, vd, vdd, vddd, viiiiiidddddd] * [audio_worklet, main]
20+
// 8 * 2 callbacks invoked, each callbacks check the correctivity of arguments
21+
void v_received_on_audio_worklet() {
22+
assert(emscripten_current_thread_is_audio_worklet());
23+
emscripten_out("v_on_audio_worklet");
24+
++callbackCount;
25+
}
26+
27+
void vi_received_on_audio_worklet(int i) {
28+
assert(emscripten_current_thread_is_audio_worklet());
29+
emscripten_out("vi_on_audio_worklet");
30+
assert(i == 1);
31+
++callbackCount;
32+
}
33+
34+
void vii_received_on_audio_worklet(int i, int j) {
35+
assert(emscripten_current_thread_is_audio_worklet());
36+
emscripten_out("vii_on_audio_worklet");
37+
assert(i == 2);
38+
assert(j == 3);
39+
++callbackCount;
40+
}
41+
42+
void viii_received_on_audio_worklet(int i, int j, int k) {
43+
assert(emscripten_current_thread_is_audio_worklet());
44+
emscripten_out("viii_on_audio_worklet");
45+
assert(i == 4);
46+
assert(j == 5);
47+
assert(k == 6);
48+
++callbackCount;
49+
}
50+
51+
void vd_received_on_audio_worklet(double i) {
2152
assert(emscripten_current_thread_is_audio_worklet());
22-
assert(a == 42 && b == 9000);
23-
emscripten_audio_worklet_post_function_viii(EMSCRIPTEN_AUDIO_MAIN_THREAD, MessageReceivedOnMainThread, /*d=*/1, /*e=*/2, /*f=*/3);
53+
emscripten_out("vd_on_audio_worklet");
54+
assert(i == 1.5);
55+
++callbackCount;
56+
}
57+
58+
void vdd_received_on_audio_worklet(double i, double j) {
59+
assert(emscripten_current_thread_is_audio_worklet());
60+
emscripten_out("vdd_on_audio_worklet");
61+
assert(i == 2.5);
62+
assert(j == 3.5);
63+
++callbackCount;
64+
}
65+
66+
void vddd_received_on_audio_worklet(double i, double j, double k) {
67+
assert(emscripten_current_thread_is_audio_worklet());
68+
emscripten_out("vddd_on_audio_worklet");
69+
assert(i == 4.5);
70+
assert(j == 5.5);
71+
assert(k == 6.5);
72+
++callbackCount;
73+
}
74+
75+
void viiiiiidddddd_received_on_audio_worklet(int a, int b, int c, int d, int e, int f, double g, double h, double i, double j, double k, double l) {
76+
assert(emscripten_current_thread_is_audio_worklet());
77+
emscripten_out("viiiiiidddddd_on_audio_worklet");
78+
assert(a == 10);
79+
assert(b == 11);
80+
assert(c == 12);
81+
assert(d == 13);
82+
assert(e == 14);
83+
assert(f == 15);
84+
assert(g == 16.5);
85+
assert(h == 17.5);
86+
assert(i == 18.5);
87+
assert(j == 19.5);
88+
assert(k == 20.5);
89+
assert(l == 21.5);
90+
++callbackCount;
91+
}
92+
93+
void v_received_on_main() {
94+
assert(!emscripten_current_thread_is_audio_worklet());
95+
emscripten_out("v_on_main");
96+
++callbackCount;
97+
}
98+
99+
void vi_received_on_main(int i) {
100+
assert(!emscripten_current_thread_is_audio_worklet());
101+
emscripten_out("vi_on_main");
102+
assert(i == 1);
103+
++callbackCount;
104+
}
105+
106+
void vii_received_on_main(int i, int j) {
107+
assert(!emscripten_current_thread_is_audio_worklet());
108+
emscripten_out("vii_on_main");
109+
assert(i == 2);
110+
assert(j == 3);
111+
++callbackCount;
112+
}
113+
114+
void viii_received_on_main(int i, int j, int k) {
115+
assert(!emscripten_current_thread_is_audio_worklet());
116+
emscripten_out("viii_on_main");
117+
assert(i == 4);
118+
assert(j == 5);
119+
assert(k == 6);
120+
++callbackCount;
121+
}
122+
123+
void vd_received_on_main(double i) {
124+
assert(!emscripten_current_thread_is_audio_worklet());
125+
emscripten_out("vd_on_main");
126+
assert(i == 1.5);
127+
++callbackCount;
128+
}
129+
130+
void vdd_received_on_main(double i, double j) {
131+
assert(!emscripten_current_thread_is_audio_worklet());
132+
emscripten_out("vdd_on_main");
133+
assert(i == 2.5);
134+
assert(j == 3.5);
135+
++callbackCount;
136+
}
137+
138+
void vddd_received_on_main(double i, double j, double k) {
139+
assert(!emscripten_current_thread_is_audio_worklet());
140+
emscripten_out("vddd_on_main");
141+
assert(i == 4.5);
142+
assert(j == 5.5);
143+
assert(k == 6.5);
144+
++callbackCount;
145+
}
146+
147+
void viiiiiidddddd_received_on_main(int a, int b, int c, int d, int e, int f, double g, double h, double i, double j, double k, double l) {
148+
assert(!emscripten_current_thread_is_audio_worklet());
149+
emscripten_out("viiiiiidddddd_on_main");
150+
assert(a == 10);
151+
assert(b == 11);
152+
assert(c == 12);
153+
assert(d == 13);
154+
assert(e == 14);
155+
assert(f == 15);
156+
assert(g == 16.5);
157+
assert(h == 17.5);
158+
assert(i == 18.5);
159+
assert(j == 19.5);
160+
assert(k == 20.5);
161+
assert(l == 21.5);
162+
++callbackCount;
163+
}
164+
165+
void lastCallbackOnAudioWorklet() {
166+
assert(emscripten_current_thread_is_audio_worklet());
167+
assert(callbackCount == 8);
168+
169+
// These event will fire callbacks on main thread.
170+
emscripten_audio_worklet_post_function_v(EMSCRIPTEN_AUDIO_MAIN_THREAD, v_received_on_main);
171+
emscripten_audio_worklet_post_function_vi(EMSCRIPTEN_AUDIO_MAIN_THREAD, vi_received_on_main, 1);
172+
emscripten_audio_worklet_post_function_vii(EMSCRIPTEN_AUDIO_MAIN_THREAD, vii_received_on_main, 2, 3);
173+
emscripten_audio_worklet_post_function_viii(EMSCRIPTEN_AUDIO_MAIN_THREAD, viii_received_on_main, 4, 5, 6);
174+
emscripten_audio_worklet_post_function_vd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vd_received_on_main, 1.5);
175+
emscripten_audio_worklet_post_function_vdd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vdd_received_on_main, 2.5, 3.5);
176+
emscripten_audio_worklet_post_function_vddd(EMSCRIPTEN_AUDIO_MAIN_THREAD, vddd_received_on_main, 4.5, 5.5, 6.5);
177+
emscripten_audio_worklet_post_function_sig(EMSCRIPTEN_AUDIO_MAIN_THREAD, viiiiiidddddd_received_on_main, "iiiiiidddddd", 10, 11, 12, 13, 14, 15, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5);
178+
emscripten_audio_worklet_post_function_v(EMSCRIPTEN_AUDIO_MAIN_THREAD, do_exit);
24179
}
25180

26181
// This callback will fire when the audio worklet thread has been initialized.
27182
void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool success, void *userData) {
28183
emscripten_out("WebAudioWorkletThreadInitialized");
29-
emscripten_audio_worklet_post_function_vii(audioContext, MessageReceivedInAudioWorkletThread, /*a=*/42, /*b=*/9000);
184+
185+
// These event will fire callbacks on audio worklet thread.
186+
emscripten_audio_worklet_post_function_v(audioContext, v_received_on_audio_worklet );
187+
emscripten_audio_worklet_post_function_vi(audioContext, vi_received_on_audio_worklet , 1);
188+
emscripten_audio_worklet_post_function_vii(audioContext, vii_received_on_audio_worklet , 2, 3);
189+
emscripten_audio_worklet_post_function_viii(audioContext, viii_received_on_audio_worklet , 4, 5, 6);
190+
emscripten_audio_worklet_post_function_vd(audioContext, vd_received_on_audio_worklet , 1.5);
191+
emscripten_audio_worklet_post_function_vdd(audioContext, vdd_received_on_audio_worklet , 2.5, 3.5);
192+
emscripten_audio_worklet_post_function_vddd(audioContext, vddd_received_on_audio_worklet , 4.5, 5.5, 6.5);
193+
emscripten_audio_worklet_post_function_sig(audioContext, viiiiiidddddd_received_on_audio_worklet , "iiiiiidddddd", 10, 11, 12, 13, 14, 15, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5);
194+
emscripten_audio_worklet_post_function_v(audioContext, lastCallbackOnAudioWorklet);
30195
}
31196

32197
uint8_t wasmAudioWorkletStack[4096];

0 commit comments

Comments
 (0)