2020-03-02 01:53:41 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2016 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2016-12-21 01:08:18 -05:00
|
|
|
|
|
|
|
/// Upgrades a version 0 schema to a version 1 schema.
|
2018-12-28 13:21:49 -05:00
|
|
|
use crate::db;
|
|
|
|
use crate::recording;
|
2023-07-10 01:04:17 -04:00
|
|
|
use base::Error;
|
2021-05-17 13:50:12 -04:00
|
|
|
use rusqlite::{named_params, params};
|
2016-12-21 01:08:18 -05:00
|
|
|
use std::collections::HashMap;
|
2023-02-16 02:14:54 -05:00
|
|
|
use tracing::warn;
|
2016-12-21 01:08:18 -05:00
|
|
|
|
2018-03-01 12:26:03 -05:00
|
|
|
pub fn run(_args: &super::Args, tx: &rusqlite::Transaction) -> Result<(), Error> {
|
|
|
|
// These create statements match the schema.sql when version 1 was the latest.
|
2021-02-17 01:15:54 -05:00
|
|
|
tx.execute_batch(
|
|
|
|
r#"
|
2018-03-01 12:26:03 -05:00
|
|
|
alter table camera rename to old_camera;
|
|
|
|
create table camera (
|
|
|
|
id integer primary key,
|
2019-07-11 11:07:40 -04:00
|
|
|
uuid blob unique not null check (length(uuid) = 16),
|
2018-03-01 12:26:03 -05:00
|
|
|
short_name text not null,
|
|
|
|
description text,
|
|
|
|
host text,
|
|
|
|
username text,
|
|
|
|
password text,
|
|
|
|
main_rtsp_path text,
|
|
|
|
sub_rtsp_path text,
|
|
|
|
retain_bytes integer not null check (retain_bytes >= 0),
|
|
|
|
next_recording_id integer not null check (next_recording_id >= 0)
|
|
|
|
);
|
|
|
|
alter table recording rename to old_recording;
|
|
|
|
drop index recording_cover;
|
|
|
|
create table recording (
|
|
|
|
composite_id integer primary key,
|
|
|
|
camera_id integer not null references camera (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),
|
|
|
|
duration_90k integer not null
|
|
|
|
check (duration_90k >= 0 and duration_90k < 5*60*90000),
|
|
|
|
local_time_delta_90k integer not null,
|
|
|
|
video_samples integer not null check (video_samples > 0),
|
|
|
|
video_sync_samples integer not null check (video_samples > 0),
|
|
|
|
video_sample_entry_id integer references video_sample_entry (id),
|
|
|
|
check (composite_id >> 32 = camera_id)
|
|
|
|
);
|
|
|
|
create index recording_cover on recording (
|
|
|
|
camera_id,
|
|
|
|
start_time_90k,
|
|
|
|
duration_90k,
|
|
|
|
video_samples,
|
|
|
|
video_sync_samples,
|
|
|
|
video_sample_entry_id,
|
|
|
|
sample_file_bytes,
|
|
|
|
run_offset,
|
|
|
|
flags
|
|
|
|
);
|
|
|
|
create table recording_playback (
|
|
|
|
composite_id integer primary key references recording (composite_id),
|
|
|
|
sample_file_uuid blob not null check (length(sample_file_uuid) = 16),
|
|
|
|
sample_file_sha1 blob not null check (length(sample_file_sha1) = 20),
|
|
|
|
video_index blob not null check (length(video_index) > 0)
|
|
|
|
);
|
2019-07-22 01:40:01 -04:00
|
|
|
insert into camera
|
|
|
|
select
|
|
|
|
id,
|
|
|
|
uuid,
|
|
|
|
short_name,
|
|
|
|
description,
|
|
|
|
host,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
main_rtsp_path,
|
|
|
|
sub_rtsp_path,
|
|
|
|
retain_bytes,
|
|
|
|
1 as next_recording_id
|
|
|
|
from
|
|
|
|
old_camera;
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2019-07-22 01:40:01 -04:00
|
|
|
let camera_state = fill_recording(tx)?;
|
|
|
|
update_camera(tx, camera_state)?;
|
2021-02-17 01:15:54 -05:00
|
|
|
tx.execute_batch(
|
|
|
|
r#"
|
2018-03-01 12:26:03 -05:00
|
|
|
drop table old_recording;
|
2019-07-22 01:40:01 -04:00
|
|
|
drop table old_camera;
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2018-03-01 12:26:03 -05:00
|
|
|
Ok(())
|
2016-12-21 01:08:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CameraState {
|
|
|
|
/// tuple of (run_start_id, next_start_90k).
|
|
|
|
current_run: Option<(i64, i64)>,
|
|
|
|
|
|
|
|
/// As in the `next_recording_id` field of the `camera` table.
|
|
|
|
next_recording_id: i32,
|
|
|
|
}
|
|
|
|
|
2017-01-06 23:48:06 -05:00
|
|
|
fn has_trailing_zero(video_index: &[u8]) -> Result<bool, Error> {
|
2021-05-17 17:31:50 -04:00
|
|
|
let mut it = recording::SampleIndexIterator::default();
|
2017-01-06 23:48:06 -05:00
|
|
|
while it.next(video_index)? {}
|
|
|
|
Ok(it.duration_90k == 0)
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:08:18 -05:00
|
|
|
/// Fills the `recording` and `recording_playback` tables from `old_recording`, returning
|
|
|
|
/// the `camera_state` map for use by a following call to `fill_cameras`.
|
|
|
|
fn fill_recording(tx: &rusqlite::Transaction) -> Result<HashMap<i32, CameraState>, Error> {
|
2021-02-17 01:15:54 -05:00
|
|
|
let mut select = tx.prepare(
|
|
|
|
r#"
|
2016-12-21 01:08:18 -05:00
|
|
|
select
|
|
|
|
camera_id,
|
|
|
|
sample_file_bytes,
|
|
|
|
start_time_90k,
|
|
|
|
duration_90k,
|
|
|
|
local_time_delta_90k,
|
|
|
|
video_samples,
|
|
|
|
video_sync_samples,
|
|
|
|
video_sample_entry_id,
|
|
|
|
sample_file_uuid,
|
|
|
|
sample_file_sha1,
|
2017-01-06 23:48:06 -05:00
|
|
|
video_index,
|
|
|
|
id
|
2016-12-21 01:08:18 -05:00
|
|
|
from
|
|
|
|
old_recording
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
let mut insert1 = tx.prepare(
|
|
|
|
r#"
|
2016-12-21 01:08:18 -05:00
|
|
|
insert into recording values (:composite_id, :camera_id, :run_offset, :flags,
|
|
|
|
:sample_file_bytes, :start_time_90k, :duration_90k,
|
|
|
|
:local_time_delta_90k, :video_samples, :video_sync_samples,
|
|
|
|
:video_sample_entry_id)
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
|
|
|
let mut insert2 = tx.prepare(
|
|
|
|
r#"
|
2016-12-21 01:08:18 -05:00
|
|
|
insert into recording_playback values (:composite_id, :sample_file_uuid, :sample_file_sha1,
|
|
|
|
:video_index)
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2019-07-22 01:40:01 -04:00
|
|
|
let mut rows = select.query(params![])?;
|
2016-12-21 01:08:18 -05:00
|
|
|
let mut camera_state: HashMap<i32, CameraState> = HashMap::new();
|
2019-05-31 19:19:04 -04:00
|
|
|
while let Some(row) = rows.next()? {
|
|
|
|
let camera_id: i32 = row.get(0)?;
|
2021-02-17 01:15:54 -05:00
|
|
|
let camera_state = camera_state
|
|
|
|
.entry(camera_id)
|
|
|
|
.or_insert_with(|| CameraState {
|
2016-12-21 01:08:18 -05:00
|
|
|
current_run: None,
|
|
|
|
next_recording_id: 1,
|
2021-02-17 01:15:54 -05:00
|
|
|
});
|
2016-12-21 01:08:18 -05:00
|
|
|
let composite_id = ((camera_id as i64) << 32) | (camera_state.next_recording_id as i64);
|
|
|
|
camera_state.next_recording_id += 1;
|
2019-05-31 19:19:04 -04:00
|
|
|
let sample_file_bytes: i32 = row.get(1)?;
|
|
|
|
let start_time_90k: i64 = row.get(2)?;
|
|
|
|
let duration_90k: i32 = row.get(3)?;
|
|
|
|
let local_time_delta_90k: i64 = row.get(4)?;
|
|
|
|
let video_samples: i32 = row.get(5)?;
|
|
|
|
let video_sync_samples: i32 = row.get(6)?;
|
|
|
|
let video_sample_entry_id: i32 = row.get(7)?;
|
2021-10-26 13:12:19 -04:00
|
|
|
let sample_file_uuid: db::SqlUuid = row.get(8)?;
|
2019-05-31 19:19:04 -04:00
|
|
|
let sample_file_sha1: Vec<u8> = row.get(9)?;
|
|
|
|
let video_index: Vec<u8> = row.get(10)?;
|
|
|
|
let old_id: i32 = row.get(11)?;
|
2017-01-06 23:48:06 -05:00
|
|
|
let trailing_zero = has_trailing_zero(&video_index).unwrap_or_else(|e| {
|
2021-02-17 01:15:54 -05:00
|
|
|
warn!(
|
|
|
|
"recording {}/{} (sample file {}, formerly recording {}) has corrupt \
|
|
|
|
video_index: {}",
|
|
|
|
camera_id,
|
|
|
|
composite_id & 0xFFFF,
|
|
|
|
sample_file_uuid.0,
|
|
|
|
old_id,
|
|
|
|
e
|
|
|
|
);
|
2017-01-06 23:48:06 -05:00
|
|
|
false
|
|
|
|
});
|
2016-12-21 01:08:18 -05:00
|
|
|
let run_id = match camera_state.current_run {
|
|
|
|
Some((run_id, expected_start)) if expected_start == start_time_90k => run_id,
|
|
|
|
_ => composite_id,
|
|
|
|
};
|
2021-05-17 13:50:12 -04:00
|
|
|
insert1.execute(named_params! {
|
|
|
|
":composite_id": &composite_id,
|
|
|
|
":camera_id": &camera_id,
|
|
|
|
":run_offset": &(composite_id - run_id),
|
|
|
|
":flags": &(
|
|
|
|
if trailing_zero {
|
2021-02-17 01:15:54 -05:00
|
|
|
db::RecordingFlags::TrailingZero as i32
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}),
|
2021-05-17 13:50:12 -04:00
|
|
|
":sample_file_bytes": &sample_file_bytes,
|
|
|
|
":start_time_90k": &start_time_90k,
|
|
|
|
":duration_90k": &duration_90k,
|
|
|
|
":local_time_delta_90k": &local_time_delta_90k,
|
|
|
|
":video_samples": &video_samples,
|
|
|
|
":video_sync_samples": &video_sync_samples,
|
|
|
|
":video_sample_entry_id": &video_sample_entry_id,
|
|
|
|
})?;
|
|
|
|
insert2.execute(named_params! {
|
|
|
|
":composite_id": &composite_id,
|
|
|
|
":sample_file_uuid": &sample_file_uuid.0.as_bytes()[..],
|
|
|
|
":sample_file_sha1": &sample_file_sha1,
|
|
|
|
":video_index": &video_index,
|
|
|
|
})?;
|
2016-12-21 01:08:18 -05:00
|
|
|
camera_state.current_run = if trailing_zero {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((run_id, start_time_90k + duration_90k as i64))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(camera_state)
|
|
|
|
}
|
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
fn update_camera(
|
|
|
|
tx: &rusqlite::Transaction,
|
|
|
|
camera_state: HashMap<i32, CameraState>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut stmt = tx.prepare(
|
|
|
|
r#"
|
2019-07-22 01:40:01 -04:00
|
|
|
update camera set next_recording_id = :next_recording_id where id = :id
|
2021-02-17 01:15:54 -05:00
|
|
|
"#,
|
|
|
|
)?;
|
2022-09-28 12:29:16 -04:00
|
|
|
for (ref id, state) in &camera_state {
|
2021-05-17 13:50:12 -04:00
|
|
|
stmt.execute(named_params! {
|
|
|
|
":id": &id,
|
|
|
|
":next_recording_id": &state.next_recording_id,
|
|
|
|
})?;
|
2016-12-21 01:08:18 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|