FreeRDP
Loading...
Searching...
No Matches
audin_main.c
1
23#include <freerdp/config.h>
24
25#include <errno.h>
26#include <winpr/assert.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <winpr/crt.h>
32#include <winpr/cmdline.h>
33#include <winpr/wlog.h>
34
35#include <freerdp/addin.h>
36
37#include <winpr/stream.h>
38#include <freerdp/freerdp.h>
39#include <freerdp/codec/dsp.h>
40#include <freerdp/client/channels.h>
41#include <freerdp/channels/audin.h>
42
43#include "audin_main.h"
44
45#define SNDIN_VERSION 0x02
46
47typedef enum
48{
49 MSG_SNDIN_VERSION = 0x01,
50 MSG_SNDIN_FORMATS = 0x02,
51 MSG_SNDIN_OPEN = 0x03,
52 MSG_SNDIN_OPEN_REPLY = 0x04,
53 MSG_SNDIN_DATA_INCOMING = 0x05,
54 MSG_SNDIN_DATA = 0x06,
55 MSG_SNDIN_FORMATCHANGE = 0x07,
56} MSG_SNDIN;
57
58typedef struct
59{
60 IWTSVirtualChannelCallback iface;
61
62 IWTSPlugin* plugin;
63 IWTSVirtualChannelManager* channel_mgr;
64 IWTSVirtualChannel* channel;
65
71 AUDIO_FORMAT* formats;
72 UINT32 formats_count;
73} AUDIN_CHANNEL_CALLBACK;
74
75typedef struct
76{
77 IWTSPlugin iface;
78
79 GENERIC_LISTENER_CALLBACK* listener_callback;
80
81 /* Parsed plugin data */
82 AUDIO_FORMAT* fixed_format;
83 char* subsystem;
84 char* device_name;
85
86 /* Device interface */
87 IAudinDevice* device;
88
89 rdpContext* rdpcontext;
90 BOOL attached;
91 wStream* data;
92 AUDIO_FORMAT* format;
93 UINT32 FramesPerPacket;
94
95 FREERDP_DSP_CONTEXT* dsp_context;
96 wLog* log;
97
98 IWTSListener* listener;
99
100 BOOL initialized;
101 UINT32 version;
102 BOOL connected;
103} AUDIN_PLUGIN;
104
105static BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args);
106
107static UINT audin_channel_write_and_free(AUDIN_CHANNEL_CALLBACK* callback, wStream* out,
108 BOOL freeStream)
109{
110 if (!callback || !out)
111 return ERROR_INVALID_PARAMETER;
112
113 if (!callback->channel || !callback->channel->Write)
114 return ERROR_INTERNAL_ERROR;
115
116 Stream_SealLength(out);
117
118 const ULONG len = WINPR_ASSERTING_INT_CAST(ULONG, Stream_Length(out));
119 const UINT error =
120 callback->channel->Write(callback->channel, len, Stream_Buffer(out), nullptr);
121
122 if (freeStream)
123 Stream_Free(out, TRUE);
124
125 return error;
126}
127
133static UINT audin_process_version(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
134{
135 const UINT32 ClientVersion = SNDIN_VERSION;
136 UINT32 ServerVersion = 0;
137
138 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
139 return ERROR_INVALID_DATA;
140
141 Stream_Read_UINT32(s, ServerVersion);
142 WLog_Print(audin->log, WLOG_DEBUG, "ServerVersion=%" PRIu32 ", ClientVersion=%" PRIu32,
143 ServerVersion, ClientVersion);
144
145 /* Do not answer server packet, we do not support the channel version. */
146 if (ServerVersion > ClientVersion)
147 {
148 WLog_Print(audin->log, WLOG_WARN,
149 "Incompatible channel version server=%" PRIu32
150 ", client supports version=%" PRIu32,
151 ServerVersion, ClientVersion);
152 return CHANNEL_RC_OK;
153 }
154 audin->version = ServerVersion;
155
156 wStream* out = Stream_New(nullptr, 5);
157
158 if (!out)
159 {
160 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
161 return ERROR_OUTOFMEMORY;
162 }
163
164 Stream_Write_UINT8(out, MSG_SNDIN_VERSION);
165 Stream_Write_UINT32(out, ClientVersion);
166 return audin_channel_write_and_free(callback, out, TRUE);
167}
168
174static UINT audin_send_incoming_data_pdu(AUDIN_CHANNEL_CALLBACK* callback)
175{
176 BYTE out_data[1] = { MSG_SNDIN_DATA_INCOMING };
177
178 if (!callback || !callback->channel || !callback->channel->Write)
179 return ERROR_INTERNAL_ERROR;
180
181 return callback->channel->Write(callback->channel, 1, out_data, nullptr);
182}
183
184static UINT audin_close_device(AUDIN_PLUGIN* audin)
185{
186 WINPR_ASSERT(audin);
187 if (audin->device)
188 {
189 UINT error = CHANNEL_RC_OK;
190 IFCALLRET(audin->device->Close, error, audin->device);
191
192 if (error != CHANNEL_RC_OK)
193 {
194 WLog_ERR(TAG, "Close failed with errorcode %" PRIu32 "", error);
195 return error;
196 }
197 }
198 return CHANNEL_RC_OK;
199}
200
206static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
207{
208 UINT error = ERROR_INTERNAL_ERROR;
209 UINT32 NumFormats = 0;
210 UINT32 cbSizeFormatsPacket = 0;
211
212 WINPR_ASSERT(audin);
213 WINPR_ASSERT(callback);
214
215 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
216 return ERROR_INVALID_DATA;
217
218 Stream_Read_UINT32(s, NumFormats);
219 WLog_Print(audin->log, WLOG_DEBUG, "NumFormats %" PRIu32 "", NumFormats);
220
221 if ((NumFormats < 1) || (NumFormats > 1000))
222 {
223 WLog_Print(audin->log, WLOG_ERROR, "bad NumFormats %" PRIu32 "", NumFormats);
224 return ERROR_INVALID_DATA;
225 }
226
227 Stream_Seek_UINT32(s); /* cbSizeFormatsPacket */
228
229 const UINT error2 = audin_close_device(audin);
230 if (error2 != CHANNEL_RC_OK)
231 return error2;
232
233 audin->format = nullptr;
234 audio_formats_free(callback->formats, callback->formats_count);
235 callback->formats_count = 0;
236
237 callback->formats = audio_formats_new(NumFormats);
238
239 if (!callback->formats)
240 {
241 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
242 return ERROR_INVALID_DATA;
243 }
244
245 wStream* out = Stream_New(nullptr, 9);
246
247 if (!out)
248 {
249 error = CHANNEL_RC_NO_MEMORY;
250 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
251 goto out;
252 }
253
254 Stream_Seek(out, 9);
255
256 /* SoundFormats (variable) */
257 size_t compatible = 0;
258 for (UINT32 i = 0; i < NumFormats; i++)
259 {
260 AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
261
262 if (!audio_format_read(s, &format))
263 {
264 error = ERROR_INVALID_DATA;
265 goto out;
266 }
267
268 audio_format_print(audin->log, WLOG_DEBUG, &format);
269
270 if (!audio_format_compatible(audin->fixed_format, &format))
271 {
272 audio_format_free(&format);
273 continue;
274 }
275
276 compatible++;
277 if (freerdp_dsp_supports_format(&format, TRUE) ||
278 audin->device->FormatSupported(audin->device, &format))
279 {
280 /* Store the agreed format in the corresponding index */
281 callback->formats[callback->formats_count++] = format;
282
283 if (!audio_format_write(out, &format))
284 {
285 error = CHANNEL_RC_NO_MEMORY;
286 WLog_Print(audin->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
287 goto out;
288 }
289 }
290 else
291 {
292 audio_format_free(&format);
293 }
294 }
295
296 if (compatible == 0)
297 {
298 WLog_Print(audin->log, WLOG_WARN,
299 "server offers no format matching /microphone format:%s (%" PRIu16
300 "),rate:%" PRIu32 ",channel:%" PRIu16 ", audio input disabled",
301 audio_format_get_tag_string(audin->fixed_format->wFormatTag),
302 audin->fixed_format->wFormatTag, audin->fixed_format->nSamplesPerSec,
303 audin->fixed_format->nChannels);
304 }
305
306 if ((error = audin_send_incoming_data_pdu(callback)))
307 {
308 WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
309 goto out;
310 }
311
312 cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out);
313 Stream_ResetPosition(out);
314 Stream_Write_UINT8(out, MSG_SNDIN_FORMATS); /* Header (1 byte) */
315 Stream_Write_UINT32(out, callback->formats_count); /* NumFormats (4 bytes) */
316 Stream_Write_UINT32(out, cbSizeFormatsPacket); /* cbSizeFormatsPacket (4 bytes) */
317 if (!Stream_SetPosition(out, cbSizeFormatsPacket))
318 goto out;
319 error = audin_channel_write_and_free(callback, out, FALSE);
320out:
321
322 if (error != CHANNEL_RC_OK)
323 {
324 audin->format = nullptr;
325 audio_formats_free(callback->formats, NumFormats);
326 callback->formats = nullptr;
327 }
328
329 Stream_Free(out, TRUE);
330 return error;
331}
332
338static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
339 UINT32 NewFormat)
340{
341 WINPR_ASSERT(audin);
342 WINPR_ASSERT(callback);
343
344 wStream* out = Stream_New(nullptr, 5);
345
346 if (!out)
347 {
348 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
349 return CHANNEL_RC_OK;
350 }
351
352 Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE);
353 Stream_Write_UINT32(out, NewFormat);
354 return audin_channel_write_and_free(callback, out, TRUE);
355}
356
362static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
363 UINT32 Result)
364{
365 WINPR_ASSERT(audin);
366 WINPR_ASSERT(callback);
367
368 wStream* out = Stream_New(nullptr, 5);
369
370 if (!out)
371 {
372 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
373 return CHANNEL_RC_NO_MEMORY;
374 }
375
376 Stream_Write_UINT8(out, MSG_SNDIN_OPEN_REPLY);
377 Stream_Write_UINT32(out, Result);
378 return audin_channel_write_and_free(callback, out, TRUE);
379}
380
386static UINT audin_send_wave_data(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
387 const AUDIO_FORMAT* format, const BYTE* data, size_t size,
388 BOOL encode)
389{
390 Stream_ResetPosition(audin->data);
391
392 if (!Stream_EnsureRemainingCapacity(audin->data, 1))
393 return CHANNEL_RC_NO_MEMORY;
394
395 Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA);
396
397 if (!encode)
398 {
399 if (!Stream_EnsureRemainingCapacity(audin->data, size))
400 return CHANNEL_RC_NO_MEMORY;
401
402 Stream_Write(audin->data, data, size);
403 }
404 else
405 {
406 if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data))
407 return ERROR_INTERNAL_ERROR;
408 }
409
410 /* Did not encode anything, skip this, the codec is not ready for output. */
411 if (Stream_GetPosition(audin->data) <= 1)
412 return CHANNEL_RC_OK;
413
414 audio_format_print(audin->log, WLOG_TRACE, audin->format);
415 WLog_Print(audin->log, WLOG_TRACE, "[%" PRIuz "/%" PRIuz "]", size,
416 Stream_GetPosition(audin->data) - 1);
417
418 const UINT error = audin_send_incoming_data_pdu(callback);
419 if (error)
420 {
421 WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
422 return error;
423 }
424
425 return audin_channel_write_and_free(callback, audin->data, FALSE);
426}
427
428static UINT audin_receive_wave_data(const AUDIO_FORMAT* format, const BYTE* data, size_t size,
429 void* user_data)
430{
431 WINPR_ASSERT(format);
432
433 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data;
434
435 if (!callback)
436 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
437
438 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
439
440 if (!audin)
441 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
442
443 if (!audin->attached)
444 return CHANNEL_RC_OK;
445
446 const BOOL compatible = audio_format_compatible(format, audin->format);
447 if (compatible && audin->device->FormatSupported(audin->device, audin->format))
448 return audin_send_wave_data(audin, callback, format, data, size, FALSE);
449
450 /* Encode at most one packet of input at a time and send each packet on its own: the
451 * device may deliver any amount, and codecs that collect samples into packets (Opus)
452 * would otherwise fall behind. */
453 const size_t frame_bytes = 1ull * format->nChannels * format->wBitsPerSample / 8;
454 size_t chunk = size;
455 if ((frame_bytes > 0) && (audin->FramesPerPacket > 0) && (audin->format->nSamplesPerSec > 0))
456 {
457 const size_t frames =
458 1ull * audin->FramesPerPacket * format->nSamplesPerSec / audin->format->nSamplesPerSec;
459 if (frames > 0)
460 chunk = frames * frame_bytes;
461 }
462
463 for (size_t offset = 0; offset < size; offset += chunk)
464 {
465 const UINT error = audin_send_wave_data(audin, callback, format, &data[offset],
466 MIN(chunk, size - offset), TRUE);
467 if (error)
468 return error;
469 }
470 return CHANNEL_RC_OK;
471}
472
473static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback)
474{
475 UINT error = ERROR_INTERNAL_ERROR;
476 AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
477
478 if (!audin || !audin->device)
479 return FALSE;
480
481 format = *audin->format;
482 const BOOL supported =
483 IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
484 WLog_Print(audin->log, WLOG_DEBUG, "microphone uses %s codec",
485 audio_format_get_tag_string(format.wFormatTag));
486
487 if (!supported)
488 {
489 /* Default sample rates supported by most backends. */
490 const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 };
491 BOOL test = FALSE;
492
493 format.wFormatTag = WAVE_FORMAT_PCM;
494 format.wBitsPerSample = 16;
495 format.cbSize = 0;
496 for (size_t x = 0; x < ARRAYSIZE(samplerates); x++)
497 {
498 format.nSamplesPerSec = samplerates[x];
499 for (UINT16 y = audin->format->nChannels; y > 0; y--)
500 {
501 format.nChannels = y;
502 format.nBlockAlign = 2 * format.nChannels;
503 test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
504 if (test)
505 break;
506 }
507 if (test)
508 break;
509 }
510 if (!test)
511 return FALSE;
512 }
513
514 IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket);
515
516 if (error != CHANNEL_RC_OK)
517 {
518 WLog_ERR(TAG, "SetFormat failed with errorcode %" PRIu32 "", error);
519 return FALSE;
520 }
521
522 if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket))
523 return FALSE;
524
525 IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback);
526
527 if (error != CHANNEL_RC_OK)
528 {
529 WLog_ERR(TAG, "Open failed with errorcode %" PRIu32 "", error);
530 return FALSE;
531 }
532
533 return TRUE;
534}
535
541static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
542{
543 UINT32 initialFormat = 0;
544 UINT32 FramesPerPacket = 0;
545 UINT error = CHANNEL_RC_OK;
546
547 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
548 return ERROR_INVALID_DATA;
549
550 Stream_Read_UINT32(s, FramesPerPacket);
551 Stream_Read_UINT32(s, initialFormat);
552 WLog_Print(audin->log, WLOG_DEBUG, "FramesPerPacket=%" PRIu32 " initialFormat=%" PRIu32 "",
553 FramesPerPacket, initialFormat);
554
555 if (initialFormat >= callback->formats_count)
556 {
557 WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
558 initialFormat, callback->formats_count);
559 return ERROR_INVALID_DATA;
560 }
561
562 /* The RDP protocol field allows UINT32_MAX, but most backend API use INT32 as input parameter
563 * or do some math with it, so ensure that the value does not exceed reasonable limits */
564 if (FramesPerPacket >= INT32_MAX)
565 {
566 WLog_Print(audin->log, WLOG_ERROR, "invalid frames per packet %" PRIu32, FramesPerPacket);
567 return ERROR_INVALID_DATA;
568 }
569
570 audin->FramesPerPacket = FramesPerPacket;
571 audin->format = &callback->formats[initialFormat];
572
573 error = audin_close_device(audin);
574 if (error != CHANNEL_RC_OK)
575 return error;
576
577 if (!audin_open_device(audin, callback))
578 {
579 audin->format = nullptr;
580 return ERROR_INTERNAL_ERROR;
581 }
582
583 if ((error = audin_send_format_change_pdu(audin, callback, initialFormat)))
584 {
585 WLog_Print(audin->log, WLOG_ERROR, "audin_send_format_change_pdu failed!");
586 return error;
587 }
588
589 if ((error = audin_send_open_reply_pdu(audin, callback, 0)))
590 WLog_Print(audin->log, WLOG_ERROR, "audin_send_open_reply_pdu failed!");
591
592 return error;
593}
594
600/* Two formats of the same stream: same codec, channels, sample rate, sample size, block alignment
601 * and codec data. Only the bit rate (nAvgBytesPerSec) may differ. */
602static BOOL audin_same_stream(const AUDIO_FORMAT* a, const AUDIO_FORMAT* b)
603{
604 if (!a || !b)
605 return FALSE;
606
607 if ((a->wFormatTag != b->wFormatTag) || (a->nChannels != b->nChannels) ||
608 (a->nSamplesPerSec != b->nSamplesPerSec) || (a->nBlockAlign != b->nBlockAlign) ||
609 (a->wBitsPerSample != b->wBitsPerSample) || (a->cbSize != b->cbSize))
610 return FALSE;
611
612 if (a->cbSize == 0)
613 return TRUE;
614
615 if (!a->data || !b->data)
616 return FALSE;
617
618 return memcmp(a->data, b->data, a->cbSize) == 0;
619}
620
621static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
622 wStream* s)
623{
624 UINT32 NewFormat = 0;
625 UINT error = CHANNEL_RC_OK;
626
627 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
628 return ERROR_INVALID_DATA;
629
630 Stream_Read_UINT32(s, NewFormat);
631 WLog_Print(audin->log, WLOG_DEBUG, "NewFormat=%" PRIu32 "", NewFormat);
632
633 if (NewFormat >= callback->formats_count)
634 {
635 WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
636 NewFormat, callback->formats_count);
637 return ERROR_INVALID_DATA;
638 }
639
640 const AUDIO_FORMAT* previous = audin->format;
641 audin->format = &callback->formats[NewFormat];
642
643 /* The server follows the line by changing the bit rate of the same codec (Windows offers eight
644 * AAC formats that differ only there), several times a second on a busy session. Closing and
645 * opening the device and resetting the encoder for each change dropped the audio queued in
646 * both, and the microphone came through with gaps: within the same stream the capture and the
647 * encoder go on. */
648 if (audin->device && audin_same_stream(previous, audin->format))
649 {
650 WLog_Print(audin->log, WLOG_DEBUG, "only the bit rate changed, the capture goes on");
651 }
652 else
653 {
654 error = audin_close_device(audin);
655 if (error != CHANNEL_RC_OK)
656 return error;
657
658 if (!audin_open_device(audin, callback))
659 {
660 audin->format = nullptr;
661 return ERROR_INTERNAL_ERROR;
662 }
663 }
664
665 if ((error = audin_send_format_change_pdu(audin, callback, NewFormat)))
666 WLog_ERR(TAG, "audin_send_format_change_pdu failed!");
667
668 return error;
669}
670
676static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
677{
678 UINT error = 0;
679 BYTE MessageId = 0;
680 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
681
682 if (!callback || !data)
683 return ERROR_INVALID_PARAMETER;
684
685 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
686
687 if (!audin)
688 return ERROR_INTERNAL_ERROR;
689
690 if (!Stream_CheckAndLogRequiredLength(TAG, data, 1))
691 return ERROR_NO_DATA;
692
693 Stream_Read_UINT8(data, MessageId);
694 WLog_Print(audin->log, WLOG_DEBUG, "MessageId=0x%02" PRIx8 "", MessageId);
695
696 switch (MessageId)
697 {
698 case MSG_SNDIN_VERSION:
699 error = audin_process_version(audin, callback, data);
700 break;
701
702 case MSG_SNDIN_FORMATS:
703 error = audin_process_formats(audin, callback, data);
704 break;
705
706 case MSG_SNDIN_OPEN:
707 error = audin_process_open(audin, callback, data);
708 break;
709
710 case MSG_SNDIN_FORMATCHANGE:
711 error = audin_process_format_change(audin, callback, data);
712 break;
713
714 default:
715 WLog_Print(audin->log, WLOG_ERROR, "unknown MessageId=0x%02" PRIx8 "", MessageId);
716 error = ERROR_INVALID_DATA;
717 break;
718 }
719
720 return error;
721}
722
728static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback)
729{
730 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
731 WINPR_ASSERT(callback);
732
733 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
734 WINPR_ASSERT(audin);
735
736 UINT error = CHANNEL_RC_OK;
737 WLog_Print(audin->log, WLOG_TRACE, "...");
738
739 (void)audin_close_device(audin);
740
741 audin->format = nullptr;
742 audio_formats_free(callback->formats, callback->formats_count);
743 free(callback);
744 audin->connected = FALSE;
745 return error;
746}
747
753static UINT audin_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
754 IWTSVirtualChannel* pChannel,
755 WINPR_ATTR_UNUSED BYTE* Data,
756 WINPR_ATTR_UNUSED BOOL* pbAccept,
757 IWTSVirtualChannelCallback** ppCallback)
758{
759 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
760
761 if (!listener_callback || !listener_callback->plugin)
762 return ERROR_INTERNAL_ERROR;
763
764 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin;
765 WLog_Print(audin->log, WLOG_TRACE, "...");
766
767 if (audin->connected)
768 {
769 WLog_Print(audin->log, WLOG_ERROR, "Channel already connected, terminating.");
770 return ERROR_DEVICE_ALREADY_ATTACHED;
771 }
772
773 AUDIN_CHANNEL_CALLBACK* callback =
774 (AUDIN_CHANNEL_CALLBACK*)calloc(1, sizeof(AUDIN_CHANNEL_CALLBACK));
775
776 if (!callback)
777 {
778 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
779 return CHANNEL_RC_NO_MEMORY;
780 }
781
782 callback->iface.OnDataReceived = audin_on_data_received;
783 callback->iface.OnClose = audin_on_close;
784 callback->plugin = listener_callback->plugin;
785 callback->channel_mgr = listener_callback->channel_mgr;
786 callback->channel = pChannel;
787 *ppCallback = &callback->iface;
788 audin->connected = TRUE;
789 return CHANNEL_RC_OK;
790}
791
797static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
798{
799 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
800
801 if (!audin)
802 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
803
804 if (!pChannelMgr)
805 return ERROR_INVALID_PARAMETER;
806
807 if (audin->initialized)
808 {
809 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME);
810 return ERROR_INVALID_DATA;
811 }
812
813 WLog_Print(audin->log, WLOG_TRACE, "...");
814 audin->listener_callback =
816
817 if (!audin->listener_callback)
818 {
819 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
820 return CHANNEL_RC_NO_MEMORY;
821 }
822
823 audin->listener_callback->iface.OnNewChannelConnection = audin_on_new_channel_connection;
824 audin->listener_callback->plugin = pPlugin;
825 audin->listener_callback->channel_mgr = pChannelMgr;
826 const UINT rc = pChannelMgr->CreateListener(pChannelMgr, AUDIN_DVC_CHANNEL_NAME, 0,
827 &audin->listener_callback->iface, &audin->listener);
828
829 audin->initialized = rc == CHANNEL_RC_OK;
830 return rc;
831}
832
838static UINT audin_plugin_terminated(IWTSPlugin* pPlugin)
839{
840 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
841 UINT error = CHANNEL_RC_OK;
842
843 if (!audin)
844 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
845
846 WLog_Print(audin->log, WLOG_TRACE, "...");
847
848 if (audin->listener_callback)
849 {
850 IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr;
851 if (mgr)
852 IFCALL(mgr->DestroyListener, mgr, audin->listener);
853 }
854 audio_formats_free(audin->fixed_format, 1);
855
856 if (audin->device)
857 {
858 IFCALLRET(audin->device->Free, error, audin->device);
859
860 if (error != CHANNEL_RC_OK)
861 {
862 WLog_Print(audin->log, WLOG_ERROR, "Free failed with errorcode %" PRIu32 "", error);
863 // don't stop on error
864 }
865
866 audin->device = nullptr;
867 }
868
869 freerdp_dsp_context_free(audin->dsp_context);
870 Stream_Free(audin->data, TRUE);
871 free(audin->subsystem);
872 free(audin->device_name);
873 free(audin->listener_callback);
874 free(audin);
875 return CHANNEL_RC_OK;
876}
877
878static UINT audin_plugin_attached(IWTSPlugin* pPlugin)
879{
880 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
881 UINT error = CHANNEL_RC_OK;
882
883 if (!audin)
884 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
885
886 audin->attached = TRUE;
887 return error;
888}
889
890static UINT audin_plugin_detached(IWTSPlugin* pPlugin)
891{
892 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
893 UINT error = CHANNEL_RC_OK;
894
895 if (!audin)
896 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
897
898 audin->attached = FALSE;
899 return error;
900}
901
907static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device)
908{
909 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
910
911 WINPR_ASSERT(audin);
912
913 if (audin->device)
914 {
915 WLog_Print(audin->log, WLOG_ERROR, "existing device, abort.");
916 return ERROR_ALREADY_EXISTS;
917 }
918
919 WLog_Print(audin->log, WLOG_DEBUG, "device registered.");
920 audin->device = device;
921 return CHANNEL_RC_OK;
922}
923
929static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin, const char* name, const ADDIN_ARGV* args)
930{
931 WINPR_ASSERT(audin);
932
933 FREERDP_AUDIN_DEVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT;
934 UINT error = ERROR_INTERNAL_ERROR;
935
936 PVIRTUALCHANNELENTRY pvce =
937 freerdp_load_channel_addin_entry(AUDIN_CHANNEL_NAME, name, nullptr, 0);
938 PFREERDP_AUDIN_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_AUDIN_DEVICE_ENTRY);
939
940 if (entry == nullptr)
941 {
942 WLog_Print(audin->log, WLOG_ERROR,
943 "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
944 name);
945 return ERROR_INVALID_FUNCTION;
946 }
947
948 entryPoints.plugin = &audin->iface;
949 entryPoints.pRegisterAudinDevice = audin_register_device_plugin;
950 entryPoints.args = args;
951 entryPoints.rdpcontext = audin->rdpcontext;
952
953 error = entry(&entryPoints);
954 if (error)
955 {
956 WLog_Print(audin->log, WLOG_ERROR, "%s entry returned error %" PRIu32 ".", name, error);
957 return error;
958 }
959
960 WLog_Print(audin->log, WLOG_INFO, "Loaded %s backend for audin", name);
961 return CHANNEL_RC_OK;
962}
963
969static UINT audin_set_subsystem(AUDIN_PLUGIN* audin, const char* subsystem)
970{
971 WINPR_ASSERT(audin);
972
973 free(audin->subsystem);
974 audin->subsystem = _strdup(subsystem);
975
976 if (!audin->subsystem)
977 {
978 WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
979 return ERROR_NOT_ENOUGH_MEMORY;
980 }
981
982 return CHANNEL_RC_OK;
983}
984
990static UINT audin_set_device_name(AUDIN_PLUGIN* audin, const char* device_name)
991{
992 WINPR_ASSERT(audin);
993
994 free(audin->device_name);
995 audin->device_name = _strdup(device_name);
996
997 if (!audin->device_name)
998 {
999 WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
1000 return ERROR_NOT_ENOUGH_MEMORY;
1001 }
1002
1003 return CHANNEL_RC_OK;
1004}
1005
1006BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args)
1007{
1008 COMMAND_LINE_ARGUMENT_A audin_args[] = {
1009 { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr,
1010 "subsystem" },
1011 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" },
1012 { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", nullptr, nullptr, -1, nullptr,
1013 "format" },
1014 { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", nullptr, nullptr, -1, nullptr, "rate" },
1015 { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", nullptr, nullptr, -1, nullptr,
1016 "channel" },
1017 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
1018 };
1019
1020 if (!args || args->argc == 1)
1021 return TRUE;
1022
1023 const DWORD flags =
1024 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
1025 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_args, flags, audin,
1026 nullptr, nullptr);
1027
1028 if (status != 0)
1029 return FALSE;
1030
1031 const COMMAND_LINE_ARGUMENT_A* arg = audin_args;
1032 errno = 0;
1033
1034 do
1035 {
1036 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
1037 continue;
1038
1039 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys")
1040 {
1041 const UINT error = audin_set_subsystem(audin, arg->Value);
1042 if (error != CHANNEL_RC_OK)
1043 {
1044 WLog_Print(audin->log, WLOG_ERROR,
1045 "audin_set_subsystem failed with error %" PRIu32 "!", error);
1046 return FALSE;
1047 }
1048 }
1049 CommandLineSwitchCase(arg, "dev")
1050 {
1051 const UINT error = audin_set_device_name(audin, arg->Value);
1052 if (error != CHANNEL_RC_OK)
1053 {
1054 WLog_Print(audin->log, WLOG_ERROR,
1055 "audin_set_device_name failed with error %" PRIu32 "!", error);
1056 return FALSE;
1057 }
1058 }
1059 CommandLineSwitchCase(arg, "format")
1060 {
1061 unsigned long val = strtoul(arg->Value, nullptr, 0);
1062
1063 if ((errno != 0) || (val > UINT16_MAX))
1064 return FALSE;
1065
1066 audin->fixed_format->wFormatTag = (UINT16)val;
1067 }
1068 CommandLineSwitchCase(arg, "rate")
1069 {
1070 unsigned long val = strtoul(arg->Value, nullptr, 0);
1071
1072 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
1073 return FALSE;
1074
1075 audin->fixed_format->nSamplesPerSec = (UINT32)val;
1076 }
1077 CommandLineSwitchCase(arg, "channel")
1078 {
1079 unsigned long val = strtoul(arg->Value, nullptr, 0);
1080
1081 if ((errno != 0) || (val <= UINT16_MAX))
1082 audin->fixed_format->nChannels = (UINT16)val;
1083 }
1084 CommandLineSwitchDefault(arg)
1085 {
1086 }
1087 CommandLineSwitchEnd(arg)
1088 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
1089
1090 return TRUE;
1091}
1092
1098FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1099{
1100 struct SubsystemEntry
1101 {
1102 char* subsystem;
1103 char* device;
1104 };
1105 UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
1106 struct SubsystemEntry entries[] = {
1107#if defined(WITH_PULSE)
1108 { "pulse", "" },
1109#endif
1110#if defined(WITH_OSS)
1111 { "oss", "default" },
1112#endif
1113#if defined(WITH_ALSA)
1114 { "alsa", "default" },
1115#endif
1116#if defined(WITH_AAUDIO)
1117 { "aaudio", "default" },
1118#endif
1119#if defined(WITH_OPENSLES)
1120 { "opensles", "default" },
1121#endif
1122#if defined(WITH_WINMM)
1123 { "winmm", "default" },
1124#endif
1125#if defined(WITH_MACAUDIO)
1126 { "mac", "default" },
1127#endif
1128#if defined(WITH_IOSAUDIO)
1129 { "ios", "default" },
1130#endif
1131#if defined(WITH_SNDIO)
1132 { "sndio", "default" },
1133#endif
1134#if defined(__EMSCRIPTEN__)
1135 { "emscripten", "" },
1136#endif
1137#if defined(WITH_SDL_AUDIO)
1138 { "sdl", "" },
1139#endif
1140 { nullptr, nullptr }
1141 };
1142 struct SubsystemEntry* entry = &entries[0];
1143 WINPR_ASSERT(pEntryPoints);
1144 WINPR_ASSERT(pEntryPoints->GetPlugin);
1145 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, AUDIN_CHANNEL_NAME);
1146
1147 if (audin != nullptr)
1148 return CHANNEL_RC_ALREADY_INITIALIZED;
1149
1150 audin = (AUDIN_PLUGIN*)calloc(1, sizeof(AUDIN_PLUGIN));
1151
1152 if (!audin)
1153 {
1154 WLog_ERR(TAG, "calloc failed!");
1155 return CHANNEL_RC_NO_MEMORY;
1156 }
1157
1158 audin->log = WLog_Get(TAG);
1159 audin->data = Stream_New(nullptr, 4096);
1160 audin->fixed_format = audio_format_new();
1161
1162 if (!audin->fixed_format)
1163 goto out;
1164
1165 if (!audin->data)
1166 goto out;
1167
1168 audin->dsp_context = freerdp_dsp_context_new(TRUE);
1169
1170 if (!audin->dsp_context)
1171 goto out;
1172
1173 audin->attached = TRUE;
1174 audin->iface.Initialize = audin_plugin_initialize;
1175 audin->iface.Connected = nullptr;
1176 audin->iface.Disconnected = nullptr;
1177 audin->iface.Terminated = audin_plugin_terminated;
1178 audin->iface.Attached = audin_plugin_attached;
1179 audin->iface.Detached = audin_plugin_detached;
1180
1181 {
1182 const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints);
1183 audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1184
1185 if (args)
1186 {
1187 if (!audin_process_addin_args(audin, args))
1188 goto out;
1189 }
1190
1191 if (audin->subsystem)
1192 {
1193 if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1194 {
1195 WLog_Print(
1196 audin->log, WLOG_ERROR,
1197 "Unable to load microphone redirection subsystem %s because of error %" PRIu32
1198 "",
1199 audin->subsystem, error);
1200 goto out;
1201 }
1202 }
1203 else
1204 {
1205 while (entry && entry->subsystem && !audin->device)
1206 {
1207 if ((error = audin_set_subsystem(audin, entry->subsystem)))
1208 {
1209 WLog_Print(audin->log, WLOG_ERROR,
1210 "audin_set_subsystem for %s failed with error %" PRIu32 "!",
1211 entry->subsystem, error);
1212 }
1213 else if ((error = audin_set_device_name(audin, entry->device)))
1214 {
1215 WLog_Print(audin->log, WLOG_ERROR,
1216 "audin_set_device_name for %s failed with error %" PRIu32 "!",
1217 entry->subsystem, error);
1218 }
1219 else if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1220 {
1221 WLog_Print(audin->log, WLOG_ERROR,
1222 "audin_load_device_plugin %s failed with error %" PRIu32 "!",
1223 entry->subsystem, error);
1224 }
1225
1226 entry++;
1227 }
1228 }
1229 }
1230
1231 if (audin->device == nullptr)
1232 {
1233 /* If we have no audin device do not register plugin but still return OK or the client will
1234 * just disconnect due to a missing microphone. */
1235 WLog_Print(audin->log, WLOG_ERROR, "No microphone device could be found.");
1236 error = CHANNEL_RC_OK;
1237 goto out;
1238 }
1239
1240 error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface);
1241 if (error == CHANNEL_RC_OK)
1242 return error;
1243
1244out:
1245 audin_plugin_terminated(&audin->iface);
1246 return error;
1247}