- if (tv_diff(now, &pad->next_chunk, &diff) < 0) {
- if (tv_diff(&s->timeout, &diff, NULL) > 0)
- s->timeout = diff;
- } else {
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
- }
+ if (*wng->loaded - wn->written < pad->bytes_per_frame)
+ return 1;
+ /*
+ * 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;