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;
101 rdpsndDevicePlugin* device;
102 rdpContext* rdpcontext;
104 FREERDP_DSP_CONTEXT* dsp_context;
107 wMessageQueue* queue;
121static DWORD WINAPI play_thread(LPVOID arg);
124static const char* rdpsnd_is_dyn_str(BOOL dynamic)
131static void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd);
139static UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s);
147static UINT rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd)
150 WINPR_ASSERT(rdpsnd);
151 pdu = Stream_New(
nullptr, 8);
155 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
156 return CHANNEL_RC_NO_MEMORY;
159 Stream_Write_UINT8(pdu, SNDC_QUALITYMODE);
160 Stream_Write_UINT8(pdu, 0);
161 Stream_Write_UINT16(pdu, 4);
162 Stream_Write_UINT16(pdu, rdpsnd->wQualityMode);
163 Stream_Write_UINT16(pdu, 0);
164 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s QualityMode: %" PRIu16
"",
165 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->wQualityMode);
166 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
169static void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd)
171 WINPR_ASSERT(rdpsnd);
172 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
173 rdpsnd->NumberOfClientFormats = 0;
174 rdpsnd->ClientFormats =
nullptr;
176 if (!rdpsnd->NumberOfServerFormats)
179 rdpsnd->ClientFormats = audio_formats_new(rdpsnd->NumberOfServerFormats);
181 if (!rdpsnd->ClientFormats || !rdpsnd->device)
184 for (UINT16 index = 0; index < rdpsnd->NumberOfServerFormats; index++)
186 const AUDIO_FORMAT* serverFormat = &rdpsnd->ServerFormats[index];
188 if (!audio_format_compatible(rdpsnd->fixed_format, serverFormat))
191 WINPR_ASSERT(rdpsnd->device->FormatSupported);
192 if (freerdp_dsp_supports_format(serverFormat, FALSE) ||
193 rdpsnd->device->FormatSupported(rdpsnd->device, serverFormat))
195 AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++];
196 audio_format_copy(serverFormat, clientFormat);
207static UINT rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd)
212 UINT16 wNumberOfFormats = 0;
213 WINPR_ASSERT(rdpsnd);
215 if (!rdpsnd->device || (!rdpsnd->dynamic && (rdpsnd->OpenHandle == 0)))
216 return CHANNEL_RC_INITIALIZATION_ERROR;
218 dwVolume = IFCALLRESULT(0, rdpsnd->device->GetVolume, rdpsnd->device);
219 wNumberOfFormats = rdpsnd->NumberOfClientFormats;
222 for (UINT16 index = 0; index < wNumberOfFormats; index++)
223 length += (18 + rdpsnd->ClientFormats[index].cbSize);
229 if (length > UINT16_MAX)
230 return ERROR_INVALID_DATA;
232 pdu = Stream_New(
nullptr, length);
236 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
237 return CHANNEL_RC_NO_MEMORY;
240 Stream_Write_UINT8(pdu, SNDC_FORMATS);
241 Stream_Write_UINT8(pdu, 0);
242 Stream_Write_UINT16(pdu, (UINT16)(length - 4));
243 Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME);
244 Stream_Write_UINT32(pdu, dwVolume);
245 Stream_Write_UINT32(pdu, 0);
246 Stream_Write_UINT16(pdu, 0);
247 Stream_Write_UINT16(pdu, wNumberOfFormats);
248 Stream_Write_UINT8(pdu, 0);
249 Stream_Write_UINT16(pdu, CHANNEL_VERSION_WIN_MAX);
250 Stream_Write_UINT8(pdu, 0);
252 for (UINT16 index = 0; index < wNumberOfFormats; index++)
254 const AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[index];
256 if (!audio_format_write(pdu, clientFormat))
258 Stream_Free(pdu, TRUE);
259 return ERROR_INTERNAL_ERROR;
263 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Client Audio Formats",
264 rdpsnd_is_dyn_str(rdpsnd->dynamic));
265 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
274static UINT rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
276 UINT16 wNumberOfFormats = 0;
277 UINT ret = ERROR_BAD_LENGTH;
279 WINPR_ASSERT(rdpsnd);
280 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
281 rdpsnd->NumberOfServerFormats = 0;
282 rdpsnd->ServerFormats =
nullptr;
284 if (!Stream_CheckAndLogRequiredLength(TAG, s, 30))
285 return ERROR_BAD_LENGTH;
288 Stream_Seek_UINT32(s);
289 Stream_Seek_UINT32(s);
290 Stream_Seek_UINT32(s);
291 Stream_Seek_UINT16(s);
292 Stream_Read_UINT16(s, wNumberOfFormats);
293 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
294 Stream_Read_UINT16(s, rdpsnd->wVersion);
295 Stream_Seek_UINT8(s);
296 rdpsnd->NumberOfServerFormats = wNumberOfFormats;
298 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 14ull))
299 return ERROR_BAD_LENGTH;
301 if (rdpsnd->NumberOfServerFormats > 0)
303 rdpsnd->ServerFormats = audio_formats_new(wNumberOfFormats);
305 if (!rdpsnd->ServerFormats)
306 return CHANNEL_RC_NO_MEMORY;
308 for (UINT16 index = 0; index < wNumberOfFormats; index++)
312 if (!audio_format_read(s, format))
317 WINPR_ASSERT(rdpsnd->device);
318 ret = IFCALLRESULT(CHANNEL_RC_OK, rdpsnd->device->ServerFormatAnnounce, rdpsnd->device,
319 rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
320 if (ret != CHANNEL_RC_OK)
323 rdpsnd_select_supported_audio_formats(rdpsnd);
324 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Server Audio Formats",
325 rdpsnd_is_dyn_str(rdpsnd->dynamic));
326 ret = rdpsnd_send_client_audio_formats(rdpsnd);
328 if (ret == CHANNEL_RC_OK)
330 if (rdpsnd->wVersion >= CHANNEL_VERSION_WIN_7)
331 ret = rdpsnd_send_quality_mode_pdu(rdpsnd);
336 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
337 rdpsnd->ServerFormats =
nullptr;
338 rdpsnd->NumberOfServerFormats = 0;
348static UINT rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
352 WINPR_ASSERT(rdpsnd);
353 pdu = Stream_New(
nullptr, 8);
357 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
358 return CHANNEL_RC_NO_MEMORY;
361 Stream_Write_UINT8(pdu, SNDC_TRAINING);
362 Stream_Write_UINT8(pdu, 0);
363 Stream_Write_UINT16(pdu, 4);
364 Stream_Write_UINT16(pdu, wTimeStamp);
365 Stream_Write_UINT16(pdu, wPackSize);
366 WLog_Print(rdpsnd->log, WLOG_DEBUG,
367 "%s Training Response: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
368 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
369 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
378static UINT rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
380 UINT16 wTimeStamp = 0;
381 UINT16 wPackSize = 0;
382 WINPR_ASSERT(rdpsnd);
385 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
386 return ERROR_BAD_LENGTH;
388 Stream_Read_UINT16(s, wTimeStamp);
389 Stream_Read_UINT16(s, wPackSize);
390 WLog_Print(rdpsnd->log, WLOG_DEBUG,
391 "%s Training Request: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
392 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
393 return rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
397static BOOL rdpsnd_apply_volume(rdpsndPlugin* rdpsnd)
399 WINPR_ASSERT(rdpsnd);
401 if (rdpsnd->isOpen && rdpsnd->applyVolume && rdpsnd->device)
403 BOOL rc = IFCALLRESULT(TRUE, rdpsnd->device->SetVolume, rdpsnd->device, rdpsnd->volume);
406 rdpsnd->applyVolume = FALSE;
412static BOOL rdpsnd_ensure_device_is_open(rdpsndPlugin* rdpsnd, UINT16 wFormatNo,
417 WINPR_ASSERT(format);
419 if (!rdpsnd->isOpen || (wFormatNo != rdpsnd->wCurrentFormatNo))
425 IFCALL(rdpsnd->device->Close, rdpsnd->device);
426 supported = IFCALLRESULT(FALSE, rdpsnd->device->FormatSupported, rdpsnd->device, format);
430 if (!IFCALLRESULT(FALSE, rdpsnd->device->DefaultFormat, rdpsnd->device, format,
433 deviceFormat.wFormatTag = WAVE_FORMAT_PCM;
434 deviceFormat.wBitsPerSample = 16;
435 deviceFormat.cbSize = 0;
439 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Opening device with format %s [backend %s]",
440 rdpsnd_is_dyn_str(rdpsnd->dynamic),
441 audio_format_get_tag_string(format->wFormatTag),
442 audio_format_get_tag_string(deviceFormat.wFormatTag));
443 rc = IFCALLRESULT(FALSE, rdpsnd->device->Open, rdpsnd->device, &deviceFormat,
451 if (!freerdp_dsp_context_reset(rdpsnd->dsp_context, format, 0u))
455 rdpsnd->isOpen = TRUE;
456 rdpsnd->wCurrentFormatNo = wFormatNo;
457 rdpsnd->startPlayTime = 0;
458 rdpsnd->totalPlaySize = 0;
461 return rdpsnd_apply_volume(rdpsnd);
470static UINT rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
472 UINT16 wFormatNo = 0;
474 WINPR_ASSERT(rdpsnd);
477 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 8))
478 return ERROR_BAD_LENGTH;
480 rdpsnd->wArrivalTime = GetTickCount64();
481 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
482 Stream_Read_UINT16(s, wFormatNo);
484 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
485 return ERROR_INVALID_DATA;
487 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
489 Stream_Read(s, rdpsnd->waveData, 4);
490 rdpsnd->waveDataSize = BodySize - 8;
491 format = &rdpsnd->ClientFormats[wFormatNo];
492 WLog_Print(rdpsnd->log, WLOG_DEBUG,
493 "%s WaveInfo: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
" [%s]",
494 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
495 audio_format_get_tag_string(format->wFormatTag));
497 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
498 return ERROR_INTERNAL_ERROR;
500 rdpsnd->expectingWave = TRUE;
501 return CHANNEL_RC_OK;
510static UINT rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
511 BYTE cConfirmedBlockNo)
514 WINPR_ASSERT(rdpsnd);
515 pdu = Stream_New(
nullptr, 8);
519 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
520 return CHANNEL_RC_NO_MEMORY;
523 Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM);
524 Stream_Write_UINT8(pdu, 0);
525 Stream_Write_UINT16(pdu, 4);
526 Stream_Write_UINT16(pdu, wTimeStamp);
527 Stream_Write_UINT8(pdu, cConfirmedBlockNo);
528 Stream_Write_UINT8(pdu, 0);
529 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
533static BOOL rdpsnd_detect_overrun(rdpsndPlugin* rdpsnd,
const AUDIO_FORMAT* format,
size_t size)
538 UINT32 totalDuration = 0;
539 UINT32 remainingDuration = 0;
540 UINT32 maxDuration = 0;
542 if (!rdpsnd || !format)
562 switch (format->wFormatTag)
564 case WAVE_FORMAT_PCM:
565 case WAVE_FORMAT_DVI_ADPCM:
566 case WAVE_FORMAT_ADPCM:
567 case WAVE_FORMAT_ALAW:
568 case WAVE_FORMAT_MULAW:
570 case WAVE_FORMAT_MSG723:
571 case WAVE_FORMAT_GSM610:
572 case WAVE_FORMAT_AAC_MS:
577 audio_format_print(WLog_Get(TAG), WLOG_DEBUG, format);
578 bpf = format->nChannels * format->wBitsPerSample * format->nSamplesPerSec / 8;
582 duration = (UINT32)(1000 * size / bpf);
583 totalDuration = (UINT32)(1000 * rdpsnd->totalPlaySize / bpf);
584 now = GetTickCountPrecise();
585 if (rdpsnd->startPlayTime == 0)
587 rdpsnd->startPlayTime = now;
588 rdpsnd->totalPlaySize = size;
591 else if (now - rdpsnd->startPlayTime > totalDuration + 10)
594 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer underrun by %u ms",
595 rdpsnd_is_dyn_str(rdpsnd->dynamic),
596 (UINT)(now - rdpsnd->startPlayTime - totalDuration));
597 rdpsnd->startPlayTime = now;
598 rdpsnd->totalPlaySize = size;
604 remainingDuration = totalDuration - (now - rdpsnd->startPlayTime);
607 maxDuration = duration * 2 + rdpsnd->latency;
609 if (remainingDuration + duration > maxDuration)
611 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer overrun pending %u ms dropping %u ms",
612 rdpsnd_is_dyn_str(rdpsnd->dynamic), remainingDuration, duration);
616 rdpsnd->totalPlaySize += size;
622static UINT rdpsnd_treat_wave(rdpsndPlugin* rdpsnd,
wStream* s,
size_t size)
631 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
632 return ERROR_BAD_LENGTH;
634 if (rdpsnd->wCurrentFormatNo >= rdpsnd->NumberOfClientFormats)
635 return ERROR_INTERNAL_ERROR;
642 error = rdpsnd_send_wave_confirm_pdu(rdpsnd, rdpsnd->wTimeStamp, rdpsnd->cBlockNo);
646 const BYTE* data = Stream_ConstPointer(s);
647 format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
648 WLog_Print(rdpsnd->log, WLOG_DEBUG,
649 "%s Wave: cBlockNo: %" PRIu8
" wTimeStamp: %" PRIu16
", size: %" PRIuz,
650 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, rdpsnd->wTimeStamp, size);
652 if (rdpsnd->device && rdpsnd->attached && !rdpsnd_detect_overrun(rdpsnd, format, size))
654 UINT status = CHANNEL_RC_OK;
655 wStream* pcmData = StreamPool_Take(rdpsnd->pool, 4096);
657 if (rdpsnd->device->FormatSupported(rdpsnd->device, format))
659 if (rdpsnd->device->PlayEx)
660 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, data, size);
662 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, data, size);
664 else if (freerdp_dsp_decode(rdpsnd->dsp_context, format, data, size, pcmData))
666 Stream_SealLength(pcmData);
668 if (rdpsnd->device->PlayEx)
669 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, Stream_Buffer(pcmData),
670 Stream_Length(pcmData));
672 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device,
673 Stream_Buffer(pcmData), Stream_Length(pcmData));
676 status = ERROR_INTERNAL_ERROR;
678 Stream_Release(pcmData);
680 if (status != CHANNEL_RC_OK)
684 end = GetTickCount64();
685 diffMS = end - rdpsnd->wArrivalTime + latency;
686 ts = (rdpsnd->wTimeStamp + diffMS) % UINT16_MAX;
693 return rdpsnd_send_wave_confirm_pdu(rdpsnd, (UINT16)ts, rdpsnd->cBlockNo);
702static UINT rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
704 rdpsnd->expectingWave = FALSE;
712 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
713 return ERROR_INVALID_DATA;
715 CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4);
716 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
720static UINT rdpsnd_recv_wave2_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
722 UINT16 wFormatNo = 0;
724 UINT32 dwAudioTimeStamp = 0;
726 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 12))
727 return ERROR_BAD_LENGTH;
729 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
730 Stream_Read_UINT16(s, wFormatNo);
731 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
733 Stream_Read_UINT32(s, dwAudioTimeStamp);
734 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
735 return ERROR_INVALID_DATA;
736 format = &rdpsnd->ClientFormats[wFormatNo];
737 rdpsnd->waveDataSize = BodySize - 12;
738 rdpsnd->wArrivalTime = GetTickCount64();
739 WLog_Print(rdpsnd->log, WLOG_DEBUG,
740 "%s Wave2PDU: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
741 " [%s] , align=%hu wTimeStamp=0x%04" PRIx16
", dwAudioTimeStamp=0x%08" PRIx32,
742 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
743 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign,
744 rdpsnd->wTimeStamp, dwAudioTimeStamp);
746 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
747 return ERROR_INTERNAL_ERROR;
749 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
752static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
756 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Closing device",
757 rdpsnd_is_dyn_str(rdpsnd->dynamic));
760 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Device already closed",
761 rdpsnd_is_dyn_str(rdpsnd->dynamic));
770static UINT rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
775 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
776 return ERROR_BAD_LENGTH;
778 Stream_Read_UINT32(s, dwVolume);
779 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Volume: 0x%08" PRIX32
"",
780 rdpsnd_is_dyn_str(rdpsnd->dynamic), dwVolume);
782 rdpsnd->volume = dwVolume;
783 rdpsnd->applyVolume = TRUE;
784 rc = rdpsnd_apply_volume(rdpsnd);
788 WLog_ERR(TAG,
"%s error setting volume", rdpsnd_is_dyn_str(rdpsnd->dynamic));
789 return CHANNEL_RC_INITIALIZATION_ERROR;
792 return CHANNEL_RC_OK;
801static UINT rdpsnd_recv_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
805 UINT status = CHANNEL_RC_OK;
807 if (rdpsnd->expectingWave)
809 status = rdpsnd_recv_wave_pdu(rdpsnd, s);
813 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
815 status = ERROR_BAD_LENGTH;
819 Stream_Read_UINT8(s, msgType);
820 Stream_Seek_UINT8(s);
821 Stream_Read_UINT16(s, BodySize);
826 status = rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
830 status = rdpsnd_recv_training_pdu(rdpsnd, s);
834 status = rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
838 rdpsnd_recv_close_pdu(rdpsnd);
842 status = rdpsnd_recv_volume_pdu(rdpsnd, s);
846 status = rdpsnd_recv_wave2_pdu(rdpsnd, s, BodySize);
850 WLog_ERR(TAG,
"%s unknown msgType %" PRIu8
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
860static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
864 WLog_ERR(TAG,
"%s existing device, abort.", rdpsnd_is_dyn_str(FALSE));
868 rdpsnd->device = device;
869 device->rdpsnd = rdpsnd;
878static UINT rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd,
const char* name,
883 DWORD flags = FREERDP_ADDIN_CHANNEL_STATIC | FREERDP_ADDIN_CHANNEL_ENTRYEX;
885 flags = FREERDP_ADDIN_CHANNEL_DYNAMIC;
886 PVIRTUALCHANNELENTRY pvce =
887 freerdp_load_channel_addin_entry(RDPSND_CHANNEL_NAME, name,
nullptr, flags);
888 PFREERDP_RDPSND_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_RDPSND_DEVICE_ENTRY);
891 return ERROR_INTERNAL_ERROR;
893 entryPoints.rdpsnd = rdpsnd;
894 entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
895 entryPoints.args = args;
897 error = entry(&entryPoints);
899 WLog_WARN(TAG,
"%s %s entry returns error %" PRIu32
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
902 WLog_INFO(TAG,
"%s Loaded %s backend for rdpsnd", rdpsnd_is_dyn_str(rdpsnd->dynamic), name);
908static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd,
const char* subsystem)
910 free(rdpsnd->subsystem);
911 rdpsnd->subsystem = _strdup(subsystem);
912 return (rdpsnd->subsystem !=
nullptr);
916static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd,
const char* device_name)
918 free(rdpsnd->device_name);
919 rdpsnd->device_name = _strdup(device_name);
920 return (rdpsnd->device_name !=
nullptr);
929static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd,
const ADDIN_ARGV* args)
935 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
nullptr,
nullptr, -1,
nullptr,
937 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
938 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>",
nullptr,
nullptr, -1,
nullptr,
940 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>",
nullptr,
nullptr, -1,
nullptr,
"rate" },
941 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>",
nullptr,
nullptr, -1,
nullptr,
943 {
"latency", COMMAND_LINE_VALUE_REQUIRED,
"<latency>",
nullptr,
nullptr, -1,
nullptr,
945 {
"quality", COMMAND_LINE_VALUE_REQUIRED,
"<quality mode>",
nullptr,
nullptr, -1,
nullptr,
947 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
949 rdpsnd->wQualityMode = HIGH_QUALITY;
953 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
954 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd,
958 return CHANNEL_RC_INITIALIZATION_ERROR;
965 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
968 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
970 if (!rdpsnd_set_subsystem(rdpsnd, arg->Value))
971 return CHANNEL_RC_NO_MEMORY;
973 CommandLineSwitchCase(arg,
"dev")
975 if (!rdpsnd_set_device_name(rdpsnd, arg->Value))
976 return CHANNEL_RC_NO_MEMORY;
978 CommandLineSwitchCase(arg,
"format")
980 unsigned long val = strtoul(arg->Value,
nullptr, 0);
982 if ((errno != 0) || (val > UINT16_MAX))
983 return CHANNEL_RC_INITIALIZATION_ERROR;
985 rdpsnd->fixed_format->wFormatTag = (UINT16)val;
987 CommandLineSwitchCase(arg,
"rate")
989 unsigned long val = strtoul(arg->Value,
nullptr, 0);
991 if ((errno != 0) || (val > UINT32_MAX))
992 return CHANNEL_RC_INITIALIZATION_ERROR;
994 rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val;
996 CommandLineSwitchCase(arg,
"channel")
998 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1000 if ((errno != 0) || (val > UINT16_MAX))
1001 return CHANNEL_RC_INITIALIZATION_ERROR;
1003 rdpsnd->fixed_format->nChannels = (UINT16)val;
1005 CommandLineSwitchCase(arg,
"latency")
1007 unsigned long val = strtoul(arg->Value,
nullptr, 0);
1009 if ((errno != 0) || (val > UINT32_MAX))
1010 return CHANNEL_RC_INITIALIZATION_ERROR;
1012 rdpsnd->latency = (UINT32)val;
1014 CommandLineSwitchCase(arg,
"quality")
1016 long wQualityMode = DYNAMIC_QUALITY;
1018 if (_stricmp(arg->Value,
"dynamic") == 0)
1019 wQualityMode = DYNAMIC_QUALITY;
1020 else if (_stricmp(arg->Value,
"medium") == 0)
1021 wQualityMode = MEDIUM_QUALITY;
1022 else if (_stricmp(arg->Value,
"high") == 0)
1023 wQualityMode = HIGH_QUALITY;
1026 wQualityMode = strtol(arg->Value,
nullptr, 0);
1029 return CHANNEL_RC_INITIALIZATION_ERROR;
1032 if ((wQualityMode < 0) || (wQualityMode > 2))
1033 wQualityMode = DYNAMIC_QUALITY;
1035 rdpsnd->wQualityMode = (UINT16)wQualityMode;
1037 CommandLineSwitchDefault(arg)
1040 CommandLineSwitchEnd(arg)
1041 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
1044 return CHANNEL_RC_OK;
1053static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd)
1057 const char* subsystem;
1060#if defined(WITH_IOSAUDIO)
1063#if defined(WITH_OPENSLES)
1066#if defined(WITH_PULSE)
1069#if defined(WITH_ALSA)
1070 {
"alsa",
"default" },
1072#if defined(WITH_OSS)
1075#if defined(WITH_MACAUDIO)
1076 {
"mac",
"default" },
1078#if defined(WITH_WINMM)
1081#if defined(WITH_SNDIO)
1087 UINT status = ERROR_INTERNAL_ERROR;
1088 WINPR_ASSERT(rdpsnd);
1089 rdpsnd->latency = 0;
1090 args = (
const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData;
1094 status = rdpsnd_process_addin_args(rdpsnd, args);
1096 if (status != CHANNEL_RC_OK)
1100 if (rdpsnd->subsystem)
1102 if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args)))
1105 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
"",
1106 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status);
1112 for (
size_t x = 0; x < ARRAYSIZE(backends); x++)
1114 const char* subsystem_name = backends[x].subsystem;
1115 const char* device_name = backends[x].device;
1117 if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args)))
1119 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
1121 rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status);
1123 if (!rdpsnd->device)
1126 if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) ||
1127 !rdpsnd_set_device_name(rdpsnd, device_name))
1128 return CHANNEL_RC_NO_MEMORY;
1133 if (!rdpsnd->device || status)
1134 return CHANNEL_RC_INITIALIZATION_ERROR;
1137 return CHANNEL_RC_OK;
1145UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s)
1147 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
1151 if (rdpsnd->dynamic)
1153 IWTSVirtualChannel* channel =
nullptr;
1154 if (rdpsnd->listener_callback)
1156 channel = rdpsnd->listener_callback->channel_callback->channel;
1158 channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s),
nullptr);
1160 Stream_Free(s, TRUE);
1164 status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx(
1165 rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s),
1166 (UINT32)Stream_GetPosition(s), s);
1168 if (status != CHANNEL_RC_OK)
1170 Stream_Free(s, TRUE);
1171 WLog_ERR(TAG,
"%s pVirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1172 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status);
1186static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin,
void* pData,
1187 UINT32 dataLength, UINT32 totalLength,
1190 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1191 return CHANNEL_RC_OK;
1193 if (dataFlags & CHANNEL_FLAG_FIRST)
1195 if (!plugin->data_in)
1196 plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
1198 Stream_ResetPosition(plugin->data_in);
1201 if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength))
1202 return CHANNEL_RC_NO_MEMORY;
1204 Stream_Write(plugin->data_in, pData, dataLength);
1206 if (dataFlags & CHANNEL_FLAG_LAST)
1208 Stream_SealLength(plugin->data_in);
1209 Stream_ResetPosition(plugin->data_in);
1213 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, plugin->data_in,
nullptr))
1214 return ERROR_INTERNAL_ERROR;
1215 plugin->data_in =
nullptr;
1219 UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in);
1220 plugin->data_in =
nullptr;
1226 return CHANNEL_RC_OK;
1229static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1230 UINT event, LPVOID pData,
1231 UINT32 dataLength, UINT32 totalLength,
1234 UINT error = CHANNEL_RC_OK;
1235 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam;
1236 WINPR_ASSERT(rdpsnd);
1237 WINPR_ASSERT(!rdpsnd->dynamic);
1241 case CHANNEL_EVENT_DATA_RECEIVED:
1245 if (rdpsnd->OpenHandle != openHandle)
1247 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic));
1250 if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength,
1251 totalLength, dataFlags)))
1253 "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32
1255 rdpsnd_is_dyn_str(rdpsnd->dynamic), error);
1259 case CHANNEL_EVENT_WRITE_CANCELLED:
1260 case CHANNEL_EVENT_WRITE_COMPLETE:
1263 Stream_Free(s, TRUE);
1267 case CHANNEL_EVENT_USER:
1273 if (error && rdpsnd && rdpsnd->rdpcontext)
1276 (void)_snprintf(buffer,
sizeof(buffer),
1277 "%s rdpsnd_virtual_channel_open_event_ex reported an error",
1278 rdpsnd_is_dyn_str(rdpsnd->dynamic));
1279 setChannelError(rdpsnd->rdpcontext, error, buffer);
1289static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData,
1294 WINPR_UNUSED(pData);
1295 WINPR_UNUSED(dataLength);
1297 WINPR_ASSERT(rdpsnd);
1298 WINPR_ASSERT(!rdpsnd->dynamic);
1300 status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx(
1301 rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex);
1303 if (status != CHANNEL_RC_OK)
1305 WLog_ERR(TAG,
"%s pVirtualChannelOpenEx failed with %s [%08" PRIX32
"]",
1306 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status);
1310 if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK)
1313 rdpsnd->OpenHandle = opened;
1314 return CHANNEL_RC_OK;
1317 rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1318 return CHANNEL_RC_NO_MEMORY;
1321static void rdpsnd_terminate_thread(rdpsndPlugin* rdpsnd)
1323 WINPR_ASSERT(rdpsnd);
1327 MessageQueue_PostQuit(rdpsnd->queue, 0);
1331 (void)WaitForSingleObject(rdpsnd->thread, INFINITE);
1332 (void)CloseHandle(rdpsnd->thread);
1334 DeleteCriticalSection(&rdpsnd->asyncLock);
1335 MessageQueue_Free(rdpsnd->queue);
1337 rdpsnd->thread =
nullptr;
1338 rdpsnd->queue =
nullptr;
1341static void cleanup_internals(rdpsndPlugin* rdpsnd)
1347 EnterCriticalSection(&rdpsnd->asyncLock);
1350 StreamPool_Return(rdpsnd->pool, rdpsnd->data_in);
1352 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
1353 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
1355 rdpsnd->NumberOfClientFormats = 0;
1356 rdpsnd->ClientFormats =
nullptr;
1357 rdpsnd->NumberOfServerFormats = 0;
1358 rdpsnd->ServerFormats =
nullptr;
1360 rdpsnd->data_in =
nullptr;
1363 LeaveCriticalSection(&rdpsnd->asyncLock);
1372static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd)
1376 WINPR_ASSERT(rdpsnd);
1377 WINPR_ASSERT(!rdpsnd->dynamic);
1378 if (rdpsnd->OpenHandle != 0)
1380 DWORD opened = rdpsnd->OpenHandle;
1381 rdpsnd->OpenHandle = 0;
1383 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1385 error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1387 if (CHANNEL_RC_OK != error)
1389 WLog_ERR(TAG,
"%s pVirtualChannelCloseEx failed with %s [%08" PRIX32
"]",
1390 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error);
1395 cleanup_internals(rdpsnd);
1399 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1400 rdpsnd->device =
nullptr;
1403 return CHANNEL_RC_OK;
1406static void queue_free(
void* obj)
1408 wMessage* msg = obj;
1417static void free_internals(rdpsndPlugin* rdpsnd)
1422 if (rdpsnd->references > 0)
1423 rdpsnd->references--;
1425 if (rdpsnd->references > 0)
1428 rdpsnd_terminate_thread(rdpsnd);
1429 freerdp_dsp_context_free(rdpsnd->dsp_context);
1430 StreamPool_Free(rdpsnd->pool);
1431 rdpsnd->pool =
nullptr;
1432 rdpsnd->dsp_context =
nullptr;
1436static BOOL allocate_internals(rdpsndPlugin* rdpsnd)
1438 WINPR_ASSERT(rdpsnd);
1442 rdpsnd->pool = StreamPool_New(TRUE, 4096);
1447 if (!rdpsnd->dsp_context)
1449 rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE);
1450 if (!rdpsnd->dsp_context)
1458 wObject obj = WINPR_C_ARRAY_INIT;
1461 rdpsnd->queue = MessageQueue_New(&obj);
1463 return CHANNEL_RC_NO_MEMORY;
1464 InitializeCriticalSection(&rdpsnd->asyncLock);
1467 if (!rdpsnd->thread)
1469 rdpsnd->thread = CreateThread(
nullptr, 0, play_thread, rdpsnd, 0,
nullptr);
1470 if (!rdpsnd->thread)
1471 return CHANNEL_RC_INITIALIZATION_ERROR;
1475 rdpsnd->references++;
1481static DWORD WINAPI play_thread(LPVOID arg)
1483 UINT error = CHANNEL_RC_OK;
1484 rdpsndPlugin* rdpsnd = arg;
1486 if (!rdpsnd || !rdpsnd->queue)
1487 return ERROR_INVALID_PARAMETER;
1492 wMessage message = WINPR_C_ARRAY_INIT;
1496 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1498 handles[nCount++] = MessageQueue_Event(rdpsnd->queue);
1499 handles[nCount++] = freerdp_abort_event(rdpsnd->rdpcontext);
1500 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1506 return ERROR_TIMEOUT;
1509 rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE);
1513 if (message.id == WMQ_QUIT)
1517 EnterCriticalSection(&rdpsnd->asyncLock);
1520 error = rdpsnd_recv_pdu(rdpsnd, s);
1523 LeaveCriticalSection(&rdpsnd->asyncLock);
1529 return CHANNEL_RC_OK;
1533static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd)
1536 return ERROR_INVALID_PARAMETER;
1538 if (!allocate_internals(rdpsnd))
1539 return CHANNEL_RC_NO_MEMORY;
1541 return CHANNEL_RC_OK;
1544void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd)
1548 free_internals(rdpsnd);
1549 audio_formats_free(rdpsnd->fixed_format, 1);
1550 free(rdpsnd->subsystem);
1551 free(rdpsnd->device_name);
1552 rdpsnd->InitHandle =
nullptr;
1558static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1559 UINT event, LPVOID pData,
1562 UINT error = CHANNEL_RC_OK;
1563 rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam;
1568 if (plugin->InitHandle != pInitHandle)
1570 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(plugin->dynamic));
1576 case CHANNEL_EVENT_INITIALIZED:
1577 error = rdpsnd_virtual_channel_event_initialized(plugin);
1580 case CHANNEL_EVENT_CONNECTED:
1581 error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength);
1584 case CHANNEL_EVENT_DISCONNECTED:
1585 error = rdpsnd_virtual_channel_event_disconnected(plugin);
1588 case CHANNEL_EVENT_TERMINATED:
1589 rdpsnd_virtual_channel_event_terminated(plugin);
1593 case CHANNEL_EVENT_ATTACHED:
1594 plugin->attached = TRUE;
1597 case CHANNEL_EVENT_DETACHED:
1598 plugin->attached = FALSE;
1605 if (error && plugin && plugin->rdpcontext)
1608 (void)_snprintf(buffer,
sizeof(buffer),
"%s reported an error",
1609 rdpsnd_is_dyn_str(plugin->dynamic));
1610 setChannelError(plugin->rdpcontext, error, buffer);
1614rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin)
1619 return plugin->rdpcontext;
1623static rdpsndPlugin* allocatePlugin(
void)
1625 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1,
sizeof(rdpsndPlugin));
1629 rdpsnd->fixed_format = audio_format_new();
1630 if (!rdpsnd->fixed_format)
1632 rdpsnd->log = WLog_Get(
"com.freerdp.channels.rdpsnd.client");
1636 rdpsnd->attached = TRUE;
1641 audio_formats_free(rdpsnd->fixed_format, 1);
1646FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx(
1647 PCHANNEL_ENTRY_POINTS_EX pEntryPoints, PVOID pInitHandle))
1655 rdpsndPlugin* rdpsnd = allocatePlugin();
1660 rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP;
1661 (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name),
1662 RDPSND_CHANNEL_NAME);
1666 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1668 rdpsnd->rdpcontext = pEntryPointsEx->context;
1670 FreeRDP_SynchronousStaticChannels))
1671 rdpsnd->async = TRUE;
1674 CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints,
1676 rdpsnd->InitHandle = pInitHandle;
1678 WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx);
1679 rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx(
1680 rdpsnd,
nullptr, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
1681 rdpsnd_virtual_channel_init_event_ex);
1683 if (CHANNEL_RC_OK != rc)
1685 WLog_ERR(TAG,
"%s pVirtualChannelInitEx failed with %s [%08" PRIX32
"]",
1686 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc);
1687 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1695static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback)
1698 rdpsndPlugin* rdpsnd =
nullptr;
1700 WINPR_ASSERT(callback);
1702 rdpsnd = (rdpsndPlugin*)callback->plugin;
1703 WINPR_ASSERT(rdpsnd);
1705 if (rdpsnd->OnOpenCalled)
1706 return CHANNEL_RC_OK;
1707 rdpsnd->OnOpenCalled = TRUE;
1709 if (!allocate_internals(rdpsnd))
1710 return ERROR_OUTOFMEMORY;
1712 return rdpsnd_process_connect(rdpsnd);
1716static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
1719 rdpsndPlugin* plugin =
nullptr;
1723 len = Stream_GetRemainingLength(data);
1725 if (!callback || !callback->plugin)
1726 return ERROR_INVALID_PARAMETER;
1727 plugin = (rdpsndPlugin*)callback->plugin;
1728 WINPR_ASSERT(plugin);
1730 copy = StreamPool_Take(plugin->pool, len);
1732 return ERROR_OUTOFMEMORY;
1733 Stream_Copy(data, copy, len);
1734 Stream_SealLength(copy);
1735 Stream_ResetPosition(copy);
1739 if (!MessageQueue_Post(plugin->queue,
nullptr, 0, copy,
nullptr))
1741 Stream_Release(copy);
1742 return ERROR_INTERNAL_ERROR;
1747 UINT error = rdpsnd_recv_pdu(plugin, copy);
1752 return CHANNEL_RC_OK;
1756static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1759 rdpsndPlugin* rdpsnd =
nullptr;
1761 WINPR_ASSERT(callback);
1763 rdpsnd = (rdpsndPlugin*)callback->plugin;
1764 WINPR_ASSERT(rdpsnd);
1766 rdpsnd->OnOpenCalled = FALSE;
1768 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1770 cleanup_internals(rdpsnd);
1772 free_internals(rdpsnd);
1775 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1776 rdpsnd->device =
nullptr;
1779 free(pChannelCallback);
1780 return CHANNEL_RC_OK;
1785static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1786 IWTSVirtualChannel* pChannel, BYTE* Data,
1788 IWTSVirtualChannelCallback** ppCallback)
1793 WINPR_ASSERT(listener_callback);
1794 WINPR_ASSERT(pChannel);
1795 WINPR_ASSERT(ppCallback);
1799 WINPR_UNUSED(pbAccept);
1803 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1804 return CHANNEL_RC_NO_MEMORY;
1807 callback->iface.OnOpen = rdpsnd_on_open;
1808 callback->iface.OnDataReceived = rdpsnd_on_data_received;
1809 callback->iface.OnClose = rdpsnd_on_close;
1810 callback->plugin = listener_callback->plugin;
1811 callback->channel_mgr = listener_callback->channel_mgr;
1812 callback->channel = pChannel;
1813 listener_callback->channel_callback = callback;
1814 *ppCallback = &callback->iface;
1815 return CHANNEL_RC_OK;
1819static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
1822 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1823 WINPR_ASSERT(rdpsnd);
1824 WINPR_ASSERT(pChannelMgr);
1825 if (rdpsnd->initialized)
1827 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME);
1828 return ERROR_INVALID_DATA;
1830 rdpsnd->listener_callback =
1833 if (!rdpsnd->listener_callback)
1835 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1836 return CHANNEL_RC_NO_MEMORY;
1839 rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection;
1840 rdpsnd->listener_callback->plugin = pPlugin;
1841 rdpsnd->listener_callback->channel_mgr = pChannelMgr;
1842 status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0,
1843 &rdpsnd->listener_callback->iface, &(rdpsnd->listener));
1844 if (status != CHANNEL_RC_OK)
1846 WLog_ERR(TAG,
"%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE));
1850 rdpsnd->listener->pInterface = rdpsnd->iface.pInterface;
1851 status = rdpsnd_virtual_channel_event_initialized(rdpsnd);
1853 rdpsnd->initialized = status == CHANNEL_RC_OK;
1863static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin)
1865 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1868 if (rdpsnd->listener_callback)
1870 IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr;
1872 IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener);
1874 free(rdpsnd->listener_callback);
1875 free(rdpsnd->iface.pInterface);
1877 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1878 return CHANNEL_RC_OK;
1886FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1888 UINT error = CHANNEL_RC_OK;
1890 WINPR_ASSERT(pEntryPoints);
1891 WINPR_ASSERT(pEntryPoints->GetPlugin);
1893 rdpsndPlugin* rdpsnd =
1894 (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME);
1898 IWTSPlugin* iface =
nullptr;
1905 rdpsnd = allocatePlugin();
1908 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1909 return CHANNEL_RC_NO_MEMORY;
1912 iface = &rdpsnd->iface;
1913 iface->Initialize = rdpsnd_plugin_initialize;
1914 iface->Connected =
nullptr;
1915 iface->Disconnected =
nullptr;
1916 iface->Terminated = rdpsnd_plugin_terminated;
1918 rdpsnd->dynamic = TRUE;
1920 WINPR_ASSERT(pEntryPoints->GetRdpContext);
1921 rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1924 FreeRDP_SynchronousDynamicChannels))
1925 rdpsnd->async = TRUE;
1928 cnv.cev = pEntryPoints->GetPluginData(pEntryPoints);
1929 WINPR_ASSERT(pEntryPoints->GetPluginData);
1930 rdpsnd->channelEntryPoints.pExtendedData = cnv.ev;
1932 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface);
1936 WLog_ERR(TAG,
"%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE));
1937 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