json-based config for cameras and streams

for #155

The config interface code for changing cameras is quite messy but
seems to work for now.
This commit is contained in:
Scott Lamb 2021-09-10 16:31:03 -07:00
parent 070400095d
commit dafd9041d6
14 changed files with 694 additions and 315 deletions

1
server/Cargo.lock generated
View File

@ -2567,6 +2567,7 @@ dependencies = [
"idna",
"matches",
"percent-encoding",
"serde",
]
[[package]]

View File

@ -43,7 +43,7 @@ smallvec = "1.0"
tempfile = "3.2.0"
time = "0.1"
tokio = { version = "1.0", features = ["macros", "parking_lot", "rt-multi-thread", "sync"] }
url = "2.1.1"
url = { version = "2.1.1", features = ["serde"] }
uuid = { version = "0.8", features = ["std", "v4"] }
itertools = "0.10.0"

View File

@ -54,7 +54,6 @@ use std::str;
use std::string::String;
use std::sync::Arc;
use std::vec::Vec;
use url::Url;
use uuid::Uuid;
/// Expected schema version. See `guide/schema.md` for more information.
@ -359,24 +358,25 @@ pub struct Camera {
pub id: i32,
pub uuid: Uuid,
pub short_name: String,
pub description: String,
pub onvif_host: String,
pub username: Option<String>,
pub password: Option<String>,
pub streams: [Option<i32>; 2],
pub config: crate::json::CameraConfig,
pub streams: [Option<i32>; NUM_STREAM_TYPES],
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum StreamType {
Main,
Sub,
Ext,
}
pub const NUM_STREAM_TYPES: usize = 3;
impl StreamType {
pub fn from_index(i: usize) -> Option<Self> {
match i {
0 => Some(StreamType::Main),
1 => Some(StreamType::Sub),
2 => Some(StreamType::Ext),
_ => None,
}
}
@ -385,6 +385,7 @@ impl StreamType {
match self {
StreamType::Main => 0,
StreamType::Sub => 1,
StreamType::Ext => 2,
}
}
@ -392,6 +393,7 @@ impl StreamType {
match self {
StreamType::Main => "main",
StreamType::Sub => "sub",
StreamType::Ext => "ext",
}
}
@ -399,6 +401,7 @@ impl StreamType {
match type_ {
"main" => Some(StreamType::Main),
"sub" => Some(StreamType::Sub),
"ext" => Some(StreamType::Ext),
_ => None,
}
}
@ -410,16 +413,15 @@ impl ::std::fmt::Display for StreamType {
}
}
pub const ALL_STREAM_TYPES: [StreamType; 2] = [StreamType::Main, StreamType::Sub];
pub const ALL_STREAM_TYPES: [StreamType; NUM_STREAM_TYPES] =
[StreamType::Main, StreamType::Sub, StreamType::Ext];
pub struct Stream {
pub id: i32,
pub camera_id: i32,
pub sample_file_dir_id: Option<i32>,
pub type_: StreamType,
pub rtsp_url: String,
pub retain_bytes: i64,
pub flush_if_sec: i64,
pub config: crate::json::StreamConfig,
/// The time range of recorded data associated with this stream (minimum start time and maximum
/// end time). `None` iff there are no recordings for this camera.
@ -455,7 +457,6 @@ pub struct Stream {
/// Mapping of calendar day (in the server's time zone) to a summary of committed recordings on
/// that day.
pub committed_days: days::Map<days::StreamValue>,
pub record: bool,
/// The `cum_recordings` currently committed to the database.
pub(crate) cum_recordings: i32,
@ -500,24 +501,19 @@ pub struct LiveSegment {
#[derive(Clone, Debug, Default)]
pub struct StreamChange {
pub sample_file_dir_id: Option<i32>,
pub rtsp_url: Option<Url>,
pub record: bool,
pub flush_if_sec: i64,
pub config: crate::json::StreamConfig,
}
/// Information about a camera, used by `add_camera` and `update_camera`.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct CameraChange {
pub short_name: String,
pub description: String,
pub onvif_host: String,
pub username: Option<String>,
pub password: Option<String>,
pub config: crate::json::CameraConfig,
/// `StreamType t` is represented by `streams[t.index()]`. A default StreamChange will
/// correspond to no stream in the database, provided there are no existing recordings for that
/// stream.
pub streams: [StreamChange; 2],
pub streams: [StreamChange; NUM_STREAM_TYPES],
}
impl Stream {
@ -528,7 +524,7 @@ impl Stream {
None => r.start..r.end,
});
self.duration += r.end - r.start;
self.sample_file_bytes += sample_file_bytes as i64;
self.sample_file_bytes += i64::from(sample_file_bytes);
self.fs_bytes += round_up(i64::from(sample_file_bytes));
self.committed_days.adjust(r, 1);
}
@ -645,7 +641,7 @@ impl ::std::fmt::Display for CompositeId {
/// Inserts, updates, or removes streams in the `State` object to match a set of `StreamChange`
/// structs.
struct StreamStateChanger {
sids: [Option<i32>; 2],
sids: [Option<i32>; NUM_STREAM_TYPES],
streams: Vec<(i32, Option<(i32, StreamType, StreamChange)>)>,
}
@ -659,8 +655,8 @@ impl StreamStateChanger {
streams_by_id: &BTreeMap<i32, Stream>,
change: &mut CameraChange,
) -> Result<Self, Error> {
let mut sids = [None; 2];
let mut streams = Vec::with_capacity(2);
let mut sids = [None; NUM_STREAM_TYPES];
let mut streams = Vec::with_capacity(NUM_STREAM_TYPES);
let existing_streams = existing.map(|e| e.streams).unwrap_or_default();
for (i, ref mut sc) in change.streams.iter_mut().enumerate() {
let type_ = StreamType::from_index(i).unwrap();
@ -681,11 +677,7 @@ impl StreamStateChanger {
);
}
}
if !have_data
&& sc.rtsp_url.is_none()
&& sc.sample_file_dir_id.is_none()
&& !sc.record
{
if !have_data && sc.config.is_empty() && sc.sample_file_dir_id.is_none() {
// Delete stream.
let mut stmt = tx.prepare_cached(
r#"
@ -701,18 +693,14 @@ impl StreamStateChanger {
let mut stmt = tx.prepare_cached(
r#"
update stream set
rtsp_url = :rtsp_url,
record = :record,
flush_if_sec = :flush_if_sec,
config = :config,
sample_file_dir_id = :sample_file_dir_id
where
id = :id
"#,
)?;
let rows = stmt.execute(named_params! {
":rtsp_url": &sc.rtsp_url.as_ref().map(Url::as_str),
":record": sc.record,
":flush_if_sec": sc.flush_if_sec,
":config": &sc.config,
":sample_file_dir_id": sc.sample_file_dir_id,
":id": sid,
})?;
@ -724,28 +712,24 @@ impl StreamStateChanger {
streams.push((sid, Some((camera_id, type_, sc))));
}
} else {
if sc.rtsp_url.is_none() && sc.sample_file_dir_id.is_none() && !sc.record {
if sc.config.is_empty() && sc.sample_file_dir_id.is_none() {
// Do nothing; there is no record and we want to keep it that way.
continue;
}
// Insert stream.
let mut stmt = tx.prepare_cached(
r#"
insert into stream (camera_id, sample_file_dir_id, type, rtsp_url, record,
retain_bytes, flush_if_sec, cum_recordings,
cum_media_duration_90k, cum_runs)
values (:camera_id, :sample_file_dir_id, :type, :rtsp_url, :record,
0, :flush_if_sec, 0,
0, 0)
insert into stream (camera_id, sample_file_dir_id, type, config,
cum_recordings, cum_media_duration_90k, cum_runs)
values (:camera_id, :sample_file_dir_id, :type, :config,
0, 0, 0)
"#,
)?;
stmt.execute(named_params! {
":camera_id": camera_id,
":sample_file_dir_id": sc.sample_file_dir_id,
":type": type_.as_str(),
":rtsp_url": &sc.rtsp_url.as_ref().map(Url::as_str),
":record": sc.record,
":flush_if_sec": sc.flush_if_sec,
":config": &sc.config,
})?;
let id = tx.last_insert_rowid() as i32;
sids[i] = Some(id);
@ -758,19 +742,20 @@ impl StreamStateChanger {
/// Applies the change to the given `streams_by_id`. The caller is expected to set
/// `Camera::streams` to the return value.
fn apply(mut self, streams_by_id: &mut BTreeMap<i32, Stream>) -> [Option<i32>; 2] {
fn apply(
mut self,
streams_by_id: &mut BTreeMap<i32, Stream>,
) -> [Option<i32>; NUM_STREAM_TYPES] {
for (id, stream) in self.streams.drain(..) {
use ::std::collections::btree_map::Entry;
match (streams_by_id.entry(id), stream) {
(Entry::Vacant(e), Some((camera_id, type_, mut sc))) => {
(Entry::Vacant(e), Some((camera_id, type_, sc))) => {
e.insert(Stream {
id,
type_,
camera_id,
sample_file_dir_id: sc.sample_file_dir_id,
rtsp_url: sc.rtsp_url.take().map(String::from).unwrap_or_default(),
retain_bytes: 0,
flush_if_sec: sc.flush_if_sec,
config: sc.config,
range: None,
sample_file_bytes: 0,
fs_bytes: 0,
@ -781,7 +766,6 @@ impl StreamStateChanger {
fs_bytes_to_add: 0,
duration: recording::Duration(0),
committed_days: days::Map::default(),
record: sc.record,
cum_recordings: 0,
cum_media_duration: recording::Duration(0),
cum_runs: 0,
@ -791,12 +775,10 @@ impl StreamStateChanger {
});
}
(Entry::Vacant(_), None) => {}
(Entry::Occupied(e), Some((_, _, mut sc))) => {
(Entry::Occupied(e), Some((_, _, sc))) => {
let e = e.into_mut();
e.sample_file_dir_id = sc.sample_file_dir_id;
e.rtsp_url = sc.rtsp_url.take().map(String::from).unwrap_or_default();
e.record = sc.record;
e.flush_if_sec = sc.flush_if_sec;
e.config = sc.config;
}
(Entry::Occupied(e), None) => {
e.remove();
@ -1596,10 +1578,7 @@ impl LockedDatabase {
id,
uuid,
short_name,
description,
onvif_host,
username,
password
config
from
camera;
"#,
@ -1614,10 +1593,7 @@ impl LockedDatabase {
id,
uuid: uuid.0,
short_name: row.get(2)?,
description: row.get(3)?,
onvif_host: row.get(4)?,
username: row.get(5)?,
password: row.get(6)?,
config: row.get(3)?,
streams: Default::default(),
},
);
@ -1638,13 +1614,10 @@ impl LockedDatabase {
type,
camera_id,
sample_file_dir_id,
rtsp_url,
retain_bytes,
flush_if_sec,
config,
cum_recordings,
cum_media_duration_90k,
cum_runs,
record
cum_runs
from
stream;
"#,
@ -1660,7 +1633,6 @@ impl LockedDatabase {
.cameras_by_id
.get_mut(&camera_id)
.ok_or_else(|| format_err!("missing camera {} for stream {}", camera_id, id))?;
let flush_if_sec = row.get(6)?;
self.streams_by_id.insert(
id,
Stream {
@ -1668,9 +1640,7 @@ impl LockedDatabase {
type_,
camera_id,
sample_file_dir_id: row.get(3)?,
rtsp_url: row.get(4)?,
retain_bytes: row.get(5)?,
flush_if_sec,
config: row.get(4)?,
range: None,
sample_file_bytes: 0,
fs_bytes: 0,
@ -1681,10 +1651,9 @@ impl LockedDatabase {
fs_bytes_to_add: 0,
duration: recording::Duration(0),
committed_days: days::Map::default(),
cum_recordings: row.get(7)?,
cum_media_duration: recording::Duration(row.get(8)?),
cum_runs: row.get(9)?,
record: row.get(10)?,
cum_recordings: row.get(5)?,
cum_media_duration: recording::Duration(row.get(6)?),
cum_runs: row.get(7)?,
uncommitted: VecDeque::new(),
synced_recordings: 0,
on_live_segment: Vec::new(),
@ -1854,19 +1823,14 @@ impl LockedDatabase {
{
let mut stmt = tx.prepare_cached(
r#"
insert into camera (uuid, short_name, description, onvif_host, username,
password)
values (:uuid, :short_name, :description, :onvif_host, :username,
:password)
insert into camera (uuid, short_name, config)
values (:uuid, :short_name, :config)
"#,
)?;
stmt.execute(named_params! {
":uuid": uuid_bytes,
":short_name": &camera.short_name,
":description": &camera.description,
":onvif_host": &camera.onvif_host,
":username": &camera.username,
":password": &camera.password,
":config": &camera.config,
})?;
camera_id = tx.last_insert_rowid() as i32;
streams =
@ -1880,10 +1844,7 @@ impl LockedDatabase {
id: camera_id,
uuid,
short_name: camera.short_name,
description: camera.description,
onvif_host: camera.onvif_host,
username: camera.username,
password: camera.password,
config: camera.config,
streams,
},
);
@ -1891,6 +1852,36 @@ impl LockedDatabase {
Ok(camera_id)
}
/// Returns a `CameraChange` for the given camera which does nothing.
///
/// The caller can modify it to taste then pass it to `update_camera`.
/// TODO: consider renaming this to `update_camera` and creating a bulk
/// `apply_camera_changes`.
pub fn null_camera_change(&mut self, camera_id: i32) -> Result<CameraChange, Error> {
let camera = self
.cameras_by_id
.get(&camera_id)
.ok_or_else(|| format_err!("no such camera {}", camera_id))?;
let mut change = CameraChange {
short_name: camera.short_name.clone(),
config: camera.config.clone(),
streams: Default::default(),
};
for i in 0..NUM_STREAM_TYPES {
if let Some(stream_id) = camera.streams[i] {
let s = self
.streams_by_id
.get(&stream_id)
.expect("cameras reference valid streams");
change.streams[i] = StreamChange {
sample_file_dir_id: s.sample_file_dir_id,
config: s.config.clone(),
};
}
}
Ok(change)
}
/// Updates a camera.
pub fn update_camera(&mut self, camera_id: i32, mut camera: CameraChange) -> Result<(), Error> {
let tx = self.conn.transaction()?;
@ -1906,10 +1897,7 @@ impl LockedDatabase {
r#"
update camera set
short_name = :short_name,
description = :description,
onvif_host = :onvif_host,
username = :username,
password = :password
config = :config
where
id = :id
"#,
@ -1917,10 +1905,7 @@ impl LockedDatabase {
let rows = stmt.execute(named_params! {
":id": camera_id,
":short_name": &camera.short_name,
":description": &camera.description,
":onvif_host": &camera.onvif_host,
":username": &camera.username,
":password": &camera.password,
":config": &camera.config,
})?;
if rows != 1 {
bail!("Camera {} missing from database", camera_id);
@ -1928,16 +1913,14 @@ impl LockedDatabase {
}
tx.commit()?;
c.short_name = camera.short_name;
c.description = camera.description;
c.onvif_host = camera.onvif_host;
c.username = camera.username;
c.password = camera.password;
c.config = camera.config;
c.streams = streams.apply(&mut self.streams_by_id);
Ok(())
}
/// Deletes a camera and its streams. The camera must have no recordings.
pub fn delete_camera(&mut self, id: i32) -> Result<(), Error> {
// TODO: also verify there are no uncommitted recordings.
let uuid = self
.cameras_by_id
.get(&id)
@ -1975,35 +1958,34 @@ impl LockedDatabase {
Ok(())
}
// TODO: it'd make more sense to have a bulk camera/stream edit API than
// this specific one.
pub fn update_retention(&mut self, changes: &[RetentionChange]) -> Result<(), Error> {
// TODO: should validate there's only one change per id.
let tx = self.conn.transaction()?;
{
let mut stmt = tx.prepare_cached(
r#"
update stream
set
record = :record,
retain_bytes = :retain
config = :config
where
id = :id
"#,
)?;
for c in changes {
if c.new_limit < 0 {
bail!(
"can't set limit for stream {} to {}; must be >= 0",
c.stream_id,
c.new_limit
);
}
let stream = self
.streams_by_id
.get(&c.stream_id)
.ok_or_else(|| format_err!("no such stream id {}", c.stream_id))?;
let mut new_config = stream.config.clone();
new_config.mode = (if c.new_record { "record" } else { "" }).into();
new_config.retain_bytes = c.new_limit;
let rows = stmt.execute(named_params! {
":record": c.new_record,
":retain": c.new_limit,
":config": &new_config,
":id": c.stream_id,
})?;
if rows != 1 {
bail!("no such stream {}", c.stream_id);
}
assert_eq!(rows, 1, "missing stream {}", c.stream_id);
}
}
tx.commit()?;
@ -2012,8 +1994,8 @@ impl LockedDatabase {
.streams_by_id
.get_mut(&c.stream_id)
.expect("stream in db but not state");
s.record = c.new_record;
s.retain_bytes = c.new_limit;
s.config.mode = (if c.new_record { "record" } else { "" }).into();
s.config.retain_bytes = c.new_limit;
}
Ok(())
}
@ -2369,6 +2351,7 @@ mod tests {
use crate::testutil;
use base::clock;
use rusqlite::Connection;
use url::Url;
use uuid::Uuid;
fn setup_conn() -> Connection {
@ -2386,9 +2369,12 @@ mod tests {
rows += 1;
camera_id = row.id;
assert_eq!(uuid, row.uuid);
assert_eq!("test-camera", row.onvif_host);
assert_eq!(Some("foo"), row.username.as_deref());
assert_eq!(Some("bar"), row.password.as_deref());
assert_eq!(
"http://test-camera/",
row.config.onvif_base_url.as_ref().unwrap().as_str()
);
assert_eq!("foo", &row.config.username);
assert_eq!("bar", &row.config.password);
//assert_eq!("/main", row.main_rtsp_url);
//assert_eq!("/sub", row.sub_rtsp_url);
//assert_eq!(42, row.retain_bytes);
@ -2537,23 +2523,33 @@ mod tests {
let sample_file_dir_id = { db.lock() }.add_sample_file_dir(path).unwrap();
let mut c = CameraChange {
short_name: "testcam".to_owned(),
config: crate::json::CameraConfig {
description: "".to_owned(),
onvif_host: "test-camera".to_owned(),
username: Some("foo".to_owned()),
password: Some("bar".to_owned()),
onvif_base_url: Some(Url::parse("http://test-camera/").unwrap()),
username: "foo".to_owned(),
password: "bar".to_owned(),
..Default::default()
},
streams: [
StreamChange {
sample_file_dir_id: Some(sample_file_dir_id),
rtsp_url: Some(Url::parse("rtsp://test-camera/main").unwrap()),
record: false,
config: crate::json::StreamConfig {
url: Some(Url::parse("rtsp://test-camera/main").unwrap()),
mode: crate::json::STREAM_MODE_RECORD.to_owned(),
flush_if_sec: 1,
..Default::default()
},
},
StreamChange {
sample_file_dir_id: Some(sample_file_dir_id),
rtsp_url: Some(Url::parse("rtsp://test-camera/sub").unwrap()),
record: true,
config: crate::json::StreamConfig {
url: Some(Url::parse("rtsp://test-camera/sub").unwrap()),
mode: crate::json::STREAM_MODE_RECORD.to_owned(),
flush_if_sec: 1,
..Default::default()
},
},
StreamChange::default(),
],
};
let camera_id = db.lock().add_camera(c.clone()).unwrap();
@ -2573,19 +2569,27 @@ mod tests {
.unwrap();
{
let main = l.streams_by_id().get(&main_stream_id).unwrap();
assert!(main.record);
assert_eq!(main.retain_bytes, 42);
assert_eq!(main.flush_if_sec, 1);
assert_eq!(main.config.mode, crate::json::STREAM_MODE_RECORD);
assert_eq!(main.config.retain_bytes, 42);
assert_eq!(main.config.flush_if_sec, 1);
}
assert_eq!(
l.streams_by_id().get(&sub_stream_id).unwrap().flush_if_sec,
l.streams_by_id()
.get(&sub_stream_id)
.unwrap()
.config
.flush_if_sec,
1
);
c.streams[1].flush_if_sec = 2;
c.streams[1].config.flush_if_sec = 2;
l.update_camera(camera_id, c).unwrap();
assert_eq!(
l.streams_by_id().get(&sub_stream_id).unwrap().flush_if_sec,
l.streams_by_id()
.get(&sub_stream_id)
.unwrap()
.config
.flush_if_sec,
2
);
}
@ -2608,6 +2612,7 @@ mod tests {
.streams_by_id()
.get(&sub_stream_id)
.unwrap()
.config
.flush_if_sec,
2
);

128
server/db/json.rs Normal file
View File

@ -0,0 +1,128 @@
// This file is part of Moonfire NVR, a security camera network video recorder.
// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
//! JSON types for use in the database schema. See references from `schema.sql`.
use rusqlite::types::{FromSqlError, ValueRef};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use url::Url;
macro_rules! sql {
($l:ident) => {
impl rusqlite::types::FromSql for $l {
fn column_result(value: ValueRef) -> Result<Self, FromSqlError> {
match value {
ValueRef::Text(t) => {
Ok(serde_json::from_slice(t)
.map_err(|e| FromSqlError::Other(Box::new(e)))?)
}
_ => Err(FromSqlError::InvalidType),
}
}
}
impl rusqlite::types::ToSql for $l {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(serde_json::to_string(&self)
.map_err(|e| rusqlite::Error::ToSqlConversionFailure(e.into()))?
.into())
}
}
};
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CameraConfig {
/// A short description of the camera.
pub description: String,
/// The base URL for accessing ONVIF; `device_service` will be joined on
/// automatically to form the device management service URL.
/// Eg with `onvif_base=http://192.168.1.110:85`, the full
/// URL of the devie management service will be
/// `http://192.168.1.110:85/device_service`.
pub onvif_base_url: Option<Url>,
/// The username to use when accessing the camera.
/// If empty, no username or password will be supplied.
pub username: String,
/// The password to use when accessing the camera.
pub password: String,
#[serde(flatten)]
pub unknown: Map<String, Value>,
}
sql!(CameraConfig);
impl CameraConfig {
pub fn is_empty(&self) -> bool {
self.description.is_empty()
&& self.onvif_base_url.is_none()
&& self.username.is_empty()
&& self.password.is_empty()
&& self.unknown.is_empty()
}
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StreamConfig {
/// The mode of operation for this camera on startup.
///
/// Null means entirely disabled. At present, so does any value other than
/// `record`.
#[serde(default)]
pub mode: String,
/// The `rtsp://` URL to use for this stream, excluding username and
/// password.
///
/// In the future, this might support additional protocols such as `rtmp://`
/// or even a private use URI scheme for the [Baichuan
/// protocol](https://github.com/thirtythreeforty/neolink).
///
/// (Credentials are taken from [`CameraConfig`]'s respective fields.)
// TODO: should this really be Option?
pub url: Option<Url>,
/// The number of bytes of video to retain, excluding the
/// currently-recording file.
///
/// Older files will be deleted as necessary to stay within this limit.
#[serde(default)]
pub retain_bytes: i64,
/// Flush the database when the first instant of completed recording is this
/// many seconds old. A value of 0 means that every completed recording will
/// cause an immediate flush. Higher values may allow flushes to be combined,
/// reducing SSD write cycles. For example, if all streams have a
/// `flush_if_sec` >= *x* sec, there will be:
///
/// * at most one flush per *x* sec in total
/// * at most *x* sec of completed but unflushed recordings per stream.
/// * at most *x* completed but unflushed recordings per stream, in the
/// worst case where a recording instantly fails, waits the 1-second retry
/// delay, then fails again, forever.
#[serde(default)]
pub flush_if_sec: u32,
#[serde(flatten)]
pub unknown: Map<String, Value>,
}
sql!(StreamConfig);
pub const STREAM_MODE_RECORD: &'static str = "record";
impl StreamConfig {
pub fn is_empty(&self) -> bool {
self.mode.is_empty()
&& self.url.is_none()
&& self.retain_bytes == 0
&& self.flush_if_sec == 0
&& self.unknown.is_empty()
}
}

View File

@ -21,6 +21,7 @@ pub mod days;
pub mod db;
pub mod dir;
mod fs;
pub mod json;
mod proto {
include!(concat!(env!("OUT_DIR"), "/mod.rs"));
}

View File

@ -70,54 +70,18 @@ create table camera (
-- A short name of the camera, used in log messages.
short_name text not null,
-- A short description of the camera.
description text,
-- The host part of the http:// URL when accessing ONVIF, optionally
-- including ":<port>". Eg with ONVIF host "192.168.1.110:85", the full URL
-- of the devie management service will be
-- "http://192.168.1.110:85/device_service".
onvif_host text,
-- The username to use when accessing the camera.
-- If empty, no username or password will be supplied.
username text,
-- The password to use when accessing the camera.
password text
-- A serialized json.CameraConfig
config text not null
);
create table stream (
id integer primary key,
camera_id integer not null references camera (id),
sample_file_dir_id integer references sample_file_dir (id),
type text not null check (type in ('main', 'sub')),
type text not null check (type in ('main', 'sub', 'ext')),
-- If record is true, the stream should start recording when moonfire
-- starts. If false, no new recordings will be made, but old recordings
-- will not be deleted.
record integer not null check (record in (1, 0)),
-- The rtsp:// URL to use for this stream, excluding username and password.
-- (Those are taken from the camera row's respective fields.)
rtsp_url text not null,
-- The number of bytes of video to retain, excluding the currently-recording
-- file. Older files will be deleted as necessary to stay within this limit.
retain_bytes integer not null check (retain_bytes >= 0),
-- Flush the database when the first instant of completed recording is this
-- many seconds old. A value of 0 means that every completed recording will
-- cause an immediate flush. Higher values may allow flushes to be combined,
-- reducing SSD write cycles. For example, if all streams have a flush_if_sec
-- >= x sec, there will be:
--
-- * at most one flush per x sec in total
-- * at most x sec of completed but unflushed recordings per stream.
-- * at most x completed but unflushed recordings per stream, in the worst
-- case where a recording instantly fails, waits the 1-second retry delay,
-- then fails again, forever.
flush_if_sec integer not null,
-- A serialized json.StreamConfig
config text not null,
-- The total number of recordings ever created on this stream, including
-- deleted ones. This is used for assigning the next recording id.

View File

@ -63,7 +63,7 @@ impl<C: Clocks + Clone> TestDb<C> {
Self::new_with_flush_if_sec(clocks, 0)
}
pub(crate) fn new_with_flush_if_sec(clocks: C, flush_if_sec: i64) -> Self {
pub(crate) fn new_with_flush_if_sec(clocks: C, flush_if_sec: u32) -> Self {
let tmpdir = tempfile::Builder::new()
.prefix("moonfire-nvr-test")
.tempdir()
@ -82,17 +82,22 @@ impl<C: Clocks + Clone> TestDb<C> {
TEST_CAMERA_ID,
l.add_camera(db::CameraChange {
short_name: "test camera".to_owned(),
description: "".to_owned(),
onvif_host: "test-camera".to_owned(),
username: Some("foo".to_owned()),
password: Some("bar".to_owned()),
config: crate::json::CameraConfig::default(),
//description: "".to_owned(),
//onvif_host: "test-camera".to_owned(),
//username: Some("foo".to_owned()),
//password: Some("bar".to_owned()),
streams: [
db::StreamChange {
sample_file_dir_id: Some(sample_file_dir_id),
rtsp_url: Some(url::Url::parse("rtsp://test-camera/main").unwrap()),
record: true,
config: crate::json::StreamConfig {
url: Some(url::Url::parse("rtsp://test-camera/main").unwrap()),
mode: crate::json::STREAM_MODE_RECORD.to_owned(),
flush_if_sec,
..Default::default()
},
},
Default::default(),
Default::default(),
],
})

View File

@ -4,8 +4,226 @@
/// Upgrades a version 6 schema to a version 7 schema.
use failure::Error;
use rusqlite::{named_params, params};
use url::Url;
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
tx.execute_batch("alter table user add preferences text")?;
use crate::{json::CameraConfig, FromSqlUuid};
fn copy_cameras(tx: &rusqlite::Transaction) -> Result<(), Error> {
let mut insert = tx.prepare(
r#"
insert into camera (id, short_name, uuid, config)
values (:id, :short_name, :uuid, :config)
"#,
)?;
let mut stmt = tx.prepare(
r#"
select
id,
uuid,
short_name,
description,
onvif_host,
username,
password
from
old_camera
"#,
)?;
let mut rows = stmt.query(params![])?;
while let Some(row) = rows.next()? {
let id: i32 = row.get(0)?;
let uuid: FromSqlUuid = row.get(1)?;
let uuid_bytes = &uuid.0.as_bytes()[..];
let short_name: String = row.get(2)?;
let mut description: Option<String> = row.get(3)?;
let onvif_host: Option<String> = row.get(4)?;
let mut username: Option<String> = row.get(5)?;
let mut password: Option<String> = row.get(6)?;
let config = CameraConfig {
description: description.take().unwrap_or_default(),
onvif_base_url: onvif_host
.map(|h| Url::parse(&format!("rtsp://{}/", h)))
.transpose()?,
username: username.take().unwrap_or_default(),
password: password.take().unwrap_or_default(),
..Default::default()
};
insert.execute(named_params! {
":id": id,
":uuid": uuid_bytes,
":short_name": short_name,
":config": config,
})?;
}
Ok(())
}
fn copy_streams(tx: &rusqlite::Transaction) -> Result<(), Error> {
let mut insert = tx.prepare(
r#"
insert into stream (id, camera_id, sample_file_dir_id, type, config, cum_recordings,
cum_media_duration_90k, cum_runs)
values (:id, :camera_id, :sample_file_dir_id, :type, :config, :cum_recordings,
:cum_media_duration_90k, :cum_runs)
"#,
)?;
let mut stmt = tx.prepare(
r#"
select
id,
camera_id,
sample_file_dir_id,
type,
record,
rtsp_url,
retain_bytes,
flush_if_sec,
cum_recordings,
cum_media_duration_90k,
cum_runs
from
old_stream
"#,
)?;
let mut rows = stmt.query(params![])?;
while let Some(row) = rows.next()? {
let id: i32 = row.get(0)?;
let camera_id: i32 = row.get(1)?;
let sample_file_dir_id: i32 = row.get(2)?;
let type_: String = row.get(3)?;
let record: bool = row.get(4)?;
let rtsp_url: String = row.get(5)?;
let retain_bytes: i64 = row.get(6)?;
let flush_if_sec: u32 = row.get(7)?;
let cum_recordings: i64 = row.get(8)?;
let cum_media_duration_90k: i64 = row.get(9)?;
let cum_runs: i64 = row.get(10)?;
let config = crate::json::StreamConfig {
mode: (if record {
""
} else {
crate::json::STREAM_MODE_RECORD
})
.to_owned(),
url: Some(Url::parse(&rtsp_url)?),
retain_bytes,
flush_if_sec,
..Default::default()
};
insert.execute(named_params! {
":id": id,
":camera_id": camera_id,
":sample_file_dir_id": sample_file_dir_id,
":type": type_,
":config": config,
":cum_recordings": cum_recordings,
":cum_media_duration_90k": cum_media_duration_90k,
":cum_runs": cum_runs,
})?;
}
Ok(())
}
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
tx.execute_batch(
r#"
alter table user add preferences text;
alter table camera rename to old_camera;
alter table stream rename to old_stream;
create table camera (
id integer primary key,
uuid blob unique not null check (length(uuid) = 16),
short_name text not null,
config text not null
);
create table stream (
id integer primary key,
camera_id integer not null references camera (id),
sample_file_dir_id integer references sample_file_dir (id),
type text not null check (type in ('main', 'sub', 'ext')),
config text not null,
cum_recordings integer not null check (cum_recordings >= 0),
cum_media_duration_90k integer not null check (cum_media_duration_90k >= 0),
cum_runs integer not null check (cum_runs >= 0),
unique (camera_id, type)
);
"#,
)?;
copy_cameras(tx)?;
copy_streams(tx)?;
tx.execute_batch(
r#"
drop index recording_cover;
alter table recording rename to old_recording;
create table recording (
composite_id integer primary key,
open_id integer not null,
stream_id integer not null references stream (id),
run_offset integer not null,
flags integer not null,
sample_file_bytes integer not null check (sample_file_bytes > 0),
start_time_90k integer not null check (start_time_90k > 0),
prev_media_duration_90k integer not null check (prev_media_duration_90k >= 0),
prev_runs integer not null check (prev_runs >= 0),
wall_duration_90k integer not null
check (wall_duration_90k >= 0 and wall_duration_90k < 5*60*90000),
media_duration_delta_90k integer not null,
video_samples integer not null check (video_samples > 0),
video_sync_samples integer not null check (video_sync_samples > 0),
video_sample_entry_id integer references video_sample_entry (id),
check (composite_id >> 32 = stream_id)
);
create index recording_cover on recording (
stream_id,
start_time_90k,
open_id,
wall_duration_90k,
media_duration_delta_90k,
video_samples,
video_sync_samples,
video_sample_entry_id,
sample_file_bytes,
run_offset,
flags
);
insert into recording select * from old_recording;
alter table recording_integrity rename to old_recording_integrity;
create table recording_integrity (
composite_id integer primary key references recording (composite_id),
local_time_delta_90k integer,
local_time_since_open_90k integer,
wall_time_delta_90k integer,
sample_file_blake3 blob check (length(sample_file_blake3) <= 32)
);
insert into recording_integrity select * from old_recording_integrity;
alter table recording_playback rename to old_recording_playback;
create table recording_playback (
composite_id integer primary key references recording (composite_id),
video_index blob not null check (length(video_index) > 0)
);
insert into recording_playback select * from old_recording_playback;
alter table signal_camera rename to old_signal_camera;
create table signal_camera (
signal_id integer references signal (id),
camera_id integer references camera (id),
type integer not null,
primary key (signal_id, camera_id)
) without rowid;
drop table old_signal_camera;
drop table old_recording_playback;
drop table old_recording_integrity;
drop table old_recording;
drop table old_stream;
drop table old_camera;
"#,
)?;
Ok(())
}

View File

@ -210,7 +210,7 @@ pub fn lower_retention(
.ok_or_else(|| format_err!("no such stream {}", l.stream_id))?;
fs_bytes_before =
stream.fs_bytes + stream.fs_bytes_to_add - stream.fs_bytes_to_delete;
extra = stream.retain_bytes - l.limit;
extra = stream.config.retain_bytes - l.limit;
}
if l.limit >= fs_bytes_before {
continue;
@ -235,14 +235,14 @@ fn delete_recordings(
Some(s) => s,
};
stream.fs_bytes + stream.fs_bytes_to_add - stream.fs_bytes_to_delete + extra_bytes_needed
- stream.retain_bytes
- stream.config.retain_bytes
};
let mut fs_bytes_to_delete = 0;
if fs_bytes_needed <= 0 {
debug!(
"{}: have remaining quota of {}",
stream_id,
base::strutil::encode_size(-fs_bytes_needed)
base::strutil::encode_size(fs_bytes_needed)
);
return Ok(());
}
@ -500,12 +500,13 @@ impl<C: Clocks + Clone, D: DirWriter> Syncer<C, D> {
let c = db.cameras_by_id().get(&s.camera_id).unwrap();
// Schedule a flush.
let how_soon = Duration::seconds(s.flush_if_sec) - wall_duration.to_tm_duration();
let how_soon =
Duration::seconds(i64::from(s.config.flush_if_sec)) - wall_duration.to_tm_duration();
let now = self.db.clocks().monotonic();
let when = now + how_soon;
let reason = format!(
"{} sec after start of {} {}-{} recording {}",
s.flush_if_sec,
s.config.flush_if_sec,
wall_duration,
c.short_name,
s.type_.as_str(),
@ -1054,7 +1055,7 @@ mod tests {
syncer_rcv: mpsc::Receiver<super::SyncerCommand<MockFile>>,
}
fn new_harness(flush_if_sec: i64) -> Harness {
fn new_harness(flush_if_sec: u32) -> Harness {
let clocks = SimulatedClocks::new(::time::Timespec::new(0, 0));
let tdb = testutil::TestDb::new_with_flush_if_sec(clocks, flush_if_sec);
let dir_id = *tdb

View File

@ -4,104 +4,117 @@
use crate::stream::{self, Opener};
use base::strutil::{decode_size, encode_size};
use cursive::traits::{Boxable, Finder, Identifiable};
use cursive::traits::{Boxable, Finder, Identifiable, Scrollable};
use cursive::views::{self, ViewRef};
use cursive::Cursive;
use db::writer;
use failure::{bail, Error, ResultExt};
use failure::{bail, format_err, Error, ResultExt};
use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
/// Builds a `CameraChange` from an active `edit_camera_dialog`.
fn get_change(siv: &mut Cursive) -> Result<db::CameraChange, Error> {
struct Camera {
short_name: String,
description: String,
onvif_base_url: String,
username: String,
password: String,
streams: [Stream; db::NUM_STREAM_TYPES],
}
#[derive(Default)]
struct Stream {
url: String,
record: bool,
flush_if_sec: String,
sample_file_dir_id: Option<i32>,
}
/// Builds a `Camera` from an active `edit_camera_dialog`. No validation.
fn get_camera(siv: &mut Cursive) -> Camera {
// Note: these find_name calls are separate statements, which seems to be important:
// https://github.com/gyscos/Cursive/issues/144
let sn = siv
let short_name = siv
.find_name::<views::EditView>("short_name")
.unwrap()
.get_content()
.as_str()
.into();
let d = siv
let description = siv
.find_name::<views::TextArea>("description")
.unwrap()
.get_content()
.into();
let h = siv
.find_name::<views::EditView>("onvif_host")
let onvif_base_url: String = siv
.find_name::<views::EditView>("onvif_base_url")
.unwrap()
.get_content()
.as_str()
.into();
let username = match siv
let username = siv
.find_name::<views::EditView>("username")
.unwrap()
.get_content()
.as_str()
{
"" => None,
u => Some(u.to_owned()),
};
let password = match siv
.to_owned();
let password = siv
.find_name::<views::EditView>("password")
.unwrap()
.get_content()
.as_str()
{
"" => None,
p => Some(p.to_owned()),
};
let mut c = db::CameraChange {
short_name: sn,
description: d,
onvif_host: h,
.to_owned();
let mut camera = Camera {
short_name,
description,
onvif_base_url,
username,
password,
streams: Default::default(),
};
for &t in &db::ALL_STREAM_TYPES {
let rtsp_url = parse_url(
siv.find_name::<views::EditView>(&format!("{}_rtsp_url", t.as_str()))
let url = siv
.find_name::<views::EditView>(&format!("{}_url", t.as_str()))
.unwrap()
.get_content()
.as_str(),
)?;
.as_str()
.to_owned();
let record = siv
.find_name::<views::Checkbox>(&format!("{}_record", t.as_str()))
.unwrap()
.is_checked();
let flush_if_sec = i64::from_str(
siv.find_name::<views::EditView>(&format!("{}_flush_if_sec", t.as_str()))
let flush_if_sec = siv
.find_name::<views::EditView>(&format!("{}_flush_if_sec", t.as_str()))
.unwrap()
.get_content()
.as_str(),
)
.unwrap_or(0);
.as_str()
.to_owned();
let sample_file_dir_id = *siv
.find_name::<views::SelectView<Option<i32>>>(&format!("{}_sample_file_dir", t.as_str()))
.unwrap()
.selection()
.unwrap();
c.streams[t.index()] = db::StreamChange {
rtsp_url,
sample_file_dir_id,
camera.streams[t.index()] = Stream {
url,
record,
flush_if_sec,
sample_file_dir_id,
};
}
Ok(c)
camera
}
/// Attempts to parse a URL field into a sort-of-validated URL.
fn parse_url(raw: &str) -> Result<Option<Url>, Error> {
fn parse_url(raw: &str, allowed_schemes: &'static [&'static str]) -> Result<Option<Url>, Error> {
if raw.is_empty() {
return Ok(None);
}
let url = url::Url::parse(&raw).with_context(|_| format!("can't parse {:?} as URL", &raw))?;
if url.scheme() != "rtsp" {
bail!("Expected URL scheme rtsp:// in URL {}", &url);
if allowed_schemes
.iter()
.find(|scheme| **scheme == url.scheme())
.is_none()
{
bail!("Unexpected scheme in URL {}", &url);
}
if !url.username().is_empty() || url.password().is_some() {
bail!(
@ -114,17 +127,46 @@ fn parse_url(raw: &str) -> Result<Option<Url>, Error> {
fn press_edit(siv: &mut Cursive, db: &Arc<db::Database>, id: Option<i32>) {
let result = (|| {
let change = get_change(siv)?;
for (i, stream) in change.streams.iter().enumerate() {
if stream.record && (stream.rtsp_url.is_none() || stream.sample_file_dir_id.is_none()) {
let mut l = db.lock();
let mut change = if let Some(id) = id {
l.null_camera_change(id)?
} else {
db::CameraChange::default()
};
let camera = get_camera(siv);
change.short_name = camera.short_name;
change.config.description = camera.description;
change.config.onvif_base_url = parse_url(&camera.onvif_base_url, &["http", "https"])?;
change.config.username = camera.username;
change.config.password = camera.password;
for (i, stream) in camera.streams.iter().enumerate() {
let type_ = db::StreamType::from_index(i).unwrap();
if stream.record && (stream.url.is_empty() || stream.sample_file_dir_id.is_none()) {
bail!(
"Can't record {} stream without RTSP URL and sample file directory",
type_.as_str()
);
}
let stream_change = &mut change.streams[i];
stream_change.config.mode = (if stream.record {
db::json::STREAM_MODE_RECORD
} else {
""
})
.to_owned();
stream_change.config.url = parse_url(&stream.url, &["rtsp"])?;
stream_change.sample_file_dir_id = stream.sample_file_dir_id;
stream_change.config.flush_if_sec = if stream.flush_if_sec.is_empty() {
0
} else {
stream.flush_if_sec.parse().map_err(|_| {
format_err!(
"flush_if_sec for {} must be a non-negative integer",
type_.as_str()
)
})?
};
}
let mut l = db.lock();
if let Some(id) = id {
l.update_camera(id, change)
} else {
@ -146,17 +188,14 @@ fn press_edit(siv: &mut Cursive, db: &Arc<db::Database>, id: Option<i32>) {
}
}
fn press_test_inner(
url: Url,
username: Option<String>,
password: Option<String>,
) -> Result<String, Error> {
fn press_test_inner(url: Url, username: String, password: String) -> Result<String, Error> {
let pass_creds = !username.is_empty();
let (extra_data, _stream) = stream::FFMPEG.open(
"test stream".to_owned(),
stream::Source::Rtsp {
url,
username,
password,
username: if pass_creds { Some(username) } else { None },
password: if pass_creds { Some(password) } else { None },
transport: retina::client::Transport::Tcp,
},
)?;
@ -167,21 +206,15 @@ fn press_test_inner(
}
fn press_test(siv: &mut Cursive, t: db::StreamType) {
let mut c = match get_change(siv) {
Ok(u) => u,
Err(e) => {
siv.add_layer(
views::Dialog::text(format!("{}", e))
.title("Stream test failed")
.dismiss_button("Back"),
);
return;
}
let c = get_camera(siv);
let url = &c.streams[t.index()].url;
let url = match parse_url(url, &["rtsp"]) {
Ok(Some(u)) => u,
_ => panic!(
"test button should only be enabled with valid URL, not {:?}",
url
),
};
let url = c.streams[t.index()]
.rtsp_url
.take()
.expect("test button only enabled when URL set");
let username = c.username;
let password = c.password;
@ -190,7 +223,7 @@ fn press_test(siv: &mut Cursive, t: db::StreamType) {
"Testing {} stream at {}. This may take a while \
on timeout or if you have a long key frame interval",
t.as_str(),
&url
url.as_str()
))
.title("Testing"),
);
@ -348,7 +381,7 @@ fn actually_delete(siv: &mut Cursive, db: &Arc<db::Database>, id: i32) {
}
fn edit_url(content: &str, mut test_button: ViewRef<views::Button>) {
let enable_test = matches!(parse_url(content), Ok(Some(_)));
let enable_test = matches!(parse_url(content, &["rtsp"]), Ok(Some(_)));
test_button.set_enabled(enable_test);
}
@ -365,7 +398,10 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
)
.child("uuid", views::TextView::new("<new>").with_name("uuid"))
.child("short name", views::EditView::new().with_name("short_name"))
.child("onvif_host", views::EditView::new().with_name("onvif_host"))
.child(
"onvif_base_url",
views::EditView::new().with_name("onvif_base_url"),
)
.child("username", views::EditView::new().with_name("username"))
.child("password", views::EditView::new().with_name("password"))
.min_height(6);
@ -399,7 +435,7 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
.unwrap();
edit_url(content, test_button)
})
.with_name(format!("{}_rtsp_url", type_.as_str()))
.with_name(format!("{}_url", type_.as_str()))
.full_width(),
)
.child(views::DummyView)
@ -434,7 +470,7 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
layout.add_child(list);
}
let mut dialog = views::Dialog::around(layout);
let mut dialog = views::Dialog::around(layout.scrollable());
let dialog = if let Some(camera_id) = *item {
let l = db.lock();
let camera = l.cameras_by_id().get(&camera_id).expect("missing camera");
@ -460,35 +496,41 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
}
}
bytes += s.sample_file_bytes;
let u = if s.retain_bytes == 0 {
let u = if s.config.retain_bytes == 0 {
"0 / 0 (0.0%)".to_owned()
} else {
format!(
"{} / {} ({:.1}%)",
s.fs_bytes,
s.retain_bytes,
100. * s.fs_bytes as f32 / s.retain_bytes as f32
s.config.retain_bytes,
100. * s.fs_bytes as f32 / s.config.retain_bytes as f32
)
};
dialog.call_on_name(
&format!("{}_rtsp_url", t.as_str()),
|v: &mut views::EditView| v.set_content(s.rtsp_url.to_owned()),
);
dialog.call_on_name(&format!("{}_url", t.as_str()), |v: &mut views::EditView| {
if let Some(url) = s.config.url.as_ref() {
v.set_content(url.as_str().to_owned());
}
});
let test_button = dialog
.find_name::<views::Button>(&format!("{}_test", t.as_str()))
.unwrap();
edit_url(&s.rtsp_url, test_button);
edit_url(
&s.config.url.as_ref().map(Url::as_str).unwrap_or(""),
test_button,
);
dialog.call_on_name(
&format!("{}_usage_cap", t.as_str()),
|v: &mut views::TextView| v.set_content(u),
);
dialog.call_on_name(
&format!("{}_record", t.as_str()),
|v: &mut views::Checkbox| v.set_checked(s.record),
|v: &mut views::Checkbox| {
v.set_checked(s.config.mode == db::json::STREAM_MODE_RECORD)
},
);
dialog.call_on_name(
&format!("{}_flush_if_sec", t.as_str()),
|v: &mut views::EditView| v.set_content(s.flush_if_sec.to_string()),
|v: &mut views::EditView| v.set_content(s.config.flush_if_sec.to_string()),
);
}
dialog.call_on_name(
@ -499,9 +541,17 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
let name = camera.short_name.clone();
for &(view_id, content) in &[
("short_name", &*camera.short_name),
("onvif_host", &*camera.onvif_host),
("username", camera.username.as_deref().unwrap_or("")),
("password", camera.password.as_deref().unwrap_or("")),
(
"onvif_base_url",
&camera
.config
.onvif_base_url
.as_ref()
.map(Url::as_str)
.unwrap_or(""),
),
("username", &camera.config.username),
("password", &camera.config.password),
] {
dialog
.call_on_name(view_id, |v: &mut views::EditView| {
@ -511,7 +561,7 @@ fn edit_camera_dialog(db: &Arc<db::Database>, siv: &mut Cursive, item: &Option<i
}
dialog
.call_on_name("description", |v: &mut views::TextArea| {
v.set_content(camera.description.to_string())
v.set_content(camera.config.description.clone())
})
.expect("missing TextArea");
dialog

View File

@ -315,12 +315,12 @@ fn edit_dir_dialog(db: &Arc<db::Database>, siv: &mut Cursive, dir_id: i32) {
Stream {
label: format!("{}: {}: {}", id, c.short_name, s.type_.as_str()),
used: s.fs_bytes,
record: s.record,
retain: Some(s.retain_bytes),
record: s.config.mode == db::json::STREAM_MODE_RECORD,
retain: Some(s.config.retain_bytes),
},
);
total_used += s.fs_bytes;
total_retain += s.retain_bytes;
total_retain += s.config.retain_bytes;
}
if streams.is_empty() {
return delete_dir_dialog(db, siv, dir_id);

View File

@ -232,12 +232,20 @@ async fn async_run(args: &Args) -> Result<i32, Error> {
// Get the directories that need syncers.
for stream in l.streams_by_id().values() {
if let (Some(id), true) = (stream.sample_file_dir_id, stream.record) {
if stream.config.mode != db::json::STREAM_MODE_RECORD {
continue;
}
if let Some(id) = stream.sample_file_dir_id {
dirs.entry(id).or_insert_with(|| {
let d = l.sample_file_dirs_by_id().get(&id).unwrap();
info!("Starting syncer for path {}", d.path);
d.get().unwrap()
});
} else {
warn!(
"Stream {} set to record but has no sample file dir id",
stream.id
);
}
}
@ -253,7 +261,7 @@ async fn async_run(args: &Args) -> Result<i32, Error> {
let handle = tokio::runtime::Handle::current();
let l = db.lock();
for (i, (id, stream)) in l.streams_by_id().iter().enumerate() {
if !stream.record {
if stream.config.mode != db::json::STREAM_MODE_RECORD {
continue;
}
let camera = l.cameras_by_id().get(&stream.camera_id).unwrap();

View File

@ -56,10 +56,9 @@ pub struct Camera<'a> {
pub uuid: Uuid,
pub id: i32,
pub short_name: &'a str,
pub description: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<CameraConfig<'a>>,
pub config: Option<&'a db::json::CameraConfig>,
#[serde(serialize_with = "Camera::serialize_streams")]
pub streams: [Option<Stream<'a>>; 2],
@ -90,13 +89,7 @@ pub struct Stream<'a> {
pub days: Option<db::days::Map<db::days::StreamValue>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<StreamConfig<'a>>,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StreamConfig<'a> {
pub rtsp_url: &'a str,
pub config: Option<&'a db::json::StreamConfig>,
}
#[derive(Serialize)]
@ -188,14 +181,9 @@ impl<'a> Camera<'a> {
uuid: c.uuid,
id: c.id,
short_name: &c.short_name,
description: &c.description,
config: match include_config {
false => None,
true => Some(CameraConfig {
onvif_host: &c.onvif_host,
username: c.username.as_deref(),
password: c.password.as_deref(),
}),
true => Some(&c.config),
},
streams: [
Stream::wrap(db, c.streams[0], include_days, include_config)?,
@ -240,19 +228,17 @@ impl<'a> Stream<'a> {
.ok_or_else(|| format_err!("missing stream {}", id))?;
Ok(Some(Stream {
id: s.id,
retain_bytes: s.retain_bytes,
retain_bytes: s.config.retain_bytes,
min_start_time_90k: s.range.as_ref().map(|r| r.start),
max_end_time_90k: s.range.as_ref().map(|r| r.end),
total_duration_90k: s.duration,
total_sample_file_bytes: s.sample_file_bytes,
fs_bytes: s.fs_bytes,
record: s.record,
record: s.config.mode == db::json::STREAM_MODE_RECORD,
days: if include_days { Some(s.days()) } else { None },
config: match include_config {
false => None,
true => Some(StreamConfig {
rtsp_url: &s.rtsp_url,
}),
true => Some(&s.config),
},
}))
}

View File

@ -5,7 +5,7 @@
use crate::stream;
use base::clock::{Clocks, TimerGuard};
use db::{dir, recording, writer, Camera, Database, Stream};
use failure::{bail, Error};
use failure::{bail, format_err, Error};
use log::{debug, info, trace, warn};
use std::result::Result;
use std::sync::atomic::{AtomicBool, Ordering};
@ -44,8 +44,8 @@ where
stream_id: i32,
short_name: String,
url: Url,
username: Option<String>,
password: Option<String>,
username: String,
password: String,
}
impl<'a, C> Streamer<'a, C>
@ -62,7 +62,11 @@ where
rotate_offset_sec: i64,
rotate_interval_sec: i64,
) -> Result<Self, Error> {
let url = Url::parse(&s.rtsp_url)?;
let url = s
.config
.url
.as_ref()
.ok_or_else(|| format_err!("Stream has no RTSP URL"))?;
if !url.username().is_empty() || url.password().is_some() {
bail!("RTSP URL shouldn't include credentials");
}
@ -77,9 +81,9 @@ where
transport: env.transport,
stream_id,
short_name: format!("{}-{}", c.short_name, s.type_.as_str()),
url,
username: c.username.clone(),
password: c.password.clone(),
url: url.clone(),
username: c.config.username.clone(),
password: c.config.password.clone(),
})
}
@ -116,8 +120,16 @@ where
self.short_name.clone(),
stream::Source::Rtsp {
url: self.url.clone(),
username: self.username.clone(),
password: self.password.clone(),
username: if self.username.is_empty() {
None
} else {
Some(self.username.clone())
},
password: if self.password.is_empty() {
None
} else {
Some(self.password.clone())
},
transport: self.transport,
},
)?