From 53ee1ae4171a0ddfb5c9a965c404a18d93e82952 Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 5 Aug 2020 23:28:24 +0200 Subject: auto_profiles: register hooks for more synchronous profile application The property observation mechanism is fairly asynchronous to the player core, and Lua scripts also are (they run in a separate thread). This may sometimes lead to profiles being applied when it's too load. For example, you may want to change network options depending on the input URL - but most of these options would have to be set before the HTTP access is made. But it could happen that the profile, and thus the option, was applied at an slightly but arbitrary later time. This is generally not fixable. But for the most important use-cases, such as applying changes before media opening or playback initialization, we can use some of the defined hooks. Hooks make it synchronous again, by allowing API users (such as scripts) to block the core because it continues with loading. For this we simply don't continue a given hook, until we receive an idle event, and have applied all changes. The idle event is in general used to wait for property change notifications to settle down. Some of this relies on the subtle ways guarantees are (maybe) given. See commit ba70b150fbe for the messy details. I'm not quite sure whether it actually works, because I can't be bothered to read and understand my bullshit from half a year ago. Should provide at least some improvement, though. --- player/lua/auto_profiles.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/player/lua/auto_profiles.lua b/player/lua/auto_profiles.lua index aebef28688..33271ec8c8 100644 --- a/player/lua/auto_profiles.lua +++ b/player/lua/auto_profiles.lua @@ -8,6 +8,7 @@ local watched_properties = {} -- indexed by property name (used as a set) local cached_properties = {} -- property name -> last known raw value local properties_to_profiles = {} -- property name -> set of profiles using it local have_dirty_profiles = false -- at least one profile is marked dirty +local pending_hooks = {} -- as set (keys only, meaningless values) -- Used during evaluation of the profile condition, and should contain the -- profile the condition is evaluated for. @@ -61,6 +62,20 @@ local function on_idle() end end have_dirty_profiles = false + -- Release all hooks (the point was to wait until an idle event) + while true do + local h = next(pending_hooks) + if not h then + break + end + pending_hooks[h] = nil + h:cont() + end +end + +local function on_hook(h) + h:defer() + pending_hooks[h] = true end function get(name, default) @@ -155,4 +170,8 @@ if #profiles < 1 and mp.get_property("load-auto-profiles") == "auto" then end mp.register_idle(on_idle) +for _, name in ipairs({"on_load", "on_preloaded", "on_before_start_file"}) do + mp.add_hook(name, 50, on_hook) +end + on_idle() -- re-evaluate all profiles immediately -- cgit v1.2.3