How do I build an MJPEG server via WebSocket on ESP32 using Mongoose

I want to build a MJPEG server via websocket on esp32 in esp idf The problem is that I am using the mongoose network stack, and I have to know exactly how would the ws header look like After some searching, I found that I need data:image/jpeg;base64, and I did this :
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "data:image/jpeg;base64,");
mg_ws_send(c, frame_buf->buf, frame_buf->len,WEBSOCKET_OP_BINARY);
mg_ws_send(c, "\r\n", 2,WEBSOCKET_OP_TEXT);
mg_ws_printf(c, WEBSOCKET_OP_TEXT, "data:image/jpeg;base64,");
mg_ws_send(c, frame_buf->buf, frame_buf->len,WEBSOCKET_OP_BINARY);
mg_ws_send(c, "\r\n", 2,WEBSOCKET_OP_TEXT);
unfortunately, nothing shows up. I have to mention that the basic text communication and the frame read are functioning
4 Replies
Marvee Amasi
Marvee Amasi3mo ago
Hey @te0 as you are sending a header as text and the frame as binary, maybe the WebSocket client might not be handling these frames together as you expect it to do so. But do you know that MJPEG actually doesn't require base64 encoding, and using base64 can increase the size unnecessarily
Marvee Amasi
Marvee Amasi3mo ago
You really don’t need the data:image/jpeg;base64 prefix for WebSocket communication that's unless you're embedding the image directly in HTML or working with Data URLs. Instead, send the raw JPEG frames directly as binary data over the WebSocket
Marvee Amasi
Marvee Amasi3mo ago
Sort of like : mg_ws_send(c, frame_buf->buf, frame_buf->len, WEBSOCKET_OP_BINARY);
te0
te03mo ago
thanks I solved it
if (frame_buf->buf != NULL)
{
size_t count = mg_ws_printf(c, WEBSOCKET_OP_TEXT, "Content-Type: image/jpeg Content-Length: %lu\r\n\r\n", frame_buf->len);
ESP_LOGI(TAG,"count : %d",count);
count = mg_ws_send(c, frame_buf->buf, frame_buf->len,WEBSOCKET_OP_BINARY);
ESP_LOGI(TAG,"count2 : %d",count);
vTaskDelay(500/portTICK_PERIOD_MS);
esp_camera_fb_return(frame_buf);
}
if (frame_buf->buf != NULL)
{
size_t count = mg_ws_printf(c, WEBSOCKET_OP_TEXT, "Content-Type: image/jpeg Content-Length: %lu\r\n\r\n", frame_buf->len);
ESP_LOGI(TAG,"count : %d",count);
count = mg_ws_send(c, frame_buf->buf, frame_buf->len,WEBSOCKET_OP_BINARY);
ESP_LOGI(TAG,"count2 : %d",count);
vTaskDelay(500/portTICK_PERIOD_MS);
esp_camera_fb_return(frame_buf);
}
Want results from more Discord servers?
Add your server