Skip to content

Commit 3db8159

Browse files
committed
mjpeg: boundary correction
refs #67
1 parent fb0f07b commit 3db8159

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

src/esp32cam/asyncweb.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ MjpegResponse::_fillBuffer(uint8_t* buf, size_t buflen) {
151151
m_ctrl.notifyReturn(std::move(frame));
152152
}
153153
m_sendSince = millis();
154-
m_sendNext = SIPartHeader;
154+
m_sendNext = SIBoundary;
155155
m_sendRemain = 0;
156156

157157
if (m_ctrl.decideAction() != Ctrl::SEND) {
@@ -191,8 +191,8 @@ size_t
191191
MjpegResponse::sendPart(uint8_t* buf, size_t buflen) {
192192
if (m_sendRemain == 0) {
193193
switch (m_sendNext) {
194-
case SIPartHeader:
195-
m_hdr.preparePartHeader(m_ctrl.getFrame()->size());
194+
case SIBoundary:
195+
m_hdr.prepareBoundary(m_ctrl.getFrame()->size());
196196
m_sendBuf = reinterpret_cast<const uint8_t*>(m_hdr.buf);
197197
m_sendRemain = m_hdr.size;
198198
m_sendNext = SIFrame;
@@ -201,15 +201,9 @@ MjpegResponse::sendPart(uint8_t* buf, size_t buflen) {
201201
Frame* frame = m_ctrl.getFrame();
202202
m_sendBuf = frame->data();
203203
m_sendRemain = frame->size();
204-
m_sendNext = SIPartTrailer;
205-
break;
206-
}
207-
case SIPartTrailer:
208-
m_hdr.preparePartTrailer();
209-
m_sendBuf = reinterpret_cast<const uint8_t*>(m_hdr.buf);
210-
m_sendRemain = m_hdr.size;
211204
m_sendNext = SINone;
212205
break;
206+
}
213207
case SINone:
214208
return 0;
215209
}

src/esp32cam/asyncweb.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ class MjpegResponse : public AsyncAbstractResponse {
112112

113113
enum SendItem {
114114
SINone,
115-
SIPartHeader,
115+
SIBoundary,
116116
SIFrame,
117-
SIPartTrailer,
118117
};
119118
SendItem m_sendNext = SINone;
120119
const uint8_t* m_sendBuf = nullptr;

src/esp32cam/camera.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ CameraClass::streamMjpeg(Client& client, const MjpegConfig& cfg) {
8383
break;
8484
}
8585
case Ctrl::SEND: {
86-
hdr.preparePartHeader(ctrl.getFrame()->size());
86+
hdr.prepareBoundary(ctrl.getFrame()->size());
8787
hdr.writeTo(client);
8888
ctrl.notifySent(ctrl.getFrame()->writeTo(client, cfg.frameTimeout));
89-
hdr.preparePartTrailer();
9089
hdr.writeTo(client);
9190
break;
9291
}
9392
case Ctrl::STOP: {
93+
hdr.prepareTrailer();
94+
hdr.writeTo(client);
9495
client.stop();
9596
return ctrl.countSentFrames();
9697
}

src/esp32cam/mjpeg.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,17 @@ MjpegHeader::prepareResponseContentType() {
7777
}
7878

7979
void
80-
MjpegHeader::preparePartHeader(size_t contentLength) {
80+
MjpegHeader::prepareBoundary(size_t contentLength) {
8181
size = snprintf(buf, sizeof(buf),
82+
"\r\n--" BOUNDARY "\r\n"
8283
"Content-Type: image/jpeg\r\n"
8384
"Content-Length: %zu\r\n"
8485
"\r\n",
8586
contentLength);
8687
}
8788

8889
void
89-
MjpegHeader::preparePartTrailer() {
90+
MjpegHeader::prepareTrailer() {
9091
size = snprintf(buf, sizeof(buf), "\r\n--" BOUNDARY "\r\n");
9192
}
9293

src/esp32cam/mjpeg.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ class MjpegHeader {
108108

109109
void prepareResponseContentType();
110110

111-
void preparePartHeader(size_t contentLength);
111+
void prepareBoundary(size_t contentLength);
112112

113-
void preparePartTrailer();
113+
void prepareTrailer();
114114

115115
size_t writeTo(Print& os);
116116

0 commit comments

Comments
 (0)