rtci_interceptor/pass_through.rs
1//! The inner end of a nested interceptor placed in a registry slot.
2
3use crate::Interceptor;
4use crate::TaggedPacket;
5use crate::stream_info::StreamInfo;
6use sansio::Protocol;
7use shared::error::Error;
8use std::collections::VecDeque;
9use std::time::Instant;
10
11/// Hands every packet on unchanged, in both directions: the inner end of a nested interceptor
12/// (one written with the [`Interceptor`](derive@crate::Interceptor) derive, such as
13/// [`GccInterceptor`](crate::GccInterceptor)) placed in a [`Registry`](crate::Registry) slot.
14///
15/// # Why not [`NoopInterceptor`](crate::NoopInterceptor)
16///
17/// `NoopInterceptor` is the chain's terminus: it ends every inbound RTCP packet no interceptor
18/// marked for the application. Wrapped inside a slot it does that *there*, so every interceptor
19/// application-ward of the slot, and the application, stop seeing RTCP: in
20/// [`Slot::CongestionControl`](crate::Slot::CongestionControl), the slot inbound packets reach
21/// first, that is all of it (NACKs, receiver reports, PLI and FIR). This one ends nothing; the
22/// terminus [`Registry::build`](crate::Registry::build) appends stays the only one.
23///
24/// # Example
25///
26/// ```
27/// use rtci_interceptor::{
28/// GccInterceptorBuilder, Interceptor, PassThroughInterceptor, Registry, Slot,
29/// TwccSenderBuilder,
30/// };
31///
32/// let (gcc_builder, _gcc_handle) = GccInterceptorBuilder::new();
33/// let chain = Registry::new()
34/// .with(
35/// Slot::CongestionControl,
36/// PassThroughInterceptor::new().with(gcc_builder.build()),
37/// )
38/// .with(Slot::TwccSender, TwccSenderBuilder::new().build())
39/// .build();
40/// # let _ = chain;
41/// ```
42#[derive(Default)]
43pub struct PassThroughInterceptor {
44 read_queue: VecDeque<TaggedPacket>,
45 write_queue: VecDeque<TaggedPacket>,
46}
47
48impl PassThroughInterceptor {
49 /// An interceptor that passes everything on.
50 pub fn new() -> Self {
51 Self::default()
52 }
53}
54
55impl Protocol<TaggedPacket, TaggedPacket, ()> for PassThroughInterceptor {
56 type Rout = TaggedPacket;
57 type Wout = TaggedPacket;
58 type Eout = ();
59 type Error = Error;
60 type Time = Instant;
61
62 fn handle_read(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
63 self.read_queue.push_back(msg);
64 Ok(())
65 }
66
67 fn poll_read(&mut self) -> Option<Self::Rout> {
68 self.read_queue.pop_front()
69 }
70
71 fn handle_write(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
72 self.write_queue.push_back(msg);
73 Ok(())
74 }
75
76 fn poll_write(&mut self) -> Option<Self::Wout> {
77 self.write_queue.pop_front()
78 }
79}
80
81impl Interceptor for PassThroughInterceptor {
82 fn bind_local_stream(&mut self, _info: &StreamInfo) {}
83 fn unbind_local_stream(&mut self, _info: &StreamInfo) {}
84 fn bind_remote_stream(&mut self, _info: &StreamInfo) {}
85 fn unbind_remote_stream(&mut self, _info: &StreamInfo) {}
86}