|
@@ -9,8 +9,6 @@
|
|
|
#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>
|
|
|
|
|
|
|
@@ -54,6 +52,8 @@ static bool streamActive = false;
|
|
|
struct AsyncStreamRequest
|
|
struct AsyncStreamRequest
|
|
|
{
|
|
{
|
|
|
httpd_req_t *request;
|
|
httpd_req_t *request;
|
|
|
|
|
+ httpd_handle_t serverHandle;
|
|
|
|
|
+ int socketFd;
|
|
|
bool flashlightOn;
|
|
bool flashlightOn;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -62,7 +62,13 @@ static void async_stream_worker(void *argument)
|
|
|
AsyncStreamRequest *stream = static_cast<AsyncStreamRequest *>(argument);
|
|
AsyncStreamRequest *stream = static_cast<AsyncStreamRequest *>(argument);
|
|
|
if (stream != NULL)
|
|
if (stream != NULL)
|
|
|
{
|
|
{
|
|
|
- Camera.CaptureToStream(stream->request, stream->flashlightOn);
|
|
|
|
|
|
|
+ const esp_err_t streamResult = Camera.CaptureToStream(stream->request, stream->flashlightOn);
|
|
|
|
|
+ if (streamResult != ESP_OK && stream->serverHandle != NULL && stream->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(stream->serverHandle, stream->socketFd);
|
|
|
|
|
+ }
|
|
|
httpd_req_async_handler_complete(stream->request);
|
|
httpd_req_async_handler_complete(stream->request);
|
|
|
delete stream;
|
|
delete stream;
|
|
|
}
|
|
}
|
|
@@ -390,6 +396,16 @@ esp_err_t handler_stream(httpd_req_t *req)
|
|
|
streamActive = true;
|
|
streamActive = true;
|
|
|
portEXIT_CRITICAL(&streamStateMux);
|
|
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;
|
|
httpd_req_t *asyncRequest = NULL;
|
|
|
esp_err_t result = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
esp_err_t result = httpd_req_async_handler_begin(req, &asyncRequest);
|
|
|
if (result != ESP_OK)
|
|
if (result != ESP_OK)
|
|
@@ -401,7 +417,8 @@ esp_err_t handler_stream(httpd_req_t *req)
|
|
|
"Unable to start asynchronous live stream");
|
|
"Unable to start asynchronous live stream");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- AsyncStreamRequest *stream = new (std::nothrow) AsyncStreamRequest{asyncRequest, flashlightOn};
|
|
|
|
|
|
|
+ AsyncStreamRequest *stream = new (std::nothrow) AsyncStreamRequest{
|
|
|
|
|
+ asyncRequest, req->handle, socketFd, flashlightOn};
|
|
|
if (stream == NULL)
|
|
if (stream == NULL)
|
|
|
{
|
|
{
|
|
|
httpd_req_async_handler_complete(asyncRequest);
|
|
httpd_req_async_handler_complete(asyncRequest);
|