- pad->buffer_frames = 1000 * pad->buffer_time / pad->samplerate;
- PARA_INFO_LOG("max buffered frames: %d\n", pad->buffer_frames);
- return 1;
-}
-
-/* Open an instance of the alsa writer. */
-static int alsa_open_nobtr(struct writer_node *wn)
-{
- struct alsa_write_args_info *conf = wn->conf;
- struct writer_node_group *wng = wn->wng;
- struct private_alsa_write_data *pad = para_calloc(sizeof(*pad));
-
- wn->private_data = pad;
- if (!conf->samplerate_given && wng->samplerate)
- pad->samplerate = *wng->samplerate;
- else
- pad->samplerate = conf->samplerate_arg;
- if (!conf->channels_given && wng->channels)
- pad->channels = *wng->channels;
- else
- pad->channels = conf->channels_arg;
- PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
- return 1;
-}
-
-static int alsa_open_btr(struct writer_node *wn)
-{
- struct alsa_write_args_info *conf = wn->conf;
- struct private_alsa_write_data *pad = para_calloc(sizeof(*pad));
- int ret;
- char *buf = NULL;
-
- sprintf(wn->task.status, "alsa writer");
- wn->private_data = pad;
-
- /* defaults */
- pad->samplerate = conf->samplerate_arg;
- pad->channels = conf->channels_arg;
-
- if (!conf->samplerate_given) { /* config option trumps btr_exec */
- /* ask parent btr nodes */
- ret = btr_exec_up(wn->btrn, "samplerate", &buf);
- if (ret >= 0) {
- int32_t rate;
- ret = para_atoi32(buf, &rate);
- if (ret < 0) /* should not happen */
- goto out;
- pad->samplerate = rate;
- }
- freep(&buf);
- }
-
- if (!conf->channels_given) {
- ret = btr_exec_up(wn->btrn, "channels", &buf);
- if (ret >= 0) {
- int32_t ch;
- ret = para_atoi32(buf, &ch);
- if (ret < 0)
- goto out;
- pad->channels = ch;
- }
- freep(&buf);
- }
- PARA_INFO_LOG("%d channel(s), %dHz\n", pad->channels, pad->samplerate);
- ret = 1;
-out:
- freep(&buf);
- if (ret < 0)
- free(pad);
- return ret;
-}
-static int alsa_open(struct writer_node *wn)
-{
- struct alsa_write_args_info *conf = wn->conf;
-
- if (conf->buffer_tree_given)
- return alsa_open_btr(wn);
- else
- return alsa_open_nobtr(wn);
-
-}
-
-static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
-{
- struct alsa_write_args_info *conf = wn->conf;
- struct private_alsa_write_data *pad = wn->private_data;
- struct writer_node_group *wng = wn->wng;
- struct timeval tv;
- snd_pcm_sframes_t avail, underrun;
-
- if (!pad->handle)
- return 1;
- if (conf->buffer_tree_given) {
- size_t sz = btr_get_input_queue_size(wn->btrn);
- if (sz < pad->bytes_per_frame)
- return 1;
- } else {
- 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;
- //PARA_CRIT_LOG("timout: %lu\n", tv2ms(&s->timeout));