Repository navigation
Expand file tree
/
Copy pathcamera.rs
More file actions
294 lines (269 loc) · 10.5 KB
/
Copy pathcamera.rs
File metadata and controls
294 lines (269 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Webcam feed: a background thread opens a camera while the camera panel is
//! open, decodes frames to RGB and keeps the latest one for the UI.
//!
//! The camera is only opened on request and released as soon as the panel is
//! closed. Frames never leave the machine; snapshots are saved locally.
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::Instant;
use eframe::egui;
use nokhwa::pixel_format::RgbFormat;
use nokhwa::utils::{CameraFormat, FrameFormat, RequestedFormat, RequestedFormatType, Resolution};
#[derive(Clone, Debug, Default, PartialEq)]
pub enum CamStatus {
#[default]
Off,
Starting,
Live,
Error(String),
}
/// One decoded frame, tightly packed RGB8.
pub struct Frame {
pub width: u32,
pub height: u32,
pub rgb: Vec<u8>,
}
#[derive(Clone, Default)]
pub struct CamState {
pub status: CamStatus,
/// Names of the cameras found, in the order the OS lists them.
pub devices: Vec<String>,
/// Index into `devices` of the camera in use.
pub active: usize,
/// e.g. "1280×720 · NV12"
pub format: String,
pub frame: Option<Arc<Frame>>,
/// Incremented for every new frame, so the UI only re-uploads changed frames.
pub seq: u64,
/// Measured frames per second.
pub fps: f32,
}
pub type SharedCam = Arc<Mutex<CamState>>;
/// Owns the capture thread. Dropping it (or calling `stop`) releases the camera.
pub struct Camera {
pub shared: SharedCam,
stop: Arc<AtomicBool>,
thread: Option<JoinHandle<()>>,
}
impl Default for Camera {
fn default() -> Self {
Self { shared: SharedCam::default(), stop: Arc::new(AtomicBool::new(false)), thread: None }
}
}
impl Camera {
/// Whether the camera is (or is about to be) capturing.
pub fn running(&self) -> bool {
!self.stop.load(Ordering::Relaxed) && self.thread.as_ref().is_some_and(|t| !t.is_finished())
}
/// Opens camera number `index` (wrapping around the device list) on a background thread.
pub fn start(&mut self, index: usize, ctx: egui::Context) {
self.stop();
// The previous thread must release the device before it can be reopened.
if let Some(t) = self.thread.take() {
let _ = t.join();
}
self.stop = Arc::new(AtomicBool::new(false));
{
let mut st = self.shared.lock().unwrap();
st.status = CamStatus::Starting;
st.frame = None;
st.fps = 0.0;
}
let (shared, stop) = (self.shared.clone(), self.stop.clone());
self.thread = Some(std::thread::spawn(move || {
let result = capture(index, &shared, &stop, &ctx);
let mut st = shared.lock().unwrap();
st.status = match result {
Ok(()) => CamStatus::Off,
Err(e) => CamStatus::Error(e),
};
st.frame = None;
drop(st);
ctx.request_repaint();
}));
}
/// Asks the capture thread to stop. Does not wait: opening a device can take
/// a while, and the UI must not freeze. The thread releases the camera after
/// its current frame.
pub fn stop(&mut self) {
self.stop.store(true, Ordering::Relaxed);
}
/// Switches to the next camera in the device list.
pub fn next(&mut self, ctx: egui::Context) {
let next = self.shared.lock().unwrap().active + 1;
self.start(next, ctx);
}
}
impl Drop for Camera {
fn drop(&mut self) {
self.stop();
}
}
/// Formats to try, best first. `Camera::new` fails when a request matches no
/// format the device offers, so we fall back to whatever the device can do.
/// Many laptop webcams (e.g. HP FHD Camera on Media Foundation) offer only NV12.
fn requests() -> [RequestedFormat<'static>; 4] {
let hd = |f| RequestedFormatType::Closest(CameraFormat::new(Resolution::new(1280, 720), f, 30));
[
RequestedFormat::new::<RgbFormat>(hd(FrameFormat::MJPEG)),
RequestedFormat::new::<RgbFormat>(hd(FrameFormat::YUYV)),
RequestedFormat::new::<RgbFormat>(hd(FrameFormat::NV12)),
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestFrameRate),
]
}
fn capture(index: usize, shared: &SharedCam, stop: &AtomicBool, ctx: &egui::Context) -> Result<(), String> {
#[cfg(target_os = "macos")]
if !nokhwa::nokhwa_check() {
// Asks macOS for camera permission; the answer arrives asynchronously.
let (tx, rx) = std::sync::mpsc::channel();
nokhwa::nokhwa_initialize(move |granted| {
let _ = tx.send(granted);
});
if !rx.recv().unwrap_or(false) {
return Err("Camera access denied. Allow it in System Settings → Privacy & Security → Camera.".into());
}
}
let backend = nokhwa::native_api_backend().ok_or("No camera backend for this platform.")?;
let devices = nokhwa::query(backend).map_err(|e| format!("Could not list cameras: {e}"))?;
if devices.is_empty() {
return Err("No camera found.".into());
}
let active = index % devices.len();
{
let mut st = shared.lock().unwrap();
st.devices = devices.iter().map(|d| d.human_name()).collect();
st.active = active;
}
let target = devices[active].index().clone();
let mut last_err = String::new();
let mut cam = None;
for req in requests() {
match nokhwa::Camera::new(target.clone(), req) {
Ok(c) => {
cam = Some(c);
break;
}
Err(e) => last_err = e.to_string(),
}
}
let mut cam = cam.ok_or_else(|| format!("Could not open the camera: {last_err}"))?;
cam.open_stream().map_err(|e| format!("Could not start the camera (is another app using it?): {e}"))?;
let fmt = cam.camera_format();
{
let mut st = shared.lock().unwrap();
// The reported frame rate is unreliable on Media Foundation; the UI shows the measured one.
st.format = format!("{}×{} · {}", fmt.width(), fmt.height(), fmt.format());
st.status = CamStatus::Live;
}
let mut window_start = Instant::now();
let mut window_frames = 0u32;
let mut failures = 0;
while !stop.load(Ordering::Relaxed) {
let decoded = cam.frame().and_then(|buf| buf.decode_image::<RgbFormat>());
let img = match decoded {
Ok(img) => {
failures = 0;
img
}
// A dropped or corrupt frame now and then is normal; a run of them means the device is gone.
Err(e) => {
failures += 1;
if failures > 30 {
let _ = cam.stop_stream();
return Err(format!("Camera stopped responding: {e}"));
}
continue;
}
};
let frame = Frame { width: img.width(), height: img.height(), rgb: img.into_raw() };
window_frames += 1;
let elapsed = window_start.elapsed().as_secs_f32();
let mut st = shared.lock().unwrap();
if elapsed >= 1.0 {
st.fps = window_frames as f32 / elapsed;
window_start = Instant::now();
window_frames = 0;
}
st.frame = Some(Arc::new(frame));
st.seq += 1;
drop(st);
ctx.request_repaint();
}
let _ = cam.stop_stream();
Ok(())
}
/// Where snapshots go: `~/Pictures/DAEMON` (falling back to the home folder).
pub fn snapshot_dir(home: &std::path::Path) -> PathBuf {
let pictures = home.join("Pictures");
if pictures.is_dir() {
pictures.join("DAEMON")
} else {
home.to_path_buf()
}
}
/// Saves `frame` as a timestamped PNG in `dir` and returns its path.
pub fn save_snapshot(frame: &Frame, dir: &std::path::Path) -> Result<PathBuf, String> {
std::fs::create_dir_all(dir).map_err(|e| format!("Could not create {}: {e}", dir.display()))?;
let name = chrono::Local::now().format("daemon-%Y%m%d-%H%M%S.png").to_string();
let path = dir.join(name);
image::save_buffer(&path, &frame.rgb, frame.width, frame.height, image::ExtendedColorType::Rgb8)
.map_err(|e| format!("Could not save snapshot: {e}"))?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn snapshot_round_trips_as_png() {
let dir = std::env::temp_dir().join(format!("daemon-cam-test-{}", std::process::id()));
let frame = Frame { width: 2, height: 1, rgb: vec![255, 0, 0, 0, 255, 0] };
let path = save_snapshot(&frame, &dir).unwrap();
let back = image::open(&path).unwrap().into_rgb8();
assert_eq!(back.dimensions(), (2, 1));
assert_eq!(back.into_raw(), frame.rgb);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn snapshot_dir_prefers_pictures() {
let home = std::env::temp_dir().join(format!("daemon-home-test-{}", std::process::id()));
std::fs::create_dir_all(&home).unwrap();
assert_eq!(snapshot_dir(&home), home);
std::fs::create_dir_all(home.join("Pictures")).unwrap();
assert_eq!(snapshot_dir(&home), home.join("Pictures").join("DAEMON"));
let _ = std::fs::remove_dir_all(&home);
}
/// Opens the real default camera. Run with `cargo test -- --ignored live_camera`.
#[test]
#[ignore]
fn live_camera_captures_a_frame() {
let mut cam = Camera::default();
cam.start(0, egui::Context::default());
let deadline = Instant::now() + std::time::Duration::from_secs(15);
loop {
let st = cam.shared.lock().unwrap().clone();
if let CamStatus::Error(e) = &st.status {
panic!("camera error: {e}");
}
if let Some(f) = &st.frame {
println!("{:?} {} on {:?}: {}x{}", st.status, st.format, st.devices, f.width, f.height);
assert_eq!(f.rgb.len(), (f.width * f.height * 3) as usize);
std::thread::sleep(std::time::Duration::from_secs(3));
println!("measured {:.1} FPS", cam.shared.lock().unwrap().fps);
break;
}
assert!(Instant::now() < deadline, "no frame within 15 s, status {:?}", st.status);
std::thread::sleep(std::time::Duration::from_millis(100));
}
cam.stop();
}
#[test]
fn idle_camera_is_not_running() {
let mut cam = Camera::default();
assert!(!cam.running());
cam.stop();
assert!(!cam.running());
assert_eq!(cam.shared.lock().unwrap().status, CamStatus::Off);
}
}