Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ homepage = "https://webrtc.rs"
repository = "https://github.com/webrtc-rs/rtp"

[dependencies]
util = { package = "webrtc-util", version = "0.4.3", default-features = false, features = ["marshal"] }
util = { package = "webrtc-util", version = "0.5.0", default-features = false, features = ["marshal"] }
bytes = "1.1.0"
rand = "0.8.4"
thiserror = "1.0.29"
anyhow = "1.0.44"
thiserror = "1.0"
async-trait = "0.1.51"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/g7xx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(test)]
mod g7xx_test;

use crate::error::Result;
use crate::packetizer::Payloader;

use anyhow::Result;
use bytes::Bytes;

/// G711Payloader payloads G711 packets
Expand Down
12 changes: 5 additions & 7 deletions src/codecs/h264/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
mod h264_test;

use crate::{
error::Error,
error::{Error, Result},
packetizer::{Depacketizer, Payloader},
};

use anyhow::Result;
use bytes::{BufMut, Bytes, BytesMut};

/// H264Payloader payloads H264 packets
Expand Down Expand Up @@ -176,7 +175,7 @@ impl Depacketizer for H264Packet {
/// depacketize parses the passed byte slice and stores the result in the H264Packet this method is called upon
fn depacketize(&mut self, packet: &Bytes) -> Result<()> {
if packet.len() <= 2 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

let mut payload = BytesMut::new();
Expand Down Expand Up @@ -207,8 +206,7 @@ impl Depacketizer for H264Packet {
return Err(Error::StapASizeLargerThanBuffer(
nalu_size,
packet.len() - curr_offset,
)
.into());
));
}

if self.is_avc {
Expand All @@ -224,7 +222,7 @@ impl Depacketizer for H264Packet {
}
FUA_NALU_TYPE => {
if packet.len() < FUA_HEADER_SIZE as usize {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

if self.fua_buffer.is_none() {
Expand Down Expand Up @@ -255,7 +253,7 @@ impl Depacketizer for H264Packet {
self.payload = Bytes::new();
}
}
_ => return Err(Error::NaluTypeIsNotHandled(nalu_type).into()),
_ => return Err(Error::NaluTypeIsNotHandled(nalu_type)),
}

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/codecs/opus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
mod opus_test;

use crate::{
error::Error,
error::{Error, Result},
packetizer::{Depacketizer, Payloader},
};

use anyhow::Result;
use bytes::Bytes;

#[derive(Default, Debug, Copy, Clone)]
Expand Down Expand Up @@ -35,7 +34,7 @@ pub struct OpusPacket {
impl Depacketizer for OpusPacket {
fn depacketize(&mut self, packet: &Bytes) -> Result<()> {
if packet.is_empty() {
Err(Error::ErrShortPacket.into())
Err(Error::ErrShortPacket)
} else {
self.payload = packet.clone();
Ok(())
Expand Down
7 changes: 3 additions & 4 deletions src/codecs/vp8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
mod vp8_test;

use crate::{
error::Error,
error::{Error, Result},
packetizer::{Depacketizer, Payloader},
};

use anyhow::Result;
use bytes::{Buf, BufMut, Bytes, BytesMut};

pub const VP8_HEADER_SIZE: usize = 1;
Expand Down Expand Up @@ -139,7 +138,7 @@ impl Depacketizer for Vp8Packet {
/// depacketize parses the passed byte slice and stores the result in the VP8Packet this method is called upon
fn depacketize(&mut self, packet: &Bytes) -> Result<()> {
if packet.len() < 4 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
// 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
// +-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
Expand Down Expand Up @@ -199,7 +198,7 @@ impl Depacketizer for Vp8Packet {
}

if payload_index >= packet.len() {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

self.payload = packet.slice(payload_index..);
Expand Down
30 changes: 15 additions & 15 deletions src/codecs/vp9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
mod vp9_test;

use crate::{
error::Error,
error::{Error, Result},
packetizer::{Depacketizer, Payloader},
};
use anyhow::Result;

use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::fmt;
use std::sync::Arc;
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Depacketizer for Vp9Packet {
/// depacketize parses the passed byte slice and stores the result in the Vp9Packet this method is called upon
fn depacketize(&mut self, packet: &Bytes) -> Result<()> {
if packet.is_empty() {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

let reader = &mut packet.clone();
Expand Down Expand Up @@ -257,14 +257,14 @@ impl Vp9Packet {
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
let b = reader.get_u8();
payload_index += 1;
// PID present?
if (b & 0x80) != 0 {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
// M == 1, PID is 15bit
self.picture_id = (((b & 0x7f) as u16) << 8) | (reader.get_u8() as u16);
Expand Down Expand Up @@ -302,7 +302,7 @@ impl Vp9Packet {
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
let b = reader.get_u8();
payload_index += 1;
Expand All @@ -313,7 +313,7 @@ impl Vp9Packet {
self.d = b & 0x01 != 0;

if self.sid >= MAX_SPATIAL_LAYERS {
Err(Error::ErrTooManySpatialLayers.into())
Err(Error::ErrTooManySpatialLayers)
} else {
Ok(payload_index)
}
Expand All @@ -333,7 +333,7 @@ impl Vp9Packet {
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
self.tl0picidx = reader.get_u8();
payload_index += 1;
Expand All @@ -355,14 +355,14 @@ impl Vp9Packet {
let mut b = 1u8;
while (b & 0x1) != 0 {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
b = reader.get_u8();
payload_index += 1;

self.pdiff.push(b >> 1);
if self.pdiff.len() >= MAX_VP9REF_PICS {
return Err(Error::ErrTooManyPDiff.into());
return Err(Error::ErrTooManyPDiff);
}
}

Expand Down Expand Up @@ -391,7 +391,7 @@ impl Vp9Packet {
//
fn parse_ssdata(&mut self, reader: &mut dyn Buf, mut payload_index: usize) -> Result<usize> {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

let b = reader.get_u8();
Expand All @@ -406,7 +406,7 @@ impl Vp9Packet {

if self.y {
if reader.remaining() < 4 * ns {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

self.width = vec![0u16; ns];
Expand All @@ -420,7 +420,7 @@ impl Vp9Packet {

if self.g {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

self.ng = reader.get_u8();
Expand All @@ -429,7 +429,7 @@ impl Vp9Packet {

for i in 0..self.ng as usize {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}
let b = reader.get_u8();
payload_index += 1;
Expand All @@ -439,7 +439,7 @@ impl Vp9Packet {

let r = ((b >> 2) & 0x3) as usize;
if reader.remaining() < r {
return Err(Error::ErrShortPacket.into());
return Err(Error::ErrShortPacket);
}

self.pgpdiff.push(vec![]);
Expand Down
8 changes: 3 additions & 5 deletions src/codecs/vp9/vp9_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,10 @@ fn test_vp9_packet_unmarshal() -> Result<()> {

if let Some(expected) = err {
if let Err(actual) = p.depacketize(&b) {
assert!(
expected.equal(&actual),
assert_eq!(
expected, actual,
"{}: expected {}, but got {}",
name,
expected,
actual
name, expected, actual
);
} else {
assert!(false, "{}: expected error, but got passed", name);
Expand Down
23 changes: 18 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use thiserror::Error;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug, PartialEq)]
#[non_exhaustive]
pub enum Error {
#[error("RTP header size insufficient")]
ErrHeaderSizeInsufficient,
Expand Down Expand Up @@ -47,14 +50,24 @@ pub enum Error {
StapASizeLargerThanBuffer(usize, usize),
#[error("nalu type {0} is currently not handled")]
NaluTypeIsNotHandled(u8),
#[error("{0}")]
Util(#[from] util::Error),

#[allow(non_camel_case_types)]
#[error("{0}")]
new(String),
Other(String),
}

impl From<Error> for util::Error {
fn from(e: Error) -> Self {
util::Error::from_std(e)
}
}

impl Error {
pub fn equal(&self, err: &anyhow::Error) -> bool {
err.downcast_ref::<Self>().map_or(false, |e| e == self)
impl PartialEq<util::Error> for Error {
fn eq(&self, other: &util::Error) -> bool {
if let Some(down) = other.downcast_ref::<Error>() {
return self == down;
}
false
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::error::Result;

use bytes::BytesMut;
use chrono::prelude::*;
Expand Down Expand Up @@ -47,7 +48,7 @@ fn test_ntp_conversion() -> Result<()> {
let input = UNIX_EPOCH
.checked_add(Duration::from_nanos(t.timestamp_nanos() as u64))
.unwrap_or(UNIX_EPOCH);
let diff = input.duration_since(output)?.as_nanos() as i128;
let diff = input.duration_since(output).unwrap().as_nanos() as i128;
if diff < -ABS_SEND_TIME_RESOLUTION || ABS_SEND_TIME_RESOLUTION < diff {
assert!(
false,
Expand Down Expand Up @@ -101,7 +102,7 @@ fn test_abs_send_time_extension_estimate() -> Result<()> {
let receive = AbsSendTimeExtension::unmarshal(buf)?;

let estimated = receive.estimate(ntp2unix(receive_ntp));
let diff = estimated.duration_since(in_time)?.as_nanos() as i128;
let diff = estimated.duration_since(in_time).unwrap().as_nanos() as i128;
if diff < -ABS_SEND_TIME_RESOLUTION || ABS_SEND_TIME_RESOLUTION < diff {
assert!(
false,
Expand Down
5 changes: 2 additions & 3 deletions src/extension/abs_send_time_extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod abs_send_time_extension_test;
use crate::error::Error;
use util::marshal::{Marshal, MarshalSize, Unmarshal};

use anyhow::Result;
use bytes::{Buf, BufMut};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

Expand All @@ -19,7 +18,7 @@ pub struct AbsSendTimeExtension {

impl Unmarshal for AbsSendTimeExtension {
/// Unmarshal parses the passed byte slice and stores the result in the members.
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self, util::Error>
where
Self: Sized,
B: Buf,
Expand All @@ -46,7 +45,7 @@ impl MarshalSize for AbsSendTimeExtension {

impl Marshal for AbsSendTimeExtension {
/// MarshalTo serializes the members to buffer.
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error> {
if buf.remaining_mut() < ABS_SEND_TIME_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall.into());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::error::Result;
use bytes::{Bytes, BytesMut};

#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/extension/audio_level_extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ mod audio_level_extension_test;
use crate::error::Error;
use util::marshal::{Marshal, MarshalSize, Unmarshal};

use anyhow::Result;
use bytes::{Buf, BufMut};

// AUDIO_LEVEL_EXTENSION_SIZE One byte header size
Expand Down Expand Up @@ -37,7 +36,7 @@ pub struct AudioLevelExtension {

impl Unmarshal for AudioLevelExtension {
/// Unmarshal parses the passed byte slice and stores the result in the members
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self>
fn unmarshal<B>(raw_packet: &mut B) -> Result<Self, util::Error>
where
Self: Sized,
B: Buf,
Expand All @@ -64,7 +63,7 @@ impl MarshalSize for AudioLevelExtension {

impl Marshal for AudioLevelExtension {
/// MarshalTo serializes the members to buffer
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error> {
if buf.remaining_mut() < AUDIO_LEVEL_EXTENSION_SIZE {
return Err(Error::ErrBufferTooSmall.into());
}
Expand Down
Loading