Skip to content

Commit 619a007

Browse files
HITGIFrainliu
authored andcommitted
more comments
1 parent 8d44405 commit 619a007

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

rtp/src/codecs/av1/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,40 @@ pub struct Av1Payloader {}
1616

1717
impl Payloader for Av1Payloader {
1818
/// Based on https://chromium.googlesource.com/external/webrtc/+/4e513346ec56c829b3a6010664998469fc237b35/modules/rtp_rtcp/source/rtp_packetizer_av1.cc
19+
/// Reference: https://aomediacodec.github.io/av1-rtp-spec/#45-payload-structure
1920
fn payload(&mut self, mtu: usize, payload: &Bytes) -> crate::error::Result<Vec<Bytes>> {
21+
// example:
22+
//
23+
// 0 1 2 3
24+
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
25+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26+
// |Z|Y|1 0|N|-|-|-| OBU element 1 size (leb128) | |
27+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
28+
// | |
29+
// : :
30+
// : OBU element 1 data :
31+
// : :
32+
// | |
33+
// | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34+
// | | |
35+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
36+
// | |
37+
// : :
38+
// : OBU element 2 data :
39+
// : :
40+
// | |
41+
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42+
43+
// Parse the payload into series of OBUs.
2044
let obus = parse_obus(payload)?;
45+
46+
// Packetize the OBUs, possibly aggregating multiple OBUs into a single packet,
47+
// or splitting a single OBU across multiple packets.
2148
let packets_metadata = packetize(&obus, mtu);
49+
2250
let mut payloads = vec![];
2351

52+
// Split the payload into RTP packets according to the packetization scheme.
2453
for packet_index in 0..packets_metadata.len() {
2554
let packet = &packets_metadata[packet_index];
2655
let mut obu_offset = packet.first_obu_offset;
@@ -83,6 +112,7 @@ impl Payloader for Av1Payloader {
83112

84113
payloads.push(out.freeze());
85114
}
115+
86116
Ok(payloads)
87117
}
88118

rtp/src/codecs/av1/obu.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/// Based on https://chromium.googlesource.com/external/webrtc/+/4e513346ec56c829b3a6010664998469fc237b35/modules/rtp_rtcp/source/rtp_packetizer_av1.cc
1+
//! Based on https://chromium.googlesource.com/external/webrtc/+/4e513346ec56c829b3a6010664998469fc237b35/modules/rtp_rtcp/source/rtp_packetizer_av1.cc
2+
//! Reference: https://aomediacodec.github.io/av1-spec/#obu-syntax
3+
24
use bytes::Bytes;
35

46
use crate::codecs::av1::leb128::read_leb128;
@@ -18,6 +20,7 @@ pub struct Obu {
1820
pub header: u8,
1921
pub extension_header: u8,
2022
pub payload: Bytes,
23+
/// size of the header and payload combined.
2124
pub size: usize,
2225
}
2326

@@ -31,7 +34,8 @@ impl Obu {
3134
}
3235
}
3336

34-
/// Parses the raw payload into a list of OBU elements.
37+
/// Parses the payload into series of OBUs.
38+
/// Reference: https://aomediacodec.github.io/av1-spec/#obu-syntax
3539
pub fn parse_obus(payload: &Bytes) -> Result<Vec<Obu>> {
3640
let mut obus = vec![];
3741
let mut payload_data_remaining = payload.len() as isize;

rtp/src/codecs/av1/packetizer.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/// Based on https://chromium.googlesource.com/external/webrtc/+/4e513346ec56c829b3a6010664998469fc237b35/modules/rtp_rtcp/source/rtp_packetizer_av1.cc
1+
//! Based on https://chromium.googlesource.com/external/webrtc/+/4e513346ec56c829b3a6010664998469fc237b35/modules/rtp_rtcp/source/rtp_packetizer_av1.cc
2+
//! Reference: https://aomediacodec.github.io/av1-rtp-spec
3+
24
use std::cmp::min;
35

46
use crate::codecs::av1::leb128::leb128_size;
57
use crate::codecs::av1::obu::{obu_type, Obu, OBU_TYPE_SEQUENCE_HEADER};
68

7-
// When there are 3 or less OBU (fragments) in a packet, size of the last one
8-
// can be omitted.
9+
/// When there are 3 or less OBU (fragments) in a packet, size of the last one
10+
/// can be omitted.
911
pub const MAX_NUM_OBUS_TO_OMIT_SIZE: usize = 3;
1012
pub const AGGREGATION_HEADER_SIZE: usize = 1;
1113

@@ -14,6 +16,7 @@ pub struct PacketMetadata {
1416
pub num_obu_elements: usize,
1517
pub first_obu_offset: usize,
1618
pub last_obu_size: usize,
19+
/// Total size consumed by the packet.
1720
pub packet_size: usize,
1821
}
1922

@@ -30,6 +33,8 @@ impl PacketMetadata {
3033
}
3134

3235
/// Returns the instructions for how to packetize the OBU elements into RTP packets.
36+
/// Reference: https://aomediacodec.github.io/av1-rtp-spec/#45-payload-structure
37+
/// https://aomediacodec.github.io/av1-rtp-spec/#5-packetization-rules
3338
pub fn packetize(obus: &Vec<Obu>, mtu: usize) -> Vec<PacketMetadata> {
3439
if obus.is_empty() {
3540
return vec![];
@@ -169,6 +174,8 @@ pub fn packetize(obus: &Vec<Obu>, mtu: usize) -> Vec<PacketMetadata> {
169174
packets
170175
}
171176

177+
/// Returns the aggregation header for the packet.
178+
/// Reference: https://aomediacodec.github.io/av1-rtp-spec/#44-av1-aggregation-header
172179
pub fn get_aggregation_header(
173180
obus: &Vec<Obu>,
174181
packets: &Vec<PacketMetadata>,
@@ -210,6 +217,8 @@ pub fn get_aggregation_header(
210217
header
211218
}
212219

220+
/// Returns the number of additional bytes needed to store the previous OBU
221+
/// element if an additional OBU element is added to the packet.
213222
fn additional_bytes_for_previous_obu_element(packet: &PacketMetadata) -> usize {
214223
if packet.packet_size == 0 {
215224
// Packet is still empty => no last OBU element, no need to reserve space
@@ -225,6 +234,9 @@ fn additional_bytes_for_previous_obu_element(packet: &PacketMetadata) -> usize {
225234
}
226235
}
227236

237+
/// Given |remaining_bytes| free bytes left in a packet, returns max size of an
238+
/// OBU fragment that can fit into the packet.
239+
/// i.e. MaxFragmentSize + Leb128Size(MaxFragmentSize) <= remaining_bytes.
228240
fn max_fragment_size(remaining_bytes: usize) -> usize {
229241
if remaining_bytes <= 1 {
230242
return 0;

0 commit comments

Comments
 (0)