2006-09-17 12:00:55 +02:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Sound Channel Process Functions - alsa-driver
|
|
|
|
Copyright (C) Matthew Chapman 2003
|
|
|
|
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
|
|
|
|
Copyright (C) Michael Gernoth mike@zerfleddert.de 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"
|
2006-09-17 12:32:18 +02:00
|
|
|
#include "rdpsnd.h"
|
2006-09-17 17:33:29 +02:00
|
|
|
#include "rdpsnd_dsp.h"
|
2006-09-17 12:00:55 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#define DEFAULTDEVICE "default"
|
|
|
|
#define MAX_FRAMES 32
|
|
|
|
|
|
|
|
static snd_pcm_t *pcm_handle = NULL;
|
|
|
|
static snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
|
|
|
|
static BOOL reopened;
|
|
|
|
static short samplewidth;
|
|
|
|
static int audiochannels;
|
2006-10-26 11:47:17 +02:00
|
|
|
static unsigned int rate;
|
2006-09-17 13:04:50 +02:00
|
|
|
static char *pcm_name;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
alsa_open(void)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_open(&pcm_handle, pcm_name, stream, 0)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_open: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dsp_fd = 0;
|
|
|
|
|
|
|
|
reopened = True;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-17 13:04:50 +02:00
|
|
|
alsa_close(void)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
/* Ack all remaining packets */
|
2006-09-17 12:32:18 +02:00
|
|
|
while (!rdpsnd_queue_empty())
|
2006-10-26 11:47:17 +02:00
|
|
|
rdpsnd_queue_next(0);
|
2006-09-17 12:00:55 +02:00
|
|
|
|
|
|
|
if (pcm_handle)
|
|
|
|
{
|
|
|
|
snd_pcm_close(pcm_handle);
|
2006-11-03 19:11:47 +01:00
|
|
|
pcm_handle = NULL;
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
alsa_format_supported(WAVEFORMATEX * pwfx)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int err;
|
|
|
|
snd_pcm_hw_params_t *hwparams = NULL;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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;
|
|
|
|
if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))
|
|
|
|
return False;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2006-09-17 13:04:50 +02:00
|
|
|
alsa_set_format(WAVEFORMATEX * pwfx)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
snd_pcm_hw_params_t *hwparams = NULL;
|
|
|
|
int err;
|
|
|
|
unsigned int buffertime;
|
|
|
|
|
|
|
|
samplewidth = pwfx->wBitsPerSample / 8;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_malloc: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_any: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err =
|
|
|
|
snd_pcm_hw_params_set_access(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_access: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwfx->wBitsPerSample == 16)
|
|
|
|
{
|
|
|
|
if ((err =
|
|
|
|
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((err =
|
|
|
|
snd_pcm_hw_params_set_format(pcm_handle, hwparams, SND_PCM_FORMAT_S8)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_format: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if ((err = snd_pcm_hw_params_set_rate_resample(pcm_handle, hwparams, 1)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_rate_resample: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
rate = pwfx->nSamplesPerSec;
|
|
|
|
if ((err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &rate, 0)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_rate_near: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
audiochannels = pwfx->nChannels;
|
|
|
|
if ((err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, pwfx->nChannels)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_channels: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buffertime = 500000; /* microseconds */
|
|
|
|
if ((err =
|
|
|
|
snd_pcm_hw_params_set_buffer_time_near(pcm_handle, hwparams, &buffertime, 0)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params_set_buffer_time_near: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params(pcm_handle, hwparams)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_hw_params: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
|
|
|
|
|
|
|
if ((err = snd_pcm_prepare(pcm_handle)) < 0)
|
|
|
|
{
|
|
|
|
error("snd_pcm_prepare: %s\n", snd_strerror(err));
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
reopened = True;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-09-17 13:04:50 +02:00
|
|
|
alsa_play(void)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
if (rdpsnd_queue_empty())
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
g_dsp_busy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
packet = rdpsnd_queue_current_packet();
|
2006-09-17 12:00:55 +02:00
|
|
|
out = &packet->s;
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
next_tick = rdpsnd_queue_next_tick();
|
2006-09-17 12:00:55 +02:00
|
|
|
|
|
|
|
len = (out->end - out->p) / (samplewidth * audiochannels);
|
|
|
|
if ((len = snd_pcm_writei(pcm_handle, out->p, ((MAX_FRAMES < len) ? MAX_FRAMES : len))) < 0)
|
|
|
|
{
|
|
|
|
snd_pcm_prepare(pcm_handle);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
out->p += (len * samplewidth * audiochannels);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2006-10-26 11:47:17 +02:00
|
|
|
snd_pcm_sframes_t delay_frames;
|
|
|
|
unsigned long delay_us;
|
|
|
|
|
2006-09-17 12:00:55 +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-10-26 11:47:17 +02:00
|
|
|
if (snd_pcm_delay(pcm_handle, &delay_frames) < 0)
|
|
|
|
delay_frames = out->size / (samplewidth * audiochannels);
|
|
|
|
if (delay_frames < 0)
|
|
|
|
delay_frames = 0;
|
|
|
|
|
|
|
|
delay_us = delay_frames * (1000000 / rate);
|
|
|
|
|
|
|
|
rdpsnd_queue_next(delay_us);
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_dsp_busy = 1;
|
|
|
|
return;
|
|
|
|
}
|
2006-09-17 13:04:50 +02:00
|
|
|
|
|
|
|
struct audio_driver *
|
|
|
|
alsa_register(char *options)
|
|
|
|
{
|
2006-09-17 13:42:22 +02:00
|
|
|
static struct audio_driver alsa_driver;
|
|
|
|
|
|
|
|
alsa_driver.wave_out_open = alsa_open;
|
|
|
|
alsa_driver.wave_out_close = alsa_close;
|
|
|
|
alsa_driver.wave_out_format_supported = alsa_format_supported;
|
|
|
|
alsa_driver.wave_out_set_format = alsa_set_format;
|
2006-09-17 17:33:29 +02:00
|
|
|
alsa_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
2006-09-17 13:42:22 +02:00
|
|
|
alsa_driver.wave_out_play = alsa_play;
|
|
|
|
alsa_driver.name = xstrdup("alsa");
|
|
|
|
alsa_driver.description = xstrdup("ALSA output driver, default device: " DEFAULTDEVICE);
|
2006-09-17 17:25:10 +02:00
|
|
|
alsa_driver.need_byteswap_on_be = 0;
|
2006-10-01 16:03:43 +02:00
|
|
|
alsa_driver.need_resampling = 0;
|
2006-09-17 13:42:22 +02:00
|
|
|
alsa_driver.next = NULL;
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
pcm_name = xstrdup(options);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pcm_name = xstrdup(DEFAULTDEVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &alsa_driver;
|
|
|
|
}
|