|
| 1 | +mod ros_bridge; |
| 2 | +mod video_pipeline; |
| 3 | +mod webrtc_link; |
| 4 | +mod webrtc_message; |
| 5 | + |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +use anyhow::Result; |
| 9 | +use bytes::Bytes; |
| 10 | +use log::{info, warn}; |
| 11 | +use tokio::sync::Mutex; |
| 12 | + |
| 13 | +use crate::ros_bridge::RosBridge; |
| 14 | +use crate::video_pipeline::VideoPipeline; |
| 15 | +use crate::video_pipeline::VideoPipelineError; |
| 16 | +use crate::webrtc_link::WebRtcLink; |
| 17 | +use crate::webrtc_link::WebRtcLinkError; |
| 18 | +use crate::webrtc_message::WebRtcMessage; |
| 19 | + |
| 20 | +#[tokio::main] |
| 21 | +async fn main() -> Result<()> { |
| 22 | + env_logger::init(); |
| 23 | + |
| 24 | + let robot_id = "robot1"; |
| 25 | + let signaling_url = "ws://192.168.132.19:8765"; |
| 26 | + let webrtc_link = Arc::new(Mutex::new(WebRtcLink::new(robot_id, signaling_url))); |
| 27 | + let pipeline = Arc::new(Mutex::new(VideoPipeline::new())); |
| 28 | + let bridge = Arc::new(Mutex::new(RosBridge::try_new()?)); |
| 29 | + |
| 30 | + info!("Creating system components and callbacks"); |
| 31 | + |
| 32 | + // Browser -> WebRTC -> ROS |
| 33 | + let bridge_clone = Arc::clone(&bridge); |
| 34 | + webrtc_link |
| 35 | + .lock() |
| 36 | + .await |
| 37 | + .on_webrtc_message(Box::new(move |msg: &WebRtcMessage| { |
| 38 | + let bridge_clone = Arc::clone(&bridge_clone); |
| 39 | + let msg_clone = msg.clone(); |
| 40 | + Box::pin(async move { |
| 41 | + match msg_clone { |
| 42 | + WebRtcMessage::MovementCommand(cmd) => { |
| 43 | + bridge_clone |
| 44 | + .lock() |
| 45 | + .await |
| 46 | + .post_movement_command(&cmd) |
| 47 | + .expect("Failed to post movement command!"); |
| 48 | + } |
| 49 | + } |
| 50 | + }) |
| 51 | + })) |
| 52 | + .await; |
| 53 | + |
| 54 | + // TODO: only queue frames in pipeline when WebRTC session is established |
| 55 | + |
| 56 | + // Pipeline -> WebRTC -> Browser |
| 57 | + let webrtc_link_clone = Arc::clone(&webrtc_link); |
| 58 | + pipeline |
| 59 | + .lock() |
| 60 | + .await |
| 61 | + .on_frame_ready(Box::new(move |frame: &Bytes| { |
| 62 | + let webrtc_link_clone = Arc::clone(&webrtc_link_clone); |
| 63 | + let frame_clone = Bytes::copy_from_slice(frame); |
| 64 | + Box::pin(async move { |
| 65 | + let err = webrtc_link_clone |
| 66 | + .lock() |
| 67 | + .await |
| 68 | + .write_frame(frame_clone) |
| 69 | + .await; |
| 70 | + match err { |
| 71 | + Err(WebRtcLinkError::IncorrectStateForOperation) => { |
| 72 | + warn!("Still waiting for WebRTC pipeline to connect.") |
| 73 | + } |
| 74 | + Err(_) => { |
| 75 | + panic!("Failed to write frame!") |
| 76 | + } |
| 77 | + _ => (), |
| 78 | + } |
| 79 | + }) |
| 80 | + })) |
| 81 | + .await; |
| 82 | + |
| 83 | + // Robot frame -> Pipeline |
| 84 | + let pipeline_clone = Arc::clone(&pipeline); |
| 85 | + bridge |
| 86 | + .lock() |
| 87 | + .await |
| 88 | + .on_image_frame_received(Box::new(move |data: &Bytes| { |
| 89 | + let data_clone = Bytes::copy_from_slice(data); |
| 90 | + let pipeline_clone = Arc::clone(&pipeline_clone); |
| 91 | + Box::pin(async move { |
| 92 | + pipeline_clone |
| 93 | + .lock() |
| 94 | + .await |
| 95 | + .queue_frame(&data_clone) |
| 96 | + .await |
| 97 | + .expect("Failed to write camera frame to pipeline!"); |
| 98 | + }) |
| 99 | + })); |
| 100 | + |
| 101 | + info!("Starting all tasks running"); |
| 102 | + |
| 103 | + tokio::spawn(async move { |
| 104 | + let mut guard = webrtc_link.lock().await; |
| 105 | + guard.try_connect().await?; |
| 106 | + guard.try_register().await?; |
| 107 | + Ok::<(), WebRtcLinkError>(()) |
| 108 | + }); |
| 109 | + let pipeline_clone = Arc::clone(&pipeline); |
| 110 | + tokio::spawn(async move { |
| 111 | + pipeline_clone.lock().await.launch().await?; |
| 112 | + info!("Spawned the launch task successfully"); |
| 113 | + Ok::<(), VideoPipelineError>(()) |
| 114 | + }); |
| 115 | + bridge.lock().await.spin(); |
| 116 | + |
| 117 | + let _ = tokio::signal::ctrl_c().await; |
| 118 | + info!("Exit requested, cleaning up..."); |
| 119 | + pipeline.lock().await.stop_pipeline().await?; |
| 120 | + Ok(()) |
| 121 | +} |
0 commit comments