use std::future::Future;
use std::pin::Pin;
use crate::*;
pub type BindRtcpReaderFn = Box<
dyn (Fn(
Arc<dyn RTCPReader + Send + Sync>,
)
-> Pin<Box<dyn Future<Output = Arc<dyn RTCPReader + Send + Sync>> + Send + Sync>>)
+ Send
+ Sync,
>;
pub type BindRtcpWriterFn = Box<
dyn (Fn(
Arc<dyn RTCPWriter + Send + Sync>,
)
-> Pin<Box<dyn Future<Output = Arc<dyn RTCPWriter + Send + Sync>> + Send + Sync>>)
+ Send
+ Sync,
>;
pub type BindLocalStreamFn = Box<
dyn (Fn(
&StreamInfo,
Arc<dyn RTPWriter + Send + Sync>,
) -> Pin<Box<dyn Future<Output = Arc<dyn RTPWriter + Send + Sync>> + Send + Sync>>)
+ Send
+ Sync,
>;
pub type UnbindLocalStreamFn =
Box<dyn (Fn(&StreamInfo) -> Pin<Box<dyn Future<Output = ()> + Send + Sync>>) + Send + Sync>;
pub type BindRemoteStreamFn = Box<
dyn (Fn(
&StreamInfo,
Arc<dyn RTPReader + Send + Sync>,
) -> Pin<Box<dyn Future<Output = Arc<dyn RTPReader + Send + Sync>> + Send + Sync>>)
+ Send
+ Sync,
>;
pub type UnbindRemoteStreamFn =
Box<dyn (Fn(&StreamInfo) -> Pin<Box<dyn Future<Output = ()> + Send + Sync>>) + Send + Sync>;
pub type CloseFn =
Box<dyn (Fn() -> Pin<Box<dyn Future<Output = Result<()>> + Send + Sync>>) + Send + Sync>;
#[derive(Default)]
pub struct MockInterceptor {
pub bind_rtcp_reader_fn: Option<BindRtcpReaderFn>,
pub bind_rtcp_writer_fn: Option<BindRtcpWriterFn>,
pub bind_local_stream_fn: Option<BindLocalStreamFn>,
pub unbind_local_stream_fn: Option<UnbindLocalStreamFn>,
pub bind_remote_stream_fn: Option<BindRemoteStreamFn>,
pub unbind_remote_stream_fn: Option<UnbindRemoteStreamFn>,
pub close_fn: Option<CloseFn>,
}
#[async_trait]
impl Interceptor for MockInterceptor {
async fn bind_rtcp_reader(
&self,
reader: Arc<dyn RTCPReader + Send + Sync>,
) -> Arc<dyn RTCPReader + Send + Sync> {
if let Some(f) = &self.bind_rtcp_reader_fn {
f(reader).await
} else {
reader
}
}
async fn bind_rtcp_writer(
&self,
writer: Arc<dyn RTCPWriter + Send + Sync>,
) -> Arc<dyn RTCPWriter + Send + Sync> {
if let Some(f) = &self.bind_rtcp_writer_fn {
f(writer).await
} else {
writer
}
}
async fn bind_local_stream(
&self,
info: &StreamInfo,
writer: Arc<dyn RTPWriter + Send + Sync>,
) -> Arc<dyn RTPWriter + Send + Sync> {
if let Some(f) = &self.bind_local_stream_fn {
f(info, writer).await
} else {
writer
}
}
async fn unbind_local_stream(&self, info: &StreamInfo) {
if let Some(f) = &self.unbind_local_stream_fn {
f(info).await
}
}
async fn bind_remote_stream(
&self,
info: &StreamInfo,
reader: Arc<dyn RTPReader + Send + Sync>,
) -> Arc<dyn RTPReader + Send + Sync> {
if let Some(f) = &self.bind_remote_stream_fn {
f(info, reader).await
} else {
reader
}
}
async fn unbind_remote_stream(&self, info: &StreamInfo) {
if let Some(f) = &self.unbind_remote_stream_fn {
f(info).await
}
}
async fn close(&self) -> Result<()> {
if let Some(f) = &self.close_fn {
f().await
} else {
Ok(())
}
}
}