Skip to content

Commit

Permalink
Rename marker() to marker_bit() everywhere and expose getters for…
Browse files Browse the repository at this point in the history
… the remaining header bits
  • Loading branch information
sdroege authored and ystreet committed Feb 23, 2024
1 parent acae6cd commit b9d2695
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
34 changes: 20 additions & 14 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum RtpWriteError {
pub struct RtpPacketBuilder<P: PayloadLength, E: PayloadLength> {
padding: Option<u8>,
csrcs: smallvec::SmallVec<[u32; 15]>,
marker: bool,
marker_bit: bool,
payload_type: u8,
sequence_number: u16,
timestamp: u32,
Expand All @@ -56,7 +56,7 @@ impl<P: PayloadLength, E: PayloadLength> RtpPacketBuilder<P, E> {
Self {
padding: None,
csrcs: smallvec::smallvec![],
marker: false,
marker_bit: false,
// set to an invalid value to force the user to update
payload_type: 0xff,
sequence_number: 0,
Expand Down Expand Up @@ -92,8 +92,14 @@ impl<P: PayloadLength, E: PayloadLength> RtpPacketBuilder<P, E> {
}

/// Set the marker bit for this packet.
pub fn marker(mut self, marker: bool) -> Self {
self.marker = marker;
#[deprecated = "Use `marker_bit()` instead"]
pub fn marker(self, marker: bool) -> Self {
self.marker_bit(marker)
}

/// Set the marker bit for this packet.
pub fn marker_bit(mut self, marker_bit: bool) -> Self {
self.marker_bit = marker_bit;
self
}

Expand Down Expand Up @@ -184,7 +190,7 @@ impl<P: PayloadLength, E: PayloadLength> RtpPacketBuilder<P, E> {
buf[0] = byte;

let mut byte = self.payload_type & 0x7f;
if self.marker {
if self.marker_bit {
byte |= 0x80;
}
buf[1] = byte;
Expand Down Expand Up @@ -589,7 +595,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0);
assert_eq!(rtp.timestamp(), 0x0);
Expand All @@ -606,7 +612,7 @@ mod tests {
let mut vec = vec![];
let builder = RtpPacketBuilder::new()
.payload_type(96)
.marker(true)
.marker_bit(true)
.sequence_number(0x0102)
.timestamp(0x03040506)
.ssrc(0x0708090a)
Expand All @@ -624,7 +630,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 1);
assert!(rtp.marker());
assert!(rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down Expand Up @@ -698,7 +704,7 @@ mod tests {
let extension_data = [1, 2, 3, 4, 5, 6, 7, 8];
let builder = RtpPacketBuilder::new()
.payload_type(96)
.marker(true)
.marker_bit(true)
.sequence_number(0x0102)
.timestamp(0x03040506)
.ssrc(0x0708090a)
Expand All @@ -717,7 +723,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 1);
assert!(rtp.marker());
assert!(rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand All @@ -738,7 +744,7 @@ mod tests {
let payload_data = [1, 2, 3, 4, 5, 6, 7, 8];
let builder = RtpPacketBuilder::new()
.payload_type(96)
.marker(true)
.marker_bit(true)
.sequence_number(0x0102)
.timestamp(0x03040506)
.ssrc(0x0708090a)
Expand All @@ -759,7 +765,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), Some(7));
assert_eq!(rtp.n_csrcs(), 1);
assert!(rtp.marker());
assert!(rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down Expand Up @@ -1011,7 +1017,7 @@ mod tests {
let payload_data = TestPayload(vec![11, 12, 13, 14, 15, 16, 17, 18]);
let builder = RtpPacketBuilder::new()
.payload_type(96)
.marker(true)
.marker_bit(true)
.sequence_number(0x0102)
.timestamp(0x03040506)
.ssrc(0x0708090a)
Expand All @@ -1028,7 +1034,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), Some(7));
assert_eq!(rtp.n_csrcs(), 1);
assert!(rtp.marker());
assert!(rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down
14 changes: 10 additions & 4 deletions src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ impl<'a> RtpPacketMut<'a> {
}

/// Change the marker bit of this packet.
#[deprecated = "Use `set_marker_bit()` instead"]
pub fn set_marker(&mut self, marker: bool) {
if marker {
self.set_marker_bit(marker);
}

/// Change the marker bit of this packet.
pub fn set_marker_bit(&mut self, marker_bit: bool) {
if marker_bit {
self.data[1] |= 0x80;
} else {
self.data[1] &= 0x7f;
Expand Down Expand Up @@ -116,16 +122,16 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
assert_eq!(rtp.ssrc(), 0x07080910);
assert_eq!(rtp.csrc().count(), 0);
assert_eq!(rtp.extension(), None);
assert_eq!(rtp.payload(), &[]);
rtp.set_marker(true);
assert!(rtp.marker());
rtp.set_marker_bit(true);
assert!(rtp.marker_bit());
rtp.set_payload_type(12).unwrap();
assert_eq!(rtp.payload_type(), 12);
rtp.set_sequence_number(0x9876);
Expand Down
24 changes: 16 additions & 8 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'a> fmt::Debug for RtpPacket<'a> {

f.debug_struct("RtpPacket")
.field("version", &self.version())
.field("marker", &self.marker())
.field("marker_bit", &self.marker_bit())
.field("payload_type", &self.payload_type())
.field("sequence_number", &self.sequence_number())
.field("timestamp", &self.timestamp())
Expand Down Expand Up @@ -141,7 +141,8 @@ impl<'a> RtpPacket<'a> {
(self.data[0] & 0b1100_0000) >> 6
}

fn padding_bit(&self) -> bool {
/// Returns whether the padding bit is set for this packet.
pub fn padding_bit(&self) -> bool {
self.data[0] & 0b0010_0000 != 0
}

Expand All @@ -167,7 +168,14 @@ impl<'a> RtpPacket<'a> {

/// Returns whether the marker bit is set for this packet. The meaning of the marker bit
/// is payload-specific.
#[deprecated = "Use `marker_bit()` instead"]
pub fn marker(&self) -> bool {
self.marker_bit()
}

/// Returns whether the marker bit is set for this packet. The meaning of the marker bit
/// is payload-specific.
pub fn marker_bit(&self) -> bool {
(self.data[1] & 0b1000_0000) != 0
}

Expand Down Expand Up @@ -266,7 +274,7 @@ impl<'a> RtpPacket<'a> {
/// any padding bytes). Any aspect of the returned builder can be modified.
pub fn as_builder(&'a self) -> crate::RtpPacketBuilder<&'a [u8], &'a [u8]> {
let mut builder = crate::RtpPacketBuilder::new()
.marker(self.marker())
.marker_bit(self.marker_bit())
.payload_type(self.payload_type())
.sequence_number(self.sequence_number())
.timestamp(self.timestamp())
Expand Down Expand Up @@ -298,7 +306,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down Expand Up @@ -334,7 +342,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 1);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down Expand Up @@ -373,7 +381,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down Expand Up @@ -428,7 +436,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), None);
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand All @@ -450,7 +458,7 @@ mod tests {
assert_eq!(rtp.version(), 2);
assert_eq!(rtp.padding(), Some(2));
assert_eq!(rtp.n_csrcs(), 0);
assert!(!rtp.marker());
assert!(!rtp.marker_bit());
assert_eq!(rtp.payload_type(), 96);
assert_eq!(rtp.sequence_number(), 0x0102);
assert_eq!(rtp.timestamp(), 0x03040506);
Expand Down

0 comments on commit b9d2695

Please sign in to comment.