mirror of
https://github.com/RustAudio/rodio
synced 2024-11-10 06:04:16 +00:00
Try next packet after a decode error (Symphonia) (#403)
* Skip no more than 3 consecutive decode errors * missing delimiter * Corrections after build * use const * reorganize second loop Co-authored-by: Rrogntudju <rrogntudju@example.com>
This commit is contained in:
parent
68b0333642
commit
2c171d6683
1 changed files with 44 additions and 11 deletions
|
@ -17,6 +17,11 @@ use crate::Source;
|
|||
|
||||
use super::DecoderError;
|
||||
|
||||
// Decoder errors are not considered fatal.
|
||||
// The correct action is to just get a new packet and try again.
|
||||
// But a decode error in more than 3 consecutive packets is fatal.
|
||||
const MAX_DECODE_ERRORS: usize = 3;
|
||||
|
||||
pub struct SymphoniaDecoder {
|
||||
decoder: Box<dyn Decoder>,
|
||||
current_frame_offset: usize,
|
||||
|
@ -72,8 +77,24 @@ impl SymphoniaDecoder {
|
|||
},
|
||||
)?;
|
||||
|
||||
let current_frame = probed.format.next_packet()?;
|
||||
let decoded = decoder.decode(¤t_frame)?;
|
||||
let mut decode_errors: usize = 0;
|
||||
let decoded = loop {
|
||||
let current_frame = probed.format.next_packet()?;
|
||||
match decoder.decode(¤t_frame) {
|
||||
Ok(decoded) => break decoded,
|
||||
Err(e) => match e {
|
||||
Error::DecodeError(_) => {
|
||||
decode_errors += 1;
|
||||
if decode_errors > MAX_DECODE_ERRORS {
|
||||
return Err(e);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => return Err(e),
|
||||
},
|
||||
}
|
||||
};
|
||||
let spec = decoded.spec().to_owned();
|
||||
let buffer = SymphoniaDecoder::get_buffer(decoded, &spec);
|
||||
|
||||
|
@ -123,16 +144,28 @@ impl Iterator for SymphoniaDecoder {
|
|||
#[inline]
|
||||
fn next(&mut self) -> Option<i16> {
|
||||
if self.current_frame_offset == self.buffer.len() {
|
||||
match self.format.next_packet() {
|
||||
Ok(packet) => match self.decoder.decode(&packet) {
|
||||
Ok(decoded) => {
|
||||
self.spec = decoded.spec().to_owned();
|
||||
self.buffer = SymphoniaDecoder::get_buffer(decoded, &self.spec);
|
||||
}
|
||||
let mut decode_errors: usize = 0;
|
||||
let decoded = loop {
|
||||
match self.format.next_packet() {
|
||||
Ok(packet) => match self.decoder.decode(&packet) {
|
||||
Ok(decoded) => break decoded,
|
||||
Err(e) => match e {
|
||||
Error::DecodeError(_) => {
|
||||
decode_errors += 1;
|
||||
if decode_errors > MAX_DECODE_ERRORS {
|
||||
return None;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
_ => return None,
|
||||
},
|
||||
},
|
||||
Err(_) => return None,
|
||||
},
|
||||
Err(_) => return None,
|
||||
}
|
||||
}
|
||||
};
|
||||
self.spec = decoded.spec().to_owned();
|
||||
self.buffer = SymphoniaDecoder::get_buffer(decoded, &self.spec);
|
||||
self.current_frame_offset = 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue