From d6297dcbd37a579eacd777bc0b899897df80ce5b Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 12 Sep 2023 22:54:53 -0500 Subject: [PATCH] sketching out network sending --- src/controller.rs | 57 ++++++++++++++++++++++++++++++++++++++--------- src/driver.rs | 25 ++++++++++++++++----- 2 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index ee964e0..8326f8f 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -6,8 +6,9 @@ use std:: pub enum MsgFromController { RepaintGui (RgbaFrame), - StartCapture, StartJpegDecoder (JpegFrame), + StartNetworkSend (JpegFrame), + StartCapture, } #[derive (Clone)] @@ -29,12 +30,16 @@ pub struct Controller gui_has_new_frame: bool, gui_needs_frame: bool, - capture_is_running: bool, - jpeg_decoder_is_running: bool, jpeg_decoder_has_new_frame: bool, jpeg_decoder_needs_frame: bool, + network_send_is_running: bool, + network_send_has_new_frame: bool, + network_send_needs_frame: bool, + + capture_is_running: bool, + rgba_frame: RgbaFrame, jpeg_frame: JpegFrame, } @@ -48,11 +53,16 @@ impl Controller gui_has_new_frame: false, gui_needs_frame: false, - capture_is_running: false, jpeg_decoder_has_new_frame: false, jpeg_decoder_is_running: false, jpeg_decoder_needs_frame: false, + network_send_has_new_frame: false, + network_send_is_running: false, + network_send_needs_frame: true, + + capture_is_running: false, + rgba_frame: RgbaFrame { data: Default::default (), @@ -66,6 +76,25 @@ impl Controller } } + pub fn handle_gui_needs_frame (&mut self) + { + // println! ("handle_gui_needs_frame"); + self.gui_needs_frame = true; + } + + pub fn handle_network_write_needs_frame (&mut self) + { + println! ("handle_network_write_needs_frame"); + self.network_send_needs_frame = true; + } + + pub fn handle_network_write_finished (&mut self) + { + println! ("handle_network_write_finished"); + self.network_send_has_new_frame = false; + self.network_send_is_running = false; + } + pub fn handle_rgba_frame (&mut self, frame: RgbaFrame) { // println! ("handle_rgba_frame"); @@ -79,18 +108,18 @@ impl Controller { // println! ("handle_capture"); self.capture_is_running = false; + self.jpeg_frame = jpeg; + self.jpeg_decoder_has_new_frame = true; - } - - pub fn handle_gui_needs_frame (&mut self) - { - // println! ("handle_gui_needs_frame"); - self.gui_needs_frame = true; + self.network_send_has_new_frame = true; } pub fn poll (&mut self) -> Option { + let capture_for_jpeg_decoder = self.jpeg_decoder_needs_frame && ! self.jpeg_decoder_has_new_frame; + let capture_for_network_send = self.network_send_needs_frame && ! self.network_send_has_new_frame; + if self.gui_needs_frame && self.gui_has_new_frame { self.gui_has_new_frame = false; @@ -105,7 +134,13 @@ impl Controller // println! ("StartJpegDecoder"); Some (MsgFromController::StartJpegDecoder (self.jpeg_frame.clone ())) } - else if self.jpeg_decoder_needs_frame && ! self.jpeg_decoder_has_new_frame && ! self.capture_is_running + else if self.network_send_has_new_frame && ! self.network_send_is_running && self.network_send_needs_frame + { + self.network_send_is_running = true; + self.network_send_needs_frame = false; + Some (MsgFromController::StartNetworkSend (self.jpeg_frame.clone ())) + } + else if (capture_for_jpeg_decoder || capture_for_network_send) && ! self.capture_is_running { self.capture_is_running = true; // println! ("StartCapture"); diff --git a/src/driver.rs b/src/driver.rs index 2b3e568..d6f977d 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -21,8 +21,9 @@ use crate:: pub enum MsgToDriver { GuiNeedsRgbaFrame, - GotCapture ((Capture, JpegFrame)), DecodedJpegToRgba (RgbaFrame), + NetworkWriteFinished, + GotCapture ((Capture, JpegFrame)), } pub enum MsgToGui @@ -75,19 +76,23 @@ impl Driver { match self.recv.recv().unwrap () { + MsgToDriver::GuiNeedsRgbaFrame => + { + self.ctl.handle_gui_needs_frame (); + }, MsgToDriver::DecodedJpegToRgba (frame) => { self.ctl.handle_rgba_frame (frame); }, + MsgToDriver::NetworkWriteFinished => + { + self.ctl.handle_network_write_finished (); + }, MsgToDriver::GotCapture ((capture, jpeg)) => { self.ctl.handle_capture (jpeg); self.capture = Some (capture); }, - MsgToDriver::GuiNeedsRgbaFrame => - { - self.ctl.handle_gui_needs_frame (); - }, } while let Some (msg) = self.ctl.poll () @@ -120,6 +125,16 @@ impl Driver send.send (MsgToDriver::DecodedJpegToRgba (rgba)).unwrap (); }); }, + MsgFromController::StartNetworkSend (jpeg) => + { + let send = self.send.clone (); + pool.spawn (move || + { + sleep_ms (50); + + send.send (MsgToDriver::NetworkWriteFinished).unwrap (); + }); + }, MsgFromController::StartCapture => { let mut capture = self.capture.take ().unwrap ();