mpeg2ts_reader/descriptor/
max_bitrate.rs

1//! Describes the maximum bitrate of the stream to which this descriptor is attached, including
2//! transport overheads.
3//!
4//! May be attached an an elementary stream, indicating the max bitrate of that elementary stream,
5//! or to the program as a whole.  In both cases it appears in the PMT.
6
7use super::descriptor_len;
8use super::DescriptorError;
9use std::fmt;
10
11/// Describes the max bitrate of an elementary stream or a whole program.
12pub struct MaximumBitrateDescriptor<'buf> {
13    buf: &'buf [u8],
14}
15impl<'buf> MaximumBitrateDescriptor<'buf> {
16    /// The descriptor tag value which identifies the descriptor as an `MaximumBitrateDescriptor`.
17    pub const TAG: u8 = 14;
18    /// Construct a `MaximumBitrateDescriptor` instance that will parse the data from the given
19    /// slice.
20    pub fn new(
21        tag: u8,
22        buf: &'buf [u8],
23    ) -> Result<MaximumBitrateDescriptor<'buf>, DescriptorError> {
24        assert_eq!(tag, Self::TAG);
25        descriptor_len(buf, tag, 3)?;
26        Ok(MaximumBitrateDescriptor { buf })
27    }
28
29    /// The maximum bitrate expressed in units of 50 bytes per second
30    pub fn maximum_bitrate(&self) -> u32 {
31        u32::from(self.buf[0] & 0b0011_1111) << 16
32            | u32::from(self.buf[1]) << 8
33            | u32::from(self.buf[2])
34    }
35
36    /// Convenience method which converts the result of `maximum_bitrate()` into a bits-per-second
37    /// value.
38    pub fn maximum_bits_per_second(&self) -> u32 {
39        self.maximum_bitrate() * 50 * 8
40    }
41}
42
43impl fmt::Debug for MaximumBitrateDescriptor<'_> {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        f.debug_struct("MaximumBitrateDescriptor")
46            .field("maximum_bitrate", &self.maximum_bitrate())
47            .finish()
48    }
49}
50
51#[cfg(test)]
52mod test {
53    use super::super::{CoreDescriptors, Descriptor};
54    use assert_matches::assert_matches;
55    use hex_literal::*;
56
57    #[test]
58    fn descriptor() {
59        let data = hex!("0e03c00184");
60        let desc = CoreDescriptors::from_bytes(&data[..]).unwrap();
61        assert_matches!(desc, CoreDescriptors::MaximumBitrate(max_bitrate) => {
62            assert_eq!(max_bitrate.maximum_bitrate(), 388);
63            assert_eq!(max_bitrate.maximum_bits_per_second(), 155200);
64            assert!(!format!("{:?}", max_bitrate).is_empty());
65        });
66    }
67}