|
|
@@ -1121,119 +1121,180 @@ esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
|
|
|
|
|
|
esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay, bool previewCapture)
|
|
|
{
|
|
|
- ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
|
|
|
- if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
- ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
- if (!cameraLock.acquired())
|
|
|
- {
|
|
|
- ESP_LOGW(TAG, "CaptureToHTTP: camera mutex unavailable");
|
|
|
- httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
- httpd_resp_send(req, "Camera is busy", HTTPD_RESP_USE_STRLEN);
|
|
|
- return ESP_ERR_TIMEOUT;
|
|
|
- }
|
|
|
- const bool useTemporaryPreview = previewCapture && m_temporarySettingsActive && m_previewCapturePending;
|
|
|
- if (useTemporaryPreview)
|
|
|
- {
|
|
|
- delay = m_previewCaptureDelayMs;
|
|
|
- }
|
|
|
- else
|
|
|
+ if (req == NULL) return ESP_ERR_INVALID_ARG;
|
|
|
+
|
|
|
+ uint8_t *responseData = NULL;
|
|
|
+ size_t responseLength = 0;
|
|
|
+ bool responseInPsram = false;
|
|
|
+ esp_err_t captureResult = ESP_OK;
|
|
|
+ const int64_t captureStart = esp_timer_get_time();
|
|
|
+
|
|
|
{
|
|
|
- const esp_err_t restoreResult = RestoreCommittedSettingsIfNeeded();
|
|
|
- if (restoreResult != ESP_OK) return restoreResult;
|
|
|
- if (previewCapture)
|
|
|
+ ScopedCameraOperationLock operationLock(m_cameraOperationMutex);
|
|
|
+ if (!operationLock.acquired())
|
|
|
{
|
|
|
- const int waitSeconds = std::max(0, CCstatus.WaitBeforePicture);
|
|
|
- delay = waitSeconds > INT_MAX / 1000 ? INT_MAX : waitSeconds * 1000;
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
+ httpd_resp_send(req, "Camera is busy", HTTPD_RESP_USE_STRLEN);
|
|
|
+ return ESP_ERR_TIMEOUT;
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- esp_err_t res = ESP_OK;
|
|
|
- size_t fb_len = 0;
|
|
|
- int64_t fr_start = esp_timer_get_time();
|
|
|
+ bool useTemporaryPreview = false;
|
|
|
+ {
|
|
|
+ ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
+ if (!cameraLock.acquired())
|
|
|
+ {
|
|
|
+ captureResult = ESP_ERR_TIMEOUT;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ useTemporaryPreview =
|
|
|
+ previewCapture && m_temporarySettingsActive && m_previewCapturePending;
|
|
|
+ if (useTemporaryPreview) delay = m_previewCaptureDelayMs;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- LEDOnOff(true); // Status-LED on
|
|
|
+ if (captureResult == ESP_OK && !useTemporaryPreview)
|
|
|
+ {
|
|
|
+ captureResult = RestoreCommittedSettingsIfNeeded();
|
|
|
+ if (captureResult == ESP_OK && previewCapture)
|
|
|
+ {
|
|
|
+ const int waitSeconds = std::max(0, CCstatus.WaitBeforePicture);
|
|
|
+ delay = waitSeconds > INT_MAX / 1000 ? INT_MAX : waitSeconds * 1000;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (delay > 0)
|
|
|
- {
|
|
|
- LightOnOff(true); // Flash-LED on
|
|
|
- vTaskDelay(pdMS_TO_TICKS(delay));
|
|
|
- }
|
|
|
+ if (captureResult == ESP_OK)
|
|
|
+ {
|
|
|
+ ScopedCameraCaptureIndicators indicators(*this, true, delay > 0);
|
|
|
+ if (delay > 0) vTaskDelay(pdMS_TO_TICKS(delay));
|
|
|
|
|
|
- {
|
|
|
- ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
|
|
|
- }
|
|
|
- camera_fb_t *fb = esp_camera_fb_get();
|
|
|
+ camera_fb_t *fb = NULL;
|
|
|
+ {
|
|
|
+ ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
+ if (!cameraLock.acquired())
|
|
|
+ {
|
|
|
+ captureResult = ESP_ERR_TIMEOUT;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ {
|
|
|
+ ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
|
|
|
+ }
|
|
|
+ fb = esp_camera_fb_get();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (!fb)
|
|
|
- {
|
|
|
- LEDOnOff(false); // Status-LED off
|
|
|
- LightOnOff(false); // Flash-LED off
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToFile: Capture Failed. "
|
|
|
- "Check camera module and/or proper electrical connection");
|
|
|
- httpd_resp_send_500(req);
|
|
|
- // doReboot();
|
|
|
+ if (captureResult == ESP_OK && fb == NULL)
|
|
|
+ {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: camera capture failed");
|
|
|
+ captureResult = ESP_FAIL;
|
|
|
+ }
|
|
|
|
|
|
- return ESP_FAIL;
|
|
|
- }
|
|
|
- ScopedCameraFrame frame(fb, m_cameraMutex);
|
|
|
+ if (captureResult == ESP_OK)
|
|
|
+ {
|
|
|
+ ScopedCameraFrame frame(fb, m_cameraMutex);
|
|
|
+ if (CCstatus.DemoMode)
|
|
|
+ {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
|
|
|
+ if (!loadNextDemoImage(&responseData, &responseLength))
|
|
|
+ {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: could not load demo image");
|
|
|
+ captureResult = ESP_FAIL;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ responseInPsram = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (fb->format == PIXFORMAT_JPEG)
|
|
|
+ {
|
|
|
+ if (fb->buf == NULL || fb->len == 0)
|
|
|
+ {
|
|
|
+ captureResult = ESP_FAIL;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ responseData = static_cast<uint8_t *>(malloc_psram_heap(
|
|
|
+ "HTTP JPEG copy", fb->len, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
|
|
|
+ if (responseData == NULL)
|
|
|
+ {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: unable to allocate JPEG copy");
|
|
|
+ captureResult = ESP_ERR_NO_MEM;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ memcpy(responseData, fb->buf, fb->len);
|
|
|
+ responseLength = fb->len;
|
|
|
+ responseInPsram = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (!frame2jpg(fb, 80, &responseData, &responseLength) ||
|
|
|
+ responseData == NULL || responseLength == 0)
|
|
|
+ {
|
|
|
+ if (responseData != NULL)
|
|
|
+ {
|
|
|
+ free(responseData);
|
|
|
+ responseData = NULL;
|
|
|
+ }
|
|
|
+ captureResult = ESP_FAIL;
|
|
|
+ }
|
|
|
|
|
|
- LEDOnOff(false); // Status-LED off
|
|
|
- res = httpd_resp_set_type(req, "image/jpeg");
|
|
|
+ frame.release();
|
|
|
+ }
|
|
|
|
|
|
- if (res == ESP_OK)
|
|
|
+ if (captureResult == ESP_OK && useTemporaryPreview)
|
|
|
+ {
|
|
|
+ ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
+ if (cameraLock.acquired()) m_previewCapturePending = false;
|
|
|
+ else captureResult = ESP_ERR_TIMEOUT;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const int64_t captureEnd = esp_timer_get_time();
|
|
|
+ if (captureResult == ESP_OK && responseData != NULL && responseLength > 0)
|
|
|
+ {
|
|
|
+ captureResult = httpd_resp_set_type(req, "image/jpeg");
|
|
|
+ if (captureResult == ESP_OK)
|
|
|
+ {
|
|
|
+ captureResult = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
|
|
|
+ }
|
|
|
+ if (captureResult == ESP_OK)
|
|
|
+ {
|
|
|
+ captureResult = httpd_resp_send(req, reinterpret_cast<const char *>(responseData),
|
|
|
+ static_cast<ssize_t>(responseLength));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (captureResult == ESP_OK)
|
|
|
{
|
|
|
- res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
|
|
|
+ captureResult = ESP_FAIL;
|
|
|
}
|
|
|
|
|
|
- if (res == ESP_OK)
|
|
|
+ if (responseData != NULL)
|
|
|
{
|
|
|
- if (CCstatus.DemoMode)
|
|
|
+ if (responseInPsram)
|
|
|
{
|
|
|
- LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Using Demo image!");
|
|
|
- uint8_t *demoData = NULL;
|
|
|
- size_t demoLength = 0;
|
|
|
- if (loadNextDemoImage(&demoData, &demoLength)) {
|
|
|
- fb_len = demoLength;
|
|
|
- res = httpd_resp_send(req, (const char *)demoData, demoLength);
|
|
|
- free_psram_heap("Demo image HTTP frame", demoData);
|
|
|
-
|
|
|
- } else {
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToHTTP: could not load demo image");
|
|
|
- res = ESP_FAIL;
|
|
|
- }
|
|
|
+ free_psram_heap("HTTP image response", responseData);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (fb->format == PIXFORMAT_JPEG)
|
|
|
- {
|
|
|
- fb_len = fb->len;
|
|
|
- res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- jpg_chunking_t jchunk = {req, 0};
|
|
|
- res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk) ? ESP_OK : ESP_FAIL;
|
|
|
- if (res == ESP_OK)
|
|
|
- {
|
|
|
- res = httpd_resp_send_chunk(req, NULL, 0);
|
|
|
- }
|
|
|
- fb_len = jchunk.len;
|
|
|
- }
|
|
|
+ free(responseData);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- frame.release();
|
|
|
- if (res == ESP_OK && useTemporaryPreview) m_previewCapturePending = false;
|
|
|
- int64_t fr_end = esp_timer_get_time();
|
|
|
-
|
|
|
- ESP_LOGI(TAG, "JPG: %dKB %dms", (int)(fb_len / 1024), (int)((fr_end - fr_start) / 1000));
|
|
|
+ ESP_LOGI(TAG, "HTTP JPEG capture: %uKB %ums",
|
|
|
+ static_cast<unsigned int>(responseLength / 1024),
|
|
|
+ static_cast<unsigned int>((captureEnd - captureStart) / 1000));
|
|
|
|
|
|
- if (delay > 0)
|
|
|
+ if (captureResult != ESP_OK)
|
|
|
{
|
|
|
- LightOnOff(false); // Flash-LED off
|
|
|
+ // The socket may already have started a response; only send the
|
|
|
+ // fallback status for failures that occurred before response setup.
|
|
|
+ if (responseData == NULL || responseLength == 0)
|
|
|
+ httpd_resp_send_500(req);
|
|
|
}
|
|
|
-
|
|
|
- return res;
|
|
|
+ return captureResult;
|
|
|
}
|
|
|
|
|
|
esp_err_t CCamera::CaptureToStream(httpd_req_t *req, bool FlashlightOn)
|