Possible to send multiple images (stream) - e.g. webcam on ESP32CAM? #143
Replies: 11 comments
-
I would also be interested in the streaming functionality |
Beta Was this translation helpful? Give feedback.
-
@HowardsPlayPen. That documentation is referencing the webhandler which is just your basic web request with one response to one request. Other web protocols are definitely possible. I'm not entirely familiar with streaming image protocols though. Do you have any information on exactly what would be implemented? Depending on the details, it would probably be best to implement this as a handler. For example the EventSource class is a handler that sends a response, but then keeps the connection open to stream events to connected clients. The way that is setup it can handle multiple clients easily. Websockets are very well supported too. My app is entirely built around websockets and its the main reason for creating this library as the ESPAsync implementation is super buggy. If you handle the responses asynchronously you can do 150-200 msg/sec. I'm going to write a simplified example of how I do that with xqueues shortly. |
Beta Was this translation helpful? Give feedback.
-
Websockets xQueue example: https://github.com/hoeken/PsychicHttp/tree/master/examples/websockets |
Beta Was this translation helpful? Give feedback.
-
I have now got a local version of ElegantOTA updated to have an extra option to support PsychicHttp and it is working (using the Response option you separately mentioned for adding gzip headers) - so I am now coming back to look at this question again. I will use the WebSockets going forward for all manner of uses - but for the webcam side your mention of implementing a new handler sounds useful. Could you give a few lines of description of how you would do it (I say this as I just read that one should not cache handlers but instead cache socket handles(?) - so I would appreciate a simple pseudo code example of how you would see a handler being retained for sending multiple responses? Thanks for all your help so far |
Beta Was this translation helpful? Give feedback.
-
@HowardsPlayPen if you have a link to a blog post or something describing what you'd like to do that would help me understand it better. that being said, I would probably start with the EventSource stuff. It has a custom handler, client, and response to generate the specific EventSource information. You might not need all of that, since the Response generates its own HTTP header, the client has custom functions to send events, and the handler overrides the onOpen, onClose, and handleResponse to add its own custom hooks. Hard to say exactly what would need to change without seeing something with more details on what you're trying to do. Can you send me that esp32-cam code you're looking to replicate? |
Beta Was this translation helpful? Give feedback.
-
The current cam code only responds to a single client at a time - so has a loop taking over the main thread. The HTTP header at the beginning is therefore telling the client to expect multiple parts and each new image has a Header showing it is a new part (but clearly this is obvious when you read the code so I do not mean to teach you to suck eggs..)
All I want to do is remove the above while(1) loop and merely push a new Handler / Request into a container to hold all active connections - then wait for the next cam image to be available, i.e.
Then use Handler.reply() for each of the active connections using the buffer
One example GitHub project is: https://github.com/rzeldent/esp32cam-rtsp/blob/main/src/main.cpp see line 232 handle_stream() I will have a look at the EventSource code though as that might be show the obvious solution. |
Beta Was this translation helpful? Give feedback.
-
Okay yep I see how it works and yeah you should definitely be able to
modify the event source stuff to do this. Send that first set of headers
in the handler and then modify the client to send the stuff in the while
loop and you should be able to handle multiple clients no problem.
Let me know if you run into any issues.
…On Thu, Dec 28, 2023, 15:24 HowardsPlayPen ***@***.***> wrote:
The current cam code only responds to a single client at a time - so has a
loop taking over the main thread. The HTTP header at the beginning is
therefore telling the client to expect multiple parts and each new image
has a Header showing it is a new part (but clearly this is obvious when you
read the code so I do not mean to teach you to suck eggs..)
WiFiClient client = server.client();
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: multipart/x-mixed-replace; boundary=frame\r\n\r\n";
server.sendContent(response);
while (1)
{
cam.run();
if (!client.connected())
break;
response = "--frame\r\n";
response += "Content-Type: image/jpeg\r\n\r\n";
server.sendContent(response);
client.write((char *)cam.getfb(), cam.getSize());
server.sendContent("\r\n");
if (!client.connected())
break;
}
All I want to do is remove the above while(1) loop and merely push a new
Handler / Request into a container to hold all active connections - then
wait for the next cam image to be available, i.e.
cam.run()
Then use Handler.reply() for each of the active connections using the
buffer
Handler.reply( (char *)cam.getfb(), cam.getSize());
One example GitHub project is:
https://github.com/rzeldent/esp32cam-rtsp/blob/main/src/main.cpp
see line 232 handle_stream()
I will have a look at the EventSource code though as that might be show
the obvious solution.
—
Reply to this email directly, view it on GitHub
<#38 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABEHSV3MAS6U56F4H7R5ETYLXIQ5AVCNFSM6AAAAABBBVWNLOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZRGQ3DQMRSGI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
I would be more than happy to see some esp32cam - webserver codes in examples. |
Beta Was this translation helpful? Give feedback.
-
Has someone already implemented video webcam streaming with PsychicHttp ? If yes, could example be shared ? This ESPAsyncWebServer code provided by me-no-dev https://gist.github.com/me-no-dev/d34fba51a8f059ac559bf62002e61aa3 works OK (for my project). I would like to migrate this ESPAsyncWebServer code to PsychicHttp (if possible ?), or use another working solution for video webcam streaming, so maybe someone has already done the job ? Thanks |
Beta Was this translation helpful? Give feedback.
-
For those interested, example below handles streaming video with PsychicHttp. Code, screenshots and output are provided. CONFIGURATION CODE
OUTPUT
|
Beta Was this translation helpful? Give feedback.
-
How does this not trigger the watchdog? |
Beta Was this translation helpful? Give feedback.
-
Background: my aim is to use an ESP32CAM as a webcam using the commonly found code on the web - but to also include ElegantOTA for simple OTA updates (which brought in ESPAsyncWebServer). Ideally I wanted to be able to stream the images - and handle more than one client (the commonly available code only handles one client at a time)
So this library looks great - better / more performant than ESPAsyncWebServer (and still supported)
Question: I can see the documentation say:
"You can not send more than one response to a single request."
which seems to categorically rule out my request(? Unless there is a distinction between multiple Response objects versus sending multiple blobs over a single connection?)
I could use WebSockets - so again this library looks great if that is the case.
Apologies if this is a pointless request or asked before (I could not find it and I believe I went through all Issues)
Summary:
Can I cache Response objects and send multiple blobs (e.g. call setContent / send multiple times?).
Beta Was this translation helpful? Give feedback.
All reactions