Skip to content

Commit f510e24

Browse files
author
blank_name
committed
Add an id to the Thumbnailer.state table so we can ignore messages from workers that are for previous state versions (we may get messages from workers for the previous video after the video has changed).
1 parent 967a572 commit f510e24

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/thumbnailer_server.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function do_worker_job(state_json_string, frames_json_string)
129129
msg.error("Failed to parse state JSON")
130130
return
131131
end
132+
local state_id = thumb_state.id
132133

133134
local thumbnail_indexes, err = utils.parse_json(frames_json_string)
134135
if err then
@@ -169,7 +170,7 @@ function do_worker_job(state_json_string, frames_json_string)
169170
-- Grab the "middle" of the thumbnail duration instead of the very start, and leave some margin in the end
170171
local timestamp = math.min(file_duration - 0.25, (thumb_idx + 0.5) * thumb_state.thumbnail_delta)
171172

172-
mp.commandv("script-message", "mpv_thumbnail_script-progress", tostring(thumbnail_index))
173+
mp.commandv("script-message", "mpv_thumbnail_script-progress", tostring(state_id), tostring(thumbnail_index))
173174

174175
-- The expected size (raw BGRA image)
175176
local thumbnail_raw_size = (thumb_state.thumbnail_size.w * thumb_state.thumbnail_size.h * 4)
@@ -234,7 +235,7 @@ function do_worker_job(state_json_string, frames_json_string)
234235
end
235236

236237
msg.debug("Finished work on thumbnail", thumb_idx)
237-
mp.commandv("script-message", "mpv_thumbnail_script-ready", tostring(thumbnail_index), thumbnail_path)
238+
mp.commandv("script-message", "mpv_thumbnail_script-ready", tostring(state_id), tostring(thumbnail_index), thumbnail_path)
238239
end
239240

240241
msg.debug(("Generating %d thumbnails @ %dx%d for %q"):format(

src/thumbnailer_shared.lua

+21-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ local Thumbnailer = {
22
cache_directory = thumbnailer_options.cache_directory,
33

44
state = {
5+
-- Used to make sure updates sent to us by workers correspond to the
6+
-- current state (the video hasn't changed)
7+
id = 0,
58
ready = false,
69
available = false,
710
enabled = false,
@@ -33,7 +36,10 @@ local Thumbnailer = {
3336
}
3437

3538
function Thumbnailer:clear_state()
39+
local prev_state_id = self.state.id
40+
3641
clear_table(self.state)
42+
self.state.id = prev_state_id + 1
3743
self.state.ready = false
3844
self.state.available = false
3945
self.state.finished_thumbnails = 0
@@ -46,7 +52,11 @@ function Thumbnailer:on_file_loaded()
4652
self:clear_state()
4753
end
4854

49-
function Thumbnailer:on_thumb_ready(index)
55+
function Thumbnailer:on_thumb_ready(state_id, index)
56+
if self.state.id ~= state_id then
57+
return
58+
end
59+
5060
self.state.thumbnails[index] = 1
5161

5262
-- Full recount instead of a naive increment (let's be safe!)
@@ -58,7 +68,11 @@ function Thumbnailer:on_thumb_ready(index)
5868
end
5969
end
6070

61-
function Thumbnailer:on_thumb_progress(index)
71+
function Thumbnailer:on_thumb_progress(state_id, index)
72+
if self.state.id ~= state_id then
73+
return
74+
end
75+
6276
self.state.thumbnails[index] = math.max(self.state.thumbnails[index], 0)
6377
end
6478

@@ -259,11 +273,11 @@ end
259273
function Thumbnailer:register_client()
260274
self.worker_register_timeout = mp.get_time() + 2
261275

262-
mp.register_script_message("mpv_thumbnail_script-ready", function(index, path)
263-
self:on_thumb_ready(tonumber(index), path)
276+
mp.register_script_message("mpv_thumbnail_script-ready", function(state_id, index, path)
277+
self:on_thumb_ready(tonumber(state_id), tonumber(index), path)
264278
end)
265-
mp.register_script_message("mpv_thumbnail_script-progress", function(index, path)
266-
self:on_thumb_progress(tonumber(index), path)
279+
mp.register_script_message("mpv_thumbnail_script-progress", function(state_id, index, path)
280+
self:on_thumb_progress(tonumber(state_id), tonumber(index), path)
267281
end)
268282

269283
mp.register_script_message("mpv_thumbnail_script-worker", function(worker_name)
@@ -371,7 +385,7 @@ function Thumbnailer:start_worker_jobs()
371385
return
372386
end
373387

374-
local worker_list = {}
388+
local worker_list = { state_id = self.state.id }
375389
for worker_name in pairs(self.workers) do table.insert(worker_list, worker_name) end
376390

377391
local worker_count = #worker_list

0 commit comments

Comments
 (0)