Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse MJPEG HTTP Stream within C++?

I need to access and read an http stream which is sending live MJPEG footage from a network camera, in order to do some opencv image processing on the image.

I can access the camera's footage through VLC, or simply by going to the URL in chrome or firefox. But how can I programmatically access the http server and separate each frame, when the server is just sending a continuous feed?

The data seems to be simply formatted, looping between the HTTP Header and JPEG data. The only way I can think of approaching this is somehow sending a request to the server, parsing the data as it comes in, and separating the header from the actual jpeg data, and, in turn, passing that to opencv.

However, that sounds awfully convoluted and I'm not quite sure where I'd start. Do you guys know if there are any libraries out there, or just a simpler approach I'm overlooking, that could make all this easier?

Thanks alot

like image 256
jonas Avatar asked Jan 26 '11 06:01

jonas


2 Answers

For HTTP download, you can use Libcurl library.

AFAIK MJPEG format is not a standardized format. Its actual byte format vary by implementations. But basically just concatenation of jpeg file with delimiters. If you look at bytes with a hex editor you could easily distinguish each jpeg file.

For example, ffmpeg's mjpeg output is structured like below:

0xff 0xd8 // start of jpeg
{ ... }   // jpeg body
0xff 0xd9 // end of jpeg
...
0xff 0xd8 // start of jpeg
{ ... }   // jpeg body
0xff 0xd9 // end of jpeg
...
like image 128
9dan Avatar answered Nov 09 '22 18:11

9dan


In this page:

http://thistleshrub.net/www/index.php?controller=posts&action=show&id=2012-05-13DisplayingStreamedMJPEGinJava.txt

Parse a MJPEG Stream with Java, I implemented this with flawlessly results in Java.

If you try to use with C++ you find some things missed: socket conection and render canvas, libcurl seems to be a good option to http request, but still missing the canvas, you can use something like GLUT or Qt.

I read in some forums that OpenCV can read input stream of type MJPEG Streamer, but seems they need to be the recent version of OpenCV (compile OpenCV from scratch it's hard).

I hope this help.

like image 1
Federico Ramos Avatar answered Nov 09 '22 18:11

Federico Ramos