diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 42ca5b3..e7b0d43 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -95,6 +95,7 @@ impl BcConnection { let mut remove_it = false; match subscribers.read().await.get(&msg_num) { Some(occ) => { + trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity()); if occ.send(response).await.is_err() { // Exceedingly unlikely, unless you mishandle the subscription object warn!("Subscriber to ID {} dropped their channel", msg_id); diff --git a/crates/core/src/bc_protocol/connection/bcsub.rs b/crates/core/src/bc_protocol/connection/bcsub.rs index 9e54186..5a391b0 100644 --- a/crates/core/src/bc_protocol/connection/bcsub.rs +++ b/crates/core/src/bc_protocol/connection/bcsub.rs @@ -64,9 +64,10 @@ impl<'a> Stream for BcPayloadStream<'a> { Poll::Pending } } + Poll::Pending => { + return Poll::Pending; + } } - Poll::Ready(None) => Poll::Ready(None), - Poll::Pending => Poll::Pending, } } } diff --git a/crates/core/src/bc_protocol/connection/discovery.rs b/crates/core/src/bc_protocol/connection/discovery.rs index 1aca440..136909d 100644 --- a/crates/core/src/bc_protocol/connection/discovery.rs +++ b/crates/core/src/bc_protocol/connection/discovery.rs @@ -216,17 +216,10 @@ impl Discoverer { // Get what ever completes first while let Some(result) = set.next().await { - match result { - Ok(ret) => { - // info!("Future pop OK"); - return Ok(ret); - } - Err(_e) => { - // info!("Future pop Err: {:?}", e); - } + if let Ok(ret) = result { + return Ok(ret); } } - // info!("set: empty"); Err(Error::DiscoveryTimeout) } diff --git a/crates/core/src/bc_protocol/connection/udpsource.rs b/crates/core/src/bc_protocol/connection/udpsource.rs index ddcb5e9..550eeda 100644 --- a/crates/core/src/bc_protocol/connection/udpsource.rs +++ b/crates/core/src/bc_protocol/connection/udpsource.rs @@ -254,6 +254,28 @@ impl UdpPayloadSource { } } } + + fn maintanence(&mut self, cx: &mut Context<'_>) { + // Check for periodic resends + if self.interval.poll_tick(cx).is_ready() { + trace!("UDPSource.RecievedPacket: Resend"); + for (_, resend) in self.sent.iter() { + self.send_buffer.push_back(BcUdp::Data(resend.clone())); + } + if self.packets_want == 0 { + // When no packets have been recieved the + // ACK is different specifically unknown_b + // is set to -1 and packet_id is also set to + // -1 + // TODO Add this style of ACK + } else { + trace!("UDPSource.RecievedPacket: QueueingAck"); + let ack = BcUdp::Ack(self.build_send_ack()); + self.send_buffer.push_back(ack); + } + self.state = State::Flushing; + } + } } impl Stream for UdpPayloadSource { @@ -270,27 +292,12 @@ impl Stream for UdpPayloadSource { if let Some(payload) = this.recieved.remove(&this.packets_want) { trace!("UDPSource.Data: PreReady: {}", this.packets_want); this.packets_want += 1; + let ack = BcUdp::Ack(this.build_send_ack()); + this.send_buffer.push_back(ack); return Poll::Ready(Some(Ok(payload))); } - // Check for periodic resends - if this.interval.poll_tick(cx).is_ready() { - trace!("UDPSource.RecievedPacker: Resend"); - for (_, resend) in this.sent.iter() { - this.send_buffer.push_back(BcUdp::Data(resend.clone())); - } - if this.packets_want == 0 { - // When no packets have been recieved the - // ACK is different specifically unknown_b - // is set to -1 and packet_id is also set to - // -1 - // TODO Add this style of ACK - } else { - let ack = BcUdp::Ack(this.build_send_ack()); - this.send_buffer.push_back(ack); - } - this.state = State::Flushing; - continue; - } + // Handle resend events + this.maintanence(cx); // Normal behaviors match this.inner.poll_next_unpin(cx) { Poll::Ready(Some(Ok(( @@ -307,6 +314,8 @@ impl Stream for UdpPayloadSource { if packet_id == this.packets_want { trace!("UDPSource.RecievedPacker: NewData: {}", this.packets_want); this.packets_want += 1; + let ack = BcUdp::Ack(this.build_send_ack()); + this.send_buffer.push_back(ack); return Poll::Ready(Some(Ok(payload))); } else { trace!("UDPSource.RecievedPacker: OtherData"); diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index e011652..596ef10 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -31,6 +31,7 @@ pub enum StreamKind { /// /// When this object is dropped the streaming is stopped pub struct StreamData { + #[allow(dead_code)] handle: Option>>, rx: Receiver>, abort_handle: Arc, @@ -50,12 +51,6 @@ impl StreamData { impl Drop for StreamData { fn drop(&mut self) { self.abort_handle.store(true, Ordering::Relaxed); - if let Some(h) = self.handle.take() { - let backoff = crossbeam::utils::Backoff::new(); - while !h.is_finished() { - backoff.snooze(); - } - } } } diff --git a/crates/core/src/bcudp/model.rs b/crates/core/src/bcudp/model.rs index c84aea2..2b1c84f 100644 --- a/crates/core/src/bcudp/model.rs +++ b/crates/core/src/bcudp/model.rs @@ -107,7 +107,7 @@ impl std::fmt::Debug for UdpData { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_map() .entry(&"connection_id", &self.connection_id) - .entry(&"packet_id", &self.connection_id) + .entry(&"packet_id", &self.packet_id) .entry(&"payload_len", &self.payload.len()) .finish() } diff --git a/src/cmdline.rs b/src/cmdline.rs index 46d9bc5..44045f2 100644 --- a/src/cmdline.rs +++ b/src/cmdline.rs @@ -23,4 +23,5 @@ pub enum Command { Pir(super::pir::Opt), Talk(super::talk::Opt), Mqtt(super::mqtt::Opt), + Image(super::image::Opt), } diff --git a/src/image/.DS_Store b/src/image/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/src/image/.DS_Store differ diff --git a/src/image/cmdline.rs b/src/image/cmdline.rs new file mode 100644 index 0000000..52dd104 --- /dev/null +++ b/src/image/cmdline.rs @@ -0,0 +1,12 @@ +use std::path::PathBuf; +use structopt::StructOpt; + +/// The image command will dump a still image from the camera +#[derive(StructOpt, Debug)] +pub struct Opt { + /// The name of the camera to get the image from. Must be a name in the config + pub camera: String, + /// The path of the output. + #[structopt(short, long, parse(from_os_str))] + pub file_path: PathBuf, +} diff --git a/src/image/gst.rs b/src/image/gst.rs new file mode 100644 index 0000000..491dcdf --- /dev/null +++ b/src/image/gst.rs @@ -0,0 +1,171 @@ +use std::path::Path; + +use anyhow::{anyhow, Context, Result}; +use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, State}; +use gstreamer_app::AppSrc; +use neolink_core::bcmedia::model::VideoType; +use tokio::{ + sync::mpsc::{channel, Sender}, + task::{self, JoinSet}, +}; + +#[derive(Debug)] +enum GstControl { + Data(Vec), + Eos, +} + +pub(super) struct GstSender { + sender: Sender, + #[allow(dead_code)] // It is used for its automatic drop + set: JoinSet>, +} + +impl GstSender { + pub(super) async fn send(&self, buf: Vec) -> Result<()> { + self.sender + .send(GstControl::Data(buf)) + .await + .map_err(|e| anyhow!("Failed to send buffer: {:?}", e)) + } + + pub(super) async fn eos(&self) -> Result<()> { + self.sender + .send(GstControl::Eos) + .await + .map_err(|e| anyhow!("Failed to send eos: {:?}", e)) + } +} + +pub(super) async fn from_input>( + format: VideoType, + out_file: T, +) -> Result { + let pipeline = create_pipeline(format, out_file.as_ref())?; + output(pipeline).await +} + +async fn output(pipeline: Pipeline) -> Result { + let source = get_source(&pipeline)?; + let (sender, mut reciever) = channel::(100); + let mut set = JoinSet::new(); + set.spawn(async move { + while let Some(control) = reciever.recv().await { + match control { + GstControl::Data(buf) => { + let mut gst_buf = gstreamer::Buffer::with_size(buf.len()).unwrap(); + { + let gst_buf_mut = gst_buf.get_mut().unwrap(); + let mut gst_buf_data = gst_buf_mut.map_writable().unwrap(); + gst_buf_data.copy_from_slice(&buf); + } + source.push_buffer(gst_buf)?; + } + GstControl::Eos => { + source.end_of_stream()?; + break; + } + } + } + Ok(()) + }); + + task::spawn_blocking(move || { + let res = start_pipeline(pipeline); + if let Err(e) = res { + log::error!("Failed to start pipeline: {:?}", e); + } + }); + + Ok(GstSender { sender, set }) +} + +fn start_pipeline(pipeline: Pipeline) -> Result<()> { + pipeline.set_state(State::Playing)?; + + let bus = pipeline + .bus() + .expect("Pipeline without bus. Shouldn't happen!"); + + for msg in bus.iter_timed(ClockTime::NONE) { + match msg.view() { + MessageView::Eos(..) => break, + MessageView::Error(err) => { + pipeline + .set_state(State::Null) + .context("Error in gstreamer when setting state to Null")?; + log::warn!( + "Error from gstreamer when setting the play state {:?} setting to Null instead", + err + ); + } + _ => (), + } + } + + pipeline + .set_state(State::Null) + .context("Error in gstreamer when setting state to Null")?; + + Ok(()) +} + +fn get_source(pipeline: &Pipeline) -> Result { + let source = pipeline + .by_name("thesource") + .expect("There shoud be a `thesource`"); + source + .dynamic_cast::() + .map_err(|_| anyhow!("Cannot find appsource in gstreamer, check your gstreamer plugins")) +} + +fn create_pipeline(format: VideoType, file_path: &Path) -> Result { + gstreamer::init() + .context("Unable to start gstreamer ensure it and all plugins are installed")?; + let file_path = file_path.with_extension("jpeg"); + + let launch_str = match format { + VideoType::H264 => { + format!( + "appsrc name=thesource \ + ! h264parse \ + ! decodebin \ + ! jpegenc snapshot=TRUE + ! filesink location={}", + file_path.display() + ) + } + VideoType::H265 => { + format!( + "appsrc name=thesource \ + ! h265parse \ + ! decodebin \ + ! jpegenc snapshot=TRUE + ! filesink location={}", + file_path.display() + ) + } + }; + + log::info!("{}", launch_str); + + // Parse the pipeline we want to probe from a static in-line string. + // Here we give our audiotestsrc a name, so we can retrieve that element + // from the resulting pipeline. + let pipeline = parse_launch(&launch_str) + .context("Unable to load gstreamer pipeline ensure all gstramer plugins are installed")?; + let pipeline = pipeline.dynamic_cast::().map_err(|_| { + anyhow!("Unable to create gstreamer pipeline ensure all gstramer plugins are installed") + })?; + + // let appssource = get_source(&pipeline)?; + + // Tell the appsink what format we produce. + // let caps = match format { + // VideoType::H264 => Caps::new_simple("video/x-h264", &[("parsed", &false)]), + // VideoType::H265 => Caps::new_simple("video/x-h265", &[("parsed", &false)]), + // }; + // appssource.set_caps(Some(&caps)); + + Ok(pipeline) +} diff --git a/src/image/mod.rs b/src/image/mod.rs new file mode 100644 index 0000000..57445dd --- /dev/null +++ b/src/image/mod.rs @@ -0,0 +1,62 @@ +/// +/// # Neolink Image +/// +/// This module can be used to send dump a still image from the camera +/// +/// +/// # Usage +/// ```bash +/// neolink image --config=config.toml --finename=filepath CameraName +/// ``` +/// +use anyhow::{Context, Result}; +use neolink_core::{bc_protocol::*, bcmedia::model::*}; + +mod cmdline; +mod gst; + +use super::config::{CameraConfig, Config}; +use crate::utils::{connect_and_login, find_camera_by_name}; +pub(crate) use cmdline::Opt; + +/// Entry point for the image subcommand +/// +/// Opt is the command line options +pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { + let camera_config = find_camera_by_name(&config, &opt.camera).context( + "failed to load config file or find the camera. Check the path and name of the camera.", + )?; + + let (vid_type, buf) = fetch_iframe(camera_config).await?; + let sender = gst::from_input(vid_type, &opt.file_path).await?; + sender.send(buf).await?; + // So I tried 1 iFrame and it wouldn't finish the convert (just seems to hang waiting for data) + // I tried sending the SAME iFrame twice and SOMETIMES it converts (othertimes it just hangs) + // Then tried two different iFrames and it always converted. Therefore we do that + let (_, buf) = fetch_iframe(camera_config).await?; + sender.send(buf).await?; + sender.eos().await?; + + Ok(()) +} + +async fn fetch_iframe(camera_config: &CameraConfig) -> Result<(VideoType, Vec)> { + let camera = connect_and_login(camera_config) + .await + .context("Failed to connect to the camera, check credentials and network")?; + let mut stream_data = camera + .start_video(StreamKind::Main, 0) + .await + .context("Failed to start video")?; + + loop { + log::info!("Awaiting data"); + let data = stream_data.get_data().await??; + log::info!("Got data"); + if let BcMedia::Iframe(iframedata) = data { + return Ok((iframedata.video_type, iframedata.data)); + } else { + log::info!("Got non iframe"); + } + } +} diff --git a/src/main.rs b/src/main.rs index 59e350f..7c4e203 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use validator::Validate; mod cmdline; mod config; +mod image; mod mqtt; mod pir; mod reboot; @@ -76,6 +77,9 @@ async fn main() -> Result<()> { Some(Command::Mqtt(opts)) => { mqtt::main(opts, config).await?; } + Some(Command::Image(opts)) => { + image::main(opts, config).await?; + } } Ok(()) diff --git a/src/rtsp/gst.rs b/src/rtsp/gst.rs index 0b6612e..5092b12 100644 --- a/src/rtsp/gst.rs +++ b/src/rtsp/gst.rs @@ -218,8 +218,6 @@ impl GstOutputs { // - In the case of PausedSources::None we just pass through without encoding // - In the case of all other pause sources we need to recencode in order // to get smooth streams when the stream is paused/resumed - // info!("self.video_format: {:?}", self.video_format); - // info!("self.audio_format: {:?}", self.audio_format); let launch_vid_select = match self.when_paused { PausedSources::None => match self.video_format { Some(StreamFormat::H264) => "! rtph264pay name=pay0",