summaryrefslogtreecommitdiff
path: root/src/bin/sshcamera.rs
blob: 270700aceae003e322315d5ada9069f804581e94 (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
45
46
47
48
49
50
51
52
53
54
55
use anyhow::{anyhow, Result};
use sshcamera::v4l2::{Device as V4l2, Field};
use sshcamera::v4l2cairo::V4l2Cairo;
use sshcamera::gtk;
use sshcamera::v4l2abst::{CaptStream, RemoteCam};
use sshcamera::io::RWBundle;
use gtk4::glib::ExitCode;
use std::env;
use std::io::{self, Read as _, Write as _};
use std::process::{Command, Stdio};

fn main() -> Result<ExitCode>{
    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.len() == 0{
        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(())
            })??;
        }
    }else{
        let child = Command::new(arg1)
            .args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn()?;
        let io = RWBundle(child.stdout.unwrap(), child.stdin.unwrap());
        let v2c = V4l2Cairo::new(RemoteCam::new(io));
        gtk::main(v2c)
    }
}