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            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 dtmf;
54pub mod file;
55pub mod forwarding;
56pub mod media_pass;
57pub mod rtc;
58pub mod track_codec;
59pub mod tts;
60pub mod websocket;
61#[async_trait]
62pub trait Track: Send + Sync {
63    fn ssrc(&self) -> u32;
64    fn id(&self) -> &TrackId;
65    fn config(&self) -> &TrackConfig;
66    fn set_paused(&self, _paused: bool) -> bool {
67        false
68    }
69    fn is_paused(&self) -> bool {
70        false
71    }
72    fn processor_chain(&mut self) -> &mut ProcessorChain;
73    fn insert_processor(&mut self, processor: Box<dyn Processor>) {
74        self.processor_chain().insert_processor(processor);
75    }
76    fn append_processor(&mut self, processor: Box<dyn Processor>) {
77        self.processor_chain().append_processor(processor);
78    }
79    /// Attach a raw-tap receiver that mirrors inbound frames at their native
80    /// (pre-resample) sample rate, used by the native-samplerate recorder.
81    /// Setting `None` detaches an existing tap.
82    fn set_raw_tap(&mut self, tap: Option<mpsc::UnboundedSender<AudioFrame>>) {
83        // Record this track's id as the tap owner so relayed frames (e.g. the
84        // caller's audio forwarded through the media-pass track) are not
85        // mirrored a second time.
86        let owner = self.id().clone();
87        let chain = self.processor_chain();
88        chain.set_raw_tap(tap);
89        chain.set_tap_owner(Some(owner));
90    }
91    async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
92    async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
93    async fn update_remote_description_force(&mut self, answer: &String) -> Result<()> {
94        // Default implementation: force update is same as regular update for most tracks
95        self.update_remote_description(answer).await
96    }
97    /// Apply a provisional remote description (SIP 183 early media). Unlike
98    /// `update_remote_description`, this must not finalize SDP negotiation —
99    /// the real answer (200 OK) is still to come. Default: same as a regular
100    /// update, for track types that don't have a signaling state machine.
101    async fn update_remote_description_provisional(&mut self, answer: &String) -> Result<()> {
102        self.update_remote_description(answer).await
103    }
104    async fn start(
105        &mut self,
106        event_sender: EventSender,
107        packet_sender: TrackPacketSender,
108    ) -> Result<()>;
109    async fn stop(&self) -> Result<()>;
110    async fn stop_graceful(&self) -> Result<()> {
111        self.stop().await
112    }
113    async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
114    /// Feed a remotely-gathered ICE candidate into this track's PeerConnection.
115    /// Default no-op for track types that aren't backed by ICE (file/tts/websocket tracks).
116    fn add_ice_candidate(
117        &self,
118        _candidate: &str,
119        _sdp_mid: Option<&str>,
120        _sdp_mline_index: Option<u32>,
121    ) -> Result<()> {
122        Ok(())
123    }
124    /// The call was answered locally: Accept sent the 200 OK, or a WebRTC
125    /// track answered the browser's offer. `rtp_timeout`, when given,
126    /// replaces the configured RTP timeout. Default no-op for track types
127    /// without RTP timeout monitoring.
128    fn on_answered(&self, _rtp_timeout: Option<Duration>) {}
129}