-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add middleware support. Middleware can function as go-between between the player and the tech. For example, it can modify the duration that the tech returns to the player. In addition, middleware allow for supporting custom video sources and types. Currently, middleware can only intercept timeline methods like duration, currentTime, and setCurrentTime. For example, ```js videojs.use('video/foo', { setSource(src, next) { next(null, { src: 'http://example.com/video.mp4', type: 'video/mp4' }); } }); ``` Will allow you to set a source with type `video/foo` which will play back `video.mp4`. This makes setting the source asynchronous, which aligns it with the spec a bit more. Methods like play can still be called synchronously on the player after setting the source and the player will play once the source has loaded. `sourceOrder` option was removed as well and it will now always use source ordering. BREAKING CHANGE: setting the source is now asynchronous. `sourceOrder` option removed and made the default.
- Loading branch information
Showing
11 changed files
with
721 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { assign } from '../utils/obj.js'; | ||
|
||
const middlewares = {}; | ||
|
||
export function use(type, middleware) { | ||
middlewares[type] = middlewares[type] || []; | ||
middlewares[type].push(middleware); | ||
} | ||
|
||
export function getMiddleware(type) { | ||
if (type) { | ||
return middlewares[type]; | ||
} | ||
|
||
return middlewares; | ||
} | ||
|
||
export function setSource(setTimeout, src, next) { | ||
setTimeout(() => setSourceHelper(src, middlewares[src.type], next), 1); | ||
} | ||
|
||
export function setTech(middleware, tech) { | ||
middleware.forEach((mw) => mw.setTech && mw.setTech(tech)); | ||
} | ||
|
||
export function get(middleware, tech, method) { | ||
return middleware.reduceRight(middlewareIterator(method), tech[method]()); | ||
} | ||
|
||
export function set(middleware, tech, method, arg) { | ||
return tech[method](middleware.reduce(middlewareIterator(method), arg)); | ||
} | ||
|
||
export const allowedGetters = { | ||
currentTime: 1, | ||
duration: 1 | ||
}; | ||
|
||
export const allowedSetters = { | ||
setCurrentTime: 1 | ||
}; | ||
|
||
function middlewareIterator(method) { | ||
return (value, mw) => { | ||
if (mw[method]) { | ||
return mw[method](value); | ||
} | ||
|
||
return value; | ||
}; | ||
} | ||
|
||
function setSourceHelper(src = {}, middleware = [], next, acc = []) { | ||
const [mw, ...mwrest] = middleware; | ||
|
||
// if mw is a string, then we're at a fork in the road | ||
if (typeof mw === 'string') { | ||
setSourceHelper(src, middlewares[mw], next, acc); | ||
|
||
// if we have an mw, call its setSource method | ||
} else if (mw) { | ||
mw.setSource(assign({}, src), function(err, _src) { | ||
|
||
// something happened, try the next middleware on the current level | ||
// make sure to use the old src | ||
if (err) { | ||
return setSourceHelper(src, mwrest, next, acc); | ||
} | ||
|
||
// we've succeeded, now we need to go deeper | ||
acc.push(mw); | ||
|
||
// if it's the same time, continue does the current chain | ||
// otherwise, we want to go down the new chain | ||
setSourceHelper(_src, | ||
src.type === _src.type ? mwrest : middlewares[_src.type], | ||
next, | ||
acc); | ||
}); | ||
} else if (mwrest.length) { | ||
setSourceHelper(src, mwrest, next, acc); | ||
} else { | ||
next(src, acc); | ||
} | ||
} |
Oops, something went wrong.