- if (tv_diff(&s->timeout, &tv, NULL) > 0)
- s->timeout = tv;
- //PARA_CRIT_LOG("timout: %lu\n", tv2ms(&s->timeout));
- return 1;
-}
-
-static void alsa_write_pre_select_btr(struct sched *s, struct task *t)
-{
- struct writer_node *wn = container_of(t, struct writer_node, task);
- t->error = alsa_write_pre_select(s, wn);
-}
-
-static void xrun(snd_pcm_t *handle)
-{
- snd_pcm_status_t *status;
- int ret;
- struct timeval tv, diff;
-
- snd_pcm_status_alloca(&status);
- ret = snd_pcm_status(handle, status);
- if (ret < 0)
- return;
- if (snd_pcm_status_get_state(status) != SND_PCM_STATE_XRUN)
- return;
- snd_pcm_status_get_trigger_tstamp(status, &tv);
- tv_diff(now, &tv, &diff);
- PARA_WARNING_LOG("underrun: %lums\n", tv2ms(&diff));
-}
-
-static int alsa_write_post_select(__a_unused struct sched *s,
- struct writer_node *wn)
-{
- struct private_alsa_write_data *pad = wn->private_data;
- struct writer_node_group *wng = wn->wng;
- size_t bytes = *wng->loaded - wn->written;
- unsigned char *data = (unsigned char*)*wng->bufp + wn->written;
- snd_pcm_sframes_t ret, frames, avail;
-
- if (*wng->input_error < 0 && (!pad->handle || bytes < pad->bytes_per_frame)) {
- wn->written = *wng->loaded;
- return *wng->input_error;
- }
- if (!bytes) /* no data available */
- return 0;
- if (!pad->handle) {
- int err = alsa_init(pad, wn->conf);
- if (err < 0)
- return err;
- }
- frames = bytes / pad->bytes_per_frame;
- if (!frames) /* less than a single frame available */
- return 0;
- avail = snd_pcm_avail_update(pad->handle);
- if (avail <= 0)
- return 0;
- frames = PARA_MIN(frames, avail);
- ret = snd_pcm_writei(pad->handle, data, frames);
- if (ret >= 0) {
- wn->written += ret * pad->bytes_per_frame;
- return 1;
- }
- if (ret == -EPIPE) {
- xrun(pad->handle);
- snd_pcm_prepare(pad->handle);
- return 0;
- }
- PARA_WARNING_LOG("%s\n", snd_strerror(-ret));
- if (ret == -EAGAIN)
- return 0;
- return -E_ALSA_WRITE;