mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2025-01-12 15:33:22 -05:00
schema version 6 with pixel aspect ratio
This makes anamorphic sub streams display correctly, even ones from old Hikvision cameras that don't properly set the aspect ratio at the H.264 layer.
This commit is contained in:
parent
066c086050
commit
e5b83c21e1
21
Cargo.lock
generated
21
Cargo.lock
generated
@ -155,6 +155,15 @@ version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
|
||||
|
||||
[[package]]
|
||||
name = "bitreader"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fa7f0adf37cd5472c978a1ff4be89c1880a923d10df4cfef6a10855a666e09b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blake2-rfc"
|
||||
version = "0.2.18"
|
||||
@ -841,6 +850,15 @@ dependencies = [
|
||||
"tokio-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "h264-reader"
|
||||
version = "0.3.0"
|
||||
source = "git+https://github.com/dholroyd/h264-reader#40863110d31bc37d924e69f6b3e4228e51ea8702"
|
||||
dependencies = [
|
||||
"bitreader",
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.1"
|
||||
@ -1236,9 +1254,11 @@ version = "0.0.1"
|
||||
dependencies = [
|
||||
"base64 0.11.0",
|
||||
"blake2-rfc",
|
||||
"byteorder",
|
||||
"cstr",
|
||||
"failure",
|
||||
"fnv",
|
||||
"h264-reader",
|
||||
"itertools",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
@ -1286,6 +1306,7 @@ dependencies = [
|
||||
"failure",
|
||||
"fnv",
|
||||
"futures",
|
||||
"h264-reader",
|
||||
"http",
|
||||
"http-serve",
|
||||
"hyper",
|
||||
|
@ -30,6 +30,7 @@ failure = "0.1.1"
|
||||
ffmpeg = { package = "moonfire-ffmpeg", path = "ffmpeg" }
|
||||
futures = "0.3"
|
||||
fnv = "1.0"
|
||||
h264-reader = { git = "https://github.com/dholroyd/h264-reader" }
|
||||
http = "0.2.0"
|
||||
http-serve = "0.2.0"
|
||||
hyper = "0.13.0"
|
||||
|
@ -15,9 +15,11 @@ path = "lib.rs"
|
||||
base = { package = "moonfire-base", path = "../base" }
|
||||
base64 = "0.11.0"
|
||||
blake2-rfc = "0.2.18"
|
||||
byteorder = "1.0"
|
||||
cstr = "0.1.7"
|
||||
failure = "0.1.1"
|
||||
fnv = "1.0"
|
||||
h264-reader = { git = "https://github.com/dholroyd/h264-reader" }
|
||||
lazy_static = "1.0"
|
||||
libc = "0.2"
|
||||
libpasta = "0.1.0-rc2"
|
||||
|
112
db/db.rs
112
db/db.rs
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -65,13 +65,13 @@ use fnv::{FnvHashMap, FnvHashSet};
|
||||
use itertools::Itertools;
|
||||
use log::{error, info, trace};
|
||||
use lru_cache::LruCache;
|
||||
use openssl::hash;
|
||||
use parking_lot::{Mutex,MutexGuard};
|
||||
use protobuf::prelude::MessageField;
|
||||
use rusqlite::{named_params, params};
|
||||
use smallvec::SmallVec;
|
||||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::convert::TryInto;
|
||||
use std::cmp;
|
||||
use std::fmt::Write as _;
|
||||
use std::io::Write;
|
||||
@ -85,7 +85,7 @@ use time;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Expected schema version. See `guide/schema.md` for more information.
|
||||
pub const EXPECTED_VERSION: i32 = 5;
|
||||
pub const EXPECTED_VERSION: i32 = 6;
|
||||
|
||||
const GET_RECORDING_PLAYBACK_SQL: &'static str = r#"
|
||||
select
|
||||
@ -97,8 +97,10 @@ const GET_RECORDING_PLAYBACK_SQL: &'static str = r#"
|
||||
"#;
|
||||
|
||||
const INSERT_VIDEO_SAMPLE_ENTRY_SQL: &'static str = r#"
|
||||
insert into video_sample_entry (sha1, width, height, rfc6381_codec, data)
|
||||
values (:sha1, :width, :height, :rfc6381_codec, :data)
|
||||
insert into video_sample_entry (sha1, width, height, pasp_h_spacing, pasp_v_spacing,
|
||||
rfc6381_codec, data)
|
||||
values (:sha1, :width, :height, :pasp_h_spacing, :pasp_v_spacing,
|
||||
:rfc6381_codec, :data)
|
||||
"#;
|
||||
|
||||
const UPDATE_NEXT_RECORDING_ID_SQL: &'static str =
|
||||
@ -126,12 +128,27 @@ impl rusqlite::types::FromSql for VideoIndex {
|
||||
/// the codec, width, height, etc.
|
||||
#[derive(Debug)]
|
||||
pub struct VideoSampleEntry {
|
||||
pub id: i32,
|
||||
pub sha1: [u8; 20],
|
||||
|
||||
// Fields matching VideoSampleEntryToInsert below.
|
||||
|
||||
pub data: Vec<u8>,
|
||||
pub rfc6381_codec: String,
|
||||
pub id: i32,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub sha1: [u8; 20],
|
||||
pub pasp_h_spacing: u16,
|
||||
pub pasp_v_spacing: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct VideoSampleEntryToInsert {
|
||||
pub data: Vec<u8>,
|
||||
pub rfc6381_codec: String,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub pasp_h_spacing: u16,
|
||||
pub pasp_v_spacing: u16,
|
||||
}
|
||||
|
||||
/// A row used in `list_recordings_by_time` and `list_recordings_by_id`.
|
||||
@ -1344,6 +1361,8 @@ impl LockedDatabase {
|
||||
sha1,
|
||||
width,
|
||||
height,
|
||||
pasp_h_spacing,
|
||||
pasp_v_spacing,
|
||||
rfc6381_codec,
|
||||
data
|
||||
from
|
||||
@ -1358,15 +1377,17 @@ impl LockedDatabase {
|
||||
bail!("video sample entry id {} has sha1 {} of wrong length", id, sha1_vec.len());
|
||||
}
|
||||
sha1.copy_from_slice(&sha1_vec);
|
||||
let data: Vec<u8> = row.get(5)?;
|
||||
let data: Vec<u8> = row.get(7)?;
|
||||
|
||||
self.video_sample_entries_by_id.insert(id, Arc::new(VideoSampleEntry {
|
||||
id: id as i32,
|
||||
width: row.get::<_, i32>(2)? as u16,
|
||||
height: row.get::<_, i32>(3)? as u16,
|
||||
id,
|
||||
width: row.get::<_, i32>(2)?.try_into()?,
|
||||
height: row.get::<_, i32>(3)?.try_into()?,
|
||||
pasp_h_spacing: row.get::<_, i32>(4)?.try_into()?,
|
||||
pasp_v_spacing: row.get::<_, i32>(5)?.try_into()?,
|
||||
sha1,
|
||||
data,
|
||||
rfc6381_codec: row.get(4)?,
|
||||
rfc6381_codec: row.get(6)?,
|
||||
}));
|
||||
}
|
||||
info!("Loaded {} video sample entries",
|
||||
@ -1509,21 +1530,21 @@ impl LockedDatabase {
|
||||
|
||||
/// Inserts the specified video sample entry if absent.
|
||||
/// On success, returns the id of a new or existing row.
|
||||
pub fn insert_video_sample_entry(&mut self, width: u16, height: u16, data: Vec<u8>,
|
||||
rfc6381_codec: String) -> Result<i32, Error> {
|
||||
let sha1 = hash::hash(hash::MessageDigest::sha1(), &data)?;
|
||||
let mut sha1_bytes = [0u8; 20];
|
||||
sha1_bytes.copy_from_slice(&sha1);
|
||||
pub fn insert_video_sample_entry(&mut self, entry: VideoSampleEntryToInsert)
|
||||
-> Result<i32, Error> {
|
||||
let sha1 = crate::sha1(&entry.data)?;
|
||||
|
||||
// Check if it already exists.
|
||||
// There shouldn't be too many entries, so it's fine to enumerate everything.
|
||||
for (&id, v) in &self.video_sample_entries_by_id {
|
||||
if v.sha1 == sha1_bytes {
|
||||
// The width and height should match given that they're also specified within data
|
||||
// and thus included in the just-compared hash.
|
||||
if v.width != width || v.height != height {
|
||||
bail!("database entry for {:?} is {}x{}, not {}x{}",
|
||||
&sha1[..], v.width, v.height, width, height);
|
||||
if v.sha1 == sha1 {
|
||||
// A hash collision (different data with the same hash) is unlikely.
|
||||
// The other fields are derived from data, so differences there indicate a bug.
|
||||
if v.width != entry.width || v.height != entry.height ||
|
||||
v.pasp_h_spacing != entry.pasp_h_spacing ||
|
||||
v.pasp_v_spacing != entry.pasp_v_spacing {
|
||||
bail!("video_sample_entry SHA-1 {} mismatch: existing entry {:?}, new {:?}",
|
||||
base::strutil::hex(&sha1[..]), v, &entry);
|
||||
}
|
||||
return Ok(id);
|
||||
}
|
||||
@ -1531,21 +1552,25 @@ impl LockedDatabase {
|
||||
|
||||
let mut stmt = self.conn.prepare_cached(INSERT_VIDEO_SAMPLE_ENTRY_SQL)?;
|
||||
stmt.execute_named(named_params!{
|
||||
":sha1": &sha1_bytes[..],
|
||||
":width": i32::from(width),
|
||||
":height": i32::from(height),
|
||||
":rfc6381_codec": &rfc6381_codec,
|
||||
":data": &data,
|
||||
":sha1": &sha1[..],
|
||||
":width": i32::from(entry.width),
|
||||
":height": i32::from(entry.height),
|
||||
":pasp_h_spacing": i32::from(entry.pasp_h_spacing),
|
||||
":pasp_v_spacing": i32::from(entry.pasp_v_spacing),
|
||||
":rfc6381_codec": &entry.rfc6381_codec,
|
||||
":data": &entry.data,
|
||||
})?;
|
||||
|
||||
let id = self.conn.last_insert_rowid() as i32;
|
||||
self.video_sample_entries_by_id.insert(id, Arc::new(VideoSampleEntry {
|
||||
id,
|
||||
width,
|
||||
height,
|
||||
sha1: sha1_bytes,
|
||||
data,
|
||||
rfc6381_codec,
|
||||
width: entry.width,
|
||||
height: entry.height,
|
||||
pasp_h_spacing: entry.pasp_h_spacing,
|
||||
pasp_v_spacing: entry.pasp_v_spacing,
|
||||
sha1,
|
||||
data: entry.data,
|
||||
rfc6381_codec: entry.rfc6381_codec,
|
||||
}));
|
||||
|
||||
Ok(id)
|
||||
@ -2215,20 +2240,20 @@ mod tests {
|
||||
fn test_version_too_old() {
|
||||
testutil::init();
|
||||
let c = setup_conn();
|
||||
c.execute_batch("delete from version; insert into version values (4, 0, '');").unwrap();
|
||||
c.execute_batch("delete from version; insert into version values (5, 0, '');").unwrap();
|
||||
let e = Database::new(clock::RealClocks {}, c, false).err().unwrap();
|
||||
assert!(e.to_string().starts_with(
|
||||
"Database schema version 4 is too old (expected 5)"), "got: {:?}", e);
|
||||
"Database schema version 5 is too old (expected 6)"), "got: {:?}", e);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_version_too_new() {
|
||||
testutil::init();
|
||||
let c = setup_conn();
|
||||
c.execute_batch("delete from version; insert into version values (6, 0, '');").unwrap();
|
||||
c.execute_batch("delete from version; insert into version values (7, 0, '');").unwrap();
|
||||
let e = Database::new(clock::RealClocks {}, c, false).err().unwrap();
|
||||
assert!(e.to_string().starts_with(
|
||||
"Database schema version 6 is too new (expected 5)"), "got: {:?}", e);
|
||||
"Database schema version 7 is too new (expected 6)"), "got: {:?}", e);
|
||||
}
|
||||
|
||||
/// Basic test of running some queries on a fresh database.
|
||||
@ -2308,9 +2333,14 @@ mod tests {
|
||||
|
||||
// TODO: assert_eq!(db.lock().list_garbage(sample_file_dir_id).unwrap(), &[]);
|
||||
|
||||
let vse_id = db.lock().insert_video_sample_entry(
|
||||
1920, 1080, include_bytes!("testdata/avc1").to_vec(),
|
||||
"avc1.4d0029".to_owned()).unwrap();
|
||||
let vse_id = db.lock().insert_video_sample_entry(VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: include_bytes!("testdata/avc1").to_vec(),
|
||||
rfc6381_codec: "avc1.4d0029".to_owned(),
|
||||
}).unwrap();
|
||||
assert!(vse_id > 0, "vse_id = {}", vse_id);
|
||||
|
||||
// Inserting a recording should succeed and advance the next recording id.
|
||||
|
11
db/lib.rs
11
db/lib.rs
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2018 The Moonfire NVR Authors
|
||||
// Copyright (C) 2018-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -51,3 +51,12 @@ pub mod testutil;
|
||||
pub use crate::db::*;
|
||||
pub use crate::schema::Permissions;
|
||||
pub use crate::signal::Signal;
|
||||
|
||||
use openssl::hash;
|
||||
|
||||
fn sha1(input: &[u8]) -> Result<[u8; 20], failure::Error> {
|
||||
let sha1 = hash::hash(hash::MessageDigest::sha1(), &input)?;
|
||||
let mut sha1_bytes = [0u8; 20];
|
||||
sha1_bytes.copy_from_slice(&sha1);
|
||||
Ok(sha1_bytes)
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
-- This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
-- Copyright (C) 2016 The Moonfire NVR Authors
|
||||
-- Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
--
|
||||
-- This program is free software: you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
@ -312,7 +312,12 @@ create table video_sample_entry (
|
||||
|
||||
-- The serialized box, including the leading length and box type (avcC in
|
||||
-- the case of H.264).
|
||||
data blob not null check (length(data) > 86)
|
||||
data blob not null check (length(data) > 86),
|
||||
|
||||
-- Pixel aspect ratio, if known. As defined in ISO/IEC 14496-12 section
|
||||
-- 12.1.4.
|
||||
pasp_h_spacing integer not null default 1 check (pasp_h_spacing > 0),
|
||||
pasp_v_spacing integer not null default 1 check (pasp_v_spacing > 0)
|
||||
);
|
||||
|
||||
create table user (
|
||||
@ -494,4 +499,4 @@ create table signal_change (
|
||||
);
|
||||
|
||||
insert into version (id, unix_time, notes)
|
||||
values (5, cast(strftime('%s', 'now') as int), 'db creation');
|
||||
values (6, cast(strftime('%s', 'now') as int), 'db creation');
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -146,8 +146,14 @@ impl<C: Clocks + Clone> TestDb<C> {
|
||||
-> db::ListRecordingsRow {
|
||||
use crate::recording::{self, TIME_UNITS_PER_SEC};
|
||||
let mut db = self.db.lock();
|
||||
let video_sample_entry_id = db.insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = db.insert_video_sample_entry(db::VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let (id, _) = db.add_recording(TEST_STREAM_ID, db::RecordingToInsert {
|
||||
start: recording::Time(1430006400i64 * TIME_UNITS_PER_SEC),
|
||||
video_sample_entry_id,
|
||||
@ -169,8 +175,14 @@ pub fn add_dummy_recordings_to_db(db: &db::Database, num: usize) {
|
||||
let mut data = Vec::new();
|
||||
data.extend_from_slice(include_bytes!("testdata/video_sample_index.bin"));
|
||||
let mut db = db.lock();
|
||||
let video_sample_entry_id = db.insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = db.insert_video_sample_entry(db::VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let mut recording = db::RecordingToInsert {
|
||||
sample_file_bytes: 30104460,
|
||||
start: recording::Time(1430006400i64 * TIME_UNITS_PER_SEC),
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -46,6 +46,7 @@ mod v1_to_v2;
|
||||
mod v2_to_v3;
|
||||
mod v3_to_v4;
|
||||
mod v4_to_v5;
|
||||
mod v5_to_v6;
|
||||
|
||||
const UPGRADE_NOTES: &'static str =
|
||||
concat!("upgraded using moonfire-db ", env!("CARGO_PKG_VERSION"));
|
||||
@ -72,6 +73,7 @@ fn upgrade(args: &Args, target_ver: i32, conn: &mut rusqlite::Connection) -> Res
|
||||
v2_to_v3::run,
|
||||
v3_to_v4::run,
|
||||
v4_to_v5::run,
|
||||
v5_to_v6::run,
|
||||
];
|
||||
|
||||
{
|
||||
@ -158,8 +160,32 @@ mod tests {
|
||||
use crate::compare;
|
||||
use crate::testutil;
|
||||
use failure::{ResultExt, format_err};
|
||||
use fnv::FnvHashMap;
|
||||
use super::*;
|
||||
|
||||
const BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] =
|
||||
b"\x00\x00\x00\x84\x61\x76\x63\x31\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x00\x01\x40\x00\xf0\x00\x48\x00\x00\x00\x48\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18\xff\xff\x00\x00\x00\x2e\
|
||||
\x61\x76\x63\x43\x01\x4d\x40\x1e\xff\xe1\x00\x17\x67\x4d\x40\x1e\
|
||||
\x9a\x66\x0a\x0f\xff\x35\x01\x01\x01\x40\x00\x00\xfa\x00\x00\x03\
|
||||
\x01\xf4\x01\x01\x00\x04\x68\xee\x3c\x80";
|
||||
|
||||
const GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY: &[u8] =
|
||||
b"\x00\x00\x00\x9f\x61\x76\x63\x31\x00\x00\x00\x00\x00\x00\x00\x01\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x02\xc0\x01\xe0\x00\x48\x00\x00\x00\x48\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
|
||||
\x00\x00\x00\x18\xff\xff\x00\x00\x00\x49\x61\x76\x63\x43\x01\x64\
|
||||
\x00\x16\xff\xe1\x00\x31\x67\x64\x00\x16\xac\x1b\x1a\x80\xb0\x3d\
|
||||
\xff\xff\x00\x28\x00\x21\x6e\x0c\x0c\x0c\x80\x00\x01\xf4\x00\x00\
|
||||
\x27\x10\x74\x30\x07\xd0\x00\x07\xa1\x25\xde\x5c\x68\x60\x0f\xa0\
|
||||
\x00\x0f\x42\x4b\xbc\xb8\x50\x01\x00\x05\x68\xee\x38\x30\x00";
|
||||
|
||||
fn new_conn() -> Result<rusqlite::Connection, Error> {
|
||||
let conn = rusqlite::Connection::open_in_memory()?;
|
||||
conn.execute("pragma foreign_keys = on", params![])?;
|
||||
@ -194,9 +220,19 @@ mod tests {
|
||||
"#)?;
|
||||
upgraded.execute(r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (1, X'3BA3EDE1BD93B7BCB7AB5BD099C047701451B822',
|
||||
1920, 1080, ?);
|
||||
"#, params![testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA])?;
|
||||
values (1, ?, 1920, 1080, ?);
|
||||
"#, params![&crate::sha1(testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA).unwrap()[..],
|
||||
testutil::TEST_VIDEO_SAMPLE_ENTRY_DATA])?;
|
||||
upgraded.execute(r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (2, ?, 320, 240, ?);
|
||||
"#, params![&crate::sha1(BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY).unwrap()[..],
|
||||
BAD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY])?;
|
||||
upgraded.execute(r#"
|
||||
insert into video_sample_entry (id, sha1, width, height, data)
|
||||
values (3, ?, 704, 480, ?);
|
||||
"#, params![&crate::sha1(GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY).unwrap()[..],
|
||||
GOOD_ANAMORPHIC_VIDEO_SAMPLE_ENTRY])?;
|
||||
upgraded.execute_batch(r#"
|
||||
insert into recording (id, camera_id, sample_file_bytes, start_time_90k, duration_90k,
|
||||
local_time_delta_90k, video_samples, video_sync_samples,
|
||||
@ -216,7 +252,8 @@ mod tests {
|
||||
(2, None), // transitional; don't compare schemas.
|
||||
(3, Some(include_str!("v3.sql"))),
|
||||
(4, None), // transitional; don't compare schemas.
|
||||
(5, Some(include_str!("../schema.sql")))] {
|
||||
(5, Some(include_str!("v5.sql"))),
|
||||
(6, Some(include_str!("../schema.sql")))] {
|
||||
upgrade(&Args {
|
||||
flag_sample_file_dir: Some(&path),
|
||||
flag_preset_journal: "delete",
|
||||
@ -232,6 +269,28 @@ mod tests {
|
||||
assert!(!garbage.exists());
|
||||
std::fs::File::create(&garbage)?;
|
||||
}
|
||||
if *ver == 6 {
|
||||
// Check that the pasp was set properly.
|
||||
let mut stmt = upgraded.prepare(r#"
|
||||
select
|
||||
id,
|
||||
pasp_h_spacing,
|
||||
pasp_v_spacing
|
||||
from
|
||||
video_sample_entry
|
||||
"#)?;
|
||||
let mut rows = stmt.query(params![])?;
|
||||
let mut pasp_by_id = FnvHashMap::default();
|
||||
while let Some(row) = rows.next()? {
|
||||
let id: i32 = row.get(0)?;
|
||||
let pasp_h_spacing: i32 = row.get(1)?;
|
||||
let pasp_v_spacing: i32 = row.get(2)?;
|
||||
pasp_by_id.insert(id, (pasp_h_spacing, pasp_v_spacing));
|
||||
}
|
||||
assert_eq!(pasp_by_id.get(&1), Some(&(1, 1)));
|
||||
assert_eq!(pasp_by_id.get(&2), Some(&(4, 3)));
|
||||
assert_eq!(pasp_by_id.get(&3), Some(&(40, 33)));
|
||||
}
|
||||
}
|
||||
|
||||
// Check that recording files get renamed.
|
||||
|
497
db/upgrade/v5.sql
Normal file
497
db/upgrade/v5.sql
Normal file
@ -0,0 +1,497 @@
|
||||
-- This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
-- Copyright (C) 2016 The Moonfire NVR Authors
|
||||
--
|
||||
-- This program is free software: you can redistribute it and/or modify
|
||||
-- it under the terms of the GNU General Public License as published by
|
||||
-- the Free Software Foundation, either version 3 of the License, or
|
||||
-- (at your option) any later version.
|
||||
--
|
||||
-- In addition, as a special exception, the copyright holders give
|
||||
-- permission to link the code of portions of this program with the
|
||||
-- OpenSSL library under certain conditions as described in each
|
||||
-- individual source file, and distribute linked combinations including
|
||||
-- the two.
|
||||
--
|
||||
-- You must obey the GNU General Public License in all respects for all
|
||||
-- of the code used other than OpenSSL. If you modify file(s) with this
|
||||
-- exception, you may extend this exception to your version of the
|
||||
-- file(s), but you are not obligated to do so. If you do not wish to do
|
||||
-- so, delete this exception statement from your version. If you delete
|
||||
-- this exception statement from all source files in the program, then
|
||||
-- also delete it here.
|
||||
--
|
||||
-- This program is distributed in the hope that it will be useful,
|
||||
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
-- GNU General Public License for more details.
|
||||
--
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
--
|
||||
-- schema.sql: SQLite3 database schema for Moonfire NVR.
|
||||
-- See also design/schema.md.
|
||||
|
||||
-- Database metadata. There should be exactly one row in this table.
|
||||
create table meta (
|
||||
uuid blob not null check (length(uuid) = 16),
|
||||
|
||||
-- The maximum number of entries in the signal_state table. If an update
|
||||
-- causes this to be exceeded, older times will be garbage collected to stay
|
||||
-- within the limit.
|
||||
max_signal_changes integer check (max_signal_changes >= 0)
|
||||
);
|
||||
|
||||
-- This table tracks the schema version.
|
||||
-- There is one row for the initial database creation (inserted below, after the
|
||||
-- create statements) and one for each upgrade procedure (if any).
|
||||
create table version (
|
||||
id integer primary key,
|
||||
|
||||
-- The unix time as of the creation/upgrade, as determined by
|
||||
-- cast(strftime('%s', 'now') as int).
|
||||
unix_time integer not null,
|
||||
|
||||
-- Optional notes on the creation/upgrade; could include the binary version.
|
||||
notes text
|
||||
);
|
||||
|
||||
-- Tracks every time the database has been opened in read/write mode.
|
||||
-- This is used to ensure directories are in sync with the database (see
|
||||
-- schema.proto:DirMeta), to disambiguate uncommitted recordings, and
|
||||
-- potentially to understand time problems.
|
||||
create table open (
|
||||
id integer primary key,
|
||||
uuid blob unique not null check (length(uuid) = 16),
|
||||
|
||||
-- Information about when / how long the database was open. These may be all
|
||||
-- null, for example in the open that represents all information written
|
||||
-- prior to database version 3.
|
||||
|
||||
-- System time when the database was opened, in 90 kHz units since
|
||||
-- 1970-01-01 00:00:00Z excluding leap seconds.
|
||||
start_time_90k integer,
|
||||
|
||||
-- System time when the database was closed or (on crash) last flushed.
|
||||
end_time_90k integer,
|
||||
|
||||
-- How long the database was open. This is end_time_90k - start_time_90k if
|
||||
-- there were no time steps or leap seconds during this time.
|
||||
duration_90k integer
|
||||
);
|
||||
|
||||
create table sample_file_dir (
|
||||
id integer primary key,
|
||||
path text unique not null,
|
||||
uuid blob unique not null check (length(uuid) = 16),
|
||||
|
||||
-- The last (read/write) open of this directory which fully completed.
|
||||
-- See schema.proto:DirMeta for a more complete description.
|
||||
last_complete_open_id integer references open (id)
|
||||
);
|
||||
|
||||
create table camera (
|
||||
id integer primary key,
|
||||
uuid blob unique not null check (length(uuid) = 16),
|
||||
|
||||
-- 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
|
||||
);
|
||||
|
||||
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')),
|
||||
|
||||
-- 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,
|
||||
|
||||
-- The low 32 bits of the next recording id to assign for this stream.
|
||||
-- Typically this is the maximum current recording + 1, but it does
|
||||
-- not decrease if that recording is deleted.
|
||||
next_recording_id integer not null check (next_recording_id >= 0),
|
||||
|
||||
unique (camera_id, type)
|
||||
);
|
||||
|
||||
-- Each row represents a single completed recorded segment of video.
|
||||
-- Recordings are typically ~60 seconds; never more than 5 minutes.
|
||||
create table recording (
|
||||
-- The high 32 bits of composite_id are taken from the stream's id, which
|
||||
-- improves locality. The low 32 bits are taken from the stream's
|
||||
-- next_recording_id (which should be post-incremented in the same
|
||||
-- transaction). It'd be simpler to use a "without rowid" table and separate
|
||||
-- fields to make up the primary key, but
|
||||
-- <https://www.sqlite.org/withoutrowid.html> points out that "without rowid"
|
||||
-- is not appropriate when the average row size is in excess of 50 bytes.
|
||||
-- recording_cover rows (which match this id format) are typically 1--5 KiB.
|
||||
composite_id integer primary key,
|
||||
|
||||
-- The open in which this was committed to the database. For a given
|
||||
-- composite_id, only one recording will ever be committed to the database,
|
||||
-- but in-memory state may reflect a recording which never gets committed.
|
||||
-- This field allows disambiguation in etags and such.
|
||||
open_id integer not null references open (id),
|
||||
|
||||
-- This field is redundant with id above, but used to enforce the reference
|
||||
-- constraint and to structure the recording_start_time index.
|
||||
stream_id integer not null references stream (id),
|
||||
|
||||
-- The offset of this recording within a run. 0 means this was the first
|
||||
-- recording made from a RTSP session. The start of the run has id
|
||||
-- (id-run_offset).
|
||||
run_offset integer not null,
|
||||
|
||||
-- flags is a bitmask:
|
||||
--
|
||||
-- * 1, or "trailing zero", indicates that this recording is the last in a
|
||||
-- stream. As the duration of a sample is not known until the next sample
|
||||
-- is received, the final sample in this recording will have duration 0.
|
||||
flags integer not null,
|
||||
|
||||
sample_file_bytes integer not null check (sample_file_bytes > 0),
|
||||
|
||||
-- The starting time of the recording, in 90 kHz units since
|
||||
-- 1970-01-01 00:00:00 UTC excluding leap seconds. Currently on initial
|
||||
-- connection, this is taken from the local system time; on subsequent
|
||||
-- recordings, it exactly matches the previous recording's end time.
|
||||
start_time_90k integer not null check (start_time_90k > 0),
|
||||
|
||||
-- The duration of the recording, in 90 kHz units.
|
||||
duration_90k integer not null
|
||||
check (duration_90k >= 0 and duration_90k < 5*60*90000),
|
||||
|
||||
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 (
|
||||
-- Typical queries use "where stream_id = ? order by start_time_90k".
|
||||
stream_id,
|
||||
start_time_90k,
|
||||
|
||||
-- These fields are not used for ordering; they cover most queries so
|
||||
-- that only database verification and actual viewing of recordings need
|
||||
-- to consult the underlying row.
|
||||
open_id,
|
||||
duration_90k,
|
||||
video_samples,
|
||||
video_sync_samples,
|
||||
video_sample_entry_id,
|
||||
sample_file_bytes,
|
||||
run_offset,
|
||||
flags
|
||||
);
|
||||
|
||||
-- Fields which are only needed to check/correct database integrity problems
|
||||
-- (such as incorrect timestamps).
|
||||
create table recording_integrity (
|
||||
-- See description on recording table.
|
||||
composite_id integer primary key references recording (composite_id),
|
||||
|
||||
-- The number of 90 kHz units the local system's monotonic clock has
|
||||
-- advanced more than the stated duration of recordings in a run since the
|
||||
-- first recording ended. Negative numbers indicate the local system time is
|
||||
-- behind the recording.
|
||||
--
|
||||
-- The first recording of a run (that is, one with run_offset=0) has null
|
||||
-- local_time_delta_90k because errors are assumed to
|
||||
-- be the result of initial buffering rather than frequency mismatch.
|
||||
--
|
||||
-- This value should be near 0 even on long runs in which the camera's clock
|
||||
-- and local system's clock frequency differ because each recording's delta
|
||||
-- is used to correct the durations of the next (up to 500 ppm error).
|
||||
local_time_delta_90k integer,
|
||||
|
||||
-- The number of 90 kHz units the local system's monotonic clock had
|
||||
-- advanced since the database was opened, as of the start of recording.
|
||||
-- TODO: fill this in!
|
||||
local_time_since_open_90k integer,
|
||||
|
||||
-- The difference between start_time_90k+duration_90k and a wall clock
|
||||
-- timestamp captured at end of this recording. This is meaningful for all
|
||||
-- recordings in a run, even the initial one (run_offset=0), because
|
||||
-- start_time_90k is derived from the wall time as of when recording
|
||||
-- starts, not when it ends.
|
||||
-- TODO: fill this in!
|
||||
wall_time_delta_90k integer,
|
||||
|
||||
-- The sha1 hash of the contents of the sample file.
|
||||
sample_file_sha1 blob check (length(sample_file_sha1) <= 20)
|
||||
);
|
||||
|
||||
-- Large fields for a recording which are needed ony for playback.
|
||||
-- In particular, when serving a byte range within a .mp4 file, the
|
||||
-- recording_playback row is needed for the recording(s) corresponding to that
|
||||
-- particular byte range, needed, but the recording rows suffice for all other
|
||||
-- recordings in the .mp4.
|
||||
create table recording_playback (
|
||||
-- See description on recording table.
|
||||
composite_id integer primary key references recording (composite_id),
|
||||
|
||||
-- See design/schema.md#video_index for a description of this field.
|
||||
video_index blob not null check (length(video_index) > 0)
|
||||
|
||||
-- audio_index could be added here in the future.
|
||||
);
|
||||
|
||||
-- Files which are to be deleted (may or may not still exist).
|
||||
-- Note that besides these files, for each stream, any recordings >= its
|
||||
-- next_recording_id should be discarded on startup.
|
||||
create table garbage (
|
||||
-- This is _mostly_ redundant with composite_id, which contains the stream
|
||||
-- id and thus a linkage to the sample file directory. Listing it here
|
||||
-- explicitly means that streams can be deleted without losing the
|
||||
-- association of garbage to directory.
|
||||
sample_file_dir_id integer not null references sample_file_dir (id),
|
||||
|
||||
-- See description on recording table.
|
||||
composite_id integer not null,
|
||||
|
||||
-- Organize the table first by directory, as that's how it will be queried.
|
||||
primary key (sample_file_dir_id, composite_id)
|
||||
) without rowid;
|
||||
|
||||
-- A concrete box derived from a ISO/IEC 14496-12 section 8.5.2
|
||||
-- VisualSampleEntry box. Describes the codec, width, height, etc.
|
||||
create table video_sample_entry (
|
||||
id integer primary key,
|
||||
|
||||
-- A SHA-1 hash of |bytes|.
|
||||
sha1 blob unique not null check (length(sha1) = 20),
|
||||
|
||||
-- The width and height in pixels; must match values within
|
||||
-- |sample_entry_bytes|.
|
||||
width integer not null check (width > 0),
|
||||
height integer not null check (height > 0),
|
||||
|
||||
-- The codec in RFC-6381 format, such as "avc1.4d001f".
|
||||
rfc6381_codec text not null,
|
||||
|
||||
-- The serialized box, including the leading length and box type (avcC in
|
||||
-- the case of H.264).
|
||||
data blob not null check (length(data) > 86)
|
||||
);
|
||||
|
||||
create table user (
|
||||
id integer primary key,
|
||||
username unique not null,
|
||||
|
||||
-- Bitwise mask of flags:
|
||||
-- 1: disabled. If set, no method of authentication for this user will succeed.
|
||||
flags integer not null,
|
||||
|
||||
-- If set, a hash for password authentication, as generated by `libpasta::hash_password`.
|
||||
password_hash text,
|
||||
|
||||
-- A counter which increments with every password reset or clear.
|
||||
password_id integer not null default 0,
|
||||
|
||||
-- Updated lazily on database flush; reset when password_id is incremented.
|
||||
-- This could be used to automatically disable the password on hitting a threshold.
|
||||
password_failure_count integer not null default 0,
|
||||
|
||||
-- If set, a Unix UID that is accepted for authentication when using HTTP over
|
||||
-- a Unix domain socket. (Additionally, the UID running Moonfire NVR can authenticate
|
||||
-- as anyone; there's no point in trying to do otherwise.) This might be an easy
|
||||
-- bootstrap method once configuration happens through a web UI rather than text UI.
|
||||
unix_uid integer,
|
||||
|
||||
-- Permissions available for newly created tokens or when authenticating via
|
||||
-- unix_uid above. A serialized "Permissions" protobuf.
|
||||
permissions blob not null default X''
|
||||
);
|
||||
|
||||
-- A single session, whether for browser or robot use.
|
||||
-- These map at the HTTP layer to an "s" cookie (exact format described
|
||||
-- elsewhere), which holds the session id and an encrypted sequence number for
|
||||
-- replay protection.
|
||||
create table user_session (
|
||||
-- The session id is a 48-byte blob. This is the unencoded, unsalted Blake2b-192
|
||||
-- (24 bytes) of the unencoded session id. Much like `password_hash`, a
|
||||
-- hash is used here so that a leaked database backup can't be trivially used
|
||||
-- to steal credentials.
|
||||
session_id_hash blob primary key not null,
|
||||
|
||||
user_id integer references user (id) not null,
|
||||
|
||||
-- A 32-byte random number. Used to derive keys for the replay protection
|
||||
-- and CSRF tokens.
|
||||
seed blob not null,
|
||||
|
||||
-- A bitwise mask of flags, currently all properties of the HTTP cookie
|
||||
-- used to hold the session:
|
||||
-- 1: HttpOnly
|
||||
-- 2: Secure
|
||||
-- 4: SameSite=Lax
|
||||
-- 8: SameSite=Strict - 4 must also be set.
|
||||
flags integer not null,
|
||||
|
||||
-- The domain of the HTTP cookie used to store this session. The outbound
|
||||
-- `Set-Cookie` header never specifies a scope, so this matches the `Host:` of
|
||||
-- the inbound HTTP request (minus the :port, if any was specified).
|
||||
domain text,
|
||||
|
||||
-- An editable description which might describe the device/program which uses
|
||||
-- this session, such as "Chromebook", "iPhone", or "motion detection worker".
|
||||
description text,
|
||||
|
||||
creation_password_id integer, -- the id it was created from, if created via password
|
||||
creation_time_sec integer not null, -- sec since epoch
|
||||
creation_user_agent text, -- User-Agent header from inbound HTTP request.
|
||||
creation_peer_addr blob, -- IPv4 or IPv6 address, or null for Unix socket.
|
||||
|
||||
revocation_time_sec integer, -- sec since epoch
|
||||
revocation_user_agent text, -- User-Agent header from inbound HTTP request.
|
||||
revocation_peer_addr blob, -- IPv4 or IPv6 address, or null for Unix socket/no peer.
|
||||
|
||||
-- A value indicating the reason for revocation, with optional additional
|
||||
-- text detail. Enumeration values:
|
||||
-- 0: logout link clicked (i.e. from within the session itself)
|
||||
--
|
||||
-- This might be extended for a variety of other reasons:
|
||||
-- x: user revoked (while authenticated in another way)
|
||||
-- x: password change invalidated all sessions created with that password
|
||||
-- x: expired (due to fixed total time or time inactive)
|
||||
-- x: evicted (due to too many sessions)
|
||||
-- x: suspicious activity
|
||||
revocation_reason integer,
|
||||
revocation_reason_detail text,
|
||||
|
||||
-- Information about requests which used this session, updated lazily on database flush.
|
||||
last_use_time_sec integer, -- sec since epoch
|
||||
last_use_user_agent text, -- User-Agent header from inbound HTTP request.
|
||||
last_use_peer_addr blob, -- IPv4 or IPv6 address, or null for Unix socket.
|
||||
use_count not null default 0,
|
||||
|
||||
-- Permissions associated with this token; a serialized "Permissions" protobuf.
|
||||
permissions blob not null default X''
|
||||
) without rowid;
|
||||
|
||||
create index user_session_uid on user_session (user_id);
|
||||
|
||||
create table signal (
|
||||
id integer primary key,
|
||||
|
||||
-- a uuid describing the originating object, such as the uuid of the camera
|
||||
-- for built-in motion detection. There will be a JSON interface for adding
|
||||
-- events; it will require this UUID to be supplied. An external uuid might
|
||||
-- indicate "my house security system's zone 23".
|
||||
source_uuid blob not null check (length(source_uuid) = 16),
|
||||
|
||||
-- a uuid describing the type of event. A registry (TBD) will list built-in
|
||||
-- supported types, such as "Hikvision on-camera motion detection", or
|
||||
-- "ONVIF on-camera motion detection". External programs can use their own
|
||||
-- uuids, such as "Elk security system watcher".
|
||||
type_uuid blob not null check (length(type_uuid) = 16),
|
||||
|
||||
-- a short human-readable description of the event to use in mouseovers or event
|
||||
-- lists, such as "driveway motion" or "front door open".
|
||||
short_name not null,
|
||||
|
||||
unique (source_uuid, type_uuid)
|
||||
);
|
||||
|
||||
-- e.g. "moving/still", "disarmed/away/stay", etc.
|
||||
-- TODO: just do a protobuf for each type? might be simpler, more flexible.
|
||||
create table signal_type_enum (
|
||||
type_uuid blob not null check (length(type_uuid) = 16),
|
||||
value integer not null check (value > 0 and value < 16),
|
||||
name text not null,
|
||||
|
||||
-- true/1 iff this signal value should be considered "motion" for directly associated cameras.
|
||||
motion int not null check (motion in (0, 1)) default 0,
|
||||
|
||||
color text
|
||||
);
|
||||
|
||||
-- Associations between event sources and cameras.
|
||||
-- For example, if two cameras have overlapping fields of view, they might be
|
||||
-- configured such that each camera is associated with both its own motion and
|
||||
-- the other camera's motion.
|
||||
create table signal_camera (
|
||||
signal_id integer references signal (id),
|
||||
camera_id integer references camera (id),
|
||||
|
||||
-- type:
|
||||
--
|
||||
-- 0 means direct association, as if the event source if the camera's own
|
||||
-- motion detection. Here are a couple ways this could be used:
|
||||
--
|
||||
-- * when viewing the camera, hotkeys to go to the start of the next or
|
||||
-- previous event should respect this event.
|
||||
-- * a list of events might include the recordings associated with the
|
||||
-- camera in the same timespan.
|
||||
--
|
||||
-- 1 means indirect association. A screen associated with the camera should
|
||||
-- given some indication of this event, but there should be no assumption
|
||||
-- that the camera will have a direct view of the event. For example, all
|
||||
-- cameras might be indirectly associated with a doorknob press. Cameras at
|
||||
-- the back of the house shouldn't be expected to have a direct view of this
|
||||
-- event, but motion events shortly afterward might warrant extra scrutiny.
|
||||
type integer not null,
|
||||
|
||||
primary key (signal_id, camera_id)
|
||||
) without rowid;
|
||||
|
||||
-- Changes to signals as of a given timestamp.
|
||||
create table signal_change (
|
||||
-- Event time, in 90 kHz units since 1970-01-01 00:00:00Z excluding leap seconds.
|
||||
time_90k integer primary key,
|
||||
|
||||
-- Changes at this timestamp.
|
||||
--
|
||||
-- A blob of varints representing a list of
|
||||
-- (signal number - next allowed, state) pairs, where signal number is
|
||||
-- non-decreasing. For example,
|
||||
-- input signals: 1 3 200 (must be sorted)
|
||||
-- delta: 1 1 196 (must be non-negative)
|
||||
-- states: 1 1 2
|
||||
-- varint: \x01 \x01 \x01 \x01 \xc4 \x01 \x02
|
||||
changes blob not null
|
||||
);
|
||||
|
||||
insert into version (id, unix_time, notes)
|
||||
values (5, cast(strftime('%s', 'now') as int), 'db creation');
|
133
db/upgrade/v5_to_v6.rs
Normal file
133
db/upgrade/v5_to_v6.rs
Normal file
@ -0,0 +1,133 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// In addition, as a special exception, the copyright holders give
|
||||
// permission to link the code of portions of this program with the
|
||||
// OpenSSL library under certain conditions as described in each
|
||||
// individual source file, and distribute linked combinations including
|
||||
// the two.
|
||||
//
|
||||
// You must obey the GNU General Public License in all respects for all
|
||||
// of the code used other than OpenSSL. If you modify file(s) with this
|
||||
// exception, you may extend this exception to your version of the
|
||||
// file(s), but you are not obligated to do so. If you do not wish to do
|
||||
// so, delete this exception statement from your version. If you delete
|
||||
// this exception statement from all source files in the program, then
|
||||
// also delete it here.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/// Upgrades a version 4 schema to a version 5 schema.
|
||||
|
||||
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
||||
use failure::{Error, bail, format_err};
|
||||
use h264_reader::avcc::AvcDecoderConfigurationRecord;
|
||||
use rusqlite::{named_params, params};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
// Copied from src/h264.rs. h264 stuff really doesn't belong in the db crate, but we do what we
|
||||
// must for schema upgrades.
|
||||
const PIXEL_ASPECT_RATIOS: [((u16, u16), (u16, u16)); 4] = [
|
||||
((320, 240), ( 4, 3)),
|
||||
((352, 240), (40, 33)),
|
||||
((640, 480), ( 4, 3)),
|
||||
((704, 480), (40, 33)),
|
||||
];
|
||||
fn default_pixel_aspect_ratio(width: u16, height: u16) -> (u16, u16) {
|
||||
let dims = (width, height);
|
||||
for r in &PIXEL_ASPECT_RATIOS {
|
||||
if r.0 == dims {
|
||||
return r.1;
|
||||
}
|
||||
}
|
||||
(1, 1)
|
||||
}
|
||||
|
||||
fn parse(data: &[u8]) -> Result<AvcDecoderConfigurationRecord, Error> {
|
||||
if data.len() < 94 || &data[4..8] != b"avc1" || &data[90..94] != b"avcC" {
|
||||
bail!("data of len {} doesn't have an avcC", data.len());
|
||||
}
|
||||
let avcc_len = BigEndian::read_u32(&data[86..90]);
|
||||
if avcc_len < 8 { // length and type.
|
||||
bail!("invalid avcc len {}", avcc_len);
|
||||
}
|
||||
let end_pos = 86 + usize::try_from(avcc_len)?;
|
||||
if end_pos != data.len() {
|
||||
bail!("expected avcC to be end of extradata; there are {} more bytes.",
|
||||
data.len() - end_pos);
|
||||
}
|
||||
AvcDecoderConfigurationRecord::try_from(&data[94..end_pos])
|
||||
.map_err(|e| format_err!("Bad AvcDecoderConfigurationRecord: {:?}", e))
|
||||
}
|
||||
|
||||
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
||||
// These create statements match the schema.sql when version 5 was the latest.
|
||||
tx.execute_batch(r#"
|
||||
alter table video_sample_entry add column pasp_h_spacing integer not null default 1 check (pasp_h_spacing > 0);
|
||||
alter table video_sample_entry add column pasp_v_spacing integer not null default 1 check (pasp_v_spacing > 0);
|
||||
"#)?;
|
||||
|
||||
let mut update = tx.prepare(r#"
|
||||
update video_sample_entry
|
||||
set data = :data,
|
||||
sha1 = :sha1,
|
||||
pasp_h_spacing = :pasp_h_spacing,
|
||||
pasp_v_spacing = :pasp_v_spacing
|
||||
where id = :id
|
||||
"#)?;
|
||||
let mut stmt = tx.prepare(r#"
|
||||
select
|
||||
id,
|
||||
width,
|
||||
height,
|
||||
data
|
||||
from
|
||||
video_sample_entry
|
||||
"#)?;
|
||||
let mut rows = stmt.query(params![])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
let id: i32 = row.get(0)?;
|
||||
let width: u16 = row.get::<_, i32>(1)?.try_into()?;
|
||||
let height: u16 = row.get::<_, i32>(2)?.try_into()?;
|
||||
let mut data: Vec<u8> = row.get(3)?;
|
||||
let avcc = parse(&data)?;
|
||||
if avcc.num_of_sequence_parameter_sets() != 1 {
|
||||
bail!("Multiple SPSs!");
|
||||
}
|
||||
let ctx = avcc.create_context(())
|
||||
.map_err(|e| format_err!("Can't load SPS+PPS: {:?}", e))?;
|
||||
let sps = ctx.sps_by_id(h264_reader::nal::pps::ParamSetId::from_u32(0).unwrap())
|
||||
.ok_or_else(|| format_err!("No SPS 0"))?;
|
||||
let pasp = sps.vui_parameters.as_ref()
|
||||
.and_then(|v| v.aspect_ratio_info.as_ref())
|
||||
.and_then(|a| a.clone().get())
|
||||
.unwrap_or_else(|| default_pixel_aspect_ratio(width, height));
|
||||
if pasp != (1, 1) {
|
||||
data.extend_from_slice(b"\x00\x00\x00\x10pasp"); // length + box name
|
||||
data.write_u32::<BigEndian>(pasp.0.into())?;
|
||||
data.write_u32::<BigEndian>(pasp.1.into())?;
|
||||
let len = data.len();
|
||||
BigEndian::write_u32(&mut data[0..4], u32::try_from(len)?);
|
||||
}
|
||||
|
||||
update.execute_named(named_params!{
|
||||
":id": id,
|
||||
":data": &data,
|
||||
":sha1": &crate::sha1(&data)?[..],
|
||||
":pasp_h_spacing": pasp.0,
|
||||
":pasp_v_spacing": pasp.1,
|
||||
})?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
44
db/writer.rs
44
db/writer.rs
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2018 The Moonfire NVR Authors
|
||||
// Copyright (C) 2018-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -847,7 +847,7 @@ impl<'a, C: Clocks + Clone, D: DirWriter> Drop for Writer<'a, C, D> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use base::clock::{Clocks, SimulatedClocks};
|
||||
use crate::db::{self, CompositeId};
|
||||
use crate::db::{self, CompositeId, VideoSampleEntryToInsert};
|
||||
use crate::recording;
|
||||
use parking_lot::Mutex;
|
||||
use log::{trace, warn};
|
||||
@ -1000,8 +1000,14 @@ mod tests {
|
||||
}]).unwrap();
|
||||
|
||||
// Setup: add a 3-byte recording.
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let mut w = Writer::new(&h.dir, &h.db, &h.channel, testutil::TEST_STREAM_ID,
|
||||
video_sample_entry_id);
|
||||
let f = MockFile::new();
|
||||
@ -1083,8 +1089,14 @@ mod tests {
|
||||
fn write_path_retries() {
|
||||
testutil::init();
|
||||
let mut h = new_harness(0);
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let mut w = Writer::new(&h.dir, &h.db, &h.channel, testutil::TEST_STREAM_ID,
|
||||
video_sample_entry_id);
|
||||
h.dir.expect(MockDirAction::Create(CompositeId::new(1, 1), Box::new(|_id| Err(nix_eio()))));
|
||||
@ -1147,8 +1159,14 @@ mod tests {
|
||||
}]).unwrap();
|
||||
|
||||
// Setup: add a 3-byte recording.
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let mut w = Writer::new(&h.dir, &h.db, &h.channel, testutil::TEST_STREAM_ID,
|
||||
video_sample_entry_id);
|
||||
let f = MockFile::new();
|
||||
@ -1238,8 +1256,14 @@ mod tests {
|
||||
h.db.clocks().sleep(time::Duration::seconds(1));
|
||||
|
||||
// Setup: add a 3-byte recording.
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(
|
||||
1920, 1080, [0u8; 100].to_vec(), "avc1.000000".to_owned()).unwrap();
|
||||
let video_sample_entry_id = h.db.lock().insert_video_sample_entry(VideoSampleEntryToInsert {
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
pasp_h_spacing: 1,
|
||||
pasp_v_spacing: 1,
|
||||
data: [0u8; 100].to_vec(),
|
||||
rfc6381_codec: "avc1.000000".to_owned(),
|
||||
}).unwrap();
|
||||
let mut w = Writer::new(&h.dir, &h.db, &h.channel, testutil::TEST_STREAM_ID,
|
||||
video_sample_entry_id);
|
||||
let f1 = MockFile::new();
|
||||
|
@ -195,7 +195,7 @@ The general upgrade procedure applies to this upgrade.
|
||||
### Version 1 to version 2 to version 3
|
||||
|
||||
This upgrade affects the sample file directory as well as the database. Thus,
|
||||
the restore procedure written above of simply copying back the databae is
|
||||
the restore procedure written above of simply copying back the database is
|
||||
insufficient. To do a full restore, you would need to back up and restore the
|
||||
sample file directory as well. This directory is considerably larger, so
|
||||
consider an alternate procedure of crossing your fingers, and being prepared
|
||||
@ -225,10 +225,10 @@ Version 3 adds over version 1:
|
||||
|
||||
### Version 3 to version 4 to version 5
|
||||
|
||||
This upgrade affects only the SQLite database.
|
||||
This upgrade affects the SQLite database and the sample file directory's
|
||||
`meta` files.
|
||||
|
||||
Version 4 represents a half-finished upgrade from version 3 to version 5; it
|
||||
is never used.
|
||||
Version 4 represents a half-finished upgrade from version 3 to version 5.
|
||||
|
||||
Version 5 adds over version 3:
|
||||
|
||||
@ -240,3 +240,16 @@ Version 5 adds over version 3:
|
||||
the `moonfire-nvr config` subcommand.
|
||||
* the ability to recover from a completely full sample file directory (#65)
|
||||
without manual intervention.
|
||||
|
||||
### Version 6 (under development on the `new-schema` branch)
|
||||
|
||||
This upgrade affects only the SQLite database.
|
||||
|
||||
Version 6 adds over version 5:
|
||||
|
||||
* metadata about the pixel aspect ratio to properly support
|
||||
[anamorphic](https://en.wikipedia.org/wiki/Anamorphic_widescreen) "sub"
|
||||
streams.
|
||||
|
||||
Before it is finalized, it likely will also add a schema for [object
|
||||
detection](https://en.wikipedia.org/wiki/Object_detection).
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2017 The Moonfire NVR Authors
|
||||
// Copyright (C) 2017-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -108,7 +108,7 @@ fn press_test_inner(url: &Url) -> Result<String, Error> {
|
||||
redacted_url: url.as_str(), // don't need redaction in config UI.
|
||||
})?;
|
||||
let extra_data = stream.get_extra_data()?;
|
||||
Ok(format!("{}x{} video stream", extra_data.width, extra_data.height))
|
||||
Ok(format!("{}x{} video stream", extra_data.entry.width, extra_data.entry.height))
|
||||
}
|
||||
|
||||
fn press_test(siv: &mut Cursive, t: db::StreamType) {
|
||||
|
178
src/h264.rs
178
src/h264.rs
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -40,10 +40,11 @@
|
||||
//! through ffmpeg's own generated `.mp4` file. Extracting just this part of their `.mp4` files
|
||||
//! would be more trouble than it's worth.
|
||||
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use failure::{Error, bail};
|
||||
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
||||
use failure::{Error, bail, format_err};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::bytes::Regex;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
// See ISO/IEC 14496-10 table 7-1 - NAL unit type codes, syntax element categories, and NAL unit
|
||||
// type classes.
|
||||
@ -52,9 +53,39 @@ const NAL_UNIT_PIC_PARAMETER_SET: u8 = 8;
|
||||
|
||||
const NAL_UNIT_TYPE_MASK: u8 = 0x1F; // bottom 5 bits of first byte of unit.
|
||||
|
||||
// For certain common sub stream anamorphic resolutions, add a pixel aspect ratio box.
|
||||
const PIXEL_ASPECT_RATIOS: [((u16, u16), (u16, u16)); 4] = [
|
||||
((320, 240), ( 4, 3)),
|
||||
((352, 240), (40, 33)),
|
||||
((640, 480), ( 4, 3)),
|
||||
((704, 480), (40, 33)),
|
||||
];
|
||||
|
||||
/// Get the pixel aspect ratio to use if none is specified.
|
||||
///
|
||||
/// The Dahua IPC-HDW5231R-Z sets the aspect ratio in the H.264 SPS (correctly) for both square and
|
||||
/// non-square pixels. The Hikvision DS-2CD2032-I doesn't set it, even though the sub stream's
|
||||
/// pixels aren't square. So define a default based on the pixel dimensions to use if the camera
|
||||
/// doesn't tell us what to do.
|
||||
///
|
||||
/// Note that at least in the case of .mp4 muxing, we don't need to fix up the underlying SPS.
|
||||
/// SPS; PixelAspectRatioBox's definition says that it overrides the H.264-level declaration.
|
||||
fn default_pixel_aspect_ratio(width: u16, height: u16) -> (u16, u16) {
|
||||
let dims = (width, height);
|
||||
for r in &PIXEL_ASPECT_RATIOS {
|
||||
if r.0 == dims {
|
||||
return r.1;
|
||||
}
|
||||
}
|
||||
(1, 1)
|
||||
}
|
||||
|
||||
/// Decodes a H.264 Annex B byte stream into NAL units. Calls `f` for each NAL unit in the byte
|
||||
/// stream. Aborts if `f` returns error.
|
||||
///
|
||||
/// Note `f` is called with the encoded NAL form, not the RBSP. The NAL header byte and any
|
||||
/// emulation prevention bytes will be present.
|
||||
///
|
||||
/// See ISO/IEC 14496-10 section B.2: Byte stream NAL unit decoding process.
|
||||
/// This is a relatively simple, unoptimized implementation.
|
||||
///
|
||||
@ -92,13 +123,34 @@ fn parse_annex_b_extra_data(data: &[u8]) -> Result<(&[u8], &[u8]), Error> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a NAL unit (minus header byte) into its RBSP.
|
||||
/// Stolen from h264-reader's src/avcc.rs. This shouldn't last long, see:
|
||||
/// <https://github.com/dholroyd/h264-reader/issues/4>.
|
||||
fn decode(encoded: &[u8]) -> Vec<u8> {
|
||||
struct NalRead(Vec<u8>);
|
||||
use h264_reader::Context;
|
||||
use h264_reader::nal::NalHandler;
|
||||
impl NalHandler for NalRead {
|
||||
type Ctx = ();
|
||||
fn start(&mut self, _ctx: &mut Context<Self::Ctx>, _header: h264_reader::nal::NalHeader) {}
|
||||
|
||||
fn push(&mut self, _ctx: &mut Context<Self::Ctx>, buf: &[u8]) {
|
||||
self.0.extend_from_slice(buf)
|
||||
}
|
||||
|
||||
fn end(&mut self, _ctx: &mut Context<Self::Ctx>) {}
|
||||
}
|
||||
let mut decode = h264_reader::rbsp::RbspDecoder::new(NalRead(vec![]));
|
||||
let mut ctx = Context::new(());
|
||||
decode.push(&mut ctx, encoded);
|
||||
let read = decode.into_handler();
|
||||
read.0
|
||||
}
|
||||
|
||||
/// Parsed representation of ffmpeg's "extradata".
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct ExtraData {
|
||||
pub sample_entry: Vec<u8>,
|
||||
pub rfc6381_codec: String,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub entry: db::VideoSampleEntryToInsert,
|
||||
|
||||
/// True iff sample data should be transformed from Annex B format to AVC format via a call to
|
||||
/// `transform_sample_data`. (The assumption is that if the extra data was in Annex B format,
|
||||
@ -109,38 +161,44 @@ pub struct ExtraData {
|
||||
impl ExtraData {
|
||||
/// Parses "extradata" from ffmpeg. This data may be in either Annex B format or AVC format.
|
||||
pub fn parse(extradata: &[u8], width: u16, height: u16) -> Result<ExtraData, Error> {
|
||||
let mut sps_and_pps = None;
|
||||
let raw_sps_and_pps;
|
||||
let need_transform;
|
||||
let avcc_len = if extradata.starts_with(b"\x00\x00\x00\x01") ||
|
||||
let ctx;
|
||||
let sps_owner;
|
||||
let sps; // reference to either within ctx or to sps_owner.
|
||||
if extradata.starts_with(b"\x00\x00\x00\x01") ||
|
||||
extradata.starts_with(b"\x00\x00\x01") {
|
||||
// ffmpeg supplied "extradata" in Annex B format.
|
||||
let (s, p) = parse_annex_b_extra_data(extradata)?;
|
||||
sps_and_pps = Some((s, p));
|
||||
let rbsp = decode(&s[1..]);
|
||||
sps_owner = h264_reader::nal::sps::SeqParameterSet::from_bytes(&rbsp)
|
||||
.map_err(|e| format_err!("Bad SPS: {:?}", e))?;
|
||||
sps = &sps_owner;
|
||||
raw_sps_and_pps = Some((s, p));
|
||||
need_transform = true;
|
||||
|
||||
// This magic value is checked at the end of the function;
|
||||
// unit tests confirm its accuracy.
|
||||
19 + s.len() + p.len()
|
||||
} else {
|
||||
// Assume "extradata" holds an AVCDecoderConfiguration.
|
||||
need_transform = false;
|
||||
8 + extradata.len()
|
||||
raw_sps_and_pps = None;
|
||||
let avcc = h264_reader::avcc::AvcDecoderConfigurationRecord::try_from(extradata)
|
||||
.map_err(|e| format_err!("Bad AvcDecoderConfigurationRecord: {:?}", e))?;
|
||||
if avcc.num_of_sequence_parameter_sets() != 1 {
|
||||
bail!("Multiple SPSs!");
|
||||
}
|
||||
ctx = avcc.create_context(())
|
||||
.map_err(|e| format_err!("Can't load SPS+PPS: {:?}", e))?;
|
||||
sps = ctx.sps_by_id(h264_reader::nal::pps::ParamSetId::from_u32(0).unwrap())
|
||||
.ok_or_else(|| format_err!("No SPS 0"))?;
|
||||
};
|
||||
let sps_and_pps = sps_and_pps;
|
||||
let need_transform = need_transform;
|
||||
|
||||
// This magic value is also checked at the end.
|
||||
let avc1_len = 86 + avcc_len;
|
||||
|
||||
let mut sample_entry = Vec::with_capacity(avc1_len);
|
||||
let mut sample_entry = Vec::with_capacity(256);
|
||||
|
||||
// This is a concatenation of the following boxes/classes.
|
||||
|
||||
// SampleEntry, ISO/IEC 14496-12 section 8.5.2.
|
||||
let avc1_len_pos = sample_entry.len();
|
||||
sample_entry.write_u32::<BigEndian>(avc1_len as u32)?; // length
|
||||
// type + reserved + data_reference_index = 1
|
||||
sample_entry.extend_from_slice(b"avc1\x00\x00\x00\x00\x00\x00\x00\x01");
|
||||
// length placeholder + type + reserved + data_reference_index = 1
|
||||
sample_entry.extend_from_slice(b"\x00\x00\x00\x00avc1\x00\x00\x00\x00\x00\x00\x00\x01");
|
||||
|
||||
// VisualSampleEntry, ISO/IEC 14496-12 section 12.1.3.
|
||||
sample_entry.extend_from_slice(&[0; 16]); // pre-defined + reserved
|
||||
@ -165,12 +223,9 @@ impl ExtraData {
|
||||
// AVCSampleEntry, ISO/IEC 14496-15 section 5.3.4.1.
|
||||
// AVCConfigurationBox, ISO/IEC 14496-15 section 5.3.4.1.
|
||||
let avcc_len_pos = sample_entry.len();
|
||||
sample_entry.write_u32::<BigEndian>(avcc_len as u32)?; // length
|
||||
sample_entry.extend_from_slice(b"avcC");
|
||||
|
||||
let avc_decoder_config_len = if let Some((sps, pps)) = sps_and_pps {
|
||||
let before = sample_entry.len();
|
||||
sample_entry.extend_from_slice(b"\x00\x00\x00\x00avcC");
|
||||
|
||||
if let Some((sps, pps)) = raw_sps_and_pps {
|
||||
// Create the AVCDecoderConfiguration, ISO/IEC 14496-15 section 5.2.4.1.
|
||||
// The beginning of the AVCDecoderConfiguration takes a few values from
|
||||
// the SPS (ISO/IEC 14496-10 section 7.3.2.1.1). One caveat: that section
|
||||
@ -192,37 +247,52 @@ impl ExtraData {
|
||||
// ffmpeg's ff_isom_write_avcc has the same limitation, so it's probably
|
||||
// fine. This next byte is a reserved 0b111 + a 5-bit # of SPSs (1).
|
||||
sample_entry.push(0xe1);
|
||||
sample_entry.write_u16::<BigEndian>(sps.len() as u16)?;
|
||||
sample_entry.write_u16::<BigEndian>(u16::try_from(sps.len())?)?;
|
||||
sample_entry.extend_from_slice(sps);
|
||||
sample_entry.push(1); // # of PPSs.
|
||||
sample_entry.write_u16::<BigEndian>(pps.len() as u16)?;
|
||||
sample_entry.write_u16::<BigEndian>(u16::try_from(pps.len())?)?;
|
||||
sample_entry.extend_from_slice(pps);
|
||||
|
||||
if sample_entry.len() - avcc_len_pos != avcc_len {
|
||||
bail!("internal error: anticipated AVCConfigurationBox \
|
||||
length {}, but was actually {}; sps length {}, pps length {}",
|
||||
avcc_len, sample_entry.len() - avcc_len_pos, sps.len(), pps.len());
|
||||
}
|
||||
sample_entry.len() - before
|
||||
} else {
|
||||
sample_entry.extend_from_slice(extradata);
|
||||
extradata.len()
|
||||
};
|
||||
|
||||
if sample_entry.len() - avc1_len_pos != avc1_len {
|
||||
bail!("internal error: anticipated AVCSampleEntry length \
|
||||
{}, but was actually {}; AVCDecoderConfiguration length {}",
|
||||
avc1_len, sample_entry.len() - avc1_len_pos, avc_decoder_config_len);
|
||||
// Fix up avc1 and avcC box lengths.
|
||||
let cur_pos = sample_entry.len();
|
||||
BigEndian::write_u32(&mut sample_entry[avcc_len_pos .. avcc_len_pos + 4],
|
||||
u32::try_from(cur_pos - avcc_len_pos)?);
|
||||
|
||||
// PixelAspectRatioBox, ISO/IEC 14496-12 section 12.1.4.2.
|
||||
// Write a PixelAspectRatioBox if necessary, as the sub streams can be be anamorphic.
|
||||
let pasp = sps.vui_parameters.as_ref()
|
||||
.and_then(|v| v.aspect_ratio_info.as_ref())
|
||||
.and_then(|a| a.clone().get())
|
||||
.unwrap_or_else(|| default_pixel_aspect_ratio(width, height));
|
||||
if pasp != (1, 1) {
|
||||
sample_entry.extend_from_slice(b"\x00\x00\x00\x10pasp"); // length + box name
|
||||
sample_entry.write_u32::<BigEndian>(pasp.0.into())?;
|
||||
sample_entry.write_u32::<BigEndian>(pasp.1.into())?;
|
||||
}
|
||||
|
||||
let cur_pos = sample_entry.len();
|
||||
BigEndian::write_u32(&mut sample_entry[avc1_len_pos .. avc1_len_pos + 4],
|
||||
u32::try_from(cur_pos - avc1_len_pos)?);
|
||||
|
||||
let profile_idc = sample_entry[103];
|
||||
let constraint_flags = sample_entry[104];
|
||||
let level_idc = sample_entry[105];
|
||||
let codec = format!("avc1.{:02x}{:02x}{:02x}", profile_idc, constraint_flags, level_idc);
|
||||
|
||||
let rfc6381_codec =
|
||||
format!("avc1.{:02x}{:02x}{:02x}", profile_idc, constraint_flags, level_idc);
|
||||
Ok(ExtraData {
|
||||
sample_entry,
|
||||
rfc6381_codec: codec,
|
||||
width,
|
||||
height,
|
||||
entry: db::VideoSampleEntryToInsert {
|
||||
data: sample_entry,
|
||||
rfc6381_codec,
|
||||
width,
|
||||
height,
|
||||
pasp_h_spacing: pasp.0,
|
||||
pasp_v_spacing: pasp.1,
|
||||
},
|
||||
need_transform,
|
||||
})
|
||||
}
|
||||
@ -303,21 +373,21 @@ mod tests {
|
||||
fn test_sample_entry_from_avc_decoder_config() {
|
||||
testutil::init();
|
||||
let e = super::ExtraData::parse(&AVC_DECODER_CONFIG_TEST_INPUT, 1280, 720).unwrap();
|
||||
assert_eq!(&e.sample_entry[..], &TEST_OUTPUT[..]);
|
||||
assert_eq!(e.width, 1280);
|
||||
assert_eq!(e.height, 720);
|
||||
assert_eq!(&e.entry.data[..], &TEST_OUTPUT[..]);
|
||||
assert_eq!(e.entry.width, 1280);
|
||||
assert_eq!(e.entry.height, 720);
|
||||
assert_eq!(e.entry.rfc6381_codec, "avc1.4d001f");
|
||||
assert_eq!(e.need_transform, false);
|
||||
assert_eq!(e.rfc6381_codec, "avc1.4d001f");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample_entry_from_annex_b() {
|
||||
testutil::init();
|
||||
let e = super::ExtraData::parse(&ANNEX_B_TEST_INPUT, 1280, 720).unwrap();
|
||||
assert_eq!(e.width, 1280);
|
||||
assert_eq!(e.height, 720);
|
||||
assert_eq!(e.entry.width, 1280);
|
||||
assert_eq!(e.entry.height, 720);
|
||||
assert_eq!(e.entry.rfc6381_codec, "avc1.4d001f");
|
||||
assert_eq!(e.need_transform, true);
|
||||
assert_eq!(e.rfc6381_codec, "avc1.4d001f");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -426,6 +426,8 @@ pub struct VideoSampleEntry {
|
||||
pub sha1: String,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub pasp_h_spacing: u16,
|
||||
pub pasp_v_spacing: u16,
|
||||
}
|
||||
|
||||
impl VideoSampleEntry {
|
||||
@ -434,6 +436,8 @@ impl VideoSampleEntry {
|
||||
sha1: base::strutil::hex(&e.sha1),
|
||||
width: e.width,
|
||||
height: e.height,
|
||||
pasp_h_spacing: e.pasp_h_spacing,
|
||||
pasp_v_spacing: e.pasp_v_spacing,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
25
src/mp4.rs
25
src/mp4.rs
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -107,7 +107,7 @@ use std::time::SystemTime;
|
||||
/// This value should be incremented any time a change is made to this file that causes different
|
||||
/// bytes to be output for a particular set of `FileBuilder` options. Incrementing this value will
|
||||
/// cause the etag to change as well.
|
||||
const FORMAT_VERSION: [u8; 1] = [0x06];
|
||||
const FORMAT_VERSION: [u8; 1] = [0x07];
|
||||
|
||||
/// An `ftyp` (ISO/IEC 14496-12 section 4.3 `FileType`) box.
|
||||
const NORMAL_FTYP_BOX: &'static [u8] = &[
|
||||
@ -1827,9 +1827,8 @@ mod tests {
|
||||
// 2015-04-26 00:00:00 UTC.
|
||||
const START_TIME: recording::Time = recording::Time(1430006400i64 * TIME_UNITS_PER_SEC);
|
||||
let extra_data = input.get_extra_data().unwrap();
|
||||
let video_sample_entry_id = db.db.lock().insert_video_sample_entry(
|
||||
extra_data.width, extra_data.height, extra_data.sample_entry,
|
||||
extra_data.rfc6381_codec).unwrap();
|
||||
let video_sample_entry_id =
|
||||
db.db.lock().insert_video_sample_entry(extra_data.entry).unwrap();
|
||||
let dir = db.dirs_by_stream_id.get(&TEST_STREAM_ID).unwrap();
|
||||
let mut output = writer::Writer::new(dir, &db.db, &db.syncer_channel, TEST_STREAM_ID,
|
||||
video_sample_entry_id);
|
||||
@ -2195,8 +2194,8 @@ mod tests {
|
||||
// here fails, it can be updated, but the etag must change as well! Otherwise clients may
|
||||
// combine ranges from the new format with ranges from the old format.
|
||||
let sha1 = digest(&mp4).await;
|
||||
assert_eq!("17376879bcf872dd4ad1197225a32d5473fb0dc6", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"953dcf1a61debe785d5dec3ae2d3992a819b68ae\"";
|
||||
assert_eq!("2ea2cb354503b9d50d028af00bddcd23d6651f28", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"7b55d0bd4370712bf1a7549f6383ca51b1eb97e9\"";
|
||||
assert_eq!(Some(HeaderValue::from_str(EXPECTED_ETAG).unwrap()), mp4.etag());
|
||||
drop(db.syncer_channel);
|
||||
db.db.lock().clear_on_flush();
|
||||
@ -2216,8 +2215,8 @@ mod tests {
|
||||
// here fails, it can be updated, but the etag must change as well! Otherwise clients may
|
||||
// combine ranges from the new format with ranges from the old format.
|
||||
let sha1 = digest(&mp4).await;
|
||||
assert_eq!("1cd90e0b49747cc54c953153d6709f2fb5df6b14", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"736655313f10747528a663190517620cdffea6d0\"";
|
||||
assert_eq!("ec79a2d2362b3ae9dec18762c78c8c60932b4ff0", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"f17085373bbee7d2ffc99046575a1ef28f8134e0\"";
|
||||
assert_eq!(Some(HeaderValue::from_str(EXPECTED_ETAG).unwrap()), mp4.etag());
|
||||
drop(db.syncer_channel);
|
||||
db.db.lock().clear_on_flush();
|
||||
@ -2237,8 +2236,8 @@ mod tests {
|
||||
// here fails, it can be updated, but the etag must change as well! Otherwise clients may
|
||||
// combine ranges from the new format with ranges from the old format.
|
||||
let sha1 = digest(&mp4).await;
|
||||
assert_eq!("49893e3997da6bc625a04b09abf4b1ddbe0bc85d", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"e87ed99dea31b7c4d1e9186045abaf5ac3c2d2f8\"";
|
||||
assert_eq!("26e5989211456a0de493e146e2cda7a89a3b485e", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"c48b2819f74b090d89c27fa615ab34e445a4b322\"";
|
||||
assert_eq!(Some(HeaderValue::from_str(EXPECTED_ETAG).unwrap()), mp4.etag());
|
||||
drop(db.syncer_channel);
|
||||
db.db.lock().clear_on_flush();
|
||||
@ -2258,8 +2257,8 @@ mod tests {
|
||||
// here fails, it can be updated, but the etag must change as well! Otherwise clients may
|
||||
// combine ranges from the new format with ranges from the old format.
|
||||
let sha1 = digest(&mp4).await;
|
||||
assert_eq!("0615feaa3c50a7889fb0e6842de3bd3d3143bc78", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"6f0d21a6027b0e444f404a68527dbf5c9a5c1a26\"";
|
||||
assert_eq!("d182fb5c9402ec863527b22526e152dccba82c4a", strutil::hex(&sha1[..]));
|
||||
const EXPECTED_ETAG: &'static str = "\"48da7c8f9c15c318ef91ae00148356b3247b671f\"";
|
||||
assert_eq!(Some(HeaderValue::from_str(EXPECTED_ETAG).unwrap()), mp4.etag());
|
||||
drop(db.syncer_channel);
|
||||
db.db.lock().clear_on_flush();
|
||||
|
@ -1,5 +1,5 @@
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2016 The Moonfire NVR Authors
|
||||
// Copyright (C) 2016-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -122,9 +122,7 @@ impl<'a, C, S> Streamer<'a, C, S> where C: 'a + Clocks + Clone, S: 'a + stream::
|
||||
let extra_data = stream.get_extra_data()?;
|
||||
let video_sample_entry_id = {
|
||||
let _t = TimerGuard::new(&clocks, || "inserting video sample entry");
|
||||
self.db.lock().insert_video_sample_entry(extra_data.width, extra_data.height,
|
||||
extra_data.sample_entry,
|
||||
extra_data.rfc6381_codec)?
|
||||
self.db.lock().insert_video_sample_entry(extra_data.entry)?
|
||||
};
|
||||
debug!("{}: video_sample_entry_id={}", self.short_name, video_sample_entry_id);
|
||||
let mut seen_key_frame = false;
|
||||
|
@ -1,7 +1,7 @@
|
||||
// vim: set et sw=2 ts=2:
|
||||
//
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2018 The Moonfire NVR Authors
|
||||
// Copyright (C) 2018-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -143,9 +143,16 @@ function onSelectVideo(nvrSettingsView, camera, streamType, range, recording) {
|
||||
);
|
||||
const videoTitle =
|
||||
camera.shortName + ', ' + formattedStart + ' to ' + formattedEnd;
|
||||
let width = recording.videoSampleEntryWidth *
|
||||
recording.videoSampleEntryPaspHSpacing /
|
||||
recording.videoSampleEntryPaspVSpacing;
|
||||
const maxWidth = window.innerWidth * 3 / 4;
|
||||
while (width > maxWidth) {
|
||||
width /= 2;
|
||||
}
|
||||
new VideoDialogView()
|
||||
.attach($('body'))
|
||||
.play(videoTitle, recording.videoSampleEntryWidth / 4, url);
|
||||
.play(videoTitle, width, url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,7 +1,7 @@
|
||||
// vim: set et sw=2 ts=2:
|
||||
//
|
||||
// This file is part of Moonfire NVR, a security camera network video recorder.
|
||||
// Copyright (C) 2018 The Moonfire NVR Authors
|
||||
// Copyright (C) 2018-2020 The Moonfire NVR Authors
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
@ -79,6 +79,12 @@ export default class Recording {
|
||||
|
||||
/** @const {!number} */
|
||||
this.videoSampleEntryHeight = videoSampleEntryJson.height;
|
||||
|
||||
/** @const {!number} */
|
||||
this.videoSampleEntryPaspHSpacing = videoSampleEntryJson.paspHSpacing;
|
||||
|
||||
/** @const {!number} */
|
||||
this.videoSampleEntryPaspVSpacing = videoSampleEntryJson.paspVSpacing;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user