28#include <freerdp/config.h>
34#include <emscripten.h>
35#include <emscripten/proxying.h>
36#include <emscripten/threading.h>
38#include <winpr/assert.h>
40#include <winpr/stream.h>
42#include <freerdp/types.h>
43#include <freerdp/codec/audio.h>
45#include "rdpsnd_main.h"
49 rdpsndDevicePlugin device;
53 volatile int queued_ms;
55} rdpsndEmscriptenPlugin;
67static uint32_t web_audio_format[3];
69EMSCRIPTEN_KEEPALIVE uint32_t* freerdp_web_audio_format(
void);
70EMSCRIPTEN_KEEPALIVE uint32_t* freerdp_web_audio_format(
void)
72 return web_audio_format;
75static void web_audio_format_set(UINT16 tag, UINT32 rate, UINT16 channels)
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);
84 rdpsndEmscriptenPlugin* plugin;
92extern void rdpsnd_web_play(BYTE* data,
int size,
int channels,
int rate,
int volume,
95static void play_on_main(
void* arg)
100 rdpsndEmscriptenPlugin* p = job->plugin;
103 rdpsnd_web_play(job->data, (
int)job->size, (
int)job->channels, (
int)job->rate,
104 (
int)(p->volume & 0xffff), (
int*)&p->queued_ms);
112 WINPR_ASSERT(format);
115 return (format->wFormatTag == WAVE_FORMAT_PCM) && (format->wBitsPerSample == 16) &&
116 (format->nChannels >= 1) && (format->nChannels <= 2) && (format->nSamplesPerSec > 0);
120static BOOL rdpsnd_emscripten_format_supported(rdpsndDevicePlugin* device,
123 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
125 WINPR_ASSERT(format);
128 p->asked.data =
nullptr;
129 return pcm_playable(format);
132static BOOL rdpsnd_emscripten_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
133 WINPR_ATTR_UNUSED UINT32 latency)
135 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
138 if (!format || !pcm_playable(format))
140 p->channels = format->nChannels;
141 p->rate = format->nSamplesPerSec;
144 if ((p->asked.wFormatTag != WAVE_FORMAT_UNKNOWN) &&
145 (p->asked.nSamplesPerSec == format->nSamplesPerSec) &&
146 (p->asked.nChannels == format->nChannels))
148 web_audio_format_set(wire->wFormatTag, wire->nSamplesPerSec, wire->nChannels);
152static void rdpsnd_emscripten_close(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device)
156static BOOL rdpsnd_emscripten_set_volume(rdpsndDevicePlugin* device, UINT32 value)
158 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
165static UINT32 rdpsnd_emscripten_get_volume(rdpsndDevicePlugin* device)
167 const rdpsndEmscriptenPlugin* p = (
const rdpsndEmscriptenPlugin*)device;
173static UINT rdpsnd_emscripten_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
175 rdpsndEmscriptenPlugin* p = (rdpsndEmscriptenPlugin*)device;
178 if (!data || size == 0 || p->channels == 0)
181 play_job* job = calloc(1,
sizeof(play_job));
184 job->data = malloc(size);
190 memcpy(job->data, data, size);
193 job->channels = p->channels;
196 if (emscripten_is_main_browser_thread())
198 else if (!emscripten_proxy_async(emscripten_proxy_get_system_queue(),
199 emscripten_main_runtime_thread_id(), play_on_main, job))
206 return (UINT)p->queued_ms;
209static void drain_on_main(WINPR_ATTR_UNUSED
void* arg)
213static void rdpsnd_emscripten_free(rdpsndDevicePlugin* device)
217 if (emscripten_is_main_browser_thread())
218 emscripten_proxy_execute_queue(emscripten_proxy_get_system_queue());
220 (
void)emscripten_proxy_sync(emscripten_proxy_get_system_queue(),
221 emscripten_main_runtime_thread_id(), drain_on_main,
nullptr);
225FREERDP_ENTRY_POINT(UINT VCAPITYPE emscripten_freerdp_rdpsnd_client_subsystem_entry(
226 PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints))
228 WINPR_ASSERT(pEntryPoints);
230 rdpsndEmscriptenPlugin* p = calloc(1,
sizeof(rdpsndEmscriptenPlugin));
232 return CHANNEL_RC_NO_MEMORY;
234 p->volume = UINT32_MAX;
235 web_audio_format_set(WAVE_FORMAT_UNKNOWN, 0, 0);
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;