rdpsnd.c revision d65680efa46fa49e8bf14e67b29b782510ff934c
/* -*- c-basic-offset: 8 -*-
rdesktop: A Remote Desktop Protocol client.
Sound Channel Process Functions
Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
Copyright (C) Matthew Chapman 2003-2007
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(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
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the General Public License version 2 (GPLv2) at this time for any software where
* a choice of GPL license versions is made available with the language indicating
* that GPLv2 or any later version may be used, or where a choice of which version
* of the GPL is applied is otherwise unspecified.
*/
#include <assert.h>
#include "rdesktop.h"
#include "rdpsnd.h"
#include "rdpsnd_dsp.h"
#define RDPSND_CLOSE 1
#define RDPSND_WRITE 2
#define RDPSND_SET_VOLUME 3
#define RDPSND_UNKNOWN4 4
#define RDPSND_COMPLETION 5
#define RDPSND_PING 6
#define RDPSND_NEGOTIATE 7
#define RDPSND_REC_NEGOTIATE 39
#define RDPSND_REC_START 40
#define RDPSND_REC_STOP 41
#define RDPSND_REC_DATA 42
#define RDPSND_REC_SET_VOLUME 43
#define RDPSND_FLAG_RECORD 0x00800000
#define MAX_FORMATS 10
#define MAX_QUEUE 50
static VCHANNEL *rdpsnd_channel;
static VCHANNEL *rdpsnddbg_channel;
static RD_BOOL device_open;
static RD_BOOL rec_device_open;
static unsigned int format_count;
static unsigned int current_format;
static unsigned int rec_format_count;
static unsigned int rec_current_format;
static char record_buffer[8192];
static uint32 record_buffer_size;
static uint8 packet_opcode;
void (*wave_out_play) (void);
static void rdpsnd_queue_init(void);
static void rdpsnd_queue_complete_pending(void);
static long rdpsnd_queue_next_completion(void);
static STREAM
{
STREAM s;
out_uint16_le(s, type);
out_uint16_le(s, size);
return s;
}
static void
{
}
static void
{
STREAM s;
out_uint16_le(s, tick);
out_uint8(s, packet_index);
out_uint8(s, 0);
s_mark_end(s);
rdpsnd_send(s);
DEBUG_SOUND(("RDPSND: -> RDPSND_COMPLETION(tick: %u, index: %u)\n",
(unsigned) tick, (unsigned) packet_index));
}
static void
rdpsnd_flush_record(void)
{
STREAM s;
unsigned int chunk_size;
char *data;
if (record_buffer_size == 0)
return;
/*
* Microsoft's RDP server keeps dropping chunks, so we need to
* transmit everything inside one channel fragment or we risk
* making the rdpsnd server go out of sync with the byte stream.
*/
while (record_buffer_size)
{
if (record_buffer_size < 1596)
else
chunk_size = 1596;
s_mark_end(s);
rdpsnd_send(s);
}
record_buffer_size = 0;
}
void
{
while (size)
{
else
#ifdef B_ENDIAN
#endif
if (record_buffer_size == sizeof(record_buffer))
}
}
static RD_BOOL
rdpsnd_auto_select(void)
{
if (!failed)
{
while (current_driver != NULL)
{
if (current_driver->wave_out_open())
{
return True;
}
}
warning("no working audio-driver found\n");
}
return False;
}
static void
{
int readcnt;
int discardcnt;
DEBUG_SOUND(("RDPSND: RDPSND_NEGOTIATE(formats: %d, pad: 0x%02x, version: %x)\n",
if (!current_driver)
{
}
format_count = 0;
{
for (i = 0; i < in_format_count; i++)
{
/* read in the buffer of unknown use */
discardcnt = 0;
{
}
{
format_count++;
if (format_count == MAX_FORMATS)
break;
}
}
}
for (i = 0; i < format_count; i++)
{
}
}
static void
{
out_uint16_le(out, 0);
}
static void
{
int readcnt;
int discardcnt;
DEBUG_SOUND(("RDPSND: RDPSND_REC_NEGOTIATE(formats: %d, version: %x)\n",
(int) in_format_count, (unsigned) version));
if (!current_driver)
&& current_driver->wave_in_open())
{
}
rec_format_count = 0;
{
for (i = 0; i < in_format_count; i++)
{
/* read in the buffer of unknown use */
discardcnt = 0;
{
}
{
if (rec_format_count == MAX_FORMATS)
break;
}
}
}
for (i = 0; i < rec_format_count; i++)
{
format = &rec_formats[i];
}
}
static void
{
static uint8 packet_index;
switch (opcode)
{
case RDPSND_WRITE:
in_uint16_le(s, tick);
in_uint16_le(s, format);
in_uint8(s, packet_index);
in_uint8s(s, 3);
DEBUG_SOUND(("RDPSND: RDPSND_WRITE(tick: %u, format: %u, index: %u, data: %u bytes)\n", (unsigned) tick, (unsigned) format, (unsigned) packet_index, (unsigned) s->size - 8));
if (format >= MAX_FORMATS)
{
error("RDPSND: Invalid format index\n");
break;
}
{
/*
* If we haven't selected a device by now, then either
* we've failed to find a working device, or the server
* is sending bogus RDPSND_WRITE.
*/
if (!current_driver)
{
break;
}
{
break;
}
{
device_open = False;
break;
}
device_open = True;
}
(s->p, s->end - s->p, current_driver,
return;
break;
case RDPSND_CLOSE:
DEBUG_SOUND(("RDPSND: RDPSND_CLOSE()\n"));
if (device_open)
device_open = False;
break;
case RDPSND_NEGOTIATE:
break;
case RDPSND_PING:
break;
case RDPSND_SET_VOLUME:
in_uint16_le(s, vol_left);
in_uint16_le(s, vol_right);
DEBUG_SOUND(("RDPSND: RDPSND_VOLUME(left: 0x%04x (%u %%), right: 0x%04x (%u %%))\n", (unsigned) vol_left, (unsigned) vol_left / 655, (unsigned) vol_right, (unsigned) vol_right / 655));
if (device_open)
break;
case RDPSND_REC_NEGOTIATE:
break;
case RDPSND_REC_START:
in_uint16_le(s, format);
if (format >= MAX_FORMATS)
{
error("RDPSND: Invalid format index\n");
break;
}
if (rec_device_open)
{
error("RDPSND: Multiple RDPSND_REC_START\n");
break;
}
if (!current_driver->wave_in_open())
break;
{
error("RDPSND: Device not accepting format\n");
break;
}
break;
case RDPSND_REC_STOP:
DEBUG_SOUND(("RDPSND: RDPSND_REC_STOP()\n"));
if (rec_device_open)
break;
case RDPSND_REC_SET_VOLUME:
in_uint16_le(s, vol_left);
in_uint16_le(s, vol_right);
DEBUG_SOUND(("RDPSND: RDPSND_REC_VOLUME(left: 0x%04x (%u %%), right: 0x%04x (%u %%))\n", (unsigned) vol_left, (unsigned) vol_left / 655, (unsigned) vol_right, (unsigned) vol_right / 655));
if (rec_device_open)
break;
default:
break;
}
}
static void
{
while (!s_check_end(s))
{
/* New packet */
{
if ((s->end - s->p) < 4)
{
error("RDPSND: Split at packet header. Things will go south from here...\n");
return;
}
in_uint8(s, packet_opcode);
in_uint16_le(s, len);
DEBUG_SOUND(("RDPSND: == Opcode %x Length: %d ==\n",
(int) packet_opcode, (int) len));
}
else
{
/* Microsoft's server is so broken it's not even funny... */
if (packet_opcode == RDPSND_WRITE)
{
{
DEBUG_SOUND(("RDPSND: Eating 4 bytes of %d bytes...\n",
len));
in_uint8s(s, 4);
len -= 4;
}
}
}
/* Packet fully assembled */
{
}
}
}
static RD_BOOL
{
#ifdef WITH_DEBUG_SOUND
#endif
return True;
}
static void
{
unsigned int pkglen;
char *buf;
/* str_handle_lines requires null terminated strings */
}
static void
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 */
#if defined(RDPSND_ALSA)
#endif
#if defined(RDPSND_SUN)
#endif
#if defined(RDPSND_OSS)
#endif
#if defined(RDPSND_SGI)
#endif
#if defined(RDPSND_LIBAO)
#endif
}
rdpsnd_init(char *optarg)
{
struct audio_driver *pos;
{
error("channel_register\n");
return False;
}
{
options++;
if (*options == ':')
{
*options = '\0';
options++;
}
if (*options == '\0')
}
if (!driver)
return True;
{
{
return True;
}
}
return False;
}
void
rdpsnd_show_help(void)
{
struct audio_driver *pos;
{
}
}
void
{
long next_pending;
if (device_open || rec_device_open)
if (next_pending >= 0)
{
long cur_timeout;
if (cur_timeout > next_pending)
{
}
}
}
void
{
if (device_open || rec_device_open)
}
static void
{
if (next_hi == queue_pending)
{
error("No space to queue audio packet\n");
return;
}
packet->s = *s;
}
struct audio_packet *
{
return &packet_queue[queue_lo];
}
rdpsnd_queue_empty(void)
{
}
static void
rdpsnd_queue_init(void)
{
}
void
rdpsnd_queue_next(unsigned long completed_in_us)
{
struct audio_packet *packet;
assert(!rdpsnd_queue_empty());
}
int
rdpsnd_queue_next_tick(void)
{
{
}
else
{
}
}
static void
{
long elapsed;
struct audio_packet *packet;
while (queue_pending != queue_lo)
{
break;
break;
elapsed /= 1000;
}
}
static long
{
struct audio_packet *packet;
long remaining;
if (queue_pending == queue_lo)
return -1;
if (remaining < 0)
return 0;
return remaining;
}