25#include <freerdp/config.h>
38#include <winpr/assert.h>
39#include <winpr/wlog.h>
40#include <winpr/stream.h>
41#include <winpr/cmdline.h>
42#include <winpr/sysinfo.h>
43#include <winpr/collections.h>
45#include <freerdp/types.h>
46#include <freerdp/addin.h>
47#include <freerdp/freerdp.h>
48#include <freerdp/codec/dsp.h>
49#include <freerdp/client/channels.h>
51#include "rdpsnd_common.h"
52#include "rdpsnd_main.h"
57 IWTSListener* listener;
73 UINT16 wCurrentFormatNo;
76 UINT16 NumberOfServerFormats;
79 UINT16 NumberOfClientFormats;
102 rdpsndDevicePlugin* device;
103 rdpContext* rdpcontext;
105 FREERDP_DSP_CONTEXT* dsp_context;
108 wMessageQueue* queue;
120static DWORD WINAPI play_thread(LPVOID arg);
122static const char* rdpsnd_is_dyn_str(BOOL dynamic)
129static void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd);
136static UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s);
143static UINT rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd)
146 WINPR_ASSERT(rdpsnd);
147 pdu = Stream_New(
nullptr, 8);
151 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
152 return CHANNEL_RC_NO_MEMORY;
155 Stream_Write_UINT8(pdu, SNDC_QUALITYMODE);
156 Stream_Write_UINT8(pdu, 0);
157 Stream_Write_UINT16(pdu, 4);
158 Stream_Write_UINT16(pdu, rdpsnd->wQualityMode);
159 Stream_Write_UINT16(pdu, 0);
160 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s QualityMode: %" PRIu16
"",
161 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->wQualityMode);
162 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
165static void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd)
167 WINPR_ASSERT(rdpsnd);
168 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
169 rdpsnd->NumberOfClientFormats = 0;
170 rdpsnd->ClientFormats =
nullptr;
172 if (!rdpsnd->NumberOfServerFormats)
175 rdpsnd->ClientFormats = audio_formats_new(rdpsnd->NumberOfServerFormats);
177 if (!rdpsnd->ClientFormats || !rdpsnd->device)
180 for (UINT16 index = 0; index < rdpsnd->NumberOfServerFormats; index++)
182 const AUDIO_FORMAT* serverFormat = &rdpsnd->ServerFormats[index];
184 if (!audio_format_compatible(rdpsnd->fixed_format, serverFormat))
187 WINPR_ASSERT(rdpsnd->device->FormatSupported);
188 if (freerdp_dsp_supports_format(serverFormat, FALSE) ||
189 rdpsnd->device->FormatSupported(rdpsnd->device, serverFormat))
191 AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++];
192 audio_format_copy(serverFormat, clientFormat);
202static UINT rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd)
207 UINT16 wNumberOfFormats = 0;
208 WINPR_ASSERT(rdpsnd);
210 if (!rdpsnd->device || (!rdpsnd->dynamic && (rdpsnd->OpenHandle == 0)))
211 return CHANNEL_RC_INITIALIZATION_ERROR;
213 dwVolume = IFCALLRESULT(0, rdpsnd->device->GetVolume, rdpsnd->device);
214 wNumberOfFormats = rdpsnd->NumberOfClientFormats;
217 for (UINT16 index = 0; index < wNumberOfFormats; index++)
218 length += (18 + rdpsnd->ClientFormats[index].cbSize);
224 if (length > UINT16_MAX)
225 return ERROR_INVALID_DATA;
227 pdu = Stream_New(
nullptr, length);
231 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
232 return CHANNEL_RC_NO_MEMORY;
235 Stream_Write_UINT8(pdu, SNDC_FORMATS);
236 Stream_Write_UINT8(pdu, 0);
237 Stream_Write_UINT16(pdu, (UINT16)(length - 4));
238 Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME);
239 Stream_Write_UINT32(pdu, dwVolume);
240 Stream_Write_UINT32(pdu, 0);
241 Stream_Write_UINT16(pdu, 0);
242 Stream_Write_UINT16(pdu, wNumberOfFormats);
243 Stream_Write_UINT8(pdu, 0);
244 Stream_Write_UINT16(pdu, CHANNEL_VERSION_WIN_MAX);
245 Stream_Write_UINT8(pdu, 0);
247 for (UINT16 index = 0; index < wNumberOfFormats; index++)
249 const AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[index];
251 if (!audio_format_write(pdu, clientFormat))
253 Stream_Free(pdu, TRUE);
254 return ERROR_INTERNAL_ERROR;
258 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Client Audio Formats",
259 rdpsnd_is_dyn_str(rdpsnd->dynamic));
260 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
268static UINT rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
270 UINT16 wNumberOfFormats = 0;
271 UINT ret = ERROR_BAD_LENGTH;
273 WINPR_ASSERT(rdpsnd);
274 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
275 rdpsnd->NumberOfServerFormats = 0;
276 rdpsnd->ServerFormats =
nullptr;
278 if (!Stream_CheckAndLogRequiredLength(TAG, s, 30))
279 return ERROR_BAD_LENGTH;
282 Stream_Seek_UINT32(s);
283 Stream_Seek_UINT32(s);
284 Stream_Seek_UINT32(s);
285 Stream_Seek_UINT16(s);
286 Stream_Read_UINT16(s, wNumberOfFormats);
287 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
288 Stream_Read_UINT16(s, rdpsnd->wVersion);
289 Stream_Seek_UINT8(s);
290 rdpsnd->NumberOfServerFormats = wNumberOfFormats;
292 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 14ull))
293 return ERROR_BAD_LENGTH;
295 if (rdpsnd->NumberOfServerFormats > 0)
297 rdpsnd->ServerFormats = audio_formats_new(wNumberOfFormats);
299 if (!rdpsnd->ServerFormats)
300 return CHANNEL_RC_NO_MEMORY;
302 for (UINT16 index = 0; index < wNumberOfFormats; index++)
306 if (!audio_format_read(s, format))
311 WINPR_ASSERT(rdpsnd->device);
312 ret = IFCALLRESULT(CHANNEL_RC_OK, rdpsnd->device->ServerFormatAnnounce, rdpsnd->device,
313 rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
314 if (ret != CHANNEL_RC_OK)
317 rdpsnd_select_supported_audio_formats(rdpsnd);
318 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Server Audio Formats",
319 rdpsnd_is_dyn_str(rdpsnd->dynamic));
320 ret = rdpsnd_send_client_audio_formats(rdpsnd);
322 if (ret == CHANNEL_RC_OK)
324 if (rdpsnd->wVersion >= CHANNEL_VERSION_WIN_7)
325 ret = rdpsnd_send_quality_mode_pdu(rdpsnd);
330 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
331 rdpsnd->ServerFormats =
nullptr;
332 rdpsnd->NumberOfServerFormats = 0;
341static UINT rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
345 WINPR_ASSERT(rdpsnd);
346 pdu = Stream_New(
nullptr, 8);
350 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
351 return CHANNEL_RC_NO_MEMORY;
354 Stream_Write_UINT8(pdu, SNDC_TRAINING);
355 Stream_Write_UINT8(pdu, 0);
356 Stream_Write_UINT16(pdu, 4);
357 Stream_Write_UINT16(pdu, wTimeStamp);
358 Stream_Write_UINT16(pdu, wPackSize);
359 WLog_Print(rdpsnd->log, WLOG_DEBUG,
360 "%s Training Response: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
361 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
362 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
370static UINT rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
372 UINT16 wTimeStamp = 0;
373 UINT16 wPackSize = 0;
374 WINPR_ASSERT(rdpsnd);
377 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
378 return ERROR_BAD_LENGTH;
380 Stream_Read_UINT16(s, wTimeStamp);
381 Stream_Read_UINT16(s, wPackSize);
382 WLog_Print(rdpsnd->log, WLOG_DEBUG,
383 "%s Training Request: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
384 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
385 return rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
388static BOOL rdpsnd_apply_volume(rdpsndPlugin* rdpsnd)
390 WINPR_ASSERT(rdpsnd);
392 if (rdpsnd->isOpen && rdpsnd->applyVolume && rdpsnd->device)
394 BOOL rc = IFCALLRESULT(TRUE, rdpsnd->device->SetVolume, rdpsnd->device, rdpsnd->volume);
397 rdpsnd->applyVolume = FALSE;
402static BOOL rdpsnd_ensure_device_is_open(rdpsndPlugin* rdpsnd, UINT16 wFormatNo,
407 WINPR_ASSERT(format);
409 if (!rdpsnd->isOpen || (wFormatNo != rdpsnd->wCurrentFormatNo))
415 IFCALL(rdpsnd->device->Close, rdpsnd->device);
416 supported = IFCALLRESULT(FALSE, rdpsnd->device->FormatSupported, rdpsnd->device, format);
420 if (!IFCALLRESULT(FALSE, rdpsnd->device->DefaultFormat, rdpsnd->device, format,
423 deviceFormat.wFormatTag = WAVE_FORMAT_PCM;
424 deviceFormat.wBitsPerSample = 16;
425 deviceFormat.cbSize = 0;
429 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Opening device with format %s [backend %s]",
430 rdpsnd_is_dyn_str(rdpsnd->dynamic),
431 audio_format_get_tag_string(format->wFormatTag),
432 audio_format_get_tag_string(deviceFormat.wFormatTag));
433 rc = IFCALLRESULT(FALSE, rdpsnd->device->Open, rdpsnd->device, &deviceFormat,
441 if (!freerdp_dsp_context_reset(rdpsnd->dsp_context, format, 0u))
445 rdpsnd->isOpen = TRUE;
446 rdpsnd->wCurrentFormatNo = wFormatNo;
447 rdpsnd->startPlayTime = 0;
448 rdpsnd->totalPlaySize = 0;
451 return rdpsnd_apply_volume(rdpsnd);
459static UINT rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
461 UINT16 wFormatNo = 0;
463 WINPR_ASSERT(rdpsnd);
466 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 8))
467 return ERROR_BAD_LENGTH;
469 rdpsnd->wArrivalTime = GetTickCount64();
470 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
471 Stream_Read_UINT16(s, wFormatNo);
473 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
474 return ERROR_INVALID_DATA;
476 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
478 Stream_Read(s, rdpsnd->waveData, 4);
479 rdpsnd->waveDataSize = BodySize - 8;
480 format = &rdpsnd->ClientFormats[wFormatNo];
481 WLog_Print(rdpsnd->log, WLOG_DEBUG,
482 "%s WaveInfo: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
" [%s]",
483 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
484 audio_format_get_tag_string(format->wFormatTag));
486 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
487 return ERROR_INTERNAL_ERROR;
489 rdpsnd->expectingWave = TRUE;
490 return CHANNEL_RC_OK;
498static UINT rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
499 BYTE cConfirmedBlockNo)
502 WINPR_ASSERT(rdpsnd);
503 pdu = Stream_New(
nullptr, 8);
507 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
508 return CHANNEL_RC_NO_MEMORY;
511 Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM);
512 Stream_Write_UINT8(pdu, 0);
513 Stream_Write_UINT16(pdu, 4);
514 Stream_Write_UINT16(pdu, wTimeStamp);
515 Stream_Write_UINT8(pdu, cConfirmedBlockNo);
516 Stream_Write_UINT8(pdu, 0);
517 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
520static BOOL rdpsnd_detect_overrun(rdpsndPlugin* rdpsnd,
const AUDIO_FORMAT* format,
size_t size)
525 UINT32 totalDuration = 0;
526 UINT32 remainingDuration = 0;
527 UINT32 maxDuration = 0;
529 if (!rdpsnd || !format)
549 switch (format->wFormatTag)
551 case WAVE_FORMAT_PCM:
552 case WAVE_FORMAT_DVI_ADPCM:
553 case WAVE_FORMAT_ADPCM:
554 case WAVE_FORMAT_ALAW:
555 case WAVE_FORMAT_MULAW:
557 case WAVE_FORMAT_MSG723:
558 case WAVE_FORMAT_GSM610:
559 case WAVE_FORMAT_AAC_MS:
564 audio_format_print(WLog_Get(TAG), WLOG_DEBUG, format);
565 bpf = format->nChannels * format->wBitsPerSample * format->nSamplesPerSec / 8;
569 duration = (UINT32)(1000 * size / bpf);
570 totalDuration = (UINT32)(1000 * rdpsnd->totalPlaySize / bpf);
571 now = GetTickCountPrecise();
572 if (rdpsnd->startPlayTime == 0)
574 rdpsnd->startPlayTime = now;
575 rdpsnd->totalPlaySize = size;
578 else if (now - rdpsnd->startPlayTime > totalDuration + 10)
581 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer underrun by %u ms",
582 rdpsnd_is_dyn_str(rdpsnd->dynamic),
583 (UINT)(now - rdpsnd->startPlayTime - totalDuration));
584 rdpsnd->startPlayTime = now;
585 rdpsnd->totalPlaySize = size;
591 remainingDuration = totalDuration - (now - rdpsnd->startPlayTime);
594 maxDuration = duration * 2 + rdpsnd->latency;
596 if (remainingDuration + duration > maxDuration)
598 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer overrun pending %u ms dropping %u ms",
599 rdpsnd_is_dyn_str(rdpsnd->dynamic), remainingDuration, duration);
603 rdpsnd->totalPlaySize += size;
608static UINT rdpsnd_treat_wave(rdpsndPlugin* rdpsnd,
wStream* s,
size_t size)
617 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
618 return ERROR_BAD_LENGTH;
620 if (rdpsnd->wCurrentFormatNo >= rdpsnd->NumberOfClientFormats)
621 return ERROR_INTERNAL_ERROR;
628 error = rdpsnd_send_wave_confirm_pdu(rdpsnd, rdpsnd->wTimeStamp, rdpsnd->cBlockNo);
632 const BYTE* data = Stream_ConstPointer(s);
633 format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
634 WLog_Print(rdpsnd->log, WLOG_DEBUG,
635 "%s Wave: cBlockNo: %" PRIu8
" wTimeStamp: %" PRIu16
", size: %" PRIuz,
636 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, rdpsnd->wTimeStamp, size);
638 if (rdpsnd->device && rdpsnd->attached && !rdpsnd_detect_overrun(rdpsnd, format, size))
640 UINT status = CHANNEL_RC_OK;
641 wStream* pcmData = StreamPool_Take(rdpsnd->pool, 4096);
643 if (rdpsnd->device->FormatSupported(rdpsnd->device, format))
645 if (rdpsnd->device->PlayEx)
646 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, data, size);
648 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, data, size);
650 else if (freerdp_dsp_decode(rdpsnd->dsp_context, format, data, size, pcmData))
652 Stream_SealLength(pcmData);
654 if (rdpsnd->device->PlayEx)
655 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, Stream_Buffer(pcmData),
656 Stream_Length(pcmData));
658 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device,
659 Stream_Buffer(pcmData), Stream_Length(pcmData));
662 status = ERROR_INTERNAL_ERROR;
664 Stream_Release(pcmData);
666 if (status != CHANNEL_RC_OK)
670 end = GetTickCount64();
671 diffMS = end - rdpsnd->wArrivalTime + latency;
672 ts = (rdpsnd->wTimeStamp + diffMS) % UINT16_MAX;
679 return rdpsnd_send_wave_confirm_pdu(rdpsnd, (UINT16)ts, rdpsnd->cBlockNo);
687static UINT rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
689 rdpsnd->expectingWave = FALSE;
697 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
698 return ERROR_INVALID_DATA;
700 CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4);
701 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
704static UINT rdpsnd_recv_wave2_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
706 UINT16 wFormatNo = 0;
708 UINT32 dwAudioTimeStamp = 0;
710 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 12))
711 return ERROR_BAD_LENGTH;
713 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
714 Stream_Read_UINT16(s, wFormatNo);
715 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
717 Stream_Read_UINT32(s, dwAudioTimeStamp);
718 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
719 return ERROR_INVALID_DATA;
720 format = &rdpsnd->ClientFormats[wFormatNo];
721 rdpsnd->waveDataSize = BodySize - 12;
722 rdpsnd->wArrivalTime = GetTickCount64();
723 WLog_Print(rdpsnd->log, WLOG_DEBUG,
724 "%s Wave2PDU: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
725 " [%s] , align=%hu wTimeStamp=0x%04" PRIx16
", dwAudioTimeStamp=0x%08" PRIx32,
726 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
727 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign,
728 rdpsnd->wTimeStamp, dwAudioTimeStamp);
730 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
731 return ERROR_INTERNAL_ERROR;
733 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
736static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
740 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Closing device",
741 rdpsnd_is_dyn_str(rdpsnd->dynamic));
744 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Device already closed",
745 rdpsnd_is_dyn_str(rdpsnd->dynamic));
753static UINT rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
758 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
759 return ERROR_BAD_LENGTH;
761 Stream_Read_UINT32(s, dwVolume);
762 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Volume: 0x%08" PRIX32
"",
763 rdpsnd_is_dyn_str(rdpsnd->dynamic), dwVolume);
765 rdpsnd->volume = dwVolume;
766 rdpsnd->applyVolume = TRUE;
767 rc = rdpsnd_apply_volume(rdpsnd);
771 WLog_ERR(TAG,
"%s error setting volume", rdpsnd_is_dyn_str(rdpsnd->dynamic));
772 return CHANNEL_RC_INITIALIZATION_ERROR;
775 return CHANNEL_RC_OK;
783static UINT rdpsnd_recv_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
787 UINT status = CHANNEL_RC_OK;
789 if (rdpsnd->expectingWave)
791 status = rdpsnd_recv_wave_pdu(rdpsnd, s);
795 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
797 status = ERROR_BAD_LENGTH;
801 Stream_Read_UINT8(s, msgType);
802 Stream_Seek_UINT8(s);
803 Stream_Read_UINT16(s, BodySize);
808 status = rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
812 status = rdpsnd_recv_training_pdu(rdpsnd, s);
816 status = rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
820 rdpsnd_recv_close_pdu(rdpsnd);
824 status = rdpsnd_recv_volume_pdu(rdpsnd, s);
828 status = rdpsnd_recv_wave2_pdu(rdpsnd, s, BodySize);
832 WLog_ERR(TAG,
"%s unknown msgType %" PRIu8
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
842static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
846 WLog_ERR(TAG,
"%s existing device, abort.", rdpsnd_is_dyn_str(FALSE));
850 rdpsnd->device = device;
851 device->rdpsnd = rdpsnd;
859static UINT rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd,
const char* name,
864 DWORD flags = FREERDP_ADDIN_CHANNEL_STATIC | FREERDP_ADDIN_CHANNEL_ENTRYEX;
866 flags = FREERDP_ADDIN_CHANNEL_DYNAMIC;
867 PVIRTUALCHANNELENTRY pvce =
868 freerdp_load_channel_addin_entry(RDPSND_CHANNEL_NAME, name,
nullptr, flags);
869 PFREERDP_RDPSND_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_RDPSND_DEVICE_ENTRY);
872 return ERROR_INTERNAL_ERROR;
874 entryPoints.rdpsnd = rdpsnd;
875 entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
876 entryPoints.args = args;
878 error = entry(&entryPoints);
880 WLog_WARN(TAG,
"%s %s entry returns error %" PRIu32
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
883 WLog_INFO(TAG,
"%s Loaded %s backend for rdpsnd", rdpsnd_is_dyn_str(rdpsnd->dynamic), name);
888static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd,
const char* subsystem)
890 free(rdpsnd->subsystem);
891 rdpsnd->subsystem = _strdup(subsystem);
892 return (rdpsnd->subsystem !=
nullptr);
895static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd,
const char* device_name)
897 free(rdpsnd->device_name);
898 rdpsnd->device_name = _strdup(device_name);
899 return (rdpsnd->device_name !=
nullptr);
907static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd,
const ADDIN_ARGV* args)
913 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
915 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
916 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
918 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
919 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
921 {
"latency", COMMAND_LINE_VALUE_REQUIRED,
"<latency>",
nullptr,
nullptr, -1,
nullptr,
923 {
"quality", COMMAND_LINE_VALUE_REQUIRED,
"<quality mode>",
nullptr,
nullptr, -1,
nullptr,
925 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
927 rdpsnd->wQualityMode = HIGH_QUALITY;
931 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
932 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd,
936 return CHANNEL_RC_INITIALIZATION_ERROR;
943 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
946 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
948 if (!rdpsnd_set_subsystem(rdpsnd, arg->Value))
949 return CHANNEL_RC_NO_MEMORY;
951 CommandLineSwitchCase(arg,
"dev")
953 if (!rdpsnd_set_device_name(rdpsnd, arg->Value))
954 return CHANNEL_RC_NO_MEMORY;
956 CommandLineSwitchCase(arg,
"format")
958 unsigned long val = strtoul(arg->Value,
nullptr, 0);
960 if ((errno != 0) || (val > UINT16_MAX))
961 return CHANNEL_RC_INITIALIZATION_ERROR;
963 rdpsnd->fixed_format->wFormatTag = (UINT16)val;
965 CommandLineSwitchCase(arg,
"rate")
967 unsigned long val = strtoul(arg->Value,
nullptr, 0);
969 if ((errno != 0) || (val > UINT32_MAX))
970 return CHANNEL_RC_INITIALIZATION_ERROR;
972 rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val;
974 CommandLineSwitchCase(arg,
"channel")
976 unsigned long val = strtoul(arg->Value,
nullptr, 0);
978 if ((errno != 0) || (val > UINT16_MAX))
979 return CHANNEL_RC_INITIALIZATION_ERROR;
981 rdpsnd->fixed_format->nChannels = (UINT16)val;
983 CommandLineSwitchCase(arg,
"latency")
985 unsigned long val = strtoul(arg->Value,
nullptr, 0);
987 if ((errno != 0) || (val > UINT32_MAX))
988 return CHANNEL_RC_INITIALIZATION_ERROR;
990 rdpsnd->latency = (UINT32)val;
992 CommandLineSwitchCase(arg,
"quality")
994 long wQualityMode = DYNAMIC_QUALITY;
996 if (_stricmp(arg->Value,
"dynamic") == 0)
997 wQualityMode = DYNAMIC_QUALITY;
998 else if (_stricmp(arg->Value,
"medium") == 0)
999 wQualityMode = MEDIUM_QUALITY;
1000 else if (_stricmp(arg->Value,
"high") == 0)
1001 wQualityMode = HIGH_QUALITY;
1004 wQualityMode = strtol(arg->Value,
nullptr, 0);
1007 return CHANNEL_RC_INITIALIZATION_ERROR;
1010 if ((wQualityMode < 0) || (wQualityMode > 2))
1011 wQualityMode = DYNAMIC_QUALITY;
1013 rdpsnd->wQualityMode = (UINT16)wQualityMode;
1015 CommandLineSwitchDefault(arg)
1018 CommandLineSwitchEnd(arg)
1019 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1022 return CHANNEL_RC_OK;
1030static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd)
1034 const char* subsystem;
1037#if defined(WITH_IOSAUDIO)
1040#if defined(WITH_OPENSLES)
1043#if defined(WITH_PULSE)
1046#if defined(WITH_ALSA)
1047 {
"alsa",
"default" },
1049#if defined(WITH_OSS)
1052#if defined(WITH_MACAUDIO)
1053 {
"mac",
"default" },
1055#if defined(WITH_WINMM)
1058#if defined(WITH_SNDIO)
1064 UINT status = ERROR_INTERNAL_ERROR;
1065 WINPR_ASSERT(rdpsnd);
1066 rdpsnd->latency = 0;
1067 args = (
const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData;
1071 status = rdpsnd_process_addin_args(rdpsnd, args);
1073 if (status != CHANNEL_RC_OK)
1077 if (rdpsnd->subsystem)
1079 if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args)))
1082 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
"",
1083 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status);
1089 for (
size_t x = 0; x < ARRAYSIZE(backends); x++)
1091 const char* subsystem_name = backends[x].subsystem;
1092 const char* device_name = backends[x].device;
1094 if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args)))
1096 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
1098 rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status);
1100 if (!rdpsnd->device)
1103 if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) ||
1104 !rdpsnd_set_device_name(rdpsnd, device_name))
1105 return CHANNEL_RC_NO_MEMORY;
1110 if (!rdpsnd->device || status)
1111 return CHANNEL_RC_INITIALIZATION_ERROR;
1114 return CHANNEL_RC_OK;
1122UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s)
1124 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
1128 if (rdpsnd->dynamic)
1130 IWTSVirtualChannel* channel =
nullptr;
1131 if (rdpsnd->listener_callback)
1133 channel = rdpsnd->listener_callback->channel_callback->channel;
1135 channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
nullptr);
1137 Stream_Free(s, TRUE);
1141 status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx(
1142 rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s),
1143 (UINT32)Stream_GetPosition(s), s);
1145 if (status != CHANNEL_RC_OK)
1147 Stream_Free(s, TRUE);
1148 WLog_ERR(TAG,
"%s pVirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1149 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status);
1162static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin,
void* pData,
1163 UINT32 dataLength, UINT32 totalLength,
1166 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1167 return CHANNEL_RC_OK;
1169 if (dataFlags & CHANNEL_FLAG_FIRST)
1171 if (!plugin->data_in)
1172 plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
1174 Stream_ResetPosition(plugin->data_in);
1177 if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength))
1178 return CHANNEL_RC_NO_MEMORY;
1180 Stream_Write(plugin->data_in, pData, dataLength);
1182 if (dataFlags & CHANNEL_FLAG_LAST)
1184 Stream_SealLength(plugin->data_in);
1185 Stream_ResetPosition(plugin->data_in);
1189 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, plugin->data_in,
nullptr))
1190 return ERROR_INTERNAL_ERROR;
1191 plugin->data_in =
nullptr;
1195 UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in);
1196 plugin->data_in =
nullptr;
1202 return CHANNEL_RC_OK;
1205static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1206 UINT event, LPVOID pData,
1207 UINT32 dataLength, UINT32 totalLength,
1210 UINT error = CHANNEL_RC_OK;
1211 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam;
1212 WINPR_ASSERT(rdpsnd);
1213 WINPR_ASSERT(!rdpsnd->dynamic);
1217 case CHANNEL_EVENT_DATA_RECEIVED:
1221 if (rdpsnd->OpenHandle != openHandle)
1223 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic));
1226 if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength,
1227 totalLength, dataFlags)))
1229 "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32
1231 rdpsnd_is_dyn_str(rdpsnd->dynamic), error);
1235 case CHANNEL_EVENT_WRITE_CANCELLED:
1236 case CHANNEL_EVENT_WRITE_COMPLETE:
1239 Stream_Free(s, TRUE);
1243 case CHANNEL_EVENT_USER:
1249 if (error && rdpsnd && rdpsnd->rdpcontext)
1252 (void)_snprintf(buffer,
sizeof(buffer),
1253 "%s rdpsnd_virtual_channel_open_event_ex reported an error",
1254 rdpsnd_is_dyn_str(rdpsnd->dynamic));
1255 setChannelError(rdpsnd->rdpcontext, error, buffer);
1264static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData,
1269 WINPR_UNUSED(pData);
1270 WINPR_UNUSED(dataLength);
1272 WINPR_ASSERT(rdpsnd);
1273 WINPR_ASSERT(!rdpsnd->dynamic);
1275 status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx(
1276 rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex);
1278 if (status != CHANNEL_RC_OK)
1280 WLog_ERR(TAG,
"%s pVirtualChannelOpenEx failed with %s [%08" PRIX32
"]",
1281 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status);
1285 if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK)
1288 rdpsnd->OpenHandle = opened;
1289 return CHANNEL_RC_OK;
1292 rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1293 return CHANNEL_RC_NO_MEMORY;
1296static void rdpsnd_terminate_thread(rdpsndPlugin* rdpsnd)
1298 WINPR_ASSERT(rdpsnd);
1300 MessageQueue_PostQuit(rdpsnd->queue, 0);
1304 (void)WaitForSingleObject(rdpsnd->thread, INFINITE);
1305 (void)CloseHandle(rdpsnd->thread);
1308 MessageQueue_Free(rdpsnd->queue);
1309 rdpsnd->thread =
nullptr;
1310 rdpsnd->queue =
nullptr;
1313static void cleanup_internals(rdpsndPlugin* rdpsnd)
1319 StreamPool_Return(rdpsnd->pool, rdpsnd->data_in);
1321 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
1322 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
1324 rdpsnd->NumberOfClientFormats = 0;
1325 rdpsnd->ClientFormats =
nullptr;
1326 rdpsnd->NumberOfServerFormats = 0;
1327 rdpsnd->ServerFormats =
nullptr;
1329 rdpsnd->data_in =
nullptr;
1337static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd)
1341 WINPR_ASSERT(rdpsnd);
1342 WINPR_ASSERT(!rdpsnd->dynamic);
1343 if (rdpsnd->OpenHandle != 0)
1345 DWORD opened = rdpsnd->OpenHandle;
1346 rdpsnd->OpenHandle = 0;
1348 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1350 error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1352 if (CHANNEL_RC_OK != error)
1354 WLog_ERR(TAG,
"%s pVirtualChannelCloseEx failed with %s [%08" PRIX32
"]",
1355 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error);
1360 cleanup_internals(rdpsnd);
1364 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1365 rdpsnd->device =
nullptr;
1368 return CHANNEL_RC_OK;
1371static void queue_free(
void* obj)
1373 wMessage* msg = obj;
1382static void free_internals(rdpsndPlugin* rdpsnd)
1387 if (rdpsnd->references > 0)
1388 rdpsnd->references--;
1390 if (rdpsnd->references > 0)
1393 rdpsnd_terminate_thread(rdpsnd);
1394 freerdp_dsp_context_free(rdpsnd->dsp_context);
1395 StreamPool_Free(rdpsnd->pool);
1396 rdpsnd->pool =
nullptr;
1397 rdpsnd->dsp_context =
nullptr;
1400static BOOL allocate_internals(rdpsndPlugin* rdpsnd)
1402 WINPR_ASSERT(rdpsnd);
1406 rdpsnd->pool = StreamPool_New(TRUE, 4096);
1411 if (!rdpsnd->dsp_context)
1413 rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE);
1414 if (!rdpsnd->dsp_context)
1422 wObject obj = WINPR_C_ARRAY_INIT;
1425 rdpsnd->queue = MessageQueue_New(&obj);
1427 return CHANNEL_RC_NO_MEMORY;
1430 if (!rdpsnd->thread)
1432 rdpsnd->thread = CreateThread(
nullptr, 0, play_thread, rdpsnd, 0,
nullptr);
1433 if (!rdpsnd->thread)
1434 return CHANNEL_RC_INITIALIZATION_ERROR;
1438 rdpsnd->references++;
1443static DWORD WINAPI play_thread(LPVOID arg)
1445 UINT error = CHANNEL_RC_OK;
1446 rdpsndPlugin* rdpsnd = arg;
1448 if (!rdpsnd || !rdpsnd->queue)
1449 return ERROR_INVALID_PARAMETER;
1454 wMessage message = WINPR_C_ARRAY_INIT;
1458 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1460 handles[nCount++] = MessageQueue_Event(rdpsnd->queue);
1461 handles[nCount++] = freerdp_abort_event(rdpsnd->rdpcontext);
1462 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1468 return ERROR_TIMEOUT;
1471 rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE);
1475 if (message.id == WMQ_QUIT)
1479 error = rdpsnd_recv_pdu(rdpsnd, s);
1485 return CHANNEL_RC_OK;
1488static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd)
1491 return ERROR_INVALID_PARAMETER;
1493 if (!allocate_internals(rdpsnd))
1494 return CHANNEL_RC_NO_MEMORY;
1496 return CHANNEL_RC_OK;
1499void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd)
1503 free_internals(rdpsnd);
1504 audio_formats_free(rdpsnd->fixed_format, 1);
1505 free(rdpsnd->subsystem);
1506 free(rdpsnd->device_name);
1507 rdpsnd->InitHandle =
nullptr;
1513static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1514 UINT event, LPVOID pData,
1517 UINT error = CHANNEL_RC_OK;
1518 rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam;
1523 if (plugin->InitHandle != pInitHandle)
1525 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(plugin->dynamic));
1531 case CHANNEL_EVENT_INITIALIZED:
1532 error = rdpsnd_virtual_channel_event_initialized(plugin);
1535 case CHANNEL_EVENT_CONNECTED:
1536 error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength);
1539 case CHANNEL_EVENT_DISCONNECTED:
1540 error = rdpsnd_virtual_channel_event_disconnected(plugin);
1543 case CHANNEL_EVENT_TERMINATED:
1544 rdpsnd_virtual_channel_event_terminated(plugin);
1548 case CHANNEL_EVENT_ATTACHED:
1549 plugin->attached = TRUE;
1552 case CHANNEL_EVENT_DETACHED:
1553 plugin->attached = FALSE;
1560 if (error && plugin && plugin->rdpcontext)
1563 (void)_snprintf(buffer,
sizeof(buffer),
"%s reported an error",
1564 rdpsnd_is_dyn_str(plugin->dynamic));
1565 setChannelError(plugin->rdpcontext, error, buffer);
1569rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin)
1574 return plugin->rdpcontext;
1577static rdpsndPlugin* allocatePlugin(
void)
1579 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1,
sizeof(rdpsndPlugin));
1583 rdpsnd->fixed_format = audio_format_new();
1584 if (!rdpsnd->fixed_format)
1586 rdpsnd->log = WLog_Get(
"com.freerdp.channels.rdpsnd.client");
1590 rdpsnd->attached = TRUE;
1595 audio_formats_free(rdpsnd->fixed_format, 1);
1600FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx(
1601 PCHANNEL_ENTRY_POINTS_EX pEntryPoints, PVOID pInitHandle))
1604 rdpsndPlugin* rdpsnd =
nullptr;
1610 rdpsnd = allocatePlugin();
1615 rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP;
1616 (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name),
1617 RDPSND_CHANNEL_NAME);
1621 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1623 rdpsnd->rdpcontext = pEntryPointsEx->context;
1625 FreeRDP_SynchronousStaticChannels))
1626 rdpsnd->async = TRUE;
1629 CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints,
1631 rdpsnd->InitHandle = pInitHandle;
1633 WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx);
1634 rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx(
1635 rdpsnd,
nullptr, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
1636 rdpsnd_virtual_channel_init_event_ex);
1638 if (CHANNEL_RC_OK != rc)
1640 WLog_ERR(TAG,
"%s pVirtualChannelInitEx failed with %s [%08" PRIX32
"]",
1641 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc);
1642 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1649static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback)
1652 rdpsndPlugin* rdpsnd =
nullptr;
1654 WINPR_ASSERT(callback);
1656 rdpsnd = (rdpsndPlugin*)callback->plugin;
1657 WINPR_ASSERT(rdpsnd);
1659 if (rdpsnd->OnOpenCalled)
1660 return CHANNEL_RC_OK;
1661 rdpsnd->OnOpenCalled = TRUE;
1663 if (!allocate_internals(rdpsnd))
1664 return ERROR_OUTOFMEMORY;
1666 return rdpsnd_process_connect(rdpsnd);
1669static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
1672 rdpsndPlugin* plugin =
nullptr;
1676 len = Stream_GetRemainingLength(data);
1678 if (!callback || !callback->plugin)
1679 return ERROR_INVALID_PARAMETER;
1680 plugin = (rdpsndPlugin*)callback->plugin;
1681 WINPR_ASSERT(plugin);
1683 copy = StreamPool_Take(plugin->pool, len);
1685 return ERROR_OUTOFMEMORY;
1686 Stream_Copy(data, copy, len);
1687 Stream_SealLength(copy);
1688 Stream_ResetPosition(copy);
1692 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, copy,
nullptr))
1694 Stream_Release(copy);
1695 return ERROR_INTERNAL_ERROR;
1700 UINT error = rdpsnd_recv_pdu(plugin, copy);
1705 return CHANNEL_RC_OK;
1708static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1711 rdpsndPlugin* rdpsnd =
nullptr;
1713 WINPR_ASSERT(callback);
1715 rdpsnd = (rdpsndPlugin*)callback->plugin;
1716 WINPR_ASSERT(rdpsnd);
1718 rdpsnd->OnOpenCalled = FALSE;
1720 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1722 cleanup_internals(rdpsnd);
1724 free_internals(rdpsnd);
1727 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1728 rdpsnd->device =
nullptr;
1731 free(pChannelCallback);
1732 return CHANNEL_RC_OK;
1736static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1737 IWTSVirtualChannel* pChannel, BYTE* Data,
1739 IWTSVirtualChannelCallback** ppCallback)
1744 WINPR_ASSERT(listener_callback);
1745 WINPR_ASSERT(pChannel);
1746 WINPR_ASSERT(ppCallback);
1750 WINPR_UNUSED(pbAccept);
1754 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1755 return CHANNEL_RC_NO_MEMORY;
1758 callback->iface.OnOpen = rdpsnd_on_open;
1759 callback->iface.OnDataReceived = rdpsnd_on_data_received;
1760 callback->iface.OnClose = rdpsnd_on_close;
1761 callback->plugin = listener_callback->plugin;
1762 callback->channel_mgr = listener_callback->channel_mgr;
1763 callback->channel = pChannel;
1764 listener_callback->channel_callback = callback;
1765 *ppCallback = &callback->iface;
1766 return CHANNEL_RC_OK;
1769static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
1772 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1773 WINPR_ASSERT(rdpsnd);
1774 WINPR_ASSERT(pChannelMgr);
1775 if (rdpsnd->initialized)
1777 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME);
1778 return ERROR_INVALID_DATA;
1780 rdpsnd->listener_callback =
1783 if (!rdpsnd->listener_callback)
1785 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1786 return CHANNEL_RC_NO_MEMORY;
1789 rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection;
1790 rdpsnd->listener_callback->plugin = pPlugin;
1791 rdpsnd->listener_callback->channel_mgr = pChannelMgr;
1792 status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0,
1793 &rdpsnd->listener_callback->iface, &(rdpsnd->listener));
1794 if (status != CHANNEL_RC_OK)
1796 WLog_ERR(TAG,
"%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE));
1800 rdpsnd->listener->pInterface = rdpsnd->iface.pInterface;
1801 status = rdpsnd_virtual_channel_event_initialized(rdpsnd);
1803 rdpsnd->initialized = status == CHANNEL_RC_OK;
1812static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin)
1814 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1817 if (rdpsnd->listener_callback)
1819 IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr;
1821 IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener);
1823 free(rdpsnd->listener_callback);
1824 free(rdpsnd->iface.pInterface);
1826 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1827 return CHANNEL_RC_OK;
1835FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1837 UINT error = CHANNEL_RC_OK;
1838 rdpsndPlugin* rdpsnd =
nullptr;
1840 WINPR_ASSERT(pEntryPoints);
1841 WINPR_ASSERT(pEntryPoints->GetPlugin);
1843 rdpsnd = (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME);
1847 IWTSPlugin* iface =
nullptr;
1854 rdpsnd = allocatePlugin();
1857 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1858 return CHANNEL_RC_NO_MEMORY;
1861 iface = &rdpsnd->iface;
1862 iface->Initialize = rdpsnd_plugin_initialize;
1863 iface->Connected =
nullptr;
1864 iface->Disconnected =
nullptr;
1865 iface->Terminated = rdpsnd_plugin_terminated;
1867 rdpsnd->dynamic = TRUE;
1869 WINPR_ASSERT(pEntryPoints->GetRdpContext);
1870 rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1873 FreeRDP_SynchronousDynamicChannels))
1874 rdpsnd->async = TRUE;
1877 cnv.cev = pEntryPoints->GetPluginData(pEntryPoints);
1878 WINPR_ASSERT(pEntryPoints->GetPluginData);
1879 rdpsnd->channelEntryPoints.pExtendedData = cnv.ev;
1881 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface);
1885 WLog_ERR(TAG,
"%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE));
1886 return CHANNEL_RC_BAD_CHANNEL;
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree