diff options
Diffstat (limited to 'player/scripting.c')
-rw-r--r-- | player/scripting.c | 229 |
1 files changed, 189 insertions, 40 deletions
diff --git a/player/scripting.c b/player/scripting.c index d83c4d241f..0b200815f1 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -20,16 +20,18 @@ #include <sys/types.h> #include <dirent.h> #include <math.h> -#include <pthread.h> #include <assert.h> +#include <unistd.h> #include "config.h" #include "osdep/io.h" +#include "osdep/subprocess.h" #include "osdep/threads.h" #include "common/common.h" #include "common/msg.h" +#include "input/input.h" #include "options/m_config.h" #include "options/parse_configfile.h" #include "options/path.h" @@ -37,10 +39,13 @@ #include "core.h" #include "client.h" #include "libmpv/client.h" +#include "libmpv/render.h" +#include "libmpv/stream_cb.h" extern const struct mp_scripting mp_scripting_lua; extern const struct mp_scripting mp_scripting_cplugin; extern const struct mp_scripting mp_scripting_js; +extern const struct mp_scripting mp_scripting_run; static const struct mp_scripting *const scripting_backends[] = { #if HAVE_LUA @@ -52,6 +57,7 @@ static const struct mp_scripting *const scripting_backends[] = { #if HAVE_JAVASCRIPT &mp_scripting_js, #endif + &mp_scripting_run, NULL }; @@ -76,26 +82,29 @@ static char *script_name_from_filename(void *talloc_ctx, const char *fname) return talloc_asprintf(talloc_ctx, "%s", name); } -static void *script_thread(void *p) +static void run_script(struct mp_script_args *arg) { - pthread_detach(pthread_self()); - - struct mp_script_args *arg = p; - - char name[90]; - snprintf(name, sizeof(name), "%s (%s)", arg->backend->name, - mpv_client_name(arg->client)); - mpthread_set_name(name); + char *name = talloc_asprintf(NULL, "%s/%s", arg->backend->name, + mpv_client_name(arg->client)); + mp_thread_set_name(name); + talloc_free(name); if (arg->backend->load(arg) < 0) - MP_ERR(arg, "Could not load %s %s\n", arg->backend->name, arg->filename); + MP_ERR(arg, "Could not load %s script %s\n", arg->backend->name, arg->filename); mpv_destroy(arg->client); talloc_free(arg); - return NULL; } -static int mp_load_script(struct MPContext *mpctx, const char *fname) +static MP_THREAD_VOID script_thread(void *p) +{ + struct mp_script_args *arg = p; + run_script(arg); + + MP_THREAD_RETURN(); +} + +static int64_t mp_load_script(struct MPContext *mpctx, const char *fname) { char *ext = mp_splitext(fname, NULL); if (ext && strcasecmp(ext, "disable") == 0) @@ -164,32 +173,39 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) }; talloc_free(tmp); + fname = NULL; // might have been freed so don't touch anymore if (!arg->client) { - MP_ERR(mpctx, "Failed to create client for script: %s\n", fname); + MP_ERR(mpctx, "Failed to create client for script: %s\n", arg->filename); talloc_free(arg); return -1; } mp_client_set_weak(arg->client); arg->log = mp_client_get_log(arg->client); + int64_t id = mpv_client_id(arg->client); - MP_DBG(arg, "Loading %s %s...\n", backend->name, fname); + MP_DBG(arg, "Loading %s script %s...\n", backend->name, arg->filename); - pthread_t thread; - if (pthread_create(&thread, NULL, script_thread, arg)) { - mpv_destroy(arg->client); - talloc_free(arg); - return -1; + if (backend->no_thread) { + run_script(arg); + } else { + mp_thread thread; + if (mp_thread_create(&thread, script_thread, arg)) { + mpv_destroy(arg->client); + talloc_free(arg); + return -1; + } + mp_thread_detach(thread); } - return 0; + return id; } -int mp_load_user_script(struct MPContext *mpctx, const char *fname) +int64_t mp_load_user_script(struct MPContext *mpctx, const char *fname) { char *path = mp_get_user_path(NULL, mpctx->global, fname); - int ret = mp_load_script(mpctx, path); + int64_t ret = mp_load_script(mpctx, path); talloc_free(path); return ret; } @@ -224,33 +240,31 @@ static char **list_script_files(void *talloc_ctx, char *path) return files; } -static void load_builtin_script(struct MPContext *mpctx, bool enable, +static void load_builtin_script(struct MPContext *mpctx, int slot, 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)) { + assert(slot < MP_ARRAY_SIZE(mpctx->builtin_script_ids)); + int64_t *pid = &mpctx->builtin_script_ids[slot]; + if (*pid > 0 && !mp_client_id_exists(mpctx, *pid)) + *pid = 0; // died + if ((*pid > 0) != enable) { if (enable) { - mp_load_script(mpctx, fname); + *pid = mp_load_script(mpctx, fname); } else { - // Try to unload it by sending a shutdown event. This can be - // unreliable, because user scripts could have clashing names, or - // disabling and then quickly re-enabling a builtin script might - // detect the still-terminating script as loaded. + char *name = mp_tprintf(22, "@%"PRIi64, *pid); mp_client_send_event(mpctx, name, 0, MPV_EVENT_SHUTDOWN, NULL); } } - 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"); - load_builtin_script(mpctx, mpctx->opts->lua_load_stats, "@stats.lua"); - load_builtin_script(mpctx, mpctx->opts->lua_load_console, "@console.lua"); + load_builtin_script(mpctx, 0, mpctx->opts->lua_load_osc, "@osc.lua"); + load_builtin_script(mpctx, 1, mpctx->opts->lua_load_ytdl, "@ytdl_hook.lua"); + load_builtin_script(mpctx, 2, mpctx->opts->lua_load_stats, "@stats.lua"); + load_builtin_script(mpctx, 3, mpctx->opts->lua_load_console, "@console.lua"); + load_builtin_script(mpctx, 4, mpctx->opts->lua_load_auto_profiles, + "@auto_profiles.lua"); } bool mp_load_scripts(struct MPContext *mpctx) @@ -281,11 +295,83 @@ bool mp_load_scripts(struct MPContext *mpctx) #if HAVE_CPLUGINS +#if !HAVE_WIN32 #include <dlfcn.h> +#endif #define MPV_DLOPEN_FN "mpv_open_cplugin" typedef int (*mpv_open_cplugin)(mpv_handle *handle); +static void init_sym_table(struct mp_script_args *args, void *lib) { +#define INIT_SYM(name) \ + { \ + void **sym = (void **)dlsym(lib, "pfn_" #name); \ + if (sym) { \ + if (*sym && *sym != &name) \ + MP_ERR(args, "Overriding already set function " #name "\n"); \ + *sym = &name; \ + } \ + } + + INIT_SYM(mpv_client_api_version); + INIT_SYM(mpv_error_string); + INIT_SYM(mpv_free); + INIT_SYM(mpv_client_name); + INIT_SYM(mpv_client_id); + INIT_SYM(mpv_create); + INIT_SYM(mpv_initialize); + INIT_SYM(mpv_destroy); + INIT_SYM(mpv_terminate_destroy); + INIT_SYM(mpv_create_client); + INIT_SYM(mpv_create_weak_client); + INIT_SYM(mpv_load_config_file); + INIT_SYM(mpv_get_time_us); + INIT_SYM(mpv_free_node_contents); + INIT_SYM(mpv_set_option); + INIT_SYM(mpv_set_option_string); + INIT_SYM(mpv_command); + INIT_SYM(mpv_command_node); + INIT_SYM(mpv_command_ret); + INIT_SYM(mpv_command_string); + INIT_SYM(mpv_command_async); + INIT_SYM(mpv_command_node_async); + INIT_SYM(mpv_abort_async_command); + INIT_SYM(mpv_set_property); + INIT_SYM(mpv_set_property_string); + INIT_SYM(mpv_del_property); + INIT_SYM(mpv_set_property_async); + INIT_SYM(mpv_get_property); + INIT_SYM(mpv_get_property_string); + INIT_SYM(mpv_get_property_osd_string); + INIT_SYM(mpv_get_property_async); + INIT_SYM(mpv_observe_property); + INIT_SYM(mpv_unobserve_property); + INIT_SYM(mpv_event_name); + INIT_SYM(mpv_event_to_node); + INIT_SYM(mpv_request_event); + INIT_SYM(mpv_request_log_messages); + INIT_SYM(mpv_wait_event); + INIT_SYM(mpv_wakeup); + INIT_SYM(mpv_set_wakeup_callback); + INIT_SYM(mpv_wait_async_requests); + INIT_SYM(mpv_hook_add); + INIT_SYM(mpv_hook_continue); + INIT_SYM(mpv_get_wakeup_pipe); + + INIT_SYM(mpv_render_context_create); + INIT_SYM(mpv_render_context_set_parameter); + INIT_SYM(mpv_render_context_get_info); + INIT_SYM(mpv_render_context_set_update_callback); + INIT_SYM(mpv_render_context_update); + INIT_SYM(mpv_render_context_render); + INIT_SYM(mpv_render_context_report_swap); + INIT_SYM(mpv_render_context_free); + + INIT_SYM(mpv_stream_cb_add_ro); + +#undef INIT_SYM +} + static int load_cplugin(struct mp_script_args *args) { void *lib = dlopen(args->filename, RTLD_NOW | RTLD_LOCAL); @@ -296,6 +382,9 @@ static int load_cplugin(struct mp_script_args *args) mpv_open_cplugin sym = (mpv_open_cplugin)dlsym(lib, MPV_DLOPEN_FN); if (!sym) goto error; + + init_sym_table(args, lib); + return sym(args->client) ? -1 : 0; error: ; char *err = dlerror(); @@ -305,9 +394,69 @@ error: ; } const struct mp_scripting mp_scripting_cplugin = { - .name = "SO plugin", + .name = "cplugin", + #if HAVE_WIN32 + .file_ext = "dll", + #else .file_ext = "so", + #endif .load = load_cplugin, }; #endif + +static int load_run(struct mp_script_args *args) +{ + // The arg->client object might die and with it args->log, so duplicate it. + args->log = mp_log_new(args, args->log, NULL); + + int fds[2]; + if (!mp_ipc_start_anon_client(args->mpctx->ipc_ctx, args->client, fds)) + return -1; + args->client = NULL; // ownership lost + + char *fdopt = fds[1] >= 0 ? mp_tprintf(80, "--mpv-ipc-fd=%d:%d", fds[0], fds[1]) + : mp_tprintf(80, "--mpv-ipc-fd=%d", fds[0]); + + struct mp_subprocess_opts opts = { + .exe = (char *)args->filename, + .args = (char *[]){(char *)args->filename, fdopt, NULL}, + .fds = { + // Keep terminal stuff + {.fd = 0, .src_fd = 0,}, + {.fd = 1, .src_fd = 1,}, + {.fd = 2, .src_fd = 2,}, + // Just hope these don't step over each other (e.g. fds[1] could be + // below 4, if the std FDs are missing). + {.fd = fds[0], .src_fd = fds[0], }, + {.fd = fds[1], .src_fd = fds[1], }, + }, + .num_fds = fds[1] >= 0 ? 5 : 4, + .detach = true, + }; + struct mp_subprocess_result res; + mp_subprocess2(&opts, &res); + + // Closing these will (probably) make the client exit, if it really died. + // They _should_ be CLOEXEC, but are not, because + // posix_spawn_file_actions_adddup2() may not clear the CLOEXEC flag + // properly if by coincidence fd==src_fd. + close(fds[0]); + if (fds[1] >= 0) + close(fds[1]); + + if (res.error < 0) { + MP_ERR(args, "Starting '%s' failed: %s\n", args->filename, + mp_subprocess_err_str(res.error)); + return -1; + } + + return 0; +} + +const struct mp_scripting mp_scripting_run = { + .name = "ipc", + .file_ext = "run", + .no_thread = true, + .load = load_run, +}; |