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);
220 pdu = Stream_New(
nullptr, length);
224 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
225 return CHANNEL_RC_NO_MEMORY;
228 Stream_Write_UINT8(pdu, SNDC_FORMATS);
229 Stream_Write_UINT8(pdu, 0);
230 Stream_Write_UINT16(pdu, length - 4);
231 Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME);
232 Stream_Write_UINT32(pdu, dwVolume);
233 Stream_Write_UINT32(pdu, 0);
234 Stream_Write_UINT16(pdu, 0);
235 Stream_Write_UINT16(pdu, wNumberOfFormats);
236 Stream_Write_UINT8(pdu, 0);
237 Stream_Write_UINT16(pdu, CHANNEL_VERSION_WIN_MAX);
238 Stream_Write_UINT8(pdu, 0);
240 for (UINT16 index = 0; index < wNumberOfFormats; index++)
242 const AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[index];
244 if (!audio_format_write(pdu, clientFormat))
246 Stream_Free(pdu, TRUE);
247 return ERROR_INTERNAL_ERROR;
251 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Client Audio Formats",
252 rdpsnd_is_dyn_str(rdpsnd->dynamic));
253 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
261static UINT rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
263 UINT16 wNumberOfFormats = 0;
264 UINT ret = ERROR_BAD_LENGTH;
266 WINPR_ASSERT(rdpsnd);
267 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
268 rdpsnd->NumberOfServerFormats = 0;
269 rdpsnd->ServerFormats =
nullptr;
271 if (!Stream_CheckAndLogRequiredLength(TAG, s, 30))
272 return ERROR_BAD_LENGTH;
275 Stream_Seek_UINT32(s);
276 Stream_Seek_UINT32(s);
277 Stream_Seek_UINT32(s);
278 Stream_Seek_UINT16(s);
279 Stream_Read_UINT16(s, wNumberOfFormats);
280 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
281 Stream_Read_UINT16(s, rdpsnd->wVersion);
282 Stream_Seek_UINT8(s);
283 rdpsnd->NumberOfServerFormats = wNumberOfFormats;
285 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 14ull))
286 return ERROR_BAD_LENGTH;
288 if (rdpsnd->NumberOfServerFormats > 0)
290 rdpsnd->ServerFormats = audio_formats_new(wNumberOfFormats);
292 if (!rdpsnd->ServerFormats)
293 return CHANNEL_RC_NO_MEMORY;
295 for (UINT16 index = 0; index < wNumberOfFormats; index++)
299 if (!audio_format_read(s, format))
304 WINPR_ASSERT(rdpsnd->device);
305 ret = IFCALLRESULT(CHANNEL_RC_OK, rdpsnd->device->ServerFormatAnnounce, rdpsnd->device,
306 rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
307 if (ret != CHANNEL_RC_OK)
310 rdpsnd_select_supported_audio_formats(rdpsnd);
311 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Server Audio Formats",
312 rdpsnd_is_dyn_str(rdpsnd->dynamic));
313 ret = rdpsnd_send_client_audio_formats(rdpsnd);
315 if (ret == CHANNEL_RC_OK)
317 if (rdpsnd->wVersion >= CHANNEL_VERSION_WIN_7)
318 ret = rdpsnd_send_quality_mode_pdu(rdpsnd);
323 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
324 rdpsnd->ServerFormats =
nullptr;
325 rdpsnd->NumberOfServerFormats = 0;
334static UINT rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
338 WINPR_ASSERT(rdpsnd);
339 pdu = Stream_New(
nullptr, 8);
343 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
344 return CHANNEL_RC_NO_MEMORY;
347 Stream_Write_UINT8(pdu, SNDC_TRAINING);
348 Stream_Write_UINT8(pdu, 0);
349 Stream_Write_UINT16(pdu, 4);
350 Stream_Write_UINT16(pdu, wTimeStamp);
351 Stream_Write_UINT16(pdu, wPackSize);
352 WLog_Print(rdpsnd->log, WLOG_DEBUG,
353 "%s Training Response: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
354 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
355 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
363static UINT rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
365 UINT16 wTimeStamp = 0;
366 UINT16 wPackSize = 0;
367 WINPR_ASSERT(rdpsnd);
370 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
371 return ERROR_BAD_LENGTH;
373 Stream_Read_UINT16(s, wTimeStamp);
374 Stream_Read_UINT16(s, wPackSize);
375 WLog_Print(rdpsnd->log, WLOG_DEBUG,
376 "%s Training Request: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
377 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
378 return rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
381static BOOL rdpsnd_apply_volume(rdpsndPlugin* rdpsnd)
383 WINPR_ASSERT(rdpsnd);
385 if (rdpsnd->isOpen && rdpsnd->applyVolume && rdpsnd->device)
387 BOOL rc = IFCALLRESULT(TRUE, rdpsnd->device->SetVolume, rdpsnd->device, rdpsnd->volume);
390 rdpsnd->applyVolume = FALSE;
395static BOOL rdpsnd_ensure_device_is_open(rdpsndPlugin* rdpsnd, UINT16 wFormatNo,
400 WINPR_ASSERT(format);
402 if (!rdpsnd->isOpen || (wFormatNo != rdpsnd->wCurrentFormatNo))
408 IFCALL(rdpsnd->device->Close, rdpsnd->device);
409 supported = IFCALLRESULT(FALSE, rdpsnd->device->FormatSupported, rdpsnd->device, format);
413 if (!IFCALLRESULT(FALSE, rdpsnd->device->DefaultFormat, rdpsnd->device, format,
416 deviceFormat.wFormatTag = WAVE_FORMAT_PCM;
417 deviceFormat.wBitsPerSample = 16;
418 deviceFormat.cbSize = 0;
422 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Opening device with format %s [backend %s]",
423 rdpsnd_is_dyn_str(rdpsnd->dynamic),
424 audio_format_get_tag_string(format->wFormatTag),
425 audio_format_get_tag_string(deviceFormat.wFormatTag));
426 rc = IFCALLRESULT(FALSE, rdpsnd->device->Open, rdpsnd->device, &deviceFormat,
434 if (!freerdp_dsp_context_reset(rdpsnd->dsp_context, format, 0u))
438 rdpsnd->isOpen = TRUE;
439 rdpsnd->wCurrentFormatNo = wFormatNo;
440 rdpsnd->startPlayTime = 0;
441 rdpsnd->totalPlaySize = 0;
444 return rdpsnd_apply_volume(rdpsnd);
452static UINT rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
454 UINT16 wFormatNo = 0;
456 WINPR_ASSERT(rdpsnd);
459 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
460 return ERROR_BAD_LENGTH;
462 rdpsnd->wArrivalTime = GetTickCount64();
463 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
464 Stream_Read_UINT16(s, wFormatNo);
466 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
467 return ERROR_INVALID_DATA;
469 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
471 Stream_Read(s, rdpsnd->waveData, 4);
472 rdpsnd->waveDataSize = BodySize - 8;
473 format = &rdpsnd->ClientFormats[wFormatNo];
474 WLog_Print(rdpsnd->log, WLOG_DEBUG,
475 "%s WaveInfo: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
" [%s]",
476 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
477 audio_format_get_tag_string(format->wFormatTag));
479 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
480 return ERROR_INTERNAL_ERROR;
482 rdpsnd->expectingWave = TRUE;
483 return CHANNEL_RC_OK;
491static UINT rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
492 BYTE cConfirmedBlockNo)
495 WINPR_ASSERT(rdpsnd);
496 pdu = Stream_New(
nullptr, 8);
500 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
501 return CHANNEL_RC_NO_MEMORY;
504 Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM);
505 Stream_Write_UINT8(pdu, 0);
506 Stream_Write_UINT16(pdu, 4);
507 Stream_Write_UINT16(pdu, wTimeStamp);
508 Stream_Write_UINT8(pdu, cConfirmedBlockNo);
509 Stream_Write_UINT8(pdu, 0);
510 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
513static BOOL rdpsnd_detect_overrun(rdpsndPlugin* rdpsnd,
const AUDIO_FORMAT* format,
size_t size)
518 UINT32 totalDuration = 0;
519 UINT32 remainingDuration = 0;
520 UINT32 maxDuration = 0;
522 if (!rdpsnd || !format)
542 switch (format->wFormatTag)
544 case WAVE_FORMAT_PCM:
545 case WAVE_FORMAT_DVI_ADPCM:
546 case WAVE_FORMAT_ADPCM:
547 case WAVE_FORMAT_ALAW:
548 case WAVE_FORMAT_MULAW:
550 case WAVE_FORMAT_MSG723:
551 case WAVE_FORMAT_GSM610:
552 case WAVE_FORMAT_AAC_MS:
557 audio_format_print(WLog_Get(TAG), WLOG_DEBUG, format);
558 bpf = format->nChannels * format->wBitsPerSample * format->nSamplesPerSec / 8;
562 duration = (UINT32)(1000 * size / bpf);
563 totalDuration = (UINT32)(1000 * rdpsnd->totalPlaySize / bpf);
564 now = GetTickCountPrecise();
565 if (rdpsnd->startPlayTime == 0)
567 rdpsnd->startPlayTime = now;
568 rdpsnd->totalPlaySize = size;
571 else if (now - rdpsnd->startPlayTime > totalDuration + 10)
574 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer underrun by %u ms",
575 rdpsnd_is_dyn_str(rdpsnd->dynamic),
576 (UINT)(now - rdpsnd->startPlayTime - totalDuration));
577 rdpsnd->startPlayTime = now;
578 rdpsnd->totalPlaySize = size;
584 remainingDuration = totalDuration - (now - rdpsnd->startPlayTime);
587 maxDuration = duration * 2 + rdpsnd->latency;
589 if (remainingDuration + duration > maxDuration)
591 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer overrun pending %u ms dropping %u ms",
592 rdpsnd_is_dyn_str(rdpsnd->dynamic), remainingDuration, duration);
596 rdpsnd->totalPlaySize += size;
601static UINT rdpsnd_treat_wave(rdpsndPlugin* rdpsnd,
wStream* s,
size_t size)
610 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
611 return ERROR_BAD_LENGTH;
613 if (rdpsnd->wCurrentFormatNo >= rdpsnd->NumberOfClientFormats)
614 return ERROR_INTERNAL_ERROR;
621 error = rdpsnd_send_wave_confirm_pdu(rdpsnd, rdpsnd->wTimeStamp, rdpsnd->cBlockNo);
625 const BYTE* data = Stream_ConstPointer(s);
626 format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
627 WLog_Print(rdpsnd->log, WLOG_DEBUG,
628 "%s Wave: cBlockNo: %" PRIu8
" wTimeStamp: %" PRIu16
", size: %" PRIuz,
629 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, rdpsnd->wTimeStamp, size);
631 if (rdpsnd->device && rdpsnd->attached && !rdpsnd_detect_overrun(rdpsnd, format, size))
633 UINT status = CHANNEL_RC_OK;
634 wStream* pcmData = StreamPool_Take(rdpsnd->pool, 4096);
636 if (rdpsnd->device->FormatSupported(rdpsnd->device, format))
638 if (rdpsnd->device->PlayEx)
639 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, data, size);
641 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, data, size);
643 else if (freerdp_dsp_decode(rdpsnd->dsp_context, format, data, size, pcmData))
645 Stream_SealLength(pcmData);
647 if (rdpsnd->device->PlayEx)
648 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, Stream_Buffer(pcmData),
649 Stream_Length(pcmData));
651 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device,
652 Stream_Buffer(pcmData), Stream_Length(pcmData));
655 status = ERROR_INTERNAL_ERROR;
657 Stream_Release(pcmData);
659 if (status != CHANNEL_RC_OK)
663 end = GetTickCount64();
664 diffMS = end - rdpsnd->wArrivalTime + latency;
665 ts = (rdpsnd->wTimeStamp + diffMS) % UINT16_MAX;
672 return rdpsnd_send_wave_confirm_pdu(rdpsnd, (UINT16)ts, rdpsnd->cBlockNo);
680static UINT rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
682 rdpsnd->expectingWave = FALSE;
690 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
691 return ERROR_INVALID_DATA;
693 CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4);
694 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
697static UINT rdpsnd_recv_wave2_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
699 UINT16 wFormatNo = 0;
701 UINT32 dwAudioTimeStamp = 0;
703 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
704 return ERROR_BAD_LENGTH;
706 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
707 Stream_Read_UINT16(s, wFormatNo);
708 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
710 Stream_Read_UINT32(s, dwAudioTimeStamp);
711 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
712 return ERROR_INVALID_DATA;
713 format = &rdpsnd->ClientFormats[wFormatNo];
714 rdpsnd->waveDataSize = BodySize - 12;
715 rdpsnd->wArrivalTime = GetTickCount64();
716 WLog_Print(rdpsnd->log, WLOG_DEBUG,
717 "%s Wave2PDU: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
718 " [%s] , align=%hu wTimeStamp=0x%04" PRIx16
", dwAudioTimeStamp=0x%08" PRIx32,
719 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
720 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign,
721 rdpsnd->wTimeStamp, dwAudioTimeStamp);
723 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
724 return ERROR_INTERNAL_ERROR;
726 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
729static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
733 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Closing device",
734 rdpsnd_is_dyn_str(rdpsnd->dynamic));
737 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Device already closed",
738 rdpsnd_is_dyn_str(rdpsnd->dynamic));
746static UINT rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
751 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
752 return ERROR_BAD_LENGTH;
754 Stream_Read_UINT32(s, dwVolume);
755 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Volume: 0x%08" PRIX32
"",
756 rdpsnd_is_dyn_str(rdpsnd->dynamic), dwVolume);
758 rdpsnd->volume = dwVolume;
759 rdpsnd->applyVolume = TRUE;
760 rc = rdpsnd_apply_volume(rdpsnd);
764 WLog_ERR(TAG,
"%s error setting volume", rdpsnd_is_dyn_str(rdpsnd->dynamic));
765 return CHANNEL_RC_INITIALIZATION_ERROR;
768 return CHANNEL_RC_OK;
776static UINT rdpsnd_recv_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
780 UINT status = CHANNEL_RC_OK;
782 if (rdpsnd->expectingWave)
784 status = rdpsnd_recv_wave_pdu(rdpsnd, s);
788 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
790 status = ERROR_BAD_LENGTH;
794 Stream_Read_UINT8(s, msgType);
795 Stream_Seek_UINT8(s);
796 Stream_Read_UINT16(s, BodySize);
801 status = rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
805 status = rdpsnd_recv_training_pdu(rdpsnd, s);
809 status = rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
813 rdpsnd_recv_close_pdu(rdpsnd);
817 status = rdpsnd_recv_volume_pdu(rdpsnd, s);
821 status = rdpsnd_recv_wave2_pdu(rdpsnd, s, BodySize);
825 WLog_ERR(TAG,
"%s unknown msgType %" PRIu8
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
835static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
839 WLog_ERR(TAG,
"%s existing device, abort.", rdpsnd_is_dyn_str(FALSE));
843 rdpsnd->device = device;
844 device->rdpsnd = rdpsnd;
852static UINT rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd,
const char* name,
857 DWORD flags = FREERDP_ADDIN_CHANNEL_STATIC | FREERDP_ADDIN_CHANNEL_ENTRYEX;
859 flags = FREERDP_ADDIN_CHANNEL_DYNAMIC;
860 PVIRTUALCHANNELENTRY pvce =
861 freerdp_load_channel_addin_entry(RDPSND_CHANNEL_NAME, name,
nullptr, flags);
862 PFREERDP_RDPSND_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_RDPSND_DEVICE_ENTRY);
865 return ERROR_INTERNAL_ERROR;
867 entryPoints.rdpsnd = rdpsnd;
868 entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
869 entryPoints.args = args;
871 error = entry(&entryPoints);
873 WLog_ERR(TAG,
"%s %s entry returns error %" PRIu32
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
876 WLog_INFO(TAG,
"%s Loaded %s backend for rdpsnd", rdpsnd_is_dyn_str(rdpsnd->dynamic), name);
880static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd,
const char* subsystem)
882 free(rdpsnd->subsystem);
883 rdpsnd->subsystem = _strdup(subsystem);
884 return (rdpsnd->subsystem !=
nullptr);
887static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd,
const char* device_name)
889 free(rdpsnd->device_name);
890 rdpsnd->device_name = _strdup(device_name);
891 return (rdpsnd->device_name !=
nullptr);
899static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd,
const ADDIN_ARGV* args)
905 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
907 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
908 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
910 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
911 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
913 {
"latency", COMMAND_LINE_VALUE_REQUIRED,
"<latency>",
nullptr,
nullptr, -1,
nullptr,
915 {
"quality", COMMAND_LINE_VALUE_REQUIRED,
"<quality mode>",
nullptr,
nullptr, -1,
nullptr,
917 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
919 rdpsnd->wQualityMode = HIGH_QUALITY;
923 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
924 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd,
928 return CHANNEL_RC_INITIALIZATION_ERROR;
935 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
938 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
940 if (!rdpsnd_set_subsystem(rdpsnd, arg->Value))
941 return CHANNEL_RC_NO_MEMORY;
943 CommandLineSwitchCase(arg,
"dev")
945 if (!rdpsnd_set_device_name(rdpsnd, arg->Value))
946 return CHANNEL_RC_NO_MEMORY;
948 CommandLineSwitchCase(arg,
"format")
950 unsigned long val = strtoul(arg->Value,
nullptr, 0);
952 if ((errno != 0) || (val > UINT16_MAX))
953 return CHANNEL_RC_INITIALIZATION_ERROR;
955 rdpsnd->fixed_format->wFormatTag = (UINT16)val;
957 CommandLineSwitchCase(arg,
"rate")
959 unsigned long val = strtoul(arg->Value,
nullptr, 0);
961 if ((errno != 0) || (val > UINT32_MAX))
962 return CHANNEL_RC_INITIALIZATION_ERROR;
964 rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val;
966 CommandLineSwitchCase(arg,
"channel")
968 unsigned long val = strtoul(arg->Value,
nullptr, 0);
970 if ((errno != 0) || (val > UINT16_MAX))
971 return CHANNEL_RC_INITIALIZATION_ERROR;
973 rdpsnd->fixed_format->nChannels = (UINT16)val;
975 CommandLineSwitchCase(arg,
"latency")
977 unsigned long val = strtoul(arg->Value,
nullptr, 0);
979 if ((errno != 0) || (val > UINT32_MAX))
980 return CHANNEL_RC_INITIALIZATION_ERROR;
982 rdpsnd->latency = (UINT32)val;
984 CommandLineSwitchCase(arg,
"quality")
986 long wQualityMode = DYNAMIC_QUALITY;
988 if (_stricmp(arg->Value,
"dynamic") == 0)
989 wQualityMode = DYNAMIC_QUALITY;
990 else if (_stricmp(arg->Value,
"medium") == 0)
991 wQualityMode = MEDIUM_QUALITY;
992 else if (_stricmp(arg->Value,
"high") == 0)
993 wQualityMode = HIGH_QUALITY;
996 wQualityMode = strtol(arg->Value,
nullptr, 0);
999 return CHANNEL_RC_INITIALIZATION_ERROR;
1002 if ((wQualityMode < 0) || (wQualityMode > 2))
1003 wQualityMode = DYNAMIC_QUALITY;
1005 rdpsnd->wQualityMode = (UINT16)wQualityMode;
1007 CommandLineSwitchDefault(arg)
1010 CommandLineSwitchEnd(arg)
1011 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1014 return CHANNEL_RC_OK;
1022static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd)
1026 const char* subsystem;
1029#if defined(WITH_IOSAUDIO)
1032#if defined(WITH_OPENSLES)
1035#if defined(WITH_PULSE)
1038#if defined(WITH_ALSA)
1039 {
"alsa",
"default" },
1041#if defined(WITH_OSS)
1044#if defined(WITH_MACAUDIO)
1045 {
"mac",
"default" },
1047#if defined(WITH_WINMM)
1050#if defined(WITH_SNDIO)
1056 UINT status = ERROR_INTERNAL_ERROR;
1057 WINPR_ASSERT(rdpsnd);
1058 rdpsnd->latency = 0;
1059 args = (
const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData;
1063 status = rdpsnd_process_addin_args(rdpsnd, args);
1065 if (status != CHANNEL_RC_OK)
1069 if (rdpsnd->subsystem)
1071 if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args)))
1074 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
"",
1075 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status);
1081 for (
size_t x = 0; x < ARRAYSIZE(backends); x++)
1083 const char* subsystem_name = backends[x].subsystem;
1084 const char* device_name = backends[x].device;
1086 if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args)))
1088 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
1090 rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status);
1092 if (!rdpsnd->device)
1095 if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) ||
1096 !rdpsnd_set_device_name(rdpsnd, device_name))
1097 return CHANNEL_RC_NO_MEMORY;
1102 if (!rdpsnd->device || status)
1103 return CHANNEL_RC_INITIALIZATION_ERROR;
1106 return CHANNEL_RC_OK;
1114UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s)
1116 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
1120 if (rdpsnd->dynamic)
1122 IWTSVirtualChannel* channel =
nullptr;
1123 if (rdpsnd->listener_callback)
1125 channel = rdpsnd->listener_callback->channel_callback->channel;
1127 channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
nullptr);
1129 Stream_Free(s, TRUE);
1133 status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx(
1134 rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s),
1135 (UINT32)Stream_GetPosition(s), s);
1137 if (status != CHANNEL_RC_OK)
1139 Stream_Free(s, TRUE);
1140 WLog_ERR(TAG,
"%s pVirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1141 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status);
1154static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin,
void* pData,
1155 UINT32 dataLength, UINT32 totalLength,
1158 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1159 return CHANNEL_RC_OK;
1161 if (dataFlags & CHANNEL_FLAG_FIRST)
1163 if (!plugin->data_in)
1164 plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
1166 Stream_SetPosition(plugin->data_in, 0);
1169 if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength))
1170 return CHANNEL_RC_NO_MEMORY;
1172 Stream_Write(plugin->data_in, pData, dataLength);
1174 if (dataFlags & CHANNEL_FLAG_LAST)
1176 Stream_SealLength(plugin->data_in);
1177 Stream_SetPosition(plugin->data_in, 0);
1181 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, plugin->data_in,
nullptr))
1182 return ERROR_INTERNAL_ERROR;
1183 plugin->data_in =
nullptr;
1187 UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in);
1188 plugin->data_in =
nullptr;
1194 return CHANNEL_RC_OK;
1197static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1198 UINT event, LPVOID pData,
1199 UINT32 dataLength, UINT32 totalLength,
1202 UINT error = CHANNEL_RC_OK;
1203 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam;
1204 WINPR_ASSERT(rdpsnd);
1205 WINPR_ASSERT(!rdpsnd->dynamic);
1209 case CHANNEL_EVENT_DATA_RECEIVED:
1213 if (rdpsnd->OpenHandle != openHandle)
1215 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic));
1218 if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength,
1219 totalLength, dataFlags)))
1221 "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32
1223 rdpsnd_is_dyn_str(rdpsnd->dynamic), error);
1227 case CHANNEL_EVENT_WRITE_CANCELLED:
1228 case CHANNEL_EVENT_WRITE_COMPLETE:
1231 Stream_Free(s, TRUE);
1235 case CHANNEL_EVENT_USER:
1241 if (error && rdpsnd && rdpsnd->rdpcontext)
1244 (void)_snprintf(buffer,
sizeof(buffer),
1245 "%s rdpsnd_virtual_channel_open_event_ex reported an error",
1246 rdpsnd_is_dyn_str(rdpsnd->dynamic));
1247 setChannelError(rdpsnd->rdpcontext, error, buffer);
1256static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData,
1261 WINPR_UNUSED(pData);
1262 WINPR_UNUSED(dataLength);
1264 WINPR_ASSERT(rdpsnd);
1265 WINPR_ASSERT(!rdpsnd->dynamic);
1267 status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx(
1268 rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex);
1270 if (status != CHANNEL_RC_OK)
1272 WLog_ERR(TAG,
"%s pVirtualChannelOpenEx failed with %s [%08" PRIX32
"]",
1273 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status);
1277 if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK)
1280 rdpsnd->OpenHandle = opened;
1281 return CHANNEL_RC_OK;
1284 rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1285 return CHANNEL_RC_NO_MEMORY;
1288static void rdpsnd_terminate_thread(rdpsndPlugin* rdpsnd)
1290 WINPR_ASSERT(rdpsnd);
1292 MessageQueue_PostQuit(rdpsnd->queue, 0);
1296 (void)WaitForSingleObject(rdpsnd->thread, INFINITE);
1297 (void)CloseHandle(rdpsnd->thread);
1300 MessageQueue_Free(rdpsnd->queue);
1301 rdpsnd->thread =
nullptr;
1302 rdpsnd->queue =
nullptr;
1305static void cleanup_internals(rdpsndPlugin* rdpsnd)
1311 StreamPool_Return(rdpsnd->pool, rdpsnd->data_in);
1313 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
1314 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
1316 rdpsnd->NumberOfClientFormats = 0;
1317 rdpsnd->ClientFormats =
nullptr;
1318 rdpsnd->NumberOfServerFormats = 0;
1319 rdpsnd->ServerFormats =
nullptr;
1321 rdpsnd->data_in =
nullptr;
1329static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd)
1333 WINPR_ASSERT(rdpsnd);
1334 WINPR_ASSERT(!rdpsnd->dynamic);
1335 if (rdpsnd->OpenHandle != 0)
1337 DWORD opened = rdpsnd->OpenHandle;
1338 rdpsnd->OpenHandle = 0;
1340 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1342 error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1344 if (CHANNEL_RC_OK != error)
1346 WLog_ERR(TAG,
"%s pVirtualChannelCloseEx failed with %s [%08" PRIX32
"]",
1347 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error);
1352 cleanup_internals(rdpsnd);
1356 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1357 rdpsnd->device =
nullptr;
1360 return CHANNEL_RC_OK;
1363static void queue_free(
void* obj)
1365 wMessage* msg = obj;
1374static void free_internals(rdpsndPlugin* rdpsnd)
1379 if (rdpsnd->references > 0)
1380 rdpsnd->references--;
1382 if (rdpsnd->references > 0)
1385 rdpsnd_terminate_thread(rdpsnd);
1386 freerdp_dsp_context_free(rdpsnd->dsp_context);
1387 StreamPool_Free(rdpsnd->pool);
1388 rdpsnd->pool =
nullptr;
1389 rdpsnd->dsp_context =
nullptr;
1392static BOOL allocate_internals(rdpsndPlugin* rdpsnd)
1394 WINPR_ASSERT(rdpsnd);
1398 rdpsnd->pool = StreamPool_New(TRUE, 4096);
1403 if (!rdpsnd->dsp_context)
1405 rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE);
1406 if (!rdpsnd->dsp_context)
1414 wObject obj = WINPR_C_ARRAY_INIT;
1417 rdpsnd->queue = MessageQueue_New(&obj);
1419 return CHANNEL_RC_NO_MEMORY;
1422 if (!rdpsnd->thread)
1424 rdpsnd->thread = CreateThread(
nullptr, 0, play_thread, rdpsnd, 0,
nullptr);
1425 if (!rdpsnd->thread)
1426 return CHANNEL_RC_INITIALIZATION_ERROR;
1430 rdpsnd->references++;
1435static DWORD WINAPI play_thread(LPVOID arg)
1437 UINT error = CHANNEL_RC_OK;
1438 rdpsndPlugin* rdpsnd = arg;
1440 if (!rdpsnd || !rdpsnd->queue)
1441 return ERROR_INVALID_PARAMETER;
1446 wMessage message = WINPR_C_ARRAY_INIT;
1450 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1452 handles[nCount++] = MessageQueue_Event(rdpsnd->queue);
1453 handles[nCount++] = freerdp_abort_event(rdpsnd->rdpcontext);
1454 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1460 return ERROR_TIMEOUT;
1463 rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE);
1467 if (message.id == WMQ_QUIT)
1471 error = rdpsnd_recv_pdu(rdpsnd, s);
1477 return CHANNEL_RC_OK;
1480static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd)
1483 return ERROR_INVALID_PARAMETER;
1485 if (!allocate_internals(rdpsnd))
1486 return CHANNEL_RC_NO_MEMORY;
1488 return CHANNEL_RC_OK;
1491void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd)
1495 free_internals(rdpsnd);
1496 audio_formats_free(rdpsnd->fixed_format, 1);
1497 free(rdpsnd->subsystem);
1498 free(rdpsnd->device_name);
1499 rdpsnd->InitHandle =
nullptr;
1505static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1506 UINT event, LPVOID pData,
1509 UINT error = CHANNEL_RC_OK;
1510 rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam;
1515 if (plugin->InitHandle != pInitHandle)
1517 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(plugin->dynamic));
1523 case CHANNEL_EVENT_INITIALIZED:
1524 error = rdpsnd_virtual_channel_event_initialized(plugin);
1527 case CHANNEL_EVENT_CONNECTED:
1528 error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength);
1531 case CHANNEL_EVENT_DISCONNECTED:
1532 error = rdpsnd_virtual_channel_event_disconnected(plugin);
1535 case CHANNEL_EVENT_TERMINATED:
1536 rdpsnd_virtual_channel_event_terminated(plugin);
1540 case CHANNEL_EVENT_ATTACHED:
1541 plugin->attached = TRUE;
1544 case CHANNEL_EVENT_DETACHED:
1545 plugin->attached = FALSE;
1552 if (error && plugin && plugin->rdpcontext)
1555 (void)_snprintf(buffer,
sizeof(buffer),
"%s reported an error",
1556 rdpsnd_is_dyn_str(plugin->dynamic));
1557 setChannelError(plugin->rdpcontext, error, buffer);
1561rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin)
1566 return plugin->rdpcontext;
1569static rdpsndPlugin* allocatePlugin(
void)
1571 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1,
sizeof(rdpsndPlugin));
1575 rdpsnd->fixed_format = audio_format_new();
1576 if (!rdpsnd->fixed_format)
1578 rdpsnd->log = WLog_Get(
"com.freerdp.channels.rdpsnd.client");
1582 rdpsnd->attached = TRUE;
1587 audio_formats_free(rdpsnd->fixed_format, 1);
1592FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx(
1593 PCHANNEL_ENTRY_POINTS_EX pEntryPoints, PVOID pInitHandle))
1596 rdpsndPlugin* rdpsnd =
nullptr;
1602 rdpsnd = allocatePlugin();
1607 rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP;
1608 (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name),
1609 RDPSND_CHANNEL_NAME);
1613 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1615 rdpsnd->rdpcontext = pEntryPointsEx->context;
1617 FreeRDP_SynchronousStaticChannels))
1618 rdpsnd->async = TRUE;
1621 CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints,
1623 rdpsnd->InitHandle = pInitHandle;
1625 WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx);
1626 rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx(
1627 rdpsnd,
nullptr, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
1628 rdpsnd_virtual_channel_init_event_ex);
1630 if (CHANNEL_RC_OK != rc)
1632 WLog_ERR(TAG,
"%s pVirtualChannelInitEx failed with %s [%08" PRIX32
"]",
1633 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc);
1634 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1641static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback)
1644 rdpsndPlugin* rdpsnd =
nullptr;
1646 WINPR_ASSERT(callback);
1648 rdpsnd = (rdpsndPlugin*)callback->plugin;
1649 WINPR_ASSERT(rdpsnd);
1651 if (rdpsnd->OnOpenCalled)
1652 return CHANNEL_RC_OK;
1653 rdpsnd->OnOpenCalled = TRUE;
1655 if (!allocate_internals(rdpsnd))
1656 return ERROR_OUTOFMEMORY;
1658 return rdpsnd_process_connect(rdpsnd);
1661static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
1664 rdpsndPlugin* plugin =
nullptr;
1668 len = Stream_GetRemainingLength(data);
1670 if (!callback || !callback->plugin)
1671 return ERROR_INVALID_PARAMETER;
1672 plugin = (rdpsndPlugin*)callback->plugin;
1673 WINPR_ASSERT(plugin);
1675 copy = StreamPool_Take(plugin->pool, len);
1677 return ERROR_OUTOFMEMORY;
1678 Stream_Copy(data, copy, len);
1679 Stream_SealLength(copy);
1680 Stream_SetPosition(copy, 0);
1684 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, copy,
nullptr))
1686 Stream_Release(copy);
1687 return ERROR_INTERNAL_ERROR;
1692 UINT error = rdpsnd_recv_pdu(plugin, copy);
1697 return CHANNEL_RC_OK;
1700static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1703 rdpsndPlugin* rdpsnd =
nullptr;
1705 WINPR_ASSERT(callback);
1707 rdpsnd = (rdpsndPlugin*)callback->plugin;
1708 WINPR_ASSERT(rdpsnd);
1710 rdpsnd->OnOpenCalled = FALSE;
1712 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1714 cleanup_internals(rdpsnd);
1716 free_internals(rdpsnd);
1719 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1720 rdpsnd->device =
nullptr;
1723 free(pChannelCallback);
1724 return CHANNEL_RC_OK;
1728static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1729 IWTSVirtualChannel* pChannel, BYTE* Data,
1731 IWTSVirtualChannelCallback** ppCallback)
1736 WINPR_ASSERT(listener_callback);
1737 WINPR_ASSERT(pChannel);
1738 WINPR_ASSERT(ppCallback);
1742 WINPR_UNUSED(pbAccept);
1746 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1747 return CHANNEL_RC_NO_MEMORY;
1750 callback->iface.OnOpen = rdpsnd_on_open;
1751 callback->iface.OnDataReceived = rdpsnd_on_data_received;
1752 callback->iface.OnClose = rdpsnd_on_close;
1753 callback->plugin = listener_callback->plugin;
1754 callback->channel_mgr = listener_callback->channel_mgr;
1755 callback->channel = pChannel;
1756 listener_callback->channel_callback = callback;
1757 *ppCallback = &callback->iface;
1758 return CHANNEL_RC_OK;
1761static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
1764 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1765 WINPR_ASSERT(rdpsnd);
1766 WINPR_ASSERT(pChannelMgr);
1767 if (rdpsnd->initialized)
1769 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME);
1770 return ERROR_INVALID_DATA;
1772 rdpsnd->listener_callback =
1775 if (!rdpsnd->listener_callback)
1777 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1778 return CHANNEL_RC_NO_MEMORY;
1781 rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection;
1782 rdpsnd->listener_callback->plugin = pPlugin;
1783 rdpsnd->listener_callback->channel_mgr = pChannelMgr;
1784 status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0,
1785 &rdpsnd->listener_callback->iface, &(rdpsnd->listener));
1786 if (status != CHANNEL_RC_OK)
1788 WLog_ERR(TAG,
"%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE));
1792 rdpsnd->listener->pInterface = rdpsnd->iface.pInterface;
1793 status = rdpsnd_virtual_channel_event_initialized(rdpsnd);
1795 rdpsnd->initialized = status == CHANNEL_RC_OK;
1804static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin)
1806 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1809 if (rdpsnd->listener_callback)
1811 IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr;
1813 IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener);
1815 free(rdpsnd->listener_callback);
1816 free(rdpsnd->iface.pInterface);
1818 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1819 return CHANNEL_RC_OK;
1827FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1829 UINT error = CHANNEL_RC_OK;
1830 rdpsndPlugin* rdpsnd =
nullptr;
1832 WINPR_ASSERT(pEntryPoints);
1833 WINPR_ASSERT(pEntryPoints->GetPlugin);
1835 rdpsnd = (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME);
1839 IWTSPlugin* iface =
nullptr;
1846 rdpsnd = allocatePlugin();
1849 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1850 return CHANNEL_RC_NO_MEMORY;
1853 iface = &rdpsnd->iface;
1854 iface->Initialize = rdpsnd_plugin_initialize;
1855 iface->Connected =
nullptr;
1856 iface->Disconnected =
nullptr;
1857 iface->Terminated = rdpsnd_plugin_terminated;
1859 rdpsnd->dynamic = TRUE;
1861 WINPR_ASSERT(pEntryPoints->GetRdpContext);
1862 rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1865 FreeRDP_SynchronousDynamicChannels))
1866 rdpsnd->async = TRUE;
1869 cnv.cev = pEntryPoints->GetPluginData(pEntryPoints);
1870 WINPR_ASSERT(pEntryPoints->GetPluginData);
1871 rdpsnd->channelEntryPoints.pExtendedData = cnv.ev;
1873 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface);
1877 WLog_ERR(TAG,
"%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE));
1878 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