From 05bad6e9e034b08366d9e0755583215d0d86d5ce Mon Sep 17 00:00:00 2001 From: Nebojsa Sabovic Date: Sat, 15 Apr 2023 22:44:12 +0200 Subject: [PATCH] Propagate errors in Input::packets(). --- examples/dump-frames.rs | 2 +- examples/remux.rs | 2 +- examples/transcode-audio.rs | 2 +- examples/transcode-x264.rs | 2 +- src/format/context/input.rs | 22 ++++++++++------------ 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/examples/dump-frames.rs b/examples/dump-frames.rs index 4d3923c..6a2d5ca 100644 --- a/examples/dump-frames.rs +++ b/examples/dump-frames.rs @@ -45,7 +45,7 @@ fn main() -> Result<(), ffmpeg::Error> { Ok(()) }; - for (stream, packet) in ictx.packets() { + for (stream, packet) in ictx.packets().filter_map(Result::ok) { if stream.index() == video_stream_index { decoder.send_packet(&packet)?; receive_and_process_decoded_frames(&mut decoder)?; diff --git a/examples/remux.rs b/examples/remux.rs index 3effa32..87f89eb 100644 --- a/examples/remux.rs +++ b/examples/remux.rs @@ -42,7 +42,7 @@ fn main() { octx.set_metadata(ictx.metadata().to_owned()); octx.write_header().unwrap(); - for (stream, mut packet) in ictx.packets() { + for (stream, mut packet) in ictx.packets().filter_map(Result::ok) { let ist_index = stream.index(); let ost_index = stream_mapping[ist_index]; if ost_index < 0 { diff --git a/examples/transcode-audio.rs b/examples/transcode-audio.rs index f6ed634..184fc49 100644 --- a/examples/transcode-audio.rs +++ b/examples/transcode-audio.rs @@ -225,7 +225,7 @@ fn main() { octx.set_metadata(ictx.metadata().to_owned()); octx.write_header().unwrap(); - for (stream, mut packet) in ictx.packets() { + for (stream, mut packet) in ictx.packets().filter_map(Result::ok) { if stream.index() == transcoder.stream { packet.rescale_ts(stream.time_base(), transcoder.in_time_base); transcoder.send_packet_to_decoder(&packet); diff --git a/examples/transcode-x264.rs b/examples/transcode-x264.rs index 21f0019..e1d226e 100644 --- a/examples/transcode-x264.rs +++ b/examples/transcode-x264.rs @@ -238,7 +238,7 @@ fn main() { ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base(); } - for (stream, mut packet) in ictx.packets() { + for (stream, mut packet) in ictx.packets().filter_map(Result::ok) { let ist_index = stream.index(); let ost_index = stream_mapping[ist_index]; if ost_index < 0 { diff --git a/src/format/context/input.rs b/src/format/context/input.rs index e043dd2..14e2ffb 100644 --- a/src/format/context/input.rs +++ b/src/format/context/input.rs @@ -159,24 +159,22 @@ impl<'a> PacketIter<'a> { } impl<'a> Iterator for PacketIter<'a> { - type Item = (Stream<'a>, Packet); + type Item = Result<(Stream<'a>, Packet), Error>; fn next(&mut self) -> Option<::Item> { let mut packet = Packet::empty(); - loop { - match packet.read(self.context) { - Ok(..) => unsafe { - return Some(( - Stream::wrap(mem::transmute_copy(&self.context), packet.stream()), - packet, - )); - }, + match packet.read(self.context) { + Ok(..) => unsafe { + Some(Ok(( + Stream::wrap(mem::transmute_copy(&self.context), packet.stream()), + packet, + ))) + }, - Err(Error::Eof) => return None, + Err(Error::Eof) => None, - Err(..) => (), - } + Err(e) => Some(Err(e)), } } }