mirror of
https://github.com/scottlamb/moonfire-nvr.git
synced 2024-12-26 07:05:56 -05:00
0a7535536d
I should have submitted/pushed more incrementally but just played with it on my computer as I was learning the language. The new Rust version more or less matches the functionality of the current C++ version, although there are many caveats listed below. Upgrade notes: when moving from the C++ version, I recommend dropping and recreating the "recording_cover" index in SQLite3 to pick up the addition of the "video_sync_samples" column: $ sudo systemctl stop moonfire-nvr $ sudo -u moonfire-nvr sqlite3 /var/lib/moonfire-nvr/db/db sqlite> drop index recording_cover; sqlite3> create index ...rest of command as in schema.sql...; sqlite3> ^D Some known visible differences from the C++ version: * .mp4 generation queries SQLite3 differently. Before it would just get all video indexes in a single query. Now it leads with a query that should be satisfiable by the covering index (assuming the index has been recreated as noted above), then queries individual recording's indexes as needed to fill a LRU cache. I believe this is roughly similar speed for the initial hit (which generates the moov part of the file) and significantly faster when seeking. I would have done it a while ago with the C++ version but didn't want to track down a lru cache library. It was easier to find with Rust. * On startup, the Rust version cleans up old reserved files. This is as in the design; the C++ version was just missing this code. * The .html recording list output is a little different. It's in ascending order, with the most current segment shorten than an hour rather than the oldest. This is less ergonomic, but it was easy. I could fix it or just wait to obsolete it with some fancier JavaScript UI. * commandline argument parsing and logging have changed formats due to different underlying libraries. * The JSON output isn't quite right (matching the spec / C++ implementation) yet. Additional caveats: * I haven't done any proof-reading of prep.sh + install instructions. * There's a lot of code quality work to do: adding (back) comments and test coverage, developing a good Rust style. * The ffmpeg foreign function interface is particularly sketchy. I'd eventually like to switch to something based on autogenerated bindings. I'd also like to use pure Rust code where practical, but once I do on-NVR motion detection I'll need to existing C/C++ libraries for speed (H.264 decoding + OpenCL-based analysis).
247 lines
6.6 KiB
Bash
Executable File
247 lines
6.6 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# This file is part of Moonfire NVR, a security camera network video recorder.
|
|
# Copyright (C) 2016 Scott Lamb <slamb@slamb.org>
|
|
#
|
|
# 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/>.
|
|
#
|
|
#
|
|
# Script to prepare for moonfire-nvr operations
|
|
#
|
|
# Command line options:
|
|
# -D: Skip database initialization
|
|
# -S: Skip apt-get update and install
|
|
#
|
|
|
|
# Configuration variables. Should only need minimal, or zero, changes.
|
|
# Empty values will use defaults.
|
|
#
|
|
|
|
# User and group
|
|
# Default: or whatever is in $NVR_USER (default "moonfire-nvr")
|
|
#
|
|
#NVR_USER=
|
|
#NVR_GROUP=
|
|
|
|
# Port for web server
|
|
# Default: 8080
|
|
#
|
|
#NVR_PORT=
|
|
|
|
# This should, ideally, be a location on flash storage under which the
|
|
# moonfire user's home directory will be created.
|
|
# Default: "/var/lib"
|
|
#
|
|
#NVR_HOME_BASE=
|
|
|
|
# Set to mountpoint of media directory, empty to stay in home directory
|
|
# Default: empty
|
|
#SAMPLES_DIR=
|
|
|
|
# Set to path for media directory relative to mountpoint
|
|
# Default: "samples"
|
|
#
|
|
#SAMPLES_DIR_NAME=
|
|
|
|
# Binary location
|
|
# Default: "/usr/local/bin/moonfire-nvr"
|
|
#
|
|
#SERVICE_BIN=
|
|
|
|
# Service name
|
|
# Default: "moonfire-nvr"
|
|
#
|
|
#SERVICE_NAME=
|
|
|
|
# Service Description
|
|
# Default: "Moonfire NVR"
|
|
#
|
|
#SERVICE_DESC=
|
|
|
|
# --------------------------------------------------------------------
|
|
# Derived variables: Do not modify!
|
|
#
|
|
# Determine directory path of this script
|
|
#
|
|
SOURCE="${BASH_SOURCE[0]}"
|
|
while [ -h "$SOURCE" ]; do # resolve $SOURCE until file no longer a symlink
|
|
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
|
SOURCE="$(readlink "$SOURCE")"
|
|
# if $SOURCE was relative symlink, resolve relative to path of symlink
|
|
[[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
|
|
done
|
|
|
|
SRC_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"/src
|
|
NVR_USER="${NVR_USER:-moonfire-nvr}"
|
|
NVR_GROUP="${NVR_GROUP:-$NVR_USER}"
|
|
NVR_PORT="${NVR_PORT:-8080}"
|
|
NVR_HOME_BASE="${NVR_HOME_BASE:-/var/lib}"
|
|
NVR_HOME="${NVR_HOME_BASE}/${NVR_USER}"
|
|
DB_NAME="${DB_NAME:-db}"
|
|
DB_DIR="${DB_DIR:-$NVR_HOME/db}"
|
|
SAMPLES_DIR_NAME="${SAMPLES_DIR_NAME:-samples}"
|
|
SAMPLES_DIR="${SAMPLES_DIR:-$NVR_HOME/$SAMPLES_DIR_NAME}"
|
|
SERVICE_NAME="${SERVICE_NAME:-moonfire-nvr}"
|
|
SERVICE_DESC="${SERVICE_DESC:-Moonfire NVR}"
|
|
SERVICE_BIN="${SERVICE_BIN:-/usr/local/bin/moonfire-nvr}"
|
|
|
|
# Process command line options
|
|
#
|
|
while getopts ":DS" opt; do
|
|
case $opt in
|
|
D) SKIP_DB=1
|
|
;;
|
|
S) SKIP_APT=1
|
|
;;
|
|
:)
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
exit 1
|
|
;;
|
|
\?)
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Setup all apt packages we need
|
|
#
|
|
echo 'Preparing and downloading packages we need...'; echo
|
|
if [ "${SKIP_APT:-0}" != 1 ]; then
|
|
sudo apt-get update
|
|
sudo apt-get install \
|
|
build-essential \
|
|
libavcodec-dev \
|
|
libavformat-dev \
|
|
libavutil-dev \
|
|
sqlite3 \
|
|
libsqlite3-dev \
|
|
uuid-runtime
|
|
fi
|
|
|
|
# Check if binary is installed. Setup for build if it is not
|
|
#
|
|
if [ ! -x "${SERVICE_BIN}" ]; then
|
|
echo "Binary not installed, building..."; echo
|
|
if ! cargo --version; then
|
|
echo "cargo not installed/working."
|
|
echo "Install a nightly Rust (see http://rustup.us) first."
|
|
echo
|
|
exit 1
|
|
fi
|
|
RUST_TEST_THREADS=1 cargo test
|
|
cargo build --release
|
|
sudo install -m 755 target/release/moonfire-nvr ${SERVICE_BIN}
|
|
if [ -x "${SERVICE_BIN}" ]; then
|
|
echo "Binary installed..."; echo
|
|
else
|
|
echo "Build failed to install..."; echo
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
# Create user and groups
|
|
#
|
|
echo 'Create user/group and directories we need...'; echo
|
|
sudo addgroup --quiet --system ${NVR_GROUP}
|
|
sudo adduser --quiet --system ${NVR_USER} --ingroup "${NVR_GROUP}" --home "${NVR_HOME}"
|
|
if [ ! -d "${NVR_HOME}" ]; then
|
|
sudo mkdir "${NVR_HOME}"
|
|
fi
|
|
sudo chown ${NVR_USER}:${NVR_GROUP} "${NVR_HOME}"
|
|
|
|
# Prepare samples directory
|
|
#
|
|
if [ -z "${SAMPLES_DIR}" ]; then
|
|
SAMPLES_PATH="${NVR_HOME}/${SAMPLES_DIR_NAME}"
|
|
if [ ! -d "${SAMPLES_PATH}" ]; then
|
|
sudo -u ${NVR_USER} -H mkdir "${SAMPLES_PATH}"
|
|
else
|
|
chown -R ${NVR_USER}.${NVR_USER} "${SAMPLES_PATH}"
|
|
fi
|
|
else
|
|
SAMPLES_PATH="${SAMPLES_DIR}"
|
|
if [ ! -d "${SAMPLES_PATH}" ]; then
|
|
sudo mkdir "${SAMPLES_PATH}"
|
|
echo ">>> Do not forget to edit /etc/fstab to mount samples media"; echo
|
|
fi
|
|
chown -R ${NVR_USER}.${NVR_USER} "${SAMPLES_PATH}"
|
|
fi
|
|
|
|
|
|
# Prepare for sqlite directory and set schema into db
|
|
#
|
|
echo 'Create database...'; echo
|
|
if [ ! -d "${DB_DIR}" ]; then
|
|
sudo -u ${NVR_USER} -H mkdir "${DB_DIR}"
|
|
fi
|
|
DB_PATH="${DB_DIR}/${DB_NAME}"
|
|
CAMERAS_PATH="${SRC_DIR}/../cameras.sql"
|
|
[ "${SKIP_DB:-0}" == 0 ] && sudo -u ${NVR_USER} -H sqlite3 "${DB_PATH}" < "${SRC_DIR}/schema.sql"
|
|
if [ -r "${CAMERAS_PATH}" ]; then
|
|
echo 'Add cameras...'; echo
|
|
sudo -u ${NVR_USER} -H sqlite3 "${DB_PATH}" < "${CAMERAS_PATH}"
|
|
fi
|
|
|
|
# Prepare service files
|
|
#
|
|
SERVICE_PATH="/etc/systemd/system/${SERVICE_NAME}.service"
|
|
sudo sh -c "cat > ${SERVICE_PATH}" <<NVR_EOF
|
|
[Unit]
|
|
Description=${SERVICE_DESC}
|
|
After=network-online.target
|
|
|
|
[Service]
|
|
ExecStart=${SERVICE_BIN} \\
|
|
--sample-file-dir=${SAMPLES_PATH} \\
|
|
--db-dir=${DB_DIR} \\
|
|
--http-addr=0.0.0.0:${NVR_PORT}
|
|
Environment=RUST_LOG=info
|
|
Type=simple
|
|
User=${NVR_USER}
|
|
Nice=-20
|
|
Restart=on-abnormal
|
|
CPUAccounting=true
|
|
MemoryAccounting=true
|
|
BlockIOAccounting=true
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
NVR_EOF
|
|
|
|
# Configure and start service
|
|
#
|
|
if [ -f "${SERVICE_PATH}" ]; then
|
|
echo 'Configuring system daemon...'; echo
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable ${SERVICE_NAME}
|
|
sudo systemctl restart ${SERVICE_NAME}
|
|
echo 'Getting system daemon status...'; echo
|
|
sudo systemctl status ${SERVICE_NAME}
|
|
fi
|