+ struct timeval tv;
+ snd_pcm_sframes_t avail, underrun;
+ int ret;
+
+ if (!pad->handle)
+ return;
+ ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
+ if (ret < 0)
+ sched_request_timeout_ms(20, s);
+ if (ret <= 0)
+ 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;
+ ms2tv(underrun, &tv);
+ if (tv_diff(&s->timeout, &tv, NULL) > 0)
+ s->timeout = tv;
+}