generated from ElnuDev/rust-project
parent
85c91f3052
commit
341f67c80b
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,59 @@
|
|||||||
fn main() {
|
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
|
||||||
println!("Hello, world!");
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue