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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Audio codec traits.
use crate::{AudioFrame, AudioResult, ChannelLayout};
use oximedia_core::{CodecId, SampleFormat};
/// Audio decoder trait.
pub trait AudioDecoder: Send {
/// Get codec identifier.
fn codec(&self) -> CodecId;
/// Send compressed packet.
///
/// # Errors
///
/// Returns error if packet is invalid.
fn send_packet(&mut self, data: &[u8], pts: i64) -> AudioResult<()>;
/// Receive decoded frame.
///
/// # Errors
///
/// Returns error if decoding fails.
fn receive_frame(&mut self) -> AudioResult<Option<AudioFrame>>;
/// Flush decoder.
///
/// # Errors
///
/// Returns error if flush fails.
fn flush(&mut self) -> AudioResult<()>;
/// Reset decoder state.
fn reset(&mut self);
/// Get output sample format.
fn output_format(&self) -> Option<SampleFormat>;
/// Get sample rate.
fn sample_rate(&self) -> Option<u32>;
/// Get channel layout.
fn channel_layout(&self) -> Option<ChannelLayout>;
}
/// Audio encoder trait.
pub trait AudioEncoder: Send {
/// Get codec identifier.
fn codec(&self) -> CodecId;
/// Send audio frame.
///
/// # Errors
///
/// Returns error if frame is invalid.
fn send_frame(&mut self, frame: &AudioFrame) -> AudioResult<()>;
/// Receive encoded packet.
///
/// # Errors
///
/// Returns error if encoding fails.
fn receive_packet(&mut self) -> AudioResult<Option<EncodedAudioPacket>>;
/// Flush encoder.
///
/// # Errors
///
/// Returns error if flush fails.
fn flush(&mut self) -> AudioResult<()>;
/// Get encoder configuration.
fn config(&self) -> &AudioEncoderConfig;
}
/// Encoded audio packet.
#[derive(Clone, Debug)]
pub struct EncodedAudioPacket {
/// Compressed data.
pub data: Vec<u8>,
/// Presentation timestamp.
pub pts: i64,
/// Duration in samples.
pub duration: u32,
}
/// Audio decoder configuration.
#[derive(Clone, Debug)]
pub struct AudioDecoderConfig {
/// Codec.
pub codec: CodecId,
/// Sample rate.
pub sample_rate: u32,
/// Channel count.
pub channels: u8,
/// Extra data (codec headers).
pub extradata: Option<Vec<u8>>,
}
impl Default for AudioDecoderConfig {
fn default() -> Self {
Self {
codec: CodecId::Opus,
sample_rate: 48000,
channels: 2,
extradata: None,
}
}
}
/// Audio encoder configuration.
#[derive(Clone, Debug)]
pub struct AudioEncoderConfig {
/// Codec.
pub codec: CodecId,
/// Sample rate.
pub sample_rate: u32,
/// Channel count.
pub channels: u8,
/// Bitrate (bits/sec).
pub bitrate: u32,
/// Frame size in samples.
pub frame_size: u32,
}
impl Default for AudioEncoderConfig {
fn default() -> Self {
Self {
codec: CodecId::Opus,
sample_rate: 48000,
channels: 2,
bitrate: 128_000,
frame_size: 960, // 20ms at 48kHz
}
}
}