summaryrefslogtreecommitdiff
path: root/src/bin/usshcamera.rs
blob: 6d73e92aad6c060178f1d8a6525f30f0faa77166 (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
44
use anyhow::{anyhow, Result};
use sshcamera::v4l2::{Device as V4l2, Field};
use sshcamera::v4l2abst::CaptStream;
use sshcamera::io::RWBundle;
use std::env;
use std::io::{self, Read as _, Write as _};

fn main() -> Result<()>{
    let mut args = env::args();
    if args.next() == None{
        return Err(anyhow!("arg0 is not present??"));
    }
    let Some(arg1) = args.next() else{
        return Err(anyhow!("Give me args"));
    };
    if args.next() != None{
        return Err(anyhow!("too many args"));
    }

    let v = V4l2::open(arg1)?;

    // TODO: It should be better.
    let mut c = v.captstream_builder()?
        .set_pixelformat("MJPG".into())
        //.set_pixelformat("YUYV".into())
        .set_field(Field::None)
        .build()?;
    assert!(["YUYV", "MJPG"].contains(&c.pixelformat().as_str()));
    assert!(c.field() == Field::None);

    let mut io = RWBundle(io::stdin(), io::stdout());
    loop{
        CaptStream::next(&mut c, |frame|{
            frame.serialize(&mut io)?;
            io.flush()?;
            let mut rb = [0];
            io.read_exact(&mut rb)?;
            if rb[0] != 0x2e{
                return Err(anyhow!("protocol error"));
            }
            Ok(())
        })??;
    }
}