|
@@ -164,25 +164,47 @@ private:
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
// Owns one camera framebuffer and returns it exactly once on every exit path.
|
|
// Owns one camera framebuffer and returns it exactly once on every exit path.
|
|
|
-class ScopedCameraFrame
|
|
|
|
|
|
|
+class ScopedSemaphoreLock
|
|
|
{
|
|
{
|
|
|
public:
|
|
public:
|
|
|
- explicit ScopedCameraFrame(camera_fb_t *frame)
|
|
|
|
|
- : m_frame(frame)
|
|
|
|
|
|
|
+ explicit ScopedSemaphoreLock(SemaphoreHandle_t mutex, TickType_t timeout = portMAX_DELAY)
|
|
|
|
|
+ : m_mutex(mutex),
|
|
|
|
|
+ m_acquired(mutex != NULL && xSemaphoreTake(mutex, timeout) == pdTRUE)
|
|
|
{
|
|
{
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ~ScopedCameraFrame()
|
|
|
|
|
|
|
+ ~ScopedSemaphoreLock()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (m_acquired) xSemaphoreGive(m_mutex);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool acquired() const { return m_acquired; }
|
|
|
|
|
+
|
|
|
|
|
+private:
|
|
|
|
|
+ SemaphoreHandle_t m_mutex;
|
|
|
|
|
+ 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 = NULL)
|
|
|
|
|
+ : m_frame(frame)
|
|
|
{
|
|
{
|
|
|
- release();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ ~ScopedCameraFrame() { release(); }
|
|
|
|
|
+
|
|
|
ScopedCameraFrame(const ScopedCameraFrame &) = delete;
|
|
ScopedCameraFrame(const ScopedCameraFrame &) = delete;
|
|
|
ScopedCameraFrame &operator=(const ScopedCameraFrame &) = delete;
|
|
ScopedCameraFrame &operator=(const ScopedCameraFrame &) = delete;
|
|
|
|
|
|
|
|
- camera_fb_t *get() const
|
|
|
|
|
|
|
+ camera_fb_t *get() const { return m_frame; }
|
|
|
|
|
+
|
|
|
|
|
+ void set(camera_fb_t *frame)
|
|
|
{
|
|
{
|
|
|
- return m_frame;
|
|
|
|
|
|
|
+ release();
|
|
|
|
|
+ m_frame = frame;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void release()
|
|
void release()
|
|
@@ -200,6 +222,7 @@ private:
|
|
|
|
|
|
|
|
CCamera::CCamera(void)
|
|
CCamera::CCamera(void)
|
|
|
: m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
|
|
: m_cameraMutex(xSemaphoreCreateRecursiveMutex()),
|
|
|
|
|
+ m_framebufferMutex(xSemaphoreCreateMutex()),
|
|
|
m_driverInitialized(false),
|
|
m_driverInitialized(false),
|
|
|
m_temporarySettingsActive(false),
|
|
m_temporarySettingsActive(false),
|
|
|
m_previewCapturePending(false),
|
|
m_previewCapturePending(false),
|
|
@@ -299,6 +322,8 @@ esp_err_t CCamera::InitCam(void)
|
|
|
|
|
|
|
|
bool CCamera::testCamera(void)
|
|
bool CCamera::testCamera(void)
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedSemaphoreLock frameLease(m_framebufferMutex);
|
|
|
|
|
+ if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
if (!cameraLock.acquired())
|
|
if (!cameraLock.acquired())
|
|
|
{
|
|
{
|
|
@@ -813,6 +838,8 @@ static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_
|
|
|
|
|
|
|
|
esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedSemaphoreLock frameLease(m_framebufferMutex);
|
|
|
|
|
+ if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
if (!cameraLock.acquired())
|
|
if (!cameraLock.acquired())
|
|
|
{
|
|
{
|
|
@@ -915,6 +942,8 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
|
|
|
|
|
esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedSemaphoreLock frameLease(m_framebufferMutex);
|
|
|
|
|
+ if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
if (!cameraLock.acquired())
|
|
if (!cameraLock.acquired())
|
|
|
{
|
|
{
|
|
@@ -1022,6 +1051,8 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
|
|
|
|
|
|
esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCapture)
|
|
esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCapture)
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedSemaphoreLock frameLease(m_framebufferMutex);
|
|
|
|
|
+ if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
if (!cameraLock.acquired())
|
|
if (!cameraLock.acquired())
|
|
|
{
|
|
{
|
|
@@ -1165,6 +1196,8 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|
|
|
// Discard one initial frame so that exposure and white balance can settle.
|
|
// Discard one initial frame so that exposure and white balance can settle.
|
|
|
if (res == ESP_OK)
|
|
if (res == ESP_OK)
|
|
|
{
|
|
{
|
|
|
|
|
+ ScopedSemaphoreLock warmupLease(m_framebufferMutex);
|
|
|
|
|
+ if (!warmupLease.acquired()) res = ESP_ERR_TIMEOUT;
|
|
|
ScopedCameraLock warmupLock(m_cameraMutex);
|
|
ScopedCameraLock warmupLock(m_cameraMutex);
|
|
|
if (!warmupLock.acquired())
|
|
if (!warmupLock.acquired())
|
|
|
{
|
|
{
|
|
@@ -1179,45 +1212,50 @@ esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|
|
|
while (res == ESP_OK)
|
|
while (res == ESP_OK)
|
|
|
{
|
|
{
|
|
|
fr_start = esp_timer_get_time();
|
|
fr_start = esp_timer_get_time();
|
|
|
|
|
+ ScopedSemaphoreLock frameLease(m_framebufferMutex);
|
|
|
|
|
+ if (!frameLease.acquired())
|
|
|
{
|
|
{
|
|
|
- ScopedCameraLock frameLock(m_cameraMutex);
|
|
|
|
|
- if (!frameLock.acquired())
|
|
|
|
|
- {
|
|
|
|
|
- res = ESP_ERR_TIMEOUT;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+ res = ESP_ERR_TIMEOUT;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ ScopedCameraFrame frame;
|
|
|
{
|
|
{
|
|
|
- camera_fb_t *fb = esp_camera_fb_get();
|
|
|
|
|
- if (fb == NULL)
|
|
|
|
|
|
|
+ // Only serialize access to the camera driver while acquiring the frame.
|
|
|
|
|
+ // Keep the framebuffer lease, not the driver mutex, during network I/O.
|
|
|
|
|
+ ScopedCameraLock frameLock(m_cameraMutex);
|
|
|
|
|
+ if (!frameLock.acquired())
|
|
|
{
|
|
{
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
|
|
|
|
|
- res = ESP_FAIL;
|
|
|
|
|
|
|
+ res = ESP_ERR_TIMEOUT;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- ScopedCameraFrame frame(fb);
|
|
|
|
|
- fb_len = fb->len;
|
|
|
|
|
- const int headerLength = snprintf(part_buf, sizeof(part_buf), _STREAM_PART,
|
|
|
|
|
- static_cast<unsigned int>(fb_len));
|
|
|
|
|
- if (headerLength < 0 || static_cast<size_t>(headerLength) >= sizeof(part_buf))
|
|
|
|
|
|
|
+ frame.set(esp_camera_fb_get());
|
|
|
|
|
+ if (frame.get() == NULL)
|
|
|
{
|
|
{
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToStream: Camera framebuffer not available");
|
|
|
res = ESP_FAIL;
|
|
res = ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- res = httpd_resp_send_chunk(req, part_buf, static_cast<size_t>(headerLength));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (res == ESP_OK)
|
|
|
|
|
- {
|
|
|
|
|
- res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
|
|
|
|
|
- }
|
|
|
|
|
- if (res == ESP_OK)
|
|
|
|
|
- {
|
|
|
|
|
- res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ if (res == ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ camera_fb_t *fb = frame.get();
|
|
|
|
|
+ fb_len = fb->len;
|
|
|
|
|
+ const int headerLength = snprintf(part_buf, sizeof(part_buf), _STREAM_PART,
|
|
|
|
|
+ static_cast<unsigned int>(fb_len));
|
|
|
|
|
+ if (headerLength < 0 || static_cast<size_t>(headerLength) >= sizeof(part_buf))
|
|
|
|
|
+ {
|
|
|
|
|
+ res = ESP_FAIL;
|
|
|
}
|
|
}
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ res = httpd_resp_send_chunk(req, part_buf, static_cast<size_t>(headerLength));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (res == ESP_OK) res = httpd_resp_send_chunk(req, (const char *)fb->buf, fb_len);
|
|
|
|
|
+ if (res == ESP_OK) res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|