pub struct Player { /* private fields */ }Expand description
One playback: video into VideoFrames, audio into the speaker and AudioFrames.
Dropping it aborts its tasks and returns at once: decoders stop at their next await point and are not waited for, so a decoder thread may outlive the player briefly. A second view of the broadcast is a second player.
Implementations§
Source§impl Player
impl Player
Sourcepub fn video_frames(&self) -> VideoFrames
pub fn video_frames(&self) -> VideoFrames
Returns the decoded video frames.
Every call returns a handle onto the same stream, and each handle keeps its own cursor. The stream survives rendition switches and decoder changes. It ends when the broadcast closes or the player is dropped.
Sourcepub fn audio_frames(&self) -> AudioFrames
pub fn audio_frames(&self) -> AudioFrames
Returns the decoded audio, as PCM with its timestamps.
The frames flow only while PlayerConfig::audio is not
AudioMode::Off, with or without a speaker. Without one, the audio is
decoded without playing it, for analysis. Each call returns a new handle
that starts at the next decoded frame and ends when the player is
dropped.
§Examples
use moq_ext_player::{AudioMode, PlayerConfig};
let player = broadcast.play(PlayerConfig::default().with_audio(AudioMode::Decode))?;
let mut audio = player.audio_frames();
while let Some(frame) = audio.next().await {
let peak = frame.samples.iter().fold(0f32, |peak, s| peak.max(s.abs()));
println!("{:?}: peak {peak}", frame.timestamp);
}Sourcepub fn broadcast(&self) -> &RemoteBroadcast
pub fn broadcast(&self) -> &RemoteBroadcast
Returns the broadcast this player plays.
Sourcepub fn set_rendition(&self, mode: RenditionMode)
pub fn set_rendition(&self, mode: RenditionMode)
Chooses how the video rendition is picked.
Sourcepub fn set_latency(&self, latency: Delay) -> Result<(), Error>
pub fn set_latency(&self, latency: Delay) -> Result<(), Error>
Changes how far behind live the player runs, without a gap in the picture.
The playout clock starts over at the new delay, so the schedule moves
once: the next video frame or audio write anchors it again. The latency
is the subscription’s Delay. A new max alone moves the video’s
subscription in place. A new min, or a change to or from no max,
rebuilds the video decoder behind the picture with the new range: the
new decoder takes over once it has caught up, as in a switch, and the
current picture stays up until then. Audio keeps its old range until it
next opens, because a rebuild would be audible.
This does not block or await, so there is nothing to cancel: the change is recorded before it returns, and the player’s tasks apply it on their next pass. Setting the latency already in force changes nothing.
§Errors
Returns Error::InvalidConfig if latency.min is above
latency.max, and leaves the latency unchanged.
Sourcepub fn set_decoder(&self, decoder: Backend)
pub fn set_decoder(&self, decoder: Backend)
Changes the video decoder backend.
The replacement opens behind the picture and takes over once it has
caught up. A backend that fails to open leaves the incumbent playing and
says so in PlayerStatus::switch_error. Automatic rendition switches
also open under the failing backend, so they fail until the next
set_decoder.
Sourcepub fn set_volume(&self, volume: f32)
pub fn set_volume(&self, volume: f32)
Sets the audio volume, where 1.0 is unattenuated.
Sourcepub fn status(&self) -> Receiver<PlayerStatus>
pub fn status(&self) -> Receiver<PlayerStatus>
Returns the player’s state.
A borrow of the receiver blocks the player’s writes, so keep it short and do not hold it across a call into the player.
Sourcepub fn stats(&self) -> PlaybackStats
pub fn stats(&self) -> PlaybackStats
Returns the player’s playback statistics.
Sourcepub fn timeline(&self) -> Vec<FrameTiming>
pub fn timeline(&self) -> Vec<FrameTiming>
Returns the last presented video and audio frames, in presentation order.
Keeps a few hundred frames of each medium, about ten seconds at usual rates. It is meant for a debugging view of pacing and A/V sync. Read it once per redraw of that view, not per frame.
Sourcepub fn video_timing(&self, timestamp: Timestamp) -> Option<FrameTiming>
pub fn video_timing(&self, timestamp: Timestamp) -> Option<FrameTiming>
Returns the timing of the presented picture at timestamp, or None once it left the timeline.
timestamp is the timestamp of a frame from
video_frames. Its arrived, decoded and
presented tell the time on the network from the time in the decoder
and in the playout clock. Look a frame up soon after it is handed out.
Also None for a timestamp without a timescale, which no presented
picture has.
§Examples
let mut frames = player.video_frames();
while let Some(frame) = frames.next().await {
let Some(timing) = player.video_timing(frame.timestamp) else {
continue;
};
if let Some(arrived) = timing.arrived {
println!("{:?} since its data arrived", arrived.elapsed());
}
println!("{:?} in the decoder", timing.decode_time());
}Sourcepub fn video_backlog(&self, capacity: usize) -> VideoBacklog
pub fn video_backlog(&self, capacity: usize) -> VideoBacklog
Returns a queue of every picture presented from now on, with its timing.
Unlike video_frames, which skips pictures a
slow reader missed, VideoBacklog::take returns each picture once.
The backlog holds at most capacity pictures, at least one, and drops
the oldest beyond it.
A held picture keeps its decoded buffer alive, which may come from a hardware decoder’s fixed pool, so a large capacity or a backlog no longer taken can stall hardware decoding. Drop it when you stop taking.
§Examples
let backlog = player.video_backlog(16);
let mut tick = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
tick.tick().await;
for (frame, timing) in backlog.take() {
println!("{:?}: in the decoder for {:?}", frame.timestamp, timing.decode_time());
}
}Sourcepub fn take_latest_video(&self) -> Option<(Arc<Frame>, FrameTiming)>
pub fn take_latest_video(&self) -> Option<(Arc<Frame>, FrameTiming)>
Returns the newest presented picture since the last call, with its timing.
For a consumer that samples the video at its own rate, such as an
analysis loop or a thumbnailer, instead of reading every picture from
video_frames. Pictures presented between two
calls are skipped. Returns None if nothing was presented since the
last call. The player keeps one cursor for this method, so two callers
take turns at the same pictures. A reader that needs its own cursor
can use a video_backlog of capacity 1.
FrameTiming::arrived tells the time on the network from the time
in the decoder. It is None for a picture the decoder could not match
to one access unit, and then decoded is the earliest instant the
player knows of.
§Examples
let mut tick = tokio::time::interval(std::time::Duration::from_millis(100));
loop {
tick.tick().await;
if let Some((frame, timing)) = player.take_latest_video() {
let age = timing.presented.elapsed();
println!("{} picture, presented {age:?} ago", frame.size());
}
}