Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Last active March 6, 2024 00:14
Show Gist options
  • Save codebrainz/eeeeead894e8bdff059b to your computer and use it in GitHub Desktop.
Save codebrainz/eeeeead894e8bdff059b to your computer and use it in GitHub Desktop.
MJPEG Player in JavaScript
Copyright 2015 Matthew Brush
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 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>
@ramesh2977
Copy link

how to set play() and pause() at run time in between video at runtime.

@asiby
Copy link

asiby commented Aug 11, 2020

This is already being done. If you click on the video, it will stop it. Click again to play it.

@tesla-srt
Copy link

Is there any documentation or can someone point me in the direction to passing basic auth credentials before streaming the data with this script?

@pir8radio
Copy link

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.

any chance you can give an example? or add the setting to your script? almost everyone who wants to use this script will be using it with a security camera, and 99% of security cameras all require basic auth to be sent with the image url.

@codebrainz
Copy link
Author

any chance you can give an example?

Not a proper example, no, as I don't have a camera handy to test with.

Some completely untested pseudo-code that might give some ideas could be like this:

async function fetchFrame(url, username, password) {
  const response = await fetch(url, {
    headers: new Headers({
      Authorization: "Basic " + btoa(username + ":" + password),
    }),
  });
  const data = await response.arrayBuffer();
  const b64str = btoa(String.fromCharCode(...new Uint8Array(data)));
  return "data:image/jpeg;base64," + b64str;
}

function frameUpdater(imgElement, url, username, password, updateRate) {
  return setInterval(async () => {
    imgElement.src = await fetchFrame(url, username, password);
  }, updateRate);
}

If something like that works, it might improve the CPU cooking issue mentioned by @mifritscher and the bandwidth issue mentioned by @geek-at, given a low enough updateRate, but it still looks quite inefficient.

And of course, as with any purely client-side solution, the login credentials would be visible to anyone who knows how to "view source" and/or open the browser's developer tools.

@gentilmente
Copy link

HI, it doesn't work for me, what could it be?
I use gphoto2 with a DSLR camera
with this command I can get a file: gphoto2 --capture-movie --stdout > liveview.mjpeg
or a live stream with: gphoto2 --capture-movie --stdout | ffmpeg -re -i pipe:0 -listen 1 -f mjpeg http://localhost:8080/feed.mjpg

and obvious change this line to: var player = new MJPEG.Player('player', './liveview.mjpeg'); in first case or
var player = new MJPEG.Player('player', 'http://localhost:8080/feed.mjpg');
but a black canvas displays, console log shows started and .

@daleffe
Copy link

daleffe commented Nov 4, 2023

For anyone who needs work with MJPEG or JPEG streams/images that are basic auth protected, check this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment