Using django as a mjpeg streaming server

Hi guys,

I´m working on a project a bit strange. I have an mjpeg cam stream who i need to fetch and refeed to a user, and give some funcionalities like pause and go forward and backward.

What i did:

When user send GET to the server for the player page i create one thread who is always fill one circular buffer, for ever.
Next when the user hits the pause button i copy the buffer to a new auxililiar buffer and send the last pic, when the user hits the backward i grab the auxiliar buffer and send the pics bacward.

In the development server works mor or less. Some times get stuck, but i try on ubuntu with gunicorn with 1 worker and nginx and the server streams the mjpeg but do nothing more, i send a bunch more GETs to the server bue he is stuck with the StreamingHttpResponse().

I need some help. probabily i´m using the Django in a way that wasn’t supposed to.

Best regards Gonçalo Marques

You’re right, you really don’t want to try to do this in Django. Typically, this is handled by some JavaScript running in the browser, taking advantage of facilities available in a common web server (e.g. nginx)

See HTTP Live Streaming - Wikipedia for some more detailed information and possible solutions. You can also do a web search for HTML5 Video players for more alternatives.

that’s what i thought. The problem is that it’s in mjpeg, I haven’t found any library or software to do this kind of thing.

That seems surprising to me based on what I’m seeing here: Motion JPEG - Wikipedia

I found nothing to js. i´m playing the stream using the src image = stream url. but its a img and have no methods or code to help me. I thought grabing the stream in the browser and parse the stream frame by frame but no luck in that either.
Its hard to find js player to play mjoeg stream. i tried a bunch of open source player, none work =(

You should be looking at the video tag for streaming media. See Live streaming web audio and video - Developer guides | MDN

First thing i tried, no luck, dont support mjpeg

How are you doing the streaming on the server-side?

Quoting directly from the referenced page:

In order to stream live audio and video, you will need to run specific streaming software on your server or use third-party services.

There is no audio. So what i have is very simple.
Server side - I´m using urllib.request.urlopen and a while True, to fetch the stream, i´m parse the stream and store img by img on a deqeue.
Client side i´m using img tag with src url tha do a GET to server side.
Server side - using while true, StreamingHttpResponse, and content_type=‘multipart/x-mixed-replace;boundary=FRAME’.
Its work just fine, the things get complicated when i execute actions on the stream, like pause and backward