atm0s_media_server_codecs/
lib.rs

1//!
2//! This module implement decode and encode logic for some codecs
3//! Currently all of codec will assume output raw audio in 48k audio
4//!
5
6#[cfg(feature = "opus")]
7pub mod opus;
8#[cfg(feature = "pcma")]
9pub mod pcma;
10#[cfg(feature = "resample")]
11pub mod resample;
12
13pub trait AudioDecoder {
14    fn decode(&mut self, in_buf: &[u8], out_buf: &mut [i16]) -> Option<usize>;
15}
16
17pub trait AudioEncodder {
18    fn encode(&mut self, in_buf: &[i16], out_buf: &mut [u8]) -> Option<usize>;
19}
20
21pub struct AudioTranscoder<Decoder, Encoder> {
22    decoder: Decoder,
23    encoder: Encoder,
24    tmp_buf: [i16; 960],
25}
26
27impl<Decoder, Encoder> AudioTranscoder<Decoder, Encoder>
28where
29    Decoder: AudioDecoder,
30    Encoder: AudioEncodder,
31{
32    pub fn new(decoder: Decoder, encoder: Encoder) -> Self {
33        Self { decoder, encoder, tmp_buf: [0; 960] }
34    }
35
36    pub fn transcode(&mut self, input: &[u8], output: &mut [u8]) -> Option<usize> {
37        let raw_samples = self.decoder.decode(input, &mut self.tmp_buf)?;
38        self.encoder.encode(&self.tmp_buf[0..raw_samples], output)
39    }
40}