2 * Copyright 2004-2005 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * SND_PCM_STATE_OPEN = 0,
27 * SND_PCM_STATE_SETUP = 1,
30 * SND_PCM_STATE_PREPARED = 2,
33 * SND_PCM_STATE_RUNNING = 3,
35 * Stopped: underrun (playback) or overrun (capture) detected
36 * SND_PCM_STATE_XRUN = 4,
38 * Draining: running (playback) or stopped (capture)
39 * SND_PCM_STATE_DRAINING = 5,
42 * SND_PCM_STATE_PAUSED = 6,
44 * Hardware is suspended
45 * SND_PCM_STATE_SUSPENDED = 7,
47 * Hardware is disconnected
48 * SND_PCM_STATE_DISCONNECTED = 8,
57 #define ALSA_PCM_NEW_HW_PARAMS_API
58 #define ALSA_PCM_NEW_SW_PARAMS_API
60 #include <alsa/asoundlib.h>
62 /* without one of these play-back won't start */
63 #define SET_BUFFER_TIME
64 #define SET_PERIOD_TIME
68 /* with this alsa hangs sometimes (ogg, not enough data with first write?) */
69 /* #define SET_START_THRESHOLD */
71 static sample_format_t alsa_sf
;
72 static snd_pcm_t
*alsa_handle
;
73 static snd_pcm_format_t alsa_fmt
;
74 static int alsa_can_pause
;
75 static snd_pcm_status_t
*status
;
77 /* bytes (bits * channels / 8) */
78 static int alsa_frame_size
;
81 static char *alsa_dsp_device
= NULL
;
83 #ifdef SET_START_THRESHOLD
84 static int alsa_buffer_size
;
88 static int alsa_period_size
;
92 #define debug_ret(func, ret) \
93 d_print("%s returned %d %s\n", func, ret, ret < 0 ? snd_strerror(ret) : "")
95 #define debug_ret(func, ret) do { } while (0)
98 static int alsa_error_to_op_error(int err
)
101 if (err
< SND_ERROR_BEGIN
) {
103 return -OP_ERROR_ERRNO
;
105 return -OP_ERROR_INTERNAL
;
108 static int op_alsa_init(void)
112 if (alsa_dsp_device
== NULL
)
113 alsa_dsp_device
= xstrdup("default");
114 rc
= snd_pcm_status_malloc(&status
);
116 free(alsa_dsp_device
);
117 alsa_dsp_device
= NULL
;
119 return -OP_ERROR_ERRNO
;
124 static int op_alsa_exit(void)
126 snd_pcm_status_free(status
);
127 free(alsa_dsp_device
);
128 alsa_dsp_device
= NULL
;
132 /* randomize hw params */
133 static int alsa_set_hw_params(void)
135 snd_pcm_hw_params_t
*hwparams
;
139 #if defined(SET_AVAIL_MIN) || defined(SET_START_THRESHOLD)
140 snd_pcm_uframes_t frames
;
142 #ifdef SET_BUFFER_TIME
143 unsigned int alsa_buffer_time
= 500e3
;
145 #ifdef SET_PERIOD_TIME
146 unsigned int alsa_period_time
= 50e3
;
149 snd_pcm_hw_params_alloca(&hwparams
);
151 cmd
= "snd_pcm_hw_params_any";
152 rc
= snd_pcm_hw_params_any(alsa_handle
, hwparams
);
156 alsa_can_pause
= snd_pcm_hw_params_can_pause(hwparams
);
157 d_print("can pause = %d\n", alsa_can_pause
);
159 cmd
= "snd_pcm_hw_params_set_access";
160 rc
= snd_pcm_hw_params_set_access(alsa_handle
, hwparams
,
161 SND_PCM_ACCESS_RW_INTERLEAVED
);
165 alsa_fmt
= snd_pcm_build_linear_format(sf_get_bits(alsa_sf
), sf_get_bits(alsa_sf
),
166 sf_get_signed(alsa_sf
) ? 0 : 1,
167 sf_get_bigendian(alsa_sf
));
168 cmd
= "snd_pcm_hw_params_set_format";
169 rc
= snd_pcm_hw_params_set_format(alsa_handle
, hwparams
, alsa_fmt
);
173 cmd
= "snd_pcm_hw_params_set_channels";
174 rc
= snd_pcm_hw_params_set_channels(alsa_handle
, hwparams
, sf_get_channels(alsa_sf
));
178 cmd
= "snd_pcm_hw_params_set_rate";
179 rate
= sf_get_rate(alsa_sf
);
181 rc
= snd_pcm_hw_params_set_rate_near(alsa_handle
, hwparams
, &rate
, &dir
);
184 d_print("rate=%d\n", rate
);
186 #ifdef SET_BUFFER_TIME
187 cmd
= "snd_pcm_hw_params_set_buffer_time_near";
189 rc
= snd_pcm_hw_params_set_buffer_time_near(alsa_handle
, hwparams
, &alsa_buffer_time
, &dir
);
194 #ifdef SET_PERIOD_TIME
195 cmd
= "snd_pcm_hw_params_set_period_time_near";
197 rc
= snd_pcm_hw_params_set_period_time_near(alsa_handle
, hwparams
, &alsa_period_time
, &dir
);
203 rc
= snd_pcm_hw_params_get_period_size(hwparams
, &frames
, &dir
);
205 alsa_period_size
= -1;
207 alsa_period_size
= frames
* alsa_frame_size
;
209 d_print("period_size = %d (dir = %d)\n", alsa_period_size
, dir
);
212 #ifdef SET_START_THRESHOLD
213 rc
= snd_pcm_hw_params_get_buffer_size(hwparams
, &frames
);
215 alsa_buffer_size
= -1;
217 alsa_buffer_size
= frames
* alsa_frame_size
;
219 d_print("buffer_size = %d\n", alsa_buffer_size
);
222 cmd
= "snd_pcm_hw_params";
223 rc
= snd_pcm_hw_params(alsa_handle
, hwparams
);
228 d_print("%s: error: %s\n", cmd
, snd_strerror(rc
));
232 /* randomize sw params */
233 static int alsa_set_sw_params(void)
235 #if defined(SET_START_THRESHOLD) || defined(SET_AVAIL_MIN)
236 snd_pcm_sw_params_t
*swparams
;
240 /* allocate the software parameter structure */
241 snd_pcm_sw_params_alloca(&swparams
);
243 /* fetch the current software parameters */
244 cmd
= "snd_pcm_sw_params_current";
245 rc
= snd_pcm_sw_params_current(alsa_handle
, swparams
);
249 #ifdef SET_START_THRESHOLD
250 if (alsa_buffer_size
> 0) {
251 /* start the transfer when N frames available */
252 cmd
= "snd_pcm_sw_params_set_start_threshold";
253 /* start playing when hardware buffer is full (64 kB, 372 ms) */
254 rc
= snd_pcm_sw_params_set_start_threshold(alsa_handle
, swparams
, alsa_buffer_size
/ alsa_frame_size
);
261 if (alsa_period_size
> 0) {
262 snd_pcm_uframes_t frames
= alsa_period_size
/ alsa_frame_size
;
264 /* minimum avail frames to consider pcm ready. must be power of 2 */
265 cmd
= "snd_pcm_sw_params_set_avail_min";
266 /* underrun when available is <8192 B or 46.5 ms */
267 rc
= snd_pcm_sw_params_set_avail_min(alsa_handle
, swparams
, frames
);
271 cmd
= "snd_pcm_sw_params_set_silence_threshold";
272 rc
= snd_pcm_sw_params_set_silence_threshold(alsa_handle
, swparams
, frames
);
278 /* commit the params structure to ALSA */
279 cmd
= "snd_pcm_sw_params";
280 rc
= snd_pcm_sw_params(alsa_handle
, swparams
);
285 d_print("%s: error: %s\n", cmd
, snd_strerror(rc
));
292 static int op_alsa_open(sample_format_t sf
)
297 alsa_frame_size
= sf_get_frame_size(alsa_sf
);
299 rc
= snd_pcm_open(&alsa_handle
, alsa_dsp_device
, SND_PCM_STREAM_PLAYBACK
, 0);
303 rc
= alsa_set_hw_params();
306 rc
= alsa_set_sw_params();
310 rc
= snd_pcm_prepare(alsa_handle
);
315 snd_pcm_close(alsa_handle
);
317 return alsa_error_to_op_error(rc
);
320 static unsigned int period_fill
= 0;
322 static int op_alsa_write(const char *buffer
, int count
);
324 static int op_alsa_close(void)
328 /* it is impossible to calculate period_fill if period_size is -1 */
329 if (alsa_period_size
> 0 && period_fill
) {
331 int silence_bytes
= alsa_period_size
- period_fill
;
333 if (silence_bytes
> sizeof(buf
)) {
334 d_print("silence buf not big enough %d\n", silence_bytes
);
335 silence_bytes
= sizeof(buf
);
337 d_print("silencing %d bytes\n", silence_bytes
);
338 snd_pcm_format_set_silence(alsa_fmt
, buf
, silence_bytes
/ sf_get_sample_size(alsa_sf
));
339 op_alsa_write(buf
, silence_bytes
);
343 rc
= snd_pcm_drain(alsa_handle
);
344 debug_ret("snd_pcm_drain", rc
);
346 rc
= snd_pcm_close(alsa_handle
);
347 debug_ret("snd_pcm_close", rc
);
351 static int op_alsa_drop(void)
357 /* infinite timeout */
358 rc
= snd_pcm_wait(alsa_handle
, -1);
359 debug_ret("snd_pcm_wait", rc
);
361 rc
= snd_pcm_drop(alsa_handle
);
362 debug_ret("snd_pcm_drop", rc
);
364 rc
= snd_pcm_prepare(alsa_handle
);
365 debug_ret("snd_pcm_prepare", rc
);
367 /* drop set state to SETUP
368 * prepare set state to PREPARED
370 * so if old state was PAUSED we can't UNPAUSE (see op_alsa_unpause)
375 static int op_alsa_write(const char *buffer
, int count
)
380 len
= count
/ alsa_frame_size
;
382 rc
= snd_pcm_writei(alsa_handle
, buffer
, len
);
384 // rc _should_ be either -EBADFD, -EPIPE or -ESTRPIPE
385 if (!recovered
&& (rc
== -EINTR
|| rc
== -EPIPE
|| rc
== -ESTRPIPE
)) {
386 d_print("snd_pcm_writei failed: %s, trying to recover\n",
389 // this handles -EINTR, -EPIPE and -ESTRPIPE
390 // for other errors it just returns the error code
391 rc
= snd_pcm_recover(alsa_handle
, rc
, 1);
396 /* this handles EAGAIN too which is not critical error */
397 return alsa_error_to_op_error(rc
);
400 rc
*= alsa_frame_size
;
402 period_fill
%= alsa_period_size
;
406 static int op_alsa_buffer_space(void)
411 rc
= snd_pcm_status(alsa_handle
, status
);
413 debug_ret("snd_pcm_status", rc
);
414 return alsa_error_to_op_error(rc
);
417 f
= snd_pcm_status_get_avail(status
);
419 d_print("snd_pcm_status_get_avail returned huge number: %lu\n", f
);
422 return f
* alsa_frame_size
;
425 static int op_alsa_pause(void)
427 if (alsa_can_pause
) {
428 snd_pcm_state_t state
;
431 state
= snd_pcm_state(alsa_handle
);
432 if (state
== SND_PCM_STATE_PREPARED
) {
433 // state is PREPARED -> no need to pause
434 } else if (state
== SND_PCM_STATE_RUNNING
) {
435 // state is RUNNING - > pause
438 rc
= snd_pcm_wait(alsa_handle
, -1);
439 debug_ret("snd_pcm_wait", rc
);
441 rc
= snd_pcm_pause(alsa_handle
, 1);
442 debug_ret("snd_pcm_pause", rc
);
444 d_print("error: state is not RUNNING or PREPARED\n");
450 static int op_alsa_unpause(void)
452 if (alsa_can_pause
) {
453 snd_pcm_state_t state
;
456 state
= snd_pcm_state(alsa_handle
);
457 if (state
== SND_PCM_STATE_PREPARED
) {
458 // state is PREPARED -> no need to unpause
459 } else if (state
== SND_PCM_STATE_PAUSED
) {
460 // state is PAUSED -> unpause
463 rc
= snd_pcm_wait(alsa_handle
, -1);
464 debug_ret("snd_pcm_wait", rc
);
466 rc
= snd_pcm_pause(alsa_handle
, 0);
467 debug_ret("snd_pcm_pause", rc
);
469 d_print("error: state is not PAUSED nor PREPARED\n");
475 static int op_alsa_set_option(int key
, const char *val
)
479 free(alsa_dsp_device
);
480 alsa_dsp_device
= xstrdup(val
);
483 return -OP_ERROR_NOT_OPTION
;
488 static int op_alsa_get_option(int key
, char **val
)
493 *val
= xstrdup(alsa_dsp_device
);
496 return -OP_ERROR_NOT_OPTION
;
501 const struct output_plugin_ops op_pcm_ops
= {
502 .init
= op_alsa_init
,
503 .exit
= op_alsa_exit
,
504 .open
= op_alsa_open
,
505 .close
= op_alsa_close
,
506 .drop
= op_alsa_drop
,
507 .write
= op_alsa_write
,
508 .buffer_space
= op_alsa_buffer_space
,
509 .pause
= op_alsa_pause
,
510 .unpause
= op_alsa_unpause
,
511 .set_option
= op_alsa_set_option
,
512 .get_option
= op_alsa_get_option
515 const char * const op_pcm_options
[] = {
520 const int op_priority
= 0;