nannou_webcam 0.20.0

The webcam API for nannou - the creative-coding framework.
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::thread;
use std::time::{Duration, Instant};

use bevy::asset::RenderAssetUsages;
use bevy::prelude::*;
use bevy::render::render_resource::{Extent3d, TextureDimension};
use flume::{Receiver, Sender};
use nokhwa::Camera;
use nokhwa::pixel_format::RgbFormat;
use nokhwa::utils::{
    ApiBackend, CameraFormat, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType,
    Resolution,
};

use crate::components::*;
use crate::events::*;
use crate::util::*;

pub(crate) struct FramePayload {
    pixels: Vec<u8>,
    extent: Extent3d,
}

struct CaptureWorker {
    handle: Option<thread::JoinHandle<()>>,
}

impl Drop for CaptureWorker {
    fn drop(&mut self) {
        if let Some(handle) = self.handle.take()
            && let Err(err) = handle.join()
        {
            warn!("capture worker exited with error: {:?}", err);
        }
    }
}

#[derive(Component)]
pub(crate) struct WebcamCapture {
    receiver: Receiver<FramePayload>,
    _worker: CaptureWorker,
    is_srgb: bool,
    device_entity: Entity,
}

#[derive(Component)]
pub(crate) struct NativeDeviceIndex(pub CameraIndex);

#[derive(Resource)]
struct DeviceEnumerationTimer {
    last_check: Instant,
}

pub(crate) fn init(app: &mut App) {
    #[cfg(target_os = "macos")]
    nokhwa_initialize_blocking();

    app.insert_resource(DeviceEnumerationTimer {
        last_check: Instant::now() - Duration::from_secs(10),
    });

    app.add_systems(
        PreUpdate,
        (enumerate_webcam_devices, open_webcams, upload_native_frames).chain(),
    );

    app.add_observer(on_webcam_removed);
}

#[cfg(target_os = "macos")]
fn nokhwa_initialize_blocking() {
    use std::sync::mpsc::channel;

    let (tx, rx) = channel();
    nokhwa::nokhwa_initialize(move |success| {
        let _ = tx.send(success);
    });

    match rx.recv() {
        Ok(true) => {}
        Ok(false) => panic!("user denied camera permission"),
        Err(_) => panic!("initialization channel closed unexpectedly"),
    }
}

fn query_device_formats(index: &CameraIndex) -> Vec<WebcamSupportedFormat> {
    let requested =
        RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestFrameRate);
    let mut camera = match Camera::new(index.clone(), requested) {
        Ok(c) => c,
        Err(err) => {
            debug!("failed to query device formats: {err}");
            return Vec::new();
        }
    };

    match camera.compatible_camera_formats() {
        Ok(formats) => formats
            .into_iter()
            .map(|f| WebcamSupportedFormat {
                resolution: UVec2::new(f.resolution().width_x, f.resolution().height_y),
                framerate: f.frame_rate(),
            })
            .collect(),
        Err(err) => {
            debug!("failed to query compatible formats: {err}");
            Vec::new()
        }
    }
}

fn enumerate_webcam_devices(
    mut commands: Commands,
    existing_devices: Query<(Entity, &NativeDeviceIndex), With<WebcamDevice>>,
    active_captures: Query<(Entity, &WebcamCapture)>,
    mut timer: ResMut<DeviceEnumerationTimer>,
) {
    let now = Instant::now();
    if now.duration_since(timer.last_check) < Duration::from_secs(2) {
        return;
    }
    timer.last_check = now;

    let discovered = match nokhwa::query(ApiBackend::Auto) {
        Ok(devices) => devices,
        Err(err) => {
            warn!("failed to enumerate webcam devices: {err}");
            return;
        }
    };

    let discovered_indices: Vec<_> = discovered.iter().map(|d| d.index().clone()).collect();

    for info in &discovered {
        let index = info.index().clone();
        let already_exists = existing_devices.iter().any(|(_, idx)| idx.0 == index);
        if !already_exists {
            let formats = query_device_formats(&index);
            let entity = commands
                .spawn((
                    Name::new(info.human_name().to_string()),
                    WebcamDevice {
                        description: info.description().to_string(),
                        formats,
                    },
                    NativeDeviceIndex(index),
                ))
                .id();
            commands
                .entity(entity)
                .trigger(|e| WebcamDeviceAdded { entity: e });
        }
    }

    for (entity, idx) in &existing_devices {
        if !discovered_indices.contains(&idx.0) {
            for (stream_entity, capture) in &active_captures {
                if capture.device_entity == entity {
                    commands
                        .entity(stream_entity)
                        .remove::<(WebcamCapture, WebcamStream)>();
                    commands
                        .entity(stream_entity)
                        .trigger(|e| WebcamDisconnected {
                            entity: e,
                            reason: "device removed".to_string(),
                        });
                }
            }
            commands
                .entity(entity)
                .trigger(|e| WebcamDeviceRemoved { entity: e });
            commands.entity(entity).despawn();
        }
    }
}

fn open_webcams(
    mut commands: Commands,
    new_webcams: Query<(Entity, &Webcam), Added<Webcam>>,
    devices: Query<(Entity, &NativeDeviceIndex), With<WebcamDevice>>,
    active_captures: Query<&WebcamCapture>,
    mut images: ResMut<Assets<Image>>,
) {
    for (stream_entity, webcam) in &new_webcams {
        let device_entity = if let Some(device) = webcam.device {
            device
        } else {
            let in_use: Vec<Entity> = active_captures.iter().map(|c| c.device_entity).collect();
            match devices
                .iter()
                .filter(|(e, _)| !in_use.contains(e))
                .min_by_key(|(_, idx)| idx.0.clone())
            {
                Some((e, _)) => e,
                None => {
                    let msg = "no available webcam device found";
                    warn!("{msg}");
                    commands.entity(stream_entity).insert(WebcamError {
                        message: msg.to_string(),
                    });
                    commands
                        .entity(stream_entity)
                        .trigger(|e| WebcamDisconnected {
                            entity: e,
                            reason: msg.to_string(),
                        });
                    continue;
                }
            }
        };

        if active_captures
            .iter()
            .any(|c| c.device_entity == device_entity)
        {
            let msg = "device already in use by another stream";
            commands.entity(stream_entity).insert(WebcamError {
                message: msg.to_string(),
            });
            commands
                .entity(stream_entity)
                .trigger(|e| WebcamDisconnected {
                    entity: e,
                    reason: msg.to_string(),
                });
            continue;
        }

        let Ok((_, native_index)) = devices.get(device_entity) else {
            let msg = "referenced device entity not found";
            commands.entity(stream_entity).insert(WebcamError {
                message: msg.to_string(),
            });
            commands
                .entity(stream_entity)
                .trigger(|e| WebcamDisconnected {
                    entity: e,
                    reason: msg.to_string(),
                });
            continue;
        };

        let format_type = match webcam.format {
            WebcamFormat::HighestFrameRate => RequestedFormatType::AbsoluteHighestFrameRate,
            WebcamFormat::HighestResolution => RequestedFormatType::AbsoluteHighestResolution,
            WebcamFormat::Resolution(res) => {
                RequestedFormatType::HighestResolution(Resolution::new(res.x, res.y))
            }
            WebcamFormat::FrameRate(fps) => RequestedFormatType::HighestFrameRate(fps),
            WebcamFormat::Exact {
                resolution,
                framerate,
            } => RequestedFormatType::Closest(CameraFormat::new(
                Resolution::new(resolution.x, resolution.y),
                FrameFormat::MJPEG,
                framerate,
            )),
        };

        let requested = RequestedFormat::new::<RgbFormat>(format_type);
        let camera = match Camera::new(native_index.0.clone(), requested) {
            Ok(c) => c,
            Err(err) => {
                let msg = format!("failed to create camera: {err}");
                commands.entity(stream_entity).insert(WebcamError {
                    message: msg.clone(),
                });
                let reason = msg;
                commands
                    .entity(stream_entity)
                    .trigger(|e| WebcamDisconnected { entity: e, reason });
                continue;
            }
        };

        match open_camera_stream(camera, webcam.srgb, device_entity, &mut images) {
            Ok((capture, stream)) => {
                let resolution = stream.resolution;
                let framerate = stream.framerate;
                commands.entity(stream_entity).insert((capture, stream));
                commands.entity(stream_entity).trigger(|e| WebcamConnected {
                    entity: e,
                    resolution,
                    framerate,
                });
            }
            Err(msg) => {
                commands.entity(stream_entity).insert(WebcamError {
                    message: msg.clone(),
                });
                let reason = msg;
                commands
                    .entity(stream_entity)
                    .trigger(|e| WebcamDisconnected { entity: e, reason });
            }
        }
    }
}

fn open_camera_stream(
    mut camera: Camera,
    is_srgb: bool,
    device_entity: Entity,
    images: &mut Assets<Image>,
) -> Result<(WebcamCapture, WebcamStream), String> {
    camera
        .open_stream()
        .map_err(|e| format!("failed to open camera stream: {e}"))?;

    let framerate = camera.frame_rate();
    let resolution = camera.resolution();
    info!(
        "camera framerate: {framerate}, resolution: {}x{}",
        resolution.width_x, resolution.height_y
    );

    let extent = Extent3d {
        width: resolution.width_x,
        height: resolution.height_y,
        depth_or_array_layers: 1,
    };

    let format = frame_texture_format(is_srgb);
    let image_handle = images.add(Image::new_fill(
        extent,
        TextureDimension::D2,
        &[0u8; 4],
        format,
        RenderAssetUsages::default(),
    ));

    let (sender, receiver) = flume::bounded(2);
    let handle = thread::Builder::new()
        .name("nannou_webcam_capture".to_string())
        .spawn(move || capture_frames(camera, sender))
        .map_err(|e| format!("failed to spawn capture thread: {e}"))?;

    Ok((
        WebcamCapture {
            receiver,
            _worker: CaptureWorker {
                handle: Some(handle),
            },
            is_srgb,
            device_entity,
        },
        WebcamStream {
            image: image_handle,
            resolution: UVec2::new(resolution.width_x, resolution.height_y),
            framerate,
        },
    ))
}

fn upload_native_frames(
    mut captures: Query<(&WebcamCapture, &mut WebcamStream)>,
    mut images: ResMut<Assets<Image>>,
) {
    for (capture, mut stream) in &mut captures {
        let mut latest_frame = None;
        while let Ok(frame) = capture.receiver.try_recv() {
            latest_frame = Some(frame);
        }

        let Some(frame) = latest_frame else {
            continue;
        };

        let Some(mut image) = images.get_mut(&stream.image) else {
            warn!("webcam texture handle is missing");
            continue;
        };

        let format = frame_texture_format(capture.is_srgb);
        let new_resolution = UVec2::new(frame.extent.width, frame.extent.height);
        if stream.resolution != new_resolution {
            warn!(
                "camera resolution changed from {}x{} to {}x{}",
                stream.resolution.x, stream.resolution.y, new_resolution.x, new_resolution.y,
            );
            stream.resolution = new_resolution;
        }

        write_frame_to_image(&mut *image, frame.extent, frame.pixels, format);
    }
}

fn capture_frames(mut camera: Camera, sender: Sender<FramePayload>) {
    loop {
        match camera.frame() {
            Ok(frame) => match frame.decode_image::<RgbFormat>() {
                Ok(image) => {
                    let (width, height) = image.dimensions();
                    let extent = Extent3d {
                        width,
                        height,
                        depth_or_array_layers: 1,
                    };
                    let rgba_pixels = rgb_to_rgba(&image.into_raw());
                    if sender
                        .send(FramePayload {
                            pixels: rgba_pixels,
                            extent,
                        })
                        .is_err()
                    {
                        break;
                    }
                }
                Err(err) => error!("failed to decode camera frame: {err}"),
            },
            Err(err) => {
                error!("failed to get camera frame: {err}");
                thread::sleep(Duration::from_millis(16));
            }
        }
    }
}

fn on_webcam_removed(event: On<Remove, Webcam>, mut commands: Commands) {
    let entity = event.event_target();
    commands
        .entity(entity)
        .remove::<(WebcamCapture, WebcamStream, WebcamError)>();
}