pub trait Decoder: Send + Sync {
// Required methods
fn decode_into(
&mut self,
data: &[u8],
out: &mut [Sample],
) -> Result<usize, CodecError>;
fn max_decode_samples(&self, n_bytes: usize) -> usize;
fn sample_rate(&self) -> u32;
fn channels(&self) -> u16;
// Provided methods
fn enable_concealment(&mut self) -> bool { ... }
fn conceal_into(
&mut self,
samples: usize,
out: &mut [Sample],
) -> Result<usize, CodecError> { ... }
fn recover_into(
&mut self,
next: &[u8],
samples: usize,
out: &mut [Sample],
) -> Result<usize, CodecError> { ... }
fn recover(&mut self, next: &[u8], samples: usize) -> PcmBuf { ... }
fn conceal(&mut self, samples: usize) -> PcmBuf { ... }
fn decode(&mut self, data: &[u8]) -> PcmBuf { ... }
}Expand description
Decoder trait: converts codec-specific bytes into PCM samples.
The slice-based decode_into is the primary, always-available method
(works in no_std). The convenience decode returning a Vec is only
available with the std feature and has a default implementation that
delegates to decode_into.
Required Methods§
Sourcefn decode_into(
&mut self,
data: &[u8],
out: &mut [Sample],
) -> Result<usize, CodecError>
fn decode_into( &mut self, data: &[u8], out: &mut [Sample], ) -> Result<usize, CodecError>
Decode data into out, returning the number of samples written.
Returns CodecError::BufferTooSmall if out cannot hold the
decoded samples (use max_decode_samples to size it).
Sourcefn max_decode_samples(&self, n_bytes: usize) -> usize
fn max_decode_samples(&self, n_bytes: usize) -> usize
Upper bound on the number of samples that decode_into will write
for an input of n_bytes bytes.
Sourcefn sample_rate(&self) -> u32
fn sample_rate(&self) -> u32
Get the sample rate of the decoded audio.
Provided Methods§
Sourcefn enable_concealment(&mut self) -> bool
fn enable_concealment(&mut self) -> bool
Turn on the decoder’s own packet loss concealment, returning whether
Decoder::conceal_into will conceal. Off by default where keeping
it ready costs something on every decoded packet: G.711 and G.722
keep a history of the decoded audio, and have concealment only with
the plc feature. Opus’s and G.729’s are part of the decoder and
always on. The default returns false: no concealment.
Sourcefn conceal_into(
&mut self,
samples: usize,
out: &mut [Sample],
) -> Result<usize, CodecError>
fn conceal_into( &mut self, samples: usize, out: &mut [Sample], ) -> Result<usize, CodecError>
Conceal a lost packet: write samples samples per channel of audio
to play in its place into out, returning the number written
(samples * channels). The decoder’s state carries on through the
loss as the codec allows, and the next packet decoded after it is
blended in. A long loss fades out: full level for 10 ms, silence at
60 ms (after G.711 Appendix I).
Call it once per lost packet, in order, between the decode_into
calls of the packets around the loss. Returns Ok(0), writing
nothing, when the decoder has no concealment or it isn’t enabled
(see Decoder::enable_concealment): the caller has to conceal the
gap itself.
Sourcefn recover_into(
&mut self,
next: &[u8],
samples: usize,
out: &mut [Sample],
) -> Result<usize, CodecError>
fn recover_into( &mut self, next: &[u8], samples: usize, out: &mut [Sample], ) -> Result<usize, CodecError>
Rebuild a lost packet from next, the packet that followed it, when
the codec carries redundancy for it (Opus in-band FEC), into out
like Decoder::conceal_into. next is not consumed: decode it
with decode_into afterwards.
Use it in place of conceal_into for the last packet of a loss when
the packet after it has already arrived (a jitter buffer usually has
it). Where the codec has no redundancy, or next carries none, this
conceals; the default is just conceal_into.
Sourcefn recover(&mut self, next: &[u8], samples: usize) -> PcmBuf
fn recover(&mut self, next: &[u8], samples: usize) -> PcmBuf
Convenience wrapper that allocates a Vec and calls recover_into.
Empty when the decoder can neither recover nor conceal.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".