rtp_types/
edit.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use crate::{RtpPacket, RtpParseError, RtpWriteError};
4
5/// Mutable parsed RTP packet for editing of some fields.
6#[derive(Debug)]
7#[repr(transparent)]
8pub struct RtpPacketMut<'a> {
9    data: &'a mut [u8],
10}
11
12impl<'a> std::ops::Deref for RtpPacketMut<'a> {
13    type Target = RtpPacket<'a>;
14
15    fn deref(&self) -> &Self::Target {
16        // SAFETY: RtpPacket and RtpPacketMut have the same contents and only differ in mut-ness
17        unsafe { std::mem::transmute(self) }
18    }
19}
20
21impl<'a> RtpPacketMut<'a> {
22    /// The minimum number of bytes a RTP packet must be to be parsed correctly.
23    pub const MIN_RTP_PACKET_LEN: usize = RtpPacket::MIN_RTP_PACKET_LEN;
24
25    /// Parse a byte slice into an editable [`RtpPacketMut`].  The packet is first parsed using
26    /// [`RtpPacket::parse`] and will fail if parsing fails.
27    pub fn parse(data: &'a mut [u8]) -> Result<RtpPacketMut<'a>, RtpParseError> {
28        let _packet = RtpPacket::parse(data)?;
29
30        Ok(RtpPacketMut { data })
31    }
32
33    /// Change the marker bit of this packet.
34    #[deprecated = "Use `set_marker_bit()` instead"]
35    pub fn set_marker(&mut self, marker: bool) {
36        self.set_marker_bit(marker);
37    }
38
39    /// Change the marker bit of this packet.
40    pub fn set_marker_bit(&mut self, marker_bit: bool) {
41        if marker_bit {
42            self.data[1] |= 0x80;
43        } else {
44            self.data[1] &= 0x7f;
45        }
46    }
47
48    /// Change the payload type of this packet.
49    pub fn set_payload_type(&mut self, pt: u8) -> Result<(), RtpWriteError> {
50        if pt > 0x7f {
51            return Err(RtpWriteError::InvalidPayloadType(pt));
52        }
53
54        self.data[1] = (self.data[1] & 0x80) | pt;
55
56        Ok(())
57    }
58
59    /// Change the sequence number of this packet.
60    pub fn set_sequence_number(&mut self, sequence: u16) {
61        self.data[2] = (sequence >> 8) as u8;
62        self.data[3] = (sequence & 0xff) as u8;
63    }
64
65    /// Change the timestamp of this packet.
66    pub fn set_timestamp(&mut self, timestamp: u32) {
67        self.data[4] = (timestamp >> 24) as u8;
68        self.data[5] = ((timestamp >> 16) & 0xff) as u8;
69        self.data[6] = ((timestamp >> 8) & 0xff) as u8;
70        self.data[7] = (timestamp & 0xff) as u8;
71    }
72
73    /// Change the SSRC of this packet.
74    pub fn set_ssrc(&mut self, ssrc: u32) {
75        self.data[8] = (ssrc >> 24) as u8;
76        self.data[9] = ((ssrc >> 16) & 0xff) as u8;
77        self.data[10] = ((ssrc >> 8) & 0xff) as u8;
78        self.data[11] = (ssrc & 0xff) as u8;
79    }
80
81    /// Change the extension identifier of this packet.
82    ///
83    /// This has no effect if the packet does not contain any extension data.
84    pub fn set_extension_id(&mut self, id: u16) {
85        if self.extension_bit() {
86            let offset = self.extension_offset();
87            self.data[offset] = (id >> 8) as u8;
88            self.data[offset + 1] = (id & 0xff) as u8;
89        }
90    }
91
92    /// Returns a mutable reference to the extension data for this packet, if any.
93    pub fn extension_mut(&mut self) -> Option<&mut [u8]> {
94        if self.extension_bit() {
95            let offset = self.extension_offset();
96            let offset = offset + 4;
97            let len = self.extension_len();
98            Some(&mut self.data[offset..][..len])
99        } else {
100            None
101        }
102    }
103
104    /// Returns a mutable reference to the payload data of this packet.
105    pub fn payload_mut(&mut self) -> &mut [u8] {
106        let offset = self.payload_offset();
107        let pad = self.padding().unwrap_or_default() as usize;
108        let data_len = self.data.len();
109        &mut self.data[offset..data_len - pad]
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn edit_rtp_no_payload_no_extensions_no_csrc() {
119        let mut data: [u8; 12] = [
120            0x80, 0x60, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
121        ];
122        let mut rtp = RtpPacketMut::parse(data.as_mut_slice()).unwrap();
123        assert_eq!(rtp.version(), 2);
124        assert_eq!(rtp.padding(), None);
125        assert_eq!(rtp.n_csrcs(), 0);
126        assert!(!rtp.marker_bit());
127        assert_eq!(rtp.payload_type(), 96);
128        assert_eq!(rtp.sequence_number(), 0x0102);
129        assert_eq!(rtp.timestamp(), 0x03040506);
130        assert_eq!(rtp.ssrc(), 0x07080910);
131        assert_eq!(rtp.csrc().count(), 0);
132        assert_eq!(rtp.extension(), None);
133        assert_eq!(rtp.payload(), &[]);
134        rtp.set_marker_bit(true);
135        assert!(rtp.marker_bit());
136        rtp.set_payload_type(12).unwrap();
137        assert_eq!(rtp.payload_type(), 12);
138        rtp.set_sequence_number(0x9876);
139        assert_eq!(rtp.sequence_number(), 0x9876);
140        rtp.set_timestamp(0x19283746);
141        assert_eq!(rtp.timestamp(), 0x19283746);
142        rtp.set_ssrc(0x90807060);
143        assert_eq!(rtp.ssrc(), 0x90807060);
144    }
145}