UNPKG

videojs-contrib-hls

Version:

Play back HLS with video.js, even where it's not natively supported

1,548 lines (1,228 loc) 108 kB
/* eslint-disable max-len */ import document from 'global/document'; import videojs from 'video.js'; import Events from 'video.js'; import QUnit from 'qunit'; import testDataManifests from './test-manifests.js'; import { useFakeEnvironment, useFakeMediaSource, createPlayer, openMediaSource, standardXHRResponse, absoluteUrl } from './test-helpers.js'; /* eslint-disable no-unused-vars */ // we need this so that it can register hls with videojs import {HlsSourceHandler, HlsHandler, Hls} from '../src/videojs-contrib-hls'; import window from 'global/window'; // we need this so the plugin registers itself import 'videojs-contrib-quality-levels'; /* eslint-enable no-unused-vars */ const Flash = videojs.getTech('Flash'); const ogHlsHandlerSetupQualityLevels = videojs.HlsHandler.prototype.setupQualityLevels_; let nextId = 0; // do a shallow copy of the properties of source onto the target object const merge = function(target, source) { let name; for (name in source) { target[name] = source[name]; } }; QUnit.module('HLS', { beforeEach(assert) { this.env = useFakeEnvironment(assert); this.requests = this.env.requests; this.mse = useFakeMediaSource(); this.clock = this.env.clock; this.old = {}; // mock out Flash features for phantomjs this.old.Flash = videojs.mergeOptions({}, Flash); /* eslint-disable camelcase */ Flash.embed = function(swf, flashVars) { let el = document.createElement('div'); el.id = 'vjs_mock_flash_' + nextId++; el.className = 'vjs-tech vjs-mock-flash'; el.duration = Infinity; el.vjs_load = function() {}; el.vjs_getProperty = function(attr) { if (attr === 'buffered') { return [[0, 0]]; } return el[attr]; }; el.vjs_setProperty = function(attr, value) { el[attr] = value; }; el.vjs_src = function() {}; el.vjs_play = function() {}; el.vjs_discontinuity = function() {}; if (flashVars.autoplay) { el.autoplay = true; } if (flashVars.preload) { el.preload = flashVars.preload; } el.currentTime = 0; return el; }; /* eslint-enable camelcase */ this.old.FlashSupported = Flash.isSupported; Flash.isSupported = function() { return true; }; // store functionality that some tests need to mock this.old.GlobalOptions = videojs.mergeOptions(videojs.options); // force the HLS tech to run this.old.NativeHlsSupport = videojs.Hls.supportsNativeHls; videojs.Hls.supportsNativeHls = false; this.old.Decrypt = videojs.Hls.Decrypter; videojs.Hls.Decrypter = function() {}; // save and restore browser detection for the Firefox-specific tests this.old.browser = videojs.browser; videojs.browser = videojs.mergeOptions({}, videojs.browser); this.standardXHRResponse = (request, data) => { standardXHRResponse(request, data); // Because SegmentLoader#fillBuffer_ is now scheduled asynchronously // we have to use clock.tick to get the expected side effects of // SegmentLoader#handleUpdateEnd_ this.clock.tick(1); }; // setup a player this.player = createPlayer(); this.clock.tick(1); }, afterEach() { this.env.restore(); this.mse.restore(); merge(videojs.options, this.old.GlobalOptions); Flash.isSupported = this.old.FlashSupported; merge(Flash, this.old.Flash); videojs.Hls.supportsNativeHls = this.old.NativeHlsSupport; videojs.Hls.Decrypter = this.old.Decrypt; videojs.browser = this.old.browser; this.player.dispose(); } }); QUnit.test('deprecation warning is show when using player.hls', function(assert) { let oldWarn = videojs.log.warn; let warning = ''; let hlsPlayerAccessEvents = 0; this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.on('usage', (event) => { if (event.name === 'hls-player-access') { hlsPlayerAccessEvents++; } }); videojs.log.warn = (text) => { warning = text; }; assert.equal(hlsPlayerAccessEvents, 0, 'no hls-player-access event was fired'); let hls = this.player.hls; assert.equal(hlsPlayerAccessEvents, 1, 'an hls-player-access event was fired'); assert.equal(warning, 'player.hls is deprecated. Use player.tech_.hls instead.', 'warning would have been shown'); assert.ok(hls, 'an instance of hls is returned by player.hls'); videojs.log.warn = oldWarn; }); QUnit.test('starts playing if autoplay is specified', function(assert) { this.player.autoplay(true); this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); // make sure play() is called *after* the media source opens openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); assert.ok(!this.player.paused(), 'not paused'); }); QUnit.test('stats are reset on each new source', function(assert) { this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); // make sure play() is called *after* the media source opens openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests.shift()); this.standardXHRResponse(this.requests.shift()); assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 1024, 'stat is set'); this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 0, 'stat is reset'); }); QUnit.test('XHR requests first byte range on play', function(assert) { this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.triggerReady(); this.clock.tick(1); this.player.tech_.trigger('play'); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); assert.equal(this.requests[1].headers.Range, 'bytes=0-522827'); }); QUnit.test('Seeking requests correct byte range', function(assert) { this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.trigger('play'); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); this.clock.tick(1); this.player.currentTime(41); this.clock.tick(2); assert.equal(this.requests[2].headers.Range, 'bytes=2299992-2835603'); }); QUnit.test('autoplay seeks to the live point after playlist load', function(assert) { let currentTime = 0; this.player.autoplay(true); this.player.on('seeking', () => { currentTime = this.player.currentTime(); }); this.player.src({ src: 'liveStart30sBefore.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.trigger('play'); this.standardXHRResponse(this.requests.shift()); this.clock.tick(1); assert.notEqual(currentTime, 0, 'seeked on autoplay'); }); QUnit.test('autoplay seeks to the live point after media source open', function(assert) { let currentTime = 0; this.player.autoplay(true); this.player.on('seeking', () => { currentTime = this.player.currentTime(); }); this.player.src({ src: 'liveStart30sBefore.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.triggerReady(); this.clock.tick(1); this.standardXHRResponse(this.requests.shift()); openMediaSource(this.player, this.clock); this.player.tech_.trigger('play'); this.clock.tick(1); assert.notEqual(currentTime, 0, 'seeked on autoplay'); }); QUnit.test('autoplay seeks to the live point after tech fires loadedmetadata in ie11', function(assert) { videojs.browser.IE_VERSION = 11; let currentTime = 0; this.player.autoplay(true); this.player.on('seeking', () => { currentTime = this.player.currentTime(); }); this.player.src({ src: 'liveStart30sBefore.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.trigger('play'); this.standardXHRResponse(this.requests.shift()); this.clock.tick(1); assert.equal(currentTime, 0, 'have not played yet'); this.player.tech_.trigger('loadedmetadata'); this.clock.tick(1); assert.notEqual(currentTime, 0, 'seeked after tech is ready'); }); QUnit.test('duration is set when the source opens after the playlist is loaded', function(assert) { this.player.src({ src: 'media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.triggerReady(); this.clock.tick(1); this.standardXHRResponse(this.requests.shift()); openMediaSource(this.player, this.clock); assert.equal(this.player.tech_.hls.mediaSource.duration, 40, 'set the duration'); }); QUnit.test('codecs are passed to the source buffer', function(assert) { let codecs = []; this.player.src({ src: 'custom-codecs.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); let addSourceBuffer = this.player.tech_.hls.mediaSource.addSourceBuffer; this.player.tech_.hls.mediaSource.addSourceBuffer = function(codec) { codecs.push(codec); return addSourceBuffer.call(this, codec); }; this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:CODECS="avc1.dd00dd, mp4a.40.f"\n' + 'media.m3u8\n'); this.standardXHRResponse(this.requests.shift()); assert.equal(codecs.length, 1, 'created a source buffer'); assert.equal(codecs[0], 'video/mp2t; codecs="avc1.dd00dd, mp4a.40.f"', 'specified the codecs'); }); QUnit.test('including HLS as a tech does not error', function(assert) { let player = createPlayer({ techOrder: ['hls', 'html5'] }); this.clock.tick(1); assert.ok(player, 'created the player'); assert.equal(this.env.log.warn.calls, 2, 'logged two warnings for deprecations'); }); QUnit.test('creates a PlaylistLoader on init', function(assert) { this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.src({ src: 'manifest/playlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); assert.equal(this.requests[0].aborted, true, 'aborted previous src'); this.standardXHRResponse(this.requests[1]); assert.ok(this.player.tech_.hls.playlists.master, 'set the master playlist'); assert.ok(this.player.tech_.hls.playlists.media(), 'set the media playlist'); assert.ok(this.player.tech_.hls.playlists.media().segments, 'the segment entries are parsed'); assert.strictEqual(this.player.tech_.hls.playlists.master.playlists[0], this.player.tech_.hls.playlists.media(), 'the playlist is selected'); }); QUnit.test('sets the duration if one is available on the playlist', function(assert) { let events = 0; this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.on('durationchange', function() { events++; }); this.standardXHRResponse(this.requests[0]); assert.equal(this.player.tech_.hls.mediaSource.duration, 40, 'set the duration'); assert.equal(events, 1, 'durationchange is fired'); }); QUnit.test('estimates individual segment durations if needed', function(assert) { let changes = 0; this.player.src({ src: 'http://example.com/manifest/missingExtinf.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.mediaSource.duration = NaN; this.player.tech_.on('durationchange', function() { changes++; }); this.standardXHRResponse(this.requests[0]); assert.strictEqual(this.player.tech_.hls.mediaSource.duration, this.player.tech_.hls.playlists.media().segments.length * 10, 'duration is updated'); assert.strictEqual(changes, 1, 'one durationchange fired'); }); QUnit.test('translates seekable by the starting time for live playlists', function(assert) { let seekable; this.player.src({ src: 'media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-MEDIA-SEQUENCE:15\n' + '#EXT-X-TARGETDURATION:10\n' + '#EXTINF:10,\n' + '0.ts\n' + '#EXTINF:10,\n' + '1.ts\n' + '#EXTINF:10,\n' + '2.ts\n' + '#EXTINF:10,\n' + '3.ts\n'); seekable = this.player.seekable(); assert.equal(seekable.length, 1, 'one seekable range'); assert.equal(seekable.start(0), 0, 'the earliest possible position is at zero'); assert.equal(seekable.end(0), 10, 'end is relative to the start'); }); QUnit.test('starts downloading a segment on loadedmetadata', function(assert) { this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.buffered = function() { return videojs.createTimeRange(0, 0); }; openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); this.standardXHRResponse(this.requests[1]); assert.strictEqual(this.requests[1].url, absoluteUrl('manifest/media-00001.ts'), 'the first segment is requested'); // verify stats assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 1024, '1024 bytes'); assert.equal(this.player.tech_.hls.stats.mediaRequests, 1, '1 request'); }); QUnit.test('re-initializes the handler for each source', function(assert) { let firstPlaylists; let secondPlaylists; let firstMSE; let secondMSE; let aborts = 0; let masterPlaylistController; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); firstPlaylists = this.player.tech_.hls.playlists; firstMSE = this.player.tech_.hls.mediaSource; this.standardXHRResponse(this.requests.shift()); this.standardXHRResponse(this.requests.shift()); masterPlaylistController = this.player.tech_.hls.masterPlaylistController_; masterPlaylistController.mainSegmentLoader_.sourceUpdater_.sourceBuffer_.abort = () => { aborts++; }; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); secondPlaylists = this.player.tech_.hls.playlists; secondMSE = this.player.tech_.hls.mediaSource; assert.equal(1, aborts, 'aborted the old source buffer'); assert.ok(this.requests[0].aborted, 'aborted the old segment request'); assert.notStrictEqual(firstPlaylists, secondPlaylists, 'the playlist object is not reused'); assert.notStrictEqual(firstMSE, secondMSE, 'the media source object is not reused'); }); QUnit.test('triggers a media source error when an initial playlist request errors', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.requests.pop().respond(500); assert.equal(this.player.tech_.hls.mediaSource.error_, 'network', 'a network error is triggered'); }); QUnit.test( 'triggers a player error when an initial playlist request errors and the media source ' + 'isn\'t open', function(assert) { const done = assert.async(); const origError = videojs.log.error; const errLogs = []; const endOfStreams = []; videojs.log.error = (log) => errLogs.push(log); this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); this.player.tech_.hls.masterPlaylistController_.mediaSource.endOfStream = (type) => { endOfStreams.push(type); throw new Error(); }; this.player.on('error', () => { const error = this.player.error(); assert.equal(endOfStreams.length, 1, 'one endOfStream called'); assert.equal(endOfStreams[0], 'network', 'endOfStream called with network'); assert.equal(error.code, 2, 'error has correct code'); assert.equal(error.message, 'HLS playlist request error at URL: manifest/master.m3u8', 'error has correct message'); assert.equal(errLogs.length, 1, 'logged an error'); videojs.log.error = origError; assert.notOk(this.player.tech_.hls.mediaSource.error_, 'no media source error'); done(); }); this.requests.pop().respond(500); }); QUnit.test('downloads media playlists after loading the master', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 20e10; this.standardXHRResponse(this.requests[0]); this.standardXHRResponse(this.requests[1]); this.standardXHRResponse(this.requests[2]); assert.strictEqual(this.requests[0].url, 'manifest/master.m3u8', 'master playlist requested'); assert.strictEqual(this.requests[1].url, absoluteUrl('manifest/media2.m3u8'), 'media playlist requested'); assert.strictEqual(this.requests[2].url, absoluteUrl('manifest/media2-00001.ts'), 'first segment requested'); // verify stats assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 1024, '1024 bytes'); assert.equal(this.player.tech_.hls.stats.mediaRequests, 1, '1 request'); }); QUnit.test('setting bandwidth resets throughput', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.hls.throughput = 1000; assert.strictEqual(this.player.tech_.hls.throughput, 1000, 'throughput is set'); this.player.tech_.hls.bandwidth = 20e10; assert.strictEqual(this.player.tech_.hls.throughput, 0, 'throughput is reset when bandwidth is specified'); }); QUnit.test('a thoughput of zero is ignored in systemBandwidth', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.hls.bandwidth = 20e10; assert.strictEqual(this.player.tech_.hls.throughput, 0, 'throughput is reset when bandwidth is specified'); assert.strictEqual(this.player.tech_.hls.systemBandwidth, 20e10, 'systemBandwidth is the same as bandwidth'); }); QUnit.test('systemBandwidth is a combination of thoughput and bandwidth', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); this.player.tech_.hls.bandwidth = 20e10; this.player.tech_.hls.throughput = 20e10; // 1 / ( 1 / 20e10 + 1 / 20e10) = 10e10 assert.strictEqual(this.player.tech_.hls.systemBandwidth, 10e10, 'systemBandwidth is the combination of bandwidth and throughput'); }); QUnit.test('upshifts if the initial bandwidth hint is high', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 10e20; this.standardXHRResponse(this.requests[0]); this.standardXHRResponse(this.requests[1]); this.standardXHRResponse(this.requests[2]); assert.strictEqual( this.requests[0].url, 'manifest/master.m3u8', 'master playlist requested' ); assert.strictEqual( this.requests[1].url, absoluteUrl('manifest/media2.m3u8'), 'media playlist requested' ); assert.strictEqual( this.requests[2].url, absoluteUrl('manifest/media2-00001.ts'), 'first segment requested' ); // verify stats assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 1024, '1024 bytes'); assert.equal(this.player.tech_.hls.stats.mediaRequests, 1, '1 request'); }); QUnit.test('downshifts if the initial bandwidth hint is low', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 100; this.standardXHRResponse(this.requests[0]); this.standardXHRResponse(this.requests[1]); this.standardXHRResponse(this.requests[2]); assert.strictEqual(this.requests[0].url, 'manifest/master.m3u8', 'master playlist requested'); assert.strictEqual(this.requests[1].url, absoluteUrl('manifest/media1.m3u8'), 'media playlist requested'); assert.strictEqual(this.requests[2].url, absoluteUrl('manifest/media1-00001.ts'), 'first segment requested'); // verify stats assert.equal(this.player.tech_.hls.stats.mediaBytesTransferred, 1024, '1024 bytes'); assert.equal(this.player.tech_.hls.stats.mediaRequests, 1, '1 request'); }); QUnit.test('buffer checks are noops until a media playlist is ready', function(assert) { this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.clock.tick(10 * 1000); assert.strictEqual(1, this.requests.length, 'one request was made'); assert.strictEqual(this.requests[0].url, 'manifest/media.m3u8', 'media playlist requested'); }); QUnit.test('buffer checks are noops when only the master is ready', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); // master this.standardXHRResponse(this.requests.shift()); // media this.standardXHRResponse(this.requests.shift()); // ignore any outstanding segment requests this.requests.length = 0; // load in a new playlist which will cause playlists.media() to be // undefined while it is being fetched this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); // respond with the master playlist but don't send the media playlist yet // force media1 to be requested this.player.tech_.hls.bandwidth = 1; // master this.standardXHRResponse(this.requests.shift()); this.clock.tick(10 * 1000); assert.strictEqual(this.requests.length, 1, 'one request was made'); assert.strictEqual(this.requests[0].url, absoluteUrl('manifest/media1.m3u8'), 'media playlist requested'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1, 'bandwidth set above'); }); QUnit.test('selects a playlist below the current bandwidth', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); // the default playlist has a really high bitrate this.player.tech_.hls.playlists.master.playlists[0].attributes.BANDWIDTH = 9e10; // playlist 1 has a very low bitrate this.player.tech_.hls.playlists.master.playlists[1].attributes.BANDWIDTH = 1; // but the detected client bandwidth is really low this.player.tech_.hls.bandwidth = 10; playlist = this.player.tech_.hls.selectPlaylist(); assert.strictEqual(playlist, this.player.tech_.hls.playlists.master.playlists[1], 'the low bitrate stream is selected'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 10, 'bandwidth set above'); }); QUnit.test('selects a primary rendtion when there are multiple rendtions share same attributes', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); standardXHRResponse(this.requests[0]); // covers playlists with same bandwidth but different resolution and different bandwidth but same resolution this.player.tech_.hls.playlists.master.playlists[0].attributes.BANDWIDTH = 528; this.player.tech_.hls.playlists.master.playlists[1].attributes.BANDWIDTH = 528; this.player.tech_.hls.playlists.master.playlists[2].attributes.BANDWIDTH = 728; this.player.tech_.hls.playlists.master.playlists[3].attributes.BANDWIDTH = 728; this.player.tech_.hls.bandwidth = 1000; playlist = this.player.tech_.hls.selectPlaylist(); assert.strictEqual(playlist, this.player.tech_.hls.playlists.master.playlists[2], 'select the rendition with largest bandwidth and just-larger-than video player'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1000, 'bandwidth set above'); // covers playlists share same bandwidth and resolutions this.player.tech_.hls.playlists.master.playlists[0].attributes.BANDWIDTH = 728; this.player.tech_.hls.playlists.master.playlists[0].attributes.RESOLUTION.width = 960; this.player.tech_.hls.playlists.master.playlists[0].attributes.RESOLUTION.height = 540; this.player.tech_.hls.playlists.master.playlists[1].attributes.BANDWIDTH = 728; this.player.tech_.hls.playlists.master.playlists[2].attributes.BANDWIDTH = 728; this.player.tech_.hls.playlists.master.playlists[2].attributes.RESOLUTION.width = 960; this.player.tech_.hls.playlists.master.playlists[2].attributes.RESOLUTION.height = 540; this.player.tech_.hls.playlists.master.playlists[3].attributes.BANDWIDTH = 728; this.player.tech_.hls.bandwidth = 1000; playlist = this.player.tech_.hls.selectPlaylist(); assert.strictEqual(playlist, this.player.tech_.hls.playlists.master.playlists[0], 'the primary rendition is selected'); }); QUnit.test('allows initial bandwidth to be provided', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 500; this.requests[0].bandwidth = 1; this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-PLAYLIST-TYPE:VOD\n' + '#EXT-X-TARGETDURATION:10\n'); assert.equal(this.player.tech_.hls.bandwidth, 500, 'prefers user-specified initial bandwidth'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 500, 'bandwidth set above'); }); QUnit.test('raises the minimum bitrate for a stream proportionially', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); // the default playlist's bandwidth + 10% is assert.equal to the current bandwidth this.player.tech_.hls.playlists.master.playlists[0].attributes.BANDWIDTH = 10; this.player.tech_.hls.bandwidth = 11; // 9.9 * 1.1 < 11 this.player.tech_.hls.playlists.master.playlists[1].attributes.BANDWIDTH = 9.9; playlist = this.player.tech_.hls.selectPlaylist(); assert.strictEqual(playlist, this.player.tech_.hls.playlists.master.playlists[1], 'a lower bitrate stream is selected'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 11, 'bandwidth set above'); }); QUnit.test('uses the lowest bitrate if no other is suitable', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); // the lowest bitrate playlist is much greater than 1b/s this.player.tech_.hls.bandwidth = 1; playlist = this.player.tech_.hls.selectPlaylist(); // playlist 1 has the lowest advertised bitrate assert.strictEqual(playlist, this.player.tech_.hls.playlists.master.playlists[1], 'the lowest bitrate stream is selected'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1, 'bandwidth set above'); }); QUnit.test('selects the correct rendition by tech dimensions', function(assert) { let playlist; let hls; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); hls = this.player.tech_.hls; this.player.width(640); this.player.height(360); hls.bandwidth = 3000000; playlist = hls.selectPlaylist(); assert.deepEqual(playlist.attributes.RESOLUTION, {width: 960, height: 540}, 'should return the correct resolution by tech dimensions'); assert.equal(playlist.attributes.BANDWIDTH, 1928000, 'should have the expected bandwidth in case of multiple'); this.player.width(1920); this.player.height(1080); hls.bandwidth = 3000000; playlist = hls.selectPlaylist(); assert.deepEqual(playlist.attributes.RESOLUTION, {width: 960, height: 540}, 'should return the correct resolution by tech dimensions'); assert.equal(playlist.attributes.BANDWIDTH, 1928000, 'should have the expected bandwidth in case of multiple'); this.player.width(396); this.player.height(224); playlist = hls.selectPlaylist(); assert.deepEqual(playlist.attributes.RESOLUTION, {width: 396, height: 224}, 'should return the correct resolution by ' + 'tech dimensions, if exact match'); assert.equal(playlist.attributes.BANDWIDTH, 440000, 'should have the expected bandwidth in case of multiple, if exact match'); this.player.width(395); this.player.height(222); playlist = this.player.tech_.hls.selectPlaylist(); assert.deepEqual(playlist.attributes.RESOLUTION, {width: 396, height: 224}, 'should return the next larger resolution by tech dimensions, ' + 'if no exact match exists'); assert.equal(playlist.attributes.BANDWIDTH, 440000, 'should have the expected bandwidth in case of multiple, if exact match'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 3000000, 'bandwidth set above'); }); QUnit.test('selects the highest bitrate playlist when the player dimensions are ' + 'larger than any of the variants', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); // master this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1000,RESOLUTION=2x1\n' + 'media.m3u8\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1,RESOLUTION=1x1\n' + 'media1.m3u8\n'); // media this.standardXHRResponse(this.requests.shift()); this.player.tech_.hls.bandwidth = 1e10; this.player.width(1024); this.player.height(768); playlist = this.player.tech_.hls.selectPlaylist(); assert.equal(playlist.attributes.BANDWIDTH, 1000, 'selected the highest bandwidth variant'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1e10, 'bandwidth set above'); }); QUnit.test('filters playlists that are currently excluded', function(assert) { let playlist; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 1e10; // master this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1000\n' + 'media.m3u8\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1\n' + 'media1.m3u8\n'); // media this.standardXHRResponse(this.requests.shift()); // exclude the current playlist this.player.tech_.hls.playlists.master.playlists[0].excludeUntil = +new Date() + 1000; playlist = this.player.tech_.hls.selectPlaylist(); assert.equal(playlist, this.player.tech_.hls.playlists.master.playlists[1], 'respected exclusions'); // timeout the exclusion this.clock.tick(1000); playlist = this.player.tech_.hls.selectPlaylist(); assert.equal(playlist, this.player.tech_.hls.playlists.master.playlists[0], 'expired the exclusion'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1e10, 'bandwidth set above'); }); QUnit.test('does not blacklist compatible H.264 codec strings', function(assert) { let master; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 1; // master this.requests.shift() .respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1,CODECS="avc1.4d400d,mp4a.40.5"\n' + 'media.m3u8\n' + '#EXT-X-STREAM-INF:BANDWIDTH=10,CODECS="avc1.4d400f,mp4a.40.5"\n' + 'media1.m3u8\n'); // media this.standardXHRResponse(this.requests.shift()); master = this.player.tech_.hls.playlists.master; assert.strictEqual(typeof master.playlists[0].excludeUntil, 'undefined', 'did not blacklist'); assert.strictEqual(typeof master.playlists[1].excludeUntil, 'undefined', 'did not blacklist'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1, 'bandwidth set above'); }); QUnit.test('does not blacklist compatible AAC codec strings', function(assert) { let master; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 1; // master this.requests.shift() .respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1,CODECS="avc1.4d400d,mp4a.40.2"\n' + 'media.m3u8\n' + '#EXT-X-STREAM-INF:BANDWIDTH=10,CODECS="avc1.4d400d,not-an-audio-codec"\n' + 'media1.m3u8\n'); // media this.standardXHRResponse(this.requests.shift()); master = this.player.tech_.hls.playlists.master; assert.strictEqual(typeof master.playlists[0].excludeUntil, 'undefined', 'did not blacklist mp4a.40.2'); assert.strictEqual(master.playlists[1].excludeUntil, Infinity, 'blacklisted invalid audio codec'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1, 'bandwidth set above'); }); QUnit.test('cancels outstanding XHRs when seeking', function(assert) { this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); this.player.tech_.hls.media = { segments: [{ uri: '0.ts', duration: 10 }, { uri: '1.ts', duration: 10 }] }; // attempt to seek while the download is in progress this.player.currentTime(7); this.clock.tick(2); assert.ok(this.requests[1].aborted, 'XHR aborted'); assert.strictEqual(this.requests.length, 3, 'opened new XHR'); }); QUnit.test('does not abort segment loading for in-buffer seeking', function(assert) { this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests.shift()); this.player.tech_.buffered = function() { return videojs.createTimeRange(0, 20); }; this.player.tech_.setCurrentTime(11); this.clock.tick(1); assert.equal(this.requests.length, 1, 'did not abort the outstanding request'); }); QUnit.test('segment 404 should trigger blacklisting of media', function(assert) { let media; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.hls.bandwidth = 20000; // master this.standardXHRResponse(this.requests[0]); // media this.standardXHRResponse(this.requests[1]); media = this.player.tech_.hls.playlists.media_; // segment this.requests[2].respond(400); assert.ok(media.excludeUntil > 0, 'original media blacklisted for some time'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 20000, 'bandwidth set above'); }); QUnit.test('playlist 404 should blacklist media', function(assert) { let media; let url; let blacklistplaylist = 0; let retryplaylist = 0; let hlsRenditionBlacklistedEvents = 0; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.player.tech_.on('blacklistplaylist', () => blacklistplaylist++); this.player.tech_.on('retryplaylist', () => retryplaylist++); this.player.tech_.on('usage', (event) => { if (event.name === 'hls-rendition-blacklisted') { hlsRenditionBlacklistedEvents++; } }); this.player.tech_.hls.bandwidth = 1e10; // master this.requests[0].respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1000\n' + 'media.m3u8\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1\n' + 'media1.m3u8\n'); assert.equal(typeof this.player.tech_.hls.playlists.media_, 'undefined', 'no media is initially set'); assert.equal(blacklistplaylist, 0, 'there is no blacklisted playlist'); assert.equal(hlsRenditionBlacklistedEvents, 0, 'no hls-rendition-blacklisted event was fired'); // media this.requests[1].respond(404); url = this.requests[1].url.slice(this.requests[1].url.lastIndexOf('/') + 1); media = this.player.tech_.hls.playlists.master.playlists[url]; assert.ok(media.excludeUntil > 0, 'original media blacklisted for some time'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); assert.equal(this.env.log.warn.args[0], 'Problem encountered with the current HLS playlist. HLS playlist request error at URL: media.m3u8 Switching to another playlist.', 'log generic error message'); assert.equal(blacklistplaylist, 1, 'there is one blacklisted playlist'); assert.equal(hlsRenditionBlacklistedEvents, 1, 'an hls-rendition-blacklisted event was fired'); assert.equal(retryplaylist, 0, 'haven\'t retried any playlist'); // request for the final available media this.requests[2].respond(404); url = this.requests[2].url.slice(this.requests[2].url.lastIndexOf('/') + 1); media = this.player.tech_.hls.playlists.master.playlists[url]; // media wasn't blacklisted because it's final rendition assert.ok(!media.excludeUntil, 'media not blacklisted after playlist 404'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); assert.equal(this.env.log.warn.args[1], 'Problem encountered with the current HLS playlist. Trying again since it is the final playlist.', 'log specific error message for final playlist'); assert.equal(retryplaylist, 1, 'retried final playlist for once'); assert.equal(blacklistplaylist, 1, 'there is one blacklisted playlist'); assert.equal(hlsRenditionBlacklistedEvents, 1, 'an hls-rendition-blacklisted event was fired'); this.clock.tick(2 * 1000); // no new request was made since it hasn't been half the segment duration assert.strictEqual(3, this.requests.length, 'no new request was made'); this.clock.tick(3 * 1000); // continue loading the final remaining playlist after it wasn't blacklisted // when half the segment duaration passed assert.strictEqual(4, this.requests.length, 'one more request was made'); assert.strictEqual(this.requests[3].url, absoluteUrl('manifest/media1.m3u8'), 'media playlist requested'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 1e10, 'bandwidth set above'); }); QUnit.test('blacklists playlist if it has stopped being updated', function(assert) { let playliststuck = 0; this.player.src({ src: 'master.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); this.player.tech_.triggerReady(); this.standardXHRResponse(this.requests.shift()); this.player.tech_.hls.masterPlaylistController_.seekable = function() { return videojs.createTimeRange(90, 130); }; this.player.tech_.setCurrentTime(170); this.player.tech_.buffered = function() { return videojs.createTimeRange(0, 170); }; Hls.Playlist.playlistEnd = function() { return 170; }; this.player.tech_.on('playliststuck', () => playliststuck++); this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-MEDIA-SEQUENCE:16\n' + '#EXTINF:10,\n' + '16.ts\n'); assert.ok(!this.player.tech_.hls.playlists.media().excludeUntil, 'playlist was not blacklisted'); assert.equal(this.env.log.warn.calls, 0, 'no warning logged for blacklist'); assert.equal(playliststuck, 0, 'there is no stuck playlist'); this.player.tech_.trigger('play'); this.player.tech_.trigger('playing'); // trigger a refresh this.clock.tick(10 * 1000); this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXT-X-MEDIA-SEQUENCE:16\n' + '#EXTINF:10,\n' + '16.ts\n'); assert.ok(this.player.tech_.hls.playlists.media().excludeUntil > 0, 'playlist blacklisted for some time'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); assert.equal(this.env.log.warn.args[0], 'Problem encountered with the current HLS playlist. Playlist no longer updating. Switching to another playlist.', 'log specific error message for not updated playlist'); assert.equal(playliststuck, 1, 'there is one stuck playlist'); }); QUnit.test('never blacklist the playlist if it is the only playlist', function(assert) { let media; this.player.src({ src: 'manifest/media.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); this.requests.shift().respond(200, null, '#EXTM3U\n' + '#EXTINF:10,\n' + '0.ts\n'); this.clock.tick(10 * 1000); this.requests.shift().respond(404); media = this.player.tech_.hls.playlists.media(); // media wasn't blacklisted because it's final rendition assert.ok(!media.excludeUntil, 'media was not blacklisted after playlist 404'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); assert.equal(this.env.log.warn.args[0], 'Problem encountered with the current HLS playlist. Trying again since it is the final playlist.', 'log specific error message for final playlist'); }); QUnit.test('error on the first playlist request does not trigger an error ' + 'when there is master playlist with only one media playlist', function(assert) { this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); openMediaSource(this.player, this.clock); this.requests[0] .respond(200, null, '#EXTM3U\n' + '#EXT-X-STREAM-INF:BANDWIDTH=1000\n' + 'media.m3u8\n'); this.requests[1].respond(404); let url = this.requests[1].url.slice(this.requests[1].url.lastIndexOf('/') + 1); let media = this.player.tech_.hls.playlists.master.playlists[url]; // media wasn't blacklisted because it's final rendition assert.ok(!media.excludeUntil, 'media was not blacklisted after playlist 404'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); assert.equal(this.env.log.warn.args[0], 'Problem encountered with the current HLS playlist. Trying again since it is the final playlist.', 'log specific error message for final playlist'); }); QUnit.test('seeking in an empty playlist is a non-erroring noop', function(assert) { let requestsLength; this.player.src({ src: 'manifest/empty-live.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.requests.shift().respond(200, null, '#EXTM3U\n'); requestsLength = this.requests.length; this.player.tech_.setCurrentTime(183); this.clock.tick(1); assert.equal(this.requests.length, requestsLength, 'made no additional requests'); }); QUnit.test('fire loadedmetadata once we successfully load a playlist', function(assert) { let count = 0; this.player.src({ src: 'manifest/master.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); let hls = this.player.tech_.hls; hls.bandwidth = 20000; hls.masterPlaylistController_.masterPlaylistLoader_.on('loadedmetadata', function() { count += 1; }); // master this.standardXHRResponse(this.requests.shift()); assert.equal(count, 0, 'loadedMedia not triggered before requesting playlist'); // media this.requests.shift().respond(404); assert.equal(count, 0, 'loadedMedia not triggered after playlist 404'); assert.equal(this.env.log.warn.calls, 1, 'warning logged for blacklist'); // media this.standardXHRResponse(this.requests.shift()); assert.equal(count, 1, 'loadedMedia triggered after successful recovery from 404'); // verify stats assert.equal(this.player.tech_.hls.stats.bandwidth, 20000, 'bandwidth set above'); }); QUnit.test('sets seekable and duration for live playlists', function(assert) { this.player.src({ src: 'http://example.com/manifest/missingEndlist.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); assert.equal(this.player.tech_.hls.mediaSource.seekable.length, 1, 'set one seekable range'); assert.equal(this.player.tech_.hls.mediaSource.seekable.start(0), this.player.tech_.hls.seekable().start(0), 'set seekable start'); assert.equal(this.player.tech_.hls.mediaSource.seekable.end(0), this.player.tech_.hls.seekable().end(0), 'set seekable end'); assert.strictEqual(this.player.tech_.hls.mediaSource.duration, Infinity, 'duration on the mediaSource is infinity'); }); QUnit.test('live playlist starts with correct currentTime value', function(assert) { this.player.src({ src: 'http://example.com/manifest/liveStart30sBefore.m3u8', type: 'application/vnd.apple.mpegurl' }); this.clock.tick(1); openMediaSource(this.player, this.clock); this.standardXHRResponse(this.requests[0]); this.player.tech_.hls.playlists.trigger('loadedmetadata'); this.player.tech_.paused = function() { return false; }; this.player.tech_.trigger('play'); this.clock.tick(1); let media = this.player.tech_.hls.playlists.media(); assert.strictEqual(this.player.currentTime(), Hls.Playlist.seekable(media).end(0), 'currentTime is updated at playback');