-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yngrtc
committed
Jan 22, 2024
1 parent
ee8bc1d
commit 5952b27
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
use crate::messages::{MessageEvent, RTPMessageEvent, TaggedMessageEvent}; | ||
use log::debug; | ||
use retty::channel::{Handler, InboundContext, InboundHandler, OutboundContext, OutboundHandler}; | ||
|
||
#[derive(Default)] | ||
struct RtcpInbound; | ||
#[derive(Default)] | ||
struct RtcpOutbound; | ||
#[derive(Default)] | ||
pub struct RtcpHandler { | ||
rtcp_inbound: RtcpInbound, | ||
rtcp_outbound: RtcpOutbound, | ||
} | ||
|
||
impl RtcpHandler { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
} | ||
|
||
impl InboundHandler for RtcpInbound { | ||
type Rin = TaggedMessageEvent; | ||
type Rout = Self::Rin; | ||
|
||
fn read(&mut self, ctx: &InboundContext<Self::Rin, Self::Rout>, msg: Self::Rin) { | ||
if let MessageEvent::RTP(RTPMessageEvent::RTCP(rtcp_messages)) = &msg.message { | ||
if let Some(rtcp_message) = rtcp_messages.first() { | ||
debug!("rtcp read {:?}", rtcp_message.header()); | ||
} | ||
} | ||
|
||
ctx.fire_read(msg); | ||
} | ||
} | ||
|
||
impl OutboundHandler for RtcpOutbound { | ||
type Win = TaggedMessageEvent; | ||
type Wout = Self::Win; | ||
|
||
fn write(&mut self, ctx: &OutboundContext<Self::Win, Self::Wout>, msg: Self::Win) { | ||
if let MessageEvent::RTP(RTPMessageEvent::RTCP(rtcp_messages)) = &msg.message { | ||
if let Some(rtcp_message) = rtcp_messages.first() { | ||
debug!("rtcp write {:?}", rtcp_message.header()); | ||
} | ||
} | ||
|
||
ctx.fire_write(msg); | ||
} | ||
} | ||
|
||
impl Handler for RtcpHandler { | ||
type Rin = TaggedMessageEvent; | ||
type Rout = Self::Rin; | ||
type Win = TaggedMessageEvent; | ||
type Wout = Self::Win; | ||
|
||
fn name(&self) -> &str { | ||
"RtcpHandler" | ||
} | ||
|
||
fn split( | ||
self, | ||
) -> ( | ||
Box<dyn InboundHandler<Rin = Self::Rin, Rout = Self::Rout>>, | ||
Box<dyn OutboundHandler<Win = Self::Win, Wout = Self::Wout>>, | ||
) { | ||
(Box::new(self.rtcp_inbound), Box::new(self.rtcp_outbound)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,118 @@ | ||
use crate::messages::{MessageEvent, RTPMessageEvent, TaggedMessageEvent}; | ||
use log::{debug, error}; | ||
use retty::channel::{Handler, InboundContext, InboundHandler, OutboundContext, OutboundHandler}; | ||
use shared::{ | ||
marshal::{Marshal, Unmarshal}, | ||
util::is_rtcp, | ||
}; | ||
|
||
#[derive(Default)] | ||
struct RtpInbound; | ||
#[derive(Default)] | ||
struct RtpOutbound; | ||
#[derive(Default)] | ||
pub struct RtpHandler { | ||
rtp_inbound: RtpInbound, | ||
rtp_outbound: RtpOutbound, | ||
} | ||
|
||
impl RtpHandler { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
} | ||
|
||
impl InboundHandler for RtpInbound { | ||
type Rin = TaggedMessageEvent; | ||
type Rout = Self::Rin; | ||
|
||
fn read(&mut self, ctx: &InboundContext<Self::Rin, Self::Rout>, mut msg: Self::Rin) { | ||
debug!("rtp read {:?}", msg.transport.peer_addr); | ||
if let MessageEvent::RTP(RTPMessageEvent::RAW(mut message)) = msg.message { | ||
if is_rtcp(&message) { | ||
match rtcp::packet::unmarshal(&mut message) { | ||
Ok(rtcp_packet) => { | ||
msg.message = MessageEvent::RTP(RTPMessageEvent::RTCP(rtcp_packet)); | ||
ctx.fire_read(msg); | ||
} | ||
Err(err) => { | ||
error!("try_read with error {}", err); | ||
ctx.fire_read_exception(Box::new(err)) | ||
} | ||
} | ||
} else { | ||
match rtp::Packet::unmarshal(&mut message) { | ||
Ok(rtp_packet) => { | ||
msg.message = MessageEvent::RTP(RTPMessageEvent::RTP(rtp_packet)); | ||
ctx.fire_read(msg); | ||
} | ||
Err(err) => { | ||
error!("try_read with error {}", err); | ||
ctx.fire_read_exception(Box::new(err)) | ||
} | ||
} | ||
} | ||
} else { | ||
debug!("bypass rtp read {:?}", msg.transport.peer_addr); | ||
ctx.fire_read(msg); | ||
} | ||
} | ||
} | ||
|
||
impl OutboundHandler for RtpOutbound { | ||
type Win = TaggedMessageEvent; | ||
type Wout = Self::Win; | ||
|
||
fn write(&mut self, ctx: &OutboundContext<Self::Win, Self::Wout>, mut msg: Self::Win) { | ||
debug!("rtp write {:?}", msg.transport.peer_addr); | ||
match msg.message { | ||
MessageEvent::RTP(RTPMessageEvent::RTCP(rtcp_message)) => { | ||
match rtcp::packet::marshal(&rtcp_message) { | ||
Ok(message) => { | ||
msg.message = MessageEvent::RTP(RTPMessageEvent::RAW(message)); | ||
ctx.fire_write(msg); | ||
} | ||
Err(err) => { | ||
error!("try_write with error {}", err); | ||
ctx.fire_write_exception(Box::new(err)) | ||
} | ||
} | ||
} | ||
MessageEvent::RTP(RTPMessageEvent::RTP(rtp_message)) => match rtp_message.marshal() { | ||
Ok(message) => { | ||
msg.message = MessageEvent::RTP(RTPMessageEvent::RAW(message)); | ||
ctx.fire_write(msg); | ||
} | ||
Err(err) => { | ||
error!("try_write with error {}", err); | ||
ctx.fire_write_exception(Box::new(err)) | ||
} | ||
}, | ||
_ => { | ||
// Bypass | ||
debug!("Bypass rtp write {:?}", msg.transport.peer_addr); | ||
ctx.fire_write(msg); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Handler for RtpHandler { | ||
type Rin = TaggedMessageEvent; | ||
type Rout = Self::Rin; | ||
type Win = TaggedMessageEvent; | ||
type Wout = Self::Win; | ||
|
||
fn name(&self) -> &str { | ||
"RtpHandler" | ||
} | ||
|
||
fn split( | ||
self, | ||
) -> ( | ||
Box<dyn InboundHandler<Rin = Self::Rin, Rout = Self::Rout>>, | ||
Box<dyn OutboundHandler<Win = Self::Win, Wout = Self::Wout>>, | ||
) { | ||
(Box::new(self.rtp_inbound), Box::new(self.rtp_outbound)) | ||
} | ||
} |