Эх сурвалжийг харах

Reuse a bounded PSRAM buffer across live-stream frames

SybexXx 1 өдөр өмнө
parent
commit
0a0d70d495

+ 40 - 20
code/components/jomjol_controlcamera/ClassControllCamera.cpp

@@ -1383,12 +1383,15 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
     }
     }
 
 
     char partBuffer[64];
     char partBuffer[64];
+    uint8_t *frameCopyBuffer = NULL;
+    size_t frameCopyCapacity = 0;
+
     while (result == ESP_OK)
     while (result == ESP_OK)
     {
     {
         const int64_t frameStart = esp_timer_get_time();
         const int64_t frameStart = esp_timer_get_time();
         uint8_t *frameData = NULL;
         uint8_t *frameData = NULL;
         size_t frameLength = 0;
         size_t frameLength = 0;
-        bool frameDataInPsram = false;
+        bool frameDataOwned = false;
 
 
         {
         {
             // Reserve the camera only long enough to acquire and detach a frame.
             // Reserve the camera only long enough to acquire and detach a frame.
@@ -1431,30 +1434,48 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
                         }
                         }
                         else
                         else
                         {
                         {
-                            frameData = static_cast<uint8_t *>(malloc_psram_heap(
-                                "Live stream JPEG copy", fb->len, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
-                            if (frameData == NULL)
+                            if (fb->len > frameCopyCapacity)
                             {
                             {
-                                LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: JPEG copy allocation failed");
-                                result = ESP_ERR_NO_MEM;
+                                uint8_t *newBuffer = static_cast<uint8_t *>(malloc_psram_heap(
+                                    "Live stream JPEG copy", fb->len, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
+                                if (newBuffer == NULL)
+                                {
+                                    LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: JPEG copy allocation failed");
+                                    result = ESP_ERR_NO_MEM;
+                                }
+                                else
+                                {
+                                    if (frameCopyBuffer != NULL)
+                                        free_psram_heap("Live stream JPEG copy", frameCopyBuffer);
+                                    frameCopyBuffer = newBuffer;
+                                    frameCopyCapacity = fb->len;
+                                }
                             }
                             }
-                            else
+
+                            if (result == ESP_OK)
                             {
                             {
-                                memcpy(frameData, fb->buf, fb->len);
+                                memcpy(frameCopyBuffer, fb->buf, fb->len);
+                                frameData = frameCopyBuffer;
                                 frameLength = fb->len;
                                 frameLength = fb->len;
-                                frameDataInPsram = true;
                             }
                             }
                         }
                         }
                     }
                     }
-                    else if (!frame2jpg(fb, 80, &frameData, &frameLength) ||
-                             frameData == NULL || frameLength == 0)
+                    else
                     {
                     {
-                        if (frameData != NULL)
+                        if (!frame2jpg(fb, 80, &frameData, &frameLength) ||
+                            frameData == NULL || frameLength == 0)
+                        {
+                            if (frameData != NULL)
+                            {
+                                free(frameData);
+                                frameData = NULL;
+                            }
+                            result = ESP_FAIL;
+                        }
+                        else
                         {
                         {
-                            free(frameData);
-                            frameData = NULL;
+                            frameDataOwned = true;
                         }
                         }
-                        result = ESP_FAIL;
                     }
                     }
 
 
                     frame.release();
                     frame.release();
@@ -1488,11 +1509,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
             }
             }
         }
         }
 
 
-        if (frameData != NULL)
-        {
-            if (frameDataInPsram) free_psram_heap("Live stream JPEG copy", frameData);
-            else free(frameData);
-        }
+        if (frameDataOwned && frameData != NULL) free(frameData);
 
 
         if (result != ESP_OK) break;
         if (result != ESP_OK) break;
 
 
@@ -1509,6 +1526,9 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
         }
         }
     }
     }
 
 
+    if (frameCopyBuffer != NULL)
+        free_psram_heap("Live stream JPEG copy", frameCopyBuffer);
+
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
     LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Live stream stopped");
     return result;
     return result;
 }
 }