Prechádzať zdrojové kódy

Release camera resources before SD-card writes

SybexXx 1 deň pred
rodič
commit
bb43fb2d53

+ 26 - 8
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -1085,6 +1085,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
     uint8_t *buffer = NULL;
     size_t bufferLength = 0;
     bool bufferNeedsFree = false;
+    bool bufferInPsram = false;
     bool captureOkay = false;
 
     if (fileType == "BMP")
@@ -1097,9 +1098,19 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
     {
         if (fb->format == PIXFORMAT_JPEG)
         {
-            buffer = fb->buf;
-            bufferLength = fb->len;
-            captureOkay = buffer != NULL && bufferLength > 0;
+            if (fb->buf != NULL && fb->len > 0)
+            {
+                buffer = static_cast<uint8_t *>(malloc_psram_heap(
+                    "Capture file JPEG copy", fb->len, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
+                if (buffer != NULL)
+                {
+                    memcpy(buffer, fb->buf, fb->len);
+                    bufferLength = fb->len;
+                    bufferNeedsFree = true;
+                    bufferInPsram = true;
+                    captureOkay = true;
+                }
+            }
         }
         else
         {
@@ -1110,6 +1121,12 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
         }
     }
 
+    // The buffer is now independent of the camera framebuffer. Release both
+    // camera leases before opening or writing the SD-card file.
+    frame.release();
+    indicators.release();
+    operationLock.release();
+
     if (captureOkay)
     {
         FILE *fp = fopen(nm.c_str(), "wb");
@@ -1131,14 +1148,15 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
     }
     else
     {
-        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: unsupported image format or conversion failed");
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: unsupported image format, allocation or conversion failed");
     }
 
-    if (bufferNeedsFree && buffer != NULL) free(buffer);
+    if (bufferNeedsFree && buffer != NULL)
+    {
+        if (bufferInPsram) free_psram_heap("Capture file JPEG copy", buffer);
+        else free(buffer);
+    }
 
-    // Return the frame under the short driver lock; SD access above does not
-    // need to hold that lock.
-    frame.release();
     return captureOkay ? ESP_OK : ESP_FAIL;
 }