Merge branch 'gst'

This commit is contained in:
George Hilliard committed 2020-04-29 12:07:42 -05:00
commit 33e4c6ac6e
13 files changed
+1387 -63

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
+32 -6
View File
@@ -3,15 +3,41 @@ name: CI
on: [push]
jobs:
build:
name: Neolink
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-18.04, windows-2019]
ubuntu:
name: neolink-ubuntu
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- run: sudo apt update
- run: sudo apt install -y libgstrtspserver-1.0-dev libgstreamer1.0-dev libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-good1.0-dev
- uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
- uses: actions/upload-artifact@v2
with:
name: release-ubuntu1804
path: target/release/neolink
win:
name: neolink-win2019
runs-on: windows-2019
steps:
- uses: actions/checkout@v2
- name: Install Gstreamer
run: |
choco install -y --no-progress gstreamer
curl -fsSL "https://gstreamer.freedesktop.org/data/pkg/windows/1.16.2/gstreamer-1.0-devel-msvc-x86_64-1.16.2.msi" -o gstreamer-1.0-devel.msi
echo "Installing gstreamer-1.0-devel"
msiexec /i gstreamer-1.0-devel.msi /qn /norestart
echo "Updating Environment"
echo "::add-path::C:\gstreamer\1.0\x86_64\bin"
echo "::set-env name=GSTREAMER_1_0_ROOT_X86_64::C:\gstreamer\1.0\x86_64\"
- uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
- uses: actions/upload-artifact@v2
with:
name: release-win64
path: target/release/neolink.exe
-4
View File
@@ -7,10 +7,6 @@
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
Generated
+1107
View File
File diff suppressed because it is too large. Load diff
+15 -9
View File
@@ -7,17 +7,23 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cookie-factory = "0.3.1"
cookie-factory = "0.3"
crossbeam-utils = "0.7"
env_logger = "*"
err-derive = "0.2.4"
log = { version = "0.4.8", features = ["release_max_level_warn"] }
indoc = "0.3.5"
md5 = "0.7.0"
nom = "5.1.1"
err-derive = "0.2"
glib = "*"
gstreamer = "0.15"
gstreamer-app = "0.15"
gstreamer-player = "0.15"
gstreamer-rtsp = "0.15"
gstreamer-rtsp-server = "0.15"
log = { version = "0.4", features = ["release_max_level_warn"] }
indoc = "0.3"
md5 = "0.7"
nom = "5.1"
serde = { version = "1.0", features = ["derive"] }
structopt = "0.3"
toml = "0.5"
yaserde = { git = "https://github.com/media-io/yaserde.git" }
yaserde_derive = { git = "https://github.com/media-io/yaserde.git" }
xml-rs = "0.8.2"
yaserde = { git = "https://github.com/media-io/yaserde.git", rev="158bf492f" }
yaserde_derive = { git = "https://github.com/media-io/yaserde.git", rev="158bf492f" }
xml-rs = "0.8"
+26 -1
View File
@@ -1,5 +1,7 @@
# Neolink
![CI](https://github.com/thirtythreeforty/neolink/workflows/CI/badge.svg)
Neolink is a small program that acts as a proxy between Reolink IP cameras and normal RTSP clients.
Certain cameras, such as the Reolink B800, do not implement ONVIF or RTSP, but instead use a proprietary protocol only compatible with their apps and NVRs (any camera that uses "port 9000" will likely be using this protocol).
@@ -11,7 +13,30 @@ The Neolink project is not affiliated with Reolink in any way; everything it doe
## Installation
In the future Neolink will be much easier to install.
Currently you need to install Gstreamer, including gst-rtsp-server, then build this project.
Currently you need to install Gstreamer.
On Windows, add the following to your path:
```
%GSTREAMER_1_0_ROOT_X86_64%\bin
```
Now you can download and run an artifact [from continuous integration][ci-download].
[ci-download]: https://github.com/thirtythreeforty/neolink/actions?query=branch%3Amaster
Launch Neolink from a shell, passing a configuration file:
```
neolink --config my_config.toml
```
## Configuration
Copy and modify the `sample_config.toml` to specify the address, username, and password for each camera (if there is no password, you can omit that line).
Each `[[cameras]]` block creates a new camera; the `name` determines the RTSP path you should connect your client to.
By default Neolink serves on all IP addresses on port 8554.
You can modify this by changing the `bind` parameter.
## Development
+14
View File
@@ -0,0 +1,14 @@
#[cfg(windows)]
fn main() {
use std::env;
let gstreamer_dir = env::var_os("GSTREAMER_1_0_ROOT_X86_64")
.and_then(|x| x.into_string().ok())
.unwrap_or_else(|| r#"C:\gstreamer\1.0\x86_64\"#.to_string());
println!(r"cargo:rustc-link-search=native={}\lib", gstreamer_dir);
}
#[cfg(not(windows))]
fn main() {
}
+2 -2
View File
@@ -1,13 +1,13 @@
bind = "0.0.0.0"
[[cameras]]
name = "driveway"
username = "admin"
password = "12345678"
address = "192.168.1.187:9000"
serve = { port = 9900 }
[[cameras]]
name = "storage shed"
username = "admin"
password = "987654321"
address = "192.168.1.245:9000"
serve = { port = 9901 }
+2 -1
View File
@@ -134,7 +134,8 @@ fn bc_modern_msg<'a, 'b, 'c>(context: &mut BcContext, header: &'a BcHeader, buf:
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
// If this is the first message containing binary, the Extension message puts the message
// ID into binary mode, then the first binary is sent after the XML.
// ID into binary mode, then the first binary is sent after the XML. All remaining
// messages for that ID are pure binary.
match parsed {
AllTopXmls::BcXml(x) => { xml = Some(x); }
AllTopXmls::Extension(Extension { binary_data: _ }) => {
+15 -1
View File
@@ -1,5 +1,19 @@
pub mod de;
//! The Baichuan message format is a 20 byte header, the contents of which vary between legacy and
//! modern messages:
//!
//!
//!
//! This header is followed by the message body. In legacy messages, the bodies are
//! message-specific binary formats. Currently, we only attempt to interpret the legacy login
//! message, as it is all that is needed to upgrade to the modern XML-based messages. Modern
//! messages are either "encrypted" XML (the encryption is a simple XOR routine) or binary data.
//! All message IDs start out as XML, but can be statefully switched to binary with a special XML
//! "Extension" message.
pub mod model;
pub mod de;
pub mod ser;
pub mod xml;
mod xml_crypto;
+5 -28
View File
@@ -1,11 +1,12 @@
use serde::Deserialize;
use std::fmt;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
#[derive(Debug, Deserialize)]
pub struct Config {
pub cameras: Vec<CameraConfig>,
#[serde(rename="bind", default="default_bind_addr")]
pub bind_addr: String,
}
#[derive(Debug, Deserialize)]
@@ -15,34 +16,10 @@ pub struct CameraConfig {
#[serde(rename="address")]
pub camera_addr: SocketAddr,
#[serde(rename="serve")]
pub bind_addr: BindAddr,
pub stream: Option<String>,
pub username: String,
pub password: Option<String>,
}
#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct BindAddr {
host: Option<String>,
port: u16,
}
impl fmt::Display for BindAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref host) = self.host {
write!(f, "{}:{}", host, self.port)
} else {
write!(f, "port {}", self.port)
}
}
}
impl ToSocketAddrs for BindAddr {
type Iter = <(&'static str, u16) as ToSocketAddrs>::Iter;
fn to_socket_addrs(&self) -> std::io::Result<Self::Iter> {
let host = self.host.as_deref().unwrap_or("0.0.0.0");
(host, self.port).to_socket_addrs()
}
fn default_bind_addr() -> String {
"0.0.0.0".to_string()
}
+153
View File
@@ -0,0 +1,153 @@
//! This module provides an "RtspServer" abstraction that allows consumers of its API to feed it
//! data using an ordinary std::io::Write interface.
pub use self::maybe_app_src::MaybeAppSrc;
use gstreamer::Bin;
use gstreamer::prelude::Cast;
use gstreamer_app::AppSrc;
//use gstreamer_rtsp::RTSPLowerTrans;
use gstreamer_rtsp_server::{RTSPServer as GstRTSPServer, RTSPAuth, RTSPMediaFactory};
use gstreamer_rtsp_server::prelude::*;
use log::debug;
use std::io;
use std::io::Write;
type Result<T> = std::result::Result<T, ()>;
pub struct RtspServer {
server: GstRTSPServer,
}
impl RtspServer {
pub fn new() -> RtspServer {
gstreamer::init().expect("Gstreamer should not explode");
RtspServer {
server: GstRTSPServer::new(),
}
}
pub fn add_stream(&self, name: &str) -> Result<MaybeAppSrc> {
let mounts = self.server.get_mount_points().expect("The server should have mountpoints");
let factory = RTSPMediaFactory::new();
//factory.set_protocols(RTSPLowerTrans::TCP);
factory.set_launch(concat!("( ",
"appsrc name=writesrc is-live=true block=true emit-signals=false max-bytes=0",
" ! h265parse",
" ! rtph265pay name=pay0",
" )"));
factory.set_shared(true);
// TODO maybe set video format via
// https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/blob/master/examples/src/bin/appsrc.rs#L66
// Create a MaybeAppSrc: Write which we will give the caller. When the backing AppSrc is
// created by the factory, fish it out and give it to the waiting MaybeAppSrc via the
// channel it provided. This callback may be called more than once by Gstreamer if it is
// unhappy with the pipeline, so keep updating the MaybeAppSrc.
let (maybe_app_src, tx) = MaybeAppSrc::new_with_tx();
factory.connect_media_configure(move |_factory, media| {
debug!("RTSP: media was configured");
let bin = media.get_element()
.expect("Media should have an element")
.dynamic_cast::<Bin>()
.expect("Media source's element should be a bin");
let app_src = bin.get_by_name_recurse_up("writesrc")
.expect("write_src must be present in created bin")
.dynamic_cast::<AppSrc>()
.expect("Source element is expected to be an appsrc!");
let _ = tx.send(app_src); // Receiver may be dropped, don't panic if so
});
mounts.add_factory(&format!("/{}", name), &factory);
Ok(maybe_app_src)
}
pub fn set_credentials(&mut self, user_pass: Option<(&str, &str)>) -> Result<()> {
let auth = user_pass.map(|(user, pass)| {
let auth = RTSPAuth::new();
/*
let perm = RTSPToken::new(
...
);
auth.add_basic(RTSPAuth::make_basic(user, pass).as_str(), &perm);
*/
// TODO TLS https://thiblahute.github.io/GStreamer-doc/gst-rtsp-server-1.0/rtsp-server.html?gi-language=c
auth
});
self.server.set_auth(auth.as_ref());
Ok(())
}
pub fn run(&self, bind_addr: &str) {
self.server.set_address(bind_addr);
// Attach server to default Glib context
self.server.attach(None);
// Run the Glib main loop.
let main_loop = glib::MainLoop::new(None, false);
main_loop.run();
}
}
mod maybe_app_src {
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};
use super::*;
/// A Write implementation around AppSrc that also allows delaying the creation of the AppSrc
/// until later, discarding written data until the AppSrc is provided.
pub struct MaybeAppSrc {
rx: Receiver<AppSrc>,
app_src: Option<AppSrc>,
}
impl MaybeAppSrc {
/// Creates a MaybeAppSrc. Also returns a Sender that you must use to provide an AppSrc as
/// soon as one is available. When it is received, the MaybeAppSrc will start pushing data
/// into the AppSrc when write() is called.
pub fn new_with_tx() -> (Self, SyncSender<AppSrc>) {
let (tx, rx) = sync_channel(3); // The sender should not send very often
(MaybeAppSrc { rx, app_src: None, }, tx)
}
/// Attempts to retrieve the AppSrc that should be passed in by the caller of new_with_tx
/// at some point after this struct has been created. At that point, we swap over to
/// owning the AppSrc directly. This function handles either case and returns the AppSrc,
/// or None if the caller has not yet sent one.
fn try_get_src(&mut self) -> Option<&AppSrc> {
while let Some(src) = self.rx.try_recv().ok() {
self.app_src = Some(src);
}
self.app_src.as_ref()
}
}
impl Write for MaybeAppSrc {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
// If we have no AppSrc yet, throw away the data and claim that it was written
let app_src = match self.try_get_src() {
Some(src) => src,
None => return Ok(buf.len()),
};
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);
}
let res = app_src.push_buffer(gst_buf); //.map_err(|e| io::Error::new(io::ErrorKind::Other, Box::new(e)))?;
if res.is_err() {
self.app_src = None;
}
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
}
+14 -11
View File
@@ -1,15 +1,19 @@
#![allow(dead_code)]
#![allow(unused_variables)]
mod bc;
mod bc_protocol;
mod config;
mod cmdline;
mod gst;
use bc_protocol::BcCamera;
use config::{Config, CameraConfig};
use cmdline::Opt;
use crossbeam_utils::thread;
use err_derive::Error;
use gst::RtspServer;
use std::fs;
use std::net::TcpListener;
use std::io::Write;
use structopt::StructOpt;
#[derive(Debug, Error)]
@@ -26,19 +30,24 @@ fn main() -> Result<(), Error> {
let opt = Opt::from_args();
let config: Config = toml::from_str(&fs::read_to_string(opt.config)?)?;
let rtsp = &RtspServer::new();
thread::scope(|s| {
for camera in config.cameras {
s.spawn(move |_| {
// TODO handle these errors
camera_main(&camera)
let mut output = rtsp.add_stream(&camera.name).unwrap(); // TODO
camera_main(&camera, &mut output)
});
}
rtsp.run(&config.bind_addr);
}).unwrap();
Ok(())
}
fn camera_main(camera_config: &CameraConfig) -> Result<(), Error> {
fn camera_main(camera_config: &CameraConfig, output: &mut dyn Write) -> Result<(), Error> {
let mut camera = BcCamera::new_with_addr(camera_config.camera_addr)?;
println!("{}: Connecting to camera at {}", camera_config.name, camera_config.camera_addr);
@@ -46,15 +55,9 @@ fn camera_main(camera_config: &CameraConfig) -> Result<(), Error> {
camera.connect()?;
camera.login(&camera_config.username, camera_config.password.as_deref())?;
let bind_addr = &camera_config.bind_addr;
println!("{}: Logged in to camera; awaiting connection on {}", camera_config.name, bind_addr);
println!("{}: Connected to camera, starting video stream", camera_config.name);
let listener = TcpListener::bind(bind_addr)?;
let (mut out_socket, remote_addr) = listener.accept()?;
println!("{}: Connected to {}, starting video stream", camera_config.name, remote_addr);
camera.start_video(&mut out_socket)?;
camera.start_video(output)?;
Ok(())
}