Files
Hodor/main/camera.c
T
Aida_Lashkari 72a2d714ab Add : RTSP audio/video with back channel, and auth
- cam_stream: shared JPEG feed for /pic, /stream and RTSP; RTSP server
  (port 554, TCP) with MJPEG video, G.711 mic audio and an ONVIF-style
  back channel to the speaker
- cam_auth: RTSP Digest login and HTTP Basic / ?key= check on all endpoints
- Mic task feeds HTTP and RTSP with a sticky channel choice
2026-10-08 14:12:48 +03:30

165 lines
4.8 KiB
C

#include "esp_http_server.h"
#include "img_converters.h"
#include "esp_camera.h"
#include "camera_pinout.h"
#include "esp32_s3_cam_ovxxxx.h"
#include "cam_stream.h"
#include "board.h"
#if defined CONFIG_CAMERA_AF_SUPPORT
#include "esp_camera_af.h"
#endif
static const char *TAG = "Camera";
#define PART_BOUNDARY "123456789000000000000987654321"
static const char* _STREAM_CONTENT_TYPE = "multipart/x-mixed-replace;boundary=" PART_BOUNDARY;
static const char* _STREAM_BOUNDARY = "\r\n--" PART_BOUNDARY "\r\n";
static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n";
#if ESP_CAMERA_SUPPORTED
static camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
.pin_sccb_sda = -1,
.pin_sccb_scl = -1,
.pin_d7 = CAM_PIN_D7,
.pin_d6 = CAM_PIN_D6,
.pin_d5 = CAM_PIN_D5,
.pin_d4 = CAM_PIN_D4,
.pin_d3 = CAM_PIN_D3,
.pin_d2 = CAM_PIN_D2,
.pin_d1 = CAM_PIN_D1,
.pin_d0 = CAM_PIN_D0,
.pin_vsync = CAM_PIN_VSYNC,
.pin_href = CAM_PIN_HREF,
.pin_pclk = CAM_PIN_PCLK,
//XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
.xclk_freq_hz = 10000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_YUV422, //YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
.jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality
.fb_count = 2, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
.fb_location = CAMERA_FB_IN_PSRAM,
// .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
.grab_mode = CAMERA_GRAB_LATEST,
};
esp_err_t init_camera(void)
{
//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera Init Failed");
return err;
}
return ESP_OK;
}
void start_camera_server(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_handle_t server = NULL;
if (httpd_start(&server, &config) == ESP_OK) {
httpd_uri_t uri_capture = { .uri = "/pic", .method = HTTP_GET, .handler = capture_handler };
httpd_uri_t uri_stream = { .uri = "/stream", .method = HTTP_GET, .handler = stream_handler };
httpd_register_uri_handler(server, &uri_capture);
httpd_register_uri_handler(server, &uri_stream);
}
}
#if defined(CONFIG_CAMERA_AF_SUPPORT) && CONFIG_CAMERA_AF_SUPPORT
static void maybe_init_autofocus(void)
{
sensor_t *s = esp_camera_sensor_get();
if (!s) {
ESP_LOGW(TAG, "AF: no sensor handle");
return;
}
if (!esp_camera_af_is_supported(s)) {
ESP_LOGI(TAG, "AF: not supported by this sensor");
return;
}
esp_camera_af_config_t af_cfg = {
.mode = ESP_CAMERA_AF_MODE_AUTO,
.timeout_ms = CONFIG_CAMERA_AF_DEFAULT_TIMEOUT_MS,
};
esp_err_t ret = esp_camera_af_init(s, &af_cfg);
if (ret != ESP_OK) {
ESP_LOGW(TAG, "AF init failed: %s", esp_err_to_name(ret));
return;
}
ESP_LOGI(TAG, "AF initialized (AUTO mode)");
}
#endif
#endif
esp_err_t capture_handler(httpd_req_t *req)
{
uint8_t *jpg = NULL;
size_t cap = 0;
uint32_t seq;
if (!cam_http_check(req)) return ESP_OK;
cam_feed_viewer_add(&seq);
size_t n = cam_feed_wait(&jpg, &cap, &seq, 3000);
cam_feed_viewer_remove();
if (!n) {
free(jpg);
httpd_resp_send_500(req);
return ESP_FAIL;
}
httpd_resp_set_type(req, "image/jpeg");
esp_err_t r = httpd_resp_send(req, (const char *)jpg, n);
free(jpg);
return r;
}
esp_err_t stream_handler(httpd_req_t *req)
{
uint8_t *jpg = NULL;
size_t cap = 0;
uint32_t seq;
int idle = 0;
char part[64];
if (!cam_http_check(req)) return ESP_OK;
esp_err_t res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
if (res != ESP_OK) return res;
cam_feed_viewer_add(&seq);
while (1) {
size_t n = cam_feed_wait(&jpg, &cap, &seq, 2000);
if (!n) {
if (++idle >= 5) { res = ESP_FAIL; break; } // no frames for 10 s: give up
continue;
}
idle = 0;
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
if (res == ESP_OK) {
size_t hl = snprintf(part, sizeof(part), _STREAM_PART, (unsigned)n);
res = httpd_resp_send_chunk(req, part, hl);
}
if (res == ESP_OK) res = httpd_resp_send_chunk(req, (const char *)jpg, n);
if (res != ESP_OK) break; // browser left
}
cam_feed_viewer_remove();
free(jpg);
return res;
}