Skip to content

Commit

Permalink
refactor: use demuxer-cache-state/cache-duration property (tomasklaen…
Browse files Browse the repository at this point in the history
…#889)

It's used by both osc.lua and stats.lua and gives a more accurate
estimation of how much media is cached.
  • Loading branch information
christoph-heinrich authored Apr 23, 2024
1 parent e6a5fd9 commit 00737e1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/uosc/elements/Timeline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,11 @@ function Timeline:render()
ass:rect(fax, fay, fbx, fby, {opacity = config.opacity.position})

-- Uncached ranges
local buffered_playtime = nil
if state.uncached_ranges then
local opts = {size = 80, anchor_y = fby}
local texture_char = visibility > 0 and 'b' or 'a'
local offset = opts.size / (visibility > 0 and 24 or 28)
for _, range in ipairs(state.uncached_ranges) do
if not buffered_playtime and (range[1] > state.time or range[2] > state.time) then
buffered_playtime = (range[1] - state.time) / state.speed
end
if options.timeline_cache then
local ax = range[1] < 0.5 and bax or math.floor(t2x(range[1]))
local bx = range[2] > state.duration - 0.5 and bbx or math.ceil(t2x(range[2]))
Expand Down Expand Up @@ -377,14 +373,14 @@ function Timeline:render()
if text_opacity > 0 then
local time_opts = {size = self.font_size, opacity = text_opacity, border = 2 * state.scale}
-- Upcoming cache time
if buffered_playtime and options.buffered_time_threshold > 0
and buffered_playtime < options.buffered_time_threshold then
if state.cache_duration and options.buffered_time_threshold > 0
and state.cache_duration < options.buffered_time_threshold then
local margin = 5 * state.scale
local x, align = fbx + margin, 4
local cache_opts = {
size = self.font_size * 0.8, opacity = text_opacity * 0.6, border = options.text_border * state.scale,
}
local human = round(math.max(buffered_playtime, 0)) .. 's'
local human = round(state.cache_duration) .. 's'
local width = text_width(human, cache_opts)
local time_width = timestamp_width(state.time_human, time_opts)
local time_width_end = timestamp_width(state.destination_time_human, time_opts)
Expand Down
3 changes: 3 additions & 0 deletions src/uosc/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ state = {
cache = nil,
cache_buffering = 100,
cache_underrun = false,
cache_duration = nil,
core_idle = false,
eof_reached = false,
render_delay = config.render_delay,
Expand Down Expand Up @@ -766,13 +767,15 @@ mp.observe_property('demuxer-cache-state', 'native', function(prop, cache_state)
if cache_state then
cached_ranges, bof, eof = cache_state['seekable-ranges'], cache_state['bof-cached'], cache_state['eof-cached']
set_state('cache_underrun', cache_state['underrun'])
set_state('cache_duration', not cache_state.eof and cache_state['cache-duration'] or nil)
else
cached_ranges = {}
end

if not (state.duration and (#cached_ranges > 0 or state.cache == 'yes' or
(state.cache == 'auto' and state.is_stream))) then
if state.uncached_ranges then set_state('uncached_ranges', nil) end
set_state('cache_duration', nil)
return
end

Expand Down

0 comments on commit 00737e1

Please sign in to comment.