Skip to content

Commit

Permalink
impl RTCHandler for demuxer
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 24, 2024
1 parent 58fa6fa commit cd660f2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 48 deletions.
72 changes: 25 additions & 47 deletions rtc/src/handlers/demuxer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::handlers::RTCHandler;
use crate::messages::{DTLSMessageEvent, RTCMessageEvent, RTPMessageEvent, STUNMessageEvent};
use crate::messages::{DTLSMessage, RTCMessage, RTPMessage, STUNMessage};
use log::{debug, error};
use shared::Transmit;

Expand Down Expand Up @@ -39,76 +39,54 @@ fn match_srtp(b: &[u8]) -> bool {

/// DemuxerHandler implements demuxing of STUN/DTLS/RTP/RTCP Protocol packets
#[derive(Default)]
pub struct DemuxerHandler {
next: Option<Box<dyn RTCHandler>>,
}
pub struct Demuxer;

impl DemuxerHandler {
impl Demuxer {
pub fn new() -> Self {
DemuxerHandler::default()
Demuxer
}
}

impl RTCHandler for DemuxerHandler {
fn chain(mut self: Box<Self>, next: Box<dyn RTCHandler>) -> Box<dyn RTCHandler> {
self.next = Some(next);
self
}

fn next(&mut self) -> Option<&mut Box<dyn RTCHandler>> {
self.next.as_mut()
}

fn handle_transmit(&mut self, msg: Transmit<RTCMessageEvent>) {
if let RTCMessageEvent::Raw(message) = msg.message {
impl RTCHandler for Demuxer {
fn handle_transmit(&mut self, msg: Transmit<RTCMessage>) -> Vec<Transmit<RTCMessage>> {
if let RTCMessage::Raw(message) = msg.message {
if message.is_empty() {
error!("drop invalid packet due to zero length");
return;
}

let next_msg = if match_dtls(&message) {
Transmit {
vec![]
} else if match_dtls(&message) {
vec![Transmit {
now: msg.now,
transport: msg.transport,
message: RTCMessageEvent::Dtls(DTLSMessageEvent::Raw(message)),
}
message: RTCMessage::Dtls(DTLSMessage::Raw(message)),
}]
} else if match_srtp(&message) {
Transmit {
vec![Transmit {
now: msg.now,
transport: msg.transport,
message: RTCMessageEvent::Rtp(RTPMessageEvent::Raw(message)),
}
message: RTCMessage::Rtp(RTPMessage::Raw(message)),
}]
} else {
Transmit {
vec![Transmit {
now: msg.now,
transport: msg.transport,
message: RTCMessageEvent::Stun(STUNMessageEvent::Raw(message)),
}
};

if let Some(next) = self.next() {
next.handle_transmit(next_msg);
message: RTCMessage::Stun(STUNMessage::Raw(message)),
}]
}
} else {
debug!("drop non-RAW packet {:?}", msg.message);
vec![]
}
}

fn poll_transmit(&mut self) -> Option<Transmit<RTCMessageEvent>> {
let transmit = if let Some(next) = self.next() {
next.poll_transmit()
} else {
None
};

if let Some(msg) = transmit {
fn poll_transmit(&mut self, msg: Option<Transmit<RTCMessage>>) -> Option<Transmit<RTCMessage>> {
if let Some(msg) = msg {
match msg.message {
RTCMessageEvent::Stun(STUNMessageEvent::Raw(message))
| RTCMessageEvent::Dtls(DTLSMessageEvent::Raw(message))
| RTCMessageEvent::Rtp(RTPMessageEvent::Raw(message)) => Some(Transmit {
RTCMessage::Stun(STUNMessage::Raw(message))
| RTCMessage::Dtls(DTLSMessage::Raw(message))
| RTCMessage::Rtp(RTPMessage::Raw(message)) => Some(Transmit {
now: msg.now,
transport: msg.transport,
message: RTCMessageEvent::Raw(message),
message: RTCMessage::Raw(message),
}),
_ => {
debug!("drop non-RAW packet {:?}", msg.message);
Expand Down
2 changes: 1 addition & 1 deletion rtc/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use shared::error::Error;
use shared::Transmit;
use std::time::Instant;

//pub mod demuxer;
pub mod demuxer;
pub mod dtls;
//pub mod stun;

Expand Down

0 comments on commit cd660f2

Please sign in to comment.