#![cfg(feature = "webrtc")]
pub mod client;
pub mod datachannel;
pub mod media;
pub mod peer;
pub mod room;
pub mod server;
pub mod signaling;
pub mod signaling_handler;
pub use client::*;
pub use datachannel::*;
pub use media::*;
pub use peer::*;
pub use room::*;
pub use server::*;
pub use signaling::*;
pub use signaling_handler::*;
use crate::PipeContext;
use std::sync::Arc;
type WebRTCCallback1 = dyn Fn(&str) + Send + Sync;
type WebRTCCallback2 = dyn Fn(&str, &str) + Send + Sync;
type WebRTCCallback3 = dyn Fn(&str, &str, &str) + Send + Sync;
type WebRTCCallback4 = dyn Fn(&str, &str, &str, &[u8]) + Send + Sync;
#[derive(Clone)]
pub struct WebRTCConfig {
pub max_peers: u32,
pub ice_servers: Vec<String>,
pub udp_port_start: u16,
pub udp_port_end: u16,
pub enable_datachannel: bool,
pub ws_path: String,
pub rest_prefix: String,
pub auto_reconnect: bool,
pub log_level: String,
pub events: std::sync::Arc<WebRTCEvents>,
}
impl Default for WebRTCConfig {
fn default() -> Self {
Self {
max_peers: 100,
ice_servers: vec!["stun:stun.l.google.com:19302".to_string()],
udp_port_start: 50000,
udp_port_end: 60000,
enable_datachannel: true,
ws_path: "/ws".to_string(),
rest_prefix: "/api/webrtc".to_string(),
auto_reconnect: true,
log_level: "info".to_string(),
events: std::sync::Arc::new(WebRTCEvents::default()),
}
}
}
#[derive(Default)]
pub struct WebRTCEvents {
pub on_room_created: Option<Arc<WebRTCCallback1>>,
pub on_peer_joined: Option<Arc<WebRTCCallback2>>,
pub on_peer_left: Option<Arc<WebRTCCallback2>>,
pub on_publish_started: Option<Arc<WebRTCCallback2>>,
pub on_subscribe_started: Option<Arc<WebRTCCallback3>>,
pub on_datachannel_message: Option<Arc<WebRTCCallback4>>,
}
impl Clone for WebRTCEvents {
fn clone(&self) -> Self {
Self {
on_room_created: self.on_room_created.clone(),
on_peer_joined: self.on_peer_joined.clone(),
on_peer_left: self.on_peer_left.clone(),
on_publish_started: self.on_publish_started.clone(),
on_subscribe_started: self.on_subscribe_started.clone(),
on_datachannel_message: self.on_datachannel_message.clone(),
}
}
}
pub struct WebRTCBuilder<'a> {
ctx: &'a mut PipeContext,
config: WebRTCConfig,
events: WebRTCEvents,
}
impl<'a> WebRTCBuilder<'a> {
pub fn new(ctx: &'a mut PipeContext) -> Self {
Self {
ctx,
config: WebRTCConfig::default(),
events: WebRTCEvents::default(),
}
}
pub fn max_peers(mut self, n: u32) -> Self {
self.config.max_peers = n;
self
}
pub fn ice_servers(mut self, servers: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.config.ice_servers = servers.into_iter().map(|s| s.into()).collect();
self
}
pub fn udp_port_range(mut self, start: u16, end: u16) -> Self {
self.config.udp_port_start = start;
self.config.udp_port_end = end;
self
}
pub fn enable_datachannel(mut self, enabled: bool) -> Self {
self.config.enable_datachannel = enabled;
self
}
pub fn ws_path(mut self, path: &str) -> Self {
self.config.ws_path = path.to_string();
self
}
pub fn rest_prefix(mut self, prefix: &str) -> Self {
self.config.rest_prefix = prefix.to_string();
self
}
pub fn auto_reconnect(mut self, enabled: bool) -> Self {
self.config.auto_reconnect = enabled;
self
}
pub fn log_level(mut self, level: &str) -> Self {
self.config.log_level = level.to_string();
self
}
pub fn on_room_created<F>(mut self, f: F) -> Self
where
F: Fn(&str) + Send + Sync + 'static,
{
self.events.on_room_created = Some(Arc::new(f));
self
}
pub fn on_peer_joined<F>(mut self, f: F) -> Self
where
F: Fn(&str, &str) + Send + Sync + 'static,
{
self.events.on_peer_joined = Some(Arc::new(f));
self
}
pub fn on_peer_left<F>(mut self, f: F) -> Self
where
F: Fn(&str, &str) + Send + Sync + 'static,
{
self.events.on_peer_left = Some(Arc::new(f));
self
}
pub fn on_publish_started<F>(mut self, f: F) -> Self
where
F: Fn(&str, &str) + Send + Sync + 'static,
{
self.events.on_publish_started = Some(Arc::new(f));
self
}
pub fn on_subscribe_started<F>(mut self, f: F) -> Self
where
F: Fn(&str, &str, &str) + Send + Sync + 'static,
{
self.events.on_subscribe_started = Some(Arc::new(f));
self
}
pub fn on_datachannel_message<F>(mut self, f: F) -> Self
where
F: Fn(&str, &str, &str, &[u8]) + Send + Sync + 'static,
{
self.events.on_datachannel_message = Some(Arc::new(f));
self
}
pub fn finish(self) {
self.ctx.add_webrtc(self.config, self.events);
}
}