alsa: Split alsa_open().
authorAndre Noll <maan@systemlinux.org>
Sun, 15 Mar 2009 16:35:37 +0000 (17:35 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 15 Mar 2009 16:35:37 +0000 (17:35 +0100)
Only perform what is absolutely necessary in alsa_open(). Call the
real alsa initialization from alsa_post_select(), when we actually
have audio data available to start streaming.

This should get rid of the sound artefacts that happen sometimes
at the beginning of the stream.

alsa_write.c

index dfd2421..f63b7a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
@@ -51,33 +51,16 @@ struct private_alsa_write_data {
        unsigned channels;
 };
 
-/*
- * open and prepare the PCM handle for writing
- *
- * Install PCM software and hardware configuration. Exit on errors.
- */
-static int alsa_open(struct writer_node *w)
+/* Install PCM software and hardware configuration. */
+static int alsa_init(struct private_alsa_write_data *pad,
+               struct alsa_write_args_info *conf)
 {
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
        snd_pcm_uframes_t buffer_size, start_threshold, stop_threshold;
-       int err;
        snd_pcm_uframes_t period_size;
-       struct private_alsa_write_data *pad = para_calloc(sizeof(struct
-               private_alsa_write_data));
-       struct alsa_write_args_info *conf = w->conf;
-       struct writer_node_group *wng = w->wng;
+       int err;
 
-       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);
-       w->private_data = pad;
        err = snd_pcm_open(&pad->handle, conf->device_arg,
                SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
        if (err < 0)
@@ -133,7 +116,27 @@ static int alsa_open(struct writer_node *w)
        PARA_INFO_LOG("bytes per frame: %zu\n", pad->bytes_per_frame);
        if (snd_pcm_nonblock(pad->handle, 1))
                PARA_ERROR_LOG("failed to set nonblock mode\n");
-       tv_add(now, &(struct timeval ){0, 100 * 1000}, &pad->next_chunk);
+       return 1;
+}
+
+/* Open an instance of the alsa writer. */
+static int alsa_open(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);
+       tv_add(now, &(struct timeval){0, 100 * 1000}, &pad->next_chunk);
        return 1;
 }
 
@@ -143,7 +146,7 @@ static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
        struct writer_node_group *wng = wn->wng;
        struct timeval diff;
 
-       if (*wng->loaded < pad->bytes_per_frame)
+       if (!*wng->loaded)
                return 1;
        if (tv_diff(now, &pad->next_chunk, &diff) < 0) {
                if (tv_diff(&s->timeout, &diff, NULL) > 0)
@@ -153,7 +156,6 @@ static int alsa_write_pre_select(struct sched *s, struct writer_node *wn)
                s->timeout.tv_usec = 1;
        }
        return 1;
-//     PARA_INFO_LOG("timeout: %lu\n", tv2ms(&s->timeout));
 }
 
 static int alsa_write_post_select(__a_unused struct sched *s,
@@ -161,19 +163,21 @@ static int alsa_write_post_select(__a_unused struct sched *s,
 {
        struct private_alsa_write_data *pad = wn->private_data;
        struct writer_node_group *wng = wn->wng;
-       size_t frames = (*wng->loaded - wn->written) / pad->bytes_per_frame;
-       snd_pcm_sframes_t ret;
+       size_t frames, bytes = *wng->loaded - wn->written;
        unsigned char *data = (unsigned char*)wng->buf + wn->written;
        struct timeval tv;
+       snd_pcm_sframes_t ret;
 
-//     PARA_INFO_LOG("%zd frames\n", frames);
-       if (!frames) {
-               if (*wng->input_error)
-                       wn->written = *wng->loaded;
-               return 1;
-       }
+       if (!bytes) /* no data available */
+               goto out;
        if (tv_diff(now, &pad->next_chunk, NULL) < 0)
-               return 1;
+               goto out;
+       if (!pad->handle) {
+               int err = alsa_init(pad, wn->conf);
+               if (err < 0)
+                       return err;
+       }
+       frames = bytes / pad->bytes_per_frame;
        ret = snd_pcm_writei(pad->handle, data, frames);
        if (ret == -EPIPE) {
                PARA_WARNING_LOG("EPIPE\n");
@@ -195,6 +199,11 @@ static int alsa_write_post_select(__a_unused struct sched *s,
                ms2tv(pad->buffer_time / pad->bytes_per_frame / 1000, &tv);
                tv_add(now, &tv, &pad->next_chunk);
        }
+out:
+       if (*wng->input_error < 0) {
+               wn->written = *wng->loaded;
+               return *wng->input_error;
+       }
        return 1;
 }