-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use std::{ | ||
sync::mpsc::{channel, Receiver, Sender}, | ||
thread::JoinHandle, | ||
}; | ||
|
||
use crate::{Camera, CameraInfo}; | ||
|
||
enum Message { | ||
NewDefaultDevice, | ||
EnumerateCameras, | ||
Check failure on line 10 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 10 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 10 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 10 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check failure on line 10 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check warning on line 10 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check warning on line 10 in src/camera_on_thread.rs GitHub Actions / windows (stable)
Check failure on line 10 in src/camera_on_thread.rs GitHub Actions / windows (stable)
|
||
Start, | ||
Stop, | ||
WaitForFrame, | ||
ChangeDevice, | ||
} | ||
|
||
enum Response { | ||
EnumerateCameras(Vec<CameraInfo>), | ||
WaitForFrame(((u32, u32), Vec<u8>)), | ||
} | ||
|
||
pub struct CameraOnThread { | ||
messages: Sender<Message>, | ||
responses: Receiver<Response>, | ||
thread_join: JoinHandle<()>, | ||
Check failure on line 25 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 25 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 25 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check warning on line 25 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check failure on line 25 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check warning on line 25 in src/camera_on_thread.rs GitHub Actions / macos (stable)
Check warning on line 25 in src/camera_on_thread.rs GitHub Actions / windows (stable)
Check failure on line 25 in src/camera_on_thread.rs GitHub Actions / windows (stable)
|
||
} | ||
|
||
impl CameraOnThread { | ||
pub fn new_default_device() -> Self { | ||
let (messages, message_rx) = channel::<Message>(); | ||
let (response_tx, responses) = channel::<Response>(); | ||
let thread_join = std::thread::spawn(move || { | ||
use Message::*; | ||
let mut camera = None; | ||
let mut send_result = Ok(()); | ||
while let (Ok(message), Ok(())) = (message_rx.recv(), &send_result) { | ||
match message { | ||
NewDefaultDevice => { | ||
camera = Some(Camera::new_default_device()); | ||
} | ||
EnumerateCameras => { | ||
send_result = response_tx | ||
.send(Response::EnumerateCameras(Camera::enumerate_cameras())); | ||
} | ||
Start => { | ||
camera.as_ref().map(|c| c.start()); | ||
Check failure on line 46 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check failure on line 46 in src/camera_on_thread.rs GitHub Actions / macos (stable)
|
||
} | ||
Stop => { | ||
camera.as_ref().map(|c| c.stop()); | ||
Check failure on line 49 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check failure on line 49 in src/camera_on_thread.rs GitHub Actions / macos (stable)
|
||
} | ||
WaitForFrame => { | ||
if let Some(ref camera) = camera { | ||
if let Some(frame) = camera.wait_for_frame() { | ||
let size = frame.size_u32(); | ||
let pixels = frame.data().data_u8().to_vec(); | ||
let response = Response::WaitForFrame((size, pixels)); | ||
send_result = response_tx.send(response); | ||
} | ||
} | ||
} | ||
ChangeDevice => { | ||
camera.as_mut().map(|c| c.change_device()); | ||
Check failure on line 62 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check failure on line 62 in src/camera_on_thread.rs GitHub Actions / macos (stable)
|
||
} | ||
} | ||
} | ||
if let Some(camera) = camera { | ||
camera.stop(); | ||
} | ||
}); | ||
|
||
messages.send(Message::NewDefaultDevice).unwrap(); | ||
|
||
CameraOnThread { messages, responses, thread_join } | ||
} | ||
|
||
pub fn enumerate_cameras() -> Vec<CameraInfo> { | ||
std::thread::spawn(|| Camera::enumerate_cameras()).join().unwrap() | ||
Check failure on line 77 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check failure on line 77 in src/camera_on_thread.rs GitHub Actions / macos (stable)
|
||
} | ||
|
||
pub fn start(&self) { | ||
self.messages.send(Message::Start).unwrap(); | ||
} | ||
|
||
pub fn stop(&self) { | ||
self.messages.send(Message::Stop).unwrap(); | ||
} | ||
|
||
pub fn change_device(&mut self) { | ||
self.messages.send(Message::ChangeDevice).unwrap(); | ||
} | ||
|
||
pub fn wait_for_frame(&self) -> Option<((u32, u32), Vec<u8>)> { | ||
self.messages.send(Message::WaitForFrame).unwrap(); | ||
if let Response::WaitForFrame((size, pixels)) = self.responses.recv().unwrap() { | ||
return Some((size, pixels)); | ||
Check failure on line 95 in src/camera_on_thread.rs GitHub Actions / linux (stable)
Check failure on line 95 in src/camera_on_thread.rs GitHub Actions / macos (stable)
|
||
} else { | ||
todo!(); | ||
} | ||
} | ||
} |