rdesktop/rdpsnd_libao.c
Pierre Ossman 139e42d9ef Restructure driver registration structures a bit so it is easier to add
new fields (and also reduce some memory usage/leaks).


git-svn-id: svn://svn.code.sf.net/p/rdesktop/code/trunk/rdesktop@1344 423420c4-83ab-492f-b58f-81f9feb106b5
2006-12-07 11:54:29 +00:00

203 lines
4.1 KiB
C

/* -*- c-basic-offset: 8 -*-
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
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006
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"
#include "rdpsnd.h"
#include "rdpsnd_dsp.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <ao/ao.h>
#include <sys/time.h>
#define WAVEOUTLEN 16
static ao_device *o_device = NULL;
static int default_driver;
static BOOL reopened;
static char *libao_device = NULL;
BOOL
libao_open(void)
{
ao_sample_format format;
ao_initialize();
if (libao_device)
{
default_driver = ao_driver_id(libao_device);
}
else
{
default_driver = ao_default_driver_id();
}
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_NATIVE;
o_device = ao_open_live(default_driver, &format, NULL);
if (o_device == NULL)
{
return False;
}
g_dsp_fd = 0;
reopened = True;
return True;
}
void
libao_close(void)
{
/* Ack all remaining packets */
while (!rdpsnd_queue_empty())
{
rdpsnd_queue_next(0);
}
if (o_device != NULL)
ao_close(o_device);
ao_shutdown();
}
BOOL
libao_set_format(WAVEFORMATEX * pwfx)
{
ao_sample_format format;
format.bits = pwfx->wBitsPerSample;
format.channels = pwfx->nChannels;
format.rate = 44100;
format.byte_format = AO_FMT_NATIVE;
if (o_device != NULL)
ao_close(o_device);
o_device = ao_open_live(default_driver, &format, NULL);
if (o_device == NULL)
{
return False;
}
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
{
return False;
}
reopened = True;
return True;
}
void
libao_play(void)
{
struct audio_packet *packet;
STREAM out;
int len;
static long prev_s, prev_us;
unsigned int duration;
struct timeval tv;
int next_tick;
if (reopened)
{
reopened = False;
gettimeofday(&tv, NULL);
prev_s = tv.tv_sec;
prev_us = tv.tv_usec;
}
if (rdpsnd_queue_empty())
{
g_dsp_busy = 0;
return;
}
packet = rdpsnd_queue_current_packet();
out = &packet->s;
next_tick = rdpsnd_queue_next_tick();
len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
ao_play(o_device, (char *) out->p, len);
out->p += len;
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)
{
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));
}
rdpsnd_queue_next(duration);
}
g_dsp_busy = 1;
return;
}
static struct audio_driver libao_driver = {
.name = "libao",
.description = "libao output driver, default device: system dependent",
.wave_out_open = libao_open,
.wave_out_close = libao_close,
.wave_out_format_supported = rdpsnd_dsp_resample_supported,
.wave_out_set_format = libao_set_format,
.wave_out_volume = rdpsnd_dsp_softvol_set,
.wave_out_play = libao_play,
.need_byteswap_on_be = 1,
.need_resampling = 1,
};
struct audio_driver *
libao_register(char *options)
{
if (options)
{
libao_device = xstrdup(options);
}
return &libao_driver;
}