Switch audiod over to the buffer tree API.
[paraslash.git] / oggdec_filter.c
index fc03077bf4fc65505affd7db301f0b3dae4fd67c..511e6405e533c1028a511b0828ea479b31b67b77 100644 (file)
@@ -159,10 +159,8 @@ static void ogg_close(struct filter_node *fn)
        fn->private_data = NULL;
 }
 
-#define OGGDEC_MAX_PENDING (640 * 1024)
 #define OGGDEC_OUTPUT_CHUNK_SIZE (64 * 1024)
 
-
 static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
 {
        struct filter_node *fn = btr_context(btrn);
@@ -183,18 +181,59 @@ static int oggdec_execute(struct btr_node *btrn, const char *cmd, char **result)
        return -ERRNO_TO_PARA_ERROR(ENOTSUP);
 }
 
-static void ogg_pre_select(struct sched *s, struct task *t)
+static int ogg_init(struct filter_node *fn)
 {
-       struct filter_node *fn = container_of(t, struct filter_node, task);
-       size_t iqs = btr_get_input_queue_size(fn->btrn);
-
-       t->error = 0;
-       if (iqs == 0)
-               return;
-       if (btr_bytes_pending(fn->btrn) > OGGDEC_MAX_PENDING)
-               return; /* FIXME, should use reasonable bound on timeout */
-       s->timeout.tv_sec = 0;
-       s->timeout.tv_usec = 1;
+       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)
@@ -202,48 +241,21 @@ static void ogg_post_select(__a_unused struct sched *s, struct task *t)
        struct filter_node *fn = container_of(t, struct filter_node, task);
        struct private_oggdec_data *pod = fn->private_data;
        struct btr_node *btrn = fn->btrn;
-       size_t iqs = btr_get_input_queue_size(btrn);
+       size_t iqs, len;
        int ret;
+       char *in;
 
        t->error = 0;
-       if (!pod->vf && iqs) {
-               struct oggdec_filter_args_info *conf = fn->conf;
-               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 */
-                       int ib = 1024 * conf->initial_buffer_arg; /* initial buffer */
-                       if (iqs < ib) {
-                               free(pod->vf);
-                               pod->vf = NULL;
-                               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 = btr_node_status(btrn, fn->min_iqs, BTR_NT_INTERNAL);
+       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) {
+               ret = ogg_init(fn);
+               if (ret <= 0)
+                       goto out;
        }
        for (;;) {
                char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
@@ -252,25 +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);
-       ogg_close(fn);
-       t->error = ret;
-       btr_del_node(btrn);
 }
 
 static ssize_t ogg_convert(char *inbuffer, size_t len, struct filter_node *fn)
@@ -370,6 +384,11 @@ err:
        return ret;
 }
 
+static void oggdec_free_config(void *conf)
+{
+       oggdec_cmdline_parser_free(conf);
+}
+
 /**
  * The init function of the ogg vorbis decoder.
  *
@@ -383,9 +402,10 @@ void oggdec_filter_init(struct filter *f)
        f->open = ogg_open;
        f->close = ogg_close;
        f->convert = ogg_convert;
-       f->pre_select = ogg_pre_select;
+       f->pre_select = generic_filter_pre_select;
        f->post_select = ogg_post_select;
        f->parse_config = oggdec_parse_config;
+       f->free_config = oggdec_free_config;
        f->execute = oggdec_execute;
        f->help = (struct ggo_help) {
                .short_help = oggdec_filter_args_info_help,