Skip to content

Commit b6dd130

Browse files
committed
osc.lua: always observe duration
Currently the duration observer is toggled in multiple places to not call request_init unecessarily. It is much simpler to always observe it and call request_init conditionally in the callback. This also allows reusing the cached value throughout the OSC. In seekRangesF, checking duration <= 0 was unnecessary because mp_property_duration returns either unavailable or > 0.
1 parent 5921fe5 commit b6dd130

1 file changed

Lines changed: 17 additions & 40 deletions

File tree

player/lua/osc.lua

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ local state = {
314314
no_video = false,
315315
playlist_count = 0,
316316
playlist_pos_1 = 0,
317+
duration = 0,
317318
pause = false,
318319
volume = 0,
319320
mute = false,
@@ -948,9 +949,8 @@ local function render_elements(master_ass)
948949
state.forced_title = nil
949950
local se, ae = state.slider_element, elements[state.active_element]
950951
if user_opts.chapter_fmt ~= "no" and se and (ae == se or (not ae and mouse_hit(se))) then
951-
local dur = mp.get_property_number("duration", 0)
952-
if dur > 0 then
953-
local possec = get_slider_value(se) * dur / 100 -- of mouse pos
952+
if state.duration then
953+
local possec = get_slider_value(se) * state.duration / 100 -- of mouse pos
954954
local ch = get_chapter(possec)
955955
if ch and ch.title and ch.title ~= "" then
956956
state.forced_title = string.format(user_opts.chapter_fmt, ch.title)
@@ -1192,7 +1192,7 @@ local function render_elements(master_ass)
11921192
ass_append_alpha(elem_ass, slider_lo.alpha, 0)
11931193
elem_ass:append(tooltiplabel)
11941194

1195-
local hover_sec = mp.get_property_number("duration", 0) * (sliderpos / 100)
1195+
local hover_sec = (state.duration or 0) * (sliderpos / 100)
11961196
mp.set_property_number("user-data/osc/hover-sec", hover_sec)
11971197

11981198
-- thumbnail
@@ -2486,11 +2486,10 @@ local function osc_init()
24862486
and user_opts.layout ~= "slimtopbar"
24872487
state.slider_element = ne.enabled and ne or nil -- used for forced_title
24882488
ne.slider.markerF = function ()
2489-
local duration = mp.get_property_number("duration")
2490-
if duration ~= nil then
2489+
if state.duration then
24912490
local markers = {}
24922491
for n = 1, #state.chapter_list do
2493-
markers[n] = (state.chapter_list[n].time / duration * 100)
2492+
markers[n] = (state.chapter_list[n].time / state.duration * 100)
24942493
end
24952494
return markers
24962495
else
@@ -2500,9 +2499,8 @@ local function osc_init()
25002499
ne.slider.posF =
25012500
function () return mp.get_property_number("percent-pos") end
25022501
ne.slider.tooltipF = function (pos)
2503-
local duration = mp.get_property_number("duration")
2504-
if duration ~= nil and pos ~= nil then
2505-
local possec = duration * (pos / 100)
2502+
if state.duration and pos then
2503+
local possec = state.duration * (pos / 100)
25062504
return mp.format_time(possec)
25072505
else
25082506
return ""
@@ -2512,15 +2510,14 @@ local function osc_init()
25122510
if user_opts.seekrangestyle == "none" or not cache_enabled() then
25132511
return nil
25142512
end
2515-
local duration = mp.get_property_number("duration")
2516-
if duration == nil or duration <= 0 then
2513+
if state.duration == nil then
25172514
return nil
25182515
end
25192516
local nranges = {}
25202517
for _, range in pairs(state.demuxer_cache_state["seekable-ranges"]) do
25212518
nranges[#nranges + 1] = {
2522-
["start"] = 100 * range["start"] / duration,
2523-
["end"] = 100 * range["end"] / duration,
2519+
["start"] = 100 * range["start"] / state.duration,
2520+
["end"] = 100 * range["end"] / state.duration,
25242521
}
25252522
end
25262523
return nranges
@@ -2598,7 +2595,7 @@ local function osc_init()
25982595
-- tc_right (total/remaining time)
25992596
ne = new_element("tc_right", "button")
26002597

2601-
ne.visible = (mp.get_property_number("duration", 0) > 0)
2598+
ne.visible = state.duration ~= nil
26022599
ne.content = function ()
26032600
if state.rightTC_trem then
26042601
local minus = user_opts.unicodeminus and UNICODE_MINUS or "-"
@@ -3150,28 +3147,6 @@ local function shutdown()
31503147
mp.del_property("user-data/osc")
31513148
end
31523149

3153-
-- duration is observed for the sole purpose of updating chapter markers
3154-
-- positions. live streams with chapters are very rare, and the update is also
3155-
-- expensive (with request_init), so it's only observed when we have chapters
3156-
-- and the user didn't disable the livemarkers option (update_duration_watch).
3157-
local function on_duration() request_init() end
3158-
3159-
local duration_watched = false
3160-
local function update_duration_watch()
3161-
local want_watch = user_opts.livemarkers and
3162-
(mp.get_property_number("chapters", 0) or 0) > 0 and
3163-
true or false -- ensure it's a boolean
3164-
3165-
if want_watch ~= duration_watched then
3166-
if want_watch then
3167-
mp.observe_property("duration", "native", on_duration)
3168-
else
3169-
mp.unobserve_property(on_duration)
3170-
end
3171-
duration_watched = want_watch
3172-
end
3173-
end
3174-
31753150
local function set_tick_delay(_, display_fps)
31763151
-- may be nil if unavailable or 0 fps is reported
31773152
if not display_fps or not user_opts.tick_delay_follow_display_fps then
@@ -3188,9 +3163,13 @@ observe_cached("playlist-count", request_init)
31883163
observe_cached("playlist-pos-1", request_init)
31893164
observe_cached("chapter-list", function ()
31903165
table.sort(state.chapter_list, function(a, b) return a.time < b.time end)
3191-
update_duration_watch()
31923166
request_init()
31933167
end)
3168+
observe_cached("duration", function ()
3169+
if user_opts.livemarkers and state.chapter_list[1] then
3170+
request_init()
3171+
end
3172+
end)
31943173

31953174
-- These are for backwards compatibility only.
31963175
mp.register_script_message("osc-message", function(message, dur)
@@ -3467,7 +3446,6 @@ opt.read_options(user_opts, "osc", function(changed)
34673446
end
34683447
request_tick()
34693448
visibility_mode(user_opts.visibility, true)
3470-
update_duration_watch()
34713449
request_init()
34723450
end)
34733451

@@ -3477,7 +3455,6 @@ set_osc_styles()
34773455
set_time_styles(true, true)
34783456
set_tick_delay()
34793457
visibility_mode(user_opts.visibility, true)
3480-
update_duration_watch()
34813458

34823459
set_virt_mouse_area(0, 0, 0, 0, "input")
34833460
set_virt_mouse_area(0, 0, 0, 0, "window-controls")

0 commit comments

Comments
 (0)