1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Audio encode config and [`AudioEncoder`] trait.
#![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};
/// Parameters for opening an audio encoder session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AudioEncoderConfig {
/// Output codec (Stage 1 Windows: [`CodecKind::Aac`]).
pub codec: CodecKind,
/// Sample rate (Hz).
pub sample_rate: u32,
/// Channel count.
pub channels: u16,
/// Input PCM format.
pub sample_format: SampleFormat,
/// Timestamp timebase.
pub time_base: Rational,
/// Target bitrate in bits per second (`0` = backend default).
pub bitrate_bps: u32,
}
impl AudioEncoderConfig {
/// AAC stereo defaults; bitrate left to the backend.
#[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,
}
}
}
/// Streaming hardware (or backend) audio encoder.
pub trait AudioEncoder {
/// Stream metadata (updated when extradata becomes available).
fn stream_info(&self) -> &StreamInfo;
/// Submit one audio buffer.
///
/// # Errors
///
/// Returns [`EncodeError`] when the buffer is rejected or the session failed.
fn push_frame(&mut self, frame: &AudioFrame) -> Result<(), EncodeError>;
/// Pull the next compressed packet, if any.
///
/// # Errors
///
/// Returns [`EncodeError`] on backend failure.
fn poll_packet(&mut self) -> Result<Option<Packet>, EncodeError>;
/// Signal end-of-input; drain with [`poll_packet`](Self::poll_packet).
///
/// # Errors
///
/// Returns [`EncodeError`] on backend failure.
fn flush(&mut self) -> Result<(), EncodeError>;
}