Merge svn changes up to r29417
[mplayer/glamo.git] / libao2 / ao_alsa.c
blob4c3629b8c9bb0e69bb247ac1e697d8355ef0f88a
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 /* 16 sets buffersize to 16 * chunksize is as default 1024
76 * which seems to be good avarge for most situations
77 * so buffersize is 16384 frames by default */
78 static int alsa_fragcount = 16;
79 static snd_pcm_uframes_t chunk_size = 1024;
81 static size_t bytes_per_sample;
83 static int ao_noblock = 0;
85 static int open_mode;
86 static int alsa_can_pause = 0;
87 static snd_pcm_sframes_t prepause_frames;
89 #define ALSA_DEVICE_SIZE 256
91 #undef BUFFERTIME
92 #define SET_CHUNKSIZE
94 static void alsa_error_handler(const char *file, int line, const char *function,
95 int err, const char *format, ...)
97 char tmp[0xc00];
98 va_list va;
100 va_start(va, format);
101 vsnprintf(tmp, sizeof tmp, format, va);
102 va_end(va);
103 tmp[sizeof tmp - 1] = '\0';
105 if (err)
106 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n",
107 file, line, function, tmp, snd_strerror(err));
108 else
109 mp_msg(MSGT_AO, MSGL_ERR, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n",
110 file, line, function, tmp);
113 /* to set/get/query special features/parameters */
114 static int control(int cmd, void *arg)
116 switch(cmd) {
117 case AOCONTROL_QUERY_FORMAT:
118 return CONTROL_TRUE;
119 case AOCONTROL_GET_VOLUME:
120 case AOCONTROL_SET_VOLUME:
122 ao_control_vol_t *vol = (ao_control_vol_t *)arg;
124 int err;
125 snd_mixer_t *handle;
126 snd_mixer_elem_t *elem;
127 snd_mixer_selem_id_t *sid;
129 static char *mix_name = "PCM";
130 static char *card = "default";
131 static int mix_index = 0;
133 long pmin, pmax;
134 long get_vol, set_vol;
135 float f_multi;
137 if(ao_data.format == AF_FORMAT_AC3)
138 return CONTROL_TRUE;
140 if(mixer_channel) {
141 char *test_mix_index;
143 mix_name = strdup(mixer_channel);
144 if ((test_mix_index = strchr(mix_name, ','))){
145 *test_mix_index = 0;
146 test_mix_index++;
147 mix_index = strtol(test_mix_index, &test_mix_index, 0);
149 if (*test_mix_index){
150 mp_tmsg(MSGT_AO,MSGL_ERR,
151 "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n");
152 mix_index = 0 ;
156 if(mixer_device) card = mixer_device;
158 //allocate simple id
159 snd_mixer_selem_id_alloca(&sid);
161 //sets simple-mixer index and name
162 snd_mixer_selem_id_set_index(sid, mix_index);
163 snd_mixer_selem_id_set_name(sid, mix_name);
165 if (mixer_channel) {
166 free(mix_name);
167 mix_name = NULL;
170 if ((err = snd_mixer_open(&handle, 0)) < 0) {
171 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer open error: %s\n", snd_strerror(err));
172 return CONTROL_ERROR;
175 if ((err = snd_mixer_attach(handle, card)) < 0) {
176 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer attach %s error: %s\n",
177 card, snd_strerror(err));
178 snd_mixer_close(handle);
179 return CONTROL_ERROR;
182 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
183 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer register error: %s\n", snd_strerror(err));
184 snd_mixer_close(handle);
185 return CONTROL_ERROR;
187 err = snd_mixer_load(handle);
188 if (err < 0) {
189 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Mixer load error: %s\n", snd_strerror(err));
190 snd_mixer_close(handle);
191 return CONTROL_ERROR;
194 elem = snd_mixer_find_selem(handle, sid);
195 if (!elem) {
196 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to find simple control '%s',%i.\n",
197 snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
198 snd_mixer_close(handle);
199 return CONTROL_ERROR;
202 snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
203 f_multi = (100 / (float)(pmax - pmin));
205 if (cmd == AOCONTROL_SET_VOLUME) {
207 set_vol = vol->left / f_multi + pmin + 0.5;
209 //setting channels
210 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, set_vol)) < 0) {
211 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting left channel, %s\n",
212 snd_strerror(err));
213 return CONTROL_ERROR;
215 mp_msg(MSGT_AO,MSGL_DBG2,"left=%li, ", set_vol);
217 set_vol = vol->right / f_multi + pmin + 0.5;
219 if ((err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, set_vol)) < 0) {
220 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Error setting right channel, %s\n",
221 snd_strerror(err));
222 return CONTROL_ERROR;
224 mp_msg(MSGT_AO,MSGL_DBG2,"right=%li, pmin=%li, pmax=%li, mult=%f\n",
225 set_vol, pmin, pmax, f_multi);
227 if (snd_mixer_selem_has_playback_switch(elem)) {
228 int lmute = (vol->left == 0.0);
229 int rmute = (vol->right == 0.0);
230 if (snd_mixer_selem_has_playback_switch_joined(elem)) {
231 lmute = rmute = lmute && rmute;
232 } else {
233 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_RIGHT, !rmute);
235 snd_mixer_selem_set_playback_switch(elem, SND_MIXER_SCHN_FRONT_LEFT, !lmute);
238 else {
239 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &get_vol);
240 vol->left = (get_vol - pmin) * f_multi;
241 snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &get_vol);
242 vol->right = (get_vol - pmin) * f_multi;
244 mp_msg(MSGT_AO,MSGL_DBG2,"left=%f, right=%f\n",vol->left,vol->right);
246 snd_mixer_close(handle);
247 return CONTROL_OK;
250 } //end switch
251 return CONTROL_UNKNOWN;
254 static void parse_device (char *dest, const char *src, int len)
256 char *tmp;
257 memmove(dest, src, len);
258 dest[len] = 0;
259 while ((tmp = strrchr(dest, '.')))
260 tmp[0] = ',';
261 while ((tmp = strrchr(dest, '=')))
262 tmp[0] = ':';
265 static void print_help (void)
267 mp_tmsg (MSGT_AO, MSGL_FATAL,
268 "\n[AO_ALSA] -ao alsa commandline help:\n"\
269 "[AO_ALSA] Example: mplayer -ao alsa:device=hw=0.3\n"\
270 "[AO_ALSA] Sets first card fourth hardware device.\n\n"\
271 "[AO_ALSA] Options:\n"\
272 "[AO_ALSA] noblock\n"\
273 "[AO_ALSA] Opens device in non-blocking mode.\n"\
274 "[AO_ALSA] device=<device-name>\n"\
275 "[AO_ALSA] Sets device (change , to . and : to =)\n");
278 static int str_maxlen(strarg_t *str) {
279 if (str->len > ALSA_DEVICE_SIZE)
280 return 0;
281 return 1;
284 static int try_open_device(const char *device, int open_mode, int try_ac3)
286 int err, len;
287 char *ac3_device, *args;
289 if (try_ac3) {
290 /* to set the non-audio bit, use AES0=6 */
291 len = strlen(device);
292 ac3_device = malloc(len + 7 + 1);
293 if (!ac3_device)
294 return -ENOMEM;
295 strcpy(ac3_device, device);
296 args = strchr(ac3_device, ':');
297 if (!args) {
298 /* no existing parameters: add it behind device name */
299 strcat(ac3_device, ":AES0=6");
300 } else {
302 ++args;
303 while (isspace(*args));
304 if (*args == '\0') {
305 /* ":" but no parameters */
306 strcat(ac3_device, "AES0=6");
307 } else if (*args != '{') {
308 /* a simple list of parameters: add it at the end of the list */
309 strcat(ac3_device, ",AES0=6");
310 } else {
311 /* parameters in config syntax: add it inside the { } block */
313 --len;
314 while (len > 0 && isspace(ac3_device[len]));
315 if (ac3_device[len] == '}')
316 strcpy(ac3_device + len, " AES0=6}");
319 err = snd_pcm_open(&alsa_handler, ac3_device, SND_PCM_STREAM_PLAYBACK,
320 open_mode);
321 free(ac3_device);
323 if (!try_ac3 || err < 0)
324 err = snd_pcm_open(&alsa_handler, device, SND_PCM_STREAM_PLAYBACK,
325 open_mode);
326 return err;
330 open & setup audio device
331 return: 1=success 0=fail
333 static int init(int rate_hz, int channels, int format, int flags)
335 int err;
336 int block;
337 strarg_t device;
338 snd_pcm_uframes_t bufsize;
339 snd_pcm_uframes_t boundary;
340 opt_t subopts[] = {
341 {"block", OPT_ARG_BOOL, &block, NULL},
342 {"device", OPT_ARG_STR, &device, (opt_test_f)str_maxlen},
343 {NULL}
346 char alsa_device[ALSA_DEVICE_SIZE + 1];
347 // make sure alsa_device is null-terminated even when using strncpy etc.
348 memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1);
350 mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz,
351 channels, format);
352 alsa_handler = NULL;
353 #if SND_LIB_VERSION >= 0x010005
354 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version());
355 #else
356 mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
357 #endif
359 prepause_frames = 0;
361 snd_lib_error_set_handler(alsa_error_handler);
363 ao_data.samplerate = rate_hz;
364 ao_data.format = format;
365 ao_data.channels = channels;
367 switch (format)
369 case AF_FORMAT_S8:
370 alsa_format = SND_PCM_FORMAT_S8;
371 break;
372 case AF_FORMAT_U8:
373 alsa_format = SND_PCM_FORMAT_U8;
374 break;
375 case AF_FORMAT_U16_LE:
376 alsa_format = SND_PCM_FORMAT_U16_LE;
377 break;
378 case AF_FORMAT_U16_BE:
379 alsa_format = SND_PCM_FORMAT_U16_BE;
380 break;
381 #ifndef WORDS_BIGENDIAN
382 case AF_FORMAT_AC3:
383 #endif
384 case AF_FORMAT_S16_LE:
385 alsa_format = SND_PCM_FORMAT_S16_LE;
386 break;
387 #ifdef WORDS_BIGENDIAN
388 case AF_FORMAT_AC3:
389 #endif
390 case AF_FORMAT_S16_BE:
391 alsa_format = SND_PCM_FORMAT_S16_BE;
392 break;
393 case AF_FORMAT_U32_LE:
394 alsa_format = SND_PCM_FORMAT_U32_LE;
395 break;
396 case AF_FORMAT_U32_BE:
397 alsa_format = SND_PCM_FORMAT_U32_BE;
398 break;
399 case AF_FORMAT_S32_LE:
400 alsa_format = SND_PCM_FORMAT_S32_LE;
401 break;
402 case AF_FORMAT_S32_BE:
403 alsa_format = SND_PCM_FORMAT_S32_BE;
404 break;
405 case AF_FORMAT_FLOAT_LE:
406 alsa_format = SND_PCM_FORMAT_FLOAT_LE;
407 break;
408 case AF_FORMAT_FLOAT_BE:
409 alsa_format = SND_PCM_FORMAT_FLOAT_BE;
410 break;
411 case AF_FORMAT_MU_LAW:
412 alsa_format = SND_PCM_FORMAT_MU_LAW;
413 break;
414 case AF_FORMAT_A_LAW:
415 alsa_format = SND_PCM_FORMAT_A_LAW;
416 break;
418 default:
419 alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1
420 break;
423 //subdevice parsing
424 // set defaults
425 block = 1;
426 /* switch for spdif
427 * sets opening sequence for SPDIF
428 * sets also the playback and other switches 'on the fly'
429 * while opening the abstract alias for the spdif subdevice
430 * 'iec958'
432 if (format == AF_FORMAT_AC3) {
433 device.str = "iec958";
434 mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3, %i channels\n", channels);
436 else
437 /* in any case for multichannel playback we should select
438 * appropriate device
440 switch (channels) {
441 case 1:
442 case 2:
443 device.str = "default";
444 mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n");
445 break;
446 case 4:
447 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
448 // hack - use the converter plugin
449 device.str = "plug:surround40";
450 else
451 device.str = "surround40";
452 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n");
453 break;
454 case 6:
455 if (alsa_format == SND_PCM_FORMAT_FLOAT_LE)
456 device.str = "plug:surround51";
457 else
458 device.str = "surround51";
459 mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n");
460 break;
461 default:
462 device.str = "default";
463 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] %d channels are not supported.\n",channels);
465 device.len = strlen(device.str);
466 if (subopt_parse(ao_subdevice, subopts) != 0) {
467 print_help();
468 return 0;
470 ao_noblock = !block;
471 parse_device(alsa_device, device.str, device.len);
473 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device);
475 //setting modes for block or nonblock-mode
476 if (ao_noblock) {
477 open_mode = SND_PCM_NONBLOCK;
479 else {
480 open_mode = 0;
483 //sets buff/chunksize if its set manually
484 if (ao_data.buffersize) {
485 switch (ao_data.buffersize)
487 case 1:
488 alsa_fragcount = 16;
489 chunk_size = 512;
490 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n");
491 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n");
492 break;
493 case 2:
494 alsa_fragcount = 8;
495 chunk_size = 1024;
496 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 8192\n");
497 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n");
498 break;
499 case 3:
500 alsa_fragcount = 32;
501 chunk_size = 512;
502 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n");
503 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 512\n");
504 break;
505 case 4:
506 alsa_fragcount = 16;
507 chunk_size = 1024;
508 mp_msg(MSGT_AO,MSGL_V,"alsa-init: buffersize set manually to 16384\n");
509 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set manually to 1024\n");
510 break;
511 default:
512 alsa_fragcount = 16;
513 chunk_size = 1024;
514 break;
518 if (!alsa_handler) {
519 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
520 if ((err = try_open_device(alsa_device, open_mode, format == AF_FORMAT_AC3)) < 0)
522 if (err != -EBUSY && ao_noblock) {
523 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Open in nonblock-mode failed, trying to open in block-mode.\n");
524 if ((err = try_open_device(alsa_device, 0, format == AF_FORMAT_AC3)) < 0) {
525 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
526 return 0;
528 } else {
529 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err));
530 return 0;
534 if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) {
535 mp_tmsg(MSGT_AO,MSGL_ERR,"[AL_ALSA] Error setting block-mode %s.\n", snd_strerror(err));
536 } else {
537 mp_msg(MSGT_AO,MSGL_V,"alsa-init: pcm opened in blocking mode\n");
540 snd_pcm_hw_params_alloca(&alsa_hwparams);
541 snd_pcm_sw_params_alloca(&alsa_swparams);
543 // setting hw-parameters
544 if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
546 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get initial parameters: %s\n",
547 snd_strerror(err));
548 return 0;
551 err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,
552 SND_PCM_ACCESS_RW_INTERLEAVED);
553 if (err < 0) {
554 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set access type: %s\n",
555 snd_strerror(err));
556 return 0;
559 /* workaround for nonsupported formats
560 sets default format to S16_LE if the given formats aren't supported */
561 if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams,
562 alsa_format)) < 0)
564 mp_tmsg(MSGT_AO,MSGL_INFO,
565 "[AO_ALSA] Format %s is not supported by hardware, trying default.\n", af_fmt2str_short(format));
566 alsa_format = SND_PCM_FORMAT_S16_LE;
567 ao_data.format = AF_FORMAT_S16_LE;
570 if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
571 alsa_format)) < 0)
573 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set format: %s\n",
574 snd_strerror(err));
575 return 0;
578 if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams,
579 &ao_data.channels)) < 0)
581 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set channels: %s\n",
582 snd_strerror(err));
583 return 0;
586 /* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11)
587 prefer our own resampler */
588 #if SND_LIB_VERSION >= 0x010009
589 if ((err = snd_pcm_hw_params_set_rate_resample(alsa_handler, alsa_hwparams,
590 0)) < 0)
592 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to disable resampling: %s\n",
593 snd_strerror(err));
594 return 0;
596 #endif
598 if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams,
599 &ao_data.samplerate, NULL)) < 0)
601 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set samplerate-2: %s\n",
602 snd_strerror(err));
603 return 0;
606 bytes_per_sample = snd_pcm_format_physical_width(alsa_format) / 8;
607 bytes_per_sample *= ao_data.channels;
608 ao_data.bps = ao_data.samplerate * bytes_per_sample;
610 #ifdef BUFFERTIME
612 int alsa_buffer_time = 500000; /* original 60 */
613 int alsa_period_time;
614 alsa_period_time = alsa_buffer_time/4;
615 if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams,
616 &alsa_buffer_time, NULL)) < 0)
618 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set buffer time near: %s\n",
619 snd_strerror(err));
620 return 0;
621 } else
622 alsa_buffer_time = err;
624 if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams,
625 &alsa_period_time, NULL)) < 0)
626 /* original: alsa_buffer_time/ao_data.bps */
628 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set period time: %s\n",
629 snd_strerror(err));
630 return 0;
632 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] buffer_time: %d, period_time :%d\n",
633 alsa_buffer_time, err);
635 #endif//end SET_BUFFERTIME
637 #ifdef SET_CHUNKSIZE
639 //set chunksize
640 if ((err = snd_pcm_hw_params_set_period_size_near(alsa_handler, alsa_hwparams,
641 &chunk_size, NULL)) < 0)
643 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO ALSA] Unable to set period size(%ld): %s\n",
644 chunk_size, snd_strerror(err));
645 return 0;
647 else {
648 mp_msg(MSGT_AO,MSGL_V,"alsa-init: chunksize set to %li\n", chunk_size);
650 if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams,
651 &alsa_fragcount, NULL)) < 0) {
652 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set periods: %s\n",
653 snd_strerror(err));
654 return 0;
656 else {
657 mp_msg(MSGT_AO,MSGL_V,"alsa-init: fragcount=%i\n", alsa_fragcount);
660 #endif//end SET_CHUNKSIZE
662 /* finally install hardware parameters */
663 if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
665 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set hw-parameters: %s\n",
666 snd_strerror(err));
667 return 0;
669 // end setting hw-params
672 // gets buffersize for control
673 if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0)
675 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get buffersize: %s\n", snd_strerror(err));
676 return 0;
678 else {
679 ao_data.buffersize = bufsize * bytes_per_sample;
680 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize);
683 if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL)) < 0) {
684 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO ALSA] Unable to get period size: %s\n", snd_strerror(err));
685 return 0;
686 } else {
687 mp_msg(MSGT_AO,MSGL_V,"alsa-init: got period size %li\n", chunk_size);
689 ao_data.outburst = chunk_size * bytes_per_sample;
691 /* setting software parameters */
692 if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) {
693 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
694 snd_strerror(err));
695 return 0;
697 #if SND_LIB_VERSION >= 0x000901
698 if ((err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary)) < 0) {
699 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get boundary: %s\n",
700 snd_strerror(err));
701 return 0;
703 #else
704 boundary = 0x7fffffff;
705 #endif
706 /* start playing when one period has been written */
707 if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, chunk_size)) < 0) {
708 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set start threshold: %s\n",
709 snd_strerror(err));
710 return 0;
712 /* disable underrun reporting */
713 if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, boundary)) < 0) {
714 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set stop threshold: %s\n",
715 snd_strerror(err));
716 return 0;
718 #if SND_LIB_VERSION >= 0x000901
719 /* play silence when there is an underrun */
720 if ((err = snd_pcm_sw_params_set_silence_size(alsa_handler, alsa_swparams, boundary)) < 0) {
721 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to set silence size: %s\n",
722 snd_strerror(err));
723 return 0;
725 #endif
726 if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) {
727 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Unable to get sw-parameters: %s\n",
728 snd_strerror(err));
729 return 0;
731 /* end setting sw-params */
733 mp_msg(MSGT_AO,MSGL_V,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
734 ao_data.samplerate, ao_data.channels, (int)bytes_per_sample, ao_data.buffersize,
735 snd_pcm_format_description(alsa_format));
737 } // end switch alsa_handler (spdif)
738 alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams);
739 return 1;
740 } // end init
743 /* close audio device */
744 static void uninit(int immed)
747 if (alsa_handler) {
748 int err;
750 if (!immed)
751 snd_pcm_drain(alsa_handler);
753 if ((err = snd_pcm_close(alsa_handler)) < 0)
755 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm close error: %s\n", snd_strerror(err));
756 return;
758 else {
759 alsa_handler = NULL;
760 mp_msg(MSGT_AO,MSGL_V,"alsa-uninit: pcm closed\n");
763 else {
764 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] No handler defined!\n");
768 static void audio_pause(void)
770 int err;
772 if (alsa_can_pause) {
773 if ((err = snd_pcm_pause(alsa_handler, 1)) < 0)
775 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm pause error: %s\n", snd_strerror(err));
776 return;
778 mp_msg(MSGT_AO,MSGL_V,"alsa-pause: pause supported by hardware\n");
779 } else {
780 if (snd_pcm_delay(alsa_handler, &prepause_frames) < 0
781 || prepause_frames < 0)
782 prepause_frames = 0;
784 if ((err = snd_pcm_drop(alsa_handler)) < 0)
786 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm drop error: %s\n", snd_strerror(err));
787 return;
792 static void audio_resume(void)
794 int err;
796 if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
797 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
798 while ((err = snd_pcm_resume(alsa_handler)) == -EAGAIN) sleep(1);
800 if (alsa_can_pause) {
801 if ((err = snd_pcm_pause(alsa_handler, 0)) < 0)
803 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm resume error: %s\n", snd_strerror(err));
804 return;
806 mp_msg(MSGT_AO,MSGL_V,"alsa-resume: resume supported by hardware\n");
807 } else {
808 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
810 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
811 return;
813 if (prepause_frames) {
814 void *silence = calloc(prepause_frames, bytes_per_sample);
815 play(silence, prepause_frames * bytes_per_sample, 0);
816 free(silence);
821 /* stop playing and empty buffers (for seeking/pause) */
822 static void reset(void)
824 int err;
826 prepause_frames = 0;
827 if ((err = snd_pcm_drop(alsa_handler)) < 0)
829 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
830 return;
832 if ((err = snd_pcm_prepare(alsa_handler)) < 0)
834 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err));
835 return;
837 return;
841 plays 'len' bytes of 'data'
842 returns: number of bytes played
843 modified last at 29.06.02 by jp
844 thanxs for marius <marius@rospot.com> for giving us the light ;)
847 static int play(void* data, int len, int flags)
849 int num_frames = len / bytes_per_sample;
850 snd_pcm_sframes_t res = 0;
852 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len);
854 if (!alsa_handler) {
855 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Device configuration error.");
856 return 0;
859 if (num_frames == 0)
860 return 0;
862 do {
863 res = snd_pcm_writei(alsa_handler, data, num_frames);
865 if (res == -EINTR) {
866 /* nothing to do */
867 res = 0;
869 else if (res == -ESTRPIPE) { /* suspend */
870 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
871 while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
872 sleep(1);
874 if (res < 0) {
875 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Write error: %s\n", snd_strerror(res));
876 mp_tmsg(MSGT_AO,MSGL_INFO,"[AO_ALSA] Trying to reset soundcard.\n");
877 if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
878 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(res));
879 return 0;
880 break;
883 } while (res == 0);
885 return res < 0 ? res : res * bytes_per_sample;
888 /* how many byes are free in the buffer */
889 static int get_space(void)
891 snd_pcm_status_t *status;
892 int ret;
894 snd_pcm_status_alloca(&status);
896 if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
898 mp_tmsg(MSGT_AO,MSGL_ERR,"[AO_ALSA] Cannot get pcm status: %s\n", snd_strerror(ret));
899 return 0;
902 unsigned space = snd_pcm_status_get_avail(status) * bytes_per_sample;
903 if (space > ao_data.buffersize) // Buffer underrun?
904 space = ao_data.buffersize;
905 return space;
908 /* delay in seconds between first and last sample in buffer */
909 static float get_delay(void)
911 if (alsa_handler) {
912 snd_pcm_sframes_t delay;
914 if (snd_pcm_delay(alsa_handler, &delay) < 0)
915 return 0;
917 if (delay < 0) {
918 /* underrun - move the application pointer forward to catch up */
919 #if SND_LIB_VERSION >= 0x000901 /* snd_pcm_forward() exists since 0.9.0rc8 */
920 snd_pcm_forward(alsa_handler, -delay);
921 #endif
922 delay = 0;
924 return (float)delay / (float)ao_data.samplerate;
925 } else {
926 return 0;