Skip to content

Commit

Permalink
peer_connection: Pass packet to handle_unknown_rtp_packet
Browse files Browse the repository at this point in the history
More robust when used with interceptors that modify read packets.
  • Loading branch information
anders-avos committed Oct 28, 2024
1 parent a1611af commit 4fe9221
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
8 changes: 3 additions & 5 deletions webrtc/src/peer_connection/peer_connection_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,17 +1050,15 @@ impl PeerConnectionInternal {

let mut buf = vec![0u8; self.setting_engine.get_receive_mtu()];
let n = rtp_stream.read(&mut buf).await?;
let mut b = &buf[..n];

let packet = rtp::packet::Packet::unmarshal(&mut &buf[..n]).unwrap();
let (mut mid, mut rid, mut rsid, payload_type) = handle_unknown_rtp_packet(
b,
&packet.header,
mid_extension_id as u8,
sid_extension_id as u8,
rsid_extension_id as u8,
)?;

let packet = rtp::packet::Packet::unmarshal(&mut b).unwrap();

// TODO: Can we have attributes on the first packets?
buffered_packets.push_back((packet, Attributes::new()));

Expand Down Expand Up @@ -1092,7 +1090,7 @@ impl PeerConnectionInternal {
if mid.is_empty() || (rid.is_empty() && rsid.is_empty()) {
let (pkt, _) = rtp_interceptor.read(&mut buf, &a).await?;
let (m, r, rs, _) = handle_unknown_rtp_packet(
&buf[..n],
&pkt.header,
mid_extension_id as u8,
sid_extension_id as u8,
rsid_extension_id as u8,
Expand Down
16 changes: 6 additions & 10 deletions webrtc/src/rtp_transceiver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use portable_atomic::{AtomicBool, AtomicU8};
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
use tokio::sync::{Mutex, OnceCell};
use util::Unmarshal;

use crate::api::media_engine::MediaEngine;
use crate::error::{Error, Result};
Expand Down Expand Up @@ -527,33 +526,30 @@ pub(crate) async fn satisfy_type_and_direction(
/// handle_unknown_rtp_packet consumes a single RTP Packet and returns information that is helpful
/// for demuxing and handling an unknown SSRC (usually for Simulcast)
pub(crate) fn handle_unknown_rtp_packet(
buf: &[u8],
header: &rtp::header::Header,
mid_extension_id: u8,
sid_extension_id: u8,
rsid_extension_id: u8,
) -> Result<(String, String, String, PayloadType)> {
let mut reader = buf;
let rp = rtp::packet::Packet::unmarshal(&mut reader)?;

if !rp.header.extension {
if !header.extension {
return Ok((String::new(), String::new(), String::new(), 0));
}

let payload_type = rp.header.payload_type;
let payload_type = header.payload_type;

let mid = if let Some(payload) = rp.header.get_extension(mid_extension_id) {
let mid = if let Some(payload) = header.get_extension(mid_extension_id) {
String::from_utf8(payload.to_vec())?
} else {
String::new()
};

let rid = if let Some(payload) = rp.header.get_extension(sid_extension_id) {
let rid = if let Some(payload) = header.get_extension(sid_extension_id) {
String::from_utf8(payload.to_vec())?
} else {
String::new()
};

let srid = if let Some(payload) = rp.header.get_extension(rsid_extension_id) {
let srid = if let Some(payload) = header.get_extension(rsid_extension_id) {
String::from_utf8(payload.to_vec())?
} else {
String::new()
Expand Down

0 comments on commit 4fe9221

Please sign in to comment.