Skip to main content

rtc_rtp/codec/opus/
mod.rs

1#[cfg(test)]
2mod opus_test;
3
4use crate::packetizer::{Depacketizer, Payloader};
5use shared::error::{Error, Result};
6
7use bytes::Bytes;
8
9#[derive(Default, Debug, Copy, Clone)]
10/// Packetizes Opus audio: one RTP payload per Opus frame, never fragmented.
11pub struct OpusPayloader;
12
13impl Payloader for OpusPayloader {
14    fn payload(&mut self, mtu: usize, payload: &Bytes) -> Result<Vec<Bytes>> {
15        if payload.is_empty() || mtu == 0 {
16            return Ok(vec![]);
17        }
18
19        Ok(vec![payload.clone()])
20    }
21
22    fn clone_to(&self) -> Box<dyn Payloader> {
23        Box::new(*self)
24    }
25}
26
27/// OpusPacket represents the Opus header that is stored in the payload of an RTP Packet
28#[derive(PartialEq, Eq, Debug, Default, Clone)]
29pub struct OpusPacket;
30
31impl Depacketizer for OpusPacket {
32    fn depacketize(&mut self, packet: &Bytes) -> Result<Bytes> {
33        if packet.is_empty() {
34            Err(Error::ErrShortPacket)
35        } else {
36            Ok(packet.clone())
37        }
38    }
39
40    fn is_partition_head(&self, _payload: &Bytes) -> bool {
41        true
42    }
43
44    fn is_partition_tail(&self, _marker: bool, _payload: &Bytes) -> bool {
45        true
46    }
47}