voice_engine/media/track/
mod.rs

1use super::codecs::CodecType;
2use crate::event::EventSender;
3use crate::media::processor::{Processor, ProcessorChain};
4use crate::media::{AudioFrame, TrackId};
5use anyhow::Result;
6use async_trait::async_trait;
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::PCMU,
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 rtp;
56pub mod track_codec;
57pub mod tts;
58pub mod webrtc;
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 processor_chain(&mut self) -> &mut ProcessorChain;
66    fn insert_processor(&mut self, processor: Box<dyn Processor>) {
67        self.processor_chain().insert_processor(processor);
68    }
69    fn append_processor(&mut self, processor: Box<dyn Processor>) {
70        self.processor_chain().append_processor(processor);
71    }
72    async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
73    async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
74    async fn start(
75        &self,
76        event_sender: EventSender,
77        packet_sender: TrackPacketSender,
78    ) -> Result<()>;
79    async fn stop(&self) -> Result<()>;
80    async fn stop_graceful(&self) -> Result<()> {
81        self.stop().await
82    }
83    async fn send_packet(&self, packet: &AudioFrame) -> Result<()>;
84}