35#include <freerdp/config.h>
41#include <emscripten.h>
42#include <emscripten/proxying.h>
43#include <emscripten/threading.h>
46#include <winpr/synch.h>
47#include <winpr/thread.h>
48#include <winpr/wlog.h>
50#include <freerdp/types.h>
51#include <freerdp/addin.h>
52#include <freerdp/freerdp.h>
53#include <freerdp/codec/audio.h>
54#include <freerdp/client/audin.h>
56#include "audin_main.h"
59#define AUDIN_WEB_RING_SAMPLES (1u << 17)
63 volatile int32_t active;
64 volatile uint32_t write_pos;
65 int16_t ring[AUDIN_WEB_RING_SAMPLES];
73 UINT32 frames_per_packet;
78 audin_web_capture* capture;
82 volatile int32_t stop;
84 rdpContext* rdpcontext;
86} AudinEmscriptenDevice;
91 audin_web_capture* capture;
96void audin_emscripten_release(
void* capture);
99EMSCRIPTEN_KEEPALIVE
void audin_emscripten_release(
void* capture)
105extern void audin_web_start(
int id, audin_web_capture* capture,
int samples,
int channels,
107extern void audin_web_stop(
int id, audin_web_capture* capture);
109static void start_on_main(
void* arg)
111 audin_web_job* job = arg;
112 audin_web_start(job->id, job->capture, (
int)AUDIN_WEB_RING_SAMPLES, job->channels, job->rate);
116static void stop_on_main(
void* arg)
118 audin_web_job* job = arg;
119 audin_web_stop(job->id, job->capture);
123static BOOL run_on_main(
void (*fn)(
void*), audin_web_job* job)
125 if (emscripten_is_main_browser_thread())
130 return emscripten_proxy_async(emscripten_proxy_get_system_queue(),
131 emscripten_main_runtime_thread_id(), fn, job);
134static DWORD WINAPI audin_emscripten_thread(LPVOID arg)
136 AudinEmscriptenDevice* dev = arg;
137 audin_web_capture* capture = dev->capture;
138 const uint32_t packet = dev->frames_per_packet * dev->format.nChannels;
139 const size_t bytes = packet *
sizeof(int16_t);
140 uint32_t read_pos = 0;
142 while (!__atomic_load_n(&dev->stop, __ATOMIC_ACQUIRE))
144 if (!__atomic_load_n(&capture->active, __ATOMIC_ACQUIRE))
146 if (!__atomic_load_n(&dev->stop, __ATOMIC_ACQUIRE))
147 WLog_Print(dev->log, WLOG_ERROR,
"microphone capture failed, sending no audio");
151 const uint32_t written = __atomic_load_n(&capture->write_pos, __ATOMIC_ACQUIRE);
152 const uint32_t available = written - read_pos;
154 if (available > AUDIN_WEB_RING_SAMPLES - packet)
157 WLog_Print(dev->log, WLOG_WARN,
158 "microphone capture overrun, dropping %" PRIu32
" samples",
159 available - AUDIN_WEB_RING_SAMPLES / 2);
160 read_pos = written - AUDIN_WEB_RING_SAMPLES / 2;
164 if (available < packet)
166 (void)emscripten_futex_wait(&capture->write_pos, written, 100.0);
170 for (uint32_t x = 0; x < packet; x++)
171 dev->packet[x] = capture->ring[(read_pos + x) & (AUDIN_WEB_RING_SAMPLES - 1)];
175 dev->receive(&dev->format, (
const BYTE*)dev->packet, bytes, dev->user_data);
179 setChannelError(dev->rdpcontext, error,
180 "audin_emscripten_thread reported an error");
189static BOOL audin_emscripten_format_supported(WINPR_ATTR_UNUSED IAudinDevice* device,
195 return (format->wFormatTag == WAVE_FORMAT_PCM) && (format->wBitsPerSample == 16) &&
196 (format->nChannels >= 1) && (format->nChannels <= 2) &&
197 (format->nSamplesPerSec >= 8000) && (format->nSamplesPerSec <= 192000);
200static UINT audin_emscripten_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
201 UINT32 FramesPerPacket)
203 AudinEmscriptenDevice* dev = (AudinEmscriptenDevice*)device;
205 if (!dev || !audin_emscripten_format_supported(device, format))
206 return ERROR_INVALID_PARAMETER;
208 dev->format = *format;
209 dev->format.cbSize = 0;
210 dev->format.data =
nullptr;
213 UINT32 frames = FramesPerPacket ? FramesPerPacket : format->nSamplesPerSec / 50;
214 const UINT32 max_frames = AUDIN_WEB_RING_SAMPLES / 4 / format->nChannels;
215 if (frames > max_frames)
217 dev->frames_per_packet = frames;
218 return CHANNEL_RC_OK;
221static UINT audin_emscripten_close(IAudinDevice* device)
223 AudinEmscriptenDevice* dev = (AudinEmscriptenDevice*)device;
226 return ERROR_INVALID_PARAMETER;
228 return CHANNEL_RC_OK;
230 __atomic_store_n(&dev->stop, 1, __ATOMIC_SEQ_CST);
231 __atomic_store_n(&dev->capture->active, 0, __ATOMIC_SEQ_CST);
232 (void)emscripten_futex_wake(&dev->capture->write_pos, INT32_MAX);
236 (void)WaitForSingleObject(dev->thread, INFINITE);
237 (void)CloseHandle(dev->thread);
238 dev->thread =
nullptr;
242 audin_web_job* job = calloc(1,
sizeof(audin_web_job));
245 job->id = dev->capture_id;
246 job->capture = dev->capture;
248 if (!job || !run_on_main(stop_on_main, job))
250 WLog_Print(dev->log, WLOG_ERROR,
"could not stop the microphone capture, leaking it");
254 dev->capture =
nullptr;
256 dev->packet =
nullptr;
257 dev->receive =
nullptr;
258 dev->user_data =
nullptr;
259 return CHANNEL_RC_OK;
262static UINT audin_emscripten_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
264 static int next_id = 0;
265 AudinEmscriptenDevice* dev = (AudinEmscriptenDevice*)device;
267 if (!dev || !receive || (dev->frames_per_packet == 0))
268 return ERROR_INVALID_PARAMETER;
270 UINT error = audin_emscripten_close(device);
274 dev->packet = calloc(1ull * dev->frames_per_packet * dev->format.nChannels,
sizeof(int16_t));
275 dev->capture = calloc(1,
sizeof(audin_web_capture));
276 audin_web_job* job = calloc(1,
sizeof(audin_web_job));
277 if (!dev->packet || !dev->capture || !job)
280 error = CHANNEL_RC_NO_MEMORY;
284 dev->capture->active = 1;
285 dev->capture_id = __atomic_add_fetch(&next_id, 1, __ATOMIC_RELAXED);
286 dev->receive = receive;
287 dev->user_data = user_data;
288 __atomic_store_n(&dev->stop, 0, __ATOMIC_RELEASE);
290 dev->thread = CreateThread(
nullptr, 0, audin_emscripten_thread, dev, 0,
nullptr);
294 error = ERROR_INTERNAL_ERROR;
298 job->id = dev->capture_id;
299 job->capture = dev->capture;
300 job->channels = dev->format.nChannels;
301 job->rate = (int)dev->format.nSamplesPerSec;
302 if (!run_on_main(start_on_main, job))
305 (void)audin_emscripten_close(device);
306 return ERROR_INTERNAL_ERROR;
308 return CHANNEL_RC_OK;
312 dev->packet =
nullptr;
314 dev->capture =
nullptr;
318static UINT audin_emscripten_free(IAudinDevice* device)
320 AudinEmscriptenDevice* dev = (AudinEmscriptenDevice*)device;
323 return ERROR_INVALID_PARAMETER;
325 const UINT error = audin_emscripten_close(device);
330FREERDP_ENTRY_POINT(UINT VCAPITYPE emscripten_freerdp_audin_client_subsystem_entry(
333 AudinEmscriptenDevice* dev = calloc(1,
sizeof(AudinEmscriptenDevice));
335 return CHANNEL_RC_NO_MEMORY;
337 dev->log = WLog_Get(TAG);
338 dev->rdpcontext = pEntryPoints->rdpcontext;
339 dev->iface.Open = audin_emscripten_open;
340 dev->iface.FormatSupported = audin_emscripten_format_supported;
341 dev->iface.SetFormat = audin_emscripten_set_format;
342 dev->iface.Close = audin_emscripten_close;
343 dev->iface.Free = audin_emscripten_free;
345 const UINT error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &dev->iface);
348 WLog_Print(dev->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",