|
@@ -9,6 +9,8 @@
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
|
#include "freertos/task.h"
|
|
#include "freertos/task.h"
|
|
|
|
|
|
|
|
|
|
+#include "freertos/FreeRTOS.h"
|
|
|
|
|
+
|
|
|
#include <iomanip>
|
|
#include <iomanip>
|
|
|
#include <sstream>
|
|
#include <sstream>
|
|
|
|
|
|
|
@@ -43,6 +45,34 @@ camera_flow_config_temp_t CFstatus;
|
|
|
|
|
|
|
|
TaskHandle_t xHandletask_autodoFlow = NULL;
|
|
TaskHandle_t xHandletask_autodoFlow = NULL;
|
|
|
|
|
|
|
|
|
|
+// The synchronous camera stream used to occupy the only HTTP server task.
|
|
|
|
|
+// Keep at most one stream active, but run its long-lived response on an async
|
|
|
|
|
+// request copy so normal UI/API requests can still be served.
|
|
|
|
|
+static portMUX_TYPE streamStateMux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
+static bool streamActive = false;
|
|
|
|
|
+
|
|
|
|
|
+struct AsyncStreamRequest
|
|
|
|
|
+{
|
|
|
|
|
+ httpd_req_t *request;
|
|
|
|
|
+ bool flashlightOn;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+static void async_stream_worker(void *argument)
|
|
|
|
|
+{
|
|
|
|
|
+ AsyncStreamRequest *stream = static_cast<AsyncStreamRequest *>(argument);
|
|
|
|
|
+ if (stream != NULL)
|
|
|
|
|
+ {
|
|
|
|
|
+ Camera.CaptureToStream(stream->request, stream->flashlightOn);
|
|
|
|
|
+ httpd_req_async_handler_complete(stream->request);
|
|
|
|
|
+ delete stream;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
+ streamActive = false;
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+ vTaskDelete(NULL);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
bool bTaskAutoFlowCreated = false;
|
|
bool bTaskAutoFlowCreated = false;
|
|
|
// True only while the flow objects are fully built. bTaskAutoFlowCreated is already true during the
|
|
// True only while the flow objects are fully built. bTaskAutoFlowCreated is already true during the
|
|
|
// post-panic init delay and while doInit() rebuilds the flow, when flowctrl's post-processing is NULL
|
|
// post-panic init delay and while doInit() rebuilds the flow, when flowctrl's post-processing is NULL
|
|
@@ -350,16 +380,58 @@ esp_err_t handler_stream(httpd_req_t *req)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Closing the browser popup makes the next stream send fail. Propagate
|
|
|
|
|
- // that error so esp_http_server closes the client session instead of
|
|
|
|
|
- // treating the interrupted stream as a reusable successful connection.
|
|
|
|
|
- const esp_err_t streamResult = Camera.CaptureToStream(req, flashlightOn);
|
|
|
|
|
|
|
+ portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
+ if (streamActive)
|
|
|
|
|
+ {
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+ httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
|
|
+ return httpd_resp_send(req, "Live stream already active", HTTPD_RESP_USE_STRLEN);
|
|
|
|
|
+ }
|
|
|
|
|
+ streamActive = true;
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+
|
|
|
|
|
+ httpd_req_t *asyncRequest = NULL;
|
|
|
|
|
+ esp_err_t result = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
|
|
+ if (result != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
+ streamActive = false;
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+ return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
+ "Unable to start asynchronous live stream");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ AsyncStreamRequest *stream = new (std::nothrow) AsyncStreamRequest{asyncRequest, flashlightOn};
|
|
|
|
|
+ if (stream == NULL)
|
|
|
|
|
+ {
|
|
|
|
|
+ httpd_req_async_handler_complete(asyncRequest);
|
|
|
|
|
+ portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
+ streamActive = false;
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+ return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
+ "Unable to allocate live stream worker");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const BaseType_t taskResult = xTaskCreate(
|
|
|
|
|
+ async_stream_worker, "camera_stream", 4096, stream,
|
|
|
|
|
+ tskIDLE_PRIORITY + 1, NULL);
|
|
|
|
|
+ if (taskResult != pdPASS)
|
|
|
|
|
+ {
|
|
|
|
|
+ delete stream;
|
|
|
|
|
+ httpd_req_async_handler_complete(asyncRequest);
|
|
|
|
|
+ portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
+ streamActive = false;
|
|
|
|
|
+ portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
+ return httpd_resp_send_err(req, HTTPD_503_SERVICE_UNAVAILABLE,
|
|
|
|
|
+ "Unable to start live stream worker");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
#ifdef DEBUG_DETAIL_ON
|
|
|
- LogFile.WriteHeapInfo("handler_stream - Done");
|
|
|
|
|
|
|
+ LogFile.WriteHeapInfo("handler_stream - Async worker started");
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
- return streamResult;
|
|
|
|
|
|
|
+ // The async request copy now owns the socket until the worker completes it.
|
|
|
|
|
+ return ESP_OK;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
esp_err_t handler_flow_start(httpd_req_t *req)
|
|
esp_err_t handler_flow_start(httpd_req_t *req)
|