23#include <freerdp/config.h>
26#include <winpr/assert.h>
32#include <winpr/cmdline.h>
33#include <winpr/wlog.h>
35#include <freerdp/addin.h>
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>
43#include "audin_main.h"
45#define SNDIN_VERSION 0x02
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,
60 IWTSVirtualChannelCallback iface;
63 IWTSVirtualChannelManager* channel_mgr;
64 IWTSVirtualChannel* channel;
73} AUDIN_CHANNEL_CALLBACK;
89 rdpContext* rdpcontext;
93 UINT32 FramesPerPacket;
95 FREERDP_DSP_CONTEXT* dsp_context;
98 IWTSListener* listener;
105static BOOL audin_process_addin_args(AUDIN_PLUGIN* audin,
const ADDIN_ARGV* args);
107static UINT audin_channel_write_and_free(AUDIN_CHANNEL_CALLBACK* callback,
wStream* out,
110 if (!callback || !out)
111 return ERROR_INVALID_PARAMETER;
113 if (!callback->channel || !callback->channel->Write)
114 return ERROR_INTERNAL_ERROR;
116 Stream_SealLength(out);
118 const ULONG len = WINPR_ASSERTING_INT_CAST(ULONG, Stream_Length(out));
120 callback->channel->Write(callback->channel, len, Stream_Buffer(out),
nullptr);
123 Stream_Free(out, TRUE);
133static UINT audin_process_version(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
wStream* s)
136 UINT32 ServerVersion = 0;
138 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
139 return ERROR_INVALID_DATA;
141 Stream_Read_UINT32(s, ServerVersion);
142 WLog_Print(audin->log, WLOG_DEBUG,
"ServerVersion=%" PRIu32
", ClientVersion=%" PRIu32,
143 ServerVersion, ClientVersion);
146 if (ServerVersion > ClientVersion)
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;
154 audin->version = ServerVersion;
156 wStream* out = Stream_New(
nullptr, 5);
160 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
161 return ERROR_OUTOFMEMORY;
164 Stream_Write_UINT8(out, MSG_SNDIN_VERSION);
165 Stream_Write_UINT32(out, ClientVersion);
166 return audin_channel_write_and_free(callback, out, TRUE);
174static UINT audin_send_incoming_data_pdu(AUDIN_CHANNEL_CALLBACK* callback)
176 BYTE out_data[1] = { MSG_SNDIN_DATA_INCOMING };
178 if (!callback || !callback->channel || !callback->channel->Write)
179 return ERROR_INTERNAL_ERROR;
181 return callback->channel->Write(callback->channel, 1, out_data,
nullptr);
184static UINT audin_close_device(AUDIN_PLUGIN* audin)
189 UINT error = CHANNEL_RC_OK;
190 IFCALLRET(audin->device->Close, error, audin->device);
192 if (error != CHANNEL_RC_OK)
194 WLog_ERR(TAG,
"Close failed with errorcode %" PRIu32
"", error);
198 return CHANNEL_RC_OK;
206static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
wStream* s)
208 UINT error = ERROR_INTERNAL_ERROR;
209 UINT32 NumFormats = 0;
210 UINT32 cbSizeFormatsPacket = 0;
213 WINPR_ASSERT(callback);
215 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
216 return ERROR_INVALID_DATA;
218 Stream_Read_UINT32(s, NumFormats);
219 WLog_Print(audin->log, WLOG_DEBUG,
"NumFormats %" PRIu32
"", NumFormats);
221 if ((NumFormats < 1) || (NumFormats > 1000))
223 WLog_Print(audin->log, WLOG_ERROR,
"bad NumFormats %" PRIu32
"", NumFormats);
224 return ERROR_INVALID_DATA;
227 Stream_Seek_UINT32(s);
229 const UINT error2 = audin_close_device(audin);
230 if (error2 != CHANNEL_RC_OK)
233 audin->format =
nullptr;
234 audio_formats_free(callback->formats, callback->formats_count);
235 callback->formats_count = 0;
237 callback->formats = audio_formats_new(NumFormats);
239 if (!callback->formats)
241 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
242 return ERROR_INVALID_DATA;
245 wStream* out = Stream_New(
nullptr, 9);
249 error = CHANNEL_RC_NO_MEMORY;
250 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
257 size_t compatible = 0;
258 for (UINT32 i = 0; i < NumFormats; i++)
262 if (!audio_format_read(s, &format))
264 error = ERROR_INVALID_DATA;
268 audio_format_print(audin->log, WLOG_DEBUG, &format);
270 if (!audio_format_compatible(audin->fixed_format, &format))
272 audio_format_free(&format);
277 if (freerdp_dsp_supports_format(&format, TRUE) ||
278 audin->device->FormatSupported(audin->device, &format))
281 callback->formats[callback->formats_count++] = format;
283 if (!audio_format_write(out, &format))
285 error = CHANNEL_RC_NO_MEMORY;
286 WLog_Print(audin->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
292 audio_format_free(&format);
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);
306 if ((error = audin_send_incoming_data_pdu(callback)))
308 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_incoming_data_pdu failed!");
312 cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out);
313 Stream_ResetPosition(out);
314 Stream_Write_UINT8(out, MSG_SNDIN_FORMATS);
315 Stream_Write_UINT32(out, callback->formats_count);
316 Stream_Write_UINT32(out, cbSizeFormatsPacket);
317 if (!Stream_SetPosition(out, cbSizeFormatsPacket))
319 error = audin_channel_write_and_free(callback, out, FALSE);
322 if (error != CHANNEL_RC_OK)
324 audin->format =
nullptr;
325 audio_formats_free(callback->formats, NumFormats);
326 callback->formats =
nullptr;
329 Stream_Free(out, TRUE);
338static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
342 WINPR_ASSERT(callback);
344 wStream* out = Stream_New(
nullptr, 5);
348 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
349 return CHANNEL_RC_OK;
352 Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE);
353 Stream_Write_UINT32(out, NewFormat);
354 return audin_channel_write_and_free(callback, out, TRUE);
362static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
366 WINPR_ASSERT(callback);
368 wStream* out = Stream_New(
nullptr, 5);
372 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
373 return CHANNEL_RC_NO_MEMORY;
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);
386static UINT audin_send_wave_data(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
387 const AUDIO_FORMAT* format,
const BYTE* data,
size_t size,
390 Stream_ResetPosition(audin->data);
392 if (!Stream_EnsureRemainingCapacity(audin->data, 1))
393 return CHANNEL_RC_NO_MEMORY;
395 Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA);
399 if (!Stream_EnsureRemainingCapacity(audin->data, size))
400 return CHANNEL_RC_NO_MEMORY;
402 Stream_Write(audin->data, data, size);
406 if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data))
407 return ERROR_INTERNAL_ERROR;
411 if (Stream_GetPosition(audin->data) <= 1)
412 return CHANNEL_RC_OK;
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);
418 const UINT error = audin_send_incoming_data_pdu(callback);
421 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_incoming_data_pdu failed!");
425 return audin_channel_write_and_free(callback, audin->data, FALSE);
428static UINT audin_receive_wave_data(
const AUDIO_FORMAT* format,
const BYTE* data,
size_t size,
431 WINPR_ASSERT(format);
433 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data;
436 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
438 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
441 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
443 if (!audin->attached)
444 return CHANNEL_RC_OK;
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);
453 const size_t frame_bytes = 1ull * format->nChannels * format->wBitsPerSample / 8;
455 if ((frame_bytes > 0) && (audin->FramesPerPacket > 0) && (audin->format->nSamplesPerSec > 0))
457 const size_t frames =
458 1ull * audin->FramesPerPacket * format->nSamplesPerSec / audin->format->nSamplesPerSec;
460 chunk = frames * frame_bytes;
463 for (
size_t offset = 0; offset < size; offset += chunk)
465 const UINT error = audin_send_wave_data(audin, callback, format, &data[offset],
466 MIN(chunk, size - offset), TRUE);
470 return CHANNEL_RC_OK;
473static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback)
475 UINT error = ERROR_INTERNAL_ERROR;
478 if (!audin || !audin->device)
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));
490 const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 };
493 format.wFormatTag = WAVE_FORMAT_PCM;
494 format.wBitsPerSample = 16;
496 for (
size_t x = 0; x < ARRAYSIZE(samplerates); x++)
498 format.nSamplesPerSec = samplerates[x];
499 for (UINT16 y = audin->format->nChannels; y > 0; y--)
501 format.nChannels = y;
502 format.nBlockAlign = 2 * format.nChannels;
503 test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
514 IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket);
516 if (error != CHANNEL_RC_OK)
518 WLog_ERR(TAG,
"SetFormat failed with errorcode %" PRIu32
"", error);
522 if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket))
525 IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback);
527 if (error != CHANNEL_RC_OK)
529 WLog_ERR(TAG,
"Open failed with errorcode %" PRIu32
"", error);
541static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
wStream* s)
543 UINT32 initialFormat = 0;
544 UINT32 FramesPerPacket = 0;
545 UINT error = CHANNEL_RC_OK;
547 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
548 return ERROR_INVALID_DATA;
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);
555 if (initialFormat >= callback->formats_count)
557 WLog_Print(audin->log, WLOG_ERROR,
"invalid format index %" PRIu32
" (total %" PRIu32
")",
558 initialFormat, callback->formats_count);
559 return ERROR_INVALID_DATA;
564 if (FramesPerPacket >= INT32_MAX)
566 WLog_Print(audin->log, WLOG_ERROR,
"invalid frames per packet %" PRIu32, FramesPerPacket);
567 return ERROR_INVALID_DATA;
570 audin->FramesPerPacket = FramesPerPacket;
571 audin->format = &callback->formats[initialFormat];
573 error = audin_close_device(audin);
574 if (error != CHANNEL_RC_OK)
577 if (!audin_open_device(audin, callback))
579 audin->format =
nullptr;
580 return ERROR_INTERNAL_ERROR;
583 if ((error = audin_send_format_change_pdu(audin, callback, initialFormat)))
585 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_format_change_pdu failed!");
589 if ((error = audin_send_open_reply_pdu(audin, callback, 0)))
590 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_open_reply_pdu failed!");
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))
615 if (!a->data || !b->data)
618 return memcmp(a->data, b->data, a->cbSize) == 0;
621static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
624 UINT32 NewFormat = 0;
625 UINT error = CHANNEL_RC_OK;
627 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
628 return ERROR_INVALID_DATA;
630 Stream_Read_UINT32(s, NewFormat);
631 WLog_Print(audin->log, WLOG_DEBUG,
"NewFormat=%" PRIu32
"", NewFormat);
633 if (NewFormat >= callback->formats_count)
635 WLog_Print(audin->log, WLOG_ERROR,
"invalid format index %" PRIu32
" (total %" PRIu32
")",
636 NewFormat, callback->formats_count);
637 return ERROR_INVALID_DATA;
641 audin->format = &callback->formats[NewFormat];
648 if (audin->device && audin_same_stream(previous, audin->format))
650 WLog_Print(audin->log, WLOG_DEBUG,
"only the bit rate changed, the capture goes on");
654 error = audin_close_device(audin);
655 if (error != CHANNEL_RC_OK)
658 if (!audin_open_device(audin, callback))
660 audin->format =
nullptr;
661 return ERROR_INTERNAL_ERROR;
665 if ((error = audin_send_format_change_pdu(audin, callback, NewFormat)))
666 WLog_ERR(TAG,
"audin_send_format_change_pdu failed!");
676static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
680 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
682 if (!callback || !data)
683 return ERROR_INVALID_PARAMETER;
685 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
688 return ERROR_INTERNAL_ERROR;
690 if (!Stream_CheckAndLogRequiredLength(TAG, data, 1))
691 return ERROR_NO_DATA;
693 Stream_Read_UINT8(data, MessageId);
694 WLog_Print(audin->log, WLOG_DEBUG,
"MessageId=0x%02" PRIx8
"", MessageId);
698 case MSG_SNDIN_VERSION:
699 error = audin_process_version(audin, callback, data);
702 case MSG_SNDIN_FORMATS:
703 error = audin_process_formats(audin, callback, data);
707 error = audin_process_open(audin, callback, data);
710 case MSG_SNDIN_FORMATCHANGE:
711 error = audin_process_format_change(audin, callback, data);
715 WLog_Print(audin->log, WLOG_ERROR,
"unknown MessageId=0x%02" PRIx8
"", MessageId);
716 error = ERROR_INVALID_DATA;
728static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback)
730 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
731 WINPR_ASSERT(callback);
733 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
736 UINT error = CHANNEL_RC_OK;
737 WLog_Print(audin->log, WLOG_TRACE,
"...");
739 (void)audin_close_device(audin);
741 audin->format =
nullptr;
742 audio_formats_free(callback->formats, callback->formats_count);
744 audin->connected = FALSE;
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)
761 if (!listener_callback || !listener_callback->plugin)
762 return ERROR_INTERNAL_ERROR;
764 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin;
765 WLog_Print(audin->log, WLOG_TRACE,
"...");
767 if (audin->connected)
769 WLog_Print(audin->log, WLOG_ERROR,
"Channel already connected, terminating.");
770 return ERROR_DEVICE_ALREADY_ATTACHED;
773 AUDIN_CHANNEL_CALLBACK* callback =
774 (AUDIN_CHANNEL_CALLBACK*)calloc(1,
sizeof(AUDIN_CHANNEL_CALLBACK));
778 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
779 return CHANNEL_RC_NO_MEMORY;
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;
797static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
799 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
802 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
805 return ERROR_INVALID_PARAMETER;
807 if (audin->initialized)
809 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME);
810 return ERROR_INVALID_DATA;
813 WLog_Print(audin->log, WLOG_TRACE,
"...");
814 audin->listener_callback =
817 if (!audin->listener_callback)
819 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
820 return CHANNEL_RC_NO_MEMORY;
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);
829 audin->initialized = rc == CHANNEL_RC_OK;
838static UINT audin_plugin_terminated(IWTSPlugin* pPlugin)
840 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
841 UINT error = CHANNEL_RC_OK;
844 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
846 WLog_Print(audin->log, WLOG_TRACE,
"...");
848 if (audin->listener_callback)
850 IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr;
852 IFCALL(mgr->DestroyListener, mgr, audin->listener);
854 audio_formats_free(audin->fixed_format, 1);
858 IFCALLRET(audin->device->Free, error, audin->device);
860 if (error != CHANNEL_RC_OK)
862 WLog_Print(audin->log, WLOG_ERROR,
"Free failed with errorcode %" PRIu32
"", error);
866 audin->device =
nullptr;
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);
875 return CHANNEL_RC_OK;
878static UINT audin_plugin_attached(IWTSPlugin* pPlugin)
880 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
881 UINT error = CHANNEL_RC_OK;
884 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
886 audin->attached = TRUE;
890static UINT audin_plugin_detached(IWTSPlugin* pPlugin)
892 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
893 UINT error = CHANNEL_RC_OK;
896 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
898 audin->attached = FALSE;
907static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device)
909 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
915 WLog_Print(audin->log, WLOG_ERROR,
"existing device, abort.");
916 return ERROR_ALREADY_EXISTS;
919 WLog_Print(audin->log, WLOG_DEBUG,
"device registered.");
920 audin->device = device;
921 return CHANNEL_RC_OK;
929static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin,
const char* name,
const ADDIN_ARGV* args)
934 UINT error = ERROR_INTERNAL_ERROR;
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);
940 if (entry ==
nullptr)
942 WLog_Print(audin->log, WLOG_ERROR,
943 "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
945 return ERROR_INVALID_FUNCTION;
948 entryPoints.plugin = &audin->iface;
949 entryPoints.pRegisterAudinDevice = audin_register_device_plugin;
950 entryPoints.args = args;
951 entryPoints.rdpcontext = audin->rdpcontext;
953 error = entry(&entryPoints);
956 WLog_Print(audin->log, WLOG_ERROR,
"%s entry returned error %" PRIu32
".", name, error);
960 WLog_Print(audin->log, WLOG_INFO,
"Loaded %s backend for audin", name);
961 return CHANNEL_RC_OK;
969static UINT audin_set_subsystem(AUDIN_PLUGIN* audin,
const char* subsystem)
973 free(audin->subsystem);
974 audin->subsystem = _strdup(subsystem);
976 if (!audin->subsystem)
978 WLog_Print(audin->log, WLOG_ERROR,
"_strdup failed!");
979 return ERROR_NOT_ENOUGH_MEMORY;
982 return CHANNEL_RC_OK;
990static UINT audin_set_device_name(AUDIN_PLUGIN* audin,
const char* device_name)
994 free(audin->device_name);
995 audin->device_name = _strdup(device_name);
997 if (!audin->device_name)
999 WLog_Print(audin->log, WLOG_ERROR,
"_strdup failed!");
1000 return ERROR_NOT_ENOUGH_MEMORY;
1003 return CHANNEL_RC_OK;
1006BOOL audin_process_addin_args(AUDIN_PLUGIN* audin,
const ADDIN_ARGV* args)
1009 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
1011 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
1012 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
1014 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
1015 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
1017 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
1020 if (!args || args->argc == 1)
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,
1036 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
1039 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
1041 const UINT error = audin_set_subsystem(audin, arg->Value);
1042 if (error != CHANNEL_RC_OK)
1044 WLog_Print(audin->log, WLOG_ERROR,
1045 "audin_set_subsystem failed with error %" PRIu32
"!", error);
1049 CommandLineSwitchCase(arg,
"dev")
1051 const UINT error = audin_set_device_name(audin, arg->Value);
1052 if (error != CHANNEL_RC_OK)
1054 WLog_Print(audin->log, WLOG_ERROR,
1055 "audin_set_device_name failed with error %" PRIu32
"!", error);
1059 CommandLineSwitchCase(arg,
"format")
1061 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1063 if ((errno != 0) || (val > UINT16_MAX))
1066 audin->fixed_format->wFormatTag = (UINT16)val;
1068 CommandLineSwitchCase(arg,
"rate")
1070 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1072 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
1075 audin->fixed_format->nSamplesPerSec = (UINT32)val;
1077 CommandLineSwitchCase(arg,
"channel")
1079 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1081 if ((errno != 0) || (val <= UINT16_MAX))
1082 audin->fixed_format->nChannels = (UINT16)val;
1084 CommandLineSwitchDefault(arg)
1087 CommandLineSwitchEnd(arg)
1088 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1098FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1100 struct SubsystemEntry
1105 UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
1106 struct SubsystemEntry entries[] = {
1107#if defined(WITH_PULSE)
1110#if defined(WITH_OSS)
1111 {
"oss",
"default" },
1113#if defined(WITH_ALSA)
1114 {
"alsa",
"default" },
1116#if defined(WITH_AAUDIO)
1117 {
"aaudio",
"default" },
1119#if defined(WITH_OPENSLES)
1120 {
"opensles",
"default" },
1122#if defined(WITH_WINMM)
1123 {
"winmm",
"default" },
1125#if defined(WITH_MACAUDIO)
1126 {
"mac",
"default" },
1128#if defined(WITH_IOSAUDIO)
1129 {
"ios",
"default" },
1131#if defined(WITH_SNDIO)
1132 {
"sndio",
"default" },
1134#if defined(__EMSCRIPTEN__)
1135 {
"emscripten",
"" },
1137#if defined(WITH_SDL_AUDIO)
1140 {
nullptr,
nullptr }
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);
1147 if (audin !=
nullptr)
1148 return CHANNEL_RC_ALREADY_INITIALIZED;
1150 audin = (AUDIN_PLUGIN*)calloc(1,
sizeof(AUDIN_PLUGIN));
1154 WLog_ERR(TAG,
"calloc failed!");
1155 return CHANNEL_RC_NO_MEMORY;
1158 audin->log = WLog_Get(TAG);
1159 audin->data = Stream_New(
nullptr, 4096);
1160 audin->fixed_format = audio_format_new();
1162 if (!audin->fixed_format)
1168 audin->dsp_context = freerdp_dsp_context_new(TRUE);
1170 if (!audin->dsp_context)
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;
1182 const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints);
1183 audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1187 if (!audin_process_addin_args(audin, args))
1191 if (audin->subsystem)
1193 if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1196 audin->log, WLOG_ERROR,
1197 "Unable to load microphone redirection subsystem %s because of error %" PRIu32
1199 audin->subsystem, error);
1205 while (entry && entry->subsystem && !audin->device)
1207 if ((error = audin_set_subsystem(audin, entry->subsystem)))
1209 WLog_Print(audin->log, WLOG_ERROR,
1210 "audin_set_subsystem for %s failed with error %" PRIu32
"!",
1211 entry->subsystem, error);
1213 else if ((error = audin_set_device_name(audin, entry->device)))
1215 WLog_Print(audin->log, WLOG_ERROR,
1216 "audin_set_device_name for %s failed with error %" PRIu32
"!",
1217 entry->subsystem, error);
1219 else if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1221 WLog_Print(audin->log, WLOG_ERROR,
1222 "audin_load_device_plugin %s failed with error %" PRIu32
"!",
1223 entry->subsystem, error);
1231 if (audin->device ==
nullptr)
1235 WLog_Print(audin->log, WLOG_ERROR,
"No microphone device could be found.");
1236 error = CHANNEL_RC_OK;
1240 error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface);
1241 if (error == CHANNEL_RC_OK)
1245 audin_plugin_terminated(&audin->iface);