Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MJPEG Player in JavaScript
// namespace MJPEG { ...
var MJPEG = (function(module) {
"use strict";
// class Stream { ...
module.Stream = function(args) {
var self = this;
var autoStart = args.autoStart || false;
self.url = args.url;
self.refreshRate = args.refreshRate || 500;
self.onStart = args.onStart || null;
self.onFrame = args.onFrame || null;
self.onStop = args.onStop || null;
self.callbacks = {};
self.running = false;
self.frameTimer = 0;
self.img = new Image();
if (autoStart) {
self.img.onload = self.start;
}
self.img.src = self.url;
function setRunning(running) {
self.running = running;
if (self.running) {
self.img.src = self.url;
self.frameTimer = setInterval(function() {
if (self.onFrame) {
self.onFrame(self.img);
}
}, self.refreshRate);
if (self.onStart) {
self.onStart();
}
} else {
self.img.src = "#";
clearInterval(self.frameTimer);
if (self.onStop) {
self.onStop();
}
}
}
self.start = function() { setRunning(true); }
self.stop = function() { setRunning(false); }
};
// class Player { ...
module.Player = function(canvas, url, options) {
var self = this;
if (typeof canvas === "string" || canvas instanceof String) {
canvas = document.getElementById(canvas);
}
var context = canvas.getContext("2d");
if (! options) {
options = {};
}
options.url = url;
options.onFrame = updateFrame;
options.onStart = function() { console.log("started"); }
options.onStop = function() { console.log("stopped"); }
self.stream = new module.Stream(options);
canvas.addEventListener("click", function() {
if (self.stream.running) { self.stop(); }
else { self.start(); }
}, false);
function scaleRect(srcSize, dstSize) {
var ratio = Math.min(dstSize.width / srcSize.width,
dstSize.height / srcSize.height);
var newRect = {
x: 0, y: 0,
width: srcSize.width * ratio,
height: srcSize.height * ratio
};
newRect.x = (dstSize.width/2) - (newRect.width/2);
newRect.y = (dstSize.height/2) - (newRect.height/2);
return newRect;
}
function updateFrame(img) {
var srcRect = {
x: 0, y: 0,
width: img.naturalWidth,
height: img.naturalHeight
};
var dstRect = scaleRect(srcRect, {
width: canvas.width,
height: canvas.height
});
try {
context.drawImage(img,
srcRect.x,
srcRect.y,
srcRect.width,
srcRect.height,
dstRect.x,
dstRect.y,
dstRect.width,
dstRect.height
);
console.log(".");
} catch (e) {
// if we can't draw, don't bother updating anymore
self.stop();
console.log("!");
throw e;
}
}
self.start = function() { self.stream.start(); }
self.stop = function() { self.stream.stop(); }
};
return module;
})(MJPEG || {});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Player</title>
</head>
<body>
<canvas id="player" style="background: #000;">
Your browser sucks.
</canvas>
</body>
<script src="mjpeg.js"></script>
<script>
var player = new MJPEG.Player("player", "http://localhost:8001");
player.start();
</script>
</html>
@mcbyte-it

This comment has been minimized.

Copy link

mcbyte-it commented Apr 20, 2016

How can I add authentication information for the stream? my IP cam uses basic http authentication, and entering the url as http://user:pass@ip/ doesn't seem to work.

Thanks

@rlamarche

This comment has been minimized.

Copy link

rlamarche commented Aug 6, 2016

thanks a lot ! 👍

@JerryGeis

This comment has been minimized.

Copy link

JerryGeis commented Aug 26, 2016

I found this and also need basic authentication. How do I do that? thanks,

@codebrainz

This comment has been minimized.

Copy link
Owner Author

codebrainz commented Aug 27, 2016

I'm don't know for sure, but a quick Google suggests you might need to use XMLHttpRequest to set the Authentication field in the request headers as discussed here, rather than just setting the image element's URL as shown in the Gist.

@vjppaz

This comment has been minimized.

Copy link

vjppaz commented Feb 8, 2017

it is not working on IE or Edge, can you tell us the reason why??

@ntfs1984

This comment has been minimized.

Copy link

ntfs1984 commented Feb 22, 2017

It also is not working with mobile browsers. Because this shit is just fat emulation of tag.
You may not use this plugin, but use simple <img src='http://localhost:8001'> instead.

@andviane

This comment has been minimized.

Copy link

andviane commented Jun 28, 2018

Direct embedding with img tag provides way less control. Here you can control the frame rate or even reuse the captured image that is valuable for advanced applications. This project is a valuable web resource.

@Kaiido

This comment has been minimized.

Copy link

Kaiido commented Sep 1, 2018

This project is relying on a browser bug. UAs are asked to only render the first frame of any animated image inside an <img> tag, and this includes MJPEG streams.

@mifritscher

This comment has been minimized.

Copy link

mifritscher commented Oct 12, 2018

@Kaaiido: Your source is only for canvas. But this JS uses an JS Image, and copies its Pictures Picture per Picture on the canvas. Btw, your sources says nothing about tags as well.

Sadly the browsers can't handle HD ready MJPEG streams without cooking on the CPU (while VLC can this flawlessly) - https://bugs.chromium.org/p/chromium/issues/detail?id=894753&can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.