five_five_five/src/driver.rs

164 lines
4.9 KiB
Rust
Raw Normal View History

2023-09-13 03:24:48 +00:00
use std::
{
sync::mpsc,
time::Instant,
};
use eframe::egui;
use crate::
{
capture::Capture,
controller::
{
Controller,
MsgFromController,
JpegFrame,
RgbaFrame,
},
};
pub enum MsgToDriver
{
GuiNeedsRgbaFrame,
DecodedJpegToRgba (RgbaFrame),
2023-09-13 03:54:53 +00:00
NetworkWriteFinished,
GotCapture ((Capture, JpegFrame)),
2023-09-13 03:24:48 +00:00
}
pub enum MsgToGui
{
NewRgbaFrame (RgbaFrame),
}
pub struct Driver
{
send: mpsc::SyncSender <MsgToDriver>,
recv: mpsc::Receiver <MsgToDriver>,
gui_ctx: egui::Context,
send_to_gui: mpsc::SyncSender <MsgToGui>,
capture: Option <Capture>,
ctl: Controller,
}
fn sleep_ms (ms: u64)
{
std::thread::sleep (std::time::Duration::from_millis (ms));
}
impl Driver
{
pub fn new (
send: mpsc::SyncSender <MsgToDriver>,
recv: mpsc::Receiver <MsgToDriver>,
gui_ctx: egui::Context,
send_to_gui: mpsc::SyncSender <MsgToGui>,
) -> Self
{
Self {
send,
recv,
gui_ctx,
send_to_gui,
capture: Some (Capture::new ().unwrap ()),
ctl: Controller::new (Instant::now ()),
}
}
pub fn run (&mut self)
{
let pool = rayon::ThreadPoolBuilder::new().build().unwrap ();
loop
{
match self.recv.recv().unwrap ()
{
2023-09-13 03:54:53 +00:00
MsgToDriver::GuiNeedsRgbaFrame =>
{
self.ctl.handle_gui_needs_frame ();
},
2023-09-13 03:24:48 +00:00
MsgToDriver::DecodedJpegToRgba (frame) =>
{
self.ctl.handle_rgba_frame (frame);
},
2023-09-13 03:54:53 +00:00
MsgToDriver::NetworkWriteFinished =>
{
self.ctl.handle_network_write_finished ();
},
2023-09-13 03:24:48 +00:00
MsgToDriver::GotCapture ((capture, jpeg)) =>
{
self.ctl.handle_capture (jpeg);
self.capture = Some (capture);
},
}
while let Some (msg) = self.ctl.poll ()
{
match msg
{
MsgFromController::RepaintGui (rgba_frame) =>
{
self.send_to_gui.send (MsgToGui::NewRgbaFrame (rgba_frame)).unwrap ();
self.gui_ctx.request_repaint();
},
MsgFromController::StartJpegDecoder (jpeg_frame) =>
{
let send = self.send.clone ();
pool.spawn (move ||
{
// sleep_ms (500);
let mut decoder = zune_jpeg::JpegDecoder::new_with_options (&jpeg_frame.data, zune_core::options::DecoderOptions::new_fast().jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::RGBA));
decoder.decode_headers().unwrap ();
let mut rgba = vec![0u8;decoder.output_buffer_size().unwrap ()];
decoder.decode_into(&mut rgba).unwrap ();
let rgba = RgbaFrame
{
data: rgba,
capture_time: jpeg_frame.capture_time,
};
send.send (MsgToDriver::DecodedJpegToRgba (rgba)).unwrap ();
});
2023-09-13 03:54:53 +00:00
},
MsgFromController::StartNetworkSend (jpeg) =>
{
let send = self.send.clone ();
pool.spawn (move ||
{
sleep_ms (50);
send.send (MsgToDriver::NetworkWriteFinished).unwrap ();
});
2023-09-13 03:24:48 +00:00
},
MsgFromController::StartCapture =>
{
let mut capture = self.capture.take ().unwrap ();
let send = self.send.clone ();
pool.spawn (move ||
{
let mut data = vec! [0u8; capture.size_image()];
capture.wait_for_frame(&mut data).unwrap ();
let frame = JpegFrame
{
data,
capture_time: Instant::now (),
};
// sleep_ms (500);
send.send (MsgToDriver::GotCapture ((capture, frame))).unwrap ();
});
}
}
}
}
}
}