859 lines
21 KiB
C
859 lines
21 KiB
C
/**
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
* Audio Output Virtual Channel
|
|
*
|
|
* Copyright 2009-2011 Jay Sorg
|
|
* Copyright 2010-2011 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
|
|
|
|
#ifndef _WIN32
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include <winpr/crt.h>
|
|
#include <winpr/synch.h>
|
|
#include <winpr/cmdline.h>
|
|
#include <winpr/sysinfo.h>
|
|
#include <winpr/collections.h>
|
|
|
|
#include <freerdp/types.h>
|
|
#include <freerdp/addin.h>
|
|
#include <freerdp/constants.h>
|
|
#include <freerdp/utils/stream.h>
|
|
#include <freerdp/utils/signal.h>
|
|
#include <freerdp/utils/svc_plugin.h>
|
|
|
|
#include "rdpsnd_main.h"
|
|
|
|
#define TIME_DELAY_MS 65
|
|
|
|
struct rdpsnd_plugin
|
|
{
|
|
rdpSvcPlugin plugin;
|
|
|
|
wMessagePipe* MsgPipe;
|
|
HANDLE thread;
|
|
|
|
BYTE cBlockNo;
|
|
int wCurrentFormatNo;
|
|
|
|
AUDIO_FORMAT* ServerFormats;
|
|
UINT16 NumberOfServerFormats;
|
|
|
|
AUDIO_FORMAT* ClientFormats;
|
|
UINT16 NumberOfClientFormats;
|
|
|
|
BOOL expectingWave;
|
|
BYTE waveData[4];
|
|
UINT16 waveDataSize;
|
|
UINT32 wTimeStamp;
|
|
UINT32 wLastTimeStamp;
|
|
UINT32 wave_timestamp;
|
|
|
|
BOOL is_open;
|
|
UINT32 close_timestamp;
|
|
|
|
UINT16 fixed_format;
|
|
UINT16 fixed_channel;
|
|
UINT32 fixed_rate;
|
|
int latency;
|
|
|
|
char* subsystem;
|
|
char* device_name;
|
|
|
|
/* Device plugin */
|
|
rdpsndDevicePlugin* device;
|
|
};
|
|
|
|
static void* rdpsnd_schedule_thread(void* arg)
|
|
{
|
|
STREAM* data;
|
|
wMessage message;
|
|
UINT16 wTimeDiff;
|
|
UINT16 wTimeStamp;
|
|
UINT16 wCurrentTime;
|
|
rdpsndPlugin* rdpsnd = (rdpsndPlugin*) arg;
|
|
|
|
while (1)
|
|
{
|
|
if (!MessageQueue_Wait(rdpsnd->MsgPipe->Out))
|
|
break;
|
|
|
|
if (!MessageQueue_Peek(rdpsnd->MsgPipe->Out, &message, TRUE))
|
|
break;
|
|
|
|
if (message.id == WMQ_QUIT)
|
|
break;
|
|
|
|
wTimeStamp = (UINT16) (size_t) message.lParam;
|
|
wCurrentTime = (UINT16) GetTickCount();
|
|
|
|
//printf("wTimeStamp: %d wCurrentTime: %d\n", wTimeStamp, wCurrentTime);
|
|
|
|
if (wCurrentTime <= wTimeStamp)
|
|
{
|
|
wTimeDiff = wTimeStamp - wCurrentTime;
|
|
//printf("Sleeping %d ms\n", wTimeDiff);
|
|
Sleep(wTimeDiff);
|
|
}
|
|
|
|
data = (STREAM*) message.wParam;
|
|
svc_plugin_send((rdpSvcPlugin*) rdpsnd, data);
|
|
DEBUG_SVC("processed output data");
|
|
}
|
|
|
|
#if 0
|
|
if (rdpsnd->is_open && (rdpsnd->close_timestamp > 0))
|
|
{
|
|
if (GetTickCount() > rdpsnd->close_timestamp)
|
|
{
|
|
if (rdpsnd->device)
|
|
IFCALL(rdpsnd->device->Close, rdpsnd->device);
|
|
|
|
rdpsnd->is_open = FALSE;
|
|
rdpsnd->close_timestamp = 0;
|
|
|
|
DEBUG_SVC("processed close");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd)
|
|
{
|
|
STREAM* pdu;
|
|
|
|
pdu = stream_new(8);
|
|
stream_write_BYTE(pdu, SNDC_QUALITYMODE); /* msgType */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
stream_write_UINT16(pdu, 4); /* BodySize */
|
|
stream_write_UINT16(pdu, HIGH_QUALITY); /* wQualityMode */
|
|
stream_write_UINT16(pdu, 0); /* Reserved */
|
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
|
|
}
|
|
|
|
void rdpsnd_free_audio_formats(AUDIO_FORMAT* formats, UINT16 count)
|
|
{
|
|
int index;
|
|
AUDIO_FORMAT* format;
|
|
|
|
if (formats)
|
|
{
|
|
for (index = 0; index < (int) count; index++)
|
|
{
|
|
format = &formats[index];
|
|
|
|
if (format->cbSize)
|
|
free(format->data);
|
|
}
|
|
|
|
free(formats);
|
|
}
|
|
}
|
|
|
|
void rdpsnd_adapt_audio_formats_new_to_old(rdpsndFormat* oldFormat, AUDIO_FORMAT* newFormat)
|
|
{
|
|
oldFormat->wFormatTag = newFormat->wFormatTag;
|
|
oldFormat->nChannels = newFormat->nChannels;
|
|
oldFormat->nSamplesPerSec = newFormat->nSamplesPerSec;
|
|
oldFormat->nBlockAlign = newFormat->nBlockAlign;
|
|
oldFormat->wBitsPerSample = newFormat->wBitsPerSample;
|
|
oldFormat->cbSize = newFormat->cbSize;
|
|
oldFormat->data = newFormat->data;
|
|
}
|
|
|
|
char* rdpsnd_get_audio_tag_string(UINT16 wFormatTag)
|
|
{
|
|
switch (wFormatTag)
|
|
{
|
|
case WAVE_FORMAT_PCM:
|
|
return "WAVE_FORMAT_PCM";
|
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
return "WAVE_FORMAT_ADPCM";
|
|
|
|
case WAVE_FORMAT_ALAW:
|
|
return "WAVE_FORMAT_ALAW";
|
|
|
|
case WAVE_FORMAT_MULAW:
|
|
return "WAVE_FORMAT_MULAW";
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
|
return "WAVE_FORMAT_DVI_ADPCM";
|
|
}
|
|
|
|
return "WAVE_FORMAT_UNKNOWN";
|
|
}
|
|
|
|
void rdpsnd_print_audio_format(AUDIO_FORMAT* format)
|
|
{
|
|
printf("%s: wFormatTag: 0x%04X nChannels: %d nSamplesPerSec: %d nAvgBytesPerSec: %d nBlockAlign: %d wBitsPerSample: %d\n",
|
|
rdpsnd_get_audio_tag_string(format->wFormatTag), format->wFormatTag,
|
|
format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
|
|
format->nBlockAlign, format->wBitsPerSample);
|
|
}
|
|
|
|
UINT32 rdpsnd_compute_audio_time_length(AUDIO_FORMAT* format, int size)
|
|
{
|
|
UINT32 mstime;
|
|
UINT32 wSamples;
|
|
|
|
/**
|
|
* [MSDN-AUDIOFORMAT]:
|
|
* http://msdn.microsoft.com/en-us/library/ms713497.aspx
|
|
*/
|
|
|
|
wSamples = (size * 8) / format->wBitsPerSample;
|
|
mstime = ((wSamples * 1000) / format->nSamplesPerSec);
|
|
|
|
return mstime;
|
|
}
|
|
|
|
void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd)
|
|
{
|
|
int index;
|
|
rdpsndFormat sndFormat;
|
|
AUDIO_FORMAT* serverFormat;
|
|
AUDIO_FORMAT* clientFormat;
|
|
|
|
rdpsnd_free_audio_formats(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
|
|
rdpsnd->NumberOfClientFormats = 0;
|
|
rdpsnd->ClientFormats = NULL;
|
|
|
|
rdpsnd->ClientFormats = (AUDIO_FORMAT*) malloc(sizeof(AUDIO_FORMAT) * rdpsnd->NumberOfServerFormats);
|
|
|
|
for (index = 0; index < (int) rdpsnd->NumberOfServerFormats; index++)
|
|
{
|
|
serverFormat = &rdpsnd->ServerFormats[index];
|
|
|
|
if (rdpsnd->fixed_format > 0 && (rdpsnd->fixed_format != serverFormat->wFormatTag))
|
|
continue;
|
|
|
|
if (rdpsnd->fixed_channel > 0 && (rdpsnd->fixed_channel != serverFormat->nChannels))
|
|
continue;
|
|
|
|
if (rdpsnd->fixed_rate > 0 && (rdpsnd->fixed_rate != serverFormat->nSamplesPerSec))
|
|
continue;
|
|
|
|
/**
|
|
* FIXME: temporary adapter to avoid breaking code depending on rdpsndFormat definition
|
|
*/
|
|
|
|
rdpsnd_adapt_audio_formats_new_to_old(&sndFormat, serverFormat);
|
|
|
|
if (rdpsnd->device && rdpsnd->device->FormatSupported(rdpsnd->device, &sndFormat))
|
|
{
|
|
clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++];
|
|
|
|
CopyMemory(clientFormat, serverFormat, sizeof(AUDIO_FORMAT));
|
|
clientFormat->cbSize = 0;
|
|
|
|
if (serverFormat->cbSize > 0)
|
|
{
|
|
clientFormat->data = (BYTE*) malloc(serverFormat->cbSize);
|
|
CopyMemory(clientFormat->data, serverFormat->data, serverFormat->cbSize);
|
|
clientFormat->cbSize = serverFormat->cbSize;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd)
|
|
{
|
|
int index;
|
|
STREAM* pdu;
|
|
UINT16 length;
|
|
UINT32 dwVolume;
|
|
UINT16 dwVolumeLeft;
|
|
UINT16 dwVolumeRight;
|
|
UINT16 wNumberOfFormats;
|
|
AUDIO_FORMAT* clientFormat;
|
|
|
|
dwVolumeLeft = (0xFFFF / 2); /* 50% ? */
|
|
dwVolumeRight = (0xFFFF / 2); /* 50% ? */
|
|
dwVolume = (dwVolumeLeft << 16) | dwVolumeRight;
|
|
|
|
wNumberOfFormats = rdpsnd->NumberOfClientFormats;
|
|
|
|
length = 4 + 20;
|
|
|
|
for (index = 0; index < (int) wNumberOfFormats; index++)
|
|
length += (18 + rdpsnd->ClientFormats[index].cbSize);
|
|
|
|
pdu = stream_new(length);
|
|
|
|
stream_write_BYTE(pdu, SNDC_FORMATS); /* msgType */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
stream_write_UINT16(pdu, length - 4); /* BodySize */
|
|
|
|
stream_write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME); /* dwFlags */
|
|
stream_write_UINT32(pdu, dwVolume); /* dwVolume */
|
|
stream_write_UINT32(pdu, 0); /* dwPitch */
|
|
stream_write_UINT16(pdu, 0); /* wDGramPort */
|
|
stream_write_UINT16(pdu, wNumberOfFormats); /* wNumberOfFormats */
|
|
stream_write_BYTE(pdu, 0); /* cLastBlockConfirmed */
|
|
stream_write_UINT16(pdu, 6); /* wVersion */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
|
|
for (index = 0; index < (int) wNumberOfFormats; index++)
|
|
{
|
|
clientFormat = &rdpsnd->ClientFormats[index];
|
|
|
|
stream_write_UINT16(pdu, clientFormat->wFormatTag);
|
|
stream_write_UINT16(pdu, clientFormat->nChannels);
|
|
stream_write_UINT32(pdu, clientFormat->nSamplesPerSec);
|
|
stream_write_UINT32(pdu, clientFormat->nAvgBytesPerSec);
|
|
stream_write_UINT16(pdu, clientFormat->nBlockAlign);
|
|
stream_write_UINT16(pdu, clientFormat->wBitsPerSample);
|
|
stream_write_UINT16(pdu, clientFormat->cbSize);
|
|
|
|
if (clientFormat->cbSize > 0)
|
|
stream_write(pdu, clientFormat->data, clientFormat->cbSize);
|
|
}
|
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
|
|
}
|
|
|
|
void rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd, STREAM* s)
|
|
{
|
|
int index;
|
|
UINT16 wVersion;
|
|
AUDIO_FORMAT* format;
|
|
UINT16 wNumberOfFormats;
|
|
|
|
rdpsnd_free_audio_formats(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
|
|
rdpsnd->NumberOfServerFormats = 0;
|
|
rdpsnd->ServerFormats = NULL;
|
|
|
|
stream_seek_UINT32(s); /* dwFlags */
|
|
stream_seek_UINT32(s); /* dwVolume */
|
|
stream_seek_UINT32(s); /* dwPitch */
|
|
stream_seek_UINT16(s); /* wDGramPort */
|
|
stream_read_UINT16(s, wNumberOfFormats);
|
|
stream_read_BYTE(s, rdpsnd->cBlockNo); /* cLastBlockConfirmed */
|
|
stream_read_UINT16(s, wVersion); /* wVersion */
|
|
stream_seek_BYTE(s); /* bPad */
|
|
|
|
rdpsnd->NumberOfServerFormats = wNumberOfFormats;
|
|
rdpsnd->ServerFormats = (AUDIO_FORMAT*) malloc(sizeof(AUDIO_FORMAT) * wNumberOfFormats);
|
|
|
|
for (index = 0; index < (int) wNumberOfFormats; index++)
|
|
{
|
|
format = &rdpsnd->ServerFormats[index];
|
|
|
|
stream_read_UINT16(s, format->wFormatTag); /* wFormatTag */
|
|
stream_read_UINT16(s, format->nChannels); /* nChannels */
|
|
stream_read_UINT32(s, format->nSamplesPerSec); /* nSamplesPerSec */
|
|
stream_read_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
|
|
stream_read_UINT16(s, format->nBlockAlign); /* nBlockAlign */
|
|
stream_read_UINT16(s, format->wBitsPerSample); /* wBitsPerSample */
|
|
stream_read_UINT16(s, format->cbSize); /* cbSize */
|
|
|
|
format->data = (BYTE*) malloc(format->cbSize);
|
|
stream_read(s, format->data, format->cbSize);
|
|
}
|
|
|
|
rdpsnd_select_supported_audio_formats(rdpsnd);
|
|
|
|
rdpsnd_send_client_audio_formats(rdpsnd);
|
|
|
|
if (wVersion >= 6)
|
|
rdpsnd_send_quality_mode_pdu(rdpsnd);
|
|
}
|
|
|
|
void rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, UINT16 wPackSize)
|
|
{
|
|
STREAM* pdu;
|
|
|
|
pdu = stream_new(8);
|
|
stream_write_BYTE(pdu, SNDC_TRAINING); /* msgType */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
stream_write_UINT16(pdu, 4); /* BodySize */
|
|
stream_write_UINT16(pdu, wTimeStamp);
|
|
stream_write_UINT16(pdu, wPackSize);
|
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
|
|
}
|
|
|
|
static void rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd, STREAM* s)
|
|
{
|
|
UINT16 wTimeStamp;
|
|
UINT16 wPackSize;
|
|
|
|
stream_read_UINT16(s, wTimeStamp);
|
|
stream_read_UINT16(s, wPackSize);
|
|
|
|
rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
|
|
}
|
|
|
|
static void rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd, STREAM* s, UINT16 BodySize)
|
|
{
|
|
UINT16 wFormatNo;
|
|
AUDIO_FORMAT* format;
|
|
rdpsndFormat sndFormat;
|
|
|
|
stream_read_UINT16(s, rdpsnd->wTimeStamp);
|
|
stream_read_UINT16(s, wFormatNo);
|
|
stream_read_BYTE(s, rdpsnd->cBlockNo);
|
|
stream_seek(s, 3); /* bPad */
|
|
stream_read(s, rdpsnd->waveData, 4);
|
|
|
|
rdpsnd->waveDataSize = BodySize - 8;
|
|
rdpsnd->wave_timestamp = GetTickCount();
|
|
rdpsnd->expectingWave = TRUE;
|
|
rdpsnd->close_timestamp = 0;
|
|
|
|
format = &rdpsnd->ClientFormats[wFormatNo];
|
|
rdpsnd_adapt_audio_formats_new_to_old(&sndFormat, format);
|
|
|
|
if (!rdpsnd->is_open)
|
|
{
|
|
rdpsnd->is_open = TRUE;
|
|
rdpsnd->wCurrentFormatNo = wFormatNo;
|
|
|
|
if (rdpsnd->device)
|
|
{
|
|
IFCALL(rdpsnd->device->Open, rdpsnd->device, &sndFormat, rdpsnd->latency);
|
|
}
|
|
}
|
|
else if (wFormatNo != rdpsnd->wCurrentFormatNo)
|
|
{
|
|
rdpsnd->wCurrentFormatNo = wFormatNo;
|
|
|
|
if (rdpsnd->device)
|
|
{
|
|
IFCALL(rdpsnd->device->SetFormat, rdpsnd->device, &sndFormat, rdpsnd->latency);
|
|
}
|
|
}
|
|
}
|
|
|
|
void rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, BYTE cConfirmedBlockNo)
|
|
{
|
|
STREAM* pdu;
|
|
|
|
pdu = stream_new(8);
|
|
stream_write_BYTE(pdu, SNDC_WAVECONFIRM);
|
|
stream_write_BYTE(pdu, 0);
|
|
stream_write_UINT16(pdu, 4);
|
|
stream_write_UINT16(pdu, wTimeStamp);
|
|
stream_write_BYTE(pdu, cConfirmedBlockNo); /* cConfirmedBlockNo */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
|
|
svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
|
|
}
|
|
|
|
void rdpsnd_device_send_wave_confirm_pdu(rdpsndDevicePlugin* device, UINT16 wTimeStamp, BYTE cConfirmedBlockNo)
|
|
{
|
|
rdpsnd_send_wave_confirm_pdu(device->rdpsnd, wTimeStamp, cConfirmedBlockNo);
|
|
}
|
|
|
|
UINT32 rdpsnd_device_wave_length(rdpsndDevicePlugin* device, int wFormatNo, int size)
|
|
{
|
|
UINT32 wAudioLength;
|
|
AUDIO_FORMAT* format;
|
|
|
|
format = &device->rdpsnd->ClientFormats[wFormatNo];
|
|
wAudioLength = rdpsnd_compute_audio_time_length(format, size);
|
|
|
|
return wAudioLength;
|
|
}
|
|
|
|
static void rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd, STREAM* s)
|
|
{
|
|
int size;
|
|
BYTE* data;
|
|
UINT16 wTimeStamp;
|
|
UINT16 wAudioLength;
|
|
AUDIO_FORMAT* format;
|
|
|
|
rdpsnd->expectingWave = FALSE;
|
|
|
|
/**
|
|
* The Wave PDU is a special case: it is always sent after a Wave Info PDU,
|
|
* and we do not process its header. Instead, the header is pad that needs
|
|
* to be filled with the first four bytes of the audio sample data sent as
|
|
* part of the preceding Wave Info PDU.
|
|
*/
|
|
|
|
CopyMemory(stream_get_head(s), rdpsnd->waveData, 4);
|
|
|
|
data = stream_get_head(s);
|
|
size = stream_get_size(s);
|
|
|
|
format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
|
|
wAudioLength = rdpsnd_compute_audio_time_length(format, size);
|
|
|
|
rdpsnd->device->WavePlay = NULL;
|
|
|
|
if (rdpsnd->device)
|
|
{
|
|
if (rdpsnd->device->WavePlay)
|
|
{
|
|
IFCALL(rdpsnd->device->WavePlay, rdpsnd->device, rdpsnd->wTimeStamp,
|
|
rdpsnd->wCurrentFormatNo, rdpsnd->cBlockNo, data, size);
|
|
}
|
|
else
|
|
{
|
|
IFCALL(rdpsnd->device->Play, rdpsnd->device, data, size);
|
|
}
|
|
}
|
|
|
|
if (!rdpsnd->device->WavePlay)
|
|
{
|
|
STREAM* pdu;
|
|
wTimeStamp = rdpsnd->wTimeStamp + wAudioLength + TIME_DELAY_MS;
|
|
|
|
pdu = stream_new(8);
|
|
stream_write_BYTE(pdu, SNDC_WAVECONFIRM);
|
|
stream_write_BYTE(pdu, 0);
|
|
stream_write_UINT16(pdu, 4);
|
|
stream_write_UINT16(pdu, wTimeStamp);
|
|
stream_write_BYTE(pdu, rdpsnd->cBlockNo); /* cConfirmedBlockNo */
|
|
stream_write_BYTE(pdu, 0); /* bPad */
|
|
|
|
wTimeStamp = rdpsnd->wave_timestamp + wAudioLength + TIME_DELAY_MS;
|
|
MessageQueue_Post(rdpsnd->MsgPipe->Out, NULL, 0, (void*) pdu, (void*) (size_t) wTimeStamp);
|
|
}
|
|
}
|
|
|
|
static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
|
|
{
|
|
DEBUG_SVC("server closes.");
|
|
|
|
if (rdpsnd->device)
|
|
{
|
|
IFCALL(rdpsnd->device->Start, rdpsnd->device);
|
|
}
|
|
|
|
rdpsnd->close_timestamp = GetTickCount() + 2000;
|
|
}
|
|
|
|
static void rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd, STREAM* s)
|
|
{
|
|
UINT32 dwVolume;
|
|
|
|
stream_read_UINT32(s, dwVolume);
|
|
DEBUG_SVC("dwVolume 0x%X", dwVolume);
|
|
|
|
if (rdpsnd->device)
|
|
{
|
|
IFCALL(rdpsnd->device->SetVolume, rdpsnd->device, dwVolume);
|
|
}
|
|
}
|
|
|
|
static void rdpsnd_recv_pdu(rdpSvcPlugin* plugin, STREAM* s)
|
|
{
|
|
BYTE msgType;
|
|
UINT16 BodySize;
|
|
rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
|
|
|
|
if (rdpsnd->expectingWave)
|
|
{
|
|
rdpsnd_recv_wave_pdu(rdpsnd, s);
|
|
stream_free(s);
|
|
return;
|
|
}
|
|
|
|
stream_read_BYTE(s, msgType); /* msgType */
|
|
stream_seek_BYTE(s); /* bPad */
|
|
stream_read_UINT16(s, BodySize);
|
|
|
|
DEBUG_SVC("msgType %d BodySize %d", msgType, BodySize);
|
|
|
|
switch (msgType)
|
|
{
|
|
case SNDC_FORMATS:
|
|
rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
|
|
break;
|
|
|
|
case SNDC_TRAINING:
|
|
rdpsnd_recv_training_pdu(rdpsnd, s);
|
|
break;
|
|
|
|
case SNDC_WAVE:
|
|
rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
|
|
break;
|
|
|
|
case SNDC_CLOSE:
|
|
rdpsnd_recv_close_pdu(rdpsnd);
|
|
break;
|
|
|
|
case SNDC_SETVOLUME:
|
|
rdpsnd_recv_volume_pdu(rdpsnd, s);
|
|
break;
|
|
|
|
default:
|
|
DEBUG_WARN("unknown msgType %d", msgType);
|
|
break;
|
|
}
|
|
|
|
stream_free(s);
|
|
}
|
|
|
|
static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
|
|
{
|
|
if (rdpsnd->device)
|
|
{
|
|
DEBUG_WARN("existing device, abort.");
|
|
return;
|
|
}
|
|
|
|
rdpsnd->device = device;
|
|
device->rdpsnd = rdpsnd;
|
|
|
|
device->WaveConfirm = rdpsnd_device_send_wave_confirm_pdu;
|
|
device->WaveLength = rdpsnd_device_wave_length;
|
|
}
|
|
|
|
static BOOL rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd, const char* name, ADDIN_ARGV* args)
|
|
{
|
|
PFREERDP_RDPSND_DEVICE_ENTRY entry;
|
|
FREERDP_RDPSND_DEVICE_ENTRY_POINTS entryPoints;
|
|
|
|
entry = (PFREERDP_RDPSND_DEVICE_ENTRY) freerdp_load_channel_addin_entry("rdpsnd", (LPSTR) name, NULL, 0);
|
|
|
|
if (!entry)
|
|
return FALSE;
|
|
|
|
entryPoints.rdpsnd = rdpsnd;
|
|
entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
|
|
entryPoints.args = args;
|
|
|
|
if (entry(&entryPoints) != 0)
|
|
{
|
|
DEBUG_WARN("%s entry returns error.", name);
|
|
return FALSE;
|
|
}
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
void rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd, char* subsystem)
|
|
{
|
|
if (rdpsnd->subsystem)
|
|
free(rdpsnd->subsystem);
|
|
|
|
rdpsnd->subsystem = _strdup(subsystem);
|
|
}
|
|
|
|
void rdpsnd_set_device_name(rdpsndPlugin* rdpsnd, char* device_name)
|
|
{
|
|
if (rdpsnd->device_name)
|
|
free(rdpsnd->device_name);
|
|
|
|
rdpsnd->device_name = _strdup(device_name);
|
|
}
|
|
|
|
COMMAND_LINE_ARGUMENT_A rdpsnd_args[] =
|
|
{
|
|
{ "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", NULL, NULL, -1, NULL, "subsystem" },
|
|
{ "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "device" },
|
|
{ "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", NULL, NULL, -1, NULL, "format" },
|
|
{ "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", NULL, NULL, -1, NULL, "rate" },
|
|
{ "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", NULL, NULL, -1, NULL, "channel" },
|
|
{ "latency", COMMAND_LINE_VALUE_REQUIRED, "<latency>", NULL, NULL, -1, NULL, "latency" },
|
|
{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
|
|
};
|
|
|
|
static void rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd, ADDIN_ARGV* args)
|
|
{
|
|
int status;
|
|
DWORD flags;
|
|
COMMAND_LINE_ARGUMENT_A* arg;
|
|
|
|
flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
|
|
|
|
status = CommandLineParseArgumentsA(args->argc, (const char**) args->argv,
|
|
rdpsnd_args, flags, rdpsnd, NULL, NULL);
|
|
|
|
arg = rdpsnd_args;
|
|
|
|
do
|
|
{
|
|
if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
|
|
continue;
|
|
|
|
CommandLineSwitchStart(arg)
|
|
|
|
CommandLineSwitchCase(arg, "sys")
|
|
{
|
|
rdpsnd_set_subsystem(rdpsnd, arg->Value);
|
|
}
|
|
CommandLineSwitchCase(arg, "dev")
|
|
{
|
|
rdpsnd_set_device_name(rdpsnd, arg->Value);
|
|
}
|
|
CommandLineSwitchCase(arg, "format")
|
|
{
|
|
rdpsnd->fixed_format = atoi(arg->Value);
|
|
}
|
|
CommandLineSwitchCase(arg, "rate")
|
|
{
|
|
rdpsnd->fixed_rate = atoi(arg->Value);
|
|
}
|
|
CommandLineSwitchCase(arg, "channel")
|
|
{
|
|
rdpsnd->fixed_channel = atoi(arg->Value);
|
|
}
|
|
CommandLineSwitchCase(arg, "latency")
|
|
{
|
|
rdpsnd->latency = atoi(arg->Value);
|
|
}
|
|
CommandLineSwitchDefault(arg)
|
|
{
|
|
|
|
}
|
|
|
|
CommandLineSwitchEnd(arg)
|
|
}
|
|
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
|
|
}
|
|
|
|
static void rdpsnd_process_connect(rdpSvcPlugin* plugin)
|
|
{
|
|
ADDIN_ARGV* args;
|
|
rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
|
|
|
|
DEBUG_SVC("connecting");
|
|
|
|
rdpsnd->latency = -1;
|
|
rdpsnd->MsgPipe = MessagePipe_New();
|
|
rdpsnd->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) rdpsnd_schedule_thread, (void*) plugin, 0, NULL);
|
|
|
|
args = (ADDIN_ARGV*) plugin->channel_entry_points.pExtendedData;
|
|
|
|
if (args)
|
|
rdpsnd_process_addin_args(rdpsnd, args);
|
|
|
|
if (rdpsnd->subsystem)
|
|
{
|
|
if (strcmp(rdpsnd->subsystem, "fake") == 0)
|
|
return;
|
|
|
|
rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
|
|
}
|
|
|
|
if (!rdpsnd->device)
|
|
{
|
|
rdpsnd_set_subsystem(rdpsnd, "pulse");
|
|
rdpsnd_set_device_name(rdpsnd, "");
|
|
rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
|
|
}
|
|
|
|
if (!rdpsnd->device)
|
|
{
|
|
rdpsnd_set_subsystem(rdpsnd, "alsa");
|
|
rdpsnd_set_device_name(rdpsnd, "default");
|
|
rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
|
|
}
|
|
|
|
if (!rdpsnd->device)
|
|
{
|
|
rdpsnd_set_subsystem(rdpsnd, "macaudio");
|
|
rdpsnd_set_device_name(rdpsnd, "default");
|
|
rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
|
|
}
|
|
|
|
if (!rdpsnd->device)
|
|
{
|
|
rdpsnd_set_subsystem(rdpsnd, "winmm");
|
|
rdpsnd_set_device_name(rdpsnd, "");
|
|
rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
|
|
}
|
|
|
|
if (!rdpsnd->device)
|
|
{
|
|
DEBUG_WARN("no sound device.");
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void rdpsnd_process_event(rdpSvcPlugin* plugin, RDP_EVENT* event)
|
|
{
|
|
freerdp_event_free(event);
|
|
}
|
|
|
|
static void rdpsnd_process_terminate(rdpSvcPlugin* plugin)
|
|
{
|
|
rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
|
|
|
|
if (rdpsnd->device)
|
|
IFCALL(rdpsnd->device->Free, rdpsnd->device);
|
|
|
|
MessagePipe_Free(rdpsnd->MsgPipe);
|
|
|
|
if (rdpsnd->subsystem)
|
|
free(rdpsnd->subsystem);
|
|
|
|
if (rdpsnd->device_name)
|
|
free(rdpsnd->device_name);
|
|
|
|
rdpsnd_free_audio_formats(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
|
|
rdpsnd->NumberOfServerFormats = 0;
|
|
rdpsnd->ServerFormats = NULL;
|
|
|
|
rdpsnd_free_audio_formats(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
|
|
rdpsnd->NumberOfClientFormats = 0;
|
|
rdpsnd->ClientFormats = NULL;
|
|
|
|
free(plugin);
|
|
}
|
|
|
|
/* rdpsnd is always built-in */
|
|
#define VirtualChannelEntry rdpsnd_VirtualChannelEntry
|
|
|
|
int VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
|
|
{
|
|
rdpsndPlugin* _p;
|
|
|
|
_p = (rdpsndPlugin*) malloc(sizeof(rdpsndPlugin));
|
|
ZeroMemory(_p, sizeof(rdpsndPlugin));
|
|
|
|
_p->plugin.channel_def.options =
|
|
CHANNEL_OPTION_INITIALIZED |
|
|
CHANNEL_OPTION_ENCRYPT_RDP;
|
|
|
|
strcpy(_p->plugin.channel_def.name, "rdpsnd");
|
|
|
|
_p->plugin.connect_callback = rdpsnd_process_connect;
|
|
_p->plugin.receive_callback = rdpsnd_recv_pdu;
|
|
_p->plugin.event_callback = rdpsnd_process_event;
|
|
_p->plugin.terminate_callback = rdpsnd_process_terminate;
|
|
|
|
#ifndef _WIN32
|
|
{
|
|
sigset_t mask;
|
|
sigemptyset(&mask);
|
|
sigaddset(&mask, SIGIO);
|
|
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
|
}
|
|
#endif
|
|
|
|
svc_plugin_init((rdpSvcPlugin*) _p, pEntryPoints);
|
|
|
|
return 1;
|
|
}
|