Skip to main content

moq_ext_sources/
encoded.rs

1//! Media that arrives encoded, for a broadcast to publish as it is.
2//!
3//! A hardware camera that encodes on its own and a media file both hand over
4//! encoded frames, which a broadcast publishes without an encoder.
5//! `MediaFile::encoded_video` lists the trade-offs.
6
7use std::time::Instant;
8
9use bytes::Bytes;
10use n0_future::boxed::BoxStream;
11
12use crate::Error;
13
14/// One encoded frame and the time it presents at.
15#[derive(Debug, Clone)]
16#[non_exhaustive]
17pub struct EncodedFrame {
18    /// The frame: one length-prefixed H.264 access unit, or one raw AAC frame.
19    pub data: Bytes,
20    /// The presentation time, on the producer's own timeline.
21    ///
22    /// Frames come in decode order, so with B-frames the times do not rise
23    /// monotonically.
24    pub timestamp: moq_net::Timestamp,
25    /// The wall-clock instant `timestamp` stands for.
26    ///
27    /// A broadcast maps every timestamp of a stream with the offset of its
28    /// first frame's `at`, so an audio and a video stream that agree on it
29    /// stay in sync wherever each starts.
30    pub at: Instant,
31}
32
33impl EncodedFrame {
34    /// Creates a frame of `data` that presents at `timestamp`, which stands for the instant `at`.
35    #[must_use]
36    pub fn new(data: Bytes, timestamp: moq_net::Timestamp, at: Instant) -> Self {
37        Self {
38            data,
39            timestamp,
40            at,
41        }
42    }
43}
44
45/// Encoded video for `LocalBroadcast::set_encoded_video`.
46#[derive(derive_more::Debug)]
47#[non_exhaustive]
48pub enum EncodedVideo {
49    /// An H.264 Annex-B byte stream, which may split anywhere.
50    ///
51    /// Access units are found by their start codes, and the stream's own
52    /// parameter sets describe it, so it should repeat them before every
53    /// keyframe. The broadcast stamps each access unit when it arrives.
54    AnnexB(#[debug(skip)] BoxStream<Bytes>),
55    /// H.264 access units in decode order, length-prefixed as an MP4 file stores them.
56    Avc1 {
57        /// The H.264 configuration record (avcC) that opens the stream.
58        config: Bytes,
59        /// The access units, which end with the first error.
60        #[debug(skip)]
61        frames: BoxStream<Result<EncodedFrame, Error>>,
62    },
63}
64
65/// Encoded audio for `LocalBroadcast::set_encoded_audio`.
66#[derive(derive_more::Debug)]
67#[non_exhaustive]
68pub enum EncodedAudio {
69    /// Raw AAC frames, one per [`EncodedFrame`], without ADTS headers.
70    Aac {
71        /// The AudioSpecificConfig that opens the stream.
72        config: Bytes,
73        /// The frames, which end with the first error.
74        #[debug(skip)]
75        frames: BoxStream<Result<EncodedFrame, Error>>,
76    },
77}