22#include <freerdp/config.h>
32#include <winpr/wtsapi.h>
33#include <winpr/cmdline.h>
34#include <freerdp/freerdp.h>
35#include <freerdp/addin.h>
36#include <freerdp/client/audin.h>
38#include "audin_main.h"
41#ifndef WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE
42#define WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE 0x0010
56 PWAVEFORMATEX pwfx_cur;
59 UINT32 frames_per_packet;
60 rdpContext* rdpcontext;
64static void CALLBACK waveInProc(HWAVEIN hWaveIn, UINT uMsg, DWORD_PTR dwInstance,
65 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
67 AudinWinmmDevice* winmm = (AudinWinmmDevice*)dwInstance;
69 UINT error = CHANNEL_RC_OK;
78 pWaveHdr = (WAVEHDR*)dwParam1;
80 if (WHDR_DONE == (WHDR_DONE & pWaveHdr->dwFlags))
82 if (pWaveHdr->dwBytesRecorded &&
83 !(WaitForSingleObject(winmm->stopEvent, 0) == WAIT_OBJECT_0))
86 format.cbSize = winmm->pwfx_cur->cbSize;
87 format.nBlockAlign = winmm->pwfx_cur->nBlockAlign;
88 format.nAvgBytesPerSec = winmm->pwfx_cur->nAvgBytesPerSec;
89 format.nChannels = winmm->pwfx_cur->nChannels;
90 format.nSamplesPerSec = winmm->pwfx_cur->nSamplesPerSec;
91 format.wBitsPerSample = winmm->pwfx_cur->wBitsPerSample;
92 format.wFormatTag = winmm->pwfx_cur->wFormatTag;
94 if ((error = winmm->receive(&format, pWaveHdr->lpData,
95 pWaveHdr->dwBytesRecorded, winmm->user_data)))
98 mmResult = waveInAddBuffer(hWaveIn, pWaveHdr,
sizeof(WAVEHDR));
100 if (mmResult != MMSYSERR_NOERROR)
101 error = ERROR_INTERNAL_ERROR;
114 if (error && winmm->rdpcontext)
115 setChannelError(winmm->rdpcontext, error,
"waveInProc reported an error");
118static BOOL log_mmresult(AudinWinmmDevice* winmm,
const char* what, MMRESULT result)
120 if (result != MMSYSERR_NOERROR)
122 CHAR buffer[8192] = WINPR_C_ARRAY_INIT;
123 CHAR msg[8192] = WINPR_C_ARRAY_INIT;
124 CHAR cmsg[8192] = WINPR_C_ARRAY_INIT;
125 waveInGetErrorTextA(result, buffer,
sizeof(buffer));
127 (void)_snprintf(msg,
sizeof(msg) - 1,
"%s failed. %" PRIu32
" [%s]", what, result, buffer);
128 (void)_snprintf(cmsg,
sizeof(cmsg) - 1,
"audin_winmm_thread_func reported an error '%s'",
130 WLog_Print(winmm->log, WLOG_DEBUG,
"%s", msg);
131 if (winmm->rdpcontext)
132 setChannelError(winmm->rdpcontext, ERROR_INTERNAL_ERROR, cmsg);
138static BOOL test_format_supported(
const PWAVEFORMATEX pwfx)
141 WAVEINCAPSA caps = WINPR_C_ARRAY_INIT;
143 rc = waveInGetDevCapsA(WAVE_MAPPER, &caps,
sizeof(caps));
144 if (rc != MMSYSERR_NOERROR)
147 switch (pwfx->nChannels)
150 if ((caps.dwFormats &
151 (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 | WAVE_FORMAT_4M08 | WAVE_FORMAT_96M08 |
152 WAVE_FORMAT_1M16 | WAVE_FORMAT_2M16 | WAVE_FORMAT_4M16 | WAVE_FORMAT_96M16)) == 0)
156 if ((caps.dwFormats &
157 (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 | WAVE_FORMAT_96S08 |
158 WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 | WAVE_FORMAT_96S16)) == 0)
165 rc = waveInOpen(
nullptr, WAVE_MAPPER, pwfx, 0, 0,
166 WAVE_FORMAT_QUERY | WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE);
167 return (rc == MMSYSERR_NOERROR);
170static DWORD WINAPI audin_winmm_thread_func(LPVOID arg)
172 AudinWinmmDevice* winmm = (AudinWinmmDevice*)arg;
173 WAVEHDR waveHdr[4] = WINPR_C_ARRAY_INIT;
179 rc = waveInOpen(&winmm->hWaveIn, WAVE_MAPPER, winmm->pwfx_cur, (DWORD_PTR)waveInProc,
181 CALLBACK_FUNCTION | WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE);
182 if (!log_mmresult(winmm,
"waveInOpen", rc))
183 return ERROR_INTERNAL_ERROR;
186 const size_t framesize =
187 1ull * winmm->pwfx_cur->wBitsPerSample / 8ull * winmm->pwfx_cur->nChannels;
188 if (framesize > UINT32_MAX)
189 return ERROR_INTERNAL_ERROR;
191 const size_t size = framesize * winmm->frames_per_packet;
192 if (size > UINT32_MAX)
193 return ERROR_INTERNAL_ERROR;
194 for (
int i = 0; i < 4; i++)
196 char* buffer = (
char*)calloc(framesize, winmm->frames_per_packet);
199 return CHANNEL_RC_NO_MEMORY;
201 waveHdr[i].dwBufferLength = WINPR_ASSERTING_INT_CAST(DWORD, size);
202 waveHdr[i].dwFlags = 0;
203 waveHdr[i].lpData = buffer;
204 rc = waveInPrepareHeader(winmm->hWaveIn, &waveHdr[i],
sizeof(waveHdr[i]));
206 if (!log_mmresult(winmm,
"waveInPrepareHeader", rc))
210 rc = waveInAddBuffer(winmm->hWaveIn, &waveHdr[i],
sizeof(waveHdr[i]));
212 if (!log_mmresult(winmm,
"waveInAddBuffer", rc))
217 rc = waveInStart(winmm->hWaveIn);
219 if (!log_mmresult(winmm,
"waveInStart", rc))
223 status = WaitForSingleObject(winmm->stopEvent, INFINITE);
225 if (status == WAIT_FAILED)
227 WLog_Print(winmm->log, WLOG_DEBUG,
"WaitForSingleObject failed.");
229 if (winmm->rdpcontext)
230 setChannelError(winmm->rdpcontext, ERROR_INTERNAL_ERROR,
231 "audin_winmm_thread_func reported an error");
234 rc = waveInReset(winmm->hWaveIn);
236 if (!log_mmresult(winmm,
"waveInReset", rc))
240 for (
int i = 0; i < 4; i++)
242 rc = waveInUnprepareHeader(winmm->hWaveIn, &waveHdr[i],
sizeof(waveHdr[i]));
244 if (!log_mmresult(winmm,
"waveInUnprepareHeader", rc))
248 free(waveHdr[i].lpData);
251 rc = waveInClose(winmm->hWaveIn);
253 if (!log_mmresult(winmm,
"waveInClose", rc))
257 winmm->hWaveIn =
nullptr;
266static UINT audin_winmm_free(IAudinDevice* device)
268 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
271 return ERROR_INVALID_PARAMETER;
273 for (UINT32 i = 0; i < winmm->cFormats; i++)
275 free(winmm->ppwfx[i]);
279 free(winmm->device_name);
281 return CHANNEL_RC_OK;
289static UINT audin_winmm_close(IAudinDevice* device)
292 UINT error = CHANNEL_RC_OK;
293 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
296 return ERROR_INVALID_PARAMETER;
300 (void)SetEvent(winmm->stopEvent);
301 status = WaitForSingleObject(winmm->thread, INFINITE);
303 if (status == WAIT_FAILED)
305 error = GetLastError();
306 WLog_Print(winmm->log, WLOG_ERROR,
"WaitForSingleObject failed with error %" PRIu32
"!",
311 (void)CloseHandle(winmm->thread);
312 (void)CloseHandle(winmm->stopEvent);
313 winmm->thread =
nullptr;
314 winmm->stopEvent =
nullptr;
316 winmm->receive =
nullptr;
317 winmm->user_data =
nullptr;
326static UINT audin_winmm_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
327 UINT32 FramesPerPacket)
329 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
331 if (!winmm || !format)
332 return ERROR_INVALID_PARAMETER;
334 winmm->frames_per_packet = FramesPerPacket;
336 for (UINT32 i = 0; i < winmm->cFormats; i++)
338 const PWAVEFORMATEX ppwfx = winmm->ppwfx[i];
339 if ((ppwfx->wFormatTag == format->wFormatTag) && (ppwfx->nChannels == format->nChannels) &&
340 (ppwfx->wBitsPerSample == format->wBitsPerSample) &&
341 (ppwfx->nSamplesPerSec == format->nSamplesPerSec))
345 if (ppwfx->nChannels > 1)
347 ppwfx->nChannels = 1;
350 if (ppwfx->nBlockAlign != 2)
352 ppwfx->nBlockAlign = 2;
353 ppwfx->nAvgBytesPerSec = ppwfx->nSamplesPerSec * ppwfx->nBlockAlign;
356 if (!test_format_supported(ppwfx))
357 return ERROR_INVALID_PARAMETER;
358 winmm->pwfx_cur = ppwfx;
359 return CHANNEL_RC_OK;
363 return ERROR_INVALID_PARAMETER;
366static BOOL audin_winmm_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
368 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
372 if (!winmm || !format)
375 if (format->wFormatTag != WAVE_FORMAT_PCM)
378 if (format->nChannels != 1)
381 pwfx = (PWAVEFORMATEX)malloc(
sizeof(WAVEFORMATEX) + format->cbSize);
386 pwfx->cbSize = format->cbSize;
387 pwfx->wFormatTag = format->wFormatTag;
388 pwfx->nChannels = format->nChannels;
389 pwfx->nSamplesPerSec = format->nSamplesPerSec;
390 pwfx->nBlockAlign = format->nBlockAlign;
391 pwfx->wBitsPerSample = format->wBitsPerSample;
392 data = (BYTE*)pwfx +
sizeof(WAVEFORMATEX);
393 memcpy(data, format->data, format->cbSize);
395 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
397 if (!test_format_supported(pwfx))
400 if (winmm->cFormats >= winmm->ppwfx_size)
402 PWAVEFORMATEX* tmp_ppwfx;
403 tmp_ppwfx = realloc(winmm->ppwfx,
sizeof(PWAVEFORMATEX) * winmm->ppwfx_size * 2);
408 winmm->ppwfx_size *= 2;
409 winmm->ppwfx = tmp_ppwfx;
412 winmm->ppwfx[winmm->cFormats++] = pwfx;
425static UINT audin_winmm_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
427 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
429 if (!winmm || !receive || !user_data)
430 return ERROR_INVALID_PARAMETER;
432 winmm->receive = receive;
433 winmm->user_data = user_data;
435 if (!(winmm->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
437 WLog_Print(winmm->log, WLOG_ERROR,
"CreateEvent failed!");
438 return ERROR_INTERNAL_ERROR;
441 if (!(winmm->thread = CreateThread(
nullptr, 0, audin_winmm_thread_func, winmm, 0,
nullptr)))
443 WLog_Print(winmm->log, WLOG_ERROR,
"CreateThread failed!");
444 (void)CloseHandle(winmm->stopEvent);
445 winmm->stopEvent =
nullptr;
446 return ERROR_INTERNAL_ERROR;
449 return CHANNEL_RC_OK;
457static UINT audin_winmm_parse_addin_args(AudinWinmmDevice* device,
const ADDIN_ARGV* args)
462 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
464 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
465 "audio device name" },
466 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
470 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
471 status = CommandLineParseArgumentsA(args->argc, args->argv, audin_winmm_args, flags, winmm,
473 arg = audin_winmm_args;
477 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
480 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
482 winmm->device_name = _strdup(arg->Value);
484 if (!winmm->device_name)
486 WLog_Print(winmm->log, WLOG_ERROR,
"_strdup failed!");
487 return CHANNEL_RC_NO_MEMORY;
490 CommandLineSwitchEnd(arg)
491 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
493 return CHANNEL_RC_OK;
501FREERDP_ENTRY_POINT(UINT VCAPITYPE winmm_freerdp_audin_client_subsystem_entry(
505 AudinWinmmDevice* winmm;
508 if (waveInGetNumDevs() == 0)
510 WLog_Print(WLog_Get(TAG), WLOG_ERROR,
"No microphone available!");
511 return ERROR_DEVICE_NOT_AVAILABLE;
514 winmm = (AudinWinmmDevice*)calloc(1,
sizeof(AudinWinmmDevice));
518 WLog_ERR(TAG,
"calloc failed!");
519 return CHANNEL_RC_NO_MEMORY;
522 winmm->log = WLog_Get(TAG);
523 winmm->iface.Open = audin_winmm_open;
524 winmm->iface.FormatSupported = audin_winmm_format_supported;
525 winmm->iface.SetFormat = audin_winmm_set_format;
526 winmm->iface.Close = audin_winmm_close;
527 winmm->iface.Free = audin_winmm_free;
528 winmm->rdpcontext = pEntryPoints->rdpcontext;
529 args = pEntryPoints->args;
531 if ((error = audin_winmm_parse_addin_args(winmm, args)))
533 WLog_Print(winmm->log, WLOG_ERROR,
534 "audin_winmm_parse_addin_args failed with error %" PRIu32
"!", error);
538 if (!winmm->device_name)
540 winmm->device_name = _strdup(
"default");
542 if (!winmm->device_name)
544 WLog_Print(winmm->log, WLOG_ERROR,
"_strdup failed!");
545 error = CHANNEL_RC_NO_MEMORY;
550 winmm->ppwfx_size = 10;
551 winmm->ppwfx = calloc(winmm->ppwfx_size,
sizeof(PWAVEFORMATEX));
555 WLog_Print(winmm->log, WLOG_ERROR,
"malloc failed!");
556 error = CHANNEL_RC_NO_MEMORY;
560 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &winmm->iface)))
562 WLog_Print(winmm->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
567 return CHANNEL_RC_OK;
570 free(winmm->device_name);