Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support For Simulator #56

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/StremioVideo/selectVideoImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var YouTubeVideo = require('../YouTubeVideo');
var withStreamingServer = require('../withStreamingServer');
var withHTMLSubtitles = require('../withHTMLSubtitles');
var withVideoParams = require('../withVideoParams');
var withFullVideoSupport = require('../withFullVideoSupport');

function selectVideoImplementation(commandArgs, options) {
if (!commandArgs.stream || typeof commandArgs.stream.externalUrl === 'string') {
Expand Down Expand Up @@ -37,6 +38,9 @@ function selectVideoImplementation(commandArgs, options) {
if (typeof global.webOS !== 'undefined') {
return withStreamingServer(withHTMLSubtitles(WebOsVideo));
}
if ((window.process || {}).__nwjs) {
return withStreamingServer(withFullVideoSupport(withHTMLSubtitles(HTMLVideo)));
}
return withStreamingServer(withHTMLSubtitles(HTMLVideo));
}

Expand All @@ -47,6 +51,9 @@ function selectVideoImplementation(commandArgs, options) {
if (typeof global.tizen !== 'undefined') {
return withVideoParams(withHTMLSubtitles(TizenVideo));
}
if ((window.process || {}).__nwjs) {
return withVideoParams(withFullVideoSupport(withHTMLSubtitles(HTMLVideo)));
}
return withVideoParams(withHTMLSubtitles(HTMLVideo));
}

Expand Down
3 changes: 3 additions & 0 deletions src/withFullVideoSupport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var withFullVideoSupport = require('./withFullVideoSupport');

module.exports = withFullVideoSupport;
145 changes: 145 additions & 0 deletions src/withFullVideoSupport/withFullVideoSupport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
var EventEmitter = require('eventemitter3');
var cloneDeep = require('lodash.clonedeep');
var deepFreeze = require('deep-freeze');

function withFullVideoSupport(Video) {
function VideoOverwriteSupported(options) {
options = options || {};

var video = new Video(options);
video.on('error', onVideoError);
video.on('propValue', onVideoPropEvent.bind(null, 'propValue'));
video.on('propChanged', onVideoPropEvent.bind(null, 'propChanged'));
Video.manifest.events
.filter(function(eventName) {
return !['error', 'propValue', 'propChanged'].includes(eventName);
})
.forEach(function(eventName) {
video.on(eventName, onOtherVideoEvent(eventName));
});
var containerElement = options.containerElement;
if (!(containerElement instanceof HTMLElement)) {
throw new Error('Container element required to be instance of HTMLElement');
}
var events = new EventEmitter();
var destroyed = false;

function onVideoError(error) {
events.emit('error', error);
if (error.critical) {
command('unload');
}
}
function onVideoPropEvent(eventName, propName, propValue) {
events.emit(eventName, propName, getProp(propName, propValue));
}
function onOtherVideoEvent(eventName) {
return function() {
events.emit.apply(events, [eventName].concat(Array.from(arguments)));
};
}
function getProp(propName, videoPropValue) {
switch (propName) {
default: {
return videoPropValue;
}
}
}
function observeProp(propName) {
switch (propName) {
default: {
return false;
}
}
}
function setProp(propName) {
switch (propName) {
default: {
return false;
}
}
}
function command(commandName) {
switch (commandName) {
case 'load': {
command('unload');
return false;
}
case 'unload': {
return false;
}
case 'destroy': {
command('unload');
destroyed = true;
video.dispatch({ type: 'command', commandName: 'destroy' });
events.removeAllListeners();
return true;
}
default: {
return false;
}
}
}

this.on = function(eventName, listener) {
if (destroyed) {
throw new Error('Video is destroyed');
}

events.on(eventName, listener);
};
this.dispatch = function(action) {
if (destroyed) {
throw new Error('Video is destroyed');
}

if (action) {
action = deepFreeze(cloneDeep(action));
switch (action.type) {
case 'observeProp': {
if (observeProp(action.propName)) {
return;
}

break;
}
case 'setProp': {
if (setProp(action.propName, action.propValue)) {
return;
}

break;
}
case 'command': {
if (command(action.commandName, action.commandArgs)) {
return;
}

break;
}
}
}

video.dispatch(action);
};
}

VideoOverwriteSupported.canPlayStream = function() {
return Promise.resolve(true);
};

VideoOverwriteSupported.manifest = {
name: Video.manifest.name + 'WithFullVideoSupport',
external: Video.manifest.external,
props: Video.manifest.props.concat([])
.filter(function(value, index, array) { return array.indexOf(value) === index; }),
commands: Video.manifest.commands.concat(['load', 'unload', 'destroy'])
.filter(function(value, index, array) { return array.indexOf(value) === index; }),
events: Video.manifest.events.concat(['propValue', 'propChanged', 'error'])
.filter(function(value, index, array) { return array.indexOf(value) === index; })
};

return VideoOverwriteSupported;
}

module.exports = withFullVideoSupport;
Loading