FreeRDP
Loading...
Searching...
No Matches
rdpsnd_emscripten.c
1
17
18/*
19 * Play() runs on the audio channel thread. It copies the PCM samples and hands them to the
20 * main browser thread asynchronously, so it never waits for the busy UI thread. The main thread
21 * converts them to float and schedules them back to back with AudioBufferSourceNode.start(),
22 * and keeps the amount of queued audio (ms) in shared memory, which Play() reports to the server
23 * as latency. Browsers only allow audio after a user gesture: a page that creates and resumes
24 * Module.freerdpAudioContext in its connect click handler gets sound at once. Otherwise the backend
25 * creates the context itself, and samples are dropped until the user interacts with the page.
26 */
27
28#include <freerdp/config.h>
29
30#include <stdint.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include <emscripten.h>
35#include <emscripten/proxying.h>
36#include <emscripten/threading.h>
37
38#include <winpr/assert.h>
39#include <winpr/crt.h>
40#include <winpr/stream.h>
41
42#include <freerdp/types.h>
43#include <freerdp/codec/audio.h>
44
45#include "rdpsnd_main.h"
46
47typedef struct
48{
49 rdpsndDevicePlugin device;
50 UINT32 channels;
51 UINT32 rate;
52 UINT32 volume;
53 volatile int queued_ms; /* written by the main thread */
54 AUDIO_FORMAT asked; /* the last format passed to FormatSupported() */
55} rdpsndEmscriptenPlugin;
56
57/*
58 * The format the server sends on the wire, for the web page's Info panel:
59 * [0] wFormatTag (WAVE_FORMAT_UNKNOWN = no audio yet), [1] nSamplesPerSec, [2] nChannels.
60 *
61 * Open() only sees the PCM format the device plays: for Opus, AAC or ADPCM rdpsnd opens the
62 * device with a PCM copy of the wire format and decodes with freerdp_dsp. The wire format is
63 * what rdpsnd asked FormatSupported() about right before Open() (rdpsnd_ensure_device_is_open),
64 * and the PCM copy keeps its rate and channels. Open() takes the asked format only when rate
65 * and channels match, and otherwise publishes the format it opened with.
66 */
67static uint32_t web_audio_format[3];
68
69EMSCRIPTEN_KEEPALIVE uint32_t* freerdp_web_audio_format(void);
70EMSCRIPTEN_KEEPALIVE uint32_t* freerdp_web_audio_format(void)
71{
72 return web_audio_format;
73}
74
75static void web_audio_format_set(UINT16 tag, UINT32 rate, UINT16 channels)
76{
77 __atomic_store_n(&web_audio_format[1], rate, __ATOMIC_RELAXED);
78 __atomic_store_n(&web_audio_format[2], channels, __ATOMIC_RELAXED);
79 __atomic_store_n(&web_audio_format[0], tag, __ATOMIC_RELEASE);
80}
81
82typedef struct
83{
84 rdpsndEmscriptenPlugin* plugin;
85 BYTE* data;
86 size_t size;
87 UINT32 channels; /* the format at Play(): Open() may change it before the job runs */
88 UINT32 rate;
89} play_job;
90
91/* in rdpsnd_emscripten.js */
92extern void rdpsnd_web_play(BYTE* data, int size, int channels, int rate, int volume,
93 int* queued_ms);
94
95static void play_on_main(void* arg)
96{
97 play_job* job = arg;
98 WINPR_ASSERT(job);
99
100 rdpsndEmscriptenPlugin* p = job->plugin;
101 WINPR_ASSERT(p);
102
103 rdpsnd_web_play(job->data, (int)job->size, (int)job->channels, (int)job->rate,
104 (int)(p->volume & 0xffff), (int*)&p->queued_ms);
105 free(job->data);
106 free(job);
107}
108
109WINPR_ATTR_NODISCARD
110static BOOL pcm_playable(const AUDIO_FORMAT* format)
111{
112 WINPR_ASSERT(format);
113
114 /* Web Audio takes float samples at any rate: accept 16 bit PCM, mono or stereo */
115 return (format->wFormatTag == WAVE_FORMAT_PCM) && (format->wBitsPerSample == 16) &&
116 (format->nChannels >= 1) && (format->nChannels <= 2) && (format->nSamplesPerSec > 0);
117}
118
119WINPR_ATTR_NODISCARD
120static BOOL rdpsnd_emscripten_format_supported(rdpsndDevicePlugin* device,
121 const AUDIO_FORMAT* format)
122{
123 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
124 WINPR_ASSERT(p);
125 WINPR_ASSERT(format);
126
127 p->asked = *format;
128 p->asked.data = nullptr;
129 return pcm_playable(format);
130}
131
132static BOOL rdpsnd_emscripten_open(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format,
133 WINPR_ATTR_UNUSED UINT32 latency)
134{
135 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
136 WINPR_ASSERT(p);
137
138 if (!format || !pcm_playable(format))
139 return FALSE;
140 p->channels = format->nChannels;
141 p->rate = format->nSamplesPerSec;
142
143 const AUDIO_FORMAT* wire = format;
144 if ((p->asked.wFormatTag != WAVE_FORMAT_UNKNOWN) &&
145 (p->asked.nSamplesPerSec == format->nSamplesPerSec) &&
146 (p->asked.nChannels == format->nChannels))
147 wire = &p->asked;
148 web_audio_format_set(wire->wFormatTag, wire->nSamplesPerSec, wire->nChannels);
149 return TRUE;
150}
151
152static void rdpsnd_emscripten_close(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device)
153{
154}
155
156static BOOL rdpsnd_emscripten_set_volume(rdpsndDevicePlugin* device, UINT32 value)
157{
158 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
159 WINPR_ASSERT(p);
160
161 p->volume = value;
162 return TRUE;
163}
164
165static UINT32 rdpsnd_emscripten_get_volume(rdpsndDevicePlugin* device)
166{
167 const rdpsndEmscriptenPlugin* p = (const rdpsndEmscriptenPlugin*)device;
168 WINPR_ASSERT(p);
169
170 return p->volume;
171}
172
173static UINT rdpsnd_emscripten_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
174{
175 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
176 WINPR_ASSERT(p);
177
178 if (!data || size == 0 || p->channels == 0)
179 return 0;
180
181 play_job* job = calloc(1, sizeof(play_job));
182 if (!job)
183 return 0;
184 job->data = malloc(size);
185 if (!job->data)
186 {
187 free(job);
188 return 0;
189 }
190 memcpy(job->data, data, size);
191 job->plugin = p;
192 job->size = size;
193 job->channels = p->channels;
194 job->rate = p->rate;
195
196 if (emscripten_is_main_browser_thread())
197 play_on_main(job);
198 else if (!emscripten_proxy_async(emscripten_proxy_get_system_queue(),
199 emscripten_main_runtime_thread_id(), play_on_main, job))
200 {
201 free(job->data);
202 free(job);
203 return 0;
204 }
205 /* latency reported to the server: what is already queued in the browser */
206 return (UINT)p->queued_ms;
207}
208
209static void drain_on_main(WINPR_ATTR_UNUSED void* arg)
210{
211}
212
213static void rdpsnd_emscripten_free(rdpsndDevicePlugin* device)
214{
215 /* Jobs queued by Play() still use the plugin. The main thread runs its queue in order, so
216 * once a synchronous no-op went through, all of them are done. */
217 if (emscripten_is_main_browser_thread())
218 emscripten_proxy_execute_queue(emscripten_proxy_get_system_queue());
219 else
220 (void)emscripten_proxy_sync(emscripten_proxy_get_system_queue(),
221 emscripten_main_runtime_thread_id(), drain_on_main, nullptr);
222 free(device);
223}
224
225FREERDP_ENTRY_POINT(UINT VCAPITYPE emscripten_freerdp_rdpsnd_client_subsystem_entry(
226 PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints))
227{
228 WINPR_ASSERT(pEntryPoints);
229
230 rdpsndEmscriptenPlugin* p = calloc(1, sizeof(rdpsndEmscriptenPlugin));
231 if (!p)
232 return CHANNEL_RC_NO_MEMORY;
233
234 p->volume = UINT32_MAX;
235 web_audio_format_set(WAVE_FORMAT_UNKNOWN, 0, 0); /* a new session: no audio yet */
236 p->device.Open = rdpsnd_emscripten_open;
237 p->device.FormatSupported = rdpsnd_emscripten_format_supported;
238 p->device.GetVolume = rdpsnd_emscripten_get_volume;
239 p->device.SetVolume = rdpsnd_emscripten_set_volume;
240 p->device.Play = rdpsnd_emscripten_play;
241 p->device.Close = rdpsnd_emscripten_close;
242 p->device.Free = rdpsnd_emscripten_free;
243 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, &p->device);
244 return CHANNEL_RC_OK;
245}