2012-05-24 10:03:21 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-05-24 10:03:21 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:20:53 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-10-11 04:44:27 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-07 22:34:12 +04:00
|
|
|
#include <winpr/print.h>
|
2013-03-08 00:33:27 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2013-03-29 22:49:09 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-10-11 04:44:27 +04:00
|
|
|
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2012-05-24 10:03:21 +04:00
|
|
|
#include <freerdp/channels/wtsvc.h>
|
|
|
|
#include <freerdp/server/rdpsnd.h>
|
|
|
|
|
|
|
|
typedef struct _rdpsnd_server
|
|
|
|
{
|
|
|
|
rdpsnd_server_context context;
|
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
HANDLE thread;
|
|
|
|
HANDLE StopEvent;
|
2012-05-24 10:03:21 +04:00
|
|
|
void* rdpsnd_channel;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* rdpsnd_pdu;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
FREERDP_DSP_CONTEXT* dsp_context;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* out_buffer;
|
2012-05-24 10:03:21 +04:00
|
|
|
int out_buffer_size;
|
|
|
|
int out_frames;
|
|
|
|
int out_pending_frames;
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 src_bytes_per_sample;
|
|
|
|
UINT32 src_bytes_per_frame;
|
2012-05-24 10:03:21 +04:00
|
|
|
} rdpsnd_server;
|
|
|
|
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static BOOL rdpsnd_server_send_formats(rdpsnd_server* rdpsnd, wStream* s)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
2013-03-08 01:04:25 +04:00
|
|
|
int pos;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 i;
|
2013-03-08 01:04:25 +04:00
|
|
|
BOOL status;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_FORMATS);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek_UINT16(s);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:09:16 +04: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 */
|
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.num_server_formats); /* wNumberOfFormats */
|
|
|
|
Stream_Write_UINT8(s, rdpsnd->context.block_no); /* cLastBlockConfirmed */
|
|
|
|
Stream_Write_UINT16(s, 0x06); /* wVersion */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
2013-03-07 22:34:12 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
for (i = 0; i < rdpsnd->context.num_server_formats; i++)
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.server_formats[i].wFormatTag); /* wFormatTag (WAVE_FORMAT_PCM) */
|
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.server_formats[i].nChannels); /* nChannels */
|
|
|
|
Stream_Write_UINT32(s, rdpsnd->context.server_formats[i].nSamplesPerSec); /* nSamplesPerSec */
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(s, rdpsnd->context.server_formats[i].nSamplesPerSec *
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd->context.server_formats[i].nChannels *
|
|
|
|
rdpsnd->context.server_formats[i].wBitsPerSample / 8); /* nAvgBytesPerSec */
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.server_formats[i].nBlockAlign); /* nBlockAlign */
|
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.server_formats[i].wBitsPerSample); /* wBitsPerSample */
|
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.server_formats[i].cbSize); /* cbSize */
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->context.server_formats[i].cbSize > 0)
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(s, rdpsnd->context.server_formats[i].data, rdpsnd->context.server_formats[i].cbSize);
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, pos);
|
2013-05-09 00:27:21 +04:00
|
|
|
status = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, Stream_Buffer(s), Stream_GetPosition(s), NULL);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2012-11-22 05:21:08 +04:00
|
|
|
|
2013-03-08 01:04:25 +04:00
|
|
|
return status;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static void rdpsnd_server_recv_waveconfirm(rdpsnd_server* rdpsnd, wStream* s)
|
2013-03-08 21:36:38 +04:00
|
|
|
{
|
|
|
|
//unhandled for now
|
|
|
|
|
|
|
|
UINT16 timestamp = 0;
|
|
|
|
BYTE confirmBlockNum = 0;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, timestamp);
|
|
|
|
Stream_Read_UINT8(s, confirmBlockNum);
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Seek_UINT8(s); // padding
|
2013-03-08 21:36:38 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static void rdpsnd_server_recv_quality_mode(rdpsnd_server* rdpsnd, wStream* s)
|
2013-03-07 22:34:12 +04:00
|
|
|
{
|
2013-03-11 22:15:48 +04:00
|
|
|
//unhandled for now
|
2013-03-07 22:34:12 +04:00
|
|
|
UINT16 quality;
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, quality);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek_UINT16(s); // reserved
|
2013-03-07 22:34:12 +04:00
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client requested sound quality: %#0X\n", quality);
|
2013-03-07 22:34:12 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static BOOL rdpsnd_server_recv_formats(rdpsnd_server* rdpsnd, wStream* s)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
2013-03-07 23:21:50 +04:00
|
|
|
int i, num_known_format = 0;
|
2013-03-07 22:34:12 +04:00
|
|
|
UINT32 flags, vol, pitch;
|
|
|
|
UINT16 udpPort, version;
|
|
|
|
BYTE lastblock;
|
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:09:16 +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, rdpsnd->context.num_client_formats); /* wNumberOfFormats */
|
|
|
|
Stream_Read_UINT8(s, lastblock); /* cLastBlockConfirmed */
|
|
|
|
Stream_Read_UINT16(s, version); /* wVersion */
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Seek_UINT8(s); /* bPad */
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
if (rdpsnd->context.num_client_formats > 0)
|
|
|
|
{
|
2013-02-26 06:46:48 +04:00
|
|
|
rdpsnd->context.client_formats = (AUDIO_FORMAT*) malloc(rdpsnd->context.num_client_formats * sizeof(AUDIO_FORMAT));
|
|
|
|
ZeroMemory(rdpsnd->context.client_formats, sizeof(AUDIO_FORMAT));
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
for (i = 0; i < rdpsnd->context.num_client_formats; i++)
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, rdpsnd->context.client_formats[i].wFormatTag);
|
|
|
|
Stream_Read_UINT16(s, rdpsnd->context.client_formats[i].nChannels);
|
|
|
|
Stream_Read_UINT32(s, rdpsnd->context.client_formats[i].nSamplesPerSec);
|
|
|
|
Stream_Read_UINT32(s, rdpsnd->context.client_formats[i].nAvgBytesPerSec);
|
|
|
|
Stream_Read_UINT16(s, rdpsnd->context.client_formats[i].nBlockAlign);
|
|
|
|
Stream_Read_UINT16(s, rdpsnd->context.client_formats[i].wBitsPerSample);
|
|
|
|
Stream_Read_UINT16(s, rdpsnd->context.client_formats[i].cbSize);
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->context.client_formats[i].cbSize > 0)
|
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, rdpsnd->context.client_formats[i].cbSize);
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
2013-03-07 22:34:12 +04:00
|
|
|
|
|
|
|
if (rdpsnd->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++;
|
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
}
|
2013-03-07 22:34:12 +04:00
|
|
|
|
|
|
|
if (num_known_format == 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Client doesnt support any known formats!\n");
|
2013-03-07 22:34:12 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* rdpsnd_server_thread_func(void* arg)
|
|
|
|
{
|
|
|
|
void* fd;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s;
|
2012-05-24 10:03:21 +04:00
|
|
|
void* buffer;
|
2013-03-08 00:33:27 +04:00
|
|
|
DWORD status;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE msgType;
|
|
|
|
UINT16 BodySize;
|
2013-03-08 00:33:27 +04:00
|
|
|
HANDLE events[2];
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 bytes_returned = 0;
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) arg;
|
2013-03-08 00:33:27 +04:00
|
|
|
|
|
|
|
events[0] = rdpsnd->StopEvent;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
if (WTSVirtualChannelQuery(rdpsnd->rdpsnd_channel, WTSVirtualFileHandle, &buffer, &bytes_returned) == TRUE)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
2012-11-27 05:15:48 +04:00
|
|
|
fd = *((void**) buffer);
|
2012-05-24 10:03:21 +04:00
|
|
|
WTSFreeMemory(buffer);
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
events[1] = CreateWaitObjectEvent(NULL, TRUE, FALSE, fd);
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
s = Stream_New(NULL, 4096);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
rdpsnd_server_send_formats(rdpsnd, s);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2013-03-08 00:33:27 +04:00
|
|
|
status = WaitForMultipleObjects(2, events, FALSE, INFINITE);
|
|
|
|
|
|
|
|
if (WaitForSingleObject(rdpsnd->StopEvent, 0) == WAIT_OBJECT_0)
|
|
|
|
{
|
2012-05-24 10:03:21 +04:00
|
|
|
break;
|
2013-03-08 00:33:27 +04:00
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
if (WTSVirtualChannelRead(rdpsnd->rdpsnd_channel, 0, Stream_Buffer(s),
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Capacity(s), &bytes_returned) == FALSE)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
if (bytes_returned == 0)
|
|
|
|
break;
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2013-05-02 02:15:55 +04:00
|
|
|
Stream_EnsureRemainingCapacity(s, (int) bytes_returned);
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
if (WTSVirtualChannelRead(rdpsnd->rdpsnd_channel, 0, Stream_Buffer(s),
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Capacity(s), &bytes_returned) == FALSE)
|
2012-05-24 10:03:21 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-03-07 22:34:12 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, msgType);
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Seek_UINT8(s); /* bPad */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, BodySize);
|
2012-08-15 02:39:07 +04:00
|
|
|
|
|
|
|
switch (msgType)
|
|
|
|
{
|
2013-03-08 21:36:38 +04:00
|
|
|
case SNDC_WAVECONFIRM:
|
|
|
|
rdpsnd_server_recv_waveconfirm(rdpsnd, s);
|
|
|
|
break;
|
|
|
|
|
2013-03-07 22:34:12 +04:00
|
|
|
case SNDC_QUALITYMODE:
|
|
|
|
rdpsnd_server_recv_quality_mode(rdpsnd, s);
|
|
|
|
break;
|
2012-05-24 10:03:21 +04:00
|
|
|
case SNDC_FORMATS:
|
|
|
|
if (rdpsnd_server_recv_formats(rdpsnd, s))
|
|
|
|
{
|
|
|
|
IFCALL(rdpsnd->context.Activated, &rdpsnd->context);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "UNKOWN MESSAGE TYPE!! (%#0X)\n\n", msgType);
|
2012-05-24 10:03:21 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-07 23:21:50 +04:00
|
|
|
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(s, TRUE);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
return NULL;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_server_initialize(rdpsnd_server_context* context)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
|
|
|
|
|
|
|
rdpsnd->rdpsnd_channel = WTSVirtualChannelOpenEx(context->vcm, "rdpsnd", 0);
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->rdpsnd_channel != NULL)
|
|
|
|
{
|
2013-05-09 01:48:30 +04:00
|
|
|
rdpsnd->rdpsnd_pdu = Stream_New(NULL, 4096);
|
2013-03-08 00:33:27 +04:00
|
|
|
|
|
|
|
rdpsnd->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
|
|
|
rdpsnd->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) rdpsnd_server_thread_func, (void*) rdpsnd, 0, NULL);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_server_select_format(rdpsnd_server_context* context, int client_format_index)
|
|
|
|
{
|
|
|
|
int bs;
|
|
|
|
int out_buffer_size;
|
2013-02-26 06:46:48 +04:00
|
|
|
AUDIO_FORMAT *format;
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
|
|
|
|
|
|
|
if (client_format_index < 0 || client_format_index >= context->num_client_formats)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rdpsnd_server_select_format: index %d is not correct.\n", client_format_index);
|
2012-05-24 10:03:21 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-03-06 02:31:31 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd->src_bytes_per_sample = context->src_format.wBitsPerSample / 8;
|
|
|
|
rdpsnd->src_bytes_per_frame = rdpsnd->src_bytes_per_sample * context->src_format.nChannels;
|
|
|
|
|
|
|
|
context->selected_client_format = client_format_index;
|
|
|
|
format = &context->client_formats[client_format_index];
|
2013-03-06 02:31:31 +04:00
|
|
|
|
|
|
|
if (format->nSamplesPerSec == 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Invalid Client Sound Format!!\n\n");
|
2013-03-06 02:31:31 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
if (format->wFormatTag == WAVE_FORMAT_DVI_ADPCM)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
bs = (format->nBlockAlign - 4 * format->nChannels) * 4;
|
|
|
|
rdpsnd->out_frames = (format->nBlockAlign * 4 * format->nChannels * 2 / bs + 1) * bs / (format->nChannels * 2);
|
|
|
|
}
|
2013-03-08 00:33:27 +04:00
|
|
|
else if (format->wFormatTag == WAVE_FORMAT_ADPCM)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
|
|
|
bs = (format->nBlockAlign - 7 * format->nChannels) * 2 / format->nChannels + 2;
|
|
|
|
rdpsnd->out_frames = bs * 4;
|
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rdpsnd->out_frames = 0x4000 / rdpsnd->src_bytes_per_frame;
|
|
|
|
}
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (format->nSamplesPerSec != context->src_format.nSamplesPerSec)
|
|
|
|
{
|
|
|
|
rdpsnd->out_frames = (rdpsnd->out_frames * context->src_format.nSamplesPerSec + format->nSamplesPerSec - 100) / format->nSamplesPerSec;
|
|
|
|
}
|
|
|
|
rdpsnd->out_pending_frames = 0;
|
|
|
|
|
|
|
|
out_buffer_size = rdpsnd->out_frames * rdpsnd->src_bytes_per_frame;
|
2012-08-15 02:39:07 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->out_buffer_size < out_buffer_size)
|
|
|
|
{
|
2012-10-11 04:44:27 +04:00
|
|
|
rdpsnd->out_buffer = (BYTE*) realloc(rdpsnd->out_buffer, out_buffer_size);
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd->out_buffer_size = out_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_dsp_context_reset_adpcm(rdpsnd->dsp_context);
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_server_send_audio_pdu(rdpsnd_server* rdpsnd)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
int size;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* src;
|
2012-05-24 10:03:21 +04:00
|
|
|
int frames;
|
|
|
|
int fill_size;
|
2013-03-08 00:33:27 +04:00
|
|
|
BOOL status;
|
2013-02-26 06:46:48 +04:00
|
|
|
AUDIO_FORMAT* format;
|
2012-10-09 00:29:40 +04:00
|
|
|
int tbytes_per_frame;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s = rdpsnd->rdpsnd_pdu;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
format = &rdpsnd->context.client_formats[rdpsnd->context.selected_client_format];
|
|
|
|
tbytes_per_frame = format->nChannels * rdpsnd->src_bytes_per_sample;
|
|
|
|
|
2012-10-09 00:29:40 +04:00
|
|
|
if ((format->nSamplesPerSec == rdpsnd->context.src_format.nSamplesPerSec) &&
|
|
|
|
(format->nChannels == rdpsnd->context.src_format.nChannels))
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
src = rdpsnd->out_buffer;
|
|
|
|
frames = rdpsnd->out_pending_frames;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rdpsnd->dsp_context->resample(rdpsnd->dsp_context, rdpsnd->out_buffer, rdpsnd->src_bytes_per_sample,
|
|
|
|
rdpsnd->context.src_format.nChannels, rdpsnd->context.src_format.nSamplesPerSec, rdpsnd->out_pending_frames,
|
|
|
|
format->nChannels, format->nSamplesPerSec);
|
|
|
|
frames = rdpsnd->dsp_context->resampled_frames;
|
|
|
|
src = rdpsnd->dsp_context->resampled_buffer;
|
|
|
|
}
|
|
|
|
size = frames * tbytes_per_frame;
|
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
if (format->wFormatTag == WAVE_FORMAT_DVI_ADPCM)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
rdpsnd->dsp_context->encode_ima_adpcm(rdpsnd->dsp_context,
|
|
|
|
src, size, format->nChannels, format->nBlockAlign);
|
|
|
|
src = rdpsnd->dsp_context->adpcm_buffer;
|
|
|
|
size = rdpsnd->dsp_context->adpcm_size;
|
|
|
|
}
|
2013-03-08 00:33:27 +04:00
|
|
|
else if (format->wFormatTag == WAVE_FORMAT_ADPCM)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
|
|
|
rdpsnd->dsp_context->encode_ms_adpcm(rdpsnd->dsp_context,
|
|
|
|
src, size, format->nChannels, format->nBlockAlign);
|
|
|
|
src = rdpsnd->dsp_context->adpcm_buffer;
|
|
|
|
size = rdpsnd->dsp_context->adpcm_size;
|
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
rdpsnd->context.block_no = (rdpsnd->context.block_no + 1) % 256;
|
|
|
|
|
|
|
|
/* Fill to nBlockAlign for the last audio packet */
|
2013-03-08 00:33:27 +04:00
|
|
|
|
|
|
|
fill_size = 0;
|
|
|
|
|
|
|
|
if ((format->wFormatTag == WAVE_FORMAT_DVI_ADPCM || format->wFormatTag == WAVE_FORMAT_ADPCM) &&
|
|
|
|
(rdpsnd->out_pending_frames < rdpsnd->out_frames) && ((size % format->nBlockAlign) != 0))
|
|
|
|
{
|
2012-05-24 10:03:21 +04:00
|
|
|
fill_size = format->nBlockAlign - (size % format->nBlockAlign);
|
2013-03-08 00:33:27 +04:00
|
|
|
}
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
/* WaveInfo PDU */
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_WAVE); /* msgType */
|
|
|
|
Stream_Write_UINT8(s, 0); /* bPad */
|
|
|
|
Stream_Write_UINT16(s, size + fill_size + 8); /* BodySize */
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, 0); /* wTimeStamp */
|
|
|
|
Stream_Write_UINT16(s, rdpsnd->context.selected_client_format); /* wFormatNo */
|
|
|
|
Stream_Write_UINT8(s, rdpsnd->context.block_no); /* cBlockNo */
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 3); /* bPad */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(s, src, 4);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, Stream_Buffer(s), Stream_GetPosition(s), NULL);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
/* Wave PDU */
|
2013-05-02 02:15:55 +04:00
|
|
|
Stream_EnsureRemainingCapacity(s, size + fill_size);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(s, 0); /* bPad */
|
|
|
|
Stream_Write(s, src + 4, size - 4);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (fill_size > 0)
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Zero(s, fill_size);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
status = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, Stream_Buffer(s), Stream_GetPosition(s), NULL);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
rdpsnd->out_pending_frames = 0;
|
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
return status;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_server_send_samples(rdpsnd_server_context* context, const void* buf, int nframes)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
|
|
|
int cframes;
|
|
|
|
int cframesize;
|
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
|
|
|
|
|
|
|
if (rdpsnd->context.selected_client_format < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
while (nframes > 0)
|
|
|
|
{
|
|
|
|
cframes = MIN(nframes, rdpsnd->out_frames - rdpsnd->out_pending_frames);
|
|
|
|
cframesize = cframes * rdpsnd->src_bytes_per_frame;
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
CopyMemory(rdpsnd->out_buffer + (rdpsnd->out_pending_frames * rdpsnd->src_bytes_per_frame), buf, cframesize);
|
2012-10-09 11:01:37 +04:00
|
|
|
buf = (BYTE*) buf + cframesize;
|
2012-05-24 10:03:21 +04:00
|
|
|
nframes -= cframes;
|
|
|
|
rdpsnd->out_pending_frames += cframes;
|
|
|
|
|
|
|
|
if (rdpsnd->out_pending_frames >= rdpsnd->out_frames)
|
|
|
|
{
|
|
|
|
if (!rdpsnd_server_send_audio_pdu(rdpsnd))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_server_set_volume(rdpsnd_server_context* context, int left, int right)
|
2012-05-29 18:22:38 +04:00
|
|
|
{
|
2013-03-08 01:04:25 +04:00
|
|
|
int pos;
|
|
|
|
BOOL status;
|
2012-05-29 18:22:38 +04:00
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s = rdpsnd->rdpsnd_pdu;
|
2012-05-29 18:22:38 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_SETVOLUME);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek_UINT16(s);
|
2012-05-29 18:22:38 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, left);
|
|
|
|
Stream_Write_UINT16(s, right);
|
2013-03-08 01:04:25 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, pos);
|
2013-05-09 00:27:21 +04:00
|
|
|
status = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, Stream_Buffer(s), Stream_GetPosition(s), NULL);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2013-03-08 01:04:25 +04:00
|
|
|
|
|
|
|
return status;
|
2012-05-29 18:22:38 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL rdpsnd_server_close(rdpsnd_server_context* context)
|
2012-05-24 10:03:21 +04:00
|
|
|
{
|
2013-03-08 01:04:25 +04:00
|
|
|
int pos;
|
|
|
|
BOOL status;
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s = rdpsnd->rdpsnd_pdu;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
if (rdpsnd->context.selected_client_format < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-24 10:03:21 +04:00
|
|
|
|
|
|
|
if (rdpsnd->out_pending_frames > 0)
|
|
|
|
{
|
|
|
|
if (!rdpsnd_server_send_audio_pdu(rdpsnd))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rdpsnd->context.selected_client_format = -1;
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, SNDC_CLOSE);
|
|
|
|
Stream_Write_UINT8(s, 0);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek_UINT16(s);
|
2013-03-08 01:04:25 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
pos = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, pos - 4);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, pos);
|
2013-05-09 00:27:21 +04:00
|
|
|
status = WTSVirtualChannelWrite(rdpsnd->rdpsnd_channel, Stream_Buffer(s), Stream_GetPosition(s), NULL);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2013-03-08 01:04:25 +04:00
|
|
|
|
|
|
|
return status;
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rdpsnd_server_context* rdpsnd_server_context_new(WTSVirtualChannelManager* vcm)
|
|
|
|
{
|
|
|
|
rdpsnd_server* rdpsnd;
|
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
rdpsnd = (rdpsnd_server*) malloc(sizeof(rdpsnd_server));
|
|
|
|
ZeroMemory(rdpsnd, sizeof(rdpsnd_server));
|
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd->context.vcm = vcm;
|
|
|
|
rdpsnd->context.selected_client_format = -1;
|
|
|
|
rdpsnd->context.Initialize = rdpsnd_server_initialize;
|
|
|
|
rdpsnd->context.SelectFormat = rdpsnd_server_select_format;
|
|
|
|
rdpsnd->context.SendSamples = rdpsnd_server_send_samples;
|
2012-05-29 18:22:38 +04:00
|
|
|
rdpsnd->context.SetVolume = rdpsnd_server_set_volume;
|
2012-05-24 10:03:21 +04:00
|
|
|
rdpsnd->context.Close = rdpsnd_server_close;
|
|
|
|
|
|
|
|
rdpsnd->dsp_context = freerdp_dsp_context_new();
|
|
|
|
|
|
|
|
return (rdpsnd_server_context*) rdpsnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdpsnd_server_context_free(rdpsnd_server_context* context)
|
|
|
|
{
|
|
|
|
rdpsnd_server* rdpsnd = (rdpsnd_server*) context;
|
|
|
|
|
2013-03-08 00:33:27 +04:00
|
|
|
SetEvent(rdpsnd->StopEvent);
|
|
|
|
WaitForSingleObject(rdpsnd->thread, INFINITE);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->rdpsnd_channel)
|
|
|
|
WTSVirtualChannelClose(rdpsnd->rdpsnd_channel);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->rdpsnd_pdu)
|
2013-05-09 01:48:30 +04:00
|
|
|
Stream_Free(rdpsnd->rdpsnd_pdu, TRUE);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->out_buffer)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(rdpsnd->out_buffer);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->dsp_context)
|
|
|
|
freerdp_dsp_context_free(rdpsnd->dsp_context);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-05-24 10:03:21 +04:00
|
|
|
if (rdpsnd->context.client_formats)
|
2012-10-09 07:21:26 +04:00
|
|
|
free(rdpsnd->context.client_formats);
|
2012-10-09 00:29:40 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(rdpsnd);
|
2012-05-24 10:03:21 +04:00
|
|
|
}
|