1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#![warn(rust_2018_idioms)]
#![allow(dead_code)]
use async_trait::async_trait;
use error::Result;
use std::collections::HashMap;
use std::sync::Arc;
use stream_info::StreamInfo;
#[cfg(test)]
pub(crate) mod mock;
pub mod chain;
mod error;
pub mod nack;
pub mod noop;
pub mod registry;
pub mod report;
pub mod stream_info;
pub mod stream_reader;
pub use error::Error;
#[async_trait]
pub trait Interceptor {
async fn bind_rtcp_reader(
&self,
reader: Arc<dyn RTCPReader + Send + Sync>,
) -> Arc<dyn RTCPReader + Send + Sync>;
async fn bind_rtcp_writer(
&self,
writer: Arc<dyn RTCPWriter + Send + Sync>,
) -> Arc<dyn RTCPWriter + Send + Sync>;
async fn bind_local_stream(
&self,
info: &StreamInfo,
writer: Arc<dyn RTPWriter + Send + Sync>,
) -> Arc<dyn RTPWriter + Send + Sync>;
async fn unbind_local_stream(&self, info: &StreamInfo);
async fn bind_remote_stream(
&self,
info: &StreamInfo,
reader: Arc<dyn RTPReader + Send + Sync>,
) -> Arc<dyn RTPReader + Send + Sync>;
async fn unbind_remote_stream(&self, info: &StreamInfo);
async fn close(&self) -> Result<()>;
}
#[async_trait]
pub trait RTPWriter {
async fn write(&self, pkt: &rtp::packet::Packet, attributes: &Attributes) -> Result<usize>;
}
#[async_trait]
pub trait RTPReader {
async fn read(&self, buf: &mut [u8], attributes: &Attributes) -> Result<(usize, Attributes)>;
}
#[async_trait]
pub trait RTCPWriter {
async fn write(
&self,
pkt: &(dyn rtcp::packet::Packet + Send + Sync),
attributes: &Attributes,
) -> Result<usize>;
}
#[async_trait]
pub trait RTCPReader {
async fn read(&self, buf: &mut [u8], attributes: &Attributes) -> Result<(usize, Attributes)>;
}
pub type Attributes = HashMap<usize, usize>;
#[cfg(test)]
mod test {
use std::future::Future;
use std::time::Duration;
pub async fn timeout_or_fail<T>(duration: Duration, future: T) -> T::Output
where
T: Future,
{
match tokio::time::timeout(duration, future).await {
Ok(v) => v,
Err(_) => {
assert!(false, "timed out");
unreachable!()
}
}
}
}