rtc/peer_connection/handler/
mod.rs1pub(crate) mod datachannel;
2pub(crate) mod demuxer;
3pub(crate) mod dtls;
4pub(crate) mod endpoint;
5pub(crate) mod ice;
6pub(crate) mod interceptor;
7pub(crate) mod sctp;
8pub(crate) mod srtp;
9
10use crate::peer_connection::RTCPeerConnection;
11use crate::peer_connection::event::RTCPeerConnectionEvent;
12use crate::peer_connection::event::{RTCEvent, RTCEventInternal};
13use crate::peer_connection::handler::datachannel::{DataChannelHandler, DataChannelHandlerContext};
14use crate::peer_connection::handler::demuxer::{DemuxerHandler, DemuxerHandlerContext};
15use crate::peer_connection::handler::dtls::{DtlsHandler, DtlsHandlerContext};
16use crate::peer_connection::handler::endpoint::{EndpointHandler, EndpointHandlerContext};
17use crate::peer_connection::handler::ice::{IceHandler, IceHandlerContext};
18use crate::peer_connection::handler::interceptor::{InterceptorHandler, InterceptorHandlerContext};
19use crate::peer_connection::handler::sctp::{SctpHandler, SctpHandlerContext};
20use crate::peer_connection::handler::srtp::{SrtpHandler, SrtpHandlerContext};
21use crate::peer_connection::message::{
22 RTCMessage,
23 internal::{
24 ApplicationMessage, DTLSMessage, DataChannelEvent, RTCMessageInternal, RTPMessage,
25 TaggedRTCMessageInternal,
26 },
27};
28use crate::peer_connection::state::peer_connection_state::RTCPeerConnectionState;
29use crate::peer_connection::state::signaling_state::RTCSignalingState;
30use crate::statistics::accumulator::RTCStatsAccumulator;
31use ::interceptor::Interceptor;
32use ::interceptor::Packet;
33use log::warn;
34use shared::TaggedBytesMut;
35use shared::error::{Error, flatten_errs};
36use std::collections::VecDeque;
37use std::time::{Duration, Instant};
38
39pub(crate) const DEFAULT_TIMEOUT_DURATION: Duration = Duration::from_secs(86400); macro_rules! forward_handlers {
43 ($callback:ident!($($args:tt)*)) => {
44 $callback!(
45 $($args)*,
46 [
47 get_demuxer_handler,
48 get_ice_handler,
49 get_dtls_handler,
50 get_sctp_handler,
51 get_datachannel_handler,
52 get_srtp_handler,
53 get_interceptor_handler,
54 get_endpoint_handler
55 ]
56 )
57 };
58}
59
60macro_rules! reverse_handlers {
62 ($callback:ident!($($args:tt)*)) => {
63 $callback!(
64 $($args)*,
65 [
66 get_endpoint_handler,
67 get_interceptor_handler,
68 get_srtp_handler,
69 get_datachannel_handler,
70 get_sctp_handler,
71 get_dtls_handler,
72 get_ice_handler,
73 get_demuxer_handler
74 ]
75 )
76 };
77}
78
79macro_rules! process_handler_list {
81 (call_macro: process_handler!($self:expr, $handler:ident, $code:block), [$($getter:ident),+]) => {{
82 $(
83 {
84 let mut $handler = $self.$getter();
85 $code
86 }
87 )+
88 }};
89}
90
91macro_rules! for_each_handler {
93 (forward: $macro:ident!($($args:tt)*)) => {
95 forward_handlers!(process_handler_list!(call_macro: $macro!($($args)*)))
96 };
97
98 (reverse: $macro:ident!($($args:tt)*)) => {
100 reverse_handlers!(process_handler_list!(call_macro: $macro!($($args)*)))
101 };
102}
103
104#[derive(Default)]
105pub(crate) struct PipelineContext {
106 pub(crate) demuxer_handler_context: DemuxerHandlerContext,
108 pub(crate) ice_handler_context: IceHandlerContext,
109 pub(crate) dtls_handler_context: DtlsHandlerContext,
110 pub(crate) sctp_handler_context: SctpHandlerContext,
111 pub(crate) datachannel_handler_context: DataChannelHandlerContext,
112 pub(crate) srtp_handler_context: SrtpHandlerContext,
113 pub(crate) interceptor_handler_context: InterceptorHandlerContext,
114 pub(crate) endpoint_handler_context: EndpointHandlerContext,
115
116 pub(crate) read_outs: VecDeque<RTCMessage>,
118 pub(crate) write_outs: VecDeque<TaggedBytesMut>,
119 pub(crate) event_outs: VecDeque<RTCPeerConnectionEvent>,
120
121 pub(crate) stats: RTCStatsAccumulator,
123}
124
125impl<I> RTCPeerConnection<I>
126where
127 I: Interceptor,
128{
129 pub(crate) fn get_demuxer_handler(&mut self) -> DemuxerHandler<'_> {
138 DemuxerHandler::new(
139 &mut self.pipeline_context.demuxer_handler_context,
140 &mut self.pipeline_context.stats,
141 )
142 }
143
144 pub(crate) fn get_ice_handler(&mut self) -> IceHandler<'_> {
145 IceHandler::new(
146 &mut self.pipeline_context.ice_handler_context,
147 &mut self.pipeline_context.stats,
148 )
149 }
150
151 pub(crate) fn get_dtls_handler(&mut self) -> DtlsHandler<'_> {
152 DtlsHandler::new(
153 &mut self.pipeline_context.dtls_handler_context,
154 &mut self.pipeline_context.stats,
155 )
156 }
157
158 pub(crate) fn get_sctp_handler(&mut self) -> SctpHandler<'_> {
159 SctpHandler::new(&mut self.pipeline_context.sctp_handler_context)
160 }
161
162 pub(crate) fn get_datachannel_handler(&mut self) -> DataChannelHandler<'_> {
163 DataChannelHandler::new(
164 &mut self.pipeline_context.datachannel_handler_context,
165 &mut self.data_channels,
166 &mut self.pipeline_context.stats,
167 )
168 }
169
170 pub(crate) fn get_srtp_handler(&mut self) -> SrtpHandler<'_> {
171 SrtpHandler::new(&mut self.pipeline_context.srtp_handler_context)
172 }
173
174 pub(crate) fn get_interceptor_handler(&mut self) -> InterceptorHandler<'_, I> {
175 InterceptorHandler::new(
176 &mut self.pipeline_context.interceptor_handler_context,
177 &mut self.interceptor,
178 &mut self.pipeline_context.stats,
179 )
180 }
181
182 pub(crate) fn get_endpoint_handler(&mut self) -> EndpointHandler<'_, I> {
183 EndpointHandler::new(
184 &mut self.pipeline_context.endpoint_handler_context,
185 &mut self.rtp_transceivers,
186 &self.media_engine,
187 &mut self.interceptor,
188 &mut self.pipeline_context.stats,
189 )
190 }
191}
192
193impl<I> sansio::Protocol<TaggedBytesMut, RTCMessage, RTCEvent> for RTCPeerConnection<I>
194where
195 I: Interceptor,
196{
197 type Rout = RTCMessage;
198 type Wout = TaggedBytesMut;
199 type Eout = RTCPeerConnectionEvent;
200 type Error = Error;
201 type Time = Instant;
202
203 fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<(), Self::Error> {
204 let mut intermediate_routs = VecDeque::new();
205 intermediate_routs.push_back(TaggedRTCMessageInternal {
206 now: msg.now,
207 transport: msg.transport,
208 message: RTCMessageInternal::Raw(msg.message),
209 });
210
211 for_each_handler!(forward: process_handler!(self, handler, {
212 while let Some(msg) = intermediate_routs.pop_front() {
213 if let Err(err) = handler.handle_read(msg) {
214 warn!("{}.handle_read got error: {}", handler.name(), err);
215 }
216 }
217 while let Some(msg) = handler.poll_read() {
218 intermediate_routs.push_back(msg);
219 }
220 }));
221
222 while let Some(msg) = intermediate_routs.pop_front() {
224 let rtc_message = match msg.message {
225 RTCMessageInternal::Dtls(DTLSMessage::DataChannel(application_message)) => {
226 if let DataChannelEvent::Message(data_channel_message) =
227 application_message.data_channel_event
228 {
229 Some(RTCMessage::DataChannelMessage(
230 application_message.data_channel_id,
231 data_channel_message,
232 ))
233 } else {
234 None
235 }
236 }
237 RTCMessageInternal::Rtp(RTPMessage::TrackPacket(track_packet)) => {
238 match track_packet.packet {
239 Packet::Rtp(packet) => {
240 Some(RTCMessage::RtpPacket(track_packet.track_id, packet))
241 }
242 Packet::Rtcp(packet) => {
243 Some(RTCMessage::RtcpPacket(track_packet.track_id, packet))
244 }
245 }
246 }
247 _ => None,
248 };
249
250 if let Some(rtc_message) = rtc_message {
251 self.pipeline_context.read_outs.push_back(rtc_message);
252 }
253 }
254
255 Ok(())
256 }
257
258 fn poll_read(&mut self) -> Option<Self::Rout> {
259 self.pipeline_context.read_outs.pop_front()
260 }
261
262 fn handle_write(&mut self, msg: RTCMessage) -> Result<(), Self::Error> {
263 let rtc_message_internal = match msg {
264 RTCMessage::DataChannelMessage(data_channel_id, data_channel_message) => {
265 RTCMessageInternal::Dtls(DTLSMessage::DataChannel(ApplicationMessage {
266 data_channel_id,
267 data_channel_event: DataChannelEvent::Message(data_channel_message),
268 }))
269 }
270 RTCMessage::RtpPacket(_track_id, rtp_packet) => {
271 RTCMessageInternal::Rtp(RTPMessage::Packet(Packet::Rtp(rtp_packet)))
272 }
273 RTCMessage::RtcpPacket(_track_id, rtcp_packet) => {
274 RTCMessageInternal::Rtp(RTPMessage::Packet(Packet::Rtcp(rtcp_packet)))
275 }
276 };
277
278 let mut endpoint_handler = self.get_endpoint_handler();
280 endpoint_handler.handle_write(TaggedRTCMessageInternal {
281 now: Instant::now(),
282 transport: Default::default(),
283 message: rtc_message_internal,
284 })
285 }
286
287 fn poll_write(&mut self) -> Option<Self::Wout> {
288 let mut intermediate_wouts = VecDeque::new();
289
290 for_each_handler!(reverse: process_handler!(self, handler, {
291 while let Some(msg) = intermediate_wouts.pop_front() {
292 if let Err(err) = handler.handle_write(msg) {
293 warn!("{}.handle_write got error: {}", handler.name(), err);
294 }
295 }
296 while let Some(msg) = handler.poll_write() {
297 intermediate_wouts.push_back(msg);
298 }
299 }));
300
301 while let Some(msg) = intermediate_wouts.pop_front() {
303 if let RTCMessageInternal::Raw(message) = msg.message {
304 self.pipeline_context.write_outs.push_back(TaggedBytesMut {
305 now: msg.now,
306 transport: msg.transport,
307 message,
308 });
309 }
310 }
311
312 self.pipeline_context.write_outs.pop_front()
313 }
314
315 fn handle_event(&mut self, evt: RTCEvent) -> Result<(), Self::Error> {
316 let mut endpoint_handler = self.get_endpoint_handler();
318 endpoint_handler.handle_event(RTCEventInternal::RTCEvent(evt))
319 }
320
321 fn poll_event(&mut self) -> Option<Self::Eout> {
322 let mut intermediate_eouts = VecDeque::new();
323
324 for_each_handler!(forward: process_handler!(self, handler, {
325 while let Some(evt) = intermediate_eouts.pop_front() {
326 if let Err(err) = handler.handle_event(evt) {
327 warn!("{}.handle_event got error: {}", handler.name(), err);
328 }
329 }
330 while let Some(msg) = handler.poll_event() {
331 intermediate_eouts.push_back(msg);
332 }
333 }));
334
335 while let Some(evt_internal) = intermediate_eouts.pop_front() {
337 match &evt_internal {
338 RTCEventInternal::RTCPeerConnectionEvent(
339 RTCPeerConnectionEvent::OnIceConnectionStateChangeEvent(_),
340 )
341 | RTCEventInternal::DTLSHandshakeComplete(_, _) => {
342 self.update_connection_state(false);
343 }
344 _ => {}
345 };
346
347 if let RTCEventInternal::RTCPeerConnectionEvent(evt) = evt_internal {
348 self.pipeline_context.event_outs.push_back(evt);
349 }
350 }
351
352 self.pipeline_context.event_outs.pop_front()
353 }
354
355 fn handle_timeout(&mut self, now: Instant) -> Result<(), Self::Error> {
356 for_each_handler!(forward: process_handler!(self, handler, {
357 handler.handle_timeout(now)?;
358 }));
359 Ok(())
360 }
361
362 fn poll_timeout(&mut self) -> Option<Instant> {
363 let mut eto: Option<Instant> = None;
364 for_each_handler!(forward: process_handler!(self, handler, {
365 if let Some(next) = handler.poll_timeout() {
366 eto = Some(eto.map_or(next, |curr| std::cmp::min(curr, next)));
367 }
368 }));
369 eto
370 }
371
372 fn close(&mut self) -> Result<(), Self::Error> {
373 if self.peer_connection_state == RTCPeerConnectionState::Closed {
375 return Ok(());
376 }
377
378 self.signaling_state = RTCSignalingState::Closed;
380
381 for_each_handler!(forward: process_handler!(self, handler, {
388 handler.close()?;
389 }));
390
391 let close_errs: Vec<Error> = vec![];
392
393 self.update_connection_state(true);
437
438 flatten_errs(close_errs)
439 }
440}