FreeRDP
Loading...
Searching...
No Matches
audin_winmm.c
1
22#include <freerdp/config.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include <windows.h>
29#include <mmsystem.h>
30
31#include <winpr/crt.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>
37
38#include "audin_main.h"
39
40/* fix missing definitions in mingw */
41#ifndef WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE
42#define WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE 0x0010
43#endif
44
45typedef struct
46{
47 IAudinDevice iface;
48
49 char* device_name;
50 AudinReceive receive;
51 void* user_data;
52 HANDLE thread;
53 HANDLE stopEvent;
54 HWAVEIN hWaveIn;
55 PWAVEFORMATEX* ppwfx;
56 PWAVEFORMATEX pwfx_cur;
57 UINT32 ppwfx_size;
58 UINT32 cFormats;
59 UINT32 frames_per_packet;
60 rdpContext* rdpcontext;
61 wLog* log;
62} AudinWinmmDevice;
63
64static void CALLBACK waveInProc(HWAVEIN hWaveIn, UINT uMsg, DWORD_PTR dwInstance,
65 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
66{
67 AudinWinmmDevice* winmm = (AudinWinmmDevice*)dwInstance;
68 PWAVEHDR pWaveHdr;
69 UINT error = CHANNEL_RC_OK;
70 MMRESULT mmResult;
71
72 switch (uMsg)
73 {
74 case WIM_CLOSE:
75 break;
76
77 case WIM_DATA:
78 pWaveHdr = (WAVEHDR*)dwParam1;
79
80 if (WHDR_DONE == (WHDR_DONE & pWaveHdr->dwFlags))
81 {
82 if (pWaveHdr->dwBytesRecorded &&
83 !(WaitForSingleObject(winmm->stopEvent, 0) == WAIT_OBJECT_0))
84 {
85 AUDIO_FORMAT format;
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;
93
94 if ((error = winmm->receive(&format, pWaveHdr->lpData,
95 pWaveHdr->dwBytesRecorded, winmm->user_data)))
96 break;
97
98 mmResult = waveInAddBuffer(hWaveIn, pWaveHdr, sizeof(WAVEHDR));
99
100 if (mmResult != MMSYSERR_NOERROR)
101 error = ERROR_INTERNAL_ERROR;
102 }
103 }
104
105 break;
106
107 case WIM_OPEN:
108 break;
109
110 default:
111 break;
112 }
113
114 if (error && winmm->rdpcontext)
115 setChannelError(winmm->rdpcontext, error, "waveInProc reported an error");
116}
117
118static BOOL log_mmresult(AudinWinmmDevice* winmm, const char* what, MMRESULT result)
119{
120 if (result != MMSYSERR_NOERROR)
121 {
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));
126
127 _snprintf(msg, sizeof(msg) - 1, "%s failed. %" PRIu32 " [%s]", what, result, buffer);
128 _snprintf(cmsg, sizeof(cmsg) - 1, "audin_winmm_thread_func reported an error '%s'", msg);
129 WLog_Print(winmm->log, WLOG_DEBUG, "%s", msg);
130 if (winmm->rdpcontext)
131 setChannelError(winmm->rdpcontext, ERROR_INTERNAL_ERROR, cmsg);
132 return FALSE;
133 }
134 return TRUE;
135}
136
137static BOOL test_format_supported(const PWAVEFORMATEX pwfx)
138{
139 MMRESULT rc;
140 WAVEINCAPSA caps = WINPR_C_ARRAY_INIT;
141
142 rc = waveInGetDevCapsA(WAVE_MAPPER, &caps, sizeof(caps));
143 if (rc != MMSYSERR_NOERROR)
144 return FALSE;
145
146 switch (pwfx->nChannels)
147 {
148 case 1:
149 if ((caps.dwFormats &
150 (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 | WAVE_FORMAT_4M08 | WAVE_FORMAT_96M08 |
151 WAVE_FORMAT_1M16 | WAVE_FORMAT_2M16 | WAVE_FORMAT_4M16 | WAVE_FORMAT_96M16)) == 0)
152 return FALSE;
153 break;
154 case 2:
155 if ((caps.dwFormats &
156 (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 | WAVE_FORMAT_96S08 |
157 WAVE_FORMAT_1S16 | WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 | WAVE_FORMAT_96S16)) == 0)
158 return FALSE;
159 break;
160 default:
161 return FALSE;
162 }
163
164 rc = waveInOpen(nullptr, WAVE_MAPPER, pwfx, 0, 0,
165 WAVE_FORMAT_QUERY | WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE);
166 return (rc == MMSYSERR_NOERROR);
167}
168
169static DWORD WINAPI audin_winmm_thread_func(LPVOID arg)
170{
171 AudinWinmmDevice* winmm = (AudinWinmmDevice*)arg;
172 WAVEHDR waveHdr[4] = WINPR_C_ARRAY_INIT;
173 DWORD status = 0;
174 MMRESULT rc = 0;
175
176 if (!winmm->hWaveIn)
177 {
178 rc = waveInOpen(&winmm->hWaveIn, WAVE_MAPPER, winmm->pwfx_cur, (DWORD_PTR)waveInProc,
179 (DWORD_PTR)winmm,
180 CALLBACK_FUNCTION | WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE);
181 if (!log_mmresult(winmm, "waveInOpen", rc))
182 return ERROR_INTERNAL_ERROR;
183 }
184
185 const size_t framesize =
186 1ull * winmm->pwfx_cur->wBitsPerSample / 8ull * winmm->pwfx_cur->nChannels;
187 if (framesize > UINT32_MAX)
188 return ERROR_INTERNAL_ERROR;
189
190 const size_t size = framesize * winmm->frames_per_packet;
191 if (size > UINT32_MAX)
192 return ERROR_INTERNAL_ERROR;
193 for (int i = 0; i < 4; i++)
194 {
195 char* buffer = (char*)calloc(framesize, winmm->frames_per_packet);
196
197 if (!buffer)
198 return CHANNEL_RC_NO_MEMORY;
199
200 waveHdr[i].dwBufferLength = WINPR_ASSERTING_INT_CAST(DWORD, size);
201 waveHdr[i].dwFlags = 0;
202 waveHdr[i].lpData = buffer;
203 rc = waveInPrepareHeader(winmm->hWaveIn, &waveHdr[i], sizeof(waveHdr[i]));
204
205 if (!log_mmresult(winmm, "waveInPrepareHeader", rc))
206 {
207 }
208
209 rc = waveInAddBuffer(winmm->hWaveIn, &waveHdr[i], sizeof(waveHdr[i]));
210
211 if (!log_mmresult(winmm, "waveInAddBuffer", rc))
212 {
213 }
214 }
215
216 rc = waveInStart(winmm->hWaveIn);
217
218 if (!log_mmresult(winmm, "waveInStart", rc))
219 {
220 }
221
222 status = WaitForSingleObject(winmm->stopEvent, INFINITE);
223
224 if (status == WAIT_FAILED)
225 {
226 WLog_Print(winmm->log, WLOG_DEBUG, "WaitForSingleObject failed.");
227
228 if (winmm->rdpcontext)
229 setChannelError(winmm->rdpcontext, ERROR_INTERNAL_ERROR,
230 "audin_winmm_thread_func reported an error");
231 }
232
233 rc = waveInReset(winmm->hWaveIn);
234
235 if (!log_mmresult(winmm, "waveInReset", rc))
236 {
237 }
238
239 for (int i = 0; i < 4; i++)
240 {
241 rc = waveInUnprepareHeader(winmm->hWaveIn, &waveHdr[i], sizeof(waveHdr[i]));
242
243 if (!log_mmresult(winmm, "waveInUnprepareHeader", rc))
244 {
245 }
246
247 free(waveHdr[i].lpData);
248 }
249
250 rc = waveInClose(winmm->hWaveIn);
251
252 if (!log_mmresult(winmm, "waveInClose", rc))
253 {
254 }
255
256 winmm->hWaveIn = nullptr;
257 return 0;
258}
259
265static UINT audin_winmm_free(IAudinDevice* device)
266{
267 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
268
269 if (!winmm)
270 return ERROR_INVALID_PARAMETER;
271
272 for (UINT32 i = 0; i < winmm->cFormats; i++)
273 {
274 free(winmm->ppwfx[i]);
275 }
276
277 free(winmm->ppwfx);
278 free(winmm->device_name);
279 free(winmm);
280 return CHANNEL_RC_OK;
281}
282
288static UINT audin_winmm_close(IAudinDevice* device)
289{
290 DWORD status;
291 UINT error = CHANNEL_RC_OK;
292 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
293
294 if (!winmm)
295 return ERROR_INVALID_PARAMETER;
296
297 (void)SetEvent(winmm->stopEvent);
298 status = WaitForSingleObject(winmm->thread, INFINITE);
299
300 if (status == WAIT_FAILED)
301 {
302 error = GetLastError();
303 WLog_Print(winmm->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "!",
304 error);
305 return error;
306 }
307
308 (void)CloseHandle(winmm->thread);
309 (void)CloseHandle(winmm->stopEvent);
310 winmm->thread = nullptr;
311 winmm->stopEvent = nullptr;
312 winmm->receive = nullptr;
313 winmm->user_data = nullptr;
314 return error;
315}
316
322static UINT audin_winmm_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
323 UINT32 FramesPerPacket)
324{
325 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
326
327 if (!winmm || !format)
328 return ERROR_INVALID_PARAMETER;
329
330 winmm->frames_per_packet = FramesPerPacket;
331
332 for (UINT32 i = 0; i < winmm->cFormats; i++)
333 {
334 const PWAVEFORMATEX ppwfx = winmm->ppwfx[i];
335 if ((ppwfx->wFormatTag == format->wFormatTag) && (ppwfx->nChannels == format->nChannels) &&
336 (ppwfx->wBitsPerSample == format->wBitsPerSample) &&
337 (ppwfx->nSamplesPerSec == format->nSamplesPerSec))
338 {
339 /* BUG: Many devices report to support stereo recording but fail here.
340 * Ensure we always use mono. */
341 if (ppwfx->nChannels > 1)
342 {
343 ppwfx->nChannels = 1;
344 }
345
346 if (ppwfx->nBlockAlign != 2)
347 {
348 ppwfx->nBlockAlign = 2;
349 ppwfx->nAvgBytesPerSec = ppwfx->nSamplesPerSec * ppwfx->nBlockAlign;
350 }
351
352 if (!test_format_supported(ppwfx))
353 return ERROR_INVALID_PARAMETER;
354 winmm->pwfx_cur = ppwfx;
355 return CHANNEL_RC_OK;
356 }
357 }
358
359 return ERROR_INVALID_PARAMETER;
360}
361
362static BOOL audin_winmm_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
363{
364 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
365 PWAVEFORMATEX pwfx;
366 BYTE* data;
367
368 if (!winmm || !format)
369 return FALSE;
370
371 if (format->wFormatTag != WAVE_FORMAT_PCM)
372 return FALSE;
373
374 if (format->nChannels != 1)
375 return FALSE;
376
377 pwfx = (PWAVEFORMATEX)malloc(sizeof(WAVEFORMATEX) + format->cbSize);
378
379 if (!pwfx)
380 return FALSE;
381
382 pwfx->cbSize = format->cbSize;
383 pwfx->wFormatTag = format->wFormatTag;
384 pwfx->nChannels = format->nChannels;
385 pwfx->nSamplesPerSec = format->nSamplesPerSec;
386 pwfx->nBlockAlign = format->nBlockAlign;
387 pwfx->wBitsPerSample = format->wBitsPerSample;
388 data = (BYTE*)pwfx + sizeof(WAVEFORMATEX);
389 memcpy(data, format->data, format->cbSize);
390
391 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
392
393 if (!test_format_supported(pwfx))
394 goto fail;
395
396 if (winmm->cFormats >= winmm->ppwfx_size)
397 {
398 PWAVEFORMATEX* tmp_ppwfx;
399 tmp_ppwfx = realloc(winmm->ppwfx, sizeof(PWAVEFORMATEX) * winmm->ppwfx_size * 2);
400
401 if (!tmp_ppwfx)
402 goto fail;
403
404 winmm->ppwfx_size *= 2;
405 winmm->ppwfx = tmp_ppwfx;
406 }
407
408 winmm->ppwfx[winmm->cFormats++] = pwfx;
409 return TRUE;
410
411fail:
412 free(pwfx);
413 return FALSE;
414}
415
421static UINT audin_winmm_open(IAudinDevice* device, AudinReceive receive, void* user_data)
422{
423 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
424
425 if (!winmm || !receive || !user_data)
426 return ERROR_INVALID_PARAMETER;
427
428 winmm->receive = receive;
429 winmm->user_data = user_data;
430
431 if (!(winmm->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
432 {
433 WLog_Print(winmm->log, WLOG_ERROR, "CreateEvent failed!");
434 return ERROR_INTERNAL_ERROR;
435 }
436
437 if (!(winmm->thread = CreateThread(nullptr, 0, audin_winmm_thread_func, winmm, 0, nullptr)))
438 {
439 WLog_Print(winmm->log, WLOG_ERROR, "CreateThread failed!");
440 (void)CloseHandle(winmm->stopEvent);
441 winmm->stopEvent = nullptr;
442 return ERROR_INTERNAL_ERROR;
443 }
444
445 return CHANNEL_RC_OK;
446}
447
453static UINT audin_winmm_parse_addin_args(AudinWinmmDevice* device, const ADDIN_ARGV* args)
454{
455 int status;
456 DWORD flags;
457 const COMMAND_LINE_ARGUMENT_A* arg;
458 AudinWinmmDevice* winmm = (AudinWinmmDevice*)device;
459 COMMAND_LINE_ARGUMENT_A audin_winmm_args[] = {
460 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr,
461 "audio device name" },
462 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
463 };
464
465 flags =
466 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
467 status = CommandLineParseArgumentsA(args->argc, args->argv, audin_winmm_args, flags, winmm,
468 nullptr, nullptr);
469 arg = audin_winmm_args;
470
471 do
472 {
473 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
474 continue;
475
476 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
477 {
478 winmm->device_name = _strdup(arg->Value);
479
480 if (!winmm->device_name)
481 {
482 WLog_Print(winmm->log, WLOG_ERROR, "_strdup failed!");
483 return CHANNEL_RC_NO_MEMORY;
484 }
485 }
486 CommandLineSwitchEnd(arg)
487 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
488
489 return CHANNEL_RC_OK;
490}
491
497FREERDP_ENTRY_POINT(UINT VCAPITYPE winmm_freerdp_audin_client_subsystem_entry(
499{
500 const ADDIN_ARGV* args;
501 AudinWinmmDevice* winmm;
502 UINT error;
503
504 if (waveInGetNumDevs() == 0)
505 {
506 WLog_Print(WLog_Get(TAG), WLOG_ERROR, "No microphone available!");
507 return ERROR_DEVICE_NOT_AVAILABLE;
508 }
509
510 winmm = (AudinWinmmDevice*)calloc(1, sizeof(AudinWinmmDevice));
511
512 if (!winmm)
513 {
514 WLog_ERR(TAG, "calloc failed!");
515 return CHANNEL_RC_NO_MEMORY;
516 }
517
518 winmm->log = WLog_Get(TAG);
519 winmm->iface.Open = audin_winmm_open;
520 winmm->iface.FormatSupported = audin_winmm_format_supported;
521 winmm->iface.SetFormat = audin_winmm_set_format;
522 winmm->iface.Close = audin_winmm_close;
523 winmm->iface.Free = audin_winmm_free;
524 winmm->rdpcontext = pEntryPoints->rdpcontext;
525 args = pEntryPoints->args;
526
527 if ((error = audin_winmm_parse_addin_args(winmm, args)))
528 {
529 WLog_Print(winmm->log, WLOG_ERROR,
530 "audin_winmm_parse_addin_args failed with error %" PRIu32 "!", error);
531 goto error_out;
532 }
533
534 if (!winmm->device_name)
535 {
536 winmm->device_name = _strdup("default");
537
538 if (!winmm->device_name)
539 {
540 WLog_Print(winmm->log, WLOG_ERROR, "_strdup failed!");
541 error = CHANNEL_RC_NO_MEMORY;
542 goto error_out;
543 }
544 }
545
546 winmm->ppwfx_size = 10;
547 winmm->ppwfx = calloc(winmm->ppwfx_size, sizeof(PWAVEFORMATEX));
548
549 if (!winmm->ppwfx)
550 {
551 WLog_Print(winmm->log, WLOG_ERROR, "malloc failed!");
552 error = CHANNEL_RC_NO_MEMORY;
553 goto error_out;
554 }
555
556 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &winmm->iface)))
557 {
558 WLog_Print(winmm->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
559 error);
560 goto error_out;
561 }
562
563 return CHANNEL_RC_OK;
564error_out:
565 free(winmm->ppwfx);
566 free(winmm->device_name);
567 free(winmm);
568 return error;
569}