Skip to content

Commit 14f0336

Browse files
committed
Always regenerate go2rtc config fresh at startup
Fixes stale/corrupted go2rtc.yaml configs from prior versions causing stream errors (0x0 resolution, Error state). - docker-entrypoint.sh: Always remove and recreate go2rtc.yaml on container startup instead of only when it doesn't exist. Removes the now-unnecessary patching logic for base_path and origin. - go2rtc_process_start(): Always call go2rtc_process_generate_config() at the top of the function, even when go2rtc is already running, so the config stays in sync with lightNVR settings. Fixes #165
1 parent 9a610fd commit 14f0336

2 files changed

Lines changed: 21 additions & 33 deletions

File tree

docker-entrypoint.sh

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,16 @@ EOF
165165
# Create go2rtc config directory if it doesn't exist
166166
mkdir -p /etc/lightnvr/go2rtc
167167

168-
# Create default go2rtc.yaml if it doesn't exist
169-
# Only create if GO2RTC_CONFIG_PERSIST is true (default)
170-
if [ ! -f /etc/lightnvr/go2rtc/go2rtc.yaml ] && [ "${GO2RTC_CONFIG_PERSIST:-true}" = "true" ]; then
171-
log_info "Creating default go2rtc configuration..."
172-
cat > /etc/lightnvr/go2rtc/go2rtc.yaml << 'EOF'
168+
# Always regenerate go2rtc.yaml fresh at startup to avoid stale/corrupted
169+
# configs from prior versions causing stream errors (see issue #165).
170+
# The C code in go2rtc_process_generate_config() will overwrite this with
171+
# the full config derived from lightNVR settings once the application starts.
172+
if [ -f /etc/lightnvr/go2rtc/go2rtc.yaml ]; then
173+
log_info "Removing old go2rtc configuration to regenerate fresh..."
174+
rm -f /etc/lightnvr/go2rtc/go2rtc.yaml
175+
fi
176+
log_info "Creating default go2rtc configuration..."
177+
cat > /etc/lightnvr/go2rtc/go2rtc.yaml << 'EOF'
173178
# go2rtc configuration file
174179
api:
175180
listen: :1984
@@ -193,28 +198,7 @@ log:
193198
streams:
194199
# Streams will be added dynamically by LightNVR
195200
EOF
196-
log_info "Default go2rtc configuration created at /etc/lightnvr/go2rtc/go2rtc.yaml"
197-
fi
198-
199-
# Ensure existing go2rtc configs have required settings
200-
if [ -f /etc/lightnvr/go2rtc/go2rtc.yaml ]; then
201-
# Ensure base_path: /go2rtc is set - lightNVR C code prefixes ALL go2rtc API calls
202-
# with /go2rtc (GO2RTC_BASE_PATH), so go2rtc must be configured to serve on that path.
203-
# Without this, readiness checks hit /go2rtc/api/streams which returns 404.
204-
if ! grep -q 'base_path:' /etc/lightnvr/go2rtc/go2rtc.yaml 2>/dev/null; then
205-
log_warn "go2rtc config missing base_path setting - adding base_path: /go2rtc for lightNVR compatibility"
206-
sed -i '/listen: :1984/a\ base_path: /go2rtc' /etc/lightnvr/go2rtc/go2rtc.yaml
207-
log_info "Added base_path to go2rtc configuration"
208-
fi
209-
210-
# Ensure CORS origin is set
211-
# Without origin: "*", browsers block cross-origin requests from the web UI (:8080) to go2rtc (:1984)
212-
if ! grep -q 'origin:' /etc/lightnvr/go2rtc/go2rtc.yaml 2>/dev/null; then
213-
log_warn "go2rtc config missing CORS origin setting - adding origin: \"*\" for browser compatibility"
214-
sed -i '/listen: :1984/a\ origin: "*"' /etc/lightnvr/go2rtc/go2rtc.yaml
215-
log_info "Added CORS origin to go2rtc configuration"
216-
fi
217-
fi
201+
log_info "Default go2rtc configuration created at /etc/lightnvr/go2rtc/go2rtc.yaml"
218202

219203
# Set up models if needed
220204
if [ -d /usr/share/lightnvr/models ] && [ -n "$(ls -A /usr/share/lightnvr/models 2>/dev/null)" ]; then

src/video/go2rtc/go2rtc_process.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,16 @@ bool go2rtc_process_start(int api_port) {
10131013
return false;
10141014
}
10151015

1016+
// Always regenerate the go2rtc config file fresh at startup to avoid
1017+
// stale/corrupted configs from prior versions causing stream errors
1018+
// (see issue #165). The file is opened with O_TRUNC so any old content
1019+
// is discarded.
1020+
log_info("Regenerating go2rtc configuration file fresh at startup");
1021+
if (!go2rtc_process_generate_config(g_config_path, api_port)) {
1022+
log_warn("Failed to regenerate go2rtc configuration at startup");
1023+
// Continue anyway - the old config may still work
1024+
}
1025+
10161026
// Check if go2rtc is already running as a service
10171027
if (is_go2rtc_running_as_service(api_port)) {
10181028
log_info("go2rtc is already running as a service on port %d, using existing service", api_port);
@@ -1084,12 +1094,6 @@ bool go2rtc_process_start(int api_port) {
10841094
return false;
10851095
}
10861096

1087-
// Generate configuration file
1088-
if (!go2rtc_process_generate_config(g_config_path, api_port)) {
1089-
log_error("Failed to generate go2rtc configuration");
1090-
return false;
1091-
}
1092-
10931097
// Seed g_rtsp_port from the user-configured value before starting the process.
10941098
// This ensures that if the post-start API call fails (e.g. because another
10951099
// service such as mediamtx already holds the default port 8554), the fallback

0 commit comments

Comments
 (0)