2013-08-19 05:52:55 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Server Audio Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2012 Vic Lee
|
2015-07-15 10:50:35 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2013-08-19 05:52:55 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/print.h>
|
|
|
|
#include <winpr/stream.h>
|
|
|
|
|
2014-08-11 11:12:01 +04:00
|
|
|
#include <freerdp/channels/log.h>
|
2014-08-07 21:05:48 +04:00
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
#include "rdpsnd_common.h"
|
2013-08-19 05:52:55 +04:00
|
|
|
#include "rdpsnd_main.h"
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2018-07-02 17:08:47 +03:00
|
|
|
static UINT rdpsnd_server_send_formats(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2017-12-11 12:25:21 +03:00
|
|
|
size_t pos;
|
2013-08-19 05:52:55 +04:00
|
|
|
UINT16 i;
|
2018-09-25 13:04:10 +03:00
|
|
|
BOOL status = FALSE;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_FORMATS);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
|
|
|
Stream_Seek_UINT16(s);
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT32(s, 0); /* dwFlags */
|
|
|
|
Stream_Write_UINT32(s, 0); /* dwVolume */
|
|
|
|
Stream_Write_UINT32(s, 0); /* dwPitch */
|
|
|
|
Stream_Write_UINT16(s, 0); /* wDGramPort */
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT16(s, context->num_server_formats); /* wNumberOfFormats */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, context->block_no); /* cLastBlockConfirmed */
|
|
|
|
Stream_Write_UINT16(s, CHANNEL_VERSION_WIN_MAX); /* wVersion */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
2016-09-26 13:02:33 +03:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
for (i = 0; i < context->num_server_formats; i++)
|
|
|
|
{
|
2018-09-25 13:04:10 +03:00
|
|
|
AUDIO_FORMAT format = context->server_formats[i];
|
|
|
|
// TODO: Eliminate this!!!
|
2019-11-06 17:24:51 +03:00
|
|
|
format.nAvgBytesPerSec =
|
|
|
|
format.nSamplesPerSec * format.nChannels * format.wBitsPerSample / 8;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2018-09-25 13:04:10 +03:00
|
|
|
if (!audio_format_write(s, &format))
|
|
|
|
goto fail;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
|
|
|
Stream_SetPosition(s, pos);
|
2019-11-06 17:24:51 +03:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
|
|
|
|
Stream_GetPosition(s), &written);
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2018-09-25 13:04:10 +03:00
|
|
|
fail:
|
2016-09-26 13:12:37 +03:00
|
|
|
return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_recv_waveconfirm(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-06-05 01:59:34 +04:00
|
|
|
UINT16 timestamp;
|
|
|
|
BYTE confirmBlockNum;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Read_UINT16(s, timestamp);
|
|
|
|
Stream_Read_UINT8(s, confirmBlockNum);
|
|
|
|
Stream_Seek_UINT8(s);
|
2019-11-06 17:24:51 +03:00
|
|
|
IFCALLRET(context->ConfirmBlock, error, context, confirmBlockNum, timestamp);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error)
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "context->ConfirmBlock failed with error %" PRIu32 "", error);
|
2014-06-05 01:59:34 +04:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_recv_quality_mode(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
UINT16 quality;
|
2016-09-26 13:02:33 +03:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Read_UINT16(s, quality);
|
|
|
|
Stream_Seek_UINT16(s); // reserved
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "Client requested sound quality: 0x%04" PRIX16 "", quality);
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpsnd_server_recv_formats(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2018-07-02 17:08:47 +03:00
|
|
|
UINT16 i, num_known_format = 0;
|
2013-08-19 05:52:55 +04:00
|
|
|
UINT32 flags, vol, pitch;
|
2014-09-11 13:33:10 +04:00
|
|
|
UINT16 udpPort;
|
2013-08-19 05:52:55 +04:00
|
|
|
BYTE lastblock;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 20)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Read_UINT32(s, flags); /* dwFlags */
|
|
|
|
Stream_Read_UINT32(s, vol); /* dwVolume */
|
|
|
|
Stream_Read_UINT32(s, pitch); /* dwPitch */
|
|
|
|
Stream_Read_UINT16(s, udpPort); /* wDGramPort */
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Read_UINT16(s, context->num_client_formats); /* wNumberOfFormats */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Read_UINT8(s, lastblock); /* cLastBlockConfirmed */
|
|
|
|
Stream_Read_UINT16(s, context->clientVersion); /* wVersion */
|
|
|
|
Stream_Seek_UINT8(s); /* bPad */
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
/* this check is only a guess as cbSize can influence the size of a format record */
|
|
|
|
if (Stream_GetRemainingLength(s) < context->num_client_formats * 18)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
if (!context->num_client_formats)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "client doesn't support any format!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2014-04-29 02:59:41 +04:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2018-09-25 15:34:14 +03:00
|
|
|
context->client_formats = audio_formats_new(context->num_client_formats);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-05-03 00:45:38 +04:00
|
|
|
if (!context->client_formats)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
for (i = 0; i < context->num_client_formats; i++)
|
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < 18)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2016-12-01 13:15:11 +03:00
|
|
|
WLog_ERR(TAG, "not enough data in stream!");
|
2015-07-15 10:50:35 +03:00
|
|
|
error = ERROR_INVALID_DATA;
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
Stream_Read_UINT16(s, context->client_formats[i].wFormatTag);
|
|
|
|
Stream_Read_UINT16(s, context->client_formats[i].nChannels);
|
|
|
|
Stream_Read_UINT32(s, context->client_formats[i].nSamplesPerSec);
|
|
|
|
Stream_Read_UINT32(s, context->client_formats[i].nAvgBytesPerSec);
|
|
|
|
Stream_Read_UINT16(s, context->client_formats[i].nBlockAlign);
|
|
|
|
Stream_Read_UINT16(s, context->client_formats[i].wBitsPerSample);
|
|
|
|
Stream_Read_UINT16(s, context->client_formats[i].cbSize);
|
|
|
|
|
|
|
|
if (context->client_formats[i].cbSize > 0)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!Stream_SafeSeek(s, context->client_formats[i].cbSize))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_SafeSeek failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (context->client_formats[i].wFormatTag != 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
// lets call this a known format
|
|
|
|
// TODO: actually look through our own list of known formats
|
2014-04-29 02:59:41 +04:00
|
|
|
num_known_format++;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
if (!context->num_client_formats)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "client doesn't support any known format!");
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-04-29 02:59:41 +04:00
|
|
|
out_free:
|
|
|
|
free(context->client_formats);
|
2015-07-15 10:50:35 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
static DWORD WINAPI rdpsnd_server_thread(LPVOID arg)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-07-02 12:31:45 +04:00
|
|
|
DWORD nCount, status;
|
2013-08-19 05:52:55 +04:00
|
|
|
HANDLE events[8];
|
|
|
|
RdpsndServerContext* context;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-09-26 13:12:37 +03:00
|
|
|
context = (RdpsndServerContext*)arg;
|
2013-08-19 05:52:55 +04:00
|
|
|
nCount = 0;
|
2014-07-02 12:31:45 +04:00
|
|
|
events[nCount++] = context->priv->channelEvent;
|
2013-08-19 05:52:55 +04:00
|
|
|
events[nCount++] = context->priv->StopEvent;
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
while (TRUE)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
status = WaitForSingleObject(context->priv->StopEvent, 0);
|
2015-07-30 16:49:21 +03:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-07-30 16:49:21 +03:00
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
2013-08-19 05:52:55 +04:00
|
|
|
break;
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if ((error = rdpsnd_server_handle_messages(context)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "rdpsnd_server_handle_messages failed with error %" PRIu32 "", error);
|
2014-04-29 02:59:41 +04:00
|
|
|
break;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && context->rdpcontext)
|
2019-11-06 17:24:51 +03:00
|
|
|
setChannelError(context->rdpcontext, error, "rdpsnd_server_thread reported an error");
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2018-03-07 14:03:10 +03:00
|
|
|
ExitThread(error);
|
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_initialize(RdpsndServerContext* context, BOOL ownThread)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-07-02 12:31:45 +04:00
|
|
|
context->priv->ownThread = ownThread;
|
2015-07-15 10:50:35 +03:00
|
|
|
return context->Start(context);
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_select_format(RdpsndServerContext* context, UINT16 client_format_index)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
int bs;
|
|
|
|
int out_buffer_size;
|
2016-09-26 13:12:37 +03:00
|
|
|
AUDIO_FORMAT* format;
|
2016-05-19 20:02:08 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if ((client_format_index >= context->num_client_formats) || (!context->src_format))
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "index %d is not correct.", client_format_index);
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INVALID_DATA;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
2016-09-26 13:02:33 +03:00
|
|
|
|
2016-05-19 20:02:08 +03:00
|
|
|
EnterCriticalSection(&context->priv->lock);
|
2018-09-25 18:05:12 +03:00
|
|
|
context->priv->src_bytes_per_sample = context->src_format->wBitsPerSample / 8;
|
2019-11-06 17:24:51 +03:00
|
|
|
context->priv->src_bytes_per_frame =
|
|
|
|
context->priv->src_bytes_per_sample * context->src_format->nChannels;
|
2013-08-19 05:52:55 +04:00
|
|
|
context->selected_client_format = client_format_index;
|
|
|
|
format = &context->client_formats[client_format_index];
|
2016-09-26 13:02:33 +03:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
if (format->nSamplesPerSec == 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "invalid Client Sound Format!!");
|
2016-05-19 20:02:08 +03:00
|
|
|
error = ERROR_INVALID_DATA;
|
|
|
|
goto out;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2016-06-21 09:05:29 +03:00
|
|
|
if (context->latency <= 0)
|
|
|
|
context->latency = 50;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
context->priv->out_frames = context->src_format->nSamplesPerSec * context->latency / 1000;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2016-06-21 09:05:29 +03:00
|
|
|
if (context->priv->out_frames < 1)
|
|
|
|
context->priv->out_frames = 1;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
|
|
|
switch (format->wFormatTag)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-04-29 02:59:41 +04:00
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
|
|
|
bs = (format->nBlockAlign - 4 * format->nChannels) * 4;
|
2016-06-21 09:05:29 +03:00
|
|
|
context->priv->out_frames -= context->priv->out_frames % bs;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2016-06-21 09:05:29 +03:00
|
|
|
if (context->priv->out_frames < bs)
|
|
|
|
context->priv->out_frames = bs;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
bs = (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
|
2016-06-21 09:05:29 +03:00
|
|
|
context->priv->out_frames -= context->priv->out_frames % bs;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2016-06-21 09:05:29 +03:00
|
|
|
if (context->priv->out_frames < bs)
|
|
|
|
context->priv->out_frames = bs;
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
break;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
context->priv->out_pending_frames = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
|
2016-09-26 13:02:33 +03:00
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
if (context->priv->out_buffer_size < out_buffer_size)
|
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
BYTE* newBuffer;
|
|
|
|
newBuffer = (BYTE*)realloc(context->priv->out_buffer, out_buffer_size);
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
if (!newBuffer)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "realloc failed!");
|
2016-05-19 20:02:08 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto out;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
context->priv->out_buffer = newBuffer;
|
2013-08-19 05:52:55 +04:00
|
|
|
context->priv->out_buffer_size = out_buffer_size;
|
|
|
|
}
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
freerdp_dsp_context_reset(context->priv->dsp_context, format);
|
2016-05-19 20:02:08 +03:00
|
|
|
out:
|
|
|
|
LeaveCriticalSection(&context->priv->lock);
|
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2018-10-22 13:00:07 +03:00
|
|
|
static BOOL rdpsnd_server_align_wave_pdu(wStream* s, UINT32 alignment)
|
|
|
|
{
|
2018-10-22 13:16:18 +03:00
|
|
|
size_t size;
|
|
|
|
Stream_SealLength(s);
|
|
|
|
size = Stream_Length(s);
|
2018-10-22 13:00:07 +03:00
|
|
|
|
|
|
|
if ((size % alignment) != 0)
|
|
|
|
{
|
|
|
|
size_t offset = alignment - size % alignment;
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, offset))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Zero(s, offset);
|
|
|
|
}
|
|
|
|
|
2018-10-22 13:16:18 +03:00
|
|
|
Stream_SealLength(s);
|
2018-10-22 13:00:07 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
2016-05-19 20:02:08 +03:00
|
|
|
* context->priv->lock should be obtained before calling this function
|
2015-08-27 15:25:09 +03:00
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_send_wave_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
size_t length;
|
|
|
|
size_t start, end = 0;
|
|
|
|
const BYTE* src;
|
2013-08-19 05:52:55 +04:00
|
|
|
AUDIO_FORMAT* format;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-19 05:52:55 +04:00
|
|
|
wStream* s = context->priv->rdpsnd_pdu;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2018-11-09 12:05:11 +03:00
|
|
|
|
|
|
|
if (context->selected_client_format >= context->num_client_formats)
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
format = &context->client_formats[context->selected_client_format];
|
|
|
|
/* WaveInfo PDU */
|
|
|
|
Stream_SetPosition(s, 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, SNDC_WAVE); /* msgType */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
|
|
|
Stream_Write_UINT16(s, 0); /* BodySize */
|
|
|
|
Stream_Write_UINT16(s, wTimestamp); /* wTimeStamp */
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT16(s, context->selected_client_format); /* wFormatNo */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, context->block_no); /* cBlockNo */
|
|
|
|
Stream_Seek(s, 3); /* bPad */
|
2018-02-20 14:15:30 +03:00
|
|
|
start = Stream_GetPosition(s);
|
|
|
|
src = context->priv->out_buffer;
|
2021-01-25 12:06:01 +03:00
|
|
|
length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame * 1ULL;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2018-11-09 12:05:11 +03:00
|
|
|
if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
|
2018-10-22 13:00:07 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2018-02-20 14:15:30 +03:00
|
|
|
else
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
/* Set stream size */
|
2018-10-22 13:00:07 +03:00
|
|
|
if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
end = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
|
|
|
Stream_Write_UINT16(s, end - start + 8);
|
|
|
|
Stream_SetPosition(s, end);
|
|
|
|
context->block_no = (context->block_no + 1) % 256;
|
2018-08-01 12:56:47 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
|
|
|
|
start + 4, &written))
|
2018-08-01 12:56:47 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:56:47 +03:00
|
|
|
if (error != CHANNEL_RC_OK)
|
2015-03-26 19:09:47 +03:00
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2015-03-26 19:09:47 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
Stream_SetPosition(s, start);
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT32(s, 0); /* bPad */
|
2018-02-20 14:15:30 +03:00
|
|
|
Stream_SetPosition(s, start);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Pointer(s), end - start,
|
|
|
|
&written))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
|
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
out:
|
|
|
|
Stream_SetPosition(s, 0);
|
2013-08-19 05:52:55 +04:00
|
|
|
context->priv->out_pending_frames = 0;
|
2015-07-15 10:50:35 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
* context->priv->lock should be obtained before calling this function
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_send_wave2_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
|
2018-07-02 17:08:47 +03:00
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
size_t end = 0;
|
|
|
|
const BYTE* src;
|
|
|
|
AUDIO_FORMAT* format;
|
|
|
|
ULONG written;
|
|
|
|
wStream* s = context->priv->rdpsnd_pdu;
|
|
|
|
UINT error = CHANNEL_RC_OK;
|
2018-11-09 12:05:11 +03:00
|
|
|
|
|
|
|
if (context->selected_client_format >= context->num_client_formats)
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
format = &context->client_formats[context->selected_client_format];
|
|
|
|
/* WaveInfo PDU */
|
|
|
|
Stream_SetPosition(s, 0);
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, SNDC_WAVE2); /* msgType */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
|
|
|
Stream_Write_UINT16(s, 0); /* BodySize */
|
|
|
|
Stream_Write_UINT16(s, wTimestamp); /* wTimeStamp */
|
2018-07-02 17:08:47 +03:00
|
|
|
Stream_Write_UINT16(s, context->selected_client_format); /* wFormatNo */
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, context->block_no); /* cBlockNo */
|
|
|
|
Stream_Seek(s, 3); /* bPad */
|
|
|
|
Stream_Write_UINT32(s, wTimestamp); /* dwAudioTimeStamp */
|
2018-07-02 17:08:47 +03:00
|
|
|
src = context->priv->out_buffer;
|
|
|
|
length = context->priv->out_pending_frames * context->priv->src_bytes_per_frame;
|
|
|
|
|
2018-11-13 16:46:51 +03:00
|
|
|
if (!freerdp_dsp_encode(context->priv->dsp_context, context->src_format, src, length, s))
|
2018-08-01 12:56:47 +03:00
|
|
|
error = ERROR_INTERNAL_ERROR;
|
2018-07-02 17:08:47 +03:00
|
|
|
else
|
|
|
|
{
|
2018-10-22 13:16:18 +03:00
|
|
|
BOOL rc;
|
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
/* Set stream size */
|
2018-10-22 13:00:07 +03:00
|
|
|
if (!rdpsnd_server_align_wave_pdu(s, format->nBlockAlign))
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
end = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
2018-07-11 16:06:31 +03:00
|
|
|
Stream_Write_UINT16(s, end - 4);
|
2018-07-02 17:08:47 +03:00
|
|
|
context->block_no = (context->block_no + 1) % 256;
|
2019-11-06 17:24:51 +03:00
|
|
|
rc = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s), end,
|
|
|
|
&written);
|
2018-07-02 17:08:47 +03:00
|
|
|
|
2018-10-22 13:16:18 +03:00
|
|
|
if (!rc || (end != written))
|
2018-08-01 12:56:47 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"WTSVirtualChannelWrite failed! [stream length=%" PRIdz " - written=%" PRIu32,
|
|
|
|
end, written);
|
2018-08-01 12:56:47 +03:00
|
|
|
error = ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2018-07-02 17:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
context->priv->out_pending_frames = 0;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper function to send WAVE or WAVE2 PDU depending on client connected */
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
|
2018-07-02 17:08:47 +03:00
|
|
|
{
|
|
|
|
if (context->clientVersion >= CHANNEL_VERSION_WIN_8)
|
|
|
|
return rdpsnd_server_send_wave2_pdu(context, wTimestamp);
|
|
|
|
else
|
|
|
|
return rdpsnd_server_send_wave_pdu(context, wTimestamp);
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_send_samples(RdpsndServerContext* context, const void* buf, int nframes,
|
|
|
|
UINT16 wTimestamp)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
int cframes;
|
|
|
|
int cframesize;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-05-19 20:02:08 +03:00
|
|
|
EnterCriticalSection(&context->priv->lock);
|
|
|
|
|
2018-11-09 12:05:11 +03:00
|
|
|
if (context->selected_client_format >= context->num_client_formats)
|
2016-05-19 20:02:08 +03:00
|
|
|
{
|
|
|
|
/* It's possible while format negotiation has not been done */
|
|
|
|
WLog_WARN(TAG, "Drop samples because client format has not been negotiated.");
|
|
|
|
error = ERROR_NOT_READY;
|
|
|
|
goto out;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
while (nframes > 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
cframes = MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
|
2013-08-19 05:52:55 +04:00
|
|
|
cframesize = cframes * context->priv->src_bytes_per_frame;
|
|
|
|
CopyMemory(context->priv->out_buffer +
|
2019-11-06 17:24:51 +03:00
|
|
|
(context->priv->out_pending_frames * context->priv->src_bytes_per_frame),
|
|
|
|
buf, cframesize);
|
|
|
|
buf = (BYTE*)buf + cframesize;
|
2013-08-19 05:52:55 +04:00
|
|
|
nframes -= cframes;
|
|
|
|
context->priv->out_pending_frames += cframes;
|
|
|
|
|
|
|
|
if (context->priv->out_pending_frames >= context->priv->out_frames)
|
|
|
|
{
|
2015-07-15 10:50:35 +03:00
|
|
|
if ((error = rdpsnd_server_send_audio_pdu(context, wTimestamp)))
|
2016-05-19 20:02:08 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "rdpsnd_server_send_audio_pdu failed with error %" PRIu32 "", error);
|
2016-05-19 20:02:08 +03:00
|
|
|
break;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 20:02:08 +03:00
|
|
|
out:
|
|
|
|
LeaveCriticalSection(&context->priv->lock);
|
2015-07-15 10:50:35 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT rdpsnd_server_set_volume(RdpsndServerContext* context, int left, int right)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2017-12-11 12:25:21 +03:00
|
|
|
size_t pos;
|
2013-08-19 05:52:55 +04:00
|
|
|
BOOL status;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-19 05:52:55 +04:00
|
|
|
wStream* s = context->priv->rdpsnd_pdu;
|
|
|
|
Stream_Write_UINT8(s, SNDC_SETVOLUME);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
|
|
|
Stream_Seek_UINT16(s);
|
|
|
|
Stream_Write_UINT16(s, left);
|
|
|
|
Stream_Write_UINT16(s, right);
|
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
|
|
|
Stream_SetPosition(s, pos);
|
2019-11-06 17:24:51 +03:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
|
|
|
|
Stream_GetPosition(s), &written);
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2015-07-15 10:50:35 +03:00
|
|
|
return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpsnd_server_close(RdpsndServerContext* context)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2017-12-11 12:25:21 +03:00
|
|
|
size_t pos;
|
2013-08-19 05:52:55 +04:00
|
|
|
BOOL status;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-19 05:52:55 +04:00
|
|
|
wStream* s = context->priv->rdpsnd_pdu;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2016-05-19 20:02:08 +03:00
|
|
|
EnterCriticalSection(&context->priv->lock);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
if (context->priv->out_pending_frames > 0)
|
|
|
|
{
|
2018-11-09 12:05:11 +03:00
|
|
|
if (context->selected_client_format >= context->num_client_formats)
|
2016-05-19 20:02:08 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Pending audio frame exists while no format selected.");
|
|
|
|
error = ERROR_INVALID_DATA;
|
2016-09-26 13:02:33 +03:00
|
|
|
}
|
2016-05-19 20:02:08 +03:00
|
|
|
else if ((error = rdpsnd_server_send_audio_pdu(context, 0)))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "rdpsnd_server_send_audio_pdu failed with error %" PRIu32 "", error);
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2016-05-19 20:02:08 +03:00
|
|
|
LeaveCriticalSection(&context->priv->lock);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
2018-11-09 12:05:11 +03:00
|
|
|
context->selected_client_format = 0xFFFF;
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_CLOSE);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
|
|
|
Stream_Seek_UINT16(s);
|
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
|
|
|
Stream_SetPosition(s, pos);
|
2019-11-06 17:24:51 +03:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR)Stream_Buffer(s),
|
|
|
|
Stream_GetPosition(s), &written);
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2015-07-15 10:50:35 +03:00
|
|
|
return status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpsnd_server_start(RdpsndServerContext* context)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
void* buffer = NULL;
|
2014-07-02 12:31:45 +04:00
|
|
|
DWORD bytesReturned;
|
2016-09-26 13:12:37 +03:00
|
|
|
RdpsndServerPrivate* priv = context->priv;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = ERROR_INTERNAL_ERROR;
|
2019-11-06 17:24:51 +03:00
|
|
|
priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpsnd");
|
2014-07-02 12:31:45 +04:00
|
|
|
|
|
|
|
if (!priv->ChannelHandle)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
|
2019-11-06 17:24:51 +03:00
|
|
|
&bytesReturned) ||
|
|
|
|
(bytesReturned != sizeof(HANDLE)))
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
WLog_ERR(TAG,
|
2019-11-06 17:24:51 +03:00
|
|
|
"error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned "
|
|
|
|
"size(%" PRIu32 ")",
|
2016-09-26 13:12:37 +03:00
|
|
|
bytesReturned);
|
2014-09-12 18:19:32 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (buffer)
|
|
|
|
WTSFreeMemory(buffer);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_close;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
CopyMemory(&priv->channelEvent, buffer, sizeof(HANDLE));
|
|
|
|
WTSFreeMemory(buffer);
|
|
|
|
priv->rdpsnd_pdu = Stream_New(NULL, 4096);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!priv->rdpsnd_pdu)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
2014-07-02 12:31:45 +04:00
|
|
|
goto out_close;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2016-05-19 20:02:08 +03:00
|
|
|
if (!InitializeCriticalSectionEx(&context->priv->lock, 0, 0))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "InitializeCriticalSectionEx failed!");
|
|
|
|
goto out_pdu;
|
|
|
|
}
|
|
|
|
|
2018-10-18 11:15:24 +03:00
|
|
|
if ((error = rdpsnd_server_send_formats(context, context->priv->rdpsnd_pdu)))
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "rdpsnd_server_send_formats failed with error %" PRIu32 "", error);
|
2018-10-18 11:15:24 +03:00
|
|
|
goto out_lock;
|
|
|
|
}
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (priv->ownThread)
|
|
|
|
{
|
|
|
|
context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!context->priv->StopEvent)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
2016-05-19 20:02:08 +03:00
|
|
|
goto out_lock;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
context->priv->Thread =
|
|
|
|
CreateThread(NULL, 0, rdpsnd_server_thread, (void*)context, 0, NULL);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!context->priv->Thread)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
2014-07-02 12:31:45 +04:00
|
|
|
goto out_stopEvent;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-04-29 02:59:41 +04:00
|
|
|
out_stopEvent:
|
|
|
|
CloseHandle(context->priv->StopEvent);
|
|
|
|
context->priv->StopEvent = NULL;
|
2016-05-19 20:02:08 +03:00
|
|
|
out_lock:
|
|
|
|
DeleteCriticalSection(&context->priv->lock);
|
2014-04-29 02:59:41 +04:00
|
|
|
out_pdu:
|
|
|
|
Stream_Free(context->priv->rdpsnd_pdu, TRUE);
|
|
|
|
context->priv->rdpsnd_pdu = NULL;
|
|
|
|
out_close:
|
|
|
|
WTSVirtualChannelClose(context->priv->ChannelHandle);
|
|
|
|
context->priv->ChannelHandle = NULL;
|
2015-07-15 10:50:35 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT rdpsnd_server_stop(RdpsndServerContext* context)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2016-09-26 13:12:37 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (context->priv->ownThread)
|
2014-04-29 02:59:41 +04:00
|
|
|
{
|
2014-07-02 12:31:45 +04:00
|
|
|
if (context->priv->StopEvent)
|
|
|
|
{
|
|
|
|
SetEvent(context->priv->StopEvent);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
|
2016-09-26 13:12:37 +03:00
|
|
|
{
|
|
|
|
error = GetLastError();
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
|
2016-09-26 13:12:37 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
CloseHandle(context->priv->Thread);
|
2015-01-20 13:59:13 +03:00
|
|
|
CloseHandle(context->priv->StopEvent);
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2016-05-19 20:02:08 +03:00
|
|
|
DeleteCriticalSection(&context->priv->lock);
|
|
|
|
|
2015-01-20 13:59:13 +03:00
|
|
|
if (context->priv->rdpsnd_pdu)
|
|
|
|
Stream_Free(context->priv->rdpsnd_pdu, TRUE);
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return error;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 22:30:04 +04:00
|
|
|
RdpsndServerContext* rdpsnd_server_context_new(HANDLE vcm)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
RdpsndServerContext* context;
|
2016-09-26 13:12:37 +03:00
|
|
|
RdpsndServerPrivate* priv;
|
|
|
|
context = (RdpsndServerContext*)calloc(1, sizeof(RdpsndServerContext));
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!context)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2014-04-29 02:59:41 +04:00
|
|
|
return NULL;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
context->vcm = vcm;
|
|
|
|
context->Start = rdpsnd_server_start;
|
|
|
|
context->Stop = rdpsnd_server_stop;
|
2018-11-09 12:05:11 +03:00
|
|
|
context->selected_client_format = 0xFFFF;
|
2014-04-29 02:59:41 +04:00
|
|
|
context->Initialize = rdpsnd_server_initialize;
|
|
|
|
context->SelectFormat = rdpsnd_server_select_format;
|
|
|
|
context->SendSamples = rdpsnd_server_send_samples;
|
|
|
|
context->SetVolume = rdpsnd_server_set_volume;
|
|
|
|
context->Close = rdpsnd_server_close;
|
2019-11-06 17:24:51 +03:00
|
|
|
context->priv = priv = (RdpsndServerPrivate*)calloc(1, sizeof(RdpsndServerPrivate));
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!priv)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
priv->dsp_context = freerdp_dsp_context_new(TRUE);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!priv->dsp_context)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "freerdp_dsp_context_new failed!");
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free_priv;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
priv->input_stream = Stream_New(NULL, 4);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!priv->input_stream)
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_New failed!");
|
2014-07-02 12:31:45 +04:00
|
|
|
goto out_free_dsp;
|
2015-07-15 10:50:35 +03:00
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
|
|
|
|
priv->expectedBytes = 4;
|
|
|
|
priv->waitingHeader = TRUE;
|
|
|
|
priv->ownThread = TRUE;
|
2013-08-19 05:52:55 +04:00
|
|
|
return context;
|
2014-07-02 12:31:45 +04:00
|
|
|
out_free_dsp:
|
|
|
|
freerdp_dsp_context_free(priv->dsp_context);
|
2014-04-29 02:59:41 +04:00
|
|
|
out_free_priv:
|
|
|
|
free(context->priv);
|
|
|
|
out_free:
|
|
|
|
free(context);
|
|
|
|
return NULL;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
void rdpsnd_server_context_reset(RdpsndServerContext* context)
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
|
|
|
context->priv->expectedBytes = 4;
|
|
|
|
context->priv->waitingHeader = TRUE;
|
|
|
|
Stream_SetPosition(context->priv->input_stream, 0);
|
|
|
|
}
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
void rdpsnd_server_context_free(RdpsndServerContext* context)
|
|
|
|
{
|
|
|
|
if (context->priv->ChannelHandle)
|
|
|
|
WTSVirtualChannelClose(context->priv->ChannelHandle);
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(context->priv->out_buffer);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
if (context->priv->dsp_context)
|
|
|
|
freerdp_dsp_context_free(context->priv->dsp_context);
|
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
if (context->priv->input_stream)
|
|
|
|
Stream_Free(context->priv->input_stream, TRUE);
|
2015-01-20 13:59:13 +03:00
|
|
|
|
2020-01-15 14:51:05 +03:00
|
|
|
free(context->server_formats);
|
2015-05-11 10:07:39 +03:00
|
|
|
free(context->client_formats);
|
2015-09-15 18:17:22 +03:00
|
|
|
free(context->priv);
|
2013-08-19 05:52:55 +04:00
|
|
|
free(context);
|
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext* context)
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
|
|
|
return context->priv->channelEvent;
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:45:23 +04:00
|
|
|
/*
|
|
|
|
* Handle rpdsnd messages - server side
|
|
|
|
*
|
|
|
|
* @param Server side context
|
|
|
|
*
|
2015-07-15 10:50:35 +03:00
|
|
|
* @return 0 on success
|
2015-07-22 13:16:46 +03:00
|
|
|
* ERROR_NO_DATA if no data could be read this time
|
2015-07-15 10:50:35 +03:00
|
|
|
* otherwise error
|
2014-09-11 15:45:23 +04:00
|
|
|
*/
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
2016-09-26 13:12:37 +03:00
|
|
|
UINT rdpsnd_server_handle_messages(RdpsndServerContext* context)
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
|
|
|
DWORD bytesReturned;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT ret = CHANNEL_RC_OK;
|
2016-09-26 13:12:37 +03:00
|
|
|
RdpsndServerPrivate* priv = context->priv;
|
|
|
|
wStream* s = priv->input_stream;
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
if (!WTSVirtualChannelRead(priv->ChannelHandle, 0, (PCHAR)Stream_Pointer(s),
|
|
|
|
priv->expectedBytes, &bytesReturned))
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
|
|
|
if (GetLastError() == ERROR_NO_DATA)
|
2015-07-22 13:16:46 +03:00
|
|
|
return ERROR_NO_DATA;
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "channel connection closed");
|
2015-07-15 10:50:35 +03:00
|
|
|
return ERROR_INTERNAL_ERROR;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
priv->expectedBytes -= bytesReturned;
|
|
|
|
Stream_Seek(s, bytesReturned);
|
|
|
|
|
|
|
|
if (priv->expectedBytes)
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2014-09-11 13:43:05 +04:00
|
|
|
Stream_SealLength(s);
|
2014-07-02 12:31:45 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (priv->waitingHeader)
|
|
|
|
{
|
|
|
|
/* header case */
|
|
|
|
Stream_Read_UINT8(s, priv->msgType);
|
|
|
|
Stream_Seek_UINT8(s); /* bPad */
|
|
|
|
Stream_Read_UINT16(s, priv->expectedBytes);
|
|
|
|
priv->waitingHeader = FALSE;
|
|
|
|
Stream_SetPosition(s, 0);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (priv->expectedBytes)
|
|
|
|
{
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureCapacity(s, priv->expectedBytes))
|
2015-07-15 10:50:35 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Stream_EnsureCapacity failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2015-07-15 10:50:35 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when here we have the header + the body */
|
|
|
|
#ifdef WITH_DEBUG_SND
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_DBG(TAG, "message type %" PRIu8 "", priv->msgType);
|
2014-07-02 12:31:45 +04:00
|
|
|
#endif
|
|
|
|
priv->expectedBytes = 4;
|
|
|
|
priv->waitingHeader = TRUE;
|
|
|
|
|
|
|
|
switch (priv->msgType)
|
|
|
|
{
|
|
|
|
case SNDC_WAVECONFIRM:
|
|
|
|
ret = rdpsnd_server_recv_waveconfirm(context, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNDC_FORMATS:
|
|
|
|
ret = rdpsnd_server_recv_formats(context, s);
|
2014-09-11 13:33:10 +04:00
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
if ((ret == CHANNEL_RC_OK) && (context->clientVersion < CHANNEL_VERSION_WIN_7))
|
2014-09-11 13:33:10 +04:00
|
|
|
IFCALL(context->Activated, context);
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SNDC_QUALITYMODE:
|
|
|
|
ret = rdpsnd_server_recv_quality_mode(context, s);
|
2016-09-26 13:12:37 +03:00
|
|
|
Stream_SetPosition(s,
|
|
|
|
0); /* in case the Activated callback tries to treat some messages */
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2018-07-02 17:08:47 +03:00
|
|
|
if ((ret == CHANNEL_RC_OK) && (context->clientVersion >= CHANNEL_VERSION_WIN_7))
|
2014-07-02 12:31:45 +04:00
|
|
|
IFCALL(context->Activated, context);
|
2016-09-26 13:12:37 +03:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "UNKNOWN MESSAGE TYPE!! (0x%02" PRIX8 ")", priv->msgType);
|
2015-07-15 10:50:35 +03:00
|
|
|
ret = ERROR_INVALID_DATA;
|
2014-07-02 12:31:45 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-26 13:12:37 +03:00
|
|
|
Stream_SetPosition(s, 0);
|
2015-07-15 10:50:35 +03:00
|
|
|
return ret;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|