Initial proof of concept

main
Elnu 6 months ago
parent 85c91f3052
commit 341f67c80b

1249
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -23,15 +23,14 @@ Some utility commands:
};
rustSettings = with pkgs; {
src = ./.;
#nativeBuildInputs = [ pkg-config ];
#buildInputs = [ openssl ];
buildInputs = with pkgs; [ ffmpeg ];
cargoHash = nixpkgs.lib.fakeHash;
};
meta = with nixpkgs.lib; {
#homepage = "https://example.com";
#license = [ licenses.gpl3 ];
homepage = "https://git.elnu.com/ElnuDev/webcam-streamer";
license = [ licenses.gpl3 ];
platforms = [ system ];
#maintainers = with maintainers; [ ];
maintainers = with maintainers; [ elnudev ];
};
in {
devShells.${system}.default = with pkgs; mkShell {

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4.4.0"
lazy_static = "1.4.0"
rscam = { version = "0.5.5", features = ["no_wrapper"] }

@ -1,3 +1,59 @@
fn main() {
println!("Hello, world!");
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
}

Loading…
Cancel
Save