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            codec: CodecType::Opus,
29            ptime: Duration::from_millis(20),
30            samplerate: 16000,
31            channels: 1,
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 media_pass;
55pub mod rtc;
56pub mod track_codec;
57pub mod tts;
58pub mod websocket;
59#[async_trait]
60pub trait Track: Send + Sync {
61    fn ssrc(&self) -> u32;
62    fn id(&self) -> &TrackId;
63    fn config(&self) -> &TrackConfig;
64    fn processor_chain(&mut self) -> &mut ProcessorChain;
65    fn insert_processor(&mut self, processor: Box<dyn Processor>) {
66        self.processor_chain().insert_processor(processor);
67    }
68    fn append_processor(&mut self, processor: Box<dyn Processor>) {
69        self.processor_chain().append_processor(processor);
70    }
71    async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
72    async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
73    async fn start(
74        &mut self,
75        event_sender: EventSender,
76        packet_sender: TrackPacketSender,
77    ) -> Result<()>;
78    async fn stop(&self) -> Result<()>;
79    async fn stop_graceful(&self) -> Result<()> {
80        self.stop().await
81    }
82    async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
83}