From c349e2f337693c53687b0bd5e4d8669363e2d79d Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 6 May 2018 21:23:28 +0200 Subject: command: make sub-add and audio-add commands async Pretty trivial, since commands can be async now, and the common code even provides convenience like running commands on a worker thread. The only ugly thing is that mp_add_external_file() needs an extra flag for locking. This is because there's still some code which calls this synchronously from the main thread, and unlocking the core makes no sense there. --- DOCS/man/input.rst | 3 +++ player/command.c | 6 ++++-- player/core.h | 2 +- player/loadfile.c | 20 ++++++++++++++++---- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/DOCS/man/input.rst b/DOCS/man/input.rst index 6706fc3198..205c732781 100644 --- a/DOCS/man/input.rst +++ b/DOCS/man/input.rst @@ -909,6 +909,9 @@ The only exception is the current legacy behavior with screenshot commands, which will be fixed later. Using the ``async`` prefix makes them run the file saving code in a detached manner. +Currently the following commands have different waiting characteristics with +sync vs. async: sub-add, audio-add + Input Sections -------------- diff --git a/player/command.c b/player/command.c index 4600e65236..9aa3a9e6d8 100644 --- a/player/command.c +++ b/player/command.c @@ -5520,7 +5520,7 @@ static void cmd_track_add(void *p) return; } } - int first = mp_add_external_file(mpctx, cmd->args[0].v.s, type); + int first = mp_add_external_file(mpctx, cmd->args[0].v.s, type, true); if (first < 0) { cmd->success = false; return; @@ -5584,7 +5584,7 @@ static void cmd_track_reload(void *p) if (t && t->is_external && t->external_filename) { char *filename = talloc_strdup(NULL, t->external_filename); mp_remove_track(mpctx, t); - nt_num = mp_add_external_file(mpctx, filename, type); + nt_num = mp_add_external_file(mpctx, filename, type, false); talloc_free(filename); } @@ -6039,6 +6039,7 @@ const struct mp_cmd_def mp_cmds[] = { OARG_STRING(""), OARG_STRING(""), }, .priv = &(const int){STREAM_SUB}, + .spawn_thread = true, }, { "sub-remove", cmd_track_remove, { OARG_INT(-1) }, .priv = &(const int){STREAM_SUB}, }, @@ -6169,6 +6170,7 @@ const struct mp_cmd_def mp_cmds[] = { OARG_STRING(""), OARG_STRING(""), }, .priv = &(const int){STREAM_AUDIO}, + .spawn_thread = true, }, { "audio-remove", cmd_track_remove, { OARG_INT(-1) }, .priv = &(const int){STREAM_AUDIO}, }, diff --git a/player/core.h b/player/core.h index 0840a7836d..5ca227a8fa 100644 --- a/player/core.h +++ b/player/core.h @@ -488,7 +488,7 @@ struct playlist_entry *mp_check_playlist_resume(struct MPContext *mpctx, void mp_abort_playback_async(struct MPContext *mpctx); void uninit_player(struct MPContext *mpctx, unsigned int mask); int mp_add_external_file(struct MPContext *mpctx, char *filename, - enum stream_type filter); + enum stream_type filter, bool unlock); #define FLAG_MARK_SELECTION 1 void mp_switch_track(struct MPContext *mpctx, enum stream_type type, struct track *track, int flags); diff --git a/player/loadfile.c b/player/loadfile.c index 65d693e264..09d3bad3a1 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -579,7 +579,7 @@ bool mp_remove_track(struct MPContext *mpctx, struct track *track) // Add the given file as additional track. The filter argument controls how or // if tracks are auto-selected at any point. int mp_add_external_file(struct MPContext *mpctx, char *filename, - enum stream_type filter) + enum stream_type filter, bool unlock) { struct MPOpts *opts = mpctx->opts; if (!filename) @@ -600,11 +600,19 @@ int mp_add_external_file(struct MPContext *mpctx, char *filename, break; } + if (unlock) + mp_core_unlock(mpctx); + struct demuxer *demuxer = demux_open_url(filename, ¶ms, mpctx->playback_abort, mpctx->global); + if (demuxer) + enable_demux_thread(mpctx, demuxer); + + if (unlock) + mp_core_lock(mpctx); + if (!demuxer) goto err_out; - enable_demux_thread(mpctx, demuxer); if (opts->rebase_start_time) demux_set_ts_offset(demuxer, -demuxer->start_time); @@ -619,7 +627,11 @@ int mp_add_external_file(struct MPContext *mpctx, char *filename, } if (!has_any) { + if (unlock) + mp_core_unlock(mpctx); free_demuxer_and_stream(demuxer); + if (unlock) + mp_core_lock(mpctx); char *tname = mp_tprintf(20, "%s ", stream_type_name(filter)); if (filter == STREAM_TYPE_COUNT) tname = ""; @@ -652,7 +664,7 @@ static void open_external_files(struct MPContext *mpctx, char **files, enum stream_type filter) { for (int n = 0; files && files[n]; n++) - mp_add_external_file(mpctx, files[n], filter); + mp_add_external_file(mpctx, files[n], filter, false); } void autoload_external_files(struct MPContext *mpctx) @@ -691,7 +703,7 @@ void autoload_external_files(struct MPContext *mpctx) goto skip; if (list[i].type == STREAM_AUDIO && !sc[STREAM_VIDEO]) goto skip; - int first = mp_add_external_file(mpctx, filename, list[i].type); + int first = mp_add_external_file(mpctx, filename, list[i].type, false); if (first < 0) goto skip; -- cgit v1.2.3