Merge svn changes up to r30136
[mplayer/glamo.git] / libao2 / ao_alsa.c
blob4c92597765feeb2c64153e7442f281c92239df63
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 ao_noblock = 0;
82 static int open_mode;
83 static int alsa_can_pause = 0;
84 static snd_pcm_sframes_t prepause_frames;
86 #define ALSA_DEVICE_SIZE 256
88 static void alsa_error_handler(const char *file, int line, const char *function,
89 int err, const char *format, ...)
91 char tmp[0xc00];
92 va_list va;
94 va_start(va, format);
95 vsnprintf(tmp, sizeof tmp, format, va);
96 va_end(va);
97 tmp[sizeof tmp - 1] = '\0';
99 if (err)
100 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n",
101 file, line, function, tmp, snd_strerror(err));
102 else
103 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n",
104 file, line, function, tmp);
107 /* to set/get/query special features/parameters */
108 static int control(int cmd, void *arg)
110 switch(cmd) {
111 case AOCONTROL_QUERY_FORMAT:
112 return CONTROL_TRUE;
113 case AOCONTROL_GET_VOLUME:
114 case AOCONTROL_SET_VOLUME:
116 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
118 int err;
119 snd_mixer_t *handle;
120 snd_mixer_elem_t *elem;
121 snd_mixer_selem_id_t *sid;
123 static char *mix_name = "PCM";
124 static char *card = "default";
125 static int mix_index = 0;
127 long pmin, pmax;
128 long get_vol, set_vol;
129 float f_multi;
131 if(ao_data.format == AF_FORMAT_AC3)
132 return CONTROL_TRUE;
134 if(mixer_channel) {
135 char *test_mix_index;
137 mix_name = strdup(mixer_channel);
138 if ((test_mix_index = strchr(mix_name, ','))){
139 *test_mix_index = 0;
140 test_mix_index++;
141 mix_index = strtol(test_mix_index, &test_mix_index, 0);
143 if (*test_mix_index){
144 mp_tmsg(MSGT_AO,MSGL_ERR,
145 "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n");
146 mix_index = 0 ;
150 if(mixer_device) card = mixer_device;
152 //allocate simple id
153 snd_mixer_selem_id_alloca(&sid);
155 //sets simple-mixer index and name
156 snd_mixer_selem_id_set_index(sid, mix_index);
157 snd_mixer_selem_id_set_name(sid, mix_name);
159 if (mixer_channel) {
160 free(mix_name);
161 mix_name = NULL;
164 if ((err = snd_mixer_open(&handle, 0)) < 0) {
165 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer open error: %s\n", snd_strerror(err));
166 return CONTROL_ERROR;
169 if ((err = snd_mixer_attach(handle, card)) < 0) {
170 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer attach %s error: %s\n",
171 card, snd_strerror(err));
172 snd_mixer_close(handle);
173 return CONTROL_ERROR;
176 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
177 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer register error: %s\n", snd_strerror(err));
178 snd_mixer_close(handle);
179 return CONTROL_ERROR;
181 err = snd_mixer_load(handle);
182 if (err < 0) {
183 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer load error: %s\n", snd_strerror(err));
184 snd_mixer_close(handle);
185 return CONTROL_ERROR;
188 elem = snd_mixer_find_selem(handle, sid);
189 if (!elem) {
190 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to find simple control '%s',%i.\n",
191 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
192 snd_mixer_close(handle);
193 return CONTROL_ERROR;
196 snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
197 f_multi = (100 / (float)(pmax - pmin));
199 if (cmd == AOCONTROL_SET_VOLUME) {
201 set_vol = vol->left / f_multi + pmin + 0.5;
203 //setting channels
204 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol)) < 0) {
205 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting left channel, %s\n",
206 snd_strerror(err));
207 snd_mixer_close(handle);
208 return CONTROL_ERROR;
210 mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol);
212 set_vol = vol->right / f_multi + pmin + 0.5;
214 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol)) < 0) {
215 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting right channel, %s\n",
216 snd_strerror(err));
217 snd_mixer_close(handle);
218 return CONTROL_ERROR;
220 mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n",
221 set_vol, pmin, pmax, f_multi);
223 if (snd_mixer_selem_has_playback_switch(elem)) {
224 int lmute = (vol->left == 0.0);
225 int rmute = (vol->right == 0.0);
226 if (snd_mixer_selem_has_playback_switch_joined(elem)) {
227 lmute = rmute = lmute && rmute;
228 } else {
229 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute);
231 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute);
234 else {
235 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &get_vol);
236 vol->left = (get_vol - pmin) * f_multi;
237 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol);
238 vol->right = (get_vol - pmin) * f_multi;
240 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right);
242 snd_mixer_close(handle);
243 return CONTROL_OK;
246 } //end switch
247 return CONTROL_UNKNOWN;
250 static void parse_device (char *dest, const char *src, int len)
252 char *tmp;
253 memmove(dest, src, len);
254 dest[len] = 0;
255 while ((tmp = strrchr(dest, '.')))
256 tmp[0] = ',';
257 while ((tmp = strrchr(dest, '=')))
258 tmp[0] = ':';
261 static void print_help (void)
263 mp_tmsg (MSGT_AO, MSGL_FATAL,
264 "\n[AO_ALSA] -ao alsa commandline help:\n"\
265 "[AO_ALSA] Example: mplayer -ao alsa:device=hw=0.3\n"\
266 "[AO_ALSA] Sets first card fourth hardware device.\n\n"\
267 "[AO_ALSA] Options:\n"\
268 "[AO_ALSA] noblock\n"\
269 "[AO_ALSA] Opens device in non-blocking mode.\n"\
270 "[AO_ALSA] device=<device-name>\n"\
271 "[AO_ALSA] Sets device (change , to . and : to =)\n");
274 static int str_maxlen(strarg_t *str) {
275 if (str->len > ALSA_DEVICE_SIZE)
276 return 0;
277 return 1;
280 static int try_open_device(const char *device, int open_mode, int try_ac3)
282 int err, len;
283 char *ac3_device, *args;
285 if (try_ac3) {
286 /* to set the non-audio bit, use AES0=6 */
287 len = strlen(device);
288 ac3_device = malloc(len + 7 + 1);
289 if (!ac3_device)
290 return -ENOMEM;
291 strcpy(ac3_device, device);
292 args = strchr(ac3_device, ':');
293 if (!args) {
294 /* no existing parameters: add it behind device name */
295 strcat(ac3_device, ":AES0=6");
296 } else {
298 ++args;
299 while (isspace(*args));
300 if (*args == '\0') {
301 /* ":" but no parameters */
302 strcat(ac3_device, "AES0=6");
303 } else if (*args != '{') {
304 /* a simple list of parameters: add it at the end of the list */
305 strcat(ac3_device, ",AES0=6");
306 } else {
307 /* parameters in config syntax: add it inside the { } block */
309 --len;
310 while (len > 0 && isspace(ac3_device[len]));
311 if (ac3_device[len] == '}')
312 strcpy(ac3_device + len, " AES0=6}");
315 err = snd_pcm_open(&alsa_handler, ac3_device, SND_PCM_STREAM_PLAYBACK,
316 open_mode);
317 free(ac3_device);
319 if (!try_ac3 || err < 0)
320 err = snd_pcm_open(&alsa_handler, device, SND_PCM_STREAM_PLAYBACK,
321 open_mode);
322 return err;
326 open & setup audio device
327 return: 1=success 0=fail
329 static int init(int rate_hz, int channels, int format, int flags)
331 int err;
332 int block;
333 strarg_t device;
334 snd_pcm_uframes_t chunk_size;
335 snd_pcm_uframes_t bufsize;
336 snd_pcm_uframes_t boundary;
337 const opt_t subopts[] = {
338 {"block", OPT_ARG_BOOL, &block, NULL},
339 {"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen},
340 {NULL}
343 char alsa_device[ALSA_DEVICE_SIZE + 1];
344 // make sure alsa_device is null-terminated even when using strncpy etc.
345 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1);
347 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz,
348 channels, format);
349 alsa_handler = NULL;
350 #if SND_LIB_VERSION >= 0x010005
351 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version());
352 #else
353 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
354 #endif
356 prepause_frames = 0;
358 snd_lib_error_set_handler(alsa_error_handler);
360 ao_data.samplerate = rate_hz;
361 ao_data.format = format;
362 ao_data.channels = channels;
364 switch (format)
366 case AF_FORMAT_S8:
367 alsa_format = SND_PCM_FORMAT_S8;
368 break;
369 case AF_FORMAT_U8:
370 alsa_format = SND_PCM_FORMAT_U8;
371 break;
372 case AF_FORMAT_U16_LE:
373 alsa_format = SND_PCM_FORMAT_U16_LE;
374 break;
375 case AF_FORMAT_U16_BE:
376 alsa_format = SND_PCM_FORMAT_U16_BE;
377 break;
378 #if !HAVE_BIGENDIAN
379 case AF_FORMAT_AC3:
380 #endif
381 case AF_FORMAT_S16_LE:
382 alsa_format = SND_PCM_FORMAT_S16_LE;
383 break;
384 #if HAVE_BIGENDIAN
385 case AF_FORMAT_AC3:
386 #endif
387 case AF_FORMAT_S16_BE:
388 alsa_format = SND_PCM_FORMAT_S16_BE;
389 break;
390 case AF_FORMAT_U32_LE:
391 alsa_format = SND_PCM_FORMAT_U32_LE;
392 break;
393 case AF_FORMAT_U32_BE:
394 alsa_format = SND_PCM_FORMAT_U32_BE;
395 break;
396 case AF_FORMAT_S32_LE:
397 alsa_format = SND_PCM_FORMAT_S32_LE;
398 break;
399 case AF_FORMAT_S32_BE:
400 alsa_format = SND_PCM_FORMAT_S32_BE;
401 break;
402 case AF_FORMAT_U24_LE:
403 alsa_format = SND_PCM_FORMAT_U24_3LE;
404 break;
405 case AF_FORMAT_U24_BE:
406 alsa_format = SND_PCM_FORMAT_U24_3BE;
407 break;
408 case AF_FORMAT_S24_LE:
409 alsa_format = SND_PCM_FORMAT_S24_3LE;
410 break;
411 case AF_FORMAT_S24_BE:
412 alsa_format = SND_PCM_FORMAT_S24_3BE;
413 break;
414 case AF_FORMAT_FLOAT_LE:
415 alsa_format = SND_PCM_FORMAT_FLOAT_LE;
416 break;
417 case AF_FORMAT_FLOAT_BE:
418 alsa_format = SND_PCM_FORMAT_FLOAT_BE;
419 break;
420 case AF_FORMAT_MU_LAW:
421 alsa_format = SND_PCM_FORMAT_MU_LAW;
422 break;
423 case AF_FORMAT_A_LAW:
424 alsa_format = SND_PCM_FORMAT_A_LAW;
425 break;
427 default:
428 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1
429 break;
432 //subdevice parsing
433 // set defaults
434 block = 1;
435 /* switch for spdif
436 * sets opening sequence for SPDIF
437 * sets also the playback and other switches 'on the fly'
438 * while opening the abstract alias for the spdif subdevice
439 * 'iec958'
441 if (format == AF_FORMAT_AC3) {
442 device.str = "iec958";
443 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels);
445 else
446 /* in any case for multichannel playback we should select
447 * appropriate device
449 switch (channels) {
450 case 1:
451 case 2:
452 device.str = "default";
453 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n");
454 break;
455 case 4:
456 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
457 // hack - use the converter plugin
458 device.str = "plug:surround40";
459 else
460 device.str = "surround40";
461 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n");
462 break;
463 case 6:
464 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
465 device.str = "plug:surround51";
466 else
467 device.str = "surround51";
468 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n");
469 break;
470 case 8:
471 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
472 device.str = "plug:surround71";
473 else
474 device.str = "surround71";
475 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround71\n");
476 break;
477 default:
478 device.str = "default";
479 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] %d channels are not supported.\n",channels);
481 device.len = strlen(device.str);
482 if (subopt_parse(ao_subdevice, subopts) != 0) {
483 print_help();
484 return 0;
486 ao_noblock = !block;
487 parse_device(alsa_device, device.str, device.len);
489 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device);
491 //setting modes for block or nonblock-mode
492 if (ao_noblock) {
493 open_mode = SND_PCM_NONBLOCK;
495 else {
496 open_mode = 0;
499 if (!alsa_handler) {
500 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
501 if ((err = try_open_device(alsa_device, open_mode, format == AF_FORMAT_AC3)) < 0)
503 if (err != -EBUSY && ao_noblock) {
504 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Open in nonblock-mode failed, trying to open in block-mode.\n");
505 if ((err = try_open_device(alsa_device, 0, format == AF_FORMAT_AC3)) < 0) {
506 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
507 return 0;
509 } else {
510 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
511 return 0;
515 if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) {
516 mp_tmsg(MSGT_AO,MSGL_ERR,"[AL_ALSA] Error setting block-mode %s.\n", snd_strerror(err));
517 } else {
518 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opened in blocking mode\n");
521 snd_pcm_hw_params_alloca(&alsa_hwparams);
522 snd_pcm_sw_params_alloca(&alsa_swparams);
524 // setting hw-parameters
525 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
527 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get initial parameters: %s\n",
528 snd_strerror(err));
529 return 0;
532 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,
533 SND_PCM_ACCESS_RW_INTERLEAVED);
534 if (err < 0) {
535 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set access type: %s\n",
536 snd_strerror(err));
537 return 0;
540 /* workaround for nonsupported formats
541 sets default format to S16_LE if the given formats aren't supported */
542 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams,
543 alsa_format)) < 0)
545 mp_tmsg(MSGT_AO,MSGL_INFO,
546 "[AO_ALSA] Format %s is not supported by hardware, trying default.\n", af_fmt2str_short(format));
547 alsa_format = SND_PCM_FORMAT_S16_LE;
548 ao_data.format = AF_FORMAT_S16_LE;
551 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
552 alsa_format)) < 0)
554 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set format: %s\n",
555 snd_strerror(err));
556 return 0;
559 if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams,
560 &ao_data.channels)) < 0)
562 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set channels: %s\n",
563 snd_strerror(err));
564 return 0;
567 /* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11)
568 prefer our own resampler */
569 #if SND_LIB_VERSION >= 0x010009
570 if ((err = snd_pcm_hw_params_set_rate_resample(alsa_handler, alsa_hwparams,
571 0)) < 0)
573 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to disable resampling: %s\n",
574 snd_strerror(err));
575 return 0;
577 #endif
579 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams,
580 &ao_data.samplerate, NULL)) < 0)
582 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set samplerate-2: %s\n",
583 snd_strerror(err));
584 return 0;
587 bytes_per_sample = snd_pcm_format_physical_width(alsa_format) / 8;
588 bytes_per_sample *= ao_data.channels;
589 ao_data.bps = ao_data.samplerate * bytes_per_sample;
591 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams,
592 &(unsigned int){BUFFER_TIME}, NULL)) < 0)
594 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set buffer time near: %s\n",
595 snd_strerror(err));
596 return 0;
599 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams,
600 &(unsigned int){FRAGCOUNT}, NULL)) < 0) {
601 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set periods: %s\n",
602 snd_strerror(err));
603 return 0;
606 /* finally install hardware parameters */
607 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
609 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set hw-parameters: %s\n",
610 snd_strerror(err));
611 return 0;
613 // end setting hw-params
616 // gets buffersize for control
617 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0)
619 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get buffersize: %s\n", snd_strerror(err));
620 return 0;
622 else {
623 ao_data.buffersize = bufsize * bytes_per_sample;
624 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize);
627 if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL)) < 0) {
628 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO ALSA] Unable to get period size: %s\n", snd_strerror(err));
629 return 0;
630 } else {
631 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got period size %li\n", chunk_size);
633 ao_data.outburst = chunk_size * bytes_per_sample;
635 /* setting software parameters */
636 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) {
637 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
638 snd_strerror(err));
639 return 0;
641 #if SND_LIB_VERSION >= 0x000901
642 if ((err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary)) < 0) {
643 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get boundary: %s\n",
644 snd_strerror(err));
645 return 0;
647 #else
648 boundary = 0x7fffffff;
649 #endif
650 /* start playing when one period has been written */
651 if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, chunk_size)) < 0) {
652 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set start threshold: %s\n",
653 snd_strerror(err));
654 return 0;
656 /* disable underrun reporting */
657 if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, boundary)) < 0) {
658 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set stop threshold: %s\n",
659 snd_strerror(err));
660 return 0;
662 #if SND_LIB_VERSION >= 0x000901
663 /* play silence when there is an underrun */
664 if ((err = snd_pcm_sw_params_set_silence_size(alsa_handler, alsa_swparams, boundary)) < 0) {
665 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set silence size: %s\n",
666 snd_strerror(err));
667 return 0;
669 #endif
670 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) {
671 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
672 snd_strerror(err));
673 return 0;
675 /* end setting sw-params */
677 mp_msg(MSGT_AO,MSGL_V,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
678 ao_data.samplerate, ao_data.channels, (int)bytes_per_sample, ao_data.buffersize,
679 snd_pcm_format_description(alsa_format));
681 } // end switch alsa_handler (spdif)
682 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
683 return 1;
684 } // end init
687 /* close audio device */
688 static void uninit(int immed)
691 if (alsa_handler) {
692 int err;
694 if (!immed)
695 snd_pcm_drain(alsa_handler);
697 if ((err = snd_pcm_close(alsa_handler)) < 0)
699 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm close error: %s\n", snd_strerror(err));
700 return;
702 else {
703 alsa_handler = NULL;
704 mp_msg(MSGT_AO,MSGL_V,"alsa-uninit: pcm closed\n");
707 else {
708 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] No handler defined!\n");
712 static void audio_pause(void)
714 int err;
716 if (alsa_can_pause) {
717 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0)
719 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm pause error: %s\n", snd_strerror(err));
720 return;
722 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n");
723 } else {
724 if (snd_pcm_delay(alsa_handler, &prepause_frames) < 0
725 || prepause_frames < 0)
726 prepause_frames = 0;
728 if ((err = snd_pcm_drop(alsa_handler)) < 0)
730 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm drop error: %s\n", snd_strerror(err));
731 return;
736 static void audio_resume(void)
738 int err;
740 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
741 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
742 while ((err = snd_pcm_resume(alsa_handler)) == -EAGAIN) sleep(1);
744 if (alsa_can_pause) {
745 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0)
747 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm resume error: %s\n", snd_strerror(err));
748 return;
750 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n");
751 } else {
752 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
754 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
755 return;
757 if (prepause_frames) {
758 void *silence = calloc(prepause_frames, bytes_per_sample);
759 play(silence, prepause_frames * bytes_per_sample, 0);
760 free(silence);
765 /* stop playing and empty buffers (for seeking/pause) */
766 static void reset(void)
768 int err;
770 prepause_frames = 0;
771 if ((err = snd_pcm_drop(alsa_handler)) < 0)
773 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
774 return;
776 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
778 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
779 return;
781 return;
785 plays 'len' bytes of 'data'
786 returns: number of bytes played
787 modified last at 29.06.02 by jp
788 thanxs for marius <marius@rospot.com> for giving us the light ;)
791 static int play(void* data, int len, int flags)
793 int num_frames;
794 snd_pcm_sframes_t res = 0;
795 if (!(flags & AOPLAY_FINAL_CHUNK))
796 len = len / ao_data.outburst * ao_data.outburst;
797 num_frames = len / bytes_per_sample;
799 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len);
801 if (!alsa_handler) {
802 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Device configuration error.");
803 return 0;
806 if (num_frames == 0)
807 return 0;
809 do {
810 res = snd_pcm_writei(alsa_handler, data, num_frames);
812 if (res == -EINTR) {
813 /* nothing to do */
814 res = 0;
816 else if (res == -ESTRPIPE) { /* suspend */
817 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
818 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
819 sleep(1);
821 if (res < 0) {
822 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Write error: %s\n", snd_strerror(res));
823 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Trying to reset soundcard.\n");
824 if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
825 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(res));
826 return 0;
827 break;
830 } while (res == 0);
832 return res < 0 ? res : res * bytes_per_sample;
835 /* how many byes are free in the buffer */
836 static int get_space(void)
838 snd_pcm_status_t *status;
839 int ret;
841 snd_pcm_status_alloca(&status);
843 if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
845 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Cannot get pcm status: %s\n", snd_strerror(ret));
846 return 0;
849 unsigned space = snd_pcm_status_get_avail(status) * bytes_per_sample;
850 if (space > ao_data.buffersize) // Buffer underrun?
851 space = ao_data.buffersize;
852 return space;
855 /* delay in seconds between first and last sample in buffer */
856 static float get_delay(void)
858 if (alsa_handler) {
859 snd_pcm_sframes_t delay;
861 if (snd_pcm_delay(alsa_handler, &delay) < 0)
862 return 0;
864 if (delay < 0) {
865 /* underrun - move the application pointer forward to catch up */
866 #if SND_LIB_VERSION >= 0x000901 /* snd_pcm_forward() exists since 0.9.0rc8 */
867 snd_pcm_forward(alsa_handler, -delay);
868 #endif
869 delay = 0;
871 return (float)delay / (float)ao_data.samplerate;
872 } else {
873 return 0;