2006-09-17 12:00:55 +02:00
|
|
|
/* -*- c-basic-offset: 8 -*-
|
|
|
|
rdesktop: A Remote Desktop Protocol client.
|
|
|
|
Sound Channel Process Functions - alsa-driver
|
2011-04-13 13:13:04 +02:00
|
|
|
Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 2003-2008
|
2011-04-13 13:28:19 +02:00
|
|
|
Copyright (C) GuoJunBo <guojunbo@ict.ac.cn> 2003
|
|
|
|
Copyright (C) Michael Gernoth <mike@zerfleddert.de> 2006-2008
|
2008-07-11 05:51:23 +02:00
|
|
|
Copyright 2006-2008 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2010-01-12 09:31:06 +01:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
2006-09-17 12:00:55 +02:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2010-01-12 09:31:06 +01:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2006-09-17 12:00:55 +02:00
|
|
|
(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
|
2010-01-12 09:31:06 +01:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-09-17 12:00:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
static struct pollfd pfds_out[32];
|
2017-10-20 12:13:21 +02:00
|
|
|
static size_t num_fds_out;
|
2007-01-02 17:36:51 +01:00
|
|
|
|
|
|
|
static struct pollfd pfds_in[32];
|
2017-10-20 12:13:21 +02:00
|
|
|
static size_t num_fds_in;
|
2007-01-02 17:36:51 +01:00
|
|
|
|
|
|
|
static snd_pcm_t *out_handle = NULL;
|
|
|
|
static snd_pcm_t *in_handle = NULL;
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
static RD_BOOL reopened;
|
2007-01-02 17:36:51 +01:00
|
|
|
|
|
|
|
static short samplewidth_out;
|
|
|
|
static int audiochannels_out;
|
|
|
|
static unsigned int rate_out;
|
|
|
|
|
|
|
|
static short samplewidth_in;
|
|
|
|
static int audiochannels_in;
|
|
|
|
static unsigned int rate_in;
|
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
static char *pcm_name;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2006-12-07 16:23:45 +01:00
|
|
|
void alsa_play(void);
|
2007-01-02 17:36:51 +01:00
|
|
|
void alsa_record(void);
|
2006-12-07 16:23:45 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
alsa_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
|
|
|
|
{
|
2017-10-20 11:05:52 +02:00
|
|
|
UNUSED(tv);
|
2006-12-07 16:23:45 +01:00
|
|
|
int err;
|
|
|
|
struct pollfd *f;
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (out_handle && !rdpsnd_queue_empty())
|
|
|
|
{
|
|
|
|
num_fds_out = snd_pcm_poll_descriptors_count(out_handle);
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (num_fds_out > sizeof(pfds_out) / sizeof(*pfds_out))
|
|
|
|
return;
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
err = snd_pcm_poll_descriptors(out_handle, pfds_out, num_fds_out);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
for (f = pfds_out; f < &pfds_out[num_fds_out]; f++)
|
|
|
|
{
|
|
|
|
if (f->events & POLLIN)
|
|
|
|
FD_SET(f->fd, rfds);
|
|
|
|
if (f->events & POLLOUT)
|
|
|
|
FD_SET(f->fd, wfds);
|
|
|
|
if (f->fd > *n && (f->events & (POLLIN | POLLOUT)))
|
|
|
|
*n = f->fd;
|
|
|
|
}
|
|
|
|
}
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (in_handle)
|
2006-12-07 16:23:45 +01:00
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
num_fds_in = snd_pcm_poll_descriptors_count(in_handle);
|
|
|
|
|
|
|
|
if (num_fds_in > sizeof(pfds_in) / sizeof(*pfds_in))
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = snd_pcm_poll_descriptors(in_handle, pfds_in, num_fds_in);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (f = pfds_in; f < &pfds_in[num_fds_in]; f++)
|
|
|
|
{
|
|
|
|
if (f->events & POLLIN)
|
|
|
|
FD_SET(f->fd, rfds);
|
|
|
|
if (f->events & POLLOUT)
|
|
|
|
FD_SET(f->fd, wfds);
|
|
|
|
if (f->fd > *n && (f->events & (POLLIN | POLLOUT)))
|
|
|
|
*n = f->fd;
|
|
|
|
}
|
2006-12-07 16:23:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alsa_check_fds(fd_set * rfds, fd_set * wfds)
|
|
|
|
{
|
|
|
|
struct pollfd *f;
|
|
|
|
int err;
|
|
|
|
unsigned short revents;
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (out_handle && !rdpsnd_queue_empty())
|
2006-12-07 16:23:45 +01:00
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
for (f = pfds_out; f < &pfds_out[num_fds_out]; f++)
|
2006-12-07 16:23:45 +01:00
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
f->revents = 0;
|
|
|
|
if (f->fd != -1)
|
|
|
|
{
|
|
|
|
/* Fixme: This doesn't properly deal with things like POLLHUP */
|
|
|
|
if (FD_ISSET(f->fd, rfds))
|
|
|
|
f->revents |= POLLIN;
|
|
|
|
if (FD_ISSET(f->fd, wfds))
|
|
|
|
f->revents |= POLLOUT;
|
|
|
|
}
|
2006-12-07 16:23:45 +01:00
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
err = snd_pcm_poll_descriptors_revents(out_handle, pfds_out, num_fds_out, &revents);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (revents & POLLOUT)
|
|
|
|
alsa_play();
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (in_handle)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
for (f = pfds_in; f < &pfds_in[num_fds_in]; f++)
|
|
|
|
{
|
|
|
|
f->revents = 0;
|
|
|
|
if (f->fd != -1)
|
|
|
|
{
|
|
|
|
/* Fixme: This doesn't properly deal with things like POLLHUP */
|
|
|
|
if (FD_ISSET(f->fd, rfds))
|
|
|
|
f->revents |= POLLIN;
|
|
|
|
if (FD_ISSET(f->fd, wfds))
|
|
|
|
f->revents |= POLLOUT;
|
|
|
|
}
|
|
|
|
}
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
err = snd_pcm_poll_descriptors_revents(in_handle, pfds_in, num_fds_in, &revents);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (revents & POLLIN)
|
|
|
|
alsa_record();
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
static RD_BOOL
|
2007-01-04 05:55:56 +01:00
|
|
|
alsa_set_format(snd_pcm_t * pcm, RD_WAVEFORMATEX * pwfx)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
|
|
|
snd_pcm_hw_params_t *hwparams = NULL;
|
|
|
|
int err;
|
|
|
|
unsigned int buffertime;
|
2007-01-02 17:36:51 +01:00
|
|
|
unsigned int rate;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_set_format(), snd_pcm_hw_params_malloc() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_any(pcm, hwparams)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_set_format(), snd_pcm_hw_params_any() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_access(pcm, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_set_format(), snd_pcm_hw_params_set_access() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pwfx->wBitsPerSample == 16)
|
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S16_LE)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_format() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_format(pcm, hwparams, SND_PCM_FORMAT_S8)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_format() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_rate_resample(pcm, hwparams, 1)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_rate_resample() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-10-26 11:47:17 +02:00
|
|
|
rate = pwfx->nSamplesPerSec;
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rate, 0)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_rate_near() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_channels(pcm, hwparams, pwfx->nChannels)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_channels() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
buffertime = 500000; /* microseconds */
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params_set_buffer_time_near(pcm, hwparams, &buffertime, 0)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_set_format(), snd_pcm_hw_params_set_buffer_time_near() failed: %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_hw_params(pcm, hwparams)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_set_format(), snd_pcm_hw_params(): %s",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(hwparams);
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if ((err = snd_pcm_prepare(pcm)) < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_set_format(), snd_pcm_prepare() failed: %s\n",
|
|
|
|
snd_strerror(err));
|
2006-09-17 12:00:55 +02:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
reopened = True;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
RD_BOOL
|
2007-01-02 17:36:51 +01:00
|
|
|
alsa_open_out(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_open(&out_handle, pcm_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_open_out(), snd_pcm_open() failed: %s",
|
|
|
|
snd_strerror(err));
|
2007-01-02 17:36:51 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
reopened = True;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alsa_close_out(void)
|
|
|
|
{
|
|
|
|
/* Ack all remaining packets */
|
|
|
|
while (!rdpsnd_queue_empty())
|
|
|
|
rdpsnd_queue_next(0);
|
|
|
|
|
|
|
|
if (out_handle)
|
|
|
|
{
|
|
|
|
snd_pcm_close(out_handle);
|
|
|
|
out_handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
RD_BOOL
|
2007-01-05 00:36:13 +01:00
|
|
|
alsa_format_supported(RD_WAVEFORMATEX * pwfx)
|
2007-01-02 17:36:51 +01:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
int err;
|
|
|
|
snd_pcm_hw_params_t *hwparams = NULL;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_malloc(&hwparams)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_format_supported(), snd_pcm_hw_params_malloc() failed: %s",
|
|
|
|
snd_strerror(err));
|
2007-01-02 17:36:51 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_any(pcm_handle, hwparams)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error,
|
|
|
|
"alsa_format_supported(), snd_pcm_hw_params_any() failed: %s\n",
|
|
|
|
snd_strerror(err));
|
2007-01-02 17:36:51 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
RD_BOOL
|
2007-01-05 00:36:13 +01:00
|
|
|
alsa_set_format_out(RD_WAVEFORMATEX * pwfx)
|
2007-01-02 17:36:51 +01:00
|
|
|
{
|
|
|
|
if (!alsa_set_format(out_handle, pwfx))
|
|
|
|
return False;
|
|
|
|
|
|
|
|
samplewidth_out = pwfx->wBitsPerSample / 8;
|
|
|
|
audiochannels_out = pwfx->nChannels;
|
|
|
|
rate_out = pwfx->nSamplesPerSec;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
2006-09-17 12:00:55 +02:00
|
|
|
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;
|
2019-04-09 13:35:46 +02:00
|
|
|
const unsigned char *data;
|
|
|
|
size_t before;
|
2006-09-17 12:00:55 +02:00
|
|
|
static long prev_s, prev_us;
|
2017-10-20 12:13:21 +02:00
|
|
|
int duration;
|
2006-09-17 12:00:55 +02:00
|
|
|
struct timeval tv;
|
|
|
|
int next_tick;
|
|
|
|
|
|
|
|
if (reopened)
|
|
|
|
{
|
|
|
|
reopened = False;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
prev_s = tv.tv_sec;
|
|
|
|
prev_us = tv.tv_usec;
|
|
|
|
}
|
|
|
|
|
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())
|
2006-09-17 12:00:55 +02:00
|
|
|
return;
|
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
packet = rdpsnd_queue_current_packet();
|
2019-04-09 13:25:50 +02:00
|
|
|
out = packet->s;
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2006-09-17 12:32:18 +02:00
|
|
|
next_tick = rdpsnd_queue_next_tick();
|
2006-09-17 12:00:55 +02:00
|
|
|
|
2019-04-09 13:35:46 +02:00
|
|
|
before = s_tell(out);
|
|
|
|
|
2019-04-09 13:13:35 +02:00
|
|
|
len = s_remaining(out) / (samplewidth_out * audiochannels_out);
|
2019-04-09 13:35:46 +02:00
|
|
|
len = MIN(len, MAX_FRAMES);
|
|
|
|
in_uint8p(out, data, len);
|
|
|
|
|
|
|
|
len = snd_pcm_writei(out_handle, data, len);
|
|
|
|
if (len < 0)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
2007-01-02 17:36:51 +01:00
|
|
|
snd_pcm_prepare(out_handle);
|
2006-09-17 12:00:55 +02:00
|
|
|
len = 0;
|
|
|
|
}
|
2019-04-09 13:35:46 +02:00
|
|
|
|
|
|
|
/* We might not have written everything */
|
|
|
|
s_seek(out, before + len * samplewidth_out * audiochannels_out);
|
2006-09-17 12:00:55 +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;
|
|
|
|
|
2019-04-09 13:35:46 +02:00
|
|
|
if (s_check_end(out) || duration > next_tick - packet->tick + 500)
|
2006-09-17 12:00:55 +02:00
|
|
|
{
|
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)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Debug,
|
|
|
|
"alsa_play(), duration=%d, calc=%d, last=%d, is=%d, should=%d",
|
|
|
|
duration, next_tick - packet->tick, packet->tick,
|
|
|
|
(packet->tick + duration) % 65536, next_tick % 65536);
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
if (snd_pcm_delay(out_handle, &delay_frames) < 0)
|
|
|
|
delay_frames = out->size / (samplewidth_out * audiochannels_out);
|
2006-10-26 11:47:17 +02:00
|
|
|
if (delay_frames < 0)
|
|
|
|
delay_frames = 0;
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
delay_us = delay_frames * (1000000 / rate_out);
|
2006-10-26 11:47:17 +02:00
|
|
|
|
|
|
|
rdpsnd_queue_next(delay_us);
|
2006-09-17 12:00:55 +02:00
|
|
|
}
|
|
|
|
}
|
2006-09-17 13:04:50 +02:00
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
RD_BOOL
|
2007-01-02 17:36:51 +01:00
|
|
|
alsa_open_in(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err =
|
|
|
|
snd_pcm_open(&in_handle, pcm_name, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_open_in(), snd_pcm_open() failed: %s",
|
|
|
|
snd_strerror(err));
|
2007-01-02 17:36:51 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alsa_close_in(void)
|
|
|
|
{
|
|
|
|
if (in_handle)
|
|
|
|
{
|
|
|
|
snd_pcm_close(in_handle);
|
|
|
|
in_handle = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 10:00:56 +01:00
|
|
|
RD_BOOL
|
2007-01-05 00:36:13 +01:00
|
|
|
alsa_set_format_in(RD_WAVEFORMATEX * pwfx)
|
2007-01-02 17:36:51 +01:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!alsa_set_format(in_handle, pwfx))
|
|
|
|
return False;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_start(in_handle)) < 0)
|
|
|
|
{
|
2017-01-26 14:19:40 +01:00
|
|
|
logger(Sound, Error, "alsa_open_in(), snd_pcm_start() failed: %s",
|
|
|
|
snd_strerror(err));
|
2007-01-02 17:36:51 +01:00
|
|
|
return False;
|
|
|
|
}
|
|
|
|
|
|
|
|
samplewidth_in = pwfx->wBitsPerSample / 8;
|
|
|
|
audiochannels_in = pwfx->nChannels;
|
|
|
|
rate_in = pwfx->nSamplesPerSec;
|
|
|
|
|
|
|
|
return True;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
alsa_record(void)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char buffer[32768];
|
|
|
|
|
|
|
|
len = snd_pcm_readi(in_handle, buffer,
|
|
|
|
sizeof(buffer) / (samplewidth_in * audiochannels_in));
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
snd_pcm_prepare(in_handle);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpsnd_record(buffer, len * samplewidth_in * audiochannels_in);
|
|
|
|
}
|
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
struct audio_driver *
|
|
|
|
alsa_register(char *options)
|
|
|
|
{
|
|
|
|
static struct audio_driver alsa_driver;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2007-01-02 12:39:56 +01:00
|
|
|
memset(&alsa_driver, 0, sizeof(alsa_driver));
|
|
|
|
|
|
|
|
alsa_driver.name = "alsa";
|
|
|
|
alsa_driver.description = "ALSA output driver, default device: " DEFAULTDEVICE;
|
2006-12-07 16:23:45 +01:00
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
alsa_driver.add_fds = alsa_add_fds;
|
|
|
|
alsa_driver.check_fds = alsa_check_fds;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
alsa_driver.wave_out_open = alsa_open_out;
|
|
|
|
alsa_driver.wave_out_close = alsa_close_out;
|
2006-12-24 14:53:23 +01:00
|
|
|
alsa_driver.wave_out_format_supported = alsa_format_supported;
|
2007-01-02 17:36:51 +01:00
|
|
|
alsa_driver.wave_out_set_format = alsa_set_format_out;
|
2006-12-24 14:53:23 +01:00
|
|
|
alsa_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
|
|
|
|
|
2007-01-02 17:36:51 +01:00
|
|
|
alsa_driver.wave_in_open = alsa_open_in;
|
|
|
|
alsa_driver.wave_in_close = alsa_close_in;
|
|
|
|
alsa_driver.wave_in_format_supported = alsa_format_supported;
|
|
|
|
alsa_driver.wave_in_set_format = alsa_set_format_in;
|
|
|
|
alsa_driver.wave_in_volume = NULL; /* FIXME */
|
|
|
|
|
2006-12-24 14:53:23 +01:00
|
|
|
alsa_driver.need_byteswap_on_be = 0;
|
|
|
|
alsa_driver.need_resampling = 0;
|
2006-12-07 12:54:29 +01:00
|
|
|
|
2006-09-17 13:04:50 +02:00
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
pcm_name = xstrdup(options);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-02 12:39:56 +01:00
|
|
|
pcm_name = DEFAULTDEVICE;
|
2006-09-17 13:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &alsa_driver;
|
|
|
|
}
|