use async_trait::async_trait;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::sync::broadcast;
use crate::api::client::transport::RtcpStats;
use crate::api::client::transport::VoipMetrics;
use crate::api::common::config::SecurityInfo;
use crate::api::common::error::MediaTransportError;
use crate::api::common::events::MediaEventCallback;
use crate::api::common::extension::ExtensionFormat;
use crate::api::common::frame::MediaFrame;
use crate::api::common::stats::MediaStats;
use crate::{CsrcMapping, RtpCsrc, RtpSsrc};
mod core;
pub mod default;
mod rtcp;
mod security;
mod ssrc;
mod stats;
#[cfg(test)]
mod tests;
mod util;
pub use default::DefaultMediaTransportServer;
#[derive(Debug, Clone)]
pub struct ClientInfo {
pub id: String,
pub address: SocketAddr,
pub secure: bool,
pub security_info: Option<SecurityInfo>,
pub connected: bool,
}
#[derive(Debug, Clone)]
pub struct HeaderExtension {
pub id: u8,
pub uri: String,
pub data: Vec<u8>,
}
#[async_trait]
pub trait MediaTransportServer: Send + Sync + Clone {
async fn start(&self) -> Result<(), MediaTransportError>;
async fn stop(&self) -> Result<(), MediaTransportError>;
async fn get_local_address(&self) -> Result<SocketAddr, MediaTransportError>;
async fn send_frame_to(
&self,
client_id: &str,
frame: MediaFrame,
) -> Result<(), MediaTransportError>;
async fn broadcast_frame(&self, frame: MediaFrame) -> Result<(), MediaTransportError>;
async fn receive_frame(&self) -> Result<(String, MediaFrame), MediaTransportError>;
fn get_frame_receiver(&self) -> broadcast::Receiver<(String, MediaFrame)>;
async fn get_clients(&self) -> Result<Vec<ClientInfo>, MediaTransportError>;
async fn disconnect_client(&self, client_id: &str) -> Result<(), MediaTransportError>;
async fn on_event(&self, callback: MediaEventCallback) -> Result<(), MediaTransportError>;
async fn on_client_connected(
&self,
callback: Box<dyn Fn(ClientInfo) + Send + Sync>,
) -> Result<(), MediaTransportError>;
async fn on_client_disconnected(
&self,
callback: Box<dyn Fn(ClientInfo) + Send + Sync>,
) -> Result<(), MediaTransportError>;
async fn get_stats(&self) -> Result<MediaStats, MediaTransportError>;
async fn get_client_stats(&self, client_id: &str) -> Result<MediaStats, MediaTransportError>;
async fn get_security_info(&self) -> Result<SecurityInfo, MediaTransportError>;
async fn send_rtcp_receiver_report(&self) -> Result<(), MediaTransportError>;
async fn send_rtcp_sender_report(&self) -> Result<(), MediaTransportError>;
async fn send_rtcp_receiver_report_to_client(
&self,
client_id: &str,
) -> Result<(), MediaTransportError>;
async fn send_rtcp_sender_report_to_client(
&self,
client_id: &str,
) -> Result<(), MediaTransportError>;
async fn get_rtcp_stats(&self) -> Result<RtcpStats, MediaTransportError>;
async fn get_client_rtcp_stats(
&self,
client_id: &str,
) -> Result<RtcpStats, MediaTransportError>;
async fn set_rtcp_interval(&self, interval: Duration) -> Result<(), MediaTransportError>;
async fn send_rtcp_app(&self, name: &str, data: Vec<u8>) -> Result<(), MediaTransportError>;
async fn send_rtcp_app_to_client(
&self,
client_id: &str,
name: &str,
data: Vec<u8>,
) -> Result<(), MediaTransportError>;
async fn send_rtcp_bye(&self, reason: Option<String>) -> Result<(), MediaTransportError>;
async fn send_rtcp_bye_to_client(
&self,
client_id: &str,
reason: Option<String>,
) -> Result<(), MediaTransportError>;
async fn send_rtcp_xr_voip_metrics(
&self,
metrics: VoipMetrics,
) -> Result<(), MediaTransportError>;
async fn send_rtcp_xr_voip_metrics_to_client(
&self,
client_id: &str,
metrics: VoipMetrics,
) -> Result<(), MediaTransportError>;
async fn is_csrc_management_enabled(&self) -> Result<bool, MediaTransportError>;
async fn enable_csrc_management(&self) -> Result<bool, MediaTransportError>;
async fn add_csrc_mapping(&self, mapping: CsrcMapping) -> Result<(), MediaTransportError>;
async fn add_simple_csrc_mapping(
&self,
original_ssrc: RtpSsrc,
csrc: RtpCsrc,
) -> Result<(), MediaTransportError>;
async fn remove_csrc_mapping_by_ssrc(
&self,
original_ssrc: RtpSsrc,
) -> Result<Option<CsrcMapping>, MediaTransportError>;
async fn get_csrc_mapping_by_ssrc(
&self,
original_ssrc: RtpSsrc,
) -> Result<Option<CsrcMapping>, MediaTransportError>;
async fn get_all_csrc_mappings(&self) -> Result<Vec<CsrcMapping>, MediaTransportError>;
async fn get_active_csrcs(
&self,
active_ssrcs: &[RtpSsrc],
) -> Result<Vec<RtpCsrc>, MediaTransportError>;
async fn is_header_extensions_enabled(&self) -> Result<bool, MediaTransportError>;
async fn enable_header_extensions(
&self,
format: ExtensionFormat,
) -> Result<bool, MediaTransportError>;
async fn configure_header_extension(
&self,
id: u8,
uri: String,
) -> Result<(), MediaTransportError>;
async fn configure_header_extensions(
&self,
mappings: HashMap<u8, String>,
) -> Result<(), MediaTransportError>;
async fn add_header_extension_for_client(
&self,
client_id: &str,
extension: HeaderExtension,
) -> Result<(), MediaTransportError>;
async fn add_header_extension_for_all_clients(
&self,
extension: HeaderExtension,
) -> Result<(), MediaTransportError>;
async fn add_audio_level_extension_for_client(
&self,
client_id: &str,
voice_activity: bool,
level: u8,
) -> Result<(), MediaTransportError>;
async fn add_audio_level_extension_for_all_clients(
&self,
voice_activity: bool,
level: u8,
) -> Result<(), MediaTransportError>;
async fn add_video_orientation_extension_for_client(
&self,
client_id: &str,
camera_front_facing: bool,
camera_flipped: bool,
rotation: u16,
) -> Result<(), MediaTransportError>;
async fn add_video_orientation_extension_for_all_clients(
&self,
camera_front_facing: bool,
camera_flipped: bool,
rotation: u16,
) -> Result<(), MediaTransportError>;
async fn add_transport_cc_extension_for_client(
&self,
client_id: &str,
sequence_number: u16,
) -> Result<(), MediaTransportError>;
async fn add_transport_cc_extension_for_all_clients(
&self,
sequence_number: u16,
) -> Result<(), MediaTransportError>;
async fn get_received_header_extensions(
&self,
client_id: &str,
) -> Result<Vec<HeaderExtension>, MediaTransportError>;
async fn get_received_audio_level(
&self,
client_id: &str,
) -> Result<Option<(bool, u8)>, MediaTransportError>;
async fn get_received_video_orientation(
&self,
client_id: &str,
) -> Result<Option<(bool, bool, u16)>, MediaTransportError>;
async fn get_received_transport_cc(
&self,
client_id: &str,
) -> Result<Option<u16>, MediaTransportError>;
async fn is_ssrc_demultiplexing_enabled(&self) -> Result<bool, MediaTransportError>;
async fn enable_ssrc_demultiplexing(&self) -> Result<bool, MediaTransportError>;
async fn register_client_ssrc(
&self,
client_id: &str,
ssrc: u32,
) -> Result<bool, MediaTransportError>;
async fn get_client_ssrcs(&self, client_id: &str) -> Result<Vec<u32>, MediaTransportError>;
async fn update_csrc_cname(
&self,
original_ssrc: RtpSsrc,
cname: String,
) -> Result<bool, MediaTransportError>;
async fn update_csrc_display_name(
&self,
original_ssrc: RtpSsrc,
name: String,
) -> Result<bool, MediaTransportError>;
}