Skip to main content

rtci_media/
lib.rs

1#![warn(rust_2018_idioms)]
2#![warn(missing_docs)]
3#![allow(dead_code)]
4
5//! Media samples and container I/O.
6//!
7//! The bridge between encoded media and RTP: a codec-agnostic [`Sample`] type, and readers
8//! and writers for the container formats the examples and tests use.
9//!
10//! # Structure
11//!
12//! * [`Sample`] — one encoded unit of media (a video frame, an audio frame) with its
13//!   duration, timestamp and packet metadata. Hand these to a sample-based local track and
14//!   the RTP packetizer does the rest.
15//! * [`io`] — the [`Writer`](io::Writer) trait plus concrete readers and writers for IVF
16//!   (VP8/VP9), Ogg (Opus) and H.264/H.265 Annex B — [`IVFReader`](io::ivf_reader::IVFReader),
17//!   [`OggReader`](io::ogg_reader::OggReader) and friends — enough to play media from disk or
18//!   record it to disk. [`SampleBuilder`](io::sample_builder::SampleBuilder) goes the other
19//!   way, reassembling inbound RTP into [`Sample`]s.
20//! * [`audio`], [`video`] — per-codec helpers, including audio buffering and frame
21//!   inspection.
22//!
23//! # Example
24//!
25//! ```
26//! use bytes::Bytes;
27//! use rtci_media::Sample;
28//! use std::time::{Duration, Instant};
29//!
30//! // The caller supplies the instant; `Sample` resolves no clock of its own.
31//! let now = Instant::now();
32//!
33//! // One encoded frame, ready to hand to a sample-based local track.
34//! let sample = Sample {
35//!     data: Bytes::from_static(&[0u8; 128]),
36//!     timestamp: now,
37//!     duration: Duration::from_millis(33), // ~30 fps
38//!     ..Sample::new(now)
39//! };
40//! assert_eq!(sample.data.len(), 128);
41//! ```
42//!
43//! Most applications do not depend on this crate directly — the
44//! [`rtci`](https://docs.rs/rtci) crate re-exports it as `rtci::media`.
45
46/// Audio sample types and multi-channel buffers.
47pub mod audio;
48/// Container readers and writers, plus RTP sample reassembly.
49pub mod io;
50/// Video frame helpers.
51pub mod video;
52
53use bytes::Bytes;
54use std::time::{Duration, Instant};
55
56/// A Sample contains encoded media and timing information
57#[derive(Debug)]
58pub struct Sample {
59    /// The assembled data in the sample, as a bitstream.
60    ///
61    /// The format is Codec dependant, but is always a bitstream format
62    /// rather than the packetized format used when carried over RTP.
63    ///
64    /// See: [`rtp::packetizer::Depacketizer`] and implementations of it for more details.
65    pub data: Bytes,
66
67    /// The time when this sample was generated.
68    pub timestamp: Instant,
69
70    /// The duration of this sample
71    pub duration: Duration,
72
73    /// The RTP packet timestamp of this sample.
74    ///
75    /// For all RTP packets that contributed to a single sample the timestamp is the same.
76    pub packet_timestamp: u32,
77
78    /// The number of packets that were dropped prior to building this sample.
79    ///
80    /// Packets being dropped doesn't necessarily indicate something wrong, e.g., packets are sometimes
81    /// dropped because they aren't relevant for sample building.
82    pub prev_dropped_packets: u16,
83
84    /// The number of packets that were identified as padding prior to building this sample.
85    ///
86    /// Some implementations, notably libWebRTC, send padding packets to keep the send rate steady.
87    /// These packets don't carry media and aren't useful for building samples.
88    ///
89    /// This field can be combined with [`Sample::prev_dropped_packets`] to determine if any
90    /// dropped packets are likely to have detrimental impact on the steadiness of the RTP stream.
91    ///
92    /// ## Example adjustment
93    ///
94    /// ```rust
95    /// # use bytes::Bytes;
96    /// # use std::time::{Instant, Duration};
97    /// # use rtci_media::Sample;
98    /// # let sample = Sample {
99    /// #   data: Bytes::new(),
100    /// #   timestamp: Instant::now(),
101    /// #   duration: Duration::from_secs(0),
102    /// #   packet_timestamp: 0,
103    /// #   prev_dropped_packets: 10,
104    /// #   prev_padding_packets: 15
105    /// # };
106    /// #
107    /// let adjusted_dropped =
108    /// sample.prev_dropped_packets.saturating_sub(sample.prev_padding_packets);
109    /// ```
110    pub prev_padding_packets: u16,
111}
112
113impl Sample {
114    /// An empty sample stamped at `now`.
115    ///
116    /// This replaces `Default`: the timestamp is a real-world observation the caller supplies,
117    /// not something the constructor may reach for.
118    pub fn new(now: Instant) -> Self {
119        Sample {
120            data: Bytes::new(),
121            timestamp: now,
122            duration: Duration::from_secs(0),
123            packet_timestamp: 0,
124            prev_dropped_packets: 0,
125            prev_padding_packets: 0,
126        }
127    }
128}
129
130impl PartialEq for Sample {
131    fn eq(&self, other: &Self) -> bool {
132        let mut equal: bool = true;
133        if self.data != other.data {
134            equal = false;
135        }
136        if self.timestamp != other.timestamp {
137            equal = false;
138        }
139        if self.duration != other.duration {
140            equal = false;
141        }
142        if self.packet_timestamp != other.packet_timestamp {
143            equal = false;
144        }
145        if self.prev_dropped_packets != other.prev_dropped_packets {
146            equal = false;
147        }
148        if self.prev_padding_packets != other.prev_padding_packets {
149            equal = false;
150        }
151
152        equal
153    }
154}