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) || (BodySize < 8))
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) || (BodySize < 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_WARN(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);
881static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd,
const char* subsystem)
883 free(rdpsnd->subsystem);
884 rdpsnd->subsystem = _strdup(subsystem);
885 return (rdpsnd->subsystem !=
nullptr);
888static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd,
const char* device_name)
890 free(rdpsnd->device_name);
891 rdpsnd->device_name = _strdup(device_name);
892 return (rdpsnd->device_name !=
nullptr);
900static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd,
const ADDIN_ARGV* args)
906 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
908 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
909 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
911 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
912 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
914 {
"latency", COMMAND_LINE_VALUE_REQUIRED,
"<latency>",
nullptr,
nullptr, -1,
nullptr,
916 {
"quality", COMMAND_LINE_VALUE_REQUIRED,
"<quality mode>",
nullptr,
nullptr, -1,
nullptr,
918 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
920 rdpsnd->wQualityMode = HIGH_QUALITY;
924 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
925 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd,
929 return CHANNEL_RC_INITIALIZATION_ERROR;
936 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
939 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
941 if (!rdpsnd_set_subsystem(rdpsnd, arg->Value))
942 return CHANNEL_RC_NO_MEMORY;
944 CommandLineSwitchCase(arg,
"dev")
946 if (!rdpsnd_set_device_name(rdpsnd, arg->Value))
947 return CHANNEL_RC_NO_MEMORY;
949 CommandLineSwitchCase(arg,
"format")
951 unsigned long val = strtoul(arg->Value,
nullptr, 0);
953 if ((errno != 0) || (val > UINT16_MAX))
954 return CHANNEL_RC_INITIALIZATION_ERROR;
956 rdpsnd->fixed_format->wFormatTag = (UINT16)val;
958 CommandLineSwitchCase(arg,
"rate")
960 unsigned long val = strtoul(arg->Value,
nullptr, 0);
962 if ((errno != 0) || (val > UINT32_MAX))
963 return CHANNEL_RC_INITIALIZATION_ERROR;
965 rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val;
967 CommandLineSwitchCase(arg,
"channel")
969 unsigned long val = strtoul(arg->Value,
nullptr, 0);
971 if ((errno != 0) || (val > UINT16_MAX))
972 return CHANNEL_RC_INITIALIZATION_ERROR;
974 rdpsnd->fixed_format->nChannels = (UINT16)val;
976 CommandLineSwitchCase(arg,
"latency")
978 unsigned long val = strtoul(arg->Value,
nullptr, 0);
980 if ((errno != 0) || (val > UINT32_MAX))
981 return CHANNEL_RC_INITIALIZATION_ERROR;
983 rdpsnd->latency = (UINT32)val;
985 CommandLineSwitchCase(arg,
"quality")
987 long wQualityMode = DYNAMIC_QUALITY;
989 if (_stricmp(arg->Value,
"dynamic") == 0)
990 wQualityMode = DYNAMIC_QUALITY;
991 else if (_stricmp(arg->Value,
"medium") == 0)
992 wQualityMode = MEDIUM_QUALITY;
993 else if (_stricmp(arg->Value,
"high") == 0)
994 wQualityMode = HIGH_QUALITY;
997 wQualityMode = strtol(arg->Value,
nullptr, 0);
1000 return CHANNEL_RC_INITIALIZATION_ERROR;
1003 if ((wQualityMode < 0) || (wQualityMode > 2))
1004 wQualityMode = DYNAMIC_QUALITY;
1006 rdpsnd->wQualityMode = (UINT16)wQualityMode;
1008 CommandLineSwitchDefault(arg)
1011 CommandLineSwitchEnd(arg)
1012 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1015 return CHANNEL_RC_OK;
1023static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd)
1027 const char* subsystem;
1030#if defined(WITH_IOSAUDIO)
1033#if defined(WITH_OPENSLES)
1036#if defined(WITH_PULSE)
1039#if defined(WITH_ALSA)
1040 {
"alsa",
"default" },
1042#if defined(WITH_OSS)
1045#if defined(WITH_MACAUDIO)
1046 {
"mac",
"default" },
1048#if defined(WITH_WINMM)
1051#if defined(WITH_SNDIO)
1057 UINT status = ERROR_INTERNAL_ERROR;
1058 WINPR_ASSERT(rdpsnd);
1059 rdpsnd->latency = 0;
1060 args = (
const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData;
1064 status = rdpsnd_process_addin_args(rdpsnd, args);
1066 if (status != CHANNEL_RC_OK)
1070 if (rdpsnd->subsystem)
1072 if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args)))
1075 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
"",
1076 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status);
1082 for (
size_t x = 0; x < ARRAYSIZE(backends); x++)
1084 const char* subsystem_name = backends[x].subsystem;
1085 const char* device_name = backends[x].device;
1087 if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args)))
1089 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
1091 rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status);
1093 if (!rdpsnd->device)
1096 if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) ||
1097 !rdpsnd_set_device_name(rdpsnd, device_name))
1098 return CHANNEL_RC_NO_MEMORY;
1103 if (!rdpsnd->device || status)
1104 return CHANNEL_RC_INITIALIZATION_ERROR;
1107 return CHANNEL_RC_OK;
1115UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s)
1117 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
1121 if (rdpsnd->dynamic)
1123 IWTSVirtualChannel* channel =
nullptr;
1124 if (rdpsnd->listener_callback)
1126 channel = rdpsnd->listener_callback->channel_callback->channel;
1128 channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
nullptr);
1130 Stream_Free(s, TRUE);
1134 status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx(
1135 rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s),
1136 (UINT32)Stream_GetPosition(s), s);
1138 if (status != CHANNEL_RC_OK)
1140 Stream_Free(s, TRUE);
1141 WLog_ERR(TAG,
"%s pVirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1142 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status);
1155static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin,
void* pData,
1156 UINT32 dataLength, UINT32 totalLength,
1159 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1160 return CHANNEL_RC_OK;
1162 if (dataFlags & CHANNEL_FLAG_FIRST)
1164 if (!plugin->data_in)
1165 plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
1167 Stream_ResetPosition(plugin->data_in);
1170 if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength))
1171 return CHANNEL_RC_NO_MEMORY;
1173 Stream_Write(plugin->data_in, pData, dataLength);
1175 if (dataFlags & CHANNEL_FLAG_LAST)
1177 Stream_SealLength(plugin->data_in);
1178 Stream_ResetPosition(plugin->data_in);
1182 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, plugin->data_in,
nullptr))
1183 return ERROR_INTERNAL_ERROR;
1184 plugin->data_in =
nullptr;
1188 UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in);
1189 plugin->data_in =
nullptr;
1195 return CHANNEL_RC_OK;
1198static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1199 UINT event, LPVOID pData,
1200 UINT32 dataLength, UINT32 totalLength,
1203 UINT error = CHANNEL_RC_OK;
1204 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam;
1205 WINPR_ASSERT(rdpsnd);
1206 WINPR_ASSERT(!rdpsnd->dynamic);
1210 case CHANNEL_EVENT_DATA_RECEIVED:
1214 if (rdpsnd->OpenHandle != openHandle)
1216 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic));
1219 if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength,
1220 totalLength, dataFlags)))
1222 "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32
1224 rdpsnd_is_dyn_str(rdpsnd->dynamic), error);
1228 case CHANNEL_EVENT_WRITE_CANCELLED:
1229 case CHANNEL_EVENT_WRITE_COMPLETE:
1232 Stream_Free(s, TRUE);
1236 case CHANNEL_EVENT_USER:
1242 if (error && rdpsnd && rdpsnd->rdpcontext)
1245 (void)_snprintf(buffer,
sizeof(buffer),
1246 "%s rdpsnd_virtual_channel_open_event_ex reported an error",
1247 rdpsnd_is_dyn_str(rdpsnd->dynamic));
1248 setChannelError(rdpsnd->rdpcontext, error, buffer);
1257static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData,
1262 WINPR_UNUSED(pData);
1263 WINPR_UNUSED(dataLength);
1265 WINPR_ASSERT(rdpsnd);
1266 WINPR_ASSERT(!rdpsnd->dynamic);
1268 status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx(
1269 rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex);
1271 if (status != CHANNEL_RC_OK)
1273 WLog_ERR(TAG,
"%s pVirtualChannelOpenEx failed with %s [%08" PRIX32
"]",
1274 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status);
1278 if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK)
1281 rdpsnd->OpenHandle = opened;
1282 return CHANNEL_RC_OK;
1285 rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1286 return CHANNEL_RC_NO_MEMORY;
1289static void rdpsnd_terminate_thread(rdpsndPlugin* rdpsnd)
1291 WINPR_ASSERT(rdpsnd);
1293 MessageQueue_PostQuit(rdpsnd->queue, 0);
1297 (void)WaitForSingleObject(rdpsnd->thread, INFINITE);
1298 (void)CloseHandle(rdpsnd->thread);
1301 MessageQueue_Free(rdpsnd->queue);
1302 rdpsnd->thread =
nullptr;
1303 rdpsnd->queue =
nullptr;
1306static void cleanup_internals(rdpsndPlugin* rdpsnd)
1312 StreamPool_Return(rdpsnd->pool, rdpsnd->data_in);
1314 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
1315 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
1317 rdpsnd->NumberOfClientFormats = 0;
1318 rdpsnd->ClientFormats =
nullptr;
1319 rdpsnd->NumberOfServerFormats = 0;
1320 rdpsnd->ServerFormats =
nullptr;
1322 rdpsnd->data_in =
nullptr;
1330static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd)
1334 WINPR_ASSERT(rdpsnd);
1335 WINPR_ASSERT(!rdpsnd->dynamic);
1336 if (rdpsnd->OpenHandle != 0)
1338 DWORD opened = rdpsnd->OpenHandle;
1339 rdpsnd->OpenHandle = 0;
1341 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1343 error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1345 if (CHANNEL_RC_OK != error)
1347 WLog_ERR(TAG,
"%s pVirtualChannelCloseEx failed with %s [%08" PRIX32
"]",
1348 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error);
1353 cleanup_internals(rdpsnd);
1357 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1358 rdpsnd->device =
nullptr;
1361 return CHANNEL_RC_OK;
1364static void queue_free(
void* obj)
1366 wMessage* msg = obj;
1375static void free_internals(rdpsndPlugin* rdpsnd)
1380 if (rdpsnd->references > 0)
1381 rdpsnd->references--;
1383 if (rdpsnd->references > 0)
1386 rdpsnd_terminate_thread(rdpsnd);
1387 freerdp_dsp_context_free(rdpsnd->dsp_context);
1388 StreamPool_Free(rdpsnd->pool);
1389 rdpsnd->pool =
nullptr;
1390 rdpsnd->dsp_context =
nullptr;
1393static BOOL allocate_internals(rdpsndPlugin* rdpsnd)
1395 WINPR_ASSERT(rdpsnd);
1399 rdpsnd->pool = StreamPool_New(TRUE, 4096);
1404 if (!rdpsnd->dsp_context)
1406 rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE);
1407 if (!rdpsnd->dsp_context)
1415 wObject obj = WINPR_C_ARRAY_INIT;
1418 rdpsnd->queue = MessageQueue_New(&obj);
1420 return CHANNEL_RC_NO_MEMORY;
1423 if (!rdpsnd->thread)
1425 rdpsnd->thread = CreateThread(
nullptr, 0, play_thread, rdpsnd, 0,
nullptr);
1426 if (!rdpsnd->thread)
1427 return CHANNEL_RC_INITIALIZATION_ERROR;
1431 rdpsnd->references++;
1436static DWORD WINAPI play_thread(LPVOID arg)
1438 UINT error = CHANNEL_RC_OK;
1439 rdpsndPlugin* rdpsnd = arg;
1441 if (!rdpsnd || !rdpsnd->queue)
1442 return ERROR_INVALID_PARAMETER;
1447 wMessage message = WINPR_C_ARRAY_INIT;
1451 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1453 handles[nCount++] = MessageQueue_Event(rdpsnd->queue);
1454 handles[nCount++] = freerdp_abort_event(rdpsnd->rdpcontext);
1455 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1461 return ERROR_TIMEOUT;
1464 rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE);
1468 if (message.id == WMQ_QUIT)
1472 error = rdpsnd_recv_pdu(rdpsnd, s);
1478 return CHANNEL_RC_OK;
1481static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd)
1484 return ERROR_INVALID_PARAMETER;
1486 if (!allocate_internals(rdpsnd))
1487 return CHANNEL_RC_NO_MEMORY;
1489 return CHANNEL_RC_OK;
1492void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd)
1496 free_internals(rdpsnd);
1497 audio_formats_free(rdpsnd->fixed_format, 1);
1498 free(rdpsnd->subsystem);
1499 free(rdpsnd->device_name);
1500 rdpsnd->InitHandle =
nullptr;
1506static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1507 UINT event, LPVOID pData,
1510 UINT error = CHANNEL_RC_OK;
1511 rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam;
1516 if (plugin->InitHandle != pInitHandle)
1518 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(plugin->dynamic));
1524 case CHANNEL_EVENT_INITIALIZED:
1525 error = rdpsnd_virtual_channel_event_initialized(plugin);
1528 case CHANNEL_EVENT_CONNECTED:
1529 error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength);
1532 case CHANNEL_EVENT_DISCONNECTED:
1533 error = rdpsnd_virtual_channel_event_disconnected(plugin);
1536 case CHANNEL_EVENT_TERMINATED:
1537 rdpsnd_virtual_channel_event_terminated(plugin);
1541 case CHANNEL_EVENT_ATTACHED:
1542 plugin->attached = TRUE;
1545 case CHANNEL_EVENT_DETACHED:
1546 plugin->attached = FALSE;
1553 if (error && plugin && plugin->rdpcontext)
1556 (void)_snprintf(buffer,
sizeof(buffer),
"%s reported an error",
1557 rdpsnd_is_dyn_str(plugin->dynamic));
1558 setChannelError(plugin->rdpcontext, error, buffer);
1562rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin)
1567 return plugin->rdpcontext;
1570static rdpsndPlugin* allocatePlugin(
void)
1572 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1,
sizeof(rdpsndPlugin));
1576 rdpsnd->fixed_format = audio_format_new();
1577 if (!rdpsnd->fixed_format)
1579 rdpsnd->log = WLog_Get(
"com.freerdp.channels.rdpsnd.client");
1583 rdpsnd->attached = TRUE;
1588 audio_formats_free(rdpsnd->fixed_format, 1);
1593FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx(
1594 PCHANNEL_ENTRY_POINTS_EX pEntryPoints, PVOID pInitHandle))
1597 rdpsndPlugin* rdpsnd =
nullptr;
1603 rdpsnd = allocatePlugin();
1608 rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP;
1609 (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name),
1610 RDPSND_CHANNEL_NAME);
1614 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1616 rdpsnd->rdpcontext = pEntryPointsEx->context;
1618 FreeRDP_SynchronousStaticChannels))
1619 rdpsnd->async = TRUE;
1622 CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints,
1624 rdpsnd->InitHandle = pInitHandle;
1626 WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx);
1627 rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx(
1628 rdpsnd,
nullptr, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
1629 rdpsnd_virtual_channel_init_event_ex);
1631 if (CHANNEL_RC_OK != rc)
1633 WLog_ERR(TAG,
"%s pVirtualChannelInitEx failed with %s [%08" PRIX32
"]",
1634 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc);
1635 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1642static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback)
1645 rdpsndPlugin* rdpsnd =
nullptr;
1647 WINPR_ASSERT(callback);
1649 rdpsnd = (rdpsndPlugin*)callback->plugin;
1650 WINPR_ASSERT(rdpsnd);
1652 if (rdpsnd->OnOpenCalled)
1653 return CHANNEL_RC_OK;
1654 rdpsnd->OnOpenCalled = TRUE;
1656 if (!allocate_internals(rdpsnd))
1657 return ERROR_OUTOFMEMORY;
1659 return rdpsnd_process_connect(rdpsnd);
1662static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
1665 rdpsndPlugin* plugin =
nullptr;
1669 len = Stream_GetRemainingLength(data);
1671 if (!callback || !callback->plugin)
1672 return ERROR_INVALID_PARAMETER;
1673 plugin = (rdpsndPlugin*)callback->plugin;
1674 WINPR_ASSERT(plugin);
1676 copy = StreamPool_Take(plugin->pool, len);
1678 return ERROR_OUTOFMEMORY;
1679 Stream_Copy(data, copy, len);
1680 Stream_SealLength(copy);
1681 Stream_ResetPosition(copy);
1685 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, copy,
nullptr))
1687 Stream_Release(copy);
1688 return ERROR_INTERNAL_ERROR;
1693 UINT error = rdpsnd_recv_pdu(plugin, copy);
1698 return CHANNEL_RC_OK;
1701static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1704 rdpsndPlugin* rdpsnd =
nullptr;
1706 WINPR_ASSERT(callback);
1708 rdpsnd = (rdpsndPlugin*)callback->plugin;
1709 WINPR_ASSERT(rdpsnd);
1711 rdpsnd->OnOpenCalled = FALSE;
1713 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1715 cleanup_internals(rdpsnd);
1717 free_internals(rdpsnd);
1720 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1721 rdpsnd->device =
nullptr;
1724 free(pChannelCallback);
1725 return CHANNEL_RC_OK;
1729static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1730 IWTSVirtualChannel* pChannel, BYTE* Data,
1732 IWTSVirtualChannelCallback** ppCallback)
1737 WINPR_ASSERT(listener_callback);
1738 WINPR_ASSERT(pChannel);
1739 WINPR_ASSERT(ppCallback);
1743 WINPR_UNUSED(pbAccept);
1747 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1748 return CHANNEL_RC_NO_MEMORY;
1751 callback->iface.OnOpen = rdpsnd_on_open;
1752 callback->iface.OnDataReceived = rdpsnd_on_data_received;
1753 callback->iface.OnClose = rdpsnd_on_close;
1754 callback->plugin = listener_callback->plugin;
1755 callback->channel_mgr = listener_callback->channel_mgr;
1756 callback->channel = pChannel;
1757 listener_callback->channel_callback = callback;
1758 *ppCallback = &callback->iface;
1759 return CHANNEL_RC_OK;
1762static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
1765 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1766 WINPR_ASSERT(rdpsnd);
1767 WINPR_ASSERT(pChannelMgr);
1768 if (rdpsnd->initialized)
1770 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME);
1771 return ERROR_INVALID_DATA;
1773 rdpsnd->listener_callback =
1776 if (!rdpsnd->listener_callback)
1778 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1779 return CHANNEL_RC_NO_MEMORY;
1782 rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection;
1783 rdpsnd->listener_callback->plugin = pPlugin;
1784 rdpsnd->listener_callback->channel_mgr = pChannelMgr;
1785 status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0,
1786 &rdpsnd->listener_callback->iface, &(rdpsnd->listener));
1787 if (status != CHANNEL_RC_OK)
1789 WLog_ERR(TAG,
"%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE));
1793 rdpsnd->listener->pInterface = rdpsnd->iface.pInterface;
1794 status = rdpsnd_virtual_channel_event_initialized(rdpsnd);
1796 rdpsnd->initialized = status == CHANNEL_RC_OK;
1805static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin)
1807 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1810 if (rdpsnd->listener_callback)
1812 IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr;
1814 IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener);
1816 free(rdpsnd->listener_callback);
1817 free(rdpsnd->iface.pInterface);
1819 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1820 return CHANNEL_RC_OK;
1828FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1830 UINT error = CHANNEL_RC_OK;
1831 rdpsndPlugin* rdpsnd =
nullptr;
1833 WINPR_ASSERT(pEntryPoints);
1834 WINPR_ASSERT(pEntryPoints->GetPlugin);
1836 rdpsnd = (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME);
1840 IWTSPlugin* iface =
nullptr;
1847 rdpsnd = allocatePlugin();
1850 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1851 return CHANNEL_RC_NO_MEMORY;
1854 iface = &rdpsnd->iface;
1855 iface->Initialize = rdpsnd_plugin_initialize;
1856 iface->Connected =
nullptr;
1857 iface->Disconnected =
nullptr;
1858 iface->Terminated = rdpsnd_plugin_terminated;
1860 rdpsnd->dynamic = TRUE;
1862 WINPR_ASSERT(pEntryPoints->GetRdpContext);
1863 rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1866 FreeRDP_SynchronousDynamicChannels))
1867 rdpsnd->async = TRUE;
1870 cnv.cev = pEntryPoints->GetPluginData(pEntryPoints);
1871 WINPR_ASSERT(pEntryPoints->GetPluginData);
1872 rdpsnd->channelEntryPoints.pExtendedData = cnv.ev;
1874 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface);
1878 WLog_ERR(TAG,
"%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE));
1879 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