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"
|
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;
|
|
|
|
static BOOL reopened;
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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_set_format(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-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
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
struct audio_driver *
|
|
|
|
libao_register(char *options)
|
|
|
|
{
|
2006-09-17 13:42:22 +02:00
|
|
|
static struct audio_driver libao_driver;
|
2006-09-18 11:34:34 +02:00
|
|
|
struct ao_info *libao_info;
|
2006-09-18 11:27:58 +02:00
|
|
|
static char description[101];
|
2006-09-17 13:42:22 +02:00
|
|
|
|
|
|
|
libao_driver.wave_out_write = rdpsnd_queue_write;
|
|
|
|
libao_driver.wave_out_open = libao_open;
|
|
|
|
libao_driver.wave_out_close = libao_close;
|
2006-10-01 14:16:50 +02:00
|
|
|
libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
|
2006-09-17 13:42:22 +02:00
|
|
|
libao_driver.wave_out_set_format = libao_set_format;
|
2006-09-17 16:41:16 +02:00
|
|
|
libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
2006-09-17 13:42:22 +02:00
|
|
|
libao_driver.wave_out_play = libao_play;
|
|
|
|
libao_driver.name = xstrdup("libao");
|
2006-09-18 11:27:58 +02:00
|
|
|
libao_driver.description = description;
|
2006-10-02 00:27:11 +02:00
|
|
|
libao_driver.need_byteswap_on_be = 1;
|
2006-10-01 16:03:43 +02:00
|
|
|
libao_driver.need_resampling = 1;
|
2006-09-17 13:42:22 +02:00
|
|
|
libao_driver.next = NULL;
|
|
|
|
|
2006-09-18 11:27:58 +02:00
|
|
|
ao_initialize();
|
2006-09-18 11:34:34 +02:00
|
|
|
|
|
|
|
libao_info = ao_driver_info(ao_default_driver_id());
|
|
|
|
|
|
|
|
if (libao_info)
|
|
|
|
{
|
|
|
|
snprintf(description, 100, "libao output driver, default device: %s",
|
2006-10-01 14:16:50 +02:00
|
|
|
libao_info->short_name);
|
2006-09-18 11:34:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(description, 100, "libao output driver, default device: none");
|
|
|
|
}
|
|
|
|
|
2006-09-18 11:27:58 +02:00
|
|
|
ao_shutdown();
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
libao_device = xstrdup(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &libao_driver;
|
|
|
|
}
|