2004-05-12 22:56:55 +00:00
|
|
|
/*
|
|
|
|
ao_alsa9/1.x - ALSA-0.9.x-1.x output plugin for MPlayer
|
|
|
|
|
|
|
|
(C) Alex Beregszaszi
|
|
|
|
|
|
|
|
modified for real alsa-0.9.0-support by Zsolt Barat <joy@streamminister.de>
|
|
|
|
additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
|
|
|
|
08/22/2002 iec958-init rewritten and merged with common init, zsolt
|
|
|
|
04/13/2004 merged with ao_alsa1.x, fixes provided by Jindrich Makovicka
|
|
|
|
04/25/2004 printfs converted to mp_msg, Zsolt.
|
|
|
|
|
|
|
|
Any bugreports regarding to this driver are welcome.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
2006-02-27 10:09:05 +00:00
|
|
|
#include <stdarg.h>
|
2007-02-09 19:49:31 +00:00
|
|
|
#include <ctype.h>
|
2004-05-12 22:56:55 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "config.h"
|
2005-01-03 14:23:18 +00:00
|
|
|
#include "subopt-helper.h"
|
2004-12-07 02:24:15 +00:00
|
|
|
#include "mixer.h"
|
|
|
|
#include "mp_msg.h"
|
2006-11-08 14:52:33 +00:00
|
|
|
#include "help_mp.h"
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
#define ALSA_PCM_NEW_HW_PARAMS_API
|
|
|
|
#define ALSA_PCM_NEW_SW_PARAMS_API
|
|
|
|
|
|
|
|
#if HAVE_SYS_ASOUNDLIB_H
|
|
|
|
#include <sys/asoundlib.h>
|
|
|
|
#elif HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#else
|
|
|
|
#error "asoundlib.h is not in sys/ or alsa/ - please bugreport"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2004-12-27 17:30:15 +00:00
|
|
|
#include "libaf/af_format.h"
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
|
|
|
"ALSA-0.9.x-1.x audio output",
|
|
|
|
"alsa",
|
|
|
|
"Alex Beregszaszi, Zsolt Barat <joy@streamminister.de>",
|
|
|
|
"under developement"
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(alsa)
|
|
|
|
|
|
|
|
static snd_pcm_t *alsa_handler;
|
|
|
|
static snd_pcm_format_t alsa_format;
|
|
|
|
static snd_pcm_hw_params_t *alsa_hwparams;
|
|
|
|
static snd_pcm_sw_params_t *alsa_swparams;
|
|
|
|
|
|
|
|
/* 16 sets buffersize to 16 * chunksize is as default 1024
|
|
|
|
* which seems to be good avarge for most situations
|
|
|
|
* so buffersize is 16384 frames by default */
|
|
|
|
static int alsa_fragcount = 16;
|
2006-02-10 09:28:08 +00:00
|
|
|
static snd_pcm_uframes_t chunk_size = 1024;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-13 11:38:12 +00:00
|
|
|
static size_t bytes_per_sample;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-10 09:28:08 +00:00
|
|
|
static int ao_noblock = 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
static int open_mode;
|
|
|
|
static int alsa_can_pause = 0;
|
|
|
|
|
2004-07-02 04:30:46 +00:00
|
|
|
#define ALSA_DEVICE_SIZE 256
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
#undef BUFFERTIME
|
2004-07-13 15:47:16 +00:00
|
|
|
#define SET_CHUNKSIZE
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-27 10:09:05 +00:00
|
|
|
static void alsa_error_handler(const char *file, int line, const char *function,
|
|
|
|
int err, const char *format, ...)
|
|
|
|
{
|
|
|
|
char tmp[0xc00];
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
vsnprintf(tmp, sizeof tmp, format, va);
|
|
|
|
va_end(va);
|
|
|
|
tmp[sizeof tmp - 1] = '\0';
|
|
|
|
|
|
|
|
if (err)
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n",
|
2006-02-27 10:09:05 +00:00
|
|
|
file, line, function, tmp, snd_strerror(err));
|
|
|
|
else
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n",
|
2006-02-27 10:09:05 +00:00
|
|
|
file, line, function, tmp);
|
|
|
|
}
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
/* to set/get/query special features/parameters */
|
|
|
|
static int control(int cmd, void *arg)
|
|
|
|
{
|
|
|
|
switch(cmd) {
|
|
|
|
case AOCONTROL_QUERY_FORMAT:
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
|
|
|
{
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
|
|
|
|
|
|
|
int err;
|
|
|
|
snd_mixer_t *handle;
|
|
|
|
snd_mixer_elem_t *elem;
|
|
|
|
snd_mixer_selem_id_t *sid;
|
|
|
|
|
2004-07-02 04:30:46 +00:00
|
|
|
static char *mix_name = "PCM";
|
|
|
|
static char *card = "default";
|
2004-09-22 14:12:53 +00:00
|
|
|
static int mix_index = 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
long pmin, pmax;
|
|
|
|
long get_vol, set_vol;
|
2004-07-13 15:47:16 +00:00
|
|
|
float f_multi;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2004-09-22 14:12:53 +00:00
|
|
|
if(mixer_channel) {
|
|
|
|
char *test_mix_index;
|
|
|
|
|
|
|
|
mix_name = strdup(mixer_channel);
|
2005-12-05 01:31:03 +00:00
|
|
|
if ((test_mix_index = strchr(mix_name, ','))){
|
2004-09-22 14:12:53 +00:00
|
|
|
*test_mix_index = 0;
|
|
|
|
test_mix_index++;
|
|
|
|
mix_index = strtol(test_mix_index, &test_mix_index, 0);
|
|
|
|
|
|
|
|
if (*test_mix_index){
|
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,
|
2006-11-08 14:52:33 +00:00
|
|
|
MSGTR_AO_ALSA_InvalidMixerIndexDefaultingToZero);
|
2004-09-22 14:12:53 +00:00
|
|
|
mix_index = 0 ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-07-02 04:30:46 +00:00
|
|
|
if(mixer_device) card = mixer_device;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2004-12-27 17:30:15 +00:00
|
|
|
if(ao_data.format == AF_FORMAT_AC3)
|
2004-05-12 22:56:55 +00:00
|
|
|
return CONTROL_TRUE;
|
|
|
|
|
|
|
|
//allocate simple id
|
|
|
|
snd_mixer_selem_id_alloca(&sid);
|
|
|
|
|
|
|
|
//sets simple-mixer index and name
|
2004-09-22 14:12:53 +00:00
|
|
|
snd_mixer_selem_id_set_index(sid, mix_index);
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_mixer_selem_id_set_name(sid, mix_name);
|
|
|
|
|
2004-09-22 14:12:53 +00:00
|
|
|
if (mixer_channel) {
|
|
|
|
free(mix_name);
|
|
|
|
mix_name = NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
if ((err = snd_mixer_open(&handle, 0)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_MixerOpenError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_mixer_attach(handle, card)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_MixerAttachError,
|
2004-05-12 22:56:55 +00:00
|
|
|
card, snd_strerror(err));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_MixerRegisterError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
err = snd_mixer_load(handle);
|
|
|
|
if (err < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_MixerLoadError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
elem = snd_mixer_find_selem(handle, sid);
|
|
|
|
if (!elem) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToFindSimpleControl,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
|
2004-07-14 00:48:26 +00:00
|
|
|
f_multi = (100 / (float)(pmax - pmin));
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
if (cmd == AOCONTROL_SET_VOLUME) {
|
|
|
|
|
2004-07-14 00:48:26 +00:00
|
|
|
set_vol = vol->left / f_multi + pmin + 0.5;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
//setting channels
|
|
|
|
if ((err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ErrorSettingLeftChannel,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
2004-07-13 15:47:16 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol);
|
|
|
|
|
2004-07-14 00:48:26 +00:00
|
|
|
set_vol = vol->right / f_multi + pmin + 0.5;
|
2004-07-13 15:47:16 +00:00
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
if ((err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ErrorSettingRightChannel,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
2004-07-13 15:47:16 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n",
|
|
|
|
set_vol, pmin, pmax, f_multi);
|
2005-12-15 17:50:29 +00:00
|
|
|
|
|
|
|
if (snd_mixer_selem_has_playback_switch(elem)) {
|
|
|
|
int lmute = (vol->left == 0.0);
|
|
|
|
int rmute = (vol->right == 0.0);
|
|
|
|
if (snd_mixer_selem_has_playback_switch_joined(elem)) {
|
|
|
|
lmute = rmute = lmute && rmute;
|
|
|
|
} else {
|
|
|
|
snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute);
|
|
|
|
}
|
|
|
|
snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute);
|
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
snd_mixer_selem_get_playback_volume(elem, 0, &get_vol);
|
2004-07-14 00:48:26 +00:00
|
|
|
vol->left = (get_vol - pmin) * f_multi;
|
2004-07-13 15:47:16 +00:00
|
|
|
snd_mixer_selem_get_playback_volume(elem, 1, &get_vol);
|
2004-07-14 00:48:26 +00:00
|
|
|
vol->right = (get_vol - pmin) * f_multi;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2004-07-13 15:47:16 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //end switch
|
|
|
|
return(CONTROL_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2005-01-03 14:23:18 +00:00
|
|
|
static void parse_device (char *dest, const char *src, int len)
|
2004-07-13 15:47:16 +00:00
|
|
|
{
|
|
|
|
char *tmp;
|
2005-01-03 14:23:18 +00:00
|
|
|
memmove(dest, src, len);
|
|
|
|
dest[len] = 0;
|
2004-07-13 15:47:16 +00:00
|
|
|
while ((tmp = strrchr(dest, '.')))
|
|
|
|
tmp[0] = ',';
|
2004-07-30 16:15:06 +00:00
|
|
|
while ((tmp = strrchr(dest, '=')))
|
2004-07-13 15:47:16 +00:00
|
|
|
tmp[0] = ':';
|
|
|
|
}
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
static void print_help (void)
|
2004-07-13 15:47:16 +00:00
|
|
|
{
|
|
|
|
mp_msg (MSGT_AO, MSGL_FATAL,
|
2006-11-08 14:52:33 +00:00
|
|
|
MSGTR_AO_ALSA_CommandlineHelp);
|
2004-07-13 15:47:16 +00:00
|
|
|
}
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2005-01-03 14:23:18 +00:00
|
|
|
static int str_maxlen(strarg_t *str) {
|
|
|
|
if (str->len > ALSA_DEVICE_SIZE)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-02-09 19:49:31 +00:00
|
|
|
static int try_open_device(const char *device, int open_mode, int try_ac3)
|
2006-09-18 16:58:21 +00:00
|
|
|
{
|
2007-02-09 19:49:31 +00:00
|
|
|
int err, len;
|
|
|
|
char *ac3_device, *args;
|
|
|
|
|
|
|
|
if (try_ac3) {
|
|
|
|
/* to set the non-audio bit, use AES0=6 */
|
|
|
|
len = strlen(device);
|
|
|
|
ac3_device = malloc(len + 7 + 1);
|
|
|
|
if (!ac3_device)
|
|
|
|
return -ENOMEM;
|
|
|
|
strcpy(ac3_device, device);
|
|
|
|
args = strchr(ac3_device, ':');
|
|
|
|
if (!args) {
|
|
|
|
/* no existing parameters: add it behind device name */
|
|
|
|
strcat(ac3_device, ":AES0=6");
|
|
|
|
} else {
|
|
|
|
do
|
|
|
|
++args;
|
|
|
|
while (isspace(*args));
|
|
|
|
if (*args == '\0') {
|
|
|
|
/* ":" but no parameters */
|
|
|
|
strcat(ac3_device, "AES0=6");
|
|
|
|
} else if (*args != '{') {
|
|
|
|
/* a simple list of parameters: add it at the end of the list */
|
|
|
|
strcat(ac3_device, ",AES0=6");
|
|
|
|
} else {
|
|
|
|
/* parameters in config syntax: add it inside the { } block */
|
|
|
|
do
|
|
|
|
--len;
|
|
|
|
while (len > 0 && isspace(ac3_device[len]));
|
|
|
|
if (ac3_device[len] == '}')
|
|
|
|
strcpy(ac3_device + len, " AES0=6}");
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-09 19:49:31 +00:00
|
|
|
err = snd_pcm_open(&alsa_handler, ac3_device, SND_PCM_STREAM_PLAYBACK,
|
|
|
|
open_mode);
|
|
|
|
free(ac3_device);
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
2007-02-09 19:49:31 +00:00
|
|
|
if (!try_ac3 || err < 0)
|
|
|
|
err = snd_pcm_open(&alsa_handler, device, SND_PCM_STREAM_PLAYBACK,
|
|
|
|
open_mode);
|
|
|
|
return err;
|
2006-09-18 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
/*
|
|
|
|
open & setup audio device
|
|
|
|
return: 1=success 0=fail
|
|
|
|
*/
|
|
|
|
static int init(int rate_hz, int channels, int format, int flags)
|
|
|
|
{
|
|
|
|
int err;
|
2005-01-03 14:23:18 +00:00
|
|
|
int block;
|
|
|
|
strarg_t device;
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_pcm_uframes_t bufsize;
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_pcm_uframes_t boundary;
|
2005-01-03 14:23:18 +00:00
|
|
|
opt_t subopts[] = {
|
|
|
|
{"block", OPT_ARG_BOOL, &block, NULL},
|
|
|
|
{"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen},
|
|
|
|
{NULL}
|
|
|
|
};
|
|
|
|
|
2004-07-02 04:30:46 +00:00
|
|
|
char alsa_device[ALSA_DEVICE_SIZE + 1];
|
|
|
|
// make sure alsa_device is null-terminated even when using strncpy etc.
|
|
|
|
memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2004-12-27 18:10:30 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz,
|
|
|
|
channels, format);
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_handler = NULL;
|
2006-02-27 10:06:27 +00:00
|
|
|
#if SND_LIB_VERSION >= 0x010005
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version());
|
|
|
|
#else
|
2004-05-12 22:56:55 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
|
2006-02-27 10:06:27 +00:00
|
|
|
#endif
|
2006-02-27 10:09:05 +00:00
|
|
|
|
|
|
|
snd_lib_error_set_handler(alsa_error_handler);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
ao_data.samplerate = rate_hz;
|
|
|
|
ao_data.format = format;
|
|
|
|
ao_data.channels = channels;
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S8:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S8;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U8:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_U8;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U16_LE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_U16_LE;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_U16_BE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_U16_BE;
|
|
|
|
break;
|
|
|
|
#ifndef WORDS_BIGENDIAN
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_AC3:
|
2004-05-12 22:56:55 +00:00
|
|
|
#endif
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S16_LE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
break;
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_AC3:
|
2004-05-12 22:56:55 +00:00
|
|
|
#endif
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S16_BE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S16_BE;
|
|
|
|
break;
|
2006-02-10 09:20:08 +00:00
|
|
|
case AF_FORMAT_U32_LE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_U32_LE;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_U32_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_U32_BE;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S32_LE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S32_LE;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_S32_BE:
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S32_BE;
|
|
|
|
break;
|
2004-12-27 17:30:15 +00:00
|
|
|
case AF_FORMAT_FLOAT_LE:
|
2004-06-11 15:20:49 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_FLOAT_LE;
|
|
|
|
break;
|
2006-02-10 09:20:08 +00:00
|
|
|
case AF_FORMAT_FLOAT_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_FLOAT_BE;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_MU_LAW:
|
|
|
|
alsa_format = SND_PCM_FORMAT_MU_LAW;
|
|
|
|
break;
|
|
|
|
case AF_FORMAT_A_LAW:
|
|
|
|
alsa_format = SND_PCM_FORMAT_A_LAW;
|
|
|
|
break;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
default:
|
2004-12-27 18:14:03 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1
|
2004-05-12 22:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-07-13 15:47:16 +00:00
|
|
|
//subdevice parsing
|
2005-01-03 14:23:18 +00:00
|
|
|
// set defaults
|
|
|
|
block = 1;
|
|
|
|
/* switch for spdif
|
|
|
|
* sets opening sequence for SPDIF
|
|
|
|
* sets also the playback and other switches 'on the fly'
|
|
|
|
* while opening the abstract alias for the spdif subdevice
|
|
|
|
* 'iec958'
|
|
|
|
*/
|
|
|
|
if (format == AF_FORMAT_AC3) {
|
2006-09-18 16:58:21 +00:00
|
|
|
device.str = "iec958";
|
2005-01-03 14:23:18 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels);
|
|
|
|
}
|
|
|
|
else
|
2004-05-12 22:56:55 +00:00
|
|
|
/* in any case for multichannel playback we should select
|
|
|
|
* appropriate device
|
|
|
|
*/
|
|
|
|
switch (channels) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "default";
|
2004-05-12 22:56:55 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n");
|
|
|
|
break;
|
|
|
|
case 4:
|
2004-07-02 04:30:46 +00:00
|
|
|
if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
|
|
|
|
// hack - use the converter plugin
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "plug:surround40";
|
2004-07-02 04:30:46 +00:00
|
|
|
else
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "surround40";
|
2004-05-12 22:56:55 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n");
|
|
|
|
break;
|
|
|
|
case 6:
|
2004-07-02 04:30:46 +00:00
|
|
|
if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "plug:surround51";
|
2004-07-02 04:30:46 +00:00
|
|
|
else
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "surround51";
|
2004-05-12 22:56:55 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n");
|
|
|
|
break;
|
|
|
|
default:
|
2005-01-03 14:23:18 +00:00
|
|
|
device.str = "default";
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ChannelsNotSupported,channels);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2005-01-03 14:23:18 +00:00
|
|
|
device.len = strlen(device.str);
|
|
|
|
if (subopt_parse(ao_subdevice, subopts) != 0) {
|
|
|
|
print_help();
|
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2005-01-03 14:23:18 +00:00
|
|
|
ao_noblock = !block;
|
|
|
|
parse_device(alsa_device, device.str, device.len);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-10-12 22:24:19 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
//setting modes for block or nonblock-mode
|
|
|
|
if (ao_noblock) {
|
|
|
|
open_mode = SND_PCM_NONBLOCK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
open_mode = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//sets buff/chunksize if its set manually
|
|
|
|
if (ao_data.buffersize) {
|
|
|
|
switch (ao_data.buffersize)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
alsa_fragcount = 16;
|
|
|
|
chunk_size = 512;
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n");
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
alsa_fragcount = 8;
|
|
|
|
chunk_size = 1024;
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n");
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
alsa_fragcount = 32;
|
|
|
|
chunk_size = 512;
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n");
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
alsa_fragcount = 16;
|
|
|
|
chunk_size = 1024;
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n");
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
alsa_fragcount = 16;
|
2006-02-13 11:15:25 +00:00
|
|
|
chunk_size = 1024;
|
2004-05-12 22:56:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!alsa_handler) {
|
|
|
|
//modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
|
2007-02-09 19:49:31 +00:00
|
|
|
if ((err = try_open_device(alsa_device, open_mode, format == AF_FORMAT_AC3)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
if (err != -EBUSY && ao_noblock) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_ALSA_OpenInNonblockModeFailed);
|
2007-02-09 19:49:31 +00:00
|
|
|
if ((err = try_open_device(alsa_device, 0, format == AF_FORMAT_AC3)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PlaybackOpenError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
} else {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PlaybackOpenError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-13 11:38:12 +00:00
|
|
|
if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ErrorSetBlockMode, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
} else {
|
2006-11-07 21:09:10 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opened in blocking mode\n");
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(&alsa_hwparams);
|
|
|
|
snd_pcm_sw_params_alloca(&alsa_swparams);
|
|
|
|
|
|
|
|
// setting hw-parameters
|
|
|
|
if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetInitialParameters,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2006-02-13 11:15:25 +00:00
|
|
|
err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED);
|
2004-05-12 22:56:55 +00:00
|
|
|
if (err < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetAccessType,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* workaround for nonsupported formats
|
|
|
|
sets default format to S16_LE if the given formats aren't supported */
|
|
|
|
if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams,
|
|
|
|
alsa_format)) < 0)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_AO,MSGL_INFO,
|
2006-11-08 14:52:33 +00:00
|
|
|
MSGTR_AO_ALSA_FormatNotSupportedByHardware, af_fmt2str_short(format));
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_format = SND_PCM_FORMAT_S16_LE;
|
2004-12-27 17:30:15 +00:00
|
|
|
ao_data.format = AF_FORMAT_S16_LE;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
|
|
|
|
alsa_format)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetFormat,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
2005-08-25 19:57:32 +00:00
|
|
|
return(0);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2005-08-25 19:57:32 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams,
|
|
|
|
&ao_data.channels)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetChannels,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
2005-08-25 19:57:32 +00:00
|
|
|
return(0);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
2006-03-13 10:50:37 +00:00
|
|
|
/* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11)
|
|
|
|
prefer our own resampler */
|
|
|
|
#if SND_LIB_VERSION >= 0x010009
|
|
|
|
if ((err = snd_pcm_hw_params_set_rate_resample(alsa_handler, alsa_hwparams,
|
|
|
|
0)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToDisableResampling,
|
2006-03-13 10:50:37 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams,
|
2006-02-10 09:28:08 +00:00
|
|
|
&ao_data.samplerate, NULL)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetSamplerate2,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2006-02-10 09:18:27 +00:00
|
|
|
bytes_per_sample = snd_pcm_format_physical_width(alsa_format) / 8;
|
|
|
|
bytes_per_sample *= ao_data.channels;
|
|
|
|
ao_data.bps = ao_data.samplerate * bytes_per_sample;
|
2005-08-25 21:46:32 +00:00
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
#ifdef BUFFERTIME
|
|
|
|
{
|
|
|
|
int alsa_buffer_time = 500000; /* original 60 */
|
|
|
|
int alsa_period_time;
|
|
|
|
alsa_period_time = alsa_buffer_time/4;
|
|
|
|
if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams,
|
2006-02-10 09:28:08 +00:00
|
|
|
&alsa_buffer_time, NULL)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetBufferTimeNear,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
} else
|
|
|
|
alsa_buffer_time = err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams,
|
2006-02-10 09:28:08 +00:00
|
|
|
&alsa_period_time, NULL)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
/* original: alsa_buffer_time/ao_data.bps */
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetPeriodTime,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_ALSA_BufferTimePeriodTime,
|
2004-05-12 22:56:55 +00:00
|
|
|
alsa_buffer_time, err);
|
|
|
|
}
|
|
|
|
#endif//end SET_BUFFERTIME
|
|
|
|
|
|
|
|
#ifdef SET_CHUNKSIZE
|
|
|
|
{
|
|
|
|
//set chunksize
|
|
|
|
if ((err = snd_pcm_hw_params_set_period_size_near(alsa_handler, alsa_hwparams,
|
2006-02-10 09:28:08 +00:00
|
|
|
&chunk_size, NULL)) < 0)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetPeriodSize,
|
2004-05-12 22:56:55 +00:00
|
|
|
chunk_size, snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-01-12 20:04:36 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set to %li\n", chunk_size);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams,
|
2006-02-10 09:28:08 +00:00
|
|
|
&alsa_fragcount, NULL)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetPeriods,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: fragcount=%i\n", alsa_fragcount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif//end SET_CHUNKSIZE
|
|
|
|
|
|
|
|
/* finally install hardware parameters */
|
|
|
|
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetHwParameters,
|
2004-05-12 22:56:55 +00:00
|
|
|
snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
// end setting hw-params
|
|
|
|
|
|
|
|
|
|
|
|
// gets buffersize for control
|
|
|
|
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetBufferSize, snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ao_data.buffersize = bufsize * bytes_per_sample;
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize);
|
|
|
|
}
|
|
|
|
|
2006-02-13 11:43:25 +00:00
|
|
|
if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetPeriodSize, snd_strerror(err));
|
2006-09-18 16:29:27 +00:00
|
|
|
return 0;
|
2006-02-13 11:43:25 +00:00
|
|
|
} else {
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-init: got period size %li\n", chunk_size);
|
|
|
|
}
|
|
|
|
ao_data.outburst = chunk_size * bytes_per_sample;
|
|
|
|
|
|
|
|
/* setting software parameters */
|
|
|
|
if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetSwParameters,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-31 09:28:37 +00:00
|
|
|
#if SND_LIB_VERSION >= 0x000901
|
2006-02-13 11:43:25 +00:00
|
|
|
if ((err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetBoundary,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-31 09:28:37 +00:00
|
|
|
#else
|
|
|
|
boundary = 0x7fffffff;
|
|
|
|
#endif
|
2006-02-13 11:43:25 +00:00
|
|
|
/* start playing when one period has been written */
|
|
|
|
if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, chunk_size)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetStartThreshold,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* disable underrun reporting */
|
|
|
|
if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, boundary)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetStopThreshold,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-31 09:28:37 +00:00
|
|
|
#if SND_LIB_VERSION >= 0x000901
|
2006-02-13 11:43:25 +00:00
|
|
|
/* play silence when there is an underrun */
|
|
|
|
if ((err = snd_pcm_sw_params_set_silence_size(alsa_handler, alsa_swparams, boundary)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetSilenceSize,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-31 09:28:37 +00:00
|
|
|
#endif
|
2006-02-13 11:43:25 +00:00
|
|
|
if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetSwParameters,
|
2006-02-13 11:43:25 +00:00
|
|
|
snd_strerror(err));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* end setting sw-params */
|
|
|
|
|
2006-10-12 22:24:19 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
|
2004-05-12 22:56:55 +00:00
|
|
|
ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize,
|
|
|
|
snd_pcm_format_description(alsa_format));
|
|
|
|
|
|
|
|
} // end switch alsa_handler (spdif)
|
|
|
|
alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
|
|
|
|
return(1);
|
|
|
|
} // end init
|
|
|
|
|
|
|
|
|
|
|
|
/* close audio device */
|
|
|
|
static void uninit(int immed)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (alsa_handler) {
|
|
|
|
int err;
|
|
|
|
|
2005-02-27 23:06:32 +00:00
|
|
|
if (!immed)
|
|
|
|
snd_pcm_drain(alsa_handler);
|
|
|
|
|
2004-05-12 22:56:55 +00:00
|
|
|
if ((err = snd_pcm_close(alsa_handler)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmCloseError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alsa_handler = NULL;
|
2006-10-12 22:24:19 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-uninit: pcm closed\n");
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_NoHandlerDefined);
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_pause(void)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (alsa_can_pause) {
|
|
|
|
if ((err = snd_pcm_pause(alsa_handler, 1)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmPauseError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n");
|
|
|
|
} else {
|
|
|
|
if ((err = snd_pcm_drop(alsa_handler)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmDropError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-09 14:08:03 +00:00
|
|
|
static void audio_resume(void)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (alsa_can_pause) {
|
|
|
|
if ((err = snd_pcm_pause(alsa_handler, 0)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmResumeError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n");
|
|
|
|
} else {
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmPrepareError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop playing and empty buffers (for seeking/pause) */
|
2006-02-09 14:08:03 +00:00
|
|
|
static void reset(void)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_drop(alsa_handler)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmPrepareError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmPrepareError, snd_strerror(err));
|
2004-05-12 22:56:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
plays 'len' bytes of 'data'
|
|
|
|
returns: number of bytes played
|
|
|
|
modified last at 29.06.02 by jp
|
|
|
|
thanxs for marius <marius@rospot.com> for giving us the light ;)
|
|
|
|
*/
|
|
|
|
|
2006-02-13 11:16:29 +00:00
|
|
|
static int play(void* data, int len, int flags)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
int num_frames = len / bytes_per_sample;
|
|
|
|
snd_pcm_sframes_t res = 0;
|
|
|
|
|
|
|
|
//mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len);
|
|
|
|
|
|
|
|
if (!alsa_handler) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_DeviceConfigurationError);
|
2004-05-12 22:56:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-02-13 11:48:29 +00:00
|
|
|
if (num_frames == 0)
|
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-13 11:48:29 +00:00
|
|
|
do {
|
|
|
|
res = snd_pcm_writei(alsa_handler, data, num_frames);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-13 11:48:29 +00:00
|
|
|
if (res == -EINTR) {
|
|
|
|
/* nothing to do */
|
|
|
|
res = 0;
|
|
|
|
}
|
|
|
|
else if (res == -ESTRPIPE) { /* suspend */
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_ALSA_PcmInSuspendModeTryingResume);
|
2004-05-12 22:56:55 +00:00
|
|
|
while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
|
|
|
|
sleep(1);
|
|
|
|
}
|
2006-02-13 11:48:29 +00:00
|
|
|
if (res < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_WriteError, snd_strerror(res));
|
|
|
|
mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_ALSA_TryingToResetSoundcard);
|
2004-05-12 22:56:55 +00:00
|
|
|
if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PcmPrepareError, snd_strerror(res));
|
2004-05-12 22:56:55 +00:00
|
|
|
return(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-02-13 11:48:29 +00:00
|
|
|
} while (res == 0);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-13 11:48:29 +00:00
|
|
|
return res < 0 ? res : res * bytes_per_sample;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* how many byes are free in the buffer */
|
2006-02-09 14:08:03 +00:00
|
|
|
static int get_space(void)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
snd_pcm_status_t *status;
|
|
|
|
int ret;
|
|
|
|
|
2004-07-02 04:30:46 +00:00
|
|
|
snd_pcm_status_alloca(&status);
|
2004-05-12 22:56:55 +00:00
|
|
|
|
|
|
|
if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
|
|
|
|
{
|
2006-11-08 14:52:33 +00:00
|
|
|
mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_CannotGetPcmStatus, snd_strerror(ret));
|
2004-05-12 22:56:55 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2006-02-10 09:21:17 +00:00
|
|
|
ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
|
2007-09-24 21:49:58 +00:00
|
|
|
if (ret > ao_data.buffersize) // Buffer underrun?
|
|
|
|
ret = ao_data.buffersize;
|
2004-05-12 22:56:55 +00:00
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delay in seconds between first and last sample in buffer */
|
2006-02-09 14:08:03 +00:00
|
|
|
static float get_delay(void)
|
2004-05-12 22:56:55 +00:00
|
|
|
{
|
|
|
|
if (alsa_handler) {
|
2006-02-10 09:24:48 +00:00
|
|
|
snd_pcm_sframes_t delay;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-10 09:24:48 +00:00
|
|
|
if (snd_pcm_delay(alsa_handler, &delay) < 0)
|
|
|
|
return 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
|
2006-02-10 09:24:48 +00:00
|
|
|
if (delay < 0) {
|
|
|
|
/* underrun - move the application pointer forward to catch up */
|
|
|
|
#if SND_LIB_VERSION >= 0x000901 /* snd_pcm_forward() exists since 0.9.0rc8 */
|
|
|
|
snd_pcm_forward(alsa_handler, -delay);
|
|
|
|
#endif
|
|
|
|
delay = 0;
|
2004-05-12 22:56:55 +00:00
|
|
|
}
|
2006-02-10 09:24:48 +00:00
|
|
|
return (float)delay / (float)ao_data.samplerate;
|
2004-05-12 22:56:55 +00:00
|
|
|
} else {
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|