+ result = PARA_MIN(result, pt->seconds);
+ result = PARA_MAX(result, 0UL);
+ return result;
+}
+
+static void wipe_receiver_node(struct play_task *pt)
+{
+ PARA_NOTICE_LOG("cleaning up receiver node\n");
+ btr_remove_node(&pt->rn.btrn);
+ afh_recv->close(&pt->rn);
+ afh_recv->free_config(pt->rn.conf);
+ memset(&pt->rn, 0, sizeof(struct receiver_node));
+}
+
+/* returns: 0 not eof, 1: eof, < 0: fatal error. */
+static int get_playback_error(struct play_task *pt)
+{
+ int err = pt->wn.task.error;
+
+ if (err >= 0)
+ return 0;
+ if (pt->fn.task.error >= 0)
+ return 0;
+ if (pt->rn.task.error >= 0)
+ return 0;
+ if (err == -E_BTR_EOF || err == -E_RECV_EOF || err == -E_EOF
+ || err == -E_WRITE_COMMON_EOF)
+ return 1;
+ return err;
+}
+
+static int eof_cleanup(struct play_task *pt)
+{
+ struct writer *w = writers + DEFAULT_WRITER;
+ struct filter *decoder = filters + pt->fn.filter_num;
+ int ret;
+
+ ret = get_playback_error(pt);
+ if (ret == 0)
+ return ret;
+ PARA_NOTICE_LOG("cleaning up wn/fn nodes\n");
+ w->close(&pt->wn);
+ btr_remove_node(&pt->wn.btrn);
+ w->free_config(pt->wn.conf);
+ memset(&pt->wn, 0, sizeof(struct writer_node));
+
+ decoder->close(&pt->fn);
+ btr_remove_node(&pt->fn.btrn);
+ free(pt->fn.conf);
+ memset(&pt->fn, 0, sizeof(struct filter_node));
+
+ btr_remove_node(&pt->rn.btrn);
+ /*
+ * On eof (ret > 0), we do not wipe the receiver node struct until a
+ * new file is loaded because we still need it for jumping around when
+ * paused.
+ */
+ if (ret < 0)
+ wipe_receiver_node(pt);
+ return ret;
+}
+
+static int shuffle_compare(__a_unused const void *a, __a_unused const void *b)
+{
+ return para_random(100) - 50;
+}
+
+static void shuffle(char **base, size_t num)
+{
+ srandom(now->tv_sec);
+ qsort(base, num, sizeof(char *), shuffle_compare);
+}
+
+static struct btr_node *new_recv_btrn(struct receiver_node *rn)
+{
+ return btr_new_node(&(struct btr_node_description)
+ EMBRACE(.name = afh_recv->name, .context = rn,
+ .handler = afh_recv->execute));
+}
+
+static int open_new_file(struct play_task *pt)
+{
+ int ret;
+ char *tmp, *path = conf.inputs[pt->next_file], *afh_recv_conf[] =
+ {"play", "-f", path, "-b", "0", NULL};
+
+ PARA_NOTICE_LOG("next file: %s\n", path);
+ wipe_receiver_node(pt);
+ pt->start_chunk = 0;
+ pt->rn.btrn = new_recv_btrn(&pt->rn);
+ pt->rn.conf = afh_recv->parse_config(ARRAY_SIZE(afh_recv_conf) - 1,
+ afh_recv_conf);
+ assert(pt->rn.conf);
+ pt->rn.receiver = afh_recv;
+ ret = afh_recv->open(&pt->rn);
+ if (ret < 0) {
+ PARA_ERROR_LOG("could not open %s: %s\n", path,
+ para_strerror(-ret));
+ goto fail;
+ }
+ pt->audio_format_num = ret;
+ free(pt->afhi_txt);
+ ret = btr_exec_up(pt->rn.btrn, "afhi", &pt->afhi_txt);
+ if (ret < 0)
+ pt->afhi_txt = make_message("[afhi command failed]\n");
+ ret = btr_exec_up(pt->rn.btrn, "seconds_total", &tmp);
+ if (ret < 0)
+ pt->seconds = 1;
+ else {
+ int32_t x;
+ ret = para_atoi32(tmp, &x);
+ pt->seconds = ret < 0? 1 : x;
+ free(tmp);
+ tmp = NULL;
+ }
+ ret = btr_exec_up(pt->rn.btrn, "chunks_total", &tmp);
+ if (ret < 0)
+ pt->num_chunks = 1;
+ else {
+ int32_t x;
+ ret = para_atoi32(tmp, &x);
+ pt->num_chunks = ret < 0? 1 : x;
+ free(tmp);
+ tmp = NULL;
+ }
+ pt->rn.task.pre_select = afh_recv->pre_select;
+ pt->rn.task.post_select = afh_recv->post_select;
+ sprintf(pt->rn.task.status, "%s receiver node", afh_recv->name);