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.
39 #include "subopt-helper.h"
43 #define ALSA_PCM_NEW_HW_PARAMS_API
44 #define ALSA_PCM_NEW_SW_PARAMS_API
46 #include <alsa/asoundlib.h>
48 #include "audio_out.h"
49 #include "audio_out_internal.h"
50 #include "libaf/af_format.h"
52 static const ao_info_t info
=
54 "ALSA-0.9.x-1.x audio output",
56 "Alex Beregszaszi, Zsolt Barat <joy@streamminister.de>",
62 static snd_pcm_t
*alsa_handler
;
63 static snd_pcm_format_t alsa_format
;
64 static snd_pcm_hw_params_t
*alsa_hwparams
;
65 static snd_pcm_sw_params_t
*alsa_swparams
;
67 #define BUFFER_TIME 500000 // 0.5 s
70 static size_t bytes_per_sample
;
72 static int alsa_can_pause
;
73 static snd_pcm_sframes_t prepause_frames
;
75 #define ALSA_DEVICE_SIZE 256
77 static void alsa_error_handler(const char *file
, int line
, const char *function
,
78 int err
, const char *format
, ...)
84 vsnprintf(tmp
, sizeof tmp
, format
, va
);
86 tmp
[sizeof tmp
- 1] = '\0';
89 mp_msg(MSGT_AO
, MSGL_ERR
, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s: %s\n",
90 file
, line
, function
, tmp
, snd_strerror(err
));
92 mp_msg(MSGT_AO
, MSGL_ERR
, "[AO_ALSA] alsa-lib: %s:%i:(%s) %s\n",
93 file
, line
, function
, tmp
);
96 /* to set/get/query special features/parameters */
97 static int control(int cmd
, void *arg
)
100 case AOCONTROL_QUERY_FORMAT
:
102 case AOCONTROL_GET_VOLUME
:
103 case AOCONTROL_SET_VOLUME
:
105 ao_control_vol_t
*vol
= (ao_control_vol_t
*)arg
;
109 snd_mixer_elem_t
*elem
;
110 snd_mixer_selem_id_t
*sid
;
112 char *mix_name
= "Master";
113 char *card
= "default";
117 long get_vol
, set_vol
;
120 if(AF_FORMAT_IS_AC3(ao_data
.format
))
124 char *test_mix_index
;
126 mix_name
= strdup(mixer_channel
);
127 if ((test_mix_index
= strchr(mix_name
, ','))){
130 mix_index
= strtol(test_mix_index
, &test_mix_index
, 0);
132 if (*test_mix_index
){
133 mp_tmsg(MSGT_AO
,MSGL_ERR
,
134 "[AO_ALSA] Invalid mixer index. Defaulting to 0.\n");
139 if(mixer_device
) card
= mixer_device
;
142 snd_mixer_selem_id_alloca(&sid
);
144 //sets simple-mixer index and name
145 snd_mixer_selem_id_set_index(sid
, mix_index
);
146 snd_mixer_selem_id_set_name(sid
, mix_name
);
153 if ((err
= snd_mixer_open(&handle
, 0)) < 0) {
154 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Mixer open error: %s\n", snd_strerror(err
));
155 return CONTROL_ERROR
;
158 if ((err
= snd_mixer_attach(handle
, card
)) < 0) {
159 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Mixer attach %s error: %s\n",
160 card
, snd_strerror(err
));
161 snd_mixer_close(handle
);
162 return CONTROL_ERROR
;
165 if ((err
= snd_mixer_selem_register(handle
, NULL
, NULL
)) < 0) {
166 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Mixer register error: %s\n", snd_strerror(err
));
167 snd_mixer_close(handle
);
168 return CONTROL_ERROR
;
170 err
= snd_mixer_load(handle
);
172 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Mixer load error: %s\n", snd_strerror(err
));
173 snd_mixer_close(handle
);
174 return CONTROL_ERROR
;
177 elem
= snd_mixer_find_selem(handle
, sid
);
179 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to find simple control '%s',%i.\n",
180 snd_mixer_selem_id_get_name(sid
), snd_mixer_selem_id_get_index(sid
));
181 snd_mixer_close(handle
);
182 return CONTROL_ERROR
;
185 snd_mixer_selem_get_playback_volume_range(elem
,&pmin
,&pmax
);
186 f_multi
= (100 / (float)(pmax
- pmin
));
188 if (cmd
== AOCONTROL_SET_VOLUME
) {
190 set_vol
= vol
->left
/ f_multi
+ pmin
+ 0.5;
193 if ((err
= snd_mixer_selem_set_playback_volume(elem
, SND_MIXER_SCHN_FRONT_LEFT
, set_vol
)) < 0) {
194 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Error setting left channel, %s\n",
196 snd_mixer_close(handle
);
197 return CONTROL_ERROR
;
199 mp_msg(MSGT_AO
,MSGL_DBG2
,"left=%li, ", set_vol
);
201 set_vol
= vol
->right
/ f_multi
+ pmin
+ 0.5;
203 if ((err
= snd_mixer_selem_set_playback_volume(elem
, SND_MIXER_SCHN_FRONT_RIGHT
, set_vol
)) < 0) {
204 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Error setting right channel, %s\n",
206 snd_mixer_close(handle
);
207 return CONTROL_ERROR
;
209 mp_msg(MSGT_AO
,MSGL_DBG2
,"right=%li, pmin=%li, pmax=%li, mult=%f\n",
210 set_vol
, pmin
, pmax
, f_multi
);
212 if (snd_mixer_selem_has_playback_switch(elem
)) {
213 int lmute
= (vol
->left
== 0.0);
214 int rmute
= (vol
->right
== 0.0);
215 if (snd_mixer_selem_has_playback_switch_joined(elem
)) {
216 lmute
= rmute
= lmute
&& rmute
;
218 snd_mixer_selem_set_playback_switch(elem
, SND_MIXER_SCHN_FRONT_RIGHT
, !rmute
);
220 snd_mixer_selem_set_playback_switch(elem
, SND_MIXER_SCHN_FRONT_LEFT
, !lmute
);
224 snd_mixer_selem_get_playback_volume(elem
, SND_MIXER_SCHN_FRONT_LEFT
, &get_vol
);
225 vol
->left
= (get_vol
- pmin
) * f_multi
;
226 snd_mixer_selem_get_playback_volume(elem
, SND_MIXER_SCHN_FRONT_RIGHT
, &get_vol
);
227 vol
->right
= (get_vol
- pmin
) * f_multi
;
229 mp_msg(MSGT_AO
,MSGL_DBG2
,"left=%f, right=%f\n",vol
->left
,vol
->right
);
231 snd_mixer_close(handle
);
236 return CONTROL_UNKNOWN
;
239 static void parse_device (char *dest
, const char *src
, int len
)
242 memmove(dest
, src
, len
);
244 while ((tmp
= strrchr(dest
, '.')))
246 while ((tmp
= strrchr(dest
, '=')))
250 static void print_help (void)
252 mp_tmsg (MSGT_AO
, MSGL_FATAL
,
253 "\n[AO_ALSA] -ao alsa commandline help:\n"\
254 "[AO_ALSA] Example: mplayer -ao alsa:device=hw=0.3\n"\
255 "[AO_ALSA] Sets first card fourth hardware device.\n\n"\
256 "[AO_ALSA] Options:\n"\
257 "[AO_ALSA] noblock\n"\
258 "[AO_ALSA] Opens device in non-blocking mode.\n"\
259 "[AO_ALSA] device=<device-name>\n"\
260 "[AO_ALSA] Sets device (change , to . and : to =)\n");
263 static int str_maxlen(void *strp
) {
264 strarg_t
*str
= strp
;
265 return str
->len
<= ALSA_DEVICE_SIZE
;
268 static int try_open_device(const char *device
, int open_mode
, int try_ac3
)
271 char *ac3_device
, *args
;
274 /* to set the non-audio bit, use AES0=6 */
275 len
= strlen(device
);
276 ac3_device
= malloc(len
+ 7 + 1);
279 strcpy(ac3_device
, device
);
280 args
= strchr(ac3_device
, ':');
282 /* no existing parameters: add it behind device name */
283 strcat(ac3_device
, ":AES0=6");
287 while (isspace(*args
));
289 /* ":" but no parameters */
290 strcat(ac3_device
, "AES0=6");
291 } else if (*args
!= '{') {
292 /* a simple list of parameters: add it at the end of the list */
293 strcat(ac3_device
, ",AES0=6");
295 /* parameters in config syntax: add it inside the { } block */
298 while (len
> 0 && isspace(ac3_device
[len
]));
299 if (ac3_device
[len
] == '}')
300 strcpy(ac3_device
+ len
, " AES0=6}");
303 err
= snd_pcm_open(&alsa_handler
, ac3_device
, SND_PCM_STREAM_PLAYBACK
,
309 return snd_pcm_open(&alsa_handler
, device
, SND_PCM_STREAM_PLAYBACK
,
314 open & setup audio device
315 return: 1=success 0=fail
317 static int init(int rate_hz
, int channels
, int format
, int flags
)
322 snd_pcm_uframes_t chunk_size
;
323 snd_pcm_uframes_t bufsize
;
324 snd_pcm_uframes_t boundary
;
325 const opt_t subopts
[] = {
326 {"block", OPT_ARG_BOOL
, &block
, NULL
},
327 {"device", OPT_ARG_STR
, &device
, str_maxlen
},
331 char alsa_device
[ALSA_DEVICE_SIZE
+ 1];
332 // make sure alsa_device is null-terminated even when using strncpy etc.
333 memset(alsa_device
, 0, ALSA_DEVICE_SIZE
+ 1);
335 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz
,
338 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: using ALSA %s\n", snd_asoundlib_version());
342 snd_lib_error_set_handler(alsa_error_handler
);
344 ao_data
.samplerate
= rate_hz
;
345 ao_data
.format
= format
;
346 ao_data
.channels
= channels
;
351 alsa_format
= SND_PCM_FORMAT_S8
;
354 alsa_format
= SND_PCM_FORMAT_U8
;
356 case AF_FORMAT_U16_LE
:
357 alsa_format
= SND_PCM_FORMAT_U16_LE
;
359 case AF_FORMAT_U16_BE
:
360 alsa_format
= SND_PCM_FORMAT_U16_BE
;
362 case AF_FORMAT_AC3_LE
:
363 case AF_FORMAT_S16_LE
:
364 alsa_format
= SND_PCM_FORMAT_S16_LE
;
366 case AF_FORMAT_AC3_BE
:
367 case AF_FORMAT_S16_BE
:
368 alsa_format
= SND_PCM_FORMAT_S16_BE
;
370 case AF_FORMAT_U32_LE
:
371 alsa_format
= SND_PCM_FORMAT_U32_LE
;
373 case AF_FORMAT_U32_BE
:
374 alsa_format
= SND_PCM_FORMAT_U32_BE
;
376 case AF_FORMAT_S32_LE
:
377 alsa_format
= SND_PCM_FORMAT_S32_LE
;
379 case AF_FORMAT_S32_BE
:
380 alsa_format
= SND_PCM_FORMAT_S32_BE
;
382 case AF_FORMAT_U24_LE
:
383 alsa_format
= SND_PCM_FORMAT_U24_3LE
;
385 case AF_FORMAT_U24_BE
:
386 alsa_format
= SND_PCM_FORMAT_U24_3BE
;
388 case AF_FORMAT_S24_LE
:
389 alsa_format
= SND_PCM_FORMAT_S24_3LE
;
391 case AF_FORMAT_S24_BE
:
392 alsa_format
= SND_PCM_FORMAT_S24_3BE
;
394 case AF_FORMAT_FLOAT_LE
:
395 alsa_format
= SND_PCM_FORMAT_FLOAT_LE
;
397 case AF_FORMAT_FLOAT_BE
:
398 alsa_format
= SND_PCM_FORMAT_FLOAT_BE
;
400 case AF_FORMAT_MU_LAW
:
401 alsa_format
= SND_PCM_FORMAT_MU_LAW
;
403 case AF_FORMAT_A_LAW
:
404 alsa_format
= SND_PCM_FORMAT_A_LAW
;
408 alsa_format
= SND_PCM_FORMAT_MPEG
; //? default should be -1
416 * sets opening sequence for SPDIF
417 * sets also the playback and other switches 'on the fly'
418 * while opening the abstract alias for the spdif subdevice
421 if (AF_FORMAT_IS_AC3(format
)) {
422 device
.str
= "iec958";
423 mp_msg(MSGT_AO
,MSGL_V
,"alsa-spdif-init: playing AC3, %i channels\n", channels
);
426 /* in any case for multichannel playback we should select
432 device
.str
= "default";
433 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: setup for 1/2 channel(s)\n");
436 if (alsa_format
== SND_PCM_FORMAT_FLOAT_LE
)
437 // hack - use the converter plugin
438 device
.str
= "plug:surround40";
440 device
.str
= "surround40";
441 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: device set to surround40\n");
444 if (alsa_format
== SND_PCM_FORMAT_FLOAT_LE
)
445 device
.str
= "plug:surround51";
447 device
.str
= "surround51";
448 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: device set to surround51\n");
451 if (alsa_format
== SND_PCM_FORMAT_FLOAT_LE
)
452 device
.str
= "plug:surround71";
454 device
.str
= "surround71";
455 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: device set to surround71\n");
458 device
.str
= "default";
459 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] %d channels are not supported.\n",channels
);
461 device
.len
= strlen(device
.str
);
462 if (subopt_parse(ao_subdevice
, subopts
) != 0) {
466 parse_device(alsa_device
, device
.str
, device
.len
);
468 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: using device %s\n", alsa_device
);
471 int open_mode
= block
? 0 : SND_PCM_NONBLOCK
;
472 int isac3
= AF_FORMAT_IS_AC3(format
);
473 //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
474 if ((err
= try_open_device(alsa_device
, open_mode
, isac3
)) < 0)
476 if (err
!= -EBUSY
&& !block
) {
477 mp_tmsg(MSGT_AO
,MSGL_INFO
,"[AO_ALSA] Open in nonblock-mode failed, trying to open in block-mode.\n");
478 if ((err
= try_open_device(alsa_device
, 0, isac3
)) < 0) {
479 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err
));
483 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Playback open error: %s\n", snd_strerror(err
));
488 if ((err
= snd_pcm_nonblock(alsa_handler
, 0)) < 0) {
489 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AL_ALSA] Error setting block-mode %s.\n", snd_strerror(err
));
491 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: pcm opened in blocking mode\n");
494 snd_pcm_hw_params_alloca(&alsa_hwparams
);
495 snd_pcm_sw_params_alloca(&alsa_swparams
);
497 // setting hw-parameters
498 if ((err
= snd_pcm_hw_params_any(alsa_handler
, alsa_hwparams
)) < 0)
500 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to get initial parameters: %s\n",
505 err
= snd_pcm_hw_params_set_access(alsa_handler
, alsa_hwparams
,
506 SND_PCM_ACCESS_RW_INTERLEAVED
);
508 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set access type: %s\n",
513 /* workaround for nonsupported formats
514 sets default format to S16_LE if the given formats aren't supported */
515 if ((err
= snd_pcm_hw_params_test_format(alsa_handler
, alsa_hwparams
,
518 mp_tmsg(MSGT_AO
,MSGL_INFO
,
519 "[AO_ALSA] Format %s is not supported by hardware, trying default.\n", af_fmt2str_short(format
));
520 alsa_format
= SND_PCM_FORMAT_S16_LE
;
521 if (AF_FORMAT_IS_AC3(ao_data
.format
))
522 ao_data
.format
= AF_FORMAT_AC3_LE
;
524 ao_data
.format
= AF_FORMAT_S16_LE
;
527 if ((err
= snd_pcm_hw_params_set_format(alsa_handler
, alsa_hwparams
,
530 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set format: %s\n",
535 if ((err
= snd_pcm_hw_params_set_channels_near(alsa_handler
, alsa_hwparams
,
536 &ao_data
.channels
)) < 0)
538 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set channels: %s\n",
543 /* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11)
544 prefer our own resampler, since that allows users to choose the resampler,
545 even per file if desired */
546 if ((err
= snd_pcm_hw_params_set_rate_resample(alsa_handler
, alsa_hwparams
,
549 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to disable resampling: %s\n",
554 if ((err
= snd_pcm_hw_params_set_rate_near(alsa_handler
, alsa_hwparams
,
555 &ao_data
.samplerate
, NULL
)) < 0)
557 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set samplerate-2: %s\n",
562 bytes_per_sample
= af_fmt2bits(ao_data
.format
) / 8;
563 bytes_per_sample
*= ao_data
.channels
;
564 ao_data
.bps
= ao_data
.samplerate
* bytes_per_sample
;
566 if ((err
= snd_pcm_hw_params_set_buffer_time_near(alsa_handler
, alsa_hwparams
,
567 &(unsigned int){BUFFER_TIME
}, NULL
)) < 0)
569 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set buffer time near: %s\n",
574 if ((err
= snd_pcm_hw_params_set_periods_near(alsa_handler
, alsa_hwparams
,
575 &(unsigned int){FRAGCOUNT
}, NULL
)) < 0) {
576 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set periods: %s\n",
581 /* finally install hardware parameters */
582 if ((err
= snd_pcm_hw_params(alsa_handler
, alsa_hwparams
)) < 0)
584 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set hw-parameters: %s\n",
588 // end setting hw-params
591 // gets buffersize for control
592 if ((err
= snd_pcm_hw_params_get_buffer_size(alsa_hwparams
, &bufsize
)) < 0)
594 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to get buffersize: %s\n", snd_strerror(err
));
598 ao_data
.buffersize
= bufsize
* bytes_per_sample
;
599 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: got buffersize=%i\n", ao_data
.buffersize
);
602 if ((err
= snd_pcm_hw_params_get_period_size(alsa_hwparams
, &chunk_size
, NULL
)) < 0) {
603 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO ALSA] Unable to get period size: %s\n", snd_strerror(err
));
606 mp_msg(MSGT_AO
,MSGL_V
,"alsa-init: got period size %li\n", chunk_size
);
608 ao_data
.outburst
= chunk_size
* bytes_per_sample
;
610 /* setting software parameters */
611 if ((err
= snd_pcm_sw_params_current(alsa_handler
, alsa_swparams
)) < 0) {
612 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to get sw-parameters: %s\n",
616 if ((err
= snd_pcm_sw_params_get_boundary(alsa_swparams
, &boundary
)) < 0) {
617 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to get boundary: %s\n",
621 /* start playing when one period has been written */
622 if ((err
= snd_pcm_sw_params_set_start_threshold(alsa_handler
, alsa_swparams
, chunk_size
)) < 0) {
623 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set start threshold: %s\n",
627 /* disable underrun reporting */
628 if ((err
= snd_pcm_sw_params_set_stop_threshold(alsa_handler
, alsa_swparams
, boundary
)) < 0) {
629 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set stop threshold: %s\n",
633 /* play silence when there is an underrun */
634 if ((err
= snd_pcm_sw_params_set_silence_size(alsa_handler
, alsa_swparams
, boundary
)) < 0) {
635 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to set silence size: %s\n",
639 if ((err
= snd_pcm_sw_params(alsa_handler
, alsa_swparams
)) < 0) {
640 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Unable to get sw-parameters: %s\n",
644 /* end setting sw-params */
646 mp_msg(MSGT_AO
,MSGL_V
,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
647 ao_data
.samplerate
, ao_data
.channels
, (int)bytes_per_sample
, ao_data
.buffersize
,
648 snd_pcm_format_description(alsa_format
));
650 } // end switch alsa_handler (spdif)
651 alsa_can_pause
= snd_pcm_hw_params_can_pause(alsa_hwparams
);
656 /* close audio device */
657 static void uninit(int immed
)
664 snd_pcm_drain(alsa_handler
);
666 if ((err
= snd_pcm_close(alsa_handler
)) < 0)
668 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm close error: %s\n", snd_strerror(err
));
673 mp_msg(MSGT_AO
,MSGL_V
,"alsa-uninit: pcm closed\n");
677 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] No handler defined!\n");
681 static void audio_pause(void)
685 if (alsa_can_pause
) {
686 if ((err
= snd_pcm_pause(alsa_handler
, 1)) < 0)
688 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm pause error: %s\n", snd_strerror(err
));
691 mp_msg(MSGT_AO
,MSGL_V
,"alsa-pause: pause supported by hardware\n");
693 if (snd_pcm_delay(alsa_handler
, &prepause_frames
) < 0
694 || prepause_frames
< 0)
697 if ((err
= snd_pcm_drop(alsa_handler
)) < 0)
699 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm drop error: %s\n", snd_strerror(err
));
705 static void audio_resume(void)
709 if (snd_pcm_state(alsa_handler
) == SND_PCM_STATE_SUSPENDED
) {
710 mp_tmsg(MSGT_AO
,MSGL_INFO
,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
711 while ((err
= snd_pcm_resume(alsa_handler
)) == -EAGAIN
) sleep(1);
713 if (alsa_can_pause
) {
714 if ((err
= snd_pcm_pause(alsa_handler
, 0)) < 0)
716 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm resume error: %s\n", snd_strerror(err
));
719 mp_msg(MSGT_AO
,MSGL_V
,"alsa-resume: resume supported by hardware\n");
721 if ((err
= snd_pcm_prepare(alsa_handler
)) < 0)
723 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err
));
726 if (prepause_frames
) {
727 void *silence
= calloc(prepause_frames
, bytes_per_sample
);
728 play(silence
, prepause_frames
* bytes_per_sample
, 0);
734 /* stop playing and empty buffers (for seeking/pause) */
735 static void reset(void)
740 if ((err
= snd_pcm_drop(alsa_handler
)) < 0)
742 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err
));
745 if ((err
= snd_pcm_prepare(alsa_handler
)) < 0)
747 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(err
));
754 plays 'len' bytes of 'data'
755 returns: number of bytes played
756 modified last at 29.06.02 by jp
757 thanxs for marius <marius@rospot.com> for giving us the light ;)
760 static int play(void* data
, int len
, int flags
)
763 snd_pcm_sframes_t res
= 0;
764 if (!(flags
& AOPLAY_FINAL_CHUNK
))
765 len
= len
/ ao_data
.outburst
* ao_data
.outburst
;
766 num_frames
= len
/ bytes_per_sample
;
768 //mp_msg(MSGT_AO,MSGL_ERR,"alsa-play: frames=%i, len=%i\n",num_frames,len);
771 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Device configuration error.");
779 res
= snd_pcm_writei(alsa_handler
, data
, num_frames
);
785 else if (res
== -ESTRPIPE
) { /* suspend */
786 mp_tmsg(MSGT_AO
,MSGL_INFO
,"[AO_ALSA] Pcm in suspend mode, trying to resume.\n");
787 while ((res
= snd_pcm_resume(alsa_handler
)) == -EAGAIN
)
791 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Write error: %s\n", snd_strerror(res
));
792 mp_tmsg(MSGT_AO
,MSGL_INFO
,"[AO_ALSA] Trying to reset soundcard.\n");
793 if ((res
= snd_pcm_prepare(alsa_handler
)) < 0) {
794 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] pcm prepare error: %s\n", snd_strerror(res
));
801 return res
< 0 ? res
: res
* bytes_per_sample
;
804 /* how many byes are free in the buffer */
805 static int get_space(void)
807 snd_pcm_status_t
*status
;
810 snd_pcm_status_alloca(&status
);
812 if ((ret
= snd_pcm_status(alsa_handler
, status
)) < 0)
814 mp_tmsg(MSGT_AO
,MSGL_ERR
,"[AO_ALSA] Cannot get pcm status: %s\n", snd_strerror(ret
));
818 unsigned space
= snd_pcm_status_get_avail(status
) * bytes_per_sample
;
819 if (space
> ao_data
.buffersize
) // Buffer underrun?
820 space
= ao_data
.buffersize
;
824 /* delay in seconds between first and last sample in buffer */
825 static float get_delay(void)
828 snd_pcm_sframes_t delay
;
830 if (snd_pcm_delay(alsa_handler
, &delay
) < 0)
834 /* underrun - move the application pointer forward to catch up */
835 snd_pcm_forward(alsa_handler
, -delay
);
838 return (float)delay
/ (float)ao_data
.samplerate
;