snd_pcm_t *handle;
/** Determined and set by alsa_init(). */
int bytes_per_frame;
- /** The approximate maximum buffer duration in us. */
- unsigned buffer_time;
- /* Number of frames that fit into the buffer. */
- snd_pcm_uframes_t buffer_frames;
/**
* The sample rate given by command line option or the decoder
* of the writer node group.
*/
unsigned channels;
struct timeval drain_barrier;
+ /* File descriptor for select(). */
+ int poll_fd;
};
static snd_pcm_format_t get_alsa_pcm_format(enum sample_format sf)
snd_pcm_hw_params_t *hwparams;
snd_pcm_sw_params_t *swparams;
snd_pcm_uframes_t start_threshold, stop_threshold;
- snd_pcm_uframes_t period_size;
+ snd_pcm_uframes_t buffer_size, period_size;
+ snd_output_t *log;
+ unsigned buffer_time;
int err;
PARA_INFO_LOG("opening %s\n", conf->device_arg);
&pad->sample_rate, NULL) < 0)
return -E_SET_RATE;
err = snd_pcm_hw_params_get_buffer_time_max(hwparams,
- &pad->buffer_time, NULL);
- if (err < 0 || !pad->buffer_time)
+ &buffer_time, NULL);
+ if (err < 0 || buffer_time == 0)
return -E_GET_BUFFER_TIME;
- PARA_INFO_LOG("buffer time: %d\n", pad->buffer_time);
if (snd_pcm_hw_params_set_buffer_time_near(pad->handle, hwparams,
- &pad->buffer_time, NULL) < 0)
+ &buffer_time, NULL) < 0)
return -E_SET_BUFFER_TIME;
if (snd_pcm_hw_params(pad->handle, hwparams) < 0)
return -E_HW_PARAMS;
snd_pcm_hw_params_get_period_size(hwparams, &period_size, NULL);
- snd_pcm_hw_params_get_buffer_size(hwparams, &pad->buffer_frames);
- PARA_INFO_LOG("buffer size: %lu, period_size: %lu\n", pad->buffer_frames,
- period_size);
- if (period_size == pad->buffer_frames)
+ snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size);
+ if (period_size == buffer_size)
return -E_BAD_PERIOD;
+ /* software parameter setup */
snd_pcm_sw_params_current(pad->handle, swparams);
snd_pcm_sw_params_set_avail_min(pad->handle, swparams, period_size);
- if (pad->buffer_frames < 1)
+ if (buffer_size < 1)
start_threshold = 1;
else
- start_threshold = PARA_MIN(pad->buffer_frames,
+ start_threshold = PARA_MIN(buffer_size,
(snd_pcm_uframes_t)pad->sample_rate);
if (snd_pcm_sw_params_set_start_threshold(pad->handle, swparams,
start_threshold) < 0)
return -E_START_THRESHOLD;
- stop_threshold = pad->buffer_frames;
+ stop_threshold = buffer_size;
if (snd_pcm_sw_params_set_stop_threshold(pad->handle, swparams,
stop_threshold) < 0)
return -E_STOP_THRESHOLD;
* pad->channels / 8;
if (pad->bytes_per_frame <= 0)
return -E_PHYSICAL_WIDTH;
- PARA_INFO_LOG("bytes per frame: %d\n", pad->bytes_per_frame);
if (snd_pcm_nonblock(pad->handle, 1))
PARA_ERROR_LOG("failed to set nonblock mode\n");
+ err = snd_output_buffer_open(&log);
+ if (err == 0) {
+ char *buf;
+ PARA_INFO_LOG("dumping alsa configuration\n");
+ snd_pcm_dump(pad->handle, log);
+ snd_output_buffer_string(log, &buf);
+ for (;;) {
+ char *p = strchr(buf, '\n');
+ if (!p) /* omit last output line, it's empty */
+ break;
+ *p = '\0';
+ PARA_INFO_LOG("%s\n", buf);
+ buf = p + 1;
+ }
+ snd_output_close(log);
+ }
return 1;
}
static void alsa_write_pre_select(struct sched *s, struct task *t)
{
+ struct pollfd pfd;
struct writer_node *wn = container_of(t, struct writer_node, task);
struct private_alsa_write_data *pad = wn->private_data;
- snd_pcm_sframes_t avail, underrun;
int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
+ if (pad)
+ pad->poll_fd = -1;
if (ret == 0)
return;
if (!pad) {
sched_request_barrier_or_min_delay(&pad->drain_barrier, s);
return;
}
- /*
- * Data is available to be written to the alsa handle. Compute number
- * of milliseconds until next buffer underrun would occur.
- *
- * snd_pcm_avail_update() updates the current available count of
- * samples for writing. It is a light method to obtain current stream
- * position, because it does not require the user <-> kernel context
- * switch, but the value is less accurate, because ring buffer pointers
- * are updated in kernel drivers only when an interrupt occurs.
- */
- avail = snd_pcm_avail_update(pad->handle);
- if (avail < 0)
- avail = 0;
- underrun = (pad->buffer_frames - avail) * pad->buffer_time
- / pad->buffer_frames / 1000;
- if (underrun < 50)
- underrun = 50;
- underrun -= 50;
- sched_request_timeout_ms(underrun, s);
+ ret = snd_pcm_poll_descriptors(pad->handle, &pfd, 1);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", snd_strerror(-ret));
+ t->error = -E_ALSA_POLL_FDS;
+ return;
+ }
+ pad->poll_fd = pfd.fd;
+ para_fd_set(pfd.fd, &s->rfds, &s->max_fileno);
}
static void alsa_close(struct writer_node *wn)
wn->min_iqs = pad->bytes_per_frame;
goto again;
}
+ if (pad->poll_fd < 0 || !FD_ISSET(pad->poll_fd, &s->rfds))
+ return;
frames = bytes / pad->bytes_per_frame;
frames = snd_pcm_writei(pad->handle, data, frames);
- if (frames == 0 || frames == -EAGAIN)
+ if (frames == 0 || frames == -EAGAIN) {
+ /*
+ * The alsa poll fd was ready for IO but we failed to write to
+ * the alsa handle. This means another event is pending. We
+ * don't care about that but we have to dispatch the event in
+ * order to avoid a busy loop. So we simply read from the poll
+ * fd.
+ */
+ char buf[100];
+ if (read(pad->poll_fd, buf, 100))
+ do_nothing;
return;
+ }
if (frames > 0) {
btr_consume(btrn, frames * pad->bytes_per_frame);
goto again;
{
struct alsa_write_args_info *conf = para_calloc(sizeof(*conf));
- PARA_INFO_LOG("options: %s, %zd\n", options, strcspn(options, " \t"));
/* exits on errors */
alsa_cmdline_parser_string(options, conf, "alsa_write");
return conf;