-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
210359d
commit 1585a75
Showing
12 changed files
with
299 additions
and
268 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,87 @@ | ||
use std::sync::Arc; | ||
use std::{pin::pin, sync::Arc, task::Poll}; | ||
|
||
use cap_media::feeds::CameraFrameReceiver; | ||
use futures::{FutureExt, SinkExt, StreamExt}; | ||
|
||
// TODO: Possibly replace this with ffmpeg's network outputs in the pipeline somehow? | ||
pub async fn create_camera_ws(frame_rx: CameraFrameReceiver) -> u16 { | ||
use axum::{ | ||
extract::{ | ||
ws::{Message, WebSocket, WebSocketUpgrade}, | ||
State, | ||
}, | ||
response::IntoResponse, | ||
routing::get, | ||
}; | ||
use tokio::sync::Mutex; | ||
// pub async fn create_camera_ws(frame_rx: CameraFrameReceiver) -> u16 { | ||
// use axum::{ | ||
// extract::{ | ||
// ws::{Message, WebSocket, WebSocketUpgrade}, | ||
// State, | ||
// }, | ||
// response::IntoResponse, | ||
// routing::get, | ||
// }; | ||
// use tokio::sync::Mutex; | ||
|
||
type RouterState = Arc<Mutex<CameraFrameReceiver>>; | ||
// type RouterState = Arc<Mutex<CameraFrameReceiver>>; | ||
|
||
async fn ws_handler( | ||
ws: WebSocketUpgrade, | ||
State(state): State<RouterState>, | ||
) -> impl IntoResponse { | ||
ws.on_upgrade(move |socket| handle_socket(socket, state)) | ||
} | ||
// async fn ws_handler( | ||
// ws: WebSocketUpgrade, | ||
// State(state): State<RouterState>, | ||
// ) -> impl IntoResponse { | ||
// ws.on_upgrade(move |socket| handle_socket(socket, state)) | ||
// } | ||
|
||
async fn handle_socket(mut socket: WebSocket, state: RouterState) { | ||
let camera_rx = state.lock().await; | ||
println!("socket connection established"); | ||
tracing::info!("Socket connection established"); | ||
let now = std::time::Instant::now(); | ||
// async fn handle_socket(socket: WebSocket, state: RouterState) { | ||
// let camera_rx = state.lock().await; | ||
// println!("socket connection established"); | ||
// tracing::info!("Socket connection established"); | ||
// let now = std::time::Instant::now(); | ||
|
||
loop { | ||
tokio::select! { | ||
_ = socket.recv() => { | ||
tracing::info!("Received message from socket"); | ||
break; | ||
} | ||
incoming_frame = camera_rx.recv_async() => { | ||
match incoming_frame { | ||
Ok(data) => { | ||
tracing::info!("Received frame from camera"); | ||
if let Err(e) = socket.send(Message::Binary(data)).await { | ||
tracing::error!("Failed to send frame to socket: {:?}", e); | ||
break; | ||
} | ||
}, | ||
Err(e) => { | ||
tracing::warn!("Connection has been lost! Shutting down camera server: {:?}", e); | ||
break; | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
// let (mut socket_sink, mut socket_stream) = socket.split(); | ||
|
||
let elapsed = now.elapsed(); | ||
println!("Websocket closing after {elapsed:.2?}"); | ||
tracing::info!("Websocket closing after {elapsed:.2?}"); | ||
} | ||
// let mut stream = futures::stream::poll_fn(|cx| { | ||
// if let Poll::Ready(_) = socket_stream.poll_next_unpin(cx) { | ||
// tracing::info!("Received message from socket"); | ||
// return Poll::Ready(None); | ||
// }; | ||
|
||
let router = axum::Router::new() | ||
.route("/", get(ws_handler)) | ||
.with_state(Arc::new(Mutex::new(frame_rx))); | ||
// camera_rx.recv_async().poll_unpin(cx).map(|v| Some(v)) | ||
// }); | ||
|
||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
let port = listener.local_addr().unwrap().port(); | ||
tracing::info!("WebSocket server listening on port {}", port); | ||
tokio::spawn(async move { | ||
axum::serve(listener, router.into_make_service()) | ||
.await | ||
.unwrap(); | ||
}); | ||
// while let Some(incoming_frame) = stream.next().await { | ||
// match incoming_frame { | ||
// Ok(mut frame) => { | ||
// frame | ||
// .data | ||
// .extend_from_slice(&(frame.width * 4).to_le_bytes()); | ||
// frame.data.extend_from_slice(&frame.height.to_le_bytes()); | ||
// frame.data.extend_from_slice(&frame.width.to_le_bytes()); | ||
|
||
port | ||
} | ||
// tracing::info!("Received frame from camera"); | ||
// if let Err(e) = socket_sink.send(Message::Binary(frame.data)).await { | ||
// tracing::error!("Failed to send frame to socket: {:?}", e); | ||
// break; | ||
// } | ||
// } | ||
// Err(e) => { | ||
// tracing::warn!( | ||
// "Connection has been lost! Shutting down camera server: {:?}", | ||
// e | ||
// ); | ||
// break; | ||
// } | ||
// } | ||
// } | ||
|
||
// let elapsed = now.elapsed(); | ||
// println!("Websocket closing after {elapsed:.2?}"); | ||
// tracing::info!("Websocket closing after {elapsed:.2?}"); | ||
// } | ||
|
||
// let router = axum::Router::new() | ||
// .route("/", get(ws_handler)) | ||
// .with_state(Arc::new(Mutex::new(frame_rx))); | ||
|
||
// let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
// let port = listener.local_addr().unwrap().port(); | ||
// tracing::info!("WebSocket server listening on port {}", port); | ||
// tokio::spawn(async move { | ||
// axum::serve(listener, router.into_make_service()) | ||
// .await | ||
// .unwrap(); | ||
// }); | ||
|
||
// port | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1585a75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
cap-web – ./
cap.so
cap-web-git-main-mc-ilroy.vercel.app
www.cap.so
cap-web-mc-ilroy.vercel.app
cap-web-nu.vercel.app