From 1b836c5479f3ed3e13408b4f502f7cf770752a86 Mon Sep 17 00:00:00 2001 From: 15498th Date: Sun, 19 Dec 2021 00:08:16 +0300 Subject: [PATCH] Apply framerate limiting delay after serving content boundary. Putting delay after finishing serving the frame causes image to appear in stream with a lag. The reason for this appears to be that browser relies on next frame content boundary in order to determine the end of image. That means that in order for browser to show frame right after receiving it, server needs to send next frame content boundary right after image itself, and delay to limit framerate should be applied after content boundary, not before it. According to https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html sending first frame without content boundary will likely cause its content to be ignored, which seems to be acceptable price for not treating first iteration of the loop as special case. --- app_httpd.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app_httpd.cpp b/app_httpd.cpp index 2026c4b..36654bb 100644 --- a/app_httpd.cpp +++ b/app_httpd.cpp @@ -251,9 +251,6 @@ static esp_err_t stream_handler(httpd_req_t *req){ _jpg_buf = fb->buf; } } - if(res == ESP_OK){ - res = httpd_resp_send_chunk(req, _STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY)); - } 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); @@ -261,6 +258,9 @@ static esp_err_t stream_handler(httpd_req_t *req){ 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;