active_call/media/track/
mod.rs1use crate::event::EventSender;
2use crate::media::processor::{Processor, ProcessorChain};
3use crate::media::{AudioFrame, TrackId};
4use anyhow::Result;
5use async_trait::async_trait;
6use audio_codec::CodecType;
7use tokio::sync::mpsc;
8use tokio::time::Duration;
9
10pub type TrackPacketSender = mpsc::UnboundedSender<AudioFrame>;
11pub type TrackPacketReceiver = mpsc::UnboundedReceiver<AudioFrame>;
12
13#[derive(Debug, Clone)]
15pub struct TrackConfig {
16 pub codec: CodecType,
17 pub ptime: Duration,
19 pub samplerate: u32,
21 pub channels: u16,
23}
24
25impl Default for TrackConfig {
26 fn default() -> Self {
27 Self {
28 codec: CodecType::G722,
29 samplerate: 16000,
30 channels: 1,
31 ptime: Duration::from_millis(20),
32 }
33 }
34}
35
36impl TrackConfig {
37 pub fn with_ptime(mut self, ptime: Duration) -> Self {
38 self.ptime = ptime;
39 self
40 }
41
42 pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
43 self.samplerate = sample_rate;
44 self
45 }
46
47 pub fn with_channels(mut self, channels: u16) -> Self {
48 self.channels = channels;
49 self
50 }
51}
52
53pub mod file;
54pub mod forwarding;
55pub mod media_pass;
56pub mod rtc;
57pub mod track_codec;
58pub mod tts;
59pub mod websocket;
60#[async_trait]
61pub trait Track: Send + Sync {
62 fn ssrc(&self) -> u32;
63 fn id(&self) -> &TrackId;
64 fn config(&self) -> &TrackConfig;
65 fn set_paused(&self, _paused: bool) -> bool {
66 false
67 }
68 fn is_paused(&self) -> bool {
69 false
70 }
71 fn processor_chain(&mut self) -> &mut ProcessorChain;
72 fn insert_processor(&mut self, processor: Box<dyn Processor>) {
73 self.processor_chain().insert_processor(processor);
74 }
75 fn append_processor(&mut self, processor: Box<dyn Processor>) {
76 self.processor_chain().append_processor(processor);
77 }
78 async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
79 async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
80 async fn update_remote_description_force(&mut self, answer: &String) -> Result<()> {
81 self.update_remote_description(answer).await
83 }
84 async fn start(
85 &mut self,
86 event_sender: EventSender,
87 packet_sender: TrackPacketSender,
88 ) -> Result<()>;
89 async fn stop(&self) -> Result<()>;
90 async fn stop_graceful(&self) -> Result<()> {
91 self.stop().await
92 }
93 async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
94 fn add_ice_candidate(
97 &self,
98 _candidate: &str,
99 _sdp_mid: Option<&str>,
100 _sdp_mline_index: Option<u32>,
101 ) -> Result<()> {
102 Ok(())
103 }
104}