- 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 (tv_diff(now, &pad->next_chunk, NULL) < 0)
- 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)
- goto delay;
- frames = PARA_MIN(frames, avail);
- ret = snd_pcm_writei(pad->handle, data, frames);
- if (ret < 0) {
- PARA_WARNING_LOG("%s\n", snd_strerror(-ret));
- if (ret == -EPIPE) {
- snd_pcm_prepare(pad->handle);
- return 0;
+again:
+ t->error = 0;
+ ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
+ if (ret == 0)
+ return;
+ btr_merge(btrn, wn->min_iqs);
+ bytes = btr_next_buffer(btrn, &data);
+ if (ret < 0 || bytes < wn->min_iqs) { /* eof */
+ assert(btr_no_parent(btrn));
+ ret = -E_WRITE_COMMON_EOF;
+ if (!pad)
+ goto err;
+ /* wait until pending frames are played */
+ if (pad->drain_barrier.tv_sec == 0) {
+ PARA_DEBUG_LOG("waiting for device to drain\n");
+ tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
+ &pad->drain_barrier);
+ return;