From 6df59f1629370b4c664c8f9cff4ca558df9d2f36 Mon Sep 17 00:00:00 2001 From: yngrtc Date: Sat, 20 Jan 2024 15:43:37 -0800 Subject: [PATCH] fix data_message_type to Text to be compatible with Chrome --- src/handlers/data/mod.rs | 2 +- src/handlers/gateway/mod.rs | 40 ++++++++++++++++++++++++++++--------- src/handlers/sctp/mod.rs | 2 +- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/handlers/data/mod.rs b/src/handlers/data/mod.rs index c3483b1..e5a5be7 100644 --- a/src/handlers/data/mod.rs +++ b/src/handlers/data/mod.rs @@ -131,7 +131,7 @@ impl OutboundHandler for DataChannelOutbound { message: MessageEvent::DTLS(DTLSMessageEvent::SCTP(DataChannelMessage { association_handle: message.association_handle, stream_id: message.stream_id, - data_message_type: DataChannelMessageType::Binary, + data_message_type: DataChannelMessageType::Text, params: DataChannelMessageParams::Outbound { ordered: true, reliable: true, diff --git a/src/handlers/gateway/mod.rs b/src/handlers/gateway/mod.rs index d266426..75b55e1 100644 --- a/src/handlers/gateway/mod.rs +++ b/src/handlers/gateway/mod.rs @@ -6,6 +6,7 @@ use crate::server::endpoint::{candidate::Candidate, Endpoint}; use crate::server::session::description::sdp_type::RTCSdpType; use crate::server::session::description::RTCSessionDescription; use crate::server::states::ServerStates; +use bytes::BytesMut; use log::{debug, info, warn}; use retty::channel::{Handler, InboundContext, InboundHandler, OutboundContext, OutboundHandler}; use retty::transport::TransportContext; @@ -167,21 +168,31 @@ impl GatewayInbound { &mut self, now: Instant, transport_context: TransportContext, - message: ApplicationMessage, + mut message: ApplicationMessage, ) -> Result { - let sdp_str = String::from_utf8(message.payload.to_vec())?; - info!("handle_dtls_message: {}", sdp_str); + let request_sdp_str = String::from_utf8(message.payload.to_vec())?; + info!("handle_dtls_message: request_sdp {}", request_sdp_str); - let mut sdp = serde_json::from_str::(&sdp_str) + let mut request_sdp = serde_json::from_str::(&request_sdp_str) .map_err(|err| Error::Other(err.to_string()))?; - sdp.parsed = Some(sdp.unmarshal()?); + request_sdp.parsed = Some(request_sdp.unmarshal()?); - match sdp.sdp_type { - RTCSdpType::Offer => {} - RTCSdpType::Answer => {} - _ => {} + let response_sdp = match request_sdp.sdp_type { + RTCSdpType::Offer => self.handle_offer_sdp(request_sdp)?, + RTCSdpType::Answer => self.handle_answer_sdp(request_sdp)?, + _ => { + return Err(Error::Other(format!( + "Unsupport SDP type {}", + request_sdp.sdp_type + ))) + } }; + let response_sdp_str = + serde_json::to_string(&response_sdp).map_err(|err| Error::Other(err.to_string()))?; + info!("handle_dtls_message: response_sdp {}", response_sdp_str); + message.payload = BytesMut::from(response_sdp_str.as_str()); + Ok(TaggedMessageEvent { now, transport: transport_context, @@ -189,6 +200,17 @@ impl GatewayInbound { }) } + fn handle_offer_sdp(&mut self, offer: RTCSessionDescription) -> Result { + Ok(offer) + } + + fn handle_answer_sdp( + &mut self, + answer: RTCSessionDescription, + ) -> Result { + Ok(answer) + } + fn check_stun_message( &self, request: &mut stun::message::Message, diff --git a/src/handlers/sctp/mod.rs b/src/handlers/sctp/mod.rs index d3edcf0..759e8e0 100644 --- a/src/handlers/sctp/mod.rs +++ b/src/handlers/sctp/mod.rs @@ -252,7 +252,7 @@ impl OutboundHandler for SctpOutbound { .server_config() .sctp_server_config .transport - .max_message_size() as usize as usize + .max_message_size() as usize }; if message.payload.len() > max_message_size { return Err(Error::ErrOutboundPacketTooLarge);