diff options
author | Sam Potts <sam@potts.es> | 2018-06-12 11:13:34 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-12 11:13:34 +1000 |
commit | 0b09b8ee6fc2212aa15d0d81999448eea62050ca (patch) | |
tree | 5d835cb0179850875450d72ba2c58edf27edbdff /src/js/html5.js | |
parent | cd14c3086d2a34548bb51570c368b6e080ecb944 (diff) | |
parent | db95b3234fd38e5dd71d00876c925514960e63fc (diff) | |
download | plyr-0b09b8ee6fc2212aa15d0d81999448eea62050ca.tar.lz plyr-0b09b8ee6fc2212aa15d0d81999448eea62050ca.tar.xz plyr-0b09b8ee6fc2212aa15d0d81999448eea62050ca.zip |
Merge pull request #1027 from friday/quality
Minor code improvements for quality switching
Diffstat (limited to 'src/js/html5.js')
-rw-r--r-- | src/js/html5.js | 73 |
1 files changed, 17 insertions, 56 deletions
diff --git a/src/js/html5.js b/src/js/html5.js index 63596cfc..fb2bc359 100644 --- a/src/js/html5.js +++ b/src/js/html5.js @@ -8,35 +8,21 @@ import utils from './utils'; const html5 = { getSources() { if (!this.isHTML5) { - return null; + return []; } - return this.media.querySelectorAll('source'); + const sources = Array.from(this.media.querySelectorAll('source')); + + // Filter out unsupported sources + return sources.filter(source => support.mime.call(this, source.getAttribute('type'))); }, // Get quality levels getQualityOptions() { - if (!this.isHTML5) { - return null; - } - - // Get sources - const sources = html5.getSources.call(this); - - if (utils.is.empty(sources)) { - return null; - } - - // Get <source> with size attribute - const sizes = Array.from(sources).filter(source => !utils.is.empty(source.getAttribute('size'))); - - // If none, bail - if (utils.is.empty(sizes)) { - return null; - } - - // Reduce to unique list - return utils.dedupe(sizes.map(source => Number(source.getAttribute('size')))); + // Get sizes from <source> elements + return html5.getSources.call(this) + .map(source => Number(source.getAttribute('size'))) + .filter(Boolean); }, extend() { @@ -51,53 +37,28 @@ const html5 = { get() { // Get sources const sources = html5.getSources.call(player); + const [source] = sources.filter(source => source.getAttribute('src') === player.source); - if (utils.is.empty(sources)) { - return null; - } - - const matches = Array.from(sources).filter(source => source.getAttribute('src') === player.source); - - if (utils.is.empty(matches)) { - return null; - } - - return Number(matches[0].getAttribute('size')); + // Return size, if match is found + return source && Number(source.getAttribute('size')); }, set(input) { // Get sources const sources = html5.getSources.call(player); - if (utils.is.empty(sources)) { - return; - } + // Get first match for requested size + const source = sources.find(source => Number(source.getAttribute('size')) === input); - // Get matches for requested size - const matches = Array.from(sources).filter(source => Number(source.getAttribute('size')) === input); - - // No matches for requested size - if (utils.is.empty(matches)) { + // No matching source found + if (!source) { return; } - // Get supported sources - const supported = matches.filter(source => support.mime.call(player, source.getAttribute('type'))); - - // No supported sources - if (utils.is.empty(supported)) { - return; - } - - // Trigger change event - utils.dispatchEvent.call(player, player.media, 'qualityrequested', false, { - quality: input, - }); - // Get current state const { currentTime, playing } = player; // Set new source - player.media.src = supported[0].getAttribute('src'); + player.media.src = source.getAttribute('src'); // Restore time const onLoadedMetaData = () => { |