improved error handling

This commit is contained in:
apolunar 2024-06-05 09:37:28 -05:00
parent 3bc964737c
commit 5f1b057b14

View file

@ -2,7 +2,7 @@ use std::time::Duration;
use symphonia::{ use symphonia::{
core::{ core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec}, audio::{AudioBufferRef, SampleBuffer, SignalSpec},
codecs::{Decoder, CODEC_TYPE_NULL}, codecs::{Decoder, DecoderOptions, CODEC_TYPE_NULL},
errors::Error, errors::Error,
formats::{FormatOptions, FormatReader, SeekedTo}, formats::{FormatOptions, FormatReader, SeekedTo},
io::MediaSourceStream, io::MediaSourceStream,
@ -77,13 +77,19 @@ impl SymphoniaDecoder {
}; };
// Select the first supported track // Select the first supported track
let track_id = probed let track_id = match probed
.format .format
.tracks() .tracks()
.iter() .iter()
.find(|t| t.codec_params.codec != CODEC_TYPE_NULL) .find(|t| t.codec_params.codec != CODEC_TYPE_NULL)
.unwrap() {
.id; Some(track) => track.id,
None => {
return Err(symphonia::core::errors::Error::Unsupported(
"No track with supported codec",
))
}
};
let track = probed let track = probed
.format .format
@ -92,8 +98,8 @@ impl SymphoniaDecoder {
.find(|track| track.id == track_id) .find(|track| track.id == track_id)
.unwrap(); .unwrap();
let mut decoder = let mut decoder = symphonia::default::get_codecs()
symphonia::default::get_codecs().make(&track.codec_params, &Default::default())?; .make(&track.codec_params, &DecoderOptions::default())?;
let total_duration = stream let total_duration = stream
.codec_params .codec_params
.time_base .time_base
@ -104,10 +110,8 @@ impl SymphoniaDecoder {
let decoded = loop { let decoded = loop {
let current_frame = match probed.format.next_packet() { let current_frame = match probed.format.next_packet() {
Ok(packet) => packet, Ok(packet) => packet,
Err(e) => match e { Err(Error::IoError(_)) => break decoder.last_decoded(),
Error::IoError(_) => break decoder.last_decoded(), Err(e) => return Err(e),
_ => return Err(e),
},
}; };
// If the packet does not belong to the selected track, skip over it // If the packet does not belong to the selected track, skip over it