Fix reboot when opening the MJPEG stream

The camera driver runs with a single frame buffer, but the firmware kept
the last photo in it and returned it to the driver repeatedly (on every
stream frame, on stream start and on stream close). A failed grab also
left the frame-buffer mutex held and streamed a stale buffer.

Photos and stream frames are now copied into PSRAM and the driver buffer
is returned immediately. The stream, the Prusa Connect upload and
/saved-photo.jpg each own their copy, so none can free a frame another
is still sending. Stream clients are counted, a failed capture ends the
stream cleanly, and web-side waits on the camera lock time out after 3 s
instead of blocking the async_tcp task.

Tested on a XIAO ESP32-S3 Sense: two overlapping streams for 75 s while
Prusa Connect uploads ran, no reboot, all uploads 200 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pull/149/head
Jakob Pless 2026-11-18 14:43:48 +01:00
parent c67d604a14
commit 8c7109e67f
No known key found for this signature in database
7 changed files with 328 additions and 279 deletions

View File

@ -40,39 +40,22 @@ void Server_InitWebServer() {
if (Server_CheckBasicAuth(request) == false)
return;
if (SystemCamera.GetCameraCaptureSuccess() == false) {
/* send a private copy of the photo, so a new capture cannot free it while it is being sent */
size_t PhotoLen = 0;
uint8_t* PhotoCopy = SystemCamera.CopyPhotoBuffer(&PhotoLen);
if (PhotoCopy == NULL) {
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;
});
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);
SystemLog.AddEvent(LogLevel_Verbose, "Photo size: " + String(PhotoLen) + " bytes");
AsyncBufferResponse* response = new AsyncBufferResponse(PhotoCopy, PhotoLen, "image/jpg");
if (response == NULL) {
free(PhotoCopy);
request->send(501);
return;
}
SystemCamera.SetPhotoSending(false);
request->send(response);
});
/* route to jquery */

View File

@ -78,6 +78,12 @@ size_t AsyncBufferResponse::_fillBuffer(uint8_t *buf, size_t maxLen) {
* @return size_t
*/
size_t AsyncBufferResponse::_content(uint8_t *buffer, size_t maxLen, size_t index) {
if ((_buf == nullptr) || (index >= _len)) {
return 0;
}
if (maxLen > (_len - index)) {
maxLen = _len - index;
}
memcpy(buffer, _buf + index, maxLen);
if ((index + maxLen) == _len) {
free(_buf);
@ -178,8 +184,9 @@ AsyncJpegStreamResponse::AsyncJpegStreamResponse(Camera *i_cam, Logs *i_log) {
*
*/
AsyncJpegStreamResponse::~AsyncJpegStreamResponse() {
Camera::FreeFrame(_frame.fb);
_frame.fb = NULL;
camera->SetStreamStatus(false);
camera->CaptureReturnFrameBuffer();
}
/**
@ -230,10 +237,12 @@ size_t AsyncJpegStreamResponse::_content(uint8_t *buffer, size_t maxLen, size_t
sprintf(buf, "Size: %uKB, FPS: %.1f", _frame.fb->len / 1024, fps);
Serial.println(buf);
lastAsyncRequest = end;
camera->CaptureReturnFrameBuffer();
_frame.fb = NULL;
}
/* release the frame that was sent */
Camera::FreeFrame(_frame.fb);
_frame.fb = NULL;
/* check space for headers */
if (maxLen < (strlen(STREAM_BOUNDARY) + strlen(STREAM_PART) + strlen(JPG_CONTENT_TYPE) + 8)) {
log->AddEvent(LogLevel_Error, F("Stream Not space for headers"));
@ -242,10 +251,10 @@ size_t AsyncJpegStreamResponse::_content(uint8_t *buffer, size_t maxLen, size_t
/* get frame */
_frame.index = 0;
camera->CaptureStream(&_dframe);
_frame.fb = &_dframe;
_frame.fb = camera->CaptureStream();
if (_frame.fb == NULL) {
/* end the stream, the client can reconnect */
log->AddEvent(LogLevel_Error, F("Stream capture frame failed"));
return 0;
}

View File

@ -65,7 +65,6 @@ private:
uint64_t lastAsyncRequest; ///< last async request
Camera *camera; ///< pointer to camera
Logs *log; ///< pointer to logs
camera_fb_t _dframe; ///< camera frame
public:
AsyncJpegStreamResponse(Camera *, Logs *);

View File

@ -28,20 +28,15 @@ Camera::Camera(Configuration* i_conf, Logs* i_log, int8_t i_FlashPin) {
CameraFlashPin = i_FlashPin;
StreamOnOff = false;
StreamClients = 0;
frameBufferSemaphore = xSemaphoreCreateMutex();
/* Allocate memory for the frame buffer */
FrameBufferDuplicate = (camera_fb_t*)heap_caps_malloc(sizeof(camera_fb_t), MALLOC_CAP_SPIRAM);
if (FrameBufferDuplicate != NULL) {
memset(FrameBufferDuplicate, 0, sizeof(camera_fb_t));
FrameBufferDuplicate->buf = NULL;
}
/* Allocate memory for the frame buffer */
FrameBufferExif = (camera_fb_t*)heap_caps_malloc(sizeof(camera_fb_t), MALLOC_CAP_SPIRAM);
if (FrameBufferExif != NULL) {
memset(FrameBufferExif, 0, sizeof(camera_fb_t));
FrameBufferExif->buf = NULL;
}
/* Photo buffers are owned copies; the camera driver frame buffer is never held outside the semaphore */
PhotoFb = NULL;
FrameBufferDuplicate = NULL;
UploadFromStream = false;
UploadPhotoReady = false;
CameraCaptureSuccess = false;
StreamSendingPhoto = false;
PhotoExifData.header = NULL;
@ -227,7 +222,11 @@ framesize_t Camera::TransformFrameSizeDataType(uint8_t i_data) {
@return none
*/
void Camera::SetPhotoSending(bool i_data) {
PhotoSending = i_data;
/* taken under the semaphore, so a capture running in another task sees it before it replaces the photo */
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
PhotoSending = i_data;
xSemaphoreGive(frameBufferSemaphore);
}
}
/**
@ -387,85 +386,109 @@ void Camera::CapturePhoto() {
return;
}
CameraCaptureSuccess = false;
/* check flash, and enable FLASH LED */
if (true == CameraFlashEnable) {
SetFlashStatus(true);
delay(CameraFlashTime);
}
if (FrameBuffer) {
esp_camera_fb_return(FrameBuffer);
}
/* Capturing a training photo. Without this sequence, the camera will not obtain the current photo but photo from the previous cycle. */
FrameBuffer = esp_camera_fb_get();
if (FrameBuffer) {
esp_camera_fb_return(FrameBuffer);
camera_fb_t* fb = esp_camera_fb_get();
if (fb) {
esp_camera_fb_return(fb);
log->AddEvent(LogLevel_Verbose, F("Camera capture training photo"));
} else {
esp_camera_fb_return(FrameBuffer);
log->AddEvent(LogLevel_Error, F("Camera capture failed training photo"));
//ReinitCameraModule();
}
int attempts = 0;
const int maxAttempts = 5;
PhotoExifData.header = NULL;
do {
camera_fb_t* NewPhoto = NULL;
for (int attempts = 0; (attempts < maxAttempts) && (NewPhoto == NULL); attempts++) {
log->AddEvent(LogLevel_Info, F("Taking photo..."));
FrameBuffer = esp_camera_fb_get();
if (!FrameBuffer) {
fb = esp_camera_fb_get();
if (!fb) {
CameraCaptureFailedCounter++;
log->AddEvent(LogLevel_Error, F("Camera capture failed! photo. Attempt: "), String(CameraCaptureFailedCounter));
xSemaphoreGive(frameBufferSemaphore); // Release semaphore before returning
return;
break;
}
char buf[150] = { '\0' };
uint8_t ControlFlag = (uint8_t)FrameBuffer->buf[15];
sprintf(buf, "The picture has been saved. Size: %d bytes, Photo resolution: %zu x %zu", FrameBuffer->len, FrameBuffer->width, FrameBuffer->height);
sprintf(buf, "The picture has been saved. Size: %d bytes, Photo resolution: %zu x %zu", fb->len, fb->width, fb->height);
log->AddEvent(LogLevel_Info, buf);
if (ControlFlag != 0x00) {
uint8_t ControlFlag = (fb->len > 15) ? (uint8_t)fb->buf[15] : 0xFF;
if ((fb->len <= 100) || (ControlFlag != 0x00)) {
log->AddEvent(LogLevel_Error, "Camera capture failed! flag: " + String(ControlFlag, HEX));
FrameBuffer->len = 0;
} else {
log->AddEvent(LogLevel_Info, F("Photo OK! "), String(ControlFlag, HEX));
CameraCaptureFailedCounter = 0;
/* generate exif header */
/* generate exif header and merge it into an owned copy of the photo */
const uint8_t* ExifHeader = NULL;
size_t ExifLen = 0;
update_exif_from_cfg(imageExifRotation);
get_exif_header(FrameBuffer, &PhotoExifData.header, &PhotoExifData.len);
PhotoExifData.offset = get_jpeg_data_offset(FrameBuffer);
CameraCaptureSuccess = true;
if (PhotoExifData.header != NULL) {
log->AddEvent(LogLevel_Info, F("Exif header OK! Len: "), String(PhotoExifData.len));
get_exif_header(fb, &ExifHeader, &ExifLen);
size_t JpegOffset = get_jpeg_data_offset(fb);
if ((ExifHeader == NULL) || (JpegOffset == 0)) {
log->AddEvent(LogLevel_Error, F("Exif header failed! "), String(ExifLen));
ExifHeader = NULL;
ExifLen = 0;
JpegOffset = 0;
} else {
log->AddEvent(LogLevel_Error, F("Exif header failed! "), String(PhotoExifData.len));
log->AddEvent(LogLevel_Info, F("Exif header OK! Len: "), String(ExifLen));
}
NewPhoto = CloneFrame(fb, ExifHeader, ExifLen, JpegOffset);
if (NewPhoto == NULL) {
log->AddEvent(LogLevel_Error, F("Failed to allocate memory for photo"));
}
}
attempts++;
if (attempts >= maxAttempts) {
log->AddEvent(LogLevel_Error, F("Failed to capture a valid photo after max attempts"));
break;
}
} while (!(FrameBuffer->len > 100));
/* the driver frame buffer is always returned before the next capture */
esp_camera_fb_return(fb);
fb = NULL;
}
if (NewPhoto == NULL) {
log->AddEvent(LogLevel_Error, F("Failed to capture a valid photo after max attempts"));
}
/* Disable flash */
if (true == CameraFlashEnable) {
SetFlashStatus(false);
}
if (NewPhoto != NULL) {
if (true == PhotoSending) {
/* photo is being sent right now, keep the old buffer alive */
FreeFrame(NewPhoto);
log->AddEvent(LogLevel_Info, F("Photo is being sent, new photo dropped"));
} else {
FreeFrame(PhotoFb);
PhotoFb = NewPhoto;
CameraCaptureSuccess = true;
UploadFromStream = false;
UploadPhotoReady = true;
}
} else {
CameraCaptureSuccess = false;
UploadPhotoReady = false;
}
xSemaphoreGive(frameBufferSemaphore);
} else {
/* Stream is on, set flag for sending photo */
/* Stream is on, freeze the last stream frame for sending photo */
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
StreamSendingPhoto = true;
if ((FrameBufferDuplicate != NULL) && (FrameBufferDuplicate->len > 0)) {
StreamSendingPhoto = true;
UploadFromStream = true;
UploadPhotoReady = true;
} else {
UploadPhotoReady = false;
log->AddEvent(LogLevel_Warning, F("Stream is on, but no stream frame is available yet"));
}
xSemaphoreGive(frameBufferSemaphore);
}
}
@ -479,151 +502,152 @@ void Camera::CapturePhoto() {
}
/**
@brief Capture Stream
@param camera_fb_t * - pointer to camera_fb_t
@brief Copy a frame into newly allocated PSRAM, optionally replacing the JPEG header with an EXIF header
@param camera_fb_t* - source frame
@param const uint8_t* - EXIF header, or NULL to copy the frame as-is
@param size_t - EXIF header length
@param size_t - offset of the JPEG data in the source frame (used only with EXIF header)
@return camera_fb_t* - owned copy, free with FreeFrame(). NULL on allocation failure
*/
camera_fb_t* Camera::CloneFrame(const camera_fb_t* i_src, const uint8_t* i_hdr, size_t i_hdrLen, size_t i_offset) {
if ((i_src == NULL) || (i_src->buf == NULL)) {
return NULL;
}
if ((i_hdr == NULL) || (i_offset >= i_src->len)) {
i_hdr = NULL;
i_hdrLen = 0;
i_offset = 0;
}
camera_fb_t* out = (camera_fb_t*)heap_caps_malloc(sizeof(camera_fb_t), MALLOC_CAP_SPIRAM);
if (out == NULL) {
return NULL;
}
memcpy(out, i_src, sizeof(camera_fb_t));
size_t totalSize = i_hdrLen + i_src->len - i_offset;
out->buf = (uint8_t*)heap_caps_malloc(totalSize, MALLOC_CAP_SPIRAM);
if (out->buf == NULL) {
free(out);
return NULL;
}
if (i_hdr != NULL) {
memcpy(out->buf, i_hdr, i_hdrLen);
}
memcpy(out->buf + i_hdrLen, i_src->buf + i_offset, i_src->len - i_offset);
out->len = totalSize;
return out;
}
/**
@brief Free a frame created by CloneFrame()
@param camera_fb_t* - frame, may be NULL
@return none
*/
void Camera::CaptureStream(camera_fb_t* i_buf) {
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
do {
/* capture final photo */
FrameBuffer = esp_camera_fb_get();
if (!FrameBuffer) {
log->AddEvent(LogLevel_Error, F("Camera capture failed! stream"));
i_buf = NULL;
return;
}
/* check if photo is correctly saved */
} while (!(FrameBuffer->len > 100));
/* check if the photo is rotated */
bool ExifStatus = false;
#if (true == CAMERA_EXIF_ROTATION_STREAM)
if (1 != imageExifRotation) { /* 1 = image rotation 0 degree */
/* generate exif header */
update_exif_from_cfg(imageExifRotation);
get_exif_header(FrameBuffer, &PhotoExifData.header, &PhotoExifData.len);
PhotoExifData.offset = get_jpeg_data_offset(FrameBuffer);
if (PhotoExifData.header != NULL) {
/* memory allocation release */
if (FrameBufferExif != NULL) {
if (FrameBufferExif->buf != NULL) {
free(FrameBufferExif->buf);
FrameBufferExif->buf = NULL; /* Set to NULL after freeing */
}
free(FrameBufferExif);
FrameBufferExif = NULL; /* Set to NULL after freeing */
}
/* Allocate memory for the duplicate frame structure */
FrameBufferExif = (camera_fb_t*)heap_caps_malloc(sizeof(camera_fb_t), MALLOC_CAP_SPIRAM);
/* Calculate the total size of the buffer */
size_t totalSize = PhotoExifData.len + FrameBuffer->len - PhotoExifData.offset;
/* Allocate memory for the image data */
FrameBufferExif->buf = (uint8_t*)heap_caps_malloc(totalSize, MALLOC_CAP_SPIRAM);
if (FrameBufferExif->buf == NULL) {
log->AddEvent(LogLevel_Error, F("Failed to allocate memory for EXIF buffer"));
return;
}
/* copy the EXIF data to the buffer */
memcpy(FrameBufferExif->buf, PhotoExifData.header, PhotoExifData.len);
/* copy the image data to the buffer */
memcpy(FrameBufferExif->buf + PhotoExifData.len, FrameBuffer->buf + PhotoExifData.offset, FrameBuffer->len - PhotoExifData.offset);
/* Set the length of the buffer */
FrameBufferExif->len = totalSize;
*i_buf = *FrameBufferExif;
ExifStatus = true;
} else {
/* copy the frame buffer to the buffer */
*i_buf = *FrameBuffer;
}
} else {
/* copy the frame buffer to the buffer */
*i_buf = *FrameBuffer;
void Camera::FreeFrame(camera_fb_t* i_fb) {
if (i_fb != NULL) {
if (i_fb->buf != NULL) {
free(i_fb->buf);
}
#else
*i_buf = *FrameBuffer;
#endif
/* copy the frame buffer to the duplicate frame buffer. For sending photo to Prusa Connect */
if (false == StreamSendingPhoto) {
/* memory allocation release */
if (FrameBufferDuplicate != NULL) {
if (FrameBufferDuplicate->buf != NULL) {
free(FrameBufferDuplicate->buf);
FrameBufferDuplicate->buf = NULL; /* Set to NULL after freeing */
}
free(FrameBufferDuplicate);
FrameBufferDuplicate = NULL; /* Set to NULL after freeing */
}
/* Allocate memory for the duplicate frame structure */
FrameBufferDuplicate = (camera_fb_t*)heap_caps_malloc(sizeof(camera_fb_t), MALLOC_CAP_SPIRAM);
/* Copy the metadata */
if (true == ExifStatus) {
memcpy(FrameBufferDuplicate, FrameBufferExif, sizeof(camera_fb_t));
} else {
memcpy(FrameBufferDuplicate, FrameBuffer, sizeof(camera_fb_t));
}
/* Allocate memory for the image data */
if (true == ExifStatus) {
FrameBufferDuplicate->buf = (uint8_t*)heap_caps_malloc(FrameBufferExif->len, MALLOC_CAP_SPIRAM);
} else {
FrameBufferDuplicate->buf = (uint8_t*)heap_caps_malloc(FrameBuffer->len, MALLOC_CAP_SPIRAM);
}
/* Check if memory allocation was successful */
if (!FrameBufferDuplicate->buf) {
/* Handle error */
free(FrameBufferDuplicate);
Serial.println("Failed to allocate memory for the duplicate frame buffer");
} else {
/* Copy the image data */
if (true == ExifStatus) {
memcpy(FrameBufferDuplicate->buf, FrameBufferExif->buf, FrameBufferExif->len);
} else {
memcpy(FrameBufferDuplicate->buf, FrameBuffer->buf, FrameBuffer->len);
}
}
}
xSemaphoreGive(frameBufferSemaphore);
free(i_fb);
}
}
/**
@brief Capture Return Frame Buffer
@brief Capture Stream frame
@param none
@return camera_fb_t* - owned copy of the frame, the caller frees it with Camera::FreeFrame(). NULL on failure
*/
camera_fb_t* Camera::CaptureStream() {
/* bounded wait, runs in the async_tcp task which must not block on a long photo capture */
if (!xSemaphoreTake(frameBufferSemaphore, pdMS_TO_TICKS(CAMERA_WEB_LOCK_TIMEOUT_MS))) {
log->AddEvent(LogLevel_Warning, F("Stream frame skipped, camera busy"));
return NULL;
}
camera_fb_t* fb = NULL;
for (int attempts = 0; attempts < 3; attempts++) {
fb = esp_camera_fb_get();
if ((fb == NULL) || (fb->len > 100)) {
break;
}
/* invalid frame, return it and try again */
esp_camera_fb_return(fb);
fb = NULL;
}
if (fb == NULL) {
log->AddEvent(LogLevel_Error, F("Camera capture failed! stream"));
xSemaphoreGive(frameBufferSemaphore);
return NULL;
}
/* generate exif header with image rotation */
const uint8_t* ExifHeader = NULL;
size_t ExifLen = 0;
size_t JpegOffset = 0;
if (1 != imageExifRotation) { /* 1 = image rotation 0 degree */
update_exif_from_cfg(imageExifRotation);
get_exif_header(fb, &ExifHeader, &ExifLen);
JpegOffset = get_jpeg_data_offset(fb);
if (JpegOffset == 0) {
ExifHeader = NULL;
}
}
/* copy of the frame for the stream client */
#if (true == CAMERA_EXIF_ROTATION_STREAM)
camera_fb_t* StreamFrame = CloneFrame(fb, ExifHeader, ExifLen, JpegOffset);
#else
camera_fb_t* StreamFrame = CloneFrame(fb, NULL, 0, 0);
#endif
if (StreamFrame == NULL) {
log->AddEvent(LogLevel_Error, F("Failed to allocate memory for stream frame"));
}
/* copy of the frame for sending photo to Prusa Connect. Kept unchanged while it is being sent */
if ((false == StreamSendingPhoto) && (false == PhotoSending)) {
camera_fb_t* dup = CloneFrame(fb, ExifHeader, ExifLen, JpegOffset);
if (dup != NULL) {
FreeFrame(FrameBufferDuplicate);
FrameBufferDuplicate = dup;
} else {
log->AddEvent(LogLevel_Error, F("Failed to allocate memory for the duplicate frame buffer"));
}
}
/* the driver frame buffer is returned immediately, so the sensor can capture the next frame */
esp_camera_fb_return(fb);
xSemaphoreGive(frameBufferSemaphore);
return StreamFrame;
}
/**
@brief Capture Return Frame Buffer. Kept for compatibility, frames are no longer held outside the camera class
@param none
@return none
*/
void Camera::CaptureReturnFrameBuffer() {
esp_camera_fb_return(FrameBuffer);
}
/**
@brief Set Stream Status
@param bool - true = on, false = off
@brief Set Stream Status. Counts stream clients, the stream is on while at least one client is connected
@param bool - true = client connected, false = client disconnected
@return none
*/
void Camera::SetStreamStatus(bool i_status) {
StreamOnOff = i_status;
if (FrameBuffer) {
esp_camera_fb_return(FrameBuffer);
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
if (true == i_status) {
StreamClients++;
} else if (StreamClients > 0) {
StreamClients--;
}
StreamOnOff = (StreamClients > 0);
xSemaphoreGive(frameBufferSemaphore);
}
log->AddEvent(LogLevel_Info, F("Camera video stream: "), String(StreamOnOff));
}
@ -703,8 +727,10 @@ void Camera::StreamSetSendingPhoto(bool i_data) {
*/
String Camera::GetPhoto() {
Photo = "";
for (size_t i = 0; i < FrameBuffer->len; i++) {
Photo += (char)FrameBuffer->buf[i];
if (PhotoFb != NULL) {
for (size_t i = 0; i < PhotoFb->len; i++) {
Photo += (char)PhotoFb->buf[i];
}
}
return Photo;
}
@ -715,7 +741,7 @@ String Camera::GetPhoto() {
@return camera_fb_t* - photo frame buffer
*/
camera_fb_t* Camera::GetPhotoFb() {
return FrameBuffer;
return PhotoFb;
}
/**
@ -727,6 +753,53 @@ camera_fb_t* Camera::GetPhotoFbDuplicate() {
return FrameBufferDuplicate;
}
/**
@brief Get the photo for sending to Prusa Connect, selected by the last CapturePhoto()
@param none
@return camera_fb_t* - photo, or NULL if no photo is available
*/
camera_fb_t* Camera::GetUploadPhotoFb() {
camera_fb_t* ret = NULL;
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
if (true == UploadPhotoReady) {
ret = (true == UploadFromStream) ? FrameBufferDuplicate : PhotoFb;
}
xSemaphoreGive(frameBufferSemaphore);
}
return ret;
}
/**
@brief Check if the photo for upload comes from the stream
@param none
@return bool - true = stream frame, false = captured photo
*/
bool Camera::GetUploadFromStream() {
return UploadFromStream;
}
/**
@brief Copy the last photo into a new buffer
@param size_t* - output length
@return uint8_t* - buffer allocated with malloc, the caller frees it. NULL if no photo
*/
uint8_t* Camera::CopyPhotoBuffer(size_t* o_len) {
uint8_t* ret = NULL;
*o_len = 0;
/* bounded wait, runs in the async_tcp task */
if (xSemaphoreTake(frameBufferSemaphore, pdMS_TO_TICKS(CAMERA_WEB_LOCK_TIMEOUT_MS))) {
if ((true == CameraCaptureSuccess) && (PhotoFb != NULL) && (PhotoFb->len > 0)) {
ret = (uint8_t*)heap_caps_malloc(PhotoFb->len, MALLOC_CAP_SPIRAM);
if (ret != NULL) {
memcpy(ret, PhotoFb->buf, PhotoFb->len);
*o_len = PhotoFb->len;
}
}
xSemaphoreGive(frameBufferSemaphore);
}
return ret;
}
/**
@brief Get Photo Exif Data
@param none
@ -742,7 +815,9 @@ PhotoExifData_t* Camera::GetPhotoExifData() {
@return none
*/
void Camera::CopyPhoto(camera_fb_t* i_data) {
*i_data = *FrameBuffer;
if (PhotoFb != NULL) {
*i_data = *PhotoFb;
}
}
/**
@ -752,8 +827,10 @@ void Camera::CopyPhoto(camera_fb_t* i_data) {
*/
void Camera::CopyPhoto(String* i_data) {
Photo = "";
for (size_t i = 0; i < FrameBuffer->len; i++) {
Photo += (char)FrameBuffer->buf[i];
if (PhotoFb != NULL) {
for (size_t i = 0; i < PhotoFb->len; i++) {
Photo += (char)PhotoFb->buf[i];
}
}
*i_data = Photo;
}
@ -769,7 +846,7 @@ void Camera::CopyPhoto(String* i_data, int i_from, int i_to) {
*i_data = "";
for (int i = i_from; i < i_to; i++) {
*i_data += (char)FrameBuffer->buf[i];
*i_data += (char)PhotoFb->buf[i];
}
}
@ -782,7 +859,7 @@ void Camera::CopyPhoto(String* i_data, int i_from, int i_to) {
*/
void Camera::CopyPhoto(char* i_data, int i_from, int i_to) {
int length = i_to - i_from;
memcpy(i_data, FrameBuffer->buf + i_from, length);
memcpy(i_data, PhotoFb->buf + i_from, length);
i_data[length + 1] = '\0';
}
@ -792,8 +869,11 @@ void Camera::CopyPhoto(char* i_data, int i_from, int i_to) {
* @return int - photo size
*/
int Camera::GetPhotoSize() {
log->AddEvent(LogLevel_Verbose, F("Photo size: "), String(FrameBuffer->len));
return (int)FrameBuffer->len;
if (PhotoFb == NULL) {
return 0;
}
log->AddEvent(LogLevel_Verbose, F("Photo size: "), String(PhotoFb->len));
return (int)PhotoFb->len;
}
/**

View File

@ -11,6 +11,8 @@
#pragma once
#define CAMERA_WEB_LOCK_TIMEOUT_MS 3000 ///< max wait for the frame buffer lock in web server handlers [ms]
#include "esp_camera.h"
#include "esp_timer.h"
#include "img_converters.h"
@ -66,12 +68,14 @@ private:
/* OV2640 camera module pinout and cfg*/
camera_config_t CameraConfig; ///< camera configuration
camera_fb_t *FrameBuffer; ///< frame buffer
camera_fb_t *FrameBufferDuplicate; ///< frame buffer duplicate
camera_fb_t *FrameBufferExif; ///< frame buffer with exif data
camera_fb_t *PhotoFb; ///< last captured photo, owned copy (EXIF merged in)
camera_fb_t *FrameBufferDuplicate; ///< last stream frame, owned copy, for sending to Prusa Connect during stream
bool UploadFromStream; ///< true = next upload uses FrameBufferDuplicate, false = PhotoFb
bool UploadPhotoReady; ///< photo for upload is available
sensor_t* sensor; ///< sensor
String Photo; ///< photo in string format
bool StreamOnOff; ///< stream on/off
uint8_t StreamClients; ///< number of active stream clients
bool StreamSendingPhoto; ///< sending photo to Prusa Connect during stream
SemaphoreHandle_t frameBufferSemaphore; ///< semaphore for frame buffer
float StreamAverageFps; ///< stream average fps
@ -85,6 +89,7 @@ private:
Logs *log; ///< pointer to Logs object
void InitCameraModule();
camera_fb_t *CloneFrame(const camera_fb_t *, const uint8_t *, size_t, size_t);
public:
Camera(Configuration*, Logs*, int8_t);
@ -95,7 +100,8 @@ public:
void ReinitCameraModule();
void GetCameraModel();
void CapturePhoto();
void CaptureStream(camera_fb_t *);
camera_fb_t *CaptureStream();
static void FreeFrame(camera_fb_t *);
void CaptureReturnFrameBuffer();
void SetStreamStatus(bool);
bool GetStreamStatus();
@ -116,6 +122,9 @@ public:
String GetPhoto();
camera_fb_t *GetPhotoFb();
camera_fb_t* GetPhotoFbDuplicate();
camera_fb_t* GetUploadPhotoFb();
bool GetUploadFromStream();
uint8_t *CopyPhotoBuffer(size_t *);
PhotoExifData_t * GetPhotoExifData();
framesize_t TransformFrameSizeDataType(uint8_t);
void SetPhotoSending(bool);

View File

@ -24,6 +24,7 @@ PrusaConnect::PrusaConnect(Configuration *i_conf, Logs *i_log, Camera *i_camera,
config = i_conf;
log = i_log;
camera = i_camera;
PhotoToSend = NULL;
wifi = i_wifi;
BackendAvailability = WaitForFirstConnection;
SendDeviceInformationToBackend = true;
@ -124,29 +125,9 @@ bool PrusaConnect::SendDataToBackend(String *i_data, int i_data_length, String i
if (SendPhoto == i_data_type) {
log->AddEvent(LogLevel_Verbose, F("Sendig photo"));
/* get photo buffer */
bool SendWithExif = false;
uint8_t *fbBuf = NULL;
size_t fbLen = 0;
if (camera->GetStreamStatus() == true) {
/* get duplicate photo buffer from camera */
fbBuf = camera->GetPhotoFbDuplicate()->buf;
fbLen = camera->GetPhotoFbDuplicate()->len;
} else {
/* get original photo buffer from camera */
fbBuf = camera->GetPhotoFb()->buf;
fbLen = camera->GetPhotoFb()->len;
}
/* sending exif data */
if ((camera->GetPhotoExifData()->header != NULL) && (camera->GetStreamStatus() == false)) {
SendWithExif = true;
sendet_data += client.write(camera->GetPhotoExifData()->header, camera->GetPhotoExifData()->len);
fbBuf += camera->GetPhotoExifData()->offset;
fbLen -= camera->GetPhotoExifData()->offset;
} else {
SendWithExif = false;
}
/* get photo buffer. EXIF data are already part of the buffer */
uint8_t *fbBuf = (PhotoToSend != NULL) ? PhotoToSend->buf : NULL;
size_t fbLen = (PhotoToSend != NULL) ? PhotoToSend->len : 0;
/* sending photo */
for (size_t i = 0; i < fbLen; i += PHOTO_FRAGMENT_SIZE) {
@ -160,13 +141,7 @@ bool PrusaConnect::SendDataToBackend(String *i_data, int i_data_length, String i
}
}
client.println("\r\n");
/* log message */
if (SendWithExif) {
SystemLog.AddEvent(LogLevel_Verbose, F("Photo with EXIF data sent"));
} else {
SystemLog.AddEvent(LogLevel_Warning, F("Photo without EXIF data sent"));
}
SystemLog.AddEvent(LogLevel_Verbose, F("Photo sent"));
/* sending device information */
} else if (SendInfo == i_data_type) {
@ -236,21 +211,12 @@ bool PrusaConnect::SendDataToBackend(String *i_data, int i_data_length, String i
* @param none
* @return none
*/
void PrusaConnect::SendPhotoToBackend() {
void PrusaConnect::SendPhotoToBackend(camera_fb_t *i_photo) {
log->AddEvent(LogLevel_Info, F("Start sending photo to prusaconnect"));
camera->SetPhotoSending(true);
String Photo = "";
size_t total_len = 0;
if ((camera->GetPhotoExifData()->header != NULL) && (camera->GetStreamStatus() == false)) {
total_len = camera->GetPhotoExifData()->len + camera->GetPhotoFb()->len - camera->GetPhotoExifData()->offset;
} else if (camera->GetStreamStatus() == false) {
total_len = camera->GetPhotoFb()->len;
} else {
total_len = camera->GetPhotoFbDuplicate()->len;
}
SendDataToBackend(&Photo, total_len, F("image/jpg"), F("Photo"), HOST_URL_CAM_PATH, SendPhoto);
camera->SetPhotoSending(false);
PhotoToSend = i_photo;
SendDataToBackend(&Photo, i_photo->len, F("image/jpg"), F("Photo"), HOST_URL_CAM_PATH, SendPhoto);
PhotoToSend = NULL;
}
/**
@ -301,26 +267,28 @@ void PrusaConnect::SendInfoToBackend() {
void PrusaConnect::TakePictureAndSendToBackend() {
camera->CapturePhoto();
/* the photo stays unchanged until SetPhotoSending(false) and StreamSetSendingPhoto(false) */
camera->SetPhotoSending(true);
camera_fb_t *photo = camera->GetUploadPhotoFb();
/* check if photo was captured */
if (camera->GetCameraCaptureSuccess() == true) {
if ((photo != NULL) && (photo->buf != NULL) && (photo->len > 0)) {
/* send photo to backend */
SendPhotoToBackend();
SendPhotoToBackend(photo);
/* save photo to SD card */
if (false == camera->GetStreamStatus()) {
if (false == camera->GetUploadFromStream()) {
SavePhotoToSdCard();
}
} else {
log->AddEvent(LogLevel_Error, F("Error capturing photo. Stop sending to backend!"));
}
/* return frame buffer */
if (camera->GetStreamStatus() == true) {
/* set stream flag for sending photo to false */
camera->StreamSetSendingPhoto(false);
}
camera->SetPhotoSending(false);
/* release the frozen stream frame */
camera->StreamSetSendingPhoto(false);
}
/**

View File

@ -32,6 +32,7 @@ class Camera;
class PrusaConnect {
private:
camera_fb_t *PhotoToSend; ///< photo being sent to backend
uint8_t RefreshInterval; ///< interval for sending photo to backend
String BackendReceivedStatus; ///< status of backend response
BackendAvailabilitStatus BackendAvailability; ///< status of backend availability
@ -58,7 +59,7 @@ public:
void LoadCfgFromEeprom();
void TakePicture();
void SendPhotoToBackend();
void SendPhotoToBackend(camera_fb_t *);
void SendInfoToBackend();
void TakePictureAndSendToBackend();
String ProcessHttpResponseCode(int);