Skip to main content

active_call/media/track/
file.rs

1use crate::callrecord::CallRecordHangupReason;
2use crate::event::{EventSender, SessionEvent};
3use crate::media::processor::ProcessorChain;
4use crate::media::track::{Track, TrackConfig, TrackPacketSender};
5use crate::media::{AudioFrame, PcmBuf, Samples, TrackId};
6use anyhow::{Result, anyhow};
7use async_trait::async_trait;
8use audio_codec::BoxedResampler;
9use hound::WavReader;
10use std::cmp::min;
11use std::fs::File;
12use std::io::BufReader;
13use std::sync::{
14    Arc,
15    atomic::{AtomicBool, Ordering},
16};
17use std::time::Instant;
18use tokio::select;
19use tokio::time::Duration;
20use tokio_util::sync::CancellationToken;
21use tracing::{debug, info, warn};
22
23trait AudioReader: Send {
24    fn fill_buffer(&mut self) -> Result<usize>;
25
26    fn read_chunk(&mut self, packet_duration_ms: u32) -> Result<Option<(PcmBuf, u32)>> {
27        let max_chunk_size = self.sample_rate() as usize * packet_duration_ms as usize / 1000;
28
29        if self.buffer_size() == 0 || self.position() >= self.buffer_size() {
30            let samples_read = self.fill_buffer()?;
31            if samples_read == 0 {
32                return Ok(None);
33            }
34            self.set_position(0);
35        }
36
37        let remaining = self.buffer_size() - self.position();
38        if remaining == 0 {
39            return Ok(None);
40        }
41
42        let chunk_size = min(max_chunk_size, remaining);
43        let end_pos = self.position() + chunk_size;
44
45        assert!(
46            end_pos <= self.buffer_size(),
47            "Buffer overrun: pos={}, end={}, size={}",
48            self.position(),
49            end_pos,
50            self.buffer_size()
51        );
52
53        let chunk = self.extract_chunk(self.position(), end_pos);
54        self.set_position(end_pos);
55
56        let final_chunk =
57            if self.sample_rate() != self.target_sample_rate() && self.sample_rate() > 0 {
58                self.resample_chunk(&chunk)
59            } else {
60                chunk
61            };
62
63        Ok(Some((final_chunk, self.target_sample_rate())))
64    }
65
66    fn buffer_size(&self) -> usize;
67    fn position(&self) -> usize;
68    fn set_position(&mut self, pos: usize);
69    fn sample_rate(&self) -> u32;
70    fn target_sample_rate(&self) -> u32;
71    fn channels(&self) -> u16;
72    fn extract_chunk(&self, start: usize, end: usize) -> Vec<i16>;
73    fn resample_chunk(&mut self, chunk: &[i16]) -> Vec<i16>;
74}
75
76struct DecodedAudioReader {
77    buffer: Vec<i16>,
78    sample_rate: u32,
79    position: usize,
80    target_sample_rate: u32,
81    resampler: Option<BoxedResampler>,
82}
83
84impl DecodedAudioReader {
85    #[cfg(test)]
86    fn from_file(
87        file: File,
88        extension: &str,
89        mime_type: Option<&str>,
90        target_sample_rate: u32,
91    ) -> Result<Self> {
92        let all_samples =
93            crate::media::loader::decode_audio(file, extension, mime_type, target_sample_rate)?;
94        Ok(Self {
95            buffer: all_samples,
96            sample_rate: target_sample_rate,
97            position: 0,
98            target_sample_rate,
99            resampler: None,
100        })
101    }
102
103    /// Build a reader from already-decoded PCM samples. The samples are assumed
104    /// to already be at `sample_rate`, so no further resampling happens when it
105    /// matches `target_sample_rate`.
106    fn from_samples(buffer: Vec<i16>, sample_rate: u32, target_sample_rate: u32) -> Self {
107        Self {
108            buffer,
109            sample_rate,
110            position: 0,
111            target_sample_rate,
112            resampler: None,
113        }
114    }
115}
116
117impl AudioReader for DecodedAudioReader {
118    fn fill_buffer(&mut self) -> Result<usize> {
119        if self.position >= self.buffer.len() {
120            return Ok(0);
121        }
122        Ok(self.buffer.len() - self.position)
123    }
124
125    fn buffer_size(&self) -> usize {
126        self.buffer.len()
127    }
128
129    fn position(&self) -> usize {
130        self.position
131    }
132
133    fn set_position(&mut self, pos: usize) {
134        self.position = pos;
135    }
136
137    fn sample_rate(&self) -> u32 {
138        self.sample_rate
139    }
140
141    fn target_sample_rate(&self) -> u32 {
142        self.target_sample_rate
143    }
144
145    fn channels(&self) -> u16 {
146        1
147    }
148
149    fn extract_chunk(&self, start: usize, end: usize) -> Vec<i16> {
150        self.buffer[start..end].to_vec()
151    }
152
153    fn resample_chunk(&mut self, chunk: &[i16]) -> Vec<i16> {
154        if self.sample_rate == 0 || self.sample_rate == self.target_sample_rate {
155            return chunk.to_vec();
156        }
157
158        if let Some(resampler) = &mut self.resampler {
159            resampler.resample(chunk)
160        } else {
161            let mut new_resampler =
162                BoxedResampler::new(self.sample_rate as usize, self.target_sample_rate as usize)
163                    .expect("invalid sample rate");
164            let result = new_resampler.resample(chunk);
165            self.resampler = Some(new_resampler);
166            result
167        }
168    }
169}
170
171// Unified function to process any audio reader and stream audio
172async fn process_audio_reader(
173    mut processor_chain: ProcessorChain,
174    mut audio_reader: Box<dyn AudioReader>,
175    track_id: &str,
176    packet_duration_ms: u32,
177    target_sample_rate: u32,
178    token: CancellationToken,
179    paused: Arc<AtomicBool>,
180    packet_sender: TrackPacketSender,
181) -> Result<()> {
182    info!(
183        "streaming audio with target_sample_rate: {}, packet_duration: {}ms",
184        target_sample_rate, packet_duration_ms
185    );
186    let stream_loop = async move {
187        let start_time = Instant::now();
188        let mut ticker = tokio::time::interval(Duration::from_millis(packet_duration_ms as u64));
189        let channels = audio_reader.channels();
190        loop {
191            if paused.load(Ordering::Relaxed) {
192                ticker.tick().await;
193                continue;
194            }
195
196            let Some((chunk, chunk_sample_rate)) = audio_reader.read_chunk(packet_duration_ms)?
197            else {
198                break;
199            };
200
201            let mut packet = AudioFrame {
202                track_id: track_id.to_string(),
203                timestamp: crate::media::get_timestamp(),
204                samples: Samples::PCM { samples: chunk },
205                sample_rate: chunk_sample_rate,
206                channels,
207                ..Default::default()
208            };
209
210            match processor_chain.process_frame(&mut packet) {
211                Ok(_) => {}
212                Err(e) => {
213                    warn!("failed to process audio packet: {}", e);
214                }
215            }
216
217            if let Err(e) = packet_sender.send(packet) {
218                warn!("failed to send audio packet: {}", e);
219                break;
220            }
221
222            ticker.tick().await;
223        }
224
225        info!("stream loop finished in {:?}", start_time.elapsed());
226        Ok(()) as Result<()>
227    };
228
229    select! {
230        _ = token.cancelled() => {
231            info!("stream cancelled");
232            return Ok(());
233        }
234        result = stream_loop => {
235            info!("stream loop finished");
236            result
237        }
238    }
239}
240
241pub struct FileTrack {
242    track_id: TrackId,
243    play_id: Option<String>,
244    config: TrackConfig,
245    cancel_token: CancellationToken,
246    processor_chain: ProcessorChain,
247    path: Option<String>,
248    use_cache: bool,
249    ssrc: u32,
250    offset_ms: u32,
251    /// Hangup intent: hang up the call when this file finishes playing
252    /// (natural completion only, not when cancelled/interrupted).
253    hangup_reason: Option<CallRecordHangupReason>,
254    paused: Arc<AtomicBool>,
255}
256
257impl FileTrack {
258    pub fn new(id: TrackId) -> Self {
259        let config = TrackConfig::default();
260        Self {
261            track_id: id,
262            play_id: None,
263            processor_chain: ProcessorChain::new(config.samplerate),
264            config,
265            cancel_token: CancellationToken::new(),
266            path: None,
267            use_cache: true,
268            ssrc: 0,
269            offset_ms: 0,
270            hangup_reason: None,
271            paused: Arc::new(AtomicBool::new(false)),
272        }
273    }
274
275    pub fn with_play_id(mut self, play_id: Option<String>) -> Self {
276        self.play_id = play_id;
277        self
278    }
279
280    pub fn with_ssrc(mut self, ssrc: u32) -> Self {
281        self.ssrc = ssrc;
282        self
283    }
284    pub fn with_config(mut self, config: TrackConfig) -> Self {
285        self.config = config;
286        self
287    }
288
289    pub fn with_cancel_token(mut self, cancel_token: CancellationToken) -> Self {
290        self.cancel_token = cancel_token;
291        self
292    }
293
294    pub fn with_path(mut self, path: String) -> Self {
295        self.path = Some(path);
296        self
297    }
298
299    pub fn with_sample_rate(mut self, sample_rate: u32) -> Self {
300        self.config = self.config.with_sample_rate(sample_rate);
301        self
302    }
303
304    pub fn with_ptime(mut self, ptime: Duration) -> Self {
305        self.config = self.config.with_ptime(ptime);
306        self
307    }
308
309    pub fn with_cache_enabled(mut self, use_cache: bool) -> Self {
310        self.use_cache = use_cache;
311        self
312    }
313
314    pub fn with_offset_ms(mut self, offset_ms: u32) -> Self {
315        self.offset_ms = offset_ms;
316        self
317    }
318
319    /// Arm the hangup intent from the play command's `auto_hangup` flag.
320    pub fn with_auto_hangup(mut self, auto_hangup: Option<bool>) -> Self {
321        self.hangup_reason = if auto_hangup == Some(true) {
322            Some(CallRecordHangupReason::BySystem)
323        } else {
324            None
325        };
326        self
327    }
328}
329
330#[async_trait]
331impl Track for FileTrack {
332    fn ssrc(&self) -> u32 {
333        self.ssrc
334    }
335    fn id(&self) -> &TrackId {
336        &self.track_id
337    }
338    fn config(&self) -> &TrackConfig {
339        &self.config
340    }
341    fn set_paused(&self, paused: bool) -> bool {
342        self.paused.store(paused, Ordering::Relaxed);
343        true
344    }
345    fn is_paused(&self) -> bool {
346        self.paused.load(Ordering::Relaxed)
347    }
348    fn processor_chain(&mut self) -> &mut ProcessorChain {
349        &mut self.processor_chain
350    }
351
352    async fn handshake(&mut self, _offer: String, _timeout: Option<Duration>) -> Result<String> {
353        Ok("".to_string())
354    }
355    async fn update_remote_description(&mut self, _answer: &String) -> Result<()> {
356        Ok(())
357    }
358
359    async fn start(
360        &mut self,
361        event_sender: EventSender,
362        packet_sender: TrackPacketSender,
363    ) -> Result<()> {
364        if self.path.is_none() {
365            return Err(anyhow::anyhow!("filetrack: No path provided for FileTrack"));
366        }
367        let path = self.path.clone().unwrap();
368        let id = self.track_id.clone();
369        let sample_rate = self.config.samplerate;
370        let use_cache = self.use_cache;
371        let packet_duration_ms = self.config.ptime.as_millis() as u32;
372        let processor_chain = self.processor_chain.clone();
373        let token = self.cancel_token.clone();
374        let start_time = crate::media::get_timestamp();
375        let ssrc = self.ssrc;
376        let offset_ms = self.offset_ms;
377        let paused = self.paused.clone();
378        // Spawn async task to handle file streaming
379        let play_id = self.play_id.clone();
380        let hangup_reason = self.hangup_reason.clone();
381        crate::spawn(async move {
382            let res = async move {
383                // Load (and cache) the decoded PCM so we don't re-download or
384                // re-decode the original file on every play.
385                let load_result = crate::media::loader::load_audio_as_pcm_cached(
386                    &path,
387                    sample_rate,
388                    use_cache,
389                    offset_ms,
390                )
391                .await;
392                let samples = match load_result {
393                    Ok(samples) => samples,
394                    Err(e) => {
395                        warn!("filetrack: Error loading audio: {} {}", path, e);
396                        event_sender
397                            .send(SessionEvent::Error {
398                                track_id: id.clone(),
399                                timestamp: crate::media::get_timestamp(),
400                                sender: format!("filetrack: {}", path),
401                                error: e.to_string(),
402                                code: None,
403                            })
404                            .ok();
405                        event_sender
406                            .send(SessionEvent::TrackEnd {
407                                track_id: id,
408                                timestamp: crate::media::get_timestamp(),
409                                duration: crate::media::get_timestamp() - start_time,
410                                ssrc,
411                                play_id: play_id.clone(),
412                                auto_hangup: hangup_reason.clone(),
413                            })
414                            .ok();
415                        return Err(e);
416                    }
417                };
418
419                // Stream the decoded PCM (offset already applied during load)
420                let stream_result = stream_pcm_samples(
421                    processor_chain,
422                    samples,
423                    sample_rate,
424                    &id,
425                    packet_duration_ms,
426                    token.clone(),
427                    paused,
428                    packet_sender,
429                )
430                .await;
431
432                // Handle any streaming errors
433                if let Err(e) = stream_result {
434                    warn!("filetrack: Error streaming audio: {}, {}", path, e);
435                    event_sender
436                        .send(SessionEvent::Error {
437                            track_id: id.clone(),
438                            timestamp: crate::media::get_timestamp(),
439                            sender: format!("filetrack: {}", path),
440                            error: e.to_string(),
441                            code: None,
442                        })
443                        .ok();
444                }
445
446                // Send track end event
447                event_sender
448                    .send(SessionEvent::TrackEnd {
449                        track_id: id,
450                        timestamp: crate::media::get_timestamp(),
451                        duration: crate::media::get_timestamp() - start_time,
452                        ssrc,
453                        play_id,
454                        auto_hangup: if token.is_cancelled() {
455                            None
456                        } else {
457                            hangup_reason
458                        },
459                    })
460                    .ok();
461                Ok::<(), anyhow::Error>(())
462            }
463            .await;
464            if let Err(e) = res {
465                debug!("filetrack: streaming task finished with error: {:?}", e);
466            }
467        });
468        Ok(())
469    }
470
471    async fn stop(&self) -> Result<()> {
472        // Cancel the file streaming task
473        self.cancel_token.cancel();
474        Ok(())
475    }
476
477    // Do nothing as we are not sending packets
478    async fn send_packet(&mut self, _packet: &AudioFrame) -> Result<()> {
479        Ok(())
480    }
481}
482
483// Helper function to stream already-decoded PCM samples
484async fn stream_pcm_samples(
485    processor_chain: ProcessorChain,
486    samples: Vec<i16>,
487    target_sample_rate: u32,
488    track_id: &str,
489    packet_duration_ms: u32,
490    token: CancellationToken,
491    paused: Arc<AtomicBool>,
492    packet_sender: TrackPacketSender,
493) -> Result<()> {
494    let reader = DecodedAudioReader::from_samples(samples, target_sample_rate, target_sample_rate);
495    let audio_reader = Box::new(reader) as Box<dyn AudioReader>;
496    info!(
497        "filetrack: streaming {} decoded samples at {} Hz",
498        audio_reader.buffer_size(),
499        audio_reader.sample_rate(),
500    );
501    process_audio_reader(
502        processor_chain,
503        audio_reader,
504        track_id,
505        packet_duration_ms,
506        target_sample_rate,
507        token,
508        paused,
509        packet_sender,
510    )
511    .await
512}
513
514/// Read WAV file and return PCM samples and sample rate
515pub fn read_wav_file(path: &str) -> Result<(PcmBuf, u32)> {
516    let reader = BufReader::new(File::open(path)?);
517    let mut wav_reader = WavReader::new(reader)?;
518    let spec = wav_reader.spec();
519    let mut all_samples = Vec::new();
520
521    match spec.sample_format {
522        hound::SampleFormat::Int => match spec.bits_per_sample {
523            16 => {
524                for sample in wav_reader.samples::<i16>() {
525                    all_samples.push(sample.unwrap_or(0));
526                }
527            }
528            8 => {
529                for sample in wav_reader.samples::<i8>() {
530                    all_samples.push(sample.unwrap_or(0) as i16);
531                }
532            }
533            24 | 32 => {
534                for sample in wav_reader.samples::<i32>() {
535                    all_samples.push((sample.unwrap_or(0) >> 16) as i16);
536                }
537            }
538            _ => {
539                return Err(anyhow!(
540                    "Unsupported bits per sample: {}",
541                    spec.bits_per_sample
542                ));
543            }
544        },
545        hound::SampleFormat::Float => {
546            for sample in wav_reader.samples::<f32>() {
547                all_samples.push((sample.unwrap_or(0.0) * 32767.0) as i16);
548            }
549        }
550    }
551
552    // If stereo, convert to mono by averaging channels
553    if spec.channels == 2 {
554        let mono_samples = all_samples
555            .chunks(2)
556            .map(|chunk| ((chunk[0] as i32 + chunk[1] as i32) / 2) as i16)
557            .collect();
558        all_samples = mono_samples;
559    }
560    Ok((all_samples, spec.sample_rate))
561}
562
563#[cfg(test)]
564mod tests {
565    use super::*;
566    use std::io::Write;
567
568    #[tokio::test]
569    async fn test_wav_reader() -> Result<()> {
570        let file_path = "fixtures/sample.wav";
571        let file = File::open(file_path)?;
572        let mut reader = DecodedAudioReader::from_file(file, "wav", None, 16000)?;
573        let mut total_samples = 0;
574        let mut total_duration_ms = 0.0;
575        let mut chunk_count = 0;
576        while let Some((chunk, chunk_sample_rate)) = reader.read_chunk(20)? {
577            total_samples += chunk.len();
578            chunk_count += 1;
579            let chunk_duration_ms = (chunk.len() as f64 / chunk_sample_rate as f64) * 1000.0;
580            total_duration_ms += chunk_duration_ms;
581        }
582
583        let duration_seconds = total_duration_ms / 1000.0;
584        println!("Total chunks: {}", chunk_count);
585        println!("Actual samples: {}", total_samples);
586        println!("Actual duration: {:.2} seconds", duration_seconds);
587        assert_eq!(format!("{:.2}", duration_seconds), "7.51");
588        Ok(())
589    }
590    #[tokio::test]
591    async fn test_wav_file_track() -> Result<()> {
592        println!("Starting WAV file track test");
593
594        let file_path = "fixtures/sample.wav";
595        let file = File::open(file_path)?;
596
597        // First get the expected duration and samples using hound directly
598        let mut reader = hound::WavReader::new(File::open(file_path)?)?;
599        let spec = reader.spec();
600        let total_expected_samples = reader.duration() as usize;
601        let expected_duration = total_expected_samples as f64 / spec.sample_rate as f64;
602        println!("WAV file spec: {:?}", spec);
603        println!("Expected samples: {}", total_expected_samples);
604        println!("Expected duration: {:.2} seconds", expected_duration);
605
606        // Verify we can read all samples
607        let mut verify_samples = Vec::new();
608        for sample in reader.samples::<i16>() {
609            verify_samples.push(sample?);
610        }
611        println!("Verified total samples: {}", verify_samples.len());
612
613        let mut reader = DecodedAudioReader::from_file(file, "wav", None, 16000)?;
614        let mut total_samples = 0;
615        let mut total_duration_ms = 0.0;
616        let mut chunk_count = 0;
617
618        while let Some((chunk, chunk_sample_rate)) = reader.read_chunk(320)? {
619            total_samples += chunk.len();
620            chunk_count += 1;
621            // Calculate duration for this chunk
622            let chunk_duration_ms = (chunk.len() as f64 / chunk_sample_rate as f64) * 1000.0;
623            total_duration_ms += chunk_duration_ms;
624        }
625
626        let duration_seconds = total_duration_ms / 1000.0;
627        println!("Total chunks: {}", chunk_count);
628        println!("Actual samples: {}", total_samples);
629        println!("Actual duration: {:.2} seconds", duration_seconds);
630
631        // Allow for 1% tolerance in duration and sample count
632        const TOLERANCE: f64 = 0.01; // 1% tolerance
633
634        // If the file is stereo, we need to adjust the expected sample count
635        let expected_samples = if spec.channels == 2 {
636            total_expected_samples / 2 // We convert stereo to mono
637        } else {
638            total_expected_samples
639        };
640
641        assert!(
642            (duration_seconds - expected_duration).abs() < expected_duration * TOLERANCE,
643            "Duration {:.2} differs from expected {:.2} by more than {}%",
644            duration_seconds,
645            expected_duration,
646            TOLERANCE * 100.0
647        );
648
649        assert!(
650            (total_samples as f64 - expected_samples as f64).abs()
651                < expected_samples as f64 * TOLERANCE,
652            "Sample count {} differs from expected {} by more than {}%",
653            total_samples,
654            expected_samples,
655            TOLERANCE * 100.0
656        );
657
658        Ok(())
659    }
660
661    #[tokio::test]
662    async fn test_mp3_decode_sample_file() -> Result<()> {
663        let file_path = "fixtures/sample.mp3".to_string();
664        match File::open(&file_path) {
665            Ok(file) => {
666                let samples = crate::media::loader::decode_audio(file, "mp3", None, 16000)?;
667                assert!(
668                    !samples.is_empty(),
669                    "Decoded sample list should not be empty"
670                );
671            }
672            Err(_) => {
673                println!("Skipping MP3 test: sample file not found at {}", file_path);
674            }
675        }
676        Ok(())
677    }
678
679    #[tokio::test]
680    async fn test_mp3_file_track() -> Result<()> {
681        println!("Starting MP3 file track test");
682
683        // Check if the MP3 file exists
684        let file_path = "fixtures/sample.mp3".to_string();
685        let file = File::open(&file_path)?;
686        let sample_rate = 16000;
687        let mut reader = DecodedAudioReader::from_file(file, "mp3", None, sample_rate)?;
688        let mut total_samples = 0;
689        let mut total_duration_ms = 0.0;
690        while let Some((chunk, _chunk_sample_rate)) = reader.read_chunk(320)? {
691            total_samples += chunk.len();
692            // Calculate duration for this chunk
693            let chunk_duration_ms = (chunk.len() as f64 / sample_rate as f64) * 1000.0;
694            total_duration_ms += chunk_duration_ms;
695        }
696        let duration_seconds = total_duration_ms / 1000.0;
697        println!("Total samples: {}", total_samples);
698        println!("Duration: {:.2} seconds", duration_seconds);
699
700        const EXPECTED_SAMPLES: usize = 226310;
701        assert!(
702            total_samples == EXPECTED_SAMPLES,
703            "Sample count {} does not match expected {}",
704            total_samples,
705            EXPECTED_SAMPLES
706        );
707        Ok(())
708    }
709
710    #[tokio::test]
711    #[ignore = "manual debug helper: dumps raw pcm for ffplay"]
712    async fn dump_mp3_as_16k_pcm_for_ffplay() -> Result<()> {
713        let input_path = "fixtures/sample.mp3";
714        let output_path = "target/tmp/sample_16k_mono_s16le.pcm";
715
716        let file = File::open(input_path)?;
717        let mut reader = DecodedAudioReader::from_file(file, "mp3", None, 16000)?;
718
719        let mut pcm_samples = Vec::<i16>::new();
720        while let Some((chunk, _chunk_sample_rate)) = reader.read_chunk(320)? {
721            pcm_samples.extend_from_slice(&chunk);
722        }
723
724        std::fs::create_dir_all("target/tmp")?;
725        let mut out = std::fs::File::create(output_path)?;
726        for sample in pcm_samples {
727            out.write_all(&sample.to_le_bytes())?;
728        }
729        out.flush()?;
730
731        println!("PCM dumped: {}", output_path);
732        println!("ffplay -f s16le -ar 16000 -ac 1 {}", output_path);
733        Ok(())
734    }
735}