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

Support for limiting streaming FPS (AIV-442) #170

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ The folder of [examples](examples) contains sample applications demonstrating th

Take one Face Detection as an example.

1. Get into one example folder `esp-who/examples/single_chip/detection_with_command_line`.
1. Get into one example folder `esp-who/examples/single_chip/face_detection_with_command_line`.
```
cd esp-who/examples/single_chip/detection_with_command_line
cd esp-who/examples/single_chip/face_detection_with_command_line
```

2. Compile and flash the project.
Expand Down
2 changes: 1 addition & 1 deletion examples/single_chip/camera_web_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ After you've completed the hardware settings, please follow the steps below:
4. **Open Your Browser** and point it to `http://[ip-of-esp32]/`;
5. **To Get Image** press `Get Still` or `Start Stream`;
6. **Use The Options** to enable/disable Face Detection, Face Recognition and more;
t. **View The Stream** in a player like VLC: Open Network `http://[ip-of-esp32]:81/stream`;
7. **View The Stream** in a player like VLC: Open Network `http://[ip-of-esp32]:81/stream`;

For more details of the http handler, please refer to [esp32-camera](https://github.com/espressif/esp32-camera).
98 changes: 83 additions & 15 deletions examples/single_chip/camera_web_server/main/app_httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ bool isStreaming = false;
#endif
#endif

#define FPS_DEFAULT -1 // -1 = Unlimited FPS
#define FPS_TO_PERIOD(FPS) (FPS<1?0:(1000000/FPS))
unsigned int fps_max = FPS_DEFAULT;
uint64_t fps_period_us = FPS_TO_PERIOD(FPS_DEFAULT);

typedef struct
{
httpd_req_t *req;
Expand Down Expand Up @@ -289,6 +294,44 @@ void enable_led(bool en)
}
#endif

static esp_err_t bmp_handler(httpd_req_t *req)
{
camera_fb_t *fb = NULL;
esp_err_t res = ESP_OK;
uint64_t fr_start = esp_timer_get_time();
fb = esp_camera_fb_get();
if (!fb)
{
ESP_LOGE(TAG, "Camera capture failed");
httpd_resp_send_500(req);
return ESP_FAIL;
}

httpd_resp_set_type(req, "image/x-windows-bmp");
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.bmp");
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

char ts[32];
snprintf(ts, 32, "%ld.%06ld", fb->timestamp.tv_sec, fb->timestamp.tv_usec);
httpd_resp_set_hdr(req, "X-Timestamp", (const char *)ts);


uint8_t * buf = NULL;
size_t buf_len = 0;
bool converted = frame2bmp(fb, &buf, &buf_len);
esp_camera_fb_return(fb);
if(!converted){
ESP_LOGE(TAG, "BMP Conversion failed");
httpd_resp_send_500(req);
return ESP_FAIL;
}
res = httpd_resp_send(req, (const char *)buf, buf_len);
free(buf);
uint64_t fr_end = esp_timer_get_time();
ESP_LOGI(TAG, "BMP: %llums, %uB", (uint64_t)((fr_end - fr_start) / 1000), buf_len);
return res;
}

static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_t len)
{
jpg_chunking_t *j = (jpg_chunking_t *)arg;
Expand Down Expand Up @@ -440,6 +483,7 @@ static esp_err_t stream_handler(httpd_req_t *req)
int64_t fr_encode = 0;
#endif

int64_t next_frame_time = 0;
static int64_t last_frame = 0;
if (!last_frame)
{
Expand All @@ -462,6 +506,13 @@ static esp_err_t stream_handler(httpd_req_t *req)

while (true)
{
// Wait to limit FPS if required
// NB fps_delay_ms can be negative if processing takes too long -> in that case don't wait
int64_t fps_delay_ms = (next_frame_time - esp_timer_get_time()) / 1000;
if (fps_max > 0 && fps_delay_ms > 0)
vTaskDelay(fps_delay_ms / portTICK_PERIOD_MS);
next_frame_time = esp_timer_get_time() + fps_period_us;

#if CONFIG_ESP_FACE_DETECT_ENABLED
detected = false;
face_id = 0;
Expand Down Expand Up @@ -664,8 +715,10 @@ static esp_err_t cmd_handler(httpd_req_t *req)
char variable[32];
char value[32];

if (parse_get(req, &buf) != ESP_OK ||
httpd_query_key_value(buf, "var", variable, sizeof(variable)) != ESP_OK ||
if (parse_get(req, &buf) != ESP_OK) {
return ESP_FAIL;
}
if (httpd_query_key_value(buf, "var", variable, sizeof(variable)) != ESP_OK ||
httpd_query_key_value(buf, "val", value, sizeof(value)) != ESP_OK) {
free(buf);
httpd_resp_send_404(req);
Expand All @@ -686,6 +739,10 @@ static esp_err_t cmd_handler(httpd_req_t *req)
}
}
}
else if (!strcmp(variable, "fps_max")) {
fps_max = val;
fps_period_us = FPS_TO_PERIOD(fps_max);
}
else if (!strcmp(variable, "quality"))
res = s->set_quality(s, val);
else if (!strcmp(variable, "contrast"))
Expand Down Expand Up @@ -761,10 +818,11 @@ static esp_err_t cmd_handler(httpd_req_t *req)
#endif
#endif
else {
ESP_LOGI(TAG, "Unknown command: %s", variable);
res = -1;
}

if (res) {
if (res < 0) {
return httpd_resp_send_500(req);
}

Expand Down Expand Up @@ -807,7 +865,7 @@ static esp_err_t status_handler(httpd_req_t *req)
p+=print_reg(p, s, reg, 0xFF);
}
p+=print_reg(p, s, 0x558a, 0x1FF);//9 bit
} else {
} else if(s->id.PID == OV2640_PID){
p+=print_reg(p, s, 0xd3, 0xFF);
p+=print_reg(p, s, 0x111, 0xFF);
p+=print_reg(p, s, 0x132, 0xFF);
Expand All @@ -817,6 +875,7 @@ static esp_err_t status_handler(httpd_req_t *req)
p += sprintf(p, "\"xclk\":%u,", s->xclk_freq_hz / 1000000);
p += sprintf(p, "\"pixformat\":%u,", s->pixformat);
p += sprintf(p, "\"framesize\":%u,", s->status.framesize);
p += sprintf(p, "\"fps_max\":%u,", fps_max);
p += sprintf(p, "\"quality\":%u,", s->status.quality);
p += sprintf(p, "\"brightness\":%d,", s->status.brightness);
p += sprintf(p, "\"contrast\":%d,", s->status.contrast);
Expand Down Expand Up @@ -873,8 +932,10 @@ static esp_err_t xclk_handler(httpd_req_t *req)
char *buf = NULL;
char _xclk[32];

if (parse_get(req, &buf) != ESP_OK ||
httpd_query_key_value(buf, "xclk", _xclk, sizeof(_xclk)) != ESP_OK) {
if (parse_get(req, &buf) != ESP_OK) {
return ESP_FAIL;
}
if (httpd_query_key_value(buf, "xclk", _xclk, sizeof(_xclk)) != ESP_OK) {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
Expand All @@ -901,8 +962,10 @@ static esp_err_t reg_handler(httpd_req_t *req)
char _mask[32];
char _val[32];

if (parse_get(req, &buf) != ESP_OK ||
httpd_query_key_value(buf, "reg", _reg, sizeof(_reg)) != ESP_OK ||
if (parse_get(req, &buf) != ESP_OK) {
return ESP_FAIL;
}
if (httpd_query_key_value(buf, "reg", _reg, sizeof(_reg)) != ESP_OK ||
httpd_query_key_value(buf, "mask", _mask, sizeof(_mask)) != ESP_OK ||
httpd_query_key_value(buf, "val", _val, sizeof(_val)) != ESP_OK) {
free(buf);
Expand Down Expand Up @@ -932,8 +995,10 @@ static esp_err_t greg_handler(httpd_req_t *req)
char _reg[32];
char _mask[32];

if (parse_get(req, &buf) != ESP_OK ||
httpd_query_key_value(buf, "reg", _reg, sizeof(_reg)) != ESP_OK ||
if (parse_get(req, &buf) != ESP_OK) {
return ESP_FAIL;
}
if (httpd_query_key_value(buf, "reg", _reg, sizeof(_reg)) != ESP_OK ||
httpd_query_key_value(buf, "mask", _mask, sizeof(_mask)) != ESP_OK) {
free(buf);
httpd_resp_send_404(req);
Expand Down Expand Up @@ -970,8 +1035,6 @@ static esp_err_t pll_handler(httpd_req_t *req)
char *buf = NULL;

if (parse_get(req, &buf) != ESP_OK) {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}

Expand Down Expand Up @@ -1001,8 +1064,6 @@ static esp_err_t win_handler(httpd_req_t *req)
char *buf = NULL;

if (parse_get(req, &buf) != ESP_OK) {
free(buf);
httpd_resp_send_404(req);
return ESP_FAIL;
}

Expand Down Expand Up @@ -1075,7 +1136,7 @@ static esp_err_t monitor_handler(httpd_req_t *req)
void app_httpd_main()
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.max_uri_handlers = 12;
config.max_uri_handlers = 16;

httpd_uri_t index_uri = {
.uri = "/",
Expand Down Expand Up @@ -1107,6 +1168,12 @@ void app_httpd_main()
.handler = stream_handler,
.user_ctx = NULL};

httpd_uri_t bmp_uri = {
.uri = "/bmp",
.method = HTTP_GET,
.handler = bmp_handler,
.user_ctx = NULL};

httpd_uri_t xclk_uri = {
.uri = "/xclk",
.method = HTTP_GET,
Expand Down Expand Up @@ -1179,6 +1246,7 @@ void app_httpd_main()
httpd_register_uri_handler(camera_httpd, &cmd_uri);
httpd_register_uri_handler(camera_httpd, &status_uri);
httpd_register_uri_handler(camera_httpd, &capture_uri);
httpd_register_uri_handler(camera_httpd, &bmp_uri);

httpd_register_uri_handler(camera_httpd, &xclk_uri);
httpd_register_uri_handler(camera_httpd, &reg_uri);
Expand Down
11 changes: 11 additions & 0 deletions examples/single_chip/camera_web_server/main/include/app_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
#define PCLK_GPIO_NUM 22

#elif CONFIG_CAMERA_MODEL_ESP32_CAM_BOARD
// The 18 pin header on the board has Y5 and Y3 swapped
#define USE_BOARD_HEADER 0

#define CAM_BOARD "ESP-DEVCAM"
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM 33
Expand All @@ -56,9 +59,17 @@
#define Y8_GPIO_NUM 19
#define Y7_GPIO_NUM 21
#define Y6_GPIO_NUM 39
#if USE_BOARD_HEADER
#define Y5_GPIO_NUM 13
#else
#define Y5_GPIO_NUM 35
#endif
#define Y4_GPIO_NUM 14
#if USE_BOARD_HEADER
#define Y3_GPIO_NUM 35
#else
#define Y3_GPIO_NUM 13
#endif
#define Y2_GPIO_NUM 34
#define VSYNC_GPIO_NUM 5
#define HREF_GPIO_NUM 27
Expand Down
13 changes: 13 additions & 0 deletions examples/single_chip/camera_web_server/main/www/index_ov2640.html
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,19 @@
<option value="0">96x96</option>
</select>
</div>
<div class="input-group" id="fps_max-group">
<label for="fps_max">FPS Max</label>
<select id="fps_max" class="default-action">
<option value="-1">Unlimited</option>
<option value="25">25</option>
<option value="20">20</option>
<option value="15">15</option>
<option value="10">10</option>
<option value="5">5</option>
<option value="2">2</option>
<option value="1">1</option>
</select>
</div>
<div class="input-group" id="quality-group">
<label for="quality">Quality</label>
<div class="range-min">4</div>
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/single_chip/camera_web_server/main/www/index_ov3660.html
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,19 @@
<option value="0">96x96</option>
</select>
</div>
<div class="input-group" id="fps_max-group">
<label for="fps_max">FPS Max</label>
<select id="fps_max" class="default-action">
<option value="-1">Unlimited</option>
<option value="25">25</option>
<option value="20">20</option>
<option value="15">15</option>
<option value="10">10</option>
<option value="5">5</option>
<option value="2">2</option>
<option value="1">1</option>
</select>
</div>
<div class="input-group" id="quality-group">
<label for="quality">Quality</label>
<div class="range-min">4</div>
Expand Down
Binary file not shown.
13 changes: 13 additions & 0 deletions examples/single_chip/camera_web_server/main/www/index_ov5640.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@
<option value="0">96x96</option>
</select>
</div>
<div class="input-group" id="fps_max-group">
<label for="fps_max">FPS Max</label>
<select id="fps_max" class="default-action">
<option value="-1">Unlimited</option>
<option value="25">25</option>
<option value="20">20</option>
<option value="15">15</option>
<option value="10">10</option>
<option value="5">5</option>
<option value="2">2</option>
<option value="1">1</option>
</select>
</div>
<div class="input-group" id="quality-group">
<label for="quality">Quality</label>
<div class="range-min">4</div>
Expand Down
Binary file not shown.