Basic setup of subcommands
This commit is contained in:
14 files changed
+196
-78
No files matched your search
Generated
+51
-1
@@ -1,5 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aes"
|
||||
version = "0.6.0"
|
||||
@@ -867,7 +869,19 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204"
|
||||
|
||||
[[package]]
|
||||
name = "neolink"
|
||||
version = "0.3.0"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"err-derive",
|
||||
"log",
|
||||
"neolink_core",
|
||||
"neolink_rtsp",
|
||||
"structopt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neolink_core"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"cfb-mode",
|
||||
@@ -900,6 +914,42 @@ dependencies = [
|
||||
"yaserde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "neolink_rtsp"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"cfb-mode",
|
||||
"cookie-factory",
|
||||
"crossbeam",
|
||||
"env_logger",
|
||||
"err-derive",
|
||||
"gio",
|
||||
"glib",
|
||||
"gstreamer",
|
||||
"gstreamer-app",
|
||||
"gstreamer-rtsp",
|
||||
"gstreamer-rtsp-server",
|
||||
"indoc",
|
||||
"itertools",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"md5",
|
||||
"neolink_core",
|
||||
"nom",
|
||||
"regex",
|
||||
"serde",
|
||||
"socket2",
|
||||
"structopt",
|
||||
"time",
|
||||
"toml",
|
||||
"validator",
|
||||
"validator_derive",
|
||||
"xml-rs",
|
||||
"yaserde",
|
||||
"yaserde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "6.1.2"
|
||||
|
||||
@@ -9,3 +9,11 @@ edition = "2018"
|
||||
members = [
|
||||
"crates/*",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
neolink_core = { path = "crates/core", version = "0.4.0" }
|
||||
neolink_rtsp = { path = "crates/rtsp", version = "0.4.0" }
|
||||
env_logger = "*"
|
||||
err-derive = "0.2"
|
||||
log = { version = "0.4" }
|
||||
structopt = "0.3"
|
||||
@@ -2,7 +2,6 @@ use self::connection::BcConnection;
|
||||
use self::media_packet::{MediaDataKind, MediaDataSubscriber};
|
||||
use crate::bc;
|
||||
use crate::bc::{model::*, xml::*};
|
||||
use crate::gst::GstOutputs;
|
||||
use adpcm::adpcm_to_pcm;
|
||||
use err_derive::Error;
|
||||
use log::*;
|
||||
@@ -19,14 +18,6 @@ mod connection;
|
||||
mod media_packet;
|
||||
mod time;
|
||||
|
||||
pub struct BcCamera {
|
||||
address: SocketAddr,
|
||||
channel_id: u8,
|
||||
connection: Option<BcConnection>,
|
||||
logged_in: bool,
|
||||
message_num: AtomicU16,
|
||||
}
|
||||
|
||||
use crate::Never;
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
@@ -82,6 +73,29 @@ impl<'a> From<std::sync::mpsc::RecvTimeoutError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BcCamera {
|
||||
address: SocketAddr,
|
||||
channel_id: u8,
|
||||
connection: Option<BcConnection>,
|
||||
logged_in: bool,
|
||||
message_num: AtomicU16,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum StreamFormat {
|
||||
H264,
|
||||
H265,
|
||||
AAC,
|
||||
ADPCM,
|
||||
}
|
||||
|
||||
pub type StreamOutputError = Result<()>;
|
||||
pub trait StreamOutput {
|
||||
fn write_audio(&mut self, data: &[u8]) -> StreamOutputError;
|
||||
fn write_video(&mut self, data: &[u8]) -> StreamOutputError;
|
||||
fn set_format(&mut self, format: Option<StreamFormat>);
|
||||
}
|
||||
|
||||
impl Drop for BcCamera {
|
||||
fn drop(&mut self) {
|
||||
self.disconnect();
|
||||
@@ -346,7 +360,10 @@ impl BcCamera {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn start_video(&self, data_outs: &mut GstOutputs, stream_name: &str) -> Result<Never> {
|
||||
pub fn start_video<Outputs>(&self, data_outs: &mut Outputs, stream_name: &str) -> Result<Never>
|
||||
where
|
||||
Outputs: StreamOutput,
|
||||
{
|
||||
let connection = self
|
||||
.connection
|
||||
.as_ref()
|
||||
@@ -391,19 +408,19 @@ impl BcCamera {
|
||||
MediaDataKind::VideoDataIframe | MediaDataKind::VideoDataPframe => {
|
||||
let media_format = binary_data.media_format();
|
||||
data_outs.set_format(media_format);
|
||||
data_outs.vidsrc.write_all(binary_data.body())?;
|
||||
data_outs.write_video(binary_data.body())?;
|
||||
}
|
||||
MediaDataKind::AudioDataAac => {
|
||||
let media_format = binary_data.media_format();
|
||||
data_outs.set_format(media_format);
|
||||
data_outs.audsrc.write_all(binary_data.body())?;
|
||||
data_outs.write_audio(binary_data.body())?;
|
||||
}
|
||||
MediaDataKind::AudioDataAdpcm => {
|
||||
let media_format = binary_data.media_format();
|
||||
data_outs.set_format(media_format);
|
||||
let adpcm = binary_data.body();
|
||||
let pcm = adpcm_to_pcm(adpcm)?;
|
||||
data_outs.audsrc.write_all(&pcm)?;
|
||||
data_outs.write_audio(&pcm)?;
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use super::{Error, Result, RX_TIMEOUT};
|
||||
use super::{Error, Result, StreamFormat, RX_TIMEOUT};
|
||||
use crate::bc::model::*;
|
||||
use crate::bc_protocol::connection::BcSubscription;
|
||||
use crate::gst::StreamFormat;
|
||||
use log::trace;
|
||||
use log::*;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_variables)]
|
||||
pub mod bc;
|
||||
pub mod bc_protocol;
|
||||
pub mod gst;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Never {}
|
||||
|
||||
@@ -6,6 +6,7 @@ authors = ["George Hilliard <thirtythreeforty@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
neolink_core = { path = "../core", version = "0.4.0" }
|
||||
aes = "0.6"
|
||||
cfb-mode = "0.6"
|
||||
cookie-factory = "0.3"
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// A standards-compliant bridge to Reolink IP cameras
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "neolink")]
|
||||
pub struct Opt {
|
||||
/// main configuration file
|
||||
#[structopt(short, long, parse(from_os_str))]
|
||||
pub config: PathBuf,
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use err_derive::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum Error {
|
||||
#[error(display = "Configuration parsing error")]
|
||||
ConfigError(#[error(source)] toml::de::Error),
|
||||
#[error(display = "Communication error")]
|
||||
ProtocolError(#[error(source)] neolink_core::Error),
|
||||
#[error(display = "I/O error")]
|
||||
IoError(#[error(source)] std::io::Error),
|
||||
#[error(display = "Validation error")]
|
||||
ValidationError(#[error(source)] validator::ValidationErrors),
|
||||
#[error(display = "ADPCM Decoding Error")]
|
||||
AdpcmDecodingError(&'static str),
|
||||
}
|
||||
+24
-20
@@ -14,6 +14,7 @@ use gstreamer_rtsp_server::{
|
||||
RTSP_TOKEN_MEDIA_FACTORY_ROLE,
|
||||
};
|
||||
use log::*;
|
||||
use neolink_core::bc_protocol::{Error, StreamFormat, StreamOutput, StreamOutputError};
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
@@ -25,14 +26,6 @@ pub struct RtspServer {
|
||||
server: GstRTSPServer,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
|
||||
pub enum StreamFormat {
|
||||
H264,
|
||||
H265,
|
||||
AAC,
|
||||
ADPCM,
|
||||
}
|
||||
|
||||
pub struct GstOutputs {
|
||||
pub audsrc: MaybeAppSrc,
|
||||
pub vidsrc: MaybeAppSrc,
|
||||
@@ -41,20 +34,17 @@ pub struct GstOutputs {
|
||||
factory: RTSPMediaFactory,
|
||||
}
|
||||
|
||||
impl GstOutputs {
|
||||
pub fn from_appsrcs(vidsrc: MaybeAppSrc, audsrc: MaybeAppSrc) -> GstOutputs {
|
||||
let result = GstOutputs {
|
||||
vidsrc,
|
||||
audsrc,
|
||||
video_format: None,
|
||||
audio_format: None,
|
||||
factory: RTSPMediaFactory::new(),
|
||||
};
|
||||
result.apply_format();
|
||||
result
|
||||
impl StreamOutput for GstOutputs {
|
||||
fn write_audio(&mut self, data: &[u8]) -> StreamOutputError {
|
||||
self.audsrc.write_all(data)?;
|
||||
Ok(())
|
||||
}
|
||||
fn write_video(&mut self, data: &[u8]) -> StreamOutputError {
|
||||
self.vidsrc.write_all(data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_format(&mut self, format: Option<StreamFormat>) {
|
||||
fn set_format(&mut self, format: Option<StreamFormat>) {
|
||||
match format {
|
||||
Some(StreamFormat::H264) | Some(StreamFormat::H265) => {
|
||||
if format != self.video_format {
|
||||
@@ -71,6 +61,20 @@ impl GstOutputs {
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GstOutputs {
|
||||
pub fn from_appsrcs(vidsrc: MaybeAppSrc, audsrc: MaybeAppSrc) -> GstOutputs {
|
||||
let result = GstOutputs {
|
||||
vidsrc,
|
||||
audsrc,
|
||||
video_format: None,
|
||||
audio_format: None,
|
||||
factory: RTSPMediaFactory::new(),
|
||||
};
|
||||
result.apply_format();
|
||||
result
|
||||
}
|
||||
|
||||
fn apply_format(&self) {
|
||||
let launch_vid = match self.video_format {
|
||||
|
||||
@@ -1,48 +1,26 @@
|
||||
use env_logger::Env;
|
||||
use err_derive::Error;
|
||||
use gio::TlsAuthenticationMode;
|
||||
use log::*;
|
||||
use neolink::bc_protocol::BcCamera;
|
||||
use neolink::gst::{GstOutputs, RtspServer};
|
||||
use neolink::Never;
|
||||
use neolink_core::bc_protocol::BcCamera;
|
||||
use neolink_core::Never;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use structopt::StructOpt;
|
||||
use validator::Validate;
|
||||
|
||||
mod cmdline;
|
||||
mod config;
|
||||
pub mod cmdline;
|
||||
pub mod config;
|
||||
pub mod errors;
|
||||
mod gst;
|
||||
|
||||
use cmdline::Opt;
|
||||
use config::{CameraConfig, Config, UserConfig};
|
||||
use errors::Error;
|
||||
use gst::{GstOutputs, RtspServer};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum Error {
|
||||
#[error(display = "Configuration parsing error")]
|
||||
ConfigError(#[error(source)] toml::de::Error),
|
||||
#[error(display = "Communication error")]
|
||||
ProtocolError(#[error(source)] neolink::Error),
|
||||
#[error(display = "I/O error")]
|
||||
IoError(#[error(source)] std::io::Error),
|
||||
#[error(display = "Validation error")]
|
||||
ValidationError(#[error(source)] validator::ValidationErrors),
|
||||
#[error(display = "ADPCM Decoding Error")]
|
||||
AdpcmDecodingError(&'static str),
|
||||
}
|
||||
pub struct Rtsp;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
info!(
|
||||
"Neolink {} {}",
|
||||
env!("NEOLINK_VERSION"),
|
||||
env!("NEOLINK_PROFILE")
|
||||
);
|
||||
|
||||
let opt = Opt::from_args();
|
||||
pub fn main(opt: Opt) -> Result<(), Error> {
|
||||
let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?;
|
||||
|
||||
config.validate()?;
|
||||
@@ -121,7 +99,7 @@ fn camera_loop(
|
||||
current_backoff = min_backoff;
|
||||
}
|
||||
match cam_err.err {
|
||||
neolink::Error::AuthFailed => {
|
||||
neolink_core::Error::AuthFailed => {
|
||||
error!(
|
||||
"Authentication failed to camera {}, not retrying",
|
||||
camera_config.name
|
||||
@@ -143,7 +121,7 @@ fn camera_loop(
|
||||
|
||||
struct CameraErr {
|
||||
connected: bool,
|
||||
err: neolink::Error,
|
||||
err: neolink_core::Error,
|
||||
}
|
||||
|
||||
fn set_up_tls(config: &Config, rtsp: &RtspServer) {
|
||||
@@ -231,7 +209,7 @@ fn camera_main(
|
||||
fn do_camera_management(
|
||||
camera: &mut BcCamera,
|
||||
camera_config: &CameraConfig,
|
||||
) -> Result<(), neolink::Error> {
|
||||
) -> Result<(), neolink_core::Error> {
|
||||
let cam_time = camera.get_time()?;
|
||||
if let Some(time) = cam_time {
|
||||
info!(
|
||||
@@ -261,7 +239,7 @@ fn do_camera_management(
|
||||
}
|
||||
}
|
||||
|
||||
use neolink::bc::xml::VersionInfo;
|
||||
use neolink_core::bc::xml::VersionInfo;
|
||||
if let Ok(VersionInfo {
|
||||
firmwareVersion: firmware_version,
|
||||
..
|
||||
@@ -0,0 +1,14 @@
|
||||
use structopt::StructOpt;
|
||||
|
||||
/// A standards-compliant bridge to Reolink IP cameras
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "neolink")]
|
||||
pub struct Opt {
|
||||
#[structopt(subcommand)]
|
||||
pub cmd: Command,
|
||||
}
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
pub enum Command {
|
||||
Rtsp(neolink_rtsp::cmdline::Opt),
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
use err_derive::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
pub enum Error {
|
||||
#[error(display = "RTSP Error")]
|
||||
RtspError(#[error(source)] neolink_rtsp::errors::Error),
|
||||
}
|
||||
Whitespace-only changes.
+29
@@ -0,0 +1,29 @@
|
||||
use env_logger::Env;
|
||||
use log::*;
|
||||
use structopt::StructOpt;
|
||||
|
||||
mod cmdline;
|
||||
mod errors;
|
||||
|
||||
use cmdline::{Command, Opt};
|
||||
use errors::Error;
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
||||
|
||||
info!(
|
||||
"Neolink {} {}",
|
||||
env!("NEOLINK_VERSION"),
|
||||
env!("NEOLINK_PROFILE")
|
||||
);
|
||||
|
||||
let opt = Opt::from_args();
|
||||
|
||||
match opt.cmd {
|
||||
Command::Rtsp(opts) => {
|
||||
neolink_rtsp::main(opts)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user