🪄 Formats CameraService

This commit is contained in:
Rune Harlyk
2024-07-09 20:03:28 +02:00
committed by Rune Harlyk
parent 74c2285800
commit bbc7498653
3 changed files with 43 additions and 59 deletions
+18 -27
View File
@@ -1,10 +1,8 @@
#include <CameraService.h> #include <CameraService.h>
static const char *_STREAM_CONTENT_TYPE = static const char *_STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
"multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n"; static const char *_STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char *_STREAM_PART = static const char *_STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
"Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
SemaphoreHandle_t cameraMutex = xSemaphoreCreateMutex(); SemaphoreHandle_t cameraMutex = xSemaphoreCreateMutex();
@@ -27,24 +25,16 @@ sensor_t *safe_sensor_get() {
void safe_sensor_return() { xSemaphoreGive(cameraMutex); } void safe_sensor_return() { xSemaphoreGive(cameraMutex); }
CameraService::CameraService(PsychicHttpServer *server, CameraService::CameraService(PsychicHttpServer *server, TaskManager *taskManager, SecurityManager *securityManager)
TaskManager *taskManager, : _server(server), _taskManager(taskManager), _securityManager(securityManager) {}
SecurityManager *securityManager)
: _server(server),
_taskManager(taskManager),
_securityManager(securityManager) {}
void CameraService::begin() { void CameraService::begin() {
InitializeCamera(); InitializeCamera();
_server->on( _server->on(STILL_SERVICE_PATH, HTTP_GET,
STILL_SERVICE_PATH, HTTP_GET, _securityManager->wrapRequest(std::bind(&CameraService::cameraStill, this, std::placeholders::_1),
_securityManager->wrapRequest( AuthenticationPredicates::IS_AUTHENTICATED));
std::bind(&CameraService::cameraStill, this, std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
_server->on(STREAM_SERVICE_PATH, HTTP_GET, _server->on(STREAM_SERVICE_PATH, HTTP_GET,
_securityManager->wrapRequest( _securityManager->wrapRequest(std::bind(&CameraService::cameraStream, this, std::placeholders::_1),
std::bind(&CameraService::cameraStream, this, AuthenticationPredicates::IS_AUTHENTICATED));
std::placeholders::_1),
AuthenticationPredicates::IS_AUTHENTICATED));
ESP_LOGV("CameraService", "Registered GET endpoint: %s", STILL_SERVICE_PATH); ESP_LOGV("CameraService", "Registered GET endpoint: %s", STILL_SERVICE_PATH);
ESP_LOGV("CameraService", "Registered GET endpoint: %s", STREAM_SERVICE_PATH); ESP_LOGV("CameraService", "Registered GET endpoint: %s", STREAM_SERVICE_PATH);
@@ -89,8 +79,10 @@ esp_err_t CameraService::InitializeCamera() {
log_i("Initializing camera"); log_i("Initializing camera");
esp_err_t err = esp_camera_init(&camera_config); esp_err_t err = esp_camera_init(&camera_config);
if (err == ESP_OK) ESP_LOGI("CameraService", "Camera probe successful"); if (err == ESP_OK)
else ESP_LOGE("CameraService", "Camera probe failed with error 0x%x", err); ESP_LOGI("CameraService", "Camera probe successful");
else
ESP_LOGE("CameraService", "Camera probe failed with error 0x%x", err);
return err; return err;
} }
@@ -102,8 +94,7 @@ esp_err_t CameraService::cameraStill(PsychicRequest *request) {
request->reply(500, "text/plain", "Camera capture failed"); request->reply(500, "text/plain", "Camera capture failed");
return ESP_FAIL; return ESP_FAIL;
} }
PsychicStreamResponse response = PsychicStreamResponse response = PsychicStreamResponse(request, "image/jpeg", "capture.jpg");
PsychicStreamResponse(request, "image/jpeg", "capture.jpg");
response.beginSend(); response.beginSend();
response.write(fb->buf, fb->len); response.write(fb->buf, fb->len);
esp_camera_fb_return(fb); esp_camera_fb_return(fb);
@@ -113,7 +104,7 @@ esp_err_t CameraService::cameraStill(PsychicRequest *request) {
void streamTask(void *pv) { void streamTask(void *pv) {
esp_err_t res = ESP_OK; esp_err_t res = ESP_OK;
PsychicRequest *request = static_cast<PsychicRequest*>(pv); PsychicRequest *request = static_cast<PsychicRequest *>(pv);
httpd_req_t *copy = nullptr; httpd_req_t *copy = nullptr;
res = httpd_req_async_handler_begin(request->request(), &copy); res = httpd_req_async_handler_begin(request->request(), &copy);
@@ -139,9 +130,9 @@ void streamTask(void *pv) {
if (!fb) { if (!fb) {
ESP_LOGE("Stream", "Camera capture failed"); ESP_LOGE("Stream", "Camera capture failed");
break; break;
} }
if(fb->format != PIXFORMAT_JPEG){ if (fb->format != PIXFORMAT_JPEG) {
if(!frame2jpg(fb, 80, &buf, &buf_len)) break; if (!frame2jpg(fb, 80, &buf, &buf_len)) break;
} else { } else {
buf_len = fb->len; buf_len = fb->len;
buf = fb->buf; buf = fb->buf;
+3 -4
View File
@@ -11,7 +11,7 @@
#include <Features.h> #include <Features.h>
#if FT_ENABLED(FT_CAMERA) #if FT_ENABLED(FT_CAMERA)
#include <CameraPins.h> #include <CameraPins.h>
#endif #endif
#define STREAM_SERVICE_PATH "/api/camera/stream" #define STREAM_SERVICE_PATH "/api/camera/stream"
@@ -20,11 +20,10 @@
#define PART_BOUNDARY "frame" #define PART_BOUNDARY "frame"
camera_fb_t *safe_camera_fb_get(); camera_fb_t *safe_camera_fb_get();
sensor_t* safe_sensor_get(); sensor_t *safe_sensor_get();
void safe_sensor_return(); void safe_sensor_return();
class CameraService class CameraService {
{
public: public:
CameraService(PsychicHttpServer *server, TaskManager *taskManager, SecurityManager *securityManager); CameraService(PsychicHttpServer *server, TaskManager *taskManager, SecurityManager *securityManager);
@@ -17,29 +17,29 @@
#define CAMERA_SETTINGS_PATH "/api/camera/settings" #define CAMERA_SETTINGS_PATH "/api/camera/settings"
class CameraSettings { class CameraSettings {
public: public:
pixformat_t pixformat; pixformat_t pixformat;
framesize_t framesize; // 0 - 10 framesize_t framesize; // 0 - 10
uint8_t quality; // 0 - 63 uint8_t quality; // 0 - 63
int8_t brightness; //-2 - 2 int8_t brightness; //-2 - 2
int8_t contrast; //-2 - 2 int8_t contrast; //-2 - 2
int8_t saturation; //-2 - 2 int8_t saturation; //-2 - 2
int8_t sharpness; //-2 - 2 int8_t sharpness; //-2 - 2
uint8_t denoise; uint8_t denoise;
gainceiling_t gainceiling; gainceiling_t gainceiling;
uint8_t whitebal; uint8_t whitebal;
uint8_t special_effect; // 0 - 6 uint8_t special_effect; // 0 - 6
uint8_t wb_mode; // 0 - 4 uint8_t wb_mode; // 0 - 4
uint8_t awb; uint8_t awb;
uint8_t exposure_ctrl; uint8_t exposure_ctrl;
uint8_t awb_gain; uint8_t awb_gain;
uint8_t gain_ctrl; uint8_t gain_ctrl;
uint8_t aec; uint8_t aec;
uint8_t aec2; uint8_t aec2;
int8_t ae_level; //-2 - 2 int8_t ae_level; //-2 - 2
uint16_t aec_value; // 0 - 1200 uint16_t aec_value; // 0 - 1200
uint8_t agc; uint8_t agc;
uint8_t agc_gain; // 0 - 30 uint8_t agc_gain; // 0 - 30
uint8_t bpc; uint8_t bpc;
uint8_t wpc; uint8_t wpc;
uint8_t raw_gma; uint8_t raw_gma;
@@ -81,8 +81,7 @@ class CameraSettings {
root["colorbar"] = settings.colorbar; root["colorbar"] = settings.colorbar;
} }
static StateUpdateResult update(JsonObject &root, static StateUpdateResult update(JsonObject &root, CameraSettings &settings) {
CameraSettings &settings) {
settings.pixformat = root["pixformat"]; settings.pixformat = root["pixformat"];
settings.framesize = root["framesize"]; settings.framesize = root["framesize"];
settings.brightness = root["brightness"]; settings.brightness = root["brightness"];
@@ -119,20 +118,15 @@ class CameraSettings {
}; };
class CameraSettingsService : public StatefulService<CameraSettings> { class CameraSettingsService : public StatefulService<CameraSettings> {
public: public:
CameraSettingsService(PsychicHttpServer *server, FS *fs, CameraSettingsService(PsychicHttpServer *server, FS *fs, SecurityManager *securityManager, EventSocket *socket)
SecurityManager *securityManager, EventSocket *socket)
: _server(server), : _server(server),
_securityManager(securityManager), _securityManager(securityManager),
_httpEndpoint(CameraSettings::read, CameraSettings::update, this, _httpEndpoint(CameraSettings::read, CameraSettings::update, this, server, CAMERA_SETTINGS_PATH,
server, CAMERA_SETTINGS_PATH, securityManager, securityManager, AuthenticationPredicates::IS_ADMIN),
AuthenticationPredicates::IS_ADMIN), _eventEndpoint(CameraSettings::read, CameraSettings::update, this, socket, EVENT_CAMERA_SETTINGS),
_eventEndpoint(CameraSettings::read, CameraSettings::update, this, _fsPersistence(CameraSettings::read, CameraSettings::update, this, fs, CAMERA_SETTINGS_FILE) {
socket, EVENT_CAMERA_SETTINGS), addUpdateHandler([&](const String &originId) { updateCamera(); }, false);
_fsPersistence(CameraSettings::read, CameraSettings::update, this, fs,
CAMERA_SETTINGS_FILE) {
addUpdateHandler([&](const String &originId) { updateCamera(); },
false);
} }
void begin() { void begin() {
@@ -201,7 +195,7 @@ class CameraSettingsService : public StatefulService<CameraSettings> {
safe_sensor_return(); safe_sensor_return();
} }
private: private:
PsychicHttpServer *_server; PsychicHttpServer *_server;
SecurityManager *_securityManager; SecurityManager *_securityManager;
HttpEndpoint<CameraSettings> _httpEndpoint; HttpEndpoint<CameraSettings> _httpEndpoint;
@@ -209,4 +203,4 @@ class CameraSettingsService : public StatefulService<CameraSettings> {
FSPersistence<CameraSettings> _fsPersistence; FSPersistence<CameraSettings> _fsPersistence;
}; };
#endif // end CameraSettingsService_h #endif // end CameraSettingsService_h