+__noreturn static void print_help_and_die(void)
+{
+ struct ggo_help help = DEFINE_GGO_HELP(play);
+ unsigned flags = conf.detailed_help_given?
+ GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
+
+ ggo_print_help(&help, flags);
+ printf("supported audio formats: %s\n", AUDIO_FORMAT_HANDLERS);
+ exit(0);
+}
+
+static void parse_config_or_die(int argc, char *argv[])
+{
+ int i, ret;
+ char *config_file;
+ struct play_cmdline_parser_params params = {
+ .override = 0,
+ .initialize = 1,
+ .check_required = 0,
+ .check_ambiguity = 0,
+ .print_errors = 1
+ };
+
+ play_cmdline_parser_ext(argc, argv, &conf, ¶ms);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ version_handle_flag("play", conf.version_given);
+ if (conf.help_given || conf.detailed_help_given)
+ print_help_and_die();
+ if (conf.config_file_given)
+ config_file = para_strdup(conf.config_file_arg);
+ else {
+ char *home = para_homedir();
+ config_file = make_message("%s/.paraslash/play.conf", home);
+ free(home);
+ }
+ ret = file_exists(config_file);
+ if (conf.config_file_given && !ret) {
+ PARA_EMERG_LOG("can not read config file %s\n", config_file);
+ goto err;
+ }
+ if (ret) {
+ params.initialize = 0;
+ params.check_required = 1;
+ play_cmdline_parser_config_file(config_file, &conf, ¶ms);
+ loglevel = get_loglevel_by_name(conf.loglevel_arg);
+ }
+ for (i = 0; i < conf.key_map_given; i++) {
+ char *s = strchr(conf.key_map_arg[i] + 1, ':');
+ if (s)
+ continue;
+ PARA_EMERG_LOG("invalid key map arg: %s\n", conf.key_map_arg[i]);
+ goto err;
+ }
+ free(config_file);
+ return;
+err:
+ free(config_file);
+ exit(EXIT_FAILURE);
+}
+
+static char get_playback_state(struct play_task *pt)
+{
+ switch (pt->rq) {
+ case CRT_NONE: return pt->playing? 'P' : 'U';
+ case CRT_REPOS: return 'R';
+ case CRT_FILE_CHANGE: return 'F';
+ case CRT_TERM_RQ: return 'X';
+ }
+ assert(false);
+};
+
+static long unsigned get_play_time(struct play_task *pt)
+{
+ char state = get_playback_state(pt);
+ long unsigned result;
+
+ if (state != 'P' && state != 'U')
+ return 0;
+ if (pt->num_chunks == 0 || pt->seconds == 0)
+ return 0;
+ /* where the stream started (in seconds) */
+ result = pt->start_chunk * pt->seconds / pt->num_chunks;
+ if (pt->wn.btrn) { /* Add the uptime of the writer node */
+ struct timeval diff = {.tv_sec = 0}, wstime;
+ btr_get_node_start(pt->wn.btrn, &wstime);
+ if (wstime.tv_sec > 0)
+ tv_diff(now, &wstime, &diff);
+ result += diff.tv_sec;
+ }
+ 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;
+
+ if (!pt->wn.task)
+ return 0;
+ err = task_status(pt->wn.task);
+ if (err >= 0)
+ return 0;
+ if (task_status(pt->fn.task) >= 0)
+ return 0;
+ if (task_status(pt->rn.task) >= 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");
+ task_reap(&pt->wn.task);
+ w->close(&pt->wn);
+ btr_remove_node(&pt->wn.btrn);
+ w->free_config(pt->wn.conf);
+ memset(&pt->wn, 0, sizeof(struct writer_node));
+
+ task_reap(&pt->fn.task);
+ decoder->close(&pt->fn);
+ btr_remove_node(&pt->fn.btrn);
+ free(pt->fn.conf);
+ memset(&pt->fn, 0, sizeof(struct filter_node));
+
+ task_reap(&pt->rn.task);
+ 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(time(NULL));
+ 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;
+ }
+ return 1;
+fail:
+ wipe_receiver_node(pt);
+ return ret;
+}
+
+static int load_file(struct play_task *pt)