#![forbid(unsafe_code)]
pub mod sw_opus;
#[cfg(test)]
mod sw_opus_tests;
use crate::error::EncodeError;
use mediaway_common::{AudioFrame, CodecKind, Packet, Rational, SampleFormat, StreamInfo};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioEncoderConfig {
pub codec: CodecKind,
pub sample_rate: u32,
pub channels: u16,
pub sample_format: SampleFormat,
pub time_base: Rational,
pub bitrate_bps: u32,
}
impl AudioEncoderConfig {
#[must_use]
pub const fn aac_stereo(sample_rate: u32, time_base: Rational) -> Self {
Self {
codec: CodecKind::Aac,
sample_rate,
channels: 2,
sample_format: SampleFormat::F32,
time_base,
bitrate_bps: 0,
}
}
}
pub trait AudioEncoder {
fn stream_info(&self) -> &StreamInfo;
fn push_frame(&mut self, frame: &AudioFrame) -> Result<(), EncodeError>;
fn poll_packet(&mut self) -> Result<Option<Packet>, EncodeError>;
fn flush(&mut self) -> Result<(), EncodeError>;
fn finish(mut self) -> Result<Vec<Packet>, EncodeError>
where
Self: Sized,
{
self.flush()?;
let mut packets = Vec::new();
while let Some(packet) = self.poll_packet()? {
packets.push(packet);
}
Ok(packets)
}
}
#[cfg(test)]
#[path = "audio_tests.rs"]
mod tests;