- if (!*wng->loaded)
- return 1;
- 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 (!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;
+}
+
+static void alsa_close(struct writer_node *wn)
+{
+ struct private_alsa_write_data *pad = wn->private_data;
+ PARA_INFO_LOG("closing writer node %p\n", wn);
+
+ if (pad->handle) {
+ /*
+ * It's OK to have a blocking operation here because we already
+ * made sure that the PCM output buffer is (nearly) empty.
+ */
+ snd_pcm_nonblock(pad->handle, 0);
+ snd_pcm_drain(pad->handle);
+ snd_pcm_close(pad->handle);
+ snd_config_update_free_global();