From be5e46222ba4adccc5a557375beea29d23969a81 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 10 May 2018 15:26:56 +0200 Subject: TOOLS: add a stupid test program for Lua async commands --- TOOLS/lua/command-test.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 TOOLS/lua/command-test.lua (limited to 'TOOLS') diff --git a/TOOLS/lua/command-test.lua b/TOOLS/lua/command-test.lua new file mode 100644 index 0000000000..5fb8c7b0d7 --- /dev/null +++ b/TOOLS/lua/command-test.lua @@ -0,0 +1,53 @@ +-- Test script for some command API details. + +local utils = require("mp.utils") + +function join(sep, arr, count) + local r = "" + if count == nil then + count = #arr + end + for i = 1, count do + if i > 1 then + r = r .. sep + end + r = r .. utils.to_string(arr[i]) + end + return r +end + +mp.observe_property("vo-configured", "bool", function(_, v) + if v ~= true then + return + end + + print("async expand-text") + mp.command_native_async({"expand-text", "hello ${path}!"}, + function(res, val, err) + print("done async expand-text: " .. join(" ", {res, val, err})) + end) + + -- make screenshot writing very slow + mp.set_property("screenshot-format", "png") + mp.set_property("screenshot-png-compression", "9") + + timer = mp.add_periodic_timer(0.1, function() print("I'm alive") end) + timer:resume() + + print("Slow screenshot command...") + res, err = mp.command_native({"screenshot"}) + print("done, res: " .. utils.to_string(res)) + + print("Slow screenshot async command...") + res, err = mp.command_native_async({"screenshot"}, function(res) + print("done (async), res: " .. utils.to_string(res)) + timer:kill() + end) + print("done (sending), res: " .. utils.to_string(res)) + + print("Broken screenshot async command...") + mp.command_native_async({"screenshot-to-file", "/nonexistent/bogus.png"}, + function(res, val, err) + print("done err scr.: " .. join(" ", {res, val, err})) + end) +end) -- cgit v1.2.3 From d9bc97bda6e4750af2fbbfcb51ddb6b2c04c277b Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 12 May 2018 15:14:07 +0200 Subject: command: add a subprocess command This supports named arguments. It benefits from the infrastructure of async commands. The plan is to reimplement Lua's utils.subprocess() on top of it. --- TOOLS/lua/command-test.lua | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'TOOLS') diff --git a/TOOLS/lua/command-test.lua b/TOOLS/lua/command-test.lua index 5fb8c7b0d7..94a91c2f57 100644 --- a/TOOLS/lua/command-test.lua +++ b/TOOLS/lua/command-test.lua @@ -50,4 +50,9 @@ mp.observe_property("vo-configured", "bool", function(_, v) function(res, val, err) print("done err scr.: " .. join(" ", {res, val, err})) end) + + mp.command_native_async({name = "subprocess", args = {"sh", "-c", "echo hi && sleep 10s"}, capture_stdout = true}, + function(res, val, err) + print("done subprocess: " .. join(" ", {res, val, err})) + end) end) -- cgit v1.2.3 From dbe831bd025d34930b97c493d9ef61278408cf46 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 12 May 2018 18:48:35 +0200 Subject: lua: expose mpv_abort_async_command() Also somewhat cleans up mp.command_native_async() error handling. --- TOOLS/lua/command-test.lua | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'TOOLS') diff --git a/TOOLS/lua/command-test.lua b/TOOLS/lua/command-test.lua index 94a91c2f57..1a8d86b4dd 100644 --- a/TOOLS/lua/command-test.lua +++ b/TOOLS/lua/command-test.lua @@ -55,4 +55,13 @@ mp.observe_property("vo-configured", "bool", function(_, v) function(res, val, err) print("done subprocess: " .. join(" ", {res, val, err})) end) + + local x = mp.command_native_async({name = "subprocess", args = {"sleep", "inf"}}, + function(res, val, err) + print("done sleep inf subprocess: " .. join(" ", {res, val, err})) + end) + mp.add_timeout(15, function() + print("aborting sleep inf subprocess after timeout") + mp.abort_async_command(x) + end) end) -- cgit v1.2.3 From 7428cc51496ca8e56600fdc4034b8f55720f09f9 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 13 May 2018 13:48:47 +0200 Subject: client API: kill async commands on termination This affects async commands started by client API, commands with async capability run in a sync way by client API (think mpv_command_node() with "subprocess"), and detached async work. Since scripts might want to do some cleanup work (that might involve launching processes, don't ask), we don't unconditionally kill everything on exit, but apply an arbitrary timeout of 2 seconds until async commands are aborted. --- TOOLS/lua/command-test.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'TOOLS') diff --git a/TOOLS/lua/command-test.lua b/TOOLS/lua/command-test.lua index 1a8d86b4dd..79c1127cbf 100644 --- a/TOOLS/lua/command-test.lua +++ b/TOOLS/lua/command-test.lua @@ -64,4 +64,20 @@ mp.observe_property("vo-configured", "bool", function(_, v) print("aborting sleep inf subprocess after timeout") mp.abort_async_command(x) end) + + -- This should get killed on script exit. + mp.command_native_async({name = "subprocess", playback_only = false, + args = {"sleep", "inf"}}, function()end) + + -- Runs detached; should be killed on player exit (forces timeout) + mp.command_native({_flags={"async"}, name = "subprocess", + playback_only = false, args = {"sleep", "inf"}}) +end) + +mp.register_event("shutdown", function() + -- This "freezes" the script, should be killed via timeout. + print("freeze!") + local x = mp.command_native({name = "subprocess", playback_only = false, + args = {"sleep", "inf"}}) + print("done, killed=" .. utils.to_string(x.killed_by_us)) end) -- cgit v1.2.3 From 12d1404b04e90f5357882e5c1048d92305248cb9 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 18 May 2018 21:38:17 +0200 Subject: player: make various commands for managing external tracks abortable Until now, they could be aborted only by ending playback, and calling mpv_abort_async_command didn't do anything. This requires furthering the mess how playback abort is done. The main reason why mp_cancel exists at all is to avoid that a "frozen" demuxer (blocked on network I/O or whatever) cannot freeze the core. The core should always get its way. Previously, there was a single mp_cancel handle, that could be signaled, and all demuxers would unfreeze. With external files, we might want to abort loading of a certain external file, which automatically means they need a separate mp_cancel. So give every demuxer its own mp_cancel, and "slave" it to whatever parent mp_cancel handles aborting. Since the mpv demuxer API conflates creating the demuxer and reading the file headers, mp_cancel strictly need to be created before the demuxer is created (or we couldn't abort loading). Although we give every demuxer its own mp_cancel (as "enforced" by cancel_and_free_demuxer), it's still rather messy to create/destroy it along with the demuxer. --- TOOLS/lua/command-test.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'TOOLS') diff --git a/TOOLS/lua/command-test.lua b/TOOLS/lua/command-test.lua index 79c1127cbf..dd384818cc 100644 --- a/TOOLS/lua/command-test.lua +++ b/TOOLS/lua/command-test.lua @@ -65,6 +65,18 @@ mp.observe_property("vo-configured", "bool", function(_, v) mp.abort_async_command(x) end) + -- (assuming this "freezes") + local y = mp.command_native_async({name = "sub-add", url = "-"}, + function(res, val, err) + print("done sub-add stdin: " .. join(" ", {res, val, err})) + end) + mp.add_timeout(20, function() + print("aborting sub-add stdin after timeout") + mp.abort_async_command(y) + end) + + + -- This should get killed on script exit. mp.command_native_async({name = "subprocess", playback_only = false, args = {"sleep", "inf"}}, function()end) -- cgit v1.2.3