From cdd14732adf61aacbf7e6f15db215b8881b3ea10 Mon Sep 17 00:00:00 2001 From: Georgi Kostov Date: Sun, 9 Nov 2025 11:53:53 +0200 Subject: [PATCH] Send the initial boundary line before the first part in the response (to conform with the RFC and appease ffmpeg) refs #67 --- src/esp32cam/asyncweb.cpp | 3 ++- src/esp32cam/asyncweb.hpp | 2 ++ src/esp32cam/mjpeg.cpp | 5 +++-- src/esp32cam/mjpeg.hpp | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/esp32cam/asyncweb.cpp b/src/esp32cam/asyncweb.cpp index c4b1f1e..b2e7d15 100644 --- a/src/esp32cam/asyncweb.cpp +++ b/src/esp32cam/asyncweb.cpp @@ -192,7 +192,8 @@ MjpegResponse::sendPart(uint8_t* buf, size_t buflen) { if (m_sendRemain == 0) { switch (m_sendNext) { case SIPartHeader: - m_hdr.preparePartHeader(m_ctrl.getFrame()->size()); + m_hdr.preparePartHeader(m_ctrl.getFrame()->size(), !isInitialBoundarySent); + isInitialBoundarySent = true; m_sendBuf = reinterpret_cast(m_hdr.buf); m_sendRemain = m_hdr.size; m_sendNext = SIFrame; diff --git a/src/esp32cam/asyncweb.hpp b/src/esp32cam/asyncweb.hpp index e697c9b..df36430 100644 --- a/src/esp32cam/asyncweb.hpp +++ b/src/esp32cam/asyncweb.hpp @@ -104,6 +104,8 @@ class MjpegResponse : public AsyncAbstractResponse { private: size_t sendPart(uint8_t* buf, size_t buflen); + bool isInitialBoundarySent = false; + private: detail::CaptureTask m_task; using Ctrl = detail::MjpegController; diff --git a/src/esp32cam/mjpeg.cpp b/src/esp32cam/mjpeg.cpp index ef99ef9..9471238 100644 --- a/src/esp32cam/mjpeg.cpp +++ b/src/esp32cam/mjpeg.cpp @@ -77,8 +77,9 @@ MjpegHeader::prepareResponseContentType() { } void -MjpegHeader::preparePartHeader(size_t contentLength) { - size = snprintf(buf, sizeof(buf), +MjpegHeader::preparePartHeader(size_t contentLength, bool includeInitialBoundary) { + size = includeInitialBoundary ? snprintf(buf, sizeof(buf), "--" BOUNDARY "\r\n") : 0; + size += snprintf(buf + size, sizeof(buf) - size, "Content-Type: image/jpeg\r\n" "Content-Length: %zu\r\n" "\r\n", diff --git a/src/esp32cam/mjpeg.hpp b/src/esp32cam/mjpeg.hpp index b273b94..0940b1e 100644 --- a/src/esp32cam/mjpeg.hpp +++ b/src/esp32cam/mjpeg.hpp @@ -108,7 +108,7 @@ class MjpegHeader { void prepareResponseContentType(); - void preparePartHeader(size_t contentLength); + void preparePartHeader(size_t contentLength, bool includeInitialBoundary = false); void preparePartTrailer();