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

Streaming question #174

Open
hollidayc opened this issue Jul 31, 2023 · 1 comment
Open

Streaming question #174

hollidayc opened this issue Jul 31, 2023 · 1 comment

Comments

@hollidayc
Copy link

I have an ESP32-CAM and I am trying to write the code to stream the images over a HTTPS connection using this library.

I have written a handler (based on someone else's code and modified it to use your library). It loops round grabbing the image from the camera and then uses the HTTPResponse write method to send the chunk to the client. It works perfectly find until the client disconnects.

If I don't break out of the loop it continues writing even though there are no clients. To stop this, I compare the length returned from write function with the number of bytes being written and if different I assume that the client has disconnected so I break out of the loop. This is my handler code:

void handleStream(httpsserver::HTTPRequest * req, httpsserver::HTTPResponse * res) {

// first check if logged in
if (!isLoggedIn(req)) {
res->setHeader("Location", "/login");
req->discardRequestBody();
res->setStatusCode(302);
res->setStatusText("Login required");
return;
}

// Camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
handle500(req, res, "Camera init failed");
return;
}

camera_fb_t * fb = NULL;
size_t _jpg_buf_len = 0;
uint8_t * _jpg_buf = NULL;
char * part_buf[64];

res->setHeader("Content-Type", _STREAM_CONTENT_TYPE);

while(true){
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
handle500(req, res, "Camera capture failed");
return;
} else {
if(fb->width > 400){
if(fb->format != PIXFORMAT_JPEG){
bool jpeg_converted = frame2jpg(fb, 80, &_jpg_buf, &_jpg_buf_len);
esp_camera_fb_return(fb);
fb = NULL;
if(!jpeg_converted){
Serial.println("JPEG compression failed");
handle500(req, res, "JPEG compression failed");
return;
}
} else {
_jpg_buf_len = fb->len;
_jpg_buf = fb->buf;
}
}
}

size_t chunk_len = snprintf((char *)part_buf, 64, _STREAM_PART, _jpg_buf_len);
size_t written_size = 0;
written_size = res->write( (const unsigned char *)part_buf, chunk_len);  
if (written_size != chunk_len) {
  Serial.println("client disconnected");
  break;
}
written_size = res->write( (const unsigned char *)_jpg_buf, _jpg_buf_len);
if (written_size != _jpg_buf_len) {
  Serial.println("client disconnected");
  break;
}
written_size = res->write( (const unsigned char *)_STREAM_BOUNDARY, strlen(_STREAM_BOUNDARY));
if (written_size != strlen(_STREAM_BOUNDARY)) {
  Serial.println("client disconnected");
  break;
}
if(fb){
  esp_camera_fb_return(fb);
  fb = NULL;
  _jpg_buf = NULL;
} else if(_jpg_buf){
  free(_jpg_buf);
  _jpg_buf = NULL;
}

}
return;
}

FIRST QUESTION: It that the best way to detect the client disconnecting?
SECOND QUESTION: Should I do something before I exit the handler with a return statement?

As it stands, the messages I see written to the serial monitor are:

12:39:52.561 -> client disconnected
12:39:52.561 -> [HTTPS:E] An receive error occured, FID=49
12:39:52.561 -> [HTTPS:I] Connection closed. Socket FID=49
12:39:52.561 -> Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
12:39:52.561 ->
12:39:52.561 -> Core 1 register dump:
12:39:52.561 -> PC : 0x4019aedb PS : 0x00060530 A0 : 0x800d85cc A1 : 0x3ffcf980
12:39:52.561 -> A2 : 0x00000000 A3 : 0x00000000 A4 : 0x22399abf A5 : 0x3ffcf93c
12:39:52.561 -> A6 : 0x3ffc596c A7 : 0x3ffcf93c A8 : 0x800d864a A9 : 0x3ffcf960
12:39:52.561 -> A10 : 0x3ffdd688 A11 : 0x00000000 A12 : 0x00000020 A13 : 0x3ffcf980
12:39:52.594 -> A14 : 0x3ffcf900 A15 : 0x00000008 SAR : 0x0000001b EXCCAUSE: 0x0000001c
12:39:52.594 -> EXCVADDR: 0x00000018 LBEG : 0x4008b338 LEND : 0x4008b354 LCOUNT : 0xffffffff
12:39:52.594 ->
12:39:52.594 -> Backtrace: 0x4019aed8:0x3ffcf980 0x400d85c9:0x3ffcf9a0 0x400d6027:0x3ffcf9c0 0x40199d42:0x3ffcf9e0 0x400d736b:0x3ffcfa00 0x400d89e6:0x3ffcfb40 0x400d3446:0x3ffcfb70 0x400dafa1:0x3ffcfb90
12:39:52.594 ->
12:39:52.594 -> ELF file SHA256: 3b10994683e92bc7
12:39:52.635 ->
12:39:52.760 -> Rebooting...

which translates in a debugger to:

PC: 0x4019aedb: SSL_pending at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/openssl/library/ssl_lib.c line 574
EXCVADDR: 0x00000018

Decoding stack results
0x4019aed8: SSL_pending at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/openssl/library/ssl_lib.c line 569
0x400d85c9: httpsserver::HTTPSConnection::pendingByteCount() at /home/chris/sketchbook/libraries/ESP32_HTTPS_Server/src/HTTPSConnection.cpp line 116
0x400d6027: httpsserver::HTTPConnection::pendingBufferSize() at /home/chris/sketchbook/libraries/ESP32_HTTPS_Server/src/HTTPConnection.cpp line 258
0x40199d42: httpsserver::HTTPRequest::requestComplete() at /home/chris/sketchbook/libraries/ESP32_HTTPS_Server/src/HTTPRequest.cpp line 109
0x400d736b: httpsserver::HTTPConnection::loop() at /home/chris/sketchbook/libraries/ESP32_HTTPS_Server/src/HTTPConnection.cpp line 515
0x400d89e6: httpsserver::HTTPServer::loop() at /home/chris/sketchbook/libraries/ESP32_HTTPS_Server/src/HTTPServer.cpp line 122
0x400d3446: loop() at /home/chris/sketchbook/ESP32-Cam-IP-Camera-with-simple-authentication/ESP32-Cam-IP-Camera-with-simple-authentication.ino line 272
0x400dafa1: loopTask(void*) at /home/chris/.arduino15/packages/esp32/hardware/esp32/2.0.11/cores/esp32/main.cpp line 50
"Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled."

QUESTION 3: Is this a bug? Don't want to raise it as such if it is me that has not coded something correctly.

Thanks in advance for any help.

@mylab4492
Copy link

can you post your working code of capturing image with https

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants