|
@@ -43,47 +43,6 @@ 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;
|
|
|
|
|
- httpd_handle_t serverHandle;
|
|
|
|
|
- int socketFd;
|
|
|
|
|
- bool flashlightOn;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-static void async_stream_worker(void *argument)
|
|
|
|
|
-{
|
|
|
|
|
- AsyncStreamRequest *stream = static_cast<AsyncStreamRequest *>(argument);
|
|
|
|
|
- if (stream != NULL)
|
|
|
|
|
- {
|
|
|
|
|
- const esp_err_t streamResult = Camera.CaptureToStream(stream->request, stream->flashlightOn);
|
|
|
|
|
- const httpd_handle_t serverHandle = stream->serverHandle;
|
|
|
|
|
- const int socketFd = stream->socketFd;
|
|
|
|
|
-
|
|
|
|
|
- // Complete the async request before scheduling socket close. This keeps
|
|
|
|
|
- // the request's internal session pointer valid while it is released.
|
|
|
|
|
- httpd_req_async_handler_complete(stream->request);
|
|
|
|
|
- if (streamResult != ESP_OK && serverHandle != NULL && socketFd >= 0)
|
|
|
|
|
- {
|
|
|
|
|
- // The original URI handler has already returned ESP_OK. Explicitly
|
|
|
|
|
- // close a broken stream session from the worker on send/capture errors.
|
|
|
|
|
- httpd_sess_trigger_close(serverHandle, socketFd);
|
|
|
|
|
- }
|
|
|
|
|
- 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
|
|
@@ -391,69 +350,16 @@ esp_err_t handler_stream(httpd_req_t *req)
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- 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);
|
|
|
|
|
-
|
|
|
|
|
- const int socketFd = httpd_req_to_sockfd(req);
|
|
|
|
|
- if (socketFd < 0)
|
|
|
|
|
- {
|
|
|
|
|
- portENTER_CRITICAL(&streamStateMux);
|
|
|
|
|
- streamActive = false;
|
|
|
|
|
- portEXIT_CRITICAL(&streamStateMux);
|
|
|
|
|
- return httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
|
|
|
|
|
- "Unable to identify live stream socket");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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, req->handle, socketFd, 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);
|
|
|
|
|
- httpd_resp_set_status(req, "503 Service Unavailable");
|
|
|
|
|
- return httpd_resp_send(req, "Unable to start live stream worker", HTTPD_RESP_USE_STRLEN);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 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);
|
|
|
|
|
|
|
|
#ifdef DEBUG_DETAIL_ON
|
|
#ifdef DEBUG_DETAIL_ON
|
|
|
- LogFile.WriteHeapInfo("handler_stream - Async worker started");
|
|
|
|
|
|
|
+ LogFile.WriteHeapInfo("handler_stream - Done");
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
- // The async request copy now owns the socket until the worker completes it.
|
|
|
|
|
- return ESP_OK;
|
|
|
|
|
|
|
+ return streamResult;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
esp_err_t handler_flow_start(httpd_req_t *req)
|
|
esp_err_t handler_flow_start(httpd_req_t *req)
|