Rendering MJpeg stream in html5

You can do this without repeatedly making Http requests. Only one will suffice. You can use the fetch api to create a ReadableStream, access it’s Reader and keep reading from the stream.

Once you have the reader keep reading chunks from the stream recursively. Look for the SOI ( 0xFF 0xD8) in the byte stream which signals the end of the header and the beginning of the JPEG frame. The header will contain the length of the JPEG in bytes to be read. Read that many bytes from the chunk and any successive chunks and store it into a Uint8Array. Once you’ve
successfully read the frame convert it into a blob, create a UrlObject out of it and assign it to the src property of your img object.

Keep doing this till the connection is closed.

Shameless plug. Here’s a link to a working sample on github.

Leave a Comment