From 749a451e826a407181e85e3e7294828517966f6a Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Sun, 18 Jun 2023 15:48:18 +0200 Subject: [PATCH] improve error reporting and handling --- videoplayer/src/main.rs | 57 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/videoplayer/src/main.rs b/videoplayer/src/main.rs index fa2c4d5..33d386f 100644 --- a/videoplayer/src/main.rs +++ b/videoplayer/src/main.rs @@ -190,14 +190,14 @@ impl<'a> DispQueue<'a> { fn new(texture_creator: &'a TextureCreator, width: usize, height: usize, len: usize) -> Self { let mut pool = Vec::with_capacity(len); for _ in 0..len + 1 { - let rgb_tex = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, width as u32, height as u32).unwrap(); - let yuv_tex = texture_creator.create_texture_streaming(PixelFormatEnum::IYUV, ((width + 1) & !1) as u32, ((height + 1) & !1) as u32).unwrap(); + let rgb_tex = texture_creator.create_texture_streaming(PixelFormatEnum::RGB24, width as u32, height as u32).expect("failed to create RGB texture"); + let yuv_tex = texture_creator.create_texture_streaming(PixelFormatEnum::IYUV, ((width + 1) & !1) as u32, ((height + 1) & !1) as u32).expect("failed to create YUV texture"); pool.push(DispFrame{ ts: 0, is_yuv: false, valid: false, rgb_tex, yuv_tex }); } pool[len].is_yuv = false; pool[len].rgb_tex.with_lock(None, |buffer: &mut [u8], _pitch: usize| { for el in buffer.iter_mut() { *el = 0; } - }).unwrap(); + }).expect("RGB texture could not be locked"); Self { pool, first_ts: 0, last_ts: 0, start: 0, end: 0, len, width, height } } @@ -217,14 +217,14 @@ impl<'a> DispQueue<'a> { if osd.is_active() { self.pool[self.len].yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| { osd.draw_yuv(buffer, pitch); - }).unwrap(); + }).expect("YUV texture locking failure"); } &self.pool[self.len].yuv_tex } else { if osd.is_active() { self.pool[self.len].rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| { osd.draw_rgb(buffer, pitch); - }).unwrap(); + }).expect("RGB texture locking failure"); } &self.pool[self.len].rgb_tex } @@ -266,19 +266,19 @@ fn try_display(disp_queue: &mut DispQueue, canvas: &mut Canvas, osd: &mu if osd.is_active() { frm.yuv_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| { osd.draw_yuv(buffer, pitch); - }).unwrap(); + }).expect("YUV texture locking failure"); } &frm.yuv_tex } else { if osd.is_active() { frm.rgb_tex.with_lock(None, |buffer: &mut [u8], pitch: usize| { osd.draw_rgb(buffer, pitch); - }).unwrap(); + }).expect("RGB texture locking failure"); } &frm.rgb_tex }; canvas.clear(); - canvas.copy(texture, None, None).unwrap(); + canvas.copy(texture, None, None).expect("canvas blit failure"); canvas.present(); disp_queue.move_start(); @@ -328,9 +328,9 @@ struct Player { impl Player { fn new() -> Self { - let sdl_context = sdl2::init().unwrap(); - let vsystem = sdl_context.video().unwrap(); - let asystem = sdl_context.audio().unwrap(); + let sdl_context = sdl2::init().expect("SDL2 init failure"); + let vsystem = sdl_context.video().expect("video subsystem init failure"); + let asystem = sdl_context.audio().expect("audio subsystem init failure"); vsystem.disable_screen_saver(); let acontrol = AudioControl::new(None, None, &asystem); let vcontrol = VideoControl::new(None, 0, 0, 0, 0); @@ -362,7 +362,7 @@ impl Player { osd: OSD::new(), #[cfg(feature="debug")] - logfile: File::create("debug.log").unwrap(), + logfile: File::create("debug.log").expect("'debug.log' should be available for writing"), } } fn seek(&mut self, off: u64, fwd: bool, dmx: &mut Demuxer, disp_queue: &mut DispQueue) -> Result<(), ()> { @@ -470,7 +470,7 @@ impl Player { } if let Event::Window {win_event: WindowEvent::Exposed, ..} = event { canvas.clear(); - canvas.copy(disp_queue.get_last_texture(&self.osd), None, None).unwrap(); + canvas.copy(disp_queue.get_last_texture(&self.osd), None, None).expect("blitting failure"); canvas.present(); } if let Event::MouseButtonDown {mouse_btn: MouseButton::Right, ..} = event { @@ -535,7 +535,12 @@ impl Player { // prepare data source let path = Path::new(name); - let mut file = File::open(path).unwrap(); + let mut file = if let Ok(handle) = File::open(path) { + handle + } else { + println!("failed to open {}", name); + return; + }; let dmx_fact; let mut fr = FileReader::new_read(&mut file); let mut br = ByteReader::new(&mut fr); @@ -563,7 +568,7 @@ impl Player { return; } dmx_fact = ret.unwrap(); - br.seek(SeekFrom::Start(0)).unwrap(); + br.seek(SeekFrom::Start(0)).expect("should be able to seek to the start"); let ret = create_demuxer(dmx_fact, &mut br); if ret.is_err() { println!("error creating demuxer"); @@ -643,7 +648,10 @@ impl Player { dsupp.pool_u8 = NAVideoBufferPool::new(reorder_depth); dsupp.pool_u16 = NAVideoBufferPool::new(reorder_depth); dsupp.pool_u32 = NAVideoBufferPool::new(reorder_depth); - dec.init(&mut dsupp, info).unwrap(); + if dec.init(&mut dsupp, info).is_err() { + println!("failed to initialise video decoder"); + return; + } video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Video(dec, reord) }); self.video_str = str_id; let (tbn, tbd) = s.get_timebase(); @@ -660,7 +668,10 @@ impl Player { let mut dec = (decfunc)(); let mut dsupp = Box::new(NADecoderSupport::new()); ainfo = info.get_properties().get_audio_info(); - dec.init(&mut dsupp, info).unwrap(); + if dec.init(&mut dsupp, info).is_err() { + println!("failed to initialise audio decoder"); + return; + } audio_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Audio(dec) }); self.audio_str = str_id; self.has_audio = true; @@ -697,22 +708,22 @@ impl Player { let fname = path.file_name(); let wname = if let Some(fname) = fname { - "NihAV player - ".to_owned() + fname.to_str().unwrap() + "NihAV player - ".to_owned() + fname.to_str().expect("should be able to set window title") } else { "NihAV player".to_owned() }; let mut builder = self.vsystem.window(&wname, width as u32, height as u32); let window = if let (Some(xpos), Some(ypos)) = (self.xpos, self.ypos) { - builder.position(xpos, ypos).build().unwrap() + builder.position(xpos, ypos).build().expect("should be able to set window position") } else { - builder.position_centered().build().unwrap() + builder.position_centered().build().expect("should be able to centre window") }; - let mut canvas = window.into_canvas().build().unwrap(); + let mut canvas = window.into_canvas().build().expect("should be able to build canvas"); let texture_creator = canvas.texture_creator(); let mut disp_q = DispQueue::new(&texture_creator, width, height, if self.has_video { FRAME_QUEUE_LEN } else { 0 }); if !self.has_video { canvas.clear(); - canvas.copy(disp_q.get_last_texture(&self.osd), None, None).unwrap(); + canvas.copy(disp_q.get_last_texture(&self.osd), None, None).expect("blit failure"); canvas.present(); } @@ -734,7 +745,7 @@ impl Player { if !self.paused { self.acontrol.resume(); } - let mut event_pump = self.sdl_context.event_pump().unwrap(); + let mut event_pump = self.sdl_context.event_pump().expect("should be able to create event pump"); let mut last_disp = Instant::now(); let mut has_data = true; 'main: loop { -- 2.39.5