Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

API and Implementation Improvements #27

Closed
wants to merge 8 commits into from
Closed
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
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ util = { package = "webrtc-util", version = "0.5.4", default-features = false, f
bytes = "1"
rand = "0.8.5"
thiserror = "1.0"
async-trait = "0.1.56"

[dev-dependencies]
chrono = "0.4.19"
criterion = "0.3.5"
tokio = { version = "1.19", features = ["full"] }
tokio-test = "0.4.0" # must match the min version of the `tokio` crate above

[[bench]]
name = "packet_bench"
Expand Down
4 changes: 0 additions & 4 deletions src/codecs/g7xx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,4 @@ impl Payloader for G7xxPayloader {

Ok(payloads)
}

fn clone_to(&self) -> Box<dyn Payloader + Send + Sync> {
Box::new(*self)
}
}
4 changes: 0 additions & 4 deletions src/codecs/h264/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ impl Payloader for H264Payloader {

Ok(payloads)
}

fn clone_to(&self) -> Box<dyn Payloader + Send + Sync> {
Box::new(self.clone())
}
}

/// H264Packet represents the H264 header that is stored in the payload of an RTP Packet
Expand Down
4 changes: 0 additions & 4 deletions src/codecs/opus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ impl Payloader for OpusPayloader {

Ok(vec![payload.clone()])
}

fn clone_to(&self) -> Box<dyn Payloader + Send + Sync> {
Box::new(*self)
}
}

/// OpusPacket represents the Opus header that is stored in the payload of an RTP Packet
Expand Down
4 changes: 0 additions & 4 deletions src/codecs/vp8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ impl Payloader for Vp8Payloader {

Ok(payloads)
}

fn clone_to(&self) -> Box<dyn Payloader + Send + Sync> {
Box::new(*self)
}
}

/// Vp8Packet represents the VP8 header that is stored in the payload of an RTP Packet
Expand Down
68 changes: 26 additions & 42 deletions src/codecs/vp9/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@ use crate::{
};

use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::fmt;
use std::sync::Arc;

/// Flexible mode 15 bit picture ID
const VP9HEADER_SIZE: usize = 3;
const MAX_SPATIAL_LAYERS: u8 = 5;
const MAX_VP9REF_PICS: usize = 3;

/// InitialPictureIDFn is a function that returns random initial picture ID.
pub type InitialPictureIDFn = Arc<dyn (Fn() -> u16) + Send + Sync>;

/// Vp9Payloader payloads VP9 packets
#[derive(Default, Clone)]
#[derive(Clone, Debug)]
pub struct Vp9Payloader {
picture_id: u16,
initialized: bool,
}

impl Vp9Payloader {
pub fn new() -> Self {
Self::new_with_id(rand::random::<u16>() & 0x7FFF)
}

pub initial_picture_id_fn: Option<InitialPictureIDFn>,
pub fn new_with_id(init_picture_id: u16) -> Self {
Self {
picture_id: init_picture_id,
}
}
}

impl fmt::Debug for Vp9Payloader {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Vp9Payloader")
.field("picture_id", &self.picture_id)
.field("initialized", &self.initialized)
.finish()
impl Default for Vp9Payloader {
fn default() -> Self {
Self::new()
}
}

Expand Down Expand Up @@ -81,19 +82,6 @@ impl Payloader for Vp9Payloader {
return Ok(vec![]);
}

if !self.initialized {
if self.initial_picture_id_fn.is_none() {
self.initial_picture_id_fn =
Some(Arc::new(|| -> u16 { rand::random::<u16>() & 0x7FFF }));
}
self.picture_id = if let Some(f) = &self.initial_picture_id_fn {
f()
} else {
0
};
self.initialized = true;
}

let max_fragment_size = mtu as isize - VP9HEADER_SIZE as isize;
let mut payloads = vec![];
let mut payload_data_remaining = payload.len();
Expand Down Expand Up @@ -135,10 +123,6 @@ impl Payloader for Vp9Payloader {

Ok(payloads)
}

fn clone_to(&self) -> Box<dyn Payloader + Send + Sync> {
Box::new(self.clone())
}
}

/// Vp9Packet represents the VP9 header that is stored in the payload of an RTP Packet
Expand Down Expand Up @@ -263,9 +247,9 @@ impl Vp9Packet {
// M: | EXTENDED PID |
// +-+-+-+-+-+-+-+-+
//
fn parse_picture_id(
fn parse_picture_id<B: Buf>(
&mut self,
reader: &mut dyn Buf,
reader: &mut B,
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
Expand All @@ -288,9 +272,9 @@ impl Vp9Packet {
Ok(payload_index)
}

fn parse_layer_info(
fn parse_layer_info<B: Buf>(
&mut self,
reader: &mut dyn Buf,
reader: &mut B,
mut payload_index: usize,
) -> Result<usize> {
payload_index = self.parse_layer_info_common(reader, payload_index)?;
Expand All @@ -308,9 +292,9 @@ impl Vp9Packet {
// L: | T |U| S |D|
// +-+-+-+-+-+-+-+-+
//
fn parse_layer_info_common(
fn parse_layer_info_common<B: Buf>(
&mut self,
reader: &mut dyn Buf,
reader: &mut B,
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
Expand Down Expand Up @@ -339,9 +323,9 @@ impl Vp9Packet {
// | tl0picidx |
// +-+-+-+-+-+-+-+-+
//
fn parse_layer_info_non_flexible_mode(
fn parse_layer_info_non_flexible_mode<B: Buf>(
&mut self,
reader: &mut dyn Buf,
reader: &mut B,
mut payload_index: usize,
) -> Result<usize> {
if reader.remaining() == 0 {
Expand All @@ -359,9 +343,9 @@ impl Vp9Packet {
// +-+-+-+-+-+-+-+-+ N=1: An additional P_DIFF follows
// current P_DIFF.
//
fn parse_ref_indices(
fn parse_ref_indices<B: Buf>(
&mut self,
reader: &mut dyn Buf,
reader: &mut B,
mut payload_index: usize,
) -> Result<usize> {
let mut b = 1u8;
Expand Down Expand Up @@ -401,7 +385,7 @@ impl Vp9Packet {
// | P_DIFF | (OPTIONAL) . R times .
// +-+-+-+-+-+-+-+-+ -| -|
//
fn parse_ssdata(&mut self, reader: &mut dyn Buf, mut payload_index: usize) -> Result<usize> {
fn parse_ssdata<B: Buf>(&mut self, reader: &mut B, mut payload_index: usize) -> Result<usize> {
if reader.remaining() == 0 {
return Err(Error::ErrShortPacket);
}
Expand Down
11 changes: 3 additions & 8 deletions src/codecs/vp9/vp9_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,7 @@ fn test_vp9_payloader_payload() -> Result<()> {
];

for (name, bs, mtu, expected) in tests {
let mut pck = Vp9Payloader {
initial_picture_id_fn: Some(Arc::new(|| -> u16 { 8692 })),
..Default::default()
};
let mut pck = Vp9Payloader::new_with_id(8692);

let mut actual = vec![];
for b in &bs {
Expand All @@ -309,10 +306,8 @@ fn test_vp9_payloader_payload() -> Result<()> {

//"PictureIDOverflow"
{
let mut pck = Vp9Payloader {
initial_picture_id_fn: Some(Arc::new(|| -> u16 { 8692 })),
..Default::default()
};
let mut pck = Vp9Payloader::new_with_id(8692);

let mut p_prev = Vp9Packet::default();
for i in 0..0x8000 {
let res = pck.payload(4, &Bytes::from_static(&[0x01]))?;
Expand Down
22 changes: 14 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ pub enum Error {
StapASizeLargerThanBuffer(usize, usize),
#[error("nalu type {0} is currently not handled")]
NaluTypeIsNotHandled(u8),
#[error("{0}")]
Util(#[from] util::Error),

// We box the util::Error and use a Box<str> to keep the size of this error to 24 bytes
#[error("{0}")]
Util(Box<util::Error>),
#[error("{0}")]
Other(String),
Other(Box<str>),
}

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

impl From<Error> for util::Error {
Expand All @@ -70,10 +77,9 @@ impl From<Error> for util::Error {

impl PartialEq<util::Error> for Error {
fn eq(&self, other: &util::Error) -> bool {
if let Some(down) = other.downcast_ref::<Error>() {
self == down
} else {
false
}
other
.downcast_ref::<Error>()
.map(|down| self == down)
.unwrap_or(false)
}
}
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(rust_2018_idioms)]
#![allow(dead_code)]
#![deny(rust_2018_idioms)]

pub mod codecs;
mod error;
Expand Down
2 changes: 2 additions & 0 deletions src/packet/packet_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]
Cassy343 marked this conversation as resolved.
Show resolved Hide resolved

use super::*;
use crate::error::Result;
use bytes::{Bytes, BytesMut};
Expand Down
Loading