+#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);
+ struct private_oggdec_data *pod = fn->private_data;
+
+ if (!strcmp(cmd, "samplerate")) {
+ if (pod->samplerate == 0)
+ return -ERRNO_TO_PARA_ERROR(ENAVAIL);
+ *result = make_message("%u", pod->samplerate);
+ return 1;
+ }
+ if (!strcmp(cmd, "channels")) {
+ if (pod->channels == 0)
+ return -ERRNO_TO_PARA_ERROR(ENAVAIL);
+ *result = make_message("%u", pod->channels);
+ return 1;
+ }
+ return -ERRNO_TO_PARA_ERROR(ENOTSUP);
+}
+
+static void ogg_pre_select(struct sched *s, struct task *t)
+{
+ 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;
+}
+
+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, len;
+ int ret;
+ char *in;
+
+ t->error = 0;
+ ret = prepare_filter_node(btrn, pod->min_iqs);
+ if (ret < 0)
+ goto err;
+ if (ret == 0)
+ return;
+ 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;
+ pod->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;
+ }
+ for (;;) {
+ char *out = para_malloc(OGGDEC_OUTPUT_CHUNK_SIZE);
+ ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
+ ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
+ if (read_ret <= 0)
+ free(out);
+ if (read_ret == 0) {
+ ret = -E_OGGDEC_EOF;
+ if (btr_no_parent(btrn))
+ goto err;
+ return;
+ }
+ if (read_ret == OV_HOLE)
+ return;
+ if (read_ret < 0) {
+ ret = -E_OGGDEC_BADLINK;
+ goto err;
+ }
+ btr_add_output(out, read_ret, btrn);
+ }
+
+err:
+ assert(ret < 0);
+ ogg_close(fn);
+ t->error = ret;
+ btr_del_node(btrn);
+}
+