webrtc-rs/rtp

padding included in payload

scottlamb opened this issue · 7 comments

impl Marshaller for Packet doesn't consider padding:

  • unmarshal shouldn't include the padding bytes in the new Packet's payload
  • marshal_size and marshal_to then also need to change to add in padding bytes

rtp/src/packet/mod.rs

Lines 33 to 52 in 54d1579

impl Marshaller for Packet {
/// Unmarshal parses the passed byte slice and stores the result in the Header this method is called upon
fn unmarshal(raw_packet: &Bytes) -> Result<Self, Error> {
let header = Header::unmarshal(raw_packet)?;
let payload = raw_packet.slice(header.marshal_size()..);
Ok(Packet { header, payload })
}
/// MarshalSize returns the size of the packet once marshaled.
fn marshal_size(&self) -> usize {
self.header.marshal_size() + self.payload.len()
}
/// MarshalTo serializes the packet and writes to the buffer.
fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize, Error> {
let n = self.header.marshal_to(buf)?;
buf.put(&*self.payload);
Ok(n + self.payload.len())
}

I'll be taking this up @scottlamb @rainliu. This might be a breaking change, which is a fun thing :))

@scottlamb mind sharing a test that fails due to this issue?

Here's an H.264 SPS packet I got from a Hikvision camera:

#[test]
fn test_padding() -> Result<(), Error> {
    let raw_pkt = Bytes::from_static(&[
        0xa0, 0x60, 0x19, 0x58, 0x63, 0xff, 0x7d, 0x7c, 0x4b, 0x98, 0xd4, 0x0a, 0x67, 0x4d, 0x00,
        0x29, 0x9a, 0x64, 0x03, 0xc0, 0x11, 0x3f, 0x2c, 0xd4, 0x04, 0x04, 0x05, 0x00, 0x00, 0x03,
        0x03, 0xe8, 0x00, 0x00, 0xea, 0x60, 0x04, 0x00, 0x00, 0x03,
    ]);
    let packet = Packet::unmarshal(&raw_pkt)?;
    assert_eq!(packet.payload, &raw_pkt[12..12+25]);
    Ok(())
}

The trailing 0x03 indicates the length of the padding (including itself), so the final three bytes should be omitted from payload.

fixed in 5da5385

@scottlamb @rainliu I really don't think this issue has been fixed completely so I'll be reopening, do indicate if its been solved.

do indicate if its been solved

I don't know, sorry. I switched to the rtp-rs crate after hitting this. rtp-rs already had correct padding handling and looked more efficient (less reference-counting due to Bytes cloning, no Vec allocations) although I didn't actually benchmark.

🥲 too bad. Should work on a fix and also improve performance this weekend. Do hope you get to using this library again when it's fixed!.