use crate::peer_connection::PeerConnectionRef;
use crate::runtime::{Mutex, Receiver};
use bytes::BytesMut;
use rtc::interceptor::{Interceptor, NoopInterceptor};
use rtc::shared::error::{Error, Result};
use std::sync::Arc;
use crate::peer_connection::driver::PeerConnectionDriverEvent;
pub use rtc::data_channel::{
RTCDataChannelId, RTCDataChannelInit, RTCDataChannelMessage, RTCDataChannelState,
};
#[async_trait::async_trait]
pub trait DataChannel: Send + Sync + 'static {
async fn label(&self) -> Result<String>;
async fn ordered(&self) -> Result<bool>;
async fn max_packet_life_time(&self) -> Result<Option<u16>>;
async fn max_retransmits(&self) -> Result<Option<u16>>;
async fn protocol(&self) -> Result<String>;
async fn negotiated(&self) -> Result<bool>;
fn id(&self) -> RTCDataChannelId;
async fn ready_state(&self) -> Result<RTCDataChannelState>;
async fn buffered_amount_high_threshold(&self) -> Result<u32>;
async fn set_buffered_amount_high_threshold(&self, threshold: u32) -> Result<()>;
async fn buffered_amount_low_threshold(&self) -> Result<u32>;
async fn set_buffered_amount_low_threshold(&self, threshold: u32) -> Result<()>;
async fn send(&self, data: BytesMut) -> Result<()>;
async fn send_text(&self, text: &str) -> Result<()>;
async fn poll(&self) -> Option<DataChannelEvent>;
async fn close(&self) -> Result<()>;
}
#[derive(Debug, Clone)]
pub enum DataChannelEvent {
OnOpen,
OnError,
OnClosing,
OnClose,
OnBufferedAmountLow,
OnBufferedAmountHigh,
OnMessage(RTCDataChannelMessage),
}
pub(crate) struct DataChannelImpl<I = NoopInterceptor>
where
I: Interceptor,
{
id: RTCDataChannelId,
inner: Arc<PeerConnectionRef<I>>,
evt_rx: Mutex<Receiver<DataChannelEvent>>,
}
impl<I> DataChannelImpl<I>
where
I: Interceptor,
{
pub(crate) fn new(
id: RTCDataChannelId,
inner: Arc<PeerConnectionRef<I>>,
evt_rx: Receiver<DataChannelEvent>,
) -> Self {
Self {
id,
inner,
evt_rx: Mutex::new(evt_rx),
}
}
}
#[async_trait::async_trait]
impl<I> DataChannel for DataChannelImpl<I>
where
I: Interceptor + 'static,
{
async fn label(&self) -> Result<String> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.label()
.to_owned())
}
async fn ordered(&self) -> Result<bool> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.ordered())
}
async fn max_packet_life_time(&self) -> Result<Option<u16>> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.max_packet_life_time())
}
async fn max_retransmits(&self) -> Result<Option<u16>> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.max_retransmits())
}
async fn protocol(&self) -> Result<String> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.protocol()
.to_owned())
}
async fn negotiated(&self) -> Result<bool> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.negotiated())
}
fn id(&self) -> RTCDataChannelId {
self.id
}
async fn ready_state(&self) -> Result<RTCDataChannelState> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.ready_state())
}
async fn buffered_amount_high_threshold(&self) -> Result<u32> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.buffered_amount_high_threshold())
}
async fn set_buffered_amount_high_threshold(&self, threshold: u32) -> Result<()> {
{
let mut peer_connection = self.inner.core.lock().await;
peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.set_buffered_amount_high_threshold(threshold);
}
self.inner
.driver_event_tx
.send(PeerConnectionDriverEvent::WriteNotify)
.await
.map_err(|e| Error::Other(format!("{:?}", e)))
}
async fn buffered_amount_low_threshold(&self) -> Result<u32> {
let mut peer_connection = self.inner.core.lock().await;
Ok(peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.buffered_amount_low_threshold())
}
async fn set_buffered_amount_low_threshold(&self, threshold: u32) -> Result<()> {
{
let mut peer_connection = self.inner.core.lock().await;
peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.set_buffered_amount_low_threshold(threshold);
}
self.inner
.driver_event_tx
.send(PeerConnectionDriverEvent::WriteNotify)
.await
.map_err(|e| Error::Other(format!("{:?}", e)))
}
async fn send(&self, data: BytesMut) -> Result<()> {
{
let mut peer_connection = self.inner.core.lock().await;
peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.send(data)?;
}
self.inner
.driver_event_tx
.send(PeerConnectionDriverEvent::WriteNotify)
.await
.map_err(|e| Error::Other(format!("{:?}", e)))
}
async fn send_text(&self, text: &str) -> Result<()> {
{
let mut peer_connection = self.inner.core.lock().await;
peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.send_text(text)?;
}
self.inner
.driver_event_tx
.send(PeerConnectionDriverEvent::WriteNotify)
.await
.map_err(|e| Error::Other(format!("{:?}", e)))
}
async fn poll(&self) -> Option<DataChannelEvent> {
self.evt_rx.lock().await.recv().await
}
async fn close(&self) -> Result<()> {
{
let mut peer_connection = self.inner.core.lock().await;
peer_connection
.data_channel(self.id)
.ok_or(Error::ErrDataChannelClosed)?
.close()?;
}
self.inner
.driver_event_tx
.send(PeerConnectionDriverEvent::WriteNotify)
.await
.map_err(|e| Error::Other(format!("{:?}", e)))
}
}