+#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 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("iqs: %zu, min_iqs: %zu, opening ov callbacks\n",
+ btr_get_input_queue_size(btrn), 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;
+ PARA_CRIT_LOG("iqs: %zu\n", iqs);
+ btr_merge(btrn, fn->min_iqs);
+ pod->converted = 0;
+ 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 {
+ btr_consume(btrn, pod->converted);
+ pod->converted = 0;
+ 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);
+ struct private_oggdec_data *pod = fn->private_data;
+ struct btr_node *btrn = fn->btrn;
+ size_t iqs, len;
+ int ret;
+ char *in;
+
+ pod->converted = 0;
+ t->error = 0;
+ 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);
+ ssize_t read_ret = ov_read(pod->vf, out, OGGDEC_OUTPUT_CHUNK_SIZE,
+ ENDIAN, 2 /* 16 bit */, 1 /* signed */, NULL);
+ btr_consume(btrn, pod->converted);
+ pod->converted = 0;
+ if (read_ret <= 0)
+ free(out);
+ if (read_ret == 0) {
+ if (btr_no_parent(btrn))
+ ret = -E_OGGDEC_EOF;
+ else
+ ret = 0;
+ goto out;
+ }
+ ret = 0;
+ if (read_ret == OV_HOLE)
+ 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);
+ }
+}
+