Skip to content

Instantly share code, notes, and snippets.

@boushley
Created January 6, 2016 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boushley/ddfcca2f00e9406a3429 to your computer and use it in GitHub Desktop.
Save boushley/ddfcca2f00e9406a3429 to your computer and use it in GitHub Desktop.
Mock Video Element for use with dash.js -- Not quite complete but gets the basics functional
function createMockVideoElement() {
"use strict";
var video = {};
video.__mock_listeners = {};
video.__mock_progress = function(toProgress) {
var endProgressAt = video.currentTime + toProgress;
video.__mock_fire_event('play');
video.__mock_fire_event('playing');
runProgress();
function runProgress() {
if (video.currentTime > endProgressAt) {
video.paused = true;
return;
}
video.paused = false;
video.currentTime += 1;
video.__mock_fire_event('timeupdate');
video.__mock_fire_event('progress');
setTimeout(runProgress, 1000);
}
};
video.__mock_fire_event = function (event, arg) {
var listeners = video.__mock_listeners[event];
if (!listeners) {
return;
}
listeners.forEach(function(fn) {
fn.call(video, arg);
});
};
var blobRegex = /^blob:/;
Object.defineProperty(video, "src", {
set: function (newSrc) {
console.log('Setting src to: %s', newSrc);
video.__src = newSrc;
if (blobRegex.test(video.__src)) {
openMediaSource(video.__src);
}
},
get: function() {
return video.__src
}
});
function openMediaSource(url) {
var el = document.createElement('video');
el.src = url;
function processDurationChange() {
if (isNaN(el.duration)) {
return;
}
video.duration = el.duration;
video.__mock_fire_event('durationchange');
video.__mock_fire_event('timeupdate');
el.removeEventListener('durationchange', processDurationChange);
el.src = '';
el = null;
}
el.addEventListener('durationchange', processDurationChange);
}
video.textTracks = [];
video.controls = false;
video.seeking = false;
video.paused = false;
video.muted = false;
video.currentTime = 0;
video.playbackRate = 1;
video.readyState = 2;
video.volume = 2;
video.addEventListener = function(event, fn) {
console.log('Called: addEventListener event: %s', event);
video.__mock_listeners[event] = video.__mock_listeners[event] || [];
video.__mock_listeners[event].push(fn);
};
video.removeEventListener = function(event, fn) {
if (video.__mock_listeners[event]) {
var i = video.__mock_listeners[event].indexOf(fn);
if (i !== -1) {
video.__mock_listeners[event].splice(i, 1);
}
}
};
video.canPlayType = callLogger('canPlayType', true);
video.play = callLogger('play');
video.pause = callLogger('pause');
video.buffered = {
length: 1,
start: function(i) {
return i * 10;
},
end: function(i) {
return (i * 10) + 1;
}
};
video.played = {
length: 1,
start: function(i) {
return i * 10;
},
end: function(i) {
return (i * 10) + 1;
}
};
return video;
function callLogger(call, returnValue) {
return function() {
console.log('Called: %s, arguments: %0', call, arguments);
return returnValue;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment