rtc_media/
lib.rs

1#![warn(rust_2018_idioms)]
2#![allow(dead_code)]
3
4pub mod audio;
5pub mod io;
6pub mod video;
7
8use bytes::Bytes;
9use shared::time::SystemInstant;
10use std::time::Duration;
11
12/// A Sample contains encoded media and timing information
13#[derive(Debug)]
14pub struct Sample {
15    /// The assembled data in the sample, as a bitstream.
16    ///
17    /// The format is Codec dependant, but is always a bitstream format
18    /// rather than the packetized format used when carried over RTP.
19    ///
20    /// See: [`rtp::packetizer::Depacketizer`] and implementations of it for more details.
21    pub data: Bytes,
22
23    /// The wallclock time when this sample was generated.
24    pub timestamp: SystemInstant,
25
26    /// The duration of this sample
27    pub duration: Duration,
28
29    /// The RTP packet timestamp of this sample.
30    ///
31    /// For all RTP packets that contributed to a single sample the timestamp is the same.
32    pub packet_timestamp: u32,
33
34    /// The number of packets that were dropped prior to building this sample.
35    ///
36    /// Packets being dropped doesn't necessarily indicate something wrong, e.g., packets are sometimes
37    /// dropped because they aren't relevant for sample building.
38    pub prev_dropped_packets: u16,
39
40    /// The number of packets that were identified as padding prior to building this sample.
41    ///
42    /// Some implementations, notably libWebRTC, send padding packets to keep the send rate steady.
43    /// These packets don't carry media and aren't useful for building samples.
44    ///
45    /// This field can be combined with [`Sample::prev_dropped_packets`] to determine if any
46    /// dropped packets are likely to have detrimental impact on the steadiness of the RTP stream.
47    ///
48    /// ## Example adjustment
49    ///
50    /// ```rust
51    /// # use bytes::Bytes;
52    /// # use std::time::{SystemTime, Duration};
53    /// # use rtc_media::Sample;
54    /// use shared::time::SystemInstant;
55    /// # let sample = Sample {
56    /// #   data: Bytes::new(),
57    /// #   timestamp: SystemInstant::now(),
58    /// #   duration: Duration::from_secs(0),
59    /// #   packet_timestamp: 0,
60    /// #   prev_dropped_packets: 10,
61    /// #   prev_padding_packets: 15
62    /// # };
63    /// #
64    /// let adjusted_dropped =
65    /// sample.prev_dropped_packets.saturating_sub(sample.prev_padding_packets);
66    /// ```
67    pub prev_padding_packets: u16,
68}
69
70impl Default for Sample {
71    fn default() -> Self {
72        Sample {
73            data: Bytes::new(),
74            timestamp: SystemInstant::now(),
75            duration: Duration::from_secs(0),
76            packet_timestamp: 0,
77            prev_dropped_packets: 0,
78            prev_padding_packets: 0,
79        }
80    }
81}
82
83impl PartialEq for Sample {
84    fn eq(&self, other: &Self) -> bool {
85        let mut equal: bool = true;
86        if self.data != other.data {
87            equal = false;
88        }
89        if self.timestamp.duration_since_unix_epoch().as_secs()
90            != other.timestamp.duration_since_unix_epoch().as_secs()
91        {
92            equal = false;
93        }
94        if self.duration != other.duration {
95            equal = false;
96        }
97        if self.packet_timestamp != other.packet_timestamp {
98            equal = false;
99        }
100        if self.prev_dropped_packets != other.prev_dropped_packets {
101            equal = false;
102        }
103        if self.prev_padding_packets != other.prev_padding_packets {
104            equal = false;
105        }
106
107        equal
108    }
109}