22#include <freerdp/config.h>
29#include <winpr/assert.h>
30#include <winpr/cast.h>
31#include <winpr/print.h>
32#include <winpr/stream.h>
34#include <freerdp/freerdp.h>
35#include <freerdp/channels/log.h>
37#include "rdpsnd_common.h"
38#include "rdpsnd_main.h"
40static wStream* rdpsnd_server_get_buffer(RdpsndServerContext* context)
43 WINPR_ASSERT(context);
44 WINPR_ASSERT(context->priv);
46 s = context->priv->rdpsnd_pdu;
47 Stream_ResetPosition(s);
56static UINT rdpsnd_server_send_formats(RdpsndServerContext* context)
58 wStream* s = rdpsnd_server_get_buffer(context);
62 if (!Stream_EnsureRemainingCapacity(s, 24))
63 return ERROR_OUTOFMEMORY;
65 Stream_Write_UINT8(s, SNDC_FORMATS);
66 Stream_Write_UINT8(s, 0);
67 Stream_Seek_UINT16(s);
68 Stream_Write_UINT32(s, 0);
69 Stream_Write_UINT32(s, 0);
70 Stream_Write_UINT32(s, 0);
71 Stream_Write_UINT16(s, 0);
73 s, WINPR_ASSERTING_INT_CAST(uint16_t, context->num_server_formats));
74 Stream_Write_UINT8(s, context->block_no);
75 Stream_Write_UINT16(s, CHANNEL_VERSION_WIN_MAX);
76 Stream_Write_UINT8(s, 0);
78 for (
size_t i = 0; i < context->num_server_formats; i++)
80 const AUDIO_FORMAT* format = &context->server_formats[i];
82 if (!audio_format_write(s, format))
87 const size_t pos = Stream_GetPosition(s);
91 WINPR_ASSERT(pos >= 4);
92 if (!Stream_SetPosition(s, 2))
94 Stream_Write_UINT16(s, (UINT16)(pos - 4));
95 if (!Stream_SetPosition(s, pos))
98 WINPR_ASSERT(context->priv);
100 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
101 (UINT32)pos, &written);
102 Stream_ResetPosition(s);
105 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
113static UINT rdpsnd_server_recv_waveconfirm(RdpsndServerContext* context,
wStream* s)
115 UINT16 timestamp = 0;
116 BYTE confirmBlockNum = 0;
117 UINT error = CHANNEL_RC_OK;
119 WINPR_ASSERT(context);
121 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
122 return ERROR_INVALID_DATA;
124 Stream_Read_UINT16(s, timestamp);
125 Stream_Read_UINT8(s, confirmBlockNum);
126 Stream_Seek_UINT8(s);
127 IFCALLRET(context->ConfirmBlock, error, context, confirmBlockNum, timestamp);
130 WLog_ERR(TAG,
"context->ConfirmBlock failed with error %" PRIu32
"", error);
140static UINT rdpsnd_server_recv_trainingconfirm(RdpsndServerContext* context,
wStream* s)
142 UINT16 timestamp = 0;
144 UINT error = CHANNEL_RC_OK;
146 WINPR_ASSERT(context);
148 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
149 return ERROR_INVALID_DATA;
151 Stream_Read_UINT16(s, timestamp);
152 Stream_Read_UINT16(s, packsize);
154 IFCALLRET(context->TrainingConfirm, error, context, timestamp, packsize);
156 WLog_ERR(TAG,
"context->TrainingConfirm failed with error %" PRIu32
"", error);
166static UINT rdpsnd_server_recv_quality_mode(RdpsndServerContext* context,
wStream* s)
168 WINPR_ASSERT(context);
170 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
172 WLog_ERR(TAG,
"not enough data in stream!");
173 return ERROR_INVALID_DATA;
176 Stream_Read_UINT16(s, context->qualityMode);
177 Stream_Seek_UINT16(s);
179 WLog_DBG(TAG,
"Client requested sound quality: 0x%04" PRIX16
"", context->qualityMode);
181 return CHANNEL_RC_OK;
184static void rdpsnd_server_client_format_free(RdpsndServerContext* context)
186 WINPR_ASSERT(context);
187 audio_formats_free(context->client_formats, context->num_client_formats);
188 context->client_formats =
nullptr;
189 context->num_client_formats = 0;
197static UINT rdpsnd_server_recv_formats(RdpsndServerContext* context,
wStream* s)
199 UINT error = CHANNEL_RC_OK;
201 WINPR_ASSERT(context);
203 rdpsnd_server_client_format_free(context);
205 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
206 return ERROR_INVALID_DATA;
208 Stream_Read_UINT32(s, context->capsFlags);
209 Stream_Read_UINT32(s, context->initialVolume);
210 Stream_Read_UINT32(s, context->initialPitch);
211 Stream_Read_UINT16(s, context->udpPort);
212 Stream_Read_UINT16(s, context->num_client_formats);
213 Stream_Read_UINT8(s, context->lastblock);
214 Stream_Read_UINT16(s, context->clientVersion);
215 Stream_Seek_UINT8(s);
218 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, context->num_client_formats, 18ull))
219 return ERROR_INVALID_DATA;
221 if (!context->num_client_formats)
223 WLog_ERR(TAG,
"client doesn't support any format!");
224 return ERROR_INTERNAL_ERROR;
227 context->client_formats = audio_formats_new(context->num_client_formats);
229 if (!context->client_formats)
231 WLog_ERR(TAG,
"calloc failed!");
232 return CHANNEL_RC_NO_MEMORY;
235 for (UINT16 i = 0; i < context->num_client_formats; i++)
238 audio_format_free(format);
240 if (!audio_format_read(s, format))
248 if ((format->nChannels == 0) || (format->nBlockAlign == 0))
250 WLog_ERR(TAG,
"invalid client audio format: nChannels or nBlockAlign is 0");
251 error = ERROR_INVALID_DATA;
256 switch (format->wFormatTag)
258 case WAVE_FORMAT_DVI_ADPCM:
259 if (format->nBlockAlign < 4)
262 "invalid client audio format %s: nBlockAlign is %" PRIu32
264 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign);
265 error = ERROR_INVALID_DATA;
269 case WAVE_FORMAT_ADPCM:
270 if (format->nBlockAlign < 8)
273 "invalid client audio format %s: nBlockAlign is %" PRIu32
275 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign);
276 error = ERROR_INVALID_DATA;
285 if (!context->num_client_formats)
287 WLog_ERR(TAG,
"client doesn't support any known format!");
291 return CHANNEL_RC_OK;
293 rdpsnd_server_client_format_free(context);
297static DWORD WINAPI rdpsnd_server_thread(LPVOID arg)
301 HANDLE events[2] = WINPR_C_ARRAY_INIT;
302 RdpsndServerContext* context = (RdpsndServerContext*)arg;
303 UINT error = CHANNEL_RC_OK;
305 WINPR_ASSERT(context);
306 WINPR_ASSERT(context->priv);
308 events[nCount++] = context->priv->channelEvent;
309 events[nCount++] = context->priv->StopEvent;
311 WINPR_ASSERT(nCount <= ARRAYSIZE(events));
315 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
317 if (status == WAIT_FAILED)
319 error = GetLastError();
320 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
324 status = WaitForSingleObject(context->priv->StopEvent, 0);
326 if (status == WAIT_FAILED)
328 error = GetLastError();
329 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
333 if (status == WAIT_OBJECT_0)
336 if ((error = rdpsnd_server_handle_messages(context)))
338 WLog_ERR(TAG,
"rdpsnd_server_handle_messages failed with error %" PRIu32
"", error);
343 if (error && context->rdpcontext)
344 setChannelError(context->rdpcontext, error,
"rdpsnd_server_thread reported an error");
355static UINT rdpsnd_server_initialize(RdpsndServerContext* context, BOOL ownThread)
357 WINPR_ASSERT(context);
358 WINPR_ASSERT(context->priv);
360 context->priv->ownThread = ownThread;
361 return context->Start(context);
369static UINT rdpsnd_server_select_format(RdpsndServerContext* context, UINT16 client_format_index)
371 UINT error = CHANNEL_RC_OK;
373 WINPR_ASSERT(context);
374 WINPR_ASSERT(context->priv);
376 if ((client_format_index >= context->num_client_formats) || (!context->src_format))
378 WLog_ERR(TAG,
"index %" PRIu16
" is not correct.", client_format_index);
379 return ERROR_INVALID_DATA;
382 EnterCriticalSection(&context->priv->lock);
383 context->priv->src_bytes_per_sample = context->src_format->wBitsPerSample / 8;
384 context->priv->src_bytes_per_frame =
385 context->priv->src_bytes_per_sample * context->src_format->nChannels;
386 context->selected_client_format = client_format_index;
388 const AUDIO_FORMAT* format = &context->client_formats[client_format_index];
390 if (format->nSamplesPerSec == 0)
392 WLog_ERR(TAG,
"invalid Client Sound Format!!");
393 error = ERROR_INVALID_DATA;
397 if (context->latency <= 0)
398 context->latency = 50;
400 context->priv->out_frames = context->src_format->nSamplesPerSec * context->latency / 1000;
402 if (context->priv->out_frames < 1)
403 context->priv->out_frames = 1;
405 switch (format->wFormatTag)
407 case WAVE_FORMAT_DVI_ADPCM:
409 WINPR_ASSERT(format->nBlockAlign >= 4);
410 WINPR_ASSERT(format->nChannels > 0);
411 const UINT64 bs = 4ULL * (format->nBlockAlign - 4ULL * format->nChannels);
412 WINPR_ASSERT(bs > 0);
414 context->priv->out_frames -= context->priv->out_frames % bs;
416 if (context->priv->out_frames < bs)
417 context->priv->out_frames = bs;
421 case WAVE_FORMAT_ADPCM:
423 WINPR_ASSERT(format->nBlockAlign >= 8);
424 WINPR_ASSERT(format->nChannels > 0);
427 (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
428 WINPR_ASSERT(bs > 0);
429 context->priv->out_frames -= context->priv->out_frames % bs;
431 if (context->priv->out_frames < bs)
432 context->priv->out_frames = bs;
439 context->priv->out_pending_frames = 0;
440 const size_t out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
442 if (context->priv->out_buffer_size < out_buffer_size)
444 BYTE* newBuffer = (BYTE*)realloc(context->priv->out_buffer, out_buffer_size);
448 WLog_ERR(TAG,
"realloc failed!");
449 error = CHANNEL_RC_NO_MEMORY;
453 context->priv->out_buffer = newBuffer;
454 context->priv->out_buffer_size = out_buffer_size;
457 if (!freerdp_dsp_context_reset(context->priv->dsp_context, format, 0u))
458 error = ERROR_INTERNAL_ERROR;
460 LeaveCriticalSection(&context->priv->lock);
469static UINT rdpsnd_server_training(RdpsndServerContext* context, UINT16 timestamp, UINT16 packsize,
474 wStream* s = rdpsnd_server_get_buffer(context);
476 if (!Stream_EnsureRemainingCapacity(s, 8))
477 return ERROR_INTERNAL_ERROR;
479 Stream_Write_UINT8(s, SNDC_TRAINING);
480 Stream_Write_UINT8(s, 0);
481 Stream_Seek_UINT16(s);
482 Stream_Write_UINT16(s, timestamp);
483 Stream_Write_UINT16(s, packsize);
487 if (!Stream_EnsureRemainingCapacity(s, packsize))
489 Stream_ResetPosition(s);
490 return ERROR_INTERNAL_ERROR;
493 Stream_Write(s, data, packsize);
496 const size_t end = Stream_GetPosition(s);
497 if ((end < 4) || (end > UINT16_MAX))
498 return ERROR_INTERNAL_ERROR;
500 if (!Stream_SetPosition(s, 2))
501 return ERROR_INTERNAL_ERROR;
502 Stream_Write_UINT16(s, (UINT16)(end - 4));
504 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
505 (UINT32)end, &written);
507 Stream_ResetPosition(s);
509 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
512static BOOL rdpsnd_server_align_wave_pdu(
wStream* s, UINT32 alignment)
515 Stream_SealLength(s);
516 size = Stream_Length(s);
518 if ((size % alignment) != 0)
520 size_t offset = alignment - size % alignment;
522 if (!Stream_EnsureRemainingCapacity(s, offset))
525 Stream_Zero(s, offset);
528 Stream_SealLength(s);
538static UINT rdpsnd_server_send_wave_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
541 UINT error = CHANNEL_RC_OK;
542 wStream* s = rdpsnd_server_get_buffer(context);
544 if (context->selected_client_format > context->num_client_formats)
545 return ERROR_INTERNAL_ERROR;
547 WINPR_ASSERT(context->client_formats);
549 const AUDIO_FORMAT* format = &context->client_formats[context->selected_client_format];
551 Stream_ResetPosition(s);
553 if (!Stream_EnsureRemainingCapacity(s, 16))
554 return ERROR_OUTOFMEMORY;
556 Stream_Write_UINT8(s, SNDC_WAVE);
557 Stream_Write_UINT8(s, 0);
558 Stream_Write_UINT16(s, 0);
559 Stream_Write_UINT16(s, wTimestamp);
560 Stream_Write_UINT16(s, context->selected_client_format);
561 Stream_Write_UINT8(s, context->block_no);
563 const size_t start = Stream_GetPosition(s);
564 const BYTE* src = context->priv->out_buffer;
565 const size_t length =
566 1ull * context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
568 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
569 return ERROR_INTERNAL_ERROR;
572 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
573 return ERROR_INTERNAL_ERROR;
575 const size_t end = Stream_GetPosition(s);
576 const size_t pos = end - start + 8ULL;
577 if (pos > UINT16_MAX)
578 return ERROR_INTERNAL_ERROR;
579 if (!Stream_SetPosition(s, 2))
580 return ERROR_INTERNAL_ERROR;
581 Stream_Write_UINT16(s, (UINT16)pos);
582 if (!Stream_SetPosition(s, end))
583 return ERROR_INTERNAL_ERROR;
585 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
586 (UINT32)(start + 4), &written))
588 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
589 error = ERROR_INTERNAL_ERROR;
592 if (error != CHANNEL_RC_OK)
594 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
595 error = ERROR_INTERNAL_ERROR;
599 if (!Stream_SetPosition(s, start))
601 error = ERROR_INTERNAL_ERROR;
604 Stream_Write_UINT32(s, 0);
605 if (!Stream_SetPosition(s, start))
607 error = ERROR_INTERNAL_ERROR;
611 WINPR_ASSERT((end - start) <= UINT32_MAX);
612 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_Pointer(s),
613 (UINT32)(end - start), &written))
615 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
616 error = ERROR_INTERNAL_ERROR;
619 context->block_no = (context->block_no + 1) % 256;
622 Stream_ResetPosition(s);
623 context->priv->out_pending_frames = 0;
633static UINT rdpsnd_server_send_wave2_pdu(RdpsndServerContext* context, UINT16 formatNo,
634 const BYTE* data,
size_t size, BOOL encoded,
635 UINT16 timestamp, UINT32 audioTimeStamp)
638 UINT error = CHANNEL_RC_OK;
639 wStream* s = rdpsnd_server_get_buffer(context);
641 if (!Stream_EnsureRemainingCapacity(s, 16))
643 error = ERROR_INTERNAL_ERROR;
648 Stream_Write_UINT8(s, SNDC_WAVE2);
649 Stream_Write_UINT8(s, 0);
650 Stream_Write_UINT16(s, 0);
651 Stream_Write_UINT16(s, timestamp);
652 Stream_Write_UINT16(s, formatNo);
653 Stream_Write_UINT8(s, context->block_no);
654 Stream_Write_UINT8(s, 0);
655 Stream_Write_UINT8(s, 0);
656 Stream_Write_UINT8(s, 0);
657 Stream_Write_UINT32(s, audioTimeStamp);
661 if (!Stream_EnsureRemainingCapacity(s, size))
663 error = ERROR_INTERNAL_ERROR;
667 Stream_Write(s, data, size);
671 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, data, size, s))
673 error = ERROR_INTERNAL_ERROR;
677 const AUDIO_FORMAT* format = &context->client_formats[formatNo];
678 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
680 error = ERROR_INTERNAL_ERROR;
686 const size_t end = Stream_GetPosition(s);
687 if (end > UINT16_MAX + 4)
689 error = ERROR_INTERNAL_ERROR;
693 if (!Stream_SetPosition(s, 2))
695 error = ERROR_INTERNAL_ERROR;
698 Stream_Write_UINT16(s, (UINT16)(end - 4));
700 const BOOL status = WTSVirtualChannelWrite(context->priv->ChannelHandle,
701 Stream_BufferAs(s,
char), (UINT32)end, &written);
703 if (!status || (end != written))
706 "WTSVirtualChannelWrite failed! [stream length=%" PRIuz
" - written=%" PRIu32,
708 error = ERROR_INTERNAL_ERROR;
712 context->block_no = (context->block_no + 1) % 256;
715 Stream_ResetPosition(s);
716 context->priv->out_pending_frames = 0;
721static UINT rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
723 const BYTE* src =
nullptr;
726 WINPR_ASSERT(context);
727 WINPR_ASSERT(context->priv);
729 if (context->selected_client_format >= context->num_client_formats)
730 return ERROR_INTERNAL_ERROR;
732 src = context->priv->out_buffer;
733 length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
735 if (context->clientVersion >= CHANNEL_VERSION_WIN_8)
736 return rdpsnd_server_send_wave2_pdu(context, context->selected_client_format, src, length,
737 FALSE, wTimestamp, wTimestamp);
739 return rdpsnd_server_send_wave_pdu(context, wTimestamp);
747static UINT rdpsnd_server_send_samples(RdpsndServerContext* context,
const void* buf,
748 size_t nframes, UINT16 wTimestamp)
750 UINT error = CHANNEL_RC_OK;
752 WINPR_ASSERT(context);
753 WINPR_ASSERT(context->priv);
755 EnterCriticalSection(&context->priv->lock);
757 if (context->selected_client_format >= context->num_client_formats)
760 WLog_WARN(TAG,
"Drop samples because client format has not been negotiated.");
761 error = ERROR_NOT_READY;
767 const size_t cframes =
768 MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
769 size_t cframesize = cframes * context->priv->src_bytes_per_frame;
770 CopyMemory(context->priv->out_buffer +
771 (context->priv->out_pending_frames * context->priv->src_bytes_per_frame),
773 buf = (
const BYTE*)buf + cframesize;
775 context->priv->out_pending_frames += cframes;
777 if (context->priv->out_pending_frames >= context->priv->out_frames)
779 if ((error = rdpsnd_server_send_audio_pdu(context, wTimestamp)))
781 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
788 LeaveCriticalSection(&context->priv->lock);
797static UINT rdpsnd_server_send_samples2(RdpsndServerContext* context, UINT16 formatNo,
798 const void* buf,
size_t size, UINT16 timestamp,
799 UINT32 audioTimeStamp)
801 UINT error = CHANNEL_RC_OK;
803 WINPR_ASSERT(context);
804 WINPR_ASSERT(context->priv);
806 if (context->clientVersion < CHANNEL_VERSION_WIN_8)
807 return ERROR_INTERNAL_ERROR;
809 EnterCriticalSection(&context->priv->lock);
812 rdpsnd_server_send_wave2_pdu(context, formatNo, buf, size, TRUE, timestamp, audioTimeStamp);
814 LeaveCriticalSection(&context->priv->lock);
824static UINT rdpsnd_server_set_volume(RdpsndServerContext* context, UINT16 left, UINT16 right)
828 wStream* s = rdpsnd_server_get_buffer(context);
830 if (!Stream_EnsureRemainingCapacity(s, 8))
831 return ERROR_NOT_ENOUGH_MEMORY;
833 Stream_Write_UINT8(s, SNDC_SETVOLUME);
834 Stream_Write_UINT8(s, 0);
835 Stream_Write_UINT16(s, 4);
836 Stream_Write_UINT16(s, left);
837 Stream_Write_UINT16(s, right);
839 const size_t len = Stream_GetPosition(s);
840 WINPR_ASSERT(len <= UINT32_MAX);
841 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
842 (ULONG)len, &written);
843 Stream_ResetPosition(s);
844 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
852static UINT rdpsnd_server_close(RdpsndServerContext* context)
856 UINT error = CHANNEL_RC_OK;
857 wStream* s = rdpsnd_server_get_buffer(context);
859 EnterCriticalSection(&context->priv->lock);
861 if (context->priv->out_pending_frames > 0)
863 if (context->selected_client_format >= context->num_client_formats)
865 WLog_ERR(TAG,
"Pending audio frame exists while no format selected.");
866 error = ERROR_INVALID_DATA;
868 else if ((error = rdpsnd_server_send_audio_pdu(context, 0)))
870 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
874 LeaveCriticalSection(&context->priv->lock);
879 context->selected_client_format = 0xFFFF;
881 if (!Stream_EnsureRemainingCapacity(s, 4))
882 return ERROR_OUTOFMEMORY;
884 Stream_Write_UINT8(s, SNDC_CLOSE);
885 Stream_Write_UINT8(s, 0);
886 Stream_Seek_UINT16(s);
887 const size_t pos = Stream_GetPosition(s);
888 WINPR_ASSERT(pos >= 4);
889 if (!Stream_SetPosition(s, 2))
890 return ERROR_INVALID_DATA;
891 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, pos - 4));
892 if (!Stream_SetPosition(s, pos))
893 return ERROR_INVALID_DATA;
895 const size_t len = Stream_GetPosition(s);
896 WINPR_ASSERT(len <= UINT32_MAX);
897 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
898 (UINT32)len, &written);
899 Stream_ResetPosition(s);
900 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
908static UINT rdpsnd_server_start(RdpsndServerContext* context)
910 void* buffer =
nullptr;
911 DWORD bytesReturned = 0;
912 RdpsndServerPrivate* priv =
nullptr;
913 UINT error = ERROR_INTERNAL_ERROR;
914 PULONG pSessionId =
nullptr;
916 WINPR_ASSERT(context);
917 WINPR_ASSERT(context->priv);
919 priv = context->priv;
920 priv->SessionId = WTS_CURRENT_SESSION;
922 if (context->use_dynamic_virtual_channel)
924 UINT32 channelId = 0;
927 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
928 (LPSTR*)&pSessionId, &bytesReturned))
930 priv->SessionId = (DWORD)*pSessionId;
931 WTSFreeMemory(pSessionId);
932 priv->ChannelHandle = WTSVirtualChannelOpenEx(priv->SessionId, RDPSND_DVC_CHANNEL_NAME,
933 WTS_CHANNEL_OPTION_DYNAMIC);
934 if (!priv->ChannelHandle)
936 WLog_ERR(TAG,
"Open audio dynamic virtual channel (%s) failed!",
937 RDPSND_DVC_CHANNEL_NAME);
938 return ERROR_INTERNAL_ERROR;
941 channelId = WTSChannelGetIdByHandle(priv->ChannelHandle);
943 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
946 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
952 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
953 return ERROR_INTERNAL_ERROR;
958 priv->ChannelHandle =
959 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, RDPSND_CHANNEL_NAME);
960 if (!priv->ChannelHandle)
962 WLog_ERR(TAG,
"Open audio static virtual channel (rdpsnd) failed!");
963 return ERROR_INTERNAL_ERROR;
967 if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
969 (bytesReturned !=
sizeof(HANDLE)))
972 "error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned "
977 WTSFreeMemory(buffer);
982 priv->channelEvent = *(HANDLE*)buffer;
983 WTSFreeMemory(buffer);
984 priv->rdpsnd_pdu = Stream_New(
nullptr, 4096);
986 if (!priv->rdpsnd_pdu)
988 WLog_ERR(TAG,
"Stream_New failed!");
989 error = CHANNEL_RC_NO_MEMORY;
993 if (!InitializeCriticalSectionEx(&context->priv->lock, 0, 0))
995 WLog_ERR(TAG,
"InitializeCriticalSectionEx failed!");
999 if ((error = rdpsnd_server_send_formats(context)))
1001 WLog_ERR(TAG,
"rdpsnd_server_send_formats failed with error %" PRIu32
"", error);
1005 if (priv->ownThread)
1007 context->priv->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1009 if (!context->priv->StopEvent)
1011 WLog_ERR(TAG,
"CreateEvent failed!");
1015 context->priv->Thread =
1016 CreateThread(
nullptr, 0, rdpsnd_server_thread, (
void*)context, 0,
nullptr);
1018 if (!context->priv->Thread)
1020 WLog_ERR(TAG,
"CreateThread failed!");
1025 return CHANNEL_RC_OK;
1027 (void)CloseHandle(context->priv->StopEvent);
1028 context->priv->StopEvent =
nullptr;
1030 DeleteCriticalSection(&context->priv->lock);
1032 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1033 context->priv->rdpsnd_pdu =
nullptr;
1035 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1036 context->priv->ChannelHandle =
nullptr;
1045static UINT rdpsnd_server_stop(RdpsndServerContext* context)
1047 UINT error = CHANNEL_RC_OK;
1049 WINPR_ASSERT(context);
1050 WINPR_ASSERT(context->priv);
1052 if (!context->priv->StopEvent)
1055 if (context->priv->ownThread)
1057 if (context->priv->StopEvent)
1059 (void)SetEvent(context->priv->StopEvent);
1061 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
1063 error = GetLastError();
1064 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
1068 (void)CloseHandle(context->priv->Thread);
1069 (void)CloseHandle(context->priv->StopEvent);
1070 context->priv->Thread =
nullptr;
1071 context->priv->StopEvent =
nullptr;
1075 DeleteCriticalSection(&context->priv->lock);
1077 if (context->priv->rdpsnd_pdu)
1079 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1080 context->priv->rdpsnd_pdu =
nullptr;
1083 if (context->priv->ChannelHandle)
1085 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1086 context->priv->ChannelHandle =
nullptr;
1092RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm)
1094 RdpsndServerPrivate* priv =
nullptr;
1095 RdpsndServerContext* context = (RdpsndServerContext*)calloc(1,
sizeof(RdpsndServerContext));
1101 context->Start = rdpsnd_server_start;
1102 context->Stop = rdpsnd_server_stop;
1103 context->selected_client_format = 0xFFFF;
1104 context->Initialize = rdpsnd_server_initialize;
1105 context->SendFormats = rdpsnd_server_send_formats;
1106 context->SelectFormat = rdpsnd_server_select_format;
1107 context->Training = rdpsnd_server_training;
1108 context->SendSamples = rdpsnd_server_send_samples;
1109 context->SendSamples2 = rdpsnd_server_send_samples2;
1110 context->SetVolume = rdpsnd_server_set_volume;
1111 context->Close = rdpsnd_server_close;
1112 context->priv = priv = (RdpsndServerPrivate*)calloc(1,
sizeof(RdpsndServerPrivate));
1116 WLog_ERR(TAG,
"calloc failed!");
1120 priv->dsp_context = freerdp_dsp_context_new(TRUE);
1122 if (!priv->dsp_context)
1124 WLog_ERR(TAG,
"freerdp_dsp_context_new failed!");
1128 priv->input_stream = Stream_New(
nullptr, 4);
1130 if (!priv->input_stream)
1132 WLog_ERR(TAG,
"Stream_New failed!");
1136 priv->expectedBytes = 4;
1137 priv->waitingHeader = TRUE;
1138 priv->ownThread = TRUE;
1141 WINPR_PRAGMA_DIAG_PUSH
1142 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1143 rdpsnd_server_context_free(context);
1144 WINPR_PRAGMA_DIAG_POP
1148void rdpsnd_server_context_reset(RdpsndServerContext* context)
1150 WINPR_ASSERT(context);
1151 WINPR_ASSERT(context->priv);
1153 context->priv->expectedBytes = 4;
1154 context->priv->waitingHeader = TRUE;
1155 Stream_ResetPosition(context->priv->input_stream);
1158void rdpsnd_server_context_free(RdpsndServerContext* context)
1165 rdpsnd_server_stop(context);
1167 free(context->priv->out_buffer);
1169 if (context->priv->dsp_context)
1170 freerdp_dsp_context_free(context->priv->dsp_context);
1172 if (context->priv->input_stream)
1173 Stream_Free(context->priv->input_stream, TRUE);
1176 free(context->server_formats);
1177 rdpsnd_server_client_format_free(context);
1178 free(context->priv);
1182HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext* context)
1184 WINPR_ASSERT(context);
1185 WINPR_ASSERT(context->priv);
1187 return context->priv->channelEvent;
1204UINT rdpsnd_server_handle_messages(RdpsndServerContext* context)
1206 DWORD bytesReturned = 0;
1207 UINT ret = CHANNEL_RC_OK;
1208 RdpsndServerPrivate* priv =
nullptr;
1211 WINPR_ASSERT(context);
1212 WINPR_ASSERT(context->priv);
1214 priv = context->priv;
1215 s = priv->input_stream;
1217 if (!WTSVirtualChannelRead(priv->ChannelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
1220 if (GetLastError() == ERROR_NO_DATA)
1221 return ERROR_NO_DATA;
1223 WLog_ERR(TAG,
"channel connection closed");
1224 return ERROR_INTERNAL_ERROR;
1227 priv->expectedBytes -= bytesReturned;
1228 Stream_Seek(s, bytesReturned);
1230 if (priv->expectedBytes)
1231 return CHANNEL_RC_OK;
1233 Stream_SealLength(s);
1234 Stream_ResetPosition(s);
1236 if (priv->waitingHeader)
1239 Stream_Read_UINT8(s, priv->msgType);
1240 Stream_Seek_UINT8(s);
1241 Stream_Read_UINT16(s, priv->expectedBytes);
1242 priv->waitingHeader = FALSE;
1243 Stream_ResetPosition(s);
1245 if (priv->expectedBytes)
1247 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
1249 WLog_ERR(TAG,
"Stream_EnsureCapacity failed!");
1250 return CHANNEL_RC_NO_MEMORY;
1253 return CHANNEL_RC_OK;
1258#ifdef WITH_DEBUG_SND
1259 WLog_DBG(TAG,
"message type %" PRIu8
"", priv->msgType);
1261 priv->expectedBytes = 4;
1262 priv->waitingHeader = TRUE;
1264 switch (priv->msgType)
1266 case SNDC_WAVECONFIRM:
1267 ret = rdpsnd_server_recv_waveconfirm(context, s);
1271 ret = rdpsnd_server_recv_trainingconfirm(context, s);
1275 ret = rdpsnd_server_recv_formats(context, s);
1277 if ((ret == CHANNEL_RC_OK) && (context->clientVersion < CHANNEL_VERSION_WIN_7))
1278 IFCALL(context->Activated, context);
1282 case SNDC_QUALITYMODE:
1283 ret = rdpsnd_server_recv_quality_mode(context, s);
1285 if ((ret == CHANNEL_RC_OK) && (context->clientVersion >= CHANNEL_VERSION_WIN_7))
1286 IFCALL(context->Activated, context);
1291 WLog_ERR(TAG,
"UNKNOWN MESSAGE TYPE!! (0x%02" PRIX8
")", priv->msgType);
1292 ret = ERROR_INVALID_DATA;
1296 Stream_ResetPosition(s);