22 #include <freerdp/config.h>
28 #include <winpr/crt.h>
29 #include <winpr/assert.h>
30 #include <winpr/print.h>
31 #include <winpr/stream.h>
33 #include <freerdp/freerdp.h>
34 #include <freerdp/channels/log.h>
36 #include "rdpsnd_common.h"
37 #include "rdpsnd_main.h"
39 static wStream* rdpsnd_server_get_buffer(RdpsndServerContext* context)
42 WINPR_ASSERT(context);
43 WINPR_ASSERT(context->priv);
45 s = context->priv->rdpsnd_pdu;
46 Stream_SetPosition(s, 0);
55 static UINT rdpsnd_server_send_formats(RdpsndServerContext* context)
57 wStream* s = rdpsnd_server_get_buffer(context);
61 if (!Stream_EnsureRemainingCapacity(s, 24))
62 return ERROR_OUTOFMEMORY;
64 Stream_Write_UINT8(s, SNDC_FORMATS);
65 Stream_Write_UINT8(s, 0);
66 Stream_Seek_UINT16(s);
67 Stream_Write_UINT32(s, 0);
68 Stream_Write_UINT32(s, 0);
69 Stream_Write_UINT32(s, 0);
70 Stream_Write_UINT16(s, 0);
71 Stream_Write_UINT16(s, context->num_server_formats);
72 Stream_Write_UINT8(s, context->block_no);
73 Stream_Write_UINT16(s, CHANNEL_VERSION_WIN_MAX);
74 Stream_Write_UINT8(s, 0);
76 for (
size_t i = 0; i < context->num_server_formats; i++)
78 const AUDIO_FORMAT* format = &context->server_formats[i];
80 if (!audio_format_write(s, format))
84 const size_t pos = Stream_GetPosition(s);
88 WINPR_ASSERT(pos >= 4);
89 Stream_SetPosition(s, 2);
90 Stream_Write_UINT16(s, (UINT16)(pos - 4));
91 Stream_SetPosition(s, pos);
93 WINPR_ASSERT(context->priv);
95 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
96 (UINT32)pos, &written);
97 Stream_SetPosition(s, 0);
99 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
107 static UINT rdpsnd_server_recv_waveconfirm(RdpsndServerContext* context,
wStream* s)
109 UINT16 timestamp = 0;
110 BYTE confirmBlockNum = 0;
111 UINT error = CHANNEL_RC_OK;
113 WINPR_ASSERT(context);
115 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
116 return ERROR_INVALID_DATA;
118 Stream_Read_UINT16(s, timestamp);
119 Stream_Read_UINT8(s, confirmBlockNum);
120 Stream_Seek_UINT8(s);
121 IFCALLRET(context->ConfirmBlock, error, context, confirmBlockNum, timestamp);
124 WLog_ERR(TAG,
"context->ConfirmBlock failed with error %" PRIu32
"", error);
134 static UINT rdpsnd_server_recv_trainingconfirm(RdpsndServerContext* context,
wStream* s)
136 UINT16 timestamp = 0;
138 UINT error = CHANNEL_RC_OK;
140 WINPR_ASSERT(context);
142 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
143 return ERROR_INVALID_DATA;
145 Stream_Read_UINT16(s, timestamp);
146 Stream_Read_UINT16(s, packsize);
148 IFCALLRET(context->TrainingConfirm, error, context, timestamp, packsize);
150 WLog_ERR(TAG,
"context->TrainingConfirm failed with error %" PRIu32
"", error);
160 static UINT rdpsnd_server_recv_quality_mode(RdpsndServerContext* context,
wStream* s)
162 WINPR_ASSERT(context);
164 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
166 WLog_ERR(TAG,
"not enough data in stream!");
167 return ERROR_INVALID_DATA;
170 Stream_Read_UINT16(s, context->qualityMode);
171 Stream_Seek_UINT16(s);
173 WLog_DBG(TAG,
"Client requested sound quality: 0x%04" PRIX16
"", context->qualityMode);
175 return CHANNEL_RC_OK;
183 static UINT rdpsnd_server_recv_formats(RdpsndServerContext* context,
wStream* s)
185 UINT16 num_known_format = 0;
188 UINT error = CHANNEL_RC_OK;
190 WINPR_ASSERT(context);
192 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
193 return ERROR_INVALID_DATA;
195 Stream_Read_UINT32(s, context->capsFlags);
196 Stream_Read_UINT32(s, context->initialVolume);
197 Stream_Read_UINT32(s, context->initialPitch);
198 Stream_Read_UINT16(s, udpPort);
199 Stream_Read_UINT16(s, context->num_client_formats);
200 Stream_Read_UINT8(s, lastblock);
201 Stream_Read_UINT16(s, context->clientVersion);
202 Stream_Seek_UINT8(s);
205 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, context->num_client_formats, 18ull))
206 return ERROR_INVALID_DATA;
208 if (!context->num_client_formats)
210 WLog_ERR(TAG,
"client doesn't support any format!");
211 return ERROR_INTERNAL_ERROR;
214 context->client_formats = audio_formats_new(context->num_client_formats);
216 if (!context->client_formats)
218 WLog_ERR(TAG,
"calloc failed!");
219 return CHANNEL_RC_NO_MEMORY;
222 for (UINT16 i = 0; i < context->num_client_formats; i++)
226 if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
228 WLog_ERR(TAG,
"not enough data in stream!");
229 error = ERROR_INVALID_DATA;
233 Stream_Read_UINT16(s, format->wFormatTag);
234 Stream_Read_UINT16(s, format->nChannels);
235 Stream_Read_UINT32(s, format->nSamplesPerSec);
236 Stream_Read_UINT32(s, format->nAvgBytesPerSec);
237 Stream_Read_UINT16(s, format->nBlockAlign);
238 Stream_Read_UINT16(s, format->wBitsPerSample);
239 Stream_Read_UINT16(s, format->cbSize);
241 if (format->cbSize > 0)
243 if (!Stream_SafeSeek(s, format->cbSize))
245 WLog_ERR(TAG,
"Stream_SafeSeek failed!");
246 error = ERROR_INTERNAL_ERROR;
251 if (format->wFormatTag != 0)
259 if (!context->num_client_formats)
261 WLog_ERR(TAG,
"client doesn't support any known format!");
265 return CHANNEL_RC_OK;
267 free(context->client_formats);
271 static DWORD WINAPI rdpsnd_server_thread(LPVOID arg)
275 HANDLE events[2] = { 0 };
276 RdpsndServerContext* context = (RdpsndServerContext*)arg;
277 UINT error = CHANNEL_RC_OK;
279 WINPR_ASSERT(context);
280 WINPR_ASSERT(context->priv);
282 events[nCount++] = context->priv->channelEvent;
283 events[nCount++] = context->priv->StopEvent;
285 WINPR_ASSERT(nCount <= ARRAYSIZE(events));
289 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
291 if (status == WAIT_FAILED)
293 error = GetLastError();
294 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
298 status = WaitForSingleObject(context->priv->StopEvent, 0);
300 if (status == WAIT_FAILED)
302 error = GetLastError();
303 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
307 if (status == WAIT_OBJECT_0)
310 if ((error = rdpsnd_server_handle_messages(context)))
312 WLog_ERR(TAG,
"rdpsnd_server_handle_messages failed with error %" PRIu32
"", error);
317 if (error && context->rdpcontext)
318 setChannelError(context->rdpcontext, error,
"rdpsnd_server_thread reported an error");
329 static UINT rdpsnd_server_initialize(RdpsndServerContext* context, BOOL ownThread)
331 WINPR_ASSERT(context);
332 WINPR_ASSERT(context->priv);
334 context->priv->ownThread = ownThread;
335 return context->Start(context);
343 static UINT rdpsnd_server_select_format(RdpsndServerContext* context, UINT16 client_format_index)
346 size_t out_buffer_size = 0;
348 UINT error = CHANNEL_RC_OK;
350 WINPR_ASSERT(context);
351 WINPR_ASSERT(context->priv);
353 if ((client_format_index >= context->num_client_formats) || (!context->src_format))
355 WLog_ERR(TAG,
"index %" PRIu16
" is not correct.", client_format_index);
356 return ERROR_INVALID_DATA;
359 EnterCriticalSection(&context->priv->lock);
360 context->priv->src_bytes_per_sample = context->src_format->wBitsPerSample / 8;
361 context->priv->src_bytes_per_frame =
362 context->priv->src_bytes_per_sample * context->src_format->nChannels;
363 context->selected_client_format = client_format_index;
364 format = &context->client_formats[client_format_index];
366 if (format->nSamplesPerSec == 0)
368 WLog_ERR(TAG,
"invalid Client Sound Format!!");
369 error = ERROR_INVALID_DATA;
373 if (context->latency <= 0)
374 context->latency = 50;
376 context->priv->out_frames = context->src_format->nSamplesPerSec * context->latency / 1000;
378 if (context->priv->out_frames < 1)
379 context->priv->out_frames = 1;
381 switch (format->wFormatTag)
383 case WAVE_FORMAT_DVI_ADPCM:
384 bs = 4ULL * (format->nBlockAlign - 4ULL * format->nChannels);
385 context->priv->out_frames -= context->priv->out_frames % bs;
387 if (context->priv->out_frames < bs)
388 context->priv->out_frames = bs;
392 case WAVE_FORMAT_ADPCM:
393 bs = (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
394 context->priv->out_frames -= context->priv->out_frames % bs;
396 if (context->priv->out_frames < bs)
397 context->priv->out_frames = bs;
404 context->priv->out_pending_frames = 0;
405 out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
407 if (context->priv->out_buffer_size < out_buffer_size)
409 BYTE* newBuffer = NULL;
410 newBuffer = (BYTE*)realloc(context->priv->out_buffer, out_buffer_size);
414 WLog_ERR(TAG,
"realloc failed!");
415 error = CHANNEL_RC_NO_MEMORY;
419 context->priv->out_buffer = newBuffer;
420 context->priv->out_buffer_size = out_buffer_size;
423 freerdp_dsp_context_reset(context->priv->dsp_context, format, 0u);
425 LeaveCriticalSection(&context->priv->lock);
434 static UINT rdpsnd_server_training(RdpsndServerContext* context, UINT16 timestamp, UINT16 packsize,
439 wStream* s = rdpsnd_server_get_buffer(context);
441 if (!Stream_EnsureRemainingCapacity(s, 8))
442 return ERROR_INTERNAL_ERROR;
444 Stream_Write_UINT8(s, SNDC_TRAINING);
445 Stream_Write_UINT8(s, 0);
446 Stream_Seek_UINT16(s);
447 Stream_Write_UINT16(s, timestamp);
448 Stream_Write_UINT16(s, packsize);
452 if (!Stream_EnsureRemainingCapacity(s, packsize))
454 Stream_SetPosition(s, 0);
455 return ERROR_INTERNAL_ERROR;
458 Stream_Write(s, data, packsize);
461 const size_t end = Stream_GetPosition(s);
462 if ((end < 4) || (end > UINT16_MAX))
463 return ERROR_INTERNAL_ERROR;
465 Stream_SetPosition(s, 2);
466 Stream_Write_UINT16(s, (UINT16)(end - 4));
468 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
469 (UINT32)end, &written);
471 Stream_SetPosition(s, 0);
473 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
476 static BOOL rdpsnd_server_align_wave_pdu(
wStream* s, UINT32 alignment)
479 Stream_SealLength(s);
480 size = Stream_Length(s);
482 if ((size % alignment) != 0)
484 size_t offset = alignment - size % alignment;
486 if (!Stream_EnsureRemainingCapacity(s, offset))
489 Stream_Zero(s, offset);
492 Stream_SealLength(s);
502 static UINT rdpsnd_server_send_wave_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
506 UINT error = CHANNEL_RC_OK;
507 wStream* s = rdpsnd_server_get_buffer(context);
509 if (context->selected_client_format > context->num_client_formats)
510 return ERROR_INTERNAL_ERROR;
512 WINPR_ASSERT(context->client_formats);
514 format = &context->client_formats[context->selected_client_format];
516 Stream_SetPosition(s, 0);
518 if (!Stream_EnsureRemainingCapacity(s, 16))
519 return ERROR_OUTOFMEMORY;
521 Stream_Write_UINT8(s, SNDC_WAVE);
522 Stream_Write_UINT8(s, 0);
523 Stream_Write_UINT16(s, 0);
524 Stream_Write_UINT16(s, wTimestamp);
525 Stream_Write_UINT16(s, context->selected_client_format);
526 Stream_Write_UINT8(s, context->block_no);
528 const size_t start = Stream_GetPosition(s);
529 const BYTE* src = context->priv->out_buffer;
530 const size_t length =
531 1ull * context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
533 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
534 return ERROR_INTERNAL_ERROR;
537 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
538 return ERROR_INTERNAL_ERROR;
540 const size_t end = Stream_GetPosition(s);
541 const size_t pos = end - start + 8ULL;
542 if (pos > UINT16_MAX)
543 return ERROR_INTERNAL_ERROR;
544 Stream_SetPosition(s, 2);
545 Stream_Write_UINT16(s, (UINT16)pos);
546 Stream_SetPosition(s, end);
548 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
549 (UINT32)(start + 4), &written))
551 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
552 error = ERROR_INTERNAL_ERROR;
555 if (error != CHANNEL_RC_OK)
557 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
558 error = ERROR_INTERNAL_ERROR;
562 Stream_SetPosition(s, start);
563 Stream_Write_UINT32(s, 0);
564 Stream_SetPosition(s, start);
566 WINPR_ASSERT((end - start) <= UINT32_MAX);
567 if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_Pointer(s),
568 (UINT32)(end - start), &written))
570 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed!");
571 error = ERROR_INTERNAL_ERROR;
574 context->block_no = (context->block_no + 1) % 256;
577 Stream_SetPosition(s, 0);
578 context->priv->out_pending_frames = 0;
588 static UINT rdpsnd_server_send_wave2_pdu(RdpsndServerContext* context, UINT16 formatNo,
589 const BYTE* data,
size_t size, BOOL encoded,
590 UINT16 timestamp, UINT32 audioTimeStamp)
593 UINT error = CHANNEL_RC_OK;
595 wStream* s = rdpsnd_server_get_buffer(context);
597 if (!Stream_EnsureRemainingCapacity(s, 16))
599 error = ERROR_INTERNAL_ERROR;
604 Stream_Write_UINT8(s, SNDC_WAVE2);
605 Stream_Write_UINT8(s, 0);
606 Stream_Write_UINT16(s, 0);
607 Stream_Write_UINT16(s, timestamp);
608 Stream_Write_UINT16(s, formatNo);
609 Stream_Write_UINT8(s, context->block_no);
610 Stream_Write_UINT8(s, 0);
611 Stream_Write_UINT8(s, 0);
612 Stream_Write_UINT8(s, 0);
613 Stream_Write_UINT32(s, audioTimeStamp);
617 if (!Stream_EnsureRemainingCapacity(s, size))
619 error = ERROR_INTERNAL_ERROR;
623 Stream_Write(s, data, size);
629 if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, data, size, s))
631 error = ERROR_INTERNAL_ERROR;
635 format = &context->client_formats[formatNo];
636 if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
638 error = ERROR_INTERNAL_ERROR;
643 const size_t end = Stream_GetPosition(s);
644 if (end > UINT16_MAX + 4)
646 error = ERROR_INTERNAL_ERROR;
650 Stream_SetPosition(s, 2);
651 Stream_Write_UINT16(s, (UINT16)(end - 4));
653 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
654 (UINT32)end, &written);
656 if (!status || (end != written))
658 WLog_ERR(TAG,
"WTSVirtualChannelWrite failed! [stream length=%" PRIuz
" - written=%" PRIu32,
660 error = ERROR_INTERNAL_ERROR;
663 context->block_no = (context->block_no + 1) % 256;
666 Stream_SetPosition(s, 0);
667 context->priv->out_pending_frames = 0;
672 static UINT rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
674 const BYTE* src = NULL;
677 WINPR_ASSERT(context);
678 WINPR_ASSERT(context->priv);
680 if (context->selected_client_format >= context->num_client_formats)
681 return ERROR_INTERNAL_ERROR;
683 src = context->priv->out_buffer;
684 length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
686 if (context->clientVersion >= CHANNEL_VERSION_WIN_8)
687 return rdpsnd_server_send_wave2_pdu(context, context->selected_client_format, src, length,
688 FALSE, wTimestamp, wTimestamp);
690 return rdpsnd_server_send_wave_pdu(context, wTimestamp);
698 static UINT rdpsnd_server_send_samples(RdpsndServerContext* context,
const void* buf,
699 size_t nframes, UINT16 wTimestamp)
701 UINT error = CHANNEL_RC_OK;
703 WINPR_ASSERT(context);
704 WINPR_ASSERT(context->priv);
706 EnterCriticalSection(&context->priv->lock);
708 if (context->selected_client_format >= context->num_client_formats)
711 WLog_WARN(TAG,
"Drop samples because client format has not been negotiated.");
712 error = ERROR_NOT_READY;
718 const size_t cframes =
719 MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
720 size_t cframesize = cframes * context->priv->src_bytes_per_frame;
721 CopyMemory(context->priv->out_buffer +
722 (context->priv->out_pending_frames * context->priv->src_bytes_per_frame),
724 buf = (
const BYTE*)buf + cframesize;
726 context->priv->out_pending_frames += cframes;
728 if (context->priv->out_pending_frames >= context->priv->out_frames)
730 if ((error = rdpsnd_server_send_audio_pdu(context, wTimestamp)))
732 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
739 LeaveCriticalSection(&context->priv->lock);
748 static UINT rdpsnd_server_send_samples2(RdpsndServerContext* context, UINT16 formatNo,
749 const void* buf,
size_t size, UINT16 timestamp,
750 UINT32 audioTimeStamp)
752 UINT error = CHANNEL_RC_OK;
754 WINPR_ASSERT(context);
755 WINPR_ASSERT(context->priv);
757 if (context->clientVersion < CHANNEL_VERSION_WIN_8)
758 return ERROR_INTERNAL_ERROR;
760 EnterCriticalSection(&context->priv->lock);
763 rdpsnd_server_send_wave2_pdu(context, formatNo, buf, size, TRUE, timestamp, audioTimeStamp);
765 LeaveCriticalSection(&context->priv->lock);
775 static UINT rdpsnd_server_set_volume(RdpsndServerContext* context, UINT16 left, UINT16 right)
779 wStream* s = rdpsnd_server_get_buffer(context);
781 if (!Stream_EnsureRemainingCapacity(s, 8))
782 return ERROR_NOT_ENOUGH_MEMORY;
784 Stream_Write_UINT8(s, SNDC_SETVOLUME);
785 Stream_Write_UINT8(s, 0);
786 Stream_Write_UINT16(s, 4);
787 Stream_Write_UINT16(s, left);
788 Stream_Write_UINT16(s, right);
790 const size_t len = Stream_GetPosition(s);
791 WINPR_ASSERT(len <= UINT32_MAX);
792 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
793 (ULONG)len, &written);
794 Stream_SetPosition(s, 0);
795 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
803 static UINT rdpsnd_server_close(RdpsndServerContext* context)
808 UINT error = CHANNEL_RC_OK;
809 wStream* s = rdpsnd_server_get_buffer(context);
811 EnterCriticalSection(&context->priv->lock);
813 if (context->priv->out_pending_frames > 0)
815 if (context->selected_client_format >= context->num_client_formats)
817 WLog_ERR(TAG,
"Pending audio frame exists while no format selected.");
818 error = ERROR_INVALID_DATA;
820 else if ((error = rdpsnd_server_send_audio_pdu(context, 0)))
822 WLog_ERR(TAG,
"rdpsnd_server_send_audio_pdu failed with error %" PRIu32
"", error);
826 LeaveCriticalSection(&context->priv->lock);
831 context->selected_client_format = 0xFFFF;
833 if (!Stream_EnsureRemainingCapacity(s, 4))
834 return ERROR_OUTOFMEMORY;
836 Stream_Write_UINT8(s, SNDC_CLOSE);
837 Stream_Write_UINT8(s, 0);
838 Stream_Seek_UINT16(s);
839 pos = Stream_GetPosition(s);
840 Stream_SetPosition(s, 2);
841 Stream_Write_UINT16(s, pos - 4);
842 Stream_SetPosition(s, pos);
844 const size_t len = Stream_GetPosition(s);
845 WINPR_ASSERT(len <= UINT32_MAX);
846 status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
847 (UINT32)len, &written);
848 Stream_SetPosition(s, 0);
849 return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
857 static UINT rdpsnd_server_start(RdpsndServerContext* context)
860 DWORD bytesReturned = 0;
861 RdpsndServerPrivate* priv = NULL;
862 UINT error = ERROR_INTERNAL_ERROR;
863 PULONG pSessionId = NULL;
865 WINPR_ASSERT(context);
866 WINPR_ASSERT(context->priv);
868 priv = context->priv;
869 priv->SessionId = WTS_CURRENT_SESSION;
871 if (context->use_dynamic_virtual_channel)
873 UINT32 channelId = 0;
876 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
877 (LPSTR*)&pSessionId, &bytesReturned))
879 priv->SessionId = (DWORD)*pSessionId;
880 WTSFreeMemory(pSessionId);
881 priv->ChannelHandle = WTSVirtualChannelOpenEx(priv->SessionId, RDPSND_DVC_CHANNEL_NAME,
882 WTS_CHANNEL_OPTION_DYNAMIC);
883 if (!priv->ChannelHandle)
885 WLog_ERR(TAG,
"Open audio dynamic virtual channel (%s) failed!",
886 RDPSND_DVC_CHANNEL_NAME);
887 return ERROR_INTERNAL_ERROR;
890 channelId = WTSChannelGetIdByHandle(priv->ChannelHandle);
892 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
895 WLog_ERR(TAG,
"context->ChannelIdAssigned failed!");
901 WLog_ERR(TAG,
"WTSQuerySessionInformationA failed!");
902 return ERROR_INTERNAL_ERROR;
907 priv->ChannelHandle =
908 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, RDPSND_CHANNEL_NAME);
909 if (!priv->ChannelHandle)
911 WLog_ERR(TAG,
"Open audio static virtual channel (rdpsnd) failed!");
912 return ERROR_INTERNAL_ERROR;
916 if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
918 (bytesReturned !=
sizeof(HANDLE)))
921 "error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned "
926 WTSFreeMemory(buffer);
931 CopyMemory(&priv->channelEvent, buffer,
sizeof(HANDLE));
932 WTSFreeMemory(buffer);
933 priv->rdpsnd_pdu = Stream_New(NULL, 4096);
935 if (!priv->rdpsnd_pdu)
937 WLog_ERR(TAG,
"Stream_New failed!");
938 error = CHANNEL_RC_NO_MEMORY;
942 if (!InitializeCriticalSectionEx(&context->priv->lock, 0, 0))
944 WLog_ERR(TAG,
"InitializeCriticalSectionEx failed!");
948 if ((error = rdpsnd_server_send_formats(context)))
950 WLog_ERR(TAG,
"rdpsnd_server_send_formats failed with error %" PRIu32
"", error);
956 context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
958 if (!context->priv->StopEvent)
960 WLog_ERR(TAG,
"CreateEvent failed!");
964 context->priv->Thread =
965 CreateThread(NULL, 0, rdpsnd_server_thread, (
void*)context, 0, NULL);
967 if (!context->priv->Thread)
969 WLog_ERR(TAG,
"CreateThread failed!");
974 return CHANNEL_RC_OK;
976 (void)CloseHandle(context->priv->StopEvent);
977 context->priv->StopEvent = NULL;
979 DeleteCriticalSection(&context->priv->lock);
981 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
982 context->priv->rdpsnd_pdu = NULL;
984 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
985 context->priv->ChannelHandle = NULL;
994 static UINT rdpsnd_server_stop(RdpsndServerContext* context)
996 UINT error = CHANNEL_RC_OK;
998 WINPR_ASSERT(context);
999 WINPR_ASSERT(context->priv);
1001 if (!context->priv->StopEvent)
1004 if (context->priv->ownThread)
1006 if (context->priv->StopEvent)
1008 (void)SetEvent(context->priv->StopEvent);
1010 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
1012 error = GetLastError();
1013 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
1017 (void)CloseHandle(context->priv->Thread);
1018 (void)CloseHandle(context->priv->StopEvent);
1019 context->priv->Thread = NULL;
1020 context->priv->StopEvent = NULL;
1024 DeleteCriticalSection(&context->priv->lock);
1026 if (context->priv->rdpsnd_pdu)
1028 Stream_Free(context->priv->rdpsnd_pdu, TRUE);
1029 context->priv->rdpsnd_pdu = NULL;
1032 if (context->priv->ChannelHandle)
1034 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
1035 context->priv->ChannelHandle = NULL;
1041 RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm)
1043 RdpsndServerPrivate* priv = NULL;
1044 RdpsndServerContext* context = (RdpsndServerContext*)calloc(1,
sizeof(RdpsndServerContext));
1050 context->Start = rdpsnd_server_start;
1051 context->Stop = rdpsnd_server_stop;
1052 context->selected_client_format = 0xFFFF;
1053 context->Initialize = rdpsnd_server_initialize;
1054 context->SendFormats = rdpsnd_server_send_formats;
1055 context->SelectFormat = rdpsnd_server_select_format;
1056 context->Training = rdpsnd_server_training;
1057 context->SendSamples = rdpsnd_server_send_samples;
1058 context->SendSamples2 = rdpsnd_server_send_samples2;
1059 context->SetVolume = rdpsnd_server_set_volume;
1060 context->Close = rdpsnd_server_close;
1061 context->priv = priv = (RdpsndServerPrivate*)calloc(1,
sizeof(RdpsndServerPrivate));
1065 WLog_ERR(TAG,
"calloc failed!");
1069 priv->dsp_context = freerdp_dsp_context_new(TRUE);
1071 if (!priv->dsp_context)
1073 WLog_ERR(TAG,
"freerdp_dsp_context_new failed!");
1077 priv->input_stream = Stream_New(NULL, 4);
1079 if (!priv->input_stream)
1081 WLog_ERR(TAG,
"Stream_New failed!");
1085 priv->expectedBytes = 4;
1086 priv->waitingHeader = TRUE;
1087 priv->ownThread = TRUE;
1090 WINPR_PRAGMA_DIAG_PUSH
1091 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1092 rdpsnd_server_context_free(context);
1093 WINPR_PRAGMA_DIAG_POP
1097 void rdpsnd_server_context_reset(RdpsndServerContext* context)
1099 WINPR_ASSERT(context);
1100 WINPR_ASSERT(context->priv);
1102 context->priv->expectedBytes = 4;
1103 context->priv->waitingHeader = TRUE;
1104 Stream_SetPosition(context->priv->input_stream, 0);
1107 void rdpsnd_server_context_free(RdpsndServerContext* context)
1114 rdpsnd_server_stop(context);
1116 free(context->priv->out_buffer);
1118 if (context->priv->dsp_context)
1119 freerdp_dsp_context_free(context->priv->dsp_context);
1121 if (context->priv->input_stream)
1122 Stream_Free(context->priv->input_stream, TRUE);
1125 free(context->server_formats);
1126 free(context->client_formats);
1127 free(context->priv);
1131 HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext* context)
1133 WINPR_ASSERT(context);
1134 WINPR_ASSERT(context->priv);
1136 return context->priv->channelEvent;
1153 UINT rdpsnd_server_handle_messages(RdpsndServerContext* context)
1155 DWORD bytesReturned = 0;
1156 UINT ret = CHANNEL_RC_OK;
1157 RdpsndServerPrivate* priv = NULL;
1160 WINPR_ASSERT(context);
1161 WINPR_ASSERT(context->priv);
1163 priv = context->priv;
1164 s = priv->input_stream;
1166 if (!WTSVirtualChannelRead(priv->ChannelHandle, 0, Stream_Pointer(s), priv->expectedBytes,
1169 if (GetLastError() == ERROR_NO_DATA)
1170 return ERROR_NO_DATA;
1172 WLog_ERR(TAG,
"channel connection closed");
1173 return ERROR_INTERNAL_ERROR;
1176 priv->expectedBytes -= bytesReturned;
1177 Stream_Seek(s, bytesReturned);
1179 if (priv->expectedBytes)
1180 return CHANNEL_RC_OK;
1182 Stream_SealLength(s);
1183 Stream_SetPosition(s, 0);
1185 if (priv->waitingHeader)
1188 Stream_Read_UINT8(s, priv->msgType);
1189 Stream_Seek_UINT8(s);
1190 Stream_Read_UINT16(s, priv->expectedBytes);
1191 priv->waitingHeader = FALSE;
1192 Stream_SetPosition(s, 0);
1194 if (priv->expectedBytes)
1196 if (!Stream_EnsureCapacity(s, priv->expectedBytes))
1198 WLog_ERR(TAG,
"Stream_EnsureCapacity failed!");
1199 return CHANNEL_RC_NO_MEMORY;
1202 return CHANNEL_RC_OK;
1207 #ifdef WITH_DEBUG_SND
1208 WLog_DBG(TAG,
"message type %" PRIu8
"", priv->msgType);
1210 priv->expectedBytes = 4;
1211 priv->waitingHeader = TRUE;
1213 switch (priv->msgType)
1215 case SNDC_WAVECONFIRM:
1216 ret = rdpsnd_server_recv_waveconfirm(context, s);
1220 ret = rdpsnd_server_recv_trainingconfirm(context, s);
1224 ret = rdpsnd_server_recv_formats(context, s);
1226 if ((ret == CHANNEL_RC_OK) && (context->clientVersion < CHANNEL_VERSION_WIN_7))
1227 IFCALL(context->Activated, context);
1231 case SNDC_QUALITYMODE:
1232 ret = rdpsnd_server_recv_quality_mode(context, s);
1234 if ((ret == CHANNEL_RC_OK) && (context->clientVersion >= CHANNEL_VERSION_WIN_7))
1235 IFCALL(context->Activated, context);
1240 WLog_ERR(TAG,
"UNKNOWN MESSAGE TYPE!! (0x%02" PRIX8
")", priv->msgType);
1241 ret = ERROR_INVALID_DATA;
1245 Stream_SetPosition(s, 0);