Skip to main content

rtci_rtp/codec/g7xx/
mod.rs

1#[cfg(test)]
2mod g7xx_test;
3
4use crate::packetizer::{MarkerRule, Payloader};
5use shared::error::Result;
6
7use bytes::Bytes;
8
9/// G711Payloader payloads G711 packets
10pub type G711Payloader = G7xxPayloader;
11/// G722Payloader payloads G722 packets
12pub type G722Payloader = G7xxPayloader;
13
14/// The static payload type of PCMU, G.711 mu-law at 8000 Hz (RFC 3551 section 6, Table 4).
15pub const PCMU_PAYLOAD_TYPE: u8 = 0;
16/// The static payload type of PCMA, G.711 A-law at 8000 Hz (RFC 3551 section 6, Table 4).
17pub const PCMA_PAYLOAD_TYPE: u8 = 8;
18/// The static payload type of G722 (RFC 3551 section 6, Table 4).
19pub const G722_PAYLOAD_TYPE: u8 = 9;
20/// The RTP clock rate of PCMU, PCMA and G722. G.722 samples at 16000 Hz, and its RTP clock is
21/// 8000 Hz all the same (RFC 3551 section 4.5.2).
22pub const G7XX_CLOCK_RATE: u32 = 8000;
23
24#[derive(Default, Debug, Copy, Clone)]
25/// Packetizes G.711/G.722 audio (RFC 3551 sections 4.5.2 and 4.5.14).
26///
27/// These are sample-based encodings (RFC 3551 section 4.3): a packet holds any whole number of
28/// octets, so a buffer larger than the MTU is cut at the MTU and nothing else is needed. One
29/// octet is one tick of the 8000 Hz RTP clock for one channel (a G.711 sample; half a G.722
30/// sample pair), which is what gives each of those packets its own timestamp.
31pub struct G7xxPayloader;
32
33impl Payloader for G7xxPayloader {
34    /// Payload fragments an G7xx packet across one or more byte arrays
35    fn payload(&mut self, mtu: usize, payload: &Bytes) -> Result<Vec<Bytes>> {
36        if payload.is_empty() || mtu == 0 {
37            return Ok(vec![]);
38        }
39
40        let mut payload_data_remaining = payload.len();
41        let mut payload_data_index = 0;
42        let mut payloads = Vec::with_capacity(payload_data_remaining / mtu);
43        while payload_data_remaining > 0 {
44            let current_fragment_size = std::cmp::min(mtu, payload_data_remaining);
45            payloads.push(
46                payload.slice(payload_data_index..payload_data_index + current_fragment_size),
47            );
48
49            payload_data_remaining -= current_fragment_size;
50            payload_data_index += current_fragment_size;
51        }
52
53        Ok(payloads)
54    }
55
56    /// Audio: the marker is for the first packet of a talkspurt (RFC 3551 section 4.1).
57    fn marker_rule(&self) -> MarkerRule {
58        MarkerRule::FirstPacketOfTalkspurt
59    }
60
61    /// One octet per tick of the 8000 Hz clock (single channel).
62    fn payload_duration(&self, payload: &Bytes) -> Option<u32> {
63        u32::try_from(payload.len()).ok()
64    }
65
66    fn clone_to(&self) -> Box<dyn Payloader> {
67        Box::new(*self)
68    }
69}