generated from ElnuDev/rust-project
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
|
|
use lazy_static::lazy_static;
|
|
use rscam::Camera;
|
|
use std::{
|
|
sync::Mutex,
|
|
thread,
|
|
};
|
|
|
|
const FPS: u32 = 30;
|
|
|
|
lazy_static! {
|
|
static ref CAMERA: Camera = {
|
|
let mut camera = rscam::new("/dev/video0").unwrap();
|
|
camera
|
|
.start(&rscam::Config {
|
|
interval: (1, FPS),
|
|
resolution: (1280, 720),
|
|
format: b"MJPG",
|
|
..Default::default()
|
|
})
|
|
.unwrap();
|
|
camera
|
|
};
|
|
static ref CURRENT_FRAME: Mutex<Vec<u8>> = Mutex::new(Vec::new());
|
|
}
|
|
|
|
#[get("/feed.jpg")]
|
|
async fn get_feed() -> impl Responder {
|
|
HttpResponse::Ok()
|
|
.content_type("image/jpeg")
|
|
.body(CURRENT_FRAME.lock().unwrap().clone())
|
|
}
|
|
|
|
#[get("/")]
|
|
async fn get_index() -> impl Responder {
|
|
HttpResponse::Ok().content_type("text/html").body(format!(
|
|
"
|
|
<img src=\"feed.jpg\">
|
|
<script>
|
|
const img = document.querySelector(\"img\");
|
|
setInterval(() => {{
|
|
img.src = \"feed.jpg?\" + new Date().getTime();
|
|
}}, {});
|
|
</script>
|
|
",
|
|
1000.0 / FPS as f32
|
|
))
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
thread::spawn(move || loop {
|
|
*CURRENT_FRAME.lock().unwrap() = CAMERA.capture().unwrap()[..].to_owned();
|
|
});
|
|
HttpServer::new(|| App::new().service(get_index).service(get_feed))
|
|
.bind(("127.0.0.1", 8080))?
|
|
.run()
|
|
.await
|
|
}
|