20#include <freerdp/config.h>
25#include <winpr/assert.h>
26#include <winpr/cast.h>
28#include <winpr/cmdline.h>
30#include <freerdp/freerdp.h>
31#include <freerdp/addin.h>
32#include <freerdp/channels/rdpsnd.h>
33#include <freerdp/client/audin.h>
37#include "audin_main.h"
39#define AUDIN_SDL_MIN_RATE 8000
40#define AUDIN_SDL_MAX_RATE 192000
47 SDL_AudioStream* stream;
55 rdpContext* rdpcontext;
60#define AUDIN_SDL_LOCK_PROPERTY "FreeRDP.SDL.audio.lock"
62static void SDLCALL audin_sdl_destroy_lock(WINPR_ATTR_UNUSED
void* userdata,
void* value)
64 SDL_DestroyMutex(value);
68static SDL_Mutex* audin_sdl_audio_lock(
void)
70 const SDL_PropertiesID props = SDL_GetGlobalProperties();
71 if (!props || !SDL_LockProperties(props))
74 SDL_Mutex* lock = SDL_GetPointerProperty(props, AUDIN_SDL_LOCK_PROPERTY,
nullptr);
77 lock = SDL_CreateMutex();
79 if (lock && !SDL_SetPointerPropertyWithCleanup(props, AUDIN_SDL_LOCK_PROPERTY, lock,
80 audin_sdl_destroy_lock,
nullptr))
83 SDL_UnlockProperties(props);
88static BOOL audin_sdl_init_audio(
void)
90 SDL_Mutex* lock = audin_sdl_audio_lock();
95 const BOOL rc = SDL_InitSubSystem(SDL_INIT_AUDIO);
96 SDL_UnlockMutex(lock);
100static void audin_sdl_quit_audio(wLog* log)
102 SDL_Mutex* lock = audin_sdl_audio_lock();
104 WLog_Print(log, WLOG_WARN,
"quitting SDL audio without the lock: %s", SDL_GetError());
107 SDL_QuitSubSystem(SDL_INIT_AUDIO);
108 SDL_UnlockMutex(lock);
112static BOOL audin_sdl_pcm_valid(
const AUDIO_FORMAT* format)
114 WINPR_ASSERT(format);
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));
123static SDL_AudioDeviceID audin_sdl_find_device(AudinSdlDevice*
sdl)
127 SDL_AudioDeviceID
id = SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
128 if (!
sdl->device_name)
132 SDL_AudioDeviceID* devices = SDL_GetAudioRecordingDevices(&count);
135 WLog_Print(
sdl->log, WLOG_WARN,
"SDL_GetAudioRecordingDevices: %s", SDL_GetError());
140 for (
int x = 0; x < count; x++)
142 const char* cur = SDL_GetAudioDeviceName(devices[x]);
143 if (cur && (strcmp(cur,
sdl->device_name) == 0))
153 WLog_Print(
sdl->log, WLOG_WARN,
"recording device '%s' not found, using the default device",
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)
163 AudinSdlDevice*
sdl = userdata;
165 WINPR_ASSERT(
sdl->receive);
166 WINPR_ASSERT(
sdl->packet);
168 const int len = WINPR_ASSERTING_INT_CAST(
int,
sdl->packet_bytes);
169 while (!
sdl->failed && (SDL_GetAudioStreamAvailable(stream) >= len))
171 const int got = SDL_GetAudioStreamData(stream,
sdl->packet, len);
175 WLog_Print(
sdl->log, WLOG_WARN,
"SDL_GetAudioStreamData: %s", SDL_GetError());
180 sdl->receive(&
sdl->format,
sdl->packet,
sdl->packet_bytes,
sdl->user_data);
181 if (error != CHANNEL_RC_OK)
185 setChannelError(
sdl->rdpcontext, error,
"audin_sdl receive reported an error");
190 if (
sdl->failed && !SDL_ClearAudioStream(stream))
191 WLog_Print(
sdl->log, WLOG_WARN,
"SDL_ClearAudioStream: %s", SDL_GetError());
195static BOOL audin_sdl_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
197 WINPR_ASSERT(device);
198 WINPR_ASSERT(format);
200 return audin_sdl_pcm_valid(format);
203static void audin_sdl_stop(AudinSdlDevice*
sdl)
208 SDL_DestroyAudioStream(
sdl->stream);
209 sdl->stream =
nullptr;
210 sdl->receive =
nullptr;
211 sdl->user_data =
nullptr;
215static UINT audin_sdl_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
216 UINT32 FramesPerPacket)
218 AudinSdlDevice*
sdl = (AudinSdlDevice*)device;
220 WINPR_ASSERT(format);
222 if (!audin_sdl_pcm_valid(format))
223 return ERROR_UNSUPPORTED_TYPE;
226 const size_t bytesPerFrame = 2ULL * format->nChannels;
227 const size_t frames =
228 MAX(MIN(FramesPerPacket, format->nSamplesPerSec), format->nSamplesPerSec / 100);
233 BYTE* packet = realloc(
sdl->packet, frames * bytesPerFrame);
235 return CHANNEL_RC_NO_MEMORY;
237 sdl->packet = packet;
238 sdl->packet_bytes = frames * bytesPerFrame;
239 sdl->format = *format;
240 return CHANNEL_RC_OK;
244static UINT audin_sdl_close(IAudinDevice* device)
246 AudinSdlDevice*
sdl = (AudinSdlDevice*)device;
250 return CHANNEL_RC_OK;
256static SDL_AudioStream* audin_sdl_open_stream(AudinSdlDevice*
sdl,
const SDL_AudioSpec* spec)
261 const char* prev = SDL_GetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
262 char* saved = prev ? _strdup(prev) : nullptr;
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());
270 SDL_AudioStream* stream =
271 SDL_OpenAudioDeviceStream(audin_sdl_find_device(
sdl), spec, audin_sdl_recorded,
sdl);
273 const bool restored = saved ? SDL_SetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, saved)
274 : SDL_ResetHint(SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
276 WLog_Print(
sdl->log, WLOG_DEBUG,
"device period hint not restored");
282static UINT audin_sdl_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
284 AudinSdlDevice*
sdl = (AudinSdlDevice*)device;
287 if (!receive || !user_data)
288 return ERROR_INVALID_PARAMETER;
291 return CHANNEL_RC_OK;
295 WLog_Print(
sdl->log, WLOG_ERROR,
"SetFormat must run with a valid PCM format first");
296 return ERROR_INVALID_PARAMETER;
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),
305 sdl->receive = receive;
306 sdl->user_data = user_data;
309 sdl->stream = audin_sdl_open_stream(
sdl, &spec);
312 WLog_Print(
sdl->log, WLOG_ERROR,
"SDL_OpenAudioDeviceStream: %s", SDL_GetError());
316 if (!SDL_ResumeAudioStreamDevice(
sdl->stream))
318 WLog_Print(
sdl->log, WLOG_ERROR,
"SDL_ResumeAudioStreamDevice: %s", SDL_GetError());
322 return CHANNEL_RC_OK;
326 return ERROR_INTERNAL_ERROR;
330static UINT audin_sdl_free(IAudinDevice* device)
332 AudinSdlDevice*
sdl = (AudinSdlDevice*)device;
334 return ERROR_INVALID_PARAMETER;
337 audin_sdl_quit_audio(
sdl->log);
339 free(
sdl->device_name);
341 return CHANNEL_RC_OK;
345static UINT audin_sdl_parse_addin_args(AudinSdlDevice*
sdl,
const ADDIN_ARGV* 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 }
353 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
358 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_sdl_args, flags,
359 sdl,
nullptr,
nullptr);
361 return ERROR_INVALID_DATA;
366 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
369 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
371 free(
sdl->device_name);
372 sdl->device_name = _strdup(arg->Value);
373 if (!
sdl->device_name)
374 return ERROR_OUTOFMEMORY;
376 CommandLineSwitchEnd(arg)
377 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
379 return CHANNEL_RC_OK;
382FREERDP_ENTRY_POINT(UINT VCAPITYPE sdl_freerdp_audin_client_subsystem_entry(
385 WINPR_ASSERT(pEntryPoints);
386 WINPR_ASSERT(pEntryPoints->pRegisterAudinDevice);
388 AudinSdlDevice*
sdl = calloc(1,
sizeof(AudinSdlDevice));
390 return CHANNEL_RC_NO_MEMORY;
392 sdl->log = WLog_Get(TAG);
393 if (!audin_sdl_init_audio())
395 WLog_Print(
sdl->log, WLOG_ERROR,
"SDL_InitSubSystem(SDL_INIT_AUDIO): %s", SDL_GetError());
397 return ERROR_INTERNAL_ERROR;
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;
407 UINT error = CHANNEL_RC_OK;
408 if (pEntryPoints->args)
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");
415 if (error == CHANNEL_RC_OK)
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,
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");