22 #include <freerdp/config.h>
28 #include <winpr/crt.h>
29 #include <winpr/synch.h>
30 #include <winpr/thread.h>
31 #include <winpr/cmdline.h>
32 #include <winpr/wlog.h>
34 #include <alsa/asoundlib.h>
36 #include <freerdp/freerdp.h>
37 #include <freerdp/addin.h>
38 #include <freerdp/channels/rdpsnd.h>
40 #include "audin_main.h"
47 UINT32 frames_per_packet;
56 rdpContext* rdpcontext;
58 size_t bytes_per_frame;
61 static snd_pcm_format_t audin_alsa_format(UINT32 wFormatTag, UINT32 bitPerChannel)
66 switch (bitPerChannel)
69 return SND_PCM_FORMAT_S16_LE;
72 return SND_PCM_FORMAT_S8;
75 return SND_PCM_FORMAT_UNKNOWN;
79 return SND_PCM_FORMAT_UNKNOWN;
83 static BOOL audin_alsa_set_params(AudinALSADevice* alsa, snd_pcm_t* capture_handle)
87 UINT32 channels = alsa->aformat.nChannels;
88 snd_pcm_hw_params_t* hw_params = NULL;
89 snd_pcm_format_t format =
90 audin_alsa_format(alsa->aformat.wFormatTag, alsa->aformat.wBitsPerSample);
92 if ((error = snd_pcm_hw_params_malloc(&hw_params)) < 0)
94 WLog_Print(alsa->log, WLOG_ERROR,
"snd_pcm_hw_params_malloc (%s)", snd_strerror(error));
98 snd_pcm_hw_params_any(capture_handle, hw_params);
99 snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
100 snd_pcm_hw_params_set_format(capture_handle, hw_params, format);
101 snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &alsa->aformat.nSamplesPerSec, NULL);
102 snd_pcm_hw_params_set_channels_near(capture_handle, hw_params, &channels);
103 snd_pcm_hw_params(capture_handle, hw_params);
104 snd_pcm_hw_params_free(hw_params);
105 snd_pcm_prepare(capture_handle);
106 if (channels > UINT16_MAX)
108 s = snd_pcm_format_size(format, 1);
109 if ((s < 0) || (s > UINT16_MAX))
111 alsa->aformat.nChannels = (UINT16)channels;
112 alsa->bytes_per_frame = (size_t)s * channels;
116 static DWORD WINAPI audin_alsa_thread_func(LPVOID arg)
118 DWORD error = CHANNEL_RC_OK;
120 AudinALSADevice* alsa = (AudinALSADevice*)arg;
124 WLog_Print(alsa->log, WLOG_DEBUG,
"in");
126 snd_pcm_t* capture_handle = NULL;
127 const int rc = snd_pcm_open(&capture_handle, alsa->device_name, SND_PCM_STREAM_CAPTURE, 0);
130 WLog_Print(alsa->log, WLOG_ERROR,
"snd_pcm_open (%s)", snd_strerror(rc));
131 error = CHANNEL_RC_INITIALIZATION_ERROR;
135 if (!audin_alsa_set_params(alsa, capture_handle))
137 WLog_Print(alsa->log, WLOG_ERROR,
"audin_alsa_set_params failed");
142 (BYTE*)calloc(alsa->frames_per_packet + alsa->aformat.nBlockAlign, alsa->bytes_per_frame);
146 WLog_Print(alsa->log, WLOG_ERROR,
"calloc failed!");
147 error = CHANNEL_RC_NO_MEMORY;
153 size_t frames = alsa->frames_per_packet;
154 const DWORD status = WaitForSingleObject(alsa->stopEvent, 0);
156 if (status == WAIT_FAILED)
158 error = GetLastError();
159 WLog_Print(alsa->log, WLOG_ERROR,
"WaitForSingleObject failed with error %" PRIu32
"!",
164 if (status == WAIT_OBJECT_0)
166 WLog_Print(alsa->log, WLOG_DEBUG,
"alsa->stopEvent requests termination");
170 snd_pcm_sframes_t framesRead = snd_pcm_readi(capture_handle, buffer, frames);
175 if (framesRead == -EPIPE)
177 const int res = snd_pcm_recover(capture_handle, (
int)framesRead, 0);
179 WLog_Print(alsa->log, WLOG_WARN,
"snd_pcm_recover (%s)", snd_strerror(res));
183 else if (framesRead < 0)
185 WLog_Print(alsa->log, WLOG_ERROR,
"snd_pcm_readi (%s)", snd_strerror((
int)framesRead));
186 error = ERROR_INTERNAL_ERROR;
190 error = alsa->receive(&alsa->aformat, buffer, (
long)framesRead * alsa->bytes_per_frame,
195 WLog_Print(alsa->log, WLOG_ERROR,
"audin_alsa_thread_receive failed with error %ld",
205 const int res = snd_pcm_close(capture_handle);
207 WLog_Print(alsa->log, WLOG_WARN,
"snd_pcm_close (%s)", snd_strerror(res));
211 WLog_Print(alsa->log, WLOG_DEBUG,
"out");
213 if (error && alsa->rdpcontext)
214 setChannelError(alsa->rdpcontext, error,
"audin_alsa_thread_func reported an error");
225 static UINT audin_alsa_free(IAudinDevice* device)
227 AudinALSADevice* alsa = (AudinALSADevice*)device;
230 free(alsa->device_name);
233 return CHANNEL_RC_OK;
236 static BOOL audin_alsa_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
238 if (!device || !format)
241 switch (format->wFormatTag)
243 case WAVE_FORMAT_PCM:
244 if (format->cbSize == 0 && (format->nSamplesPerSec <= 48000) &&
245 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
246 (format->nChannels == 1 || format->nChannels == 2))
265 static UINT audin_alsa_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
266 UINT32 FramesPerPacket)
268 AudinALSADevice* alsa = (AudinALSADevice*)device;
270 if (!alsa || !format)
271 return ERROR_INVALID_PARAMETER;
273 alsa->aformat = *format;
274 alsa->frames_per_packet = FramesPerPacket;
276 if (audin_alsa_format(format->wFormatTag, format->wBitsPerSample) == SND_PCM_FORMAT_UNKNOWN)
277 return ERROR_INTERNAL_ERROR;
279 return CHANNEL_RC_OK;
287 static UINT audin_alsa_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
289 AudinALSADevice* alsa = (AudinALSADevice*)device;
291 if (!device || !receive || !user_data)
292 return ERROR_INVALID_PARAMETER;
294 alsa->receive = receive;
295 alsa->user_data = user_data;
297 if (!(alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
299 WLog_Print(alsa->log, WLOG_ERROR,
"CreateEvent failed!");
303 if (!(alsa->thread = CreateThread(NULL, 0, audin_alsa_thread_func, alsa, 0, NULL)))
305 WLog_Print(alsa->log, WLOG_ERROR,
"CreateThread failed!");
309 return CHANNEL_RC_OK;
311 (void)CloseHandle(alsa->stopEvent);
312 alsa->stopEvent = NULL;
313 return ERROR_INTERNAL_ERROR;
321 static UINT audin_alsa_close(IAudinDevice* device)
323 UINT error = CHANNEL_RC_OK;
324 AudinALSADevice* alsa = (AudinALSADevice*)device;
327 return ERROR_INVALID_PARAMETER;
331 (void)SetEvent(alsa->stopEvent);
333 if (WaitForSingleObject(alsa->thread, INFINITE) == WAIT_FAILED)
335 error = GetLastError();
336 WLog_Print(alsa->log, WLOG_ERROR,
"WaitForSingleObject failed with error %" PRIu32
"",
341 (void)CloseHandle(alsa->stopEvent);
342 alsa->stopEvent = NULL;
343 (void)CloseHandle(alsa->thread);
347 alsa->receive = NULL;
348 alsa->user_data = NULL;
357 static UINT audin_alsa_parse_addin_args(AudinALSADevice* device,
const ADDIN_ARGV* args)
362 AudinALSADevice* alsa = device;
364 NULL, NULL, -1, NULL,
"audio device name" },
365 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
367 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
368 status = CommandLineParseArgumentsA(args->argc, args->argv, audin_alsa_args, flags, alsa, NULL,
372 return ERROR_INVALID_PARAMETER;
374 arg = audin_alsa_args;
378 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
381 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
383 alsa->device_name = _strdup(arg->Value);
385 if (!alsa->device_name)
387 WLog_Print(alsa->log, WLOG_ERROR,
"_strdup failed!");
388 return CHANNEL_RC_NO_MEMORY;
391 CommandLineSwitchEnd(arg)
392 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
394 return CHANNEL_RC_OK;
402 FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_audin_client_subsystem_entry(
406 AudinALSADevice* alsa = NULL;
408 alsa = (AudinALSADevice*)calloc(1,
sizeof(AudinALSADevice));
412 WLog_ERR(TAG,
"calloc failed!");
413 return CHANNEL_RC_NO_MEMORY;
416 alsa->log = WLog_Get(TAG);
417 alsa->iface.Open = audin_alsa_open;
418 alsa->iface.FormatSupported = audin_alsa_format_supported;
419 alsa->iface.SetFormat = audin_alsa_set_format;
420 alsa->iface.Close = audin_alsa_close;
421 alsa->iface.Free = audin_alsa_free;
422 alsa->rdpcontext = pEntryPoints->rdpcontext;
423 args = pEntryPoints->args;
425 if ((error = audin_alsa_parse_addin_args(alsa, args)))
427 WLog_Print(alsa->log, WLOG_ERROR,
428 "audin_alsa_parse_addin_args failed with errorcode %" PRIu32
"!", error);
432 if (!alsa->device_name)
434 alsa->device_name = _strdup(
"default");
436 if (!alsa->device_name)
438 WLog_Print(alsa->log, WLOG_ERROR,
"_strdup failed!");
439 error = CHANNEL_RC_NO_MEMORY;
444 alsa->frames_per_packet = 128;
445 alsa->aformat.nChannels = 2;
446 alsa->aformat.wBitsPerSample = 16;
447 alsa->aformat.wFormatTag = WAVE_FORMAT_PCM;
448 alsa->aformat.nSamplesPerSec = 44100;
450 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)alsa)))
452 WLog_Print(alsa->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
457 return CHANNEL_RC_OK;
459 free(alsa->device_name);