FreeRDP
Loading...
Searching...
No Matches
audin_sdl.c
1
20#include <freerdp/config.h>
21
22#include <stdlib.h>
23#include <string.h>
24
25#include <winpr/assert.h>
26#include <winpr/cast.h>
27#include <winpr/crt.h>
28#include <winpr/cmdline.h>
29
30#include <freerdp/freerdp.h>
31#include <freerdp/addin.h>
32#include <freerdp/channels/rdpsnd.h>
33#include <freerdp/client/audin.h>
34
35#include <SDL3/SDL.h>
36
37#include "audin_main.h"
38
39#define AUDIN_SDL_MIN_RATE 8000
40#define AUDIN_SDL_MAX_RATE 192000
41
42typedef struct
43{
44 IAudinDevice iface;
45
46 char* device_name;
47 SDL_AudioStream* stream;
48 AUDIO_FORMAT format;
49 size_t packet_bytes;
50 BYTE* packet;
51 BOOL failed;
52
53 AudinReceive receive;
54 void* user_data;
55 rdpContext* rdpcontext;
56 wLog* log;
57} AudinSdlDevice;
58
59/* Shared by the SDL audio channels: SDL_InitSubSystem is not thread safe. */
60#define AUDIN_SDL_LOCK_PROPERTY "FreeRDP.SDL.audio.lock"
61
62static void SDLCALL audin_sdl_destroy_lock(WINPR_ATTR_UNUSED void* userdata, void* value)
63{
64 SDL_DestroyMutex(value);
65}
66
67WINPR_ATTR_NODISCARD
68static SDL_Mutex* audin_sdl_audio_lock(void)
69{
70 const SDL_PropertiesID props = SDL_GetGlobalProperties();
71 if (!props || !SDL_LockProperties(props))
72 return nullptr;
73
74 SDL_Mutex* lock = SDL_GetPointerProperty(props, AUDIN_SDL_LOCK_PROPERTY, nullptr);
75 if (!lock)
76 {
77 lock = SDL_CreateMutex();
78 /* On failure the cleanup callback already destroyed the mutex. */
79 if (lock && !SDL_SetPointerPropertyWithCleanup(props, AUDIN_SDL_LOCK_PROPERTY, lock,
80 audin_sdl_destroy_lock, nullptr))
81 lock = nullptr;
82 }
83 SDL_UnlockProperties(props);
84 return lock;
85}
86
87WINPR_ATTR_NODISCARD
88static BOOL audin_sdl_init_audio(void)
89{
90 SDL_Mutex* lock = audin_sdl_audio_lock();
91 if (!lock)
92 return FALSE;
93
94 SDL_LockMutex(lock);
95 const BOOL rc = SDL_InitSubSystem(SDL_INIT_AUDIO);
96 SDL_UnlockMutex(lock);
97 return rc;
98}
99
100static void audin_sdl_quit_audio(wLog* log)
101{
102 SDL_Mutex* lock = audin_sdl_audio_lock();
103 if (!lock)
104 WLog_Print(log, WLOG_WARN, "quitting SDL audio without the lock: %s", SDL_GetError());
105
106 SDL_LockMutex(lock);
107 SDL_QuitSubSystem(SDL_INIT_AUDIO);
108 SDL_UnlockMutex(lock);
109}
110
111WINPR_ATTR_NODISCARD
112static BOOL audin_sdl_pcm_valid(const AUDIO_FORMAT* format)
113{
114 WINPR_ASSERT(format);
115
116 return (format->wFormatTag == WAVE_FORMAT_PCM) && (format->wBitsPerSample == 16) &&
117 (format->nSamplesPerSec >= AUDIN_SDL_MIN_RATE) &&
118 (format->nSamplesPerSec <= AUDIN_SDL_MAX_RATE) &&
119 ((format->nChannels == 1) || (format->nChannels == 2));
120}
121
122WINPR_ATTR_NODISCARD
123static SDL_AudioDeviceID audin_sdl_find_device(AudinSdlDevice* sdl)
124{
125 WINPR_ASSERT(sdl);
126
127 SDL_AudioDeviceID id = SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
128 if (!sdl->device_name)
129 return id;
130
131 int count = 0;
132 SDL_AudioDeviceID* devices = SDL_GetAudioRecordingDevices(&count);
133 if (!devices)
134 {
135 WLog_Print(sdl->log, WLOG_WARN, "SDL_GetAudioRecordingDevices: %s", SDL_GetError());
136 return id;
137 }
138
139 BOOL found = FALSE;
140 for (int x = 0; x < count; x++)
141 {
142 const char* cur = SDL_GetAudioDeviceName(devices[x]);
143 if (cur && (strcmp(cur, sdl->device_name) == 0))
144 {
145 id = devices[x];
146 found = TRUE;
147 break;
148 }
149 }
150 SDL_free(devices);
151
152 if (!found)
153 WLog_Print(sdl->log, WLOG_WARN, "recording device '%s' not found, using the default device",
154 sdl->device_name);
155 return id;
156}
157
158/* Runs on the SDL audio thread. */
159static void SDLCALL audin_sdl_recorded(void* userdata, SDL_AudioStream* stream,
160 WINPR_ATTR_UNUSED int additional_amount,
161 WINPR_ATTR_UNUSED int total_amount)
162{
163 AudinSdlDevice* sdl = userdata;
164 WINPR_ASSERT(sdl);
165 WINPR_ASSERT(sdl->receive);
166 WINPR_ASSERT(sdl->packet);
167
168 const int len = WINPR_ASSERTING_INT_CAST(int, sdl->packet_bytes);
169 while (!sdl->failed && (SDL_GetAudioStreamAvailable(stream) >= len))
170 {
171 const int got = SDL_GetAudioStreamData(stream, sdl->packet, len);
172 if (got != len)
173 {
174 if (got < 0)
175 WLog_Print(sdl->log, WLOG_WARN, "SDL_GetAudioStreamData: %s", SDL_GetError());
176 break;
177 }
178
179 const UINT error =
180 sdl->receive(&sdl->format, sdl->packet, sdl->packet_bytes, sdl->user_data);
181 if (error != CHANNEL_RC_OK)
182 {
183 sdl->failed = TRUE;
184 if (sdl->rdpcontext)
185 setChannelError(sdl->rdpcontext, error, "audin_sdl receive reported an error");
186 }
187 }
188
189 /* Drop input after a receive error. */
190 if (sdl->failed && !SDL_ClearAudioStream(stream))
191 WLog_Print(sdl->log, WLOG_WARN, "SDL_ClearAudioStream: %s", SDL_GetError());
192}
193
194WINPR_ATTR_NODISCARD
195static BOOL audin_sdl_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
196{
197 WINPR_ASSERT(device);
198 WINPR_ASSERT(format);
199
200 return audin_sdl_pcm_valid(format);
201}
202
203static void audin_sdl_stop(AudinSdlDevice* sdl)
204{
205 WINPR_ASSERT(sdl);
206
207 /* Waits for a running callback. */
208 SDL_DestroyAudioStream(sdl->stream);
209 sdl->stream = nullptr;
210 sdl->receive = nullptr;
211 sdl->user_data = nullptr;
212}
213
214WINPR_ATTR_NODISCARD
215static UINT audin_sdl_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
216 UINT32 FramesPerPacket)
217{
218 AudinSdlDevice* sdl = (AudinSdlDevice*)device;
219 WINPR_ASSERT(sdl);
220 WINPR_ASSERT(format);
221
222 if (!audin_sdl_pcm_valid(format))
223 return ERROR_UNSUPPORTED_TYPE;
224
225 /* Server provided, keep it between 10 ms and 1 s. */
226 const size_t bytesPerFrame = 2ULL * format->nChannels;
227 const size_t frames =
228 MAX(MIN(FramesPerPacket, format->nSamplesPerSec), format->nSamplesPerSec / 100);
229
230 /* The callback uses the packet buffer. */
231 audin_sdl_stop(sdl);
232
233 BYTE* packet = realloc(sdl->packet, frames * bytesPerFrame);
234 if (!packet)
235 return CHANNEL_RC_NO_MEMORY;
236
237 sdl->packet = packet;
238 sdl->packet_bytes = frames * bytesPerFrame;
239 sdl->format = *format;
240 return CHANNEL_RC_OK;
241}
242
243WINPR_ATTR_NODISCARD
244static UINT audin_sdl_close(IAudinDevice* device)
245{
246 AudinSdlDevice* sdl = (AudinSdlDevice*)device;
247 WINPR_ASSERT(sdl);
248
249 audin_sdl_stop(sdl);
250 return CHANNEL_RC_OK;
251}
252
253/* One packet per device period: with a longer period (1024 frames on PipeWire) the packets
254 * leave in bursts, and Windows loses some of the AAC encoded audio. */
255WINPR_ATTR_NODISCARD
256static SDL_AudioStream* audin_sdl_open_stream(AudinSdlDevice* sdl, const SDL_AudioSpec* spec)
257{
258 WINPR_ASSERT(sdl);
259 WINPR_ASSERT(spec);
260
261 const char* prev = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
262 char* saved = prev ? _strdup(prev) : nullptr;
263
264 char frames[32] = WINPR_C_ARRAY_INIT;
265 const size_t packetFrames = sdl->packet_bytes / (2ULL * sdl->format.nChannels);
266 if ((_snprintf(frames, sizeof(frames), "%" PRIuz, packetFrames) < 0) ||
267 !SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, frames))
268 WLog_Print(sdl->log, WLOG_DEBUG, "device period not set: %s", SDL_GetError());
269
270 SDL_AudioStream* stream =
271 SDL_OpenAudioDeviceStream(audin_sdl_find_device(sdl), spec, audin_sdl_recorded, sdl);
272
273 const bool restored = saved ? SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, saved)
274 : SDL_ResetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
275 if (!restored)
276 WLog_Print(sdl->log, WLOG_DEBUG, "device period hint not restored");
277 free(saved);
278 return stream;
279}
280
281WINPR_ATTR_NODISCARD
282static UINT audin_sdl_open(IAudinDevice* device, AudinReceive receive, void* user_data)
283{
284 AudinSdlDevice* sdl = (AudinSdlDevice*)device;
285 WINPR_ASSERT(sdl);
286
287 if (!receive || !user_data)
288 return ERROR_INVALID_PARAMETER;
289
290 if (sdl->stream)
291 return CHANNEL_RC_OK;
292
293 if (!sdl->packet)
294 {
295 WLog_Print(sdl->log, WLOG_ERROR, "SetFormat must run with a valid PCM format first");
296 return ERROR_INVALID_PARAMETER;
297 }
298
299 const SDL_AudioSpec spec = {
300 .format = SDL_AUDIO_S16LE,
301 .channels = sdl->format.nChannels,
302 .freq = WINPR_ASSERTING_INT_CAST(int, sdl->format.nSamplesPerSec),
303 };
304
305 sdl->receive = receive;
306 sdl->user_data = user_data;
307 sdl->failed = FALSE;
308
309 sdl->stream = audin_sdl_open_stream(sdl, &spec);
310 if (!sdl->stream)
311 {
312 WLog_Print(sdl->log, WLOG_ERROR, "SDL_OpenAudioDeviceStream: %s", SDL_GetError());
313 goto fail;
314 }
315
316 if (!SDL_ResumeAudioStreamDevice(sdl->stream))
317 {
318 WLog_Print(sdl->log, WLOG_ERROR, "SDL_ResumeAudioStreamDevice: %s", SDL_GetError());
319 goto fail;
320 }
321
322 return CHANNEL_RC_OK;
323
324fail:
325 audin_sdl_stop(sdl);
326 return ERROR_INTERNAL_ERROR;
327}
328
329WINPR_ATTR_NODISCARD
330static UINT audin_sdl_free(IAudinDevice* device)
331{
332 AudinSdlDevice* sdl = (AudinSdlDevice*)device;
333 if (!sdl)
334 return ERROR_INVALID_PARAMETER;
335
336 audin_sdl_stop(sdl);
337 audin_sdl_quit_audio(sdl->log);
338 free(sdl->packet);
339 free(sdl->device_name);
340 free(sdl);
341 return CHANNEL_RC_OK;
342}
343
344WINPR_ATTR_NODISCARD
345static UINT audin_sdl_parse_addin_args(AudinSdlDevice* sdl, const ADDIN_ARGV* args)
346{
347 COMMAND_LINE_ARGUMENT_A audin_sdl_args[] = {
348 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr,
349 "audio device name" },
350 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
351 };
352 const DWORD flags =
353 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
354
355 WINPR_ASSERT(sdl);
356 WINPR_ASSERT(args);
357
358 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_sdl_args, flags,
359 sdl, nullptr, nullptr);
360 if (status < 0)
361 return ERROR_INVALID_DATA;
362
363 const COMMAND_LINE_ARGUMENT_A* arg = audin_sdl_args;
364 do
365 {
366 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
367 continue;
368
369 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
370 {
371 free(sdl->device_name);
372 sdl->device_name = _strdup(arg->Value);
373 if (!sdl->device_name)
374 return ERROR_OUTOFMEMORY;
375 }
376 CommandLineSwitchEnd(arg)
377 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
378
379 return CHANNEL_RC_OK;
380}
381
382FREERDP_ENTRY_POINT(UINT VCAPITYPE sdl_freerdp_audin_client_subsystem_entry(
384{
385 WINPR_ASSERT(pEntryPoints);
386 WINPR_ASSERT(pEntryPoints->pRegisterAudinDevice);
387
388 AudinSdlDevice* sdl = calloc(1, sizeof(AudinSdlDevice));
389 if (!sdl)
390 return CHANNEL_RC_NO_MEMORY;
391
392 sdl->log = WLog_Get(TAG);
393 if (!audin_sdl_init_audio())
394 {
395 WLog_Print(sdl->log, WLOG_ERROR, "SDL_InitSubSystem(SDL_INIT_AUDIO): %s", SDL_GetError());
396 free(sdl);
397 return ERROR_INTERNAL_ERROR;
398 }
399
400 sdl->iface.Open = audin_sdl_open;
401 sdl->iface.FormatSupported = audin_sdl_format_supported;
402 sdl->iface.SetFormat = audin_sdl_set_format;
403 sdl->iface.Close = audin_sdl_close;
404 sdl->iface.Free = audin_sdl_free;
405 sdl->rdpcontext = pEntryPoints->rdpcontext;
406
407 UINT error = CHANNEL_RC_OK;
408 if (pEntryPoints->args)
409 {
410 error = audin_sdl_parse_addin_args(sdl, pEntryPoints->args);
411 if (error != CHANNEL_RC_OK)
412 WLog_Print(sdl->log, WLOG_ERROR, "error parsing arguments");
413 }
414
415 if (error == CHANNEL_RC_OK)
416 {
417 error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &sdl->iface);
418 if (error != CHANNEL_RC_OK)
419 WLog_Print(sdl->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32,
420 error);
421 }
422
423 wLog* log = sdl->log;
424 if ((error != CHANNEL_RC_OK) && (audin_sdl_free(&sdl->iface) != CHANNEL_RC_OK))
425 WLog_Print(log, WLOG_WARN, "audin_sdl_free failed");
426 return error;
427}