Added still image command

This commit is contained in:
QuantumEntangledAndy authored and Andrew King committed 2023-02-27 09:14:07 +07:00
1 parent 6d0d290644
commit bd9da2620b
13 files changed
+286 -39

No files matched your search

@@ -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);
@@ -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,
}
}
}
@@ -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)
}
@@ -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");
+1 -6
View File
@@ -31,6 +31,7 @@ pub enum StreamKind {
///
/// When this object is dropped the streaming is stopped
pub struct StreamData {
#[allow(dead_code)]
handle: Option<JoinHandle<Result<()>>>,
rx: Receiver<Result<BcMedia>>,
abort_handle: Arc<AtomicBool>,
@@ -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();
}
}
}
}
+1 -1
View File
@@ -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()
}
+1
View File
@@ -23,4 +23,5 @@ pub enum Command {
Pir(super::pir::Opt),
Talk(super::talk::Opt),
Mqtt(super::mqtt::Opt),
Image(super::image::Opt),
}
BIN
View File
Binary file not shown.
+12
View File
@@ -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,
}
+171
View File
@@ -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<u8>),
Eos,
}
pub(super) struct GstSender {
sender: Sender<GstControl>,
#[allow(dead_code)] // It is used for its automatic drop
set: JoinSet<Result<()>>,
}
impl GstSender {
pub(super) async fn send(&self, buf: Vec<u8>) -> 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<T: AsRef<Path>>(
format: VideoType,
out_file: T,
) -> Result<GstSender> {
let pipeline = create_pipeline(format, out_file.as_ref())?;
output(pipeline).await
}
async fn output(pipeline: Pipeline) -> Result<GstSender> {
let source = get_source(&pipeline)?;
let (sender, mut reciever) = channel::<GstControl>(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<AppSrc> {
let source = pipeline
.by_name("thesource")
.expect("There shoud be a `thesource`");
source
.dynamic_cast::<AppSrc>()
.map_err(|_| anyhow!("Cannot find appsource in gstreamer, check your gstreamer plugins"))
}
fn create_pipeline(format: VideoType, file_path: &Path) -> Result<Pipeline> {
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::<Pipeline>().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)
}
+62
View File
@@ -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<u8>)> {
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");
}
}
}
+4
View File
@@ -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(())
-2
View File
@@ -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",