Explorar el Código

Keep SD file writes outside the camera-driver mutex

SybexXx hace 1 día
padre
commit
3d40ce5c1a
Se han modificado 1 ficheros con 54 adiciones y 58 borrados
  1. 54 58
      code/components/jomjol_controlcamera/ClassControllCamera.cpp

+ 54 - 58
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -1016,110 +1016,106 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
 
 esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
 {
-    ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
-    if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
-    ScopedCameraLock cameraLock(m_cameraMutex);
-    if (!cameraLock.acquired())
+    ScopedCameraOperationLock operationLock(m_cameraOperationMutex);
+    if (!operationLock.acquired())
     {
-        ESP_LOGW(TAG, "CaptureToFile: camera mutex unavailable");
+        ESP_LOGW(TAG, "CaptureToFile: camera operation lock unavailable");
         return ESP_ERR_TIMEOUT;
     }
+
     const esp_err_t restoreResult = RestoreCommittedSettingsIfNeeded();
     if (restoreResult != ESP_OK) return restoreResult;
 
-    const CameraSettings &captureSettings = static_cast<const CameraSettings &>(CCstatus);
-    string ftype;
-
-    LEDOnOff(true); // Status-LED on
+    const CameraSettings captureSettings = static_cast<const CameraSettings &>(CCstatus);
+    ScopedCameraCaptureIndicators indicators(*this, true, delay > 0);
+    if (delay > 0) vTaskDelay(pdMS_TO_TICKS(delay));
 
-    if (delay > 0)
+    camera_fb_t *fb = NULL;
     {
-        LightOnOff(true); // Flash-LED on
-        vTaskDelay(pdMS_TO_TICKS(delay));
-    }
+        ScopedCameraLock cameraLock(m_cameraMutex);
+        if (!cameraLock.acquired()) return ESP_ERR_TIMEOUT;
 
-    {
-        ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
+        {
+            ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
+        }
+        fb = esp_camera_fb_get();
     }
-    camera_fb_t *fb = esp_camera_fb_get();
 
-    if (!fb)
+    if (fb == NULL)
     {
-        LEDOnOff(false);   // Status-LED off
-        if (delay > 0) LightOnOff(false); // Flash-LED off
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
                                                 "Check camera module and/or proper electrical connection");
-        // doReboot();
-
         return ESP_FAIL;
     }
     ScopedCameraFrame frame(fb, m_cameraMutex);
 
-    LEDOnOff(false); // Status-LED off
-
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "w %d, h %d, size %d", fb->width, fb->height, fb->len);
 #endif
 
     nm = FormatFileName(nm);
-
 #ifdef DEBUG_DETAIL_ON
     ESP_LOGD(TAG, "Save Camera to: %s", nm.c_str());
 #endif
 
-    ftype = toUpper(getFileType(nm));
-
-#ifdef DEBUG_DETAIL_ON
-    ESP_LOGD(TAG, "Filetype: %s", ftype.c_str());
-#endif
-
-    uint8_t *buf = NULL;
-    size_t buf_len = 0;
-    bool converted = false;
+    const string fileType = toUpper(getFileType(nm));
+    uint8_t *buffer = NULL;
+    size_t bufferLength = 0;
+    bool bufferNeedsFree = false;
     bool captureOkay = false;
 
-    if (ftype == "BMP") {
-        converted = true;
-        captureOkay = frame2bmp(fb, &buf, &buf_len) && buf != NULL && buf_len > 0;
-    } else if (ftype == "JPG") {
-        if (fb->format == PIXFORMAT_JPEG) {
-            buf_len = fb->len;
-            buf = fb->buf;
-            captureOkay = (buf != NULL && buf_len > 0);
-        } else {
-            converted = true;
-            captureOkay = frame2jpg(fb, captureSettings.ImageQuality, &buf, &buf_len) &&
-                          buf != NULL && buf_len > 0;
+    if (fileType == "BMP")
+    {
+        bufferNeedsFree = true;
+        captureOkay = frame2bmp(fb, &buffer, &bufferLength) &&
+                      buffer != NULL && bufferLength > 0;
+    }
+    else if (fileType == "JPG")
+    {
+        if (fb->format == PIXFORMAT_JPEG)
+        {
+            buffer = fb->buf;
+            bufferLength = fb->len;
+            captureOkay = buffer != NULL && bufferLength > 0;
+        }
+        else
+        {
+            bufferNeedsFree = true;
+            captureOkay = frame2jpg(fb, captureSettings.ImageQuality, &buffer, &bufferLength) &&
+                          buffer != NULL && bufferLength > 0;
             if (!captureOkay) ESP_LOGE(TAG, "JPEG compression failed");
         }
     }
 
-    if (captureOkay) {
+    if (captureOkay)
+    {
         FILE *fp = fopen(nm.c_str(), "wb");
-        if (fp == NULL) {
+        if (fp == NULL)
+        {
             LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Failed to open file " + nm);
             captureOkay = false;
-        } else {
-            const size_t written = fwrite(buf, 1, buf_len, fp);
+        }
+        else
+        {
+            const size_t written = fwrite(buffer, 1, bufferLength, fp);
             const int closeResult = fclose(fp);
-            if (written != buf_len || closeResult != 0) {
+            if (written != bufferLength || closeResult != 0)
+            {
                 LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: incomplete write to " + nm);
                 captureOkay = false;
             }
         }
-    } else {
+    }
+    else
+    {
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: unsupported image format or conversion failed");
     }
 
-    if (converted && buf != NULL) free(buf);
+    if (bufferNeedsFree && buffer != NULL) free(buffer);
 
+    // Return the frame under the short driver lock; SD access above does not
+    // need to hold that lock.
     frame.release();
-
-    if (delay > 0)
-    {
-        LightOnOff(false); // Flash-LED off
-    }
-
     return captureOkay ? ESP_OK : ESP_FAIL;
 }