Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dholroyd committed Mar 11, 2024
1 parent 092b20f commit 1294be2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jobs:

steps:
- uses: actions/checkout@v1
- name: Check formatting
run: cargo fmt --check
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
5 changes: 3 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::*;
use rtp_rs::{RtpReader, RtpPacketBuilder};
use rtp_rs::{RtpPacketBuilder, RtpReader};

fn rtp_reader(c: &mut Criterion) {
let data = [
Expand Down Expand Up @@ -68,7 +68,8 @@ fn rtp_builder(c: &mut Criterion) {
.payload(&payload)
.marked(true)
.add_csrc(12)
.build().unwrap();
.build()
.unwrap();
});
});
}
Expand Down
76 changes: 46 additions & 30 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl PadInner {
} else {
Some(*n as usize - remainder)
}
},
}
}
}
}
Expand All @@ -94,7 +94,7 @@ pub struct RtpPacketBuilder<'a> {

ssrc: u32,
csrcs: [u32; 15],
csrc_count: u8
csrc_count: u8,
}

impl<'a> RtpPacketBuilder<'a> {
Expand All @@ -117,7 +117,7 @@ impl<'a> RtpPacketBuilder<'a> {

ssrc: 0,
csrcs: [0u32; 15],
csrc_count: 0
csrc_count: 0,
}
}

Expand Down Expand Up @@ -210,8 +210,16 @@ impl<'a> RtpPacketBuilder<'a> {
/* 12 is the length of the basic header */
let mut length = 12usize;
length += self.csrc_count as usize * 4;
length += if let Some((_, ext)) = self.extension { ext.len() + 4 } else { 0 };
length += if let Some(payload) = self.payload { payload.len() } else { 0 };
length += if let Some((_, ext)) = self.extension {
ext.len() + 4
} else {
0
};
length += if let Some(payload) = self.payload {
payload.len()
} else {
0
};
if let Some(adj) = self.padded.adjust_len(length) {
length += adj;
}
Expand All @@ -232,37 +240,37 @@ impl<'a> RtpPacketBuilder<'a> {
target[1] |= 0x80;
}

target[ 2] = (self.sequence.0 >> 8) as u8;
target[ 3] = (self.sequence.0 & 0xFF) as u8;
target[2] = (self.sequence.0 >> 8) as u8;
target[3] = (self.sequence.0 & 0xFF) as u8;

target[ 4] = (self.timestamp >> 24) as u8;
target[ 5] = (self.timestamp >> 16) as u8;
target[ 6] = (self.timestamp >> 8) as u8;
target[ 7] = (self.timestamp ) as u8;
target[4] = (self.timestamp >> 24) as u8;
target[5] = (self.timestamp >> 16) as u8;
target[6] = (self.timestamp >> 8) as u8;
target[7] = (self.timestamp) as u8;

target[ 8] = (self.ssrc >> 24) as u8;
target[ 9] = (self.ssrc >> 16) as u8;
target[10] = (self.ssrc >> 8) as u8;
target[11] = (self.ssrc ) as u8;
target[8] = (self.ssrc >> 24) as u8;
target[9] = (self.ssrc >> 16) as u8;
target[10] = (self.ssrc >> 8) as u8;
target[11] = (self.ssrc) as u8;

let mut write_index = 12usize;
for index in 0..self.csrc_count as usize {
let csrc = self.csrcs[index];
target[write_index + 0] = (csrc >> 24) as u8;
target[write_index + 1] = (csrc >> 16) as u8;
target[write_index + 2] = (csrc >> 8) as u8;
target[write_index + 3] = (csrc ) as u8;
target[write_index + 2] = (csrc >> 8) as u8;
target[write_index + 3] = (csrc) as u8;

write_index = write_index + 4;
}

if let Some((id, payload)) = self.extension {
target[write_index + 0] = (id >> 8) as u8;
target[write_index + 1] = (id & 0xFF) as u8;
target[write_index + 0] = (id >> 8) as u8;
target[write_index + 1] = (id & 0xFF) as u8;

let len = payload.len() / 4;
target[write_index + 2] = (len >> 8) as u8;
target[write_index + 3] = (len & 0xFF) as u8;
target[write_index + 2] = (len >> 8) as u8;
target[write_index + 3] = (len & 0xFF) as u8;

write_index = write_index + 4;

Expand All @@ -278,7 +286,7 @@ impl<'a> RtpPacketBuilder<'a> {
}

if let Some(padded_bytes) = self.padded.adjust_len(write_index) {
target[0] |= 1 << 5; /* set the padded flag */
target[0] |= 1 << 5; /* set the padded flag */

write_index += padded_bytes;
target[write_index - 1] = padded_bytes as u8;
Expand All @@ -298,7 +306,6 @@ impl<'a> RtpPacketBuilder<'a> {
Ok(self.build_into_unchecked(target))
}


/// Build the RTP packet.
/// On success it returns a buffer containing the target packet.
pub fn build(&self) -> Result<Vec<u8>, RtpPacketBuildError> {
Expand Down Expand Up @@ -351,7 +358,7 @@ impl std::fmt::Display for RtpPacketBuildError {

#[cfg(test)]
mod test {
use crate::{RtpPacketBuilder, Pad};
use crate::{Pad, RtpPacketBuilder};

#[test]
fn test_padded() {
Expand All @@ -360,10 +367,14 @@ mod test {
.payload_type(1)
.payload(&payload)
.padded(Pad::round_to(4))
.build().unwrap();
.build()
.unwrap();

assert_eq!(packet.len() & 0x03, 0);
assert!(crate::reader::RtpReader::new(&packet).unwrap().padding().is_some());
assert!(crate::reader::RtpReader::new(&packet)
.unwrap()
.padding()
.is_some());
}

#[test]
Expand All @@ -373,11 +384,15 @@ mod test {
.payload_type(1)
.payload(&payload)
.padded(Pad::round_to(4))
.build().unwrap();
.build()
.unwrap();

// assert the length is not increased beyond the 12 bytes of header + the payload
assert_eq!(packet.len(), 12 + payload.len());
assert!(crate::reader::RtpReader::new(&packet).unwrap().padding().is_none());
assert!(crate::reader::RtpReader::new(&packet)
.unwrap()
.padding()
.is_none());
}

#[test]
Expand All @@ -386,7 +401,8 @@ mod test {
let packet = RtpPacketBuilder::new()
.payload_type(1)
.payload(&payload)
.build().unwrap();
.build()
.unwrap();

assert_eq!(packet.len() & 0x03, 1);
}
Expand All @@ -401,4 +417,4 @@ mod test {
let mut buffer = [0u8; 100];
builder.build_into(&mut buffer).unwrap();
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ mod reader;
pub use reader::*;

mod builder;
pub use builder::*;
pub use builder::*;
23 changes: 12 additions & 11 deletions src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{RtpPacketBuilder, Seq};
use std::fmt;
use crate::{Seq, RtpPacketBuilder};

/// Wrapper around a byte-slice of RTP data, providing accessor methods for the RTP header fields.
pub struct RtpReader<'a> {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl<'a> RtpReader<'a> {
});
}
if r.padding_flag() {
let post_header_bytes = b.len() - r.payload_offset();
let post_header_bytes = b.len() - r.payload_offset();
// with 'padding' flag set, there must be at least a single byte after the headers to
// hold the padding length
if post_header_bytes == 0 {
Expand Down Expand Up @@ -308,7 +308,6 @@ mod tests {
use super::*;
use crate::IntoSeqIterator;


const TEST_RTP_PACKET: [u8; 391] = [
0x80u8, 0xe0u8, 0x27u8, 0x38u8, 0x64u8, 0xe4u8, 0x05u8, 0xa7u8, 0xa2u8, 0x42u8, 0xafu8,
0x01u8, 0x3cu8, 0x41u8, 0xa4u8, 0xa3u8, 0x5du8, 0x13u8, 0xf9u8, 0xcau8, 0x2cu8, 0x7eu8,
Expand Down Expand Up @@ -349,12 +348,11 @@ mod tests {
];

const TEST_RTP_PACKET_WITH_EXTENSION: [u8; 63] = [
144u8, 111u8, 79u8, 252u8, 224u8, 94u8, 104u8, 203u8, 30u8, 112u8, 208u8,
191u8, 190u8, 222u8, 0u8, 3u8, 34u8, 175u8, 185u8, 88u8, 49u8, 0u8, 171u8,
64u8, 48u8, 16u8, 219u8, 0u8, 104u8, 9u8, 136u8, 90u8, 174u8, 145u8, 68u8,
165u8, 227u8, 178u8, 187u8, 68u8, 166u8, 66u8, 235u8, 40u8, 171u8, 135u8,
30u8, 174u8, 130u8, 239u8, 205u8, 14u8, 211u8, 232u8, 65u8, 67u8, 153u8,
120u8, 63u8, 17u8, 101u8, 55u8, 17u8
144u8, 111u8, 79u8, 252u8, 224u8, 94u8, 104u8, 203u8, 30u8, 112u8, 208u8, 191u8, 190u8,
222u8, 0u8, 3u8, 34u8, 175u8, 185u8, 88u8, 49u8, 0u8, 171u8, 64u8, 48u8, 16u8, 219u8, 0u8,
104u8, 9u8, 136u8, 90u8, 174u8, 145u8, 68u8, 165u8, 227u8, 178u8, 187u8, 68u8, 166u8, 66u8,
235u8, 40u8, 171u8, 135u8, 30u8, 174u8, 130u8, 239u8, 205u8, 14u8, 211u8, 232u8, 65u8,
67u8, 153u8, 120u8, 63u8, 17u8, 101u8, 55u8, 17u8,
];

#[test]
Expand Down Expand Up @@ -413,8 +411,11 @@ mod tests {
fn builder_juggle_clear_payload() {
let new_payload = vec![];
let reader = RtpReader::new(&TEST_RTP_PACKET_WITH_EXTENSION).unwrap();
let buffer = reader.create_builder()
.payload(&new_payload).build().unwrap();
let buffer = reader
.create_builder()
.payload(&new_payload)
.build()
.unwrap();

let expected = &TEST_RTP_PACKET_WITH_EXTENSION[0..(3 + 4) * 4];
assert_eq!(&buffer.as_slice()[..], expected);
Expand Down

0 comments on commit 1294be2

Please sign in to comment.