|
@@ -163,6 +163,41 @@ private:
|
|
|
bool m_acquired;
|
|
bool m_acquired;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+// Owns one camera framebuffer and returns it exactly once on every exit path.
|
|
|
|
|
+class ScopedCameraFrame
|
|
|
|
|
+{
|
|
|
|
|
+public:
|
|
|
|
|
+ explicit ScopedCameraFrame(camera_fb_t *frame)
|
|
|
|
|
+ : m_frame(frame)
|
|
|
|
|
+ {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ~ScopedCameraFrame()
|
|
|
|
|
+ {
|
|
|
|
|
+ release();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ScopedCameraFrame(const ScopedCameraFrame &) = delete;
|
|
|
|
|
+ ScopedCameraFrame &operator=(const ScopedCameraFrame &) = delete;
|
|
|
|
|
+
|
|
|
|
|
+ camera_fb_t *get() const
|
|
|
|
|
+ {
|
|
|
|
|
+ return m_frame;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void release()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (m_frame != NULL)
|
|
|
|
|
+ {
|
|
|
|
|
+ esp_camera_fb_return(m_frame);
|
|
|
|
|
+ m_frame = NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+private:
|
|
|
|
|
+ camera_fb_t *m_frame;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
CCamera::CCamera(void)
|
|
CCamera::CCamera(void)
|
|
|
: m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
|
|
: m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
|
|
|
m_driverInitialized(false),
|
|
m_driverInitialized(false),
|
|
@@ -271,14 +306,13 @@ bool CCamera::testCamera(void)
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- camera_fb_t *fb = esp_camera_fb_get();
|
|
|
|
|
- if (fb == NULL)
|
|
|
|
|
|
|
+ ScopedCameraFrame frame(esp_camera_fb_get());
|
|
|
|
|
+ if (frame.get() == NULL)
|
|
|
{
|
|
{
|
|
|
ESP_LOGW(TAG, "testCamera: framebuffer capture failed");
|
|
ESP_LOGW(TAG, "testCamera: framebuffer capture failed");
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -803,8 +837,9 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
vTaskDelay(pdMS_TO_TICKS(delay));
|
|
vTaskDelay(pdMS_TO_TICKS(delay));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- camera_fb_t *warmup = esp_camera_fb_get();
|
|
|
|
|
- if (warmup != NULL) esp_camera_fb_return(warmup);
|
|
|
|
|
|
|
+ {
|
|
|
|
|
+ ScopedCameraFrame warmupFrame(esp_camera_fb_get());
|
|
|
|
|
+ }
|
|
|
camera_fb_t *fb = esp_camera_fb_get();
|
|
camera_fb_t *fb = esp_camera_fb_get();
|
|
|
if (fb == NULL) {
|
|
if (fb == NULL) {
|
|
|
LEDOnOff(false);
|
|
LEDOnOff(false);
|
|
@@ -812,6 +847,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);
|
|
|
|
|
|
|
|
uint8_t *frameData = fb->buf;
|
|
uint8_t *frameData = fb->buf;
|
|
|
size_t frameLength = fb->len;
|
|
size_t frameLength = fb->len;
|
|
@@ -819,7 +855,6 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
bool demoBufferAllocated = false;
|
|
bool demoBufferAllocated = false;
|
|
|
if (CCstatus.DemoMode) {
|
|
if (CCstatus.DemoMode) {
|
|
|
if (!loadNextDemoImage(&demoData, &frameLength)) {
|
|
if (!loadNextDemoImage(&demoData, &frameLength)) {
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
LEDOnOff(false);
|
|
LEDOnOff(false);
|
|
|
LightOnOff(false);
|
|
LightOnOff(false);
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: could not load demo image");
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: could not load demo image");
|
|
@@ -833,7 +868,7 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
bool decoded = tmpImage != NULL && frameLength <= static_cast<size_t>(INT_MAX) &&
|
|
bool decoded = tmpImage != NULL && frameLength <= static_cast<size_t>(INT_MAX) &&
|
|
|
tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
|
|
tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
|
|
|
if (demoBufferAllocated) free_psram_heap("Demo image frame", demoData);
|
|
if (demoBufferAllocated) free_psram_heap("Demo image frame", demoData);
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
|
|
+ frame.release();
|
|
|
|
|
|
|
|
LEDOnOff(false);
|
|
LEDOnOff(false);
|
|
|
if (delay > 0) LightOnOff(false);
|
|
if (delay > 0) LightOnOff(false);
|
|
@@ -914,6 +949,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
return ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
|
|
+ ScopedCameraFrame frame(fb);
|
|
|
|
|
|
|
|
LEDOnOff(false); // Status-LED off
|
|
LEDOnOff(false); // Status-LED off
|
|
|
|
|
|
|
@@ -973,7 +1009,7 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
|
|
|
|
|
|
if (converted && buf != NULL) free(buf);
|
|
if (converted && buf != NULL) free(buf);
|
|
|
|
|
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
|
|
+ frame.release();
|
|
|
|
|
|
|
|
if (delay > 0)
|
|
if (delay > 0)
|
|
|
{
|
|
{
|
|
@@ -1036,6 +1072,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCaptur
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
return ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
|
|
+ ScopedCameraFrame frame(fb);
|
|
|
|
|
|
|
|
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");
|
|
@@ -1082,7 +1119,7 @@ esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCaptur
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
|
|
+ frame.release();
|
|
|
if (res == ESP_OK && useTemporaryPreview) m_previewCapturePending = false;
|
|
if (res == ESP_OK && useTemporaryPreview) m_previewCapturePending = false;
|
|
|
int64_t fr_end = esp_timer_get_time();
|
|
int64_t fr_end = esp_timer_get_time();
|
|
|
|
|
|
|
@@ -1133,11 +1170,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- camera_fb_t *warmup = esp_camera_fb_get();
|
|
|
|
|
- if (warmup != NULL)
|
|
|
|
|
- {
|
|
|
|
|
- esp_camera_fb_return(warmup);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ ScopedCameraFrame warmupFrame(esp_camera_fb_get());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -1160,6 +1193,7 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedCameraFrame frame(fb);
|
|
|
fb_len = fb->len;
|
|
fb_len = fb->len;
|
|
|
const int headerLength = snprintf(part_buf, sizeof(part_buf), _STREAM_PART,
|
|
const int headerLength = snprintf(part_buf, sizeof(part_buf), _STREAM_PART,
|
|
|
static_cast<unsigned int>(fb_len));
|
|
static_cast<unsigned int>(fb_len));
|
|
@@ -1181,7 +1215,6 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|
|
|
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
|
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- esp_camera_fb_return(fb);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|