When users toggled the "Enable go2rtc (WebRTC/MSE)" setting (the webrtc_disabled option) in the Settings page, the change was saved to the configuration file but did not take effect until lightNVR was completely restarted. This was inconvenient and required manual intervention.
The settings save handler (handle_post_settings in src/web/api_handlers_settings.c) was:
- Saving the
webrtc_disabledsetting to the config file - Reloading the configuration into memory
However, it was NOT:
- Regenerating the go2rtc configuration file (
go2rtc.yaml) with the updated settings - Restarting the go2rtc process to pick up the new configuration
This meant that even though the lightNVR configuration was updated, go2rtc continued running with its old configuration, and the WebRTC/MSE functionality didn't change until a full system restart.
Modified src/web/api_handlers_settings.c to:
- Track go2rtc-related setting changes: Added a
go2rtc_config_changedflag that is set whenwebrtc_disabledchanges - Detect actual changes: Compare the old and new values of
webrtc_disabledto only trigger restart when the value actually changes - Restart go2rtc automatically: After saving and reloading the configuration, if go2rtc settings changed:
- Stop the go2rtc process
- Regenerate the go2rtc configuration file with the new settings
- Restart the go2rtc process
- Re-register all active streams with go2rtc
#include "video/go2rtc/go2rtc_process.h"
#include "video/go2rtc/go2rtc_integration.h"bool go2rtc_config_changed = false; // Track if go2rtc-related settings changed// WebRTC disabled - track old value to detect changes
cJSON *webrtc_disabled = cJSON_GetObjectItem(settings, "webrtc_disabled");
if (webrtc_disabled && cJSON_IsBool(webrtc_disabled)) {
bool old_webrtc_disabled = g_config.webrtc_disabled;
g_config.webrtc_disabled = cJSON_IsTrue(webrtc_disabled);
settings_changed = true;
log_info("Updated webrtc_disabled: %s", g_config.webrtc_disabled ? "true" : "false");
// If webrtc_disabled changed, we need to restart go2rtc
if (old_webrtc_disabled != g_config.webrtc_disabled) {
go2rtc_config_changed = true;
log_info("WebRTC disabled setting changed, will restart go2rtc");
}
}After the configuration is saved and reloaded, the handler now:
- Checks if
go2rtc_config_changedis true - Stops the go2rtc process if running
- Regenerates the go2rtc configuration file
- Restarts the go2rtc process
- Re-registers all active streams with go2rtc
- Immediate effect: Changes to the WebRTC/MSE setting now take effect immediately without requiring a full lightNVR restart
- Better user experience: Users can toggle between HLS and WebRTC/MSE modes seamlessly
- Automatic stream re-registration: All active streams are automatically re-registered with go2rtc after the restart
- Proper cleanup: The old go2rtc process is cleanly stopped before starting the new one
To test this fix:
- Start lightNVR with go2rtc enabled (webrtc_disabled = false)
- Navigate to Settings page
- Toggle "Enable go2rtc (WebRTC/MSE)" checkbox
- Click Save
- Verify that:
- Settings save successfully
- go2rtc process restarts automatically
- Streams continue working with the new mode (HLS or WebRTC/MSE)
- No manual restart of lightNVR is required
This pattern could be extended to handle other go2rtc-related settings that might change:
- WebRTC port changes
- STUN server configuration changes
- ICE server configuration changes
- Authentication settings changes
The same approach (detect change → regenerate config → restart process) can be applied to any go2rtc configuration parameter.