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