diff options
author | wm4 <wm4@nowhere> | 2014-10-15 23:09:53 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-10-16 01:00:22 +0200 |
commit | 8e4fa5fcd15ecba6a046aa71650bbfc759856fa8 (patch) | |
tree | 5ee966a04f590cc2b1ac482c420f7c704faa17ec /player/loadfile.c | |
parent | bc0ed904811b4e4a0fc3d6129ba92e2c786cbabf (diff) | |
download | mpv-8e4fa5fcd15ecba6a046aa71650bbfc759856fa8.tar.bz2 mpv-8e4fa5fcd15ecba6a046aa71650bbfc759856fa8.tar.xz |
command: add a mechanism to allow scripts to intercept file loads
A vague idea to get something similar what libquvi did.
Undocumented because it might change a lot, or even be removed. To give
an idea what it does, a Lua script could do the following:
-- type ID priority
mp.commandv("hook_add", "on_load", 0, 0)
mp.register_script_message("hook_run", function(param, param2)
-- param is "0", the user-chosen ID from the hook_add command
-- param2 is the magic value that has to be passed to finish
-- the hook
mp.resume_all()
-- do something, maybe set options that are reset on end:
mp.set_property("file-local-options/name", "value")
-- or change the URL that's being opened:
local url = mp.get_property("stream-open-filename")
mp.set_property("stream-open-filename", url .. ".png")
-- let the player (or the next script) continue
mp.commandv("hook_ack", param2)
end)
Diffstat (limited to 'player/loadfile.c')
-rw-r--r-- | player/loadfile.c | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/player/loadfile.c b/player/loadfile.c index 306e340127..6d6de1676b 100644 --- a/player/loadfile.c +++ b/player/loadfile.c @@ -794,6 +794,36 @@ static void transfer_playlist(struct MPContext *mpctx, struct playlist *pl) } } +static int process_open_hooks(struct MPContext *mpctx) +{ + + mp_hook_run(mpctx, NULL, "on_load"); + + while (!mp_hook_test_completion(mpctx, "on_load")) { + mp_idle(mpctx); + if (mpctx->stop_play) { + if (mpctx->stop_play == PT_QUIT) + return -1; + // Can't exit immediately, the script would interfere with the + // next file being loaded. + mp_hook_abort(mpctx, "on_load"); + } + } + + // quvi stuff + char *filename = mpctx->stream_open_filename; + mpctx->resolve_result = resolve_url(filename, mpctx->global); + if (mpctx->resolve_result) { + print_resolve_contents(mpctx->log, mpctx->resolve_result); + if (mpctx->resolve_result->playlist) { + transfer_playlist(mpctx, mpctx->resolve_result->playlist); + return 1; + } + mpctx->stream_open_filename = mpctx->resolve_result->url; + } + return 0; +} + static void print_timeline(struct MPContext *mpctx) { if (mpctx->timeline) { @@ -936,6 +966,7 @@ static void play_current_file(struct MPContext *mpctx) mpctx->playing->reserved += 1; mpctx->filename = talloc_strdup(tmp, mpctx->playing->filename); + mpctx->stream_open_filename = mpctx->filename; mpctx->add_osd_seek_info &= OSD_SEEK_INFO_EDITION; @@ -972,21 +1003,16 @@ static void play_current_file(struct MPContext *mpctx) assert(mpctx->d_sub[0] == NULL); assert(mpctx->d_sub[1] == NULL); - char *stream_filename = mpctx->filename; - mpctx->resolve_result = resolve_url(stream_filename, mpctx->global); - if (mpctx->resolve_result) { - talloc_steal(tmp, mpctx->resolve_result); - print_resolve_contents(mpctx->log, mpctx->resolve_result); - if (mpctx->resolve_result->playlist) { - transfer_playlist(mpctx, mpctx->resolve_result->playlist); - goto terminate_playback; - } - stream_filename = mpctx->resolve_result->url; - } + int hooks_res = process_open_hooks(mpctx); + talloc_steal(tmp, mpctx->resolve_result); + if (hooks_res) + goto terminate_playback; // quit or preloaded playlist special-case + int stream_flags = STREAM_READ; if (!opts->load_unsafe_playlists) stream_flags |= mpctx->playing->stream_flags; - mpctx->stream = open_stream_async(mpctx, stream_filename, stream_flags); + mpctx->stream = open_stream_async(mpctx, mpctx->stream_open_filename, + stream_flags); if (!mpctx->stream) goto terminate_playback; @@ -1245,6 +1271,7 @@ terminate_playback: playlist_entry_unref(mpctx->playing); mpctx->playing = NULL; mpctx->filename = NULL; + mpctx->stream_open_filename = NULL; talloc_free(tmp); } |