From e4ca3bd2b6f19a4ffe170a2766d0d952d2196999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 9 Aug 2026 13:42:11 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(playback):=20seek=20=E2=80=94?= =?UTF-8?q?=20rebuild=20workers,=20epoch-guarded=20feeder,=20clock=20re-an?= =?UTF-8?q?chor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PlaybackSession::seek(secs) replaces both decode workers instead of steering them: a fresh channel cannot hold a stale frame, and it works after EOF where the old workers have already exited. The audio device stays open across a seek. An epoch counter, bumped and flushed while holding the device lock and re-checked by the feeder inside that same lock right before every write, is what stops a superseded feeder from landing stale PCM behind the flush. `fed` is re-based on the device position, which keeps counting across a flush, so the audio-EOF test stays exact. --- crates/engine/src/playback.rs | 149 +++++++++++++++++++++----- crates/engine/tests/playback.rs | 178 ++++++++++++++++++++++++++++++++ 2 files changed, 301 insertions(+), 26 deletions(-) diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 9e2a10d..12d11e3 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -10,7 +10,7 @@ //! Drift policy stays with the caller -- this type answers "what time is it", //! the renderer decides which frame that means. -use std::path::Path; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::mpsc::Receiver; use std::sync::{Arc, Mutex}; @@ -28,6 +28,8 @@ use crate::demux::VideoMeta; const RING_FULL_WAIT: Duration = Duration::from_millis(10); /// The output half of a session: the device, plus what the feeder has handed it. +/// Cloned into the feeder thread; every field that matters is shared. +#[derive(Clone)] struct Audio { /// Shared because `write` runs on the feeder thread while `position` and /// `set_active` are called from the caller's. The plugin serialises those @@ -44,6 +46,9 @@ struct Audio { /// so it can never satisfy [`Audio::played_out`] -- the flag is what lets /// [`PlaybackSession::tick`] hand the timeline to wall time instead. died: Arc, + /// Bumped by every seek. A feeder only owns the device while this still + /// matches the value it started with; see [`feed`]. + epoch: Arc, } impl Audio { @@ -53,12 +58,35 @@ impl Audio { self.fed_all.load(Ordering::Acquire) && position as u64 * self.channels >= self.fed.load(Ordering::Relaxed) } + + /// Starts a feeder for the current epoch, draining `rx` into the device. + /// `false` if the thread would not start, i.e. nothing will ever be fed. + fn spawn_feeder(&self, rx: Receiver) -> bool { + let me = self.clone(); + let epoch = self.epoch.load(Ordering::Acquire); + thread::Builder::new() + .name("audio-feed".into()) + .spawn(move || { + feed(rx, &me, epoch); + // Only the current feeder gets to declare the end of the audio: + // a superseded one is finished, the stream is not. + if me.epoch.load(Ordering::Acquire) == epoch { + me.fed_all.store(true, Ordering::Release); + } + }) + .is_ok() + } } /// A file opened for playback. Starts paused at t=0; call [`PlaybackSession::play`]. pub struct PlaybackSession { + /// Kept for [`PlaybackSession::seek`], which reopens both workers. + path: PathBuf, meta: VideoMeta, frames: Receiver, + /// Stops the current decode worker, so an abandoned one cannot outlive a + /// seek by more than one access unit. + cancel: Arc, clock: PlaybackClock, audio: Option