-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Stremio/add-video-params
Add Video Params Required For Subtitles Requests
- Loading branch information
Showing
7 changed files
with
230 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var url = require('url'); | ||
|
||
function fetchVideoParams(streamingServerURL, mediaURL) { | ||
var queryParams = new URLSearchParams([['videoUrl', mediaURL]]); | ||
return fetch(url.resolve(streamingServerURL, '/opensubHash?' + queryParams.toString())) | ||
.then(function(resp) { | ||
if (resp.ok) { | ||
return resp.json(); | ||
} | ||
|
||
throw new Error(resp.status + ' (' + resp.statusText + ')'); | ||
}) | ||
.then(function(resp) { | ||
if (resp.error) { | ||
throw new Error(resp.error); | ||
} | ||
|
||
return resp.result; | ||
}); | ||
} | ||
|
||
module.exports = fetchVideoParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var withVideoParams = require('./withVideoParams'); | ||
|
||
module.exports = withVideoParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
var EventEmitter = require('eventemitter3'); | ||
var cloneDeep = require('lodash.clonedeep'); | ||
var deepFreeze = require('deep-freeze'); | ||
|
||
function withVideoParams(Video) { | ||
function VideoWithVideoParams(options) { | ||
options = options || {}; | ||
|
||
var video = new Video(options); | ||
video.on('propValue', onVideoPropEvent.bind(null, 'propValue')); | ||
video.on('propChanged', onVideoPropEvent.bind(null, 'propChanged')); | ||
Video.manifest.events | ||
.filter(function(eventName) { | ||
return !['propValue', 'propChanged'].includes(eventName); | ||
}) | ||
.forEach(function(eventName) { | ||
video.on(eventName, onOtherVideoEvent(eventName)); | ||
}); | ||
|
||
var stream = null; | ||
var events = new EventEmitter(); | ||
var destroyed = false; | ||
var observedProps = { | ||
videoParams: false | ||
}; | ||
|
||
function onVideoPropEvent(eventName, propName, propValue) { | ||
if (propName !== 'videoParams') { | ||
events.emit(eventName, propName, getProp(propName, propValue)); | ||
} | ||
if (propName === 'stream') { | ||
stream = propValue; | ||
onPropChanged('videoParams'); | ||
} | ||
} | ||
function onOtherVideoEvent(eventName) { | ||
return function() { | ||
events.emit.apply(events, [eventName].concat(Array.from(arguments))); | ||
}; | ||
} | ||
function onPropChanged(propName) { | ||
if (observedProps[propName]) { | ||
events.emit('propChanged', propName, getProp(propName, null)); | ||
} | ||
} | ||
function getProp(propName, videoPropValue) { | ||
switch (propName) { | ||
case 'videoParams': { | ||
if (stream === null) { | ||
return null; | ||
} | ||
|
||
return { hash: null, size: null }; | ||
} | ||
default: { | ||
return videoPropValue; | ||
} | ||
} | ||
} | ||
function observeProp(propName) { | ||
switch (propName) { | ||
case 'videoParams': { | ||
events.emit('propValue', propName, getProp(propName, null)); | ||
observedProps[propName] = true; | ||
return true; | ||
} | ||
default: { | ||
return false; | ||
} | ||
} | ||
} | ||
function command(commandName) { | ||
switch (commandName) { | ||
case 'destroy': { | ||
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 'command': { | ||
if (command(action.commandName, action.commandArgs)) { | ||
return; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
video.dispatch(action); | ||
}; | ||
} | ||
|
||
VideoWithVideoParams.canPlayStream = function(stream, options) { | ||
return Video.canPlayStream(stream, options); | ||
}; | ||
|
||
VideoWithVideoParams.manifest = { | ||
name: Video.manifest.name + 'WithVideoParams', | ||
external: Video.manifest.external, | ||
props: Video.manifest.props.concat(['videoParams']) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }), | ||
commands: Video.manifest.commands.concat(['destroy']) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }), | ||
events: Video.manifest.events.concat(['propValue', 'propChanged']) | ||
.filter(function(value, index, array) { return array.indexOf(value) === index; }) | ||
}; | ||
|
||
return VideoWithVideoParams; | ||
} | ||
|
||
module.exports = withVideoParams; |