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