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> = 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!( " ", 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 }