From 8ead7ad2fcde81ee7b3875ff61136f8c4bcbd2de Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sun, 26 Feb 2023 10:30:22 +0700 Subject: [PATCH] Keep sending data until jpeg is made --- src/image/gst.rs | 28 +++++++++++++++++++++---- src/image/mod.rs | 54 +++++++++++++++++++++++++++++++----------------- 2 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/image/gst.rs b/src/image/gst.rs index 491dcdf..930a234 100644 --- a/src/image/gst.rs +++ b/src/image/gst.rs @@ -5,7 +5,10 @@ use gstreamer::{parse_launch, prelude::*, ClockTime, MessageView, Pipeline, Stat use gstreamer_app::AppSrc; use neolink_core::bcmedia::model::VideoType; use tokio::{ - sync::mpsc::{channel, Sender}, + sync::{ + self, + mpsc::{channel, Sender}, + }, task::{self, JoinSet}, }; @@ -19,6 +22,7 @@ pub(super) struct GstSender { sender: Sender, #[allow(dead_code)] // It is used for its automatic drop set: JoinSet>, + finished: sync::oneshot::Receiver>, } impl GstSender { @@ -35,6 +39,16 @@ impl GstSender { .await .map_err(|e| anyhow!("Failed to send eos: {:?}", e)) } + + pub(super) async fn is_finished(&mut self) -> Option> { + match self.finished.try_recv() { + Ok(res) => Some(res), + Err(sync::oneshot::error::TryRecvError::Empty) => None, + Err(sync::oneshot::error::TryRecvError::Closed) => { + Some(Err(anyhow!("Gstreamer finished channel is closed"))) + } + } + } } pub(super) async fn from_input>( @@ -70,14 +84,20 @@ async fn output(pipeline: Pipeline) -> Result { Ok(()) }); + let (tx, finished) = sync::oneshot::channel(); task::spawn_blocking(move || { let res = start_pipeline(pipeline); - if let Err(e) = res { - log::error!("Failed to start pipeline: {:?}", e); + if let Err(e) = &res { + log::error!("Failed to run pipeline: {:?}", e); } + tx.send(res) }); - Ok(GstSender { sender, set }) + Ok(GstSender { + sender, + set, + finished, + }) } fn start_pipeline(pipeline: Pipeline) -> Result<()> { diff --git a/src/image/mod.rs b/src/image/mod.rs index 51abb95..7eb30a5 100644 --- a/src/image/mod.rs +++ b/src/image/mod.rs @@ -10,12 +10,13 @@ /// ``` /// use anyhow::{Context, Result}; +use log::*; use neolink_core::{bc_protocol::*, bcmedia::model::*}; mod cmdline; mod gst; -use super::config::{CameraConfig, Config}; +use super::config::Config; use crate::utils::{connect_and_login, find_camera_by_name}; pub(crate) use cmdline::Opt; @@ -27,20 +28,7 @@ pub(crate) async fn main(opt: Opt, config: Config) -> Result<()> { "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)> { + // Set up camera to recieve the stream let camera = connect_and_login(camera_config) .await .context("Failed to connect to the camera, check credentials and network")?; @@ -49,11 +37,39 @@ async fn fetch_iframe(camera_config: &CameraConfig) -> Result<(VideoType, Vec frame.data, + BcMedia::Pframe(frame) => frame.data, + _ => { + continue; + } + }; + + debug!("Sending frame data to gstreamer"); + if sender.send(buf).await.is_err() { + // Assume that the sender is closed + // because the pipeline is finished + break; + } + } + debug!("Sending EOS"); + let _ = sender.eos().await; // Ignore return because if pipeline is finished this will error + + Ok(()) }