2005-08-03 12:56:16 +02:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
2005-03-08 04:33:36 +01:00
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Sound Channel Process Functions - libao-driver
|
|
|
|
Copyright (C) Matthew Chapman 2003
|
|
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
|
2006-09-17 12:32:18 +02:00
|
|
|
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rdesktop.h"
|
2006-09-17 12:32:18 +02:00
|
|
|
#include "rdpsnd.h"
|
2005-03-08 04:33:36 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ao/ao.h>
|
2005-04-22 23:54:57 +02:00
|
|
|
#include <sys/time.h>
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-02-08 14:35:35 +01:00
|
|
|
#define WAVEOUTBUF 16
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
static ao_device *o_device = NULL;
|
|
|
|
static int default_driver;
|
|
|
|
static int samplerate;
|
|
|
|
static int audiochannels;
|
|
|
|
static BOOL reopened;
|
|
|
|
static short samplewidth;
|
2006-09-17 13:04:50 +02:00
|
|
|
static char *libao_device = NULL;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_open(void)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
ao_sample_format format;
|
2006-09-17 13:04:50 +02:00
|
|
|
static int warned = 0;
|
|
|
|
|
|
|
|
if (!warned && libao_device)
|
|
|
|
{
|
|
|
|
warning("device-options not supported for libao-driver\n");
|
|
|
|
warned = 1;
|
|
|
|
}
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
ao_initialize();
|
|
|
|
default_driver = ao_default_driver_id();
|
|
|
|
|
|
|
|
format.bits = 16;
|
|
|
|
format.channels = 2;
|
2006-07-10 01:41:07 +02:00
|
|
|
audiochannels = 2;
|
2005-03-08 04:33:36 +01:00
|
|
|
format.rate = 44100;
|
2006-07-10 01:41:07 +02:00
|
|
|
samplerate = 44100;
|
2005-03-08 04:33:36 +01:00
|
|
|
format.byte_format = AO_FMT_LITTLE;
|
|
|
|
|
|
|
|
o_device = ao_open_live(default_driver, &format, NULL);
|
|
|
|
if (o_device == NULL)
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dsp_fd = 0;
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_queue_init();
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
reopened = True;
|
2005-04-22 23:54:57 +02:00
|
|
|
|
2005-03-08 04:33:36 +01:00
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_close(void)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
/* Ack all remaining packets */
|
2006-09-17 12:32:18 +02:00
|
|
|
while (!rdpsnd_queue_empty())
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,
|
|
|
|
rdpsnd_queue_current_packet()->index);
|
|
|
|
rdpsnd_queue_next();
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (o_device != NULL)
|
|
|
|
ao_close(o_device);
|
2005-03-08 13:29:19 +01:00
|
|
|
|
2005-03-08 04:33:36 +01:00
|
|
|
ao_shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_format_supported(WAVEFORMATEX * pwfx)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
if (pwfx->wFormatTag != WAVE_FORMAT_PCM)
|
|
|
|
return False;
|
|
|
|
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))
|
|
|
|
return False;
|
|
|
|
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))
|
|
|
|
return False;
|
|
|
|
/* The only common denominator between libao output drivers is a sample-rate of
|
2005-03-08 05:25:12 +01:00
|
|
|
44100, we need to upsample 22050 to it */
|
|
|
|
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))
|
2005-03-08 04:33:36 +01:00
|
|
|
return False;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_set_format(WAVEFORMATEX * pwfx)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
ao_sample_format format;
|
|
|
|
|
|
|
|
format.bits = pwfx->wBitsPerSample;
|
|
|
|
format.channels = pwfx->nChannels;
|
2006-07-10 01:41:07 +02:00
|
|
|
audiochannels = pwfx->nChannels;
|
2005-03-08 04:33:36 +01:00
|
|
|
format.rate = 44100;
|
2006-07-10 01:41:07 +02:00
|
|
|
samplerate = pwfx->nSamplesPerSec;
|
2005-03-08 04:33:36 +01:00
|
|
|
format.byte_format = AO_FMT_LITTLE;
|
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
samplewidth = pwfx->wBitsPerSample / 8;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2005-03-08 13:16:22 +01:00
|
|
|
if (o_device != NULL)
|
2005-03-08 04:33:36 +01:00
|
|
|
ao_close(o_device);
|
|
|
|
|
|
|
|
o_device = ao_open_live(default_driver, &format, NULL);
|
|
|
|
if (o_device == NULL)
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
reopened = True;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_volume(uint16 left, uint16 right)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
2005-03-08 13:29:19 +01:00
|
|
|
warning("volume changes not supported with libao-output\n");
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-17 13:04:50 +02:00
|
|
|
libao_play(void)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
struct audio_packet *packet;
|
|
|
|
STREAM out;
|
2006-02-07 17:21:31 +01:00
|
|
|
char outbuf[WAVEOUTBUF];
|
2005-03-08 13:16:22 +01:00
|
|
|
int offset, len, i;
|
2005-04-22 23:54:57 +02:00
|
|
|
static long prev_s, prev_us;
|
|
|
|
unsigned int duration;
|
|
|
|
struct timeval tv;
|
|
|
|
int next_tick;
|
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
if (reopened)
|
2005-04-22 23:54:57 +02:00
|
|
|
{
|
2006-07-10 01:41:07 +02:00
|
|
|
reopened = False;
|
2005-04-22 23:54:57 +02:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
prev_s = tv.tv_sec;
|
|
|
|
prev_us = tv.tv_usec;
|
|
|
|
}
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
if (rdpsnd_queue_empty())
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
2005-03-08 05:25:12 +01:00
|
|
|
g_dsp_busy = 0;
|
|
|
|
return;
|
|
|
|
}
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
packet = rdpsnd_queue_current_packet();
|
2005-03-08 05:25:12 +01:00
|
|
|
out = &packet->s;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
next_tick = rdpsnd_queue_next_tick();
|
2005-04-22 23:54:57 +02:00
|
|
|
|
2005-03-08 05:25:12 +01:00
|
|
|
len = 0;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
if (samplerate == 22050)
|
2005-03-08 05:25:12 +01:00
|
|
|
{
|
|
|
|
/* Resample to 44100 */
|
2006-07-10 01:41:07 +02:00
|
|
|
for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - samplewidth))) && (out->p < out->end);
|
2005-03-08 13:16:22 +01:00
|
|
|
i++)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
2005-03-08 18:23:26 +01:00
|
|
|
/* On a stereo-channel we must make sure that left and right
|
|
|
|
does not get mixed up, so we need to expand the sample-
|
|
|
|
data with channels in mind: 1234 -> 12123434
|
|
|
|
If we have a mono-channel, we can expand the data by simply
|
|
|
|
doubling the sample-data: 1234 -> 11223344 */
|
2006-07-10 01:41:07 +02:00
|
|
|
if (audiochannels == 2)
|
|
|
|
offset = ((i * 2) - (i & 1)) * samplewidth;
|
2005-03-08 18:03:08 +01:00
|
|
|
else
|
2006-07-10 01:41:07 +02:00
|
|
|
offset = (i * 2) * samplewidth;
|
2005-03-08 05:25:12 +01:00
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
memcpy(&outbuf[offset], out->p, samplewidth);
|
|
|
|
memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth);
|
2005-03-08 18:03:08 +01:00
|
|
|
|
2006-07-10 01:41:07 +02:00
|
|
|
out->p += samplewidth;
|
|
|
|
len += 2 * samplewidth;
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
|
|
|
}
|
2005-03-08 05:25:12 +01:00
|
|
|
else
|
|
|
|
{
|
2005-03-08 13:09:20 +01:00
|
|
|
len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;
|
2005-03-08 13:16:22 +01:00
|
|
|
memcpy(outbuf, out->p, len);
|
2005-03-08 05:25:12 +01:00
|
|
|
out->p += len;
|
|
|
|
}
|
|
|
|
|
2006-02-07 17:21:31 +01:00
|
|
|
ao_play(o_device, outbuf, len);
|
2005-03-08 05:25:12 +01:00
|
|
|
|
2005-04-22 23:54:57 +02:00
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
|
|
|
duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
|
|
|
|
|
|
|
|
if (packet->tick > next_tick)
|
|
|
|
next_tick += 65536;
|
|
|
|
|
|
|
|
if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
|
2005-03-08 05:25:12 +01:00
|
|
|
{
|
2005-04-22 23:54:57 +02:00
|
|
|
prev_s = tv.tv_sec;
|
|
|
|
prev_us = tv.tv_usec;
|
|
|
|
|
|
|
|
if (abs((next_tick - packet->tick) - duration) > 20)
|
|
|
|
{
|
|
|
|
DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
|
|
|
|
DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
|
|
|
|
(packet->tick + duration) % 65536, next_tick % 65536));
|
|
|
|
}
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);
|
|
|
|
rdpsnd_queue_next();
|
2005-03-08 05:25:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_dsp_busy = 1;
|
|
|
|
return;
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
2006-09-17 13:04:50 +02:00
|
|
|
|
|
|
|
static struct audio_driver libao_driver = {
|
|
|
|
wave_out_write:rdpsnd_queue_write,
|
|
|
|
wave_out_open:libao_open,
|
|
|
|
wave_out_close:libao_close,
|
|
|
|
wave_out_format_supported:libao_format_supported,
|
|
|
|
wave_out_set_format:libao_set_format,
|
|
|
|
wave_out_volume:libao_volume,
|
|
|
|
wave_out_play:libao_play,
|
|
|
|
name:"libao",
|
|
|
|
description:"libao output driver",
|
|
|
|
next:NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct audio_driver *
|
|
|
|
libao_register(char *options)
|
|
|
|
{
|
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
libao_device = xstrdup(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &libao_driver;
|
|
|
|
}
|