web: fix the crash of /saved-photo.jpg, the photo is sent from a copy in PSRAM
parent
8bad94ce4f
commit
0109e99c14
|
|
@ -44,36 +44,27 @@ void Server_InitWebServer() {
|
|||
request->send(404, "text/plain", "Photo not found!");
|
||||
return;
|
||||
}
|
||||
SystemCamera.SetPhotoSending(true);
|
||||
|
||||
SystemLog.AddEvent(LogLevel_Verbose, "Photo size: " + String(SystemCamera.GetPhotoFb()->len) + " bytes");
|
||||
|
||||
if (SystemCamera.GetPhotoExifData()->header != NULL) {
|
||||
/* send photo with exif data */
|
||||
SystemLog.AddEvent(LogLevel_Verbose, F("Send photo with EXIF data"));
|
||||
size_t total_len = SystemCamera.GetPhotoExifData()->len + SystemCamera.GetPhotoFb()->len - SystemCamera.GetPhotoExifData()->offset;
|
||||
auto response = request->beginChunkedResponse("image/jpg", [total_len](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
|
||||
size_t len = 0;
|
||||
if (index < SystemCamera.GetPhotoExifData()->len) {
|
||||
len = min(maxLen, SystemCamera.GetPhotoExifData()->len - index);
|
||||
memcpy(buffer, SystemCamera.GetPhotoExifData()->header + index, len);
|
||||
} else {
|
||||
size_t offset = index - SystemCamera.GetPhotoExifData()->len + SystemCamera.GetPhotoExifData()->offset;
|
||||
len = min(maxLen, SystemCamera.GetPhotoFb()->len - offset);
|
||||
memcpy(buffer, SystemCamera.GetPhotoFb()->buf + offset, len);
|
||||
}
|
||||
return len;
|
||||
});
|
||||
response->addHeader("Content-Length", String(total_len));
|
||||
request->send(response);
|
||||
|
||||
} else {
|
||||
/* send photo without exif data */
|
||||
SystemLog.AddEvent(LogLevel_Verbose, F("Send photo without EXIF data"));
|
||||
request->send(200, "image/jpg", SystemCamera.GetPhotoFb()->buf, SystemCamera.GetPhotoFb()->len);
|
||||
/* copy of the photo (EXIF + JPEG) in PSRAM under the frame buffer semaphore. The response is sent later from async_tcp,
|
||||
the photo task or the stream can capture a new photo into the frame buffer during sending */
|
||||
uint8_t *photo = NULL;
|
||||
size_t photo_len = 0;
|
||||
if (false == SystemCamera.CopyPhoto(&photo, &photo_len, WEB_PHOTO_COPY_TIMEOUT)) {
|
||||
request->send(503, "text/plain", "Photo is not available, try again");
|
||||
return;
|
||||
}
|
||||
SystemLog.AddEvent(LogLevel_Verbose, "Photo size: " + String(photo_len) + " bytes");
|
||||
|
||||
SystemCamera.SetPhotoSending(false);
|
||||
/* the copy is released together with the response */
|
||||
std::shared_ptr<uint8_t> photo_copy(photo, heap_caps_free);
|
||||
request->send("image/jpg", photo_len, [photo_copy, photo_len](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
|
||||
if (index >= photo_len) {
|
||||
return 0;
|
||||
}
|
||||
size_t len = min(maxLen, photo_len - index);
|
||||
memcpy(buffer, photo_copy.get() + index, len);
|
||||
return len;
|
||||
});
|
||||
});
|
||||
|
||||
/* route to jquery */
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <memory>
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
|
|
|||
|
|
@ -756,6 +756,44 @@ void Camera::CopyPhoto(camera_fb_t* i_data) {
|
|||
*i_data = *FrameBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Copy the last photo (EXIF header + JPEG data) to PSRAM under the frame buffer semaphore.
|
||||
The copy is sent by the web server, the photo task can capture a new photo during sending
|
||||
@param uint8_t ** - pointer to the copy of the photo. The caller releases it with heap_caps_free()
|
||||
@param size_t * - length of the copy
|
||||
@param uint32_t - maximum waiting for the frame buffer semaphore [ms]
|
||||
@return bool - true = copy created
|
||||
*/
|
||||
bool Camera::CopyPhoto(uint8_t **o_buf, size_t *o_len, uint32_t i_timeout) {
|
||||
*o_buf = NULL;
|
||||
*o_len = 0;
|
||||
|
||||
if (pdTRUE != xSemaphoreTake(frameBufferSemaphore, pdMS_TO_TICKS(i_timeout))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((true == CameraCaptureSuccess) && (NULL != FrameBuffer) && (NULL != FrameBuffer->buf) && (FrameBuffer->len > 0)) {
|
||||
/* EXIF header replaces the beginning of the JPEG data (up to offset) */
|
||||
bool exif = ((NULL != PhotoExifData.header) && (PhotoExifData.offset <= FrameBuffer->len));
|
||||
size_t exif_len = (true == exif) ? PhotoExifData.len : 0;
|
||||
size_t jpeg_offset = (true == exif) ? PhotoExifData.offset : 0;
|
||||
size_t len = exif_len + FrameBuffer->len - jpeg_offset;
|
||||
|
||||
uint8_t *buf = (uint8_t *)heap_caps_malloc(len, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
||||
if (NULL != buf) {
|
||||
if (true == exif) {
|
||||
memcpy(buf, PhotoExifData.header, exif_len);
|
||||
}
|
||||
memcpy(buf + exif_len, FrameBuffer->buf + jpeg_offset, FrameBuffer->len - jpeg_offset);
|
||||
*o_buf = buf;
|
||||
*o_len = len;
|
||||
}
|
||||
}
|
||||
|
||||
xSemaphoreGive(frameBufferSemaphore);
|
||||
return (NULL != *o_buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief Copy Photo
|
||||
@param String* - pointer to string
|
||||
|
|
|
|||
|
|
@ -112,6 +112,7 @@ public:
|
|||
void CopyPhoto(String*);
|
||||
void CopyPhoto(String*, int, int);
|
||||
void CopyPhoto(char*, int, int);
|
||||
bool CopyPhoto(uint8_t **, size_t *, uint32_t);
|
||||
int GetPhotoSize();
|
||||
String GetPhoto();
|
||||
camera_fb_t *GetPhotoFb();
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@
|
|||
#define LOOP_DELAY 100 ///< loop delay [ms]
|
||||
#define WIFI_CLIENT_WAIT_CON false ///< wait for connecting to WiFi network
|
||||
#define WEB_CACHE_INTERVAL 86400 ///< cache interval for browser [s] 86400s = 24h
|
||||
#define WEB_PHOTO_COPY_TIMEOUT 2000 ///< maximum waiting for the copy of the photo for the web server, during capture of the photo [ms]
|
||||
|
||||
/* --------------- OTA UPDATE CFG --------------*/
|
||||
#define OTA_UPDATE_API_SERVER "api.github.com" ///< OTA update server URL
|
||||
|
|
|
|||
Loading…
Reference in New Issue