Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dskkato committed Nov 26, 2019
1 parent 6a8fd6c commit a02c7c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "mjpeg_test"
name = "mjpeg-rs"
version = "0.1.0"
authors = ["Daisuke Kato"]
edition = "2018"
Expand Down
33 changes: 21 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use actix_rt::Arbiter;
use actix_web::error::ErrorInternalServerError;
use actix_web::web::{Bytes, Data, Path};
use actix_web::http::header;
use actix_web::{web, App, Error, HttpResponse, HttpServer, Responder};

use env_logger;
Expand Down Expand Up @@ -37,26 +38,27 @@ fn index() -> impl Responder {
let content = include_str!("index.html");

HttpResponse::Ok()
.header("content-type", "text/html")
.header("Content-Type", "text/html")
.body(content)
}

fn new_client(broadcaster: Data<Mutex<Broadcaster>>) -> impl Responder {
let rx = broadcaster.lock().unwrap().new_client();

HttpResponse::Ok()
.header("Cache-Control", "no-store, must-revalidate")
.header("Pragma", "no-cache")
.header("Expires", "0")
.header("Connection", "close")
.header(
"content-type",
"Content-Type",
"multipart/x-mixed-replace;boundary=boundarydonotcross",
)
.no_chunking()
.streaming(rx)
}

fn broadcast(
msg: Path<String>,
broadcaster: Data<Mutex<Broadcaster>>,
) -> impl Responder {
fn broadcast(msg: Path<String>, broadcaster: Data<Mutex<Broadcaster>>) -> impl Responder {
broadcaster.lock().unwrap().send(&msg.into_inner());

HttpResponse::Ok().body("msg sent")
Expand Down Expand Up @@ -121,13 +123,17 @@ impl Broadcaster {
.encode(&buf, 640, 480, image::ColorType::Gray(8))
.unwrap();

let msg = format!("--boundarydonotcross\r\ncontent-length:{}\r\ncontent-type:image/jpeg\r\n\r\n", out.len());
let mut buf2: Vec<u8> = Vec::from(msg.as_bytes());
buf2.extend(out.iter().clone());
buf2.extend("\r\n".as_bytes());
let mut msg: Vec<u8> = Vec::from(
format!(
"--boundarydonotcross\r\ncontent-length:{}\r\ncontent-type:image/jpeg\r\n\r\n",
out.len()
)
.as_bytes(),
);
msg.extend(out.iter().clone());

for client in self.clients.iter() {
let result = client.clone().try_send(Bytes::from(&buf2[..]));
let result = client.clone().try_send(Bytes::from(&msg[..]));

if let Ok(()) = result {
ok_clients.push(client.clone());
Expand Down Expand Up @@ -158,7 +164,10 @@ impl Broadcaster {
.encode(&buf, 640, 480, image::ColorType::Gray(8))
.unwrap();

let msg = format!("--boundarydonotcross\r\ncontent-length:{}\r\ncontent-type:image/jpeg\r\n\r\n", out.len());
let msg = format!(
"--boundarydonotcross\r\ncontent-length:{}\r\ncontent-type:image/jpeg\r\n\r\n",
out.len()
);
let mut buf2: Vec<u8> = Vec::from(msg.as_bytes());
buf2.extend(out.iter().clone());
buf2.extend("\r\n".as_bytes());
Expand Down

0 comments on commit a02c7c0

Please sign in to comment.