Hold the camera lock while reinitializing the camera module

ReinitCameraModule deinitialized and reinitialized the camera driver
without taking frameBufferSemaphore. It is called from the web handlers
for frame size and photo quality, so a settings change could tear the
driver down while the capture task or a stream was inside
esp_camera_fb_get().

ReinitCameraModule now takes the semaphore itself, waiting up to 10 s
(CAMERA_REINIT_LOCK_TIMEOUT_MS) so a capture with a long flash time can
finish. If the lock can't be taken, the reinit is skipped and logged;
the new setting is already saved and applies on the next reinit or boot.
All callers call it without holding the semaphore.

Tested on a XIAO ESP32-S3 Sense: frame size and quality changes during
an open stream and alongside a capture, no reboot, stream kept running,
uploads 200 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pull/149/head
Jakob Pless 2026-11-18 15:04:40 +01:00
parent fc4b6de49a
commit 094d1e3fe0
No known key found for this signature in database
2 changed files with 11 additions and 1 deletions

View File

@ -324,11 +324,18 @@ void Camera::ApplyCameraCfg() {
}
/**
@brief Function for reinit camera module
@brief Function for reinit camera module.
Takes the frame buffer semaphore, so no capture or stream frame can run
while the driver is deinitialized. Must not be called with the semaphore held.
@param none
@return none
*/
void Camera::ReinitCameraModule() {
if (!xSemaphoreTake(frameBufferSemaphore, pdMS_TO_TICKS(CAMERA_REINIT_LOCK_TIMEOUT_MS))) {
log->AddEvent(LogLevel_Error, F("Camera reinit skipped, camera busy"));
return;
}
esp_err_t err = esp_camera_deinit();
if (err != ESP_OK) {
log->AddEvent(LogLevel_Warning, F("Camera error deinit camera module. Error: "), String(err, HEX));
@ -336,6 +343,8 @@ void Camera::ReinitCameraModule() {
delay(100);
InitCameraModule();
ApplyCameraCfg();
xSemaphoreGive(frameBufferSemaphore);
}
/**

View File

@ -12,6 +12,7 @@
#pragma once
#define CAMERA_WEB_LOCK_TIMEOUT_MS 3000 ///< max wait for the frame buffer lock in web server handlers [ms]
#define CAMERA_REINIT_LOCK_TIMEOUT_MS 10000 ///< max wait for the frame buffer lock before reinit of the camera module [ms]
#include "esp_camera.h"
#include "esp_timer.h"