diff --git a/src/capture.rs b/src/capture.rs index 9574175..378aaa5 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -23,7 +23,7 @@ impl Capture { let x = Device::open ("/dev/video0")?; dbg! (x.formats (linuxvideo::BufType::VIDEO_CAPTURE).collect::> ()); - let x = x.video_capture (PixFormat::new (u32::MAX, u32::MAX, PixelFormat::MJPG))?; + let x = x.video_capture (PixFormat::new (u32::MAX, u32::MAX, PixelFormat::YUYV))?; dbg! (x.format ()); let size_image = usize::try_from (x.format ().size_image ()).unwrap (); let stream = x.into_stream ()?; @@ -57,6 +57,11 @@ impl Capture Ok (input.len ()) })?) } + + pub fn will_block (&self) -> std::io::Result + { + self.stream.will_block () + } } #[derive (Debug, thiserror::Error)] diff --git a/src/main.rs b/src/main.rs index a0a09dd..080a179 100644 --- a/src/main.rs +++ b/src/main.rs @@ -127,31 +127,60 @@ fn main() -> Result <(), Error> Ok (()) } -#[derive(Default)] -struct MyEguiApp {} +struct MyEguiApp { + img: egui::ColorImage, + texture: Option , + capture: capture::Capture, +} impl MyEguiApp { - fn new(cc: &eframe::CreationContext<'_>) -> Self { + fn new(_cc: &eframe::CreationContext<'_>) -> Self { // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_visuals. // Restore app state using cc.storage (requires the "persistence" feature). // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use // for e.g. egui::PaintCallback. - Self::default() + + let img = egui::ColorImage::new ([1920,1080], egui::Color32::TEMPORARY_COLOR); + + Self + { + img, + texture: None, + capture: capture::Capture::new ().unwrap (), + } } } impl eframe::App for MyEguiApp { - fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { - egui::CentralPanel::default().show(ctx, |ui| { - ui.heading("Hello World!"); - }); - } + fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + egui::CentralPanel::default().show(ctx, |ui| { + let texture = self.texture.get_or_insert_with(|| + { + ui.ctx ().load_texture ("my_image", self.img.clone (), Default::default()) + }); + + match self.capture.will_block () + { + Ok (false) => + { + self.capture.wait_for_frame(self.img.as_raw_mut()).unwrap (); + texture.set (self.img.clone (), Default::default ()); + }, + _ => (), + } + + let texture: &egui::TextureHandle = texture; + + ui.heading("Hello World!"); + ui.image (texture, texture.size_vec2()); + }); + } } fn main_egui () { let native_options = eframe::NativeOptions::default(); - eframe::run_native("My egui App", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc)))); + eframe::run_native("My egui App", native_options, Box::new(|cc| Box::new(MyEguiApp::new(cc)))).unwrap (); } struct ThreadSystem <'a>