ipkn / crow

Crow is very fast and easy to use C++ micro web framework (inspired by Python Flask)
BSD 3-Clause "New" or "Revised" License
7.49k stars 890 forks source link

Use CROW to implement a MJPEG video streaming server #302

Open shogunxam opened 6 years ago

shogunxam commented 6 years ago

Using flask and sending a response like: ---Response start here---- Content-Type: multipart/x-mixed-replace; boundary=frame --frame Content-Type: image/jpeg Content-Length: #image_size#

image_data

--frame-- ---Response end here----

I was able to build a very simple MJPEG video streaming server. Is it possible achieve the same result with CROW? I have tried with a similar approach but I'm able to show only the first frame, I have to manually refresh the browser to send another request and receive the new frame.

mrozigor commented 6 years ago

Could you show requests and responses, both from Flask and Crow? Maybe there are some differences in handling these requests?

shogunxam commented 6 years ago

I have tried to use wireshark to capture the packets and I have noticed a strange thing, filtering the pakets with "http.request or http.response" using crow I can se both the request and the response, using flask I can see only the request and I need to filter with "http" to see also the packets transmitted by flask. Here what I got:

--REQUEST (it's the same for flask and crow, I'm using chrome)-- GET /video.mjpg HTTP/1.1 Host: 192.168.1.5:7071 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8 DNT: 1 Accept-Encoding: gzip, deflate Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7,fr;q=0.6

--CROW RESPONSE-- HTTP/1.1 200 OK Content-Type: multipart/x-mixed-replace; boundary=frame Server: Crow/0.1 Date: Sat, 05 May 2018 10:03:34 GMT Connection: Keep-Alive --frame Content-Type: image/jpeg Content-Length: 147113

<< Image data >>

--FLASK RESPONSE (captured filtering with http)-- --frame Content-Type: image/jpeg Content-Length:149932

<< Image data >>

When I'm using flask, I'm passing the "multipart/x-mixed-replace; boundary=frame" to the response class as mime-type and not as a Content-Type but I don't know how to achive this with CROW.

mrozigor commented 6 years ago

Did you try try to return crow:response object, with custom headers? I see there is 'add_header(std::string key, std::string value)' method in this class.

shogunxam commented 6 years ago

Yes, I'm already using the method add_header, and I'm passing the mime-type as: response.add_header("Content-Type", "multipart/x-mixed-replace; boundary=frame"); I'm using "Content-Type" because, if I have understood well, mime-type is not a valid entry.

Anyway I think I will drop CROW to use a more tailored frame for my needed, because I have to stream only a mjpg from a webcam. Thank you any way for your help

mrozigor commented 6 years ago

I looks like mimetype from Flask's Response class only set 'Content-Type' header. Maybe CROW doesn't stream due to double Content-Type entry as it is in your responses?

shogunxam commented 6 years ago

I have already tried, but I get the same result, and without setting the mime-type on flask it doesn't work as well. Pheraps I'm missing a loop in frame generator. Anyway, also using flask, I don't understand who is triggering the request for the new frame.

mrozigor commented 6 years ago

Can you maybe make wireshark dump with Flask streaming session, as well as Crow? Or maybe there is some method to dump response object in Flask?

Rz-Rz commented 2 months ago

Sorry for posting in an old topic but I'm having the same problem I can't seem to get MJPEG streaming to work on Crow cpp webserver, here's an issue I opened : https://github.com/CrowCpp/Crow/issues/890