Skip to main content

PlayerHandle

Struct PlayerHandle 

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

Shared, cloneable handle to a running PlayerRunner.

All methods are non-blocking. Commands that cannot be queued immediately (channel full) are silently dropped.

§Thread safety

PlayerHandle is Clone + Send + Sync and can be shared freely across threads without locking.

Implementations§

Source§

impl PlayerHandle

Source

pub fn play(&self)

Resume playback.

Source

pub fn pause(&self)

Pause playback.

Source

pub fn stop(&self)

Stop the presentation loop.

Source

pub fn seek(&self, pts: Duration)

Seek to pts.

Consecutive calls before the runner processes them are coalesced — only the most recent pts executes.

Source

pub fn set_rate(&self, rate: f64)

Set the playback rate.

  • Positive values play forward at the given speed multiplier (e.g. 2.0 = 2×).
  • Negative values play in reverse at abs(rate) speed (e.g. -1.0 = 1× reverse). Audio is muted during reverse playback and automatically resumes on the next positive-rate call.
  • 0.0 is ignored.
Source

pub fn set_av_offset(&self, ms: i64)

Set the A/V offset correction in milliseconds.

Positive: video PTS is shifted down relative to audio (video appears delayed). Negative: video PTS is shifted up (audio appears delayed).

Source

pub fn update_timeline(&self, timeline: Timeline)

Replace the running timeline’s clip layout in place.

Sends a PlayerCommand::UpdateLayout to TimelineRunner. The runner updates timeline_start / timeline_end / in_point / out_point for every existing clip, stops audio decode threads, and seeks all decode buffers to the last known media PTS — so the next presented frame is spatially correct after the move.

The MasterClock and paused / stopped atomics are unaffected. Drops silently if the command channel (capacity 64) is full.

No-op when called on a PlayerRunner-backed handle (single-track player). Only TimelineRunner handles this command.

Source

pub fn current_pts(&self) -> Duration

PTS of the most recently presented frame.

Returns Duration::ZERO before the first frame is presented.

Source

pub fn duration(&self) -> Option<Duration>

Container-reported duration, or None for live / streaming sources.

Source

pub fn audio_sample_rate(&self) -> Option<u32>

Sample rate of the PCM data returned by pop_audio_samples.

Returns Some(48_000) for files that contain an audio stream, and None for video-only files (where pop_audio_samples always returns an empty Vec).

Use this to configure your audio backend without hardcoding a magic constant:

let cfg = cpal::StreamConfig {
    channels: 2,
    sample_rate: cpal::SampleRate(handle.audio_sample_rate().unwrap_or(48_000)),
    ..Default::default()
};
Source

pub fn pop_audio_samples(&self, n: usize) -> Vec<f32>

Pull up to n interleaved stereo f32 PCM samples at 48 kHz.

Returns an empty Vec when:

  • playback is paused or stopped,
  • n is 0,
  • there is no audio track, or
  • the ring buffer is empty (underrun — caller should output silence).

Advances the audio master clock by samples.len() / 2 stereo frames.

Source

pub fn pop_audio_samples_for_rate( &self, pop_n: usize, clock_stereo_pairs: u64, ) -> Vec<f32>

Pull up to pop_n interleaved stereo f32 PCM samples at 48 kHz and advance the A/V sync clock by exactly clock_stereo_pairs — independent of how many samples are actually available in the ring buffer.

Use this instead of pop_audio_samples when playing at rates other than 1×. The cpal callback pops out_len * rate decoded samples to drive rate-scaled audio, but the master clock must still advance at the hardware output rate (out_len / 2 per callback) so that MasterClock::Audio’s pts_base + delta / sr * rate formula yields the correct media PTS without double-counting the rate.

§Arguments
  • pop_n — decoded samples to drain from the ring buffer (output_buf.len() * rate, rounded).
  • clock_stereo_pairs — hardware stereo pairs to add to the sync counter (output_buf.len() / 2, constant regardless of rate).
Source

pub fn poll_event(&self) -> Option<PlayerEvent>

Poll for the next PlayerEvent without blocking.

Returns None when no events are pending.

Source

pub fn recv_event(&self) -> Option<PlayerEvent>

Block until the next PlayerEvent arrives or the channel closes.

Returns None when the runner has exited and all events have been drained. Intended for use inside spawn_blocking.

Trait Implementations§

Source§

impl Clone for PlayerHandle

Source§

fn clone(&self) -> PlayerHandle

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.