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 std::time::{Duration, SystemTime};
9
10use bytes::Bytes;
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: SystemTime,
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    /// # let sample = Sample {
55    /// #   data: Bytes::new(),
56    /// #   timestamp: SystemTime::now(),
57    /// #   duration: Duration::from_secs(0),
58    /// #   packet_timestamp: 0,
59    /// #   prev_dropped_packets: 10,
60    /// #   prev_padding_packets: 15
61    /// # };
62    /// #
63    /// let adjusted_dropped =
64    /// sample.prev_dropped_packets.saturating_sub(sample.prev_padding_packets);
65    /// ```
66    pub prev_padding_packets: u16,
67}
68
69impl Default for Sample {
70    fn default() -> Self {
71        Sample {
72            data: Bytes::new(),
73            timestamp: SystemTime::now(),
74            duration: Duration::from_secs(0),
75            packet_timestamp: 0,
76            prev_dropped_packets: 0,
77            prev_padding_packets: 0,
78        }
79    }
80}
81
82impl PartialEq for Sample {
83    fn eq(&self, other: &Self) -> bool {
84        let mut equal: bool = true;
85        if self.data != other.data {
86            equal = false;
87        }
88        if self.timestamp.elapsed().unwrap().as_secs()
89            != other.timestamp.elapsed().unwrap().as_secs()
90        {
91            equal = false;
92        }
93        if self.duration != other.duration {
94            equal = false;
95        }
96        if self.packet_timestamp != other.packet_timestamp {
97            equal = false;
98        }
99        if self.prev_dropped_packets != other.prev_dropped_packets {
100            equal = false;
101        }
102        if self.prev_padding_packets != other.prev_padding_packets {
103            equal = false;
104        }
105
106        equal
107    }
108}