Beginner developer question: streaming mjpeg in nginx

Cristiano Belloni belloni at imavis.com
Wed Feb 8 11:30:10 UTC 2012


Hi to all,

I'd like to stream live, continuous Motion JPEG throught nginx web 
server. Right now, I have a little program that captures the frames from 
my hardware, separates them with boundaries and send them to stdout. 
Here's, schematically, what my program does:

// As soon as someone connects

fprintf (stdout, "Connection: close"\
                  "\r\n" \
                  "Cache-Control: no-store, no-cache, must-revalidate, 
pre-check=0, post-check=0, max-age=0" \
                  "\r\n" \
                  "Pragma: no-cache" \
                  "\r\n" \
                  "Content-Type: multipart/x-mixed-replace;boundary=" 
BOUNDARY \
                  "\r\n" \
                  "\r\n" \
                  "--" BOUNDARY "\r\n");

[...]

while (1) {

     //Magically take the frame from the shared memory

     fprintf(stdout, "Content-Type: image/jpeg\r\n" \
                     "Content-Length: %d\r\n" \
                     "\r\n", frame_length);

     DBG("Sending MJPEG frame\n");

     if ((fwrite(frame_data, frame_length, sizeof (char) , stdout)) == 0 ) {
         ERR ("Error sending frame");
         break;
     }

     DBG("Sending boundary\n");

     fprintf(stdout, "\r\n--" BOUNDARY "\r\n");
     }



I've seen examples of nginx modules that stream h264 files, but this 
case is quite different: live streaming means a potentially infinite 
MJPEG stream and no buffering done by the webserver.

I'm an nginx beginner, so I'm asking for some directions.

How can I stream this (potentially infinite) series of frames? A CGI is 
sufficient o will I need to write a module? Is it even possible to do 
something like this with nginx?

Thanks,
Cristiano.

-- 
Belloni Cristiano
Imavis Srl.
www.imavis.com <http://www.imavis.com>
belloni at imavis.com <mailto://belloni@imavis.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20120208/93bf154e/attachment.html>


More information about the nginx mailing list