Prechádzať zdrojové kódy

Use a recursive camera operation lock and synchronized framebuffer release

SybexXx 1 deň pred
rodič
commit
abe017512a

+ 64 - 33
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -164,18 +164,18 @@ private:
 };
 };
 
 
 // Owns one camera framebuffer and returns it exactly once on every exit path.
 // Owns one camera framebuffer and returns it exactly once on every exit path.
-class ScopedSemaphoreLock
+class ScopedCameraOperationLock
 {
 {
 public:
 public:
-    explicit ScopedSemaphoreLock(SemaphoreHandle_t mutex, TickType_t timeout = portMAX_DELAY)
+    explicit ScopedCameraOperationLock(SemaphoreHandle_t mutex, TickType_t timeout = pdMS_TO_TICKS(5000))
         : m_mutex(mutex),
         : m_mutex(mutex),
-          m_acquired(mutex != NULL && xSemaphoreTake(mutex, timeout) == pdTRUE)
+          m_acquired(mutex != NULL && xSemaphoreTakeRecursive(mutex, timeout) == pdTRUE)
     {
     {
     }
     }
 
 
-    ~ScopedSemaphoreLock()
+    ~ScopedCameraOperationLock()
     {
     {
-        if (m_acquired) xSemaphoreGive(m_mutex);
+        if (m_acquired) xSemaphoreGiveRecursive(m_mutex);
     }
     }
 
 
     bool acquired() const { return m_acquired; }
     bool acquired() const { return m_acquired; }
@@ -189,8 +189,8 @@ private:
 class ScopedCameraFrame
 class ScopedCameraFrame
 {
 {
 public:
 public:
-    explicit ScopedCameraFrame(camera_fb_t *frame = NULL)
-        : m_frame(frame)
+    ScopedCameraFrame(camera_fb_t *frame, SemaphoreHandle_t cameraMutex)
+        : m_frame(frame), m_cameraMutex(cameraMutex)
     {
     {
     }
     }
 
 
@@ -201,32 +201,63 @@ public:
 
 
     camera_fb_t *get() const { return m_frame; }
     camera_fb_t *get() const { return m_frame; }
 
 
-    void set(camera_fb_t *frame)
-    {
-        release();
-        m_frame = frame;
-    }
-
     void release()
     void release()
     {
     {
-        if (m_frame != NULL)
+        if (m_frame == NULL) return;
+
+        // Frame return touches camera-driver state and must use the driver lock.
+        ScopedCameraLock cameraLock(m_cameraMutex, portMAX_DELAY);
+        if (!cameraLock.acquired())
         {
         {
-            esp_camera_fb_return(m_frame);
-            m_frame = NULL;
+            ESP_LOGE(TAG, "Unable to lock camera driver to return framebuffer");
+            return;
         }
         }
+
+        esp_camera_fb_return(m_frame);
+        m_frame = NULL;
     }
     }
 
 
 private:
 private:
     camera_fb_t *m_frame;
     camera_fb_t *m_frame;
+    SemaphoreHandle_t m_cameraMutex;
+};
+
+class ScopedCameraCaptureIndicators
+{
+public:
+    ScopedCameraCaptureIndicators(CCamera &camera, bool statusLed, bool flashLed)
+        : m_camera(camera), m_statusLed(statusLed), m_flashLed(flashLed)
+    {
+        if (m_statusLed) m_camera.SetCaptureStatusLedActive(true);
+        if (m_flashLed) m_camera.SetCaptureFlashActive(true);
+    }
+
+    ~ScopedCameraCaptureIndicators()
+    {
+        if (m_flashLed) m_camera.SetCaptureFlashActive(false);
+        if (m_statusLed) m_camera.SetCaptureStatusLedActive(false);
+    }
+
+    ScopedCameraCaptureIndicators(const ScopedCameraCaptureIndicators &) = delete;
+    ScopedCameraCaptureIndicators &operator=(const ScopedCameraCaptureIndicators &) = delete;
+
+private:
+    CCamera &m_camera;
+    bool m_statusLed;
+    bool m_flashLed;
 };
 };
 
 
 CCamera::CCamera(void)
 CCamera::CCamera(void)
     : m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
     : m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
-      m_framebufferMutex(xSemaphoreCreateMutex()),
+      m_cameraOperationMutex(xSemaphoreCreateRecursiveMutex()),
       m_driverInitialized(false),
       m_driverInitialized(false),
       m_temporarySettingsActive(false),
       m_temporarySettingsActive(false),
       m_previewCapturePending(false),
       m_previewCapturePending(false),
-      m_previewCaptureDelayMs(0)
+      m_previewCaptureDelayMs(0),
+      m_captureFlashUsers(0),
+      m_captureStatusLedUsers(0),
+      m_manualFlashOn(false),
+      m_manualStatusLedOn(false)
 {
 {
 #ifdef DEBUG_DETAIL_ON
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "CreateClassCamera");
     ESP_LOGD(TAG, "CreateClassCamera");
@@ -245,7 +276,7 @@ CCamera::CCamera(void)
 esp_err_t CCamera::InitCam(void)
 esp_err_t CCamera::InitCam(void)
 {
 {
     // Driver teardown/reinitialization must not invalidate a leased framebuffer.
     // Driver teardown/reinitialization must not invalidate a leased framebuffer.
-    ScopedSemaphoreLock frameLease(m_framebufferMutex);
+    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     ScopedCameraLock cameraLock(m_cameraMutex);
     ScopedCameraLock cameraLock(m_cameraMutex);
     if (!cameraLock.acquired())
     if (!cameraLock.acquired())
@@ -325,7 +356,7 @@ esp_err_t CCamera::InitCam(void)
 
 
 bool CCamera::testCamera(void)
 bool CCamera::testCamera(void)
 {
 {
-    ScopedSemaphoreLock frameLease(m_framebufferMutex);
+    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
     if (!frameLease.acquired()) return false;
     if (!frameLease.acquired()) return false;
     ScopedCameraLock cameraLock(m_cameraMutex);
     ScopedCameraLock cameraLock(m_cameraMutex);
     if (!cameraLock.acquired())
     if (!cameraLock.acquired())
@@ -334,7 +365,7 @@ bool CCamera::testCamera(void)
         return false;
         return false;
     }
     }
 
 
-    ScopedCameraFrame frame(esp_camera_fb_get());
+    ScopedCameraFrame frame(esp_camera_fb_get(), m_cameraMutex);
     if (frame.get() == NULL)
     if (frame.get() == NULL)
     {
     {
         ESP_LOGW(TAG, "testCamera: framebuffer capture failed");
         ESP_LOGW(TAG, "testCamera: framebuffer capture failed");
@@ -841,7 +872,7 @@ static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_
 
 
 esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
 esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
 {
 {
-    ScopedSemaphoreLock frameLease(m_framebufferMutex);
+    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     ScopedCameraLock cameraLock(m_cameraMutex);
     ScopedCameraLock cameraLock(m_cameraMutex);
     if (!cameraLock.acquired())
     if (!cameraLock.acquired())
@@ -868,7 +899,7 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
     }
     }
 
 
     {
     {
-        ScopedCameraFrame warmupFrame(esp_camera_fb_get());
+        ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
     }
     }
     camera_fb_t *fb = esp_camera_fb_get();
     camera_fb_t *fb = esp_camera_fb_get();
     if (fb == NULL) {
     if (fb == NULL) {
@@ -877,7 +908,7 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: camera capture failed");
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: camera capture failed");
         return ESP_FAIL;
         return ESP_FAIL;
     }
     }
-    ScopedCameraFrame frame(fb);
+    ScopedCameraFrame frame(fb, m_cameraMutex);
 
 
     uint8_t *frameData = fb->buf;
     uint8_t *frameData = fb->buf;
     size_t frameLength = fb->len;
     size_t frameLength = fb->len;
@@ -945,7 +976,7 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
 
 
 esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
 esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
 {
 {
-    ScopedSemaphoreLock frameLease(m_framebufferMutex);
+    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     ScopedCameraLock cameraLock(m_cameraMutex);
     ScopedCameraLock cameraLock(m_cameraMutex);
     if (!cameraLock.acquired())
     if (!cameraLock.acquired())
@@ -968,7 +999,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
     }
     }
 
 
     {
     {
-        ScopedCameraFrame warmupFrame(esp_camera_fb_get());
+        ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
     }
     }
     camera_fb_t *fb = esp_camera_fb_get();
     camera_fb_t *fb = esp_camera_fb_get();
 
 
@@ -982,7 +1013,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
 
 
         return ESP_FAIL;
         return ESP_FAIL;
     }
     }
-    ScopedCameraFrame frame(fb);
+    ScopedCameraFrame frame(fb, m_cameraMutex);
 
 
     LEDOnOff(false); // Status-LED off
     LEDOnOff(false); // Status-LED off
 
 
@@ -1054,7 +1085,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
 
 
 esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCapture)
 esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCapture)
 {
 {
-    ScopedSemaphoreLock frameLease(m_framebufferMutex);
+    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
     ScopedCameraLock cameraLock(m_cameraMutex);
     ScopedCameraLock cameraLock(m_cameraMutex);
     if (!cameraLock.acquired())
     if (!cameraLock.acquired())
@@ -1093,7 +1124,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCaptur
     }
     }
 
 
     {
     {
-        ScopedCameraFrame warmupFrame(esp_camera_fb_get());
+        ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
     }
     }
     camera_fb_t *fb = esp_camera_fb_get();
     camera_fb_t *fb = esp_camera_fb_get();
 
 
@@ -1108,7 +1139,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCaptur
 
 
         return ESP_FAIL;
         return ESP_FAIL;
     }
     }
-    ScopedCameraFrame frame(fb);
+    ScopedCameraFrame frame(fb, m_cameraMutex);
 
 
     LEDOnOff(false); // Status-LED off
     LEDOnOff(false); // Status-LED off
     res = httpd_resp_set_type(req, "image/jpeg");
     res = httpd_resp_set_type(req, "image/jpeg");
@@ -1199,7 +1230,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
     // Discard one initial frame so that exposure and white balance can settle.
     // Discard one initial frame so that exposure and white balance can settle.
     if (res == ESP_OK)
     if (res == ESP_OK)
     {
     {
-        ScopedSemaphoreLock warmupLease(m_framebufferMutex);
+        ScopedCameraOperationLock warmupLease(m_cameraOperationMutex);
         if (!warmupLease.acquired())
         if (!warmupLease.acquired())
         {
         {
             res = ESP_ERR_TIMEOUT;
             res = ESP_ERR_TIMEOUT;
@@ -1213,7 +1244,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
             }
             }
             else
             else
             {
             {
-                ScopedCameraFrame warmupFrame(esp_camera_fb_get());
+                ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
             }
             }
         }
         }
     }
     }
@@ -1221,7 +1252,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
     while (res == ESP_OK)
     while (res == ESP_OK)
     {
     {
         fr_start = esp_timer_get_time();
         fr_start = esp_timer_get_time();
-        ScopedSemaphoreLock frameLease(m_framebufferMutex);
+        ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
         if (!frameLease.acquired())
         if (!frameLease.acquired())
         {
         {
             res = ESP_ERR_TIMEOUT;
             res = ESP_ERR_TIMEOUT;