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 media_pass;
58pub mod rtc;
59pub mod track_codec;
60pub mod tts;
61pub mod websocket;
62#[async_trait]
63pub trait Track: Send + Sync {
64    fn ssrc(&self) -> u32;
65    fn id(&self) -> &TrackId;
66    fn config(&self) -> &TrackConfig;
67    fn processor_chain(&mut self) -> &mut ProcessorChain;
68    fn insert_processor(&mut self, processor: Box<dyn Processor>) {
69        self.processor_chain().insert_processor(processor);
70    }
71    fn append_processor(&mut self, processor: Box<dyn Processor>) {
72        self.processor_chain().append_processor(processor);
73    }
74    async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
75    async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
76    async fn update_remote_description_force(&mut self, answer: &String) -> Result<()> {
77        // Default implementation: force update is same as regular update for most tracks
78        self.update_remote_description(answer).await
79    }
80    async fn start(
81        &mut self,
82        event_sender: EventSender,
83        packet_sender: TrackPacketSender,
84    ) -> Result<()>;
85    async fn stop(&self) -> Result<()>;
86    async fn stop_graceful(&self) -> Result<()> {
87        self.stop().await
88    }
89    async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
90}