use crate::audio::AudioSpec;
use crate::decoder::Decoder;
use crate::error::DecodeError;
use crate::g711::{G711Decoder, G711Law};
use crate::pcm::PcmDecoder;
use crate::sample::SampleFormat;
#[derive(Debug)]
pub(crate) enum Payload {
Pcm(PcmDecoder),
G711(G711Decoder),
}
impl Payload {
pub(crate) fn from_parts(
format: Option<SampleFormat>,
law: Option<G711Law>,
spec: AudioSpec,
) -> Self {
match (format, law) {
(Some(format), _) => Self::Pcm(PcmDecoder::new(format, spec)),
(_, Some(law)) => Self::G711(G711Decoder::new(law, spec)),
_ => Self::Pcm(PcmDecoder::new(SampleFormat::U8, spec)),
}
}
pub(crate) fn feed(&mut self, input: &[u8]) -> Result<usize, DecodeError> {
match self {
Self::Pcm(decoder) => decoder.feed(input),
Self::G711(decoder) => decoder.feed(input),
}
}
pub(crate) fn decode(&mut self, output: &mut Vec<f32>) -> Result<usize, DecodeError> {
match self {
Self::Pcm(decoder) => decoder.decode(output),
Self::G711(decoder) => decoder.decode(output),
}
}
pub(crate) fn flush(&mut self, output: &mut Vec<f32>) -> Result<usize, DecodeError> {
match self {
Self::Pcm(decoder) => decoder.flush(output),
Self::G711(decoder) => decoder.flush(output),
}
}
pub(crate) fn buffered_bytes(&self) -> usize {
match self {
Self::Pcm(decoder) => decoder.buffered_bytes(),
Self::G711(decoder) => decoder.buffered_bytes(),
}
}
}