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