Skip to content

Commit

Permalink
add RTCEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 24, 2024
1 parent e470d19 commit 3e94edf
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 60 deletions.
44 changes: 40 additions & 4 deletions rtc/src/api/setting_engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//TODO:#[cfg(test)]
//TODO:mod setting_engine_test;

use std::fmt;
use std::sync::Arc;
use std::time::Duration;

Expand All @@ -15,12 +16,12 @@ use crate::transport::dtls_transport::dtls_role::DTLSRole;
use crate::transport::ice_transport::ice_candidate_type::RTCIceCandidateType;
use shared::error::{Error, Result};

#[derive(Default, Clone)]
#[derive(Default, Debug, Clone)]
pub struct Detach {
pub data_channels: bool,
}

#[derive(Default, Clone)]
#[derive(Default, Debug, Clone)]
pub struct Timeout {
pub ice_disconnected_timeout: Option<Duration>,
pub ice_failed_timeout: Option<Duration>,
Expand All @@ -31,7 +32,7 @@ pub struct Timeout {
pub ice_relay_acceptance_min_wait: Option<Duration>,
}

#[derive(Default, Clone)]
#[derive(Default, Debug, Clone)]
pub struct Candidates {
pub ice_lite: bool,
pub ice_network_types: Vec<NetworkType>,
Expand All @@ -45,7 +46,7 @@ pub struct Candidates {
pub password: String,
}

#[derive(Default, Clone)]
#[derive(Default, Debug, Clone)]
pub struct ReplayProtection {
pub dtls: usize,
pub srtp: usize,
Expand Down Expand Up @@ -74,6 +75,41 @@ pub struct SettingEngine {
pub(crate) mid_generator: Option<Arc<dyn Fn(isize) -> String + Send + Sync>>,
}

impl fmt::Debug for SettingEngine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SettingEngine")
.field("detach", &self.detach)
.field("timeout", &self.timeout)
.field("candidates", &self.candidates)
.field("replay_protection", &self.replay_protection)
.field(
"sdp_media_level_fingerprints",
&self.sdp_media_level_fingerprints,
)
.field("answering_dtls_role", &self.answering_dtls_role)
.field(
"disable_certificate_fingerprint_verification",
&self.disable_certificate_fingerprint_verification,
)
.field(
"allow_insecure_verification_algorithm",
&self.allow_insecure_verification_algorithm,
)
.field(
"disable_srtp_replay_protection",
&self.disable_srtp_replay_protection,
)
.field(
"disable_srtcp_replay_protection",
&self.disable_srtcp_replay_protection,
)
.field("disable_media_engine_copy", &self.disable_media_engine_copy)
.field("srtp_protection_profiles", &self.srtp_protection_profiles)
.field("receive_mtu", &self.receive_mtu)
.finish()
}
}

impl SettingEngine {
/// get_receive_mtu returns the configured MTU. If SettingEngine's MTU is configured to 0 it returns the default
pub(crate) fn get_receive_mtu(&self) -> usize {
Expand Down
44 changes: 6 additions & 38 deletions rtc/src/handlers/dtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::sync::Arc;
use crate::api::setting_engine::SettingEngine;
use crate::handlers::RTCHandler;
use crate::messages::{DTLSMessageEvent, RTCMessageEvent};
use crate::transport::dtls_transport::RTCDtlsTransport;
use crate::transport::RTCTransport;
use dtls::endpoint::EndpointEvent;
use dtls::extension::extension_use_srtp::SrtpProtectionProfile;
Expand All @@ -16,44 +17,11 @@ use shared::Transmit;
use srtp::option::{srtcp_replay_protection, srtp_no_replay_protection, srtp_replay_protection};
use srtp::protection_profile::ProtectionProfile;

/// DtlsHandler implements DTLS Protocol handling
pub struct DtlsHandler<'a> {
next: Option<Box<dyn RTCHandler>>,

local_addr: SocketAddr,
setting_engine: Arc<SettingEngine>,
transport: &'a mut RTCTransport,
transmits: VecDeque<Transmit<RTCMessageEvent>>,
}

impl<'a> DtlsHandler<'a> {
pub fn new(
local_addr: SocketAddr,
setting_engine: Arc<SettingEngine>,
transport: &'a mut RTCTransport,
) -> Self {
DtlsHandler {
next: None,

local_addr,
setting_engine,
transport,
transmits: VecDeque::new(),
}
}
}

impl<'a> RTCHandler for DtlsHandler<'a> {
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>) {
impl RTCHandler for RTCDtlsTransport {
fn handle_transmit(
&mut self,
msg: Transmit<RTCMessageEvent>,
) -> Vec<Transmit<RTCMessageEvent>> {
let next_msgs = if let RTCMessageEvent::Dtls(DTLSMessageEvent::Raw(dtls_message)) =
msg.message
{
Expand Down
13 changes: 7 additions & 6 deletions rtc/src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::messages::RTCMessageEvent;
use crate::messages::{RTCEvent, RTCMessage};
use shared::error::Error;
use shared::Transmit;
use std::time::Instant;
Expand All @@ -9,15 +9,16 @@ use std::time::Instant;

pub trait RTCHandler {
/// Handles input message
fn handle_transmit(
&mut self,
msg: Transmit<RTCMessageEvent>,
) -> Vec<Transmit<RTCMessageEvent>> {
fn handle_transmit(&mut self, msg: Transmit<RTCMessage>) -> Vec<Transmit<RTCMessage>> {
vec![msg]
}

/// Polls output message from internal transmit queue
fn poll_transmit(&mut self) -> Option<Transmit<RTCMessageEvent>> {
fn poll_transmit(&mut self) -> Option<Transmit<RTCMessage>> {
None
}

fn poll_event(&mut self) -> Option<RTCEvent> {
None
}

Expand Down
30 changes: 21 additions & 9 deletions rtc/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use crate::transport::data_channel::DataChannelEvent;
use crate::transport::dtls_transport::DtlsTransportEvent;
use crate::transport::ice_transport::IceTransportEvent;
use crate::transport::sctp_transport::SctpTransportEvent;
use bytes::BytesMut;
use sctp::ReliabilityType;

Expand All @@ -17,7 +21,7 @@ pub(crate) struct DataChannelMessageParams {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum DataChannelEvent {
pub(crate) enum DataChannelPayload {
Open,
Message(BytesMut),
Close,
Expand All @@ -36,33 +40,41 @@ pub struct DataChannelMessage {
pub struct ApplicationMessage {
pub(crate) association_handle: usize,
pub(crate) stream_id: u16,
pub(crate) data_channel_event: DataChannelEvent,
pub(crate) data_channel_payload: DataChannelPayload,
}

#[derive(Debug)]
pub enum STUNMessageEvent {
pub enum STUNMessage {
Raw(BytesMut),
Stun(stun::message::Message),
}

#[derive(Debug)]
pub enum DTLSMessageEvent {
pub enum DTLSMessage {
Raw(BytesMut),
Sctp(DataChannelMessage),
DataChannel(ApplicationMessage),
}

#[derive(Debug)]
pub enum RTPMessageEvent {
pub enum RTPMessage {
Raw(BytesMut),
Rtp(rtp::packet::Packet),
Rtcp(Vec<Box<dyn rtcp::packet::Packet>>),
}

#[derive(Debug)]
pub enum RTCMessageEvent {
pub enum RTCMessage {
Raw(BytesMut),
Stun(STUNMessageEvent),
Dtls(DTLSMessageEvent),
Rtp(RTPMessageEvent),
Stun(STUNMessage),
Dtls(DTLSMessage),
Rtp(RTPMessage),
}

#[derive(Debug)]
pub enum RTCEvent {
DataChannelEvent(DataChannelEvent),
DtlsTransportEvent(DtlsTransportEvent),
IceTransportEvent(IceTransportEvent),
SctpTransportEvent(SctpTransportEvent),
}
3 changes: 2 additions & 1 deletion rtc/src/transport/data_channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::time::SystemTime;
/// message size limit for Chromium
const DATA_CHANNEL_BUFFER_SIZE: u16 = u16::MAX;

#[derive(Debug)]
pub enum DataChannelEvent {
OnMessage,
OnOpen,
Expand All @@ -35,7 +36,7 @@ pub enum DataChannelEvent {
/// DataChannel represents a WebRTC DataChannel
/// The DataChannel interface represents a network channel
/// which can be used for bidirectional peer-to-peer transfers of arbitrary data
#[derive(Default)]
#[derive(Default, Debug)]
pub struct RTCDataChannel {
pub(crate) stats_id: String,
pub(crate) label: String,
Expand Down
1 change: 1 addition & 0 deletions rtc/src/transport/dtls_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(crate) fn default_srtp_protection_profiles() -> Vec<SrtpProtectionProfile> {
]
}

#[derive(Debug)]
pub enum DtlsTransportEvent {
OnDtlsTransportStateChange(RTCDtlsTransportState),
}
Expand Down
1 change: 1 addition & 0 deletions rtc/src/transport/ice_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod ice_role;
pub mod ice_server;
pub mod ice_transport_state;

#[derive(Debug)]
pub enum IceTransportEvent {
OnConnectionStateChange(RTCIceTransportState),
OnSelectedCandidatePairChange(Box<RTCIceCandidatePair>),
Expand Down
5 changes: 3 additions & 2 deletions rtc/src/transport/sctp_transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use shared::error::*;

const SCTP_MAX_CHANNELS: u16 = u16::MAX;

#[derive(Debug)]
pub enum SctpTransportEvent {
OnError,
OnDataChannel(RTCDataChannel),
OnDataChannelOpened(RTCDataChannel),
OnDataChannel(Box<RTCDataChannel>),
OnDataChannelOpened(Box<RTCDataChannel>),
}

/// SCTPTransport provides details about the SCTP transport.
Expand Down

0 comments on commit 3e94edf

Please sign in to comment.