From 03fec24e192ea1b5c0cf957a5a64c0db9d33e67a Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 16 Sep 2016 14:24:15 +0200 Subject: player: litter code with explicit wakeup calls This does 3 kinds of changes: - change sleeptime=x to mp_set_timeout() - change sleeptime=0 to mp_wakeup_core() calls (to be more explicit) - change commands etc. to call mp_wakeup_core() if they do changes that require the playloop to be rerun This is preparation for the following changes. The goal is to process client API requests without having to rerun the playloop every time. As of this commit, the changes should not change behavior. In particular, the playloop is still implicitly woken up on every command. --- player/scripting.c | 1 + 1 file changed, 1 insertion(+) (limited to 'player/scripting.c') diff --git a/player/scripting.c b/player/scripting.c index cb35b1bfd7..afa567ca6a 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -97,6 +97,7 @@ static void wait_loaded(struct MPContext *mpctx) { while (!mp_clients_all_initialized(mpctx)) mp_idle(mpctx); + mp_wakeup_core(mpctx); // avoid lost wakeups during waiting } static void mp_load_script(struct MPContext *mpctx, const char *fname) -- cgit v1.2.3 From fb67db8b72a329fd017409338d6742c773ce85bf Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 20 Sep 2016 00:59:54 +0200 Subject: player: make --osc/--ytdl settable during playback Setting the osc or ytdl properties will now load/unload the associated scripts. (For ytdl this does not mean the currently played URL will be reloaded.) Also add a changelog entry for this, which also covers the preceding work for --terminal. --- player/scripting.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'player/scripting.c') diff --git a/player/scripting.c b/player/scripting.c index afa567ca6a..b6ba69ac07 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -176,13 +176,41 @@ static char **list_script_files(void *talloc_ctx, char *path) return files; } +static void load_builtin_script(struct MPContext *mpctx, bool enable, + const char *fname) +{ + void *tmp = talloc_new(NULL); + // (The name doesn't have to match if there were conflicts with other + // scripts, so this is on best-effort basis.) + char *name = script_name_from_filename(tmp, fname); + if (enable != mp_client_exists(mpctx, name)) { + if (enable) { + mp_load_script(mpctx, fname); + } else { + // Try to unload it by sending a shutdown event. Wait until it has + // terminated, or re-enabling the script could be racy (because it'd + // recognize a still-terminating script as "loaded"). + while (mp_client_exists(mpctx, name)) { + if (mp_client_send_event(mpctx, name, MPV_EVENT_SHUTDOWN, NULL) < 0) + break; + mp_idle(mpctx); + } + mp_wakeup_core(mpctx); // avoid lost wakeups during waiting + } + } + talloc_free(tmp); +} + +void mp_load_builtin_scripts(struct MPContext *mpctx) +{ + load_builtin_script(mpctx, mpctx->opts->lua_load_osc, "@osc.lua"); + load_builtin_script(mpctx, mpctx->opts->lua_load_ytdl, "@ytdl_hook.lua"); +} + void mp_load_scripts(struct MPContext *mpctx) { // Load scripts from options - if (mpctx->opts->lua_load_osc) - mp_load_script(mpctx, "@osc.lua"); - if (mpctx->opts->lua_load_ytdl) - mp_load_script(mpctx, "@ytdl_hook.lua"); + mp_load_builtin_scripts(mpctx); char **files = mpctx->opts->script_files; for (int n = 0; files && files[n]; n++) { if (files[n][0]) -- cgit v1.2.3 From 2ac74977c5f6561e33ad37c68b9da47739c7fd6c Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 22 Sep 2016 20:43:33 +0200 Subject: command: add a load-script command The intention is to give libmpv users as much flexibility to load scripts as using mpv from CLI, but without restricting libmpv users from having to decide everything on creation time, or having to go through hacks like recreating the libmpv context to update state. --- player/scripting.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'player/scripting.c') diff --git a/player/scripting.c b/player/scripting.c index b6ba69ac07..76395c6ad6 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -100,7 +100,7 @@ static void wait_loaded(struct MPContext *mpctx) mp_wakeup_core(mpctx); // avoid lost wakeups during waiting } -static void mp_load_script(struct MPContext *mpctx, const char *fname) +int mp_load_script(struct MPContext *mpctx, const char *fname) { char *ext = mp_splitext(fname, NULL); const struct mp_scripting *backend = NULL; @@ -114,7 +114,7 @@ static void mp_load_script(struct MPContext *mpctx, const char *fname) if (!backend) { MP_VERBOSE(mpctx, "Can't load unknown script: %s\n", fname); - return; + return -1; } struct thread_arg *arg = talloc_ptrtype(NULL, arg); @@ -129,7 +129,7 @@ static void mp_load_script(struct MPContext *mpctx, const char *fname) }; if (!arg->client) { talloc_free(arg); - return; + return -1; } arg->log = mp_client_get_log(arg->client); @@ -139,13 +139,13 @@ static void mp_load_script(struct MPContext *mpctx, const char *fname) if (pthread_create(&thread, NULL, script_thread, arg)) { mpv_detach_destroy(arg->client); talloc_free(arg); - return; + return -1; } wait_loaded(mpctx); MP_VERBOSE(mpctx, "Done loading %s.\n", fname); - return; + return 0; } static int compare_filename(const void *pa, const void *pb) -- cgit v1.2.3 From a314b1013fc148d1038129b0bda3b6a3cc85ed2a Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 26 Sep 2016 14:50:20 +0200 Subject: scripting: don't attempt to load builtin scripts twice During init it will first call mp_load_builtin_scripts(), and then again via mp_load_scripts(). This was harmless (a second attempt won't load it again if the first one was successful), but it's unnecessary, and also looks confusing if the scripts failed to load the first time. --- player/scripting.c | 1 - 1 file changed, 1 deletion(-) (limited to 'player/scripting.c') diff --git a/player/scripting.c b/player/scripting.c index 76395c6ad6..4b92f7bf1b 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -210,7 +210,6 @@ void mp_load_builtin_scripts(struct MPContext *mpctx) void mp_load_scripts(struct MPContext *mpctx) { // Load scripts from options - mp_load_builtin_scripts(mpctx); char **files = mpctx->opts->script_files; for (int n = 0; files && files[n]; n++) { if (files[n][0]) -- cgit v1.2.3