2005-08-03 12:56:16 +02:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
2003-09-30 11:11:08 +02:00
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Sound Channel Process Functions
|
2010-04-09 15:54:46 +02:00
|
|
|
Copyright 2006-2010 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
2011-04-13 10:55:42 +02:00
|
|
|
Copyright 2009-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
|
2011-04-13 13:13:04 +02:00
|
|
|
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 2003-2008
|
2011-04-13 13:28:19 +02:00
|
|
|
Copyright (C) GuoJunBo <guojunbo@ict.ac.cn> 2003
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2010-01-12 09:31:06 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
2003-09-30 11:11:08 +02:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2010-01-12 09:31:06 +01:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2003-09-30 11:11:08 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2010-01-12 09:31:06 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-09-30 11:11:08 +02:00
|
|
|
*/
|
|
|
|
|
2006-10-19 13:27:40 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2003-07-01 11:31:25 +02:00
|
|
|
#include "rdesktop.h"
|
2006-09-17 12:32:18 +02:00
|
|
|
#include "rdpsnd.h"
|
2006-09-17 16:41:16 +02:00
|
|
|
#include "rdpsnd_dsp.h"
|
2003-07-01 11:31:25 +02:00
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
#define SNDC_CLOSE 0x01
|
|
|
|
#define SNDC_WAVE 0x02
|
|
|
|
#define SNDC_SETVOLUME 0x03
|
|
|
|
#define SNDC_SETPITCH 0x04
|
|
|
|
#define SNDC_WAVECONFIRM 0x05
|
|
|
|
#define SNDC_TRAINING 0x06
|
|
|
|
#define SNDC_FORMATS 0x07
|
|
|
|
#define SNDC_CRYPTKEY 0x08
|
|
|
|
#define SNDC_WAVEENCRYPT 0x09
|
|
|
|
#define SNDC_UDPWAVE 0x0A
|
|
|
|
#define SNDC_UDPWAVELAST 0x0B
|
|
|
|
#define SNDC_QUALITYMODE 0x0C
|
|
|
|
#define SNDC_WAVE2 0x0D
|
2003-09-30 11:11:08 +02:00
|
|
|
|
|
|
|
#define MAX_FORMATS 10
|
2007-01-02 17:30:06 +01:00
|
|
|
#define MAX_QUEUE 50
|
2006-09-17 12:32:18 +02:00
|
|
|
|
2014-03-06 12:43:43 +01:00
|
|
|
extern RD_BOOL g_rdpsnd;
|
|
|
|
|
2003-07-01 11:31:25 +02:00
|
|
|
static VCHANNEL *rdpsnd_channel;
|
2007-01-02 17:30:06 +01:00
|
|
|
static VCHANNEL *rdpsnddbg_channel;
|
2006-09-17 13:04:50 +02:00
|
|
|
static struct audio_driver *drivers = NULL;
|
2006-09-17 13:42:22 +02:00
|
|
|
struct audio_driver *current_driver = NULL;
|
2003-07-01 11:31:25 +02:00
|
|
|
|
2010-04-09 15:54:46 +02:00
|
|
|
static RD_BOOL rdpsnd_negotiated;
|
|
|
|
|
2007-01-08 05:47:06 +01:00
|
|
|
static RD_BOOL device_open;
|
2007-01-02 17:30:06 +01:00
|
|
|
|
2007-01-04 05:55:56 +01:00
|
|
|
static RD_WAVEFORMATEX formats[MAX_FORMATS];
|
2003-09-30 11:11:08 +02:00
|
|
|
static unsigned int format_count;
|
|
|
|
static unsigned int current_format;
|
2007-01-02 17:30:06 +01:00
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
unsigned int queue_hi, queue_lo, queue_pending;
|
2006-09-17 13:42:22 +02:00
|
|
|
struct audio_packet packet_queue[MAX_QUEUE];
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
static uint8 packet_opcode;
|
|
|
|
static struct stream packet;
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
void (*wave_out_play) (void);
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
static void rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index);
|
|
|
|
static void rdpsnd_queue_init(void);
|
2010-04-09 15:12:31 +02:00
|
|
|
static void rdpsnd_queue_clear(void);
|
2006-10-26 11:47:17 +02:00
|
|
|
static void rdpsnd_queue_complete_pending(void);
|
|
|
|
static long rdpsnd_queue_next_completion(void);
|
|
|
|
|
2004-04-17 09:24:22 +02:00
|
|
|
static STREAM
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_init_packet(uint8 type, uint16 size)
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
|
|
|
STREAM s;
|
|
|
|
|
2003-10-15 16:01:32 +02:00
|
|
|
s = channel_init(rdpsnd_channel, size + 4);
|
2017-10-04 16:09:44 +02:00
|
|
|
out_uint8(s, type);
|
|
|
|
out_uint8(s, 0); /* protocol-mandated padding */
|
2003-09-30 11:11:08 +02:00
|
|
|
out_uint16_le(s, size);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:24:22 +02:00
|
|
|
static void
|
2003-09-30 11:11:08 +02:00
|
|
|
rdpsnd_send(STREAM s)
|
|
|
|
{
|
|
|
|
channel_send(s, rdpsnd_channel);
|
|
|
|
}
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
static void
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_send_waveconfirm(uint16 tick, uint8 packet_index)
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
|
|
|
STREAM s;
|
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
s = rdpsnd_init_packet(SNDC_WAVECONFIRM, 4);
|
2006-09-17 12:32:18 +02:00
|
|
|
out_uint16_le(s, tick);
|
2003-09-30 11:11:08 +02:00
|
|
|
out_uint8(s, packet_index);
|
|
|
|
out_uint8(s, 0);
|
|
|
|
s_mark_end(s);
|
|
|
|
rdpsnd_send(s);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
logger(Sound, Debug, "rdpsnd_send_waveconfirm(), tick=%u, index=%u",
|
2017-01-26 14:19:40 +01:00
|
|
|
(unsigned) tick, (unsigned) packet_index);
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-02 17:30:06 +01:00
|
|
|
void
|
|
|
|
rdpsnd_record(const void *data, unsigned int size)
|
|
|
|
{
|
2016-12-05 09:06:35 +01:00
|
|
|
/* TODO: Send audio over RDP */
|
2007-01-02 17:30:06 +01:00
|
|
|
}
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-08 05:47:06 +01:00
|
|
|
static RD_BOOL
|
2006-12-07 16:23:45 +01:00
|
|
|
rdpsnd_auto_select(void)
|
|
|
|
{
|
2007-01-08 05:47:06 +01:00
|
|
|
static RD_BOOL failed = False;
|
2006-12-07 16:23:45 +01:00
|
|
|
|
|
|
|
if (!failed)
|
|
|
|
{
|
|
|
|
current_driver = drivers;
|
|
|
|
while (current_driver != NULL)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "rdpsnd_auto_select(), trying driver '%s'",
|
|
|
|
current_driver->name);
|
2006-12-07 16:23:45 +01:00
|
|
|
if (current_driver->wave_out_open())
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "rdpsnd_auto_select(), using driver '%s'",
|
|
|
|
current_driver->name);
|
2006-12-24 14:48:50 +01:00
|
|
|
current_driver->wave_out_close();
|
2006-12-07 16:23:45 +01:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
current_driver = current_driver->next;
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "no working audio-driver found");
|
2006-12-07 16:23:45 +01:00
|
|
|
failed = True;
|
|
|
|
current_driver = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:24:22 +02:00
|
|
|
static void
|
2003-09-30 11:11:08 +02:00
|
|
|
rdpsnd_process_negotiate(STREAM in)
|
|
|
|
{
|
2006-12-06 14:11:35 +01:00
|
|
|
uint16 in_format_count, i;
|
|
|
|
uint8 pad;
|
|
|
|
uint16 version;
|
2007-01-04 05:55:56 +01:00
|
|
|
RD_WAVEFORMATEX *format;
|
2003-09-30 11:11:08 +02:00
|
|
|
STREAM out;
|
2007-01-08 05:47:06 +01:00
|
|
|
RD_BOOL device_available = False;
|
2003-10-14 09:53:10 +02:00
|
|
|
int readcnt;
|
|
|
|
int discardcnt;
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2006-12-06 14:11:35 +01:00
|
|
|
in_uint8s(in, 14); /* initial bytes not valid from server */
|
2003-09-30 11:11:08 +02:00
|
|
|
in_uint16_le(in, in_format_count);
|
2006-12-06 14:11:35 +01:00
|
|
|
in_uint8(in, pad);
|
|
|
|
in_uint16_le(in, version);
|
|
|
|
in_uint8s(in, 1); /* padding */
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
|
|
|
"rdpsnd_process_negotiate(), formats = %d, pad = 0x%02x, version = 0x%x",
|
|
|
|
(int) in_format_count, (unsigned) pad, (unsigned) version);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2010-04-09 15:54:46 +02:00
|
|
|
if (rdpsnd_negotiated)
|
|
|
|
{
|
|
|
|
/* Do a complete reset of the sound state */
|
|
|
|
rdpsnd_reset_state();
|
|
|
|
}
|
|
|
|
|
2014-03-06 12:43:43 +01:00
|
|
|
if (!current_driver && g_rdpsnd)
|
2006-12-24 16:22:15 +01:00
|
|
|
device_available = rdpsnd_auto_select();
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2006-12-24 16:22:15 +01:00
|
|
|
if (current_driver && !device_available && current_driver->wave_out_open())
|
2003-10-13 18:09:45 +02:00
|
|
|
{
|
2006-12-24 16:22:15 +01:00
|
|
|
current_driver->wave_out_close();
|
2003-10-13 18:09:45 +02:00
|
|
|
device_available = True;
|
|
|
|
}
|
|
|
|
|
2003-09-30 11:11:08 +02:00
|
|
|
format_count = 0;
|
2003-10-15 16:01:32 +02:00
|
|
|
if (s_check_rem(in, 18 * in_format_count))
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
|
|
|
for (i = 0; i < in_format_count; i++)
|
|
|
|
{
|
|
|
|
format = &formats[format_count];
|
|
|
|
in_uint16_le(in, format->wFormatTag);
|
|
|
|
in_uint16_le(in, format->nChannels);
|
|
|
|
in_uint32_le(in, format->nSamplesPerSec);
|
|
|
|
in_uint32_le(in, format->nAvgBytesPerSec);
|
|
|
|
in_uint16_le(in, format->nBlockAlign);
|
|
|
|
in_uint16_le(in, format->wBitsPerSample);
|
|
|
|
in_uint16_le(in, format->cbSize);
|
|
|
|
|
2003-10-14 09:46:49 +02:00
|
|
|
/* read in the buffer of unknown use */
|
2003-10-14 09:53:10 +02:00
|
|
|
readcnt = format->cbSize;
|
|
|
|
discardcnt = 0;
|
2003-10-14 09:46:49 +02:00
|
|
|
if (format->cbSize > MAX_CBSIZE)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
|
|
|
"rdpsnd_process_negotiate(), cbSize too large for buffer: %d",
|
|
|
|
format->cbSize);
|
2003-10-14 09:46:49 +02:00
|
|
|
readcnt = MAX_CBSIZE;
|
|
|
|
discardcnt = format->cbSize - MAX_CBSIZE;
|
|
|
|
}
|
|
|
|
in_uint8a(in, format->cb, readcnt);
|
|
|
|
in_uint8s(in, discardcnt);
|
|
|
|
|
2007-06-18 10:31:27 +02:00
|
|
|
if (current_driver && current_driver->wave_out_format_supported(format))
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
|
|
|
format_count++;
|
|
|
|
if (format_count == MAX_FORMATS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
out = rdpsnd_init_packet(SNDC_FORMATS, 20 + 18 * format_count);
|
2014-03-06 12:43:43 +01:00
|
|
|
|
|
|
|
uint32 flags = TSSNDCAPS_VOLUME;
|
|
|
|
|
|
|
|
/* if sound is enabled, set snd caps to alive to enable
|
|
|
|
transmision of audio from server */
|
|
|
|
if (g_rdpsnd)
|
2014-05-13 16:03:08 +02:00
|
|
|
{
|
2014-03-06 12:43:43 +01:00
|
|
|
flags |= TSSNDCAPS_ALIVE;
|
2014-05-13 16:03:08 +02:00
|
|
|
}
|
2014-03-06 12:43:43 +01:00
|
|
|
out_uint32_le(out, flags); /* TSSNDCAPS flags */
|
|
|
|
|
2003-09-30 11:11:08 +02:00
|
|
|
out_uint32(out, 0xffffffff); /* volume */
|
2003-10-15 16:01:32 +02:00
|
|
|
out_uint32(out, 0); /* pitch */
|
|
|
|
out_uint16(out, 0); /* UDP port */
|
2003-09-30 11:11:08 +02:00
|
|
|
|
|
|
|
out_uint16_le(out, format_count);
|
2006-12-06 14:11:35 +01:00
|
|
|
out_uint8(out, 0); /* padding */
|
|
|
|
out_uint16_le(out, 2); /* version */
|
|
|
|
out_uint8(out, 0); /* padding */
|
2003-09-30 11:11:08 +02:00
|
|
|
|
|
|
|
for (i = 0; i < format_count; i++)
|
|
|
|
{
|
|
|
|
format = &formats[i];
|
|
|
|
out_uint16_le(out, format->wFormatTag);
|
|
|
|
out_uint16_le(out, format->nChannels);
|
|
|
|
out_uint32_le(out, format->nSamplesPerSec);
|
|
|
|
out_uint32_le(out, format->nAvgBytesPerSec);
|
|
|
|
out_uint16_le(out, format->nBlockAlign);
|
|
|
|
out_uint16_le(out, format->wBitsPerSample);
|
2003-10-15 16:01:32 +02:00
|
|
|
out_uint16(out, 0); /* cbSize */
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s_mark_end(out);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "rdpsnd_process_negotiate(), %d formats available",
|
|
|
|
(int) format_count);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2003-09-30 11:11:08 +02:00
|
|
|
rdpsnd_send(out);
|
2010-04-09 15:54:46 +02:00
|
|
|
|
|
|
|
rdpsnd_negotiated = True;
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
|
|
|
|
2004-04-17 09:24:22 +02:00
|
|
|
static void
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_process_training(STREAM in)
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
2006-12-06 14:01:45 +01:00
|
|
|
uint16 tick;
|
2003-09-30 11:11:08 +02:00
|
|
|
STREAM out;
|
|
|
|
|
2006-12-06 14:01:45 +01:00
|
|
|
in_uint16_le(in, tick);
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
logger(Sound, Debug, "rdpsnd_process_training(), tick=0x%04x, packsize=%d", (unsigned) tick,
|
|
|
|
packsize);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2017-10-04 16:09:44 +02:00
|
|
|
out = rdpsnd_init_packet(SNDC_TRAINING, 4);
|
2006-12-06 14:01:45 +01:00
|
|
|
out_uint16_le(out, tick);
|
|
|
|
out_uint16_le(out, 0);
|
2003-09-30 11:11:08 +02:00
|
|
|
s_mark_end(out);
|
|
|
|
rdpsnd_send(out);
|
|
|
|
}
|
|
|
|
|
2004-04-17 09:24:22 +02:00
|
|
|
static void
|
2006-12-06 14:29:42 +01:00
|
|
|
rdpsnd_process_packet(uint8 opcode, STREAM s)
|
2003-07-01 11:31:25 +02:00
|
|
|
{
|
2006-12-06 14:18:36 +01:00
|
|
|
uint16 vol_left, vol_right;
|
2003-09-30 11:11:08 +02:00
|
|
|
static uint16 tick, format;
|
|
|
|
static uint8 packet_index;
|
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
switch (opcode)
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
2017-10-04 16:09:44 +02:00
|
|
|
case SNDC_WAVE:
|
2006-12-06 14:29:42 +01:00
|
|
|
in_uint16_le(s, tick);
|
|
|
|
in_uint16_le(s, format);
|
|
|
|
in_uint8(s, packet_index);
|
|
|
|
in_uint8s(s, 3);
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
|
|
|
"rdpsnd_process_packet(), RDPSND_WRITE(tick: %u, format: %u, index: %u, data: %u bytes)\n",
|
|
|
|
(unsigned) tick, (unsigned) format, (unsigned) packet_index,
|
|
|
|
(unsigned) s->size - 8);
|
2003-09-30 11:11:08 +02:00
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
if (format >= MAX_FORMATS)
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"rdpsnd_process_packet(), invalid format index");
|
2006-12-06 14:29:42 +01:00
|
|
|
break;
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
2006-12-06 14:29:42 +01:00
|
|
|
|
|
|
|
if (!device_open || (format != current_format))
|
2003-09-30 11:11:08 +02:00
|
|
|
{
|
2006-12-07 16:23:45 +01:00
|
|
|
/*
|
|
|
|
* If we haven't selected a device by now, then either
|
|
|
|
* we've failed to find a working device, or the server
|
2017-10-04 16:09:44 +02:00
|
|
|
* is sending bogus SNDC_WAVE.
|
2006-12-07 16:23:45 +01:00
|
|
|
*/
|
|
|
|
if (!current_driver)
|
|
|
|
{
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_send_waveconfirm(tick, packet_index);
|
2006-12-07 16:23:45 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-12-06 14:29:42 +01:00
|
|
|
if (!device_open && !current_driver->wave_out_open())
|
|
|
|
{
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_send_waveconfirm(tick, packet_index);
|
2006-12-06 14:29:42 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!current_driver->wave_out_set_format(&formats[format]))
|
|
|
|
{
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_send_waveconfirm(tick, packet_index);
|
2006-12-06 14:29:42 +01:00
|
|
|
current_driver->wave_out_close();
|
|
|
|
device_open = False;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
device_open = True;
|
|
|
|
current_format = format;
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
rdpsnd_queue_write(rdpsnd_dsp_process
|
|
|
|
(s->p, s->end - s->p, current_driver,
|
|
|
|
&formats[current_format]), tick, packet_index);
|
|
|
|
return;
|
2003-10-15 16:01:32 +02:00
|
|
|
break;
|
2017-10-04 16:09:44 +02:00
|
|
|
case SNDC_CLOSE:
|
|
|
|
logger(Sound, Debug, "rdpsnd_process_packet(), SNDC_CLOSE()");
|
2006-12-07 16:23:45 +01:00
|
|
|
if (device_open)
|
|
|
|
current_driver->wave_out_close();
|
2003-10-15 16:01:32 +02:00
|
|
|
device_open = False;
|
|
|
|
break;
|
2017-10-04 16:09:44 +02:00
|
|
|
case SNDC_FORMATS:
|
2003-10-15 16:01:32 +02:00
|
|
|
rdpsnd_process_negotiate(s);
|
|
|
|
break;
|
2017-10-04 16:09:44 +02:00
|
|
|
case SNDC_TRAINING:
|
|
|
|
rdpsnd_process_training(s);
|
2003-10-15 16:01:32 +02:00
|
|
|
break;
|
2017-10-04 16:09:44 +02:00
|
|
|
case SNDC_SETVOLUME:
|
2006-12-06 14:18:36 +01:00
|
|
|
in_uint16_le(s, vol_left);
|
|
|
|
in_uint16_le(s, vol_right);
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
2017-10-04 16:09:44 +02:00
|
|
|
"rdpsnd_process_packet(), SNDC_SETVOLUME(left: 0x%04x (%u %%), right: 0x%04x (%u %%))",
|
2017-01-26 14:19:40 +01:00
|
|
|
(unsigned) vol_left, (unsigned) vol_left / 655, (unsigned) vol_right,
|
|
|
|
(unsigned) vol_right / 655);
|
2003-10-15 16:01:32 +02:00
|
|
|
if (device_open)
|
2006-12-06 14:18:36 +01:00
|
|
|
current_driver->wave_out_volume(vol_left, vol_right);
|
2003-10-15 16:01:32 +02:00
|
|
|
break;
|
|
|
|
default:
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Warning, "rdpsnd_process_packet(), Unhandled opcode 0x%x",
|
|
|
|
opcode);
|
2003-10-15 16:01:32 +02:00
|
|
|
break;
|
2003-09-30 11:11:08 +02:00
|
|
|
}
|
2003-07-01 11:31:25 +02:00
|
|
|
}
|
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
static void
|
|
|
|
rdpsnd_process(STREAM s)
|
|
|
|
{
|
|
|
|
uint16 len;
|
|
|
|
|
|
|
|
while (!s_check_end(s))
|
|
|
|
{
|
|
|
|
/* New packet */
|
|
|
|
if (packet.size == 0)
|
|
|
|
{
|
|
|
|
if ((s->end - s->p) < 4)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"rdpsnd_process(), split at packet header, things will go south from here...");
|
2006-12-06 14:29:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
in_uint8(s, packet_opcode);
|
|
|
|
in_uint8s(s, 1); /* Padding */
|
|
|
|
in_uint16_le(s, len);
|
|
|
|
|
2017-09-26 15:04:28 +02:00
|
|
|
logger(Sound, Debug, "rdpsnd_process(), Opcode = 0x%x Length= %d",
|
2017-01-26 14:19:40 +01:00
|
|
|
(int) packet_opcode, (int) len);
|
2006-12-06 14:55:34 +01:00
|
|
|
|
2006-12-06 14:29:42 +01:00
|
|
|
packet.p = packet.data;
|
|
|
|
packet.end = packet.data + len;
|
|
|
|
packet.size = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = MIN(s->end - s->p, packet.end - packet.p);
|
|
|
|
|
|
|
|
/* Microsoft's server is so broken it's not even funny... */
|
2017-10-04 16:09:44 +02:00
|
|
|
if (packet_opcode == SNDC_WAVE)
|
2006-12-06 14:29:42 +01:00
|
|
|
{
|
|
|
|
if ((packet.p - packet.data) < 12)
|
|
|
|
len = MIN(len, 12 - (packet.p - packet.data));
|
|
|
|
else if ((packet.p - packet.data) == 12)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
2017-10-04 16:09:44 +02:00
|
|
|
"rdpsnd_process(), eating 4 bytes of %d bytes...",
|
2017-01-26 14:19:40 +01:00
|
|
|
len);
|
2006-12-06 14:29:42 +01:00
|
|
|
in_uint8s(s, 4);
|
|
|
|
len -= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
in_uint8a(s, packet.p, len);
|
|
|
|
packet.p += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Packet fully assembled */
|
|
|
|
if (packet.p == packet.end)
|
|
|
|
{
|
|
|
|
packet.p = packet.data;
|
|
|
|
rdpsnd_process_packet(packet_opcode, &packet);
|
|
|
|
packet.size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-08 05:47:06 +01:00
|
|
|
static RD_BOOL
|
2007-01-02 17:30:06 +01:00
|
|
|
rdpsnddbg_line_handler(const char *line, void *data)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "rdpsnddbg_line_handler(), \"%s\"", line);
|
2007-01-02 17:30:06 +01:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rdpsnddbg_process(STREAM s)
|
|
|
|
{
|
|
|
|
unsigned int pkglen;
|
|
|
|
static char *rest = NULL;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
pkglen = s->end - s->p;
|
|
|
|
/* str_handle_lines requires null terminated strings */
|
2007-01-18 06:42:11 +01:00
|
|
|
buf = (char *) xmalloc(pkglen + 1);
|
2007-01-02 17:30:06 +01:00
|
|
|
STRNCPY(buf, (char *) s->p, pkglen + 1);
|
|
|
|
|
|
|
|
str_handle_lines(buf, &rest, rdpsnddbg_line_handler, NULL);
|
|
|
|
|
|
|
|
xfree(buf);
|
|
|
|
}
|
|
|
|
|
2006-10-01 13:00:03 +02:00
|
|
|
static void
|
2006-09-17 13:04:50 +02:00
|
|
|
rdpsnd_register_drivers(char *options)
|
|
|
|
{
|
|
|
|
struct audio_driver **reg;
|
|
|
|
|
|
|
|
/* The order of registrations define the probe-order
|
|
|
|
when opening the device for the first time */
|
|
|
|
reg = &drivers;
|
|
|
|
#if defined(RDPSND_ALSA)
|
|
|
|
*reg = alsa_register(options);
|
2006-10-19 13:27:40 +02:00
|
|
|
assert(*reg);
|
2006-09-17 13:04:50 +02:00
|
|
|
reg = &((*reg)->next);
|
|
|
|
#endif
|
|
|
|
#if defined(RDPSND_SUN)
|
|
|
|
*reg = sun_register(options);
|
2006-10-19 13:27:40 +02:00
|
|
|
assert(*reg);
|
2006-09-17 13:04:50 +02:00
|
|
|
reg = &((*reg)->next);
|
|
|
|
#endif
|
2006-09-17 21:08:14 +02:00
|
|
|
#if defined(RDPSND_OSS)
|
|
|
|
*reg = oss_register(options);
|
2006-10-19 13:27:40 +02:00
|
|
|
assert(*reg);
|
2006-09-17 21:08:14 +02:00
|
|
|
reg = &((*reg)->next);
|
|
|
|
#endif
|
2006-09-17 13:04:50 +02:00
|
|
|
#if defined(RDPSND_SGI)
|
|
|
|
*reg = sgi_register(options);
|
2006-10-19 13:27:40 +02:00
|
|
|
assert(*reg);
|
2006-09-17 13:04:50 +02:00
|
|
|
reg = &((*reg)->next);
|
|
|
|
#endif
|
|
|
|
#if defined(RDPSND_LIBAO)
|
|
|
|
*reg = libao_register(options);
|
2006-10-26 11:47:17 +02:00
|
|
|
assert(*reg);
|
2006-09-17 13:04:50 +02:00
|
|
|
reg = &((*reg)->next);
|
|
|
|
#endif
|
2006-11-07 00:05:39 +01:00
|
|
|
*reg = NULL;
|
2006-09-17 13:04:50 +02:00
|
|
|
}
|
|
|
|
|
2007-01-08 05:47:06 +01:00
|
|
|
RD_BOOL
|
2006-09-21 21:00:11 +02:00
|
|
|
rdpsnd_init(char *optarg)
|
2006-09-17 13:04:50 +02:00
|
|
|
{
|
|
|
|
struct audio_driver *pos;
|
2006-09-21 21:00:11 +02:00
|
|
|
char *driver = NULL, *options = NULL;
|
2006-09-17 13:04:50 +02:00
|
|
|
|
|
|
|
drivers = NULL;
|
2006-09-21 21:00:11 +02:00
|
|
|
|
2007-01-18 06:42:11 +01:00
|
|
|
packet.data = (uint8 *) xmalloc(65536);
|
2006-12-06 14:29:42 +01:00
|
|
|
packet.p = packet.end = packet.data;
|
|
|
|
packet.size = 0;
|
|
|
|
|
2006-09-21 21:00:11 +02:00
|
|
|
rdpsnd_channel =
|
|
|
|
channel_register("rdpsnd", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
|
|
|
|
rdpsnd_process);
|
|
|
|
|
2007-01-02 17:30:06 +01:00
|
|
|
rdpsnddbg_channel =
|
|
|
|
channel_register("snddbg", CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP,
|
|
|
|
rdpsnddbg_process);
|
|
|
|
|
|
|
|
if ((rdpsnd_channel == NULL) || (rdpsnddbg_channel == NULL))
|
2006-09-21 21:00:11 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"rdpsnd_init(), failed to register rdpsnd / snddbg virtual channels");
|
2006-09-21 21:00:11 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
rdpsnd_queue_init();
|
|
|
|
|
2006-09-21 21:00:11 +02:00
|
|
|
if (optarg != NULL && strlen(optarg) > 0)
|
|
|
|
{
|
|
|
|
driver = options = optarg;
|
|
|
|
|
|
|
|
while (*options != '\0' && *options != ':')
|
|
|
|
options++;
|
|
|
|
|
|
|
|
if (*options == ':')
|
|
|
|
{
|
|
|
|
*options = '\0';
|
|
|
|
options++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*options == '\0')
|
|
|
|
options = NULL;
|
|
|
|
}
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
rdpsnd_register_drivers(options);
|
|
|
|
|
|
|
|
if (!driver)
|
|
|
|
return True;
|
|
|
|
|
|
|
|
pos = drivers;
|
|
|
|
while (pos != NULL)
|
|
|
|
{
|
|
|
|
if (!strcmp(pos->name, driver))
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug, "rdpsnd_init(), using driver '%s'", pos->name);
|
2006-09-17 13:04:50 +02:00
|
|
|
current_driver = pos;
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
pos = pos->next;
|
|
|
|
}
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2010-04-09 15:12:31 +02:00
|
|
|
void
|
|
|
|
rdpsnd_reset_state(void)
|
|
|
|
{
|
|
|
|
if (device_open)
|
|
|
|
current_driver->wave_out_close();
|
|
|
|
device_open = False;
|
|
|
|
rdpsnd_queue_clear();
|
2010-04-09 15:54:46 +02:00
|
|
|
rdpsnd_negotiated = False;
|
2010-04-09 15:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
void
|
|
|
|
rdpsnd_show_help(void)
|
|
|
|
{
|
|
|
|
struct audio_driver *pos;
|
|
|
|
|
|
|
|
rdpsnd_register_drivers(NULL);
|
|
|
|
|
|
|
|
pos = drivers;
|
|
|
|
while (pos != NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, " %s:\t%s\n", pos->name, pos->description);
|
|
|
|
pos = pos->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:28:53 +02:00
|
|
|
void
|
2006-10-26 11:47:17 +02:00
|
|
|
rdpsnd_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
|
|
|
|
{
|
|
|
|
long next_pending;
|
|
|
|
|
2016-12-05 09:06:35 +01:00
|
|
|
if (device_open)
|
2006-12-07 16:23:45 +01:00
|
|
|
current_driver->add_fds(n, rfds, wfds, tv);
|
2006-10-26 11:47:17 +02:00
|
|
|
|
|
|
|
next_pending = rdpsnd_queue_next_completion();
|
|
|
|
if (next_pending >= 0)
|
|
|
|
{
|
|
|
|
long cur_timeout;
|
|
|
|
|
|
|
|
cur_timeout = tv->tv_sec * 1000000 + tv->tv_usec;
|
|
|
|
if (cur_timeout > next_pending)
|
|
|
|
{
|
|
|
|
tv->tv_sec = next_pending / 1000000;
|
|
|
|
tv->tv_usec = next_pending % 1000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rdpsnd_check_fds(fd_set * rfds, fd_set * wfds)
|
|
|
|
{
|
|
|
|
rdpsnd_queue_complete_pending();
|
|
|
|
|
2016-12-05 09:06:35 +01:00
|
|
|
if (device_open)
|
2006-12-07 16:23:45 +01:00
|
|
|
current_driver->check_fds(rfds, wfds);
|
2006-10-26 11:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_write(STREAM s, uint16 tick, uint8 index)
|
|
|
|
{
|
|
|
|
struct audio_packet *packet = &packet_queue[queue_hi];
|
|
|
|
unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
if (next_hi == queue_pending)
|
2006-09-17 12:32:18 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "rdpsnd_queue_write(), no space to queue audio packet");
|
2006-09-17 12:32:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_hi = next_hi;
|
|
|
|
|
|
|
|
packet->s = *s;
|
|
|
|
packet->tick = tick;
|
|
|
|
packet->index = index;
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
gettimeofday(&packet->arrive_tv, NULL);
|
2006-09-17 12:32:18 +02:00
|
|
|
}
|
|
|
|
|
2006-10-19 13:28:53 +02:00
|
|
|
struct audio_packet *
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_current_packet(void)
|
|
|
|
{
|
|
|
|
return &packet_queue[queue_lo];
|
|
|
|
}
|
|
|
|
|
2007-01-08 05:47:06 +01:00
|
|
|
RD_BOOL
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_empty(void)
|
|
|
|
{
|
|
|
|
return (queue_lo == queue_hi);
|
|
|
|
}
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
static void
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_init(void)
|
|
|
|
{
|
2006-10-26 11:47:17 +02:00
|
|
|
queue_pending = queue_lo = queue_hi = 0;
|
2006-09-17 12:32:18 +02:00
|
|
|
}
|
|
|
|
|
2010-04-09 15:12:31 +02:00
|
|
|
static void
|
|
|
|
rdpsnd_queue_clear(void)
|
|
|
|
{
|
|
|
|
struct audio_packet *packet;
|
|
|
|
|
|
|
|
/* Go through everything, not just the pending packets */
|
|
|
|
while (queue_pending != queue_hi)
|
|
|
|
{
|
|
|
|
packet = &packet_queue[queue_pending];
|
|
|
|
xfree(packet->s.data);
|
|
|
|
queue_pending = (queue_pending + 1) % MAX_QUEUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset everything back to the initial state */
|
|
|
|
queue_pending = queue_lo = queue_hi = 0;
|
|
|
|
}
|
|
|
|
|
2006-10-19 13:28:53 +02:00
|
|
|
void
|
2006-10-26 11:47:17 +02:00
|
|
|
rdpsnd_queue_next(unsigned long completed_in_us)
|
2006-09-17 12:32:18 +02:00
|
|
|
{
|
2006-10-26 11:47:17 +02:00
|
|
|
struct audio_packet *packet;
|
|
|
|
|
|
|
|
assert(!rdpsnd_queue_empty());
|
|
|
|
|
|
|
|
packet = &packet_queue[queue_lo];
|
|
|
|
|
|
|
|
gettimeofday(&packet->completion_tv, NULL);
|
|
|
|
|
|
|
|
packet->completion_tv.tv_usec += completed_in_us;
|
|
|
|
packet->completion_tv.tv_sec += packet->completion_tv.tv_usec / 1000000;
|
|
|
|
packet->completion_tv.tv_usec %= 1000000;
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
queue_lo = (queue_lo + 1) % MAX_QUEUE;
|
2006-10-26 11:47:17 +02:00
|
|
|
|
|
|
|
rdpsnd_queue_complete_pending();
|
2006-09-17 12:32:18 +02:00
|
|
|
}
|
|
|
|
|
2006-10-19 13:28:53 +02:00
|
|
|
int
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_next_tick(void)
|
|
|
|
{
|
|
|
|
if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)
|
|
|
|
{
|
|
|
|
return packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (packet_queue[queue_lo].tick + 65535) % 65536;
|
|
|
|
}
|
|
|
|
}
|
2006-10-26 11:47:17 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
rdpsnd_queue_complete_pending(void)
|
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
long elapsed;
|
|
|
|
struct audio_packet *packet;
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
while (queue_pending != queue_lo)
|
|
|
|
{
|
|
|
|
packet = &packet_queue[queue_pending];
|
|
|
|
|
|
|
|
if (now.tv_sec < packet->completion_tv.tv_sec)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ((now.tv_sec == packet->completion_tv.tv_sec) &&
|
|
|
|
(now.tv_usec < packet->completion_tv.tv_usec))
|
|
|
|
break;
|
|
|
|
|
|
|
|
elapsed = (packet->completion_tv.tv_sec - packet->arrive_tv.tv_sec) * 1000000 +
|
|
|
|
(packet->completion_tv.tv_usec - packet->arrive_tv.tv_usec);
|
2006-12-06 13:31:58 +01:00
|
|
|
elapsed /= 1000;
|
2006-10-26 11:47:17 +02:00
|
|
|
|
|
|
|
xfree(packet->s.data);
|
2017-10-04 16:09:44 +02:00
|
|
|
rdpsnd_send_waveconfirm((packet->tick + elapsed) % 65536, packet->index);
|
2006-10-26 11:47:17 +02:00
|
|
|
queue_pending = (queue_pending + 1) % MAX_QUEUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
rdpsnd_queue_next_completion(void)
|
|
|
|
{
|
|
|
|
struct audio_packet *packet;
|
|
|
|
long remaining;
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
if (queue_pending == queue_lo)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
|
|
|
|
packet = &packet_queue[queue_pending];
|
|
|
|
|
|
|
|
remaining = (packet->completion_tv.tv_sec - now.tv_sec) * 1000000 +
|
|
|
|
(packet->completion_tv.tv_usec - now.tv_usec);
|
|
|
|
|
|
|
|
if (remaining < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return remaining;
|
|
|
|
}
|