153 Commits
Author SHA1 Message Date
Scott Lamb ffc8bd6b98 v0.7.30: fix severe timestamp drift
* reset local start time at each recording boundary
* include the final frame's time when converting from wall to media time

Together at 30 fps these caused about 15 seconds of drift over 4 hours.
2026-02-19 15:47:00 -08:00
Scott Lamb 10e48265cf v0.7.29: fix garbage collection
As noted in the changelog, this problem caused restarts to take a long
time, and the SQLite database to (slowly) grow without bound.

The problem was simple: the directory worker incorrectly aborted
`collect_garbage` if nothing was watching the result. For some
operations (e.g. reading from sample files) this is the correct
behavior, but in this case it contradicts the `Pool::collect_garbage`
contract.
2026-02-19 10:11:17 -08:00
Scott Lamb b74b643d3e gracefully degrade live stream
See #343
2026-02-19 09:32:24 -08:00
Scott Lamb a384f2cb2a fix dir pool shutdown with multiple streams
`v0.7.26` introduced a busy loop error when the dir pool was shutting
down with multiple streams. This commit tests and fixes this scenario.

Also fixes a second problem in dropping a writer without an explicit
close would panic in debug mode about using (dropping) the blake3 hasher
while holding a lock. The check is meant to detect slow operations while
holding a lock. The panic wouldn't happen in release mode and probably
isn't a significant source of slowness, but fix it anyway.
2026-02-18 11:19:43 -08:00
Scott Lamb 316d3d8482 allow inserting VSEs without the db lock 2026-02-17 11:55:55 -08:00
Scott Lamb fd7ae68657 overhaul streams!
This puts a lot of state into a new `LockedStream` that was formerly
guarded more coarsely by the recording lock or more granularly by
`RecordingToInsert` locks. And it restructures `LockedDatabase::flush`
to avoid holding this lock while performing SQLite operations.

Streamers write only into a recent frames buffer in RAM, which the
directory pool workers asynchronously write to disk approximately once
per GOP, where formerly there was a write call per frame.

Before, streamers could fall behind due to slow sample file directory
disk I/O calls or if writing got stuck in a retry loop, for example if
out of disk space. If Moonfire falls too far behind in receiving,
cameras tend to disconnect or (sadly) mis-frame messages because they do
not handle this case well. This removes a confusing potential cause of
such errors.

In the new model, streamers never wait for disk I/O. Frames are buffered
up to a limit. Recordings are aborted if the I/O pool falls too far
behind. There's no retry loop.

This roughly halves CPU usage as thread hand-offs and syscalls are less
common. It also enables several desirable future changes:

* audio recording (which radically increases the number of frames and
  thus would otherwise significantly increase CPU usage).
* "monitor mode" (allowing live view without recording to disk),
* further CPU reductions for live view (by skipping all SQLite ops and
  disk I/O).

In the new model, streamers also rarely wait for SQLite database
operations either—now only on video parameter changes. That might go
away in an upcoming change too.

The "syncer" (which synced files/dirs and kept track of what has to be
flushed into the SQLite database when) has mostly gone away too. The
directory pool workers handle the syncing now, and then they notify
a new "flusher" for the latter portion.

I'd like to go further still and have the database as a whole be owned
by a dedicated thread, the only one that uses the SQLite connection.
This prevents the tokio threads from blocking on SQLite operations.
I tried out doing so before making the streamers less dependent on the
db, but the extra thread handoffs were awful for performance.
2026-02-17 11:55:55 -08:00
Scott Lamb 0dc285b159 move all sample file dir I/O to pool
Now, all IO happens on each directory's two-thread pool with threads
named `dir-<PATH>`. Formerly, IO could happen on the per-stream writer
threads, per-directory reader threads, and occasional on even the tokio
worker threads.

The recording path uses more CPU than before due to extra thread
hand-offs. That will change in the next commit, which will radically
change the approach to writing.
2026-02-17 11:17:28 -08:00
Scott Lamb 4f45a7f34d clarify db ops used by sample file dir code 2026-02-16 17:20:35 -08:00
Scott Lamb c1bf79a01c cloneable Error
This is useful in `mp4::Segment::index`. It will also be useful in
an upcoming change that can fan out errors from directory pool
transitions.
2026-02-16 17:20:35 -08:00
Scott Lamb 0cc1aa96d6 quick fix for #346 2026-02-14 12:45:19 -08:00
Scott Lamb 5cc93349fc prep v0.7.22
...with dependency updates, notably including Retina.
2025-10-03 14:48:12 -07:00
Scott Lamb dc1909d073 fix lifetime elision warnings 2025-10-03 14:19:52 -07:00
Scott Lamb 39b6f6c49c build releases with mimalloc 2025-04-03 10:06:50 -07:00
Scott Lamb 0ccc6d0769 wrap Mutex and Condvar to handle poison
This centralizes a major source of `.unwrap()` throughout the code,
and one that would otherwise grow with upcoming changes. The new
error message should be more clear.
2025-04-03 09:16:44 -07:00
Scott Lamb 2903b680df clippy fixes to tests 2025-04-03 09:16:44 -07:00
Scott Lamb 2985214d87 reduce per-slice allocations
`slices::Slices::get_range` was too closely following the example of
`http_serve::Entity::get_range`:

The latter is once-per-request, so just boxing is low-cost and makes
sense to easily avoid monomorphization bloat when there are potentially
many types of entity streams in one program. In Moonfire, it's used with
different streams defined in the `moonfire_nvr::web::mp4`,
`moonfire_nvr::bundled_ui`, and `http_serve::file` modules. Putting them
all into a single boxless enum would be a pain. In particular, the last
one is not a nameable type today and would need more generic parameters
to implement the caller-demanded `Entity` definition.

The former is once-per-slice, there are tons of slices per request, and
it's easy to define a two-case enum right where it's needed. So the
trade-off is quite different.

Also fix up some out-of-date comments.
2025-03-07 18:54:41 -08:00
Scott Lamb 3cc9603ff3 resurrect benches
Because these require nightly, CI isn't configured to fail if they're
broken. And they have been broken since the conversion to hyper 1.x.
2025-03-07 09:26:35 -08:00
Scott Lamb 0bfa09b1f1 drop ulid dependency
...and use v7 UUIDs exclusively. It's useful to have the timestamp in
request ids in particular, and no reason we _need_ v4 anywhere.
2025-02-07 10:04:29 -08:00
Scott Lamb d780b28cc2 remove unused cstr dep 2025-02-07 09:07:32 -08:00
Scott Lamb b4836f3abb v0.7.18: support H.265
There's no transcoding, so if the browser/player doesn't support these
formats, they don't play. But it will record and allow downloads, and
it seems to be working with Chrome on macOS at least.

Fixes #33
2025-01-28 09:05:10 -08:00
Scott Lamb dd30d5bcf8 update Rust dependencies
This pulls in a duplicate version of `thiserror` for now, but oh well.
2025-01-25 10:40:21 -08:00
Scott Lamb cbb2c30b56 use jiff crate 2025-01-22 14:16:31 -08:00
Scott Lamb c46832369a automated clippy fixes 2025-01-22 09:40:25 -08:00
Scott Lamb 9256561362 fix a couple clippy errors
...with Rust 1.82.
2024-12-11 15:03:31 -08:00
Scott Lamb f9e3fb56b3 update some deps 2024-08-23 21:37:21 -07:00
Scott Lamb 140f625a80 clippy 2024-08-19 08:13:35 -07:00
Scott Lamb dad664c244 upgrade to retina 0.4.9
This is a step toward H.265 recording support (#33) by using Retina
for the `VisualSampleEncoding` instead of the prior H.264-specific
logic. The only further change to Moonfire itself needed for H.265 is to
add `| "h265"` to the check for the right video stream.

This also adds support for recording MJPEG, although browser playback
almost certainly won't work.
2024-08-19 08:13:32 -07:00
Scott Lamb 89f230004e avoid unbounded channels in live streams
It's cleaner anyway to use `tokio::broadcast::channel` than the list
of callbacks.

Also make it send pings only on long pauses between frames, as when
the camera is disconnected.
2024-08-15 17:32:08 -07:00
Scott Lamb 0422593ec6 ui list view: tool tip to see why recording ended
Users are often puzzled why there are short recordings. Previously
the only way to see this was to examine Moonfire's logs. This should
be a much better experience to find it right in the UI where you're
wondering, and without the potential the logs are gone.

Fixes #302
2024-06-01 07:46:11 -07:00
Scott Lamb e6c7b800fe tolerate bad sps/pps, continued 2024-05-30 18:19:58 -07:00
Scott Lamb f385215d6e clippy 2024-02-12 17:32:37 -08:00
Scott Lamb 86816e862a update some Rust dependencies
I didn't go to quite the latest version of everything, in an effort to
minimize duplicates in the cargo tree.
2024-01-06 11:41:28 -08:00
Scott Lamb 7d12e8033d upgrade Rust deps including Retina 2023-12-29 15:30:17 -08:00
Scott Lamb 4a0cb6e62d fix incorrect comment
Fixes #288
2023-10-20 22:13:01 -07:00
Scott Lamb faf0201b52 clear pkg versions in subcrates too 2023-10-17 10:18:28 -07:00
Scott Lamb a76483a912 stop using cargo pkg version
One fewer thing to manage at release time.
2023-10-17 09:44:30 -07:00
Scott Lamb ef62ebfc6c Merge remote-tracking branch 'origin/release-0.7.7' 2023-10-16 22:05:54 -07:00
Scott Lamb b4eb573ca2 cargo fmt
...necessary to pass CI since changes to stable Rust's formatting.
2023-09-30 16:15:02 -07:00
Scott Lamb 2a8c1bb632 let libtest capture logs 2023-08-08 18:24:50 -07:00
Scott Lamb cff832e646 prep v0.7.7 2023-08-03 14:03:48 -07:00
Scott Lamb b9db9c11cc improve some error logs
...in particular, pass the error as a tracing param rather than directly
in the main error message. The new behavior is nicer with e.g.
OpenTelemetry.
2023-08-03 15:46:36 -05:00
Scott Lamb 64ca096ff3 massive error overhaul
* fully stop using ancient `failure` crate in favor of own error type
* set an `ErrorKind` on everything
2023-07-09 22:04:17 -07:00
Scott Lamb 6a5b751bd6 log error messages in web paths
HTTP requests were only returning the error message to the caller, not
logging locally. In most cases the problem could be understood
client-side, but there are some exceptions. E.g. if Moonfire returns
a 403 on WebSocket update, even in the Chrome debug tools's network
tab the HTTP response body seems to be unavailable. And in general,
it's nice to have more context server-side.

Logging a `response::Body` isn't practical (it could be a stream), so
convert all the web stuff to use `base::Error` err returns.

Convert the `METHOD_NOT_ALLOWED` paths to return `Ok` for now. This is a
bit lame but punts on having some way of plumbing an explicit/overridden
status code in `base::Error`, as no gRPC error kind cleanly maps to
that.

Also convert `db::auth`, rather than making up an error kind in the web
layer.

This is also a small step toward getting rid of `failure::Error`.
2023-07-09 10:15:56 -07:00
Scott Lamb 4ad627b997 prep 0.7.6 release 2023-07-08 10:22:04 -07:00
Scott Lamb aa60bc991c test and fix #282
Sessions' last use updates weren't getting persisted to the database
because the update statement wasn't passing through the hash.

Also address a TODO of tracing in tests by using the same tracing
setup as in production.
2023-07-04 20:53:53 -07:00
Scott Lamb 028243532a upgrade to Rust 1.70, use std::sync::OnceLock
The most notable part of this is that `db::auth` no longer holds a lock
during password hashing operations. That was probably never a great
idea...
2023-07-04 20:44:21 -07:00
Scott Lamb ebcdd76084 switch from log to tracing
I think this is a big improvement in readability.

I removed the `lnav` config, which is a little sad, but I don't think it
supports this structured logging format well. Still seems worthwhile on
balance.
2023-07-04 16:51:22 -07:00
Skye 1fde947f36 Tiny misc code improvements 2023-07-04 16:37:36 -07:00
Scott Lamb e4ecd0d853 compile and lint cleanly on latest nightly 2023-07-04 13:46:15 -07:00
Scott Lamb 2b27797f42 tweak bpaf usage message
As discussed here: https://github.com/pacak/bpaf/discussions/165#discussioncomment-4967176

I also snuck in a conversion from `lazy_static` to `once_cell`, rather
than adding another usage of the former.
2023-02-13 22:55:22 -08:00