2005-10-31 02:58:22 +08:00
|
|
|
/*
|
|
|
|
* QEMU ALSA audio driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005 Vassili Karpov (malc)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2019-05-23 22:35:07 +08:00
|
|
|
|
2016-01-19 01:33:52 +08:00
|
|
|
#include "qemu/osdep.h"
|
2005-10-31 02:58:22 +08:00
|
|
|
#include <alsa/asoundlib.h>
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 22:35:07 +08:00
|
|
|
#include "qemu/module.h"
|
2007-11-18 01:35:54 +08:00
|
|
|
#include "audio.h"
|
2015-06-12 20:33:06 +08:00
|
|
|
#include "trace.h"
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2009-07-17 09:57:04 +08:00
|
|
|
#pragma GCC diagnostic ignored "-Waddress"
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
#define AUDIO_CAP "alsa"
|
|
|
|
#include "audio_int.h"
|
|
|
|
|
2009-09-12 06:29:25 +08:00
|
|
|
struct pollhlp {
|
|
|
|
snd_pcm_t *handle;
|
|
|
|
struct pollfd *pfds;
|
|
|
|
int count;
|
2009-09-14 07:53:25 +08:00
|
|
|
int mask;
|
2019-08-19 07:06:55 +08:00
|
|
|
AudioState *s;
|
2009-09-12 06:29:25 +08:00
|
|
|
};
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
typedef struct ALSAVoiceOut {
|
|
|
|
HWVoiceOut hw;
|
|
|
|
snd_pcm_t *handle;
|
2009-09-12 06:29:25 +08:00
|
|
|
struct pollhlp pollhlp;
|
2019-03-09 06:34:16 +08:00
|
|
|
Audiodev *dev;
|
2005-10-31 02:58:22 +08:00
|
|
|
} ALSAVoiceOut;
|
|
|
|
|
|
|
|
typedef struct ALSAVoiceIn {
|
|
|
|
HWVoiceIn hw;
|
|
|
|
snd_pcm_t *handle;
|
2009-09-12 06:29:25 +08:00
|
|
|
struct pollhlp pollhlp;
|
2019-03-09 06:34:16 +08:00
|
|
|
Audiodev *dev;
|
2005-10-31 02:58:22 +08:00
|
|
|
} ALSAVoiceIn;
|
|
|
|
|
|
|
|
struct alsa_params_req {
|
2008-01-14 12:24:29 +08:00
|
|
|
int freq;
|
|
|
|
snd_pcm_format_t fmt;
|
|
|
|
int nchannels;
|
2005-10-31 02:58:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct alsa_params_obt {
|
|
|
|
int freq;
|
2019-03-09 06:34:13 +08:00
|
|
|
AudioFormat fmt;
|
2008-01-14 12:24:29 +08:00
|
|
|
int endianness;
|
2005-10-31 02:58:22 +08:00
|
|
|
int nchannels;
|
2005-11-06 02:55:28 +08:00
|
|
|
snd_pcm_uframes_t samples;
|
2005-10-31 02:58:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void GCC_FMT_ATTR (2, 3) alsa_logerr (int err, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GCC_FMT_ATTR (3, 4) alsa_logerr2 (
|
|
|
|
int err,
|
|
|
|
const char *typ,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
AUD_vlog (AUDIO_CAP, fmt, ap);
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
AUD_log (AUDIO_CAP, "Reason: %s\n", snd_strerror (err));
|
|
|
|
}
|
|
|
|
|
2009-09-14 07:51:48 +08:00
|
|
|
static void alsa_fini_poll (struct pollhlp *hlp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct pollfd *pfds = hlp->pfds;
|
|
|
|
|
|
|
|
if (pfds) {
|
|
|
|
for (i = 0; i < hlp->count; ++i) {
|
|
|
|
qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
|
|
|
|
}
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free (pfds);
|
2009-09-14 07:51:48 +08:00
|
|
|
}
|
|
|
|
hlp->pfds = NULL;
|
|
|
|
hlp->count = 0;
|
|
|
|
hlp->handle = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_anal_close1 (snd_pcm_t **handlep)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
int err = snd_pcm_close (*handlep);
|
|
|
|
if (err) {
|
|
|
|
alsa_logerr (err, "Failed to close PCM handle %p\n", *handlep);
|
|
|
|
}
|
|
|
|
*handlep = NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-14 07:51:48 +08:00
|
|
|
static void alsa_anal_close (snd_pcm_t **handlep, struct pollhlp *hlp)
|
|
|
|
{
|
|
|
|
alsa_fini_poll (hlp);
|
|
|
|
alsa_anal_close1 (handlep);
|
|
|
|
}
|
|
|
|
|
2009-09-12 06:29:25 +08:00
|
|
|
static int alsa_recover (snd_pcm_t *handle)
|
|
|
|
{
|
|
|
|
int err = snd_pcm_prepare (handle);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr (err, "Failed to prepare handle %p\n", handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alsa_resume (snd_pcm_t *handle)
|
|
|
|
{
|
|
|
|
int err = snd_pcm_resume (handle);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr (err, "Failed to resume handle %p\n", handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_poll_handler (void *opaque)
|
|
|
|
{
|
|
|
|
int err, count;
|
|
|
|
snd_pcm_state_t state;
|
|
|
|
struct pollhlp *hlp = opaque;
|
|
|
|
unsigned short revents;
|
|
|
|
|
|
|
|
count = poll (hlp->pfds, hlp->count, 0);
|
|
|
|
if (count < 0) {
|
|
|
|
dolog ("alsa_poll_handler: poll %s\n", strerror (errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: ALSA example uses initial count, not the one returned by
|
|
|
|
poll, correct? */
|
|
|
|
err = snd_pcm_poll_descriptors_revents (hlp->handle, hlp->pfds,
|
|
|
|
hlp->count, &revents);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr (err, "snd_pcm_poll_descriptors_revents");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-14 07:53:25 +08:00
|
|
|
if (!(revents & hlp->mask)) {
|
2015-06-12 20:33:06 +08:00
|
|
|
trace_alsa_revents(revents);
|
2009-09-12 06:29:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = snd_pcm_state (hlp->handle);
|
|
|
|
switch (state) {
|
2010-02-28 23:34:21 +08:00
|
|
|
case SND_PCM_STATE_SETUP:
|
|
|
|
alsa_recover (hlp->handle);
|
|
|
|
break;
|
|
|
|
|
2009-09-12 06:29:25 +08:00
|
|
|
case SND_PCM_STATE_XRUN:
|
|
|
|
alsa_recover (hlp->handle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_SUSPENDED:
|
|
|
|
alsa_resume (hlp->handle);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
2019-08-19 07:06:55 +08:00
|
|
|
audio_run(hlp->s, "alsa run (prepared)");
|
2009-09-12 06:29:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
2019-08-19 07:06:55 +08:00
|
|
|
audio_run(hlp->s, "alsa run (running)");
|
2009-09-12 06:29:25 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
dolog ("Unexpected state %d\n", state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-14 07:53:25 +08:00
|
|
|
static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
|
2009-09-12 06:29:25 +08:00
|
|
|
{
|
|
|
|
int i, count, err;
|
|
|
|
struct pollfd *pfds;
|
|
|
|
|
|
|
|
count = snd_pcm_poll_descriptors_count (handle);
|
|
|
|
if (count <= 0) {
|
|
|
|
dolog ("Could not initialize poll mode\n"
|
|
|
|
"Invalid number of poll descriptors %d\n", count);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pfds = audio_calloc ("alsa_poll_helper", count, sizeof (*pfds));
|
|
|
|
if (!pfds) {
|
|
|
|
dolog ("Could not initialize poll mode\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_poll_descriptors (handle, pfds, count);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr (err, "Could not initialize poll mode\n"
|
|
|
|
"Could not obtain poll descriptors\n");
|
2011-08-21 11:09:37 +08:00
|
|
|
g_free (pfds);
|
2009-09-12 06:29:25 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
if (pfds[i].events & POLLIN) {
|
2015-06-04 14:45:20 +08:00
|
|
|
qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
|
|
|
if (pfds[i].events & POLLOUT) {
|
2015-06-12 20:33:06 +08:00
|
|
|
trace_alsa_pollout(i, pfds[i].fd);
|
2015-06-04 14:45:20 +08:00
|
|
|
qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
2015-06-12 20:33:06 +08:00
|
|
|
trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
|
2009-09-12 06:29:25 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
hlp->pfds = pfds;
|
|
|
|
hlp->count = count;
|
|
|
|
hlp->handle = handle;
|
2009-09-14 07:53:25 +08:00
|
|
|
hlp->mask = mask;
|
2009-09-12 06:29:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int alsa_poll_out (HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
|
|
|
|
2009-09-14 07:53:25 +08:00
|
|
|
return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLOUT);
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int alsa_poll_in (HWVoiceIn *hw)
|
|
|
|
{
|
|
|
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
|
|
|
|
2009-09-14 07:53:25 +08:00
|
|
|
return alsa_poll_helper (alsa->handle, &alsa->pollhlp, POLLIN);
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
static snd_pcm_format_t aud_to_alsafmt (AudioFormat fmt, int endianness)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
switch (fmt) {
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_S8:
|
2005-10-31 02:58:22 +08:00
|
|
|
return SND_PCM_FORMAT_S8;
|
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_U8:
|
2005-10-31 02:58:22 +08:00
|
|
|
return SND_PCM_FORMAT_U8;
|
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_S16:
|
2011-01-09 00:53:30 +08:00
|
|
|
if (endianness) {
|
|
|
|
return SND_PCM_FORMAT_S16_BE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SND_PCM_FORMAT_S16_LE;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_U16:
|
2011-01-09 00:53:30 +08:00
|
|
|
if (endianness) {
|
|
|
|
return SND_PCM_FORMAT_U16_BE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SND_PCM_FORMAT_U16_LE;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_S32:
|
2011-01-09 00:53:30 +08:00
|
|
|
if (endianness) {
|
|
|
|
return SND_PCM_FORMAT_S32_BE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SND_PCM_FORMAT_S32_LE;
|
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
case AUDIO_FORMAT_U32:
|
2011-01-09 00:53:30 +08:00
|
|
|
if (endianness) {
|
|
|
|
return SND_PCM_FORMAT_U32_BE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return SND_PCM_FORMAT_U32_LE;
|
|
|
|
}
|
2007-02-18 06:19:29 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
default:
|
|
|
|
dolog ("Internal logic error: Bad audio format %d\n", fmt);
|
|
|
|
#ifdef DEBUG_AUDIO
|
|
|
|
abort ();
|
|
|
|
#endif
|
|
|
|
return SND_PCM_FORMAT_U8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:13 +08:00
|
|
|
static int alsa_to_audfmt (snd_pcm_format_t alsafmt, AudioFormat *fmt,
|
2008-01-14 12:24:29 +08:00
|
|
|
int *endianness)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
switch (alsafmt) {
|
|
|
|
case SND_PCM_FORMAT_S8:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_S8;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_U8:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_U8;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S16_LE:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_S16;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_U16_LE:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_U16;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S16_BE:
|
|
|
|
*endianness = 1;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_S16;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_U16_BE:
|
|
|
|
*endianness = 1;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_U16;
|
2005-10-31 02:58:22 +08:00
|
|
|
break;
|
|
|
|
|
2007-02-18 06:19:29 +08:00
|
|
|
case SND_PCM_FORMAT_S32_LE:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_S32;
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_U32_LE:
|
|
|
|
*endianness = 0;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_U32;
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_S32_BE:
|
|
|
|
*endianness = 1;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_S32;
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SND_PCM_FORMAT_U32_BE:
|
|
|
|
*endianness = 1;
|
2019-03-09 06:34:13 +08:00
|
|
|
*fmt = AUDIO_FORMAT_U32;
|
2007-02-18 06:19:29 +08:00
|
|
|
break;
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
default:
|
|
|
|
dolog ("Unrecognized audio format %d\n", alsafmt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_dump_info (struct alsa_params_req *req,
|
2010-04-21 19:40:23 +08:00
|
|
|
struct alsa_params_obt *obt,
|
2019-03-09 06:34:16 +08:00
|
|
|
snd_pcm_format_t obtfmt,
|
|
|
|
AudiodevAlsaPerDirectionOptions *apdo)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2019-03-09 06:34:16 +08:00
|
|
|
dolog("parameter | requested value | obtained value\n");
|
|
|
|
dolog("format | %10d | %10d\n", req->fmt, obtfmt);
|
|
|
|
dolog("channels | %10d | %10d\n",
|
|
|
|
req->nchannels, obt->nchannels);
|
|
|
|
dolog("frequency | %10d | %10d\n", req->freq, obt->freq);
|
|
|
|
dolog("============================================\n");
|
|
|
|
dolog("requested: buffer len %" PRId32 " period len %" PRId32 "\n",
|
|
|
|
apdo->buffer_length, apdo->period_length);
|
|
|
|
dolog("obtained: samples %ld\n", obt->samples);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
snd_pcm_sw_params_t *sw_params;
|
|
|
|
|
|
|
|
snd_pcm_sw_params_alloca (&sw_params);
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params_current (handle, sw_params);
|
|
|
|
if (err < 0) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Could not fully initialize DAC\n");
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa_logerr (err, "Failed to get current software parameters\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, threshold);
|
|
|
|
if (err < 0) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Could not fully initialize DAC\n");
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa_logerr (err, "Failed to set software threshold to %ld\n",
|
|
|
|
threshold);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_sw_params (handle, sw_params);
|
|
|
|
if (err < 0) {
|
2005-11-06 02:55:28 +08:00
|
|
|
dolog ("Could not fully initialize DAC\n");
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa_logerr (err, "Failed to set software parameters\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
static int alsa_open(bool in, struct alsa_params_req *req,
|
|
|
|
struct alsa_params_obt *obt, snd_pcm_t **handlep,
|
|
|
|
Audiodev *dev)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2019-03-09 06:34:16 +08:00
|
|
|
AudiodevAlsaOptions *aopts = &dev->u.alsa;
|
|
|
|
AudiodevAlsaPerDirectionOptions *apdo = in ? aopts->in : aopts->out;
|
2005-10-31 02:58:22 +08:00
|
|
|
snd_pcm_t *handle;
|
|
|
|
snd_pcm_hw_params_t *hw_params;
|
2007-12-16 11:02:09 +08:00
|
|
|
int err;
|
|
|
|
unsigned int freq, nchannels;
|
2019-03-09 06:34:16 +08:00
|
|
|
const char *pcm_name = apdo->has_dev ? apdo->dev : "default";
|
2005-10-31 02:58:22 +08:00
|
|
|
snd_pcm_uframes_t obt_buffer_size;
|
|
|
|
const char *typ = in ? "ADC" : "DAC";
|
2008-01-14 12:24:29 +08:00
|
|
|
snd_pcm_format_t obtfmt;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
|
|
|
freq = req->freq;
|
|
|
|
nchannels = req->nchannels;
|
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca (&hw_params);
|
|
|
|
|
|
|
|
err = snd_pcm_open (
|
|
|
|
&handle,
|
|
|
|
pcm_name,
|
|
|
|
in ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
|
|
|
|
SND_PCM_NONBLOCK
|
|
|
|
);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to open `%s':\n", pcm_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_any (handle, hw_params);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to initialize hardware parameters\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_access (
|
|
|
|
handle,
|
|
|
|
hw_params,
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED
|
|
|
|
);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to set access type\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
|
2015-06-12 20:33:06 +08:00
|
|
|
if (err < 0) {
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &freq, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to set frequency %d\n", req->freq);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_set_channels_near (
|
|
|
|
handle,
|
|
|
|
hw_params,
|
|
|
|
&nchannels
|
|
|
|
);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to set number of channels %d\n",
|
|
|
|
req->nchannels);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
if (apdo->buffer_length) {
|
|
|
|
int dir = 0;
|
|
|
|
unsigned int btime = apdo->buffer_length;
|
2008-06-22 04:09:32 +08:00
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
err = snd_pcm_hw_params_set_buffer_time_near(
|
|
|
|
handle, hw_params, &btime, &dir);
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2008-06-22 01:14:57 +08:00
|
|
|
if (err < 0) {
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa_logerr2(err, typ, "Failed to set buffer time to %" PRId32 "\n",
|
|
|
|
apdo->buffer_length);
|
2008-06-22 01:14:57 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2008-06-22 04:09:32 +08:00
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
if (apdo->has_buffer_length && btime != apdo->buffer_length) {
|
|
|
|
dolog("Requested buffer time %" PRId32
|
|
|
|
" was rejected, using %u\n", apdo->buffer_length, btime);
|
|
|
|
}
|
2008-06-22 01:14:57 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
if (apdo->period_length) {
|
|
|
|
int dir = 0;
|
|
|
|
unsigned int ptime = apdo->period_length;
|
2008-06-22 04:09:32 +08:00
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
err = snd_pcm_hw_params_set_period_time_near(handle, hw_params, &ptime,
|
|
|
|
&dir);
|
2008-06-22 01:14:57 +08:00
|
|
|
|
|
|
|
if (err < 0) {
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa_logerr2(err, typ, "Failed to set period time to %" PRId32 "\n",
|
|
|
|
apdo->period_length);
|
2008-06-22 01:14:57 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2008-06-22 04:09:32 +08:00
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
if (apdo->has_period_length && ptime != apdo->period_length) {
|
|
|
|
dolog("Requested period time %" PRId32 " was rejected, using %d\n",
|
|
|
|
apdo->period_length, ptime);
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params (handle, hw_params);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to apply audio parameters\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snd_pcm_hw_params_get_buffer_size (hw_params, &obt_buffer_size);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to get buffer size\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2008-01-14 12:24:29 +08:00
|
|
|
err = snd_pcm_hw_params_get_format (hw_params, &obtfmt);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr2 (err, typ, "Failed to get format\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alsa_to_audfmt (obtfmt, &obt->fmt, &obt->endianness)) {
|
|
|
|
dolog ("Invalid format was returned %d\n", obtfmt);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
err = snd_pcm_prepare (handle);
|
|
|
|
if (err < 0) {
|
2005-11-06 02:55:28 +08:00
|
|
|
alsa_logerr2 (err, typ, "Could not prepare handle %p\n", handle);
|
2005-10-31 02:58:22 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
if (!in && aopts->has_threshold && aopts->threshold) {
|
|
|
|
struct audsettings as = { .freq = freq };
|
|
|
|
alsa_set_threshold(
|
|
|
|
handle,
|
|
|
|
audio_buffer_frames(qapi_AudiodevAlsaPerDirectionOptions_base(apdo),
|
|
|
|
&as, aopts->threshold));
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
obt->nchannels = nchannels;
|
|
|
|
obt->freq = freq;
|
2005-11-06 02:55:28 +08:00
|
|
|
obt->samples = obt_buffer_size;
|
2008-01-14 12:24:29 +08:00
|
|
|
|
2005-10-31 02:58:22 +08:00
|
|
|
*handlep = handle;
|
|
|
|
|
2015-06-12 20:33:06 +08:00
|
|
|
if (obtfmt != req->fmt ||
|
2008-01-14 12:24:29 +08:00
|
|
|
obt->nchannels != req->nchannels ||
|
2015-06-12 20:33:06 +08:00
|
|
|
obt->freq != req->freq) {
|
2010-02-27 05:39:46 +08:00
|
|
|
dolog ("Audio parameters for %s\n", typ);
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa_dump_info(req, obt, obtfmt, apdo);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa_dump_info(req, obt, obtfmt, pdo);
|
2005-10-31 02:58:22 +08:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2009-09-14 07:51:48 +08:00
|
|
|
alsa_anal_close1 (&handle);
|
2005-10-31 02:58:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len)
|
2005-11-21 00:24:34 +08:00
|
|
|
{
|
2019-09-20 05:24:10 +08:00
|
|
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
|
|
|
size_t pos = 0;
|
2019-10-14 03:58:02 +08:00
|
|
|
size_t len_frames = len / hw->info.bytes_per_frame;
|
2019-09-20 05:24:10 +08:00
|
|
|
|
|
|
|
while (len_frames) {
|
|
|
|
char *src = advance(buf, pos);
|
|
|
|
snd_pcm_sframes_t written;
|
|
|
|
|
|
|
|
written = snd_pcm_writei(alsa->handle, src, len_frames);
|
|
|
|
|
|
|
|
if (written <= 0) {
|
|
|
|
switch (written) {
|
|
|
|
case 0:
|
|
|
|
trace_alsa_wrote_zero(len_frames);
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
case -EPIPE:
|
|
|
|
if (alsa_recover(alsa->handle)) {
|
|
|
|
alsa_logerr(written, "Failed to write %zu frames\n",
|
|
|
|
len_frames);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
trace_alsa_xrun_out();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case -ESTRPIPE:
|
|
|
|
/*
|
|
|
|
* stream is suspended and waiting for an application
|
|
|
|
* recovery
|
|
|
|
*/
|
|
|
|
if (alsa_resume(alsa->handle)) {
|
|
|
|
alsa_logerr(written, "Failed to write %zu frames\n",
|
|
|
|
len_frames);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
trace_alsa_resume_out();
|
|
|
|
continue;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
case -EAGAIN:
|
|
|
|
return pos;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
default:
|
|
|
|
alsa_logerr(written, "Failed to write %zu frames from %p\n",
|
|
|
|
len, src);
|
|
|
|
return pos;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2005-11-11 08:06:10 +08:00
|
|
|
}
|
2009-09-18 14:59:50 +08:00
|
|
|
|
2019-10-14 03:58:02 +08:00
|
|
|
pos += written * hw->info.bytes_per_frame;
|
2019-09-20 05:24:10 +08:00
|
|
|
if (written < len_frames) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len_frames -= written;
|
2009-09-18 14:59:50 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
return pos;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_fini_out (HWVoiceOut *hw)
|
|
|
|
{
|
|
|
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
|
|
|
|
|
|
|
ldebug ("alsa_fini\n");
|
2009-09-14 07:51:48 +08:00
|
|
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2015-06-04 05:03:47 +08:00
|
|
|
static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
|
|
|
|
void *drv_opaque)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
|
|
|
struct alsa_params_req req;
|
|
|
|
struct alsa_params_obt obt;
|
|
|
|
snd_pcm_t *handle;
|
2008-12-04 06:48:44 +08:00
|
|
|
struct audsettings obt_as;
|
2019-03-09 06:34:16 +08:00
|
|
|
Audiodev *dev = drv_opaque;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2011-01-09 00:53:30 +08:00
|
|
|
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
2005-11-06 02:55:28 +08:00
|
|
|
req.freq = as->freq;
|
|
|
|
req.nchannels = as->nchannels;
|
2019-03-09 06:34:16 +08:00
|
|
|
|
|
|
|
if (alsa_open(0, &req, &obt, &handle, dev)) {
|
2005-10-31 02:58:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
obt_as.freq = obt.freq;
|
|
|
|
obt_as.nchannels = obt.nchannels;
|
2008-01-14 12:24:29 +08:00
|
|
|
obt_as.fmt = obt.fmt;
|
|
|
|
obt_as.endianness = obt.endianness;
|
2005-11-06 02:55:28 +08:00
|
|
|
|
2006-07-05 05:47:22 +08:00
|
|
|
audio_pcm_init_info (&hw->info, &obt_as);
|
2005-11-06 02:55:28 +08:00
|
|
|
hw->samples = obt.samples;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-08-19 07:06:55 +08:00
|
|
|
alsa->pollhlp.s = hw->s;
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa->handle = handle;
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa->dev = dev;
|
2005-10-31 02:58:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-10-18 02:17:19 +08:00
|
|
|
#define VOICE_CTL_PAUSE 0
|
|
|
|
#define VOICE_CTL_PREPARE 1
|
|
|
|
#define VOICE_CTL_START 2
|
|
|
|
|
|
|
|
static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
int err;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2010-10-18 02:17:19 +08:00
|
|
|
if (ctl == VOICE_CTL_PAUSE) {
|
2005-11-21 00:24:34 +08:00
|
|
|
err = snd_pcm_drop (handle);
|
|
|
|
if (err < 0) {
|
2005-11-21 02:53:05 +08:00
|
|
|
alsa_logerr (err, "Could not stop %s\n", typ);
|
2005-11-21 00:24:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = snd_pcm_prepare (handle);
|
|
|
|
if (err < 0) {
|
2005-11-21 02:53:05 +08:00
|
|
|
alsa_logerr (err, "Could not prepare handle for %s\n", typ);
|
2005-11-21 00:24:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-18 02:17:19 +08:00
|
|
|
if (ctl == VOICE_CTL_START) {
|
|
|
|
err = snd_pcm_start(handle);
|
|
|
|
if (err < 0) {
|
|
|
|
alsa_logerr (err, "Could not start handle for %s\n", typ);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2005-11-21 00:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
static void alsa_enable_out(HWVoiceOut *hw, bool enable)
|
2005-11-21 00:24:34 +08:00
|
|
|
{
|
2005-10-31 02:58:22 +08:00
|
|
|
ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
|
2019-03-09 06:34:16 +08:00
|
|
|
AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
if (enable) {
|
|
|
|
bool poll_mode = apdo->try_poll;
|
2009-10-02 06:37:40 +08:00
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
ldebug("enabling voice\n");
|
|
|
|
if (poll_mode && alsa_poll_out(hw)) {
|
|
|
|
poll_mode = 0;
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
2019-09-20 05:24:22 +08:00
|
|
|
hw->poll_mode = poll_mode;
|
|
|
|
alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PREPARE);
|
|
|
|
} else {
|
|
|
|
ldebug("disabling voice\n");
|
2010-10-18 01:28:14 +08:00
|
|
|
if (hw->poll_mode) {
|
|
|
|
hw->poll_mode = 0;
|
2019-09-20 05:24:22 +08:00
|
|
|
alsa_fini_poll(&alsa->pollhlp);
|
2010-10-18 01:28:14 +08:00
|
|
|
}
|
2019-09-20 05:24:22 +08:00
|
|
|
alsa_voice_ctl(alsa->handle, "playback", VOICE_CTL_PAUSE);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-04 05:03:47 +08:00
|
|
|
static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
|
|
|
struct alsa_params_req req;
|
|
|
|
struct alsa_params_obt obt;
|
|
|
|
snd_pcm_t *handle;
|
2008-12-04 06:48:44 +08:00
|
|
|
struct audsettings obt_as;
|
2019-03-09 06:34:16 +08:00
|
|
|
Audiodev *dev = drv_opaque;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2011-01-09 00:53:30 +08:00
|
|
|
req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
|
2005-11-06 02:55:28 +08:00
|
|
|
req.freq = as->freq;
|
|
|
|
req.nchannels = as->nchannels;
|
2019-03-09 06:34:16 +08:00
|
|
|
|
|
|
|
if (alsa_open(1, &req, &obt, &handle, dev)) {
|
2005-10-31 02:58:22 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-06 02:55:28 +08:00
|
|
|
obt_as.freq = obt.freq;
|
|
|
|
obt_as.nchannels = obt.nchannels;
|
2008-01-14 12:24:29 +08:00
|
|
|
obt_as.fmt = obt.fmt;
|
|
|
|
obt_as.endianness = obt.endianness;
|
2005-11-06 02:55:28 +08:00
|
|
|
|
2006-07-05 05:47:22 +08:00
|
|
|
audio_pcm_init_info (&hw->info, &obt_as);
|
2005-11-06 02:55:28 +08:00
|
|
|
hw->samples = obt.samples;
|
|
|
|
|
2019-08-19 07:06:55 +08:00
|
|
|
alsa->pollhlp.s = hw->s;
|
2005-10-31 02:58:22 +08:00
|
|
|
alsa->handle = handle;
|
2019-03-09 06:34:16 +08:00
|
|
|
alsa->dev = dev;
|
2005-10-31 02:58:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_fini_in (HWVoiceIn *hw)
|
|
|
|
{
|
|
|
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
|
|
|
|
2009-09-14 07:51:48 +08:00
|
|
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
|
|
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
2019-09-20 05:24:10 +08:00
|
|
|
size_t pos = 0;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
while (len) {
|
|
|
|
void *dst = advance(buf, pos);
|
|
|
|
snd_pcm_sframes_t nread;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-10-14 03:58:02 +08:00
|
|
|
nread = snd_pcm_readi(
|
|
|
|
alsa->handle, dst, len / hw->info.bytes_per_frame);
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
if (nread <= 0) {
|
|
|
|
switch (nread) {
|
|
|
|
case 0:
|
|
|
|
trace_alsa_read_zero(len);
|
|
|
|
return pos;;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
case -EPIPE:
|
|
|
|
if (alsa_recover(alsa->handle)) {
|
|
|
|
alsa_logerr(nread, "Failed to read %zu frames\n", len);
|
|
|
|
return pos;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2019-09-20 05:24:10 +08:00
|
|
|
trace_alsa_xrun_in();
|
|
|
|
continue;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
case -EAGAIN:
|
|
|
|
return pos;
|
2005-10-31 02:58:22 +08:00
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
default:
|
|
|
|
alsa_logerr(nread, "Failed to read %zu frames to %p\n",
|
|
|
|
len, dst);
|
|
|
|
return pos;;
|
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
2019-09-20 05:24:10 +08:00
|
|
|
|
2019-10-14 03:58:02 +08:00
|
|
|
pos += nread * hw->info.bytes_per_frame;
|
|
|
|
len -= nread * hw->info.bytes_per_frame;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:10 +08:00
|
|
|
return pos;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
static void alsa_enable_in(HWVoiceIn *hw, bool enable)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2005-11-21 00:24:34 +08:00
|
|
|
ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
|
2019-03-09 06:34:16 +08:00
|
|
|
AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
if (enable) {
|
|
|
|
bool poll_mode = apdo->try_poll;
|
2009-10-03 07:30:06 +08:00
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
ldebug("enabling voice\n");
|
|
|
|
if (poll_mode && alsa_poll_in(hw)) {
|
|
|
|
poll_mode = 0;
|
2009-10-03 07:30:06 +08:00
|
|
|
}
|
2019-09-20 05:24:22 +08:00
|
|
|
hw->poll_mode = poll_mode;
|
2005-11-21 00:24:34 +08:00
|
|
|
|
2019-09-20 05:24:22 +08:00
|
|
|
alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_START);
|
|
|
|
} else {
|
2005-11-21 00:24:34 +08:00
|
|
|
ldebug ("disabling voice\n");
|
2009-09-12 06:29:25 +08:00
|
|
|
if (hw->poll_mode) {
|
|
|
|
hw->poll_mode = 0;
|
2019-09-20 05:24:22 +08:00
|
|
|
alsa_fini_poll(&alsa->pollhlp);
|
2009-09-12 06:29:25 +08:00
|
|
|
}
|
2019-09-20 05:24:22 +08:00
|
|
|
alsa_voice_ctl(alsa->handle, "capture", VOICE_CTL_PAUSE);
|
2005-11-21 00:24:34 +08:00
|
|
|
}
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
2019-03-09 06:34:16 +08:00
|
|
|
static void alsa_init_per_direction(AudiodevAlsaPerDirectionOptions *apdo)
|
|
|
|
{
|
|
|
|
if (!apdo->has_try_poll) {
|
|
|
|
apdo->try_poll = true;
|
|
|
|
apdo->has_try_poll = true;
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 05:03:48 +08:00
|
|
|
|
2019-03-09 06:34:15 +08:00
|
|
|
static void *alsa_audio_init(Audiodev *dev)
|
2005-10-31 02:58:22 +08:00
|
|
|
{
|
2019-03-09 06:34:16 +08:00
|
|
|
AudiodevAlsaOptions *aopts;
|
|
|
|
assert(dev->driver == AUDIODEV_DRIVER_ALSA);
|
|
|
|
|
|
|
|
aopts = &dev->u.alsa;
|
|
|
|
alsa_init_per_direction(aopts->in);
|
|
|
|
alsa_init_per_direction(aopts->out);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* need to define them, as otherwise alsa produces no sound
|
|
|
|
* doesn't set has_* so alsa_open can identify it wasn't set by the user
|
|
|
|
*/
|
|
|
|
if (!dev->u.alsa.out->has_period_length) {
|
|
|
|
/* 1024 frames assuming 44100Hz */
|
|
|
|
dev->u.alsa.out->period_length = 1024 * 1000000 / 44100;
|
|
|
|
}
|
|
|
|
if (!dev->u.alsa.out->has_buffer_length) {
|
|
|
|
/* 4096 frames assuming 44100Hz */
|
|
|
|
dev->u.alsa.out->buffer_length = 4096ll * 1000000 / 44100;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OptsVisitor sets unspecified optional fields to zero, but do not depend
|
|
|
|
* on it...
|
|
|
|
*/
|
|
|
|
if (!dev->u.alsa.in->has_period_length) {
|
|
|
|
dev->u.alsa.in->period_length = 0;
|
|
|
|
}
|
|
|
|
if (!dev->u.alsa.in->has_buffer_length) {
|
|
|
|
dev->u.alsa.in->buffer_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
2005-10-31 02:58:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void alsa_audio_fini (void *opaque)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-07 02:08:30 +08:00
|
|
|
static struct audio_pcm_ops alsa_pcm_ops = {
|
2009-08-11 08:31:15 +08:00
|
|
|
.init_out = alsa_init_out,
|
|
|
|
.fini_out = alsa_fini_out,
|
2019-09-20 05:24:10 +08:00
|
|
|
.write = alsa_write,
|
2019-09-20 05:24:22 +08:00
|
|
|
.enable_out = alsa_enable_out,
|
2009-08-11 08:31:15 +08:00
|
|
|
|
|
|
|
.init_in = alsa_init_in,
|
|
|
|
.fini_in = alsa_fini_in,
|
2019-09-20 05:24:10 +08:00
|
|
|
.read = alsa_read,
|
2019-09-20 05:24:22 +08:00
|
|
|
.enable_in = alsa_enable_in,
|
2005-10-31 02:58:22 +08:00
|
|
|
};
|
|
|
|
|
2018-03-06 15:40:47 +08:00
|
|
|
static struct audio_driver alsa_audio_driver = {
|
2009-08-11 08:31:14 +08:00
|
|
|
.name = "alsa",
|
|
|
|
.descr = "ALSA http://www.alsa-project.org",
|
|
|
|
.init = alsa_audio_init,
|
|
|
|
.fini = alsa_audio_fini,
|
|
|
|
.pcm_ops = &alsa_pcm_ops,
|
|
|
|
.can_be_default = 1,
|
|
|
|
.max_voices_out = INT_MAX,
|
|
|
|
.max_voices_in = INT_MAX,
|
|
|
|
.voice_size_out = sizeof (ALSAVoiceOut),
|
|
|
|
.voice_size_in = sizeof (ALSAVoiceIn)
|
2005-10-31 02:58:22 +08:00
|
|
|
};
|
2018-03-06 15:40:47 +08:00
|
|
|
|
|
|
|
static void register_audio_alsa(void)
|
|
|
|
{
|
|
|
|
audio_driver_register(&alsa_audio_driver);
|
|
|
|
}
|
|
|
|
type_init(register_audio_alsa);
|