Skip to main content

moq_ext_player/
lib.rs

1//! A player over `moq_video` and `moq_audio`.
2//!
3//! It picks the rendition the link can carry and keeps audio and video in
4//! step. Nothing here depends on iroh or on a publisher. A transport builds a
5//! [`RemoteBroadcast`] from what it subscribed to, and attaches its view of the
6//! link with [`RemoteBroadcast::set_network`]. A broadcast that lives in the
7//! same process is read with [`RemoteBroadcast::from_consumer`].
8//!
9//! # Examples
10//!
11//! ```no_run
12//! # async fn example(consumer: moq_net::broadcast::Consumer) -> Result<(), moq_ext_player::Error> {
13//! use moq_ext_player::{PlayerConfig, RemoteBroadcast};
14//!
15//! let player = RemoteBroadcast::from_consumer(consumer).play(PlayerConfig::default())?;
16//! let mut frames = player.video_frames();
17//! while let Some(frame) = frames.next().await {
18//!     // draw `frame`
19//! #   let _ = frame;
20//! }
21//! # Ok(())
22//! # }
23//! ```
24//!
25//! The main types are:
26//!
27//! - [`RemoteBroadcast`]: a subscribed broadcast and its [`Catalog`] (hang's).
28//!   [`RemoteBroadcast::play`] starts a [`Player`].
29//! - [`Player`]: decodes the chosen rendition and keeps audio and video in step.
30//! - [`VideoFrames`]: the frame stream every renderer reads.
31//! - [`VideoBacklog`]: a queue of every presented picture with its timing.
32//! - [`AudioFrames`]: the decoded audio, for analysis or a custom output.
33//! - [`RenditionMode`]: how the video rendition is chosen, re-exported from
34//!   `moq_ext_adaptive`.
35//!
36//! A [`RemoteBroadcast`] that follows a route table looks for another route
37//! when its broadcast ends. A player adapts only to the network signals a
38//! transport attached. Without them, [`RenditionMode::Auto`] plays the best
39//! rendition its limits allow.
40//!
41//! Dropping a [`Player`] aborts its tasks and returns at once. Dropping the
42//! last clone of a [`RemoteBroadcast`] aborts the task that follows it.
43//!
44//! The futures of the crate and what dropping them does:
45//!
46//! | Future | Safe | Dropping it |
47//! |---|---|---|
48//! | [`VideoFrames::next`], [`AudioFrames::next`], [`RemoteBroadcast::closed`] | yes | loses nothing |
49//! | [`RemoteBroadcast::catalog_within`] | yes | stops waiting |
50//! | `RemoteBroadcast::mux_source` (feature `record`) | yes | stops waiting |
51
52#![forbid(unsafe_code)]
53
54/// Returns `timestamp` as the time since its track's zero.
55///
56/// Media timestamps always carry a time; only a scale-less track's key has
57/// none, and that reads as zero.
58pub(crate) fn duration(timestamp: moq_net::Timestamp) -> std::time::Duration {
59    std::time::Duration::try_from(timestamp).unwrap_or_default()
60}
61
62mod catalog;
63mod clean_close;
64mod network;
65mod player;
66mod remote;
67mod stats;
68// Helpers for this workspace's tests, not part of the public API.
69#[cfg(feature = "test-util")]
70#[doc(hidden)]
71pub mod test_util;
72
73pub use hang::catalog::Catalog;
74use moq_ext_sources::frames;
75pub use moq_ext_sources::{Error, SlotState, SourceClosed, VideoFrames};
76pub use moq_mux::container::Delay;
77
78pub use self::{
79    catalog::video_rendition,
80    network::NetworkSample,
81    player::{
82        Adaptation, AudioFrame, AudioFrames, AudioMode, DEFAULT_LATENCY, IMMEDIATE_LATENCY, Player,
83        PlayerConfig, PlayerStatus, RenditionMode, VideoBacklog,
84    },
85    remote::RemoteBroadcast,
86    stats::{AudioPlaybackStats, FrameTiming, MediaKind, PlaybackStats, VideoPlaybackStats},
87};