//! One timeline over one or more files: the decode workers, the audio output //! and the master clock wired together so a front-end only has to render and //! call [`tick`]. //! //! [`tick`]: PlaybackSession::tick //! //! Everything degrades to silent-but-correct: no audio track, no plugin, no //! PipeWire daemon and an unusable format all end up in [`ClockSource::Wall`], //! where the video is paced by real time instead of by the device. //! //! 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::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::mpsc::{Receiver, TryRecvError}; use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; use crate::ao::AoSession; use crate::audio::{AudioChunk, AudioSession}; use crate::clock::{ClockSource, PlaybackClock}; use crate::color::ColorParams; use crate::decode::{DecodeSession, Frame, Worker}; use crate::demux::{Demuxer, VideoMeta}; use crate::eq::EqParams; use crate::project::{Clip, Lane, LaneKind, Project, Source, Span}; use crate::scale::{Composer, FitPolicy}; /// How long the feeder waits out a full ring. The ring holds a second, so this /// only has to be short next to that; it costs one wakeup per 10 ms of audio. 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 /// itself (atomics and a channel), but the lock is what makes it sayable in /// Rust and it is never held for more than a memcpy. ao: Arc>, sample_rate: u32, channels: u64, /// Interleaved samples accepted by the device so far. fed: Arc, /// The decoder ran out and `fed` is final. fed_all: Arc, /// The output plugin failed mid-stream. Its position is frozen from here on, /// 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 { /// Whether the device has played everything there will ever be, i.e. the /// audio clock is about to stop meaning anything. fn played_out(&self, position: i64) -> bool { 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 { /// The *timeline's* parameters, not any one file's: its frame rate and codec /// come from source 0 and are what every other source is held to, while /// `width`/`height` are the **project resolution** -- source 0's picture to /// begin with, and whatever [`PlaybackSession::set_resolution`] or a saved /// `resolution` line makes it after that. Every clip is composed onto it, so /// a file of another size is a placed picture rather than a refusal. meta: VideoMeta, /// Source 0's own picture, kept from the open: the project resolution /// starts here, and a caller offering sizes to pick from needs the media's /// own among them or a project moved off it could never come back. native: (u32, u32), frames: Receiver, /// The current decode worker. worker: Worker, /// Workers cancelled by a seek or a clip change, waiting to be reaped. /// Joining one inline costs whatever VA-API init it is still inside (98 ms /// measured worst case), which is a visible hitch on the caller's thread; /// parking it here instead makes that free, and [`retire`] drops the ones /// that have already returned every time it adds another. /// /// Never a leak, and that is the invariant: everything in here is joined /// when the session drops (`Vec`'s own drop does it, one /// [`Worker::drop`] each), so no decode worker can outlive the session and /// meet Mesa's `atexit` handlers from inside libva. /// /// [`retire`]: PlaybackSession::retire retired: Vec, clock: PlaybackClock, audio: Option