Remove HAVE_MATRIXVIEW references
[mplayer/kovensky.git] / libao2 / ao_alsa.c
blob49f4237604894451fab41e87c105020c44742cdb
1 /*
2 * ALSA 0.9.x-1.x audio output driver
4 * Copyright (C) 2004 Alex Beregszaszi
6 * modified for real ALSA 0.9.0 support by Zsolt Barat <joy@streamminister.de>
7 * additional AC-3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
8 * 08/22/2002 iec958-init rewritten and merged with common init, zsolt
9 * 04/13/2004 merged with ao_alsa1.x, fixes provided by Jindrich Makovicka
10 * 04/25/2004 printfs converted to mp_msg, Zsolt.
12 * This file is part of MPlayer.
14 * MPlayer is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * MPlayer is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <errno.h>
30 #include <sys/time.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <ctype.h>
34 #include <math.h>
35 #include <string.h>
36 #include <alloca.h>
38 #include "config.h"
39 #include "subopt-helper.h"
40 #include "mixer.h"
41 #include "mp_msg.h"
42 #include "help_mp.h"
44 #define ALSA_PCM_NEW_HW_PARAMS_API
45 #define ALSA_PCM_NEW_SW_PARAMS_API
47 #ifdef HAVE_SYS_ASOUNDLIB_H
48 #include <sys/asoundlib.h>
49 #elif defined(HAVE_ALSA_ASOUNDLIB_H)
50 #include <alsa/asoundlib.h>
51 #else
52 #error "asoundlib.h is not in sys/ or alsa/ - please bugreport"
53 #endif
56 #include "audio_out.h"
57 #include "audio_out_internal.h"
58 #include "libaf/af_format.h"
60 static const ao_info_t info =
62 "ALSA-0.9.x-1.x audio output",
63 "alsa",
64 "Alex Beregszaszi, Zsolt Barat <joy@streamminister.de>",
65 "under developement"
68 LIBAO_EXTERN(alsa)
70 static snd_pcm_t *alsa_handler;
71 static snd_pcm_format_t alsa_format;
72 static snd_pcm_hw_params_t *alsa_hwparams;
73 static snd_pcm_sw_params_t *alsa_swparams;
75 #define BUFFER_TIME 500000 // 0.5 s
76 #define FRAGCOUNT 16
78 static size_t bytes_per_sample;
80 static int alsa_can_pause;
81 static snd_pcm_sframes_t prepause_frames;
83 #define ALSA_DEVICE_SIZE 256
85 static void alsa_error_handler(const char *file, int line, const char *function,
86 int err, const char *format, ...)
88 char tmp[0xc00];
89 va_list va;
91 va_start(va, format);
92 vsnprintf(tmp, sizeof tmp, format, va);
93 va_end(va);
94 tmp[sizeof tmp - 1] = '\0';
96 if (err)
97 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n",
98 file, line, function, tmp, snd_strerror(err));
99 else
100 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n",
101 file, line, function, tmp);
104 /* to set/get/query special features/parameters */
105 static int control(int cmd, void *arg)
107 switch(cmd) {
108 case AOCONTROL_QUERY_FORMAT:
109 return CONTROL_TRUE;
110 case AOCONTROL_GET_VOLUME:
111 case AOCONTROL_SET_VOLUME:
113 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
115 int err;
116 snd_mixer_t *handle;
117 snd_mixer_elem_t *elem;
118 snd_mixer_selem_id_t *sid;
120 char *mix_name = "PCM";
121 char *card = "default";
122 int mix_index = 0;
124 long pmin, pmax;
125 long get_vol, set_vol;
126 float f_multi;
128 if(AF_FORMAT_IS_AC3(ao_data.format))
129 return CONTROL_TRUE;
131 if(mixer_channel) {
132 char *test_mix_index;
134 mix_name = strdup(mixer_channel);
135 if ((test_mix_index = strchr(mix_name, ','))){
136 *test_mix_index = 0;
137 test_mix_index++;
138 mix_index = strtol(test_mix_index, &test_mix_index, 0);
140 if (*test_mix_index){
141 mp_tmsg(MSGT_AO,MSGL_ERR,
142 "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n");
143 mix_index = 0 ;
147 if(mixer_device) card = mixer_device;
149 //allocate simple id
150 snd_mixer_selem_id_alloca(&sid);
152 //sets simple-mixer index and name
153 snd_mixer_selem_id_set_index(sid, mix_index);
154 snd_mixer_selem_id_set_name(sid, mix_name);
156 if (mixer_channel) {
157 free(mix_name);
158 mix_name = NULL;
161 if ((err = snd_mixer_open(&handle, 0)) < 0) {
162 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer open error: %s\n", snd_strerror(err));
163 return CONTROL_ERROR;
166 if ((err = snd_mixer_attach(handle, card)) < 0) {
167 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer attach %s error: %s\n",
168 card, snd_strerror(err));
169 snd_mixer_close(handle);
170 return CONTROL_ERROR;
173 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
174 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer register error: %s\n", snd_strerror(err));
175 snd_mixer_close(handle);
176 return CONTROL_ERROR;
178 err = snd_mixer_load(handle);
179 if (err < 0) {
180 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer load error: %s\n", snd_strerror(err));
181 snd_mixer_close(handle);
182 return CONTROL_ERROR;
185 elem = snd_mixer_find_selem(handle, sid);
186 if (!elem) {
187 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to find simple control '%s',%i.\n",
188 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
189 snd_mixer_close(handle);
190 return CONTROL_ERROR;
193 snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
194 f_multi = (100 / (float)(pmax - pmin));
196 if (cmd == AOCONTROL_SET_VOLUME) {
198 set_vol = vol->left / f_multi + pmin + 0.5;
200 //setting channels
201 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol)) < 0) {
202 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting left channel, %s\n",
203 snd_strerror(err));
204 snd_mixer_close(handle);
205 return CONTROL_ERROR;
207 mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol);
209 set_vol = vol->right / f_multi + pmin + 0.5;
211 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol)) < 0) {
212 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting right channel, %s\n",
213 snd_strerror(err));
214 snd_mixer_close(handle);
215 return CONTROL_ERROR;
217 mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n",
218 set_vol, pmin, pmax, f_multi);
220 if (snd_mixer_selem_has_playback_switch(elem)) {
221 int lmute = (vol->left == 0.0);
222 int rmute = (vol->right == 0.0);
223 if (snd_mixer_selem_has_playback_switch_joined(elem)) {
224 lmute = rmute = lmute && rmute;
225 } else {
226 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute);
228 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute);
231 else {
232 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &get_vol);
233 vol->left = (get_vol - pmin) * f_multi;
234 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol);
235 vol->right = (get_vol - pmin) * f_multi;
237 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right);
239 snd_mixer_close(handle);
240 return CONTROL_OK;
243 } //end switch
244 return CONTROL_UNKNOWN;
247 static void parse_device (char *dest, const char *src, int len)
249 char *tmp;
250 memmove(dest, src, len);
251 dest[len] = 0;
252 while ((tmp = strrchr(dest, '.')))
253 tmp[0] = ',';
254 while ((tmp = strrchr(dest, '=')))
255 tmp[0] = ':';
258 static void print_help (void)
260 mp_tmsg (MSGT_AO, MSGL_FATAL,
261 "\n[AO_ALSA] -ao alsa commandline help:\n"\
262 "[AO_ALSA] Example: mplayer -ao alsa:device=hw=0.3\n"\
263 "[AO_ALSA] Sets first card fourth hardware device.\n\n"\
264 "[AO_ALSA] Options:\n"\
265 "[AO_ALSA] noblock\n"\
266 "[AO_ALSA] Opens device in non-blocking mode.\n"\
267 "[AO_ALSA] device=<device-name>\n"\
268 "[AO_ALSA] Sets device (change , to . and : to =)\n");
271 static int str_maxlen(void *strp) {
272 strarg_t *str = strp;
273 return str->len <= ALSA_DEVICE_SIZE;
276 static int try_open_device(const char *device, int open_mode, int try_ac3)
278 int err, len;
279 char *ac3_device, *args;
281 if (try_ac3) {
282 /* to set the non-audio bit, use AES0=6 */
283 len = strlen(device);
284 ac3_device = malloc(len + 7 + 1);
285 if (!ac3_device)
286 return -ENOMEM;
287 strcpy(ac3_device, device);
288 args = strchr(ac3_device, ':');
289 if (!args) {
290 /* no existing parameters: add it behind device name */
291 strcat(ac3_device, ":AES0=6");
292 } else {
294 ++args;
295 while (isspace(*args));
296 if (*args == '\0') {
297 /* ":" but no parameters */
298 strcat(ac3_device, "AES0=6");
299 } else if (*args != '{') {
300 /* a simple list of parameters: add it at the end of the list */
301 strcat(ac3_device, ",AES0=6");
302 } else {
303 /* parameters in config syntax: add it inside the { } block */
305 --len;
306 while (len > 0 && isspace(ac3_device[len]));
307 if (ac3_device[len] == '}')
308 strcpy(ac3_device + len, " AES0=6}");
311 err = snd_pcm_open(&alsa_handler, ac3_device, SND_PCM_STREAM_PLAYBACK,
312 open_mode);
313 free(ac3_device);
315 if (!try_ac3 || err < 0)
316 err = snd_pcm_open(&alsa_handler, device, SND_PCM_STREAM_PLAYBACK,
317 open_mode);
318 return err;
322 open & setup audio device
323 return: 1=success 0=fail
325 static int init(int rate_hz, int channels, int format, int flags)
327 int err;
328 int block;
329 strarg_t device;
330 snd_pcm_uframes_t chunk_size;
331 snd_pcm_uframes_t bufsize;
332 snd_pcm_uframes_t boundary;
333 const opt_t subopts[] = {
334 {"block", OPT_ARG_BOOL, &block, NULL},
335 {"device", OPT_ARG_STR, &device, str_maxlen},
336 {NULL}
339 char alsa_device[ALSA_DEVICE_SIZE + 1];
340 // make sure alsa_device is null-terminated even when using strncpy etc.
341 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1);
343 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz,
344 channels, format);
345 alsa_handler = NULL;
346 #if SND_LIB_VERSION >= 0x010005
347 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version());
348 #else
349 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
350 #endif
352 prepause_frames = 0;
354 snd_lib_error_set_handler(alsa_error_handler);
356 ao_data.samplerate = rate_hz;
357 ao_data.format = format;
358 ao_data.channels = channels;
360 switch (format)
362 case AF_FORMAT_S8:
363 alsa_format = SND_PCM_FORMAT_S8;
364 break;
365 case AF_FORMAT_U8:
366 alsa_format = SND_PCM_FORMAT_U8;
367 break;
368 case AF_FORMAT_U16_LE:
369 alsa_format = SND_PCM_FORMAT_U16_LE;
370 break;
371 case AF_FORMAT_U16_BE:
372 alsa_format = SND_PCM_FORMAT_U16_BE;
373 break;
374 case AF_FORMAT_AC3_LE:
375 case AF_FORMAT_S16_LE:
376 alsa_format = SND_PCM_FORMAT_S16_LE;
377 break;
378 case AF_FORMAT_AC3_BE:
379 case AF_FORMAT_S16_BE:
380 alsa_format = SND_PCM_FORMAT_S16_BE;
381 break;
382 case AF_FORMAT_U32_LE:
383 alsa_format = SND_PCM_FORMAT_U32_LE;
384 break;
385 case AF_FORMAT_U32_BE:
386 alsa_format = SND_PCM_FORMAT_U32_BE;
387 break;
388 case AF_FORMAT_S32_LE:
389 alsa_format = SND_PCM_FORMAT_S32_LE;
390 break;
391 case AF_FORMAT_S32_BE:
392 alsa_format = SND_PCM_FORMAT_S32_BE;
393 break;
394 case AF_FORMAT_U24_LE:
395 alsa_format = SND_PCM_FORMAT_U24_3LE;
396 break;
397 case AF_FORMAT_U24_BE:
398 alsa_format = SND_PCM_FORMAT_U24_3BE;
399 break;
400 case AF_FORMAT_S24_LE:
401 alsa_format = SND_PCM_FORMAT_S24_3LE;
402 break;
403 case AF_FORMAT_S24_BE:
404 alsa_format = SND_PCM_FORMAT_S24_3BE;
405 break;
406 case AF_FORMAT_FLOAT_LE:
407 alsa_format = SND_PCM_FORMAT_FLOAT_LE;
408 break;
409 case AF_FORMAT_FLOAT_BE:
410 alsa_format = SND_PCM_FORMAT_FLOAT_BE;
411 break;
412 case AF_FORMAT_MU_LAW:
413 alsa_format = SND_PCM_FORMAT_MU_LAW;
414 break;
415 case AF_FORMAT_A_LAW:
416 alsa_format = SND_PCM_FORMAT_A_LAW;
417 break;
419 default:
420 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1
421 break;
424 //subdevice parsing
425 // set defaults
426 block = 1;
427 /* switch for spdif
428 * sets opening sequence for SPDIF
429 * sets also the playback and other switches 'on the fly'
430 * while opening the abstract alias for the spdif subdevice
431 * 'iec958'
433 if (AF_FORMAT_IS_AC3(format)) {
434 device.str = "iec958";
435 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels);
437 else
438 /* in any case for multichannel playback we should select
439 * appropriate device
441 switch (channels) {
442 case 1:
443 case 2:
444 device.str = "default";
445 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n");
446 break;
447 case 4:
448 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
449 // hack - use the converter plugin
450 device.str = "plug:surround40";
451 else
452 device.str = "surround40";
453 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n");
454 break;
455 case 6:
456 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
457 device.str = "plug:surround51";
458 else
459 device.str = "surround51";
460 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n");
461 break;
462 case 8:
463 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
464 device.str = "plug:surround71";
465 else
466 device.str = "surround71";
467 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround71\n");
468 break;
469 default:
470 device.str = "default";
471 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] %d channels are not supported.\n",channels);
473 device.len = strlen(device.str);
474 if (subopt_parse(ao_subdevice, subopts) != 0) {
475 print_help();
476 return 0;
478 parse_device(alsa_device, device.str, device.len);
480 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device);
482 if (!alsa_handler) {
483 int open_mode = block ? 0 : SND_PCM_NONBLOCK;
484 int isac3 = AF_FORMAT_IS_AC3(format);
485 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
486 if ((err = try_open_device(alsa_device, open_mode, isac3)) < 0)
488 if (err != -EBUSY && !block) {
489 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Open in nonblock-mode failed, trying to open in block-mode.\n");
490 if ((err = try_open_device(alsa_device, 0, isac3)) < 0) {
491 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
492 return 0;
494 } else {
495 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
496 return 0;
500 if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) {
501 mp_tmsg(MSGT_AO,MSGL_ERR,"[AL_ALSA] Error setting block-mode %s.\n", snd_strerror(err));
502 } else {
503 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opened in blocking mode\n");
506 snd_pcm_hw_params_alloca(&alsa_hwparams);
507 snd_pcm_sw_params_alloca(&alsa_swparams);
509 // setting hw-parameters
510 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
512 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get initial parameters: %s\n",
513 snd_strerror(err));
514 return 0;
517 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,
518 SND_PCM_ACCESS_RW_INTERLEAVED);
519 if (err < 0) {
520 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set access type: %s\n",
521 snd_strerror(err));
522 return 0;
525 /* workaround for nonsupported formats
526 sets default format to S16_LE if the given formats aren't supported */
527 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams,
528 alsa_format)) < 0)
530 mp_tmsg(MSGT_AO,MSGL_INFO,
531 "[AO_ALSA] Format %s is not supported by hardware, trying default.\n", af_fmt2str_short(format));
532 alsa_format = SND_PCM_FORMAT_S16_LE;
533 if (AF_FORMAT_IS_AC3(ao_data.format))
534 ao_data.format = AF_FORMAT_AC3_LE;
535 else
536 ao_data.format = AF_FORMAT_S16_LE;
539 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
540 alsa_format)) < 0)
542 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set format: %s\n",
543 snd_strerror(err));
544 return 0;
547 if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams,
548 &ao_data.channels)) < 0)
550 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set channels: %s\n",
551 snd_strerror(err));
552 return 0;
555 /* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11)
556 prefer our own resampler */
557 #if SND_LIB_VERSION >= 0x010009
558 if ((err = snd_pcm_hw_params_set_rate_resample(alsa_handler, alsa_hwparams,
559 0)) < 0)
561 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to disable resampling: %s\n",
562 snd_strerror(err));
563 return 0;
565 #endif
567 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams,
568 &ao_data.samplerate, NULL)) < 0)
570 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set samplerate-2: %s\n",
571 snd_strerror(err));
572 return 0;
575 bytes_per_sample = af_fmt2bits(ao_data.format) / 8;
576 bytes_per_sample *= ao_data.channels;
577 ao_data.bps = ao_data.samplerate * bytes_per_sample;
579 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams,
580 &(unsigned int){BUFFER_TIME}, NULL)) < 0)
582 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set buffer time near: %s\n",
583 snd_strerror(err));
584 return 0;
587 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams,
588 &(unsigned int){FRAGCOUNT}, NULL)) < 0) {
589 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set periods: %s\n",
590 snd_strerror(err));
591 return 0;
594 /* finally install hardware parameters */
595 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
597 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set hw-parameters: %s\n",
598 snd_strerror(err));
599 return 0;
601 // end setting hw-params
604 // gets buffersize for control
605 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0)
607 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get buffersize: %s\n", snd_strerror(err));
608 return 0;
610 else {
611 ao_data.buffersize = bufsize * bytes_per_sample;
612 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize);
615 if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL)) < 0) {
616 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO ALSA] Unable to get period size: %s\n", snd_strerror(err));
617 return 0;
618 } else {
619 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got period size %li\n", chunk_size);
621 ao_data.outburst = chunk_size * bytes_per_sample;
623 /* setting software parameters */
624 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) {
625 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
626 snd_strerror(err));
627 return 0;
629 #if SND_LIB_VERSION >= 0x000901
630 if ((err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary)) < 0) {
631 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get boundary: %s\n",
632 snd_strerror(err));
633 return 0;
635 #else
636 boundary = 0x7fffffff;
637 #endif
638 /* start playing when one period has been written */
639 if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, chunk_size)) < 0) {
640 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set start threshold: %s\n",
641 snd_strerror(err));
642 return 0;
644 /* disable underrun reporting */
645 if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, boundary)) < 0) {
646 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set stop threshold: %s\n",
647 snd_strerror(err));
648 return 0;
650 #if SND_LIB_VERSION >= 0x000901
651 /* play silence when there is an underrun */
652 if ((err = snd_pcm_sw_params_set_silence_size(alsa_handler, alsa_swparams, boundary)) < 0) {
653 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set silence size: %s\n",
654 snd_strerror(err));
655 return 0;
657 #endif
658 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) {
659 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
660 snd_strerror(err));
661 return 0;
663 /* end setting sw-params */
665 mp_msg(MSGT_AO,MSGL_V,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
666 ao_data.samplerate, ao_data.channels, (int)bytes_per_sample, ao_data.buffersize,
667 snd_pcm_format_description(alsa_format));
669 } // end switch alsa_handler (spdif)
670 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
671 return 1;
672 } // end init
675 /* close audio device */
676 static void uninit(int immed)
679 if (alsa_handler) {
680 int err;
682 if (!immed)
683 snd_pcm_drain(alsa_handler);
685 if ((err = snd_pcm_close(alsa_handler)) < 0)
687 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm close error: %s\n", snd_strerror(err));
688 return;
690 else {
691 alsa_handler = NULL;
692 mp_msg(MSGT_AO,MSGL_V,"alsa-uninit: pcm closed\n");
695 else {
696 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] No handler defined!\n");
700 static void audio_pause(void)
702 int err;
704 if (alsa_can_pause) {
705 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0)
707 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm pause error: %s\n", snd_strerror(err));
708 return;
710 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n");
711 } else {
712 if (snd_pcm_delay(alsa_handler, &prepause_frames) < 0
713 || prepause_frames < 0)
714 prepause_frames = 0;
716 if ((err = snd_pcm_drop(alsa_handler)) < 0)
718 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm drop error: %s\n", snd_strerror(err));
719 return;
724 static void audio_resume(void)
726 int err;
728 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
729 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
730 while ((err = snd_pcm_resume(alsa_handler)) == -EAGAIN) sleep(1);
732 if (alsa_can_pause) {
733 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0)
735 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm resume error: %s\n", snd_strerror(err));
736 return;
738 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n");
739 } else {
740 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
742 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
743 return;
745 if (prepause_frames) {
746 void *silence = calloc(prepause_frames, bytes_per_sample);
747 play(silence, prepause_frames * bytes_per_sample, 0);
748 free(silence);
753 /* stop playing and empty buffers (for seeking/pause) */
754 static void reset(void)
756 int err;
758 prepause_frames = 0;
759 if ((err = snd_pcm_drop(alsa_handler)) < 0)
761 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
762 return;
764 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
766 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
767 return;
769 return;
773 plays 'len' bytes of 'data'
774 returns: number of bytes played
775 modified last at 29.06.02 by jp
776 thanxs for marius <marius@rospot.com> for giving us the light ;)
779 static int play(void* data, int len, int flags)
781 int num_frames;
782 snd_pcm_sframes_t res = 0;
783 if (!(flags & AOPLAY_FINAL_CHUNK))
784 len = len / ao_data.outburst * ao_data.outburst;
785 num_frames = len / bytes_per_sample;
787 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len);
789 if (!alsa_handler) {
790 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Device configuration error.");
791 return 0;
794 if (num_frames == 0)
795 return 0;
797 do {
798 res = snd_pcm_writei(alsa_handler, data, num_frames);
800 if (res == -EINTR) {
801 /* nothing to do */
802 res = 0;
804 else if (res == -ESTRPIPE) { /* suspend */
805 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
806 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
807 sleep(1);
809 if (res < 0) {
810 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Write error: %s\n", snd_strerror(res));
811 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Trying to reset soundcard.\n");
812 if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
813 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(res));
814 return 0;
815 break;
818 } while (res == 0);
820 return res < 0 ? res : res * bytes_per_sample;
823 /* how many byes are free in the buffer */
824 static int get_space(void)
826 snd_pcm_status_t *status;
827 int ret;
829 snd_pcm_status_alloca(&status);
831 if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
833 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Cannot get pcm status: %s\n", snd_strerror(ret));
834 return 0;
837 unsigned space = snd_pcm_status_get_avail(status) * bytes_per_sample;
838 if (space > ao_data.buffersize) // Buffer underrun?
839 space = ao_data.buffersize;
840 return space;
843 /* delay in seconds between first and last sample in buffer */
844 static float get_delay(void)
846 if (alsa_handler) {
847 snd_pcm_sframes_t delay;
849 if (snd_pcm_delay(alsa_handler, &delay) < 0)
850 return 0;
852 if (delay < 0) {
853 /* underrun - move the application pointer forward to catch up */
854 #if SND_LIB_VERSION >= 0x000901 /* snd_pcm_forward() exists since 0.9.0rc8 */
855 snd_pcm_forward(alsa_handler, -delay);
856 #endif
857 delay = 0;
859 return (float)delay / (float)ao_data.samplerate;
860 } else {
861 return 0;