Просмотр исходного кода

Fix alignment HTTP blocking and publish complete ROI JPEG snapshots

SybexXx 1 день назад
Родитель
Сommit
06fa915f5a
1 измененных файлов с 106 добавлено и 15 удалено
  1. 106 15
      code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp

+ 106 - 15
code/components/jomjol_flowcontroll/ClassFlowAlignment.cpp

@@ -10,6 +10,7 @@
 #include "psram.h"
 #include "../../include/defines.h"
 #include <new>
+#include <cstring>
 
 static const char *TAG = "ALIGN";
 
@@ -30,7 +31,11 @@ void ClassFlowAlignment::SetInitialParameter(void)
     ownsImageBasis = false;
     ImageTMP = NULL;
 #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
+    AlgROIMutex = xSemaphoreCreateMutex();
     AlgROI = new (std::nothrow) ImageData;
+    if (AlgROIMutex == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to create AlgROI publication mutex");
+    }
 #endif
     previousElement = NULL;
     disabled = false;
@@ -69,11 +74,87 @@ ClassFlowAlignment::~ClassFlowAlignment()
         ownsImageBasis = false;
     }
 #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
-    delete AlgROI;
-    AlgROI = NULL;
+    if (AlgROIMutex != NULL) {
+        (void)xSemaphoreTake(AlgROIMutex, portMAX_DELAY);
+        delete AlgROI;
+        AlgROI = NULL;
+        (void)xSemaphoreGive(AlgROIMutex);
+        vSemaphoreDelete(AlgROIMutex);
+        AlgROIMutex = NULL;
+    } else {
+        delete AlgROI;
+        AlgROI = NULL;
+    }
 #endif
 }
 
+#ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
+bool ClassFlowAlignment::PublishAlgROI(const ImageData *image)
+{
+    if (image == NULL || image->size == 0 || image->size > MAX_JPG_SIZE ||
+        AlgROIMutex == NULL) return false;
+
+    if (xSemaphoreTake(AlgROIMutex, portMAX_DELAY) != pdTRUE) return false;
+
+    if (AlgROI == NULL) AlgROI = new (std::nothrow) ImageData;
+    if (AlgROI == NULL) {
+        (void)xSemaphoreGive(AlgROIMutex);
+        return false;
+    }
+
+    // Keep the last complete JPEG visible until the replacement is fully copied.
+    std::memcpy(AlgROI->data, image->data, image->size);
+    AlgROI->size = image->size;
+
+    (void)xSemaphoreGive(AlgROIMutex);
+    return true;
+}
+
+esp_err_t ClassFlowAlignment::SendAlgROIToHTTP(httpd_req_t *req)
+{
+    if (req == NULL || AlgROIMutex == NULL) return ESP_ERR_INVALID_ARG;
+    if (xSemaphoreTake(AlgROIMutex, pdMS_TO_TICKS(250)) != pdTRUE) {
+        return ESP_ERR_TIMEOUT;
+    }
+
+    if (AlgROI == NULL || AlgROI->size == 0 || AlgROI->size > MAX_JPG_SIZE) {
+        (void)xSemaphoreGive(AlgROIMutex);
+        return ESP_ERR_NOT_FOUND;
+    }
+
+    esp_err_t result = httpd_resp_set_type(req, "image/jpeg");
+    if (result == ESP_OK) {
+        result = httpd_resp_send(req, (const char *)AlgROI->data, AlgROI->size);
+    }
+
+    (void)xSemaphoreGive(AlgROIMutex);
+    return result;
+}
+
+ImageData *ClassFlowAlignment::GetAlgROICopy(void)
+{
+    if (AlgROIMutex == NULL) return NULL;
+    ImageData *copy = new (std::nothrow) ImageData;
+    if (copy == NULL) return NULL;
+
+    if (xSemaphoreTake(AlgROIMutex, pdMS_TO_TICKS(1000)) != pdTRUE) {
+        delete copy;
+        return NULL;
+    }
+
+    if (AlgROI == NULL || AlgROI->size == 0 || AlgROI->size > MAX_JPG_SIZE) {
+        (void)xSemaphoreGive(AlgROIMutex);
+        delete copy;
+        return NULL;
+    }
+
+    std::memcpy(copy->data, AlgROI->data, AlgROI->size);
+    copy->size = AlgROI->size;
+    (void)xSemaphoreGive(AlgROIMutex);
+    return copy;
+}
+#endif
+
 bool ClassFlowAlignment::ReadParameter(FILE *pfile, string &aktparamgraph)
 {
     std::vector<string> splitted;
@@ -196,12 +277,16 @@ bool ClassFlowAlignment::doFlow(string time)
     }
 
 #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
-    // Reserve the optional JPEG output before tmpImage to reduce PSRAM fragmentation.
-    if (AlgROI == NULL) AlgROI = new (std::nothrow) ImageData;
-    if (AlgROI != NULL) {
-        ImageBasis->writeToMemoryAsJPG(AlgROI, 90);
+    // Encode into a private temporary buffer, then publish the complete JPEG.
+    // HTTP readers never observe a buffer while the encoder is modifying it.
+    ImageData *initialPreview = ImageBasis->writeToMemoryAsJPG(90);
+    if (initialPreview != NULL) {
+        if (!PublishAlgROI(initialPreview)) {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Unable to publish initial ROI preview");
+        }
+        delete initialPreview;
     } else {
-        LogFile.WriteToFile(ESP_LOG_WARN, TAG, "AlgROI PSRAM allocation failed; continuing without in-memory ROI JPEG");
+        LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Unable to create initial ROI preview");
         LogFile.WriteHeapInfo("ClassFlowAlignment-doFlow");
     }
 #endif
@@ -283,15 +368,21 @@ bool ClassFlowAlignment::doFlow(string time)
     } // no align
 
 #ifdef ALGROI_LOAD_FROM_MEM_AS_JPG
-    if (AlgROI) {
-        // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
-        if (References[0].alignment_algo != 3) {
-            DrawRef(ImageTMP);
-        }
+    // no align algo if set to 3 = off => no draw ref //add disable aligment algo |01.2023
+    if (References[0].alignment_algo != 3) {
+        DrawRef(ImageTMP);
+    }
 
-        flowctrl.DigitDrawROI(ImageTMP);
-        flowctrl.AnalogDrawROI(ImageTMP);
-        ImageTMP->writeToMemoryAsJPG((ImageData *)AlgROI, 90);
+    flowctrl.DigitDrawROI(ImageTMP);
+    flowctrl.AnalogDrawROI(ImageTMP);
+    ImageData *alignedPreview = ImageTMP->writeToMemoryAsJPG(90);
+    if (alignedPreview != NULL) {
+        if (!PublishAlgROI(alignedPreview)) {
+            LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Unable to publish aligned ROI preview");
+        }
+        delete alignedPreview;
+    } else {
+        LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Unable to create aligned ROI preview");
     }
 #endif