struct writer_node {
struct writer *writer;
void *private_data;
+ int chunk_bytes;
};
struct writer {
- int (*open)(struct writer_node **);
+ int (*open)(struct writer_node *);
int (*write)(char *data, size_t nbytes, struct writer_node *);
void (*close)(struct writer_node *);
void (*shutdown)(struct writer_node *);
#define NUM_WRITERS 1
static struct writer writers[NUM_WRITERS];
#define FOR_EACH_WRITER(i) for (i = 0; i < NUM_WRITERS, i++)
-static struct writer_node **writer_nodes;
+static struct writer_node *writer_nodes;
static unsigned char *audiobuf;
INIT_PLAY_ERRLISTS;
-void para_log(__a_unused int ll, const char* fmt,...)
+void para_log(int ll, const char* fmt,...)
{
va_list argp;
+ if (ll < conf.loglevel_arg)
+ return;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
*
* Install PCM software and hardware configuration. Exit on errors.
*/
-static int alsa_open(void **private_data)
+static int alsa_open(struct writer_node *w)
{
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
snd_output_t *log;
snd_pcm_uframes_t period_size;
struct private_alsa_data *pad = para_malloc(sizeof(struct private_alsa_data));
- *private_data = pad;
+ w->private_data = pad;
snd_pcm_info_alloca(&info);
if (snd_output_stdio_attach(&log, stderr, 0) < 0)
return -E_HW_PARAMS;
snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
- PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size, period_size);
+ PARA_DEBUG_LOG("buffer size: %lu, period_size: %lu\n", buffer_size,
+ period_size);
if (period_size == buffer_size)
return -E_BAD_PERIOD;
snd_pcm_sw_params_current(pad->handle, swparams);
*
* \return Number of bytes written, -E_ALSA_WRITE on errors.
*/
-static int alsa_write(char *data, size_t nbytes, void *private_data)
+static int alsa_write(char *data, size_t nbytes, struct writer_node *wn)
{
- struct private_alsa_data *pad = private_data;
+ struct private_alsa_data *pad = wn->private_data;
size_t frames = nbytes / pad->bytes_per_frame;
unsigned char *d = data;
snd_pcm_sframes_t r, result = 0;
return result * pad->bytes_per_frame;
}
-static void alsa_close(void *private_data)
+static void alsa_close(struct writer_node *wn)
{
- struct private_alsa_data *pad = private_data;
+ struct private_alsa_data *pad = wn->private_data;
snd_pcm_drain(pad->handle);
snd_pcm_close(pad->handle);
snd_config_update_free_global();
while (start_time_in_future(delay));
}
+static int read_stdin(char *buf, size_t bytes_to_load, size_t *loaded)
+{
+ ssize_t ret;
+
+ while (*loaded < bytes_to_load) {
+ ret = read(STDIN_FILENO, buf + *loaded, bytes_to_load - *loaded);
+ if (ret <= 0) {
+ if (ret < 0)
+ ret = -E_READ_STDIN;
+ return ret;
+ }
+ *loaded += ret;
+ }
+ return 1;
+}
+
/**
* play raw pcm data
* \param loaded number of bytes already loaded
*/
static int play_pcm(size_t loaded)
{
- size_t bufsize, written = 0, prebuf_size;
- unsigned char *p;
+ size_t bufsize, min_written = 0, prebuf_size, bytes_to_load;
struct timeval delay;
- void *private_data;
- int chunk_bytes, ret = alsa_open(&private_data);
+ int i, max_chunk_bytes = 0, ret, not_yet_started = 1, need_more_writes;
+ struct writer_node *wn;
+ size_t *written = para_calloc(1 * sizeof(size_t));
- if (ret < 0)
- goto out;
- chunk_bytes = ret;
- bufsize = (conf.bufsize_arg * 1024 / chunk_bytes) * chunk_bytes;
+ for (i = 0; i < 1; i++) {
+ wn = &writer_nodes[i];
+ ret = wn->writer->open(wn);
+ if (ret < 0)
+ goto out;
+ wn->chunk_bytes = ret;
+ max_chunk_bytes = PARA_MAX(max_chunk_bytes, ret);
+ }
+ PARA_INFO_LOG("max chunk_bytes: %d\n", max_chunk_bytes);
+ bufsize = (conf.bufsize_arg * 1024 / max_chunk_bytes) * max_chunk_bytes;
audiobuf = para_realloc(audiobuf, bufsize);
prebuf_size = conf.prebuffer_arg * bufsize / 100;
+ bytes_to_load = PARA_MAX(prebuf_size, max_chunk_bytes);
+ ret = read_stdin(audiobuf, bytes_to_load, &loaded);
+ if (ret <= 0 || loaded < bytes_to_load) {
+ if (ret >= 0)
+ ret = -E_PREMATURE_END;
+ goto out;
+ }
+ if (not_yet_started && start_time && start_time_in_future(&delay))
+ do_initial_delay(&delay);
+ not_yet_started = 0;
again:
- if (!written) {
- if (loaded < prebuf_size)
- goto read;
- if (start_time && start_time_in_future(&delay)) {
- do_initial_delay(&delay);
- start_time = NULL;
+ need_more_writes = 1;
+ while (need_more_writes) {
+ need_more_writes = 0;
+ for (i = 0; i < 1; i++) {
+ unsigned char *p = audiobuf + written[i];
+ wn = &writer_nodes[i];
+ if (!i)
+ min_written = written[i];
+ else
+ min_written = PARA_MIN(min_written, written[i]);
+ if (loaded < wn->chunk_bytes + written[i])
+ continue;
+ ret = wn->writer->write(p, wn->chunk_bytes, wn);
+ if (ret < 0)
+ goto out;
+ if (ret != wn->chunk_bytes)
+ PARA_WARNING_LOG("short write: %d/%d", ret,
+ wn->chunk_bytes);
+ written[i] += ret;
+ need_more_writes = 1;
}
}
- p = audiobuf;
- while (loaded >= chunk_bytes) {
- ret = alsa_write(p, chunk_bytes, private_data);
- if (ret < 0)
+ loaded -= min_written;
+ if (loaded > 0) {
+ if (loaded >= bufsize) {
+ ret = -E_PLAY_OVERRUN;
goto out;
- p += ret;
- written += ret;
- loaded -= ret;
+ }
+ memmove(audiobuf, audiobuf + min_written, loaded);
}
- if (loaded && p != audiobuf)
- memmove(audiobuf, p, loaded);
-read:
- ret = read(STDIN_FILENO, audiobuf + loaded, bufsize - loaded);
- if (ret < 0) {
- ret = -E_READ_STDIN;
+ for (i = 0; i < 1; i++)
+ written[i] -= min_written;
+ bytes_to_load = PARA_MIN(max_chunk_bytes, bufsize);
+ ret = read_stdin(audiobuf, bytes_to_load, &loaded);
+ if (ret < 0)
goto out;
- }
- if (ret) {
- loaded += ret;
+ if (ret)
goto again;
+ for (i = 0; i < 1; i++) {
+ unsigned char *p = audiobuf + written[i];
+ wn = &writer_nodes[i];
+ int bytes_to_write;
+ if (written[i] >= loaded)
+ continue;
+ bytes_to_write = PARA_MIN(wn->chunk_bytes, loaded - written[i]);
+ ret = wn->writer->write(p, bytes_to_write, wn);
+ if (ret < 0)
+ goto out;
+ written[i] += ret;
}
- ret = 1;
+ ret = 0;
out:
- alsa_close(private_data);
+ free(written);
+ for (i = 0; i < 1; i++) {
+ wn = &writer_nodes[i];
+ wn->writer->close(wn);
+ }
return ret;
}
int main(int argc, char *argv[])
{
struct timeval tv;
- int ret;
+ int ret = -E_PLAY_SYNTAX;
cmdline_parser(argc, argv, &conf);
+ if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
+ goto out;
if (conf.start_time_given) {
- ret = -E_PLAY_SYNTAX;
if (sscanf(conf.start_time_arg, "%lu:%lu",
&tv.tv_sec, &tv.tv_usec) != 2)
goto out;
alsa_writer_init(&writers[0]);
/* one for each given writer */
writer_nodes = para_calloc(2 * sizeof(struct writer_node));
+ writer_nodes[0].writer = &writers[0]; /* alsa */
audiobuf = para_malloc(WAV_HEADER_LEN);
ret = read_wav_header();
ret = play_pcm(check_wave());
out:
free(audiobuf);
+ free(writer_nodes);
if (ret < 0)
PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
return ret;