]> git.nihav.org Git - nihav-player.git/commitdiff
videoplayer: use single global window instead of creating a new one for each file
authorKostya Shishkov <kostya.shishkov@gmail.com>
Wed, 2 Aug 2023 16:03:51 +0000 (18:03 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Thu, 3 Aug 2023 13:21:48 +0000 (15:21 +0200)
videoplayer/src/main.rs

index 89deb7c910e748d6542ed7470fd3374e9ed5e4c3..d3aef1babb219e53b4240452c1b009e62b7f6e4a 100644 (file)
@@ -293,8 +293,6 @@ struct Player {
     sdl_context:    sdl2::Sdl,
     vsystem:        sdl2::VideoSubsystem,
     asystem:        sdl2::AudioSubsystem,
-    xpos:           Option<i32>,
-    ypos:           Option<i32>,
 
     acontrol:       AudioControl,
     vcontrol:       VideoControl,
@@ -333,8 +331,6 @@ impl Player {
         let vcontrol = VideoControl::new(None, 0, 0, 0, 0);
         Self {
             sdl_context, asystem, vsystem,
-            xpos:           None,
-            ypos:           None,
 
             acontrol, vcontrol,
 
@@ -539,7 +535,7 @@ impl Player {
         }
         Ok(false)
     }
-    fn play(&mut self, name: &str, start_time: NATimePoint) {
+    fn play(&mut self, mut window: Window, name: &str, start_time: NATimePoint) -> Window {
         debug_log!(self; {format!("Playing {}", name)});
 
         // prepare data source
@@ -548,14 +544,14 @@ impl Player {
                 handle
             } else {
                 println!("failed to open {}", name);
-                return;
+                return window;
             };
         let mut fr = FileReader::new_read(&mut file);
         let mut br = ByteReader::new(&mut fr);
         let res = detect::detect_format(name, &mut br);
         if res.is_none() {
             println!("cannot detect format for {}", name);
-            return;
+            return window;
         }
         let (dmx_name, _score) = res.unwrap();
         debug_log!(self; {format!(" found demuxer {} with score {:?}", dmx_name, _score)});
@@ -573,14 +569,14 @@ impl Player {
         let ret = dmx_reg.find_demuxer(dmx_name);
         if ret.is_none() {
             println!("error finding {} demuxer", dmx_name);
-            return;
+            return window;
         }
         let dmx_fact = ret.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");
-            return;
+            return window;
         }
         let mut dmx = ret.unwrap();
         if start_time != NATimePoint::None {
@@ -659,7 +655,7 @@ impl Player {
                         dsupp.pool_u32 = NAVideoBufferPool::new(reorder_depth);
                         if dec.init(&mut dsupp, info).is_err() {
                             println!("failed to initialise video decoder");
-                            return;
+                            return window;
                         }
                         video_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Video(dec, reord) });
                         self.video_str = str_id;
@@ -684,7 +680,7 @@ impl Player {
                         }
                         if dec.init(&mut dsupp, info).is_err() {
                             println!("failed to initialise audio decoder");
-                            return;
+                            return window;
                         }
                         audio_dec = Some(DecoderStuff{ dsupp, dec: DecoderType::Audio(dec) });
                         self.audio_str = str_id;
@@ -699,7 +695,7 @@ impl Player {
         }
         if !self.has_video && !self.has_audio {
             println!("No playable streams found.");
-            return;
+            return window;
         }
 
         while (width <= 384) && (height <= 288) {
@@ -729,12 +725,9 @@ impl Player {
             } 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().expect("should be able to set window position")
-            } else {
-                builder.position_centered().build().expect("should be able to centre window")
-            };
+        window.set_title(&wname).expect("set window title");
+        window.set_size(width as u32, height as u32).expect("resize window");
+        window.show();
         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 });
@@ -747,7 +740,7 @@ impl Player {
         self.has_audio = self.acontrol.has_audio();
         if !self.has_video && !self.has_audio {
             println!("No playable streams.");
-            return;
+            return canvas.into_window();
         }
 
         // play
@@ -756,7 +749,7 @@ impl Player {
             new_vcontrol.finish();
             std::mem::swap(&mut self.acontrol, &mut new_acontrol);
             new_acontrol.finish();
-            return;
+            return canvas.into_window();
         }
         self.tkeep.reset_all(if !disp_q.is_empty() { disp_q.first_ts } else { 0 });
         if !self.paused {
@@ -854,14 +847,12 @@ impl Player {
                 thread::sleep(Duration::from_millis(20));
             }
         }
-        let (xpos, ypos) = canvas.into_window().position();
-        self.xpos = Some(xpos);
-        self.ypos = Some(ypos);
         println!();
         std::mem::swap(&mut self.vcontrol, &mut new_vcontrol);
         new_vcontrol.finish();
         std::mem::swap(&mut self.acontrol, &mut new_acontrol);
         new_acontrol.finish();
+        canvas.into_window()
     }
 }
 
@@ -874,6 +865,8 @@ fn main() {
     }
 
     let mut player = Player::new();
+    let mut builder = player.vsystem.window("NihAV Player", 640, 480);
+    let mut window = builder.position_centered().hidden().build().expect("should be able to centre window");
 
     let mut aiter = args.iter().skip(1);
     let mut seek_time = NATimePoint::None;
@@ -924,7 +917,7 @@ fn main() {
                 }
             },
             _ => {
-                player.play(arg, seek_time);
+                window = player.play(window, arg, seek_time);
                 if player.end { break; }
                 seek_time = NATimePoint::None;
             },