23 #include <freerdp/config.h>
27 #include <winpr/cmdline.h>
29 #include <freerdp/freerdp.h>
30 #include <freerdp/channels/rdpsnd.h>
32 #include "audin_main.h"
42 UINT32 FramesPerPacket;
47 rdpContext* rdpcontext;
50 static BOOL audin_sndio_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
52 if (device == NULL || format == NULL)
55 return (format->wFormatTag == WAVE_FORMAT_PCM);
63 static UINT audin_sndio_set_format(IAudinDevice* device,
AUDIO_FORMAT* format,
64 UINT32 FramesPerPacket)
66 AudinSndioDevice* sndio = (AudinSndioDevice*)device;
68 if (device == NULL || format == NULL)
69 return ERROR_INVALID_PARAMETER;
71 if (format->wFormatTag != WAVE_FORMAT_PCM)
72 return ERROR_INTERNAL_ERROR;
74 sndio->format = *format;
75 sndio->FramesPerPacket = FramesPerPacket;
80 static void* audin_sndio_thread_func(
void* arg)
86 AudinSndioDevice* sndio = (AudinSndioDevice*)arg;
92 error = ERROR_INVALID_PARAMETER;
96 hdl = sio_open(SIO_DEVANY, SIO_REC, 0);
99 WLog_ERR(TAG,
"could not open audio device");
100 error = ERROR_INTERNAL_ERROR;
105 par.bits = sndio->format.wBitsPerSample;
106 par.rchan = sndio->format.nChannels;
107 par.rate = sndio->format.nSamplesPerSec;
108 if (!sio_setpar(hdl, &par))
110 WLog_ERR(TAG,
"could not set audio parameters");
111 error = ERROR_INTERNAL_ERROR;
114 if (!sio_getpar(hdl, &par))
116 WLog_ERR(TAG,
"could not get audio parameters");
117 error = ERROR_INTERNAL_ERROR;
123 WLog_ERR(TAG,
"could not start audio device");
124 error = ERROR_INTERNAL_ERROR;
129 (sndio->FramesPerPacket * sndio->format.nChannels * (sndio->format.wBitsPerSample / 8));
130 buffer = (BYTE*)calloc((nbytes +
sizeof(
void*)),
sizeof(BYTE));
134 error = ERROR_NOT_ENOUGH_MEMORY;
140 status = WaitForSingleObject(sndio->stopEvent, 0);
142 if (status == WAIT_FAILED)
144 error = GetLastError();
145 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
149 if (status == WAIT_OBJECT_0)
152 n = sio_read(hdl, buffer, nbytes);
156 WLog_ERR(TAG,
"could not read");
163 if ((error = sndio->receive(&sndio->format, buffer, nbytes, sndio->user_data)))
165 WLog_ERR(TAG,
"sndio->receive failed with error %" PRIu32
"", error);
171 if (error && sndio->rdpcontext)
172 setChannelError(sndio->rdpcontext, error,
"audin_sndio_thread_func reported an error");
176 WLog_INFO(TAG,
"sio_close");
191 static UINT audin_sndio_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
193 AudinSndioDevice* sndio = (AudinSndioDevice*)device;
194 sndio->receive = receive;
195 sndio->user_data = user_data;
197 if (!(sndio->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
199 WLog_ERR(TAG,
"CreateEvent failed");
200 return ERROR_INTERNAL_ERROR;
203 if (!(sndio->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)audin_sndio_thread_func,
206 WLog_ERR(TAG,
"CreateThread failed");
207 (void)CloseHandle(sndio->stopEvent);
208 sndio->stopEvent = NULL;
209 return ERROR_INTERNAL_ERROR;
212 return CHANNEL_RC_OK;
220 static UINT audin_sndio_close(IAudinDevice* device)
223 AudinSndioDevice* sndio = (AudinSndioDevice*)device;
226 return ERROR_INVALID_PARAMETER;
228 if (sndio->stopEvent != NULL)
230 (void)SetEvent(sndio->stopEvent);
232 if (WaitForSingleObject(sndio->thread, INFINITE) == WAIT_FAILED)
234 error = GetLastError();
235 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
239 (void)CloseHandle(sndio->stopEvent);
240 sndio->stopEvent = NULL;
241 (void)CloseHandle(sndio->thread);
242 sndio->thread = NULL;
245 sndio->receive = NULL;
246 sndio->user_data = NULL;
248 return CHANNEL_RC_OK;
256 static UINT audin_sndio_free(IAudinDevice* device)
258 AudinSndioDevice* sndio = (AudinSndioDevice*)device;
262 return ERROR_INVALID_PARAMETER;
264 if ((error = audin_sndio_close(device)))
266 WLog_ERR(TAG,
"audin_sndio_close failed with error code %d", error);
271 return CHANNEL_RC_OK;
279 static UINT audin_sndio_parse_addin_args(AudinSndioDevice* device,
ADDIN_ARGV* args)
284 AudinSndioDevice* sndio = (AudinSndioDevice*)device;
287 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
288 status = CommandLineParseArgumentsA(args->argc, (
const char**)args->argv, audin_sndio_args,
289 flags, sndio, NULL, NULL);
292 return ERROR_INVALID_PARAMETER;
294 arg = audin_sndio_args;
298 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
301 CommandLineSwitchStart(arg) CommandLineSwitchEnd(arg)
302 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
304 return CHANNEL_RC_OK;
312 FREERDP_ENTRY_POINT(UINT VCAPITYPE sndio_freerdp_audin_client_subsystem_entry(
316 AudinSndioDevice* sndio;
317 UINT ret = CHANNEL_RC_OK;
318 sndio = (AudinSndioDevice*)calloc(1,
sizeof(AudinSndioDevice));
321 return CHANNEL_RC_NO_MEMORY;
323 sndio->device.Open = audin_sndio_open;
324 sndio->device.FormatSupported = audin_sndio_format_supported;
325 sndio->device.SetFormat = audin_sndio_set_format;
326 sndio->device.Close = audin_sndio_close;
327 sndio->device.Free = audin_sndio_free;
328 sndio->rdpcontext = pEntryPoints->rdpcontext;
329 args = pEntryPoints->args;
333 ret = audin_sndio_parse_addin_args(sndio, args);
335 if (ret != CHANNEL_RC_OK)
337 WLog_ERR(TAG,
"error parsing arguments");
342 if ((ret = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)sndio)))
344 WLog_ERR(TAG,
"RegisterAudinDevice failed with error %" PRIu32
"", ret);
350 audin_sndio_free(&sndio->device);