Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add marker bit when packetizing opus. #572

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use std::panic::UnwindSafe;
use thiserror::Error;

use crate::format::Codec;
use crate::format::{Codec, CodecSpec};
use crate::sdp::MediaType;

mod g7xx;
Expand Down Expand Up @@ -206,10 +206,12 @@ pub(crate) enum CodecDepacketizer {
Boxed(Box<dyn Depacketizer + Send + Sync + UnwindSafe>),
}

impl From<Codec> for CodecPacketizer {
fn from(c: Codec) -> Self {
match c {
Codec::Opus => CodecPacketizer::Opus(OpusPacketizer),
impl From<CodecSpec> for CodecPacketizer {
fn from(c: CodecSpec) -> Self {
match c.codec {
Codec::Opus => {
CodecPacketizer::Opus(OpusPacketizer::new(c.format.use_dtx.unwrap_or_default()))
}
Codec::H264 => CodecPacketizer::H264(H264Packetizer::default()),
Codec::H265 => unimplemented!("Missing packetizer for H265"),
Codec::Vp8 => CodecPacketizer::Vp8(Vp8Packetizer::default()),
Expand Down
78 changes: 73 additions & 5 deletions src/packet/opus.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use super::{CodecExtra, Depacketizer, MediaKind, PacketError, Packetizer};

/// Packetizes Opus RTP packets.
#[derive(Default, Debug, Copy, Clone)]
pub struct OpusPacketizer;
#[derive(Debug, Copy, Clone)]
pub struct OpusPacketizer {
// stores if a marker was previously set
marker: bool,
use_dtx: bool,
}

impl OpusPacketizer {
pub fn new(use_dtx: bool) -> Self {
Self {
marker: false,
use_dtx,
}
}
}

impl Packetizer for OpusPacketizer {
fn packetize(&mut self, mtu: usize, payload: &[u8]) -> Result<Vec<Vec<u8>>, PacketError> {
Expand All @@ -23,8 +36,26 @@ impl Packetizer for OpusPacketizer {
}

fn is_marker(&mut self, data: &[u8], previous: Option<&[u8]>, last: bool) -> bool {
// TODO: dtx
false
if !self.use_dtx {
return false;
}
// any non silenced packet would generally have more than 2 byts
davibe marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/byts/bytes/

let mut is_marker = data.len() > 2;

match self.marker {
true => {
if !is_marker {
self.marker = false;
}
is_marker = false;
}
false => {
if is_marker {
self.marker = true;
}
}
}
is_marker
}
}

Expand Down Expand Up @@ -84,7 +115,7 @@ mod test {

#[test]
fn test_opus_payload() -> Result<(), PacketError> {
let mut pck = OpusPacketizer;
let mut pck = OpusPacketizer::new(true);
let empty = &[];
let payload = &[0x90, 0x90, 0x90];

Expand All @@ -103,6 +134,43 @@ mod test {
Ok(())
}

#[test]
fn test_opus_packetizer_dtx() -> Result<(), PacketError> {
// packetizer with dtx on
let mut pck = OpusPacketizer::new(true);

let payload = &[0x90, 0x90, 0x90];

// Start of talking spurt, marker bit is set.
let is_marker = pck.is_marker(payload, None, false);
assert!(is_marker);
assert!(pck.marker);

// More talking so is_marker should be false.
let is_marker = pck.is_marker(payload, None, false);
assert!(!is_marker);
assert!(pck.marker);

// silence packet inserted, internal packetizer state should be reset.
let is_marker = pck.is_marker(&[], None, false);
assert!(!is_marker);
assert!(!pck.marker);

// talking start again, marker bit is set.
let is_marker = pck.is_marker(payload, None, false);
assert!(is_marker);
assert!(pck.marker);

let mut pck = OpusPacketizer::new(false);

// Start of talking spurt, since dtx is false marker should be false
let is_marker = pck.is_marker(payload, None, false);
assert!(!is_marker);
assert!(!pck.marker);

Ok(())
}

#[test]
fn test_opus_is_partition_head() -> Result<(), PacketError> {
let opus = OpusDepacketizer;
Expand Down
2 changes: 1 addition & 1 deletion src/packet/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Payloader {
impl Payloader {
pub(crate) fn new(spec: CodecSpec) -> Self {
Payloader {
pack: spec.codec.into(),
pack: spec.into(),
clock_rate: spec.clock_rate,
}
}
Expand Down
110 changes: 0 additions & 110 deletions tests/rtp_to_frame.rs

This file was deleted.

Loading