cmus_play_file: Treat URLs specially
[cmus.git] / alsa.c
blob7de83185284d191f3f294501559259d6ad1c462e
1 /*
2 * Copyright 2004-2005 Timo Hirvonen
3 *
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
17 * 02111-1307, USA.
21 * snd_pcm_state_t:
23 * Open
24 * SND_PCM_STATE_OPEN = 0,
26 * Setup installed
27 * SND_PCM_STATE_SETUP = 1,
29 * Ready to start
30 * SND_PCM_STATE_PREPARED = 2,
32 * Running
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,
41 * Paused
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,
51 #include "op.h"
52 #include "utils.h"
53 #include "xmalloc.h"
54 #include "sf.h"
55 #include "debug.h"
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
66 #define SET_AVAIL_MIN
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;
80 /* configuration */
81 static char *alsa_dsp_device = NULL;
83 #ifdef SET_START_THRESHOLD
84 static int alsa_buffer_size;
85 #endif
87 #ifdef SET_AVAIL_MIN
88 static int alsa_period_size;
89 #endif
91 #if 0
92 #define debug_ret(func, ret) \
93 d_print("%s returned %d %s\n", func, ret, ret < 0 ? snd_strerror(ret) : "")
94 #else
95 #define debug_ret(func, ret) do { } while (0)
96 #endif
98 static int alsa_error_to_op_error(int err)
100 err = -err;
101 if (err < SND_ERROR_BEGIN) {
102 errno = err;
103 return -OP_ERROR_ERRNO;
105 return -OP_ERROR_INTERNAL;
108 static int op_alsa_init(void)
110 int rc;
112 if (alsa_dsp_device == NULL)
113 alsa_dsp_device = xstrdup("default");
114 rc = snd_pcm_status_malloc(&status);
115 if (rc < 0) {
116 free(alsa_dsp_device);
117 alsa_dsp_device = NULL;
118 errno = ENOMEM;
119 return -OP_ERROR_ERRNO;
121 return 0;
124 static int op_alsa_exit(void)
126 snd_pcm_status_free(status);
127 free(alsa_dsp_device);
128 alsa_dsp_device = NULL;
129 return 0;
132 /* randomize hw params */
133 static int alsa_set_hw_params(void)
135 snd_pcm_hw_params_t *hwparams;
136 const char *cmd;
137 unsigned int rate;
138 int rc, dir;
139 #if defined(SET_AVAIL_MIN) || defined(SET_START_THRESHOLD)
140 snd_pcm_uframes_t frames;
141 #endif
142 #ifdef SET_BUFFER_TIME
143 unsigned int alsa_buffer_time = 500e3;
144 #endif
145 #ifdef SET_PERIOD_TIME
146 unsigned int alsa_period_time = 50e3;
147 #endif
149 snd_pcm_hw_params_alloca(&hwparams);
151 cmd = "snd_pcm_hw_params_any";
152 rc = snd_pcm_hw_params_any(alsa_handle, hwparams);
153 if (rc < 0)
154 goto error;
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);
162 if (rc < 0)
163 goto error;
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);
170 if (rc < 0)
171 goto error;
173 cmd = "snd_pcm_hw_params_set_channels";
174 rc = snd_pcm_hw_params_set_channels(alsa_handle, hwparams, sf_get_channels(alsa_sf));
175 if (rc < 0)
176 goto error;
178 cmd = "snd_pcm_hw_params_set_rate";
179 rate = sf_get_rate(alsa_sf);
180 dir = 0;
181 rc = snd_pcm_hw_params_set_rate_near(alsa_handle, hwparams, &rate, &dir);
182 if (rc < 0)
183 goto error;
184 d_print("rate=%d\n", rate);
186 #ifdef SET_BUFFER_TIME
187 cmd = "snd_pcm_hw_params_set_buffer_time_near";
188 dir = 0;
189 rc = snd_pcm_hw_params_set_buffer_time_near(alsa_handle, hwparams, &alsa_buffer_time, &dir);
190 if (rc < 0)
191 goto error;
192 #endif
194 #ifdef SET_PERIOD_TIME
195 cmd = "snd_pcm_hw_params_set_period_time_near";
196 dir = 0;
197 rc = snd_pcm_hw_params_set_period_time_near(alsa_handle, hwparams, &alsa_period_time, &dir);
198 if (rc < 0)
199 goto error;
200 #endif
202 #ifdef SET_AVAIL_MIN
203 rc = snd_pcm_hw_params_get_period_size(hwparams, &frames, &dir);
204 if (rc < 0) {
205 alsa_period_size = -1;
206 } else {
207 alsa_period_size = frames * alsa_frame_size;
209 d_print("period_size = %d (dir = %d)\n", alsa_period_size, dir);
210 #endif
212 #ifdef SET_START_THRESHOLD
213 rc = snd_pcm_hw_params_get_buffer_size(hwparams, &frames);
214 if (rc < 0) {
215 alsa_buffer_size = -1;
216 } else {
217 alsa_buffer_size = frames * alsa_frame_size;
219 d_print("buffer_size = %d\n", alsa_buffer_size);
220 #endif
222 cmd = "snd_pcm_hw_params";
223 rc = snd_pcm_hw_params(alsa_handle, hwparams);
224 if (rc < 0)
225 goto error;
226 return 0;
227 error:
228 d_print("%s: error: %s\n", cmd, snd_strerror(rc));
229 return 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;
237 const char *cmd;
238 int rc;
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);
246 if (rc < 0)
247 goto error;
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);
255 if (rc < 0)
256 goto error;
258 #endif
260 #ifdef SET_AVAIL_MIN
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);
268 if (rc < 0)
269 goto error;
271 cmd = "snd_pcm_sw_params_set_silence_threshold";
272 rc = snd_pcm_sw_params_set_silence_threshold(alsa_handle, swparams, frames);
273 if (rc < 0)
274 goto error;
276 #endif
278 /* commit the params structure to ALSA */
279 cmd = "snd_pcm_sw_params";
280 rc = snd_pcm_sw_params(alsa_handle, swparams);
281 if (rc < 0)
282 goto error;
283 return 0;
284 error:
285 d_print("%s: error: %s\n", cmd, snd_strerror(rc));
286 return rc;
287 #else
288 return 0;
289 #endif
292 static int op_alsa_open(sample_format_t sf)
294 int rc;
296 alsa_sf = 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);
300 if (rc < 0)
301 goto error;
303 rc = alsa_set_hw_params();
304 if (rc)
305 goto close_error;
306 rc = alsa_set_sw_params();
307 if (rc)
308 goto close_error;
310 rc = snd_pcm_prepare(alsa_handle);
311 if (rc < 0)
312 goto close_error;
313 return 0;
314 close_error:
315 snd_pcm_close(alsa_handle);
316 error:
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)
326 int rc;
328 /* it is impossible to calculate period_fill if period_size is -1 */
329 if (alsa_period_size > 0 && period_fill) {
330 char buf[8192];
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);
340 period_fill = 0;
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);
348 return 0;
351 static int op_alsa_drop(void)
353 int rc;
355 period_fill = 0;
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)
372 return 0;
375 static int op_alsa_write(const char *buffer, int count)
377 int rc, len;
378 int recovered = 0;
380 len = count / alsa_frame_size;
381 again:
382 rc = snd_pcm_writei(alsa_handle, buffer, len);
383 if (rc < 0) {
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",
387 snd_strerror(rc));
388 recovered++;
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);
392 if (!rc)
393 goto again;
396 /* this handles EAGAIN too which is not critical error */
397 return alsa_error_to_op_error(rc);
400 rc *= alsa_frame_size;
401 period_fill += rc;
402 period_fill %= alsa_period_size;
403 return rc;
406 static int op_alsa_buffer_space(void)
408 int rc;
409 snd_pcm_uframes_t f;
411 rc = snd_pcm_status(alsa_handle, status);
412 if (rc < 0) {
413 debug_ret("snd_pcm_status", rc);
414 return alsa_error_to_op_error(rc);
417 f = snd_pcm_status_get_avail(status);
418 if (f > 1e6) {
419 d_print("snd_pcm_status_get_avail returned huge number: %lu\n", f);
420 f = 1024;
422 return f * alsa_frame_size;
425 static int op_alsa_pause(void)
427 if (alsa_can_pause) {
428 snd_pcm_state_t state;
429 int rc;
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
437 // infinite timeout
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);
443 } else {
444 d_print("error: state is not RUNNING or PREPARED\n");
447 return 0;
450 static int op_alsa_unpause(void)
452 if (alsa_can_pause) {
453 snd_pcm_state_t state;
454 int rc;
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
462 // infinite timeout
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);
468 } else {
469 d_print("error: state is not PAUSED nor PREPARED\n");
472 return 0;
475 static int op_alsa_set_option(int key, const char *val)
477 switch (key) {
478 case 0:
479 free(alsa_dsp_device);
480 alsa_dsp_device = xstrdup(val);
481 break;
482 default:
483 return -OP_ERROR_NOT_OPTION;
485 return 0;
488 static int op_alsa_get_option(int key, char **val)
490 switch (key) {
491 case 0:
492 if (alsa_dsp_device)
493 *val = xstrdup(alsa_dsp_device);
494 break;
495 default:
496 return -OP_ERROR_NOT_OPTION;
498 return 0;
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[] = {
516 "device",
517 NULL
520 const int op_priority = 0;