sipio 0.4.1

SIP client with rtp input / output handling
Documentation
use anyhow::{Context, Result};
use audio_codec_algorithms::{encode_alaw, encode_ulaw};
use rsipstack::transport::SipAddr;
use rsipstack::transport::udp::UdpConnection;
use rtp_rs::RtpPacketBuilder;
use std::path::Path;
use std::time::Duration;
use symphonia::core::codecs::audio::AudioDecoderOptions;
use symphonia::core::errors::Error as SymphoniaError;
use symphonia::core::formats::probe::Hint;
use symphonia::core::formats::{FormatOptions, TrackType};
use symphonia::core::io::{MediaSource, MediaSourceStream, ReadOnlySource};
use symphonia::core::meta::MetadataOptions;
use tokio::select;
use tokio::sync::mpsc;
use tokio_stream::StreamExt;
use tokio_util::sync::CancellationToken;

pub const RTP_CLOCK_RATE: u32 = 8000;
const FRAME_SAMPLES: usize = 160; // 20ms at 8kHz

pub struct EncodedSound {
    source: Box<dyn MediaSource>,
    hint: Hint,
}

impl EncodedSound {
    pub fn file(path: &Path) -> std::io::Result<Self> {
        let mut hint = Hint::new();
        if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
            hint.with_extension(ext);
        }

        let file = std::fs::File::open(path)?;

        Ok(Self {
            source: Box::new(file),
            hint,
        })
    }

    pub fn read<R: std::io::Read + Send + 'static>(read: R) -> Self
    where
        ReadOnlySource<R>: MediaSource,
    {
        Self {
            source: Box::new(ReadOnlySource::new(read)),
            hint: Hint::new(),
        }
    }

    pub fn hint_mut(&mut self) -> &mut Hint {
        &mut self.hint
    }
}

pub struct RawSound {
    stream: Box<dyn tokio_stream::Stream<Item = Vec<f32>> + Send + Unpin>,
    rate: u32,
}

pub enum Sound {
    Encoded(EncodedSound),
    Raw(RawSound),
}

impl Sound {
    pub fn file(file: &Path) -> Result<Self> {
        Ok(Self::Encoded(EncodedSound::file(file)?))
    }

    pub fn read_encoded<R: std::io::Read + Send + 'static>(read: R) -> Self
    where
        ReadOnlySource<R>: MediaSource,
    {
        Self::Encoded(EncodedSound::read(read))
    }

    pub fn stream_raw<S: tokio_stream::Stream<Item = Vec<f32>> + Send + Unpin + 'static>(
        stream: S,
        rate: u32,
    ) -> Self {
        Self::Raw(RawSound {
            stream: Box::new(stream),
            rate,
        })
    }
}

pub struct SoundStream {
    rx: mpsc::Receiver<Vec<u8>>,
}

impl SoundStream {
    pub async fn next_frame(&mut self) -> Option<Vec<u8>> {
        self.rx.recv().await
    }
}

pub fn stream_sound(sound: Sound, payload_type: u8) -> SoundStream {
    let (tx, rx) = mpsc::channel::<Vec<u8>>(50);

    match sound {
        Sound::Encoded(sound) => {
            tokio::task::spawn_blocking(move || {
                if let Err(e) = decode_streaming(sound, payload_type, &tx) {
                    log::error!("sound decode pipeline failed: {e}");
                }
            });
        }
        Sound::Raw(stream) => {
            tokio::task::spawn(async move {
                if let Err(e) = encode_raw(stream, payload_type, &tx).await {
                    log::error!("sound encode pipeline fialed: {e}");
                }
            });
        }
    }

    SoundStream { rx }
}

async fn encode_raw(
    mut sound: RawSound,
    payload_type: u8,
    tx: &mpsc::Sender<Vec<u8>>,
) -> Result<()> {
    let mut resampler = LinearResampler::new(sound.rate, RTP_CLOCK_RATE);
    let mut pcm_carry: Vec<i16> = Vec::new();

    while let Some(chunk) = sound.stream.next().await {
        let resampled = resampler.process(&chunk);
        pcm_carry.extend(f32_to_pcm16(&resampled));

        while pcm_carry.len() >= FRAME_SAMPLES {
            let frame_pcm: Vec<i16> = pcm_carry.drain(0..FRAME_SAMPLES).collect();
            let frame = pcm16_to_g711(&frame_pcm, payload_type)?;
            if tx.send(frame).await.is_err() {
                return Ok(());
            }
        }
    }

    if !pcm_carry.is_empty() {
        let frame = pcm16_to_g711(&pcm_carry, payload_type)?;
        let _ = tx.send(frame).await;
    }

    Ok(())
}

fn decode_streaming(
    sound: EncodedSound,
    payload_type: u8,
    tx: &mpsc::Sender<Vec<u8>>,
) -> Result<()> {
    let mss = MediaSourceStream::new(sound.source, Default::default());

    let mut format = symphonia::default::get_probe()
        .probe(
            &sound.hint,
            mss,
            FormatOptions::default(),
            MetadataOptions::default(),
        )
        .context("probing audio format")?;

    let track = format
        .default_track(TrackType::Audio)
        .context("no default audio track")?;
    let track_id = track.id;
    let audio_params = track
        .codec_params
        .as_ref()
        .and_then(|p| p.audio())
        .context("not an audio track")?;

    let mut decoder = symphonia::default::get_codecs()
        .make_audio_decoder(audio_params, &AudioDecoderOptions::default())
        .context("unsupported codec")?;

    let mut interleaved: Vec<f32> = Vec::new();
    let mut resampler: Option<LinearResampler> = None;
    let mut pcm_carry: Vec<i16> = Vec::new();

    while let Some(packet) = format.next_packet().context("reading next packet")? {
        if packet.track_id != track_id {
            continue;
        }

        let audio_buf = match decoder.decode(&packet) {
            Ok(buf) => buf,
            Err(SymphoniaError::DecodeError(e)) => {
                log::warn!("skipping bad packet: {e}");
                continue;
            }
            Err(e) => return Err(e).context("decoding packet"),
        };

        let rate = audio_buf.spec().rate();
        let channels = audio_buf.spec().channels().count();

        let resampler = resampler.get_or_insert_with(|| LinearResampler::new(rate, RTP_CLOCK_RATE));

        interleaved.resize(audio_buf.samples_interleaved(), 0.0);
        audio_buf.copy_to_slice_interleaved(&mut interleaved);

        let mono = downmix_to_mono(&interleaved, channels);
        let resampled = resampler.process(&mono);
        pcm_carry.extend(f32_to_pcm16(&resampled));

        while pcm_carry.len() >= FRAME_SAMPLES {
            let frame_pcm: Vec<i16> = pcm_carry.drain(0..FRAME_SAMPLES).collect();
            let frame = pcm16_to_g711(&frame_pcm, payload_type)?;
            if tx.blocking_send(frame).is_err() {
                return Ok(());
            }
        }
    }

    if !pcm_carry.is_empty() {
        let frame = pcm16_to_g711(&pcm_carry, payload_type)?;
        let _ = tx.blocking_send(frame);
    }

    Ok(())
}

fn downmix_to_mono(samples: &[f32], channels: usize) -> Vec<f32> {
    if channels <= 1 {
        return samples.to_vec();
    }
    samples
        .chunks_exact(channels)
        .map(|frame| frame.iter().sum::<f32>() / channels as f32)
        .collect()
}

fn f32_to_pcm16(samples: &[f32]) -> Vec<i16> {
    samples
        .iter()
        .map(|&s| (s.clamp(-1.0, 1.0) * i16::MAX as f32) as i16)
        .collect()
}

fn pcm16_to_g711(pcm: &[i16], payload_type: u8) -> Result<Vec<u8>> {
    match payload_type {
        0 => Ok(pcm.iter().map(|&s| encode_ulaw(s)).collect()),
        8 => Ok(pcm.iter().map(|&s| encode_alaw(s)).collect()),
        other => anyhow::bail!("unsupported payload type: {other}"),
    }
}

#[expect(clippy::too_many_arguments)]
pub async fn play_sound(
    sound: Sound,
    conn: UdpConnection,
    token: CancellationToken,
    ssrc: u32,
    mut ts: u32,
    mut seq: u16,
    peer_addr: SipAddr,
    payload_type: u8,
) -> (u32, u16) {
    select! {
        _ = token.cancelled() => {
            log::debug!("RTP session cancelled");
        }
        _ = async {
            log::debug!("Playing sound for {peer_addr}");
            let mut stream = stream_sound(sound, payload_type);
            let mut ticker = tokio::time::interval(Duration::from_millis(20));

            while let Some(chunk) = stream.next_frame().await {
                let result = match RtpPacketBuilder::new()
                    .payload_type(payload_type)
                    .ssrc(ssrc)
                    .sequence(seq.into())
                    .timestamp(ts)
                    .payload(&chunk)
                    .build()
                {
                    Ok(r) => r,
                    Err(e) => {
                        log::error!("Failed to build RTP packet: {e:?}");
                        break;
                    }
                };
                ts += chunk.len() as u32;
                seq += 1;
                if let Err(e) = conn.send_raw(&result, &peer_addr).await {
                    log::error!("Failed to send RTP: {e}");
                    break;
                }
                ticker.tick().await;
            }
        } => {
            log::debug!("playback finished");
        }
    };

    (ts, seq)
}

// TODO: Try non-linear resampler.
pub struct LinearResampler {
    from_rate: u32,
    to_rate: u32,
    carry: Vec<f32>,
    pos: f64,
}

impl LinearResampler {
    pub fn new(from_rate: u32, to_rate: u32) -> Self {
        Self {
            from_rate,
            to_rate,
            carry: Vec::new(),
            pos: 0.0,
        }
    }

    pub fn process(&mut self, input: &[f32]) -> Vec<f32> {
        if self.from_rate == self.to_rate {
            return input.to_vec();
        }
        self.carry.extend_from_slice(input);
        let ratio = self.from_rate as f64 / self.to_rate as f64;

        let mut out = Vec::new();
        loop {
            let idx = self.pos.floor() as usize;
            if idx + 1 >= self.carry.len() {
                break; // not enough input yet — wait for the next chunk
            }
            let frac = (self.pos - idx as f64) as f32;
            out.push(self.carry[idx] + (self.carry[idx + 1] - self.carry[idx]) * frac);
            self.pos += ratio;
        }

        let consumed = self.pos.floor() as usize;
        if consumed > 0 {
            self.carry.drain(0..consumed.min(self.carry.len()));
            self.pos -= consumed as f64;
        }
        out
    }
}