11#include <esp_http_server.h>
16static const int IMAGE_REQUEST_TIMEOUT = 5000;
17static const char *
const TAG =
"esp32_camera_web_server";
19#define PART_BOUNDARY "123456789000000000000987654321"
20#define CONTENT_TYPE "image/jpeg"
21#define CONTENT_LENGTH "Content-Length"
23static const char *
const STREAM_HEADER =
"HTTP/1.0 200 OK\r\n"
24 "Access-Control-Allow-Origin: *\r\n"
25 "Connection: close\r\n"
26 "Content-Type: multipart/x-mixed-replace;boundary=" PART_BOUNDARY
"\r\n"
28 "--" PART_BOUNDARY
"\r\n";
29static const char *
const STREAM_ERROR =
"Content-Type: text/plain\r\n"
32 "--" PART_BOUNDARY
"\r\n";
33static const char *
const STREAM_PART =
"Content-Type: " CONTENT_TYPE
"\r\n" CONTENT_LENGTH
": %u\r\n\r\n";
34static const char *
const STREAM_BOUNDARY =
"\r\n"
35 "--" PART_BOUNDARY
"\r\n";
49 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
50 config.server_port = this->
port_;
51 config.ctrl_port = this->
port_;
52 config.max_open_sockets = 1;
53 config.backlog_conn = 2;
54 config.lru_purge_enable =
true;
56 if (httpd_start(&this->
httpd_, &config) != ESP_OK) {
64 .handler = [](
struct httpd_req *req) {
return ((
CameraWebServer *) req->user_ctx)->handler_(req); },
67 httpd_register_uri_handler(this->
httpd_, &uri);
90 "ESP32 Camera Web Server:\n"
94 ESP_LOGCONFIG(TAG,
" Mode: stream");
96 ESP_LOGCONFIG(TAG,
" Mode: snapshot");
100 ESP_LOGE(TAG,
" Setup Failed");
113 std::shared_ptr<esphome::camera::CameraImage> image;
118 xSemaphoreTake(this->
semaphore_, IMAGE_REQUEST_TIMEOUT / portTICK_PERIOD_MS);
126 esp_err_t res = ESP_FAIL;
131 switch (this->
mode_) {
146static esp_err_t httpd_send_all(httpd_req_t *r,
const char *buf,
size_t buf_len) {
149 while (buf_len > 0) {
150 ret = httpd_send(r, buf, buf_len);
161 esp_err_t res = ESP_OK;
167 res = httpd_send_all(req, STREAM_HEADER, strlen(STREAM_HEADER));
169 ESP_LOGW(TAG,
"STREAM: failed to set HTTP header");
178 while (res == ESP_OK && this->
running_) {
182 ESP_LOGW(TAG,
"STREAM: failed to acquire frame");
186 size_t hlen = snprintf(part_buf, 64, STREAM_PART, image->get_data_length());
187 res = httpd_send_all(req, part_buf, hlen);
190 res = httpd_send_all(req, (
const char *) image->get_data_buffer(), image->get_data_length());
193 res = httpd_send_all(req, STREAM_BOUNDARY, strlen(STREAM_BOUNDARY));
197 int64_t frame_time =
millis() - last_frame;
200 ESP_LOGD(TAG,
"MJPG: %" PRIu32
"B %" PRIu32
"ms (%.1ffps)", (
uint32_t) image->get_data_length(),
206 res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
211 ESP_LOGI(TAG,
"STREAM: closed. Frames: %" PRIu32, frames);
217 esp_err_t res = ESP_OK;
224 ESP_LOGW(TAG,
"SNAPSHOT: failed to acquire frame");
225 httpd_resp_send_500(req);
230 res = httpd_resp_set_type(req, CONTENT_TYPE);
232 ESP_LOGW(TAG,
"SNAPSHOT: failed to set HTTP response type");
236 httpd_resp_set_hdr(req,
"Content-Disposition",
"inline; filename=capture.jpg");
239 res = httpd_resp_send(req, (
const char *) image->get_data_buffer(), image->get_data_length());
void mark_failed()
Mark this component as failed.
virtual void start_stream(CameraRequester requester)=0
virtual void stop_stream(CameraRequester requester)=0
virtual void add_listener(CameraListener *listener)=0
Add a listener to receive camera events.
virtual void request_image(CameraRequester requester)=0
static Camera * instance()
The singleton instance of the camera implementation.
void on_camera_image(const std::shared_ptr< camera::CameraImage > &image) override
CameraListener interface.
float get_setup_priority() const override
void dump_config() override
std::shared_ptr< camera::CameraImage > image_
SemaphoreHandle_t semaphore_
esp_err_t handler_(struct httpd_req *req)
void on_shutdown() override
std::shared_ptr< camera::CameraImage > wait_for_image_()
esp_err_t streaming_handler_(struct httpd_req *req)
esp_err_t snapshot_handler_(struct httpd_req *req)
constexpr float LATE
For components that should be initialized at the very end of the setup process.
uint32_t IRAM_ATTR HOT millis()