rdpsnd_libao.c revision d65680efa46fa49e8bf14e67b29b782510ff934c
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync/* -*- c-basic-offset: 8 -*-
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync rdesktop: A Remote Desktop Protocol client.
1ce069685b24d243eb0464f46d4c56b250c64445vboxsync Sound Channel Process Functions - libao-driver
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync Copyright (C) Matthew Chapman 2003-2007
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2007
c58f1213e628a545081c70e26c6b67a841cff880vboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync This program is free software; you can redistribute it and/or modify
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync it under the terms of the GNU General Public License as published by
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync the Free Software Foundation; either version 2 of the License, or
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync (at your option) any later version.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync This program is distributed in the hope that it will be useful,
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync but WITHOUT ANY WARRANTY; without even the implied warranty of
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync GNU General Public License for more details.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync You should have received a copy of the GNU General Public License
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync along with this program; if not, write to the Free Software
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync*/
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync/*
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * Sun GPL Disclaimer: For the avoidance of doubt, except that if any license choice
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * other than GPL or LGPL is available it will apply instead, Sun elects to use only
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * the General Public License version 2 (GPLv2) at this time for any software where
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * a choice of GPL license versions is made available with the language indicating
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * that GPLv2 or any later version may be used, or where a choice of which version
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync * of the GPL is applied is otherwise unspecified.
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include "rdesktop.h"
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include "rdpsnd.h"
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include "rdpsnd_dsp.h"
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <unistd.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <fcntl.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <errno.h>
9ce5d949e4f1572d445a5c0aecabe9de8b672c99vboxsync#include <ao/ao.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#include <sys/time.h>
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync#define WAVEOUTLEN 16
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsyncstatic ao_device *o_device = NULL;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsyncstatic int default_driver;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncstatic RD_BOOL reopened;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncstatic char *libao_device = NULL;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid libao_play(void);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
ad27e1d5e48ca41245120c331cc88b50464813cevboxsyncvoid
4328e87247f4a96449677e199c7e99ef516fc1cevboxsynclibao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync /* We need to be called rather often... */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (o_device != NULL && !rdpsnd_queue_empty())
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync FD_SET(0, wfds);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid
4328e87247f4a96449677e199c7e99ef516fc1cevboxsynclibao_check_fds(fd_set * rfds, fd_set * wfds)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (o_device == NULL)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync return;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (!rdpsnd_queue_empty())
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_play();
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
376b92d26cc4fad78e813cf33afcc0784adc9b19vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsyncRD_BOOL
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsynclibao_open(void)
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync{
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync ao_sample_format format;
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync ao_initialize();
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync if (libao_device)
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync {
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync default_driver = ao_driver_id(libao_device);
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync }
376b92d26cc4fad78e813cf33afcc0784adc9b19vboxsync else
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync {
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync default_driver = ao_default_driver_id();
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync }
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync format.bits = 16;
89aedeb1d8af54aba6ae46dbbd256281315c1be6vboxsync format.channels = 2;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync format.rate = 44100;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync format.byte_format = AO_FMT_NATIVE;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync o_device = ao_open_live(default_driver, &format, NULL);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (o_device == NULL)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync return False;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync }
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync reopened = True;
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync return True;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncvoid
f409459bdd4c15cdb8d7fb6c6d54338cce9ac814vboxsynclibao_close(void)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync /* Ack all remaining packets */
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync while (!rdpsnd_queue_empty())
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync rdpsnd_queue_next(0);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync }
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (o_device != NULL)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync ao_close(o_device);
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync o_device = NULL;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync ao_shutdown();
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync}
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsyncRD_BOOL
4328e87247f4a96449677e199c7e99ef516fc1cevboxsynclibao_set_format(RD_WAVEFORMATEX * pwfx)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync{
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync ao_sample_format format;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync format.bits = pwfx->wBitsPerSample;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync format.channels = pwfx->nChannels;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync format.rate = 44100;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync format.byte_format = AO_FMT_NATIVE;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync if (o_device != NULL)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync ao_close(o_device);
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync o_device = ao_open_live(default_driver, &format, NULL);
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync if (o_device == NULL)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync {
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync return False;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync }
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync {
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync return False;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync }
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync reopened = True;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync return True;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync}
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsyncvoid
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsynclibao_play(void)
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync{
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync struct audio_packet *packet;
6e12ccc60ac657fb87e27b7a2b26e0a63bebe024vboxsync STREAM out;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync int len;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync static long prev_s, prev_us;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync unsigned int duration;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync struct timeval tv;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync int next_tick;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync if (reopened)
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync {
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync reopened = False;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync gettimeofday(&tv, NULL);
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync prev_s = tv.tv_sec;
9cb702c3a5fd2287c57c7c1e98a61ba9e357b4devboxsync prev_us = tv.tv_usec;
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync }
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync /* We shouldn't be called if the queue is empty, but still */
6fe1329154975472e055284198df7fa8e64dee3avboxsync if (rdpsnd_queue_empty())
6fe1329154975472e055284198df7fa8e64dee3avboxsync return;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync packet = rdpsnd_queue_current_packet();
6fe1329154975472e055284198df7fa8e64dee3avboxsync out = &packet->s;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync next_tick = rdpsnd_queue_next_tick();
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
6fe1329154975472e055284198df7fa8e64dee3avboxsync ao_play(o_device, (char *) out->p, len);
6fe1329154975472e055284198df7fa8e64dee3avboxsync out->p += len;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync gettimeofday(&tv, NULL);
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync duration = ((tv.tv_sec - prev_s) * 1000000 + (tv.tv_usec - prev_us)) / 1000;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync if (packet->tick > next_tick)
6fe1329154975472e055284198df7fa8e64dee3avboxsync next_tick += 65536;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync if ((out->p == out->end) || duration > next_tick - packet->tick + 500)
6fe1329154975472e055284198df7fa8e64dee3avboxsync {
6fe1329154975472e055284198df7fa8e64dee3avboxsync unsigned int delay_us;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
6fe1329154975472e055284198df7fa8e64dee3avboxsync prev_s = tv.tv_sec;
6fe1329154975472e055284198df7fa8e64dee3avboxsync prev_us = tv.tv_usec;
6fe1329154975472e055284198df7fa8e64dee3avboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync if (abs((next_tick - packet->tick) - duration) > 20)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync {
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DEBUG(("duration: %d, calc: %d, ", duration, next_tick - packet->tick));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync DEBUG(("last: %d, is: %d, should: %d\n", packet->tick,
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync (packet->tick + duration) % 65536, next_tick % 65536));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync delay_us = ((out->size / 4) * (1000000 / 44100));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync rdpsnd_queue_next(delay_us);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync }
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync}
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsyncstruct audio_driver *
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsynclibao_register(char *options)
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync{
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync static struct audio_driver libao_driver;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync memset(&libao_driver, 0, sizeof(libao_driver));
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.name = "libao";
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.description = "libao output driver, default device: system dependent";
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.add_fds = libao_add_fds;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.check_fds = libao_check_fds;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.wave_out_open = libao_open;
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync libao_driver.wave_out_close = libao_close;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_driver.wave_out_set_format = libao_set_format;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_driver.need_byteswap_on_be = 1;
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_driver.need_resampling = 1;
ae16af2d7d3c99d359094a7f19f5937efc2e66bdvboxsync
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync if (options)
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync {
4328e87247f4a96449677e199c7e99ef516fc1cevboxsync libao_device = xstrdup(options);
ae017640afff8b6cc50453182a4edf2eb0903a12vboxsync }
cdf129515a2b03bc9d122091ce7656d6e6934cc7vboxsync
7e837ad8d6aeb3f86520ea7adb61e4eb15f2087evboxsync return &libao_driver;
7e837ad8d6aeb3f86520ea7adb61e4eb15f2087evboxsync}
7e837ad8d6aeb3f86520ea7adb61e4eb15f2087evboxsync