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 EnterCriticalSection(&context->priv->lock);
188 audio_formats_free(context->client_formats, context->num_client_formats);
189 context->client_formats =
nullptr;
190 context->num_client_formats = 0;
191 LeaveCriticalSection(&context->priv->lock);
199static UINT rdpsnd_server_recv_formats(RdpsndServerContext* context,
wStream* s)
201 UINT error = ERROR_INTERNAL_ERROR;
203 WINPR_ASSERT(context);
205 rdpsnd_server_client_format_free(context);
207 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
208 return ERROR_INVALID_DATA;
210 Stream_Read_UINT32(s, context->capsFlags);
211 Stream_Read_UINT32(s, context->initialVolume);
212 Stream_Read_UINT32(s, context->initialPitch);
213 Stream_Read_UINT16(s, context->udpPort);
214 const UINT16 wNumberOfFormats = Stream_Get_UINT16(s);
215 Stream_Read_UINT8(s, context->lastblock);
216 Stream_Read_UINT16(s, context->clientVersion);
217 Stream_Seek_UINT8(s);
220 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 18ull))
221 return ERROR_INVALID_DATA;
223 if (wNumberOfFormats == 0)
225 WLog_ERR(TAG,
"client doesn't support any format!");
226 return CHANNEL_RC_OK;
229 EnterCriticalSection(&context->priv->lock);
230 context->client_formats = audio_formats_new(wNumberOfFormats);
232 if (!context->client_formats)
234 WLog_ERR(TAG,
"calloc failed!");
235 error = CHANNEL_RC_NO_MEMORY;
239 context->num_client_formats = wNumberOfFormats;
241 for (UINT16 i = 0; i < context->num_client_formats; i++)
244 audio_format_free(format);
246 if (!audio_format_read(s, format))
254 if ((format->nChannels == 0) || (format->nBlockAlign == 0))
256 WLog_ERR(TAG,
"invalid client audio format: nChannels or nBlockAlign is 0");
257 error = ERROR_INVALID_DATA;
262 switch (format->wFormatTag)
264 case WAVE_FORMAT_DVI_ADPCM:
265 if (format->nBlockAlign < 4)
268 "invalid client audio format %s: nBlockAlign is %" PRIu32
270 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign);
271 error = ERROR_INVALID_DATA;
275 case WAVE_FORMAT_ADPCM:
276 if (format->nBlockAlign < 8)
279 "invalid client audio format %s: nBlockAlign is %" PRIu32
281 audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign);
282 error = ERROR_INVALID_DATA;
291 if (!context->num_client_formats)
293 WLog_ERR(TAG,
"client doesn't support any known format!");
296 LeaveCriticalSection(&context->priv->lock);
298 return CHANNEL_RC_OK;
300 LeaveCriticalSection(&context->priv->lock);
301 rdpsnd_server_client_format_free(context);
305static DWORD WINAPI rdpsnd_server_thread(LPVOID arg)
309 HANDLE events[2] = WINPR_C_ARRAY_INIT;
310 RdpsndServerContext* context = (RdpsndServerContext*)arg;
311 UINT error = CHANNEL_RC_OK;
313 WINPR_ASSERT(context);
314 WINPR_ASSERT(context->priv);
316 events[nCount++] = context->priv->channelEvent;
317 events[nCount++] = context->priv->StopEvent;
319 WINPR_ASSERT(nCount <= ARRAYSIZE(events));
323 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
325 if (status == WAIT_FAILED)
327 error = GetLastError();
328 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
332 status = WaitForSingleObject(context->priv->StopEvent, 0);
334 if (status == WAIT_FAILED)
336 error = GetLastError();
337 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
341 if (status == WAIT_OBJECT_0)
344 if ((error = rdpsnd_server_handle_messages(context)))
346 WLog_ERR(TAG,
"rdpsnd_server_handle_messages failed with error %" PRIu32
"", error);
351 if (error && context->rdpcontext)
352 setChannelError(context->rdpcontext, error,
"rdpsnd_server_thread reported an error");
363static UINT rdpsnd_server_initialize(RdpsndServerContext* context, BOOL ownThread)
365 WINPR_ASSERT(context);
366 WINPR_ASSERT(context->priv);
368 context->priv->ownThread = ownThread;
369 return context->Start(context);
377static UINT rdpsnd_server_select_format(RdpsndServerContext* context, UINT16 client_format_index)
379 UINT error = ERROR_INVALID_DATA;
381 WINPR_ASSERT(context);
382 WINPR_ASSERT(context->priv);
384 EnterCriticalSection(&context->priv->lock);
385 if ((client_format_index >= context->num_client_formats) || (!context->src_format))
387 WLog_ERR(TAG,
"index %" PRIu16
" is not correct.", client_format_index);
393 context->selected_client_format = UINT16_MAX;
395 context->priv->src_bytes_per_sample = context->src_format->wBitsPerSample / 8;
396 context->priv->src_bytes_per_frame =
397 context->priv->src_bytes_per_sample * context->src_format->nChannels;
399 const AUDIO_FORMAT* format = &context->client_formats[client_format_index];
401 if (format->nSamplesPerSec == 0)
403 WLog_ERR(TAG,
"invalid Client Sound Format!");
404 error = ERROR_INVALID_DATA;
408 if (context->latency <= 0)
409 context->latency = 50;
411 context->priv->out_frames = context->src_format->nSamplesPerSec * context->latency / 1000;
413 if (context->priv->out_frames < 1)
414 context->priv->out_frames = 1;
416 switch (format->wFormatTag)
418 case WAVE_FORMAT_DVI_ADPCM:
420 error = ERROR_INVALID_DATA;
421 if (format->nBlockAlign / 4ull < format->nChannels)
423 if (format->nChannels == 0)
426 error = CHANNEL_RC_OK;
432 const size_t bs = 4ULL * (format->nBlockAlign - 4ULL * format->nChannels);
436 context->priv->out_frames -= context->priv->out_frames % bs;
438 if (context->priv->out_frames < bs)
439 context->priv->out_frames = bs;
443 case WAVE_FORMAT_ADPCM:
445 error = ERROR_INVALID_DATA;
446 if (format->nBlockAlign / 7 < format->nChannels)
448 if (format->nChannels == 0)
451 error = CHANNEL_RC_OK;
459 (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
461 context->priv->out_frames -= context->priv->out_frames % bs;
463 if (context->priv->out_frames < bs)
464 context->priv->out_frames = bs;
471 context->priv->out_pending_frames = 0;
472 if (context->priv->out_frames > UINT32_MAX / context->priv->src_bytes_per_frame)
475 const size_t out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
476 if (context->priv->out_buffer_size < out_buffer_size)
478 BYTE* newBuffer = (BYTE*)realloc(context->priv->out_buffer, out_buffer_size);
482 WLog_ERR(TAG,
"realloc failed!");
483 error = CHANNEL_RC_NO_MEMORY;
487 context->priv->out_buffer = newBuffer;
488 context->priv->out_buffer_size = out_buffer_size;
491 if (!freerdp_dsp_context_reset(context->priv->dsp_context, format, 0u))
493 error = ERROR_INTERNAL_ERROR;
497 context->selected_client_format = client_format_index;
498 error = CHANNEL_RC_OK;
500 LeaveCriticalSection(&context->priv->lock);
509static UINT rdpsnd_server_training(RdpsndServerContext* context, UINT16 timestamp, UINT16 packsize,
514 wStream* s = rdpsnd_server_get_buffer(context);
516 if (!Stream_EnsureRemainingCapacity(s, 8))
517 return ERROR_INTERNAL_ERROR;
519 Stream_Write_UINT8(s, SNDC_TRAINING);
520 Stream_Write_UINT8(s, 0);
521 Stream_Seek_UINT16(s);
522 Stream_Write_UINT16(s, timestamp);
523 Stream_Write_UINT16(s, packsize);
527 if (!Stream_EnsureRemainingCapacity(s, packsize))
529 Stream_ResetPosition(s);
530 return ERROR_INTERNAL_ERROR;
533 Stream_Write(s, data, packsize);
536 const size_t end = Stream_GetPosition(s);
537 if ((end < 4) || (end > UINT16_MAX))
538 return ERROR_INTERNAL_ERROR;
540 if (!Stream_SetPosition(s, 2))
541 return ERROR_INTERNAL_ERROR;
542 Stream_Write_UINT16(s, (UINT16)(end - 4));
544 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
545 (UINT32)end, &written);
547 Stream_ResetPosition(s);
549 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
559 Stream_SealLength(s);
560 size = Stream_Length(s);
562 WINPR_ASSERT(format);
563 const UINT32 alignment = format->nBlockAlign;
564 if ((format->wFormatTag != WAVE_FORMAT_OPUS) && ((size % alignment) != 0))
566 size_t offset = alignment - size % alignment;
568 if (!Stream_EnsureRemainingCapacity(s, offset))
571 Stream_Zero(s, offset);
574 Stream_SealLength(s);
584static UINT rdpsnd_server_send_wave_pdu_int(RdpsndServerContext* context, UINT16 wTimestamp)
587 UINT error = CHANNEL_RC_OK;
588 wStream* s = rdpsnd_server_get_buffer(context);
590 if (context->selected_client_format > context->num_client_formats)
591 return ERROR_INTERNAL_ERROR;
593 WINPR_ASSERT(context->client_formats);
595 const AUDIO_FORMAT* format = &context->client_formats[context->selected_client_format];
597 Stream_ResetPosition(s);
599 if (!Stream_EnsureRemainingCapacity(s, 16))
600 return ERROR_OUTOFMEMORY;
602 Stream_Write_UINT8(s, SNDC_WAVE);
603 Stream_Write_UINT8(s, 0);
604 Stream_Write_UINT16(s, 0);
605 Stream_Write_UINT16(s, wTimestamp);
606 Stream_Write_UINT16(s, context->selected_client_format);
607 Stream_Write_UINT8(s, context->block_no);
609 const size_t start = Stream_GetPosition(s);
610 const BYTE* src = context->priv->out_buffer;
611 const size_t length =
612 1ull * context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
614 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
615 return ERROR_INTERNAL_ERROR;
618 if (!rdpsnd_server_align_wave_pdu(s, format))
619 return ERROR_INTERNAL_ERROR;
621 const size_t end = Stream_GetPosition(s);
622 const size_t pos = end - start + 8ULL;
623 if (pos > UINT16_MAX)
624 return ERROR_INTERNAL_ERROR;
625 if (!Stream_SetPosition(s, 2))
626 return ERROR_INTERNAL_ERROR;
627 Stream_Write_UINT16(s, (UINT16)pos);
628 if (!Stream_SetPosition(s, end))
629 return ERROR_INTERNAL_ERROR;
631 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
632 (UINT32)(start + 4), &written))
634 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
635 error = ERROR_INTERNAL_ERROR;
638 if (error != CHANNEL_RC_OK)
640 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
641 error = ERROR_INTERNAL_ERROR;
645 if (!Stream_SetPosition(s, start))
647 error = ERROR_INTERNAL_ERROR;
650 Stream_Write_UINT32(s, 0);
651 if (!Stream_SetPosition(s, start))
653 error = ERROR_INTERNAL_ERROR;
657 WINPR_ASSERT((end - start) <= UINT32_MAX);
658 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_Pointer(s),
659 (UINT32)(end - start), &written))
661 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
662 error = ERROR_INTERNAL_ERROR;
665 context->block_no = (context->block_no + 1) % 256;
668 Stream_ResetPosition(s);
669 context->priv->out_pending_frames = 0;
673static UINT rdpsnd_server_send_wave_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
675 WINPR_ASSERT(context);
676 EnterCriticalSection(&context->priv->lock);
677 const UINT rc = rdpsnd_server_send_wave_pdu_int(context, wTimestamp);
678 LeaveCriticalSection(&context->priv->lock);
687static UINT rdpsnd_server_send_wave2_pdu(RdpsndServerContext* context, UINT16 formatNo,
688 const BYTE* data,
size_t size, BOOL encoded,
689 UINT16 timestamp, UINT32 audioTimeStamp)
692 UINT error = CHANNEL_RC_OK;
693 wStream* s = rdpsnd_server_get_buffer(context);
695 if (!Stream_EnsureRemainingCapacity(s, 16))
697 error = ERROR_INTERNAL_ERROR;
702 Stream_Write_UINT8(s, SNDC_WAVE2);
703 Stream_Write_UINT8(s, 0);
704 Stream_Write_UINT16(s, 0);
705 Stream_Write_UINT16(s, timestamp);
706 Stream_Write_UINT16(s, formatNo);
707 Stream_Write_UINT8(s, context->block_no);
708 Stream_Write_UINT8(s, 0);
709 Stream_Write_UINT8(s, 0);
710 Stream_Write_UINT8(s, 0);
711 Stream_Write_UINT32(s, audioTimeStamp);
715 if (!Stream_EnsureRemainingCapacity(s, size))
717 error = ERROR_INTERNAL_ERROR;
721 Stream_Write(s, data, size);
725 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, data, size, s))
727 error = ERROR_INTERNAL_ERROR;
731 EnterCriticalSection(&context->priv->lock);
732 const AUDIO_FORMAT* format = &context->client_formats[formatNo];
733 const BOOL rc = rdpsnd_server_align_wave_pdu(s, format);
734 LeaveCriticalSection(&context->priv->lock);
737 error = ERROR_INTERNAL_ERROR;
743 const size_t end = Stream_GetPosition(s);
744 if (end > UINT16_MAX + 4)
746 error = ERROR_INTERNAL_ERROR;
750 if (!Stream_SetPosition(s, 2))
752 error = ERROR_INTERNAL_ERROR;
755 Stream_Write_UINT16(s, (UINT16)(end - 4));
757 const BOOL status = WTSVirtualChannelWrite(context->priv->ChannelHandle,
758 Stream_BufferAs(s,
char), (UINT32)end, &written);
760 if (!status || (end != written))
763 "WTSVirtualChannelWrite failed! [stream length=%" PRIuz
" - written=%" PRIu32,
765 error = ERROR_INTERNAL_ERROR;
769 context->block_no = (context->block_no + 1) % 256;
772 Stream_ResetPosition(s);
773 context->priv->out_pending_frames = 0;
778static UINT rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
780 const BYTE* src =
nullptr;
783 WINPR_ASSERT(context);
784 WINPR_ASSERT(context->priv);
786 if (context->selected_client_format >= context->num_client_formats)
787 return ERROR_INTERNAL_ERROR;
789 src = context->priv->out_buffer;
790 length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
792 if (context->clientVersion >= CHANNEL_VERSION_WIN_8)
793 return rdpsnd_server_send_wave2_pdu(context, context->selected_client_format, src, length,
794 FALSE, wTimestamp, wTimestamp);
796 return rdpsnd_server_send_wave_pdu(context, wTimestamp);
804static UINT rdpsnd_server_send_samples(RdpsndServerContext* context,
const void* buf,
805 size_t nframes, UINT16 wTimestamp)
807 UINT error = CHANNEL_RC_OK;
809 WINPR_ASSERT(context);
810 WINPR_ASSERT(context->priv);
812 EnterCriticalSection(&context->priv->lock);
814 if (context->selected_client_format >= context->num_client_formats)
817 WLog_WARN(TAG,
"Drop samples because client format has not been negotiated.");
818 error = ERROR_NOT_READY;
824 const size_t cframes =
825 MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
826 size_t cframesize = cframes * context->priv->src_bytes_per_frame;
827 CopyMemory(context->priv->out_buffer +
828 (context->priv->out_pending_frames * context->priv->src_bytes_per_frame),
830 buf = (
const BYTE*)buf + cframesize;
832 context->priv->out_pending_frames += cframes;
834 if (context->priv->out_pending_frames >= context->priv->out_frames)
836 if ((error = rdpsnd_server_send_audio_pdu(context, wTimestamp)))
838 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
845 LeaveCriticalSection(&context->priv->lock);
854static UINT rdpsnd_server_send_samples2(RdpsndServerContext* context, UINT16 formatNo,
855 const void* buf,
size_t size, UINT16 timestamp,
856 UINT32 audioTimeStamp)
858 UINT error = CHANNEL_RC_OK;
860 WINPR_ASSERT(context);
861 WINPR_ASSERT(context->priv);
863 if (context->clientVersion < CHANNEL_VERSION_WIN_8)
864 return ERROR_INTERNAL_ERROR;
866 EnterCriticalSection(&context->priv->lock);
869 rdpsnd_server_send_wave2_pdu(context, formatNo, buf, size, TRUE, timestamp, audioTimeStamp);
871 LeaveCriticalSection(&context->priv->lock);
881static UINT rdpsnd_server_set_volume(RdpsndServerContext* context, UINT16 left, UINT16 right)
885 wStream* s = rdpsnd_server_get_buffer(context);
887 if (!Stream_EnsureRemainingCapacity(s, 8))
888 return ERROR_NOT_ENOUGH_MEMORY;
890 Stream_Write_UINT8(s, SNDC_SETVOLUME);
891 Stream_Write_UINT8(s, 0);
892 Stream_Write_UINT16(s, 4);
893 Stream_Write_UINT16(s, left);
894 Stream_Write_UINT16(s, right);
896 const size_t len = Stream_GetPosition(s);
897 WINPR_ASSERT(len <= UINT32_MAX);
898 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
899 (ULONG)len, &written);
900 Stream_ResetPosition(s);
901 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
909static UINT rdpsnd_server_close(RdpsndServerContext* context)
913 UINT error = CHANNEL_RC_OK;
914 wStream* s = rdpsnd_server_get_buffer(context);
916 EnterCriticalSection(&context->priv->lock);
918 if (context->priv->out_pending_frames > 0)
920 if (context->selected_client_format >= context->num_client_formats)
922 WLog_ERR(TAG,
"Pending audio frame exists while no format selected.");
923 error = ERROR_INVALID_DATA;
925 else if ((error = rdpsnd_server_send_audio_pdu(context, 0)))
927 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
931 LeaveCriticalSection(&context->priv->lock);
936 context->selected_client_format = UINT16_MAX;
938 if (!Stream_EnsureRemainingCapacity(s, 4))
939 return ERROR_OUTOFMEMORY;
941 Stream_Write_UINT8(s, SNDC_CLOSE);
942 Stream_Write_UINT8(s, 0);
943 Stream_Seek_UINT16(s);
944 const size_t pos = Stream_GetPosition(s);
945 WINPR_ASSERT(pos >= 4);
946 if (!Stream_SetPosition(s, 2))
947 return ERROR_INVALID_DATA;
948 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, pos - 4));
949 if (!Stream_SetPosition(s, pos))
950 return ERROR_INVALID_DATA;
952 const size_t len = Stream_GetPosition(s);
953 WINPR_ASSERT(len <= UINT32_MAX);
954 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
955 (UINT32)len, &written);
956 Stream_ResetPosition(s);
957 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
965static UINT rdpsnd_server_start(RdpsndServerContext* context)
967 void* buffer =
nullptr;
968 DWORD bytesReturned = 0;
969 RdpsndServerPrivate* priv =
nullptr;
970 UINT error = ERROR_INTERNAL_ERROR;
971 PULONG pSessionId =
nullptr;
973 WINPR_ASSERT(context);
974 WINPR_ASSERT(context->priv);
976 priv = context->priv;
977 priv->SessionId = WTS_CURRENT_SESSION;
979 if (context->use_dynamic_virtual_channel)
981 UINT32 channelId = 0;
984 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
985 (LPSTR*)&pSessionId, &bytesReturned))
987 priv->SessionId = (DWORD)*pSessionId;
988 WTSFreeMemory(pSessionId);
989 priv->ChannelHandle = WTSVirtualChannelOpenEx(priv->SessionId, RDPSND_DVC_CHANNEL_NAME,
990 WTS_CHANNEL_OPTION_DYNAMIC);
991 if (!priv->ChannelHandle)
993 WLog_ERR(TAG,
"Open audio dynamic virtual channel (%s) failed!",
994 RDPSND_DVC_CHANNEL_NAME);
995 return ERROR_INTERNAL_ERROR;
998 channelId = WTSChannelGetIdByHandle(priv->ChannelHandle);
1000 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
1003 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
1009 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
1010 return ERROR_INTERNAL_ERROR;
1015 priv->ChannelHandle =
1016 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, RDPSND_CHANNEL_NAME);
1017 if (!priv->ChannelHandle)
1019 WLog_ERR(TAG,
"Open audio static virtual channel (rdpsnd) failed!");
1020 return ERROR_INTERNAL_ERROR;
1024 if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
1026 (bytesReturned !=
sizeof(HANDLE)))
1029 "error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned "
1030 "size(%" PRIu32
")",
1034 WTSFreeMemory(buffer);
1039 priv->channelEvent = *(HANDLE*)buffer;
1040 WTSFreeMemory(buffer);
1041 priv->rdpsnd_pdu = Stream_New(
nullptr, 4096);
1043 if (!priv->rdpsnd_pdu)
1045 WLog_ERR(TAG,
"Stream_New failed!");
1046 error = CHANNEL_RC_NO_MEMORY;
1050 if (!InitializeCriticalSectionEx(&context->priv->lock, 0, 0))
1052 WLog_ERR(TAG,
"InitializeCriticalSectionEx failed!");
1056 if ((error = rdpsnd_server_send_formats(context)))
1058 WLog_ERR(TAG,
"rdpsnd_server_send_formats failed with error %" PRIu32
"", error);
1062 if (priv->ownThread)
1064 context->priv->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1066 if (!context->priv->StopEvent)
1068 WLog_ERR(TAG,
"CreateEvent failed!");
1072 context->priv->Thread =
1073 CreateThread(
nullptr, 0, rdpsnd_server_thread, (
void*)context, 0,
nullptr);
1075 if (!context->priv->Thread)
1077 WLog_ERR(TAG,
"CreateThread failed!");
1082 return CHANNEL_RC_OK;
1084 (void)CloseHandle(context->priv->StopEvent);
1085 context->priv->StopEvent =
nullptr;
1087 DeleteCriticalSection(&context->priv->lock);
1089 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1090 context->priv->rdpsnd_pdu =
nullptr;
1092 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1093 context->priv->ChannelHandle =
nullptr;
1102static UINT rdpsnd_server_stop(RdpsndServerContext* context)
1104 UINT error = CHANNEL_RC_OK;
1106 WINPR_ASSERT(context);
1107 WINPR_ASSERT(context->priv);
1109 if (!context->priv->ChannelHandle)
1112 if (context->priv->ownThread)
1114 if (context->priv->StopEvent)
1116 (void)SetEvent(context->priv->StopEvent);
1118 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
1120 error = GetLastError();
1121 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
1125 (void)CloseHandle(context->priv->Thread);
1126 (void)CloseHandle(context->priv->StopEvent);
1127 context->priv->Thread =
nullptr;
1128 context->priv->StopEvent =
nullptr;
1132 DeleteCriticalSection(&context->priv->lock);
1134 if (context->priv->rdpsnd_pdu)
1136 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1137 context->priv->rdpsnd_pdu =
nullptr;
1140 if (context->priv->ChannelHandle)
1142 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1143 context->priv->ChannelHandle =
nullptr;
1149RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm)
1151 RdpsndServerPrivate* priv =
nullptr;
1152 RdpsndServerContext* context = (RdpsndServerContext*)calloc(1,
sizeof(RdpsndServerContext));
1158 context->Start = rdpsnd_server_start;
1159 context->Stop = rdpsnd_server_stop;
1160 context->selected_client_format = UINT16_MAX;
1161 context->Initialize = rdpsnd_server_initialize;
1162 context->SendFormats = rdpsnd_server_send_formats;
1163 context->SelectFormat = rdpsnd_server_select_format;
1164 context->Training = rdpsnd_server_training;
1165 context->SendSamples = rdpsnd_server_send_samples;
1166 context->SendSamples2 = rdpsnd_server_send_samples2;
1167 context->SetVolume = rdpsnd_server_set_volume;
1168 context->Close = rdpsnd_server_close;
1169 context->priv = priv = (RdpsndServerPrivate*)calloc(1,
sizeof(RdpsndServerPrivate));
1173 WLog_ERR(TAG,
"calloc failed!");
1177 priv->dsp_context = freerdp_dsp_context_new(TRUE);
1179 if (!priv->dsp_context)
1181 WLog_ERR(TAG,
"freerdp_dsp_context_new failed!");
1185 priv->input_stream = Stream_New(
nullptr, 4);
1187 if (!priv->input_stream)
1189 WLog_ERR(TAG,
"Stream_New failed!");
1193 priv->expectedBytes = 4;
1194 priv->waitingHeader = TRUE;
1195 priv->ownThread = TRUE;
1198 WINPR_PRAGMA_DIAG_PUSH
1199 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1200 rdpsnd_server_context_free(context);
1201 WINPR_PRAGMA_DIAG_POP
1205void rdpsnd_server_context_reset(RdpsndServerContext* context)
1207 WINPR_ASSERT(context);
1208 WINPR_ASSERT(context->priv);
1210 context->priv->expectedBytes = 4;
1211 context->priv->waitingHeader = TRUE;
1212 Stream_ResetPosition(context->priv->input_stream);
1215void rdpsnd_server_context_free(RdpsndServerContext* context)
1222 rdpsnd_server_stop(context);
1224 free(context->priv->out_buffer);
1226 if (context->priv->dsp_context)
1227 freerdp_dsp_context_free(context->priv->dsp_context);
1229 if (context->priv->input_stream)
1230 Stream_Free(context->priv->input_stream, TRUE);
1233 free(context->server_formats);
1235 rdpsnd_server_client_format_free(context);
1236 free(context->priv);
1240HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext* context)
1242 WINPR_ASSERT(context);
1243 WINPR_ASSERT(context->priv);
1245 return context->priv->channelEvent;
1262UINT rdpsnd_server_handle_messages(RdpsndServerContext* context)
1264 DWORD bytesReturned = 0;
1265 UINT ret = CHANNEL_RC_OK;
1266 RdpsndServerPrivate* priv =
nullptr;
1269 WINPR_ASSERT(context);
1270 WINPR_ASSERT(context->priv);
1272 priv = context->priv;
1273 s = priv->input_stream;
1275 if (!WTSVirtualChannelRead(priv->ChannelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
1278 if (GetLastError() == ERROR_NO_DATA)
1279 return ERROR_NO_DATA;
1281 WLog_ERR(TAG,
"channel connection closed");
1282 return ERROR_INTERNAL_ERROR;
1285 priv->expectedBytes -= bytesReturned;
1286 Stream_Seek(s, bytesReturned);
1288 if (priv->expectedBytes)
1289 return CHANNEL_RC_OK;
1291 Stream_SealLength(s);
1292 Stream_ResetPosition(s);
1294 if (priv->waitingHeader)
1297 Stream_Read_UINT8(s, priv->msgType);
1298 Stream_Seek_UINT8(s);
1299 Stream_Read_UINT16(s, priv->expectedBytes);
1300 priv->waitingHeader = FALSE;
1301 Stream_ResetPosition(s);
1303 if (priv->expectedBytes)
1305 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
1307 WLog_ERR(TAG,
"Stream_EnsureCapacity failed!");
1308 return CHANNEL_RC_NO_MEMORY;
1311 return CHANNEL_RC_OK;
1316#ifdef WITH_DEBUG_SND
1317 WLog_DBG(TAG,
"message type %" PRIu8
"", priv->msgType);
1319 priv->expectedBytes = 4;
1320 priv->waitingHeader = TRUE;
1322 EnterCriticalSection(&priv->lock);
1323 switch (priv->msgType)
1325 case SNDC_WAVECONFIRM:
1326 ret = rdpsnd_server_recv_waveconfirm(context, s);
1330 ret = rdpsnd_server_recv_trainingconfirm(context, s);
1334 ret = rdpsnd_server_recv_formats(context, s);
1336 if ((ret == CHANNEL_RC_OK) && (context->clientVersion < CHANNEL_VERSION_WIN_7))
1337 IFCALL(context->Activated, context);
1341 case SNDC_QUALITYMODE:
1342 ret = rdpsnd_server_recv_quality_mode(context, s);
1344 if ((ret == CHANNEL_RC_OK) && (context->clientVersion >= CHANNEL_VERSION_WIN_7))
1345 IFCALL(context->Activated, context);
1350 WLog_ERR(TAG,
"UNKNOWN MESSAGE TYPE!! (0x%02" PRIX8
")", priv->msgType);
1351 ret = ERROR_INVALID_DATA;
1354 LeaveCriticalSection(&priv->lock);
1355 Stream_ResetPosition(s);
1359void rdpsnd_server_context_lock(RdpsndServerContext* context)
1361 WINPR_ASSERT(context);
1362 WINPR_ASSERT(context->priv);
1363 EnterCriticalSection(&context->priv->lock);
1365void rdpsnd_server_context_unlock(RdpsndServerContext* context)
1367 WINPR_ASSERT(context);
1368 WINPR_ASSERT(context->priv);
1369 LeaveCriticalSection(&context->priv->lock);