Synopsis
When attempting to use ONVIF for motion detection, I get a ONVIF detection system not initialized error. Indeed, static bool initialized in src/video/onvif_detection.c is false, probably because init_onvif_detection_system() (in src/video/onvif_detection.c) is never called.
Analysis
I used clangd to determine what functions might call init_onvif_detection_system().
init_onvif_detection_system() could be called from load_detection_model() (in src/video/detection_model.c), itself called by run_detection_on_frame() (in src/video/unified_detection_thread.c) except that when using ONVIF detection, run_detection_on_frame() first attempts to call detect_motion_onvif(), which fails because ONVIF was not initialized, causing run_detection_on_frame() to log a warning and return false.
init_onvif_detection_system() could also be called from init_onvif_detection_integration() (in src/video/onvif_detection_integration.c), which is itself called by init_detection_integration() (in src/video/detection_integration.c). Unfortunately, the only code that calls init_detection_integration() is in the main() function of tests/test_stream_detection.c.
Debugging Attempts
I added two debug logging calls to src/video/onvif_detection.c to confirm whether initialization had taken place, or whether the curl handle had been nullified:
--- a/src/video/onvif_detection.c
+++ b/src/video/onvif_detection.c
@@ -243,6 +243,7 @@ static char *send_onvif_request(const char *url, const char *username, const cha
const char *request_body, const char *service) {
if (!initialized || !curl_handle) {
log_error("ONVIF detection system not initialized");
+ log_debug("initialized: %d, curl_handle %p", initialized, curl_handle);
return NULL;
}
@@ -551,6 +552,7 @@ int detect_motion_onvif(const char *onvif_url, const char *username, const char
if (!initialized || !curl_handle) {
log_error("ONVIF detection system not initialized");
+ log_debug("initialized: %d, curl_handle %p", initialized, curl_handle);
pthread_mutex_unlock(&curl_mutex);
return -1;
}
This additional logging is present in the log excerpts below.
Potential Fix
Having load_detection_model() call load_detection_model() before calling onvif-specific code (see patch below) seems to fix the problem.
--- a/src/video/unified_detection_thread.c
+++ b/src/video/unified_detection_thread.c
@@ -1697,6 +1697,24 @@ static bool run_detection_on_frame(unified_detection_ctx_t *ctx, AVPacket *pkt)
return mot_triggered;
}
+ // Embedded model detection - requires frame decoding
+ if (!pkt || !ctx->decoder_ctx) return false;
+
+ // Check if we have a detection model loaded
+ if (!ctx->model) {
+ // Try to load the model if we have a path
+ if (ctx->model_path[0] != '\0') {
+ ctx->model = load_detection_model(ctx->model_path, ctx->detection_threshold);
+ if (!ctx->model) {
+ log_warn("[%s] Failed to load detection model: %s", ctx->stream_name, ctx->model_path);
+ return false;
+ }
+ log_info("[%s] Loaded detection model: %s", ctx->stream_name, ctx->model_path);
+ } else {
+ return false;
+ }
+ }
+
// ONVIF event-based detection <E2><80><94> no frame decoding needed
if (is_onvif_detection_model(ctx->model_path)) {
// Fetch the stream config to obtain the camera URL and ONVIF credentials
@@ -1740,24 +1758,6 @@ static bool run_detection_on_frame(unified_detection_ctx_t *ctx, AVPacket *pkt)
return onvif_triggered;
}
- // Embedded model detection - requires frame decoding
- if (!pkt || !ctx->decoder_ctx) return false;
-
- // Check if we have a detection model loaded
- if (!ctx->model) {
- // Try to load the model if we have a path
- if (ctx->model_path[0] != '\0') {
- ctx->model = load_detection_model(ctx->model_path, ctx->detection_threshold);
- if (!ctx->model) {
- log_warn("[%s] Failed to load detection model: %s", ctx->stream_name, ctx->model_path);
- return false;
- }
- log_info("[%s] Loaded detection model: %s", ctx->stream_name, ctx->model_path);
- } else {
- return false;
- }
- }
-
// Decode the packet to get a frame
int ret = avcodec_send_packet(ctx->decoder_ctx, pkt);
if (ret < 0) {
Relevant log excerpts
[2026-03-10 03:40:32] [INFO] Initializing ONVIF motion recording system
[2026-03-10 03:40:32] [INFO] Packet buffer pool initialized (memory limit: 50 MB)
[2026-03-10 03:40:32] [INFO] Motion event queue initialized
[2026-03-10 03:40:32] [INFO] Loading motion recording configurations from database
[2026-03-10 03:40:32] [INFO] Motion event processor thread started
[2026-03-10 03:40:32] [INFO] Loaded 1 motion recording configurations
[2026-03-10 03:40:32] [INFO] Loaded 1 motion recording configurations from database
[2026-03-10 03:40:32] [INFO] Enabling motion recording for stream: test (pre:5s, post:10s)
[2026-03-10 03:40:32] [INFO] Created motion recording context for stream: test
[2026-03-10 03:40:32] [INFO] Created packet buffer for stream: test (duration: 5s, max packets: 90, mode: 0)
[2026-03-10 03:40:32] [INFO] Created pre-event buffer for stream: test (5s)
[2026-03-10 03:40:32] [INFO] Saved motion recording config for stream: test
[2026-03-10 03:40:32] [INFO] Enabled motion recording for stream: test (pre: 5s, post: 10s, max: 300s, buffer: yes)
[2026-03-10 03:40:32] [INFO] ONVIF motion recording system initialized successfully
[2026-03-10 03:40:32] [INFO] ONVIF motion recording system initialized successfully
[2026-03-10 03:40:32] [INFO] Detection model system initialized
[2026-03-10 03:40:32] [INFO] Motion detection system initialized with embedded device optimizations
[2026-03-10 03:40:32] [INFO] Motion detection system initialized
[2026-03-10 03:40:32] [INFO] API detection system initialized successfully
[2026-03-10 03:40:32] [INFO] API detection system initialized
[2026-03-10 03:40:32] [INFO] Unified detection system initialized
[2026-03-10 03:40:32] [INFO] Unified detection thread system initialized
[2026-03-10 03:40:32] [INFO] Detection system initialized
[2026-03-10 03:40:32] [INFO] Detection system initialized successfully
[2026-03-10 03:40:32] [INFO] Detection stream system initialized
[2026-03-10 03:40:32] [INFO] ONVIF discovery module initialized
[2026-03-10 03:40:32] [INFO] ONVIF discovery module initialized successfully
[2026-03-10 03:40:32] [INFO] Starting detection-based recording for stream test with model onvif
[2026-03-10 03:40:32] [INFO] Starting detection stream reader for stream test with model onvif
[2026-03-10 03:40:32] [INFO] Starting detection for stream test with interval 10 (using HLS streaming thread)
[2026-03-10 03:40:32] [INFO] Detection enabled for stream test with interval 10
[2026-03-10 03:40:32] [INFO] Detection stream reader marked as enabled for stream test
[2026-03-10 03:40:32] [INFO] Unified detection thread not yet running for stream test - will be started by detection system
[2026-03-10 03:40:32] [INFO] Successfully started detection stream reader for stream test
[2026-03-10 03:40:32] [INFO] Detection stream reader for test: reader=running, thread=not running
[2026-03-10 03:40:32] [WARN] Detection stream reader reported as not running for test despite successful start
[2026-03-10 03:40:32] [INFO] Directly starting unified detection thread for stream test with model onvif (annotation_only=false)
[2026-03-10 03:40:32] [INFO] Created packet buffer for stream: test (duration: 10s, max packets: 180, mode: 0)
[2026-03-10 03:40:32] [INFO] [test] Unified detection thread started
[2026-03-10 03:40:32] [INFO] Started unified detection thread for stream test (model=onvif, threshold=0.50, interval=10, pre-buffer=10s, post-buffer=30s, segment=900s)
[2026-03-10 03:40:32] [INFO] Successfully started unified detection thread for stream test
[2026-03-10 03:40:32] [INFO] [test] State: INITIALIZING
[2026-03-10 03:40:32] [INFO] [test] State: CONNECTING (attempt 1)
[2026-03-10 03:40:32] [INFO] [test] Connecting to stream: rtsp://localhost:8554/test
[2026-03-10 03:40:32] [INFO] Waiting 5 seconds for go2rtc to settle before starting recordings...
[2026-03-10 03:40:33] [INFO] [test] Connected successfully (video stream: 0, audio stream: 1)
[2026-03-10 03:40:33] [INFO] [test] Detected video params: 2560x1440 @ 20 fps, codec=h264
[2026-03-10 03:40:33] [INFO] [test] Heartbeat: state=BUFFERING, packets=0, detections=0, last_check=1s ago
[2026-03-10 03:40:34] [DEBUG] [test] Time since last detection check: 2/10 seconds, model=onvif, state=2
[2026-03-10 03:40:37] [DEBUG] Stream test exists in go2rtc
[2026-03-10 03:40:37] [DEBUG] Stream test is registered with go2rtc
[2026-03-10 03:40:37] [INFO] Using go2rtc native HLS for stream test (no ffmpeg HLS thread needed)
[2026-03-10 03:40:37] [INFO] Preloading stream with URL: http://localhost:1984/go2rtc/api/preload?src=test&video&audio
[2026-03-10 03:40:37] [INFO] Successfully preloaded stream in go2rtc: test (query=video&audio)
[2026-03-10 03:40:37] [INFO] Preloaded stream test to keep go2rtc producer active for HLS/detection
[2026-03-10 03:40:37] [INFO] go2rtc native HLS ready for stream test
[2026-03-10 03:40:37] [INFO] Ensuring detection-based recording is active for stream: test (annotation_only=false)
[2026-03-10 03:40:37] [INFO] Unified detection already running for stream test
[2026-03-10 03:40:37] [INFO] Successfully started detection-based recording for stream: test
[2026-03-10 03:40:37] [INFO] === Detection Stream Status ===
[2026-03-10 03:40:37] [INFO] Stream test: ACTIVE (thread running) (interval: 10, frame counter: 0)
[2026-03-10 03:40:37] [INFO] - Unified detection state: 2
[2026-03-10 03:40:37] [INFO] Total active detection streams: 1 (running threads: 1)
[2026-03-10 03:40:37] [INFO] ==============================
[2026-03-10 03:40:37] [INFO] LightNVR initialized successfully
[2026-03-10 03:40:37] [DEBUG] Daemon is still running... (running=1, restart_requested=0)
[2026-03-10 03:40:37] [INFO] === Detection Stream Status ===
[2026-03-10 03:40:37] [INFO] Stream test: ACTIVE (thread running) (interval: 10, frame counter: 0)
[2026-03-10 03:40:37] [INFO] - Unified detection state: 2
[2026-03-10 03:40:37] [INFO] Total active detection streams: 1 (running threads: 1)
[2026-03-10 03:40:37] [INFO] ==============================
[2026-03-10 03:40:42] [INFO] [test] Running detection (interval=10s, elapsed=10s, model=onvif)
[2026-03-10 03:40:42] [DEBUG] [test] ONVIF detection: polling events at REDACTED (user=REDACTED)
[2026-03-10 03:40:42] [INFO] ONVIF Detection: Starting detection with URL: REDACTED
[2026-03-10 03:40:42] [INFO] ONVIF Detection: Stream name: test
[2026-03-10 03:41:17] [DEBUG] on_connection: New connection from 127.0.0.1
[2026-03-10 03:41:17] [ERROR] ONVIF detection system not initialized
[2026-03-10 03:41:17] [DEBUG] initialized: 0, curl_handle (nil)
[2026-03-10 03:41:17] [WARN] [test] ONVIF detection failed with error -1
[2026-03-10 03:41:17] [INFO] [test] Heartbeat: state=BUFFERING, packets=350, detections=0, last_check=35s ago
[2026-03-10 03:41:17] [INFO] [test] Running detection (interval=10s, elapsed=35s, model=onvif)
[2026-03-10 03:41:17] [DEBUG] [test] ONVIF detection: polling events at REDACTED (user=REDACTED)
[2026-03-10 03:41:17] [INFO] ONVIF Detection: Starting detection with URL: REDACTED
[2026-03-10 03:41:17] [INFO] ONVIF Detection: Stream name: test
[2026-03-10 03:41:17] [INFO] Running periodic service check (32 max streams)
[2026-03-10 03:41:17] [INFO] Service check for stream test: enabled=1, record=0, streaming_enabled=1
[2026-03-10 03:41:17] [ERROR] ONVIF detection system not initialized
[2026-03-10 03:41:17] [DEBUG] initialized: 0, curl_handle (nil)
[2026-03-10 03:41:17] [WARN] [test] ONVIF detection failed with error -1
Synopsis
When attempting to use ONVIF for motion detection, I get a
ONVIF detection system not initializederror. Indeed,static bool initializedinsrc/video/onvif_detection.cisfalse, probably becauseinit_onvif_detection_system()(insrc/video/onvif_detection.c) is never called.Analysis
I used clangd to determine what functions might call
init_onvif_detection_system().init_onvif_detection_system()could be called fromload_detection_model()(insrc/video/detection_model.c), itself called byrun_detection_on_frame()(insrc/video/unified_detection_thread.c) except that when using ONVIF detection,run_detection_on_frame()first attempts to calldetect_motion_onvif(), which fails because ONVIF was not initialized, causingrun_detection_on_frame()to log a warning andreturn false.init_onvif_detection_system()could also be called frominit_onvif_detection_integration()(insrc/video/onvif_detection_integration.c), which is itself called byinit_detection_integration()(insrc/video/detection_integration.c). Unfortunately, the only code that callsinit_detection_integration()is in themain()function oftests/test_stream_detection.c.Debugging Attempts
I added two debug logging calls to
src/video/onvif_detection.cto confirm whether initialization had taken place, or whether the curl handle had been nullified:This additional logging is present in the log excerpts below.
Potential Fix
Having
load_detection_model()callload_detection_model()before calling onvif-specific code (see patch below) seems to fix the problem.Relevant log excerpts