|
|
@@ -5,6 +5,11 @@
|
|
|
#include <string>
|
|
|
#include "esp_timer.h"
|
|
|
#include "ClassLogFile.h"
|
|
|
+#include "freertos/FreeRTOS.h"
|
|
|
+#include "freertos/semphr.h"
|
|
|
+#include "freertos/task.h"
|
|
|
+#include <new>
|
|
|
+#include <cstring>
|
|
|
|
|
|
|
|
|
#define HTTPD_401 "401 UNAUTHORIZED"
|
|
|
@@ -18,6 +23,94 @@ typedef struct {
|
|
|
|
|
|
basic_auth_info_t basic_auth_info = { NULL, NULL };
|
|
|
|
|
|
+// Camera operations share one physical camera and are therefore dispatched through
|
|
|
+// one bounded asynchronous slot. This keeps long captures/streams out of the
|
|
|
+// single esp_http_server task without allowing concurrent camera requests.
|
|
|
+static SemaphoreHandle_t cameraAsyncRequestSlot = NULL;
|
|
|
+
|
|
|
+struct AsyncCameraHttpRequest
|
|
|
+{
|
|
|
+ httpd_req_t *request;
|
|
|
+ basic_auth_async_handler_t handler;
|
|
|
+ char handlerName[32];
|
|
|
+ char uri[128];
|
|
|
+ int64_t startUs;
|
|
|
+};
|
|
|
+
|
|
|
+static void async_camera_http_request_task(void *argument)
|
|
|
+{
|
|
|
+ AsyncCameraHttpRequest *work = static_cast<AsyncCameraHttpRequest *>(argument);
|
|
|
+ const esp_err_t result = work->handler(work->request);
|
|
|
+ const int64_t elapsedUs = esp_timer_get_time() - work->startUs;
|
|
|
+
|
|
|
+ LogFile.WriteToFile(ESP_LOG_WARN, TAG,
|
|
|
+ std::string("Async camera HTTP request: ") + work->handlerName +
|
|
|
+ " " + work->uri + " took " +
|
|
|
+ std::to_string(static_cast<long long>(elapsedUs / 1000)) +
|
|
|
+ " ms (result=" + std::to_string(static_cast<int>(result)) + ")");
|
|
|
+
|
|
|
+ // Complete the async request on every worker exit path so the HTTP server
|
|
|
+ // can release the socket and accept subsequent requests.
|
|
|
+ httpd_req_async_handler_complete(work->request);
|
|
|
+ if (cameraAsyncRequestSlot != NULL) {
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
+ }
|
|
|
+ delete work;
|
|
|
+ vTaskDelete(NULL);
|
|
|
+}
|
|
|
+
|
|
|
+esp_err_t dispatch_camera_http_request_async(httpd_req_t *req,
|
|
|
+ basic_auth_async_handler_t handler,
|
|
|
+ const char *handler_name)
|
|
|
+{
|
|
|
+ if (req == NULL || handler == NULL) {
|
|
|
+ return ESP_ERR_INVALID_ARG;
|
|
|
+ }
|
|
|
+ if (cameraAsyncRequestSlot == NULL ||
|
|
|
+ xSemaphoreTake(cameraAsyncRequestSlot, 0) != pdTRUE) {
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
+ httpd_resp_sendstr(req, "Camera is busy with another capture or stream");
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ AsyncCameraHttpRequest *work = new (std::nothrow) AsyncCameraHttpRequest{};
|
|
|
+ if (work == NULL) {
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
+ httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Not enough memory for camera request");
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ httpd_req_t *asyncRequest = NULL;
|
|
|
+ const esp_err_t copyResult = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
+ if (copyResult != ESP_OK) {
|
|
|
+ delete work;
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
+ httpd_resp_send_err(req, HTTPD_503_SERVICE_UNAVAILABLE, "Could not queue camera request");
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ work->request = asyncRequest;
|
|
|
+ work->handler = handler;
|
|
|
+ strlcpy(work->handlerName, handler_name != NULL ? handler_name : "camera",
|
|
|
+ sizeof(work->handlerName));
|
|
|
+ strlcpy(work->uri, asyncRequest->uri, sizeof(work->uri));
|
|
|
+ work->startUs = esp_timer_get_time();
|
|
|
+
|
|
|
+ // Lower priority than the HTTP server task. Socket sends and camera waits
|
|
|
+ // happen on this worker, while short UI/API requests remain serviceable.
|
|
|
+ if (xTaskCreatePinnedToCore(async_camera_http_request_task, "camera_http",
|
|
|
+ 8 * 1024, work, tskIDLE_PRIORITY + 3,
|
|
|
+ NULL, 1) != pdPASS) {
|
|
|
+ httpd_resp_send_err(req, HTTPD_503_SERVICE_UNAVAILABLE, "Could not start camera request worker");
|
|
|
+ httpd_req_async_handler_complete(asyncRequest);
|
|
|
+ delete work;
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
+ return ESP_OK;
|
|
|
+ }
|
|
|
+
|
|
|
+ return ESP_OK;
|
|
|
+}
|
|
|
+
|
|
|
// All URI handlers are registered through APPLY_BASIC_AUTH_FILTER. Measuring here
|
|
|
// gives us a common duration for every HTTP endpoint, including static HTML/CSS/JS
|
|
|
// and image handlers. Time spent waiting in the ESP-IDF server queue is not included.
|
|
|
@@ -58,6 +151,16 @@ void init_basic_auth() {
|
|
|
basic_auth_info.username = wlan_config.http_username.c_str();
|
|
|
basic_auth_info.password = wlan_config.http_password.c_str();
|
|
|
}
|
|
|
+
|
|
|
+ if (cameraAsyncRequestSlot == NULL) {
|
|
|
+ cameraAsyncRequestSlot = xSemaphoreCreateBinary();
|
|
|
+ if (cameraAsyncRequestSlot != NULL) {
|
|
|
+ xSemaphoreGive(cameraAsyncRequestSlot);
|
|
|
+ } else {
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG,
|
|
|
+ "Failed to create asynchronous camera request slot");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static char *http_auth_basic(const char *username, const char *password)
|