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:
Mario 2021-11-29 17:18:21 -05:00 committed by GitHub
parent 68b0333642
commit 2c171d6683
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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(&current_frame)?;
let mut decode_errors: usize = 0;
let decoded = loop {
let current_frame = probed.format.next_packet()?;
match decoder.decode(&current_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;
}