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} AUDIN_PLUGIN;
103
104static BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args);
105
106static UINT audin_channel_write_and_free(AUDIN_CHANNEL_CALLBACK* callback, wStream* out,
107 BOOL freeStream)
108{
109 if (!callback || !out)
110 return ERROR_INVALID_PARAMETER;
111
112 if (!callback->channel || !callback->channel->Write)
113 return ERROR_INTERNAL_ERROR;
114
115 Stream_SealLength(out);
116
117 const ULONG len = WINPR_ASSERTING_INT_CAST(ULONG, Stream_Length(out));
118 const UINT error =
119 callback->channel->Write(callback->channel, len, Stream_Buffer(out), nullptr);
120
121 if (freeStream)
122 Stream_Free(out, TRUE);
123
124 return error;
125}
126
132static UINT audin_process_version(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
133{
134 const UINT32 ClientVersion = SNDIN_VERSION;
135 UINT32 ServerVersion = 0;
136
137 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
138 return ERROR_INVALID_DATA;
139
140 Stream_Read_UINT32(s, ServerVersion);
141 WLog_Print(audin->log, WLOG_DEBUG, "ServerVersion=%" PRIu32 ", ClientVersion=%" PRIu32,
142 ServerVersion, ClientVersion);
143
144 /* Do not answer server packet, we do not support the channel version. */
145 if (ServerVersion > ClientVersion)
146 {
147 WLog_Print(audin->log, WLOG_WARN,
148 "Incompatible channel version server=%" PRIu32
149 ", client supports version=%" PRIu32,
150 ServerVersion, ClientVersion);
151 return CHANNEL_RC_OK;
152 }
153 audin->version = ServerVersion;
154
155 wStream* out = Stream_New(nullptr, 5);
156
157 if (!out)
158 {
159 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
160 return ERROR_OUTOFMEMORY;
161 }
162
163 Stream_Write_UINT8(out, MSG_SNDIN_VERSION);
164 Stream_Write_UINT32(out, ClientVersion);
165 return audin_channel_write_and_free(callback, out, TRUE);
166}
167
173static UINT audin_send_incoming_data_pdu(AUDIN_CHANNEL_CALLBACK* callback)
174{
175 BYTE out_data[1] = { MSG_SNDIN_DATA_INCOMING };
176
177 if (!callback || !callback->channel || !callback->channel->Write)
178 return ERROR_INTERNAL_ERROR;
179
180 return callback->channel->Write(callback->channel, 1, out_data, nullptr);
181}
182
188static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
189{
190 UINT error = ERROR_INTERNAL_ERROR;
191 UINT32 NumFormats = 0;
192 UINT32 cbSizeFormatsPacket = 0;
193
194 WINPR_ASSERT(audin);
195 WINPR_ASSERT(callback);
196
197 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
198 return ERROR_INVALID_DATA;
199
200 Stream_Read_UINT32(s, NumFormats);
201 WLog_Print(audin->log, WLOG_DEBUG, "NumFormats %" PRIu32 "", NumFormats);
202
203 if ((NumFormats < 1) || (NumFormats > 1000))
204 {
205 WLog_Print(audin->log, WLOG_ERROR, "bad NumFormats %" PRIu32 "", NumFormats);
206 return ERROR_INVALID_DATA;
207 }
208
209 Stream_Seek_UINT32(s); /* cbSizeFormatsPacket */
210
211 audin->format = nullptr;
212 audio_formats_free(callback->formats, callback->formats_count);
213 callback->formats_count = 0;
214
215 callback->formats = audio_formats_new(NumFormats);
216
217 if (!callback->formats)
218 {
219 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
220 return ERROR_INVALID_DATA;
221 }
222
223 wStream* out = Stream_New(nullptr, 9);
224
225 if (!out)
226 {
227 error = CHANNEL_RC_NO_MEMORY;
228 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
229 goto out;
230 }
231
232 Stream_Seek(out, 9);
233
234 /* SoundFormats (variable) */
235 for (UINT32 i = 0; i < NumFormats; i++)
236 {
237 AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
238
239 if (!audio_format_read(s, &format))
240 {
241 error = ERROR_INVALID_DATA;
242 goto out;
243 }
244
245 audio_format_print(audin->log, WLOG_DEBUG, &format);
246
247 if (!audio_format_compatible(audin->fixed_format, &format))
248 {
249 audio_format_free(&format);
250 continue;
251 }
252
253 if (freerdp_dsp_supports_format(&format, TRUE) ||
254 audin->device->FormatSupported(audin->device, &format))
255 {
256 /* Store the agreed format in the corresponding index */
257 callback->formats[callback->formats_count++] = format;
258
259 if (!audio_format_write(out, &format))
260 {
261 error = CHANNEL_RC_NO_MEMORY;
262 WLog_Print(audin->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
263 goto out;
264 }
265 }
266 else
267 {
268 audio_format_free(&format);
269 }
270 }
271
272 if ((error = audin_send_incoming_data_pdu(callback)))
273 {
274 WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
275 goto out;
276 }
277
278 cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out);
279 Stream_ResetPosition(out);
280 Stream_Write_UINT8(out, MSG_SNDIN_FORMATS); /* Header (1 byte) */
281 Stream_Write_UINT32(out, callback->formats_count); /* NumFormats (4 bytes) */
282 Stream_Write_UINT32(out, cbSizeFormatsPacket); /* cbSizeFormatsPacket (4 bytes) */
283 if (!Stream_SetPosition(out, cbSizeFormatsPacket))
284 goto out;
285 error = audin_channel_write_and_free(callback, out, FALSE);
286out:
287
288 if (error != CHANNEL_RC_OK)
289 {
290 audin->format = nullptr;
291 audio_formats_free(callback->formats, NumFormats);
292 callback->formats = nullptr;
293 }
294
295 Stream_Free(out, TRUE);
296 return error;
297}
298
304static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
305 UINT32 NewFormat)
306{
307 WINPR_ASSERT(audin);
308 WINPR_ASSERT(callback);
309
310 wStream* out = Stream_New(nullptr, 5);
311
312 if (!out)
313 {
314 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
315 return CHANNEL_RC_OK;
316 }
317
318 Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE);
319 Stream_Write_UINT32(out, NewFormat);
320 return audin_channel_write_and_free(callback, out, TRUE);
321}
322
328static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
329 UINT32 Result)
330{
331 WINPR_ASSERT(audin);
332 WINPR_ASSERT(callback);
333
334 wStream* out = Stream_New(nullptr, 5);
335
336 if (!out)
337 {
338 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
339 return CHANNEL_RC_NO_MEMORY;
340 }
341
342 Stream_Write_UINT8(out, MSG_SNDIN_OPEN_REPLY);
343 Stream_Write_UINT32(out, Result);
344 return audin_channel_write_and_free(callback, out, TRUE);
345}
346
352static UINT audin_receive_wave_data(const AUDIO_FORMAT* format, const BYTE* data, size_t size,
353 void* user_data)
354{
355 WINPR_ASSERT(format);
356
357 UINT error = ERROR_INTERNAL_ERROR;
358 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data;
359
360 if (!callback)
361 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
362
363 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
364
365 if (!audin)
366 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
367
368 if (!audin->attached)
369 return CHANNEL_RC_OK;
370
371 Stream_ResetPosition(audin->data);
372
373 if (!Stream_EnsureRemainingCapacity(audin->data, 1))
374 return CHANNEL_RC_NO_MEMORY;
375
376 Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA);
377
378 const BOOL compatible = audio_format_compatible(format, audin->format);
379 if (compatible && audin->device->FormatSupported(audin->device, audin->format))
380 {
381 if (!Stream_EnsureRemainingCapacity(audin->data, size))
382 return CHANNEL_RC_NO_MEMORY;
383
384 Stream_Write(audin->data, data, size);
385 }
386 else
387 {
388 if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data))
389 return ERROR_INTERNAL_ERROR;
390 }
391
392 /* Did not encode anything, skip this, the codec is not ready for output. */
393 if (Stream_GetPosition(audin->data) <= 1)
394 return CHANNEL_RC_OK;
395
396 audio_format_print(audin->log, WLOG_TRACE, audin->format);
397 WLog_Print(audin->log, WLOG_TRACE, "[%" PRIuz "/%" PRIuz "]", size,
398 Stream_GetPosition(audin->data) - 1);
399
400 if ((error = audin_send_incoming_data_pdu(callback)))
401 {
402 WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
403 return error;
404 }
405
406 return audin_channel_write_and_free(callback, audin->data, FALSE);
407}
408
409static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback)
410{
411 UINT error = ERROR_INTERNAL_ERROR;
412 AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
413
414 if (!audin || !audin->device)
415 return FALSE;
416
417 format = *audin->format;
418 const BOOL supported =
419 IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
420 WLog_Print(audin->log, WLOG_DEBUG, "microphone uses %s codec",
421 audio_format_get_tag_string(format.wFormatTag));
422
423 if (!supported)
424 {
425 /* Default sample rates supported by most backends. */
426 const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 };
427 BOOL test = FALSE;
428
429 format.wFormatTag = WAVE_FORMAT_PCM;
430 format.wBitsPerSample = 16;
431 format.cbSize = 0;
432 for (size_t x = 0; x < ARRAYSIZE(samplerates); x++)
433 {
434 format.nSamplesPerSec = samplerates[x];
435 for (UINT16 y = audin->format->nChannels; y > 0; y--)
436 {
437 format.nChannels = y;
438 format.nBlockAlign = 2 * format.nChannels;
439 test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
440 if (test)
441 break;
442 }
443 if (test)
444 break;
445 }
446 if (!test)
447 return FALSE;
448 }
449
450 IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket);
451
452 if (error != CHANNEL_RC_OK)
453 {
454 WLog_ERR(TAG, "SetFormat failed with errorcode %" PRIu32 "", error);
455 return FALSE;
456 }
457
458 if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket))
459 return FALSE;
460
461 IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback);
462
463 if (error != CHANNEL_RC_OK)
464 {
465 WLog_ERR(TAG, "Open failed with errorcode %" PRIu32 "", error);
466 return FALSE;
467 }
468
469 return TRUE;
470}
471
477static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
478{
479 UINT32 initialFormat = 0;
480 UINT32 FramesPerPacket = 0;
481 UINT error = CHANNEL_RC_OK;
482
483 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
484 return ERROR_INVALID_DATA;
485
486 Stream_Read_UINT32(s, FramesPerPacket);
487 Stream_Read_UINT32(s, initialFormat);
488 WLog_Print(audin->log, WLOG_DEBUG, "FramesPerPacket=%" PRIu32 " initialFormat=%" PRIu32 "",
489 FramesPerPacket, initialFormat);
490 audin->FramesPerPacket = FramesPerPacket;
491
492 if (initialFormat >= callback->formats_count)
493 {
494 WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
495 initialFormat, callback->formats_count);
496 return ERROR_INVALID_DATA;
497 }
498
499 audin->format = &callback->formats[initialFormat];
500
501 if (!audin_open_device(audin, callback))
502 return ERROR_INTERNAL_ERROR;
503
504 if ((error = audin_send_format_change_pdu(audin, callback, initialFormat)))
505 {
506 WLog_Print(audin->log, WLOG_ERROR, "audin_send_format_change_pdu failed!");
507 return error;
508 }
509
510 if ((error = audin_send_open_reply_pdu(audin, callback, 0)))
511 WLog_Print(audin->log, WLOG_ERROR, "audin_send_open_reply_pdu failed!");
512
513 return error;
514}
515
521static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
522 wStream* s)
523{
524 UINT32 NewFormat = 0;
525 UINT error = CHANNEL_RC_OK;
526
527 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
528 return ERROR_INVALID_DATA;
529
530 Stream_Read_UINT32(s, NewFormat);
531 WLog_Print(audin->log, WLOG_DEBUG, "NewFormat=%" PRIu32 "", NewFormat);
532
533 if (NewFormat >= callback->formats_count)
534 {
535 WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
536 NewFormat, callback->formats_count);
537 return ERROR_INVALID_DATA;
538 }
539
540 audin->format = &callback->formats[NewFormat];
541
542 if (audin->device)
543 {
544 IFCALLRET(audin->device->Close, error, audin->device);
545
546 if (error != CHANNEL_RC_OK)
547 {
548 WLog_ERR(TAG, "Close failed with errorcode %" PRIu32 "", error);
549 return error;
550 }
551 }
552
553 if (!audin_open_device(audin, callback))
554 return ERROR_INTERNAL_ERROR;
555
556 if ((error = audin_send_format_change_pdu(audin, callback, NewFormat)))
557 WLog_ERR(TAG, "audin_send_format_change_pdu failed!");
558
559 return error;
560}
561
567static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
568{
569 UINT error = 0;
570 BYTE MessageId = 0;
571 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
572
573 if (!callback || !data)
574 return ERROR_INVALID_PARAMETER;
575
576 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
577
578 if (!audin)
579 return ERROR_INTERNAL_ERROR;
580
581 if (!Stream_CheckAndLogRequiredCapacity(TAG, data, 1))
582 return ERROR_NO_DATA;
583
584 Stream_Read_UINT8(data, MessageId);
585 WLog_Print(audin->log, WLOG_DEBUG, "MessageId=0x%02" PRIx8 "", MessageId);
586
587 switch (MessageId)
588 {
589 case MSG_SNDIN_VERSION:
590 error = audin_process_version(audin, callback, data);
591 break;
592
593 case MSG_SNDIN_FORMATS:
594 error = audin_process_formats(audin, callback, data);
595 break;
596
597 case MSG_SNDIN_OPEN:
598 error = audin_process_open(audin, callback, data);
599 break;
600
601 case MSG_SNDIN_FORMATCHANGE:
602 error = audin_process_format_change(audin, callback, data);
603 break;
604
605 default:
606 WLog_Print(audin->log, WLOG_ERROR, "unknown MessageId=0x%02" PRIx8 "", MessageId);
607 error = ERROR_INVALID_DATA;
608 break;
609 }
610
611 return error;
612}
613
619static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback)
620{
621 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
622 WINPR_ASSERT(callback);
623
624 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
625 WINPR_ASSERT(audin);
626
627 UINT error = CHANNEL_RC_OK;
628 WLog_Print(audin->log, WLOG_TRACE, "...");
629
630 if (audin->device)
631 {
632 IFCALLRET(audin->device->Close, error, audin->device);
633
634 if (error != CHANNEL_RC_OK)
635 WLog_Print(audin->log, WLOG_ERROR, "Close failed with errorcode %" PRIu32 "", error);
636 }
637
638 audin->format = nullptr;
639 audio_formats_free(callback->formats, callback->formats_count);
640 free(callback);
641 return error;
642}
643
649static UINT audin_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
650 IWTSVirtualChannel* pChannel,
651 WINPR_ATTR_UNUSED BYTE* Data,
652 WINPR_ATTR_UNUSED BOOL* pbAccept,
653 IWTSVirtualChannelCallback** ppCallback)
654{
655 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
656
657 if (!listener_callback || !listener_callback->plugin)
658 return ERROR_INTERNAL_ERROR;
659
660 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin;
661 WLog_Print(audin->log, WLOG_TRACE, "...");
662 AUDIN_CHANNEL_CALLBACK* callback =
663 (AUDIN_CHANNEL_CALLBACK*)calloc(1, sizeof(AUDIN_CHANNEL_CALLBACK));
664
665 if (!callback)
666 {
667 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
668 return CHANNEL_RC_NO_MEMORY;
669 }
670
671 callback->iface.OnDataReceived = audin_on_data_received;
672 callback->iface.OnClose = audin_on_close;
673 callback->plugin = listener_callback->plugin;
674 callback->channel_mgr = listener_callback->channel_mgr;
675 callback->channel = pChannel;
676 *ppCallback = &callback->iface;
677 return CHANNEL_RC_OK;
678}
679
685static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
686{
687 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
688
689 if (!audin)
690 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
691
692 if (!pChannelMgr)
693 return ERROR_INVALID_PARAMETER;
694
695 if (audin->initialized)
696 {
697 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME);
698 return ERROR_INVALID_DATA;
699 }
700
701 WLog_Print(audin->log, WLOG_TRACE, "...");
702 audin->listener_callback =
704
705 if (!audin->listener_callback)
706 {
707 WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
708 return CHANNEL_RC_NO_MEMORY;
709 }
710
711 audin->listener_callback->iface.OnNewChannelConnection = audin_on_new_channel_connection;
712 audin->listener_callback->plugin = pPlugin;
713 audin->listener_callback->channel_mgr = pChannelMgr;
714 const UINT rc = pChannelMgr->CreateListener(pChannelMgr, AUDIN_DVC_CHANNEL_NAME, 0,
715 &audin->listener_callback->iface, &audin->listener);
716
717 audin->initialized = rc == CHANNEL_RC_OK;
718 return rc;
719}
720
726static UINT audin_plugin_terminated(IWTSPlugin* pPlugin)
727{
728 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
729 UINT error = CHANNEL_RC_OK;
730
731 if (!audin)
732 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
733
734 WLog_Print(audin->log, WLOG_TRACE, "...");
735
736 if (audin->listener_callback)
737 {
738 IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr;
739 if (mgr)
740 IFCALL(mgr->DestroyListener, mgr, audin->listener);
741 }
742 audio_formats_free(audin->fixed_format, 1);
743
744 if (audin->device)
745 {
746 IFCALLRET(audin->device->Free, error, audin->device);
747
748 if (error != CHANNEL_RC_OK)
749 {
750 WLog_Print(audin->log, WLOG_ERROR, "Free failed with errorcode %" PRIu32 "", error);
751 // don't stop on error
752 }
753
754 audin->device = nullptr;
755 }
756
757 freerdp_dsp_context_free(audin->dsp_context);
758 Stream_Free(audin->data, TRUE);
759 free(audin->subsystem);
760 free(audin->device_name);
761 free(audin->listener_callback);
762 free(audin);
763 return CHANNEL_RC_OK;
764}
765
766static UINT audin_plugin_attached(IWTSPlugin* pPlugin)
767{
768 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
769 UINT error = CHANNEL_RC_OK;
770
771 if (!audin)
772 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
773
774 audin->attached = TRUE;
775 return error;
776}
777
778static UINT audin_plugin_detached(IWTSPlugin* pPlugin)
779{
780 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
781 UINT error = CHANNEL_RC_OK;
782
783 if (!audin)
784 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
785
786 audin->attached = FALSE;
787 return error;
788}
789
795static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device)
796{
797 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
798
799 WINPR_ASSERT(audin);
800
801 if (audin->device)
802 {
803 WLog_Print(audin->log, WLOG_ERROR, "existing device, abort.");
804 return ERROR_ALREADY_EXISTS;
805 }
806
807 WLog_Print(audin->log, WLOG_DEBUG, "device registered.");
808 audin->device = device;
809 return CHANNEL_RC_OK;
810}
811
817static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin, const char* name, const ADDIN_ARGV* args)
818{
819 WINPR_ASSERT(audin);
820
821 FREERDP_AUDIN_DEVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT;
822 UINT error = ERROR_INTERNAL_ERROR;
823
824 PVIRTUALCHANNELENTRY pvce =
825 freerdp_load_channel_addin_entry(AUDIN_CHANNEL_NAME, name, nullptr, 0);
826 PFREERDP_AUDIN_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_AUDIN_DEVICE_ENTRY);
827
828 if (entry == nullptr)
829 {
830 WLog_Print(audin->log, WLOG_ERROR,
831 "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
832 name);
833 return ERROR_INVALID_FUNCTION;
834 }
835
836 entryPoints.plugin = &audin->iface;
837 entryPoints.pRegisterAudinDevice = audin_register_device_plugin;
838 entryPoints.args = args;
839 entryPoints.rdpcontext = audin->rdpcontext;
840
841 error = entry(&entryPoints);
842 if (error)
843 {
844 WLog_Print(audin->log, WLOG_ERROR, "%s entry returned error %" PRIu32 ".", name, error);
845 return error;
846 }
847
848 WLog_Print(audin->log, WLOG_INFO, "Loaded %s backend for audin", name);
849 return CHANNEL_RC_OK;
850}
851
857static UINT audin_set_subsystem(AUDIN_PLUGIN* audin, const char* subsystem)
858{
859 WINPR_ASSERT(audin);
860
861 free(audin->subsystem);
862 audin->subsystem = _strdup(subsystem);
863
864 if (!audin->subsystem)
865 {
866 WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
867 return ERROR_NOT_ENOUGH_MEMORY;
868 }
869
870 return CHANNEL_RC_OK;
871}
872
878static UINT audin_set_device_name(AUDIN_PLUGIN* audin, const char* device_name)
879{
880 WINPR_ASSERT(audin);
881
882 free(audin->device_name);
883 audin->device_name = _strdup(device_name);
884
885 if (!audin->device_name)
886 {
887 WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
888 return ERROR_NOT_ENOUGH_MEMORY;
889 }
890
891 return CHANNEL_RC_OK;
892}
893
894BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args)
895{
896 COMMAND_LINE_ARGUMENT_A audin_args[] = {
897 { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr,
898 "subsystem" },
899 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" },
900 { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", nullptr, nullptr, -1, nullptr,
901 "format" },
902 { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", nullptr, nullptr, -1, nullptr, "rate" },
903 { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", nullptr, nullptr, -1, nullptr,
904 "channel" },
905 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
906 };
907
908 if (!args || args->argc == 1)
909 return TRUE;
910
911 const DWORD flags =
912 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
913 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_args, flags, audin,
914 nullptr, nullptr);
915
916 if (status != 0)
917 return FALSE;
918
919 const COMMAND_LINE_ARGUMENT_A* arg = audin_args;
920 errno = 0;
921
922 do
923 {
924 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
925 continue;
926
927 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys")
928 {
929 const UINT error = audin_set_subsystem(audin, arg->Value);
930 if (error != CHANNEL_RC_OK)
931 {
932 WLog_Print(audin->log, WLOG_ERROR,
933 "audin_set_subsystem failed with error %" PRIu32 "!", error);
934 return FALSE;
935 }
936 }
937 CommandLineSwitchCase(arg, "dev")
938 {
939 const UINT error = audin_set_device_name(audin, arg->Value);
940 if (error != CHANNEL_RC_OK)
941 {
942 WLog_Print(audin->log, WLOG_ERROR,
943 "audin_set_device_name failed with error %" PRIu32 "!", error);
944 return FALSE;
945 }
946 }
947 CommandLineSwitchCase(arg, "format")
948 {
949 unsigned long val = strtoul(arg->Value, nullptr, 0);
950
951 if ((errno != 0) || (val > UINT16_MAX))
952 return FALSE;
953
954 audin->fixed_format->wFormatTag = (UINT16)val;
955 }
956 CommandLineSwitchCase(arg, "rate")
957 {
958 unsigned long val = strtoul(arg->Value, nullptr, 0);
959
960 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
961 return FALSE;
962
963 audin->fixed_format->nSamplesPerSec = (UINT32)val;
964 }
965 CommandLineSwitchCase(arg, "channel")
966 {
967 unsigned long val = strtoul(arg->Value, nullptr, 0);
968
969 if ((errno != 0) || (val <= UINT16_MAX))
970 audin->fixed_format->nChannels = (UINT16)val;
971 }
972 CommandLineSwitchDefault(arg)
973 {
974 }
975 CommandLineSwitchEnd(arg)
976 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
977
978 return TRUE;
979}
980
986FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
987{
988 struct SubsystemEntry
989 {
990 char* subsystem;
991 char* device;
992 };
993 UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
994 struct SubsystemEntry entries[] = {
995#if defined(WITH_PULSE)
996 { "pulse", "" },
997#endif
998#if defined(WITH_OSS)
999 { "oss", "default" },
1000#endif
1001#if defined(WITH_ALSA)
1002 { "alsa", "default" },
1003#endif
1004#if defined(WITH_OPENSLES)
1005 { "opensles", "default" },
1006#endif
1007#if defined(WITH_WINMM)
1008 { "winmm", "default" },
1009#endif
1010#if defined(WITH_MACAUDIO)
1011 { "mac", "default" },
1012#endif
1013#if defined(WITH_IOSAUDIO)
1014 { "ios", "default" },
1015#endif
1016#if defined(WITH_SNDIO)
1017 { "sndio", "default" },
1018#endif
1019 { nullptr, nullptr }
1020 };
1021 struct SubsystemEntry* entry = &entries[0];
1022 WINPR_ASSERT(pEntryPoints);
1023 WINPR_ASSERT(pEntryPoints->GetPlugin);
1024 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, AUDIN_CHANNEL_NAME);
1025
1026 if (audin != nullptr)
1027 return CHANNEL_RC_ALREADY_INITIALIZED;
1028
1029 audin = (AUDIN_PLUGIN*)calloc(1, sizeof(AUDIN_PLUGIN));
1030
1031 if (!audin)
1032 {
1033 WLog_ERR(TAG, "calloc failed!");
1034 return CHANNEL_RC_NO_MEMORY;
1035 }
1036
1037 audin->log = WLog_Get(TAG);
1038 audin->data = Stream_New(nullptr, 4096);
1039 audin->fixed_format = audio_format_new();
1040
1041 if (!audin->fixed_format)
1042 goto out;
1043
1044 if (!audin->data)
1045 goto out;
1046
1047 audin->dsp_context = freerdp_dsp_context_new(TRUE);
1048
1049 if (!audin->dsp_context)
1050 goto out;
1051
1052 audin->attached = TRUE;
1053 audin->iface.Initialize = audin_plugin_initialize;
1054 audin->iface.Connected = nullptr;
1055 audin->iface.Disconnected = nullptr;
1056 audin->iface.Terminated = audin_plugin_terminated;
1057 audin->iface.Attached = audin_plugin_attached;
1058 audin->iface.Detached = audin_plugin_detached;
1059
1060 {
1061 const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints);
1062 audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1063
1064 if (args)
1065 {
1066 if (!audin_process_addin_args(audin, args))
1067 goto out;
1068 }
1069
1070 if (audin->subsystem)
1071 {
1072 if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1073 {
1074 WLog_Print(
1075 audin->log, WLOG_ERROR,
1076 "Unable to load microphone redirection subsystem %s because of error %" PRIu32
1077 "",
1078 audin->subsystem, error);
1079 goto out;
1080 }
1081 }
1082 else
1083 {
1084 while (entry && entry->subsystem && !audin->device)
1085 {
1086 if ((error = audin_set_subsystem(audin, entry->subsystem)))
1087 {
1088 WLog_Print(audin->log, WLOG_ERROR,
1089 "audin_set_subsystem for %s failed with error %" PRIu32 "!",
1090 entry->subsystem, error);
1091 }
1092 else if ((error = audin_set_device_name(audin, entry->device)))
1093 {
1094 WLog_Print(audin->log, WLOG_ERROR,
1095 "audin_set_device_name for %s failed with error %" PRIu32 "!",
1096 entry->subsystem, error);
1097 }
1098 else if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1099 {
1100 WLog_Print(audin->log, WLOG_ERROR,
1101 "audin_load_device_plugin %s failed with error %" PRIu32 "!",
1102 entry->subsystem, error);
1103 }
1104
1105 entry++;
1106 }
1107 }
1108 }
1109
1110 if (audin->device == nullptr)
1111 {
1112 /* If we have no audin device do not register plugin but still return OK or the client will
1113 * just disconnect due to a missing microphone. */
1114 WLog_Print(audin->log, WLOG_ERROR, "No microphone device could be found.");
1115 error = CHANNEL_RC_OK;
1116 goto out;
1117 }
1118
1119 error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface);
1120 if (error == CHANNEL_RC_OK)
1121 return error;
1122
1123out:
1124 audin_plugin_terminated(&audin->iface);
1125 return error;
1126}