summaryrefslogtreecommitdiff
path: root/crates/renderer/src/overlay/handcam.rs
blob: fadbf24e1027f7efd21a5239ce97e3bc21a75365 (plain)
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
use teleia::*;

use crate::overlay;

const WIDTH: usize = 1920;
const HEIGHT: usize = 1080;
const BLOCK_SIZE: usize = 16;
const BWIDTH: usize = WIDTH / BLOCK_SIZE;
const BHEIGHT: usize = HEIGHT / BLOCK_SIZE;

pub struct Overlay {
    camera: nokhwa::Camera,
    texbuf: Vec<u8>,
}
impl Overlay {
    pub fn new(ctx: &context::Context) -> Self {
        let index = nokhwa::utils::CameraIndex::Index(0);
        let requested = nokhwa::utils::RequestedFormat::new::<nokhwa::pixel_format::RgbAFormat>(
            nokhwa::utils::RequestedFormatType::AbsoluteHighestFrameRate
        );
        let mut camera = nokhwa::Camera::new(index, requested).expect("failed to open webcam");
        camera.open_stream().expect("failed to open stream");
        Self {
            camera,
            texbuf: vec![0; WIDTH * HEIGHT * 4],
        }
    }
}

impl overlay::Overlay for Overlay {
    fn handle_binary(&mut self, ctx: &context::Context, st: &mut state::State, ost: &mut overlay::State, msg: &net::fig::BinaryMessage) -> Erm<()> {
        Ok(())
    }
    fn update(&mut self, ctx: &context::Context, st: &mut state::State, ost: &mut overlay::State) -> Erm<()> {
        let frame = self.camera.frame().unwrap();
        frame.decode_image_to_buffer::<nokhwa::pixel_format::RgbAFormat>(&mut self.texbuf)
            .expect("failed to decode frame");
        Ok(())
    }
    fn render(&mut self, ctx: &context::Context, st: &mut state::State, ost: &mut overlay::State) -> Erm<()> {
        Ok(())
    }
}