Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiclient streaming support #237

Merged
merged 2 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 27 additions & 106 deletions app_httpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "src/logo.h"
#include "storage.h"

extern "C"{
#include "cam_streamer.h"
}
// Functions from the main .ino
extern void flashLED(int flashtime);
extern void setLamp(int newVal);
Expand All @@ -48,7 +51,6 @@ extern int streamPort;
extern char httpURL[];
extern char streamURL[];
extern char default_index[];
extern int8_t streamCount;
extern unsigned long streamsServed;
extern unsigned long imagesServed;
extern int myRotation;
Expand All @@ -67,6 +69,8 @@ extern char otaPassword[];
extern unsigned long xclk;
extern int sensorPID;

cam_streamer_t *cam_streamer;

typedef struct {
httpd_req_t *req;
size_t len;
Expand All @@ -80,9 +84,6 @@ static const char* _STREAM_PART = "Content-Type: image/jpeg\r\nContent-Length: %
httpd_handle_t stream_httpd = NULL;
httpd_handle_t camera_httpd = NULL;

// Flag that can be set to kill all active streams
bool streamKill;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -147,7 +148,7 @@ void serialDump() {
int McuTc = (temprature_sens_read() - 32) / 1.8; // celsius
int McuTf = temprature_sens_read(); // fahrenheit
Serial.printf("System up: %" PRId64 ":%02i:%02i:%02i (d:h:m:s)\r\n", upDays, upHours, upMin, upSec);
Serial.printf("Active streams: %i, Previous streams: %lu, Images captured: %lu\r\n", streamCount, streamsServed, imagesServed);
Serial.printf("Active streams: %lu, Previous streams: %lu, Images captured: %lu\r\n", cam_streamer_get_num_clients(cam_streamer), streamsServed, imagesServed);
Serial.printf("CPU Freq: %i MHz, Xclk Freq: %i MHz\r\n", ESP.getCpuFreqMHz(), xclk);
Serial.printf("MCU temperature : %i C, %i F (approximate)\r\n", McuTc, McuTf);
Serial.printf("Heap: %i, free: %i, min free: %i, max block: %i\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap());
Expand Down Expand Up @@ -222,102 +223,16 @@ static esp_err_t capture_handler(httpd_req_t *req){
}

static esp_err_t stream_handler(httpd_req_t *req){
camera_fb_t * fb = NULL;
esp_err_t res = ESP_OK;
size_t _jpg_buf_len = 0;
uint8_t * _jpg_buf = NULL;
char * part_buf[64];

streamKill = false;

Serial.println("Stream requested");
if (autoLamp && (lampVal != -1)) setLamp(lampVal);
streamCount = 1; // at present we only have one stream handler, so values are 0 or 1..
flashLED(75); // double flash of status LED
delay(75);
flashLED(75);

static int64_t last_frame = 0;
if(!last_frame) {
last_frame = esp_timer_get_time();
}

res = httpd_resp_set_type(req, _STREAM_CONTENT_TYPE);
if(res != ESP_OK){
streamCount = 0;
if (autoLamp && (lampVal != -1)) setLamp(0);
Serial.println("STREAM: failed to set HTTP response type");
return res;
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if(res == ESP_OK){
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
}

while(true){
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("STREAM: failed to acquire frame");
res = ESP_FAIL;
} else {
if(fb->format != PIXFORMAT_JPEG){
Serial.println("STREAM: Non-JPEG frame returned by camera module");
res = ESP_FAIL;
} else {
_jpg_buf_len = fb->len;
_jpg_buf = fb->buf;
}
}
if(res == ESP_OK){
size_t hlen = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
res = httpd_resp_send_chunk(req, (const char *)part_buf, hlen);
}
if(res == ESP_OK){
res = httpd_resp_send_chunk(req, (const char *)_jpg_buf, _jpg_buf_len);
}
if(res == ESP_OK){
res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
}
if(fb){
esp_camera_fb_return(fb);
fb = NULL;
_jpg_buf = NULL;
} else if(_jpg_buf){
free(_jpg_buf);
_jpg_buf = NULL;
}
if(res != ESP_OK){
// This is the error exit point from the stream loop.
// We end the stream here only if a Hard failure has been encountered or the connection has been interrupted.
Serial.printf("Stream failed, code = %i : %s\r\n", res, esp_err_to_name(res));
break;
}
if((res != ESP_OK) || streamKill){
// We end the stream here when a kill is signalled.
Serial.printf("Stream killed\r\n");
break;
}
int64_t frame_time = esp_timer_get_time() - last_frame;
frame_time /= 1000;
int32_t frame_delay = (minFrameTime > frame_time) ? minFrameTime - frame_time : 0;
delay(frame_delay);

if (debugData) {
Serial.printf("MJPG: %uB %ums, delay: %ums, framerate (%.1ffps)\r\n",
(uint32_t)(_jpg_buf_len),
(uint32_t)frame_time, frame_delay, 1000.0 / (uint32_t)(frame_time + frame_delay));
}
last_frame = esp_timer_get_time();
}

streamsServed++;
streamCount = 0;
if (autoLamp && (lampVal != -1)) setLamp(0);
Serial.println("Stream ended");
last_frame = 0;
return res;
int fd=httpd_req_to_sockfd(req);
if(fd==-1){
printf("[stream_handler] could not get socket fd!\n");
return ESP_FAIL;
}

if(cam_streamer_enqueue_client(cam_streamer, fd))
++streamsServed;

return ESP_OK;
}

static esp_err_t cmd_handler(httpd_req_t *req){
Expand Down Expand Up @@ -391,7 +306,7 @@ static esp_err_t cmd_handler(httpd_req_t *req){
else if(!strcmp(variable, "autolamp") && (lampVal != -1)) {
autoLamp = val;
if (autoLamp) {
if (streamCount > 0) setLamp(lampVal);
if (cam_streamer_get_num_clients(cam_streamer) > 0) setLamp(lampVal);
else setLamp(0);
} else {
setLamp(lampVal);
Expand All @@ -400,7 +315,7 @@ static esp_err_t cmd_handler(httpd_req_t *req){
else if(!strcmp(variable, "lamp") && (lampVal != -1)) {
lampVal = constrain(val,0,100);
if (autoLamp) {
if (streamCount > 0) setLamp(lampVal);
if (cam_streamer_get_num_clients(cam_streamer) > 0) setLamp(lampVal);
else setLamp(0);
} else {
setLamp(lampVal);
Expand Down Expand Up @@ -597,7 +512,7 @@ static esp_err_t dump_handler(httpd_req_t *req){
int McuTf = temprature_sens_read(); // fahrenheit

d+= sprintf(d,"Up: %" PRId64 ":%02i:%02i:%02i (d:h:m:s)<br>\n", upDays, upHours, upMin, upSec);
d+= sprintf(d,"Active streams: %i, Previous streams: %lu, Images captured: %lu<br>\n", streamCount, streamsServed, imagesServed);
d+= sprintf(d,"Active streams: %i, Previous streams: %lu, Images captured: %lu<br>\n", cam_streamer_get_num_clients(cam_streamer), streamsServed, imagesServed);
d+= sprintf(d,"CPU Freq: %i MHz, Xclk Freq: %i MHz<br>\n", ESP.getCpuFreqMHz(), xclk);
d+= sprintf(d,"<span title=\"NOTE: Internal temperature sensor readings can be innacurate on the ESP32-c1 chipset, and may vary significantly between devices!\">");
d+= sprintf(d,"MCU temperature : %i &deg;C, %i &deg;F</span>\n<br>", McuTc, McuTf);
Expand Down Expand Up @@ -634,7 +549,7 @@ static esp_err_t dump_handler(httpd_req_t *req){
static esp_err_t stop_handler(httpd_req_t *req){
flashLED(75);
Serial.println("\r\nStream stop requested via Web");
streamKill = true;
cam_streamer_dequeue_all_clients(cam_streamer);
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
return httpd_resp_send(req, NULL, 0);
}
Expand Down Expand Up @@ -877,7 +792,13 @@ void startCameraServer(int hPort, int sPort){
httpd_register_uri_handler(stream_httpd, &stream_uri);
httpd_register_uri_handler(stream_httpd, &info_uri);
httpd_register_uri_handler(stream_httpd, &streamviewer_uri);
}
cam_streamer=(cam_streamer_t *) malloc(sizeof(cam_streamer_t));
#ifndef CAM_STREAMER_DESIRED_FPS
#define CAM_STREAMER_DESIRED_FPS 2
#endif
cam_streamer_init(cam_streamer, stream_httpd, CAM_STREAMER_DESIRED_FPS);
cam_streamer_start(cam_streamer);
}
httpd_register_uri_handler(stream_httpd, &favicon_16x16_uri);
httpd_register_uri_handler(stream_httpd, &favicon_32x32_uri);
httpd_register_uri_handler(stream_httpd, &favicon_ico_uri);
Expand Down
Loading