Skip to main content

PlaybackClock

Struct PlaybackClock 

Source
pub struct PlaybackClock { /* private fields */ }
Expand description

A monotonic clock that tracks elapsed playback time.

The clock supports start, stop, pause, resume, and playback-rate scaling. It is used by PreviewPlayer internally to drive frame presentation timing and A/V synchronisation. Callers may also query it directly.

PlaybackClock is a value type — it is not Arc<Mutex<...>> internally. When multi-thread access is required, wrap it in a Mutex.

§Usage

let mut clock = PlaybackClock::new();
clock.start();
let pts = clock.current_pts();
clock.pause();
// current_pts() is now frozen
clock.resume();
// current_pts() continues advancing from the frozen point
clock.set_rate(2.0);          // fast-forward at 2×
clock.set_position(Duration::from_secs(30)); // seek to 30 s

Implementations§

Source§

impl PlaybackClock

Source

pub fn new() -> Self

Create a new clock in the Stopped state with a rate of 1.0.

Source

pub fn start(&mut self)

Start the clock from the current position.

  • If the clock is Stopped, it starts from the position last set by set_position, or Duration::ZERO if no seek has been performed.
  • If the clock is Paused, it starts from the frozen position.
  • If the clock is already Running, this is a no-op.
Source

pub fn stop(&mut self)

Stop the clock and reset the position to Duration::ZERO.

current_time() and current_pts() will return Duration::ZERO until start() or set_position() is called again.

Source

pub fn pause(&mut self)

Pause the clock at the current position.

current_time() and current_pts() are frozen until resume is called. If already Paused or Stopped, no-op.

Source

pub fn resume(&mut self)

Resume from a paused position. No-op if not paused.

Source

pub fn current_time(&self) -> Duration

Current wall-clock elapsed time since start (affected by rate).

Equivalent to current_pts for clocks that start at zero; use current_pts() when a seek offset has been set.

Source

pub fn current_pts(&self) -> Duration

Current presentation timestamp (elapsed time since position zero).

Identical to current_time() when the clock was started from zero. When a set_position(t) was called before start(), the clock advances from t and this method returns values ≥ t.

Source

pub fn is_running(&self) -> bool

Returns true if the clock is actively advancing.

Source

pub fn set_rate(&mut self, rate: f64)

Set the playback rate. Values ≤ 0.0 are ignored (rate stays unchanged).

When the clock is Running, the current position is re-anchored at Instant::now() so that current_time() does not jump.

Source

pub fn rate(&self) -> f64

Current playback rate (default: 1.0).

Source

pub fn set_position(&mut self, pts: Duration)

Jump to an arbitrary position in media time.

  • Running: the clock continues advancing from pts immediately.
  • Paused: the frozen position is updated to pts.
  • Stopped: pts is stored and applied as the starting position when start is next called.

After set_position(t) + start(), current_pts will immediately return values ≥ t.

Trait Implementations§

Source§

impl Default for PlaybackClock

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.