oggdec: Move init into a separate function.
authorAndre Noll <maan@systemlinux.org>
Wed, 6 Jan 2010 23:35:55 +0000 (00:35 +0100)
committerAndre Noll <maan@systemlinux.org>
Wed, 6 Jan 2010 23:35:55 +0000 (00:35 +0100)
oggdec_filter.c

index 4b6232b..2c9d25f 100644 (file)
@@ -181,6 +181,61 @@ static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
        return -ERRNO_TO_PARA_ERROR(ENOTSUP);
 }
 
+static int ogg_init(struct filter_node *fn)
+{
+       struct private_oggdec_data *pod = fn->private_data;
+       struct btr_node *btrn = fn->btrn;
+       int ret, oret;
+       size_t iqs;
+       struct timeval delay = {0, 500 * 1000};
+
+       pod->vf = para_malloc(sizeof(struct OggVorbis_File));
+       PARA_NOTICE_LOG("min_iqs: %zu, opening ov callbacks\n", fn->min_iqs);
+open:
+       oret = ov_open_callbacks(fn, pod->vf,
+               NULL, /* no initial buffer */
+               0, /* no initial bytes */
+               ovc); /* the ov_open_callbacks */
+       if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
+               /* this might be due to the input buffer being too small */
+               if (!btr_no_parent(btrn)) {
+                       fn->min_iqs += 1000;
+                       iqs = btr_get_input_queue_size(btrn);
+                       ret = 0;
+                       if (iqs <= fn->min_iqs)
+                               goto out;
+                       btr_merge(btrn, fn->min_iqs);
+                       goto open;
+               }
+               ret = (oret == OV_ENOTVORBIS)?
+                       -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
+               goto out;
+       }
+       ret = -E_OGGDEC_READ;
+       if (oret == OV_EREAD)
+               goto out;
+       ret = -E_OGGDEC_VERSION;
+       if (oret == OV_EVERSION)
+               goto out;
+       ret = -E_OGGDEC_FAULT;
+       if (oret < 0)
+               goto out;
+       pod->channels = ov_info(pod->vf, 0)->channels;
+       pod->samplerate = ov_info(pod->vf, 0)->rate;
+       PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
+               pod->samplerate);
+       /* wait a bit to avoid buffer underruns */
+       tv_add(now, &delay, &pod->stream_start);
+       ret = 1;
+out:
+       if (ret <= 0) {
+               free(pod->vf);
+               pod->vf = NULL;
+       } else
+               fn->min_iqs = 0;
+       return ret;
+}
+
 static void ogg_post_select(__a_unused struct sched *s, struct task *t)
 {
        struct filter_node *fn = container_of(t, struct filter_node, task);
@@ -192,50 +247,15 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
 
        t->error = 0;
        ret = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
-       if (ret < 0)
-               goto err;
-       if (ret == 0)
-               return;
+       if (ret <= 0)
+               goto out;
        btr_merge(btrn, fn->min_iqs);
        len = btr_next_buffer(btrn, &in);
        iqs = btr_get_input_queue_size(btrn);
        if (!pod->vf) {
-               int oret;
-
-               pod->vf = para_malloc(sizeof(struct OggVorbis_File));
-               PARA_NOTICE_LOG("input queue: %zu, opening ov callbacks\n", iqs);
-               oret = ov_open_callbacks(fn, pod->vf,
-                       NULL, /* no initial buffer */
-                       0, /* no initial bytes */
-                       ovc); /* the ov_open_callbacks */
-               if (oret == OV_ENOTVORBIS || oret == OV_EBADHEADER) {
-                       /* this might be due to the input buffer being too small */
-                       if (!btr_no_parent(btrn)) {
-                               free(pod->vf);
-                               pod->vf = NULL;
-                               fn->min_iqs = iqs + 1;
-                               return;
-                       }
-                       ret = (oret == OV_ENOTVORBIS)?
-                               -E_OGGDEC_NOTVORBIS : -E_OGGDEC_BADHEADER;
-                       goto err;
-               }
-               ret = -E_OGGDEC_READ;
-               if (oret == OV_EREAD)
-                       goto err;
-               ret = -E_OGGDEC_VERSION;
-               if (oret == OV_EVERSION)
-                       goto err;
-               ret = -E_OGGDEC_FAULT;
-               if (oret < 0)
-                       goto err;
-               pod->channels = ov_info(pod->vf, 0)->channels;
-               pod->samplerate = ov_info(pod->vf, 0)->rate;
-               PARA_NOTICE_LOG("%d channels, %d Hz\n", pod->channels,
-                       pod->samplerate);
-               ///* wait a bit to avoid buffer underruns */
-               //tv_add(now, &(struct timeval){0, 500 * 1000}, &pod->stream_start);
-               return;
+               ret = ogg_init(fn);
+               if (ret <= 0)
+                       goto out;
        }
        for (;;) {
                char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
@@ -244,24 +264,27 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
                if (read_ret <= 0)
                        free(out);
                if (read_ret == 0) {
-                       ret = -E_OGGDEC_EOF;
                        if (btr_no_parent(btrn))
-                               goto err;
-                       return;
+                               ret = -E_OGGDEC_EOF;
+                       else
+                               ret = 0;
+                       goto out;
                }
+               ret = 0;
                if (read_ret == OV_HOLE)
-                       return;
-               if (read_ret < 0) {
-                       ret = -E_OGGDEC_BADLINK;
-                       goto err;
-               }
+                       goto out;
+               ret = -E_OGGDEC_BADLINK;
+               if (read_ret < 0)
+                       goto out;
                btr_add_output(out, read_ret, btrn);
+               if (btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL) == 0)
+                       return; /* enough data for the moment */
+       }
+out:
+       if (ret < 0) {
+               t->error = ret;
+               btr_remove_node(btrn);
        }
-
-err:
-       assert(ret < 0);
-       t->error = ret;
-       btr_remove_node(btrn);
 }
 
 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)