25 #include <freerdp/config.h>
37 #include <winpr/crt.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;
120 static const char* rdpsnd_is_dyn_str(BOOL dynamic)
127 static void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd);
134 static UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s);
141 static UINT rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd)
144 WINPR_ASSERT(rdpsnd);
145 pdu = Stream_New(NULL, 8);
149 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
150 return CHANNEL_RC_NO_MEMORY;
153 Stream_Write_UINT8(pdu, SNDC_QUALITYMODE);
154 Stream_Write_UINT8(pdu, 0);
155 Stream_Write_UINT16(pdu, 4);
156 Stream_Write_UINT16(pdu, rdpsnd->wQualityMode);
157 Stream_Write_UINT16(pdu, 0);
158 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s QualityMode: %" PRIu16
"",
159 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->wQualityMode);
160 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
163 static void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd)
165 WINPR_ASSERT(rdpsnd);
166 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
167 rdpsnd->NumberOfClientFormats = 0;
168 rdpsnd->ClientFormats = NULL;
170 if (!rdpsnd->NumberOfServerFormats)
173 rdpsnd->ClientFormats = audio_formats_new(rdpsnd->NumberOfServerFormats);
175 if (!rdpsnd->ClientFormats || !rdpsnd->device)
178 for (UINT16 index = 0; index < rdpsnd->NumberOfServerFormats; index++)
180 const AUDIO_FORMAT* serverFormat = &rdpsnd->ServerFormats[index];
182 if (!audio_format_compatible(rdpsnd->fixed_format, serverFormat))
185 WINPR_ASSERT(rdpsnd->device->FormatSupported);
186 if (freerdp_dsp_supports_format(serverFormat, FALSE) ||
187 rdpsnd->device->FormatSupported(rdpsnd->device, serverFormat))
189 AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++];
190 audio_format_copy(serverFormat, clientFormat);
200 static UINT rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd)
205 UINT16 wNumberOfFormats = 0;
206 WINPR_ASSERT(rdpsnd);
208 if (!rdpsnd->device || (!rdpsnd->dynamic && (rdpsnd->OpenHandle == 0)))
209 return CHANNEL_RC_INITIALIZATION_ERROR;
211 dwVolume = IFCALLRESULT(0, rdpsnd->device->GetVolume, rdpsnd->device);
212 wNumberOfFormats = rdpsnd->NumberOfClientFormats;
215 for (UINT16 index = 0; index < wNumberOfFormats; index++)
216 length += (18 + rdpsnd->ClientFormats[index].cbSize);
218 pdu = Stream_New(NULL, length);
222 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
223 return CHANNEL_RC_NO_MEMORY;
226 Stream_Write_UINT8(pdu, SNDC_FORMATS);
227 Stream_Write_UINT8(pdu, 0);
228 Stream_Write_UINT16(pdu, length - 4);
229 Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME);
230 Stream_Write_UINT32(pdu, dwVolume);
231 Stream_Write_UINT32(pdu, 0);
232 Stream_Write_UINT16(pdu, 0);
233 Stream_Write_UINT16(pdu, wNumberOfFormats);
234 Stream_Write_UINT8(pdu, 0);
235 Stream_Write_UINT16(pdu, CHANNEL_VERSION_WIN_MAX);
236 Stream_Write_UINT8(pdu, 0);
238 for (UINT16 index = 0; index < wNumberOfFormats; index++)
240 const AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[index];
242 if (!audio_format_write(pdu, clientFormat))
244 Stream_Free(pdu, TRUE);
245 return ERROR_INTERNAL_ERROR;
249 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Client Audio Formats",
250 rdpsnd_is_dyn_str(rdpsnd->dynamic));
251 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
259 static UINT rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
261 UINT16 wNumberOfFormats = 0;
262 UINT ret = ERROR_BAD_LENGTH;
264 WINPR_ASSERT(rdpsnd);
265 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
266 rdpsnd->NumberOfServerFormats = 0;
267 rdpsnd->ServerFormats = NULL;
269 if (!Stream_CheckAndLogRequiredLength(TAG, s, 30))
270 return ERROR_BAD_LENGTH;
273 Stream_Seek_UINT32(s);
274 Stream_Seek_UINT32(s);
275 Stream_Seek_UINT32(s);
276 Stream_Seek_UINT16(s);
277 Stream_Read_UINT16(s, wNumberOfFormats);
278 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
279 Stream_Read_UINT16(s, rdpsnd->wVersion);
280 Stream_Seek_UINT8(s);
281 rdpsnd->NumberOfServerFormats = wNumberOfFormats;
283 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 14ull))
284 return ERROR_BAD_LENGTH;
286 if (rdpsnd->NumberOfServerFormats > 0)
288 rdpsnd->ServerFormats = audio_formats_new(wNumberOfFormats);
290 if (!rdpsnd->ServerFormats)
291 return CHANNEL_RC_NO_MEMORY;
293 for (UINT16 index = 0; index < wNumberOfFormats; index++)
297 if (!audio_format_read(s, format))
302 WINPR_ASSERT(rdpsnd->device);
303 ret = IFCALLRESULT(CHANNEL_RC_OK, rdpsnd->device->ServerFormatAnnounce, rdpsnd->device,
304 rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
305 if (ret != CHANNEL_RC_OK)
308 rdpsnd_select_supported_audio_formats(rdpsnd);
309 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Server Audio Formats",
310 rdpsnd_is_dyn_str(rdpsnd->dynamic));
311 ret = rdpsnd_send_client_audio_formats(rdpsnd);
313 if (ret == CHANNEL_RC_OK)
315 if (rdpsnd->wVersion >= CHANNEL_VERSION_WIN_7)
316 ret = rdpsnd_send_quality_mode_pdu(rdpsnd);
321 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
322 rdpsnd->ServerFormats = NULL;
323 rdpsnd->NumberOfServerFormats = 0;
332 static UINT rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
336 WINPR_ASSERT(rdpsnd);
337 pdu = Stream_New(NULL, 8);
341 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
342 return CHANNEL_RC_NO_MEMORY;
345 Stream_Write_UINT8(pdu, SNDC_TRAINING);
346 Stream_Write_UINT8(pdu, 0);
347 Stream_Write_UINT16(pdu, 4);
348 Stream_Write_UINT16(pdu, wTimeStamp);
349 Stream_Write_UINT16(pdu, wPackSize);
350 WLog_Print(rdpsnd->log, WLOG_DEBUG,
351 "%s Training Response: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
352 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
353 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
361 static UINT rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
363 UINT16 wTimeStamp = 0;
364 UINT16 wPackSize = 0;
365 WINPR_ASSERT(rdpsnd);
368 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
369 return ERROR_BAD_LENGTH;
371 Stream_Read_UINT16(s, wTimeStamp);
372 Stream_Read_UINT16(s, wPackSize);
373 WLog_Print(rdpsnd->log, WLOG_DEBUG,
374 "%s Training Request: wTimeStamp: %" PRIu16
" wPackSize: %" PRIu16
"",
375 rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize);
376 return rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
379 static BOOL rdpsnd_apply_volume(rdpsndPlugin* rdpsnd)
381 WINPR_ASSERT(rdpsnd);
383 if (rdpsnd->isOpen && rdpsnd->applyVolume && rdpsnd->device)
385 BOOL rc = IFCALLRESULT(TRUE, rdpsnd->device->SetVolume, rdpsnd->device, rdpsnd->volume);
388 rdpsnd->applyVolume = FALSE;
393 static BOOL rdpsnd_ensure_device_is_open(rdpsndPlugin* rdpsnd, UINT16 wFormatNo,
398 WINPR_ASSERT(format);
400 if (!rdpsnd->isOpen || (wFormatNo != rdpsnd->wCurrentFormatNo))
406 IFCALL(rdpsnd->device->Close, rdpsnd->device);
407 supported = IFCALLRESULT(FALSE, rdpsnd->device->FormatSupported, rdpsnd->device, format);
411 if (!IFCALLRESULT(FALSE, rdpsnd->device->DefaultFormat, rdpsnd->device, format,
414 deviceFormat.wFormatTag = WAVE_FORMAT_PCM;
415 deviceFormat.wBitsPerSample = 16;
416 deviceFormat.cbSize = 0;
420 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Opening device with format %s [backend %s]",
421 rdpsnd_is_dyn_str(rdpsnd->dynamic),
422 audio_format_get_tag_string(format->wFormatTag),
423 audio_format_get_tag_string(deviceFormat.wFormatTag));
424 rc = IFCALLRESULT(FALSE, rdpsnd->device->Open, rdpsnd->device, &deviceFormat,
432 if (!freerdp_dsp_context_reset(rdpsnd->dsp_context, format, 0u))
436 rdpsnd->isOpen = TRUE;
437 rdpsnd->wCurrentFormatNo = wFormatNo;
438 rdpsnd->startPlayTime = 0;
439 rdpsnd->totalPlaySize = 0;
442 return rdpsnd_apply_volume(rdpsnd);
450 static UINT rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
452 UINT16 wFormatNo = 0;
454 WINPR_ASSERT(rdpsnd);
457 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
458 return ERROR_BAD_LENGTH;
460 rdpsnd->wArrivalTime = GetTickCount64();
461 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
462 Stream_Read_UINT16(s, wFormatNo);
464 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
465 return ERROR_INVALID_DATA;
467 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
469 Stream_Read(s, rdpsnd->waveData, 4);
470 rdpsnd->waveDataSize = BodySize - 8;
471 format = &rdpsnd->ClientFormats[wFormatNo];
472 WLog_Print(rdpsnd->log, WLOG_DEBUG,
473 "%s WaveInfo: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
" [%s]",
474 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
475 audio_format_get_tag_string(format->wFormatTag));
477 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
478 return ERROR_INTERNAL_ERROR;
480 rdpsnd->expectingWave = TRUE;
481 return CHANNEL_RC_OK;
489 static UINT rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp,
490 BYTE cConfirmedBlockNo)
493 WINPR_ASSERT(rdpsnd);
494 pdu = Stream_New(NULL, 8);
498 WLog_ERR(TAG,
"%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic));
499 return CHANNEL_RC_NO_MEMORY;
502 Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM);
503 Stream_Write_UINT8(pdu, 0);
504 Stream_Write_UINT16(pdu, 4);
505 Stream_Write_UINT16(pdu, wTimeStamp);
506 Stream_Write_UINT8(pdu, cConfirmedBlockNo);
507 Stream_Write_UINT8(pdu, 0);
508 return rdpsnd_virtual_channel_write(rdpsnd, pdu);
511 static BOOL rdpsnd_detect_overrun(rdpsndPlugin* rdpsnd,
const AUDIO_FORMAT* format,
size_t size)
516 UINT32 totalDuration = 0;
517 UINT32 remainingDuration = 0;
518 UINT32 maxDuration = 0;
520 if (!rdpsnd || !format)
540 switch (format->wFormatTag)
542 case WAVE_FORMAT_PCM:
543 case WAVE_FORMAT_DVI_ADPCM:
544 case WAVE_FORMAT_ADPCM:
545 case WAVE_FORMAT_ALAW:
546 case WAVE_FORMAT_MULAW:
548 case WAVE_FORMAT_MSG723:
549 case WAVE_FORMAT_GSM610:
550 case WAVE_FORMAT_AAC_MS:
555 audio_format_print(WLog_Get(TAG), WLOG_DEBUG, format);
556 bpf = format->nChannels * format->wBitsPerSample * format->nSamplesPerSec / 8;
560 duration = (UINT32)(1000 * size / bpf);
561 totalDuration = (UINT32)(1000 * rdpsnd->totalPlaySize / bpf);
562 now = GetTickCountPrecise();
563 if (rdpsnd->startPlayTime == 0)
565 rdpsnd->startPlayTime = now;
566 rdpsnd->totalPlaySize = size;
569 else if (now - rdpsnd->startPlayTime > totalDuration + 10)
572 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer underrun by %u ms",
573 rdpsnd_is_dyn_str(rdpsnd->dynamic),
574 (UINT)(now - rdpsnd->startPlayTime - totalDuration));
575 rdpsnd->startPlayTime = now;
576 rdpsnd->totalPlaySize = size;
582 remainingDuration = totalDuration - (now - rdpsnd->startPlayTime);
585 maxDuration = duration * 2 + rdpsnd->latency;
587 if (remainingDuration + duration > maxDuration)
589 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Buffer overrun pending %u ms dropping %u ms",
590 rdpsnd_is_dyn_str(rdpsnd->dynamic), remainingDuration, duration);
594 rdpsnd->totalPlaySize += size;
599 static UINT rdpsnd_treat_wave(rdpsndPlugin* rdpsnd,
wStream* s,
size_t size)
608 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
609 return ERROR_BAD_LENGTH;
611 if (rdpsnd->wCurrentFormatNo >= rdpsnd->NumberOfClientFormats)
612 return ERROR_INTERNAL_ERROR;
619 error = rdpsnd_send_wave_confirm_pdu(rdpsnd, rdpsnd->wTimeStamp, rdpsnd->cBlockNo);
623 const BYTE* data = Stream_ConstPointer(s);
624 format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
625 WLog_Print(rdpsnd->log, WLOG_DEBUG,
626 "%s Wave: cBlockNo: %" PRIu8
" wTimeStamp: %" PRIu16
", size: %" PRIdz,
627 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, rdpsnd->wTimeStamp, size);
629 if (rdpsnd->device && rdpsnd->attached && !rdpsnd_detect_overrun(rdpsnd, format, size))
631 UINT status = CHANNEL_RC_OK;
632 wStream* pcmData = StreamPool_Take(rdpsnd->pool, 4096);
634 if (rdpsnd->device->FormatSupported(rdpsnd->device, format))
636 if (rdpsnd->device->PlayEx)
637 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, data, size);
639 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, data, size);
641 else if (freerdp_dsp_decode(rdpsnd->dsp_context, format, data, size, pcmData))
643 Stream_SealLength(pcmData);
645 if (rdpsnd->device->PlayEx)
646 latency = rdpsnd->device->PlayEx(rdpsnd->device, format, Stream_Buffer(pcmData),
647 Stream_Length(pcmData));
649 latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device,
650 Stream_Buffer(pcmData), Stream_Length(pcmData));
653 status = ERROR_INTERNAL_ERROR;
655 Stream_Release(pcmData);
657 if (status != CHANNEL_RC_OK)
661 end = GetTickCount64();
662 diffMS = end - rdpsnd->wArrivalTime + latency;
663 ts = (rdpsnd->wTimeStamp + diffMS) % UINT16_MAX;
670 return rdpsnd_send_wave_confirm_pdu(rdpsnd, (UINT16)ts, rdpsnd->cBlockNo);
678 static UINT rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
680 rdpsnd->expectingWave = FALSE;
688 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
689 return ERROR_INVALID_DATA;
691 CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4);
692 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
695 static UINT rdpsnd_recv_wave2_pdu(rdpsndPlugin* rdpsnd,
wStream* s, UINT16 BodySize)
697 UINT16 wFormatNo = 0;
699 UINT32 dwAudioTimeStamp = 0;
701 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
702 return ERROR_BAD_LENGTH;
704 Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
705 Stream_Read_UINT16(s, wFormatNo);
706 Stream_Read_UINT8(s, rdpsnd->cBlockNo);
708 Stream_Read_UINT32(s, dwAudioTimeStamp);
709 if (wFormatNo >= rdpsnd->NumberOfClientFormats)
710 return ERROR_INVALID_DATA;
711 format = &rdpsnd->ClientFormats[wFormatNo];
712 rdpsnd->waveDataSize = BodySize - 12;
713 rdpsnd->wArrivalTime = GetTickCount64();
714 WLog_Print(rdpsnd->log, WLOG_DEBUG,
715 "%s Wave2PDU: cBlockNo: %" PRIu8
" wFormatNo: %" PRIu16
" [%s] , align=%hu",
716 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo,
717 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign);
719 if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format))
720 return ERROR_INTERNAL_ERROR;
722 return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize);
725 static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
729 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Closing device",
730 rdpsnd_is_dyn_str(rdpsnd->dynamic));
733 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Device already closed",
734 rdpsnd_is_dyn_str(rdpsnd->dynamic));
742 static UINT rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
747 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
748 return ERROR_BAD_LENGTH;
750 Stream_Read_UINT32(s, dwVolume);
751 WLog_Print(rdpsnd->log, WLOG_DEBUG,
"%s Volume: 0x%08" PRIX32
"",
752 rdpsnd_is_dyn_str(rdpsnd->dynamic), dwVolume);
754 rdpsnd->volume = dwVolume;
755 rdpsnd->applyVolume = TRUE;
756 rc = rdpsnd_apply_volume(rdpsnd);
760 WLog_ERR(TAG,
"%s error setting volume", rdpsnd_is_dyn_str(rdpsnd->dynamic));
761 return CHANNEL_RC_INITIALIZATION_ERROR;
764 return CHANNEL_RC_OK;
772 static UINT rdpsnd_recv_pdu(rdpsndPlugin* rdpsnd,
wStream* s)
776 UINT status = CHANNEL_RC_OK;
778 if (rdpsnd->expectingWave)
780 status = rdpsnd_recv_wave_pdu(rdpsnd, s);
784 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
786 status = ERROR_BAD_LENGTH;
790 Stream_Read_UINT8(s, msgType);
791 Stream_Seek_UINT8(s);
792 Stream_Read_UINT16(s, BodySize);
797 status = rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
801 status = rdpsnd_recv_training_pdu(rdpsnd, s);
805 status = rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
809 rdpsnd_recv_close_pdu(rdpsnd);
813 status = rdpsnd_recv_volume_pdu(rdpsnd, s);
817 status = rdpsnd_recv_wave2_pdu(rdpsnd, s, BodySize);
821 WLog_ERR(TAG,
"%s unknown msgType %" PRIu8
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
831 static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
835 WLog_ERR(TAG,
"%s existing device, abort.", rdpsnd_is_dyn_str(FALSE));
839 rdpsnd->device = device;
840 device->rdpsnd = rdpsnd;
848 static UINT rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd,
const char* name,
853 DWORD flags = FREERDP_ADDIN_CHANNEL_STATIC | FREERDP_ADDIN_CHANNEL_ENTRYEX;
855 flags = FREERDP_ADDIN_CHANNEL_DYNAMIC;
856 PVIRTUALCHANNELENTRY pvce =
857 freerdp_load_channel_addin_entry(RDPSND_CHANNEL_NAME, name, NULL, flags);
858 PFREERDP_RDPSND_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_RDPSND_DEVICE_ENTRY);
861 return ERROR_INTERNAL_ERROR;
863 entryPoints.rdpsnd = rdpsnd;
864 entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
865 entryPoints.args = args;
867 error = entry(&entryPoints);
869 WLog_ERR(TAG,
"%s %s entry returns error %" PRIu32
"", rdpsnd_is_dyn_str(rdpsnd->dynamic),
872 WLog_INFO(TAG,
"%s Loaded %s backend for rdpsnd", rdpsnd_is_dyn_str(rdpsnd->dynamic), name);
876 static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd,
const char* subsystem)
878 free(rdpsnd->subsystem);
879 rdpsnd->subsystem = _strdup(subsystem);
880 return (rdpsnd->subsystem != NULL);
883 static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd,
const char* device_name)
885 free(rdpsnd->device_name);
886 rdpsnd->device_name = _strdup(device_name);
887 return (rdpsnd->device_name != NULL);
895 static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd,
const ADDIN_ARGV* args)
901 {
"sys", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>", NULL, NULL, -1, NULL,
"subsystem" },
902 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
"device" },
903 {
"format", COMMAND_LINE_VALUE_REQUIRED,
"<format>", NULL, NULL, -1, NULL,
"format" },
904 {
"rate", COMMAND_LINE_VALUE_REQUIRED,
"<rate>", NULL, NULL, -1, NULL,
"rate" },
905 {
"channel", COMMAND_LINE_VALUE_REQUIRED,
"<channel>", NULL, NULL, -1, NULL,
"channel" },
906 {
"latency", COMMAND_LINE_VALUE_REQUIRED,
"<latency>", NULL, NULL, -1, NULL,
"latency" },
907 {
"quality", COMMAND_LINE_VALUE_REQUIRED,
"<quality mode>", NULL, NULL, -1, NULL,
909 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
911 rdpsnd->wQualityMode = HIGH_QUALITY;
915 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
916 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd,
920 return CHANNEL_RC_INITIALIZATION_ERROR;
927 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
930 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
932 if (!rdpsnd_set_subsystem(rdpsnd, arg->Value))
933 return CHANNEL_RC_NO_MEMORY;
935 CommandLineSwitchCase(arg,
"dev")
937 if (!rdpsnd_set_device_name(rdpsnd, arg->Value))
938 return CHANNEL_RC_NO_MEMORY;
940 CommandLineSwitchCase(arg,
"format")
942 unsigned long val = strtoul(arg->Value, NULL, 0);
944 if ((errno != 0) || (val > UINT16_MAX))
945 return CHANNEL_RC_INITIALIZATION_ERROR;
947 rdpsnd->fixed_format->wFormatTag = (UINT16)val;
949 CommandLineSwitchCase(arg,
"rate")
951 unsigned long val = strtoul(arg->Value, NULL, 0);
953 if ((errno != 0) || (val > UINT32_MAX))
954 return CHANNEL_RC_INITIALIZATION_ERROR;
956 rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val;
958 CommandLineSwitchCase(arg,
"channel")
960 unsigned long val = strtoul(arg->Value, NULL, 0);
962 if ((errno != 0) || (val > UINT16_MAX))
963 return CHANNEL_RC_INITIALIZATION_ERROR;
965 rdpsnd->fixed_format->nChannels = (UINT16)val;
967 CommandLineSwitchCase(arg,
"latency")
969 unsigned long val = strtoul(arg->Value, NULL, 0);
971 if ((errno != 0) || (val > UINT32_MAX))
972 return CHANNEL_RC_INITIALIZATION_ERROR;
974 rdpsnd->latency = (UINT32)val;
976 CommandLineSwitchCase(arg,
"quality")
978 long wQualityMode = DYNAMIC_QUALITY;
980 if (_stricmp(arg->Value,
"dynamic") == 0)
981 wQualityMode = DYNAMIC_QUALITY;
982 else if (_stricmp(arg->Value,
"medium") == 0)
983 wQualityMode = MEDIUM_QUALITY;
984 else if (_stricmp(arg->Value,
"high") == 0)
985 wQualityMode = HIGH_QUALITY;
988 wQualityMode = strtol(arg->Value, NULL, 0);
991 return CHANNEL_RC_INITIALIZATION_ERROR;
994 if ((wQualityMode < 0) || (wQualityMode > 2))
995 wQualityMode = DYNAMIC_QUALITY;
997 rdpsnd->wQualityMode = (UINT16)wQualityMode;
999 CommandLineSwitchDefault(arg)
1002 CommandLineSwitchEnd(arg)
1003 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
1006 return CHANNEL_RC_OK;
1014 static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd)
1018 const char* subsystem;
1021 #if defined(WITH_IOSAUDIO)
1024 #if defined(WITH_OPENSLES)
1027 #if defined(WITH_PULSE)
1030 #if defined(WITH_ALSA)
1031 {
"alsa",
"default" },
1033 #if defined(WITH_OSS)
1036 #if defined(WITH_MACAUDIO)
1037 {
"mac",
"default" },
1039 #if defined(WITH_WINMM)
1042 #if defined(WITH_SNDIO)
1048 UINT status = ERROR_INTERNAL_ERROR;
1049 WINPR_ASSERT(rdpsnd);
1050 rdpsnd->latency = 0;
1051 args = (
const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData;
1055 status = rdpsnd_process_addin_args(rdpsnd, args);
1057 if (status != CHANNEL_RC_OK)
1061 if (rdpsnd->subsystem)
1063 if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args)))
1066 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
"",
1067 rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status);
1073 for (
size_t x = 0; x < ARRAYSIZE(backends); x++)
1075 const char* subsystem_name = backends[x].subsystem;
1076 const char* device_name = backends[x].device;
1078 if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args)))
1080 "%s Unable to load sound playback subsystem %s because of error %" PRIu32
1082 rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status);
1084 if (!rdpsnd->device)
1087 if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) ||
1088 !rdpsnd_set_device_name(rdpsnd, device_name))
1089 return CHANNEL_RC_NO_MEMORY;
1094 if (!rdpsnd->device || status)
1095 return CHANNEL_RC_INITIALIZATION_ERROR;
1098 return CHANNEL_RC_OK;
1106 UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd,
wStream* s)
1108 UINT status = CHANNEL_RC_BAD_INIT_HANDLE;
1112 if (rdpsnd->dynamic)
1114 IWTSVirtualChannel* channel = NULL;
1115 if (rdpsnd->listener_callback)
1117 channel = rdpsnd->listener_callback->channel_callback->channel;
1118 status = channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s), NULL);
1120 Stream_Free(s, TRUE);
1124 status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx(
1125 rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s),
1126 (UINT32)Stream_GetPosition(s), s);
1128 if (status != CHANNEL_RC_OK)
1130 Stream_Free(s, TRUE);
1131 WLog_ERR(TAG,
"%s pVirtualChannelWriteEx failed with %s [%08" PRIX32
"]",
1132 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status);
1145 static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin,
void* pData,
1146 UINT32 dataLength, UINT32 totalLength,
1149 if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME))
1150 return CHANNEL_RC_OK;
1152 if (dataFlags & CHANNEL_FLAG_FIRST)
1154 if (!plugin->data_in)
1155 plugin->data_in = StreamPool_Take(plugin->pool, totalLength);
1157 Stream_SetPosition(plugin->data_in, 0);
1160 if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength))
1161 return CHANNEL_RC_NO_MEMORY;
1163 Stream_Write(plugin->data_in, pData, dataLength);
1165 if (dataFlags & CHANNEL_FLAG_LAST)
1167 Stream_SealLength(plugin->data_in);
1168 Stream_SetPosition(plugin->data_in, 0);
1172 if (!MessageQueue_Post(plugin->queue, NULL, 0, plugin->data_in, NULL))
1173 return ERROR_INTERNAL_ERROR;
1174 plugin->data_in = NULL;
1178 UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in);
1179 plugin->data_in = NULL;
1185 return CHANNEL_RC_OK;
1188 static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
1189 UINT event, LPVOID pData,
1190 UINT32 dataLength, UINT32 totalLength,
1193 UINT error = CHANNEL_RC_OK;
1194 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam;
1195 WINPR_ASSERT(rdpsnd);
1196 WINPR_ASSERT(!rdpsnd->dynamic);
1200 case CHANNEL_EVENT_DATA_RECEIVED:
1204 if (rdpsnd->OpenHandle != openHandle)
1206 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic));
1209 if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength,
1210 totalLength, dataFlags)))
1212 "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32
1214 rdpsnd_is_dyn_str(rdpsnd->dynamic), error);
1218 case CHANNEL_EVENT_WRITE_CANCELLED:
1219 case CHANNEL_EVENT_WRITE_COMPLETE:
1222 Stream_Free(s, TRUE);
1226 case CHANNEL_EVENT_USER:
1232 if (error && rdpsnd && rdpsnd->rdpcontext)
1235 (void)_snprintf(buffer,
sizeof(buffer),
1236 "%s rdpsnd_virtual_channel_open_event_ex reported an error",
1237 rdpsnd_is_dyn_str(rdpsnd->dynamic));
1238 setChannelError(rdpsnd->rdpcontext, error, buffer);
1247 static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData,
1252 WINPR_UNUSED(pData);
1253 WINPR_UNUSED(dataLength);
1255 WINPR_ASSERT(rdpsnd);
1256 WINPR_ASSERT(!rdpsnd->dynamic);
1258 status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx(
1259 rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex);
1261 if (status != CHANNEL_RC_OK)
1263 WLog_ERR(TAG,
"%s pVirtualChannelOpenEx failed with %s [%08" PRIX32
"]",
1264 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status);
1268 if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK)
1271 rdpsnd->OpenHandle = opened;
1272 return CHANNEL_RC_OK;
1275 rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1276 return CHANNEL_RC_NO_MEMORY;
1279 static void cleanup_internals(rdpsndPlugin* rdpsnd)
1285 StreamPool_Return(rdpsnd->pool, rdpsnd->data_in);
1287 audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
1288 audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
1290 rdpsnd->NumberOfClientFormats = 0;
1291 rdpsnd->ClientFormats = NULL;
1292 rdpsnd->NumberOfServerFormats = 0;
1293 rdpsnd->ServerFormats = NULL;
1295 rdpsnd->data_in = NULL;
1303 static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd)
1307 WINPR_ASSERT(rdpsnd);
1308 WINPR_ASSERT(!rdpsnd->dynamic);
1309 if (rdpsnd->OpenHandle != 0)
1311 DWORD opened = rdpsnd->OpenHandle;
1312 rdpsnd->OpenHandle = 0;
1314 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1316 error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened);
1318 if (CHANNEL_RC_OK != error)
1320 WLog_ERR(TAG,
"%s pVirtualChannelCloseEx failed with %s [%08" PRIX32
"]",
1321 rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error);
1326 cleanup_internals(rdpsnd);
1330 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1331 rdpsnd->device = NULL;
1334 return CHANNEL_RC_OK;
1337 static void queue_free(
void* obj)
1339 wMessage* msg = obj;
1348 static void free_internals(rdpsndPlugin* rdpsnd)
1353 if (rdpsnd->references > 0)
1354 rdpsnd->references--;
1356 if (rdpsnd->references > 0)
1359 freerdp_dsp_context_free(rdpsnd->dsp_context);
1360 StreamPool_Free(rdpsnd->pool);
1361 rdpsnd->pool = NULL;
1362 rdpsnd->dsp_context = NULL;
1365 static BOOL allocate_internals(rdpsndPlugin* rdpsnd)
1367 WINPR_ASSERT(rdpsnd);
1371 rdpsnd->pool = StreamPool_New(TRUE, 4096);
1376 if (!rdpsnd->dsp_context)
1378 rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE);
1379 if (!rdpsnd->dsp_context)
1382 rdpsnd->references++;
1387 static DWORD WINAPI play_thread(LPVOID arg)
1389 UINT error = CHANNEL_RC_OK;
1390 rdpsndPlugin* rdpsnd = arg;
1392 if (!rdpsnd || !rdpsnd->queue)
1393 return ERROR_INVALID_PARAMETER;
1398 wMessage message = { 0 };
1402 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1404 handles[nCount++] = MessageQueue_Event(rdpsnd->queue);
1405 handles[nCount++] = freerdp_abort_event(rdpsnd->rdpcontext);
1406 status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE);
1412 return ERROR_TIMEOUT;
1415 rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE);
1419 if (message.id == WMQ_QUIT)
1423 error = rdpsnd_recv_pdu(rdpsnd, s);
1429 return CHANNEL_RC_OK;
1432 static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd)
1435 return ERROR_INVALID_PARAMETER;
1441 obj.fnObjectFree = queue_free;
1442 rdpsnd->queue = MessageQueue_New(&obj);
1444 return CHANNEL_RC_NO_MEMORY;
1446 rdpsnd->thread = CreateThread(NULL, 0, play_thread, rdpsnd, 0, NULL);
1447 if (!rdpsnd->thread)
1448 return CHANNEL_RC_INITIALIZATION_ERROR;
1451 if (!allocate_internals(rdpsnd))
1452 return CHANNEL_RC_NO_MEMORY;
1454 return CHANNEL_RC_OK;
1457 void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd)
1462 MessageQueue_PostQuit(rdpsnd->queue, 0);
1466 (void)WaitForSingleObject(rdpsnd->thread, INFINITE);
1467 (void)CloseHandle(rdpsnd->thread);
1469 MessageQueue_Free(rdpsnd->queue);
1471 free_internals(rdpsnd);
1472 audio_formats_free(rdpsnd->fixed_format, 1);
1473 free(rdpsnd->subsystem);
1474 free(rdpsnd->device_name);
1475 rdpsnd->InitHandle = 0;
1481 static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1482 UINT event, LPVOID pData,
1485 UINT error = CHANNEL_RC_OK;
1486 rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam;
1491 if (plugin->InitHandle != pInitHandle)
1493 WLog_ERR(TAG,
"%s error no match", rdpsnd_is_dyn_str(plugin->dynamic));
1499 case CHANNEL_EVENT_INITIALIZED:
1500 error = rdpsnd_virtual_channel_event_initialized(plugin);
1503 case CHANNEL_EVENT_CONNECTED:
1504 error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength);
1507 case CHANNEL_EVENT_DISCONNECTED:
1508 error = rdpsnd_virtual_channel_event_disconnected(plugin);
1511 case CHANNEL_EVENT_TERMINATED:
1512 rdpsnd_virtual_channel_event_terminated(plugin);
1516 case CHANNEL_EVENT_ATTACHED:
1517 plugin->attached = TRUE;
1520 case CHANNEL_EVENT_DETACHED:
1521 plugin->attached = FALSE;
1528 if (error && plugin && plugin->rdpcontext)
1531 (void)_snprintf(buffer,
sizeof(buffer),
"%s reported an error",
1532 rdpsnd_is_dyn_str(plugin->dynamic));
1533 setChannelError(plugin->rdpcontext, error, buffer);
1537 rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin)
1542 return plugin->rdpcontext;
1545 static rdpsndPlugin* allocatePlugin(
void)
1547 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1,
sizeof(rdpsndPlugin));
1551 rdpsnd->fixed_format = audio_format_new();
1552 if (!rdpsnd->fixed_format)
1554 rdpsnd->log = WLog_Get(
"com.freerdp.channels.rdpsnd.client");
1558 rdpsnd->attached = TRUE;
1563 audio_format_free(rdpsnd->fixed_format);
1568 FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints,
1572 rdpsndPlugin* rdpsnd = NULL;
1578 rdpsnd = allocatePlugin();
1583 rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP;
1584 (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name),
1585 RDPSND_CHANNEL_NAME);
1589 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1591 rdpsnd->rdpcontext = pEntryPointsEx->context;
1593 FreeRDP_SynchronousStaticChannels))
1594 rdpsnd->async = TRUE;
1597 CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints,
1599 rdpsnd->InitHandle = pInitHandle;
1601 WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx);
1602 rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx(
1603 rdpsnd, NULL, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000,
1604 rdpsnd_virtual_channel_init_event_ex);
1606 if (CHANNEL_RC_OK != rc)
1608 WLog_ERR(TAG,
"%s pVirtualChannelInitEx failed with %s [%08" PRIX32
"]",
1609 rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc);
1610 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1617 static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback)
1620 rdpsndPlugin* rdpsnd = NULL;
1622 WINPR_ASSERT(callback);
1624 rdpsnd = (rdpsndPlugin*)callback->plugin;
1625 WINPR_ASSERT(rdpsnd);
1627 if (rdpsnd->OnOpenCalled)
1628 return CHANNEL_RC_OK;
1629 rdpsnd->OnOpenCalled = TRUE;
1631 if (!allocate_internals(rdpsnd))
1632 return ERROR_OUTOFMEMORY;
1634 return rdpsnd_process_connect(rdpsnd);
1637 static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
1640 rdpsndPlugin* plugin = NULL;
1644 len = Stream_GetRemainingLength(data);
1646 if (!callback || !callback->plugin)
1647 return ERROR_INVALID_PARAMETER;
1648 plugin = (rdpsndPlugin*)callback->plugin;
1649 WINPR_ASSERT(plugin);
1651 copy = StreamPool_Take(plugin->pool, len);
1653 return ERROR_OUTOFMEMORY;
1654 Stream_Copy(data, copy, len);
1655 Stream_SealLength(copy);
1656 Stream_SetPosition(copy, 0);
1660 if (!MessageQueue_Post(plugin->queue, NULL, 0, copy, NULL))
1662 Stream_Release(copy);
1663 return ERROR_INTERNAL_ERROR;
1668 UINT error = rdpsnd_recv_pdu(plugin, copy);
1673 return CHANNEL_RC_OK;
1676 static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1679 rdpsndPlugin* rdpsnd = NULL;
1681 WINPR_ASSERT(callback);
1683 rdpsnd = (rdpsndPlugin*)callback->plugin;
1684 WINPR_ASSERT(rdpsnd);
1686 rdpsnd->OnOpenCalled = FALSE;
1688 IFCALL(rdpsnd->device->Close, rdpsnd->device);
1690 cleanup_internals(rdpsnd);
1694 IFCALL(rdpsnd->device->Free, rdpsnd->device);
1695 rdpsnd->device = NULL;
1698 free_internals(rdpsnd);
1699 free(pChannelCallback);
1700 return CHANNEL_RC_OK;
1704 static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1705 IWTSVirtualChannel* pChannel, BYTE* Data,
1707 IWTSVirtualChannelCallback** ppCallback)
1712 WINPR_ASSERT(listener_callback);
1713 WINPR_ASSERT(pChannel);
1714 WINPR_ASSERT(ppCallback);
1718 WINPR_UNUSED(pbAccept);
1722 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1723 return CHANNEL_RC_NO_MEMORY;
1726 callback->iface.OnOpen = rdpsnd_on_open;
1727 callback->iface.OnDataReceived = rdpsnd_on_data_received;
1728 callback->iface.OnClose = rdpsnd_on_close;
1729 callback->plugin = listener_callback->plugin;
1730 callback->channel_mgr = listener_callback->channel_mgr;
1731 callback->channel = pChannel;
1732 listener_callback->channel_callback = callback;
1733 *ppCallback = &callback->iface;
1734 return CHANNEL_RC_OK;
1737 static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
1740 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1741 WINPR_ASSERT(rdpsnd);
1742 WINPR_ASSERT(pChannelMgr);
1743 if (rdpsnd->initialized)
1745 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME);
1746 return ERROR_INVALID_DATA;
1748 rdpsnd->listener_callback =
1751 if (!rdpsnd->listener_callback)
1753 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1754 return CHANNEL_RC_NO_MEMORY;
1757 rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection;
1758 rdpsnd->listener_callback->plugin = pPlugin;
1759 rdpsnd->listener_callback->channel_mgr = pChannelMgr;
1760 status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0,
1761 &rdpsnd->listener_callback->iface, &(rdpsnd->listener));
1762 if (status != CHANNEL_RC_OK)
1764 WLog_ERR(TAG,
"%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE));
1768 rdpsnd->listener->pInterface = rdpsnd->iface.pInterface;
1769 status = rdpsnd_virtual_channel_event_initialized(rdpsnd);
1771 rdpsnd->initialized = status == CHANNEL_RC_OK;
1780 static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin)
1782 rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin;
1785 if (rdpsnd->listener_callback)
1787 IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr;
1789 IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener);
1791 free(rdpsnd->listener_callback);
1792 free(rdpsnd->iface.pInterface);
1794 rdpsnd_virtual_channel_event_terminated(rdpsnd);
1795 return CHANNEL_RC_OK;
1803 FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1805 UINT error = CHANNEL_RC_OK;
1806 rdpsndPlugin* rdpsnd = NULL;
1808 WINPR_ASSERT(pEntryPoints);
1809 WINPR_ASSERT(pEntryPoints->GetPlugin);
1811 rdpsnd = (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME);
1815 IWTSPlugin* iface = NULL;
1822 rdpsnd = allocatePlugin();
1825 WLog_ERR(TAG,
"%s calloc failed!", rdpsnd_is_dyn_str(TRUE));
1826 return CHANNEL_RC_NO_MEMORY;
1829 iface = &rdpsnd->iface;
1830 iface->Initialize = rdpsnd_plugin_initialize;
1831 iface->Connected = NULL;
1832 iface->Disconnected = NULL;
1833 iface->Terminated = rdpsnd_plugin_terminated;
1835 rdpsnd->dynamic = TRUE;
1837 WINPR_ASSERT(pEntryPoints->GetRdpContext);
1838 rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1841 FreeRDP_SynchronousDynamicChannels))
1842 rdpsnd->async = TRUE;
1845 cnv.cev = pEntryPoints->GetPluginData(pEntryPoints);
1846 WINPR_ASSERT(pEntryPoints->GetPluginData);
1847 rdpsnd->channelEntryPoints.pExtendedData = cnv.ev;
1849 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface);
1853 WLog_ERR(TAG,
"%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE));
1854 return CHANNEL_RC_BAD_CHANNEL;
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.