rustpbx 0.3.18

A SIP PBX implementation in Rust
Documentation
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
use anyhow::{Result, anyhow};
use async_trait::async_trait;
use audio_codec::CodecType;
use rustrtc::{
    Attribute, IceServer, MediaKind, PeerConnection, RtcConfiguration, RtpCodecParameters, SdpType,
    SessionDescription, TransceiverDirection, TransportMode,
};
use std::{
    collections::{HashMap, HashSet},
    sync::{Arc, Mutex},
};
use tokio::sync::Mutex as AsyncMutex;
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};
pub use transcoder::Transcoder;

use crate::media::recorder::RecorderOption;

pub mod audio_source;
#[cfg(test)]
mod file_track_tests;
pub mod negotiate;
pub mod transcoder;
#[cfg(test)]
mod unified_pc_tests;
pub mod wav_writer;

pub trait StreamWriter: Send + Sync {
    fn write_header(&mut self) -> Result<()>;
    fn write_packet(&mut self, data: &[u8], samples: usize) -> Result<()>;
    fn finalize(&mut self) -> Result<()>;
}

pub fn get_timestamp() -> u64 {
    let now = std::time::SystemTime::now();
    now.duration_since(std::time::UNIX_EPOCH)
        .expect("Time went backwards")
        .as_millis() as u64
}

#[async_trait]
pub trait Track: Send + Sync {
    fn id(&self) -> &str;
    async fn handshake(&self, remote_offer: String) -> Result<String>;
    async fn local_description(&self) -> Result<String>;
    async fn set_remote_description(&self, remote: &str) -> Result<()>;
    async fn stop(&self);
    async fn get_peer_connection(&self) -> Option<rustrtc::PeerConnection>;
    async fn set_recorder_option(&mut self, _option: RecorderOption) {}
    fn set_codec_preference(&mut self, _codecs: Vec<CodecType>) {
        // Optional: override to set codec preference
    }

    /// Allow downcasting to concrete types for dynamic audio source switching
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        unimplemented!("as_any_mut not implemented for this Track type")
    }
}

pub struct MediaStreamBuilder {
    id: Option<String>,
    cancel_token: Option<CancellationToken>,
    recorder_option: Option<RecorderOption>,
}

impl MediaStreamBuilder {
    pub fn new() -> Self {
        Self {
            id: None,
            cancel_token: None,
            recorder_option: None,
        }
    }

    pub fn with_id(mut self, id: String) -> Self {
        self.id = Some(id);
        self
    }

    pub fn with_cancel_token(mut self, token: CancellationToken) -> Self {
        self.cancel_token = Some(token);
        self
    }

    pub fn with_recorder_config(mut self, option: RecorderOption) -> Self {
        self.recorder_option = Some(option);
        self
    }

    pub fn build(self) -> MediaStream {
        MediaStream {
            id: self.id.unwrap_or_else(|| "media-stream".to_string()),
            cancel_token: self.cancel_token.unwrap_or_else(CancellationToken::new),
            tracks: Mutex::new(HashMap::new()),
            suppressed: Mutex::new(HashSet::new()),
            recorder_option: self.recorder_option,
        }
    }
}

pub struct MediaStream {
    pub id: String,
    pub cancel_token: CancellationToken,
    tracks: Mutex<HashMap<String, Arc<AsyncMutex<Box<dyn Track>>>>>,
    suppressed: Mutex<HashSet<String>>,
    pub recorder_option: Option<RecorderOption>,
}

impl MediaStream {
    pub async fn serve(&self) -> Result<()> {
        self.cancel_token.cancelled().await;
        Ok(())
    }

    pub async fn update_track(&self, mut track: Box<dyn Track>, play_id: Option<String>) {
        if let Some(ref option) = self.recorder_option {
            track.set_recorder_option(option.clone()).await;
        }
        let id = track.id().to_string();
        let wrapped = Arc::new(AsyncMutex::new(track));
        {
            let mut tracks = self.tracks.lock().unwrap();
            tracks.insert(id.clone(), wrapped.clone());
        }
        if let Some(play_id) = play_id {
            debug!(track_id = %id, play_id = %play_id, "track updated (playback id)");
        }
    }

    pub async fn get_tracks(&self) -> Vec<Arc<AsyncMutex<Box<dyn Track>>>> {
        let tracks = self.tracks.lock().unwrap();
        tracks.values().cloned().collect()
    }

    pub async fn update_remote_description(&self, track_id: &str, remote: &str) -> Result<()> {
        let handle = {
            let tracks = self.tracks.lock().unwrap();
            tracks.get(track_id).cloned()
        };
        let Some(track) = handle else {
            return Err(anyhow!("track not found: {track_id}"));
        };
        let guard = track.lock().await;
        guard.set_remote_description(remote).await
    }

    pub async fn suppress_forwarding(&self, track_id: &str) {
        let mut suppressed = self.suppressed.lock().unwrap();
        suppressed.insert(track_id.to_string());
    }

    pub async fn resume_forwarding(&self, track_id: &str) {
        let mut suppressed = self.suppressed.lock().unwrap();
        suppressed.remove(track_id);
    }

    pub fn is_suppressed(&self, track_id: &str) -> bool {
        let suppressed = self.suppressed.lock().unwrap();
        suppressed.contains(track_id)
    }

    pub async fn remove_track(&self, track_id: &str, _stop_audio_immediately: bool) {
        let mut tracks = self.tracks.lock().unwrap();
        tracks.remove(track_id);
    }
}

pub struct RtcTrack {
    track_id: String,
    pc: PeerConnection,
    pub recorder_option: Option<RecorderOption>,
    rtp_map: Vec<negotiate::CodecInfo>,
}

impl RtcTrack {
    pub fn new(
        track_id: String,
        config: RtcConfiguration,
        rtp_map: Vec<negotiate::CodecInfo>,
    ) -> Self {
        let pc = PeerConnection::new(config);

        // Add a dummy track to ensure a sender is created and SSRC is signaled in SDP
        let (_, track, _) =
            rustrtc::media::track::sample_track(rustrtc::media::MediaKind::Audio, 100);
        let mut params = RtpCodecParameters::default();
        if let Some(info) = rtp_map.first() {
            params.payload_type = info.payload_type;
            params.clock_rate = info.clock_rate;
            params.channels = info.channels as u8;
        }
        let _ = pc.add_track(track, params);

        Self {
            track_id,
            pc,
            recorder_option: None,
            rtp_map,
        }
    }

    pub fn with_recorder_option(mut self, option: RecorderOption) -> Self {
        self.recorder_option = Some(option);
        self
    }

    async fn set_local(&self, pc: &PeerConnection, mut desc: SessionDescription) -> Result<String> {
        if !self.rtp_map.is_empty() {
            if let Some(section) = desc
                .media_sections
                .iter_mut()
                .find(|m| m.kind == MediaKind::Audio)
            {
                section.formats.clear();
                section
                    .attributes
                    .retain(|a| a.key != "rtpmap" && a.key != "fmtp");

                // Build RTP map from codec preference list
                let mut seen_pts = HashSet::new();
                for info in self.rtp_map.iter() {
                    let pt = info.payload_type;
                    if !seen_pts.insert(pt) {
                        continue;
                    }
                    section.formats.push(pt.to_string());

                    section.attributes.push(Attribute {
                        key: "rtpmap".to_string(),
                        value: Some(format!("{} {}", pt, info.codec.rtpmap())),
                    });
                    if let Some(fmtp) = info.codec.fmtp() {
                        section.attributes.push(Attribute {
                            key: "fmtp".to_string(),
                            value: Some(format!("{} {}", pt, fmtp)),
                        });
                    }
                }
            }
        }
        pc.set_local_description(desc)?;
        let desc = pc
            .local_description()
            .ok_or_else(|| anyhow!("missing local description"))?;
        Ok(desc.to_sdp_string())
    }

    async fn set_remote(&self, pc: &PeerConnection, sdp: &str, ty: SdpType) -> Result<()> {
        let desc = SessionDescription::parse(ty, sdp)
            .map_err(|e| anyhow!("failed to parse sdp: {:?}", e))?;
        match pc.set_remote_description(desc).await {
            Ok(_) => (),
            Err(e) => {
                warn!(
                    track_id = self.track_id,
                    error = %e,
                    "failed to set remote description"
                );
            }
        }
        Ok(())
    }
}

#[async_trait]
impl Track for RtcTrack {
    fn id(&self) -> &str {
        &self.track_id
    }

    async fn handshake(&self, remote_offer: String) -> Result<String> {
        self.pc.wait_for_gathering_complete().await;
        self.set_remote(&self.pc, &remote_offer, SdpType::Offer)
            .await?;
        let answer = self.pc.create_answer().await?;
        let sdp = self.set_local(&self.pc, answer).await?;
        Ok(sdp)
    }

    async fn local_description(&self) -> Result<String> {
        self.pc.wait_for_gathering_complete().await;
        match self.pc.create_offer().await {
            Ok(offer) => {
                let sdp = self.set_local(&self.pc, offer).await?;
                Ok(sdp)
            }
            Err(e) => {
                let err_str = e.to_string();
                if err_str.contains("HaveLocalOffer") {
                    if let Some(desc) = self.pc.local_description() {
                        return Ok(desc.to_sdp_string());
                    }
                }
                Err(anyhow!(e))
            }
        }
    }

    async fn set_remote_description(&self, remote: &str) -> Result<()> {
        self.pc.wait_for_gathering_complete().await;
        self.set_remote(&self.pc, remote, SdpType::Answer).await
    }

    async fn stop(&self) {
        self.pc.close();
    }

    async fn get_peer_connection(&self) -> Option<PeerConnection> {
        Some(self.pc.clone())
    }
}

pub mod recorder;

#[cfg(test)]
mod recorder_tests;

pub struct RtpTrackBuilder {
    track_id: String,
    cancel_token: Option<CancellationToken>,
    external_ip: Option<String>,
    rtp_start_port: Option<u16>,
    rtp_end_port: Option<u16>,
    mode: TransportMode,
    rtp_map: Vec<negotiate::CodecInfo>,
    enable_latching: bool,
    ice_servers: Vec<IceServer>,
}

impl RtpTrackBuilder {
    pub fn new(track_id: String) -> Self {
        Self {
            track_id,
            cancel_token: None,
            external_ip: None,
            rtp_start_port: None,
            rtp_end_port: None,
            mode: TransportMode::Rtp,
            enable_latching: false,
            ice_servers: Vec::new(),
            rtp_map: vec![
                #[cfg(feature = "opus")]
                CodecType::Opus,
                CodecType::G729,
                CodecType::G722,
                CodecType::PCMU,
                CodecType::PCMA,
                CodecType::TelephoneEvent,
            ]
            .into_iter()
            .map(|c| negotiate::CodecInfo {
                payload_type: c.payload_type(),
                clock_rate: c.clock_rate(),
                channels: c.channels() as u16,
                codec: c,
            })
            .collect(),
        }
    }

    pub fn with_cancel_token(mut self, token: CancellationToken) -> Self {
        self.cancel_token = Some(token);
        self
    }
    pub fn with_rtp_range(mut self, start: u16, end: u16) -> Self {
        self.rtp_start_port = Some(start);
        self.rtp_end_port = Some(end);
        self
    }

    pub fn with_mode(mut self, mode: TransportMode) -> Self {
        self.mode = mode;
        self
    }

    pub fn with_external_ip(mut self, addr: String) -> Self {
        self.external_ip = Some(addr);
        self
    }

    pub fn with_codec_preference(mut self, codecs: Vec<CodecType>) -> Self {
        self.rtp_map = codecs
            .into_iter()
            .map(|c| negotiate::CodecInfo {
                payload_type: c.payload_type(),
                clock_rate: c.clock_rate(),
                channels: c.channels() as u16,
                codec: c,
            })
            .collect();
        self
    }

    pub fn with_codec_info(mut self, codecs: Vec<negotiate::CodecInfo>) -> Self {
        self.rtp_map = codecs;
        self
    }

    pub fn with_enable_latching(mut self, enable: bool) -> Self {
        self.enable_latching = enable;
        self
    }

    pub fn with_ice_servers(mut self, servers: Vec<IceServer>) -> Self {
        self.ice_servers = servers;
        self
    }

    pub fn build(self) -> RtcTrack {
        let config = RtcConfiguration {
            ice_servers: self.ice_servers,
            transport_mode: self.mode,
            rtp_start_port: self.rtp_start_port,
            rtp_end_port: self.rtp_end_port,
            external_ip: self.external_ip,
            enable_latching: self.enable_latching,
            ..Default::default()
        };

        RtcTrack::new(self.track_id, config, self.rtp_map)
    }
}

/// Audio file playback track with loop support
///
/// Used for playing audio files (e.g., ringback tones, hold music, announcements).
#[derive(Clone)]
pub struct FileTrack {
    track_id: String,
    file_path: Option<String>,
    loop_playback: bool,
    cancel_token: CancellationToken,
    pc: PeerConnection,
    completion_notify: Arc<tokio::sync::Notify>,
    codec_preference: Vec<CodecType>,
    mode: TransportMode,
    rtp_start_port: Option<u16>,
    rtp_end_port: Option<u16>,
    external_ip: Option<String>,
    audio_source_manager: Option<Arc<audio_source::AudioSourceManager>>,
}

impl FileTrack {
    pub fn new(track_id: String) -> Self {
        let config = RtcConfiguration {
            transport_mode: TransportMode::Rtp,
            ..Default::default()
        };

        let pc = PeerConnection::new(config);
        pc.add_transceiver(MediaKind::Audio, TransceiverDirection::SendOnly);

        Self {
            track_id,
            file_path: None,
            loop_playback: false,
            cancel_token: CancellationToken::new(),
            pc,
            completion_notify: Arc::new(tokio::sync::Notify::new()),
            codec_preference: vec![CodecType::PCMU, CodecType::PCMA],
            mode: TransportMode::Rtp,
            rtp_start_port: None,
            rtp_end_port: None,
            external_ip: None,
            audio_source_manager: None,
        }
    }

    pub fn with_path(mut self, path: String) -> Self {
        self.file_path = Some(path);
        self
    }

    pub fn with_loop(mut self, loop_playback: bool) -> Self {
        self.loop_playback = loop_playback;
        self
    }

    pub fn with_cancel_token(mut self, token: CancellationToken) -> Self {
        self.cancel_token = token;
        self
    }

    pub fn with_codec_preference(mut self, codecs: Vec<CodecType>) -> Self {
        self.codec_preference = codecs;
        self
    }

    pub fn with_mode(mut self, mode: TransportMode) -> Self {
        self.mode = mode;
        self.recreate_pc();
        self
    }

    pub fn with_rtp_range(mut self, start: u16, end: u16) -> Self {
        self.rtp_start_port = Some(start);
        self.rtp_end_port = Some(end);
        self.recreate_pc();
        self
    }

    pub fn with_external_ip(mut self, ip: String) -> Self {
        self.external_ip = Some(ip);
        self.recreate_pc();
        self
    }

    fn recreate_pc(&mut self) {
        let config = RtcConfiguration {
            transport_mode: self.mode.clone(),
            rtp_start_port: self.rtp_start_port,
            rtp_end_port: self.rtp_end_port,
            external_ip: self.external_ip.clone(),
            ..Default::default()
        };

        self.pc = PeerConnection::new(config);
        self.pc
            .add_transceiver(MediaKind::Audio, TransceiverDirection::SendOnly);
    }

    pub fn with_ssrc(self, _ssrc: u32) -> Self {
        self
    }

    pub async fn wait_for_completion(&self) {
        self.completion_notify.notified().await;
    }

    fn init_audio_source(&mut self) -> Result<()> {
        if self.audio_source_manager.is_some() {
            return Ok(());
        }

        let target_sample_rate = self
            .codec_preference
            .first()
            .map(|c| c.clock_rate())
            .unwrap_or(8000);

        let manager = Arc::new(audio_source::AudioSourceManager::new(target_sample_rate));

        if let Some(ref path) = self.file_path {
            manager.switch_to_file(path.clone(), self.loop_playback)?;
        } else {
            manager.switch_to_silence();
        }

        self.audio_source_manager = Some(manager);
        Ok(())
    }

    /// Start audio playback task
    ///
    pub async fn start_playback(&self) -> Result<()> {
        let file_path = self
            .file_path
            .as_ref()
            .ok_or_else(|| anyhow!("No file path set"))?;

        if !std::path::Path::new(file_path).exists() {
            return Err(anyhow!("Audio file not found: {}", file_path));
        }

        debug!(
            file = %file_path,
            loop_playback = self.loop_playback,
            "FileTrack playback started"
        );

        let completion_notify = self.completion_notify.clone();
        let loop_playback = self.loop_playback;
        let cancel_token = self.cancel_token.clone();

        crate::utils::spawn(async move {
            if !loop_playback {
                tokio::select! {
                    _ = tokio::time::sleep(tokio::time::Duration::from_millis(100)) => {
                        debug!("FileTrack playback completed");
                    }
                    _ = cancel_token.cancelled() => {
                        debug!("FileTrack playback cancelled");
                    }
                }
                completion_notify.notify_waiters();
            } else {
                cancel_token.cancelled().await;
                debug!("FileTrack playback stopped");
            }
        });

        Ok(())
    }

    pub fn switch_audio_source(&mut self, file_path: String, loop_playback: bool) -> Result<()> {
        if self.audio_source_manager.is_none() {
            self.init_audio_source()?;
        }

        if let Some(ref manager) = self.audio_source_manager {
            manager.switch_to_file(file_path, loop_playback)?;
        }

        Ok(())
    }

    pub fn switch_to_silence(&mut self) {
        if let Some(ref manager) = self.audio_source_manager {
            manager.switch_to_silence();
        }
    }
}

#[async_trait]
impl Track for FileTrack {
    fn id(&self) -> &str {
        &self.track_id
    }

    async fn handshake(&self, remote_offer: String) -> Result<String> {
        self.pc.wait_for_gathering_complete().await;

        let offer = SessionDescription::parse(SdpType::Offer, &remote_offer)?;

        self.pc.set_remote_description(offer).await?;
        let answer = self.pc.create_answer().await?;
        self.pc.set_local_description(answer.clone())?;

        Ok(answer.to_sdp_string())
    }

    async fn local_description(&self) -> Result<String> {
        self.pc.wait_for_gathering_complete().await;

        let mut offer = self.pc.create_offer().await?;

        if !self.codec_preference.is_empty() {
            if let Some(section) = offer
                .media_sections
                .iter_mut()
                .find(|m| m.kind == MediaKind::Audio)
            {
                section.formats.clear();
                section
                    .attributes
                    .retain(|a| a.key != "rtpmap" && a.key != "fmtp");

                let mut seen_pts = HashSet::new();
                for codec in &self.codec_preference {
                    let pt = codec.payload_type();
                    if !seen_pts.insert(pt) {
                        continue;
                    }
                    let pt_str = pt.to_string();
                    section.formats.push(pt_str.clone());

                    section.attributes.push(Attribute {
                        key: "rtpmap".to_string(),
                        value: Some(format!("{} {}", pt_str, codec.rtpmap())),
                    });
                    if let Some(fmtp) = codec.fmtp() {
                        section.attributes.push(Attribute {
                            key: "fmtp".to_string(),
                            value: Some(format!("{} {}", pt_str, fmtp)),
                        });
                    }
                }
            }
        }

        self.pc.set_local_description(offer.clone())?;
        Ok(offer.to_sdp_string())
    }

    async fn set_remote_description(&self, remote: &str) -> Result<()> {
        self.pc.wait_for_gathering_complete().await;
        let desc = SessionDescription::parse(SdpType::Answer, remote)?;
        self.pc.set_remote_description(desc).await?;
        Ok(())
    }

    async fn stop(&self) {
        self.cancel_token.cancel();
    }

    async fn get_peer_connection(&self) -> Option<PeerConnection> {
        Some(self.pc.clone())
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

#[cfg(test)]
mod media_track_tests;