1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-26 12:43:48 +00:00

Introduce base Camera class to support alternative camera implementations (#9285)

Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick+github@koston.org>
This commit is contained in:
DT-art1
2025-07-07 05:45:00 +02:00
committed by GitHub
parent bdd52dbaa4
commit e49b89a051
27 changed files with 254 additions and 132 deletions

View File

@@ -3,7 +3,8 @@ import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_MODE, CONF_PORT
CODEOWNERS = ["@ayufan"]
DEPENDENCIES = ["esp32_camera", "network"]
AUTO_LOAD = ["camera"]
DEPENDENCIES = ["network"]
MULTI_CONF = True
esp32_camera_web_server_ns = cg.esphome_ns.namespace("esp32_camera_web_server")

View File

@@ -40,7 +40,7 @@ CameraWebServer::CameraWebServer() {}
CameraWebServer::~CameraWebServer() {}
void CameraWebServer::setup() {
if (!esp32_camera::global_esp32_camera || esp32_camera::global_esp32_camera->is_failed()) {
if (!camera::Camera::instance() || camera::Camera::instance()->is_failed()) {
this->mark_failed();
return;
}
@@ -67,8 +67,8 @@ void CameraWebServer::setup() {
httpd_register_uri_handler(this->httpd_, &uri);
esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr<esp32_camera::CameraImage> image) {
if (this->running_ && image->was_requested_by(esp32_camera::WEB_REQUESTER)) {
camera::Camera::instance()->add_image_callback([this](std::shared_ptr<camera::CameraImage> image) {
if (this->running_ && image->was_requested_by(camera::WEB_REQUESTER)) {
this->image_ = std::move(image);
xSemaphoreGive(this->semaphore_);
}
@@ -108,8 +108,8 @@ void CameraWebServer::loop() {
}
}
std::shared_ptr<esphome::esp32_camera::CameraImage> CameraWebServer::wait_for_image_() {
std::shared_ptr<esphome::esp32_camera::CameraImage> image;
std::shared_ptr<esphome::camera::CameraImage> CameraWebServer::wait_for_image_() {
std::shared_ptr<esphome::camera::CameraImage> image;
image.swap(this->image_);
if (!image) {
@@ -172,7 +172,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
uint32_t last_frame = millis();
uint32_t frames = 0;
esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::WEB_REQUESTER);
camera::Camera::instance()->start_stream(esphome::camera::WEB_REQUESTER);
while (res == ESP_OK && this->running_) {
auto image = this->wait_for_image_();
@@ -205,7 +205,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
}
esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::WEB_REQUESTER);
camera::Camera::instance()->stop_stream(esphome::camera::WEB_REQUESTER);
ESP_LOGI(TAG, "STREAM: closed. Frames: %" PRIu32, frames);
@@ -215,7 +215,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
esp_err_t res = ESP_OK;
esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::WEB_REQUESTER);
camera::Camera::instance()->request_image(esphome::camera::WEB_REQUESTER);
auto image = this->wait_for_image_();

View File

@@ -6,7 +6,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include "esphome/components/esp32_camera/esp32_camera.h"
#include "esphome/components/camera/camera.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/core/preferences.h"
@@ -32,7 +32,7 @@ class CameraWebServer : public Component {
void loop() override;
protected:
std::shared_ptr<esphome::esp32_camera::CameraImage> wait_for_image_();
std::shared_ptr<camera::CameraImage> wait_for_image_();
esp_err_t handler_(struct httpd_req *req);
esp_err_t streaming_handler_(struct httpd_req *req);
esp_err_t snapshot_handler_(struct httpd_req *req);
@@ -40,7 +40,7 @@ class CameraWebServer : public Component {
uint16_t port_{0};
void *httpd_{nullptr};
SemaphoreHandle_t semaphore_;
std::shared_ptr<esphome::esp32_camera::CameraImage> image_;
std::shared_ptr<camera::CameraImage> image_;
bool running_{false};
Mode mode_{STREAM};
};