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);
189static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
wStream* s)
191 UINT error = ERROR_INTERNAL_ERROR;
192 UINT32 NumFormats = 0;
193 UINT32 cbSizeFormatsPacket = 0;
196 WINPR_ASSERT(callback);
198 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
199 return ERROR_INVALID_DATA;
201 Stream_Read_UINT32(s, NumFormats);
202 WLog_Print(audin->log, WLOG_DEBUG,
"NumFormats %" PRIu32
"", NumFormats);
204 if ((NumFormats < 1) || (NumFormats > 1000))
206 WLog_Print(audin->log, WLOG_ERROR,
"bad NumFormats %" PRIu32
"", NumFormats);
207 return ERROR_INVALID_DATA;
210 Stream_Seek_UINT32(s);
212 audin->format =
nullptr;
213 audio_formats_free(callback->formats, callback->formats_count);
214 callback->formats_count = 0;
216 callback->formats = audio_formats_new(NumFormats);
218 if (!callback->formats)
220 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
221 return ERROR_INVALID_DATA;
224 wStream* out = Stream_New(
nullptr, 9);
228 error = CHANNEL_RC_NO_MEMORY;
229 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
236 for (UINT32 i = 0; i < NumFormats; i++)
240 if (!audio_format_read(s, &format))
242 error = ERROR_INVALID_DATA;
246 audio_format_print(audin->log, WLOG_DEBUG, &format);
248 if (!audio_format_compatible(audin->fixed_format, &format))
250 audio_format_free(&format);
254 if (freerdp_dsp_supports_format(&format, TRUE) ||
255 audin->device->FormatSupported(audin->device, &format))
258 callback->formats[callback->formats_count++] = format;
260 if (!audio_format_write(out, &format))
262 error = CHANNEL_RC_NO_MEMORY;
263 WLog_Print(audin->log, WLOG_ERROR,
"Stream_EnsureRemainingCapacity failed!");
269 audio_format_free(&format);
273 if ((error = audin_send_incoming_data_pdu(callback)))
275 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_incoming_data_pdu failed!");
279 cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out);
280 Stream_ResetPosition(out);
281 Stream_Write_UINT8(out, MSG_SNDIN_FORMATS);
282 Stream_Write_UINT32(out, callback->formats_count);
283 Stream_Write_UINT32(out, cbSizeFormatsPacket);
284 if (!Stream_SetPosition(out, cbSizeFormatsPacket))
286 error = audin_channel_write_and_free(callback, out, FALSE);
289 if (error != CHANNEL_RC_OK)
291 audin->format =
nullptr;
292 audio_formats_free(callback->formats, NumFormats);
293 callback->formats =
nullptr;
296 Stream_Free(out, TRUE);
305static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
309 WINPR_ASSERT(callback);
311 wStream* out = Stream_New(
nullptr, 5);
315 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
316 return CHANNEL_RC_OK;
319 Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE);
320 Stream_Write_UINT32(out, NewFormat);
321 return audin_channel_write_and_free(callback, out, TRUE);
329static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
333 WINPR_ASSERT(callback);
335 wStream* out = Stream_New(
nullptr, 5);
339 WLog_Print(audin->log, WLOG_ERROR,
"Stream_New failed!");
340 return CHANNEL_RC_NO_MEMORY;
343 Stream_Write_UINT8(out, MSG_SNDIN_OPEN_REPLY);
344 Stream_Write_UINT32(out, Result);
345 return audin_channel_write_and_free(callback, out, TRUE);
353static UINT audin_send_wave_data(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
354 const AUDIO_FORMAT* format,
const BYTE* data,
size_t size,
357 Stream_ResetPosition(audin->data);
359 if (!Stream_EnsureRemainingCapacity(audin->data, 1))
360 return CHANNEL_RC_NO_MEMORY;
362 Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA);
366 if (!Stream_EnsureRemainingCapacity(audin->data, size))
367 return CHANNEL_RC_NO_MEMORY;
369 Stream_Write(audin->data, data, size);
373 if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data))
374 return ERROR_INTERNAL_ERROR;
378 if (Stream_GetPosition(audin->data) <= 1)
379 return CHANNEL_RC_OK;
381 audio_format_print(audin->log, WLOG_TRACE, audin->format);
382 WLog_Print(audin->log, WLOG_TRACE,
"[%" PRIuz
"/%" PRIuz
"]", size,
383 Stream_GetPosition(audin->data) - 1);
385 const UINT error = audin_send_incoming_data_pdu(callback);
388 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_incoming_data_pdu failed!");
392 return audin_channel_write_and_free(callback, audin->data, FALSE);
395static UINT audin_receive_wave_data(
const AUDIO_FORMAT* format,
const BYTE* data,
size_t size,
398 WINPR_ASSERT(format);
400 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data;
403 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
405 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
408 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
410 if (!audin->attached)
411 return CHANNEL_RC_OK;
413 const BOOL compatible = audio_format_compatible(format, audin->format);
414 if (compatible && audin->device->FormatSupported(audin->device, audin->format))
415 return audin_send_wave_data(audin, callback, format, data, size, FALSE);
420 const size_t frame_bytes = 1ull * format->nChannels * format->wBitsPerSample / 8;
422 if ((frame_bytes > 0) && (audin->FramesPerPacket > 0) && (audin->format->nSamplesPerSec > 0))
424 const size_t frames =
425 1ull * audin->FramesPerPacket * format->nSamplesPerSec / audin->format->nSamplesPerSec;
427 chunk = frames * frame_bytes;
430 for (
size_t offset = 0; offset < size; offset += chunk)
432 const UINT error = audin_send_wave_data(audin, callback, format, &data[offset],
433 MIN(chunk, size - offset), TRUE);
437 return CHANNEL_RC_OK;
440static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback)
442 UINT error = ERROR_INTERNAL_ERROR;
445 if (!audin || !audin->device)
448 format = *audin->format;
449 const BOOL supported =
450 IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
451 WLog_Print(audin->log, WLOG_DEBUG,
"microphone uses %s codec",
452 audio_format_get_tag_string(format.wFormatTag));
457 const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 };
460 format.wFormatTag = WAVE_FORMAT_PCM;
461 format.wBitsPerSample = 16;
463 for (
size_t x = 0; x < ARRAYSIZE(samplerates); x++)
465 format.nSamplesPerSec = samplerates[x];
466 for (UINT16 y = audin->format->nChannels; y > 0; y--)
468 format.nChannels = y;
469 format.nBlockAlign = 2 * format.nChannels;
470 test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
481 IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket);
483 if (error != CHANNEL_RC_OK)
485 WLog_ERR(TAG,
"SetFormat failed with errorcode %" PRIu32
"", error);
489 if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket))
492 IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback);
494 if (error != CHANNEL_RC_OK)
496 WLog_ERR(TAG,
"Open failed with errorcode %" PRIu32
"", error);
508static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
wStream* s)
510 UINT32 initialFormat = 0;
511 UINT32 FramesPerPacket = 0;
512 UINT error = CHANNEL_RC_OK;
514 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
515 return ERROR_INVALID_DATA;
517 Stream_Read_UINT32(s, FramesPerPacket);
518 Stream_Read_UINT32(s, initialFormat);
519 WLog_Print(audin->log, WLOG_DEBUG,
"FramesPerPacket=%" PRIu32
" initialFormat=%" PRIu32
"",
520 FramesPerPacket, initialFormat);
522 if (initialFormat >= callback->formats_count)
524 WLog_Print(audin->log, WLOG_ERROR,
"invalid format index %" PRIu32
" (total %" PRIu32
")",
525 initialFormat, callback->formats_count);
526 return ERROR_INVALID_DATA;
531 if (FramesPerPacket >= INT32_MAX)
533 WLog_Print(audin->log, WLOG_ERROR,
"invalid frames per packet %" PRIu32, FramesPerPacket);
534 return ERROR_INVALID_DATA;
537 audin->FramesPerPacket = FramesPerPacket;
538 audin->format = &callback->formats[initialFormat];
540 if (!audin_open_device(audin, callback))
542 audin->format =
nullptr;
543 return ERROR_INTERNAL_ERROR;
546 if ((error = audin_send_format_change_pdu(audin, callback, initialFormat)))
548 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_format_change_pdu failed!");
552 if ((error = audin_send_open_reply_pdu(audin, callback, 0)))
553 WLog_Print(audin->log, WLOG_ERROR,
"audin_send_open_reply_pdu failed!");
570 if ((a->wFormatTag != b->wFormatTag) || (a->nChannels != b->nChannels) ||
571 (a->nSamplesPerSec != b->nSamplesPerSec) || (a->nBlockAlign != b->nBlockAlign) ||
572 (a->wBitsPerSample != b->wBitsPerSample) || (a->cbSize != b->cbSize))
578 if (!a->data || !b->data)
581 return memcmp(a->data, b->data, a->cbSize) == 0;
584static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
587 UINT32 NewFormat = 0;
588 UINT error = CHANNEL_RC_OK;
590 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
591 return ERROR_INVALID_DATA;
593 Stream_Read_UINT32(s, NewFormat);
594 WLog_Print(audin->log, WLOG_DEBUG,
"NewFormat=%" PRIu32
"", NewFormat);
596 if (NewFormat >= callback->formats_count)
598 WLog_Print(audin->log, WLOG_ERROR,
"invalid format index %" PRIu32
" (total %" PRIu32
")",
599 NewFormat, callback->formats_count);
600 return ERROR_INVALID_DATA;
604 audin->format = &callback->formats[NewFormat];
611 if (audin->device && audin_same_stream(previous, audin->format))
613 WLog_Print(audin->log, WLOG_DEBUG,
"only the bit rate changed, the capture goes on");
619 IFCALLRET(audin->device->Close, error, audin->device);
621 if (error != CHANNEL_RC_OK)
623 WLog_ERR(TAG,
"Close failed with errorcode %" PRIu32
"", error);
628 if (!audin_open_device(audin, callback))
630 audin->format =
nullptr;
631 return ERROR_INTERNAL_ERROR;
635 if ((error = audin_send_format_change_pdu(audin, callback, NewFormat)))
636 WLog_ERR(TAG,
"audin_send_format_change_pdu failed!");
646static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
650 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
652 if (!callback || !data)
653 return ERROR_INVALID_PARAMETER;
655 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
658 return ERROR_INTERNAL_ERROR;
660 if (!Stream_CheckAndLogRequiredLength(TAG, data, 1))
661 return ERROR_NO_DATA;
663 Stream_Read_UINT8(data, MessageId);
664 WLog_Print(audin->log, WLOG_DEBUG,
"MessageId=0x%02" PRIx8
"", MessageId);
668 case MSG_SNDIN_VERSION:
669 error = audin_process_version(audin, callback, data);
672 case MSG_SNDIN_FORMATS:
673 error = audin_process_formats(audin, callback, data);
677 error = audin_process_open(audin, callback, data);
680 case MSG_SNDIN_FORMATCHANGE:
681 error = audin_process_format_change(audin, callback, data);
685 WLog_Print(audin->log, WLOG_ERROR,
"unknown MessageId=0x%02" PRIx8
"", MessageId);
686 error = ERROR_INVALID_DATA;
698static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback)
700 AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
701 WINPR_ASSERT(callback);
703 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
706 UINT error = CHANNEL_RC_OK;
707 WLog_Print(audin->log, WLOG_TRACE,
"...");
711 IFCALLRET(audin->device->Close, error, audin->device);
713 if (error != CHANNEL_RC_OK)
714 WLog_Print(audin->log, WLOG_ERROR,
"Close failed with errorcode %" PRIu32
"", error);
717 audin->format =
nullptr;
718 audio_formats_free(callback->formats, callback->formats_count);
720 audin->connected = FALSE;
729static UINT audin_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
730 IWTSVirtualChannel* pChannel,
731 WINPR_ATTR_UNUSED BYTE* Data,
732 WINPR_ATTR_UNUSED BOOL* pbAccept,
733 IWTSVirtualChannelCallback** ppCallback)
737 if (!listener_callback || !listener_callback->plugin)
738 return ERROR_INTERNAL_ERROR;
740 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin;
741 WLog_Print(audin->log, WLOG_TRACE,
"...");
743 if (audin->connected)
745 WLog_Print(audin->log, WLOG_ERROR,
"Channel already connected, terminating.");
746 return ERROR_DEVICE_ALREADY_ATTACHED;
749 AUDIN_CHANNEL_CALLBACK* callback =
750 (AUDIN_CHANNEL_CALLBACK*)calloc(1,
sizeof(AUDIN_CHANNEL_CALLBACK));
754 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
755 return CHANNEL_RC_NO_MEMORY;
758 callback->iface.OnDataReceived = audin_on_data_received;
759 callback->iface.OnClose = audin_on_close;
760 callback->plugin = listener_callback->plugin;
761 callback->channel_mgr = listener_callback->channel_mgr;
762 callback->channel = pChannel;
763 *ppCallback = &callback->iface;
764 audin->connected = TRUE;
765 return CHANNEL_RC_OK;
773static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
775 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
778 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
781 return ERROR_INVALID_PARAMETER;
783 if (audin->initialized)
785 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME);
786 return ERROR_INVALID_DATA;
789 WLog_Print(audin->log, WLOG_TRACE,
"...");
790 audin->listener_callback =
793 if (!audin->listener_callback)
795 WLog_Print(audin->log, WLOG_ERROR,
"calloc failed!");
796 return CHANNEL_RC_NO_MEMORY;
799 audin->listener_callback->iface.OnNewChannelConnection = audin_on_new_channel_connection;
800 audin->listener_callback->plugin = pPlugin;
801 audin->listener_callback->channel_mgr = pChannelMgr;
802 const UINT rc = pChannelMgr->CreateListener(pChannelMgr, AUDIN_DVC_CHANNEL_NAME, 0,
803 &audin->listener_callback->iface, &audin->listener);
805 audin->initialized = rc == CHANNEL_RC_OK;
814static UINT audin_plugin_terminated(IWTSPlugin* pPlugin)
816 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
817 UINT error = CHANNEL_RC_OK;
820 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
822 WLog_Print(audin->log, WLOG_TRACE,
"...");
824 if (audin->listener_callback)
826 IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr;
828 IFCALL(mgr->DestroyListener, mgr, audin->listener);
830 audio_formats_free(audin->fixed_format, 1);
834 IFCALLRET(audin->device->Free, error, audin->device);
836 if (error != CHANNEL_RC_OK)
838 WLog_Print(audin->log, WLOG_ERROR,
"Free failed with errorcode %" PRIu32
"", error);
842 audin->device =
nullptr;
845 freerdp_dsp_context_free(audin->dsp_context);
846 Stream_Free(audin->data, TRUE);
847 free(audin->subsystem);
848 free(audin->device_name);
849 free(audin->listener_callback);
851 return CHANNEL_RC_OK;
854static UINT audin_plugin_attached(IWTSPlugin* pPlugin)
856 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
857 UINT error = CHANNEL_RC_OK;
860 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
862 audin->attached = TRUE;
866static UINT audin_plugin_detached(IWTSPlugin* pPlugin)
868 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
869 UINT error = CHANNEL_RC_OK;
872 return CHANNEL_RC_BAD_CHANNEL_HANDLE;
874 audin->attached = FALSE;
883static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device)
885 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
891 WLog_Print(audin->log, WLOG_ERROR,
"existing device, abort.");
892 return ERROR_ALREADY_EXISTS;
895 WLog_Print(audin->log, WLOG_DEBUG,
"device registered.");
896 audin->device = device;
897 return CHANNEL_RC_OK;
905static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin,
const char* name,
const ADDIN_ARGV* args)
910 UINT error = ERROR_INTERNAL_ERROR;
912 PVIRTUALCHANNELENTRY pvce =
913 freerdp_load_channel_addin_entry(AUDIN_CHANNEL_NAME, name,
nullptr, 0);
914 PFREERDP_AUDIN_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_AUDIN_DEVICE_ENTRY);
916 if (entry ==
nullptr)
918 WLog_Print(audin->log, WLOG_ERROR,
919 "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
921 return ERROR_INVALID_FUNCTION;
924 entryPoints.plugin = &audin->iface;
925 entryPoints.pRegisterAudinDevice = audin_register_device_plugin;
926 entryPoints.args = args;
927 entryPoints.rdpcontext = audin->rdpcontext;
929 error = entry(&entryPoints);
932 WLog_Print(audin->log, WLOG_ERROR,
"%s entry returned error %" PRIu32
".", name, error);
936 WLog_Print(audin->log, WLOG_INFO,
"Loaded %s backend for audin", name);
937 return CHANNEL_RC_OK;
945static UINT audin_set_subsystem(AUDIN_PLUGIN* audin,
const char* subsystem)
949 free(audin->subsystem);
950 audin->subsystem = _strdup(subsystem);
952 if (!audin->subsystem)
954 WLog_Print(audin->log, WLOG_ERROR,
"_strdup failed!");
955 return ERROR_NOT_ENOUGH_MEMORY;
958 return CHANNEL_RC_OK;
966static UINT audin_set_device_name(AUDIN_PLUGIN* audin,
const char* device_name)
970 free(audin->device_name);
971 audin->device_name = _strdup(device_name);
973 if (!audin->device_name)
975 WLog_Print(audin->log, WLOG_ERROR,
"_strdup failed!");
976 return ERROR_NOT_ENOUGH_MEMORY;
979 return CHANNEL_RC_OK;
982BOOL audin_process_addin_args(AUDIN_PLUGIN* audin,
const ADDIN_ARGV* args)
985 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
987 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
988 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
990 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
991 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
993 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
996 if (!args || args->argc == 1)
1000 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
1001 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_args, flags, audin,
1012 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
1015 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
1017 const UINT error = audin_set_subsystem(audin, arg->Value);
1018 if (error != CHANNEL_RC_OK)
1020 WLog_Print(audin->log, WLOG_ERROR,
1021 "audin_set_subsystem failed with error %" PRIu32
"!", error);
1025 CommandLineSwitchCase(arg,
"dev")
1027 const UINT error = audin_set_device_name(audin, arg->Value);
1028 if (error != CHANNEL_RC_OK)
1030 WLog_Print(audin->log, WLOG_ERROR,
1031 "audin_set_device_name failed with error %" PRIu32
"!", error);
1035 CommandLineSwitchCase(arg,
"format")
1037 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1039 if ((errno != 0) || (val > UINT16_MAX))
1042 audin->fixed_format->wFormatTag = (UINT16)val;
1044 CommandLineSwitchCase(arg,
"rate")
1046 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1048 if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
1051 audin->fixed_format->nSamplesPerSec = (UINT32)val;
1053 CommandLineSwitchCase(arg,
"channel")
1055 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1057 if ((errno != 0) || (val <= UINT16_MAX))
1058 audin->fixed_format->nChannels = (UINT16)val;
1060 CommandLineSwitchDefault(arg)
1063 CommandLineSwitchEnd(arg)
1064 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1074FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1076 struct SubsystemEntry
1081 UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
1082 struct SubsystemEntry entries[] = {
1083#if defined(WITH_PULSE)
1086#if defined(WITH_OSS)
1087 {
"oss",
"default" },
1089#if defined(WITH_ALSA)
1090 {
"alsa",
"default" },
1092#if defined(WITH_AAUDIO)
1093 {
"aaudio",
"default" },
1095#if defined(WITH_OPENSLES)
1096 {
"opensles",
"default" },
1098#if defined(WITH_WINMM)
1099 {
"winmm",
"default" },
1101#if defined(WITH_MACAUDIO)
1102 {
"mac",
"default" },
1104#if defined(WITH_IOSAUDIO)
1105 {
"ios",
"default" },
1107#if defined(WITH_SNDIO)
1108 {
"sndio",
"default" },
1110 {
nullptr,
nullptr }
1112 struct SubsystemEntry* entry = &entries[0];
1113 WINPR_ASSERT(pEntryPoints);
1114 WINPR_ASSERT(pEntryPoints->GetPlugin);
1115 AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, AUDIN_CHANNEL_NAME);
1117 if (audin !=
nullptr)
1118 return CHANNEL_RC_ALREADY_INITIALIZED;
1120 audin = (AUDIN_PLUGIN*)calloc(1,
sizeof(AUDIN_PLUGIN));
1124 WLog_ERR(TAG,
"calloc failed!");
1125 return CHANNEL_RC_NO_MEMORY;
1128 audin->log = WLog_Get(TAG);
1129 audin->data = Stream_New(
nullptr, 4096);
1130 audin->fixed_format = audio_format_new();
1132 if (!audin->fixed_format)
1138 audin->dsp_context = freerdp_dsp_context_new(TRUE);
1140 if (!audin->dsp_context)
1143 audin->attached = TRUE;
1144 audin->iface.Initialize = audin_plugin_initialize;
1145 audin->iface.Connected =
nullptr;
1146 audin->iface.Disconnected =
nullptr;
1147 audin->iface.Terminated = audin_plugin_terminated;
1148 audin->iface.Attached = audin_plugin_attached;
1149 audin->iface.Detached = audin_plugin_detached;
1152 const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints);
1153 audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1157 if (!audin_process_addin_args(audin, args))
1161 if (audin->subsystem)
1163 if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1166 audin->log, WLOG_ERROR,
1167 "Unable to load microphone redirection subsystem %s because of error %" PRIu32
1169 audin->subsystem, error);
1175 while (entry && entry->subsystem && !audin->device)
1177 if ((error = audin_set_subsystem(audin, entry->subsystem)))
1179 WLog_Print(audin->log, WLOG_ERROR,
1180 "audin_set_subsystem for %s failed with error %" PRIu32
"!",
1181 entry->subsystem, error);
1183 else if ((error = audin_set_device_name(audin, entry->device)))
1185 WLog_Print(audin->log, WLOG_ERROR,
1186 "audin_set_device_name for %s failed with error %" PRIu32
"!",
1187 entry->subsystem, error);
1189 else if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1191 WLog_Print(audin->log, WLOG_ERROR,
1192 "audin_load_device_plugin %s failed with error %" PRIu32
"!",
1193 entry->subsystem, error);
1201 if (audin->device ==
nullptr)
1205 WLog_Print(audin->log, WLOG_ERROR,
"No microphone device could be found.");
1206 error = CHANNEL_RC_OK;
1210 error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface);
1211 if (error == CHANNEL_RC_OK)
1215 audin_plugin_terminated(&audin->iface);