Skip to content

Commit

Permalink
rave_rtp: use h264 unit type in
Browse files Browse the repository at this point in the history
  • Loading branch information
gerwin3 committed Aug 16, 2023
1 parent a60fd94 commit b231547
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
21 changes: 11 additions & 10 deletions .justfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
publish:
cargo publish -p rave_types
cargo publish -p rave_ops
cargo publish -p rave_ops_nvidia
cargo publish -p rave_h264
cargo publish -p rave_h264_nvidia
cargo publish -p rave_mp4
cargo publish -p rave_sdp
cargo publish -p rave_rtp
cargo publish -p rave_rtsp
cargo publish -p rave
# TODO: maybe we do it like this in the future but for now simple path dependencies
# cargo publish -p rave_types
# cargo publish -p rave_ops
# cargo publish -p rave_ops_nvidia
# cargo publish -p rave_h264
# cargo publish -p rave_h264_nvidia
# cargo publish -p rave_mp4
# cargo publish -p rave_sdp
# cargo publish -p rave_rtp
# cargo publish -p rave_rtsp
cargo publish -p rave
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ base64 = "0.21"
bytes = { version = "1.4" }
http = "0.2"
rand = { version = "0.8" }
rave_types = { version = "0.1.1" }
rave_types = { path = "src/rave_types" }
tokio-util = { version = "0.7", default-features = false, features = ["codec"] }
1 change: 1 addition & 0 deletions src/rave_rtp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ authors.workspace = true
[dependencies]
bytes = { workspace = true }
rand = { workspace = true }
rave_types = { workspace = true }
32 changes: 20 additions & 12 deletions src/rave_rtp/src/packetization/h264.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::packetization::common::{PacketizationParameters, Packetizer};

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

use rave_types::codec::H264;
use rave_types::unit::Unit;

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

/// RTP H264 packetizer.
Expand Down Expand Up @@ -65,7 +68,7 @@ impl H264Packetizer {
///
/// Zero or more RTP packets.
#[inline]
pub fn packetize(&mut self, data: Vec<Bytes>, timestamp: u32) -> Result<Vec<Packet>> {
pub fn packetize(&mut self, data: Vec<Unit<H264>>, timestamp: u32) -> Result<Vec<Packet>> {
self.inner.packetize(data, timestamp)
}
}
Expand All @@ -89,7 +92,7 @@ pub trait H264Packetize {
///
/// No packets may be returned even if valid data was passed. More than one packet may be
/// produced if the data is fragmented over multiple packets to fit within the configured MTU.
fn packetize(&mut self, data: Vec<Bytes>, timestamp: u32) -> Result<Vec<Packet>>;
fn packetize(&mut self, data: Vec<Unit<H264>>, timestamp: u32) -> Result<Vec<Packet>>;
}

/// Single NAL unit mode H264 packetizer.
Expand Down Expand Up @@ -134,16 +137,19 @@ impl H264Packetize for H264PacketizerMode0 {
/// # Return value
///
/// Zero or more RTP packets.
fn packetize(&mut self, data: Vec<Bytes>, timestamp: u32) -> Result<Vec<Packet>> {
fn packetize(&mut self, data: Vec<Unit<H264>>, timestamp: u32) -> Result<Vec<Packet>> {
let num_packets = data.len();
data.into_iter()
.enumerate()
.map(|(i, nal_unit)| {
// Since the caller must call this function exactly once per access unit, we can
// reliably set the marker bit on the last packet.
let is_last_packet_in_access_unit = i == num_packets - 1;
self.inner
.packetize(nal_unit, timestamp, is_last_packet_in_access_unit)
self.inner.packetize(
nal_unit.into_data(),
timestamp,
is_last_packet_in_access_unit,
)
})
.collect()
}
Expand Down Expand Up @@ -259,7 +265,6 @@ impl H264PacketizerMode1 {
})?);
payload.put(nal_unit);
}

Ok(payload.into())
}
}
Expand All @@ -286,10 +291,13 @@ impl H264Packetize for H264PacketizerMode1 {
/// # Return value
///
/// Zero or more packets.
fn packetize(&mut self, data: Vec<Bytes>, timestamp: u32) -> Result<Vec<Packet>> {
fn packetize(&mut self, data: Vec<Unit<H264>>, timestamp: u32) -> Result<Vec<Packet>> {
let data = data
.into_iter()
.map(|nal_unit| nal_unit.into_data())
.collect();
if let Some(mtu) = self.mtu {
let mut packets: Vec<Packet> = Vec::new();

let groups = self.group_nal_units(data, mtu);
let groups_len = groups.len();
for (i, group) in groups.into_iter().enumerate() {
Expand Down Expand Up @@ -391,7 +399,7 @@ impl H264Depacketizer {
///
/// No NAL units may be produced if the packet contains part of a fragmented unit. More packets
/// may be produced if the RTP packet payload is an aggregation packet (STAP or MTAP).
pub fn depacketize(&mut self, packet: &Packet) -> Result<Vec<Bytes>> {
pub fn depacketize(&mut self, packet: &Packet) -> Result<Vec<Unit<H264>>> {
if packet.payload.len() <= 1 {
return Err(Error::H264NalUnitLengthTooSmall {
len: packet.payload.len(),
Expand All @@ -403,7 +411,7 @@ impl H264Depacketizer {
// NAL
1..=23 => {
// This is just a normal NAL unit and can be passed on to the decoder as is.
Ok(vec![packet.payload.clone()])
Ok(vec![Unit::new(packet.payload.clone())])
}
// STAP-A
24 => {
Expand All @@ -424,7 +432,7 @@ impl H264Depacketizer {
need: nal_unit_length,
}));
}
Some(Ok(payload.copy_to_bytes(nal_unit_length)))
Some(Ok(Unit::new(payload.copy_to_bytes(nal_unit_length))))
} else {
None
}
Expand Down Expand Up @@ -495,7 +503,7 @@ impl H264Depacketizer {
let mut nal_unit = BytesMut::new();
nal_unit.put_u8(nal_unit_type);
nal_unit.put(recovered_nal_unit_payload);
Ok(vec![nal_unit.freeze()])
Ok(vec![Unit::new(nal_unit.freeze())])
} else {
Ok(Vec::new())
}
Expand Down
1 change: 1 addition & 0 deletions src/rave_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ repository.workspace = true
authors.workspace = true

[dependencies]
bytes = { workspace = true }
6 changes: 6 additions & 0 deletions src/rave_types/src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use bytes::Bytes;

pub trait Codec {
const ID: &'static str;

type Data;
}

pub struct H264;

impl Codec for H264 {
const ID: &'static str = "h264";

type Data = Bytes;
}
14 changes: 7 additions & 7 deletions src/rave_types/src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
use crate::codec::Codec;

pub struct Unit<C: Codec> {
pub data: Vec<u8>,
_phantom: std::marker::PhantomData<C>,
pub data: C::Data,
}

impl<C: Codec> Unit<C> {
pub fn new(data: Vec<u8>) -> Self {
Self {
data,
_phantom: Default::default(),
}
pub fn new(data: C::Data) -> Self {
Self { data }
}

pub fn into_data(self) -> C::Data {
self.data
}
}

0 comments on commit b231547

Please sign in to comment.