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
|
2007-01-04 06:39:39 +01:00
|
|
|
Copyright (C) Matthew Chapman 2003-2007
|
2005-03-08 04:33:36 +01:00
|
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
|
2007-01-04 06:39:39 +01:00
|
|
|
Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2007
|
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"
|
2006-09-17 16:41:16 +02:00
|
|
|
#include "rdpsnd_dsp.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-10-15 00:12:47 +02:00
|
|
|
#define WAVEOUTLEN 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;
|
2007-01-08 13:38:34 +01:00
|
|
|
static RD_BOOL reopened;
|
2006-09-17 13:04:50 +02:00
|
|
|
static char *libao_device = NULL;
|
2005-03-08 04:33:36 +01:00
|
|
|
|
2006-12-07 16:23:45 +01:00
|
|
|
void libao_play(void);
|
|
|
|
|
|
|
|
void
|
|
|
|
libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
|
|
|
|
{
|
2006-12-24 16:16:45 +01:00
|
|
|
/* We need to be called rather often... */
|
2007-10-30 14:57:31 +01:00
|
|
|
if (o_device != NULL && !rdpsnd_queue_empty())
|
|
|
|
FD_SET(0, wfds);
|
2006-12-07 16:23:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libao_check_fds(fd_set * rfds, fd_set * wfds)
|
|
|
|
{
|
|
|
|
if (o_device == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!rdpsnd_queue_empty())
|
|
|
|
libao_play();
|
|
|
|
}
|
|
|
|
|
2007-01-08 13:38:34 +01:00
|
|
|
RD_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
|
|
|
|
2006-09-18 11:27:58 +02:00
|
|
|
ao_initialize();
|
|
|
|
|
|
|
|
if (libao_device)
|
2006-09-17 13:04:50 +02:00
|
|
|
{
|
2006-09-18 11:27:58 +02:00
|
|
|
default_driver = ao_driver_id(libao_device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
default_driver = ao_default_driver_id();
|
2006-09-17 13:04:50 +02:00
|
|
|
}
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
format.bits = 16;
|
|
|
|
format.channels = 2;
|
|
|
|
format.rate = 44100;
|
2006-10-02 00:27:11 +02:00
|
|
|
format.byte_format = AO_FMT_NATIVE;
|
|
|
|
|
2005-03-08 04:33:36 +01:00
|
|
|
|
|
|
|
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-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-10-26 11:47:17 +02:00
|
|
|
rdpsnd_queue_next(0);
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (o_device != NULL)
|
|
|
|
ao_close(o_device);
|
2005-03-08 13:29:19 +01:00
|
|
|
|
2006-12-07 16:23:45 +01:00
|
|
|
o_device = NULL;
|
|
|
|
|
2005-03-08 04:33:36 +01:00
|
|
|
ao_shutdown();
|
|
|
|
}
|
|
|
|
|
2007-01-08 13:38:34 +01:00
|
|
|
RD_BOOL
|
2007-01-04 05:55:56 +01:00
|
|
|
libao_set_format(RD_WAVEFORMATEX * pwfx)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
ao_sample_format format;
|
|
|
|
|
|
|
|
format.bits = pwfx->wBitsPerSample;
|
|
|
|
format.channels = pwfx->nChannels;
|
|
|
|
format.rate = 44100;
|
2006-10-02 00:27:11 +02:00
|
|
|
format.byte_format = AO_FMT_NATIVE;
|
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-10-01 14:16:50 +02:00
|
|
|
if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
|
|
|
|
{
|
|
|
|
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_play(void)
|
2005-03-08 04:33:36 +01:00
|
|
|
{
|
|
|
|
struct audio_packet *packet;
|
|
|
|
STREAM out;
|
2006-10-01 14:16:50 +02:00
|
|
|
int len;
|
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-12-07 16:23:45 +01:00
|
|
|
/* We shouldn't be called if the queue is empty, but still */
|
2006-09-17 12:32:18 +02:00
|
|
|
if (rdpsnd_queue_empty())
|
2005-03-08 05:25:12 +01:00
|
|
|
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
|
|
|
|
2006-10-02 00:30:34 +02:00
|
|
|
len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
|
2006-10-01 14:16:50 +02:00
|
|
|
ao_play(o_device, (char *) out->p, len);
|
|
|
|
out->p += 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
|
|
|
{
|
2006-12-24 16:16:45 +01:00
|
|
|
unsigned int delay_us;
|
|
|
|
|
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-12-24 16:23:00 +01:00
|
|
|
delay_us = ((out->size / 4) * (1000000 / 44100));
|
2006-12-24 16:16:45 +01:00
|
|
|
|
|
|
|
rdpsnd_queue_next(delay_us);
|
2005-03-08 05:25:12 +01:00
|
|
|
}
|
2005-03-08 04:33:36 +01:00
|
|
|
}
|
2006-09-17 13:04:50 +02:00
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
struct audio_driver *
|
|
|
|
libao_register(char *options)
|
|
|
|
{
|
|
|
|
static struct audio_driver libao_driver;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2007-01-02 12:39:56 +01:00
|
|
|
memset(&libao_driver, 0, sizeof(libao_driver));
|
|
|
|
|
|
|
|
libao_driver.name = "libao";
|
|
|
|
libao_driver.description = "libao output driver, default device: system dependent";
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
libao_driver.add_fds = libao_add_fds;
|
|
|
|
libao_driver.check_fds = libao_check_fds;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
libao_driver.wave_out_open = libao_open;
|
|
|
|
libao_driver.wave_out_close = libao_close;
|
|
|
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
|
|
|
|
libao_driver.wave_out_set_format = libao_set_format;
|
|
|
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
|
|
|
|
|
|
|
libao_driver.need_byteswap_on_be = 1;
|
|
|
|
libao_driver.need_resampling = 1;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
libao_device = xstrdup(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &libao_driver;
|
|
|
|
}
|