11 #include <esp_http_server.h> 15 namespace esp32_camera_web_server {
17 static const int IMAGE_REQUEST_TIMEOUT = 5000;
18 static const char *
const TAG =
"esp32_camera_web_server";
20 #define PART_BOUNDARY "123456789000000000000987654321" 21 #define CONTENT_TYPE "image/jpeg" 22 #define CONTENT_LENGTH "Content-Length" 24 static const char *
const STREAM_HEADER =
"HTTP/1.0 200 OK\r\n" 25 "Access-Control-Allow-Origin: *\r\n" 26 "Connection: close\r\n" 27 "Content-Type: multipart/x-mixed-replace;boundary=" PART_BOUNDARY
"\r\n" 29 "--" PART_BOUNDARY
"\r\n";
30 static const char *
const STREAM_ERROR =
"Content-Type: text/plain\r\n" 33 "--" PART_BOUNDARY
"\r\n";
34 static const char *
const STREAM_PART =
"Content-Type: " CONTENT_TYPE
"\r\n" CONTENT_LENGTH
": %u\r\n\r\n";
35 static const char *
const STREAM_BOUNDARY =
"\r\n" 36 "--" PART_BOUNDARY
"\r\n";
50 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
51 config.server_port = this->
port_;
52 config.ctrl_port = this->
port_;
53 config.max_open_sockets = 1;
54 config.backlog_conn = 2;
55 config.lru_purge_enable =
true;
57 if (httpd_start(&this->
httpd_, &config) != ESP_OK) {
68 httpd_register_uri_handler(this->
httpd_, &uri);
72 this->
image_ = std::move(image);
88 ESP_LOGCONFIG(TAG,
"ESP32 Camera Web Server:");
89 ESP_LOGCONFIG(TAG,
" Port: %d", this->
port_);
91 ESP_LOGCONFIG(TAG,
" Mode: stream");
93 ESP_LOGCONFIG(TAG,
" Mode: snapshot");
97 ESP_LOGE(TAG,
" Setup Failed");
110 std::shared_ptr<esphome::esp32_camera::CameraImage> image;
115 xSemaphoreTake(this->
semaphore_, IMAGE_REQUEST_TIMEOUT / portTICK_PERIOD_MS);
123 esp_err_t res = ESP_FAIL;
128 switch (this->
mode_) {
143 static esp_err_t httpd_send_all(httpd_req_t *r,
const char *buf,
size_t buf_len) {
146 while (buf_len > 0) {
147 ret = httpd_send(r, buf, buf_len);
158 esp_err_t res = ESP_OK;
164 res = httpd_send_all(req, STREAM_HEADER, strlen(STREAM_HEADER));
166 ESP_LOGW(TAG,
"STREAM: failed to set HTTP header");
170 uint32_t last_frame =
millis();
175 while (res == ESP_OK && this->
running_) {
179 ESP_LOGW(TAG,
"STREAM: failed to acquire frame");
183 size_t hlen = snprintf(part_buf, 64, STREAM_PART, image->get_data_length());
184 res = httpd_send_all(req, part_buf, hlen);
187 res = httpd_send_all(req, (
const char *) image->get_data_buffer(), image->get_data_length());
190 res = httpd_send_all(req, STREAM_BOUNDARY, strlen(STREAM_BOUNDARY));
194 int64_t frame_time =
millis() - last_frame;
197 ESP_LOGD(TAG,
"MJPG: %" PRIu32
"B %" PRIu32
"ms (%.1ffps)", (uint32_t) image->get_data_length(),
198 (uint32_t) frame_time, 1000.0 / (uint32_t) frame_time);
203 res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
208 ESP_LOGI(TAG,
"STREAM: closed. Frames: %" PRIu32, frames);
214 esp_err_t res = ESP_OK;
221 ESP_LOGW(TAG,
"SNAPSHOT: failed to acquire frame");
222 httpd_resp_send_500(req);
227 res = httpd_resp_set_type(req, CONTENT_TYPE);
229 ESP_LOGW(TAG,
"SNAPSHOT: failed to set HTTP response type");
233 httpd_resp_set_hdr(req,
"Content-Disposition",
"inline; filename=capture.jpg");
236 res = httpd_resp_send(req, (
const char *) image->get_data_buffer(), image->get_data_length());
void request_image(CameraRequester requester)
void start_stream(CameraRequester requester)
esp_err_t streaming_handler_(struct httpd_req *req)
float get_setup_priority() const override
const float LATE
For components that should be initialized at the very end of the setup process.
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&callback)
std::shared_ptr< esphome::esp32_camera::CameraImage > image_
void dump_config() override
void stop_stream(CameraRequester requester)
uint32_t IRAM_ATTR HOT millis()
ESP32Camera * global_esp32_camera
esp_err_t handler_(struct httpd_req *req)
std::shared_ptr< esphome::esp32_camera::CameraImage > wait_for_image_()
esp_err_t snapshot_handler_(struct httpd_req *req)
virtual void mark_failed()
Mark this component as failed.
void on_shutdown() override
Implementation of SPI Controller mode.
SemaphoreHandle_t semaphore_