mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-18 14:03:45 +02:00
Add basic HTTP MJPEG stream server
This commit is contained in:
4
components/StreamServer/CMakeLists.txt
Normal file
4
components/StreamServer/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "StreamServer/StreamServer.cpp"
|
||||
INCLUDE_DIRS "StreamServer"
|
||||
REQUIRES esp32-camera StateManager ProjectConfig esp_http_server Helpers
|
||||
)
|
||||
115
components/StreamServer/StreamServer/StreamServer.cpp
Normal file
115
components/StreamServer/StreamServer/StreamServer.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#include "StreamServer.hpp"
|
||||
|
||||
constexpr static const char *STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
|
||||
constexpr static const char *STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
|
||||
constexpr static const char *STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\nX-Timestamp: %lli.%06li\r\n\r\n";
|
||||
|
||||
static const char *STREAM_SERVER_TAG = "[STREAM_SERVER]";
|
||||
|
||||
// the stream is super low FPS, find out why
|
||||
esp_err_t StreamHelpers::stream(httpd_req_t *req)
|
||||
{
|
||||
long last_request_time = 0;
|
||||
camera_fb_t *fb = nullptr;
|
||||
struct timeval _timestamp;
|
||||
|
||||
esp_err_t response = ESP_OK;
|
||||
size_t _jpg_buf_len = 0;
|
||||
uint8_t *_jpg_buf = nullptr;
|
||||
|
||||
char *part_buf[256];
|
||||
static int64_t last_frame = 0;
|
||||
if (!last_frame)
|
||||
last_frame = esp_timer_get_time();
|
||||
|
||||
response = httpd_resp_set_type(req, STREAM_CONTENT_TYPE);
|
||||
if (response != ESP_OK)
|
||||
return response;
|
||||
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "X-Framerate", "60");
|
||||
|
||||
while (true)
|
||||
{
|
||||
fb = esp_camera_fb_get();
|
||||
|
||||
if (!fb)
|
||||
{
|
||||
ESP_LOGE(STREAM_SERVER_TAG, "Camera capture failed with resposne %s", esp_err_to_name(response));
|
||||
response = ESP_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
_timestamp.tv_sec = fb->timestamp.tv_sec;
|
||||
_timestamp.tv_usec = fb->timestamp.tv_usec;
|
||||
_jpg_buf_len = fb->len;
|
||||
_jpg_buf = fb->buf;
|
||||
}
|
||||
if (response == ESP_OK)
|
||||
response = httpd_resp_send_chunk(req, STREAM_BOUNDARY, strlen(STREAM_BOUNDARY));
|
||||
if (response == ESP_OK)
|
||||
{
|
||||
size_t hlen = snprintf((char *)part_buf, 128, STREAM_PART, _jpg_buf_len, _timestamp.tv_sec, _timestamp.tv_usec);
|
||||
response = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
|
||||
}
|
||||
if (response == ESP_OK)
|
||||
response = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
|
||||
if (fb)
|
||||
{
|
||||
esp_camera_fb_return(fb);
|
||||
fb = NULL;
|
||||
_jpg_buf = NULL;
|
||||
}
|
||||
else if (_jpg_buf)
|
||||
{
|
||||
free(_jpg_buf);
|
||||
_jpg_buf = NULL;
|
||||
}
|
||||
if (response != ESP_OK)
|
||||
break;
|
||||
long request_end = Helpers::getTimeInMillis();
|
||||
long latency = (request_end - last_request_time);
|
||||
last_request_time = request_end;
|
||||
ESP_LOGD(STREAM_SERVER_TAG, "Size: %uKB, Time: %lims (%lifps)\n", _jpg_buf_len / 1024, latency, 1000 / latency);
|
||||
}
|
||||
last_frame = 0;
|
||||
return response;
|
||||
}
|
||||
|
||||
StreamServer::StreamServer(const int STREAM_PORT) : STREAM_SERVER_PORT(STREAM_PORT) {}
|
||||
|
||||
esp_err_t StreamServer::startStreamServer()
|
||||
{
|
||||
if (cameraStateManager.getCurrentState() != CameraState_e::Camera_Success)
|
||||
{
|
||||
ESP_LOGE(STREAM_SERVER_TAG, "Camera not initialized. Cannot start stream server.");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.stack_size = 20480;
|
||||
config.max_uri_handlers = 1;
|
||||
config.server_port = STREAM_SERVER_PORT;
|
||||
config.ctrl_port = STREAM_SERVER_PORT;
|
||||
|
||||
httpd_uri_t stream_page = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = &StreamHelpers::stream,
|
||||
.user_ctx = nullptr,
|
||||
};
|
||||
|
||||
int status = httpd_start(&camera_stream, &config);
|
||||
|
||||
if (status != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(STREAM_SERVER_TAG, "Cannot start stream server.");
|
||||
return status;
|
||||
}
|
||||
|
||||
httpd_register_uri_handler(camera_stream, &stream_page);
|
||||
ESP_LOGI(STREAM_SERVER_TAG, "Stream server started on port %d", STREAM_SERVER_PORT);
|
||||
// todo add printing IP addr here
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
30
components/StreamServer/StreamServer/StreamServer.hpp
Normal file
30
components/StreamServer/StreamServer/StreamServer.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#ifndef STREAMSERVER_HPP
|
||||
#define STREAMSERVER_HPP
|
||||
|
||||
#define PART_BOUNDARY "123456789000000000000987654321"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_camera.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_timer.h"
|
||||
#include <StateManager.hpp>
|
||||
#include <Helpers.hpp>
|
||||
|
||||
namespace StreamHelpers
|
||||
{
|
||||
esp_err_t stream(httpd_req_t *req);
|
||||
}
|
||||
|
||||
class StreamServer
|
||||
{
|
||||
private:
|
||||
httpd_handle_t camera_stream = nullptr;
|
||||
int STREAM_SERVER_PORT;
|
||||
|
||||
public:
|
||||
StreamServer(const int STREAM_PORT = 80);
|
||||
esp_err_t startStreamServer();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user