2013-08-19 05:52:55 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* Server Audio Virtual Channel
|
|
|
|
*
|
|
|
|
* Copyright 2012 Vic Lee
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
#include "rdpsnd_main.h"
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
BOOL rdpsnd_server_send_formats(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
UINT16 i;
|
|
|
|
BOOL status;
|
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);
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, 0); /* dwFlags */
|
|
|
|
Stream_Write_UINT32(s, 0); /* dwVolume */
|
|
|
|
Stream_Write_UINT32(s, 0); /* dwPitch */
|
|
|
|
Stream_Write_UINT16(s, 0); /* wDGramPort */
|
|
|
|
Stream_Write_UINT16(s, context->num_server_formats); /* wNumberOfFormats */
|
|
|
|
Stream_Write_UINT8(s, context->block_no); /* cLastBlockConfirmed */
|
|
|
|
Stream_Write_UINT16(s, 0x06); /* wVersion */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
|
|
|
|
|
|
|
for (i = 0; i < context->num_server_formats; i++)
|
|
|
|
{
|
|
|
|
Stream_Write_UINT16(s, context->server_formats[i].wFormatTag); /* wFormatTag (WAVE_FORMAT_PCM) */
|
|
|
|
Stream_Write_UINT16(s, context->server_formats[i].nChannels); /* nChannels */
|
|
|
|
Stream_Write_UINT32(s, context->server_formats[i].nSamplesPerSec); /* nSamplesPerSec */
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, context->server_formats[i].nSamplesPerSec *
|
|
|
|
context->server_formats[i].nChannels *
|
|
|
|
context->server_formats[i].wBitsPerSample / 8); /* nAvgBytesPerSec */
|
|
|
|
|
|
|
|
Stream_Write_UINT16(s, context->server_formats[i].nBlockAlign); /* nBlockAlign */
|
|
|
|
Stream_Write_UINT16(s, context->server_formats[i].wBitsPerSample); /* wBitsPerSample */
|
|
|
|
Stream_Write_UINT16(s, context->server_formats[i].cbSize); /* cbSize */
|
|
|
|
|
|
|
|
if (context->server_formats[i].cbSize > 0)
|
|
|
|
{
|
|
|
|
Stream_Write(s, context->server_formats[i].data, context->server_formats[i].cbSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
|
|
|
Stream_SetPosition(s, pos);
|
2014-05-28 19:04:24 +04: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);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
static BOOL 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;
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Read_UINT16(s, timestamp);
|
|
|
|
Stream_Read_UINT8(s, confirmBlockNum);
|
|
|
|
Stream_Seek_UINT8(s);
|
2014-06-05 01:59:34 +04:00
|
|
|
|
|
|
|
IFCALL(context->ConfirmBlock, context, confirmBlockNum, timestamp);
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
return TRUE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
static BOOL rdpsnd_server_recv_quality_mode(RdpsndServerContext* context, wStream* s)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
UINT16 quality;
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Read_UINT16(s, quality);
|
|
|
|
Stream_Seek_UINT16(s); // reserved
|
|
|
|
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "Client requested sound quality: %#0X\n", quality);
|
2014-04-29 02:59:41 +04:00
|
|
|
return TRUE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rdpsnd_server_recv_formats(RdpsndServerContext* context, wStream* s)
|
|
|
|
{
|
|
|
|
int i, num_known_format = 0;
|
|
|
|
UINT32 flags, vol, pitch;
|
2014-09-11 13:33:10 +04:00
|
|
|
UINT16 udpPort;
|
2013-08-19 05:52:55 +04:00
|
|
|
BYTE lastblock;
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 20)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-08-19 05:52:55 +04: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 */
|
|
|
|
Stream_Read_UINT16(s, context->num_client_formats); /* wNumberOfFormats */
|
|
|
|
Stream_Read_UINT8(s, lastblock); /* cLastBlockConfirmed */
|
2014-09-11 13:33:10 +04:00
|
|
|
Stream_Read_UINT16(s, context->clientVersion); /* wVersion */
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Seek_UINT8(s); /* bPad */
|
|
|
|
|
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)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!context->num_client_formats)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: client doesn't support any format!\n", __FUNCTION__);
|
2014-04-29 02:59:41 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
context->client_formats = (AUDIO_FORMAT *)calloc(context->num_client_formats, sizeof(AUDIO_FORMAT));
|
2014-05-03 00:45:38 +04:00
|
|
|
if (!context->client_formats)
|
2014-04-29 02:59:41 +04:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (i = 0; i < context->num_client_formats; i++)
|
|
|
|
{
|
|
|
|
if (Stream_GetRemainingLength(s) < 18)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
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))
|
|
|
|
goto out_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context->client_formats[i].wFormatTag != 0)
|
|
|
|
{
|
|
|
|
//lets call this a known format
|
|
|
|
//TODO: actually look through our own list of known formats
|
|
|
|
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
|
|
|
{
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: client doesn't support any known format!\n", __FUNCTION__);
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
out_free:
|
|
|
|
free(context->client_formats);
|
|
|
|
return FALSE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* rdpsnd_server_thread(void* arg)
|
|
|
|
{
|
2014-07-02 12:31:45 +04:00
|
|
|
DWORD nCount, status;
|
2013-08-19 05:52:55 +04:00
|
|
|
HANDLE events[8];
|
|
|
|
RdpsndServerContext* context;
|
2014-04-29 02:59:41 +04:00
|
|
|
BOOL doRun;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04: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;
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!rdpsnd_server_send_formats(context, context->priv->rdpsnd_pdu))
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
doRun = TRUE;
|
|
|
|
while (doRun)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(context->priv->StopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
2014-09-11 15:45:23 +04:00
|
|
|
if (rdpsnd_server_handle_messages(context) == 0)
|
2014-04-29 02:59:41 +04:00
|
|
|
break;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
out:
|
2013-08-19 05:52:55 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
static BOOL 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;
|
2014-05-02 23:53:52 +04:00
|
|
|
return context->Start(context) >= 0;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
static BOOL rdpsnd_server_select_format(RdpsndServerContext* context, int client_format_index)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
int bs;
|
|
|
|
int out_buffer_size;
|
|
|
|
AUDIO_FORMAT *format;
|
|
|
|
|
|
|
|
if (client_format_index < 0 || client_format_index >= context->num_client_formats)
|
|
|
|
{
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: index %d is not correct.\n", __FUNCTION__, client_format_index);
|
2014-04-29 02:59:41 +04:00
|
|
|
return FALSE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
context->priv->src_bytes_per_sample = context->src_format.wBitsPerSample / 8;
|
|
|
|
context->priv->src_bytes_per_frame = context->priv->src_bytes_per_sample * context->src_format.nChannels;
|
|
|
|
|
|
|
|
context->selected_client_format = client_format_index;
|
|
|
|
format = &context->client_formats[client_format_index];
|
|
|
|
|
|
|
|
if (format->nSamplesPerSec == 0)
|
|
|
|
{
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: invalid Client Sound Format!!\n", __FUNCTION__);
|
2014-04-29 02:59:41 +04:00
|
|
|
return FALSE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:59:41 +04: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;
|
|
|
|
context->priv->out_frames = (format->nBlockAlign * 4 * format->nChannels * 2 / bs + 1) * bs / (format->nChannels * 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
bs = (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
|
|
|
|
context->priv->out_frames = bs * 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
context->priv->out_frames = 0x4000 / context->priv->src_bytes_per_frame;
|
|
|
|
break;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (format->nSamplesPerSec != context->src_format.nSamplesPerSec)
|
|
|
|
{
|
|
|
|
context->priv->out_frames = (context->priv->out_frames * context->src_format.nSamplesPerSec + format->nSamplesPerSec - 100) / format->nSamplesPerSec;
|
|
|
|
}
|
|
|
|
context->priv->out_pending_frames = 0;
|
|
|
|
|
|
|
|
out_buffer_size = context->priv->out_frames * context->priv->src_bytes_per_frame;
|
|
|
|
|
|
|
|
if (context->priv->out_buffer_size < out_buffer_size)
|
|
|
|
{
|
2014-04-29 02:59:41 +04:00
|
|
|
BYTE *newBuffer;
|
|
|
|
|
|
|
|
newBuffer = (BYTE *)realloc(context->priv->out_buffer, out_buffer_size);
|
|
|
|
if (!newBuffer)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
context->priv->out_buffer = newBuffer;
|
2013-08-19 05:52:55 +04:00
|
|
|
context->priv->out_buffer_size = out_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_dsp_context_reset_adpcm(context->priv->dsp_context);
|
2014-04-29 02:59:41 +04:00
|
|
|
return TRUE;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
2014-06-05 02:16:10 +04:00
|
|
|
static BOOL rdpsnd_server_send_audio_pdu(RdpsndServerContext* context, UINT16 wTimestamp)
|
2013-08-19 05:52:55 +04:00
|
|
|
{
|
|
|
|
int size;
|
|
|
|
BYTE* src;
|
|
|
|
int frames;
|
|
|
|
int fill_size;
|
|
|
|
BOOL status;
|
|
|
|
AUDIO_FORMAT* format;
|
|
|
|
int tbytes_per_frame;
|
2014-05-28 19:04:24 +04:00
|
|
|
ULONG written;
|
2013-08-19 05:52:55 +04:00
|
|
|
wStream* s = context->priv->rdpsnd_pdu;
|
|
|
|
|
|
|
|
format = &context->client_formats[context->selected_client_format];
|
|
|
|
tbytes_per_frame = format->nChannels * context->priv->src_bytes_per_sample;
|
|
|
|
|
|
|
|
if ((format->nSamplesPerSec == context->src_format.nSamplesPerSec) &&
|
|
|
|
(format->nChannels == context->src_format.nChannels))
|
|
|
|
{
|
|
|
|
src = context->priv->out_buffer;
|
|
|
|
frames = context->priv->out_pending_frames;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context->priv->dsp_context->resample(context->priv->dsp_context, context->priv->out_buffer,
|
|
|
|
context->priv->src_bytes_per_sample, context->src_format.nChannels,
|
|
|
|
context->src_format.nSamplesPerSec, context->priv->out_pending_frames,
|
|
|
|
format->nChannels, format->nSamplesPerSec);
|
|
|
|
frames = context->priv->dsp_context->resampled_frames;
|
|
|
|
src = context->priv->dsp_context->resampled_buffer;
|
|
|
|
}
|
|
|
|
size = frames * tbytes_per_frame;
|
|
|
|
|
|
|
|
if (format->wFormatTag == WAVE_FORMAT_DVI_ADPCM)
|
|
|
|
{
|
|
|
|
context->priv->dsp_context->encode_ima_adpcm(context->priv->dsp_context,
|
|
|
|
src, size, format->nChannels, format->nBlockAlign);
|
|
|
|
src = context->priv->dsp_context->adpcm_buffer;
|
|
|
|
size = context->priv->dsp_context->adpcm_size;
|
|
|
|
}
|
|
|
|
else if (format->wFormatTag == WAVE_FORMAT_ADPCM)
|
|
|
|
{
|
|
|
|
context->priv->dsp_context->encode_ms_adpcm(context->priv->dsp_context,
|
|
|
|
src, size, format->nChannels, format->nBlockAlign);
|
|
|
|
src = context->priv->dsp_context->adpcm_buffer;
|
|
|
|
size = context->priv->dsp_context->adpcm_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->block_no = (context->block_no + 1) % 256;
|
|
|
|
|
|
|
|
/* Fill to nBlockAlign for the last audio packet */
|
|
|
|
|
|
|
|
fill_size = 0;
|
|
|
|
|
|
|
|
if ((format->wFormatTag == WAVE_FORMAT_DVI_ADPCM || format->wFormatTag == WAVE_FORMAT_ADPCM) &&
|
|
|
|
(context->priv->out_pending_frames < context->priv->out_frames) && ((size % format->nBlockAlign) != 0))
|
|
|
|
{
|
|
|
|
fill_size = format->nBlockAlign - (size % format->nBlockAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* WaveInfo PDU */
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
Stream_Write_UINT8(s, SNDC_WAVE); /* msgType */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
|
|
|
Stream_Write_UINT16(s, size + fill_size + 8); /* BodySize */
|
|
|
|
|
2014-06-05 02:16:10 +04:00
|
|
|
Stream_Write_UINT16(s, wTimestamp); /* wTimeStamp */
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_Write_UINT16(s, context->selected_client_format); /* wFormatNo */
|
|
|
|
Stream_Write_UINT8(s, context->block_no); /* cBlockNo */
|
|
|
|
Stream_Seek(s, 3); /* bPad */
|
|
|
|
Stream_Write(s, src, 4);
|
|
|
|
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_GetPosition(s), &written);
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!status)
|
|
|
|
goto out;
|
2013-08-19 05:52:55 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
|
|
|
|
/* Wave PDU */
|
|
|
|
Stream_EnsureRemainingCapacity(s, size + fill_size);
|
|
|
|
Stream_Write_UINT32(s, 0); /* bPad */
|
|
|
|
Stream_Write(s, src + 4, size - 4);
|
|
|
|
|
|
|
|
if (fill_size > 0)
|
|
|
|
Stream_Zero(s, fill_size);
|
|
|
|
|
2014-05-28 19:04:24 +04:00
|
|
|
status = WTSVirtualChannelWrite(context->priv->ChannelHandle, (PCHAR) Stream_Buffer(s), Stream_GetPosition(s), &written);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
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;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-06-05 02:16:10 +04:00
|
|
|
static BOOL 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;
|
|
|
|
|
|
|
|
if (context->selected_client_format < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while (nframes > 0)
|
|
|
|
{
|
|
|
|
cframes = MIN(nframes, context->priv->out_frames - context->priv->out_pending_frames);
|
|
|
|
cframesize = cframes * context->priv->src_bytes_per_frame;
|
|
|
|
|
|
|
|
CopyMemory(context->priv->out_buffer +
|
|
|
|
(context->priv->out_pending_frames * context->priv->src_bytes_per_frame), buf, cframesize);
|
|
|
|
buf = (BYTE*) buf + cframesize;
|
|
|
|
nframes -= cframes;
|
|
|
|
context->priv->out_pending_frames += cframes;
|
|
|
|
|
|
|
|
if (context->priv->out_pending_frames >= context->priv->out_frames)
|
|
|
|
{
|
2014-06-05 02:16:10 +04:00
|
|
|
if (!rdpsnd_server_send_audio_pdu(context, wTimestamp))
|
2013-08-19 05:52:55 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rdpsnd_server_set_volume(RdpsndServerContext* context, int left, int right)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
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);
|
2014-05-28 19:04:24 +04: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);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL rdpsnd_server_close(RdpsndServerContext* context)
|
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (context->selected_client_format < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (context->priv->out_pending_frames > 0)
|
|
|
|
{
|
2014-06-05 02:16:10 +04:00
|
|
|
if (!rdpsnd_server_send_audio_pdu(context, 0))
|
2013-08-19 05:52:55 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->selected_client_format = -1;
|
|
|
|
|
|
|
|
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);
|
2014-05-28 19:04:24 +04: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);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rdpsnd_server_start(RdpsndServerContext* context)
|
|
|
|
{
|
2014-07-02 12:31:45 +04:00
|
|
|
void *buffer = NULL;
|
|
|
|
DWORD bytesReturned;
|
|
|
|
RdpsndServerPrivate *priv = context->priv;
|
|
|
|
|
|
|
|
priv->ChannelHandle = WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, "rdpsnd");
|
|
|
|
if (!priv->ChannelHandle)
|
2013-08-19 05:52:55 +04:00
|
|
|
return -1;
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
if (!WTSVirtualChannelQuery(priv->ChannelHandle, WTSVirtualEventHandle, &buffer, &bytesReturned) || (bytesReturned != sizeof(HANDLE)))
|
|
|
|
{
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: error during WTSVirtualChannelQuery(WTSVirtualEventHandle) or invalid returned size(%d)\n",
|
2014-07-02 12:31:45 +04:00
|
|
|
__FUNCTION__, bytesReturned);
|
|
|
|
if (buffer)
|
|
|
|
WTSFreeMemory(buffer);
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_close;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
|
|
|
CopyMemory(&priv->channelEvent, buffer, sizeof(HANDLE));
|
|
|
|
WTSFreeMemory(buffer);
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
priv->rdpsnd_pdu = Stream_New(NULL, 4096);
|
|
|
|
if (!priv->rdpsnd_pdu)
|
|
|
|
goto out_close;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
|
|
|
|
if (priv->ownThread)
|
|
|
|
{
|
|
|
|
context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
if (!context->priv->StopEvent)
|
|
|
|
goto out_pdu;
|
|
|
|
|
|
|
|
context->priv->Thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) rdpsnd_server_thread, (void*) context, 0, NULL);
|
|
|
|
if (!context->priv->Thread)
|
|
|
|
goto out_stopEvent;
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
return 0;
|
2014-04-29 02:59:41 +04:00
|
|
|
|
|
|
|
out_stopEvent:
|
|
|
|
CloseHandle(context->priv->StopEvent);
|
|
|
|
context->priv->StopEvent = NULL;
|
|
|
|
out_pdu:
|
|
|
|
Stream_Free(context->priv->rdpsnd_pdu, TRUE);
|
|
|
|
context->priv->rdpsnd_pdu = NULL;
|
|
|
|
out_close:
|
|
|
|
WTSVirtualChannelClose(context->priv->ChannelHandle);
|
|
|
|
context->priv->ChannelHandle = NULL;
|
|
|
|
return -1;
|
2013-08-19 05:52:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rdpsnd_server_stop(RdpsndServerContext* context)
|
|
|
|
{
|
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
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
WaitForSingleObject(context->priv->Thread, INFINITE);
|
|
|
|
CloseHandle(context->priv->Thread);
|
|
|
|
}
|
2014-04-29 02:59:41 +04:00
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2014-07-02 12:31:45 +04:00
|
|
|
RdpsndServerPrivate *priv;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
context = (RdpsndServerContext *)calloc(1, sizeof(RdpsndServerContext));
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!context)
|
|
|
|
return NULL;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
context->vcm = vcm;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
context->Start = rdpsnd_server_start;
|
|
|
|
context->Stop = rdpsnd_server_stop;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
context->selected_client_format = -1;
|
|
|
|
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;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
context->priv = priv = (RdpsndServerPrivate *)calloc(1, sizeof(RdpsndServerPrivate));
|
|
|
|
if (!priv)
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
priv->dsp_context = freerdp_dsp_context_new();
|
|
|
|
if (!priv->dsp_context)
|
2014-04-29 02:59:41 +04:00
|
|
|
goto out_free_priv;
|
2013-08-19 05:52:55 +04:00
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
priv->input_stream = Stream_New(NULL, 4);
|
|
|
|
if (!priv->input_stream)
|
|
|
|
goto out_free_dsp;
|
|
|
|
|
|
|
|
priv->expectedBytes = 4;
|
|
|
|
priv->waitingHeader = TRUE;
|
|
|
|
priv->ownThread = TRUE;
|
2013-08-19 05:52:55 +04:00
|
|
|
return context;
|
2014-04-29 02:59:41 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
|
|
|
|
void rdpsnd_server_context_reset(RdpsndServerContext *context)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!context->priv->StopEvent)
|
|
|
|
{
|
|
|
|
SetEvent(context->priv->StopEvent);
|
|
|
|
WaitForSingleObject(context->priv->Thread, INFINITE);
|
|
|
|
}
|
2013-08-19 05:52:55 +04:00
|
|
|
|
|
|
|
if (context->priv->ChannelHandle)
|
|
|
|
WTSVirtualChannelClose(context->priv->ChannelHandle);
|
|
|
|
|
|
|
|
if (context->priv->rdpsnd_pdu)
|
|
|
|
Stream_Free(context->priv->rdpsnd_pdu, TRUE);
|
|
|
|
|
|
|
|
if (context->priv->out_buffer)
|
|
|
|
free(context->priv->out_buffer);
|
|
|
|
|
|
|
|
if (context->priv->dsp_context)
|
|
|
|
freerdp_dsp_context_free(context->priv->dsp_context);
|
|
|
|
|
|
|
|
if (context->client_formats)
|
|
|
|
free(context->client_formats);
|
|
|
|
|
|
|
|
free(context);
|
|
|
|
}
|
2014-07-02 12:31:45 +04:00
|
|
|
|
|
|
|
HANDLE rdpsnd_server_get_event_handle(RdpsndServerContext *context)
|
|
|
|
{
|
|
|
|
return context->priv->channelEvent;
|
|
|
|
}
|
|
|
|
|
2014-09-11 15:45:23 +04:00
|
|
|
/*
|
|
|
|
* Handle rpdsnd messages - server side
|
|
|
|
*
|
|
|
|
* @param Server side context
|
|
|
|
*
|
|
|
|
* @return -1 if no data could be read,
|
|
|
|
* 0 on error (like connection close),
|
|
|
|
* 1 on succsess (also if further bytes need to be read)
|
|
|
|
*/
|
|
|
|
int rdpsnd_server_handle_messages(RdpsndServerContext *context)
|
2014-07-02 12:31:45 +04:00
|
|
|
{
|
|
|
|
DWORD bytesReturned;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
RdpsndServerPrivate *priv = context->priv;
|
|
|
|
wStream *s = priv->input_stream;
|
|
|
|
|
2014-07-14 16:00:38 +04: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)
|
2014-09-11 15:45:23 +04:00
|
|
|
return -1;
|
2014-07-02 12:31:45 +04:00
|
|
|
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: channel connection closed\n", __FUNCTION__);
|
2014-09-11 15:45:23 +04:00
|
|
|
return 0;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
|
|
|
priv->expectedBytes -= bytesReturned;
|
|
|
|
Stream_Seek(s, bytesReturned);
|
|
|
|
|
|
|
|
if (priv->expectedBytes)
|
2014-09-11 15:45:23 +04:00
|
|
|
return 1;
|
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);
|
|
|
|
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);
|
|
|
|
if (priv->expectedBytes)
|
|
|
|
{
|
|
|
|
Stream_EnsureCapacity(s, priv->expectedBytes);
|
2014-09-11 15:45:23 +04:00
|
|
|
return 1;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* when here we have the header + the body */
|
|
|
|
#ifdef WITH_DEBUG_SND
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: message type %d\n", __FUNCTION__, 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
|
|
|
|
|
|
|
if (ret && context->clientVersion < 6)
|
|
|
|
IFCALL(context->Activated, context);
|
|
|
|
|
2014-07-02 12:31:45 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SNDC_QUALITYMODE:
|
|
|
|
ret = rdpsnd_server_recv_quality_mode(context, s);
|
|
|
|
Stream_SetPosition(s, 0); /* in case the Activated callback tries to treat some messages */
|
|
|
|
|
2014-09-11 13:33:10 +04:00
|
|
|
if (ret && context->clientVersion >= 6)
|
2014-07-02 12:31:45 +04:00
|
|
|
IFCALL(context->Activated, context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-08-11 11:12:01 +04:00
|
|
|
CLOG_ERR( "%s: UNKOWN MESSAGE TYPE!! (%#0X)\n\n", __FUNCTION__, priv->msgType);
|
2014-07-02 12:31:45 +04:00
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
|
2014-09-11 15:45:23 +04:00
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2014-07-02 12:31:45 +04:00
|
|
|
}
|