|
|
@@ -904,18 +904,18 @@ static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_
|
|
|
|
|
|
esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
{
|
|
|
- ScopedCameraOperationLock frameLease(m_cameraOperationMutex);
|
|
|
- if (!frameLease.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
- ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
- if (!cameraLock.acquired())
|
|
|
+ ScopedCameraOperationLock operationLock(m_cameraOperationMutex);
|
|
|
+ if (!operationLock.acquired())
|
|
|
{
|
|
|
- ESP_LOGW(TAG, "CaptureToBasisImage: camera mutex unavailable");
|
|
|
+ ESP_LOGW(TAG, "CaptureToBasisImage: camera operation lock unavailable");
|
|
|
return ESP_ERR_TIMEOUT;
|
|
|
}
|
|
|
+
|
|
|
const esp_err_t restoreResult = RestoreCommittedSettingsIfNeeded();
|
|
|
if (restoreResult != ESP_OK) return restoreResult;
|
|
|
|
|
|
- if (_Image == NULL || !_Image->ImageOkay()) {
|
|
|
+ if (_Image == NULL || !_Image->ImageOkay())
|
|
|
+ {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: target image is unavailable");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
@@ -924,19 +924,22 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
LogFile.WriteHeapInfo("CaptureToBasisImage - Start");
|
|
|
#endif
|
|
|
|
|
|
- LEDOnOff(true);
|
|
|
- if (delay > 0) {
|
|
|
- LightOnOff(true);
|
|
|
- vTaskDelay(pdMS_TO_TICKS(delay));
|
|
|
- }
|
|
|
+ ScopedCameraCaptureIndicators indicators(*this, true, delay > 0);
|
|
|
+ if (delay > 0) vTaskDelay(pdMS_TO_TICKS(delay));
|
|
|
|
|
|
+ camera_fb_t *fb = NULL;
|
|
|
{
|
|
|
- ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
|
|
|
+ ScopedCameraLock cameraLock(m_cameraMutex);
|
|
|
+ if (!cameraLock.acquired()) return ESP_ERR_TIMEOUT;
|
|
|
+
|
|
|
+ {
|
|
|
+ ScopedCameraFrame warmupFrame(esp_camera_fb_get(), m_cameraMutex);
|
|
|
+ }
|
|
|
+ fb = esp_camera_fb_get();
|
|
|
}
|
|
|
- camera_fb_t *fb = esp_camera_fb_get();
|
|
|
- if (fb == NULL) {
|
|
|
- LEDOnOff(false);
|
|
|
- if (delay > 0) LightOnOff(false);
|
|
|
+
|
|
|
+ if (fb == NULL)
|
|
|
+ {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: camera capture failed");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
@@ -946,10 +949,10 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
size_t frameLength = fb->len;
|
|
|
uint8_t *demoData = NULL;
|
|
|
bool demoBufferAllocated = false;
|
|
|
- if (CCstatus.DemoMode) {
|
|
|
- if (!loadNextDemoImage(&demoData, &frameLength)) {
|
|
|
- LEDOnOff(false);
|
|
|
- LightOnOff(false);
|
|
|
+ if (CCstatus.DemoMode)
|
|
|
+ {
|
|
|
+ if (!loadNextDemoImage(&demoData, &frameLength))
|
|
|
+ {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: could not load demo image");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
@@ -958,44 +961,49 @@ esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
|
|
|
}
|
|
|
|
|
|
CImageBasis *tmpImage = new (std::nothrow) CImageBasis("tmpImage");
|
|
|
- bool decoded = tmpImage != NULL && frameLength <= static_cast<size_t>(INT_MAX) &&
|
|
|
- tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
|
|
|
+ const bool decoded = tmpImage != NULL &&
|
|
|
+ frameLength <= static_cast<size_t>(INT_MAX) &&
|
|
|
+ tmpImage->LoadFromMemory(frameData, static_cast<int>(frameLength));
|
|
|
if (demoBufferAllocated) free_psram_heap("Demo image frame", demoData);
|
|
|
- frame.release();
|
|
|
|
|
|
- LEDOnOff(false);
|
|
|
- if (delay > 0) LightOnOff(false);
|
|
|
+ frame.release();
|
|
|
|
|
|
- if (!decoded || tmpImage == NULL || !tmpImage->ImageOkay()) {
|
|
|
+ if (!decoded || tmpImage == NULL || !tmpImage->ImageOkay())
|
|
|
+ {
|
|
|
delete tmpImage;
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: JPEG decode failed");
|
|
|
LogFile.WriteHeapInfo("CaptureToBasisImage decode");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
|
|
|
- size_t expectedBytes = 0;
|
|
|
if (_Image->width != tmpImage->width || _Image->height != tmpImage->height ||
|
|
|
_Image->channels != tmpImage->channels || tmpImage->channels != 3 ||
|
|
|
tmpImage->width <= 0 || tmpImage->height <= 0 ||
|
|
|
static_cast<size_t>(tmpImage->width) > SIZE_MAX / static_cast<size_t>(tmpImage->height) ||
|
|
|
- static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) > SIZE_MAX / 3U) {
|
|
|
+ static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) > SIZE_MAX / 3U)
|
|
|
+ {
|
|
|
delete tmpImage;
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded dimensions do not match target image");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
- expectedBytes = static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) * 3U;
|
|
|
- if (expectedBytes > static_cast<size_t>(INT_MAX)) {
|
|
|
+
|
|
|
+ const size_t expectedBytes =
|
|
|
+ static_cast<size_t>(tmpImage->width) * static_cast<size_t>(tmpImage->height) * 3U;
|
|
|
+ if (expectedBytes > static_cast<size_t>(INT_MAX))
|
|
|
+ {
|
|
|
delete tmpImage;
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image is too large");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
|
|
|
uint8_t *source = tmpImage->RGBImageLock();
|
|
|
- if (source == NULL) {
|
|
|
+ if (source == NULL)
|
|
|
+ {
|
|
|
delete tmpImage;
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "CaptureToBasisImage: decoded image lock failed");
|
|
|
return ESP_FAIL;
|
|
|
}
|
|
|
+
|
|
|
const bool copied = _Image->CopyFromMemory(source, static_cast<int>(expectedBytes));
|
|
|
tmpImage->RGBImageRelease();
|
|
|
delete tmpImage;
|