2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU 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 02111, USA.
20 * Based in parts on aplay.c from the alsa-utils-1.0.8 package,
21 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>, which is
22 * based on the vplay program by Michael Beck.
25 #define WAV_HEADER_LEN 44
26 #include <sys/time.h> /* gettimeofday */
29 #include "play.cmdline.h"
30 #include <alsa/asoundlib.h>
34 #define FORMAT SND_PCM_FORMAT_S16_LE
36 struct private_alsa_data
{
38 size_t bytes_per_frame
;
42 struct writer
*writer
;
48 int (*open
)(struct writer_node
*);
49 int (*write
)(char *data
, size_t nbytes
, struct writer_node
*);
50 void (*close
)(struct writer_node
*);
51 void (*shutdown
)(struct writer_node
*);
55 static struct writer writers
[NUM_WRITERS
];
56 #define FOR_EACH_WRITER(i) for (i = 0; i < NUM_WRITERS, i++)
57 static struct writer_node
*writer_nodes
;
60 static unsigned char *audiobuf
;
61 static struct timeval
*start_time
;
62 static struct gengetopt_args_info conf
;
66 void para_log(int ll
, const char* fmt
,...)
70 if (ll
< conf
.loglevel_arg
)
73 vfprintf(stderr
, fmt
, argp
);
78 * read WAV_HEADER_LEN bytes from stdin to audio buffer
80 * \return -E_READ_HDR on errors and on eof before WAV_HEADER_LEN could be
81 * read. A positive return value indicates success.
83 static int read_wav_header(void)
85 ssize_t ret
, count
= 0;
87 while (count
< WAV_HEADER_LEN
) {
88 ret
= read(STDIN_FILENO
, audiobuf
+ count
, WAV_HEADER_LEN
- count
);
97 * open and prepare the PCM handle for writing
99 * Install PCM software and hardware configuration. Exit on errors.
101 static int alsa_open(struct writer_node
*w
)
103 snd_pcm_hw_params_t
*hwparams
;
104 snd_pcm_sw_params_t
*swparams
;
105 snd_pcm_uframes_t buffer_size
, xfer_align
, start_threshold
,
107 unsigned buffer_time
= 0;
109 snd_pcm_info_t
*info
;
111 snd_pcm_uframes_t period_size
;
112 struct private_alsa_data
*pad
= para_malloc(sizeof(struct private_alsa_data
));
113 w
->private_data
= pad
;
115 snd_pcm_info_alloca(&info
);
116 if (snd_output_stdio_attach(&log
, stderr
, 0) < 0)
118 err
= snd_pcm_open(&pad
->handle
, conf
.device_arg
,
119 SND_PCM_STREAM_PLAYBACK
, 0);
122 if ((err
= snd_pcm_info(pad
->handle
, info
)) < 0)
123 return -E_SND_PCM_INFO
;
125 snd_pcm_hw_params_alloca(&hwparams
);
126 snd_pcm_sw_params_alloca(&swparams
);
127 if (snd_pcm_hw_params_any(pad
->handle
, hwparams
) < 0)
128 return -E_BROKEN_CONF
;
129 if (snd_pcm_hw_params_set_access(pad
->handle
, hwparams
,
130 SND_PCM_ACCESS_RW_INTERLEAVED
) < 0)
131 return -E_ACCESS_TYPE
;
132 if (snd_pcm_hw_params_set_format(pad
->handle
, hwparams
, FORMAT
) < 0)
133 return -E_SAMPLE_FORMAT
;
134 if (snd_pcm_hw_params_set_channels(pad
->handle
, hwparams
,
135 conf
.channels_arg
) < 0)
136 return -E_CHANNEL_COUNT
;
137 if (snd_pcm_hw_params_set_rate_near(pad
->handle
, hwparams
,
138 (unsigned int*) &conf
.sample_rate_arg
, 0) < 0)
140 err
= snd_pcm_hw_params_get_buffer_time_max(hwparams
, &buffer_time
, 0);
141 if (err
< 0 || !buffer_time
)
142 return -E_GET_BUFFER_TIME
;
143 PARA_DEBUG_LOG("buffer time: %d\n", buffer_time
);
144 if (snd_pcm_hw_params_set_buffer_time_near(pad
->handle
, hwparams
,
145 &buffer_time
, 0) < 0)
146 return -E_SET_BUFFER_TIME
;
147 if (snd_pcm_hw_params(pad
->handle
, hwparams
) < 0)
149 snd_pcm_hw_params_get_period_size(hwparams
, &period_size
, 0);
150 snd_pcm_hw_params_get_buffer_size(hwparams
, &buffer_size
);
151 PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size
,
153 if (period_size
== buffer_size
)
154 return -E_BAD_PERIOD
;
155 snd_pcm_sw_params_current(pad
->handle
, swparams
);
156 err
= snd_pcm_sw_params_get_xfer_align(swparams
, &xfer_align
);
157 if (err
< 0 || !xfer_align
)
159 snd_pcm_sw_params_set_avail_min(pad
->handle
, swparams
, period_size
);
160 /* round to closest transfer boundary */
161 start_threshold
= (buffer_size
/ xfer_align
) * xfer_align
;
162 if (start_threshold
< 1)
164 if (snd_pcm_sw_params_set_start_threshold(pad
->handle
, swparams
,
165 start_threshold
) < 0)
166 return -E_START_THRESHOLD
;
167 stop_threshold
= buffer_size
;
168 if (snd_pcm_sw_params_set_stop_threshold(pad
->handle
, swparams
,
170 return -E_STOP_THRESHOLD
;
171 if (snd_pcm_sw_params_set_xfer_align(pad
->handle
, swparams
,
174 if (snd_pcm_sw_params(pad
->handle
, swparams
) < 0)
176 pad
->bytes_per_frame
= snd_pcm_format_physical_width(FORMAT
)
177 * conf
.channels_arg
/ 8;
178 return period_size
* pad
->bytes_per_frame
;
182 * push out pcm frames
183 * \param data pointer do data to be written
184 * \param nbytes number of bytes (not frames)
186 * \return Number of bytes written, -E_ALSA_WRITE on errors.
188 static int alsa_write(char *data
, size_t nbytes
, struct writer_node
*wn
)
190 struct private_alsa_data
*pad
= wn
->private_data
;
191 size_t frames
= nbytes
/ pad
->bytes_per_frame
;
192 unsigned char *d
= data
;
193 snd_pcm_sframes_t r
, result
= 0;
196 /* write interleaved frames */
197 r
= snd_pcm_writei(pad
->handle
, d
, frames
);
199 PARA_ERROR_LOG("write error: %s\n", snd_strerror(r
));
200 if (r
== -EAGAIN
|| (r
>= 0 && r
< frames
))
201 snd_pcm_wait(pad
->handle
, 1);
202 else if (r
== -EPIPE
)
203 snd_pcm_prepare(pad
->handle
);
205 return -E_ALSA_WRITE
;
209 d
+= r
* pad
->bytes_per_frame
;
212 return result
* pad
->bytes_per_frame
;
215 static void alsa_close(struct writer_node
*wn
)
217 struct private_alsa_data
*pad
= wn
->private_data
;
218 snd_pcm_drain(pad
->handle
);
219 snd_pcm_close(pad
->handle
);
220 snd_config_update_free_global();
224 void alsa_writer_init(struct writer
*w
)
227 w
->write
= alsa_write
;
228 w
->close
= alsa_close
;
229 w
->shutdown
= NULL
; /* nothing to do */
234 * check if current time is later than start_time
235 * \param diff pointer to write remaining time to
237 * If start_time was not given, or current time is later than given
238 * start_time, return 0. Otherwise, return 1 and write the time
239 * difference between current time and start_time to diff. diff may be
243 static int start_time_in_future(struct timeval
*diff
)
247 if (!conf
.start_time_given
)
249 gettimeofday(&now
, NULL
);
250 return tv_diff(start_time
, &now
, diff
) > 0? 1 : 0;
254 * sleep until time given at command line
256 * This is called if the initial buffer is filled. It returns
257 * immediately if no start_time was given at the command line
258 * or if the given start time is in the past.
261 static void do_initial_delay(struct timeval
*delay
)
264 para_select(1, NULL
, NULL
, delay
);
265 while (start_time_in_future(delay
));
268 static int read_stdin(char *buf
, size_t bytes_to_load
, size_t *loaded
)
272 while (*loaded
< bytes_to_load
) {
273 ret
= read(STDIN_FILENO
, buf
+ *loaded
, bytes_to_load
- *loaded
);
286 * \param loaded number of bytes already loaded
288 * If start_time was given, prebuffer data until buffer is full or
289 * start_time is reached. In any case, do not start playing before
292 * \return positive on success, negative on errors.
294 static int play_pcm(size_t loaded
)
296 size_t bufsize
, min_written
= 0, prebuf_size
, bytes_to_load
;
297 struct timeval delay
;
298 int i
, max_chunk_bytes
= 0, ret
, not_yet_started
= 1, need_more_writes
;
299 struct writer_node
*wn
;
300 size_t *written
= para_calloc(1 * sizeof(size_t));
302 for (i
= 0; i
< 1; i
++) {
303 wn
= &writer_nodes
[i
];
304 ret
= wn
->writer
->open(wn
);
307 wn
->chunk_bytes
= ret
;
308 max_chunk_bytes
= PARA_MAX(max_chunk_bytes
, ret
);
310 PARA_INFO_LOG("max chunk_bytes: %d\n", max_chunk_bytes
);
311 bufsize
= (conf
.bufsize_arg
* 1024 / max_chunk_bytes
) * max_chunk_bytes
;
312 audiobuf
= para_realloc(audiobuf
, bufsize
);
313 prebuf_size
= conf
.prebuffer_arg
* bufsize
/ 100;
314 bytes_to_load
= PARA_MAX(prebuf_size
, max_chunk_bytes
);
315 ret
= read_stdin(audiobuf
, bytes_to_load
, &loaded
);
316 if (ret
<= 0 || loaded
< bytes_to_load
) {
318 ret
= -E_PREMATURE_END
;
321 if (not_yet_started
&& start_time
&& start_time_in_future(&delay
))
322 do_initial_delay(&delay
);
325 need_more_writes
= 1;
326 while (need_more_writes
) {
327 need_more_writes
= 0;
328 for (i
= 0; i
< 1; i
++) {
329 unsigned char *p
= audiobuf
+ written
[i
];
330 wn
= &writer_nodes
[i
];
332 min_written
= written
[i
];
334 min_written
= PARA_MIN(min_written
, written
[i
]);
335 if (loaded
< wn
->chunk_bytes
+ written
[i
])
337 ret
= wn
->writer
->write(p
, wn
->chunk_bytes
, wn
);
340 if (ret
!= wn
->chunk_bytes
)
341 PARA_WARNING_LOG("short write: %d/%d", ret
,
344 need_more_writes
= 1;
347 loaded
-= min_written
;
349 if (loaded
>= bufsize
) {
350 ret
= -E_PLAY_OVERRUN
;
353 memmove(audiobuf
, audiobuf
+ min_written
, loaded
);
355 for (i
= 0; i
< 1; i
++)
356 written
[i
] -= min_written
;
357 bytes_to_load
= PARA_MIN(max_chunk_bytes
, bufsize
);
358 ret
= read_stdin(audiobuf
, bytes_to_load
, &loaded
);
363 for (i
= 0; i
< 1; i
++) {
364 unsigned char *p
= audiobuf
+ written
[i
];
365 wn
= &writer_nodes
[i
];
367 if (written
[i
] >= loaded
)
369 bytes_to_write
= PARA_MIN(wn
->chunk_bytes
, loaded
- written
[i
]);
370 ret
= wn
->writer
->write(p
, bytes_to_write
, wn
);
378 for (i
= 0; i
< 1; i
++) {
379 wn
= &writer_nodes
[i
];
380 wn
->writer
->close(wn
);
386 * test if audio buffer contains a valid wave header
388 * \return If not, return 0, otherwise, store number of channels and sample rate
389 * in struct conf and return WAV_HEADER_LEN.
391 static size_t check_wave(void)
393 unsigned char *a
= audiobuf
;
394 if (a
[0] != 'R' || a
[1] != 'I' || a
[2] != 'F' || a
[3] != 'F')
395 return WAV_HEADER_LEN
;
396 conf
.channels_arg
= (unsigned) a
[22];
397 conf
.sample_rate_arg
= a
[24] + (a
[25] << 8) + (a
[26] << 16) + (a
[27] << 24);
401 int main(int argc
, char *argv
[])
404 int ret
= -E_PLAY_SYNTAX
;
406 cmdline_parser(argc
, argv
, &conf
);
407 if (conf
.prebuffer_arg
< 0 || conf
.prebuffer_arg
> 100)
409 if (conf
.start_time_given
) {
410 if (sscanf(conf
.start_time_arg
, "%lu:%lu",
411 &tv
.tv_sec
, &tv
.tv_usec
) != 2)
415 /* call init for each supported writer */
416 alsa_writer_init(&writers
[0]);
417 /* one for each given writer */
418 writer_nodes
= para_calloc(2 * sizeof(struct writer_node
));
419 writer_nodes
[0].writer
= &writers
[0]; /* alsa */
421 audiobuf
= para_malloc(WAV_HEADER_LEN
);
422 ret
= read_wav_header();
425 ret
= play_pcm(check_wave());
430 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));