Skip to main content

active_call/media/track/
mod.rs

1use 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// New shared track configuration struct
14#[derive(Debug, Clone)]
15pub struct TrackConfig {
16    pub codec: CodecType,
17    // Packet time in milliseconds (typically 10, 20, or 30ms)
18    pub ptime: Duration,
19    // Sample rate for PCM audio (e.g., 8000, 16000, 48000)
20    pub samplerate: u32,
21    // Number of audio channels (1 for mono, 2 for stereo)
22    pub channels: u16,
23}
24
25impl Default for TrackConfig {
26    fn default() -> Self {
27        Self {
28            #[cfg(feature = "opus")]
29            codec: CodecType::Opus,
30            #[cfg(not(feature = "opus"))]
31            codec: CodecType::G722,
32            samplerate: 16000,
33            channels: 1,
34            ptime: Duration::from_millis(20),
35        }
36    }
37}
38
39impl TrackConfig {
40    pub fn with_ptime(mut self, ptime: Duration) -> Self {
41        self.ptime = ptime;
42        self
43    }
44
45    pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
46        self.samplerate = sample_rate;
47        self
48    }
49
50    pub fn with_channels(mut self, channels: u16) -> Self {
51        self.channels = channels;
52        self
53    }
54}
55
56pub mod file;
57pub mod forwarding;
58pub mod media_pass;
59pub mod rtc;
60pub mod track_codec;
61pub mod tts;
62pub mod websocket;
63#[async_trait]
64pub trait Track: Send + Sync {
65    fn ssrc(&self) -> u32;
66    fn id(&self) -> &TrackId;
67    fn config(&self) -> &TrackConfig;
68    fn set_paused(&self, _paused: bool) -> bool {
69        false
70    }
71    fn is_paused(&self) -> bool {
72        false
73    }
74    fn processor_chain(&mut self) -> &mut ProcessorChain;
75    fn insert_processor(&mut self, processor: Box<dyn Processor>) {
76        self.processor_chain().insert_processor(processor);
77    }
78    fn append_processor(&mut self, processor: Box<dyn Processor>) {
79        self.processor_chain().append_processor(processor);
80    }
81    async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
82    async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
83    async fn update_remote_description_force(&mut self, answer: &String) -> Result<()> {
84        // Default implementation: force update is same as regular update for most tracks
85        self.update_remote_description(answer).await
86    }
87    async fn start(
88        &mut self,
89        event_sender: EventSender,
90        packet_sender: TrackPacketSender,
91    ) -> Result<()>;
92    async fn stop(&self) -> Result<()>;
93    async fn stop_graceful(&self) -> Result<()> {
94        self.stop().await
95    }
96    async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
97}