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 if (format->nBlockAlign / 4ull < format->nChannels)
411 if (format->nChannels == 0)
418 const size_t bs = 4ULL * (format->nBlockAlign - 4ULL * format->nChannels);
420 context->priv->out_frames -= context->priv->out_frames % bs;
422 if (context->priv->out_frames < bs)
423 context->priv->out_frames = bs;
427 case WAVE_FORMAT_ADPCM:
429 if (format->nBlockAlign / 7 < format->nChannels)
431 if (format->nChannels == 0)
440 (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
442 context->priv->out_frames -= context->priv->out_frames % bs;
444 if (context->priv->out_frames < bs)
445 context->priv->out_frames = bs;
452 context->priv->out_pending_frames = 0;
453 const size_t out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
455 if (context->priv->out_buffer_size < out_buffer_size)
457 BYTE* newBuffer = (BYTE*)realloc(context->priv->out_buffer, out_buffer_size);
461 WLog_ERR(TAG,
"realloc failed!");
462 error = CHANNEL_RC_NO_MEMORY;
466 context->priv->out_buffer = newBuffer;
467 context->priv->out_buffer_size = out_buffer_size;
470 if (!freerdp_dsp_context_reset(context->priv->dsp_context, format, 0u))
471 error = ERROR_INTERNAL_ERROR;
473 LeaveCriticalSection(&context->priv->lock);
482static UINT rdpsnd_server_training(RdpsndServerContext* context, UINT16 timestamp, UINT16 packsize,
487 wStream* s = rdpsnd_server_get_buffer(context);
489 if (!Stream_EnsureRemainingCapacity(s, 8))
490 return ERROR_INTERNAL_ERROR;
492 Stream_Write_UINT8(s, SNDC_TRAINING);
493 Stream_Write_UINT8(s, 0);
494 Stream_Seek_UINT16(s);
495 Stream_Write_UINT16(s, timestamp);
496 Stream_Write_UINT16(s, packsize);
500 if (!Stream_EnsureRemainingCapacity(s, packsize))
502 Stream_ResetPosition(s);
503 return ERROR_INTERNAL_ERROR;
506 Stream_Write(s, data, packsize);
509 const size_t end = Stream_GetPosition(s);
510 if ((end < 4) || (end > UINT16_MAX))
511 return ERROR_INTERNAL_ERROR;
513 if (!Stream_SetPosition(s, 2))
514 return ERROR_INTERNAL_ERROR;
515 Stream_Write_UINT16(s, (UINT16)(end - 4));
517 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
518 (UINT32)end, &written);
520 Stream_ResetPosition(s);
522 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
525static BOOL rdpsnd_server_align_wave_pdu(
wStream* s, UINT32 alignment)
528 Stream_SealLength(s);
529 size = Stream_Length(s);
531 if ((size % alignment) != 0)
533 size_t offset = alignment - size % alignment;
535 if (!Stream_EnsureRemainingCapacity(s, offset))
538 Stream_Zero(s, offset);
541 Stream_SealLength(s);
551static UINT rdpsnd_server_send_wave_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
554 UINT error = CHANNEL_RC_OK;
555 wStream* s = rdpsnd_server_get_buffer(context);
557 if (context->selected_client_format > context->num_client_formats)
558 return ERROR_INTERNAL_ERROR;
560 WINPR_ASSERT(context->client_formats);
562 const AUDIO_FORMAT* format = &context->client_formats[context->selected_client_format];
564 Stream_ResetPosition(s);
566 if (!Stream_EnsureRemainingCapacity(s, 16))
567 return ERROR_OUTOFMEMORY;
569 Stream_Write_UINT8(s, SNDC_WAVE);
570 Stream_Write_UINT8(s, 0);
571 Stream_Write_UINT16(s, 0);
572 Stream_Write_UINT16(s, wTimestamp);
573 Stream_Write_UINT16(s, context->selected_client_format);
574 Stream_Write_UINT8(s, context->block_no);
576 const size_t start = Stream_GetPosition(s);
577 const BYTE* src = context->priv->out_buffer;
578 const size_t length =
579 1ull * context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
581 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
582 return ERROR_INTERNAL_ERROR;
585 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
586 return ERROR_INTERNAL_ERROR;
588 const size_t end = Stream_GetPosition(s);
589 const size_t pos = end - start + 8ULL;
590 if (pos > UINT16_MAX)
591 return ERROR_INTERNAL_ERROR;
592 if (!Stream_SetPosition(s, 2))
593 return ERROR_INTERNAL_ERROR;
594 Stream_Write_UINT16(s, (UINT16)pos);
595 if (!Stream_SetPosition(s, end))
596 return ERROR_INTERNAL_ERROR;
598 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
599 (UINT32)(start + 4), &written))
601 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
602 error = ERROR_INTERNAL_ERROR;
605 if (error != CHANNEL_RC_OK)
607 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
608 error = ERROR_INTERNAL_ERROR;
612 if (!Stream_SetPosition(s, start))
614 error = ERROR_INTERNAL_ERROR;
617 Stream_Write_UINT32(s, 0);
618 if (!Stream_SetPosition(s, start))
620 error = ERROR_INTERNAL_ERROR;
624 WINPR_ASSERT((end - start) <= UINT32_MAX);
625 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_Pointer(s),
626 (UINT32)(end - start), &written))
628 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
629 error = ERROR_INTERNAL_ERROR;
632 context->block_no = (context->block_no + 1) % 256;
635 Stream_ResetPosition(s);
636 context->priv->out_pending_frames = 0;
646static UINT rdpsnd_server_send_wave2_pdu(RdpsndServerContext* context, UINT16 formatNo,
647 const BYTE* data,
size_t size, BOOL encoded,
648 UINT16 timestamp, UINT32 audioTimeStamp)
651 UINT error = CHANNEL_RC_OK;
652 wStream* s = rdpsnd_server_get_buffer(context);
654 if (!Stream_EnsureRemainingCapacity(s, 16))
656 error = ERROR_INTERNAL_ERROR;
661 Stream_Write_UINT8(s, SNDC_WAVE2);
662 Stream_Write_UINT8(s, 0);
663 Stream_Write_UINT16(s, 0);
664 Stream_Write_UINT16(s, timestamp);
665 Stream_Write_UINT16(s, formatNo);
666 Stream_Write_UINT8(s, context->block_no);
667 Stream_Write_UINT8(s, 0);
668 Stream_Write_UINT8(s, 0);
669 Stream_Write_UINT8(s, 0);
670 Stream_Write_UINT32(s, audioTimeStamp);
674 if (!Stream_EnsureRemainingCapacity(s, size))
676 error = ERROR_INTERNAL_ERROR;
680 Stream_Write(s, data, size);
684 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, data, size, s))
686 error = ERROR_INTERNAL_ERROR;
690 const AUDIO_FORMAT* format = &context->client_formats[formatNo];
691 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
693 error = ERROR_INTERNAL_ERROR;
699 const size_t end = Stream_GetPosition(s);
700 if (end > UINT16_MAX + 4)
702 error = ERROR_INTERNAL_ERROR;
706 if (!Stream_SetPosition(s, 2))
708 error = ERROR_INTERNAL_ERROR;
711 Stream_Write_UINT16(s, (UINT16)(end - 4));
713 const BOOL status = WTSVirtualChannelWrite(context->priv->ChannelHandle,
714 Stream_BufferAs(s,
char), (UINT32)end, &written);
716 if (!status || (end != written))
719 "WTSVirtualChannelWrite failed! [stream length=%" PRIuz
" - written=%" PRIu32,
721 error = ERROR_INTERNAL_ERROR;
725 context->block_no = (context->block_no + 1) % 256;
728 Stream_ResetPosition(s);
729 context->priv->out_pending_frames = 0;
734static UINT rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
736 const BYTE* src =
nullptr;
739 WINPR_ASSERT(context);
740 WINPR_ASSERT(context->priv);
742 if (context->selected_client_format >= context->num_client_formats)
743 return ERROR_INTERNAL_ERROR;
745 src = context->priv->out_buffer;
746 length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
748 if (context->clientVersion >= CHANNEL_VERSION_WIN_8)
749 return rdpsnd_server_send_wave2_pdu(context, context->selected_client_format, src, length,
750 FALSE, wTimestamp, wTimestamp);
752 return rdpsnd_server_send_wave_pdu(context, wTimestamp);
760static UINT rdpsnd_server_send_samples(RdpsndServerContext* context,
const void* buf,
761 size_t nframes, UINT16 wTimestamp)
763 UINT error = CHANNEL_RC_OK;
765 WINPR_ASSERT(context);
766 WINPR_ASSERT(context->priv);
768 EnterCriticalSection(&context->priv->lock);
770 if (context->selected_client_format >= context->num_client_formats)
773 WLog_WARN(TAG,
"Drop samples because client format has not been negotiated.");
774 error = ERROR_NOT_READY;
780 const size_t cframes =
781 MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
782 size_t cframesize = cframes * context->priv->src_bytes_per_frame;
783 CopyMemory(context->priv->out_buffer +
784 (context->priv->out_pending_frames * context->priv->src_bytes_per_frame),
786 buf = (
const BYTE*)buf + cframesize;
788 context->priv->out_pending_frames += cframes;
790 if (context->priv->out_pending_frames >= context->priv->out_frames)
792 if ((error = rdpsnd_server_send_audio_pdu(context, wTimestamp)))
794 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
801 LeaveCriticalSection(&context->priv->lock);
810static UINT rdpsnd_server_send_samples2(RdpsndServerContext* context, UINT16 formatNo,
811 const void* buf,
size_t size, UINT16 timestamp,
812 UINT32 audioTimeStamp)
814 UINT error = CHANNEL_RC_OK;
816 WINPR_ASSERT(context);
817 WINPR_ASSERT(context->priv);
819 if (context->clientVersion < CHANNEL_VERSION_WIN_8)
820 return ERROR_INTERNAL_ERROR;
822 EnterCriticalSection(&context->priv->lock);
825 rdpsnd_server_send_wave2_pdu(context, formatNo, buf, size, TRUE, timestamp, audioTimeStamp);
827 LeaveCriticalSection(&context->priv->lock);
837static UINT rdpsnd_server_set_volume(RdpsndServerContext* context, UINT16 left, UINT16 right)
841 wStream* s = rdpsnd_server_get_buffer(context);
843 if (!Stream_EnsureRemainingCapacity(s, 8))
844 return ERROR_NOT_ENOUGH_MEMORY;
846 Stream_Write_UINT8(s, SNDC_SETVOLUME);
847 Stream_Write_UINT8(s, 0);
848 Stream_Write_UINT16(s, 4);
849 Stream_Write_UINT16(s, left);
850 Stream_Write_UINT16(s, right);
852 const size_t len = Stream_GetPosition(s);
853 WINPR_ASSERT(len <= UINT32_MAX);
854 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
855 (ULONG)len, &written);
856 Stream_ResetPosition(s);
857 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
865static UINT rdpsnd_server_close(RdpsndServerContext* context)
869 UINT error = CHANNEL_RC_OK;
870 wStream* s = rdpsnd_server_get_buffer(context);
872 EnterCriticalSection(&context->priv->lock);
874 if (context->priv->out_pending_frames > 0)
876 if (context->selected_client_format >= context->num_client_formats)
878 WLog_ERR(TAG,
"Pending audio frame exists while no format selected.");
879 error = ERROR_INVALID_DATA;
881 else if ((error = rdpsnd_server_send_audio_pdu(context, 0)))
883 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
887 LeaveCriticalSection(&context->priv->lock);
892 context->selected_client_format = 0xFFFF;
894 if (!Stream_EnsureRemainingCapacity(s, 4))
895 return ERROR_OUTOFMEMORY;
897 Stream_Write_UINT8(s, SNDC_CLOSE);
898 Stream_Write_UINT8(s, 0);
899 Stream_Seek_UINT16(s);
900 const size_t pos = Stream_GetPosition(s);
901 WINPR_ASSERT(pos >= 4);
902 if (!Stream_SetPosition(s, 2))
903 return ERROR_INVALID_DATA;
904 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, pos - 4));
905 if (!Stream_SetPosition(s, pos))
906 return ERROR_INVALID_DATA;
908 const size_t len = Stream_GetPosition(s);
909 WINPR_ASSERT(len <= UINT32_MAX);
910 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
911 (UINT32)len, &written);
912 Stream_ResetPosition(s);
913 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
921static UINT rdpsnd_server_start(RdpsndServerContext* context)
923 void* buffer =
nullptr;
924 DWORD bytesReturned = 0;
925 RdpsndServerPrivate* priv =
nullptr;
926 UINT error = ERROR_INTERNAL_ERROR;
927 PULONG pSessionId =
nullptr;
929 WINPR_ASSERT(context);
930 WINPR_ASSERT(context->priv);
932 priv = context->priv;
933 priv->SessionId = WTS_CURRENT_SESSION;
935 if (context->use_dynamic_virtual_channel)
937 UINT32 channelId = 0;
940 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
941 (LPSTR*)&pSessionId, &bytesReturned))
943 priv->SessionId = (DWORD)*pSessionId;
944 WTSFreeMemory(pSessionId);
945 priv->ChannelHandle = WTSVirtualChannelOpenEx(priv->SessionId, RDPSND_DVC_CHANNEL_NAME,
946 WTS_CHANNEL_OPTION_DYNAMIC);
947 if (!priv->ChannelHandle)
949 WLog_ERR(TAG,
"Open audio dynamic virtual channel (%s) failed!",
950 RDPSND_DVC_CHANNEL_NAME);
951 return ERROR_INTERNAL_ERROR;
954 channelId = WTSChannelGetIdByHandle(priv->ChannelHandle);
956 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
959 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
965 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
966 return ERROR_INTERNAL_ERROR;
971 priv->ChannelHandle =
972 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, RDPSND_CHANNEL_NAME);
973 if (!priv->ChannelHandle)
975 WLog_ERR(TAG,
"Open audio static virtual channel (rdpsnd) failed!");
976 return ERROR_INTERNAL_ERROR;
980 if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
982 (bytesReturned !=
sizeof(HANDLE)))
985 "error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned "
990 WTSFreeMemory(buffer);
995 priv->channelEvent = *(HANDLE*)buffer;
996 WTSFreeMemory(buffer);
997 priv->rdpsnd_pdu = Stream_New(
nullptr, 4096);
999 if (!priv->rdpsnd_pdu)
1001 WLog_ERR(TAG,
"Stream_New failed!");
1002 error = CHANNEL_RC_NO_MEMORY;
1006 if (!InitializeCriticalSectionEx(&context->priv->lock, 0, 0))
1008 WLog_ERR(TAG,
"InitializeCriticalSectionEx failed!");
1012 if ((error = rdpsnd_server_send_formats(context)))
1014 WLog_ERR(TAG,
"rdpsnd_server_send_formats failed with error %" PRIu32
"", error);
1018 if (priv->ownThread)
1020 context->priv->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1022 if (!context->priv->StopEvent)
1024 WLog_ERR(TAG,
"CreateEvent failed!");
1028 context->priv->Thread =
1029 CreateThread(
nullptr, 0, rdpsnd_server_thread, (
void*)context, 0,
nullptr);
1031 if (!context->priv->Thread)
1033 WLog_ERR(TAG,
"CreateThread failed!");
1038 return CHANNEL_RC_OK;
1040 (void)CloseHandle(context->priv->StopEvent);
1041 context->priv->StopEvent =
nullptr;
1043 DeleteCriticalSection(&context->priv->lock);
1045 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1046 context->priv->rdpsnd_pdu =
nullptr;
1048 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1049 context->priv->ChannelHandle =
nullptr;
1058static UINT rdpsnd_server_stop(RdpsndServerContext* context)
1060 UINT error = CHANNEL_RC_OK;
1062 WINPR_ASSERT(context);
1063 WINPR_ASSERT(context->priv);
1065 if (!context->priv->ChannelHandle)
1068 if (context->priv->ownThread)
1070 if (context->priv->StopEvent)
1072 (void)SetEvent(context->priv->StopEvent);
1074 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
1076 error = GetLastError();
1077 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
1081 (void)CloseHandle(context->priv->Thread);
1082 (void)CloseHandle(context->priv->StopEvent);
1083 context->priv->Thread =
nullptr;
1084 context->priv->StopEvent =
nullptr;
1088 DeleteCriticalSection(&context->priv->lock);
1090 if (context->priv->rdpsnd_pdu)
1092 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1093 context->priv->rdpsnd_pdu =
nullptr;
1096 if (context->priv->ChannelHandle)
1098 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1099 context->priv->ChannelHandle =
nullptr;
1105RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm)
1107 RdpsndServerPrivate* priv =
nullptr;
1108 RdpsndServerContext* context = (RdpsndServerContext*)calloc(1,
sizeof(RdpsndServerContext));
1114 context->Start = rdpsnd_server_start;
1115 context->Stop = rdpsnd_server_stop;
1116 context->selected_client_format = 0xFFFF;
1117 context->Initialize = rdpsnd_server_initialize;
1118 context->SendFormats = rdpsnd_server_send_formats;
1119 context->SelectFormat = rdpsnd_server_select_format;
1120 context->Training = rdpsnd_server_training;
1121 context->SendSamples = rdpsnd_server_send_samples;
1122 context->SendSamples2 = rdpsnd_server_send_samples2;
1123 context->SetVolume = rdpsnd_server_set_volume;
1124 context->Close = rdpsnd_server_close;
1125 context->priv = priv = (RdpsndServerPrivate*)calloc(1,
sizeof(RdpsndServerPrivate));
1129 WLog_ERR(TAG,
"calloc failed!");
1133 priv->dsp_context = freerdp_dsp_context_new(TRUE);
1135 if (!priv->dsp_context)
1137 WLog_ERR(TAG,
"freerdp_dsp_context_new failed!");
1141 priv->input_stream = Stream_New(
nullptr, 4);
1143 if (!priv->input_stream)
1145 WLog_ERR(TAG,
"Stream_New failed!");
1149 priv->expectedBytes = 4;
1150 priv->waitingHeader = TRUE;
1151 priv->ownThread = TRUE;
1154 WINPR_PRAGMA_DIAG_PUSH
1155 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1156 rdpsnd_server_context_free(context);
1157 WINPR_PRAGMA_DIAG_POP
1161void rdpsnd_server_context_reset(RdpsndServerContext* context)
1163 WINPR_ASSERT(context);
1164 WINPR_ASSERT(context->priv);
1166 context->priv->expectedBytes = 4;
1167 context->priv->waitingHeader = TRUE;
1168 Stream_ResetPosition(context->priv->input_stream);
1171void rdpsnd_server_context_free(RdpsndServerContext* context)
1178 rdpsnd_server_stop(context);
1180 free(context->priv->out_buffer);
1182 if (context->priv->dsp_context)
1183 freerdp_dsp_context_free(context->priv->dsp_context);
1185 if (context->priv->input_stream)
1186 Stream_Free(context->priv->input_stream, TRUE);
1189 free(context->server_formats);
1190 rdpsnd_server_client_format_free(context);
1191 free(context->priv);
1195HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext* context)
1197 WINPR_ASSERT(context);
1198 WINPR_ASSERT(context->priv);
1200 return context->priv->channelEvent;
1217UINT rdpsnd_server_handle_messages(RdpsndServerContext* context)
1219 DWORD bytesReturned = 0;
1220 UINT ret = CHANNEL_RC_OK;
1221 RdpsndServerPrivate* priv =
nullptr;
1224 WINPR_ASSERT(context);
1225 WINPR_ASSERT(context->priv);
1227 priv = context->priv;
1228 s = priv->input_stream;
1230 if (!WTSVirtualChannelRead(priv->ChannelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
1233 if (GetLastError() == ERROR_NO_DATA)
1234 return ERROR_NO_DATA;
1236 WLog_ERR(TAG,
"channel connection closed");
1237 return ERROR_INTERNAL_ERROR;
1240 priv->expectedBytes -= bytesReturned;
1241 Stream_Seek(s, bytesReturned);
1243 if (priv->expectedBytes)
1244 return CHANNEL_RC_OK;
1246 Stream_SealLength(s);
1247 Stream_ResetPosition(s);
1249 if (priv->waitingHeader)
1252 Stream_Read_UINT8(s, priv->msgType);
1253 Stream_Seek_UINT8(s);
1254 Stream_Read_UINT16(s, priv->expectedBytes);
1255 priv->waitingHeader = FALSE;
1256 Stream_ResetPosition(s);
1258 if (priv->expectedBytes)
1260 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
1262 WLog_ERR(TAG,
"Stream_EnsureCapacity failed!");
1263 return CHANNEL_RC_NO_MEMORY;
1266 return CHANNEL_RC_OK;
1271#ifdef WITH_DEBUG_SND
1272 WLog_DBG(TAG,
"message type %" PRIu8
"", priv->msgType);
1274 priv->expectedBytes = 4;
1275 priv->waitingHeader = TRUE;
1277 switch (priv->msgType)
1279 case SNDC_WAVECONFIRM:
1280 ret = rdpsnd_server_recv_waveconfirm(context, s);
1284 ret = rdpsnd_server_recv_trainingconfirm(context, s);
1288 ret = rdpsnd_server_recv_formats(context, s);
1290 if ((ret == CHANNEL_RC_OK) && (context->clientVersion < CHANNEL_VERSION_WIN_7))
1291 IFCALL(context->Activated, context);
1295 case SNDC_QUALITYMODE:
1296 ret = rdpsnd_server_recv_quality_mode(context, s);
1298 if ((ret == CHANNEL_RC_OK) && (context->clientVersion >= CHANNEL_VERSION_WIN_7))
1299 IFCALL(context->Activated, context);
1304 WLog_ERR(TAG,
"UNKNOWN MESSAGE TYPE!! (0x%02" PRIX8
")", priv->msgType);
1305 ret = ERROR_INVALID_DATA;
1309 Stream_ResetPosition(s);