1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::event::EventSender;
use crate::media::processor::{Processor, ProcessorChain};
use crate::media::{AudioFrame, TrackId};
use anyhow::Result;
use async_trait::async_trait;
use audio_codec::CodecType;
use tokio::sync::mpsc;
use tokio::time::Duration;
use tokio_util::sync::CancellationToken;
pub type TrackPacketSender = mpsc::UnboundedSender<AudioFrame>;
pub type TrackPacketReceiver = mpsc::UnboundedReceiver<AudioFrame>;
// New shared track configuration struct
#[derive(Debug, Clone)]
pub struct TrackConfig {
pub codec: CodecType,
// Packet time in milliseconds (typically 10, 20, or 30ms)
pub ptime: Duration,
// Sample rate for PCM audio (e.g., 8000, 16000, 48000)
pub samplerate: u32,
// Number of audio channels (1 for mono, 2 for stereo)
pub channels: u16,
}
impl Default for TrackConfig {
fn default() -> Self {
Self {
codec: CodecType::G722,
samplerate: 16000,
channels: 1,
ptime: Duration::from_millis(20),
}
}
}
impl TrackConfig {
pub fn with_ptime(mut self, ptime: Duration) -> Self {
self.ptime = ptime;
self
}
pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
self.samplerate = sample_rate;
self
}
pub fn with_channels(mut self, channels: u16) -> Self {
self.channels = channels;
self
}
}
pub mod dtmf;
pub mod file;
pub mod forwarding;
pub mod media_pass;
pub mod rtc;
pub mod track_codec;
pub mod tts;
pub mod websocket;
#[async_trait]
pub trait Track: Send + Sync {
fn ssrc(&self) -> u32;
fn id(&self) -> &TrackId;
/// The track's lifecycle token: cancelled by `stop()`/`stop_graceful()`
/// (directly or through the track's own event loop teardown).
///
/// Per-track background workers spawned on behalf of this track (e.g. the
/// inactivity watchdog created by `setup_track_with_stream`) MUST derive
/// their lifetime from this token, so that stopping or replacing the
/// track disarms them. Binding those workers to the session token instead
/// leaves "zombie" watchdogs behind: a retired leg keeps its frozen
/// last-frame timestamp and eventually fires `SessionEvent::Inactivity`
/// against a call that is alive and well (seen with INVITE-mode
/// auto-refer transfers, where the superseded bridge leg's watchdog
/// killed the session ~50s after a successful transfer).
fn cancel_token(&self) -> CancellationToken;
fn config(&self) -> &TrackConfig;
fn set_paused(&self, _paused: bool) -> bool {
false
}
fn is_paused(&self) -> bool {
false
}
fn processor_chain(&mut self) -> &mut ProcessorChain;
/// Cancel the track's media pipeline (per-track workers, inactivity
/// watchdog) WITHOUT closing the underlying transport. Used when a track
/// is replaced by a successor: the successor's media setup must not race
/// against the predecessor's transport teardown.
/// The full `stop()` (which closes the transport) follows afterwards.
fn cancel_pipeline(&self) {
// Default: nothing to cancel for tracks without a pipeline.
}
fn insert_processor(&mut self, processor: Box<dyn Processor>) {
self.processor_chain().insert_processor(processor);
}
fn append_processor(&mut self, processor: Box<dyn Processor>) {
self.processor_chain().append_processor(processor);
}
/// Attach a raw-tap receiver that mirrors inbound frames at their native
/// (pre-resample) sample rate, used by the native-samplerate recorder.
/// Setting `None` detaches an existing tap.
fn set_raw_tap(&mut self, tap: Option<mpsc::UnboundedSender<AudioFrame>>) {
// Record this track's id as the tap owner so relayed frames (e.g. the
// caller's audio forwarded through the media-pass track) are not
// mirrored a second time.
let owner = self.id().clone();
let chain = self.processor_chain();
chain.set_raw_tap(tap);
chain.set_tap_owner(Some(owner));
}
async fn handshake(&mut self, offer: String, timeout: Option<Duration>) -> Result<String>;
async fn update_remote_description(&mut self, answer: &String) -> Result<()>;
async fn update_remote_description_force(&mut self, answer: &String) -> Result<()> {
// Default implementation: force update is same as regular update for most tracks
self.update_remote_description(answer).await
}
/// Apply a provisional remote description (SIP 183 early media). Unlike
/// `update_remote_description`, this must not finalize SDP negotiation —
/// the real answer (200 OK) is still to come. Default: same as a regular
/// update, for track types that don't have a signaling state machine.
async fn update_remote_description_provisional(&mut self, answer: &String) -> Result<()> {
self.update_remote_description(answer).await
}
async fn start(
&mut self,
event_sender: EventSender,
packet_sender: TrackPacketSender,
) -> Result<()>;
async fn stop(&self) -> Result<()>;
async fn stop_graceful(&self) -> Result<()> {
self.stop().await
}
async fn send_packet(&mut self, packet: &AudioFrame) -> Result<()>;
/// Feed a remotely-gathered ICE candidate into this track's PeerConnection.
/// Default no-op for track types that aren't backed by ICE (file/tts/websocket tracks).
fn add_ice_candidate(
&self,
_candidate: &str,
_sdp_mid: Option<&str>,
_sdp_mline_index: Option<u32>,
) -> Result<()> {
Ok(())
}
/// The call was answered locally: Accept sent the 200 OK, or a WebRTC
/// track answered the browser's offer. `rtp_timeout`, when given,
/// replaces the configured RTP timeout. Default no-op for track types
/// without RTP timeout monitoring.
fn on_answered(&self, _rtp_timeout: Option<Duration>) {}
/// The track is done for good (stopped, or lost its transport), though
/// it may stay registered until replaced. Default: never.
fn has_ended(&self) -> bool {
false
}
}