From 4e4b2abf9f67fe8eb33ef3b00b72ce444d8c1141 Mon Sep 17 00:00:00 2001 From: Stephan Hesse Date: Mon, 5 Mar 2018 10:53:55 +0100 Subject: [PATCH] fix all auto lint errors --- src/config.js | 151 +- src/controller/abr-controller.js | 197 +- src/controller/audio-stream-controller.js | 853 ++++--- src/controller/audio-track-controller.js | 75 +- src/controller/buffer-controller.js | 218 +- src/controller/cap-level-controller.js | 50 +- src/controller/eme-controller.js | 626 +++--- src/controller/fps-controller.js | 38 +- src/controller/id3-track-controller.js | 26 +- src/controller/level-controller.js | 178 +- src/controller/stream-controller.js | 899 ++++---- src/controller/subtitle-stream-controller.js | 177 +- src/controller/subtitle-track-controller.js | 91 +- src/controller/timeline-controller.js | 199 +- src/crypt/aes-crypto.js | 6 +- src/crypt/aes-decryptor.js | 60 +- src/crypt/decrypter.js | 56 +- src/crypt/fast-aes-key.js | 6 +- src/demux/aacdemuxer.js | 22 +- src/demux/adts.js | 60 +- src/demux/demuxer-inline.js | 30 +- src/demux/demuxer-worker.js | 59 +- src/demux/demuxer.js | 115 +- src/demux/exp-golomb.js | 154 +- src/demux/id3.js | 163 +- src/demux/mp3demuxer.js | 29 +- src/demux/mp4demuxer.js | 250 +-- src/demux/mpegaudio.js | 242 +- src/demux/sample-aes.js | 58 +- src/demux/tsdemuxer.js | 978 ++++---- src/errors.js | 4 +- src/event-handler.js | 41 +- src/events.js | 2 +- src/helper/aac.js | 58 +- src/helper/buffer-helper.js | 63 +- src/helper/fragment-tracker.js | 77 +- src/helper/is-supported.js | 4 +- src/helper/level-helper.js | 138 +- src/helper/mediakeys-helper.js | 8 +- src/helper/mediasource-helper.js | 5 +- src/hls.js | 179 +- src/loader/fragment-loader.js | 56 +- src/loader/fragment.js | 58 +- src/loader/key-loader.js | 86 +- src/loader/level-key.js | 9 +- src/loader/m3u8-parser.js | 266 ++- src/loader/playlist-loader.js | 131 +- src/remux/dummy-remuxer.js | 47 +- src/remux/mp4-generator.js | 376 ++-- src/remux/mp4-remuxer.js | 514 +++-- src/remux/passthrough-remuxer.js | 26 +- src/task-loop.js | 20 +- src/utils/attr-list.js | 57 +- src/utils/binary-search.js | 47 +- src/utils/cea-608-parser.js | 2000 ++++++++--------- src/utils/codecs.js | 132 +- src/utils/cues.js | 46 +- src/utils/discontinuities.js | 28 +- src/utils/ewma-bandwidth-estimator.js | 32 +- src/utils/ewma.js | 15 +- src/utils/fetch-loader.js | 62 +- src/utils/hex.js | 12 +- src/utils/logger.js | 31 +- src/utils/output-filter.js | 15 +- src/utils/time-ranges.js | 8 +- src/utils/vttcue.js | 130 +- src/utils/vttparser.js | 402 ++-- src/utils/webvtt-parser.js | 297 ++- src/utils/xhr-loader.js | 84 +- tests/functional/auto/hlsjs.html | 173 -- tests/functional/auto/hlsjs.js | 257 --- tests/functional/auto/index.html | 16 + tests/functional/auto/setup.js | 257 +++ tests/functional/auto/style.css | 36 + tests/functional/auto/testbench.js | 135 ++ tests/mocks/hls.mock.js | 9 +- tests/test-streams.js | 166 +- tests/unit/controller/abr-controller.js | 20 +- tests/unit/controller/cap-level-controller.js | 4 +- tests/unit/controller/eme-controller.js | 67 +- .../controller/ewma-bandwidth-estimator.js | 38 +- tests/unit/controller/level-controller.js | 153 +- tests/unit/controller/stream-controller.js | 355 ++- .../controller/subtitle-track-controller.js | 1 - tests/unit/controller/timeline-controller.js | 2 - tests/unit/demuxer/demuxer.js | 472 ++-- tests/unit/dummy.js | 6 +- tests/unit/events.js | 27 +- tests/unit/helper/buffer-helper.js | 38 +- tests/unit/helper/fragment-tracker.js | 65 +- tests/unit/loader/playlist-loader.js | 345 ++- tests/unit/utils/attr-list.js | 14 +- tests/unit/utils/binary-search.js | 31 +- tests/unit/utils/discontinuities.js | 136 +- tests/unit/utils/output-filter.js | 10 +- 95 files changed, 7009 insertions(+), 7456 deletions(-) delete mode 100644 tests/functional/auto/hlsjs.html delete mode 100644 tests/functional/auto/hlsjs.js create mode 100644 tests/functional/auto/index.html create mode 100644 tests/functional/auto/setup.js create mode 100644 tests/functional/auto/style.css create mode 100644 tests/functional/auto/testbench.js diff --git a/src/config.js b/src/config.js index 075074dd35e..9bc23733f3a 100644 --- a/src/config.js +++ b/src/config.js @@ -2,107 +2,107 @@ * HLS config */ -import AbrController from './controller/abr-controller'; -import BufferController from './controller/buffer-controller'; -import CapLevelController from './controller/cap-level-controller'; +import AbrController from './controller/abr-controller'; +import BufferController from './controller/buffer-controller'; +import CapLevelController from './controller/cap-level-controller'; import FPSController from './controller/fps-controller'; import XhrLoader from './utils/xhr-loader'; -//import FetchLoader from './utils/fetch-loader'; +// import FetchLoader from './utils/fetch-loader'; import AudioTrackController from './controller/audio-track-controller'; -import AudioStreamController from './controller/audio-stream-controller'; +import AudioStreamController from './controller/audio-stream-controller'; import * as Cues from './utils/cues'; import TimelineController from './controller/timeline-controller'; import SubtitleTrackController from './controller/subtitle-track-controller'; -import SubtitleStreamController from './controller/subtitle-stream-controller'; +import SubtitleStreamController from './controller/subtitle-stream-controller'; import EMEController from './controller/eme-controller'; -import {requestMediaKeySystemAccess} from './helper/mediakeys-helper'; +import { requestMediaKeySystemAccess } from './helper/mediakeys-helper'; export var hlsDefaultConfig = { - autoStartLoad: true, // used by stream-controller - startPosition: -1, // used by stream-controller - defaultAudioCodec: undefined, // used by stream-controller - debug: false, // used by logger - capLevelOnFPSDrop: false, // used by fps-controller - capLevelToPlayerSize: false, // used by cap-level-controller - initialLiveManifestSize: 1, // used by stream-controller - maxBufferLength: 30, // used by stream-controller - maxBufferSize: 60 * 1000 * 1000, // used by stream-controller - maxBufferHole: 0.5, // used by stream-controller - maxSeekHole: 2, // used by stream-controller - lowBufferWatchdogPeriod: 0.5, // used by stream-controller - highBufferWatchdogPeriod: 3, // used by stream-controller - nudgeOffset: 0.1, // used by stream-controller - nudgeMaxRetry : 3, // used by stream-controller - maxFragLookUpTolerance: 0.25, // used by stream-controller - liveSyncDurationCount:3, // used by stream-controller - liveMaxLatencyDurationCount: Infinity, // used by stream-controller - liveSyncDuration: undefined, // used by stream-controller - liveMaxLatencyDuration: undefined, // used by stream-controller - liveDurationInfinity: false, // used by buffer-controller - maxMaxBufferLength: 600, // used by stream-controller - enableWorker: true, // used by demuxer - enableSoftwareAES: true, // used by decrypter - manifestLoadingTimeOut: 10000, // used by playlist-loader - manifestLoadingMaxRetry: 1, // used by playlist-loader - manifestLoadingRetryDelay: 1000, // used by playlist-loader - manifestLoadingMaxRetryTimeout: 64000, // used by playlist-loader - startLevel: undefined, // used by level-controller - levelLoadingTimeOut: 10000, // used by playlist-loader - levelLoadingMaxRetry: 4, // used by playlist-loader - levelLoadingRetryDelay: 1000, // used by playlist-loader - levelLoadingMaxRetryTimeout: 64000, // used by playlist-loader - fragLoadingTimeOut: 20000, // used by fragment-loader - fragLoadingMaxRetry: 6, // used by fragment-loader - fragLoadingRetryDelay: 1000, // used by fragment-loader - fragLoadingMaxRetryTimeout: 64000, // used by fragment-loader - startFragPrefetch: false, // used by stream-controller - fpsDroppedMonitoringPeriod: 5000, // used by fps-controller - fpsDroppedMonitoringThreshold: 0.2, // used by fps-controller - appendErrorMaxRetry: 3, // used by buffer-controller + autoStartLoad: true, // used by stream-controller + startPosition: -1, // used by stream-controller + defaultAudioCodec: undefined, // used by stream-controller + debug: false, // used by logger + capLevelOnFPSDrop: false, // used by fps-controller + capLevelToPlayerSize: false, // used by cap-level-controller + initialLiveManifestSize: 1, // used by stream-controller + maxBufferLength: 30, // used by stream-controller + maxBufferSize: 60 * 1000 * 1000, // used by stream-controller + maxBufferHole: 0.5, // used by stream-controller + maxSeekHole: 2, // used by stream-controller + lowBufferWatchdogPeriod: 0.5, // used by stream-controller + highBufferWatchdogPeriod: 3, // used by stream-controller + nudgeOffset: 0.1, // used by stream-controller + nudgeMaxRetry: 3, // used by stream-controller + maxFragLookUpTolerance: 0.25, // used by stream-controller + liveSyncDurationCount: 3, // used by stream-controller + liveMaxLatencyDurationCount: Infinity, // used by stream-controller + liveSyncDuration: undefined, // used by stream-controller + liveMaxLatencyDuration: undefined, // used by stream-controller + liveDurationInfinity: false, // used by buffer-controller + maxMaxBufferLength: 600, // used by stream-controller + enableWorker: true, // used by demuxer + enableSoftwareAES: true, // used by decrypter + manifestLoadingTimeOut: 10000, // used by playlist-loader + manifestLoadingMaxRetry: 1, // used by playlist-loader + manifestLoadingRetryDelay: 1000, // used by playlist-loader + manifestLoadingMaxRetryTimeout: 64000, // used by playlist-loader + startLevel: undefined, // used by level-controller + levelLoadingTimeOut: 10000, // used by playlist-loader + levelLoadingMaxRetry: 4, // used by playlist-loader + levelLoadingRetryDelay: 1000, // used by playlist-loader + levelLoadingMaxRetryTimeout: 64000, // used by playlist-loader + fragLoadingTimeOut: 20000, // used by fragment-loader + fragLoadingMaxRetry: 6, // used by fragment-loader + fragLoadingRetryDelay: 1000, // used by fragment-loader + fragLoadingMaxRetryTimeout: 64000, // used by fragment-loader + startFragPrefetch: false, // used by stream-controller + fpsDroppedMonitoringPeriod: 5000, // used by fps-controller + fpsDroppedMonitoringThreshold: 0.2, // used by fps-controller + appendErrorMaxRetry: 3, // used by buffer-controller loader: XhrLoader, - //loader: FetchLoader, - fLoader: undefined, // used by fragment-loader - pLoader: undefined, // used by playlist-loader - xhrSetup: undefined, // used by xhr-loader - licenseXhrSetup: undefined, // used by eme-controller + // loader: FetchLoader, + fLoader: undefined, // used by fragment-loader + pLoader: undefined, // used by playlist-loader + xhrSetup: undefined, // used by xhr-loader + licenseXhrSetup: undefined, // used by eme-controller // fetchSetup: undefined, abrController: AbrController, bufferController: BufferController, capLevelController: CapLevelController, fpsController: FPSController, - stretchShortVideoTrack: false, // used by mp4-remuxer - maxAudioFramesDrift :1, // used by mp4-remuxer - forceKeyFrameOnDiscontinuity: true, // used by ts-demuxer - abrEwmaFastLive: 3, // used by abr-controller - abrEwmaSlowLive: 9, // used by abr-controller - abrEwmaFastVoD: 3, // used by abr-controller - abrEwmaSlowVoD: 9, // used by abr-controller + stretchShortVideoTrack: false, // used by mp4-remuxer + maxAudioFramesDrift: 1, // used by mp4-remuxer + forceKeyFrameOnDiscontinuity: true, // used by ts-demuxer + abrEwmaFastLive: 3, // used by abr-controller + abrEwmaSlowLive: 9, // used by abr-controller + abrEwmaFastVoD: 3, // used by abr-controller + abrEwmaSlowVoD: 9, // used by abr-controller abrEwmaDefaultEstimate: 5e5, // 500 kbps // used by abr-controller - abrBandWidthFactor : 0.95, // used by abr-controller - abrBandWidthUpFactor : 0.7, // used by abr-controller - abrMaxWithRealBitrate : false, // used by abr-controller - maxStarvationDelay : 4, // used by abr-controller - maxLoadingDelay : 4, // used by abr-controller - minAutoBitrate: 0, // used by hls - emeEnabled: false, // used by eme-controller - widevineLicenseUrl: undefined, // used by eme-controller + abrBandWidthFactor: 0.95, // used by abr-controller + abrBandWidthUpFactor: 0.7, // used by abr-controller + abrMaxWithRealBitrate: false, // used by abr-controller + maxStarvationDelay: 4, // used by abr-controller + maxLoadingDelay: 4, // used by abr-controller + minAutoBitrate: 0, // used by hls + emeEnabled: false, // used by eme-controller + widevineLicenseUrl: undefined, // used by eme-controller requestMediaKeySystemAccessFunc: - requestMediaKeySystemAccess, // used by eme-controller + requestMediaKeySystemAccess // used by eme-controller }; if (__USE_SUBTITLES__) { hlsDefaultConfig.subtitleStreamController = SubtitleStreamController; hlsDefaultConfig.subtitleTrackController = SubtitleTrackController; hlsDefaultConfig.timelineController = TimelineController; - hlsDefaultConfig.cueHandler = Cues; // used by timeline-controller - hlsDefaultConfig.enableCEA708Captions = true; // used by timeline-controller - hlsDefaultConfig.enableWebVTT = true; // used by timeline-controller - hlsDefaultConfig.captionsTextTrack1Label = 'English'; // used by timeline-controller + hlsDefaultConfig.cueHandler = Cues; // used by timeline-controller + hlsDefaultConfig.enableCEA708Captions = true; // used by timeline-controller + hlsDefaultConfig.enableWebVTT = true; // used by timeline-controller + hlsDefaultConfig.captionsTextTrack1Label = 'English'; // used by timeline-controller hlsDefaultConfig.captionsTextTrack1LanguageCode = 'en'; // used by timeline-controller - hlsDefaultConfig.captionsTextTrack2Label = 'Spanish'; // used by timeline-controller + hlsDefaultConfig.captionsTextTrack2Label = 'Spanish'; // used by timeline-controller hlsDefaultConfig.captionsTextTrack2LanguageCode = 'es'; // used by timeline-controller } @@ -111,6 +111,5 @@ if (__USE_ALT_AUDIO__) { hlsDefaultConfig.audioTrackController = AudioTrackController; } -if (__USE_EME_DRM__) { +if (__USE_EME_DRM__) hlsDefaultConfig.emeController = EMEController; -} diff --git a/src/controller/abr-controller.js b/src/controller/abr-controller.js index 0486bb8794f..1719cd44dc3 100644 --- a/src/controller/abr-controller.js +++ b/src/controller/abr-controller.js @@ -7,17 +7,16 @@ import Event from '../events'; import EventHandler from '../event-handler'; import BufferHelper from '../helper/buffer-helper'; -import {ErrorDetails} from '../errors'; -import {logger} from '../utils/logger'; +import { ErrorDetails } from '../errors'; +import { logger } from '../utils/logger'; import EwmaBandWidthEstimator from '../utils/ewma-bandwidth-estimator'; class AbrController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.FRAG_LOADING, - Event.FRAG_LOADED, - Event.FRAG_BUFFERED, - Event.ERROR); + Event.FRAG_LOADED, + Event.FRAG_BUFFERED, + Event.ERROR); this.lastLoadedFragLevel = 0; this._nextAutoLevel = -1; this.hls = hls; @@ -26,25 +25,25 @@ class AbrController extends EventHandler { this.onCheck = this._abandonRulesCheck.bind(this); } - destroy() { + destroy () { this.clearTimer(); EventHandler.prototype.destroy.call(this); } - onFragLoading(data) { + onFragLoading (data) { let frag = data.frag; if (frag.type === 'main') { - if (!this.timer) { + if (!this.timer) this.timer = setInterval(this.onCheck, 100); - } + // lazy init of bw Estimator, rationale is that we use different params for Live/VoD // so we need to wait for stream manifest / playlist type to instantiate it. if (!this._bwEstimator) { let hls = this.hls, - level = data.frag.level, - isLive = hls.levels[level].details.live, - config = hls.config, - ewmaFast, ewmaSlow; + level = data.frag.level, + isLive = hls.levels[level].details.live, + config = hls.config, + ewmaFast, ewmaSlow; if (isLive) { ewmaFast = config.abrEwmaFastLive; @@ -53,22 +52,22 @@ class AbrController extends EventHandler { ewmaFast = config.abrEwmaFastVoD; ewmaSlow = config.abrEwmaSlowVoD; } - this._bwEstimator = new EwmaBandWidthEstimator(hls,ewmaSlow,ewmaFast,config.abrEwmaDefaultEstimate); + this._bwEstimator = new EwmaBandWidthEstimator(hls, ewmaSlow, ewmaFast, config.abrEwmaDefaultEstimate); } this.fragCurrent = frag; } } - _abandonRulesCheck() { + _abandonRulesCheck () { /* monitor fragment retrieval time... we compute expected time of arrival of the complete fragment. we compare it to expected time of buffer starvation */ - let hls = this.hls, v = hls.media,frag = this.fragCurrent, loader = frag.loader, minAutoLevel = hls.minAutoLevel; + let hls = this.hls, v = hls.media, frag = this.fragCurrent, loader = frag.loader, minAutoLevel = hls.minAutoLevel; // if loader has been destroyed or loading has been aborted, stop timer and return - if(!loader || ( loader.stats && loader.stats.aborted)) { + if (!loader || (loader.stats && loader.stats.aborted)) { logger.warn('frag loader destroy or aborted, disarm abandonRules'); this.clearTimer(); // reset forced auto level value so that next level will be selected @@ -80,18 +79,18 @@ class AbrController extends EventHandler { (video not paused OR first fragment being loaded(ready state === HAVE_NOTHING = 0)) AND autoswitching enabled AND not lowest level (=> means that we have several levels) */ if (v && stats && ((!v.paused && (v.playbackRate !== 0)) || !v.readyState) && frag.autoLevel && frag.level) { let requestDelay = performance.now() - stats.trequest, - playbackRate = Math.abs(v.playbackRate); + playbackRate = Math.abs(v.playbackRate); // monitor fragment load progress after half of expected fragment duration,to stabilize bitrate if (requestDelay > (500 * frag.duration / playbackRate)) { let levels = hls.levels, - loadRate = Math.max(1, stats.bw ? stats.bw / 8 : stats.loaded * 1000 / requestDelay), // byte/s; at least 1 byte/s to avoid division by zero - // compute expected fragment length using frag duration and level bitrate. also ensure that expected len is gte than already loaded size - level = levels[frag.level], - levelBitrate = level.realBitrate ? Math.max(level.realBitrate,level.bitrate) : level.bitrate, - expectedLen = stats.total ? stats.total : Math.max(stats.loaded, Math.round(frag.duration * levelBitrate / 8)), - pos = v.currentTime, - fragLoadedDelay = (expectedLen - stats.loaded) / loadRate, - bufferStarvationDelay = (BufferHelper.bufferInfo(v,pos,hls.config.maxBufferHole).end - pos) / playbackRate; + loadRate = Math.max(1, stats.bw ? stats.bw / 8 : stats.loaded * 1000 / requestDelay), // byte/s; at least 1 byte/s to avoid division by zero + // compute expected fragment length using frag duration and level bitrate. also ensure that expected len is gte than already loaded size + level = levels[frag.level], + levelBitrate = level.realBitrate ? Math.max(level.realBitrate, level.bitrate) : level.bitrate, + expectedLen = stats.total ? stats.total : Math.max(stats.loaded, Math.round(frag.duration * levelBitrate / 8)), + pos = v.currentTime, + fragLoadedDelay = (expectedLen - stats.loaded) / loadRate, + bufferStarvationDelay = (BufferHelper.bufferInfo(v, pos, hls.config.maxBufferHole).end - pos) / playbackRate; // consider emergency switch down only if we have less than 2 frag buffered AND // time to finish loading current fragment is bigger than buffer starvation delay // ie if we risk buffer starvation if bw does not increase quickly @@ -99,11 +98,11 @@ class AbrController extends EventHandler { let fragLevelNextLoadedDelay, nextLoadLevel; // lets iterate through lower level and try to find the biggest one that could avoid rebuffering // we start from current level - 1 and we step down , until we find a matching level - for (nextLoadLevel = frag.level - 1 ; nextLoadLevel > minAutoLevel ; nextLoadLevel--) { + for (nextLoadLevel = frag.level - 1; nextLoadLevel > minAutoLevel; nextLoadLevel--) { // compute time to load next fragment at lower level // 0.8 : consider only 80% of current bw to be conservative // 8 = bits per byte (bps/Bps) - let levelNextBitrate = levels[nextLoadLevel].realBitrate ? Math.max(levels[nextLoadLevel].realBitrate,levels[nextLoadLevel].bitrate) : levels[nextLoadLevel].bitrate; + let levelNextBitrate = levels[nextLoadLevel].realBitrate ? Math.max(levels[nextLoadLevel].realBitrate, levels[nextLoadLevel].bitrate) : levels[nextLoadLevel].bitrate; fragLevelNextLoadedDelay = frag.duration * levelNextBitrate / (8 * 0.8 * loadRate); if (fragLevelNextLoadedDelay < bufferStarvationDelay) { // we found a lower level that be rebuffering free with current estimated bw ! @@ -113,23 +112,23 @@ class AbrController extends EventHandler { // only emergency switch down if it takes less time to load new fragment at lowest level instead // of finishing loading current one ... if (fragLevelNextLoadedDelay < fragLoadedDelay) { - logger.warn(`loading too slow, abort fragment loading and switch to level ${nextLoadLevel}:fragLoadedDelay[${nextLoadLevel}]= 0) { return bestLevel; } else { logger.trace('rebuffering expected to happen, lets try to find a quality level minimizing the rebuffering'); // not possible to get rid of rebuffering ... let's try to find level that will guarantee less than maxStarvationDelay of rebuffering // if no matching level found, logic will return 0 - let maxStarvationDelay = currentFragDuration ? Math.min(currentFragDuration,config.maxStarvationDelay) : config.maxStarvationDelay, - bwFactor = config.abrBandWidthFactor, - bwUpFactor = config.abrBandWidthUpFactor; + let maxStarvationDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxStarvationDelay) : config.maxStarvationDelay, + bwFactor = config.abrBandWidthFactor, + bwUpFactor = config.abrBandWidthUpFactor; if (bufferStarvationDelay === 0) { // in case buffer is empty, let's check if previous fragment was loaded to perform a bitrate test let bitrateTestDelay = this.bitrateTestDelay; @@ -245,46 +243,46 @@ class AbrController extends EventHandler { // in that mode ABR controller will ensure that video loading time (ie the time to fetch the first fragment at lowest quality level + // the time to fetch the fragment at the appropriate quality level is less than ```maxLoadingDelay``` ) // cap maxLoadingDelay and ensure it is not bigger 'than bitrate test' frag duration - const maxLoadingDelay = currentFragDuration ? Math.min(currentFragDuration,config.maxLoadingDelay) : config.maxLoadingDelay; + const maxLoadingDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxLoadingDelay) : config.maxLoadingDelay; maxStarvationDelay = maxLoadingDelay - bitrateTestDelay; - logger.trace(`bitrate test took ${Math.round(1000*bitrateTestDelay)}ms, set first fragment max fetchDuration to ${Math.round(1000*maxStarvationDelay)} ms`); + logger.trace(`bitrate test took ${Math.round(1000 * bitrateTestDelay)}ms, set first fragment max fetchDuration to ${Math.round(1000 * maxStarvationDelay)} ms`); // don't use conservative factor on bitrate test bwFactor = bwUpFactor = 1; } } - bestLevel = this._findBestLevel(currentLevel,currentFragDuration,avgbw,minAutoLevel,maxAutoLevel,bufferStarvationDelay+maxStarvationDelay,bwFactor,bwUpFactor,levels); - return Math.max(bestLevel,0); + bestLevel = this._findBestLevel(currentLevel, currentFragDuration, avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay + maxStarvationDelay, bwFactor, bwUpFactor, levels); + return Math.max(bestLevel, 0); } } - _findBestLevel(currentLevel,currentFragDuration,currentBw,minAutoLevel,maxAutoLevel,maxFetchDuration,bwFactor,bwUpFactor,levels) { + _findBestLevel (currentLevel, currentFragDuration, currentBw, minAutoLevel, maxAutoLevel, maxFetchDuration, bwFactor, bwUpFactor, levels) { for (let i = maxAutoLevel; i >= minAutoLevel; i--) { let levelInfo = levels[i], - levelDetails = levelInfo.details, - avgDuration = levelDetails ? levelDetails.totalduration/levelDetails.fragments.length : currentFragDuration, - live = levelDetails ? levelDetails.live : false, - adjustedbw; - // follow algorithm captured from stagefright : - // https://android.googlesource.com/platform/frameworks/av/+/master/media/libstagefright/httplive/LiveSession.cpp - // Pick the highest bandwidth stream below or equal to estimated bandwidth. - // consider only 80% of the available bandwidth, but if we are switching up, - // be even more conservative (70%) to avoid overestimating and immediately - // switching back. - if (i <= currentLevel) { + levelDetails = levelInfo.details, + avgDuration = levelDetails ? levelDetails.totalduration / levelDetails.fragments.length : currentFragDuration, + live = levelDetails ? levelDetails.live : false, + adjustedbw; + // follow algorithm captured from stagefright : + // https://android.googlesource.com/platform/frameworks/av/+/master/media/libstagefright/httplive/LiveSession.cpp + // Pick the highest bandwidth stream below or equal to estimated bandwidth. + // consider only 80% of the available bandwidth, but if we are switching up, + // be even more conservative (70%) to avoid overestimating and immediately + // switching back. + if (i <= currentLevel) adjustedbw = bwFactor * currentBw; - } else { + else adjustedbw = bwUpFactor * currentBw; - } - const bitrate = levels[i].realBitrate ? Math.max(levels[i].realBitrate,levels[i].bitrate) : levels[i].bitrate, - fetchDuration = bitrate * avgDuration / adjustedbw; - logger.trace(`level/adjustedbw/bitrate/avgDuration/maxFetchDuration/fetchDuration: ${i}/${Math.round(adjustedbw)}/${bitrate}/${avgDuration}/${maxFetchDuration}/${fetchDuration}`); + const bitrate = levels[i].realBitrate ? Math.max(levels[i].realBitrate, levels[i].bitrate) : levels[i].bitrate, + fetchDuration = bitrate * avgDuration / adjustedbw; + + logger.trace(`level/adjustedbw/bitrate/avgDuration/maxFetchDuration/fetchDuration: ${i}/${Math.round(adjustedbw)}/${bitrate}/${avgDuration}/${maxFetchDuration}/${fetchDuration}`); // if adjusted bw is greater than level bitrate AND if (adjustedbw > bitrate && // fragment fetchDuration unknown OR live stream OR fragment fetchDuration less than max allowed fetch duration, then this level matches // we don't account for max Fetch Duration for live streams, this is to avoid switching down when near the edge of live sliding window ... // special case to support startLevel = -1 (bitrateTest) on live streams : in that case we should not exit loop so that _findBestLevel will return -1 - (!fetchDuration || (live && !this.bitrateTestDelay) || fetchDuration < maxFetchDuration) ) { + (!fetchDuration || (live && !this.bitrateTestDelay) || fetchDuration < maxFetchDuration)) { // as we are looping from highest to lowest, this will return the best achievable quality level return i; } @@ -293,10 +291,9 @@ class AbrController extends EventHandler { return -1; } - set nextAutoLevel(nextLevel) { + set nextAutoLevel (nextLevel) { this._nextAutoLevel = nextLevel; } } export default AbrController; - diff --git a/src/controller/audio-stream-controller.js b/src/controller/audio-stream-controller.js index 6644a695e5b..2f86d4020a6 100644 --- a/src/controller/audio-stream-controller.js +++ b/src/controller/audio-stream-controller.js @@ -8,33 +8,32 @@ import Demuxer from '../demux/demuxer'; import Event from '../events'; import * as LevelHelper from '../helper/level-helper'; import TimeRanges from '../utils/time-ranges'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {logger} from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { logger } from '../utils/logger'; import { findFragWithCC } from '../utils/discontinuities'; import TaskLoop from '../task-loop'; -import {FragmentState} from '../helper/fragment-tracker'; +import { FragmentState } from '../helper/fragment-tracker'; import Fragment from '../loader/fragment'; const State = { - STOPPED : 'STOPPED', - STARTING : 'STARTING', - IDLE : 'IDLE', - PAUSED : 'PAUSED', - KEY_LOADING : 'KEY_LOADING', - FRAG_LOADING : 'FRAG_LOADING', - FRAG_LOADING_WAITING_RETRY : 'FRAG_LOADING_WAITING_RETRY', - WAITING_TRACK : 'WAITING_TRACK', - PARSING : 'PARSING', - PARSED : 'PARSED', - BUFFER_FLUSHING : 'BUFFER_FLUSHING', - ENDED : 'ENDED', - ERROR : 'ERROR', - WAITING_INIT_PTS : 'WAITING_INIT_PTS' + STOPPED: 'STOPPED', + STARTING: 'STARTING', + IDLE: 'IDLE', + PAUSED: 'PAUSED', + KEY_LOADING: 'KEY_LOADING', + FRAG_LOADING: 'FRAG_LOADING', + FRAG_LOADING_WAITING_RETRY: 'FRAG_LOADING_WAITING_RETRY', + WAITING_TRACK: 'WAITING_TRACK', + PARSING: 'PARSING', + PARSED: 'PARSED', + BUFFER_FLUSHING: 'BUFFER_FLUSHING', + ENDED: 'ENDED', + ERROR: 'ERROR', + WAITING_INIT_PTS: 'WAITING_INIT_PTS' }; class AudioStreamController extends TaskLoop { - - constructor(hls, fragmentTracker) { + constructor (hls, fragmentTracker) { super(hls, Event.MEDIA_ATTACHED, Event.MEDIA_DETACHING, @@ -61,36 +60,35 @@ class AudioStreamController extends TaskLoop { this.videoTrackCC = null; } - onHandlerDestroying() { + onHandlerDestroying () { this.stopLoad(); } - onHandlerDestroyed() { + onHandlerDestroyed () { this.state = State.STOPPED; this.fragmentTracker = null; } - //Signal that video PTS was found - onInitPtsFound(data) { - var demuxerId=data.id, cc = data.frag.cc, initPTS = data.initPTS; - if(demuxerId === 'main') { - //Always update the new INIT PTS - //Can change due level switch + // Signal that video PTS was found + onInitPtsFound (data) { + let demuxerId = data.id, cc = data.frag.cc, initPTS = data.initPTS; + if (demuxerId === 'main') { + // Always update the new INIT PTS + // Can change due level switch this.initPTS[cc] = initPTS; this.videoTrackCC = cc; logger.log(`InitPTS for cc: ${cc} found from video track: ${initPTS}`); - //If we are waiting we need to demux/remux the waiting frag - //With the new initPTS - if (this.state === State.WAITING_INIT_PTS) { + // If we are waiting we need to demux/remux the waiting frag + // With the new initPTS + if (this.state === State.WAITING_INIT_PTS) this.tick(); - } } } - startLoad(startPosition) { + startLoad (startPosition) { if (this.tracks) { - var lastCurrentTime = this.lastCurrentTime; + let lastCurrentTime = this.lastCurrentTime; this.stopLoad(); this.setInterval(100); this.fragLoadError = 0; @@ -109,12 +107,12 @@ class AudioStreamController extends TaskLoop { } } - stopLoad() { - var frag = this.fragCurrent; + stopLoad () { + let frag = this.fragCurrent; if (frag) { - if (frag.loader) { + if (frag.loader) frag.loader.abort(); - } + this.fragmentTracker.removeFragment(frag); this.fragCurrent = null; } @@ -126,7 +124,7 @@ class AudioStreamController extends TaskLoop { this.state = State.STOPPED; } - set state(nextState) { + set state (nextState) { if (this.state !== nextState) { const previousState = this.state; this._state = nextState; @@ -134,282 +132,278 @@ class AudioStreamController extends TaskLoop { } } - get state() { + get state () { return this._state; } - doTick() { - var pos, track, trackDetails, hls = this.hls, config = hls.config; - //logger.log('audioStream:' + this.state); - switch(this.state) { - case State.ERROR: - //don't do anything in error state to avoid breaking further ... - case State.PAUSED: - //don't do anything in paused state either ... - case State.BUFFER_FLUSHING: + doTick () { + let pos, track, trackDetails, hls = this.hls, config = hls.config; + // logger.log('audioStream:' + this.state); + switch (this.state) { + case State.ERROR: + // don't do anything in error state to avoid breaking further ... + case State.PAUSED: + // don't do anything in paused state either ... + case State.BUFFER_FLUSHING: + break; + case State.STARTING: + this.state = State.WAITING_TRACK; + this.loadedmetadata = false; + break; + case State.IDLE: + const tracks = this.tracks; + // audio tracks not received => exit loop + if (!tracks) break; - case State.STARTING: - this.state = State.WAITING_TRACK; - this.loadedmetadata = false; + + // if video not attached AND + // start fragment already requested OR start frag prefetch disable + // exit loop + // => if media not attached but start frag prefetch is enabled and start frag not requested yet, we will not exit loop + if (!this.media && + (this.startFragRequested || !config.startFragPrefetch)) break; - case State.IDLE: - const tracks = this.tracks; - // audio tracks not received => exit loop - if (!tracks) { + + // determine next candidate fragment to be loaded, based on current position and + // end of buffer position + // if we have not yet loaded any fragment, start loading from start position + if (this.loadedmetadata) { + pos = this.media.currentTime; + } else { + pos = this.nextLoadPosition; + if (pos === undefined) break; - } - // if video not attached AND - // start fragment already requested OR start frag prefetch disable - // exit loop - // => if media not attached but start frag prefetch is enabled and start frag not requested yet, we will not exit loop - if (!this.media && - (this.startFragRequested || !config.startFragPrefetch)) { + } + let media = this.mediaBuffer ? this.mediaBuffer : this.media, + videoBuffer = this.videoBuffer ? this.videoBuffer : this.media, + bufferInfo = BufferHelper.bufferInfo(media, pos, config.maxBufferHole), + mainBufferInfo = BufferHelper.bufferInfo(videoBuffer, pos, config.maxBufferHole), + bufferLen = bufferInfo.len, + bufferEnd = bufferInfo.end, + fragPrevious = this.fragPrevious, + // ensure we buffer at least config.maxBufferLength (default 30s) or config.maxMaxBufferLength (default: 600s) + // whichever is smaller. + // once we reach that threshold, don't buffer more than video (mainBufferInfo.len) + maxConfigBuffer = Math.min(config.maxBufferLength, config.maxMaxBufferLength), + maxBufLen = Math.max(maxConfigBuffer, mainBufferInfo.len), + audioSwitch = this.audioSwitch, + trackId = this.trackId; + + // if buffer length is less than maxBufLen try to load a new fragment + if ((bufferLen < maxBufLen || audioSwitch) && trackId < tracks.length) { + trackDetails = tracks[trackId].details; + // if track info not retrieved yet, switch state and wait for track retrieval + if (typeof trackDetails === 'undefined') { + this.state = State.WAITING_TRACK; break; } - // determine next candidate fragment to be loaded, based on current position and - // end of buffer position - // if we have not yet loaded any fragment, start loading from start position - if (this.loadedmetadata) { - pos = this.media.currentTime; - } else { - pos = this.nextLoadPosition; - if (pos === undefined) { - break; - } - } - let media = this.mediaBuffer ? this.mediaBuffer : this.media, - videoBuffer = this.videoBuffer ? this.videoBuffer : this.media, - bufferInfo = BufferHelper.bufferInfo(media,pos,config.maxBufferHole), - mainBufferInfo = BufferHelper.bufferInfo(videoBuffer,pos,config.maxBufferHole), - bufferLen = bufferInfo.len, - bufferEnd = bufferInfo.end, - fragPrevious = this.fragPrevious, - // ensure we buffer at least config.maxBufferLength (default 30s) or config.maxMaxBufferLength (default: 600s) - // whichever is smaller. - // once we reach that threshold, don't buffer more than video (mainBufferInfo.len) - maxConfigBuffer = Math.min(config.maxBufferLength,config.maxMaxBufferLength), - maxBufLen = Math.max(maxConfigBuffer,mainBufferInfo.len), - audioSwitch = this.audioSwitch, - trackId = this.trackId; - // if buffer length is less than maxBufLen try to load a new fragment - if ((bufferLen < maxBufLen || audioSwitch) && trackId < tracks.length) { - trackDetails = tracks[trackId].details; - // if track info not retrieved yet, switch state and wait for track retrieval - if (typeof trackDetails === 'undefined') { - this.state = State.WAITING_TRACK; + // check if we need to finalize media stream + // we just got done loading the final fragment and there is no other buffered range after ... + // rationale is that in case there are any buffered ranges after, it means that there are unbuffered portion in between + // so we should not switch to ENDED in that case, to be able to buffer them + if (!audioSwitch && !trackDetails.live && fragPrevious && fragPrevious.sn === trackDetails.endSN && !bufferInfo.nextStart) { + // if we are not seeking or if we are seeking but everything (almost) til the end is buffered, let's signal eos + // we don't compare exactly media.duration === bufferInfo.end as there could be some subtle media duration difference when switching + // between different renditions. using half frag duration should help cope with these cases. + if (!this.media.seeking || (this.media.duration - bufferEnd) < fragPrevious.duration / 2) { + // Finalize the media stream + this.hls.trigger(Event.BUFFER_EOS, { type: 'audio' }); + this.state = State.ENDED; break; } + } - // check if we need to finalize media stream - // we just got done loading the final fragment and there is no other buffered range after ... - // rationale is that in case there are any buffered ranges after, it means that there are unbuffered portion in between - // so we should not switch to ENDED in that case, to be able to buffer them - if (!audioSwitch && !trackDetails.live && fragPrevious && fragPrevious.sn === trackDetails.endSN && !bufferInfo.nextStart) { - // if we are not seeking or if we are seeking but everything (almost) til the end is buffered, let's signal eos - // we don't compare exactly media.duration === bufferInfo.end as there could be some subtle media duration difference when switching - // between different renditions. using half frag duration should help cope with these cases. - if (!this.media.seeking || (this.media.duration-bufferEnd) < fragPrevious.duration/2) { - // Finalize the media stream - this.hls.trigger(Event.BUFFER_EOS,{ type : 'audio'}); - this.state = State.ENDED; - break; - } - } - - // find fragment index, contiguous with end of buffer position - let fragments = trackDetails.fragments, - fragLen = fragments.length, - start = fragments[0].start, - end = fragments[fragLen-1].start + fragments[fragLen-1].duration, - frag; + // find fragment index, contiguous with end of buffer position + let fragments = trackDetails.fragments, + fragLen = fragments.length, + start = fragments[0].start, + end = fragments[fragLen - 1].start + fragments[fragLen - 1].duration, + frag; // When switching audio track, reload audio as close as possible to currentTime - if(audioSwitch){ - if (trackDetails.live && !trackDetails.PTSKnown) { - logger.log(`switching audiotrack, live stream, unknown PTS,load first fragment`); - bufferEnd = 0; - } else { - bufferEnd = pos; - // if currentTime (pos) is less than alt audio playlist start time, it means that alt audio is ahead of currentTime - if (trackDetails.PTSKnown && pos < start) { - // if everything is buffered from pos to start or if audio buffer upfront, let's seek to start - if (bufferInfo.end > start || bufferInfo.nextStart) { - logger.log('alt audio track ahead of main track, seek to start of alt audio track'); - this.media.currentTime = start + 0.05; - } else { - return; - } + if (audioSwitch) { + if (trackDetails.live && !trackDetails.PTSKnown) { + logger.log('switching audiotrack, live stream, unknown PTS,load first fragment'); + bufferEnd = 0; + } else { + bufferEnd = pos; + // if currentTime (pos) is less than alt audio playlist start time, it means that alt audio is ahead of currentTime + if (trackDetails.PTSKnown && pos < start) { + // if everything is buffered from pos to start or if audio buffer upfront, let's seek to start + if (bufferInfo.end > start || bufferInfo.nextStart) { + logger.log('alt audio track ahead of main track, seek to start of alt audio track'); + this.media.currentTime = start + 0.05; + } else { + return; } } } - if (trackDetails.initSegment && !trackDetails.initSegment.data) { - frag = trackDetails.initSegment; - } - // if bufferEnd before start of playlist, load first fragment - else if (bufferEnd <= start) { - frag = fragments[0]; - if (this.videoTrackCC !== null && frag.cc !== this.videoTrackCC) { - // Ensure we find a fragment which matches the continuity of the video track - frag = findFragWithCC(fragments, this.videoTrackCC); - } - if (trackDetails.live && frag.loadIdx && frag.loadIdx === this.fragLoadIdx) { - // we just loaded this first fragment, and we are still lagging behind the start of the live playlist - // let's force seek to start - const nextBuffered = bufferInfo.nextStart ? bufferInfo.nextStart : start; - logger.log(`no alt audio available @currentTime:${this.media.currentTime}, seeking @${nextBuffered + 0.05}`); - this.media.currentTime = nextBuffered + 0.05; - return; - } + } + if (trackDetails.initSegment && !trackDetails.initSegment.data) { + frag = trackDetails.initSegment; + } // eslint-disable-line brace-style + // if bufferEnd before start of playlist, load first fragment + else if (bufferEnd <= start) { + frag = fragments[0]; + if (this.videoTrackCC !== null && frag.cc !== this.videoTrackCC) { + // Ensure we find a fragment which matches the continuity of the video track + frag = findFragWithCC(fragments, this.videoTrackCC); + } + if (trackDetails.live && frag.loadIdx && frag.loadIdx === this.fragLoadIdx) { + // we just loaded this first fragment, and we are still lagging behind the start of the live playlist + // let's force seek to start + const nextBuffered = bufferInfo.nextStart ? bufferInfo.nextStart : start; + logger.log(`no alt audio available @currentTime:${this.media.currentTime}, seeking @${nextBuffered + 0.05}`); + this.media.currentTime = nextBuffered + 0.05; + return; + } + } else { + let foundFrag; + let maxFragLookUpTolerance = config.maxFragLookUpTolerance; + const fragNext = fragPrevious ? fragments[fragPrevious.sn - fragments[0].sn + 1] : undefined; + let fragmentWithinToleranceTest = (candidate) => { + // offset should be within fragment boundary - config.maxFragLookUpTolerance + // this is to cope with situations like + // bufferEnd = 9.991 + // frag[Ø] : [0,10] + // frag[1] : [10,20] + // bufferEnd is within frag[0] range ... although what we are expecting is to return frag[1] here + // frag start frag start+duration + // |-----------------------------| + // <---> <---> + // ...--------><-----------------------------><---------.... + // previous frag matching fragment next frag + // return -1 return 0 return 1 + // logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`); + // Set the lookup tolerance to be small enough to detect the current segment - ensures we don't skip over very small segments + let candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration); + if ((candidate.start + candidate.duration - candidateLookupTolerance) <= bufferEnd) + return 1; + // if maxFragLookUpTolerance will have negative value then don't return -1 for first element + else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) + return -1; + + return 0; + }; + + if (bufferEnd < end) { + if (bufferEnd > end - maxFragLookUpTolerance) + maxFragLookUpTolerance = 0; + + // Prefer the next fragment if it's within tolerance + if (fragNext && !fragmentWithinToleranceTest(fragNext)) + foundFrag = fragNext; + else + foundFrag = BinarySearch.search(fragments, fragmentWithinToleranceTest); } else { - let foundFrag; - let maxFragLookUpTolerance = config.maxFragLookUpTolerance; - const fragNext = fragPrevious ? fragments[fragPrevious.sn - fragments[0].sn + 1] : undefined; - let fragmentWithinToleranceTest = (candidate) => { - // offset should be within fragment boundary - config.maxFragLookUpTolerance - // this is to cope with situations like - // bufferEnd = 9.991 - // frag[Ø] : [0,10] - // frag[1] : [10,20] - // bufferEnd is within frag[0] range ... although what we are expecting is to return frag[1] here - // frag start frag start+duration - // |-----------------------------| - // <---> <---> - // ...--------><-----------------------------><---------.... - // previous frag matching fragment next frag - // return -1 return 0 return 1 - //logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`); - // Set the lookup tolerance to be small enough to detect the current segment - ensures we don't skip over very small segments - let candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration); - if ((candidate.start + candidate.duration - candidateLookupTolerance) <= bufferEnd) { - return 1; - }// if maxFragLookUpTolerance will have negative value then don't return -1 for first element - else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) { - return -1; - } - return 0; - }; - - if (bufferEnd < end) { - if (bufferEnd > end - maxFragLookUpTolerance) { - maxFragLookUpTolerance = 0; - } - // Prefer the next fragment if it's within tolerance - if (fragNext && !fragmentWithinToleranceTest(fragNext)) { - foundFrag = fragNext; + // reach end of playlist + foundFrag = fragments[fragLen - 1]; + } + if (foundFrag) { + frag = foundFrag; + start = foundFrag.start; + // logger.log('find SN matching with pos:' + bufferEnd + ':' + frag.sn); + if (fragPrevious && frag.level === fragPrevious.level && frag.sn === fragPrevious.sn) { + if (frag.sn < trackDetails.endSN) { + frag = fragments[frag.sn + 1 - trackDetails.startSN]; + logger.log(`SN just loaded, load next one: ${frag.sn}`); } else { - foundFrag = BinarySearch.search(fragments, fragmentWithinToleranceTest); - } - } else { - // reach end of playlist - foundFrag = fragments[fragLen-1]; - } - if (foundFrag) { - frag = foundFrag; - start = foundFrag.start; - //logger.log('find SN matching with pos:' + bufferEnd + ':' + frag.sn); - if (fragPrevious && frag.level === fragPrevious.level && frag.sn === fragPrevious.sn) { - if (frag.sn < trackDetails.endSN) { - frag = fragments[frag.sn + 1 - trackDetails.startSN]; - logger.log(`SN just loaded, load next one: ${frag.sn}`); - } else { - frag = null; - } + frag = null; } } } - if(frag) { - //logger.log(' loading frag ' + i +',pos/bufEnd:' + pos.toFixed(3) + '/' + bufferEnd.toFixed(3)); - if (frag.decryptdata && (frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) { - logger.log(`Loading key for ${frag.sn} of [${trackDetails.startSN} ,${trackDetails.endSN}],track ${trackId}`); - this.state = State.KEY_LOADING; - hls.trigger(Event.KEY_LOADING, {frag: frag}); - } else { - logger.log(`Loading ${frag.sn}, cc: ${frag.cc} of [${trackDetails.startSN} ,${trackDetails.endSN}],track ${trackId}, currentTime:${pos},bufferEnd:${bufferEnd.toFixed(3)}`); - // Check if fragment is not loaded - if(this.fragmentTracker.getState(frag) === FragmentState.NOT_LOADED) { - this.fragCurrent = frag; - this.startFragRequested = true; - if (!isNaN(frag.sn)) { - this.nextLoadPosition = frag.start + frag.duration; - } - hls.trigger(Event.FRAG_LOADING, {frag}); - this.state = State.FRAG_LOADING; - } + } + if (frag) { + // logger.log(' loading frag ' + i +',pos/bufEnd:' + pos.toFixed(3) + '/' + bufferEnd.toFixed(3)); + if (frag.decryptdata && (frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) { + logger.log(`Loading key for ${frag.sn} of [${trackDetails.startSN} ,${trackDetails.endSN}],track ${trackId}`); + this.state = State.KEY_LOADING; + hls.trigger(Event.KEY_LOADING, { frag: frag }); + } else { + logger.log(`Loading ${frag.sn}, cc: ${frag.cc} of [${trackDetails.startSN} ,${trackDetails.endSN}],track ${trackId}, currentTime:${pos},bufferEnd:${bufferEnd.toFixed(3)}`); + // Check if fragment is not loaded + if (this.fragmentTracker.getState(frag) === FragmentState.NOT_LOADED) { + this.fragCurrent = frag; + this.startFragRequested = true; + if (!isNaN(frag.sn)) + this.nextLoadPosition = frag.start + frag.duration; + + hls.trigger(Event.FRAG_LOADING, { frag }); + this.state = State.FRAG_LOADING; } } } - break; - case State.WAITING_TRACK: - track = this.tracks[this.trackId]; - // check if playlist is already loaded - if (track && track.details) { - this.state = State.IDLE; - } - break; - case State.FRAG_LOADING_WAITING_RETRY: - var now = performance.now(); - var retryDate = this.retryDate; - media = this.media; - var isSeeking = media && media.seeking; - // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading - if(!retryDate || (now >= retryDate) || isSeeking) { - logger.log(`audioStreamController: retryDate reached, switch back to IDLE state`); - this.state = State.IDLE; - } - break; - case State.WAITING_INIT_PTS: + } + break; + case State.WAITING_TRACK: + track = this.tracks[this.trackId]; + // check if playlist is already loaded + if (track && track.details) + this.state = State.IDLE; + + break; + case State.FRAG_LOADING_WAITING_RETRY: + var now = performance.now(); + var retryDate = this.retryDate; + media = this.media; + var isSeeking = media && media.seeking; + // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading + if (!retryDate || (now >= retryDate) || isSeeking) { + logger.log('audioStreamController: retryDate reached, switch back to IDLE state'); + this.state = State.IDLE; + } + break; + case State.WAITING_INIT_PTS: const videoTrackCC = this.videoTrackCC; - if (this.initPTS[videoTrackCC] === undefined) { - break; - } + if (this.initPTS[videoTrackCC] === undefined) + break; - // Ensure we don't get stuck in the WAITING_INIT_PTS state if the waiting frag CC doesn't match any initPTS - const waitingFrag = this.waitingFragment; - if (waitingFrag) { - const waitingFragCC = waitingFrag.frag.cc; - if (videoTrackCC !== waitingFragCC) { - track = this.tracks[this.trackId]; - if (track.details && track.details.live) { - logger.warn(`Waiting fragment CC (${waitingFragCC}) does not match video track CC (${videoTrackCC})`); - this.waitingFragment = null; - this.state = State.IDLE; - } - } else { - this.state = State.FRAG_LOADING; - this.onFragLoaded(this.waitingFragment); + // Ensure we don't get stuck in the WAITING_INIT_PTS state if the waiting frag CC doesn't match any initPTS + const waitingFrag = this.waitingFragment; + if (waitingFrag) { + const waitingFragCC = waitingFrag.frag.cc; + if (videoTrackCC !== waitingFragCC) { + track = this.tracks[this.trackId]; + if (track.details && track.details.live) { + logger.warn(`Waiting fragment CC (${waitingFragCC}) does not match video track CC (${videoTrackCC})`); this.waitingFragment = null; + this.state = State.IDLE; } } else { - this.state = State.IDLE; + this.state = State.FRAG_LOADING; + this.onFragLoaded(this.waitingFragment); + this.waitingFragment = null; } + } else { + this.state = State.IDLE; + } - break; - case State.STOPPED: - case State.FRAG_LOADING: - case State.PARSING: - case State.PARSED: - case State.ENDED: - break; - default: - break; + break; + case State.STOPPED: + case State.FRAG_LOADING: + case State.PARSING: + case State.PARSED: + case State.ENDED: + break; + default: + break; } } - onMediaAttached(data) { - var media = this.media = this.mediaBuffer = data.media; + onMediaAttached (data) { + let media = this.media = this.mediaBuffer = data.media; this.onvseeking = this.onMediaSeeking.bind(this); this.onvended = this.onMediaEnded.bind(this); media.addEventListener('seeking', this.onvseeking); media.addEventListener('ended', this.onvended); let config = this.config; - if(this.tracks && config.autoStartLoad) { + if (this.tracks && config.autoStartLoad) this.startLoad(config.startPosition); - } } - onMediaDetaching() { - var media = this.media; + onMediaDetaching () { + let media = this.media; if (media && media.ended) { logger.log('MSE detaching and video ended, reset startPosition'); this.startPosition = this.lastCurrentTime = 0; @@ -419,44 +413,43 @@ class AudioStreamController extends TaskLoop { if (media) { media.removeEventListener('seeking', this.onvseeking); media.removeEventListener('ended', this.onvended); - this.onvseeking = this.onvseeked = this.onvended = null; + this.onvseeking = this.onvseeked = this.onvended = null; } this.media = this.mediaBuffer = this.videoBuffer = null; this.loadedmetadata = false; this.stopLoad(); } - onMediaSeeking() { + onMediaSeeking () { if (this.state === State.ENDED) { - // switch to IDLE state to check for potential new fragment - this.state = State.IDLE; + // switch to IDLE state to check for potential new fragment + this.state = State.IDLE; } - if (this.media) { + if (this.media) this.lastCurrentTime = this.media.currentTime; - } + // tick to speed up processing this.tick(); } - onMediaEnded() { + onMediaEnded () { // reset startPosition and lastCurrentTime to restart playback @ stream beginning this.startPosition = this.lastCurrentTime = 0; } - - onAudioTracksUpdated(data) { + onAudioTracksUpdated (data) { logger.log('audio tracks updated'); this.tracks = data.audioTracks; } - onAudioTrackSwitching(data) { + onAudioTrackSwitching (data) { // if any URL found on new audio track, it is an alternate audio track - var altAudio = !!data.url; + let altAudio = !!data.url; this.trackId = data.id; this.fragCurrent = null; this.state = State.PAUSED; - this.waitingFragment=null; + this.waitingFragment = null; // destroy useless demuxer when switching audio to main if (!altAudio) { if (this.demuxer) { @@ -468,37 +461,36 @@ class AudioStreamController extends TaskLoop { this.setInterval(100); } - //should we switch tracks ? - if(altAudio){ + // should we switch tracks ? + if (altAudio) { this.audioSwitch = true; - //main audio track are handled by stream-controller, just do something if switching to alt audio track - this.state=State.IDLE; + // main audio track are handled by stream-controller, just do something if switching to alt audio track + this.state = State.IDLE; } this.tick(); } - onAudioTrackLoaded(data) { - var newDetails = data.details, - trackId = data.id, - track = this.tracks[trackId], - duration = newDetails.totalduration, - sliding = 0; + onAudioTrackLoaded (data) { + let newDetails = data.details, + trackId = data.id, + track = this.tracks[trackId], + duration = newDetails.totalduration, + sliding = 0; logger.log(`track ${trackId} loaded [${newDetails.startSN},${newDetails.endSN}],duration:${duration}`); if (newDetails.live) { - var curDetails = track.details; + let curDetails = track.details; if (curDetails && newDetails.fragments.length > 0) { // we already have details for that level, merge them - LevelHelper.mergeDetails(curDetails,newDetails); + LevelHelper.mergeDetails(curDetails, newDetails); sliding = newDetails.fragments[0].start; // TODO - //this.liveSyncPosition = this.computeLivePosition(sliding, curDetails); - if (newDetails.PTSKnown) { + // this.liveSyncPosition = this.computeLivePosition(sliding, curDetails); + if (newDetails.PTSKnown) logger.log(`live audio playlist sliding:${sliding.toFixed(3)}`); - } else { + else logger.log('live audio playlist - outdated PTS, unknown sliding'); - } } else { newDetails.PTSKnown = false; logger.log('live audio playlist - first load, unknown sliding'); @@ -514,7 +506,7 @@ class AudioStreamController extends TaskLoop { if (this.startPosition === -1) { // first, check if start time offset has been set in playlist, if yes, use this value let startTimeOffset = newDetails.startTimeOffset; - if(!isNaN(startTimeOffset)) { + if (!isNaN(startTimeOffset)) { logger.log(`start time offset found in playlist, adjust startPosition to ${startTimeOffset}`); this.startPosition = startTimeOffset; } else { @@ -524,51 +516,51 @@ class AudioStreamController extends TaskLoop { this.nextLoadPosition = this.startPosition; } // only switch batck to IDLE state if we were waiting for track to start downloading a new fragment - if (this.state === State.WAITING_TRACK) { + if (this.state === State.WAITING_TRACK) this.state = State.IDLE; - } - //trigger handler right now + + // trigger handler right now this.tick(); } - onKeyLoaded() { + onKeyLoaded () { if (this.state === State.KEY_LOADING) { this.state = State.IDLE; this.tick(); } } - onFragLoaded(data) { - var fragCurrent = this.fragCurrent, - fragLoaded = data.frag; + onFragLoaded (data) { + let fragCurrent = this.fragCurrent, + fragLoaded = data.frag; if (this.state === State.FRAG_LOADING && fragCurrent && fragLoaded.type === 'audio' && fragLoaded.level === fragCurrent.level && fragLoaded.sn === fragCurrent.sn) { - var track = this.tracks[this.trackId], - details = track.details, - duration = details.totalduration, - trackId = fragCurrent.level, - sn = fragCurrent.sn, - cc = fragCurrent.cc, - audioCodec = this.config.defaultAudioCodec || track.audioCodec || 'mp4a.40.2', - stats = this.stats = data.stats; + let track = this.tracks[this.trackId], + details = track.details, + duration = details.totalduration, + trackId = fragCurrent.level, + sn = fragCurrent.sn, + cc = fragCurrent.cc, + audioCodec = this.config.defaultAudioCodec || track.audioCodec || 'mp4a.40.2', + stats = this.stats = data.stats; if (sn === 'initSegment') { this.state = State.IDLE; stats.tparsed = stats.tbuffered = performance.now(); details.initSegment.data = data.payload; - this.hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: fragCurrent, id : 'audio'}); + this.hls.trigger(Event.FRAG_BUFFERED, { stats: stats, frag: fragCurrent, id: 'audio' }); this.tick(); } else { this.state = State.PARSING; // transmux the MPEG-TS data to ISO-BMFF segments this.appended = false; - if(!this.demuxer) { - this.demuxer = new Demuxer(this.hls,'audio'); - } - //Check if we have video initPTS + if (!this.demuxer) + this.demuxer = new Demuxer(this.hls, 'audio'); + + // Check if we have video initPTS // If not we need to wait for it let initPTS = this.initPTS[cc]; let initSegmentData = details.initSegment ? details.initSegment.data : []; @@ -576,20 +568,19 @@ class AudioStreamController extends TaskLoop { this.pendingBuffering = true; logger.log(`Demuxing ${sn} of [${details.startSN} ,${details.endSN}],track ${trackId}`); // time Offset is accurate if level PTS is known, or if playlist is not sliding (not live) - let accurateTimeOffset = false; //details.PTSKnown || !details.live; + let accurateTimeOffset = false; // details.PTSKnown || !details.live; this.demuxer.push(data.payload, initSegmentData, audioCodec, null, fragCurrent, duration, accurateTimeOffset, initPTS); } else { logger.log(`unknown video PTS for continuity counter ${cc}, waiting for video PTS before demuxing audio frag ${sn} of [${details.startSN} ,${details.endSN}],track ${trackId}`); - this.waitingFragment=data; - this.state=State.WAITING_INIT_PTS; + this.waitingFragment = data; + this.state = State.WAITING_INIT_PTS; } } } this.fragLoadError = 0; } - onFragParsingInitSegment(data) { - + onFragParsingInitSegment (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; if (fragCurrent && @@ -600,21 +591,19 @@ class AudioStreamController extends TaskLoop { let tracks = data.tracks, track; // delete any video track found on audio demuxer - if (tracks.video) { + if (tracks.video) delete tracks.video; - } // include levelCodec in audio and video tracks track = tracks.audio; - if(track) { + if (track) { track.levelCodec = track.codec; track.id = data.id; - this.hls.trigger(Event.BUFFER_CODECS,tracks); + this.hls.trigger(Event.BUFFER_CODECS, tracks); logger.log(`audio track:audio,container:${track.container},codecs[level/parsed]=[${track.levelCodec}/${track.codec}]`); let initSegment = track.initSegment; if (initSegment) { - - let appendObj = {type: 'audio', data: initSegment, parent: 'audio', content : 'initSegment'}; + let appendObj = { type: 'audio', data: initSegment, parent: 'audio', content: 'initSegment' }; if (this.audioSwitch) { this.pendingData = [appendObj]; } else { @@ -624,13 +613,13 @@ class AudioStreamController extends TaskLoop { this.hls.trigger(Event.BUFFER_APPENDING, appendObj); } } - //trigger handler right now + // trigger handler right now this.tick(); } } } - onFragParsingData(data) { + onFragParsingData (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; if (fragCurrent && @@ -639,9 +628,9 @@ class AudioStreamController extends TaskLoop { fragNew.sn === fragCurrent.sn && fragNew.level === fragCurrent.level && this.state === State.PARSING) { - let trackId= this.trackId, - track = this.tracks[trackId], - hls = this.hls; + let trackId = this.trackId, + track = this.tracks[trackId], + hls = this.hls; if (isNaN(data.endPTS)) { data.endPTS = data.startPTS + fragCurrent.duration; @@ -651,27 +640,27 @@ class AudioStreamController extends TaskLoop { fragCurrent.addElementaryStream(Fragment.ElementaryStreamTypes.AUDIO); logger.log(`parsed ${data.type},PTS:[${data.startPTS.toFixed(3)},${data.endPTS.toFixed(3)}],DTS:[${data.startDTS.toFixed(3)}/${data.endDTS.toFixed(3)}],nb:${data.nb}`); - LevelHelper.updateFragPTSDTS(track.details,fragCurrent,data.startPTS,data.endPTS); + LevelHelper.updateFragPTSDTS(track.details, fragCurrent, data.startPTS, data.endPTS); let audioSwitch = this.audioSwitch, media = this.media, appendOnBufferFlush = false; - //Only flush audio from old audio tracks when PTS is known on new audio track - if(audioSwitch && media) { + // Only flush audio from old audio tracks when PTS is known on new audio track + if (audioSwitch && media) { if (media.readyState) { let currentTime = media.currentTime; - logger.log('switching audio track : currentTime:'+ currentTime); + logger.log('switching audio track : currentTime:' + currentTime); if (currentTime >= data.startPTS) { logger.log('switching audio track : flushing all audio'); this.state = State.BUFFER_FLUSHING; - hls.trigger(Event.BUFFER_FLUSHING, {startOffset: 0 , endOffset: Number.POSITIVE_INFINITY, type : 'audio'}); + hls.trigger(Event.BUFFER_FLUSHING, { startOffset: 0, endOffset: Number.POSITIVE_INFINITY, type: 'audio' }); appendOnBufferFlush = true; - //Lets announce that the initial audio track switch flush occur + // Lets announce that the initial audio track switch flush occur this.audioSwitch = false; - hls.trigger(Event.AUDIO_TRACK_SWITCHED, {id : trackId}); + hls.trigger(Event.AUDIO_TRACK_SWITCHED, { id: trackId }); } } else { - //Lets announce that the initial audio track switch flush occur - this.audioSwitch=false; - hls.trigger(Event.AUDIO_TRACK_SWITCHED, {id : trackId}); + // Lets announce that the initial audio track switch flush occur + this.audioSwitch = false; + hls.trigger(Event.AUDIO_TRACK_SWITCHED, { id: trackId }); } } @@ -679,18 +668,16 @@ class AudioStreamController extends TaskLoop { if (!pendingData) { console.warn('Apparently attempt to enqueue media payload without codec initialization data upfront'); - hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: null, fatal: true}); + hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: null, fatal: true }); return; } - if(!this.audioSwitch) { + if (!this.audioSwitch) { [data.data1, data.data2].forEach(buffer => { - if (buffer && buffer.length) { - - pendingData.push({type: data.type, data: buffer, parent: 'audio', content: 'data'}); - } + if (buffer && buffer.length) + pendingData.push({ type: data.type, data: buffer, parent: 'audio', content: 'data' }); }); - if (!appendOnBufferFlush && pendingData.length) { + if (!appendOnBufferFlush && pendingData.length) { pendingData.forEach(appendObj => { // only append in PARSING state (rationale is that an appending error could happen synchronously on first segment appending) // in that case it is useless to append following segments @@ -704,12 +691,12 @@ class AudioStreamController extends TaskLoop { this.appended = true; } } - //trigger handler right now + // trigger handler right now this.tick(); } } - onFragParsed(data) { + onFragParsed (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; if (fragCurrent && @@ -723,24 +710,23 @@ class AudioStreamController extends TaskLoop { } } - onBufferReset() { + onBufferReset () { // reset reference to sourcebuffers this.mediaBuffer = this.videoBuffer = null; this.loadedmetadata = false; - } + } - onBufferCreated(data) { + onBufferCreated (data) { let audioTrack = data.tracks.audio; if (audioTrack) { this.mediaBuffer = audioTrack.buffer; this.loadedmetadata = true; } - if (data.tracks.video) { + if (data.tracks.video) this.videoBuffer = data.tracks.video.buffer; - } } - onBufferAppended(data) { + onBufferAppended (data) { if (data.parent === 'audio') { const state = this.state; if (state === State.PARSING || state === State.PARSED) { @@ -751,19 +737,19 @@ class AudioStreamController extends TaskLoop { } } - _checkAppendedParsed() { - //trigger handler right now - if (this.state === State.PARSED && (!this.appended || !this.pendingBuffering)) { + _checkAppendedParsed () { + // trigger handler right now + if (this.state === State.PARSED && (!this.appended || !this.pendingBuffering)) { let frag = this.fragCurrent, stats = this.stats, hls = this.hls; if (frag) { this.fragPrevious = frag; stats.tbuffered = performance.now(); - hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: frag, id : 'audio'}); + hls.trigger(Event.FRAG_BUFFERED, { stats: stats, frag: frag, id: 'audio' }); let media = this.mediaBuffer ? this.mediaBuffer : this.media; logger.log(`audio buffered : ${TimeRanges.toString(media.buffered)}`); if (this.audioSwitch && this.appended) { this.audioSwitch = false; - hls.trigger(Event.AUDIO_TRACK_SWITCHED, {id : this.trackId}); + hls.trigger(Event.AUDIO_TRACK_SWITCHED, { id: this.trackId }); } this.state = State.IDLE; } @@ -771,83 +757,83 @@ class AudioStreamController extends TaskLoop { } } - onError(data) { + onError (data) { let frag = data.frag; // don't handle frag error not related to audio fragment - if (frag && frag.type !== 'audio') { + if (frag && frag.type !== 'audio') return; - } - switch(data.details) { - case ErrorDetails.FRAG_LOAD_ERROR: - case ErrorDetails.FRAG_LOAD_TIMEOUT: - if(!data.fatal) { - var loadError = this.fragLoadError; - if(loadError) { - loadError++; - } else { - loadError=1; - } - let config = this.config; - if (loadError <= config.fragLoadingMaxRetry) { - this.fragLoadError = loadError; - // exponential backoff capped to config.fragLoadingMaxRetryTimeout - var delay = Math.min(Math.pow(2,loadError-1)*config.fragLoadingRetryDelay,config.fragLoadingMaxRetryTimeout); - logger.warn(`audioStreamController: frag loading failed, retry in ${delay} ms`); - this.retryDate = performance.now() + delay; - // retry loading state - this.state = State.FRAG_LOADING_WAITING_RETRY; - } else { - logger.error(`audioStreamController: ${data.details} reaches max retry, redispatch as fatal ...`); - // switch error to fatal - data.fatal = true; - this.state = State.ERROR; - } - } - break; - case ErrorDetails.AUDIO_TRACK_LOAD_ERROR: - case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT: - case ErrorDetails.KEY_LOAD_ERROR: - case ErrorDetails.KEY_LOAD_TIMEOUT: - // when in ERROR state, don't switch back to IDLE state in case a non-fatal error is received - if(this.state !== State.ERROR) { - // if fatal error, stop processing, otherwise move to IDLE to retry loading - this.state = data.fatal ? State.ERROR : State.IDLE; - logger.warn(`audioStreamController: ${data.details} while loading frag,switch to ${this.state} state ...`); + + switch (data.details) { + case ErrorDetails.FRAG_LOAD_ERROR: + case ErrorDetails.FRAG_LOAD_TIMEOUT: + if (!data.fatal) { + let loadError = this.fragLoadError; + if (loadError) + loadError++; + else + loadError = 1; + + let config = this.config; + if (loadError <= config.fragLoadingMaxRetry) { + this.fragLoadError = loadError; + // exponential backoff capped to config.fragLoadingMaxRetryTimeout + let delay = Math.min(Math.pow(2, loadError - 1) * config.fragLoadingRetryDelay, config.fragLoadingMaxRetryTimeout); + logger.warn(`audioStreamController: frag loading failed, retry in ${delay} ms`); + this.retryDate = performance.now() + delay; + // retry loading state + this.state = State.FRAG_LOADING_WAITING_RETRY; + } else { + logger.error(`audioStreamController: ${data.details} reaches max retry, redispatch as fatal ...`); + // switch error to fatal + data.fatal = true; + this.state = State.ERROR; } - break; - case ErrorDetails.BUFFER_FULL_ERROR: - // if in appending state - if (data.parent === 'audio' && (this.state === State.PARSING || this.state === State.PARSED)) { - const media = this.mediaBuffer, - currentTime = this.media.currentTime, - mediaBuffered = media && BufferHelper.isBuffered(media,currentTime) && BufferHelper.isBuffered(media,currentTime+0.5); + } + break; + case ErrorDetails.AUDIO_TRACK_LOAD_ERROR: + case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT: + case ErrorDetails.KEY_LOAD_ERROR: + case ErrorDetails.KEY_LOAD_TIMEOUT: + // when in ERROR state, don't switch back to IDLE state in case a non-fatal error is received + if (this.state !== State.ERROR) { + // if fatal error, stop processing, otherwise move to IDLE to retry loading + this.state = data.fatal ? State.ERROR : State.IDLE; + logger.warn(`audioStreamController: ${data.details} while loading frag,switch to ${this.state} state ...`); + } + break; + case ErrorDetails.BUFFER_FULL_ERROR: + // if in appending state + if (data.parent === 'audio' && (this.state === State.PARSING || this.state === State.PARSED)) { + const media = this.mediaBuffer, + currentTime = this.media.currentTime, + mediaBuffered = media && BufferHelper.isBuffered(media, currentTime) && BufferHelper.isBuffered(media, currentTime + 0.5); // reduce max buf len if current position is buffered - if (mediaBuffered) { - const config = this.config; - if(config.maxMaxBufferLength >= config.maxBufferLength) { - // reduce max buffer length as it might be too high. we do this to avoid loop flushing ... - config.maxMaxBufferLength/=2; - logger.warn(`audio:reduce max buffer length to ${config.maxMaxBufferLength}s`); - } - this.state = State.IDLE; - } else { - // current position is not buffered, but browser is still complaining about buffer full error - // this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708 - // in that case flush the whole audio buffer to recover - logger.warn('buffer full error also media.currentTime is not buffered, flush audio buffer'); - this.fragCurrent = null; - // flush everything - this.state = State.BUFFER_FLUSHING; - this.hls.trigger(Event.BUFFER_FLUSHING, {startOffset: 0 , endOffset: Number.POSITIVE_INFINITY, type : 'audio'}); + if (mediaBuffered) { + const config = this.config; + if (config.maxMaxBufferLength >= config.maxBufferLength) { + // reduce max buffer length as it might be too high. we do this to avoid loop flushing ... + config.maxMaxBufferLength /= 2; + logger.warn(`audio:reduce max buffer length to ${config.maxMaxBufferLength}s`); } + this.state = State.IDLE; + } else { + // current position is not buffered, but browser is still complaining about buffer full error + // this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708 + // in that case flush the whole audio buffer to recover + logger.warn('buffer full error also media.currentTime is not buffered, flush audio buffer'); + this.fragCurrent = null; + // flush everything + this.state = State.BUFFER_FLUSHING; + this.hls.trigger(Event.BUFFER_FLUSHING, { startOffset: 0, endOffset: Number.POSITIVE_INFINITY, type: 'audio' }); } - break; - default: - break; + } + break; + default: + break; } } - onBufferFlushed() { + onBufferFlushed () { let pendingData = this.pendingData; if (pendingData && pendingData.length) { logger.log('appending pending audio data on Buffer Flushed'); @@ -867,4 +853,3 @@ class AudioStreamController extends TaskLoop { } } export default AudioStreamController; - diff --git a/src/controller/audio-track-controller.js b/src/controller/audio-track-controller.js index ca9096fb689..228cfa6fc1d 100644 --- a/src/controller/audio-track-controller.js +++ b/src/controller/audio-track-controller.js @@ -4,68 +4,66 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {logger} from '../utils/logger'; -import {ErrorTypes} from '../errors'; +import { logger } from '../utils/logger'; +import { ErrorTypes } from '../errors'; class AudioTrackController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.MANIFEST_LOADING, - Event.MANIFEST_PARSED, - Event.AUDIO_TRACK_LOADED, - Event.ERROR); + Event.MANIFEST_PARSED, + Event.AUDIO_TRACK_LOADED, + Event.ERROR); this.ticks = 0; this.ontick = this.tick.bind(this); } - destroy() { + destroy () { this.cleanTimer(); EventHandler.prototype.destroy.call(this); } - cleanTimer() { + cleanTimer () { if (this.timer) { clearTimeout(this.timer); this.timer = null; } } - tick() { + tick () { this.ticks++; if (this.ticks === 1) { this.doTick(); - if (this.ticks > 1) { + if (this.ticks > 1) setTimeout(this.tick, 1); - } + this.ticks = 0; } } - doTick() { + doTick () { this.updateTrack(this.trackId); } - onError(data) { - if(data.fatal && data.type === ErrorTypes.NETWORK_ERROR) { + onError (data) { + if (data.fatal && data.type === ErrorTypes.NETWORK_ERROR) this.cleanTimer(); - } } - onManifestLoading() { + onManifestLoading () { // reset audio tracks on manifest loading this.tracks = []; this.trackId = -1; } - onManifestParsed(data) { + onManifestParsed (data) { let tracks = data.audioTracks || []; let defaultFound = false; this.tracks = tracks; - this.hls.trigger(Event.AUDIO_TRACKS_UPDATED, {audioTracks : tracks}); + this.hls.trigger(Event.AUDIO_TRACKS_UPDATED, { audioTracks: tracks }); // loop through available audio tracks and autoselect default if needed let id = 0; tracks.forEach(track => { - if(track.default && !defaultFound) { + if (track.default && !defaultFound) { this.audioTrack = id; defaultFound = true; return; @@ -78,7 +76,7 @@ class AudioTrackController extends EventHandler { } } - onAudioTrackLoaded(data) { + onAudioTrackLoaded (data) { if (data.id < this.tracks.length) { logger.log(`audioTrack ${data.id} loaded`); this.tracks[data.id].details = data.details; @@ -96,23 +94,22 @@ class AudioTrackController extends EventHandler { } /** get alternate audio tracks list from playlist **/ - get audioTracks() { + get audioTracks () { return this.tracks; } /** get index of the selected audio track (index in audio track lists) **/ - get audioTrack() { - return this.trackId; + get audioTrack () { + return this.trackId; } /** select an audio track, based on its index in audio track lists**/ - set audioTrack(audioTrackId) { - if (this.trackId !== audioTrackId || this.tracks[audioTrackId].details === undefined) { + set audioTrack (audioTrackId) { + if (this.trackId !== audioTrackId || this.tracks[audioTrackId].details === undefined) this.setAudioTrackInternal(audioTrackId); - } } - setAudioTrackInternal(newId) { + setAudioTrackInternal (newId) { // check if level idx is valid if (newId >= 0 && newId < this.tracks.length) { // stopping live reloading timer if any @@ -120,24 +117,24 @@ class AudioTrackController extends EventHandler { this.trackId = newId; logger.log(`switching to audioTrack ${newId}`); let audioTrack = this.tracks[newId], - hls = this.hls, - type = audioTrack.type, - url = audioTrack.url, - eventObj = {id: newId, type : type, url : url}; + hls = this.hls, + type = audioTrack.type, + url = audioTrack.url, + eventObj = { id: newId, type: type, url: url }; // keep AUDIO_TRACK_SWITCH for legacy reason hls.trigger(Event.AUDIO_TRACK_SWITCH, eventObj); hls.trigger(Event.AUDIO_TRACK_SWITCHING, eventObj); - // check if we need to load playlist for this audio Track - let details = audioTrack.details; + // check if we need to load playlist for this audio Track + let details = audioTrack.details; if (url && (details === undefined || details.live === true)) { // track not retrieved yet, or live playlist we need to (re)load it logger.log(`(re)loading playlist for audioTrack ${newId}`); - hls.trigger(Event.AUDIO_TRACK_LOADING, {url: url, id: newId}); + hls.trigger(Event.AUDIO_TRACK_LOADING, { url: url, id: newId }); } } } - updateTrack(newId) { + updateTrack (newId) { // check if level idx is valid if (newId >= 0 && newId < this.tracks.length) { // stopping live reloading timer if any @@ -145,12 +142,12 @@ class AudioTrackController extends EventHandler { this.trackId = newId; logger.log(`updating audioTrack ${newId}`); let audioTrack = this.tracks[newId], url = audioTrack.url; - // check if we need to load playlist for this audio Track - let details = audioTrack.details; + // check if we need to load playlist for this audio Track + let details = audioTrack.details; if (url && (details === undefined || details.live === true)) { // track not retrieved yet, or live playlist we need to (re)load it logger.log(`(re)loading playlist for audioTrack ${newId}`); - this.hls.trigger(Event.AUDIO_TRACK_LOADING, {url: url, id: newId}); + this.hls.trigger(Event.AUDIO_TRACK_LOADING, { url: url, id: newId }); } } } diff --git a/src/controller/buffer-controller.js b/src/controller/buffer-controller.js index 29ae4143cb6..1a45f464ffc 100644 --- a/src/controller/buffer-controller.js +++ b/src/controller/buffer-controller.js @@ -4,15 +4,14 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {logger} from '../utils/logger'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {getMediaSource} from '../helper/mediasource-helper'; +import { logger } from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { getMediaSource } from '../helper/mediasource-helper'; const MediaSource = getMediaSource(); class BufferController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.MEDIA_ATTACHING, Event.MEDIA_DETACHING, @@ -37,16 +36,16 @@ class BufferController extends EventHandler { // Source Buffer listeners this.onsbue = this.onSBUpdateEnd.bind(this); - this.onsbe = this.onSBUpdateError.bind(this); + this.onsbe = this.onSBUpdateError.bind(this); this.pendingTracks = {}; this.tracks = {}; } - destroy() { + destroy () { EventHandler.prototype.destroy.call(this); } - onLevelPtsUpdated(data) { + onLevelPtsUpdated (data) { let type = data.type; let audioTrack = this.tracks.audio; @@ -82,10 +81,10 @@ class BufferController extends EventHandler { } } - onManifestParsed(data) { + onManifestParsed (data) { let audioExpected = data.audio, - videoExpected = data.video || (data.levels.length && data.audio), - sourceBufferNb = 0; + videoExpected = data.video || (data.levels.length && data.audio), + sourceBufferNb = 0; // in case of alt audio 2 BUFFER_CODECS events will be triggered, one per stream controller // sourcebuffers will be created all at once when the expected nb of tracks will be reached // in case alt audio is not used, only one BUFFER_CODEC event will be fired from main stream controller @@ -97,12 +96,12 @@ class BufferController extends EventHandler { this.sourceBufferNb = sourceBufferNb; } - onMediaAttaching(data) { + onMediaAttaching (data) { let media = this.media = data.media; if (media) { // setup the media source - var ms = this.mediaSource = new MediaSource(); - //Media Source listeners + let ms = this.mediaSource = new MediaSource(); + // Media Source listeners this.onmso = this.onMediaSourceOpen.bind(this); this.onmse = this.onMediaSourceEnded.bind(this); this.onmsc = this.onMediaSourceClose.bind(this); @@ -116,9 +115,9 @@ class BufferController extends EventHandler { } } - onMediaDetaching() { + onMediaDetaching () { logger.log('media source detaching'); - var ms = this.mediaSource; + let ms = this.mediaSource; if (ms) { if (ms.readyState === 'open') { try { @@ -127,7 +126,7 @@ class BufferController extends EventHandler { // as we are anyway detaching the MediaSource // let's just avoid this exception to propagate ms.endOfStream(); - } catch(err) { + } catch (err) { logger.warn(`onMediaDetaching:${err.message} while calling endOfStream`); } } @@ -164,9 +163,9 @@ class BufferController extends EventHandler { this.hls.trigger(Event.MEDIA_DETACHED); } - onMediaSourceOpen() { + onMediaSourceOpen () { logger.log('media source opened'); - this.hls.trigger(Event.MEDIA_ATTACHED, { media : this.media }); + this.hls.trigger(Event.MEDIA_ATTACHED, { media: this.media }); let mediaSource = this.mediaSource; if (mediaSource) { // once received, don't listen anymore to sourceopen event @@ -175,13 +174,13 @@ class BufferController extends EventHandler { this.checkPendingTracks(); } - checkPendingTracks() { + checkPendingTracks () { // if any buffer codecs pending, check if we have enough to create sourceBuffers let pendingTracks = this.pendingTracks, - pendingTracksNb = Object.keys(pendingTracks).length; + pendingTracksNb = Object.keys(pendingTracks).length; // if any pending tracks and (if nb of pending tracks gt or equal than expected nb or if unknown expected nb) if (pendingTracksNb && ( - this.sourceBufferNb <= pendingTracksNb || + this.sourceBufferNb <= pendingTracksNb || this.sourceBufferNb === 0)) { // ok, let's create them now ! this.createSourceBuffers(pendingTracks); @@ -191,16 +190,15 @@ class BufferController extends EventHandler { } } - onMediaSourceClose() { + onMediaSourceClose () { logger.log('media source closed'); } - onMediaSourceEnded() { + onMediaSourceEnded () { logger.log('media source ended'); } - - onSBUpdateEnd() { + onSBUpdateEnd () { // update timestampOffset if (this.audioTimestampOffset) { let audioBuffer = this.sourceBuffer.audio; @@ -209,52 +207,49 @@ class BufferController extends EventHandler { delete this.audioTimestampOffset; } - if (this._needsFlush) { + if (this._needsFlush) this.doFlush(); - } - if (this._needsEos) { + if (this._needsEos) this.checkEos(); - } + this.appending = false; let parent = this.parent; // count nb of pending segments waiting for appending on this sourcebuffer - let pending = this.segments.reduce( (counter, segment) => (segment.parent === parent) ? counter + 1 : counter , 0); + let pending = this.segments.reduce((counter, segment) => (segment.parent === parent) ? counter + 1 : counter, 0); // this.sourceBuffer is better to use than media.buffered as it is closer to the PTS data from the fragments let timeRanges = {}; const sourceBuffer = this.sourceBuffer; - for (let streamType in sourceBuffer) { + for (let streamType in sourceBuffer) timeRanges[streamType] = sourceBuffer[streamType].buffered; - } this.hls.trigger(Event.BUFFER_APPENDED, { parent, pending, timeRanges }); // don't append in flushing mode - if (!this._needsFlush) { + if (!this._needsFlush) this.doAppending(); - } this.updateMediaElementDuration(); } - onSBUpdateError(event) { + onSBUpdateError (event) { logger.error('sourceBuffer error:', event); // according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error // this error might not always be fatal (it is fatal if decode error is set, in that case // it will be followed by a mediaElement error ...) - this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false }); // we don't need to do more than that, as accordin to the spec, updateend will be fired just after } - onBufferReset() { - var sourceBuffer = this.sourceBuffer; - for(var type in sourceBuffer) { - var sb = sourceBuffer[type]; + onBufferReset () { + let sourceBuffer = this.sourceBuffer; + for (let type in sourceBuffer) { + let sb = sourceBuffer[type]; try { this.mediaSource.removeSourceBuffer(sb); sb.removeEventListener('updateend', this.onsbue); sb.removeEventListener('error', this.onsbe); - } catch(err) { + } catch (err) { } } this.sourceBuffer = {}; @@ -263,11 +258,11 @@ class BufferController extends EventHandler { this.appended = 0; } - onBufferCodecs(tracks) { + onBufferCodecs (tracks) { // if source buffer(s) not created yet, appended buffer tracks in this.pendingTracks // if sourcebuffers already created, do nothing ... if (Object.keys(this.sourceBuffer).length === 0) { - for (var trackName in tracks) { this.pendingTracks[trackName] = tracks[trackName]; } + for (let trackName in tracks) this.pendingTracks[trackName] = tracks[trackName]; let mediaSource = this.mediaSource; if (mediaSource && mediaSource.readyState === 'open') { // try to create sourcebuffers if mediasource opened @@ -276,12 +271,11 @@ class BufferController extends EventHandler { } } - - createSourceBuffers(tracks) { - var sourceBuffer = this.sourceBuffer,mediaSource = this.mediaSource; + createSourceBuffers (tracks) { + let sourceBuffer = this.sourceBuffer, mediaSource = this.mediaSource; for (let trackName in tracks) { - if(!sourceBuffer[trackName]) { + if (!sourceBuffer[trackName]) { let track = tracks[trackName]; // use levelCodec as first priority let codec = track.levelCodec || track.codec; @@ -291,41 +285,41 @@ class BufferController extends EventHandler { let sb = sourceBuffer[trackName] = mediaSource.addSourceBuffer(mimeType); sb.addEventListener('updateend', this.onsbue); sb.addEventListener('error', this.onsbe); - this.tracks[trackName] = {codec: codec, container: track.container}; + this.tracks[trackName] = { codec: codec, container: track.container }; track.buffer = sb; - } catch(err) { + } catch (err) { logger.error(`error while trying to add sourceBuffer:${err.message}`); - this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_ADD_CODEC_ERROR, fatal: false, err: err, mimeType : mimeType}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_ADD_CODEC_ERROR, fatal: false, err: err, mimeType: mimeType }); } } } - this.hls.trigger(Event.BUFFER_CREATED, { tracks : tracks } ); + this.hls.trigger(Event.BUFFER_CREATED, { tracks: tracks }); } - onBufferAppending(data) { + onBufferAppending (data) { if (!this._needsFlush) { - if (!this.segments) { + if (!this.segments) this.segments = [ data ]; - } else { + else this.segments.push(data); - } + this.doAppending(); } } - onBufferAppendFail(data) { - logger.error('sourceBuffer error:',data.event); + onBufferAppendFail (data) { + logger.error('sourceBuffer error:', data.event); // according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error // this error might not always be fatal (it is fatal if decode error is set, in that case // it will be followed by a mediaElement error ...) - this.hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_APPENDING_ERROR, fatal: false }); } // on BUFFER_EOS mark matching sourcebuffer(s) as ended and trigger checkEos() - onBufferEos(data) { - var sb = this.sourceBuffer; + onBufferEos (data) { + let sb = this.sourceBuffer; let dataType = data.type; - for(let type in sb) { + for (let type in sb) { if (!dataType || type === dataType) { if (!sb[type].ended) { sb[type].ended = true; @@ -336,42 +330,41 @@ class BufferController extends EventHandler { this.checkEos(); } - // if all source buffers are marked as ended, signal endOfStream() to MediaSource. - checkEos() { - var sb = this.sourceBuffer, mediaSource = this.mediaSource; + // if all source buffers are marked as ended, signal endOfStream() to MediaSource. + checkEos () { + let sb = this.sourceBuffer, mediaSource = this.mediaSource; if (!mediaSource || mediaSource.readyState !== 'open') { this._needsEos = false; return; } - for(let type in sb) { + for (let type in sb) { let sbobj = sb[type]; - if (!sbobj.ended) { + if (!sbobj.ended) return; - } - if(sbobj.updating) { + + if (sbobj.updating) { this._needsEos = true; return; } } logger.log('all media data available, signal endOfStream() to MediaSource and stop loading fragment'); - //Notify the media element that it now has all of the media data + // Notify the media element that it now has all of the media data try { mediaSource.endOfStream(); - } catch(e) { + } catch (e) { logger.warn('exception while calling mediaSource.endOfStream()'); } this._needsEos = false; - } - + } - onBufferFlushing(data) { - this.flushRange.push({start: data.startOffset, end: data.endOffset, type : data.type}); + onBufferFlushing (data) { + this.flushRange.push({ start: data.startOffset, end: data.endOffset, type: data.type }); // attempt flush immediately this.flushBufferCounter = 0; this.doFlush(); } - onLevelUpdated({details}) { + onLevelUpdated ({ details }) { if (details.fragments.length > 0) { this._levelDuration = details.totalduration + details.fragments[0].start; this._live = details.live; @@ -384,8 +377,8 @@ class BufferController extends EventHandler { * 'liveDurationInfinity` is set to `true` * More details: https://github.com/video-dev/hls.js/issues/355 */ - updateMediaElementDuration() { - let {config} = this.hls; + updateMediaElementDuration () { + let { config } = this.hls; let duration; if (this._levelDuration === null || @@ -393,9 +386,8 @@ class BufferController extends EventHandler { !this.mediaSource || !this.sourceBuffer || this.media.readyState === 0 || - this.mediaSource.readyState !== 'open') { + this.mediaSource.readyState !== 'open') return; - } for (let type in this.sourceBuffer) { if (this.sourceBuffer[type].updating === true) { @@ -406,16 +398,15 @@ class BufferController extends EventHandler { duration = this.media.duration; // initialise to the value that the media source is reporting - if (this._msDuration === null) { + if (this._msDuration === null) this._msDuration = this.mediaSource.duration; - } if (this._live === true && config.liveDurationInfinity === true) { // Override duration to Infinity logger.log('Media Source duration is set to Infinity'); this._msDuration = this.mediaSource.duration = Infinity; } else if ((this._levelDuration > this._msDuration && this._levelDuration > duration) || - (duration === Infinity || isNaN(duration) )) { + (duration === Infinity || isNaN(duration))) { // levelDuration was the last value we set. // not using mediaSource.duration as the browser may tweak this value // only update Media Source duration if its value increase, this is to avoid @@ -425,10 +416,10 @@ class BufferController extends EventHandler { } } - doFlush() { + doFlush () { // loop through all buffer ranges to flush - while(this.flushRange.length) { - var range = this.flushRange[0]; + while (this.flushRange.length) { + let range = this.flushRange[0]; // flushBuffer will abort any buffer append in progress and flush Audio/Video Buffer if (this.flushBuffer(range.start, range.end, range.type)) { // range flushed, remove from flush array @@ -445,13 +436,12 @@ class BufferController extends EventHandler { this._needsFlush = false; // let's recompute this.appended, which is used to avoid flush looping - var appended = 0; - var sourceBuffer = this.sourceBuffer; + let appended = 0; + let sourceBuffer = this.sourceBuffer; try { - for (var type in sourceBuffer) { + for (let type in sourceBuffer) appended += sourceBuffer[type].buffered.length; - } - } catch(error) { + } catch (error) { // error could be thrown while accessing buffered, in case sourcebuffer has already been removed from MediaSource // this is harmess at this stage, catch this to avoid reporting an internal exception logger.error('error while accessing sourceBuffer.buffered'); @@ -461,8 +451,8 @@ class BufferController extends EventHandler { } } - doAppending() { - var hls = this.hls, sourceBuffer = this.sourceBuffer, segments = this.segments; + doAppending () { + let hls = this.hls, sourceBuffer = this.sourceBuffer, segments = this.segments; if (Object.keys(sourceBuffer).length) { if (this.media.error) { this.segments = []; @@ -470,18 +460,18 @@ class BufferController extends EventHandler { return; } if (this.appending) { - //logger.log(`sb appending in progress`); + // logger.log(`sb appending in progress`); return; } if (segments && segments.length) { let segment = segments.shift(); try { let type = segment.type, sb = sourceBuffer[type]; - if(sb) { - if(!sb.updating) { + if (sb) { + if (!sb.updating) { // reset sourceBuffer ended flag before appending segment sb.ended = false; - //logger.log(`appending ${segment.content} ${type} SB, size:${segment.data.length}, ${segment.parent}`); + // logger.log(`appending ${segment.content} ${type} SB, size:${segment.data.length}, ${segment.parent}`); this.parent = segment.parent; sb.appendBuffer(segment.data); this.appendError = 0; @@ -496,17 +486,17 @@ class BufferController extends EventHandler { // discard this segment, and trigger update end this.onSBUpdateEnd(); } - } catch(err) { + } catch (err) { // in case any error occured while appending, put back segment in segments table logger.error(`error while trying to append buffer:${err.message}`); segments.unshift(segment); - var event = {type: ErrorTypes.MEDIA_ERROR, parent : segment.parent}; - if(err.code !== 22) { - if (this.appendError) { + let event = { type: ErrorTypes.MEDIA_ERROR, parent: segment.parent }; + if (err.code !== 22) { + if (this.appendError) this.appendError++; - } else { + else this.appendError = 1; - } + event.details = ErrorDetails.BUFFER_APPEND_ERROR; /* with UHD content, we could get loop of quota exceeded error until browser is able to evict some data from sourcebuffer. retrying help recovering this @@ -516,7 +506,6 @@ class BufferController extends EventHandler { segments = []; event.fatal = true; hls.trigger(Event.ERROR, event); - return; } else { event.fatal = false; hls.trigger(Event.ERROR, event); @@ -527,8 +516,7 @@ class BufferController extends EventHandler { this.segments = []; event.details = ErrorDetails.BUFFER_FULL_ERROR; event.fatal = false; - hls.trigger(Event.ERROR,event); - return; + hls.trigger(Event.ERROR, event); } } } @@ -540,18 +528,18 @@ class BufferController extends EventHandler { return true once range has been flushed. as sourceBuffer.remove() is asynchronous, flushBuffer will be retriggered on sourceBuffer update end */ - flushBuffer(startOffset, endOffset, typeIn) { - var sb, i, bufStart, bufEnd, flushStart, flushEnd, sourceBuffer = this.sourceBuffer; + flushBuffer (startOffset, endOffset, typeIn) { + let sb, i, bufStart, bufEnd, flushStart, flushEnd, sourceBuffer = this.sourceBuffer; if (Object.keys(sourceBuffer).length) { logger.log(`flushBuffer,pos/start/end: ${this.media.currentTime.toFixed(3)}/${startOffset}/${endOffset}`); // safeguard to avoid infinite looping : don't try to flush more than the nb of appended segments if (this.flushBufferCounter < this.appended) { - for (var type in sourceBuffer) { + for (let type in sourceBuffer) { // check if sourcebuffer type is defined (typeIn): if yes, let's only flush this one // if no, let's flush all sourcebuffers - if (typeIn && type !== typeIn) { + if (typeIn && type !== typeIn) continue; - } + sb = sourceBuffer[type]; // we are going to flush buffer, mark source buffer as 'not ended' sb.ended = false; @@ -573,20 +561,20 @@ class BufferController extends EventHandler { to avoid rounding issues/infinite loop, only flush buffer range of length greater than 500ms. */ - if (Math.min(flushEnd,bufEnd) - flushStart > 0.5 ) { + if (Math.min(flushEnd, bufEnd) - flushStart > 0.5) { this.flushBufferCounter++; logger.log(`flush ${type} [${flushStart},${flushEnd}], of [${bufStart},${bufEnd}], pos:${this.media.currentTime}`); sb.remove(flushStart, flushEnd); return false; } } - } catch(e) { + } catch (e) { logger.warn('exception while accessing sourcebuffer, it might have been removed from MediaSource'); } } else { - //logger.log('abort ' + type + ' append in progress'); + // logger.log('abort ' + type + ' append in progress'); // this will abort any appending in progress - //sb.abort(); + // sb.abort(); logger.warn('cannot flush, sb updating in progress'); return false; } diff --git a/src/controller/cap-level-controller.js b/src/controller/cap-level-controller.js index d21cfbfd116..7f55514deb7 100644 --- a/src/controller/cap-level-controller.js +++ b/src/controller/cap-level-controller.js @@ -6,35 +6,33 @@ import Event from '../events'; import EventHandler from '../event-handler'; class CapLevelController extends EventHandler { - constructor(hls) { + constructor (hls) { super(hls, Event.FPS_DROP_LEVEL_CAPPING, Event.MEDIA_ATTACHING, Event.MANIFEST_PARSED); - } + } - destroy() { + destroy () { if (this.hls.config.capLevelToPlayerSize) { this.media = this.restrictedLevels = null; this.autoLevelCapping = Number.POSITIVE_INFINITY; - if (this.timer) { + if (this.timer) this.timer = clearInterval(this.timer); - } } } - onFpsDropLevelCapping(data) { - // Don't add a restricted level more than once - if (CapLevelController.isLevelAllowed(data.droppedLevel, this.restrictedLevels)) { + onFpsDropLevelCapping (data) { + // Don't add a restricted level more than once + if (CapLevelController.isLevelAllowed(data.droppedLevel, this.restrictedLevels)) this.restrictedLevels.push(data.droppedLevel); - } } - onMediaAttaching(data) { + onMediaAttaching (data) { this.media = data.media instanceof HTMLVideoElement ? data.media : null; } - onManifestParsed(data) { + onManifestParsed (data) { const hls = this.hls; this.restrictedLevels = []; if (hls.config.capLevelToPlayerSize) { @@ -47,7 +45,7 @@ class CapLevelController extends EventHandler { } } - detectPlayerSize() { + detectPlayerSize () { if (this.media) { let levelsLength = this.levels ? this.levels.length : 0; if (levelsLength) { @@ -66,10 +64,9 @@ class CapLevelController extends EventHandler { /* * returns level should be the one with the dimensions equal or greater than the media (player) dimensions (so the video will be downscaled) */ - getMaxLevel(capLevelIndex) { - if (!this.levels) { + getMaxLevel (capLevelIndex) { + if (!this.levels) return -1; - } const validLevels = this.levels.filter((level, index) => CapLevelController.isLevelAllowed(index, this.restrictedLevels) && index <= capLevelIndex @@ -78,7 +75,7 @@ class CapLevelController extends EventHandler { return CapLevelController.getMaxLevelByMediaSize(validLevels, this.mediaWidth, this.mediaHeight); } - get mediaWidth() { + get mediaWidth () { let width; const media = this.media; if (media) { @@ -88,7 +85,7 @@ class CapLevelController extends EventHandler { return width; } - get mediaHeight() { + get mediaHeight () { let height; const media = this.media; if (media) { @@ -98,29 +95,28 @@ class CapLevelController extends EventHandler { return height; } - static get contentScaleFactor() { + static get contentScaleFactor () { let pixelRatio = 1; try { - pixelRatio = window.devicePixelRatio; - } catch(e) {} + pixelRatio = window.devicePixelRatio; + } catch (e) {} return pixelRatio; } - static isLevelAllowed(level, restrictedLevels = []) { + static isLevelAllowed (level, restrictedLevels = []) { return restrictedLevels.indexOf(level) === -1; } - static getMaxLevelByMediaSize(levels, width, height) { - if (!levels || (levels && !levels.length)) { + static getMaxLevelByMediaSize (levels, width, height) { + if (!levels || (levels && !levels.length)) return -1; - } // Levels can have the same dimensions but differing bandwidths - since levels are ordered, we can look to the next // to determine whether we've chosen the greatest bandwidth for the media's dimensions const atGreatestBandiwdth = (curLevel, nextLevel) => { - if (!nextLevel) { + if (!nextLevel) return true; - } + return curLevel.width !== nextLevel.width || curLevel.height !== nextLevel.height; }; @@ -128,7 +124,7 @@ class CapLevelController extends EventHandler { // the max level let maxLevelIndex = levels.length - 1; - for (let i = 0; i < levels.length; i+= 1) { + for (let i = 0; i < levels.length; i += 1) { const level = levels[i]; if ((level.width >= width || level.height >= height) && atGreatestBandiwdth(level, levels[i + 1])) { maxLevelIndex = i; diff --git a/src/controller/eme-controller.js b/src/controller/eme-controller.js index 932fd98d703..803ef46eca3 100644 --- a/src/controller/eme-controller.js +++ b/src/controller/eme-controller.js @@ -6,9 +6,9 @@ import EventHandler from '../event-handler'; import Event from '../events'; -import {ErrorTypes, ErrorDetails} from '../errors'; +import { ErrorTypes, ErrorDetails } from '../errors'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; const MAX_LICENSE_REQUEST_FAILURES = 3; @@ -28,27 +28,27 @@ const KeySystems = { * @returns {Array} An array of supported configurations */ -const createWidevineMediaKeySystemConfigurations = function(audioCodecs, videoCodecs, drmSystemOptions) { /* jshint ignore:line */ - const baseConfig = { - //initDataTypes: ['keyids', 'mp4'], - //label: "", - //persistentState: "not-allowed", // or "required" ? - //distinctiveIdentifier: "not-allowed", // or "required" ? - //sessionTypes: ['temporary'], - videoCapabilities: [ - //{ contentType: 'video/mp4; codecs="avc1.42E01E"' } - ] - }; - - videoCodecs.forEach((codec) => { - baseConfig.videoCapabilities.push({ - contentType: `video/mp4; codecs="${codec}"` - }); +const createWidevineMediaKeySystemConfigurations = function (audioCodecs, videoCodecs, drmSystemOptions) { /* jshint ignore:line */ + const baseConfig = { + // initDataTypes: ['keyids', 'mp4'], + // label: "", + // persistentState: "not-allowed", // or "required" ? + // distinctiveIdentifier: "not-allowed", // or "required" ? + // sessionTypes: ['temporary'], + videoCapabilities: [ + // { contentType: 'video/mp4; codecs="avc1.42E01E"' } + ] + }; + + videoCodecs.forEach((codec) => { + baseConfig.videoCapabilities.push({ + contentType: `video/mp4; codecs="${codec}"` }); + }); - return [ - baseConfig - ]; + return [ + baseConfig + ]; }; /** @@ -62,13 +62,13 @@ const createWidevineMediaKeySystemConfigurations = function(audioCodecs, videoCo * @param {Array} videoCodecs List of required video codecs to support * @returns {Array | null} A non-empty Array of MediaKeySystemConfiguration objects or `null` */ -const getSupportedMediaKeySystemConfigurations = function(keySystem, audioCodecs, videoCodecs) { - switch(keySystem) { - case KeySystems.WIDEVINE: - return createWidevineMediaKeySystemConfigurations(audioCodecs, videoCodecs); - default: - throw Error('Unknown key-system: ' + keySystem); - } +const getSupportedMediaKeySystemConfigurations = function (keySystem, audioCodecs, videoCodecs) { + switch (keySystem) { + case KeySystems.WIDEVINE: + return createWidevineMediaKeySystemConfigurations(audioCodecs, videoCodecs); + default: + throw Error('Unknown key-system: ' + keySystem); + } }; /** @@ -79,343 +79,331 @@ const getSupportedMediaKeySystemConfigurations = function(keySystem, audioCodecs * @constructor */ class EMEController extends EventHandler { - - /** + /** * @constructs * @param {Hls} hls Our Hls.js instance */ - constructor(hls) { - super(hls, - Event.MEDIA_ATTACHED, - Event.MANIFEST_PARSED - ); + constructor (hls) { + super(hls, + Event.MEDIA_ATTACHED, + Event.MANIFEST_PARSED + ); - this._widevineLicenseUrl = hls.config.widevineLicenseUrl; - this._licenseXhrSetup = hls.config.licenseXhrSetup; - this._emeEnabled = hls.config.emeEnabled; + this._widevineLicenseUrl = hls.config.widevineLicenseUrl; + this._licenseXhrSetup = hls.config.licenseXhrSetup; + this._emeEnabled = hls.config.emeEnabled; - this._requestMediaKeySystemAccess = hls.config.requestMediaKeySystemAccessFunc; + this._requestMediaKeySystemAccess = hls.config.requestMediaKeySystemAccessFunc; - this._mediaKeysList = []; - this._media = null; + this._mediaKeysList = []; + this._media = null; - this._hasSetMediaKeys = false; - this._isMediaEncrypted = false; + this._hasSetMediaKeys = false; + this._isMediaEncrypted = false; - this._requestLicenseFailureCount = 0; - } + this._requestLicenseFailureCount = 0; + } - /** + /** * * @param {string} keySystem Identifier for the key-system, see `KeySystems` enum * @returns {string} License server URL for key-system (if any configured, otherwise causes error) */ - getLicenseServerUrl(keySystem) { - let url; - switch(keySystem) { - case KeySystems.WIDEVINE: - url = this._widevineLicenseUrl; - break; - default: - url = null; - break; - } - - if (!url) { - logger.error(`No license server URL configured for key-system "${keySystem}"`); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, - fatal: true - }); - } + getLicenseServerUrl (keySystem) { + let url; + switch (keySystem) { + case KeySystems.WIDEVINE: + url = this._widevineLicenseUrl; + break; + default: + url = null; + break; + } - return url; + if (!url) { + logger.error(`No license server URL configured for key-system "${keySystem}"`); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, + fatal: true + }); } - /** + return url; + } + + /** * Requests access object and adds it to our list upon success * @private * @param {string} keySystem System ID (see `KeySystems`) * @param {Array} audioCodecs List of required audio codecs to support * @param {Array} videoCodecs List of required video codecs to support */ - _attemptKeySystemAccess(keySystem, audioCodecs, videoCodecs) { + _attemptKeySystemAccess (keySystem, audioCodecs, videoCodecs) { + // TODO: add other DRM "options" - // TODO: add other DRM "options" + const mediaKeySystemConfigs = getSupportedMediaKeySystemConfigurations(keySystem, audioCodecs, videoCodecs); - const mediaKeySystemConfigs = getSupportedMediaKeySystemConfigurations(keySystem, audioCodecs, videoCodecs); + if (!mediaKeySystemConfigs) { + logger.warn('Can not create config for key-system (maybe because platform is not supported):', keySystem); + return; + } - if (!mediaKeySystemConfigs) { - logger.warn('Can not create config for key-system (maybe because platform is not supported):', keySystem); - return; - } + logger.log('Requesting encrypted media key-system access'); - logger.log('Requesting encrypted media key-system access'); + // expecting interface like window.navigator.requestMediaKeySystemAccess + this.requestMediaKeySystemAccess(keySystem, mediaKeySystemConfigs) + .then((mediaKeySystemAccess) => { + this._onMediaKeySystemAccessObtained(keySystem, mediaKeySystemAccess); + }) + .catch((err) => { + logger.error(`Failed to obtain key-system "${keySystem}" access:`, err); + }); + } - // expecting interface like window.navigator.requestMediaKeySystemAccess - this.requestMediaKeySystemAccess(keySystem, mediaKeySystemConfigs) - .then((mediaKeySystemAccess) => { - this._onMediaKeySystemAccessObtained(keySystem, mediaKeySystemAccess); - }) - .catch((err) => { - logger.error(`Failed to obtain key-system "${keySystem}" access:`, err); - }); - } + get requestMediaKeySystemAccess () { + if (!this._requestMediaKeySystemAccess) + throw new Error('No requestMediaKeySystemAccess function configured'); - get requestMediaKeySystemAccess() { - if (!this._requestMediaKeySystemAccess) { - throw new Error('No requestMediaKeySystemAccess function configured'); - } - return this._requestMediaKeySystemAccess; - } + return this._requestMediaKeySystemAccess; + } - /** + /** * Handles obtaining access to a key-system * * @param {string} keySystem * @param {MediaKeySystemAccess} mediaKeySystemAccess https://developer.mozilla.org/en-US/docs/Web/API/MediaKeySystemAccess */ - _onMediaKeySystemAccessObtained(keySystem, mediaKeySystemAccess) { - - logger.log(`Access for key-system "${keySystem}" obtained`); - - const mediaKeysListItem = { - mediaKeys: null, - mediaKeysSession: null, - mediaKeysSessionInitialized: false, - mediaKeySystemAccess: mediaKeySystemAccess, - mediaKeySystemDomain: keySystem - }; + _onMediaKeySystemAccessObtained (keySystem, mediaKeySystemAccess) { + logger.log(`Access for key-system "${keySystem}" obtained`); + + const mediaKeysListItem = { + mediaKeys: null, + mediaKeysSession: null, + mediaKeysSessionInitialized: false, + mediaKeySystemAccess: mediaKeySystemAccess, + mediaKeySystemDomain: keySystem + }; - this._mediaKeysList.push(mediaKeysListItem); + this._mediaKeysList.push(mediaKeysListItem); - mediaKeySystemAccess.createMediaKeys() - .then((mediaKeys) => { - mediaKeysListItem.mediaKeys = mediaKeys; + mediaKeySystemAccess.createMediaKeys() + .then((mediaKeys) => { + mediaKeysListItem.mediaKeys = mediaKeys; - logger.log(`Media-keys created for key-system "${keySystem}"`); + logger.log(`Media-keys created for key-system "${keySystem}"`); - this._onMediaKeysCreated(); - }) - .catch((err) => { - logger.error('Failed to create media-keys:', err); - }); - } + this._onMediaKeysCreated(); + }) + .catch((err) => { + logger.error('Failed to create media-keys:', err); + }); + } - /** + /** * Handles key-creation (represents access to CDM). We are going to create key-sessions upon this * for all existing keys where no session exists yet. */ - _onMediaKeysCreated() { - - // check for all key-list items if a session exists, otherwise, create one - this._mediaKeysList.forEach((mediaKeysListItem) => { - if(!mediaKeysListItem.mediaKeysSession) { - mediaKeysListItem.mediaKeysSession = mediaKeysListItem.mediaKeys.createSession(); - this._onNewMediaKeySession(mediaKeysListItem.mediaKeysSession); - } - }); - } + _onMediaKeysCreated () { + // check for all key-list items if a session exists, otherwise, create one + this._mediaKeysList.forEach((mediaKeysListItem) => { + if (!mediaKeysListItem.mediaKeysSession) { + mediaKeysListItem.mediaKeysSession = mediaKeysListItem.mediaKeys.createSession(); + this._onNewMediaKeySession(mediaKeysListItem.mediaKeysSession); + } + }); + } - /** + /** * * @param {*} keySession */ - _onNewMediaKeySession(keySession) { - logger.log(`New key-system session ${keySession.sessionId}`); + _onNewMediaKeySession (keySession) { + logger.log(`New key-system session ${keySession.sessionId}`); - keySession.addEventListener('message', (event) => { - this._onKeySessionMessage(keySession, event.message); - }, false); - } + keySession.addEventListener('message', (event) => { + this._onKeySessionMessage(keySession, event.message); + }, false); + } + + _onKeySessionMessage (keySession, message) { + logger.log('Got EME message event, creating license request'); - _onKeySessionMessage(keySession, message) { + this._requestLicense(message, (data) => { + logger.log('Received license data, updating key-session'); + keySession.update(data); + }); + } - logger.log(`Got EME message event, creating license request`); + _onMediaEncrypted (initDataType, initData) { + logger.log(`Media is encrypted using "${initDataType}" init data type`); - this._requestLicense(message, (data) => { - logger.log('Received license data, updating key-session'); - keySession.update(data); - }); - } + this._isMediaEncrypted = true; + this._mediaEncryptionInitDataType = initDataType; + this._mediaEncryptionInitData = initData; - _onMediaEncrypted(initDataType, initData) { + this._attemptSetMediaKeys(); + this._generateRequestWithPreferredKeySession(); + } - logger.log(`Media is encrypted using "${initDataType}" init data type`); + _attemptSetMediaKeys () { + if (!this._hasSetMediaKeys) { + // FIXME: see if we can/want/need-to really to deal with several potential key-sessions? + const keysListItem = this._mediaKeysList[0]; + if (!keysListItem || !keysListItem.mediaKeys) { + logger.error('Fatal: Media is encrypted but no CDM access or no keys have been obtained yet'); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_NO_KEYS, + fatal: true + }); + return; + } - this._isMediaEncrypted = true; - this._mediaEncryptionInitDataType = initDataType; - this._mediaEncryptionInitData = initData; + logger.log('Setting keys for encrypted media'); - this._attemptSetMediaKeys(); - this._generateRequestWithPreferredKeySession(); + this._media.setMediaKeys(keysListItem.mediaKeys); + this._hasSetMediaKeys = true; + } + } + + _generateRequestWithPreferredKeySession () { + // FIXME: see if we can/want/need-to really to deal with several potential key-sessions? + const keysListItem = this._mediaKeysList[0]; + if (!keysListItem) { + logger.error('Fatal: Media is encrypted but not any key-system access has been obtained yet'); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_NO_ACCESS, + fatal: true + }); + return; } - _attemptSetMediaKeys() { - if (!this._hasSetMediaKeys) { - - // FIXME: see if we can/want/need-to really to deal with several potential key-sessions? - const keysListItem = this._mediaKeysList[0]; - if (!keysListItem || !keysListItem.mediaKeys) { - logger.error('Fatal: Media is encrypted but no CDM access or no keys have been obtained yet'); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_NO_KEYS, - fatal: true - }); - return; - } - - logger.log('Setting keys for encrypted media'); + if (keysListItem.mediaKeysSessionInitialized) { + logger.warn('Key-Session already initialized but requested again'); + return; + } - this._media.setMediaKeys(keysListItem.mediaKeys); - this._hasSetMediaKeys = true; - } + const keySession = keysListItem.mediaKeysSession; + if (!keySession) { + logger.error('Fatal: Media is encrypted but no key-session existing'); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_NO_SESSION, + fatal: true + }); } - _generateRequestWithPreferredKeySession() { - - // FIXME: see if we can/want/need-to really to deal with several potential key-sessions? - const keysListItem = this._mediaKeysList[0]; - if (!keysListItem) { - logger.error('Fatal: Media is encrypted but not any key-system access has been obtained yet'); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_NO_ACCESS, - fatal: true - }); - return; - } + const initDataType = this._mediaEncryptionInitDataType; + const initData = this._mediaEncryptionInitData; - if (keysListItem.mediaKeysSessionInitialized) { - logger.warn('Key-Session already initialized but requested again'); - return; - } + logger.log(`Generating key-session request for "${initDataType}" init data type`); - const keySession = keysListItem.mediaKeysSession; - if (!keySession) { - logger.error('Fatal: Media is encrypted but no key-session existing'); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_NO_SESSION, - fatal: true - }); - } + keysListItem.mediaKeysSessionInitialized = true; - const initDataType = this._mediaEncryptionInitDataType; - const initData = this._mediaEncryptionInitData; - - logger.log(`Generating key-session request for "${initDataType}" init data type`); - - keysListItem.mediaKeysSessionInitialized = true; - - keySession.generateRequest(initDataType, initData) - .then(() => { - logger.debug('Key-session generation succeeded'); - }) - .catch((err) => { - logger.error('Error generating key-session request:', err); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_NO_SESSION, - fatal: false - }); - }); - } + keySession.generateRequest(initDataType, initData) + .then(() => { + logger.debug('Key-session generation succeeded'); + }) + .catch((err) => { + logger.error('Error generating key-session request:', err); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_NO_SESSION, + fatal: false + }); + }); + } - /** + /** * @param {string} url License server URL * @param {ArrayBuffer} keyMessage Message data issued by key-system * @param {function} callback Called when XHR has succeeded * @returns {XMLHttpRequest} Unsent (but opened state) XHR object */ - _createLicenseXhr(url, keyMessage, callback) { - const xhr = new XMLHttpRequest(); - const licenseXhrSetup = this._licenseXhrSetup; - - try { - if (licenseXhrSetup) { - try { - licenseXhrSetup(xhr, url); - } catch (e) { - // let's try to open before running setup - xhr.open('POST', url, true); - licenseXhrSetup(xhr, url); - } - } - // if licenseXhrSetup did not yet call open, let's do it now - if (!xhr.readyState) { + _createLicenseXhr (url, keyMessage, callback) { + const xhr = new XMLHttpRequest(); + const licenseXhrSetup = this._licenseXhrSetup; + + try { + if (licenseXhrSetup) { + try { + licenseXhrSetup(xhr, url); + } catch (e) { + // let's try to open before running setup xhr.open('POST', url, true); + licenseXhrSetup(xhr, url); } - } catch (e) { - // IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS - logger.error('Error setting up key-system license XHR', e); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, - fatal: true - }); - return; } + // if licenseXhrSetup did not yet call open, let's do it now + if (!xhr.readyState) + xhr.open('POST', url, true); + } catch (e) { + // IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS + logger.error('Error setting up key-system license XHR', e); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, + fatal: true + }); + return; + } - xhr.responseType = 'arraybuffer'; - xhr.onreadystatechange = + xhr.responseType = 'arraybuffer'; + xhr.onreadystatechange = this._onLicenseRequestReadyStageChange.bind(this, xhr, url, keyMessage, callback); - return xhr; - } + return xhr; + } - /** + /** * @param {XMLHttpRequest} xhr * @param {string} url License server URL * @param {ArrayBuffer} keyMessage Message data issued by key-system * @param {function} callback Called when XHR has succeeded * */ - _onLicenseRequestReadyStageChange(xhr, url, keyMessage, callback) { - - switch(xhr.readyState) { - case 4: - if (xhr.status === 200) { - this._requestLicenseFailureCount = 0; - logger.log('License request succeeded'); - callback(xhr.response); - } else { - logger.error(`License Request XHR failed (${url}). Status: ${xhr.status} (${xhr.statusText})`); - - this._requestLicenseFailureCount++; - if (this._requestLicenseFailureCount <= MAX_LICENSE_REQUEST_FAILURES) { - const attemptsLeft = MAX_LICENSE_REQUEST_FAILURES - this._requestLicenseFailureCount + 1; - logger.warn(`Retrying license request, ${attemptsLeft} attempts left`); - this._requestLicense(keyMessage, callback); - return; - } - - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, - fatal: true - }); + _onLicenseRequestReadyStageChange (xhr, url, keyMessage, callback) { + switch (xhr.readyState) { + case 4: + if (xhr.status === 200) { + this._requestLicenseFailureCount = 0; + logger.log('License request succeeded'); + callback(xhr.response); + } else { + logger.error(`License Request XHR failed (${url}). Status: ${xhr.status} (${xhr.statusText})`); + + this._requestLicenseFailureCount++; + if (this._requestLicenseFailureCount <= MAX_LICENSE_REQUEST_FAILURES) { + const attemptsLeft = MAX_LICENSE_REQUEST_FAILURES - this._requestLicenseFailureCount + 1; + logger.warn(`Retrying license request, ${attemptsLeft} attempts left`); + this._requestLicense(keyMessage, callback); + return; } - break; - } + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED, + fatal: true + }); + } + break; } + } - /** + /** * @param {object} keysListItem * @param {ArrayBuffer} keyMessage * @returns {ArrayBuffer} Challenge data posted to license server */ - _generateLicenseRequestChallenge(keysListItem, keyMessage) { - let challenge; - - if (keysListItem.mediaKeySystemDomain === KeySystems.PLAYREADY) { + _generateLicenseRequestChallenge (keysListItem, keyMessage) { + let challenge; - logger.error('PlayReady is not supported (yet)'); + if (keysListItem.mediaKeySystemDomain === KeySystems.PLAYREADY) { + logger.error('PlayReady is not supported (yet)'); - // from https://github.com/MicrosoftEdge/Demos/blob/master/eme/scripts/demo.js - /* + // from https://github.com/MicrosoftEdge/Demos/blob/master/eme/scripts/demo.js + /* if (this.licenseType !== this.LICENSE_TYPE_WIDEVINE) { // For PlayReady CDMs, we need to dig the Challenge out of the XML. var keyMessageXml = new DOMParser().parseFromString(String.fromCharCode.apply(null, new Uint16Array(keyMessage)), 'application/xml'); @@ -434,67 +422,63 @@ class EMEController extends EventHandler { } } */ - - } else if (keysListItem.mediaKeySystemDomain === KeySystems.WIDEVINE) { - // For Widevine CDMs, the challenge is the keyMessage. - challenge = keyMessage; - } else { - logger.error('Unsupported key-system:', keysListItem.mediaKeySystemDomain); - } - - return challenge; + } else if (keysListItem.mediaKeySystemDomain === KeySystems.WIDEVINE) { + // For Widevine CDMs, the challenge is the keyMessage. + challenge = keyMessage; + } else { + logger.error('Unsupported key-system:', keysListItem.mediaKeySystemDomain); } - _requestLicense(keyMessage, callback) { + return challenge; + } - logger.log('Requesting content license for key-system'); + _requestLicense (keyMessage, callback) { + logger.log('Requesting content license for key-system'); - const keysListItem = this._mediaKeysList[0]; - if (!keysListItem) { - logger.error('Fatal error: Media is encrypted but no key-system access has been obtained yet'); - this.hls.trigger(Event.ERROR, { - type: ErrorTypes.KEY_SYSTEM_ERROR, - details: ErrorDetails.KEY_SYSTEM_NO_ACCESS, - fatal: true - }); - return; - } + const keysListItem = this._mediaKeysList[0]; + if (!keysListItem) { + logger.error('Fatal error: Media is encrypted but no key-system access has been obtained yet'); + this.hls.trigger(Event.ERROR, { + type: ErrorTypes.KEY_SYSTEM_ERROR, + details: ErrorDetails.KEY_SYSTEM_NO_ACCESS, + fatal: true + }); + return; + } - const url = this.getLicenseServerUrl(keysListItem.mediaKeySystemDomain); - const xhr = this._createLicenseXhr(url, keyMessage, callback); + const url = this.getLicenseServerUrl(keysListItem.mediaKeySystemDomain); + const xhr = this._createLicenseXhr(url, keyMessage, callback); - logger.log(`Sending license request to URL: ${url}`); + logger.log(`Sending license request to URL: ${url}`); - xhr.send(this._generateLicenseRequestChallenge(keysListItem, keyMessage)); - } + xhr.send(this._generateLicenseRequestChallenge(keysListItem, keyMessage)); + } - onMediaAttached(data) { - if (!this._emeEnabled) { - return; - } + onMediaAttached (data) { + if (!this._emeEnabled) + return; - const media = data.media; + const media = data.media; - // keep reference of media - this._media = media; + // keep reference of media + this._media = media; - // FIXME: also handle detaching media ! + // FIXME: also handle detaching media ! - media.addEventListener('encrypted', (e) => { - this._onMediaEncrypted(e.initDataType, e.initData); - }); - } + media.addEventListener('encrypted', (e) => { + this._onMediaEncrypted(e.initDataType, e.initData); + }); + } - onManifestParsed(data) { - if (!this._emeEnabled) { - return; - } + onManifestParsed (data) { + if (!this._emeEnabled) + return; - const audioCodecs = data.levels.map((level) => level.audioCodec); - const videoCodecs = data.levels.map((level) => level.videoCodec); + const audioCodecs = data.levels.map((level) => level.audioCodec); + const videoCodecs = data.levels.map((level) => level.videoCodec); - this._attemptKeySystemAccess(KeySystems.WIDEVINE, audioCodecs, videoCodecs); - } + this._attemptKeySystemAccess(KeySystems.WIDEVINE, audioCodecs, videoCodecs); + } } export default EMEController; diff --git a/src/controller/fps-controller.js b/src/controller/fps-controller.js index a15ecc6a0a4..496c5ceb7fc 100644 --- a/src/controller/fps-controller.js +++ b/src/controller/fps-controller.js @@ -4,51 +4,50 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; -class FPSController extends EventHandler{ - - constructor(hls) { +class FPSController extends EventHandler { + constructor (hls) { super(hls, Event.MEDIA_ATTACHING); } - destroy() { - if (this.timer) { + destroy () { + if (this.timer) clearInterval(this.timer); - } + this.isVideoPlaybackQualityAvailable = false; } - onMediaAttaching(data) { + onMediaAttaching (data) { const config = this.hls.config; if (config.capLevelOnFPSDrop) { const video = this.video = data.media instanceof HTMLVideoElement ? data.media : null; - if (typeof video.getVideoPlaybackQuality === 'function') { + if (typeof video.getVideoPlaybackQuality === 'function') this.isVideoPlaybackQualityAvailable = true; - } + clearInterval(this.timer); this.timer = setInterval(this.checkFPSInterval.bind(this), config.fpsDroppedMonitoringPeriod); } } - checkFPS(video, decodedFrames, droppedFrames) { + checkFPS (video, decodedFrames, droppedFrames) { let currentTime = performance.now(); if (decodedFrames) { if (this.lastTime) { let currentPeriod = currentTime - this.lastTime, - currentDropped = droppedFrames - this.lastDroppedFrames, - currentDecoded = decodedFrames - this.lastDecodedFrames, - droppedFPS = 1000 * currentDropped / currentPeriod, - hls = this.hls; - hls.trigger(Event.FPS_DROP, {currentDropped: currentDropped, currentDecoded: currentDecoded, totalDroppedFrames: droppedFrames}); + currentDropped = droppedFrames - this.lastDroppedFrames, + currentDecoded = decodedFrames - this.lastDecodedFrames, + droppedFPS = 1000 * currentDropped / currentPeriod, + hls = this.hls; + hls.trigger(Event.FPS_DROP, { currentDropped: currentDropped, currentDecoded: currentDecoded, totalDroppedFrames: droppedFrames }); if (droppedFPS > 0) { - //logger.log('checkFPS : droppedFPS/decodedFPS:' + droppedFPS/(1000 * currentDecoded / currentPeriod)); + // logger.log('checkFPS : droppedFPS/decodedFPS:' + droppedFPS/(1000 * currentDecoded / currentPeriod)); if (currentDropped > hls.config.fpsDroppedMonitoringThreshold * currentDecoded) { let currentLevel = hls.currentLevel; logger.warn('drop FPS ratio greater than max allowed value for currentLevel: ' + currentLevel); if (currentLevel > 0 && (hls.autoLevelCapping === -1 || hls.autoLevelCapping >= currentLevel)) { currentLevel = currentLevel - 1; - hls.trigger(Event.FPS_DROP_LEVEL_CAPPING, {level: currentLevel, droppedLevel: hls.currentLevel}); + hls.trigger(Event.FPS_DROP_LEVEL_CAPPING, { level: currentLevel, droppedLevel: hls.currentLevel }); hls.autoLevelCapping = currentLevel; hls.streamController.nextLevelSwitch(); } @@ -61,7 +60,7 @@ class FPSController extends EventHandler{ } } - checkFPSInterval() { + checkFPSInterval () { const video = this.video; if (video) { if (this.isVideoPlaybackQualityAvailable) { @@ -75,4 +74,3 @@ class FPSController extends EventHandler{ } export default FPSController; - diff --git a/src/controller/id3-track-controller.js b/src/controller/id3-track-controller.js index 5d110165d64..2796e78ef41 100644 --- a/src/controller/id3-track-controller.js +++ b/src/controller/id3-track-controller.js @@ -7,33 +7,32 @@ import EventHandler from '../event-handler'; import ID3 from '../demux/id3'; class ID3TrackController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, - Event.MEDIA_ATTACHED, - Event.MEDIA_DETACHING, - Event.FRAG_PARSING_METADATA); + Event.MEDIA_ATTACHED, + Event.MEDIA_DETACHING, + Event.FRAG_PARSING_METADATA); this.id3Track = undefined; this.media = undefined; } - destroy() { + destroy () { EventHandler.prototype.destroy.call(this); } // Add ID3 metatadata text track. - onMediaAttached(data) { + onMediaAttached (data) { this.media = data.media; if (!this.media) { - return; + } } - onMediaDetaching() { + onMediaDetaching () { this.media = undefined; } - onFragParsingMetadata(data) { + onFragParsingMetadata (data) { const fragment = data.frag; const samples = data.samples; @@ -52,14 +51,13 @@ class ID3TrackController extends EventHandler { const frames = ID3.getID3Frames(samples[i].data); if (frames) { const startTime = samples[i].pts; - let endTime = i < samples.length - 1 ? samples[i+1].pts : fragment.endPTS; + let endTime = i < samples.length - 1 ? samples[i + 1].pts : fragment.endPTS; // Give a slight bump to the endTime if it's equal to startTime to avoid a SyntaxError in IE - if (startTime === endTime) { + if (startTime === endTime) endTime += 0.0001; - } - for(let j = 0; j < frames.length; j++) { + for (let j = 0; j < frames.length; j++) { const frame = frames[j]; // Safari doesn't put the timestamp frame in the TextTrack if (!ID3.isTimeStampFrame(frame)) { diff --git a/src/controller/level-controller.js b/src/controller/level-controller.js index ad5d32bad86..374ba1d62b7 100644 --- a/src/controller/level-controller.js +++ b/src/controller/level-controller.js @@ -4,13 +4,12 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {logger} from '../utils/logger'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {isCodecSupportedInMp4} from '../utils/codecs'; +import { logger } from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { isCodecSupportedInMp4 } from '../utils/codecs'; export default class LevelController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.MANIFEST_LOADED, Event.LEVEL_LOADED, @@ -22,19 +21,19 @@ export default class LevelController extends EventHandler { this.timer = null; } - onHandlerDestroying() { + onHandlerDestroying () { this.cleanTimer(); this.manualLevelIndex = -1; } - cleanTimer() { + cleanTimer () { if (this.timer !== null) { clearTimeout(this.timer); this.timer = null; } } - startLoad() { + startLoad () { let levels = this._levels; this.canload = true; @@ -45,22 +44,20 @@ export default class LevelController extends EventHandler { levels.forEach(level => { level.loadError = 0; const levelDetails = level.details; - if (levelDetails && levelDetails.live) { + if (levelDetails && levelDetails.live) level.details = undefined; - } }); } // speed up live playlist refresh if timer exists - if (this.timer !== null) { + if (this.timer !== null) this.loadLevel(); - } } - stopLoad() { + stopLoad () { this.canload = false; } - onManifestLoaded(data) { + onManifestLoaded (data) { let levels = []; let bitrateStart; let levelSet = {}; @@ -80,9 +77,8 @@ export default class LevelController extends EventHandler { // erase audio codec info if browser does not support mp4a.40.34. // demuxer will autodetect codec and fallback to mpeg/audio - if (chromeOrFirefox === true && level.audioCodec && level.audioCodec.indexOf('mp4a.40.34') !== -1) { + if (chromeOrFirefox === true && level.audioCodec && level.audioCodec.indexOf('mp4a.40.34') !== -1) level.audioCodec = undefined; - } levelFromSet = levelSet[level.bitrate]; @@ -97,18 +93,16 @@ export default class LevelController extends EventHandler { }); // remove audio-only level if we also have levels with audio+video codecs signalled - if (videoCodecFound === true && audioCodecFound === true) { - levels = levels.filter(({videoCodec}) => !!videoCodec); - } + if (videoCodecFound === true && audioCodecFound === true) + levels = levels.filter(({ videoCodec }) => !!videoCodec); // only keep levels with supported audio/video codecs - levels = levels.filter(({audioCodec, videoCodec}) => { + levels = levels.filter(({ audioCodec, videoCodec }) => { return (!audioCodec || isCodecSupportedInMp4(audioCodec)) && (!videoCodec || isCodecSupportedInMp4(videoCodec)); }); - if (data.audioTracks) { + if (data.audioTracks) audioTracks = data.audioTracks.filter(track => !track.audioCodec || isCodecSupportedInMp4(track.audioCodec, 'audio')); - } if (levels.length > 0) { // start bitrate is the first bitrate of the manifest @@ -130,41 +124,40 @@ export default class LevelController extends EventHandler { levels, audioTracks, firstLevel: this._firstLevel, - stats : data.stats, - audio : audioCodecFound, - video : videoCodecFound, - altAudio : audioTracks.length > 0 + stats: data.stats, + audio: audioCodecFound, + video: videoCodecFound, + altAudio: audioTracks.length > 0 }); } else { this.hls.trigger(Event.ERROR, { - type : ErrorTypes.MEDIA_ERROR, + type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR, - fatal : true, - url : this.hls.url, - reason : 'no level with compatible codecs found in manifest' + fatal: true, + url: this.hls.url, + reason: 'no level with compatible codecs found in manifest' }); } } - get levels() { + get levels () { return this._levels; } - get level() { + get level () { return this.currentLevelIndex; } - set level(newLevel) { + set level (newLevel) { let levels = this._levels; if (levels) { newLevel = Math.min(newLevel, levels.length - 1); - if (this.currentLevelIndex !== newLevel || levels[newLevel].details === undefined) { + if (this.currentLevelIndex !== newLevel || levels[newLevel].details === undefined) this.setLevelInternal(newLevel); - } } } - setLevelInternal(newLevel) { + setLevelInternal (newLevel) { const levels = this._levels; const hls = this.hls; // check if level idx is valid @@ -174,77 +167,75 @@ export default class LevelController extends EventHandler { if (this.currentLevelIndex !== newLevel) { logger.log(`switching to level ${newLevel}`); this.currentLevelIndex = newLevel; - var levelProperties = levels[newLevel]; + let levelProperties = levels[newLevel]; levelProperties.level = newLevel; // LEVEL_SWITCH to be deprecated in next major release hls.trigger(Event.LEVEL_SWITCH, levelProperties); hls.trigger(Event.LEVEL_SWITCHING, levelProperties); } - var level = levels[newLevel], levelDetails = level.details; + let level = levels[newLevel], levelDetails = level.details; // check if we need to load playlist for this level if (!levelDetails || levelDetails.live === true) { // level not retrieved yet, or live playlist we need to (re)load it - var urlId = level.urlId; - hls.trigger(Event.LEVEL_LOADING, {url: level.url[urlId], level: newLevel, id: urlId}); + let urlId = level.urlId; + hls.trigger(Event.LEVEL_LOADING, { url: level.url[urlId], level: newLevel, id: urlId }); } } else { // invalid level id given, trigger error hls.trigger(Event.ERROR, { - type : ErrorTypes.OTHER_ERROR, + type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.LEVEL_SWITCH_ERROR, - level : newLevel, - fatal : false, - reason : 'invalid level idx' + level: newLevel, + fatal: false, + reason: 'invalid level idx' }); } } - get manualLevel() { + get manualLevel () { return this.manualLevelIndex; } - set manualLevel(newLevel) { + set manualLevel (newLevel) { this.manualLevelIndex = newLevel; - if (this._startLevel === undefined) { + if (this._startLevel === undefined) this._startLevel = newLevel; - } - if (newLevel !== -1) { + + if (newLevel !== -1) this.level = newLevel; - } } - get firstLevel() { + get firstLevel () { return this._firstLevel; } - set firstLevel(newLevel) { + set firstLevel (newLevel) { this._firstLevel = newLevel; } - get startLevel() { + get startLevel () { // hls.startLevel takes precedence over config.startLevel // if none of these values are defined, fallback on this._firstLevel (first quality level appearing in variant manifest) if (this._startLevel === undefined) { let configStartLevel = this.hls.config.startLevel; - if (configStartLevel !== undefined) { + if (configStartLevel !== undefined) return configStartLevel; - } else { + else return this._firstLevel; - } } else { return this._startLevel; } } - set startLevel(newLevel) { + set startLevel (newLevel) { this._startLevel = newLevel; } - onError(data) { + onError (data) { if (data.fatal === true) { - if (data.type === ErrorTypes.NETWORK_ERROR) { + if (data.type === ErrorTypes.NETWORK_ERROR) this.cleanTimer(); - } + return; } @@ -253,27 +244,26 @@ export default class LevelController extends EventHandler { // try to recover not fatal errors switch (data.details) { - case ErrorDetails.FRAG_LOAD_ERROR: - case ErrorDetails.FRAG_LOAD_TIMEOUT: - case ErrorDetails.KEY_LOAD_ERROR: - case ErrorDetails.KEY_LOAD_TIMEOUT: - levelIndex = data.frag.level; - fragmentError = true; - break; - case ErrorDetails.LEVEL_LOAD_ERROR: - case ErrorDetails.LEVEL_LOAD_TIMEOUT: - levelIndex = data.context.level; - levelError = true; - break; - case ErrorDetails.REMUX_ALLOC_ERROR: - levelIndex = data.level; - levelError = true; - break; + case ErrorDetails.FRAG_LOAD_ERROR: + case ErrorDetails.FRAG_LOAD_TIMEOUT: + case ErrorDetails.KEY_LOAD_ERROR: + case ErrorDetails.KEY_LOAD_TIMEOUT: + levelIndex = data.frag.level; + fragmentError = true; + break; + case ErrorDetails.LEVEL_LOAD_ERROR: + case ErrorDetails.LEVEL_LOAD_TIMEOUT: + levelIndex = data.context.level; + levelError = true; + break; + case ErrorDetails.REMUX_ALLOC_ERROR: + levelIndex = data.level; + levelError = true; + break; } - if (levelIndex !== undefined) { + if (levelIndex !== undefined) this.recoverLevel(data, levelIndex, levelError, fragmentError); - } } /** @@ -286,9 +276,9 @@ export default class LevelController extends EventHandler { * @param {Boolean} fragmentError */ // FIXME Find a better abstraction where fragment/level retry management is well decoupled - recoverLevel(errorEvent, levelIndex, levelError, fragmentError) { - let {config} = this.hls; - let {details: errorDetails} = errorEvent; + recoverLevel (errorEvent, levelIndex, levelError, fragmentError) { + let { config } = this.hls; + let { details: errorDetails } = errorEvent; let level = this._levels[levelIndex]; let redundantLevels, delay, nextLevel; @@ -343,7 +333,7 @@ export default class LevelController extends EventHandler { } // reset errors on the successful load of a fragment - onFragLoaded({frag}) { + onFragLoaded ({ frag }) { if (frag !== undefined && frag.type === 'main') { const level = this._levels[frag.level]; if (level !== undefined) { @@ -354,7 +344,7 @@ export default class LevelController extends EventHandler { } } - onLevelLoaded(data) { + onLevelLoaded (data) { const levelId = data.level; // only process level loaded events matching with expected level if (levelId === this.currentLevelIndex) { @@ -367,14 +357,14 @@ export default class LevelController extends EventHandler { let newDetails = data.details; // if current playlist is a live playlist, arm a timer to reload it if (newDetails.live) { - let reloadInterval = 1000 * ( newDetails.averagetargetduration ? newDetails.averagetargetduration : newDetails.targetduration), - curDetails = curLevel.details; + let reloadInterval = 1000 * (newDetails.averagetargetduration ? newDetails.averagetargetduration : newDetails.targetduration), + curDetails = curLevel.details; if (curDetails && newDetails.endSN === curDetails.endSN) { // follow HLS Spec, If the client reloads a Playlist file and finds that it has not // changed then it MUST wait for a period of one-half the target // duration before retrying. reloadInterval /= 2; - logger.log(`same live playlist, reload twice faster`); + logger.log('same live playlist, reload twice faster'); } // decrement reloadInterval with level loading delay reloadInterval -= performance.now() - data.stats.trequest; @@ -388,30 +378,28 @@ export default class LevelController extends EventHandler { } } - loadLevel() { + loadLevel () { let level, urlIndex; if (this.currentLevelIndex !== null && this.canload === true) { level = this._levels[this.currentLevelIndex]; if (level !== undefined && level.url.length > 0) { urlIndex = level.urlId; - this.hls.trigger(Event.LEVEL_LOADING, {url: level.url[urlIndex], level: this.currentLevelIndex, id: urlIndex}); + this.hls.trigger(Event.LEVEL_LOADING, { url: level.url[urlIndex], level: this.currentLevelIndex, id: urlIndex }); } } } - get nextLoadLevel() { - if (this.manualLevelIndex !== -1) { + get nextLoadLevel () { + if (this.manualLevelIndex !== -1) return this.manualLevelIndex; - } else { + else return this.hls.nextAutoLevel; - } } - set nextLoadLevel(nextLevel) { + set nextLoadLevel (nextLevel) { this.level = nextLevel; - if (this.manualLevelIndex === -1) { + if (this.manualLevelIndex === -1) this.hls.nextAutoLevel = nextLevel; - } } } diff --git a/src/controller/stream-controller.js b/src/controller/stream-controller.js index b6a2aba2b11..657c7d22d16 100644 --- a/src/controller/stream-controller.js +++ b/src/controller/stream-controller.js @@ -6,33 +6,32 @@ import BinarySearch from '../utils/binary-search'; import BufferHelper from '../helper/buffer-helper'; import Demuxer from '../demux/demuxer'; import Event from '../events'; -import {FragmentState} from '../helper/fragment-tracker'; +import { FragmentState } from '../helper/fragment-tracker'; import Fragment from '../loader/fragment'; import PlaylistLoader from '../loader/playlist-loader'; import * as LevelHelper from '../helper/level-helper'; import TimeRanges from '../utils/time-ranges'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {logger} from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { logger } from '../utils/logger'; import { alignDiscontinuities } from '../utils/discontinuities'; import TaskLoop from '../task-loop'; export const State = { - STOPPED : 'STOPPED', - IDLE : 'IDLE', - KEY_LOADING : 'KEY_LOADING', - FRAG_LOADING : 'FRAG_LOADING', - FRAG_LOADING_WAITING_RETRY : 'FRAG_LOADING_WAITING_RETRY', - WAITING_LEVEL : 'WAITING_LEVEL', - PARSING : 'PARSING', - PARSED : 'PARSED', - BUFFER_FLUSHING : 'BUFFER_FLUSHING', - ENDED : 'ENDED', - ERROR : 'ERROR' + STOPPED: 'STOPPED', + IDLE: 'IDLE', + KEY_LOADING: 'KEY_LOADING', + FRAG_LOADING: 'FRAG_LOADING', + FRAG_LOADING_WAITING_RETRY: 'FRAG_LOADING_WAITING_RETRY', + WAITING_LEVEL: 'WAITING_LEVEL', + PARSING: 'PARSING', + PARSED: 'PARSED', + BUFFER_FLUSHING: 'BUFFER_FLUSHING', + ENDED: 'ENDED', + ERROR: 'ERROR' }; class StreamController extends TaskLoop { - - constructor(hls, fragmentTracker) { + constructor (hls, fragmentTracker) { super(hls, Event.MEDIA_ATTACHED, Event.MEDIA_DETACHING, @@ -58,16 +57,16 @@ class StreamController extends TaskLoop { this._state = State.STOPPED; } - onHandlerDestroying() { + onHandlerDestroying () { this.stopLoad(); } - onHandlerDestroyed() { + onHandlerDestroyed () { this.state = State.STOPPED; this.fragmentTracker = null; } - startLoad(startPosition) { + startLoad (startPosition) { if (this.levels) { let lastCurrentTime = this.lastCurrentTime, hls = this.hls; this.stopLoad(); @@ -101,12 +100,12 @@ class StreamController extends TaskLoop { } } - stopLoad() { - var frag = this.fragCurrent; + stopLoad () { + let frag = this.fragCurrent; if (frag) { - if (frag.loader) { + if (frag.loader) frag.loader.abort(); - } + this.fragmentTracker.removeFragment(frag); this.fragCurrent = null; } @@ -120,44 +119,40 @@ class StreamController extends TaskLoop { this.forceStartLoad = false; } - doTick() { - - switch(this.state) { - case State.ERROR: - //don't do anything in error state to avoid breaking further ... - break; - case State.BUFFER_FLUSHING: + doTick () { + switch (this.state) { + case State.BUFFER_FLUSHING: // in buffer flushing state, reset fragLoadError counter - this.fragLoadError = 0; - break; - case State.IDLE: - this._doTickIdle(); - break; - case State.WAITING_LEVEL: - var level = this.levels[this.level]; - // check if playlist is already loaded - if (level && level.details) { - this.state = State.IDLE; - } - break; - case State.FRAG_LOADING_WAITING_RETRY: - var now = performance.now(); - var retryDate = this.retryDate; - // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading - if(!retryDate || (now >= retryDate) || (this.media && this.media.seeking)) { - logger.log(`mediaController: retryDate reached, switch back to IDLE state`); - this.state = State.IDLE; - } - break; - case State.ERROR: - case State.STOPPED: - case State.FRAG_LOADING: - case State.PARSING: - case State.PARSED: - case State.ENDED: - break; - default: - break; + this.fragLoadError = 0; + break; + case State.IDLE: + this._doTickIdle(); + break; + case State.WAITING_LEVEL: + var level = this.levels[this.level]; + // check if playlist is already loaded + if (level && level.details) + this.state = State.IDLE; + + break; + case State.FRAG_LOADING_WAITING_RETRY: + var now = performance.now(); + var retryDate = this.retryDate; + // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading + if (!retryDate || (now >= retryDate) || (this.media && this.media.seeking)) { + logger.log('mediaController: retryDate reached, switch back to IDLE state'); + this.state = State.IDLE; + } + break; + case State.ERROR: + case State.STOPPED: + case State.FRAG_LOADING: + case State.PARSING: + case State.PARSED: + case State.ENDED: + break; + default: + break; } // check buffer this._checkBuffer(); @@ -168,54 +163,51 @@ class StreamController extends TaskLoop { // Ironically the "idle" state is the on we do the most logic in it seems .... // NOTE: Maybe we could rather schedule a check for buffer length after half of the currently // played segment, or on pause/play/seek instead of naively checking every 100ms? - _doTickIdle() { + _doTickIdle () { const hls = this.hls, - config = hls.config, - media = this.media; + config = hls.config, + media = this.media; // if start level not parsed yet OR // if video not attached AND start fragment already requested OR start frag prefetch disable // exit loop, as we either need more info (level not parsed) or we need media to be attached to load new fragment if (this.levelLastLoaded === undefined || ( - !media && (this.startFragRequested || !config.startFragPrefetch))) { + !media && (this.startFragRequested || !config.startFragPrefetch))) return; - } // if we have not yet loaded any fragment, start loading from start position let pos; - if (this.loadedmetadata) { + if (this.loadedmetadata) pos = media.currentTime; - } else { + else pos = this.nextLoadPosition; - } + // determine next load level let level = hls.nextLoadLevel, - levelInfo = this.levels[level]; + levelInfo = this.levels[level]; - if (!levelInfo) { + if (!levelInfo) return; - } let levelBitrate = levelInfo.bitrate, - maxBufLen; + maxBufLen; // compute max Buffer Length that we could get from this load level, based on level bitrate. don't buffer more than 60 MB and more than 30s - if (levelBitrate) { + if (levelBitrate) maxBufLen = Math.max(8 * config.maxBufferSize / levelBitrate, config.maxBufferLength); - } else { + else maxBufLen = config.maxBufferLength; - } + maxBufLen = Math.min(maxBufLen, config.maxMaxBufferLength); // determine next candidate fragment to be loaded, based on current position and end of buffer position // ensure up to `config.maxMaxBufferLength` of buffer upfront const bufferInfo = BufferHelper.bufferInfo(this.mediaBuffer ? this.mediaBuffer : media, pos, config.maxBufferHole), - bufferLen = bufferInfo.len; + bufferLen = bufferInfo.len; // Stay idle if we are still with buffer margins - if (bufferLen >= maxBufLen) { + if (bufferLen >= maxBufLen) return; - } // if buffer length is less than maxBufLen try to load a new fragment ... logger.trace(`buffer length of ${bufferLen.toFixed(3)} is below max of ${maxBufLen.toFixed(3)}. checking for more payload ...`); @@ -238,22 +230,22 @@ class StreamController extends TaskLoop { // dont switch to ENDED if we need to backtrack last fragment let fragPrevious = this.fragPrevious; if (!levelDetails.live && fragPrevious && !fragPrevious.backtracked && fragPrevious.sn === levelDetails.endSN && !bufferInfo.nextStart) { - // fragPrevious is last fragment. retrieve level duration using last frag start offset + duration - // real duration might be lower than initial duration if there are drifts between real frag duration and playlist signaling - const duration = Math.min(media.duration,fragPrevious.start + fragPrevious.duration); - // if everything (almost) til the end is buffered, let's signal eos - // we don't compare exactly media.duration === bufferInfo.end as there could be some subtle media duration difference (audio/video offsets...) - // tolerate up to one frag duration to cope with these cases. - // also cope with almost zero last frag duration (max last frag duration with 200ms) refer to https://github.com/video-dev/hls.js/pull/657 - if (duration - Math.max(bufferInfo.end,fragPrevious.start) <= Math.max(0.2,fragPrevious.duration)) { - // Finalize the media stream - let data = {}; - if (this.altAudio) { - data.type = 'video'; - } - this.hls.trigger(Event.BUFFER_EOS,data); - this.state = State.ENDED; - return; + // fragPrevious is last fragment. retrieve level duration using last frag start offset + duration + // real duration might be lower than initial duration if there are drifts between real frag duration and playlist signaling + const duration = Math.min(media.duration, fragPrevious.start + fragPrevious.duration); + // if everything (almost) til the end is buffered, let's signal eos + // we don't compare exactly media.duration === bufferInfo.end as there could be some subtle media duration difference (audio/video offsets...) + // tolerate up to one frag duration to cope with these cases. + // also cope with almost zero last frag duration (max last frag duration with 200ms) refer to https://github.com/video-dev/hls.js/pull/657 + if (duration - Math.max(bufferInfo.end, fragPrevious.start) <= Math.max(0.2, fragPrevious.duration)) { + // Finalize the media stream + let data = {}; + if (this.altAudio) + data.type = 'video'; + + this.hls.trigger(Event.BUFFER_EOS, data); + this.state = State.ENDED; + return; } } @@ -261,22 +253,21 @@ class StreamController extends TaskLoop { this._fetchPayloadOrEos(pos, bufferInfo, levelDetails); } - _fetchPayloadOrEos(pos, bufferInfo, levelDetails) { + _fetchPayloadOrEos (pos, bufferInfo, levelDetails) { const fragPrevious = this.fragPrevious, - level = this.level, - fragments = levelDetails.fragments, - fragLen = fragments.length; + level = this.level, + fragments = levelDetails.fragments, + fragLen = fragments.length; // empty playlist - if (fragLen === 0) { + if (fragLen === 0) return; - } // find fragment index, contiguous with end of buffer position let start = fragments[0].start, - end = fragments[fragLen-1].start + fragments[fragLen-1].duration, - bufferEnd = bufferInfo.end, - frag; + end = fragments[fragLen - 1].start + fragments[fragLen - 1].duration, + bufferEnd = bufferInfo.end, + frag; if (levelDetails.initSegment && !levelDetails.initSegment.data) { frag = levelDetails.initSegment; @@ -284,50 +275,45 @@ class StreamController extends TaskLoop { // in case of live playlist we need to ensure that requested position is not located before playlist start if (levelDetails.live) { let initialLiveManifestSize = this.config.initialLiveManifestSize; - if(fragLen < initialLiveManifestSize){ + if (fragLen < initialLiveManifestSize) { logger.warn(`Can not start playback of a level, reason: not enough fragments ${fragLen} < ${initialLiveManifestSize}`); return; } frag = this._ensureFragmentAtLivePoint(levelDetails, bufferEnd, start, end, fragPrevious, fragments, fragLen); // if it explicitely returns null don't load any fragment and exit function now - if (frag === null) { + if (frag === null) return; - } - } else { // VoD playlist: if bufferEnd before start of playlist, load first fragment - if (bufferEnd < start) { + if (bufferEnd < start) frag = fragments[0]; - } } } - if (!frag) { + if (!frag) frag = this._findFragment(start, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails); - } - if(frag) { + + if (frag) this._loadFragmentOrKey(frag, level, levelDetails, pos, bufferEnd); - } - return; } - _ensureFragmentAtLivePoint(levelDetails, bufferEnd, start, end, fragPrevious, fragments, fragLen) { + _ensureFragmentAtLivePoint (levelDetails, bufferEnd, start, end, fragPrevious, fragments, fragLen) { const config = this.hls.config, media = this.media; let frag; // check if requested position is within seekable boundaries : - //logger.log(`start/pos/bufEnd/seeking:${start.toFixed(3)}/${pos.toFixed(3)}/${bufferEnd.toFixed(3)}/${this.media.seeking}`); - let maxLatency = config.liveMaxLatencyDuration !== undefined ? config.liveMaxLatencyDuration : config.liveMaxLatencyDurationCount*levelDetails.targetduration; - - if (bufferEnd < Math.max(start-config.maxFragLookUpTolerance, end - maxLatency)) { - let liveSyncPosition = this.liveSyncPosition = this.computeLivePosition(start, levelDetails); - logger.log(`buffer end: ${bufferEnd.toFixed(3)} is located too far from the end of live sliding playlist, reset currentTime to : ${liveSyncPosition.toFixed(3)}`); - bufferEnd = liveSyncPosition; - if (media && media.readyState && media.duration > liveSyncPosition) { - media.currentTime = liveSyncPosition; - } - this.nextLoadPosition = liveSyncPosition; + // logger.log(`start/pos/bufEnd/seeking:${start.toFixed(3)}/${pos.toFixed(3)}/${bufferEnd.toFixed(3)}/${this.media.seeking}`); + let maxLatency = config.liveMaxLatencyDuration !== undefined ? config.liveMaxLatencyDuration : config.liveMaxLatencyDurationCount * levelDetails.targetduration; + + if (bufferEnd < Math.max(start - config.maxFragLookUpTolerance, end - maxLatency)) { + let liveSyncPosition = this.liveSyncPosition = this.computeLivePosition(start, levelDetails); + logger.log(`buffer end: ${bufferEnd.toFixed(3)} is located too far from the end of live sliding playlist, reset currentTime to : ${liveSyncPosition.toFixed(3)}`); + bufferEnd = liveSyncPosition; + if (media && media.readyState && media.duration > liveSyncPosition) + media.currentTime = liveSyncPosition; + + this.nextLoadPosition = liveSyncPosition; } // if end of buffer greater than live edge, don't load any fragment @@ -340,9 +326,8 @@ class StreamController extends TaskLoop { // level 1 loaded [182580164,182580171] // // don't return null in case media not loaded yet (readystate === 0) - if (levelDetails.PTSKnown && bufferEnd > end && media && media.readyState) { + if (levelDetails.PTSKnown && bufferEnd > end && media && media.readyState) return null; - } if (this.startFragRequested && !levelDetails.PTSKnown) { /* we are switching level on live playlist, but we don't have any PTS info for that quality level ... @@ -350,29 +335,27 @@ class StreamController extends TaskLoop { even if SN are not synchronized between playlists, loading this frag will help us compute playlist sliding and find the right one after in case it was not the right consecutive one */ if (fragPrevious) { - - if (!levelDetails.programDateTime) {//Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE) - const targetSN = fragPrevious.sn + 1; - if (targetSN >= levelDetails.startSN && targetSN <= levelDetails.endSN) { - const fragNext = fragments[targetSN - levelDetails.startSN]; - if (fragPrevious.cc === fragNext.cc) { - frag = fragNext; - logger.log(`live playlist, switching playlist, load frag with next SN: ${frag.sn}`); - } - } - // next frag SN not available (or not with same continuity counter) - // look for a frag sharing the same CC - if (!frag) { - frag = BinarySearch.search(fragments, function(frag) { - return fragPrevious.cc - frag.cc; - }); - if (frag) { - logger.log(`live playlist, switching playlist, load frag with same CC: ${frag.sn}`); - } - } - } else {//Relies on PDT in order to switch bitrates (Support EXT-X-DISCONTINUITY without EXT-X-DISCONTINUITY-SEQUENCE) - frag = this._findFragmentByPDT(fragments, fragPrevious.endPdt + 1); - } + if (!levelDetails.programDateTime) { // Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE) + const targetSN = fragPrevious.sn + 1; + if (targetSN >= levelDetails.startSN && targetSN <= levelDetails.endSN) { + const fragNext = fragments[targetSN - levelDetails.startSN]; + if (fragPrevious.cc === fragNext.cc) { + frag = fragNext; + logger.log(`live playlist, switching playlist, load frag with next SN: ${frag.sn}`); + } + } + // next frag SN not available (or not with same continuity counter) + // look for a frag sharing the same CC + if (!frag) { + frag = BinarySearch.search(fragments, function (frag) { + return fragPrevious.cc - frag.cc; + }); + if (frag) + logger.log(`live playlist, switching playlist, load frag with same CC: ${frag.sn}`); + } + } else { // Relies on PDT in order to switch bitrates (Support EXT-X-DISCONTINUITY without EXT-X-DISCONTINUITY-SEQUENCE) + frag = this._findFragmentByPDT(fragments, fragPrevious.endPdt + 1); + } } if (!frag) { /* we have no idea about which fragment should be loaded. @@ -385,32 +368,27 @@ class StreamController extends TaskLoop { return frag; } - _findFragmentByPDT(fragments, PDTValue){ - - if(!fragments || PDTValue === undefined){ + _findFragmentByPDT (fragments, PDTValue) { + if (!fragments || PDTValue === undefined) return null; - } - //if less than start + // if less than start let firstSegment = fragments[0]; - if(PDTValue < firstSegment.pdt){ + if (PDTValue < firstSegment.pdt) return null; - } let lastSegment = fragments[fragments.length - 1]; - if(PDTValue >= lastSegment.endPdt){ + if (PDTValue >= lastSegment.endPdt) return null; - } - return BinarySearch.search(fragments, function(frag) { + return BinarySearch.search(fragments, function (frag) { return PDTValue < frag.pdt ? -1 : PDTValue >= frag.endPdt ? 1 : 0; }); } - - _findFragmentBySN(fragPrevious, fragments, bufferEnd, end) { + _findFragmentBySN (fragPrevious, fragments, bufferEnd, end) { const config = this.hls.config; let foundFrag; let maxFragLookUpTolerance = config.maxFragLookUpTolerance; @@ -428,47 +406,45 @@ class StreamController extends TaskLoop { // ...--------><-----------------------------><---------.... // previous frag matching fragment next frag // return -1 return 0 return 1 - //logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`); + // logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`); // Set the lookup tolerance to be small enough to detect the current segment - ensures we don't skip over very small segments let candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration + (candidate.deltaPTS ? candidate.deltaPTS : 0)); - if (candidate.start + candidate.duration - candidateLookupTolerance <= bufferEnd) { + if (candidate.start + candidate.duration - candidateLookupTolerance <= bufferEnd) return 1; - } // if maxFragLookUpTolerance will have negative value then don't return -1 for first element - else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) { + // if maxFragLookUpTolerance will have negative value then don't return -1 for first element + else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) return -1; - } + return 0; }; if (bufferEnd < end) { - if (bufferEnd > end - maxFragLookUpTolerance) { + if (bufferEnd > end - maxFragLookUpTolerance) maxFragLookUpTolerance = 0; - } + // Prefer the next fragment if it's within tolerance - if (fragNext && !fragmentWithinToleranceTest(fragNext)) { + if (fragNext && !fragmentWithinToleranceTest(fragNext)) foundFrag = fragNext; - } else { + else foundFrag = BinarySearch.search(fragments, fragmentWithinToleranceTest); - } - } - return foundFrag; + } + return foundFrag; } - _findFragment(start, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails) { + _findFragment (start, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails) { const config = this.hls.config; let frag; let foundFrag; if (bufferEnd < end) { - if (!levelDetails.programDateTime) {//Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE) + if (!levelDetails.programDateTime) { // Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE) foundFrag = this._findFragmentBySN(fragPrevious, fragments, bufferEnd, end); - } else {//Relies on PDT in order to switch bitrates (Support EXT-X-DISCONTINUITY without EXT-X-DISCONTINUITY-SEQUENCE) + } else { // Relies on PDT in order to switch bitrates (Support EXT-X-DISCONTINUITY without EXT-X-DISCONTINUITY-SEQUENCE) foundFrag = this._findFragmentByPDT(fragments, fragPrevious ? fragPrevious.endPdt + 1 : bufferEnd + (levelDetails.programDateTime ? Date.parse(levelDetails.programDateTime) : 0)); } - } else { // reach end of playlist - foundFrag = fragments[fragLen-1]; + foundFrag = fragments[fragLen - 1]; } if (foundFrag) { frag = foundFrag; @@ -476,7 +452,7 @@ class StreamController extends TaskLoop { const sameLevel = fragPrevious && frag.level === fragPrevious.level; const prevFrag = fragments[curSNIdx - 1]; const nextFrag = fragments[curSNIdx + 1]; - //logger.log('find SN matching with pos:' + bufferEnd + ':' + frag.sn); + // logger.log('find SN matching with pos:' + bufferEnd + ':' + frag.sn); if (fragPrevious && frag.sn === fragPrevious.sn) { if (sameLevel && !frag.backtracked) { if (frag.sn < levelDetails.endSN) { @@ -487,7 +463,7 @@ class StreamController extends TaskLoop { // then we will reload again current fragment (that way we should be able to fill the buffer hole ...) if (deltaPTS && deltaPTS > config.maxBufferHole && fragPrevious.dropped && curSNIdx) { frag = prevFrag; - logger.warn(`SN just loaded, with large PTS gap between audio and video, maybe frag is not starting with a keyframe ? load previous one to try to overcome this`); + logger.warn('SN just loaded, with large PTS gap between audio and video, maybe frag is not starting with a keyframe ? load previous one to try to overcome this'); } else { frag = nextFrag; logger.log(`SN just loaded, load next one: ${frag.sn}`); @@ -519,12 +495,12 @@ class StreamController extends TaskLoop { return frag; } - _loadFragmentOrKey(frag, level, levelDetails, pos, bufferEnd) { - //logger.log('loading frag ' + i +',pos/bufEnd:' + pos.toFixed(3) + '/' + bufferEnd.toFixed(3)); + _loadFragmentOrKey (frag, level, levelDetails, pos, bufferEnd) { + // logger.log('loading frag ' + i +',pos/bufEnd:' + pos.toFixed(3) + '/' + bufferEnd.toFixed(3)); if ((frag.decryptdata && frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) { logger.log(`Loading key for ${frag.sn} of [${levelDetails.startSN} ,${levelDetails.endSN}],level ${level}`); this.state = State.KEY_LOADING; - this.hls.trigger(Event.KEY_LOADING, {frag}); + this.hls.trigger(Event.KEY_LOADING, { frag }); } else { logger.log(`Loading ${frag.sn} of [${levelDetails.startSN} ,${levelDetails.endSN}],level ${level}, currentTime:${pos.toFixed(3)},bufferEnd:${bufferEnd.toFixed(3)}`); // Check if fragment is not loaded @@ -533,59 +509,56 @@ class StreamController extends TaskLoop { this.fragCurrent = frag; this.startFragRequested = true; // Don't update nextLoadPosition for fragments which are not buffered - if (!isNaN(frag.sn) && !frag.bitrateTest) { + if (!isNaN(frag.sn) && !frag.bitrateTest) this.nextLoadPosition = frag.start + frag.duration; - } // Allow backtracked fragments to load if (frag.backtracked || fragState === FragmentState.NOT_LOADED) { frag.autoLevel = this.hls.autoLevelEnabled; frag.bitrateTest = this.bitrateTest; - this.hls.trigger(Event.FRAG_LOADING, {frag}); + this.hls.trigger(Event.FRAG_LOADING, { frag }); // lazy demuxer init, as this could take some time ... do it during frag loading - if (!this.demuxer) { - this.demuxer = new Demuxer(this.hls,'main'); - } + if (!this.demuxer) + this.demuxer = new Demuxer(this.hls, 'main'); + this.state = State.FRAG_LOADING; } else if (fragState === FragmentState.APPENDING) { // Lower the buffer size and try again - if (this._reduceMaxBufferLength(frag.duration)) { + if (this._reduceMaxBufferLength(frag.duration)) this.fragmentTracker.removeFragment(frag); - } } } } - set state(nextState) { + set state (nextState) { if (this.state !== nextState) { const previousState = this.state; this._state = nextState; logger.log(`main stream:${previousState}->${nextState}`); - this.hls.trigger(Event.STREAM_STATE_TRANSITION, {previousState, nextState}); + this.hls.trigger(Event.STREAM_STATE_TRANSITION, { previousState, nextState }); } } - get state() { + get state () { return this._state; } - getBufferedFrag(position) { + getBufferedFrag (position) { return this.fragmentTracker.getBufferedFrag(position, PlaylistLoader.LevelType.MAIN); } - get currentLevel() { + get currentLevel () { let media = this.media; if (media) { const frag = this.getBufferedFrag(media.currentTime); - if (frag) { + if (frag) return frag.level; - } } return -1; } - get nextBufferedFrag() { + get nextBufferedFrag () { let media = this.media; if (media) { // first get end range of current fragment @@ -595,7 +568,7 @@ class StreamController extends TaskLoop { } } - followingBufferedFrag(frag) { + followingBufferedFrag (frag) { if (frag) { // try to get range of next fragment (500ms after this range) return this.getBufferedFrag(frag.endPTS + 0.5); @@ -603,17 +576,16 @@ class StreamController extends TaskLoop { return null; } - get nextLevel() { + get nextLevel () { const frag = this.nextBufferedFrag; - if (frag) { + if (frag) return frag.level; - } else { + else return -1; - } } - _checkFragmentChanged() { - var fragPlayingCurrent, currentTime, video = this.media; + _checkFragmentChanged () { + let fragPlayingCurrent, currentTime, video = this.media; if (video && video.readyState && video.seeking === false) { currentTime = video.currentTime; /* if video element is in seeked state, currentTime can only increase. @@ -622,12 +594,12 @@ class StreamController extends TaskLoop { media decode error, check this, to avoid seeking back to wrong position after a media decode error */ - if(currentTime > video.playbackRate*this.lastCurrentTime) { + if (currentTime > video.playbackRate * this.lastCurrentTime) this.lastCurrentTime = currentTime; - } - if (BufferHelper.isBuffered(video,currentTime)) { + + if (BufferHelper.isBuffered(video, currentTime)) { fragPlayingCurrent = this.getBufferedFrag(currentTime); - } else if (BufferHelper.isBuffered(video,currentTime + 0.1)) { + } else if (BufferHelper.isBuffered(video, currentTime + 0.1)) { /* ensure that FRAG_CHANGED event is triggered at startup, when first video frame is displayed and playback is paused. add a tolerance of 100ms, in case current position is not buffered, @@ -636,13 +608,13 @@ class StreamController extends TaskLoop { fragPlayingCurrent = this.getBufferedFrag(currentTime + 0.1); } if (fragPlayingCurrent) { - var fragPlaying = fragPlayingCurrent; + let fragPlaying = fragPlayingCurrent; if (fragPlaying !== this.fragPlaying) { - this.hls.trigger(Event.FRAG_CHANGED, {frag: fragPlaying}); + this.hls.trigger(Event.FRAG_CHANGED, { frag: fragPlaying }); const fragPlayingLevel = fragPlaying.level; - if (!this.fragPlaying || this.fragPlaying.level !== fragPlayingLevel) { - this.hls.trigger(Event.LEVEL_SWITCHED, {level: fragPlayingLevel}); - } + if (!this.fragPlaying || this.fragPlaying.level !== fragPlayingLevel) + this.hls.trigger(Event.LEVEL_SWITCHED, { level: fragPlayingLevel }); + this.fragPlaying = fragPlaying; } } @@ -655,7 +627,7 @@ class StreamController extends TaskLoop { - cancel any pending load request - and trigger a buffer flush */ - immediateLevelSwitch() { + immediateLevelSwitch () { logger.log('immediateLevelSwitch'); if (!this.immediateSwitch) { this.immediateSwitch = true; @@ -669,13 +641,13 @@ class StreamController extends TaskLoop { } this.previouslyPaused = previouslyPaused; } - var fragCurrent = this.fragCurrent; - if (fragCurrent && fragCurrent.loader) { + let fragCurrent = this.fragCurrent; + if (fragCurrent && fragCurrent.loader) fragCurrent.loader.abort(); - } + this.fragCurrent = null; // flush everything - this.flushMainBuffer(0,Number.POSITIVE_INFINITY); + this.flushMainBuffer(0, Number.POSITIVE_INFINITY); } /** @@ -683,17 +655,16 @@ class StreamController extends TaskLoop { * - nudge video decoder by slightly adjusting video currentTime (if currentTime buffered) * - resume the playback if needed */ - immediateLevelSwitchEnd() { + immediateLevelSwitchEnd () { const media = this.media; if (media && media.buffered.length) { this.immediateSwitch = false; - if(BufferHelper.isBuffered(media,media.currentTime)) { + if (BufferHelper.isBuffered(media, media.currentTime)) { // only nudge if currentTime is buffered media.currentTime -= 0.0001; } - if (!this.previouslyPaused) { + if (!this.previouslyPaused) media.play(); - } } } @@ -703,7 +674,7 @@ class StreamController extends TaskLoop { * we need to find the next flushable buffer range * we should take into account new segment fetch time */ - nextLevelSwitch() { + nextLevelSwitch () { const media = this.media; // ensure that media is defined and that metadata are available (to retrieve currentTime) if (media && media.readyState) { @@ -712,20 +683,19 @@ class StreamController extends TaskLoop { if (fragPlayingCurrent && fragPlayingCurrent.startPTS > 1) { // flush buffer preceding current fragment (flush until current fragment start offset) // minus 1s to avoid video freezing, that could happen if we flush keyframe of current video ... - this.flushMainBuffer(0,fragPlayingCurrent.startPTS - 1); + this.flushMainBuffer(0, fragPlayingCurrent.startPTS - 1); } if (!media.paused) { // add a safety delay of 1s - var nextLevelId = this.hls.nextLoadLevel,nextLevel = this.levels[nextLevelId], fragLastKbps = this.fragLastKbps; - if (fragLastKbps && this.fragCurrent) { + let nextLevelId = this.hls.nextLoadLevel, nextLevel = this.levels[nextLevelId], fragLastKbps = this.fragLastKbps; + if (fragLastKbps && this.fragCurrent) fetchdelay = this.fragCurrent.duration * nextLevel.bitrate / (1000 * fragLastKbps) + 1; - } else { + else fetchdelay = 0; - } } else { fetchdelay = 0; } - //logger.log('fetchdelay:'+fetchdelay); + // logger.log('fetchdelay:'+fetchdelay); // find buffer range that will be reached once new fragment will be fetched nextBufferedFrag = this.getBufferedFrag(media.currentTime + fetchdelay); if (nextBufferedFrag) { @@ -733,32 +703,32 @@ class StreamController extends TaskLoop { nextBufferedFrag = this.followingBufferedFrag(nextBufferedFrag); if (nextBufferedFrag) { // if we are here, we can also cancel any loading/demuxing in progress, as they are useless - var fragCurrent = this.fragCurrent; - if (fragCurrent && fragCurrent.loader) { + let fragCurrent = this.fragCurrent; + if (fragCurrent && fragCurrent.loader) fragCurrent.loader.abort(); - } + this.fragCurrent = null; // start flush position is the start PTS of next buffered frag. // we use frag.naxStartPTS which is max(audio startPTS, video startPTS). // in case there is a small PTS Delta between audio and video, using maxStartPTS avoids flushing last samples from current fragment - this.flushMainBuffer(nextBufferedFrag.maxStartPTS , Number.POSITIVE_INFINITY); + this.flushMainBuffer(nextBufferedFrag.maxStartPTS, Number.POSITIVE_INFINITY); } } } } - flushMainBuffer(startOffset,endOffset) { + flushMainBuffer (startOffset, endOffset) { this.state = State.BUFFER_FLUSHING; - let flushScope = {startOffset: startOffset, endOffset: endOffset}; + let flushScope = { startOffset: startOffset, endOffset: endOffset }; // if alternate audio tracks are used, only flush video, otherwise flush everything - if (this.altAudio) { + if (this.altAudio) flushScope.type = 'video'; - } + this.hls.trigger(Event.BUFFER_FLUSHING, flushScope); } - onMediaAttached(data) { - var media = this.media = this.mediaBuffer = data.media; + onMediaAttached (data) { + let media = this.media = this.mediaBuffer = data.media; this.onvseeking = this.onMediaSeeking.bind(this); this.onvseeked = this.onMediaSeeked.bind(this); this.onvended = this.onMediaEnded.bind(this); @@ -766,27 +736,26 @@ class StreamController extends TaskLoop { media.addEventListener('seeked', this.onvseeked); media.addEventListener('ended', this.onvended); let config = this.config; - if(this.levels && config.autoStartLoad) { + if (this.levels && config.autoStartLoad) this.hls.startLoad(config.startPosition); - } } - onMediaDetaching() { - var media = this.media; + onMediaDetaching () { + let media = this.media; if (media && media.ended) { logger.log('MSE detaching and video ended, reset startPosition'); this.startPosition = this.lastCurrentTime = 0; } // reset fragment backtracked flag - var levels = this.levels; + let levels = this.levels; if (levels) { - levels.forEach(level => { - if(level.details) { - level.details.fragments.forEach(fragment => { - fragment.backtracked = undefined; - }); - } + levels.forEach(level => { + if (level.details) { + level.details.fragments.forEach(fragment => { + fragment.backtracked = undefined; + }); + } }); } // remove video listeners @@ -794,27 +763,27 @@ class StreamController extends TaskLoop { media.removeEventListener('seeking', this.onvseeking); media.removeEventListener('seeked', this.onvseeked); media.removeEventListener('ended', this.onvended); - this.onvseeking = this.onvseeked = this.onvended = null; + this.onvseeking = this.onvseeked = this.onvended = null; } this.media = this.mediaBuffer = null; this.loadedmetadata = false; this.stopLoad(); } - onMediaSeeking() { + onMediaSeeking () { let media = this.media, currentTime = media ? media.currentTime : undefined, config = this.config; - if (!isNaN(currentTime)) { + if (!isNaN(currentTime)) logger.log(`media seeking to ${currentTime.toFixed(3)}`); - } + let mediaBuffer = this.mediaBuffer ? this.mediaBuffer : media; - let bufferInfo = BufferHelper.bufferInfo(mediaBuffer,currentTime,this.config.maxBufferHole); + let bufferInfo = BufferHelper.bufferInfo(mediaBuffer, currentTime, this.config.maxBufferHole); if (this.state === State.FRAG_LOADING) { let fragCurrent = this.fragCurrent; // check if we are seeking to a unbuffered area AND if frag loading is in progress if (bufferInfo.len === 0 && fragCurrent) { let tolerance = config.maxFragLookUpTolerance, - fragStartOffset = fragCurrent.start - tolerance, - fragEndOffset = fragCurrent.start + fragCurrent.duration + tolerance; + fragStartOffset = fragCurrent.start - tolerance, + fragEndOffset = fragCurrent.start + fragCurrent.duration + tolerance; // check if we seek position will be out of currently loaded frag range : if out cancel frag load, if in, don't do anything if (currentTime < fragStartOffset || currentTime > fragEndOffset) { if (fragCurrent.loader) { @@ -830,41 +799,40 @@ class StreamController extends TaskLoop { } } } else if (this.state === State.ENDED) { - // if seeking to unbuffered area, clean up fragPrevious - if (bufferInfo.len === 0) { - this.fragPrevious = 0; - } - // switch to IDLE state to check for potential new fragment - this.state = State.IDLE; + // if seeking to unbuffered area, clean up fragPrevious + if (bufferInfo.len === 0) + this.fragPrevious = 0; + + // switch to IDLE state to check for potential new fragment + this.state = State.IDLE; } - if (media) { + if (media) this.lastCurrentTime = currentTime; - } + // in case seeking occurs although no media buffered, adjust startPosition and nextLoadPosition to seek target - if(!this.loadedmetadata) { + if (!this.loadedmetadata) this.nextLoadPosition = this.startPosition = currentTime; - } + // tick to speed up processing this.tick(); } - onMediaSeeked() { + onMediaSeeked () { const media = this.media, currentTime = media ? media.currentTime : undefined; - if (!isNaN(currentTime)) { + if (!isNaN(currentTime)) logger.log(`media seeked to ${currentTime.toFixed(3)}`); - } + // tick to speed up FRAGMENT_PLAYING triggering this.tick(); } - onMediaEnded() { + onMediaEnded () { logger.log('media ended'); // reset startPosition and lastCurrentTime to restart playback @ stream beginning this.startPosition = this.lastCurrentTime = 0; } - - onManifestLoading() { + onManifestLoading () { // reset buffer on manifest loading logger.log('trigger BUFFER_RESET'); this.hls.trigger(Event.BUFFER_RESET); @@ -873,33 +841,31 @@ class StreamController extends TaskLoop { this.startPosition = this.lastCurrentTime = 0; } - onManifestParsed(data) { - var aac = false, heaac = false, codec; + onManifestParsed (data) { + let aac = false, heaac = false, codec; data.levels.forEach(level => { // detect if we have different kind of audio codecs used amongst playlists codec = level.audioCodec; if (codec) { - if (codec.indexOf('mp4a.40.2') !== -1) { + if (codec.indexOf('mp4a.40.2') !== -1) aac = true; - } - if (codec.indexOf('mp4a.40.5') !== -1) { + + if (codec.indexOf('mp4a.40.5') !== -1) heaac = true; - } } }); this.audioCodecSwitch = (aac && heaac); - if (this.audioCodecSwitch) { + if (this.audioCodecSwitch) logger.log('both AAC/HE-AAC audio found in levels; declaring level codec as HE-AAC'); - } + this.levels = data.levels; this.startFragRequested = false; let config = this.config; - if (config.autoStartLoad || this.forceStartLoad) { + if (config.autoStartLoad || this.forceStartLoad) this.hls.startLoad(config.startPosition); - } } - onLevelLoaded(data) { + onLevelLoaded (data) { const newDetails = data.details; const newLevelId = data.level; const lastLevel = this.levels[this.levelLastLoaded]; @@ -910,10 +876,10 @@ class StreamController extends TaskLoop { logger.log(`level ${newLevelId} loaded [${newDetails.startSN},${newDetails.endSN}],duration:${duration}`); if (newDetails.live) { - var curDetails = curLevel.details; + let curDetails = curLevel.details; if (curDetails && newDetails.fragments.length > 0) { // we already have details for that level, merge them - LevelHelper.mergeDetails(curDetails,newDetails); + LevelHelper.mergeDetails(curDetails, newDetails); sliding = newDetails.fragments[0].start; this.liveSyncPosition = this.computeLivePosition(sliding, curDetails); if (newDetails.PTSKnown && !isNaN(sliding)) { @@ -940,7 +906,7 @@ class StreamController extends TaskLoop { if (this.startPosition === -1 || this.lastCurrentTime === -1) { // first, check if start time offset has been set in playlist, if yes, use this value let startTimeOffset = newDetails.startTimeOffset; - if(!isNaN(startTimeOffset)) { + if (!isNaN(startTimeOffset)) { if (startTimeOffset < 0) { logger.log(`negative start time offset ${startTimeOffset}, count from end of last fragment`); startTimeOffset = sliding + duration + startTimeOffset; @@ -961,31 +927,31 @@ class StreamController extends TaskLoop { this.nextLoadPosition = this.startPosition; } // only switch batck to IDLE state if we were waiting for level to start downloading a new fragment - if (this.state === State.WAITING_LEVEL) { + if (this.state === State.WAITING_LEVEL) this.state = State.IDLE; - } - //trigger handler right now + + // trigger handler right now this.tick(); } - onKeyLoaded() { + onKeyLoaded () { if (this.state === State.KEY_LOADING) { this.state = State.IDLE; this.tick(); } } - onFragLoaded(data) { - var fragCurrent = this.fragCurrent, - fragLoaded = data.frag; + onFragLoaded (data) { + let fragCurrent = this.fragCurrent, + fragLoaded = data.frag; if (this.state === State.FRAG_LOADING && fragCurrent && fragLoaded.type === 'main' && fragLoaded.level === fragCurrent.level && fragLoaded.sn === fragCurrent.sn) { let stats = data.stats, - currentLevel = this.levels[fragCurrent.level], - details = currentLevel.details; + currentLevel = this.levels[fragCurrent.level], + details = currentLevel.details; logger.log(`Loaded ${fragCurrent.sn} of [${details.startSN} ,${details.endSN}],level ${fragCurrent.level}`); // reset frag bitrate test in any case after frag loaded event this.bitrateTest = false; @@ -997,53 +963,52 @@ class StreamController extends TaskLoop { this.state = State.IDLE; this.startFragRequested = false; stats.tparsed = stats.tbuffered = performance.now(); - this.hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: fragCurrent, id : 'main'}); + this.hls.trigger(Event.FRAG_BUFFERED, { stats: stats, frag: fragCurrent, id: 'main' }); this.tick(); } else if (fragLoaded.sn === 'initSegment') { this.state = State.IDLE; stats.tparsed = stats.tbuffered = performance.now(); details.initSegment.data = data.payload; - this.hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: fragCurrent, id : 'main'}); + this.hls.trigger(Event.FRAG_BUFFERED, { stats: stats, frag: fragCurrent, id: 'main' }); this.tick(); } else { this.state = State.PARSING; // transmux the MPEG-TS data to ISO-BMFF segments let duration = details.totalduration, - level = fragCurrent.level, - sn = fragCurrent.sn, - audioCodec = this.config.defaultAudioCodec || currentLevel.audioCodec; - if(this.audioCodecSwap) { + level = fragCurrent.level, + sn = fragCurrent.sn, + audioCodec = this.config.defaultAudioCodec || currentLevel.audioCodec; + if (this.audioCodecSwap) { logger.log('swapping playlist audio codec'); - if(audioCodec === undefined) { + if (audioCodec === undefined) audioCodec = this.lastAudioCodec; - } - if(audioCodec) { - if(audioCodec.indexOf('mp4a.40.5') !==-1) { + + if (audioCodec) { + if (audioCodec.indexOf('mp4a.40.5') !== -1) audioCodec = 'mp4a.40.2'; - } else { + else audioCodec = 'mp4a.40.5'; - } } } this.pendingBuffering = true; this.appended = false; logger.log(`Parsing ${sn} of [${details.startSN} ,${details.endSN}],level ${level}, cc ${fragCurrent.cc}`); let demuxer = this.demuxer; - if (!demuxer) { - demuxer = this.demuxer = new Demuxer(this.hls,'main'); - } + if (!demuxer) + demuxer = this.demuxer = new Demuxer(this.hls, 'main'); + // time Offset is accurate if level PTS is known, or if playlist is not sliding (not live) and if media is not seeking (this is to overcome potential timestamp drifts between playlists and fragments) let media = this.media; let mediaSeeking = media && media.seeking; let accurateTimeOffset = !mediaSeeking && (details.PTSKnown || !details.live); let initSegmentData = details.initSegment ? details.initSegment.data : []; - demuxer.push(data.payload, initSegmentData,audioCodec, currentLevel.videoCodec, fragCurrent, duration, accurateTimeOffset,undefined); + demuxer.push(data.payload, initSegmentData, audioCodec, currentLevel.videoCodec, fragCurrent, duration, accurateTimeOffset, undefined); } } this.fragLoadError = 0; } - onFragParsingInitSegment(data) { + onFragParsingInitSegment (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; @@ -1052,40 +1017,37 @@ class StreamController extends TaskLoop { fragNew.sn === fragCurrent.sn && fragNew.level === fragCurrent.level && this.state === State.PARSING) { - var tracks = data.tracks, trackName, track; + let tracks = data.tracks, trackName, track; // if audio track is expected to come from audio stream controller, discard any coming from main - if (tracks.audio && this.altAudio) { + if (tracks.audio && this.altAudio) delete tracks.audio; - } + // include levelCodec in audio and video tracks track = tracks.audio; - if(track) { - - var audioCodec = this.levels[this.level].audioCodec, - ua = navigator.userAgent.toLowerCase(); - if(audioCodec && this.audioCodecSwap) { + if (track) { + let audioCodec = this.levels[this.level].audioCodec, + ua = navigator.userAgent.toLowerCase(); + if (audioCodec && this.audioCodecSwap) { logger.log('swapping playlist audio codec'); - if(audioCodec.indexOf('mp4a.40.5') !==-1) { + if (audioCodec.indexOf('mp4a.40.5') !== -1) audioCodec = 'mp4a.40.2'; - } else { + else audioCodec = 'mp4a.40.5'; - } } // in case AAC and HE-AAC audio codecs are signalled in manifest // force HE-AAC , as it seems that most browsers prefers that way, // except for mono streams OR on FF // these conditions might need to be reviewed ... if (this.audioCodecSwitch) { - // don't force HE-AAC if mono stream - if(track.metadata.channelCount !== 1 && + // don't force HE-AAC if mono stream + if (track.metadata.channelCount !== 1 && // don't force HE-AAC if firefox - ua.indexOf('firefox') === -1) { - audioCodec = 'mp4a.40.5'; - } + ua.indexOf('firefox') === -1) + audioCodec = 'mp4a.40.5'; } // HE-AAC is broken on Android, always signal audio codec as AAC even if variant manifest states otherwise - if(ua.indexOf('android') !== -1 && track.container !== 'audio/mpeg') { // Exclude mpeg audio + if (ua.indexOf('android') !== -1 && track.container !== 'audio/mpeg') { // Exclude mpeg audio audioCodec = 'mp4a.40.2'; logger.log(`Android: force audio codec to ${audioCodec}`); } @@ -1093,29 +1055,29 @@ class StreamController extends TaskLoop { track.id = data.id; } track = tracks.video; - if(track) { + if (track) { track.levelCodec = this.levels[this.level].videoCodec; track.id = data.id; } - this.hls.trigger(Event.BUFFER_CODECS,tracks); + this.hls.trigger(Event.BUFFER_CODECS, tracks); // loop through tracks that are going to be provided to bufferController for (trackName in tracks) { track = tracks[trackName]; logger.log(`main track:${trackName},container:${track.container},codecs[level/parsed]=[${track.levelCodec}/${track.codec}]`); - var initSegment = track.initSegment; + let initSegment = track.initSegment; if (initSegment) { this.appended = true; // arm pending Buffering flag before appending a segment this.pendingBuffering = true; - this.hls.trigger(Event.BUFFER_APPENDING, {type: trackName, data: initSegment, parent : 'main', content : 'initSegment'}); + this.hls.trigger(Event.BUFFER_APPENDING, { type: trackName, data: initSegment, parent: 'main', content: 'initSegment' }); } } - //trigger handler right now + // trigger handler right now this.tick(); } } - onFragParsingData(data) { + onFragParsingData (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; if (fragCurrent && @@ -1124,25 +1086,23 @@ class StreamController extends TaskLoop { fragNew.level === fragCurrent.level && !(data.type === 'audio' && this.altAudio) && // filter out main audio if audio track is loaded through audio stream controller this.state === State.PARSING) { - var level = this.levels[this.level], - frag = fragCurrent; + let level = this.levels[this.level], + frag = fragCurrent; if (isNaN(data.endPTS)) { data.endPTS = data.startPTS + fragCurrent.duration; data.endDTS = data.startDTS + fragCurrent.duration; } - if(data.hasAudio === true) { + if (data.hasAudio === true) frag.addElementaryStream(Fragment.ElementaryStreamTypes.AUDIO); - } - if(data.hasVideo === true) { + if (data.hasVideo === true) frag.addElementaryStream(Fragment.ElementaryStreamTypes.VIDEO); - } logger.log(`Parsed ${data.type},PTS:[${data.startPTS.toFixed(3)},${data.endPTS.toFixed(3)}],DTS:[${data.startDTS.toFixed(3)}/${data.endDTS.toFixed(3)}],nb:${data.nb},dropped:${data.dropped || 0}`); // Detect gaps in a fragment and try to fix it by finding a keyframe in the previous fragment (see _findFragments) - if(data.type === 'video') { + if (data.type === 'video') { frag.dropped = data.dropped; if (frag.dropped) { if (!frag.backtracked) { @@ -1171,9 +1131,9 @@ class StreamController extends TaskLoop { } } - var drift = LevelHelper.updateFragPTSDTS(level.details,frag,data.startPTS,data.endPTS,data.startDTS,data.endDTS), - hls = this.hls; - hls.trigger(Event.LEVEL_PTS_UPDATED, {details: level.details, level: this.level, drift: drift, type: data.type, start: data.startPTS, end: data.endPTS}); + let drift = LevelHelper.updateFragPTSDTS(level.details, frag, data.startPTS, data.endPTS, data.startDTS, data.endDTS), + hls = this.hls; + hls.trigger(Event.LEVEL_PTS_UPDATED, { details: level.details, level: this.level, drift: drift, type: data.type, start: data.startPTS, end: data.endPTS }); // has remuxer dropped video frames located before first keyframe ? [data.data1, data.data2].forEach(buffer => { // only append in PARSING state (rationale is that an appending error could happen synchronously on first segment appending) @@ -1182,15 +1142,15 @@ class StreamController extends TaskLoop { this.appended = true; // arm pending Buffering flag before appending a segment this.pendingBuffering = true; - hls.trigger(Event.BUFFER_APPENDING, {type: data.type, data: buffer, parent : 'main',content : 'data'}); + hls.trigger(Event.BUFFER_APPENDING, { type: data.type, data: buffer, parent: 'main', content: 'data' }); } }); - //trigger handler right now + // trigger handler right now this.tick(); } } - onFragParsed(data) { + onFragParsed (data) { const fragCurrent = this.fragCurrent; const fragNew = data.frag; if (fragCurrent && @@ -1204,16 +1164,16 @@ class StreamController extends TaskLoop { } } - onAudioTrackSwitching(data) { + onAudioTrackSwitching (data) { // if any URL found on new audio track, it is an alternate audio track - var altAudio = !!data.url, - trackId = data.id; + let altAudio = !!data.url, + trackId = data.id; // if we switch on main audio, ensure that main fragment scheduling is synced with media.buffered // don't do anything if we switch to alt audio: audio stream controller is handling it. // we will just have to change buffer scheduling on audioTrackSwitched if (!altAudio) { if (this.mediaBuffer !== this.media) { - logger.log(`switching on main audio, use media.buffered to schedule main fragment loading`); + logger.log('switching on main audio, use media.buffered to schedule main fragment loading'); this.mediaBuffer = this.media; let fragCurrent = this.fragCurrent; // we need to refill audio buffer from main: cancel any frag loading to speed up audio switch @@ -1233,20 +1193,20 @@ class StreamController extends TaskLoop { } let hls = this.hls; // switching to main audio, flush all audio and trigger track switched - hls.trigger(Event.BUFFER_FLUSHING, {startOffset: 0 , endOffset: Number.POSITIVE_INFINITY, type : 'audio'}); - hls.trigger(Event.AUDIO_TRACK_SWITCHED, {id : trackId}); + hls.trigger(Event.BUFFER_FLUSHING, { startOffset: 0, endOffset: Number.POSITIVE_INFINITY, type: 'audio' }); + hls.trigger(Event.AUDIO_TRACK_SWITCHED, { id: trackId }); this.altAudio = false; } } - onAudioTrackSwitched(data) { - var trackId = data.id, - altAudio = !!this.hls.audioTracks[trackId].url; + onAudioTrackSwitched (data) { + let trackId = data.id, + altAudio = !!this.hls.audioTracks[trackId].url; if (altAudio) { let videoBuffer = this.videoBuffer; // if we switched on alternate audio, ensure that main fragment scheduling is synced with video sourcebuffer buffered if (videoBuffer && this.mediaBuffer !== videoBuffer) { - logger.log(`switching on alternate audio, use video.buffered to schedule main fragment loading`); + logger.log('switching on alternate audio, use video.buffered to schedule main fragment loading'); this.mediaBuffer = videoBuffer; } } @@ -1254,19 +1214,16 @@ class StreamController extends TaskLoop { this.tick(); } - - - onBufferCreated(data) { + onBufferCreated (data) { let tracks = data.tracks, mediaTrack, name, alternate = false; - for(var type in tracks) { + for (let type in tracks) { let track = tracks[type]; if (track.id === 'main') { name = type; mediaTrack = track; // keep video source buffer reference - if (type === 'video') { + if (type === 'video') this.videoBuffer = tracks[type].buffer; - } } else { alternate = true; } @@ -1279,7 +1236,7 @@ class StreamController extends TaskLoop { } } - onBufferAppended(data) { + onBufferAppended (data) { if (data.parent === 'main') { const state = this.state; if (state === State.PARSING || state === State.PARSED) { @@ -1290,8 +1247,8 @@ class StreamController extends TaskLoop { } } - _checkAppendedParsed() { - //trigger handler right now + _checkAppendedParsed () { + // trigger handler right now if (this.state === State.PARSED && (!this.appended || !this.pendingBuffering)) { const frag = this.fragCurrent; if (frag) { @@ -1302,123 +1259,122 @@ class StreamController extends TaskLoop { stats.tbuffered = performance.now(); // we should get rid of this.fragLastKbps this.fragLastKbps = Math.round(8 * stats.total / (stats.tbuffered - stats.tfirst)); - this.hls.trigger(Event.FRAG_BUFFERED, {stats: stats, frag: frag, id : 'main'}); + this.hls.trigger(Event.FRAG_BUFFERED, { stats: stats, frag: frag, id: 'main' }); this.state = State.IDLE; } this.tick(); } } - onError(data) { + onError (data) { let frag = data.frag || this.fragCurrent; // don't handle frag error not related to main fragment - if (frag && frag.type !== 'main') { + if (frag && frag.type !== 'main') return; - } + // 0.5 : tolerance needed as some browsers stalls playback before reaching buffered end let mediaBuffered = !!this.media && BufferHelper.isBuffered(this.media, this.media.currentTime) && BufferHelper.isBuffered(this.media, this.media.currentTime + 0.5); - switch(data.details) { - case ErrorDetails.FRAG_LOAD_ERROR: - case ErrorDetails.FRAG_LOAD_TIMEOUT: - case ErrorDetails.KEY_LOAD_ERROR: - case ErrorDetails.KEY_LOAD_TIMEOUT: - if (!data.fatal) { - // keep retrying until the limit will be reached - if ((this.fragLoadError + 1) <= this.config.fragLoadingMaxRetry) { - // exponential backoff capped to config.fragLoadingMaxRetryTimeout - let delay = Math.min(Math.pow(2, this.fragLoadError) * this.config.fragLoadingRetryDelay, this.config.fragLoadingMaxRetryTimeout); - logger.warn(`mediaController: frag loading failed, retry in ${delay} ms`); - this.retryDate = performance.now() + delay; - // retry loading state - // if loadedmetadata is not set, it means that we are emergency switch down on first frag - // in that case, reset startFragRequested flag - if (!this.loadedmetadata) { - this.startFragRequested = false; - this.nextLoadPosition = this.startPosition; - } - this.fragLoadError++; - this.state = State.FRAG_LOADING_WAITING_RETRY; - } else { - logger.error(`mediaController: ${data.details} reaches max retry, redispatch as fatal ...`); - // switch error to fatal - data.fatal = true; - this.state = State.ERROR; - } - } - break; - case ErrorDetails.LEVEL_LOAD_ERROR: - case ErrorDetails.LEVEL_LOAD_TIMEOUT: - if(this.state !== State.ERROR) { - if (data.fatal) { - // if fatal error, stop processing - this.state = State.ERROR; - logger.warn(`streamController: ${data.details},switch to ${this.state} state ...`); - } else { - // in case of non fatal error while loading level, if level controller is not retrying to load level , switch back to IDLE - if (!data.levelRetry && this.state === State.WAITING_LEVEL) { - this.state = State.IDLE; - } + switch (data.details) { + case ErrorDetails.FRAG_LOAD_ERROR: + case ErrorDetails.FRAG_LOAD_TIMEOUT: + case ErrorDetails.KEY_LOAD_ERROR: + case ErrorDetails.KEY_LOAD_TIMEOUT: + if (!data.fatal) { + // keep retrying until the limit will be reached + if ((this.fragLoadError + 1) <= this.config.fragLoadingMaxRetry) { + // exponential backoff capped to config.fragLoadingMaxRetryTimeout + let delay = Math.min(Math.pow(2, this.fragLoadError) * this.config.fragLoadingRetryDelay, this.config.fragLoadingMaxRetryTimeout); + logger.warn(`mediaController: frag loading failed, retry in ${delay} ms`); + this.retryDate = performance.now() + delay; + // retry loading state + // if loadedmetadata is not set, it means that we are emergency switch down on first frag + // in that case, reset startFragRequested flag + if (!this.loadedmetadata) { + this.startFragRequested = false; + this.nextLoadPosition = this.startPosition; } + this.fragLoadError++; + this.state = State.FRAG_LOADING_WAITING_RETRY; + } else { + logger.error(`mediaController: ${data.details} reaches max retry, redispatch as fatal ...`); + // switch error to fatal + data.fatal = true; + this.state = State.ERROR; } - break; - case ErrorDetails.BUFFER_FULL_ERROR: - // if in appending state - if (data.parent === 'main' && (this.state === State.PARSING || this.state === State.PARSED)) { - // reduce max buf len if current position is buffered - if (mediaBuffered) { - this._reduceMaxBufferLength(this.config.maxBufferLength); + } + break; + case ErrorDetails.LEVEL_LOAD_ERROR: + case ErrorDetails.LEVEL_LOAD_TIMEOUT: + if (this.state !== State.ERROR) { + if (data.fatal) { + // if fatal error, stop processing + this.state = State.ERROR; + logger.warn(`streamController: ${data.details},switch to ${this.state} state ...`); + } else { + // in case of non fatal error while loading level, if level controller is not retrying to load level , switch back to IDLE + if (!data.levelRetry && this.state === State.WAITING_LEVEL) this.state = State.IDLE; - } else { - // current position is not buffered, but browser is still complaining about buffer full error - // this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708 - // in that case flush the whole buffer to recover - logger.warn('buffer full error also media.currentTime is not buffered, flush everything'); - this.fragCurrent = null; - // flush everything - this.flushMainBuffer(0,Number.POSITIVE_INFINITY); - } } - break; - default: - break; + } + break; + case ErrorDetails.BUFFER_FULL_ERROR: + // if in appending state + if (data.parent === 'main' && (this.state === State.PARSING || this.state === State.PARSED)) { + // reduce max buf len if current position is buffered + if (mediaBuffered) { + this._reduceMaxBufferLength(this.config.maxBufferLength); + this.state = State.IDLE; + } else { + // current position is not buffered, but browser is still complaining about buffer full error + // this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708 + // in that case flush the whole buffer to recover + logger.warn('buffer full error also media.currentTime is not buffered, flush everything'); + this.fragCurrent = null; + // flush everything + this.flushMainBuffer(0, Number.POSITIVE_INFINITY); + } + } + break; + default: + break; } } - _reduceMaxBufferLength(minLength) { + _reduceMaxBufferLength (minLength) { let config = this.config; if (config.maxMaxBufferLength >= minLength) { // reduce max buffer length as it might be too high. we do this to avoid loop flushing ... - config.maxMaxBufferLength/=2; + config.maxMaxBufferLength /= 2; logger.warn(`main:reduce max buffer length to ${config.maxMaxBufferLength}s`); return true; } return false; } -_checkBuffer() { - var media = this.media, - config = this.config; + _checkBuffer () { + let media = this.media, + config = this.config; // if ready state different from HAVE_NOTHING (numeric value 0), we are allowed to seek - if(media && media.readyState) { - let currentTime = media.currentTime, - mediaBuffer = this.mediaBuffer ? this.mediaBuffer : media, - buffered = mediaBuffer.buffered; + if (media && media.readyState) { + let currentTime = media.currentTime, + mediaBuffer = this.mediaBuffer ? this.mediaBuffer : media, + buffered = mediaBuffer.buffered; // adjust currentTime to start position on loaded metadata - if(!this.loadedmetadata && buffered.length) { + if (!this.loadedmetadata && buffered.length) { this.loadedmetadata = true; // only adjust currentTime if different from startPosition or if startPosition not buffered // at that stage, there should be only one buffered range, as we reach that code after first fragment has been buffered let startPosition = media.seeking ? currentTime : this.startPosition, - startPositionBuffered = BufferHelper.isBuffered(mediaBuffer,startPosition), - firstbufferedPosition = buffered.start(0), - startNotBufferedButClose = !startPositionBuffered && (Math.abs(startPosition-firstbufferedPosition) < config.maxSeekHole); + startPositionBuffered = BufferHelper.isBuffered(mediaBuffer, startPosition), + firstbufferedPosition = buffered.start(0), + startNotBufferedButClose = !startPositionBuffered && (Math.abs(startPosition - firstbufferedPosition) < config.maxSeekHole); // if currentTime not matching with expected startPosition or startPosition not buffered but close to first buffered if (currentTime !== startPosition || startNotBufferedButClose) { logger.log(`target start position:${startPosition}`); // if startPosition not buffered, let's seek to buffered.start(0) - if(startNotBufferedButClose) { + if (startNotBufferedButClose) { startPosition = firstbufferedPosition; logger.log(`target start position not buffered, seek to buffered.start(0) ${startPosition}`); } @@ -1428,28 +1384,28 @@ _checkBuffer() { } else if (this.immediateSwitch) { this.immediateLevelSwitchEnd(); } else { - let bufferInfo = BufferHelper.bufferInfo(media,currentTime,config.maxBufferHole), - expectedPlaying = !(media.paused || // not playing when media is paused - media.ended || // not playing when media is ended + let bufferInfo = BufferHelper.bufferInfo(media, currentTime, config.maxBufferHole), + expectedPlaying = !(media.paused || // not playing when media is paused + media.ended || // not playing when media is ended media.buffered.length === 0), // not playing if nothing buffered - jumpThreshold = 0.5, // tolerance needed as some browsers stalls playback before reaching buffered range end - playheadMoving = currentTime !== this.lastCurrentTime; + jumpThreshold = 0.5, // tolerance needed as some browsers stalls playback before reaching buffered range end + playheadMoving = currentTime !== this.lastCurrentTime; if (playheadMoving) { // played moving, but was previously stalled => now not stuck anymore if (this.stallReported) { - logger.warn(`playback not stuck anymore @${currentTime}, after ${Math.round(performance.now()-this.stalled)}ms`); + logger.warn(`playback not stuck anymore @${currentTime}, after ${Math.round(performance.now() - this.stalled)}ms`); this.stallReported = false; } this.stalled = undefined; this.nudgeRetry = 0; } else { // playhead not moving - if(expectedPlaying) { + if (expectedPlaying) { // playhead not moving BUT media expected to play const tnow = performance.now(); const hls = this.hls; - if(!this.stalled) { + if (!this.stalled) { // stall just detected, store current time this.stalled = tnow; this.stallReported = false; @@ -1461,16 +1417,16 @@ _checkBuffer() { let nudgeRetry = this.nudgeRetry || 0; // Check if fragment is broken let partial = this.fragmentTracker.getPartialFragment(currentTime); - if(partial !== null) { + if (partial !== null) { let lastEndTime = 0; // Check if currentTime is between unbuffered regions of partial fragments for (let i = 0; i < media.buffered.length; i++) { let startTime = media.buffered.start(i); - if(currentTime >= lastEndTime && currentTime < startTime) { + if (currentTime >= lastEndTime && currentTime < startTime) { media.currentTime = Math.max(startTime, media.currentTime + 0.1); logger.warn(`skipping hole, adjusting currentTime from ${currentTime} to ${media.currentTime}`); this.stalled = undefined; - hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_SEEK_OVER_HOLE, fatal: false, reason: `fragment loaded with buffer holes, seeking from ${currentTime} to ${media.currentTime}`, frag: partial}); + hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_SEEK_OVER_HOLE, fatal: false, reason: `fragment loaded with buffer holes, seeking from ${currentTime} to ${media.currentTime}`, frag: partial }); return; } lastEndTime = media.buffered.end(i); @@ -1481,7 +1437,7 @@ _checkBuffer() { if (!this.stallReported) { this.stallReported = true; logger.warn(`playback stalling in high buffer @${currentTime}`); - hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_STALLED_ERROR, fatal: false, buffer : bufferLen}); + hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_STALLED_ERROR, fatal: false, buffer: bufferLen }); } // reset stalled so to rearm watchdog timer this.stalled = undefined; @@ -1492,10 +1448,10 @@ _checkBuffer() { logger.log(`adjust currentTime from ${currentTime} to ${targetTime}`); // playback stalled in buffered area ... let's nudge currentTime to try to overcome this media.currentTime = targetTime; - hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_NUDGE_ON_STALL, fatal: false}); + hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_NUDGE_ON_STALL, fatal: false }); } else { logger.error(`still stuck in high buffer @${currentTime} after ${config.nudgeMaxRetry}, raise fatal error`); - hls.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_STALLED_ERROR, fatal: true}); + hls.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.BUFFER_STALLED_ERROR, fatal: true }); } } } @@ -1505,18 +1461,18 @@ _checkBuffer() { } } - onFragLoadEmergencyAborted() { + onFragLoadEmergencyAborted () { this.state = State.IDLE; // if loadedmetadata is not set, it means that we are emergency switch down on first frag // in that case, reset startFragRequested flag - if(!this.loadedmetadata) { + if (!this.loadedmetadata) { this.startFragRequested = false; this.nextLoadPosition = this.startPosition; } this.tick(); } - onBufferFlushed() { + onBufferFlushed () { /* after successful buffer flushing, filter flushed fragments from bufferedFrags use mediaBuffered instead of media (so that we will check against video.buffered ranges in case of alt audio track) */ @@ -1530,22 +1486,21 @@ _checkBuffer() { this.fragPrevious = null; } - swapAudioCodec() { + swapAudioCodec () { this.audioCodecSwap = !this.audioCodecSwap; } - computeLivePosition(sliding, levelDetails) { + computeLivePosition (sliding, levelDetails) { let targetLatency = this.config.liveSyncDuration !== undefined ? this.config.liveSyncDuration : this.config.liveSyncDurationCount * levelDetails.targetduration; return sliding + Math.max(0, levelDetails.totalduration - targetLatency); } - get liveSyncPosition() { + get liveSyncPosition () { return this._liveSyncPosition; } - set liveSyncPosition(value) { + set liveSyncPosition (value) { this._liveSyncPosition = value; } } export default StreamController; - diff --git a/src/controller/subtitle-stream-controller.js b/src/controller/subtitle-stream-controller.js index 9f8e2c5c11a..5e2d71286ab 100644 --- a/src/controller/subtitle-stream-controller.js +++ b/src/controller/subtitle-stream-controller.js @@ -3,20 +3,19 @@ */ import Event from '../events'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; import Decrypter from '../crypt/decrypter'; import TaskLoop from '../task-loop'; const State = { - STOPPED : 'STOPPED', - IDLE : 'IDLE', - KEY_LOADING : 'KEY_LOADING', - FRAG_LOADING : 'FRAG_LOADING' + STOPPED: 'STOPPED', + IDLE: 'IDLE', + KEY_LOADING: 'KEY_LOADING', + FRAG_LOADING: 'FRAG_LOADING' }; class SubtitleStreamController extends TaskLoop { - - constructor(hls) { + constructor (hls) { super(hls, Event.MEDIA_ATTACHED, Event.ERROR, @@ -36,12 +35,12 @@ class SubtitleStreamController extends TaskLoop { this.decrypter = new Decrypter(hls.observer, hls.config); } - onHandlerDestroyed() { + onHandlerDestroyed () { this.state = State.STOPPED; } // Remove all queued items and create a new, empty queue for each track. - clearVttFragQueues() { + clearVttFragQueues () { this.vttFragQueues = {}; this.tracks.forEach(track => { this.vttFragQueues[track.id] = []; @@ -49,95 +48,93 @@ class SubtitleStreamController extends TaskLoop { } // If no frag is being processed and queue isn't empty, initiate processing of next frag in line. - nextFrag() { - if(this.currentlyProcessing === null && this.currentTrackId > -1 && this.vttFragQueues[this.currentTrackId].length) { + nextFrag () { + if (this.currentlyProcessing === null && this.currentTrackId > -1 && this.vttFragQueues[this.currentTrackId].length) { let frag = this.currentlyProcessing = this.vttFragQueues[this.currentTrackId].shift(); this.fragCurrent = frag; - this.hls.trigger(Event.FRAG_LOADING, {frag: frag}); + this.hls.trigger(Event.FRAG_LOADING, { frag: frag }); this.state = State.FRAG_LOADING; } } // When fragment has finished processing, add sn to list of completed if successful. - onSubtitleFragProcessed(data) { - if(data.success) { + onSubtitleFragProcessed (data) { + if (data.success) this.vttFragSNsProcessed[data.frag.trackId].push(data.frag.sn); - } + this.currentlyProcessing = null; this.state = State.IDLE; this.nextFrag(); } - onMediaAttached() { + onMediaAttached () { this.state = State.IDLE; } // If something goes wrong, procede to next frag, if we were processing one. - onError(data) { + onError (data) { let frag = data.frag; // don't handle frag error not related to subtitle fragment - if (frag && frag.type !== 'subtitle') { + if (frag && frag.type !== 'subtitle') return; - } - if(this.currentlyProcessing) { + + if (this.currentlyProcessing) { this.currentlyProcessing = null; this.nextFrag(); } } - doTick() { - switch(this.state) { - case State.IDLE: - const tracks = this.tracks; - let trackId = this.currentTrackId; + doTick () { + switch (this.state) { + case State.IDLE: + const tracks = this.tracks; + let trackId = this.currentTrackId; - const processedFragSNs = this.vttFragSNsProcessed[trackId], - fragQueue = this.vttFragQueues[trackId], - currentFragSN = !!this.currentlyProcessing ? this.currentlyProcessing.sn : -1; + const processedFragSNs = this.vttFragSNsProcessed[trackId], + fragQueue = this.vttFragQueues[trackId], + currentFragSN = this.currentlyProcessing ? this.currentlyProcessing.sn : -1; - const alreadyProcessed = function(frag) { - return processedFragSNs.indexOf(frag.sn) > -1; - }; + const alreadyProcessed = function (frag) { + return processedFragSNs.indexOf(frag.sn) > -1; + }; - const alreadyInQueue = function(frag) { - return fragQueue.some(fragInQueue => {return fragInQueue.sn === frag.sn;}); - }; + const alreadyInQueue = function (frag) { + return fragQueue.some(fragInQueue => { return fragInQueue.sn === frag.sn; }); + }; // exit if tracks don't exist - if (!tracks) { - break; - } - var trackDetails; - - if (trackId < tracks.length) { - trackDetails = tracks[trackId].details; - } - - if (typeof trackDetails === 'undefined') { - break; - } - - // Add all fragments that haven't been, aren't currently being and aren't waiting to be processed, to queue. - trackDetails.fragments.forEach(frag => { - if(!(alreadyProcessed(frag) || frag.sn === currentFragSN || alreadyInQueue(frag))) { - // Load key if subtitles are encrypted - if ((frag.decryptdata && frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) { - logger.log(`Loading key for ${frag.sn}`); - this.state = State.KEY_LOADING; - this.hls.trigger(Event.KEY_LOADING, {frag: frag}); - } else { - // Frags don't know their subtitle track ID, so let's just add that... - frag.trackId = trackId; - fragQueue.push(frag); - this.nextFrag(); - } + if (!tracks) + break; + + var trackDetails; + + if (trackId < tracks.length) + trackDetails = tracks[trackId].details; + + if (typeof trackDetails === 'undefined') + break; + + // Add all fragments that haven't been, aren't currently being and aren't waiting to be processed, to queue. + trackDetails.fragments.forEach(frag => { + if (!(alreadyProcessed(frag) || frag.sn === currentFragSN || alreadyInQueue(frag))) { + // Load key if subtitles are encrypted + if ((frag.decryptdata && frag.decryptdata.uri != null) && (frag.decryptdata.key == null)) { + logger.log(`Loading key for ${frag.sn}`); + this.state = State.KEY_LOADING; + this.hls.trigger(Event.KEY_LOADING, { frag: frag }); + } else { + // Frags don't know their subtitle track ID, so let's just add that... + frag.trackId = trackId; + fragQueue.push(frag); + this.nextFrag(); } - }); - } + } + }); + } } // Got all new subtitle tracks. - onSubtitleTracksUpdated(data) { + onSubtitleTracksUpdated (data) { logger.log('subtitle tracks updated'); this.tracks = data.subtitleTracks; this.clearVttFragQueues(); @@ -147,52 +144,52 @@ class SubtitleStreamController extends TaskLoop { }); } - onSubtitleTrackSwitch(data) { + onSubtitleTrackSwitch (data) { this.currentTrackId = data.id; this.clearVttFragQueues(); } // Got a new set of subtitle fragments. - onSubtitleTrackLoaded() { + onSubtitleTrackLoaded () { this.tick(); } - onKeyLoaded() { + onKeyLoaded () { if (this.state === State.KEY_LOADING) { this.state = State.IDLE; this.tick(); } } - onFragLoaded(data) { - var fragCurrent = this.fragCurrent, - decryptData = data.frag.decryptdata; + onFragLoaded (data) { + let fragCurrent = this.fragCurrent, + decryptData = data.frag.decryptdata; let fragLoaded = data.frag, - hls = this.hls; + hls = this.hls; if (this.state === State.FRAG_LOADING && fragCurrent && data.frag.type === 'subtitle' && fragCurrent.sn === data.frag.sn) { - // check to see if the payload needs to be decrypted - if ((data.payload.byteLength > 0) && (decryptData != null) && (decryptData.key != null) && (decryptData.method === 'AES-128')) { - var startTime; - try { - startTime = performance.now(); - } catch (error) { - startTime = Date.now(); - } - // decrypt the subtitles - this.decrypter.decrypt(data.payload, decryptData.key.buffer, decryptData.iv.buffer, function(decryptedData) { - var endTime; - try { - endTime = performance.now(); - } catch (error) { - endTime = Date.now(); - } - hls.trigger(Event.FRAG_DECRYPTED, { frag: fragLoaded, payload : decryptedData, stats: { tstart: startTime, tdecrypt: endTime } }); - }); - } + // check to see if the payload needs to be decrypted + if ((data.payload.byteLength > 0) && (decryptData != null) && (decryptData.key != null) && (decryptData.method === 'AES-128')) { + let startTime; + try { + startTime = performance.now(); + } catch (error) { + startTime = Date.now(); } + // decrypt the subtitles + this.decrypter.decrypt(data.payload, decryptData.key.buffer, decryptData.iv.buffer, function (decryptedData) { + let endTime; + try { + endTime = performance.now(); + } catch (error) { + endTime = Date.now(); + } + hls.trigger(Event.FRAG_DECRYPTED, { frag: fragLoaded, payload: decryptedData, stats: { tstart: startTime, tdecrypt: endTime } }); + }); + } + } } } export default SubtitleStreamController; diff --git a/src/controller/subtitle-track-controller.js b/src/controller/subtitle-track-controller.js index 68504fcd971..7da2421410e 100644 --- a/src/controller/subtitle-track-controller.js +++ b/src/controller/subtitle-track-controller.js @@ -4,27 +4,25 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; -function filterSubtitleTracks(textTrackList) { +function filterSubtitleTracks (textTrackList) { let tracks = []; for (let i = 0; i < textTrackList.length; i++) { - if (textTrackList[i].kind === 'subtitles') { + if (textTrackList[i].kind === 'subtitles') tracks.push(textTrackList[i]); - } } return tracks; } class SubtitleTrackController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, - Event.MEDIA_ATTACHED, - Event.MEDIA_DETACHING, - Event.MANIFEST_LOADING, - Event.MANIFEST_LOADED, - Event.SUBTITLE_TRACK_LOADED); + Event.MEDIA_ATTACHED, + Event.MEDIA_DETACHING, + Event.MANIFEST_LOADING, + Event.MANIFEST_LOADED, + Event.SUBTITLE_TRACK_LOADED); this.tracks = []; this.trackId = -1; this.media = undefined; @@ -35,11 +33,10 @@ class SubtitleTrackController extends EventHandler { this.subtitleDisplay = false; } - _onTextTracksChanged() { + _onTextTracksChanged () { // Media is undefined when switching streams via loadSource() - if (!this.media) { + if (!this.media) return; - } let trackId = -1; let tracks = filterSubtitleTracks(this.media.textTracks); @@ -57,16 +54,15 @@ class SubtitleTrackController extends EventHandler { this.subtitleTrack = trackId; } - destroy() { + destroy () { EventHandler.prototype.destroy.call(this); } // Listen for subtitle track change, then extract the current track ID. - onMediaAttached(data) { + onMediaAttached (data) { this.media = data.media; - if (!this.media) { + if (!this.media) return; - } if (this.queuedDefaultTrack !== undefined) { this.subtitleTrack = this.queuedDefaultTrack; @@ -85,31 +81,30 @@ class SubtitleTrackController extends EventHandler { } } - onMediaDetaching() { - if (!this.media) { + onMediaDetaching () { + if (!this.media) return; - } - if (this.useTextTrackPolling) { + + if (this.useTextTrackPolling) clearInterval(this.subtitlePollingInterval); - } else { + else this.media.textTracks.removeEventListener('change', this.trackChangeListener); - } this.media = undefined; } // Reset subtitle tracks on manifest loading - onManifestLoading() { + onManifestLoading () { this.tracks = []; this.trackId = -1; } // Fired whenever a new manifest is loaded. - onManifestLoaded(data) { + onManifestLoaded (data) { let tracks = data.subtitles || []; this.tracks = tracks; this.trackId = -1; - this.hls.trigger(Event.SUBTITLE_TRACKS_UPDATED, {subtitleTracks : tracks}); + this.hls.trigger(Event.SUBTITLE_TRACKS_UPDATED, { subtitleTracks: tracks }); // loop through available subtitle tracks and autoselect default if needed // TODO: improve selection logic to handle forced, etc @@ -119,33 +114,31 @@ class SubtitleTrackController extends EventHandler { // if media has not been attached yet, it will fail // we keep a reference to the default track id // and we'll set subtitleTrack when onMediaAttached is triggered - if (this.media) { + if (this.media) this.subtitleTrack = track.id; - } else { + else this.queuedDefaultTrack = track.id; - } } }); } // Trigger subtitle track playlist reload. - onTick() { + onTick () { const trackId = this.trackId; const subtitleTrack = this.tracks[trackId]; - if (!subtitleTrack) { + if (!subtitleTrack) return; - } const details = subtitleTrack.details; // check if we need to load playlist for this subtitle Track if (details === undefined || details.live === true) { // track not retrieved yet, or live playlist we need to (re)load it logger.log(`(re)loading playlist for subtitle track ${trackId}`); - this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, {url: subtitleTrack.url, id: trackId}); + this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, { url: subtitleTrack.url, id: trackId }); } } - onSubtitleTrackLoaded(data) { + onSubtitleTrackLoaded (data) { if (data.id < this.tracks.length) { logger.log(`subtitle track ${data.id} loaded`); this.tracks[data.id].details = data.details; @@ -166,27 +159,26 @@ class SubtitleTrackController extends EventHandler { } /** get alternate subtitle tracks list from playlist **/ - get subtitleTracks() { + get subtitleTracks () { return this.tracks; } /** get index of the selected subtitle track (index in subtitle track lists) **/ - get subtitleTrack() { - return this.trackId; + get subtitleTrack () { + return this.trackId; } /** select a subtitle track, based on its index in subtitle track lists**/ - set subtitleTrack(subtitleTrackId) { - if (this.trackId !== subtitleTrackId) {// || this.tracks[subtitleTrackId].details === undefined) { + set subtitleTrack (subtitleTrackId) { + if (this.trackId !== subtitleTrackId) { // || this.tracks[subtitleTrackId].details === undefined) { this.setSubtitleTrackInternal(subtitleTrackId); } } - setSubtitleTrackInternal(newId) { + setSubtitleTrackInternal (newId) { // check if level idx is valid - if (newId < -1 || newId >= this.tracks.length) { + if (newId < -1 || newId >= this.tracks.length) return; - } // stopping live reloading timer if any if (this.timer) { @@ -197,29 +189,26 @@ class SubtitleTrackController extends EventHandler { let textTracks = filterSubtitleTracks(this.media.textTracks); // hide currently enabled subtitle track - if (this.trackId !== -1) { + if (this.trackId !== -1) textTracks[this.trackId].mode = 'disabled'; - } this.trackId = newId; logger.log(`switching to subtitle track ${newId}`); - this.hls.trigger(Event.SUBTITLE_TRACK_SWITCH, {id: newId}); + this.hls.trigger(Event.SUBTITLE_TRACK_SWITCH, { id: newId }); - if (newId === -1) { + if (newId === -1) return; - } const subtitleTrack = this.tracks[newId]; - if(newId < textTracks.length) { + if (newId < textTracks.length) textTracks[newId].mode = this.subtitleDisplay ? 'showing' : 'hidden'; - } // check if we need to load playlist for this subtitle Track let details = subtitleTrack.details; if (details === undefined || details.live === true) { // track not retrieved yet, or live playlist we need to (re)load it logger.log(`(re)loading playlist for subtitle track ${newId}`); - this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, {url: subtitleTrack.url, id: newId}); + this.hls.trigger(Event.SUBTITLE_TRACK_LOADING, { url: subtitleTrack.url, id: newId }); } } } diff --git a/src/controller/timeline-controller.js b/src/controller/timeline-controller.js index 009696d2cee..497fc8ec1af 100644 --- a/src/controller/timeline-controller.js +++ b/src/controller/timeline-controller.js @@ -7,36 +7,34 @@ import EventHandler from '../event-handler'; import Cea608Parser from '../utils/cea-608-parser'; import OutputFilter from '../utils/output-filter'; import WebVTTParser from '../utils/webvtt-parser'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; -function clearCurrentCues(track) { +function clearCurrentCues (track) { if (track && track.cues) { - while (track.cues.length > 0) { + while (track.cues.length > 0) track.removeCue(track.cues[0]); - } } } -function reuseVttTextTrack(inUseTrack, manifestTrack) { +function reuseVttTextTrack (inUseTrack, manifestTrack) { return inUseTrack && inUseTrack.label === manifestTrack.name && !(inUseTrack.textTrack1 || inUseTrack.textTrack2); } -function intersection(x1, x2, y1, y2) { +function intersection (x1, x2, y1, y2) { return Math.min(x2, y2) - Math.max(x1, y1); } class TimelineController extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.MEDIA_ATTACHING, - Event.MEDIA_DETACHING, - Event.FRAG_PARSING_USERDATA, - Event.FRAG_DECRYPTED, - Event.MANIFEST_LOADING, - Event.MANIFEST_LOADED, - Event.FRAG_LOADED, - Event.LEVEL_SWITCHING, - Event.INIT_PTS_FOUND); + Event.MEDIA_DETACHING, + Event.FRAG_PARSING_USERDATA, + Event.FRAG_DECRYPTED, + Event.MANIFEST_LOADING, + Event.MANIFEST_LOADED, + Event.FRAG_LOADED, + Event.LEVEL_SWITCHING, + Event.INIT_PTS_FOUND); this.hls = hls; this.config = hls.config; @@ -48,16 +46,15 @@ class TimelineController extends EventHandler { this.initPTS = undefined; this.cueRanges = []; - if (this.config.enableCEA708Captions) - { - var channel1 = new OutputFilter(this, 1); - var channel2 = new OutputFilter(this, 2); + if (this.config.enableCEA708Captions) { + let channel1 = new OutputFilter(this, 1); + let channel2 = new OutputFilter(this, 2); this.cea608Parser = new Cea608Parser(0, channel1, channel2); } } - addCues(channel, startTime, endTime, screen) { + addCues (channel, startTime, endTime, screen) { // skip cues which overlap more than 50% with previously parsed time ranges const ranges = this.cueRanges; let merged = false; @@ -68,22 +65,20 @@ class TimelineController extends EventHandler { cueRange[0] = Math.min(cueRange[0], startTime); cueRange[1] = Math.max(cueRange[1], endTime); merged = true; - if ((overlap / (endTime - startTime)) > 0.5) { + if ((overlap / (endTime - startTime)) > 0.5) return; - } } } - if (!merged) { + if (!merged) ranges.push([startTime, endTime]); - } + this.Cues.newCue(this[channel], startTime, endTime, screen); } // Triggered when an initial PTS is found; used for synchronisation of WebVTT. - onInitPtsFound(data) { - if (typeof this.initPTS === 'undefined') { + onInitPtsFound (data) { + if (typeof this.initPTS === 'undefined') this.initPTS = data.initPTS; - } // Due to asynchrony, initial PTS may arrive later than the first VTT fragments are loaded. // Parse any unparsed fragments upon receiving the initial PTS. @@ -95,26 +90,25 @@ class TimelineController extends EventHandler { } } - getExistingTrack(channelNumber) { + getExistingTrack (channelNumber) { const media = this.media; if (media) { for (let i = 0; i < media.textTracks.length; i++) { let textTrack = media.textTracks[i]; let propName = 'textTrack' + channelNumber; - if (textTrack[propName] === true) { + if (textTrack[propName] === true) return textTrack; - } } } return null; } - sendAddTrackEvent(track, media) { - var e = null; + sendAddTrackEvent (track, media) { + let e = null; try { e = new window.Event('addtrack'); } catch (err) { - //for IE11 + // for IE11 e = document.createEvent('Event'); e.initEvent('addtrack', false, false); } @@ -122,10 +116,10 @@ class TimelineController extends EventHandler { media.dispatchEvent(e); } - createCaptionsTrack(track) { + createCaptionsTrack (track) { let trackVar = 'textTrack' + track; if (!this[trackVar]) { - //Enable reuse of existing text track. + // Enable reuse of existing text track. let existingTrack = this.getExistingTrack(track); if (!existingTrack) { const textTrack = this.createTextTrack('captions', this.config['captionsTextTrack' + track + 'Label'], this.config['captionsTextTrack' + track + 'LanguageCode']); @@ -142,51 +136,46 @@ class TimelineController extends EventHandler { } } - createTextTrack(kind, label, lang) { + createTextTrack (kind, label, lang) { const media = this.media; if (media) - { return media.addTextTrack(kind, label, lang); - } } - destroy() { + destroy () { EventHandler.prototype.destroy.call(this); } - onMediaAttaching(data) { + onMediaAttaching (data) { this.media = data.media; this._cleanTracks(); } - onMediaDetaching() { + onMediaDetaching () { clearCurrentCues(this.textTrack1); clearCurrentCues(this.textTrack2); } - onManifestLoading() - { + onManifestLoading () { this.lastSn = -1; // Detect discontiguity in fragment parsing this.prevCC = -1; - this.vttCCs = {ccOffset: 0, presentationOffset: 0}; // Detect discontinuity in subtitle manifests + this.vttCCs = { ccOffset: 0, presentationOffset: 0 }; // Detect discontinuity in subtitle manifests this._cleanTracks(); - } - _cleanTracks() { + _cleanTracks () { // clear outdated subtitles const media = this.media; if (media) { const textTracks = media.textTracks; if (textTracks) { - for (let i = 0; i < textTracks.length; i++) { + for (let i = 0; i < textTracks.length; i++) clearCurrentCues(textTracks[i]); - } } } } - onManifestLoaded(data) { + onManifestLoaded (data) { this.textTracks = []; this.unparsedVttFrags = this.unparsedVttFrags || []; this.initPTS = undefined; @@ -201,41 +190,39 @@ class TimelineController extends EventHandler { if (index < inUseTracks.length) { const inUseTrack = inUseTracks[index]; // Reuse tracks with the same label, but do not reuse 608/708 tracks - if (reuseVttTextTrack(inUseTrack, track)) { + if (reuseVttTextTrack(inUseTrack, track)) textTrack = inUseTrack; - } } - if (!textTrack) { - textTrack = this.createTextTrack('subtitles', track.name, track.lang); - } - if (track.default) { + if (!textTrack) + textTrack = this.createTextTrack('subtitles', track.name, track.lang); + + if (track.default) textTrack.mode = this.hls.subtitleDisplay ? 'showing' : 'hidden'; - } else { + else textTrack.mode = 'disabled'; - } + this.textTracks.push(textTrack); }); } } - onLevelSwitching() { + onLevelSwitching () { this.enabled = this.hls.currentLevel.closedCaptions !== 'NONE'; } - onFragLoaded(data) { + onFragLoaded (data) { let frag = data.frag, payload = data.payload; if (frag.type === 'main') { - var sn = frag.sn; + let sn = frag.sn; // if this frag isn't contiguous, clear the parser so cues with bad start/end times aren't added to the textTrack if (sn !== this.lastSn + 1) { const cea608Parser = this.cea608Parser; - if (cea608Parser) { + if (cea608Parser) cea608Parser.reset(); - } } this.lastSn = sn; - } + } // eslint-disable-line brace-style // If fragment is subtitle type, parse as WebVTT. else if (frag.type === 'subtitle') { if (payload.byteLength) { @@ -245,20 +232,18 @@ class TimelineController extends EventHandler { return; } - var decryptData = frag.decryptdata; + let decryptData = frag.decryptdata; // If the subtitles are not encrypted, parse VTTs now. Otherwise, we need to wait. - if ((decryptData == null) || (decryptData.key == null) || (decryptData.method !== 'AES-128')) { + if ((decryptData == null) || (decryptData.key == null) || (decryptData.method !== 'AES-128')) this._parseVTTs(frag, payload); - } - } - else { + } else { // In case there is no payload, finish unsuccessfully. - this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, {success: false, frag: frag}); + this.hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag }); } } } - _parseVTTs(frag, payload) { + _parseVTTs (frag, payload) { let vttCCs = this.vttCCs; if (!vttCCs[frag.cc]) { vttCCs[frag.cc] = { start: frag.start, prevCC: this.prevCC, new: true }; @@ -269,34 +254,34 @@ class TimelineController extends EventHandler { // Parse the WebVTT file contents. WebVTTParser.parse(payload, this.initPTS, vttCCs, frag.cc, function (cues) { - const currentTrack = textTracks[frag.trackId]; - // Add cues and trigger event with success true. - cues.forEach(cue => { - // Sometimes there are cue overlaps on segmented vtts so the same - // cue can appear more than once in different vtt files. - // This avoid showing duplicated cues with same timecode and text. - if (!currentTrack.cues.getCueById(cue.id)) { - try { - currentTrack.addCue(cue); - } catch (err) { - const textTrackCue = new window.TextTrackCue(cue.startTime, cue.endTime, cue.text); - textTrackCue.id = cue.id; - currentTrack.addCue(textTrackCue); - } + const currentTrack = textTracks[frag.trackId]; + // Add cues and trigger event with success true. + cues.forEach(cue => { + // Sometimes there are cue overlaps on segmented vtts so the same + // cue can appear more than once in different vtt files. + // This avoid showing duplicated cues with same timecode and text. + if (!currentTrack.cues.getCueById(cue.id)) { + try { + currentTrack.addCue(cue); + } catch (err) { + const textTrackCue = new window.TextTrackCue(cue.startTime, cue.endTime, cue.text); + textTrackCue.id = cue.id; + currentTrack.addCue(textTrackCue); } - }); - hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, {success: true, frag: frag}); - }, - function (e) { - // Something went wrong while parsing. Trigger event with success false. - logger.log(`Failed to parse VTT cue: ${e}`); - hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, {success: false, frag: frag}); + } }); + hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: true, frag: frag }); + }, + function (e) { + // Something went wrong while parsing. Trigger event with success false. + logger.log(`Failed to parse VTT cue: ${e}`); + hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, { success: false, frag: frag }); + }); } - onFragDecrypted(data) { - var decryptedData = data.payload, - frag = data.frag; + onFragDecrypted (data) { + let decryptedData = data.payload, + frag = data.frag; if (frag.type === 'subtitle') { if (typeof this.initPTS === 'undefined') { @@ -308,37 +293,35 @@ class TimelineController extends EventHandler { } } - onFragParsingUserdata(data) { + onFragParsingUserdata (data) { // push all of the CEA-708 messages into the interpreter // immediately. It will create the proper timestamps based on our PTS value if (this.enabled && this.config.enableCEA708Captions) { - for (var i=0; i { + this.fastAesKey.expandKey() + .then((aesKey) => { // decrypt using web crypto - let crypto = new AESCrypto(subtle,iv); - crypto.decrypt(data, aesKey). - catch ((err) => { - this.onWebCryptoError(err, data, key, iv, callback); - }). - then((result) => { - callback(result); - }); - }). - catch ((err) => { + let crypto = new AESCrypto(subtle, iv); + crypto.decrypt(data, aesKey) + .catch((err) => { + this.onWebCryptoError(err, data, key, iv, callback); + }) + .then((result) => { + callback(result); + }); + }) + .catch((err) => { this.onWebCryptoError(err, data, key, iv, callback); }); } } - onWebCryptoError(err, data, key, iv, callback) { + onWebCryptoError (err, data, key, iv, callback) { if (this.config.enableSoftwareAES) { logger.log('WebCrypto Error, disable WebCrypto API'); this.disableWebCrypto = true; this.logEnabled = true; this.decrypt(data, key, iv, callback); - } - else { + } else { logger.error(`decrypting error : ${err.message}`); - this.observer.trigger(Event.ERROR, {type : ErrorTypes.MEDIA_ERROR, details : ErrorDetails.FRAG_DECRYPT_ERROR, fatal : true, reason : err.message}); + this.observer.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_DECRYPT_ERROR, fatal: true, reason: err.message }); } } - destroy() { + destroy () { let decryptor = this.decryptor; if (decryptor) { decryptor.destroy(); diff --git a/src/crypt/fast-aes-key.js b/src/crypt/fast-aes-key.js index cd6e517f509..998b02c62b4 100644 --- a/src/crypt/fast-aes-key.js +++ b/src/crypt/fast-aes-key.js @@ -1,11 +1,11 @@ class FastAESKey { - constructor(subtle,key) { + constructor (subtle, key) { this.subtle = subtle; this.key = key; } - expandKey() { - return this.subtle.importKey('raw', this.key, {name: 'AES-CBC'}, false, ['encrypt', 'decrypt']); + expandKey () { + return this.subtle.importKey('raw', this.key, { name: 'AES-CBC' }, false, ['encrypt', 'decrypt']); } } diff --git a/src/demux/aacdemuxer.js b/src/demux/aacdemuxer.js index 25df4a6d58d..b24cb383083 100644 --- a/src/demux/aacdemuxer.js +++ b/src/demux/aacdemuxer.js @@ -6,24 +6,23 @@ import { logger } from '../utils/logger'; import ID3 from '../demux/id3'; class AACDemuxer { - - constructor(observer, remuxer, config) { + constructor (observer, remuxer, config) { this.observer = observer; this.config = config; this.remuxer = remuxer; } - resetInitSegment(initSegment, audioCodec, videoCodec, duration) { + resetInitSegment (initSegment, audioCodec, videoCodec, duration) { this._audioTrack = { container: 'audio/adts', type: 'audio', id: 0, sequenceNumber: 0, isAAC: true, samples: [], len: 0, manifestCodec: audioCodec, duration: duration, inputTimeScale: 90000 }; } - resetTimeStamp() { + resetTimeStamp () { } - static probe(data) { - if (!data) { + static probe (data) { + if (!data) return false; - } + // Check for the ADTS sync word // Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1 // Layer bits (position 14 and 15) in header should be always 0 for ADTS @@ -41,7 +40,7 @@ class AACDemuxer { } // feed incoming data to the front of the parsing pipeline - append(data, timeOffset, contiguous, accurateTimeOffset) { + append (data, timeOffset, contiguous, accurateTimeOffset) { let track = this._audioTrack; let id3Data = ID3.getID3Data(data, 0) || []; let timestamp = ID3.getTimeStamp(id3Data); @@ -56,7 +55,7 @@ class AACDemuxer { while (offset < length - 1) { if (ADTS.isHeader(data, offset) && (offset + 5) < length) { ADTS.initTrackConfig(track, this.observer, data, offset, track.manifestCodec); - var frame = ADTS.appendFrame(track, data, offset, pts, frameIndex); + let frame = ADTS.appendFrame(track, data, offset, pts, frameIndex); if (frame) { offset += frame.length; stamp = frame.sample.pts; @@ -70,7 +69,7 @@ class AACDemuxer { id3Samples.push({ pts: stamp, dts: stamp, data: id3Data }); offset += id3Data.length; } else { - //nothing found, keep looking + // nothing found, keep looking offset++; } } @@ -84,9 +83,8 @@ class AACDemuxer { accurateTimeOffset); } - destroy() { + destroy () { } - } export default AACDemuxer; diff --git a/src/demux/adts.js b/src/demux/adts.js index 5c602ebb142..f87faf7a19c 100644 --- a/src/demux/adts.js +++ b/src/demux/adts.js @@ -4,8 +4,8 @@ import { logger } from '../utils/logger'; import { ErrorTypes, ErrorDetails } from '../errors'; -export function getAudioConfig(observer, data, offset, audioCodec) { - var adtsObjectType, // :int +export function getAudioConfig (observer, data, offset, audioCodec) { + let adtsObjectType, // :int adtsSampleingIndex, // :int adtsExtensionSampleingIndex, // :int adtsChanelConfig, // :int @@ -128,31 +128,31 @@ export function getAudioConfig(observer, data, offset, audioCodec) { return { config: config, samplerate: adtsSampleingRates[adtsSampleingIndex], channelCount: adtsChanelConfig, codec: ('mp4a.40.' + adtsObjectType), manifestCodec: manifestCodec }; } -export function isHeaderPattern(data, offset) { +export function isHeaderPattern (data, offset) { return data[offset] === 0xff && (data[offset + 1] & 0xf6) === 0xf0; } -export function getHeaderLength(data, offset) { - return (!!(data[offset + 1] & 0x01) ? 7 : 9); +export function getHeaderLength (data, offset) { + return (data[offset + 1] & 0x01 ? 7 : 9); } -export function getFullFrameLength(data, offset) { +export function getFullFrameLength (data, offset) { return ((data[offset + 3] & 0x03) << 11) | (data[offset + 4] << 3) | ((data[offset + 5] & 0xE0) >>> 5); } -export function isHeader(data, offset) { +export function isHeader (data, offset) { // Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1 // Layer bits (position 14 and 15) in header should be always 0 for ADTS // More info https://wiki.multimedia.cx/index.php?title=ADTS - if (offset + 1 < data.length && isHeaderPattern(data, offset)) { + if (offset + 1 < data.length && isHeaderPattern(data, offset)) return true; - } + return false; } -export function probe(data, offset) { +export function probe (data, offset) { // same as isHeader but we also check that ADTS frame follows last ADTS frame // or end of data is reached if (offset + 1 < data.length && isHeaderPattern(data, offset)) { @@ -160,20 +160,19 @@ export function probe(data, offset) { let headerLength = getHeaderLength(data, offset); // ADTS frame Length let frameLength = headerLength; - if (offset + 5 < data.length) { + if (offset + 5 < data.length) frameLength = getFullFrameLength(data, offset); - } + let newOffset = offset + frameLength; - if (newOffset === data.length || (newOffset + 1 < data.length && isHeaderPattern(data, newOffset))) { + if (newOffset === data.length || (newOffset + 1 < data.length && isHeaderPattern(data, newOffset))) return true; - } } return false; } -export function initTrackConfig(track, observer, data, offset, audioCodec) { +export function initTrackConfig (track, observer, data, offset, audioCodec) { if (!track.samplerate) { - var config = getAudioConfig(observer, data, offset, audioCodec); + let config = getAudioConfig(observer, data, offset, audioCodec); track.config = config.config; track.samplerate = config.samplerate; track.channelCount = config.channelCount; @@ -183,13 +182,13 @@ export function initTrackConfig(track, observer, data, offset, audioCodec) { } } -export function getFrameDuration(samplerate) { +export function getFrameDuration (samplerate) { return 1024 * 90000 / samplerate; } -export function parseFrameHeader(data, offset, pts, frameIndex, frameDuration) { - var headerLength, frameLength, stamp; - var length = data.length; +export function parseFrameHeader (data, offset, pts, frameIndex, frameDuration) { + let headerLength, frameLength, stamp; + let length = data.length; // The protection skip bit tells us if we have 2 bytes of CRC data at the end of the ADTS header headerLength = getHeaderLength(data, offset); @@ -199,23 +198,23 @@ export function parseFrameHeader(data, offset, pts, frameIndex, frameDuration) { if ((frameLength > 0) && ((offset + headerLength + frameLength) <= length)) { stamp = pts + frameIndex * frameDuration; - //logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}/${(stamp/90).toFixed(0)}`); + // logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}/${(stamp/90).toFixed(0)}`); return { headerLength, frameLength, stamp }; } return undefined; } -export function appendFrame(track, data, offset, pts, frameIndex) { - var frameDuration = getFrameDuration(track.samplerate); - var header = parseFrameHeader(data, offset, pts, frameIndex, frameDuration); +export function appendFrame (track, data, offset, pts, frameIndex) { + let frameDuration = getFrameDuration(track.samplerate); + let header = parseFrameHeader(data, offset, pts, frameIndex, frameDuration); if (header) { - var stamp = header.stamp; - var headerLength = header.headerLength; - var frameLength = header.frameLength; + let stamp = header.stamp; + let headerLength = header.headerLength; + let frameLength = header.frameLength; - //logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}/${(stamp/90).toFixed(0)}`); - var aacSample = { + // logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}/${(stamp/90).toFixed(0)}`); + let aacSample = { unit: data.subarray(offset + headerLength, offset + headerLength + frameLength), pts: stamp, dts: stamp @@ -224,9 +223,8 @@ export function appendFrame(track, data, offset, pts, frameIndex) { track.samples.push(aacSample); track.len += frameLength; - return {sample: aacSample, length: frameLength + headerLength}; + return { sample: aacSample, length: frameLength + headerLength }; } return undefined; } - diff --git a/src/demux/demuxer-inline.js b/src/demux/demuxer-inline.js index 95763a9440f..5a08e68eafa 100644 --- a/src/demux/demuxer-inline.js +++ b/src/demux/demuxer-inline.js @@ -13,37 +13,35 @@ import MP4Remuxer from '../remux/mp4-remuxer'; import PassThroughRemuxer from '../remux/passthrough-remuxer'; class DemuxerInline { - - constructor(observer, typeSupported, config, vendor) { + constructor (observer, typeSupported, config, vendor) { this.observer = observer; this.typeSupported = typeSupported; this.config = config; this.vendor = vendor; } - destroy() { - var demuxer = this.demuxer; - if (demuxer) { + destroy () { + let demuxer = this.demuxer; + if (demuxer) demuxer.destroy(); - } } - push(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS) { + push (data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS) { if ((data.byteLength > 0) && (decryptdata != null) && (decryptdata.key != null) && (decryptdata.method === 'AES-128')) { let decrypter = this.decrypter; - if (decrypter == null) { + if (decrypter == null) decrypter = this.decrypter = new Decrypter(this.observer, this.config); - } - var localthis = this; + + let localthis = this; // performance.now() not available on WebWorker, at least on Safari Desktop - var startTime; + let startTime; try { startTime = performance.now(); } catch (error) { startTime = Date.now(); } decrypter.decrypt(data, decryptdata.key.buffer, decryptdata.iv.buffer, function (decryptedData) { - var endTime; + let endTime; try { endTime = performance.now(); } catch (error) { @@ -57,8 +55,8 @@ class DemuxerInline { } } - pushDecrypted(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS) { - var demuxer = this.demuxer; + pushDecrypted (data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS) { + let demuxer = this.demuxer; if (!demuxer || // in case of continuity change, or track switch // we might switch from content type (AAC container to TS container, or TS to fmp4 for example) @@ -102,9 +100,9 @@ class DemuxerInline { demuxer.resetTimeStamp(defaultInitPTS); remuxer.resetTimeStamp(defaultInitPTS); } - if (typeof demuxer.setDecryptData === 'function') { + if (typeof demuxer.setDecryptData === 'function') demuxer.setDecryptData(decryptdata); - } + demuxer.append(data, timeOffset, contiguous, accurateTimeOffset); } } diff --git a/src/demux/demuxer-worker.js b/src/demux/demuxer-worker.js index 074189ecad0..1c4d80464cc 100644 --- a/src/demux/demuxer-worker.js +++ b/src/demux/demuxer-worker.js @@ -3,14 +3,14 @@ * - provides MP4 Boxes back to main thread using [transferable objects](https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast) in order to minimize message passing overhead. */ - import DemuxerInline from '../demux/demuxer-inline'; - import Event from '../events'; - import {enableLogs} from '../utils/logger'; - import EventEmitter from 'events'; +import DemuxerInline from '../demux/demuxer-inline'; +import Event from '../events'; +import { enableLogs } from '../utils/logger'; +import EventEmitter from 'events'; -var DemuxerWorker = function (self) { +let DemuxerWorker = function (self) { // observer setup - var observer = new EventEmitter(); + let observer = new EventEmitter(); observer.trigger = function trigger (event, ...data) { observer.emit(event, event, ...data); }; @@ -19,30 +19,30 @@ var DemuxerWorker = function (self) { observer.removeListener(event, ...data); }; - var forwardMessage = function(ev,data) { - self.postMessage({event: ev, data:data }); + let forwardMessage = function (ev, data) { + self.postMessage({ event: ev, data: data }); }; self.addEventListener('message', function (ev) { - var data = ev.data; - //console.log('demuxer cmd:' + data.cmd); + let data = ev.data; + // console.log('demuxer cmd:' + data.cmd); switch (data.cmd) { - case 'init': - let config = JSON.parse(data.config); - self.demuxer = new DemuxerInline(observer, data.typeSupported, config, data.vendor); - try { - enableLogs(config.debug === true); - } catch(err) { - console.warn('demuxerWorker: unable to enable logs'); - } - // signal end of worker init - forwardMessage('init',null); - break; - case 'demux': - self.demuxer.push(data.data, data.decryptdata, data.initSegment, data.audioCodec, data.videoCodec, data.timeOffset,data.discontinuity, data.trackSwitch,data.contiguous,data.duration,data.accurateTimeOffset,data.defaultInitPTS); - break; - default: - break; + case 'init': + let config = JSON.parse(data.config); + self.demuxer = new DemuxerInline(observer, data.typeSupported, config, data.vendor); + try { + enableLogs(config.debug === true); + } catch (err) { + console.warn('demuxerWorker: unable to enable logs'); + } + // signal end of worker init + forwardMessage('init', null); + break; + case 'demux': + self.demuxer.push(data.data, data.decryptdata, data.initSegment, data.audioCodec, data.videoCodec, data.timeOffset, data.discontinuity, data.trackSwitch, data.contiguous, data.duration, data.accurateTimeOffset, data.defaultInitPTS); + break; + default: + break; } }); @@ -56,9 +56,9 @@ var DemuxerWorker = function (self) { observer.on(Event.INIT_PTS_FOUND, forwardMessage); // special case for FRAG_PARSING_DATA: pass data1/data2 as transferable object (no copy) - observer.on(Event.FRAG_PARSING_DATA, function(ev, data) { + observer.on(Event.FRAG_PARSING_DATA, function (ev, data) { let transferable = []; - let message = {event: ev, data:data}; + let message = { event: ev, data: data }; if (data.data1) { message.data1 = data.data1.buffer; transferable.push(data.data1.buffer); @@ -69,9 +69,8 @@ var DemuxerWorker = function (self) { transferable.push(data.data2.buffer); delete data.data2; } - self.postMessage(message,transferable); + self.postMessage(message, transferable); }); }; export default DemuxerWorker; - diff --git a/src/demux/demuxer.js b/src/demux/demuxer.js index 09860169f35..506d356294b 100644 --- a/src/demux/demuxer.js +++ b/src/demux/demuxer.js @@ -1,16 +1,15 @@ import Event from '../events'; import DemuxerInline from '../demux/demuxer-inline'; -import {logger} from '../utils/logger'; -import {ErrorTypes, ErrorDetails} from '../errors'; +import { logger } from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; import EventEmitter from 'events'; import work from 'webworkify-webpack'; -import {getMediaSource} from '../helper/mediasource-helper'; +import { getMediaSource } from '../helper/mediasource-helper'; const MediaSource = getMediaSource(); class Demuxer { - - constructor(hls, id) { + constructor (hls, id) { this.hls = hls; this.id = id; // observer setup @@ -24,11 +23,11 @@ class Demuxer { observer.removeListener(event, ...data); }; - var forwardMessage = function(ev,data) { + let forwardMessage = function (ev, data) { data = data || {}; data.frag = this.frag; data.id = this.id; - hls.trigger(ev,data); + hls.trigger(ev, data); }.bind(this); // forward events to main thread @@ -42,37 +41,37 @@ class Demuxer { observer.on(Event.INIT_PTS_FOUND, forwardMessage); const typeSupported = { - mp4 : MediaSource.isTypeSupported('video/mp4'), + mp4: MediaSource.isTypeSupported('video/mp4'), mpeg: MediaSource.isTypeSupported('audio/mpeg'), mp3: MediaSource.isTypeSupported('audio/mp4; codecs="mp3"') }; // navigator.vendor is not always available in Web Worker // refer to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/navigator const vendor = navigator.vendor; - if (config.enableWorker && (typeof(Worker) !== 'undefined')) { - logger.log('demuxing in webworker'); - let w; - try { - w = this.w = work(require.resolve('../demux/demuxer-worker.js')); - this.onwmsg = this.onWorkerMessage.bind(this); - w.addEventListener('message', this.onwmsg); - w.onerror = function(event) { hls.trigger(Event.ERROR, {type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: true, event : 'demuxerWorker', err : { message : event.message + ' (' + event.filename + ':' + event.lineno + ')' }});}; - w.postMessage({cmd: 'init', typeSupported : typeSupported, vendor : vendor, id : id, config: JSON.stringify(config)}); - } catch(err) { - logger.error('error while initializing DemuxerWorker, fallback on DemuxerInline'); - if (w) { - // revoke the Object URL that was used to create demuxer worker, so as not to leak it - URL.revokeObjectURL(w.objectURL); - } - this.demuxer = new DemuxerInline(observer,typeSupported,config,vendor); - this.w = undefined; + if (config.enableWorker && (typeof (Worker) !== 'undefined')) { + logger.log('demuxing in webworker'); + let w; + try { + w = this.w = work(require.resolve('../demux/demuxer-worker.js')); + this.onwmsg = this.onWorkerMessage.bind(this); + w.addEventListener('message', this.onwmsg); + w.onerror = function (event) { hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: true, event: 'demuxerWorker', err: { message: event.message + ' (' + event.filename + ':' + event.lineno + ')' } }); }; + w.postMessage({ cmd: 'init', typeSupported: typeSupported, vendor: vendor, id: id, config: JSON.stringify(config) }); + } catch (err) { + logger.error('error while initializing DemuxerWorker, fallback on DemuxerInline'); + if (w) { + // revoke the Object URL that was used to create demuxer worker, so as not to leak it + URL.revokeObjectURL(w.objectURL); } - } else { - this.demuxer = new DemuxerInline(observer,typeSupported,config, vendor); + this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor); + this.w = undefined; } + } else { + this.demuxer = new DemuxerInline(observer, typeSupported, config, vendor); + } } - destroy() { + destroy () { let w = this.w; if (w) { w.removeEventListener('message', this.onwmsg); @@ -92,57 +91,55 @@ class Demuxer { } } - push(data, initSegment, audioCodec, videoCodec, frag, duration,accurateTimeOffset,defaultInitPTS) { + push (data, initSegment, audioCodec, videoCodec, frag, duration, accurateTimeOffset, defaultInitPTS) { const w = this.w; - const timeOffset = !isNaN(frag.startDTS) ? frag.startDTS : frag.start; + const timeOffset = !isNaN(frag.startDTS) ? frag.startDTS : frag.start; const decryptdata = frag.decryptdata; const lastFrag = this.frag; const discontinuity = !(lastFrag && (frag.cc === lastFrag.cc)); const trackSwitch = !(lastFrag && (frag.level === lastFrag.level)); - const nextSN = lastFrag && (frag.sn === (lastFrag.sn+1)); + const nextSN = lastFrag && (frag.sn === (lastFrag.sn + 1)); const contiguous = !trackSwitch && nextSN; - if (discontinuity) { + if (discontinuity) logger.log(`${this.id}:discontinuity detected`); - } - if (trackSwitch) { + + if (trackSwitch) logger.log(`${this.id}:switch detected`); - } + this.frag = frag; if (w) { // post fragment payload as transferable objects for ArrayBuffer (no copy) - w.postMessage({cmd: 'demux', data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset,defaultInitPTS}, data instanceof ArrayBuffer ? [data] : []); + w.postMessage({ cmd: 'demux', data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS }, data instanceof ArrayBuffer ? [data] : []); } else { let demuxer = this.demuxer; - if (demuxer) { - demuxer.push(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset,defaultInitPTS); - } + if (demuxer) + demuxer.push(data, decryptdata, initSegment, audioCodec, videoCodec, timeOffset, discontinuity, trackSwitch, contiguous, duration, accurateTimeOffset, defaultInitPTS); } } - onWorkerMessage(ev) { + onWorkerMessage (ev) { let data = ev.data, - hls = this.hls; - switch(data.event) { - case 'init': - // revoke the Object URL that was used to create demuxer worker, so as not to leak it - URL.revokeObjectURL(this.w.objectURL); - break; + hls = this.hls; + switch (data.event) { + case 'init': + // revoke the Object URL that was used to create demuxer worker, so as not to leak it + URL.revokeObjectURL(this.w.objectURL); + break; // special case for FRAG_PARSING_DATA: data1 and data2 are transferable objects - case Event.FRAG_PARSING_DATA: - data.data.data1 = new Uint8Array(data.data1); - if (data.data2) { - data.data.data2 = new Uint8Array(data.data2); - } - /* falls through */ - default: - data.data = data.data || {}; - data.data.frag = this.frag; - data.data.id = this.id; - hls.trigger(data.event, data.data); - break; + case Event.FRAG_PARSING_DATA: + data.data.data1 = new Uint8Array(data.data1); + if (data.data2) + data.data.data2 = new Uint8Array(data.data2); + + /* falls through */ + default: + data.data = data.data || {}; + data.data.frag = this.frag; + data.data.id = this.id; + hls.trigger(data.event, data.data); + break; } } } export default Demuxer; - diff --git a/src/demux/exp-golomb.js b/src/demux/exp-golomb.js index cb44ae448df..d479e981f95 100644 --- a/src/demux/exp-golomb.js +++ b/src/demux/exp-golomb.js @@ -2,11 +2,10 @@ * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264. */ -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; class ExpGolomb { - - constructor(data) { + constructor (data) { this.data = data; // the number of bytes left to examine in this.data this.bytesAvailable = data.byteLength; @@ -17,16 +16,16 @@ class ExpGolomb { } // ():void - loadWord() { - var + loadWord () { + let data = this.data, bytesAvailable = this.bytesAvailable, position = data.byteLength - bytesAvailable, workingBytes = new Uint8Array(4), availableBytes = Math.min(4, bytesAvailable); - if (availableBytes === 0) { + if (availableBytes === 0) throw new Error('no bytes available'); - } + workingBytes.set(data.subarray(position, position + availableBytes)); this.word = new DataView(workingBytes.buffer).getUint32(0); // track the amount of this.data that has been processed @@ -35,8 +34,8 @@ class ExpGolomb { } // (count:int):void - skipBits(count) { - var skipBytes; // :int + skipBits (count) { + let skipBytes; // :int if (this.bitsAvailable > count) { this.word <<= count; this.bitsAvailable -= count; @@ -52,32 +51,31 @@ class ExpGolomb { } // (size:int):uint - readBits(size) { - var + readBits (size) { + let bits = Math.min(this.bitsAvailable, size), // :uint valu = this.word >>> (32 - bits); // :uint - if (size > 32) { + if (size > 32) logger.error('Cannot read more than 32 bits at a time'); - } + this.bitsAvailable -= bits; - if (this.bitsAvailable > 0) { + if (this.bitsAvailable > 0) this.word <<= bits; - } else if (this.bytesAvailable > 0) { + else if (this.bytesAvailable > 0) this.loadWord(); - } + bits = size - bits; - if (bits > 0 && this.bitsAvailable) { + if (bits > 0 && this.bitsAvailable) return valu << bits | this.readBits(bits); - } else { + else return valu; - } } // ():uint - skipLZ() { - var leadingZeroCount; // :uint + skipLZ () { + let leadingZeroCount; // :uint for (leadingZeroCount = 0; leadingZeroCount < this.bitsAvailable; ++leadingZeroCount) { - if (0 !== (this.word & (0x80000000 >>> leadingZeroCount))) { + if ((this.word & (0x80000000 >>> leadingZeroCount)) !== 0) { // the first bit of working word is 1 this.word <<= leadingZeroCount; this.bitsAvailable -= leadingZeroCount; @@ -90,24 +88,24 @@ class ExpGolomb { } // ():void - skipUEG() { + skipUEG () { this.skipBits(1 + this.skipLZ()); } // ():void - skipEG() { + skipEG () { this.skipBits(1 + this.skipLZ()); } // ():uint - readUEG() { - var clz = this.skipLZ(); // :uint + readUEG () { + let clz = this.skipLZ(); // :uint return this.readBits(clz + 1) - 1; } // ():int - readEG() { - var valu = this.readUEG(); // :int + readEG () { + let valu = this.readUEG(); // :int if (0x01 & valu) { // the number is odd if the low order bit is set return (1 + valu) >>> 1; // add 1 to make it even, and divide by 2 @@ -118,21 +116,21 @@ class ExpGolomb { // Some convenience functions // :Boolean - readBoolean() { - return 1 === this.readBits(1); + readBoolean () { + return this.readBits(1) === 1; } // ():int - readUByte() { + readUByte () { return this.readBits(8); } // ():int - readUShort() { + readUShort () { return this.readBits(16); } - // ():int - readUInt() { + // ():int + readUInt () { return this.readBits(32); } @@ -143,8 +141,8 @@ class ExpGolomb { * @param count {number} the number of entries in this scaling list * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1 */ - skipScalingList(count) { - var + skipScalingList (count) { + let lastScale = 8, nextScale = 8, j, @@ -167,13 +165,13 @@ class ExpGolomb { * sequence parameter set, including the dimensions of the * associated video frames. */ - readSPS() { - var + readSPS () { + let frameCropLeftOffset = 0, frameCropRightOffset = 0, frameCropTopOffset = 0, frameCropBottomOffset = 0, - profileIdc,profileCompat,levelIdc, + profileIdc, profileCompat, levelIdc, numRefFramesInPicOrderCntCycle, picWidthInMbsMinus1, picHeightInMapUnitsMinus1, frameMbsOnlyFlag, @@ -192,22 +190,22 @@ class ExpGolomb { profileIdc = readUByte(); // profile_idc profileCompat = readBits(5); // constraint_set[0-4]_flag, u(5) skipBits(3); // reserved_zero_3bits u(3), - levelIdc = readUByte(); //level_idc u(8) + levelIdc = readUByte(); // level_idc u(8) skipUEG(); // seq_parameter_set_id // some profiles have more optional data we don't need if (profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || - profileIdc === 44 || - profileIdc === 83 || - profileIdc === 86 || + profileIdc === 44 || + profileIdc === 83 || + profileIdc === 86 || profileIdc === 118 || profileIdc === 128) { - var chromaFormatIdc = readUEG(); - if (chromaFormatIdc === 3) { + let chromaFormatIdc = readUEG(); + if (chromaFormatIdc === 3) skipBits(1); // separate_colour_plane_flag - } + skipUEG(); // bit_depth_luma_minus8 skipUEG(); // bit_depth_chroma_minus8 skipBits(1); // qpprime_y_zero_transform_bypass_flag @@ -215,36 +213,34 @@ class ExpGolomb { scalingListCount = (chromaFormatIdc !== 3) ? 8 : 12; for (i = 0; i < scalingListCount; i++) { if (readBoolean()) { // seq_scaling_list_present_flag[ i ] - if (i < 6) { + if (i < 6) skipScalingList(16); - } else { + else skipScalingList(64); - } } } } } skipUEG(); // log2_max_frame_num_minus4 - var picOrderCntType = readUEG(); + let picOrderCntType = readUEG(); if (picOrderCntType === 0) { - readUEG(); //log2_max_pic_order_cnt_lsb_minus4 + readUEG(); // log2_max_pic_order_cnt_lsb_minus4 } else if (picOrderCntType === 1) { skipBits(1); // delta_pic_order_always_zero_flag skipEG(); // offset_for_non_ref_pic skipEG(); // offset_for_top_to_bottom_field numRefFramesInPicOrderCntCycle = readUEG(); - for(i = 0; i < numRefFramesInPicOrderCntCycle; i++) { + for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) skipEG(); // offset_for_ref_frame[ i ] - } } skipUEG(); // max_num_ref_frames skipBits(1); // gaps_in_frame_num_value_allowed_flag picWidthInMbsMinus1 = readUEG(); picHeightInMapUnitsMinus1 = readUEG(); frameMbsOnlyFlag = readBits(1); - if (frameMbsOnlyFlag === 0) { + if (frameMbsOnlyFlag === 0) skipBits(1); // mb_adaptive_frame_field_flag - } + skipBits(1); // direct_8x8_inference_flag if (readBoolean()) { // frame_cropping_flag frameCropLeftOffset = readUEG(); @@ -252,44 +248,44 @@ class ExpGolomb { frameCropTopOffset = readUEG(); frameCropBottomOffset = readUEG(); } - let pixelRatio = [1,1]; + let pixelRatio = [1, 1]; if (readBoolean()) { // vui_parameters_present_flag if (readBoolean()) { // aspect_ratio_info_present_flag const aspectRatioIdc = readUByte(); switch (aspectRatioIdc) { - case 1: pixelRatio = [1,1]; break; - case 2: pixelRatio = [12,11]; break; - case 3: pixelRatio = [10,11]; break; - case 4: pixelRatio = [16,11]; break; - case 5: pixelRatio = [40,33]; break; - case 6: pixelRatio = [24,11]; break; - case 7: pixelRatio = [20,11]; break; - case 8: pixelRatio = [32,11]; break; - case 9: pixelRatio = [80,33]; break; - case 10: pixelRatio = [18,11]; break; - case 11: pixelRatio = [15,11]; break; - case 12: pixelRatio = [64,33]; break; - case 13: pixelRatio = [160,99]; break; - case 14: pixelRatio = [4,3]; break; - case 15: pixelRatio = [3,2]; break; - case 16: pixelRatio = [2,1]; break; - case 255: { - pixelRatio = [readUByte() << 8 | readUByte(), readUByte() << 8 | readUByte()]; - break; - } + case 1: pixelRatio = [1, 1]; break; + case 2: pixelRatio = [12, 11]; break; + case 3: pixelRatio = [10, 11]; break; + case 4: pixelRatio = [16, 11]; break; + case 5: pixelRatio = [40, 33]; break; + case 6: pixelRatio = [24, 11]; break; + case 7: pixelRatio = [20, 11]; break; + case 8: pixelRatio = [32, 11]; break; + case 9: pixelRatio = [80, 33]; break; + case 10: pixelRatio = [18, 11]; break; + case 11: pixelRatio = [15, 11]; break; + case 12: pixelRatio = [64, 33]; break; + case 13: pixelRatio = [160, 99]; break; + case 14: pixelRatio = [4, 3]; break; + case 15: pixelRatio = [3, 2]; break; + case 16: pixelRatio = [2, 1]; break; + case 255: { + pixelRatio = [readUByte() << 8 | readUByte(), readUByte() << 8 | readUByte()]; + break; + } } } } return { width: Math.ceil((((picWidthInMbsMinus1 + 1) * 16) - frameCropLeftOffset * 2 - frameCropRightOffset * 2)), - height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) - ((frameMbsOnlyFlag? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)), - pixelRatio : pixelRatio + height: ((2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16) - ((frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)), + pixelRatio: pixelRatio }; } - readSliceType() { + readSliceType () { // skip NALu type this.readUByte(); // discard first_mb_in_slice diff --git a/src/demux/id3.js b/src/demux/id3.js index f4008a82a3b..0f6450cd16a 100644 --- a/src/demux/id3.js +++ b/src/demux/id3.js @@ -1,14 +1,14 @@ /** * ID3 parser */ - class ID3 { +class ID3 { /** * Returns true if an ID3 header can be found at offset in data * @param {Uint8Array} data - The data to search in * @param {number} offset - The offset at which to start searching * @return {boolean} - True if an ID3 header is found */ - static isHeader(data, offset) { + static isHeader (data, offset) { /* * http://id3.org/id3v2.3.0 * [0] = 'I' @@ -23,14 +23,13 @@ * Where yy is less than $FF, xx is the 'flags' byte and zz is less than $80 */ if (offset + 10 <= data.length) { - //look for 'ID3' identifier - if (data[offset] === 0x49 && data[offset+1] === 0x44 && data[offset+2] === 0x33) { - //check version is within range - if (data[offset+3] < 0xFF && data[offset+4] < 0xFF) { - //check size is within range - if (data[offset+6] < 0x80 && data[offset+7] < 0x80 && data[offset+8] < 0x80 && data[offset+9] < 0x80) { + // look for 'ID3' identifier + if (data[offset] === 0x49 && data[offset + 1] === 0x44 && data[offset + 2] === 0x33) { + // check version is within range + if (data[offset + 3] < 0xFF && data[offset + 4] < 0xFF) { + // check size is within range + if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) return true; - } } } } @@ -44,19 +43,18 @@ * @param {number} offset - The offset at which to start searching * @return {boolean} - True if an ID3 footer is found */ - static isFooter(data, offset) { + static isFooter (data, offset) { /* * The footer is a copy of the header, but with a different identifier */ if (offset + 10 <= data.length) { - //look for '3DI' identifier - if (data[offset] === 0x33 && data[offset+1] === 0x44 && data[offset+2] === 0x49) { - //check version is within range - if (data[offset+3] < 0xFF && data[offset+4] < 0xFF) { - //check size is within range - if (data[offset+6] < 0x80 && data[offset+7] < 0x80 && data[offset+8] < 0x80 && data[offset+9] < 0x80) { + // look for '3DI' identifier + if (data[offset] === 0x33 && data[offset + 1] === 0x44 && data[offset + 2] === 0x49) { + // check version is within range + if (data[offset + 3] < 0xFF && data[offset + 4] < 0xFF) { + // check size is within range + if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) return true; - } } } } @@ -70,38 +68,37 @@ * @param {number} offset - The offset at which to start searching * @return {Uint8Array} - The block of data containing any ID3 tags found */ - static getID3Data(data, offset) { + static getID3Data (data, offset) { const front = offset; let length = 0; while (ID3.isHeader(data, offset)) { - //ID3 header is 10 bytes + // ID3 header is 10 bytes length += 10; const size = ID3._readSize(data, offset + 6); length += size; if (ID3.isFooter(data, offset + 10)) { - //ID3 footer is 10 bytes + // ID3 footer is 10 bytes length += 10; } offset += length; } - if (length > 0) { + if (length > 0) return data.subarray(front, front + length); - } return undefined; } - static _readSize(data, offset) { + static _readSize (data, offset) { let size = 0; - size = ((data[offset] & 0x7f) << 21); - size |= ((data[offset+1] & 0x7f) << 14); - size |= ((data[offset+2] & 0x7f) << 7); - size |= (data[offset+3] & 0x7f); + size = ((data[offset] & 0x7f) << 21); + size |= ((data[offset + 1] & 0x7f) << 14); + size |= ((data[offset + 2] & 0x7f) << 7); + size |= (data[offset + 3] & 0x7f); return size; } @@ -110,13 +107,12 @@ * @param {Uint8Array} data - Block of data containing one or more ID3 tags * @return {number} - The timestamp */ - static getTimeStamp(data) { + static getTimeStamp (data) { const frames = ID3.getID3Frames(data); - for(let i = 0; i < frames.length; i++) { + for (let i = 0; i < frames.length; i++) { const frame = frames[i]; - if (ID3.isTimeStampFrame(frame)) { + if (ID3.isTimeStampFrame(frame)) return ID3._readTimeStamp(frame); - } } return undefined; @@ -126,11 +122,11 @@ * Returns true if the ID3 frame is an Elementary Stream timestamp frame * @param {ID3 frame} frame */ - static isTimeStampFrame(frame) { + static isTimeStampFrame (frame) { return (frame && frame.key === 'PRIV' && frame.info === 'com.apple.streaming.transportStreamTimestamp'); } - static _getFrameData(data) { + static _getFrameData (data) { /* Frame ID $xx xx xx xx (four characters) Size $xx xx xx xx @@ -139,7 +135,7 @@ const type = String.fromCharCode(data[0], data[1], data[2], data[3]); const size = ID3._readSize(data, 4); - //skip frame id, size, and flags + // skip frame id, size, and flags let offset = 10; return { type, size, data: data.subarray(offset, offset + size) }; @@ -150,47 +146,45 @@ * @param {Uint8Array} id3Data - The ID3 data containing one or more ID3 tags * @return {ID3 frame[]} - Array of ID3 frame objects */ - static getID3Frames(id3Data) { + static getID3Frames (id3Data) { let offset = 0; const frames = []; while (ID3.isHeader(id3Data, offset)) { const size = ID3._readSize(id3Data, offset + 6); - //skip past ID3 header + // skip past ID3 header offset += 10; const end = offset + size; - //loop through frames in the ID3 tag + // loop through frames in the ID3 tag while (offset + 8 < end) { const frameData = ID3._getFrameData(id3Data.subarray(offset)); const frame = ID3._decodeFrame(frameData); - if (frame) { + if (frame) frames.push(frame); - } - //skip frame header and frame data + + // skip frame header and frame data offset += frameData.size + 10; } - if (ID3.isFooter(id3Data, offset)) { + if (ID3.isFooter(id3Data, offset)) offset += 10; - } } return frames; } - static _decodeFrame(frame) { - if (frame.type === 'PRIV') { + static _decodeFrame (frame) { + if (frame.type === 'PRIV') return ID3._decodePrivFrame(frame); - } else if (frame.type[0] === 'T') { + else if (frame.type[0] === 'T') return ID3._decodeTextFrame(frame); - } else if (frame.type[0] === 'W') { + else if (frame.type[0] === 'W') return ID3._decodeURLFrame(frame); - } return undefined; } - static _readTimeStamp(timeStampFrame) { + static _readTimeStamp (timeStampFrame) { if (timeStampFrame.data.byteLength === 8) { const data = new Uint8Array(timeStampFrame.data); // timestamp is 33 bit expressed as a big-endian eight-octet number, @@ -198,13 +192,12 @@ const pts33Bit = data[3] & 0x1; let timestamp = (data[4] << 23) + (data[5] << 15) + - (data[6] << 7) + + (data[6] << 7) + data[7]; timestamp /= 45; - if (pts33Bit) { + if (pts33Bit) timestamp += 47721858.84; // 2^32 / 90 - } return Math.round(timestamp); } @@ -212,13 +205,12 @@ return undefined; } - static _decodePrivFrame(frame) { + static _decodePrivFrame (frame) { /* Format: \0 */ - if (frame.size < 2) { + if (frame.size < 2) return undefined; - } const owner = ID3._utf8ArrayToStr(frame.data, true); const privateData = new Uint8Array(frame.data.subarray(owner.length + 1)); @@ -226,10 +218,9 @@ return { key: frame.type, info: owner, data: privateData.buffer }; } - static _decodeTextFrame(frame) { - if (frame.size < 2) { + static _decodeTextFrame (frame) { + if (frame.size < 2) return undefined; - } if (frame.type === 'TXXX') { /* @@ -255,16 +246,15 @@ } } - static _decodeURLFrame(frame) { + static _decodeURLFrame (frame) { if (frame.type === 'WXXX') { /* Format: [0] = {Text Encoding} [1-?] = {Description}\0{URL} */ - if (frame.size < 2) { + if (frame.size < 2) return undefined; - } let index = 1; const description = ID3._utf8ArrayToStr(frame.data.subarray(index)); @@ -292,8 +282,7 @@ * LastModified: Dec 25 1999 * This library is free. You can redistribute it and/or modify it. */ - static _utf8ArrayToStr(array, exitOnNull = false) { - + static _utf8ArrayToStr (array, exitOnNull = false) { const len = array.length; let c; let char2; @@ -301,33 +290,33 @@ let out = ''; let i = 0; while (i < len) { - c = array[i++]; - if (c === 0x00 && exitOnNull) { - return out; - } else if (c === 0x00 || c === 0x03) { - // If the character is 3 (END_OF_TEXT) or 0 (NULL) then skip it - continue; - } - switch (c >> 4) { - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: - // 0xxxxxxx - out += String.fromCharCode(c); - break; - case 12: case 13: - // 110x xxxx 10xx xxxx - char2 = array[i++]; - out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); - break; - case 14: - // 1110 xxxx 10xx xxxx 10xx xxxx - char2 = array[i++]; - char3 = array[i++]; - out += String.fromCharCode(((c & 0x0F) << 12) | + c = array[i++]; + if (c === 0x00 && exitOnNull) { + return out; + } else if (c === 0x00 || c === 0x03) { + // If the character is 3 (END_OF_TEXT) or 0 (NULL) then skip it + continue; + } + switch (c >> 4) { + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: + // 0xxxxxxx + out += String.fromCharCode(c); + break; + case 12: case 13: + // 110x xxxx 10xx xxxx + char2 = array[i++]; + out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); + break; + case 14: + // 1110 xxxx 10xx xxxx 10xx xxxx + char2 = array[i++]; + char3 = array[i++]; + out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); - break; - default: - } + break; + default: + } } return out; } diff --git a/src/demux/mp3demuxer.js b/src/demux/mp3demuxer.js index 1c949b99fd3..b0eb1d1f774 100644 --- a/src/demux/mp3demuxer.js +++ b/src/demux/mp3demuxer.js @@ -6,23 +6,22 @@ import { logger } from '../utils/logger'; import MpegAudio from './mpegaudio'; class MP3Demuxer { - - constructor(observer, remuxer, config) { + constructor (observer, remuxer, config) { this.observer = observer; this.config = config; this.remuxer = remuxer; } - resetInitSegment(initSegment, audioCodec, videoCodec, duration) { + resetInitSegment (initSegment, audioCodec, videoCodec, duration) { this._audioTrack = { container: 'audio/mpeg', type: 'audio', id: -1, sequenceNumber: 0, isAAC: false, samples: [], len: 0, manifestCodec: audioCodec, duration: duration, inputTimeScale: 90000 }; } - resetTimeStamp() { + resetTimeStamp () { } - static probe(data) { + static probe (data) { // check if data contains ID3 timestamp and MPEG sync word - var offset, length; + let offset, length; let id3Data = ID3.getID3Data(data, 0); if (id3Data && ID3.getTimeStamp(id3Data) !== undefined) { // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1 @@ -39,26 +38,26 @@ class MP3Demuxer { } // feed incoming data to the front of the parsing pipeline - append(data, timeOffset, contiguous, accurateTimeOffset) { + append (data, timeOffset, contiguous, accurateTimeOffset) { let id3Data = ID3.getID3Data(data, 0); let timestamp = ID3.getTimeStamp(id3Data); let pts = timestamp ? 90 * timestamp : timeOffset * 90000; - var offset = id3Data.length; - var length = data.length; - var frameIndex = 0, stamp = 0; - var track = this._audioTrack; + let offset = id3Data.length; + let length = data.length; + let frameIndex = 0, stamp = 0; + let track = this._audioTrack; let id3Samples = [{ pts: pts, dts: pts, data: id3Data }]; while (offset < length) { if (MpegAudio.isHeader(data, offset)) { - var frame = MpegAudio.appendFrame(track, data, offset, pts, frameIndex); + let frame = MpegAudio.appendFrame(track, data, offset, pts, frameIndex); if (frame) { offset += frame.length; stamp = frame.sample.pts; frameIndex++; } else { - //logger.log('Unable to parse Mpeg audio frame'); + // logger.log('Unable to parse Mpeg audio frame'); break; } } else if (ID3.isHeader(data, offset)) { @@ -66,7 +65,7 @@ class MP3Demuxer { id3Samples.push({ pts: stamp, dts: stamp, data: id3Data }); offset += id3Data.length; } else { - //nothing found, keep looking + // nothing found, keep looking offset++; } } @@ -80,7 +79,7 @@ class MP3Demuxer { accurateTimeOffset); } - destroy() { + destroy () { } } diff --git a/src/demux/mp4demuxer.js b/src/demux/mp4demuxer.js index e7388195d7a..092ac3797ac 100644 --- a/src/demux/mp4demuxer.js +++ b/src/demux/mp4demuxer.js @@ -1,70 +1,64 @@ /** * MP4 demuxer */ -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; import Event from '../events'; const UINT32_MAX = Math.pow(2, 32) - 1; - class MP4Demuxer { - - constructor(observer, remuxer) { +class MP4Demuxer { + constructor (observer, remuxer) { this.observer = observer; this.remuxer = remuxer; } - resetTimeStamp(initPTS) { + resetTimeStamp (initPTS) { this.initPTS = initPTS; } - resetInitSegment(initSegment, audioCodec, videoCodec, duration) { - - //jshint unused:false + resetInitSegment (initSegment, audioCodec, videoCodec, duration) { + // jshint unused:false if (initSegment && initSegment.byteLength) { - const initData = this.initData = MP4Demuxer.parseInitSegment(initSegment); // default audio codec if nothing specified // TODO : extract that from initsegment - if (audioCodec == null) { + if (audioCodec == null) audioCodec = 'mp4a.40.5'; - } - if (videoCodec == null) { + + if (videoCodec == null) videoCodec = 'avc1.42e01e'; - } const tracks = {}; - if(initData.audio && initData.video) { - tracks.audiovideo = { container : 'video/mp4', codec : audioCodec + ',' + videoCodec, initSegment : duration ? initSegment : null }; + if (initData.audio && initData.video) { + tracks.audiovideo = { container: 'video/mp4', codec: audioCodec + ',' + videoCodec, initSegment: duration ? initSegment : null }; } else { - if (initData.audio) { - tracks.audio = { container : 'audio/mp4', codec : audioCodec, initSegment : duration ? initSegment : null }; - } - if (initData.video) { - tracks.video = { container : 'video/mp4', codec : videoCodec, initSegment : duration ? initSegment : null }; - } + if (initData.audio) + tracks.audio = { container: 'audio/mp4', codec: audioCodec, initSegment: duration ? initSegment : null }; + + if (initData.video) + tracks.video = { container: 'video/mp4', codec: videoCodec, initSegment: duration ? initSegment : null }; } this.observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT, { tracks }); } else { - if (audioCodec) { + if (audioCodec) this.audioCodec = audioCodec; - } - if (videoCodec) { + + if (videoCodec) this.videoCodec = videoCodec; - } } } - static probe(data) { + static probe (data) { // ensure we find a moof box in the first 16 kB - return MP4Demuxer.findBox( { data : data, start : 0, end : Math.min(data.length, 16384) } ,['moof']).length > 0; + return MP4Demuxer.findBox({ data: data, start: 0, end: Math.min(data.length, 16384) }, ['moof']).length > 0; } - static bin2str(buffer) { + static bin2str (buffer) { return String.fromCharCode.apply(null, buffer); } - static readUint16(buffer, offset) { + static readUint16 (buffer, offset) { if (buffer.data) { offset += buffer.start; buffer = buffer.data; @@ -76,7 +70,7 @@ const UINT32_MAX = Math.pow(2, 32) - 1; return val < 0 ? 65536 + val : val; } - static readUint32(buffer, offset) { + static readUint32 (buffer, offset) { if (buffer.data) { offset += buffer.start; buffer = buffer.data; @@ -89,22 +83,21 @@ const UINT32_MAX = Math.pow(2, 32) - 1; return val < 0 ? 4294967296 + val : val; } - static writeUint32(buffer, offset, value) { + static writeUint32 (buffer, offset, value) { if (buffer.data) { offset += buffer.start; buffer = buffer.data; } buffer[offset] = value >> 24; - buffer[offset+1] = (value >> 16) & 0xff; - buffer[offset+2] = (value >> 8) & 0xff; - buffer[offset+3] = value & 0xff; + buffer[offset + 1] = (value >> 16) & 0xff; + buffer[offset + 2] = (value >> 8) & 0xff; + buffer[offset + 3] = value & 0xff; } - // Find the data for a box specified by its path - static findBox(data,path) { - var results = [], - i, size, type, end, subresults, start, endbox; + static findBox (data, path) { + let results = [], + i, size, type, end, subresults, start, endbox; if (data.data) { start = data.start; @@ -126,17 +119,15 @@ const UINT32_MAX = Math.pow(2, 32) - 1; endbox = size > 1 ? i + size : end; if (type === path[0]) { - if (path.length === 1) { // this is the end of the path and we've found the box we were // looking for - results.push({ data : data, start : i + 8, end : endbox}); + results.push({ data: data, start: i + 8, end: endbox }); } else { // recursively search for the next box along the path - subresults = MP4Demuxer.findBox({ data : data, start : i +8, end : endbox }, path.slice(1)); - if (subresults.length) { + subresults = MP4Demuxer.findBox({ data: data, start: i + 8, end: endbox }, path.slice(1)); + if (subresults.length) results = results.concat(subresults); - } } } i = endbox; @@ -146,8 +137,7 @@ const UINT32_MAX = Math.pow(2, 32) - 1; return results; } - static parseSegmentIndex(initSegment) { - + static parseSegmentIndex (initSegment) { const moov = MP4Demuxer.findBox(initSegment, ['moov'])[0]; const moovEndOffset = moov ? moov.end : null; // we need this in case we need to chop of garbage of the end of current data @@ -155,9 +145,8 @@ const UINT32_MAX = Math.pow(2, 32) - 1; let sidx = MP4Demuxer.findBox(initSegment, ['sidx']); let references; - if (!sidx || !sidx[0]) { + if (!sidx || !sidx[0]) return null; - } references = []; sidx = sidx[0]; @@ -175,11 +164,11 @@ const UINT32_MAX = Math.pow(2, 32) - 1; let earliestPresentationTime = 0; let firstOffset = 0; - if (version === 0) { + if (version === 0) index += 8; - } else { + else index += 16; - } + // skip reserved index += 2; @@ -254,10 +243,9 @@ const UINT32_MAX = Math.pow(2, 32) - 1; * @return {object} a hash of track type to timescale values or null if * the init segment is malformed. */ - static parseInitSegment(initSegment) { - - var result = []; - var traks = MP4Demuxer.findBox(initSegment, ['moov', 'trak']); + static parseInitSegment (initSegment) { + let result = []; + let traks = MP4Demuxer.findBox(initSegment, ['moov', 'trak']); traks.forEach(trak => { const tkhd = MP4Demuxer.findBox(trak, ['tkhd'])[0]; @@ -274,18 +262,18 @@ const UINT32_MAX = Math.pow(2, 32) - 1; const hdlr = MP4Demuxer.findBox(trak, ['mdia', 'hdlr'])[0]; if (hdlr) { - const hdlrType = MP4Demuxer.bin2str(hdlr.data.subarray(hdlr.start+8, hdlr.start+12)); - let type = {'soun' : 'audio', 'vide' : 'video'}[hdlrType]; + const hdlrType = MP4Demuxer.bin2str(hdlr.data.subarray(hdlr.start + 8, hdlr.start + 12)); + let type = { 'soun': 'audio', 'vide': 'video' }[hdlrType]; if (type) { - // extract codec info. TODO : parse codec details to be able to build MIME type - let codecBox = MP4Demuxer.findBox( trak, ['mdia','minf','stbl','stsd']); - if (codecBox.length) { - codecBox = codecBox[0]; - let codecType = MP4Demuxer.bin2str(codecBox.data.subarray(codecBox.start+12, codecBox.start+16)); - logger.log(`MP4Demuxer:${type}:${codecType} found`); - } - result[trackId] = { timescale : timescale , type : type}; - result[type] = { timescale : timescale , id : trackId}; + // extract codec info. TODO : parse codec details to be able to build MIME type + let codecBox = MP4Demuxer.findBox(trak, ['mdia', 'minf', 'stbl', 'stsd']); + if (codecBox.length) { + codecBox = codecBox[0]; + let codecType = MP4Demuxer.bin2str(codecBox.data.subarray(codecBox.start + 12, codecBox.start + 16)); + logger.log(`MP4Demuxer:${type}:${codecType} found`); + } + result[trackId] = { timescale: timescale, type: type }; + result[type] = { timescale: timescale, id: trackId }; } } } @@ -294,8 +282,7 @@ const UINT32_MAX = Math.pow(2, 32) - 1; return result; } - -/** + /** * Determine the base media decode start time, in seconds, for an MP4 * fragment. If multiple fragments are specified, the earliest time is * returned. @@ -311,94 +298,93 @@ const UINT32_MAX = Math.pow(2, 32) - 1; * @return {number} the earliest base media decode start time for the * fragment, in seconds */ -static getStartDTS(initData, fragment) { - var trafs, baseTimes, result; + static getStartDTS (initData, fragment) { + let trafs, baseTimes, result; - // we need info from two childrend of each track fragment box - trafs = MP4Demuxer.findBox(fragment, ['moof', 'traf']); + // we need info from two childrend of each track fragment box + trafs = MP4Demuxer.findBox(fragment, ['moof', 'traf']); - // determine the start times for each track - baseTimes = [].concat.apply([], trafs.map(function(traf) { - return MP4Demuxer.findBox(traf, ['tfhd']).map(function(tfhd) { - var id, scale, baseTime; + // determine the start times for each track + baseTimes = [].concat.apply([], trafs.map(function (traf) { + return MP4Demuxer.findBox(traf, ['tfhd']).map(function (tfhd) { + let id, scale, baseTime; - // get the track id from the tfhd - id = MP4Demuxer.readUint32(tfhd, 4); - // assume a 90kHz clock if no timescale was specified - scale = initData[id].timescale || 90e3; + // get the track id from the tfhd + id = MP4Demuxer.readUint32(tfhd, 4); + // assume a 90kHz clock if no timescale was specified + scale = initData[id].timescale || 90e3; - // get the base media decode time from the tfdt - baseTime = MP4Demuxer.findBox(traf, ['tfdt']).map(function(tfdt) { - var version, result; + // get the base media decode time from the tfdt + baseTime = MP4Demuxer.findBox(traf, ['tfdt']).map(function (tfdt) { + let version, result; - version = tfdt.data[tfdt.start]; - result = MP4Demuxer.readUint32(tfdt, 4); - if (version === 1) { - result *= Math.pow(2, 32); + version = tfdt.data[tfdt.start]; + result = MP4Demuxer.readUint32(tfdt, 4); + if (version === 1) { + result *= Math.pow(2, 32); - result += MP4Demuxer.readUint32(tfdt, 8); - } - return result; - })[0]; - // convert base time to seconds - return baseTime / scale; - }); - })); + result += MP4Demuxer.readUint32(tfdt, 8); + } + return result; + })[0]; + // convert base time to seconds + return baseTime / scale; + }); + })); - // return the minimum - result = Math.min.apply(null, baseTimes); - return isFinite(result) ? result : 0; -} + // return the minimum + result = Math.min.apply(null, baseTimes); + return isFinite(result) ? result : 0; + } -static offsetStartDTS(initData,fragment,timeOffset) { - MP4Demuxer.findBox(fragment, ['moof', 'traf']).map(function(traf) { - return MP4Demuxer.findBox(traf, ['tfhd']).map(function(tfhd) { + static offsetStartDTS (initData, fragment, timeOffset) { + MP4Demuxer.findBox(fragment, ['moof', 'traf']).map(function (traf) { + return MP4Demuxer.findBox(traf, ['tfhd']).map(function (tfhd) { // get the track id from the tfhd - var id = MP4Demuxer.readUint32(tfhd, 4); - // assume a 90kHz clock if no timescale was specified - var timescale = initData[id].timescale || 90e3; - - // get the base media decode time from the tfdt - MP4Demuxer.findBox(traf, ['tfdt']).map(function(tfdt) { - var version = tfdt.data[tfdt.start]; - var baseMediaDecodeTime = MP4Demuxer.readUint32(tfdt, 4); - if (version === 0) { - MP4Demuxer.writeUint32(tfdt, 4, baseMediaDecodeTime - timeOffset*timescale); - } else { - baseMediaDecodeTime *= Math.pow(2, 32); - baseMediaDecodeTime += MP4Demuxer.readUint32(tfdt, 8); - baseMediaDecodeTime -= timeOffset*timescale; - baseMediaDecodeTime = Math.max(baseMediaDecodeTime,0); - const upper = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1)); - const lower = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1)); - MP4Demuxer.writeUint32(tfdt, 4, upper); - MP4Demuxer.writeUint32(tfdt, 8, lower); - } + let id = MP4Demuxer.readUint32(tfhd, 4); + // assume a 90kHz clock if no timescale was specified + let timescale = initData[id].timescale || 90e3; + + // get the base media decode time from the tfdt + MP4Demuxer.findBox(traf, ['tfdt']).map(function (tfdt) { + let version = tfdt.data[tfdt.start]; + let baseMediaDecodeTime = MP4Demuxer.readUint32(tfdt, 4); + if (version === 0) { + MP4Demuxer.writeUint32(tfdt, 4, baseMediaDecodeTime - timeOffset * timescale); + } else { + baseMediaDecodeTime *= Math.pow(2, 32); + baseMediaDecodeTime += MP4Demuxer.readUint32(tfdt, 8); + baseMediaDecodeTime -= timeOffset * timescale; + baseMediaDecodeTime = Math.max(baseMediaDecodeTime, 0); + const upper = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1)); + const lower = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1)); + MP4Demuxer.writeUint32(tfdt, 4, upper); + MP4Demuxer.writeUint32(tfdt, 8, lower); + } + }); }); }); - }); -} + } // feed incoming data to the front of the parsing pipeline - append(data, timeOffset,contiguous,accurateTimeOffset) { + append (data, timeOffset, contiguous, accurateTimeOffset) { let initData = this.initData; - if(!initData) { + if (!initData) { this.resetInitSegment(data, this.audioCodec, this.videoCodec, false); initData = this.initData; } let startDTS, initPTS = this.initPTS; if (initPTS === undefined) { - let startDTS = MP4Demuxer.getStartDTS(initData,data); + let startDTS = MP4Demuxer.getStartDTS(initData, data); this.initPTS = initPTS = startDTS - timeOffset; - this.observer.trigger(Event.INIT_PTS_FOUND, { initPTS: initPTS}); + this.observer.trigger(Event.INIT_PTS_FOUND, { initPTS: initPTS }); } - MP4Demuxer.offsetStartDTS(initData,data,initPTS); - startDTS = MP4Demuxer.getStartDTS(initData,data); - this.remuxer.remux(initData.audio, initData.video, null, null, startDTS, contiguous,accurateTimeOffset,data); + MP4Demuxer.offsetStartDTS(initData, data, initPTS); + startDTS = MP4Demuxer.getStartDTS(initData, data); + this.remuxer.remux(initData.audio, initData.video, null, null, startDTS, contiguous, accurateTimeOffset, data); } - destroy() {} - + destroy () {} } export default MP4Demuxer; diff --git a/src/demux/mpegaudio.js b/src/demux/mpegaudio.js index ac932ff5bca..c3de38a06ec 100644 --- a/src/demux/mpegaudio.js +++ b/src/demux/mpegaudio.js @@ -4,133 +4,131 @@ const MpegAudio = { - BitratesMap: [ - 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, - 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, - 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, - 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, - 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160], - - SamplingRateMap: [44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000], - - SamplesCoefficients: [ - // MPEG 2.5 - [ - 0, // Reserved - 72, // Layer3 - 144, // Layer2 - 12 // Layer1 - ], - // Reserved - [ - 0, // Reserved - 0, // Layer3 - 0, // Layer2 - 0 // Layer1 - ], - // MPEG 2 - [ - 0, // Reserved - 72, // Layer3 - 144, // Layer2 - 12 // Layer1 - ], - // MPEG 1 - [ - 0, // Reserved - 144, // Layer3 - 144, // Layer2 - 12 // Layer1 - ] + BitratesMap: [ + 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, + 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, + 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, + 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, + 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160], + + SamplingRateMap: [44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000], + + SamplesCoefficients: [ + // MPEG 2.5 + [ + 0, // Reserved + 72, // Layer3 + 144, // Layer2 + 12 // Layer1 ], - - BytesInSlot: [ + // Reserved + [ + 0, // Reserved + 0, // Layer3 + 0, // Layer2 + 0 // Layer1 + ], + // MPEG 2 + [ 0, // Reserved - 1, // Layer3 - 1, // Layer2 - 4 // Layer1 + 72, // Layer3 + 144, // Layer2 + 12 // Layer1 ], + // MPEG 1 + [ + 0, // Reserved + 144, // Layer3 + 144, // Layer2 + 12 // Layer1 + ] + ], + + BytesInSlot: [ + 0, // Reserved + 1, // Layer3 + 1, // Layer2 + 4 // Layer1 + ], + + appendFrame: function (track, data, offset, pts, frameIndex) { + // Using http://www.datavoyage.com/mpgscript/mpeghdr.htm as a reference + if (offset + 24 > data.length) + return undefined; + + let header = this.parseHeader(data, offset); + if (header && offset + header.frameLength <= data.length) { + let frameDuration = header.samplesPerFrame * 90000 / header.sampleRate; + let stamp = pts + frameIndex * frameDuration; + let sample = { unit: data.subarray(offset, offset + header.frameLength), pts: stamp, dts: stamp }; + + track.config = []; + track.channelCount = header.channelCount; + track.samplerate = header.sampleRate; + track.samples.push(sample); + track.len += header.frameLength; + + return { sample, length: header.frameLength }; + } + + return undefined; + }, + + parseHeader: function (data, offset) { + let headerB = (data[offset + 1] >> 3) & 3; + let headerC = (data[offset + 1] >> 1) & 3; + let headerE = (data[offset + 2] >> 4) & 15; + let headerF = (data[offset + 2] >> 2) & 3; + let headerG = (data[offset + 2] >> 1) & 1; + if (headerB !== 1 && headerE !== 0 && headerE !== 15 && headerF !== 3) { + let columnInBitrates = headerB === 3 ? (3 - headerC) : (headerC === 3 ? 3 : 4); + let bitRate = MpegAudio.BitratesMap[columnInBitrates * 14 + headerE - 1] * 1000; + let columnInSampleRates = headerB === 3 ? 0 : headerB === 2 ? 1 : 2; + let sampleRate = MpegAudio.SamplingRateMap[columnInSampleRates * 3 + headerF]; + let channelCount = data[offset + 3] >> 6 === 3 ? 1 : 2; // If bits of channel mode are `11` then it is a single channel (Mono) + let sampleCoefficient = MpegAudio.SamplesCoefficients[headerB][headerC]; + let bytesInSlot = MpegAudio.BytesInSlot[headerC]; + let samplesPerFrame = sampleCoefficient * 8 * bytesInSlot; + let frameLength = parseInt(sampleCoefficient * bitRate / sampleRate + headerG, 10) * bytesInSlot; + + return { sampleRate, channelCount, frameLength, samplesPerFrame }; + } - appendFrame: function (track, data, offset, pts, frameIndex) { - // Using http://www.datavoyage.com/mpgscript/mpeghdr.htm as a reference - if (offset + 24 > data.length) { - return undefined; - } - - var header = this.parseHeader(data, offset); - if (header && offset + header.frameLength <= data.length) { - var frameDuration = header.samplesPerFrame * 90000 / header.sampleRate; - var stamp = pts + frameIndex * frameDuration; - var sample = { unit: data.subarray(offset, offset + header.frameLength), pts: stamp, dts: stamp }; - - track.config = []; - track.channelCount = header.channelCount; - track.samplerate = header.sampleRate; - track.samples.push(sample); - track.len += header.frameLength; - - return { sample, length: header.frameLength }; - } - - return undefined; - }, - - parseHeader: function (data, offset) { - var headerB = (data[offset + 1] >> 3) & 3; - var headerC = (data[offset + 1] >> 1) & 3; - var headerE = (data[offset + 2] >> 4) & 15; - var headerF = (data[offset + 2] >> 2) & 3; - var headerG = (data[offset + 2] >> 1) & 1; - if (headerB !== 1 && headerE !== 0 && headerE !== 15 && headerF !== 3) { - var columnInBitrates = headerB === 3 ? (3 - headerC) : (headerC === 3 ? 3 : 4); - var bitRate = MpegAudio.BitratesMap[columnInBitrates * 14 + headerE - 1] * 1000; - var columnInSampleRates = headerB === 3 ? 0 : headerB === 2 ? 1 : 2; - var sampleRate = MpegAudio.SamplingRateMap[columnInSampleRates * 3 + headerF]; - var channelCount = data[offset + 3] >> 6 === 3 ? 1 : 2; // If bits of channel mode are `11` then it is a single channel (Mono) - var sampleCoefficient = MpegAudio.SamplesCoefficients[headerB][headerC]; - var bytesInSlot = MpegAudio.BytesInSlot[headerC]; - var samplesPerFrame = sampleCoefficient * 8 * bytesInSlot; - var frameLength = parseInt(sampleCoefficient * bitRate / sampleRate + headerG, 10) * bytesInSlot; - - return { sampleRate, channelCount, frameLength, samplesPerFrame }; - } - - return undefined; - }, - - isHeaderPattern: function (data, offset) { - return data[offset] === 0xff && (data[offset + 1] & 0xe0) === 0xe0 && (data[offset + 1] & 0x06) !== 0x00; - }, - - isHeader: function (data, offset) { - // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1 - // Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III) - // More info http://www.mp3-tech.org/programmer/frame_header.html - if (offset + 1 < data.length && this.isHeaderPattern(data, offset)) { - return true; - } - return false; - }, - - probe: function (data, offset) { - // same as isHeader but we also check that MPEG frame follows last MPEG frame - // or end of data is reached - if (offset + 1 < data.length && this.isHeaderPattern(data, offset)) { - // MPEG header Length - let headerLength = 4; - // MPEG frame Length - let header = this.parseHeader(data, offset); - let frameLength = headerLength; - if (header && header.frameLength) { - frameLength = header.frameLength; - } - let newOffset = offset + frameLength; - if (newOffset === data.length || (newOffset + 1 < data.length && this.isHeaderPattern(data, newOffset))) { - return true; - } - } - return false; + return undefined; + }, + + isHeaderPattern: function (data, offset) { + return data[offset] === 0xff && (data[offset + 1] & 0xe0) === 0xe0 && (data[offset + 1] & 0x06) !== 0x00; + }, + + isHeader: function (data, offset) { + // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1 + // Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III) + // More info http://www.mp3-tech.org/programmer/frame_header.html + if (offset + 1 < data.length && this.isHeaderPattern(data, offset)) + return true; + + return false; + }, + + probe: function (data, offset) { + // same as isHeader but we also check that MPEG frame follows last MPEG frame + // or end of data is reached + if (offset + 1 < data.length && this.isHeaderPattern(data, offset)) { + // MPEG header Length + let headerLength = 4; + // MPEG frame Length + let header = this.parseHeader(data, offset); + let frameLength = headerLength; + if (header && header.frameLength) + frameLength = header.frameLength; + + let newOffset = offset + frameLength; + if (newOffset === data.length || (newOffset + 1 < data.length && this.isHeaderPattern(data, newOffset))) + return true; } + return false; + } }; export default MpegAudio; diff --git a/src/demux/sample-aes.js b/src/demux/sample-aes.js index 5901f030062..e49205f30b5 100644 --- a/src/demux/sample-aes.js +++ b/src/demux/sample-aes.js @@ -2,81 +2,77 @@ * SAMPLE-AES decrypter */ - import Decrypter from '../crypt/decrypter'; +import Decrypter from '../crypt/decrypter'; - class SampleAesDecrypter { - - constructor(observer, config, decryptdata, discardEPB) { +class SampleAesDecrypter { + constructor (observer, config, decryptdata, discardEPB) { this.decryptdata = decryptdata; this.discardEPB = discardEPB; this.decrypter = new Decrypter(observer, config, { removePKCS7Padding: false }); } - decryptBuffer(encryptedData, callback) { + decryptBuffer (encryptedData, callback) { this.decrypter.decrypt(encryptedData, this.decryptdata.key.buffer, this.decryptdata.iv.buffer, callback); } // AAC - encrypt all full 16 bytes blocks starting from offset 16 - decryptAacSample(samples, sampleIndex, callback, sync) { + decryptAacSample (samples, sampleIndex, callback, sync) { let curUnit = samples[sampleIndex].unit; let encryptedData = curUnit.subarray(16, curUnit.length - curUnit.length % 16); let encryptedBuffer = encryptedData.buffer.slice( - encryptedData.byteOffset, - encryptedData.byteOffset + encryptedData.length); + encryptedData.byteOffset, + encryptedData.byteOffset + encryptedData.length); let localthis = this; this.decryptBuffer(encryptedBuffer, function (decryptedData) { decryptedData = new Uint8Array(decryptedData); curUnit.set(decryptedData, 16); - if (!sync) { + if (!sync) localthis.decryptAacSamples(samples, sampleIndex + 1, callback); - } }); } - decryptAacSamples(samples, sampleIndex, callback) { + decryptAacSamples (samples, sampleIndex, callback) { for (;; sampleIndex++) { if (sampleIndex >= samples.length) { callback(); return; } - if (samples[sampleIndex].unit.length < 32) { + if (samples[sampleIndex].unit.length < 32) continue; - } let sync = this.decrypter.isSync(); this.decryptAacSample(samples, sampleIndex, callback, sync); - if (!sync) { + if (!sync) return; - } } } // AVC - encrypt one 16 bytes block out of ten, starting from offset 32 - getAvcEncryptedData(decodedData) { + getAvcEncryptedData (decodedData) { let encryptedDataLen = Math.floor((decodedData.length - 48) / 160) * 16 + 16; let encryptedData = new Int8Array(encryptedDataLen); let outputPos = 0; - for (let inputPos = 32; inputPos <= decodedData.length - 16; inputPos += 160, outputPos += 16) { + for (let inputPos = 32; inputPos <= decodedData.length - 16; inputPos += 160, outputPos += 16) encryptedData.set(decodedData.subarray(inputPos, inputPos + 16), outputPos); - } + return encryptedData; } - getAvcDecryptedUnit(decodedData, decryptedData) { + getAvcDecryptedUnit (decodedData, decryptedData) { decryptedData = new Uint8Array(decryptedData); let inputPos = 0; - for (let outputPos = 32; outputPos <= decodedData.length - 16; outputPos += 160, inputPos += 16) { + for (let outputPos = 32; outputPos <= decodedData.length - 16; outputPos += 160, inputPos += 16) decodedData.set(decryptedData.subarray(inputPos, inputPos + 16), outputPos); - } + return decodedData; } - decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit, sync) { + decryptAvcSample (samples, sampleIndex, unitIndex, callback, curUnit, sync) { let decodedData = this.discardEPB(curUnit.data); let encryptedData = this.getAvcEncryptedData(decodedData); let localthis = this; @@ -84,13 +80,12 @@ this.decryptBuffer(encryptedData.buffer, function (decryptedData) { curUnit.data = localthis.getAvcDecryptedUnit(decodedData, decryptedData); - if (!sync) { + if (!sync) localthis.decryptAvcSamples(samples, sampleIndex, unitIndex + 1, callback); - } }); } - decryptAvcSamples(samples, sampleIndex, unitIndex, callback) { + decryptAvcSamples (samples, sampleIndex, unitIndex, callback) { for (;; sampleIndex++, unitIndex = 0) { if (sampleIndex >= samples.length) { callback(); @@ -99,25 +94,22 @@ let curUnits = samples[sampleIndex].units; for (;; unitIndex++) { - if (unitIndex >= curUnits.length) { + if (unitIndex >= curUnits.length) break; - } let curUnit = curUnits[unitIndex]; - if (curUnit.length <= 48 || (curUnit.type !== 1 && curUnit.type !== 5)) { + if (curUnit.length <= 48 || (curUnit.type !== 1 && curUnit.type !== 5)) continue; - } let sync = this.decrypter.isSync(); this.decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit, sync); - if (!sync) { + if (!sync) return; - } } } } - } +} - export default SampleAesDecrypter; +export default SampleAesDecrypter; diff --git a/src/demux/tsdemuxer.js b/src/demux/tsdemuxer.js index 2a7b3a23325..63a7e7c5f16 100644 --- a/src/demux/tsdemuxer.js +++ b/src/demux/tsdemuxer.js @@ -9,14 +9,14 @@ * upon discontinuity or level switch detection, it will also notifies the remuxer so that it can reset its state. */ - import * as ADTS from './adts'; - import MpegAudio from './mpegaudio'; - import Event from '../events'; - import ExpGolomb from './exp-golomb'; - import SampleAesDecrypter from './sample-aes'; +import * as ADTS from './adts'; +import MpegAudio from './mpegaudio'; +import Event from '../events'; +import ExpGolomb from './exp-golomb'; +import SampleAesDecrypter from './sample-aes'; // import Hex from '../utils/hex'; - import {logger} from '../utils/logger'; - import {ErrorTypes, ErrorDetails} from '../errors'; +import { logger } from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; // We are using fixed track IDs for driving the MP4 remuxer // instead of following the TS PIDs. @@ -34,8 +34,7 @@ const RemuxerTrackIdConfig = { }; class TSDemuxer { - - constructor(observer, remuxer, config, typeSupported) { + constructor (observer, remuxer, config, typeSupported) { this.observer = observer; this.config = config; this.typeSupported = typeSupported; @@ -43,37 +42,35 @@ class TSDemuxer { this.sampleAes = null; } - setDecryptData(decryptdata) { - if ((decryptdata != null) && (decryptdata.key != null) && (decryptdata.method === 'SAMPLE-AES')) { + setDecryptData (decryptdata) { + if ((decryptdata != null) && (decryptdata.key != null) && (decryptdata.method === 'SAMPLE-AES')) this.sampleAes = new SampleAesDecrypter(this.observer, this.config, decryptdata, this.discardEPB); - } else { + else this.sampleAes = null; - } } - static probe(data) { + static probe (data) { const syncOffset = TSDemuxer._syncOffset(data); - if (syncOffset < 0) { + if (syncOffset < 0) { return false; } else { - if (syncOffset) { + if (syncOffset) logger.warn(`MPEG2-TS detected but first sync word found @ offset ${syncOffset}, junk ahead ?`); - } + return true; } } - static _syncOffset(data) { + static _syncOffset (data) { // scan 1000 first bytes - const scanwindow = Math.min(1000,data.length - 3*188); + const scanwindow = Math.min(1000, data.length - 3 * 188); let i = 0; - while(i < scanwindow) { + while (i < scanwindow) { // a TS fragment should contain at least 3 TS packets, a PAT, a PMT, and one PID, each starting with 0x47 - if (data[i] === 0x47 && data[i+188] === 0x47 && data[i+2*188] === 0x47) { + if (data[i] === 0x47 && data[i + 188] === 0x47 && data[i + 2 * 188] === 0x47) return i; - } else { + else i++; - } } return -1; } @@ -85,24 +82,24 @@ class TSDemuxer { * @param {number} duration * @return {object} TSDemuxer's internal track model */ - static createTrack(type, duration) { + static createTrack (type, duration) { return { - container: type === 'video' || type === 'audio' ? 'video/mp2t' : undefined, - type, + container: type === 'video' || type === 'audio' ? 'video/mp2t' : undefined, + type, id: RemuxerTrackIdConfig[type], - pid : -1, - inputTimeScale : 90000, + pid: -1, + inputTimeScale: 90000, sequenceNumber: 0, - samples : [], - len : 0, - dropped : type === 'video' ? 0 : undefined, + samples: [], + len: 0, + dropped: type === 'video' ? 0 : undefined, isAAC: type === 'audio' ? true : undefined, duration: type === 'audio' ? duration : undefined }; } /** - * Initializes a new init segment on the demuxer/remuxer interface. Needed for discontinuities/track-switches (or at stream start) + * Initializes a new init segment on the demuxer/remuxer interface. Needed for discontinuities/track-switches (or at stream start) * Resets all internal track instances of the demuxer. * * @override Implements generic demuxing/remuxing interface (see DemuxerInline) @@ -111,7 +108,7 @@ class TSDemuxer { * @param {string} videoCodec * @param {number} duration (in TS timescale = 90kHz) */ - resetInitSegment(initSegment, audioCodec, videoCodec, duration) { + resetInitSegment (initSegment, audioCodec, videoCodec, duration) { this.pmtParsed = false; this._pmtId = -1; @@ -130,34 +127,34 @@ class TSDemuxer { } /** - * + * * @override */ - resetTimeStamp() {} + resetTimeStamp () {} // feed incoming data to the front of the parsing pipeline - append(data, timeOffset, contiguous,accurateTimeOffset) { - var start, len = data.length, stt, pid, atf, offset,pes, - unknownPIDs = false; + append (data, timeOffset, contiguous, accurateTimeOffset) { + let start, len = data.length, stt, pid, atf, offset, pes, + unknownPIDs = false; this.contiguous = contiguous; - var pmtParsed = this.pmtParsed, - avcTrack = this._avcTrack, - audioTrack = this._audioTrack, - id3Track = this._id3Track, - avcId = avcTrack.pid, - audioId = audioTrack.pid, - id3Id = id3Track.pid, - pmtId = this._pmtId, - avcData = avcTrack.pesData, - audioData = audioTrack.pesData, - id3Data = id3Track.pesData, - parsePAT = this._parsePAT, - parsePMT = this._parsePMT, - parsePES = this._parsePES, - parseAVCPES = this._parseAVCPES.bind(this), - parseAACPES = this._parseAACPES.bind(this), - parseMPEGPES = this._parseMPEGPES.bind(this), - parseID3PES = this._parseID3PES.bind(this); + let pmtParsed = this.pmtParsed, + avcTrack = this._avcTrack, + audioTrack = this._audioTrack, + id3Track = this._id3Track, + avcId = avcTrack.pid, + audioId = audioTrack.pid, + id3Id = id3Track.pid, + pmtId = this._pmtId, + avcData = avcTrack.pesData, + audioData = audioTrack.pesData, + id3Data = id3Track.pesData, + parsePAT = this._parsePAT, + parsePMT = this._parsePMT, + parsePES = this._parsePES, + parseAVCPES = this._parseAVCPES.bind(this), + parseAACPES = this._parseAACPES.bind(this), + parseMPEGPES = this._parseMPEGPES.bind(this), + parseID3PES = this._parseID3PES.bind(this); const syncOffset = TSDemuxer._syncOffset(data); @@ -175,106 +172,104 @@ class TSDemuxer { if (atf > 1) { offset = start + 5 + data[start + 4]; // continue if there is only adaptation field - if (offset === (start + 188)) { + if (offset === (start + 188)) continue; - } } else { offset = start + 4; } - switch(pid) { - case avcId: - if (stt) { - if (avcData && (pes = parsePES(avcData)) && pes.pts !== undefined) { - parseAVCPES(pes,false); - } - avcData = {data: [], size: 0}; - } - if (avcData) { - avcData.data.push(data.subarray(offset, start + 188)); - avcData.size += start + 188 - offset; - } - break; - case audioId: - if (stt) { - if (audioData && (pes = parsePES(audioData)) && pes.pts !== undefined) { - if (audioTrack.isAAC) { - parseAACPES(pes); - } else { - parseMPEGPES(pes); - } - } - audioData = {data: [], size: 0}; - } - if (audioData) { - audioData.data.push(data.subarray(offset, start + 188)); - audioData.size += start + 188 - offset; - } - break; - case id3Id: - if (stt) { - if (id3Data && (pes = parsePES(id3Data)) && pes.pts !== undefined) { - parseID3PES(pes); - } - id3Data = {data: [], size: 0}; - } - if (id3Data) { - id3Data.data.push(data.subarray(offset, start + 188)); - id3Data.size += start + 188 - offset; - } - break; - case 0: - if (stt) { - offset += data[offset] + 1; - } - pmtId = this._pmtId = parsePAT(data, offset); - break; - case pmtId: - if (stt) { - offset += data[offset] + 1; - } - let parsedPIDs = parsePMT(data, offset, this.typeSupported.mpeg === true || this.typeSupported.mp3 === true, this.sampleAes != null); - - // only update track id if track PID found while parsing PMT - // this is to avoid resetting the PID to -1 in case - // track PID transiently disappears from the stream - // this could happen in case of transient missing audio samples for example - // NOTE this is only the PID of the track as found in TS, - // but we are not using this for MP4 track IDs. - avcId = parsedPIDs.avc; - if (avcId > 0) { - avcTrack.pid = avcId; - } - audioId = parsedPIDs.audio; - if (audioId > 0) { - audioTrack.pid = audioId; - audioTrack.isAAC = parsedPIDs.isAAC; - } - id3Id = parsedPIDs.id3; - if (id3Id > 0) { - id3Track.pid = id3Id; - } - if (unknownPIDs && !pmtParsed) { - logger.log('reparse from beginning'); - unknownPIDs = false; - // we set it to -188, the += 188 in the for loop will reset start to 0 - start = syncOffset-188; + switch (pid) { + case avcId: + if (stt) { + if (avcData && (pes = parsePES(avcData)) && pes.pts !== undefined) + parseAVCPES(pes, false); + + avcData = { data: [], size: 0 }; + } + if (avcData) { + avcData.data.push(data.subarray(offset, start + 188)); + avcData.size += start + 188 - offset; + } + break; + case audioId: + if (stt) { + if (audioData && (pes = parsePES(audioData)) && pes.pts !== undefined) { + if (audioTrack.isAAC) + parseAACPES(pes); + else + parseMPEGPES(pes); } - pmtParsed = this.pmtParsed = true; - break; - case 17: - case 0x1fff: - break; - default: - unknownPIDs = true; - break; + audioData = { data: [], size: 0 }; + } + if (audioData) { + audioData.data.push(data.subarray(offset, start + 188)); + audioData.size += start + 188 - offset; + } + break; + case id3Id: + if (stt) { + if (id3Data && (pes = parsePES(id3Data)) && pes.pts !== undefined) + parseID3PES(pes); + + id3Data = { data: [], size: 0 }; + } + if (id3Data) { + id3Data.data.push(data.subarray(offset, start + 188)); + id3Data.size += start + 188 - offset; + } + break; + case 0: + if (stt) + offset += data[offset] + 1; + + pmtId = this._pmtId = parsePAT(data, offset); + break; + case pmtId: + if (stt) + offset += data[offset] + 1; + + let parsedPIDs = parsePMT(data, offset, this.typeSupported.mpeg === true || this.typeSupported.mp3 === true, this.sampleAes != null); + + // only update track id if track PID found while parsing PMT + // this is to avoid resetting the PID to -1 in case + // track PID transiently disappears from the stream + // this could happen in case of transient missing audio samples for example + // NOTE this is only the PID of the track as found in TS, + // but we are not using this for MP4 track IDs. + avcId = parsedPIDs.avc; + if (avcId > 0) + avcTrack.pid = avcId; + + audioId = parsedPIDs.audio; + if (audioId > 0) { + audioTrack.pid = audioId; + audioTrack.isAAC = parsedPIDs.isAAC; + } + id3Id = parsedPIDs.id3; + if (id3Id > 0) + id3Track.pid = id3Id; + + if (unknownPIDs && !pmtParsed) { + logger.log('reparse from beginning'); + unknownPIDs = false; + // we set it to -188, the += 188 in the for loop will reset start to 0 + start = syncOffset - 188; + } + pmtParsed = this.pmtParsed = true; + break; + case 17: + case 0x1fff: + break; + default: + unknownPIDs = true; + break; } } else { - this.observer.trigger(Event.ERROR, {type : ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: false, reason: 'TS packet did not start with 0x47'}); + this.observer.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: false, reason: 'TS packet did not start with 0x47' }); } } // try to parse last PES packets if (avcData && (pes = parsePES(avcData)) && pes.pts !== undefined) { - parseAVCPES(pes,true); + parseAVCPES(pes, true); avcTrack.pesData = null; } else { // either avcData null or PES truncated, keep it for next frag parsing @@ -282,17 +277,17 @@ class TSDemuxer { } if (audioData && (pes = parsePES(audioData)) && pes.pts !== undefined) { - if (audioTrack.isAAC) { + if (audioTrack.isAAC) parseAACPES(pes); - } else { + else parseMPEGPES(pes); - } + audioTrack.pesData = null; } else { - if (audioData && audioData.size) { + if (audioData && audioData.size) logger.log('last AAC PES packet truncated,might overlap between fragments'); - } - // either audioData null or PES truncated, keep it for next frag parsing + + // either audioData null or PES truncated, keep it for next frag parsing audioTrack.pesData = audioData; } @@ -304,17 +299,16 @@ class TSDemuxer { id3Track.pesData = id3Data; } - if (this.sampleAes == null) { + if (this.sampleAes == null) this.remuxer.remux(audioTrack, avcTrack, id3Track, this._txtTrack, timeOffset, contiguous, accurateTimeOffset); - } else { + else this.decryptAndRemux(audioTrack, avcTrack, id3Track, this._txtTrack, timeOffset, contiguous, accurateTimeOffset); - } } - decryptAndRemux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) { + decryptAndRemux (audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) { if (audioTrack.samples && audioTrack.isAAC) { let localthis = this; - this.sampleAes.decryptAacSamples(audioTrack.samples, 0, function() { + this.sampleAes.decryptAacSamples(audioTrack.samples, 0, function () { localthis.decryptAndRemuxAvc(audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset); }); } else { @@ -322,7 +316,7 @@ class TSDemuxer { } } - decryptAndRemuxAvc(audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) { + decryptAndRemuxAvc (audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) { if (videoTrack.samples) { let localthis = this; this.sampleAes.decryptAvcSamples(videoTrack.samples, 0, 0, function () { @@ -333,19 +327,19 @@ class TSDemuxer { } } - destroy() { + destroy () { this._initPTS = this._initDTS = undefined; this._duration = 0; } - _parsePAT(data, offset) { + _parsePAT (data, offset) { // skip the PSI header and parse the first PMT entry return (data[offset + 10] & 0x1F) << 8 | data[offset + 11]; - //logger.log('PMT PID:' + this._pmtId); + // logger.log('PMT PID:' + this._pmtId); } - _parsePMT(data, offset, mpegSupported, isSampleAes) { - var sectionLength, tableEnd, programInfoLength, pid, result = { audio : -1, avc : -1, id3 : -1, isAAC : true}; + _parsePMT (data, offset, mpegSupported, isSampleAes) { + let sectionLength, tableEnd, programInfoLength, pid, result = { audio: -1, avc: -1, id3: -1, isAAC: true }; sectionLength = (data[offset + 1] & 0x0f) << 8 | data[offset + 2]; tableEnd = offset + 3 + sectionLength - 4; // to determine where the table is, we have to figure out how @@ -355,65 +349,65 @@ class TSDemuxer { offset += 12 + programInfoLength; while (offset < tableEnd) { pid = (data[offset + 1] & 0x1F) << 8 | data[offset + 2]; - switch(data[offset]) { - case 0xcf: // SAMPLE-AES AAC - if (!isSampleAes) { - logger.log('unkown stream type:' + data[offset]); - break; - } - /* falls through */ + switch (data[offset]) { + case 0xcf: // SAMPLE-AES AAC + if (!isSampleAes) { + logger.log('unkown stream type:' + data[offset]); + break; + } + /* falls through */ // ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio) - case 0x0f: - //logger.log('AAC PID:' + pid); - if (result.audio === -1) { - result.audio = pid; - } - break; + case 0x0f: + // logger.log('AAC PID:' + pid); + if (result.audio === -1) + result.audio = pid; + + break; // Packetized metadata (ID3) - case 0x15: - //logger.log('ID3 PID:' + pid); - if (result.id3 === -1) { - result.id3 = pid; - } - break; + case 0x15: + // logger.log('ID3 PID:' + pid); + if (result.id3 === -1) + result.id3 = pid; - case 0xdb: // SAMPLE-AES AVC - if (!isSampleAes) { - logger.log('unkown stream type:' + data[offset]); - break; - } - /* falls through */ + break; - // ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video) - case 0x1b: - //logger.log('AVC PID:' + pid); - if (result.avc === -1) { - result.avc = pid; - } + case 0xdb: // SAMPLE-AES AVC + if (!isSampleAes) { + logger.log('unkown stream type:' + data[offset]); break; + } + /* falls through */ + + // ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video) + case 0x1b: + // logger.log('AVC PID:' + pid); + if (result.avc === -1) + result.avc = pid; + + break; // ISO/IEC 11172-3 (MPEG-1 audio) // or ISO/IEC 13818-3 (MPEG-2 halved sample rate audio) - case 0x03: - case 0x04: - //logger.log('MPEG PID:' + pid); - if (!mpegSupported) { - logger.log('MPEG audio found, not supported in this browser for now'); - } else if (result.audio === -1) { - result.audio = pid; - result.isAAC = false; - } - break; + case 0x03: + case 0x04: + // logger.log('MPEG PID:' + pid); + if (!mpegSupported) { + logger.log('MPEG audio found, not supported in this browser for now'); + } else if (result.audio === -1) { + result.audio = pid; + result.isAAC = false; + } + break; - case 0x24: - logger.warn('HEVC stream type found, not supported for now'); - break; + case 0x24: + logger.warn('HEVC stream type found, not supported for now'); + break; - default: - logger.log('unkown stream type:' + data[offset]); - break; + default: + logger.log('unkown stream type:' + data[offset]); + break; } // move to the next table entry // skip past the elementary stream descriptors, if present @@ -422,33 +416,32 @@ class TSDemuxer { return result; } - _parsePES(stream) { - var i = 0, frag, pesFlags, pesPrefix, pesLen, pesHdrLen, pesData, pesPts, pesDts, payloadStartOffset, data = stream.data; + _parsePES (stream) { + let i = 0, frag, pesFlags, pesPrefix, pesLen, pesHdrLen, pesData, pesPts, pesDts, payloadStartOffset, data = stream.data; // safety check - if (!stream || stream.size === 0) { + if (!stream || stream.size === 0) return null; - } // we might need up to 19 bytes to read PES header // if first chunk of data is less than 19 bytes, let's merge it with following ones until we get 19 bytes // usually only one merge is needed (and this is rare ...) - while(data[0].length < 19 && data.length > 1) { + while (data[0].length < 19 && data.length > 1) { let newData = new Uint8Array(data[0].length + data[1].length); newData.set(data[0]); newData.set(data[1], data[0].length); data[0] = newData; - data.splice(1,1); + data.splice(1, 1); } - //retrieve PTS/DTS from first fragment + // retrieve PTS/DTS from first fragment frag = data[0]; pesPrefix = (frag[0] << 16) + (frag[1] << 8) + frag[2]; if (pesPrefix === 1) { pesLen = (frag[4] << 8) + frag[5]; // if PES parsed length is not zero and greater than total received length, stop parsing. PES might be truncated // minus 6 : PES header size - if (pesLen && pesLen > stream.size - 6) { + if (pesLen && pesLen > stream.size - 6) return null; - } + pesFlags = frag[7]; if (pesFlags & 0xC0) { /* PES header described here : http://dvd.sourceforge.net/dvdinfo/pes-hdr.html @@ -459,24 +452,24 @@ class TSDemuxer { (frag[11] & 0xFE) * 16384 +// 1 << 14 (frag[12] & 0xFF) * 128 +// 1 << 7 (frag[13] & 0xFE) / 2; - // check if greater than 2^32 -1 - if (pesPts > 4294967295) { - // decrement 2^33 - pesPts -= 8589934592; - } + // check if greater than 2^32 -1 + if (pesPts > 4294967295) { + // decrement 2^33 + pesPts -= 8589934592; + } if (pesFlags & 0x40) { - pesDts = (frag[14] & 0x0E ) * 536870912 +// 1 << 29 - (frag[15] & 0xFF ) * 4194304 +// 1 << 22 - (frag[16] & 0xFE ) * 16384 +// 1 << 14 - (frag[17] & 0xFF ) * 128 +// 1 << 7 - (frag[18] & 0xFE ) / 2; + pesDts = (frag[14] & 0x0E) * 536870912 +// 1 << 29 + (frag[15] & 0xFF) * 4194304 +// 1 << 22 + (frag[16] & 0xFE) * 16384 +// 1 << 14 + (frag[17] & 0xFF) * 128 +// 1 << 7 + (frag[18] & 0xFE) / 2; // check if greater than 2^32 -1 if (pesDts > 4294967295) { // decrement 2^33 pesDts -= 8589934592; } - if (pesPts - pesDts > 60*90000) { - logger.warn(`${Math.round((pesPts - pesDts)/90000)}s delta between PTS and DTS, align them`); + if (pesPts - pesDts > 60 * 90000) { + logger.warn(`${Math.round((pesPts - pesDts) / 90000)}s delta between PTS and DTS, align them`); pesPts = pesDts; } } else { @@ -488,37 +481,37 @@ class TSDemuxer { payloadStartOffset = pesHdrLen + 9; stream.size -= payloadStartOffset; - //reassemble PES packet + // reassemble PES packet pesData = new Uint8Array(stream.size); - for( let j = 0, dataLen = data.length; j < dataLen ; j++) { + for (let j = 0, dataLen = data.length; j < dataLen; j++) { frag = data[j]; let len = frag.byteLength; if (payloadStartOffset) { if (payloadStartOffset > len) { // trim full frag if PES header bigger than frag - payloadStartOffset-=len; + payloadStartOffset -= len; continue; } else { // trim partial frag if PES header smaller than frag frag = frag.subarray(payloadStartOffset); - len-=payloadStartOffset; + len -= payloadStartOffset; payloadStartOffset = 0; } } pesData.set(frag, i); - i+=len; + i += len; } if (pesLen) { // payload size : remove PES header + PES extension - pesLen -= pesHdrLen+3; + pesLen -= pesHdrLen + 3; } - return {data: pesData, pts: pesPts, dts: pesDts, len: pesLen}; + return { data: pesData, pts: pesPts, dts: pesDts, len: pesLen }; } else { return null; } } - pushAccesUnit(avcSample,avcTrack) { + pushAccesUnit (avcSample, avcTrack) { if (avcSample.units.length && avcSample.frame) { const samples = avcTrack.samples; const nbSamples = samples.length; @@ -536,248 +529,238 @@ class TSDemuxer { avcTrack.dropped++; } } - if(avcSample.debug.length) { + if (avcSample.debug.length) logger.log(avcSample.pts + '/' + avcSample.dts + ':' + avcSample.debug); - } } - _parseAVCPES(pes,last) { - //logger.log('parse new PES'); - var track = this._avcTrack, - units = this._parseAVCNALu(pes.data), - debug = false, - expGolombDecoder, - avcSample = this.avcSample, - push, - spsfound = false, - i, - pushAccesUnit = this.pushAccesUnit.bind(this), - createAVCSample = function(key,pts,dts,debug) { - return { key : key, pts : pts, dts : dts, units : [], debug : debug}; - }; - //free pes.data to save up some memory + _parseAVCPES (pes, last) { + // logger.log('parse new PES'); + let track = this._avcTrack, + units = this._parseAVCNALu(pes.data), + debug = false, + expGolombDecoder, + avcSample = this.avcSample, + push, + spsfound = false, + i, + pushAccesUnit = this.pushAccesUnit.bind(this), + createAVCSample = function (key, pts, dts, debug) { + return { key: key, pts: pts, dts: dts, units: [], debug: debug }; + }; + // free pes.data to save up some memory pes.data = null; // if new NAL units found and last sample still there, let's push ... // this helps parsing streams with missing AUD (only do this if AUD never found) if (avcSample && units.length && !track.audFound) { - pushAccesUnit(avcSample,track); - avcSample = this.avcSample = createAVCSample(false,pes.pts,pes.dts,''); + pushAccesUnit(avcSample, track); + avcSample = this.avcSample = createAVCSample(false, pes.pts, pes.dts, ''); } units.forEach(unit => { - switch(unit.type) { - //NDR - case 1: - push = true; - if (!avcSample) { - avcSample = this.avcSample = createAVCSample(true,pes.pts,pes.dts,''); - } - if(debug) { - avcSample.debug += 'NDR '; - } - avcSample.frame = true; - let data = unit.data; - // only check slice type to detect KF in case SPS found in same packet (any keyframe is preceded by SPS ...) - if (spsfound && data.length > 4) { - // retrieve slice type by parsing beginning of NAL unit (follow H264 spec, slice_header definition) to detect keyframe embedded in NDR - let sliceType = new ExpGolomb(data).readSliceType(); - // 2 : I slice, 4 : SI slice, 7 : I slice, 9: SI slice - // SI slice : A slice that is coded using intra prediction only and using quantisation of the prediction samples. - // An SI slice can be coded such that its decoded samples can be constructed identically to an SP slice. - // I slice: A slice that is not an SI slice that is decoded using intra prediction only. - //if (sliceType === 2 || sliceType === 7) { - if (sliceType === 2 || sliceType === 4 || sliceType === 7 || sliceType === 9) { - avcSample.key = true; - } - } - break; - //IDR - case 5: - push = true; - // handle PES not starting with AUD - if (!avcSample) { - avcSample = this.avcSample = createAVCSample(true,pes.pts,pes.dts,''); - } - if(debug) { - avcSample.debug += 'IDR '; - } - avcSample.key = true; - avcSample.frame = true; - break; - //SEI - case 6: - push = true; - if(debug && avcSample) { - avcSample.debug += 'SEI '; - } - expGolombDecoder = new ExpGolomb(this.discardEPB(unit.data)); - - // skip frameType - expGolombDecoder.readUByte(); - - var payloadType = 0; - var payloadSize = 0; - var endOfCaptions = false; - var b = 0; - - while (!endOfCaptions && expGolombDecoder.bytesAvailable > 1) { - payloadType = 0; - do { - b = expGolombDecoder.readUByte(); - payloadType += b; - } while (b === 0xFF); - - // Parse payload size. - payloadSize = 0; - do { - b = expGolombDecoder.readUByte(); - payloadSize += b; - } while (b === 0xFF); - - // TODO: there can be more than one payload in an SEI packet... - // TODO: need to read type and size in a while loop to get them all - if (payloadType === 4 && expGolombDecoder.bytesAvailable !== 0) { - - endOfCaptions = true; - - var countryCode = expGolombDecoder.readUByte(); - - if (countryCode === 181) { - var providerCode = expGolombDecoder.readUShort(); - - if (providerCode === 49) { - var userStructure = expGolombDecoder.readUInt(); - - if (userStructure === 0x47413934) { - var userDataType = expGolombDecoder.readUByte(); - - // Raw CEA-608 bytes wrapped in CEA-708 packet - if (userDataType === 3) { - var firstByte = expGolombDecoder.readUByte(); - var secondByte = expGolombDecoder.readUByte(); - - var totalCCs = 31 & firstByte; - var byteArray = [firstByte, secondByte]; - - for (i = 0; i < totalCCs; i++) { - // 3 bytes per CC - byteArray.push(expGolombDecoder.readUByte()); - byteArray.push(expGolombDecoder.readUByte()); - byteArray.push(expGolombDecoder.readUByte()); - } - - this._insertSampleInOrder(this._txtTrack.samples, { type: 3, pts: pes.pts, bytes: byteArray }); + switch (unit.type) { + // NDR + case 1: + push = true; + if (!avcSample) + avcSample = this.avcSample = createAVCSample(true, pes.pts, pes.dts, ''); + + if (debug) + avcSample.debug += 'NDR '; + + avcSample.frame = true; + let data = unit.data; + // only check slice type to detect KF in case SPS found in same packet (any keyframe is preceded by SPS ...) + if (spsfound && data.length > 4) { + // retrieve slice type by parsing beginning of NAL unit (follow H264 spec, slice_header definition) to detect keyframe embedded in NDR + let sliceType = new ExpGolomb(data).readSliceType(); + // 2 : I slice, 4 : SI slice, 7 : I slice, 9: SI slice + // SI slice : A slice that is coded using intra prediction only and using quantisation of the prediction samples. + // An SI slice can be coded such that its decoded samples can be constructed identically to an SP slice. + // I slice: A slice that is not an SI slice that is decoded using intra prediction only. + // if (sliceType === 2 || sliceType === 7) { + if (sliceType === 2 || sliceType === 4 || sliceType === 7 || sliceType === 9) + avcSample.key = true; + } + break; + // IDR + case 5: + push = true; + // handle PES not starting with AUD + if (!avcSample) + avcSample = this.avcSample = createAVCSample(true, pes.pts, pes.dts, ''); + + if (debug) + avcSample.debug += 'IDR '; + + avcSample.key = true; + avcSample.frame = true; + break; + // SEI + case 6: + push = true; + if (debug && avcSample) + avcSample.debug += 'SEI '; + + expGolombDecoder = new ExpGolomb(this.discardEPB(unit.data)); + + // skip frameType + expGolombDecoder.readUByte(); + + var payloadType = 0; + var payloadSize = 0; + var endOfCaptions = false; + var b = 0; + + while (!endOfCaptions && expGolombDecoder.bytesAvailable > 1) { + payloadType = 0; + do { + b = expGolombDecoder.readUByte(); + payloadType += b; + } while (b === 0xFF); + + // Parse payload size. + payloadSize = 0; + do { + b = expGolombDecoder.readUByte(); + payloadSize += b; + } while (b === 0xFF); + + // TODO: there can be more than one payload in an SEI packet... + // TODO: need to read type and size in a while loop to get them all + if (payloadType === 4 && expGolombDecoder.bytesAvailable !== 0) { + endOfCaptions = true; + + let countryCode = expGolombDecoder.readUByte(); + + if (countryCode === 181) { + let providerCode = expGolombDecoder.readUShort(); + + if (providerCode === 49) { + let userStructure = expGolombDecoder.readUInt(); + + if (userStructure === 0x47413934) { + let userDataType = expGolombDecoder.readUByte(); + + // Raw CEA-608 bytes wrapped in CEA-708 packet + if (userDataType === 3) { + let firstByte = expGolombDecoder.readUByte(); + let secondByte = expGolombDecoder.readUByte(); + + let totalCCs = 31 & firstByte; + let byteArray = [firstByte, secondByte]; + + for (i = 0; i < totalCCs; i++) { + // 3 bytes per CC + byteArray.push(expGolombDecoder.readUByte()); + byteArray.push(expGolombDecoder.readUByte()); + byteArray.push(expGolombDecoder.readUByte()); } + + this._insertSampleInOrder(this._txtTrack.samples, { type: 3, pts: pes.pts, bytes: byteArray }); } } } } - else if (payloadSize < expGolombDecoder.bytesAvailable) - { - for (i = 0; i 0) { - if (data.pts >= arr[len-1].pts) - { + if (data.pts >= arr[len - 1].pts) { arr.push(data); - } - else { - for (var pos = len - 1; pos >= 0; pos--) { + } else { + for (let pos = len - 1; pos >= 0; pos--) { if (data.pts < arr[pos].pts) { arr.splice(pos, 0, data); break; } } } - } - else { + } else { arr.push(data); } } - _getLastNalUnit() { + _getLastNalUnit () { let avcSample = this.avcSample, lastUnit; // try to fallback to previous sample if current one is empty if (!avcSample || avcSample.units.length === 0) { let track = this._avcTrack, samples = track.samples; - avcSample = samples[samples.length-1]; + avcSample = samples[samples.length - 1]; } if (avcSample) { let units = avcSample.units; @@ -786,10 +769,10 @@ class TSDemuxer { return lastUnit; } - _parseAVCNALu(array) { - var i = 0, len = array.byteLength, value, overflow, track = this._avcTrack, state = track.naluState || 0, lastState = state; - var units = [], unit, unitType, lastUnitStart = -1, lastUnitType; - //logger.log('PES:' + Hex.hexDump(array)); + _parseAVCNALu (array) { + let i = 0, len = array.byteLength, value, overflow, track = this._avcTrack, state = track.naluState || 0, lastState = state; + let units = [], unit, unitType, lastUnitStart = -1, lastUnitType; + // logger.log('PES:' + Hex.hexDump(array)); if (state === -1) { // special use case where we found 3 or 4-byte start codes exactly at the end of previous PES packet @@ -812,12 +795,12 @@ class TSDemuxer { continue; } // here we have state either equal to 2 or 3 - if(!value) { + if (!value) { state = 3; } else if (value === 1) { - if (lastUnitStart >=0) { - unit = {data: array.subarray(lastUnitStart, i - state - 1), type: lastUnitType}; - //logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength); + if (lastUnitStart >= 0) { + unit = { data: array.subarray(lastUnitStart, i - state - 1), type: lastUnitType }; + // logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength); units.push(unit); } else { // lastUnitStart is undefined => this is the first start code found in this PES packet @@ -826,19 +809,19 @@ class TSDemuxer { // and ended at the beginning of this PES packet (i <= 4 - lastState) let lastUnit = this._getLastNalUnit(); if (lastUnit) { - if(lastState && (i <= 4 - lastState)) { + if (lastState && (i <= 4 - lastState)) { // start delimiter overlapping between PES packets // strip start delimiter bytes from the end of last NAL unit - // check if lastUnit had a state different from zero + // check if lastUnit had a state different from zero if (lastUnit.state) { // strip last bytes - lastUnit.data = lastUnit.data.subarray(0,lastUnit.data.byteLength - lastState); + lastUnit.data = lastUnit.data.subarray(0, lastUnit.data.byteLength - lastState); } } // If NAL units are not starting right at the beginning of the PES packet, push preceding data into previous NAL unit. - overflow = i - state - 1; + overflow = i - state - 1; if (overflow > 0) { - //logger.log('first NALU found with overflow:' + overflow); + // logger.log('first NALU found with overflow:' + overflow); let tmp = new Uint8Array(lastUnit.data.byteLength + overflow); tmp.set(lastUnit.data, 0); tmp.set(array.subarray(0, overflow), lastUnit.data.byteLength); @@ -849,7 +832,7 @@ class TSDemuxer { // check if we can read unit type if (i < len) { unitType = array[i] & 0x1f; - //logger.log('find NALU @ offset:' + i + ',type:' + unitType); + // logger.log('find NALU @ offset:' + i + ',type:' + unitType); lastUnitStart = i; lastUnitType = unitType; state = 0; @@ -861,15 +844,15 @@ class TSDemuxer { state = 0; } } - if (lastUnitStart >=0 && state >=0) { - unit = {data: array.subarray(lastUnitStart, len), type: lastUnitType, state : state}; + if (lastUnitStart >= 0 && state >= 0) { + unit = { data: array.subarray(lastUnitStart, len), type: lastUnitType, state: state }; units.push(unit); - //logger.log('pushing NALU, type/size/state:' + unit.type + '/' + unit.data.byteLength + '/' + state); + // logger.log('pushing NALU, type/size/state:' + unit.type + '/' + unit.data.byteLength + '/' + state); } // no NALu found if (units.length === 0) { // append pes.data to previous NAL unit - let lastUnit = this._getLastNalUnit(); + let lastUnit = this._getLastNalUnit(); if (lastUnit) { let tmp = new Uint8Array(lastUnit.data.byteLength + array.byteLength); tmp.set(lastUnit.data, 0); @@ -884,11 +867,11 @@ class TSDemuxer { /** * remove Emulation Prevention bytes from a RBSP */ - discardEPB(data) { - var length = data.byteLength, - EPBPositions = [], - i = 1, - newLength, newData; + discardEPB (data) { + let length = data.byteLength, + EPBPositions = [], + i = 1, + newLength, newData; // Find all `Emulation Prevention Bytes` while (i < length - 2) { @@ -904,14 +887,13 @@ class TSDemuxer { // If no Emulation Prevention Bytes were found just return the original // array - if (EPBPositions.length === 0) { + if (EPBPositions.length === 0) return data; - } // Create a new array to hold the NAL unit data newLength = length - EPBPositions.length; newData = new Uint8Array(newLength); - var sourceIndex = 0; + let sourceIndex = 0; for (i = 0; i < newLength; sourceIndex++, i++) { if (sourceIndex === EPBPositions[0]) { @@ -925,30 +907,29 @@ class TSDemuxer { return newData; } - _parseAACPES(pes) { - var track = this._audioTrack, - data = pes.data, - pts = pes.pts, - startOffset = 0, - aacOverFlow = this.aacOverFlow, - aacLastPTS = this.aacLastPTS, - frameDuration, frameIndex, offset, stamp, len; + _parseAACPES (pes) { + let track = this._audioTrack, + data = pes.data, + pts = pes.pts, + startOffset = 0, + aacOverFlow = this.aacOverFlow, + aacLastPTS = this.aacLastPTS, + frameDuration, frameIndex, offset, stamp, len; if (aacOverFlow) { - var tmp = new Uint8Array(aacOverFlow.byteLength + data.byteLength); + let tmp = new Uint8Array(aacOverFlow.byteLength + data.byteLength); tmp.set(aacOverFlow, 0); tmp.set(data, aacOverFlow.byteLength); - //logger.log(`AAC: append overflowing ${aacOverFlow.byteLength} bytes to beginning of new PES`); + // logger.log(`AAC: append overflowing ${aacOverFlow.byteLength} bytes to beginning of new PES`); data = tmp; } // look for ADTS header (0xFFFx) for (offset = startOffset, len = data.length; offset < len - 1; offset++) { - if (ADTS.isHeader(data, offset)) { + if (ADTS.isHeader(data, offset)) break; - } } // if ADTS header does not start straight from the beginning of the PES payload, raise an error if (offset) { - var reason, fatal; + let reason, fatal; if (offset < len - 1) { reason = `AAC PES did not start with ADTS header,offset:${offset}`; fatal = false; @@ -957,10 +938,9 @@ class TSDemuxer { fatal = true; } logger.warn(`parsing error:${reason}`); - this.observer.trigger(Event.ERROR, {type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: fatal, reason: reason}); - if (fatal) { + this.observer.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: fatal, reason: reason }); + if (fatal) return; - } } ADTS.initTrackConfig(track, this.observer, data, offset, this.audioCodec); @@ -969,68 +949,68 @@ class TSDemuxer { // if last AAC frame is overflowing, we should ensure timestamps are contiguous: // first sample PTS should be equal to last sample PTS + frameDuration - if(aacOverFlow && aacLastPTS) { - var newPTS = aacLastPTS+frameDuration; - if(Math.abs(newPTS-pts) > 1) { - logger.log(`AAC: align PTS for overlapping frames by ${Math.round((newPTS-pts)/90)}`); - pts=newPTS; + if (aacOverFlow && aacLastPTS) { + let newPTS = aacLastPTS + frameDuration; + if (Math.abs(newPTS - pts) > 1) { + logger.log(`AAC: align PTS for overlapping frames by ${Math.round((newPTS - pts) / 90)}`); + pts = newPTS; } } - //scan for aac samples + // scan for aac samples while (offset < len) { if (ADTS.isHeader(data, offset) && (offset + 5) < len) { - var frame = ADTS.appendFrame(track, data, offset, pts, frameIndex); + let frame = ADTS.appendFrame(track, data, offset, pts, frameIndex); if (frame) { - //logger.log(`${Math.round(frame.sample.pts)} : AAC`); + // logger.log(`${Math.round(frame.sample.pts)} : AAC`); offset += frame.length; stamp = frame.sample.pts; frameIndex++; } else { - //logger.log('Unable to parse AAC frame'); + // logger.log('Unable to parse AAC frame'); break; } } else { - //nothing found, keep looking + // nothing found, keep looking offset++; } } - if (offset < len) { + if (offset < len) aacOverFlow = data.subarray(offset, len); - //logger.log(`AAC: overflow detected:${len-offset}`); - } else { + // logger.log(`AAC: overflow detected:${len-offset}`); + else aacOverFlow = null; - } + this.aacOverFlow = aacOverFlow; this.aacLastPTS = stamp; } - _parseMPEGPES(pes) { - var data = pes.data; - var length = data.length; - var frameIndex = 0; - var offset = 0; - var pts = pes.pts; + _parseMPEGPES (pes) { + let data = pes.data; + let length = data.length; + let frameIndex = 0; + let offset = 0; + let pts = pes.pts; while (offset < length) { if (MpegAudio.isHeader(data, offset)) { - var frame = MpegAudio.appendFrame(this._audioTrack, data, offset, pts, frameIndex); + let frame = MpegAudio.appendFrame(this._audioTrack, data, offset, pts, frameIndex); if (frame) { offset += frame.length; frameIndex++; } else { - //logger.log('Unable to parse Mpeg audio frame'); + // logger.log('Unable to parse Mpeg audio frame'); break; } } else { - //nothing found, keep looking + // nothing found, keep looking offset++; } } } - _parseID3PES(pes) { + _parseID3PES (pes) { this._id3Track.samples.push(pes); } } diff --git a/src/errors.js b/src/errors.js index 59da1d148cf..a0a37768a62 100644 --- a/src/errors.js +++ b/src/errors.js @@ -48,7 +48,7 @@ export const ErrorDetails = { // will be renamed DEMUX_PARSING_ERROR and switched to MUX_ERROR in the next major release FRAG_PARSING_ERROR: 'fragParsingError', // Identifier for a remux alloc error event - data: { id : demuxer Id, frag : fragment object, bytes : nb of bytes on which allocation failed , reason : error text } - REMUX_ALLOC_ERROR : 'remuxAllocError', + REMUX_ALLOC_ERROR: 'remuxAllocError', // Identifier for decrypt key load error - data: { frag : fragment object, response : { code: error code, text: error text }} KEY_LOAD_ERROR: 'keyLoadError', // Identifier for decrypt key load timeout error - data: { frag : fragment object} @@ -66,7 +66,7 @@ export const ErrorDetails = { // Identifier for a buffer seek over hole event BUFFER_SEEK_OVER_HOLE: 'bufferSeekOverHole', // Identifier for a buffer nudge on stall (playback is stuck although currentTime is in a buffered area) - BUFFER_NUDGE_ON_STALL : 'bufferNudgeOnStall', + BUFFER_NUDGE_ON_STALL: 'bufferNudgeOnStall', // Identifier for an internal exception happening inside hls.js while handling an event INTERNAL_EXCEPTION: 'internalException' }; diff --git a/src/event-handler.js b/src/event-handler.js index e94dba8ed53..7b78c4aee15 100644 --- a/src/event-handler.js +++ b/src/event-handler.js @@ -4,8 +4,8 @@ * */ -import {logger} from './utils/logger'; -import {ErrorTypes, ErrorDetails} from './errors'; +import { logger } from './utils/logger'; +import { ErrorTypes, ErrorDetails } from './errors'; import Event from './events'; const FORBIDDEN_EVENT_NAMES = new Set([ @@ -15,8 +15,7 @@ const FORBIDDEN_EVENT_NAMES = new Set([ ]); class EventHandler { - - constructor(hls, ...events) { + constructor (hls, ...events) { this.hls = hls; this.onEvent = this.onEvent.bind(this); this.handledEvents = events; @@ -25,33 +24,33 @@ class EventHandler { this.registerListeners(); } - destroy() { + destroy () { this.onHandlerDestroying(); this.unregisterListeners(); this.onHandlerDestroyed(); } - onHandlerDestroying() {} - onHandlerDestroyed() {} + onHandlerDestroying () {} + onHandlerDestroyed () {} - isEventHandler() { + isEventHandler () { return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function'; } - registerListeners() { + registerListeners () { if (this.isEventHandler()) { - this.handledEvents.forEach(function(event) { - if (FORBIDDEN_EVENT_NAMES.has(event)) { + this.handledEvents.forEach(function (event) { + if (FORBIDDEN_EVENT_NAMES.has(event)) throw new Error('Forbidden event-name: ' + event); - } + this.hls.on(event, this.onEvent); }, this); } } - unregisterListeners() { + unregisterListeners () { if (this.isEventHandler()) { - this.handledEvents.forEach(function(event) { + this.handledEvents.forEach(function (event) { this.hls.off(event, this.onEvent); }, this); } @@ -60,23 +59,23 @@ class EventHandler { /** * arguments: event (string), data (any) */ - onEvent(event, data) { + onEvent (event, data) { this.onEventGeneric(event, data); } - onEventGeneric(event, data) { - var eventToFunction = function(event, data) { - var funcName = 'on' + event.replace('hls', ''); - if (typeof this[funcName] !== 'function') { + onEventGeneric (event, data) { + let eventToFunction = function (event, data) { + let funcName = 'on' + event.replace('hls', ''); + if (typeof this[funcName] !== 'function') throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`); - } + return this[funcName].bind(this, data); }; try { eventToFunction.call(this, event, data).call(); } catch (err) { logger.error(`An internal error happened while handling event ${event}. Error message: "${err.message}". Here is a stacktrace:`, err); - this.hls.trigger(Event.ERROR, {type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event : event, err : err}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event: event, err: err }); } } } diff --git a/src/events.js b/src/events.js index 969fcffc394..5924ab44bfa 100644 --- a/src/events.js +++ b/src/events.js @@ -97,7 +97,7 @@ const HlsEvents = { FRAG_CHANGED: 'hlsFragChanged', // Identifier for a FPS drop event - data: { curentDropped, currentDecoded, totalDroppedFrames } FPS_DROP: 'hlsFpsDrop', - //triggered when FPS drop triggers auto level capping - data: { level, droppedlevel } + // triggered when FPS drop triggers auto level capping - data: { level, droppedlevel } FPS_DROP_LEVEL_CAPPING: 'hlsFpsDropLevelCapping', // Identifier for an error event - data: { type : error type, details : error details, fatal : if true, hls.js cannot/will not try to recover, if false, hls.js will try to recover,other error specific data } ERROR: 'hlsError', diff --git a/src/helper/aac.js b/src/helper/aac.js index be82d700656..f6c2841b5fa 100644 --- a/src/helper/aac.js +++ b/src/helper/aac.js @@ -3,36 +3,36 @@ */ class AAC { - static getSilentFrame(codec,channelCount) { - switch(codec) { - case 'mp4a.40.2': - if (channelCount === 1) { - return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]); - } else if (channelCount === 2) { - return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]); - } else if (channelCount === 3) { - return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]); - } else if (channelCount === 4) { - return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]); - } else if (channelCount === 5) { - return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]); - } else if (channelCount === 6) { - return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]); - } - break; + static getSilentFrame (codec, channelCount) { + switch (codec) { + case 'mp4a.40.2': + if (channelCount === 1) + return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]); + else if (channelCount === 2) + return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]); + else if (channelCount === 3) + return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]); + else if (channelCount === 4) + return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]); + else if (channelCount === 5) + return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]); + else if (channelCount === 6) + return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]); + + break; // handle HE-AAC below (mp4a.40.5 / mp4a.40.29) - default: - if (channelCount === 1) { - // ffmpeg -y -f lavfi -i "aevalsrc=0:d=0.05" -c:a libfdk_aac -profile:a aac_he -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac - return new Uint8Array([0x1,0x40,0x22,0x80,0xa3,0x4e,0xe6,0x80,0xba,0x8,0x0,0x0,0x0,0x1c,0x6,0xf1,0xc1,0xa,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5e]); - } else if (channelCount === 2) { - // ffmpeg -y -f lavfi -i "aevalsrc=0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac - return new Uint8Array([0x1,0x40,0x22,0x80,0xa3,0x5e,0xe6,0x80,0xba,0x8,0x0,0x0,0x0,0x0,0x95,0x0,0x6,0xf1,0xa1,0xa,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5e]); - } else if (channelCount === 3) { - // ffmpeg -y -f lavfi -i "aevalsrc=0|0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac - return new Uint8Array([0x1,0x40,0x22,0x80,0xa3,0x5e,0xe6,0x80,0xba,0x8,0x0,0x0,0x0,0x0,0x95,0x0,0x6,0xf1,0xa1,0xa,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5a,0x5e]); - } - break; + default: + if (channelCount === 1) { + // ffmpeg -y -f lavfi -i "aevalsrc=0:d=0.05" -c:a libfdk_aac -profile:a aac_he -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac + return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x4e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x1c, 0x6, 0xf1, 0xc1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]); + } else if (channelCount === 2) { + // ffmpeg -y -f lavfi -i "aevalsrc=0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac + return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]); + } else if (channelCount === 3) { + // ffmpeg -y -f lavfi -i "aevalsrc=0|0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac + return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]); + } + break; } return null; } diff --git a/src/helper/buffer-helper.js b/src/helper/buffer-helper.js index 054450aa544..ae641119657 100644 --- a/src/helper/buffer-helper.js +++ b/src/helper/buffer-helper.js @@ -9,17 +9,16 @@ const BufferHelper = { * @param {number} position * @returns {boolean} */ - isBuffered : function(media,position) { + isBuffered: function (media, position) { try { if (media) { let buffered = media.buffered; for (let i = 0; i < buffered.length; i++) { - if (position >= buffered.start(i) && position <= buffered.end(i)) { + if (position >= buffered.start(i) && position <= buffered.end(i)) return true; - } } } - } catch(error) { + } catch (error) { // this is to catch // InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer': // This SourceBuffer has been removed from the parent media source @@ -27,52 +26,50 @@ const BufferHelper = { return false; }, - bufferInfo : function(media, pos,maxHoleDuration) { + bufferInfo: function (media, pos, maxHoleDuration) { try { if (media) { - var vbuffered = media.buffered, buffered = [],i; - for (i = 0; i < vbuffered.length; i++) { - buffered.push({start: vbuffered.start(i), end: vbuffered.end(i)}); - } - return this.bufferedInfo(buffered,pos,maxHoleDuration); + let vbuffered = media.buffered, buffered = [], i; + for (i = 0; i < vbuffered.length; i++) + buffered.push({ start: vbuffered.start(i), end: vbuffered.end(i) }); + + return this.bufferedInfo(buffered, pos, maxHoleDuration); } - } catch(error) { - // this is to catch - // InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer': - // This SourceBuffer has been removed from the parent media source + } catch (error) { + // this is to catch + // InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer': + // This SourceBuffer has been removed from the parent media source } - return {len: 0, start: pos, end: pos, nextStart : undefined} ; + return { len: 0, start: pos, end: pos, nextStart: undefined }; }, - bufferedInfo : function(buffered,pos,maxHoleDuration) { - var buffered2 = [], - // bufferStart and bufferEnd are buffer boundaries around current video position - bufferLen,bufferStart, bufferEnd,bufferStartNext,i; + bufferedInfo: function (buffered, pos, maxHoleDuration) { + let buffered2 = [], + // bufferStart and bufferEnd are buffer boundaries around current video position + bufferLen, bufferStart, bufferEnd, bufferStartNext, i; // sort on buffer.start/smaller end (IE does not always return sorted buffered range) buffered.sort(function (a, b) { - var diff = a.start - b.start; - if (diff) { + let diff = a.start - b.start; + if (diff) return diff; - } else { + else return b.end - a.end; - } }); // there might be some small holes between buffer time range // consider that holes smaller than maxHoleDuration are irrelevant and build another // buffer time range representations that discards those holes for (i = 0; i < buffered.length; i++) { - var buf2len = buffered2.length; - if(buf2len) { - var buf2end = buffered2[buf2len - 1].end; + let buf2len = buffered2.length; + if (buf2len) { + let buf2end = buffered2[buf2len - 1].end; // if small hole (value between 0 or maxHoleDuration ) or overlapping (negative) - if((buffered[i].start - buf2end) < maxHoleDuration) { + if ((buffered[i].start - buf2end) < maxHoleDuration) { // merge overlapping time ranges // update lastRange.end only if smaller than item.end // e.g. [ 1, 15] with [ 2,8] => [ 1,15] (no need to modify lastRange.end) // whereas [ 1, 8] with [ 2,15] => [ 1,15] ( lastRange should switch from [1,8] to [1,15]) - if(buffered[i].end > buf2end) { + if (buffered[i].end > buf2end) buffered2[buf2len - 1].end = buffered[i].end; - } } else { // big hole buffered2.push(buffered[i]); @@ -83,9 +80,9 @@ const BufferHelper = { } } for (i = 0, bufferLen = 0, bufferStart = bufferEnd = pos; i < buffered2.length; i++) { - var start = buffered2[i].start, - end = buffered2[i].end; - //logger.log('buf start/end:' + buffered.start(i) + '/' + buffered.end(i)); + let start = buffered2[i].start, + end = buffered2[i].end; + // logger.log('buf start/end:' + buffered.start(i) + '/' + buffered.end(i)); if ((pos + maxHoleDuration) >= start && pos < end) { // play position is inside this buffer TimeRange, retrieve end of buffer position and buffer length bufferStart = start; @@ -96,7 +93,7 @@ const BufferHelper = { break; } } - return {len: bufferLen, start: bufferStart, end: bufferEnd, nextStart : bufferStartNext}; + return { len: bufferLen, start: bufferStart, end: bufferEnd, nextStart: bufferStartNext }; } }; diff --git a/src/helper/fragment-tracker.js b/src/helper/fragment-tracker.js index e77545f06fb..77ca97d1e72 100644 --- a/src/helper/fragment-tracker.js +++ b/src/helper/fragment-tracker.js @@ -5,12 +5,11 @@ export const FragmentState = { NOT_LOADED: 'NOT_LOADED', APPENDING: 'APPENDING', PARTIAL: 'PARTIAL', - OK: 'OK', + OK: 'OK' }; export class FragmentTracker extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.BUFFER_APPENDED, Event.FRAG_BUFFERED, @@ -25,7 +24,7 @@ export class FragmentTracker extends EventHandler { this.config = hls.config; } - destroy() { + destroy () { this.fragments = null; this.timeRanges = null; this.config = null; @@ -33,7 +32,6 @@ export class FragmentTracker extends EventHandler { super.destroy(); } - /** * Return a Fragment that match the position and levelType. * If not found any Fragment, return null @@ -41,16 +39,16 @@ export class FragmentTracker extends EventHandler { * @param {LevelType} levelType * @returns {Fragment|null} */ - getBufferedFrag(position, levelType) { + getBufferedFrag (position, levelType) { const fragments = this.fragments; const bufferedFrags = Object.keys(fragments).filter(key => { const fragmentEntity = fragments[key]; - if(fragmentEntity.body.type !== levelType){ + if (fragmentEntity.body.type !== levelType) return false; - } - if(!fragmentEntity.buffered){ + + if (!fragmentEntity.buffered) return false; - } + const frag = fragmentEntity.body; return frag.startPTS <= position && position <= frag.endPTS; }); @@ -70,19 +68,19 @@ export class FragmentTracker extends EventHandler { * @param {String} elementaryStream The elementaryStream of media this is (eg. video/audio) * @param {TimeRanges} timeRange TimeRange object from a sourceBuffer */ - detectEvictedFragments(elementaryStream, timeRange) { + detectEvictedFragments (elementaryStream, timeRange) { let fragmentTimes, time; // Check if any flagged fragments have been unloaded Object.keys(this.fragments).forEach(key => { const fragmentEntity = this.fragments[key]; - if(fragmentEntity.buffered === true) { + if (fragmentEntity.buffered === true) { const esData = fragmentEntity.range[elementaryStream]; if (esData) { fragmentTimes = esData.time; for (let i = 0; i < fragmentTimes.length; i++) { time = fragmentTimes[i]; - if(this.isTimeBuffered(time.startPTS, time.endPTS, timeRange) === false) { + if (this.isTimeBuffered(time.startPTS, time.endPTS, timeRange) === false) { // Unregister partial fragment as it needs to load again to be reused this.removeFragment(fragmentEntity.body); break; @@ -98,13 +96,13 @@ export class FragmentTracker extends EventHandler { * Partially loaded fragments will be registered as a partial fragment * @param {Object} fragment Check the fragment against all sourceBuffers loaded */ - detectPartialFragments(fragment) { + detectPartialFragments (fragment) { let fragKey = this.getFragmentKey(fragment); let fragmentEntity = this.fragments[fragKey]; fragmentEntity.buffered = true; Object.keys(this.timeRanges).forEach(elementaryStream => { - if(fragment.hasElementaryStream(elementaryStream) === true) { + if (fragment.hasElementaryStream(elementaryStream) === true) { let timeRange = this.timeRanges[elementaryStream]; // Check for malformed fragments // Gaps need to be calculated for each elementaryStream @@ -113,7 +111,7 @@ export class FragmentTracker extends EventHandler { }); } - getBufferedTimes(startPTS, endPTS, timeRange) { + getBufferedTimes (startPTS, endPTS, timeRange) { let fragmentTimes = []; let startTime, endTime; let fragmentPartial = false; @@ -136,7 +134,7 @@ export class FragmentTracker extends EventHandler { endPTS: Math.min(endPTS, timeRange.end(i)) }); fragmentPartial = true; - } else if(endPTS <= startTime) { + } else if (endPTS <= startTime) { // No need to check the rest of the timeRange as it is in order break; } @@ -148,7 +146,7 @@ export class FragmentTracker extends EventHandler { }; } - getFragmentKey(fragment) { + getFragmentKey (fragment) { return `${fragment.type}_${fragment.level}_${fragment.sn}`; } @@ -157,19 +155,19 @@ export class FragmentTracker extends EventHandler { * @param {Number} time * @returns {Object} fragment Returns a partial fragment at a time or null if there is no partial fragment */ - getPartialFragment(time) { + getPartialFragment (time) { let timePadding, startTime, endTime; let bestFragment = null; let bestOverlap = 0; Object.keys(this.fragments).forEach(key => { const fragmentEntity = this.fragments[key]; - if(this.isPartial(fragmentEntity)) { + if (this.isPartial(fragmentEntity)) { startTime = fragmentEntity.body.startPTS - this.bufferPadding; endTime = fragmentEntity.body.endPTS + this.bufferPadding; - if(time >= startTime && time <= endTime) { + if (time >= startTime && time <= endTime) { // Use the fragment that has the most padding from start and end time timePadding = Math.min(time - startTime, endTime - time); - if(bestOverlap <= timePadding) { + if (bestOverlap <= timePadding) { bestFragment = fragmentEntity.body; bestOverlap = timePadding; } @@ -183,39 +181,38 @@ export class FragmentTracker extends EventHandler { * @param {Object} fragment The fragment to check * @returns {String} Returns the fragment state when a fragment never loaded or if it partially loaded */ - getState(fragment) { + getState (fragment) { let fragKey = this.getFragmentKey(fragment); let fragmentEntity = this.fragments[fragKey]; let state = FragmentState.NOT_LOADED; - if(fragmentEntity !== undefined) { - if(!fragmentEntity.buffered) { + if (fragmentEntity !== undefined) { + if (!fragmentEntity.buffered) state = FragmentState.APPENDING; - } else if(this.isPartial(fragmentEntity) === true) { + else if (this.isPartial(fragmentEntity) === true) state = FragmentState.PARTIAL; - } else { + else state = FragmentState.OK; - } } return state; } - isPartial(fragmentEntity) { + isPartial (fragmentEntity) { return fragmentEntity.buffered === true && ((fragmentEntity.range.video !== undefined && fragmentEntity.range.video.partial === true) || (fragmentEntity.range.audio !== undefined && fragmentEntity.range.audio.partial === true)); } - isTimeBuffered(startPTS, endPTS, timeRange) { + isTimeBuffered (startPTS, endPTS, timeRange) { let startTime, endTime; for (let i = 0; i < timeRange.length; i++) { startTime = timeRange.start(i) - this.bufferPadding; endTime = timeRange.end(i) + this.bufferPadding; - if (startPTS >= startTime && endPTS <= endTime) { + if (startPTS >= startTime && endPTS <= endTime) return true; - } - if(endPTS <= startTime) { + + if (endPTS <= startTime) { // No need to check the rest of the timeRange as it is in order return false; } @@ -227,13 +224,13 @@ export class FragmentTracker extends EventHandler { /** * Fires when a fragment loading is completed */ - onFragLoaded(e) { + onFragLoaded (e) { let fragment = e.frag; let fragKey = this.getFragmentKey(fragment); let fragmentEntity = { body: fragment, range: Object.create(null), - buffered: false, + buffered: false }; this.fragments[fragKey] = fragmentEntity; } @@ -241,7 +238,7 @@ export class FragmentTracker extends EventHandler { /** * Fires when the buffer is updated */ - onBufferAppended(e) { + onBufferAppended (e) { // Store the latest timeRanges loaded in the buffer this.timeRanges = e.timeRanges; Object.keys(this.timeRanges).forEach(elementaryStream => { @@ -253,7 +250,7 @@ export class FragmentTracker extends EventHandler { /** * Fires after a fragment has been loaded into the source buffer */ - onFragBuffered(e) { + onFragBuffered (e) { this.detectPartialFragments(e.frag); } @@ -262,7 +259,7 @@ export class FragmentTracker extends EventHandler { * @param {Object} fragment * @returns {boolean} */ - hasFragment(fragment) { + hasFragment (fragment) { const fragKey = this.getFragmentKey(fragment); return this.fragments[fragKey] !== undefined; } @@ -271,7 +268,7 @@ export class FragmentTracker extends EventHandler { * Remove a fragment from fragment tracker until it is loaded again * @param {Object} fragment The fragment to remove */ - removeFragment(fragment) { + removeFragment (fragment) { let fragKey = this.getFragmentKey(fragment); delete this.fragments[fragKey]; } @@ -279,7 +276,7 @@ export class FragmentTracker extends EventHandler { /** * Remove all fragments from fragment tracker. */ - removeAllFragments(){ + removeAllFragments () { this.fragments = Object.create(null); } } diff --git a/src/helper/is-supported.js b/src/helper/is-supported.js index ce3c0a2786c..c6258f86fb5 100644 --- a/src/helper/is-supported.js +++ b/src/helper/is-supported.js @@ -1,6 +1,6 @@ -import {getMediaSource} from './mediasource-helper'; +import { getMediaSource } from './mediasource-helper'; -export function isSupported() { +export function isSupported () { const mediaSource = getMediaSource(); const sourceBuffer = window.SourceBuffer || window.WebKitSourceBuffer; const isTypeSupported = mediaSource && diff --git a/src/helper/level-helper.js b/src/helper/level-helper.js index 329d79220a7..7608f4da647 100644 --- a/src/helper/level-helper.js +++ b/src/helper/level-helper.js @@ -2,47 +2,44 @@ * Level Helper class, providing methods dealing with playlist sliding and drift */ -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; -export function updatePTS(fragments,fromIdx, toIdx) { - var fragFrom = fragments[fromIdx], fragTo = fragments[toIdx], fragToPTS = fragTo.startPTS; +export function updatePTS (fragments, fromIdx, toIdx) { + let fragFrom = fragments[fromIdx], fragTo = fragments[toIdx], fragToPTS = fragTo.startPTS; // if we know startPTS[toIdx] if (!isNaN(fragToPTS)) { // update fragment duration. // it helps to fix drifts between playlist reported duration and fragment real duration if (toIdx > fromIdx) { fragFrom.duration = fragToPTS - fragFrom.start; - if (fragFrom.duration < 0) { + if (fragFrom.duration < 0) logger.warn(`negative duration computed for frag ${fragFrom.sn},level ${fragFrom.level}, there should be some duration drift between playlist and fragment!`); - } } else { fragTo.duration = fragFrom.start - fragToPTS; - if (fragTo.duration < 0) { + if (fragTo.duration < 0) logger.warn(`negative duration computed for frag ${fragTo.sn},level ${fragTo.level}, there should be some duration drift between playlist and fragment!`); - } } } else { // we dont know startPTS[toIdx] - if (toIdx > fromIdx) { + if (toIdx > fromIdx) fragTo.start = fragFrom.start + fragFrom.duration; - } else { + else fragTo.start = Math.max(fragFrom.start - fragTo.duration, 0); - } } } -export function updateFragPTSDTS(details,frag,startPTS,endPTS,startDTS,endDTS) { +export function updateFragPTSDTS (details, frag, startPTS, endPTS, startDTS, endDTS) { // update frag PTS/DTS let maxStartPTS = startPTS; if (!isNaN(frag.startPTS)) { // delta PTS between audio and video let deltaPTS = Math.abs(frag.startPTS - startPTS); - if (isNaN(frag.deltaPTS)) { + if (isNaN(frag.deltaPTS)) frag.deltaPTS = deltaPTS; - } else { + else frag.deltaPTS = Math.max(deltaPTS, frag.deltaPTS); - } - maxStartPTS = Math.max(startPTS,frag.startPTS); + + maxStartPTS = Math.max(startPTS, frag.startPTS); startPTS = Math.min(startPTS, frag.startPTS); endPTS = Math.max(endPTS, frag.endPTS); startDTS = Math.min(startDTS, frag.startDTS); @@ -59,10 +56,10 @@ export function updateFragPTSDTS(details,frag,startPTS,endPTS,startDTS,endDTS) { const sn = frag.sn; // exit if sn out of range - if (!details || sn < details.startSN || sn > details.endSN) { + if (!details || sn < details.startSN || sn > details.endSN) return 0; - } - var fragIdx, fragments, i; + + let fragIdx, fragments, i; fragIdx = sn - details.startSN; fragments = details.fragments; // update frag reference in fragments array @@ -72,74 +69,71 @@ export function updateFragPTSDTS(details,frag,startPTS,endPTS,startDTS,endDTS) { // resulting in invalid sliding computation fragments[fragIdx] = frag; // adjust fragment PTS/duration from seqnum-1 to frag 0 - for (i = fragIdx; i > 0; i--) { + for (i = fragIdx; i > 0; i--) updatePTS(fragments, i, i - 1); - } // adjust fragment PTS/duration from seqnum to last frag - for (i = fragIdx; i < fragments.length - 1; i++) { + for (i = fragIdx; i < fragments.length - 1; i++) updatePTS(fragments, i, i + 1); - } + details.PTSKnown = true; - //logger.log(` frag start/end:${startPTS.toFixed(3)}/${endPTS.toFixed(3)}`); + // logger.log(` frag start/end:${startPTS.toFixed(3)}/${endPTS.toFixed(3)}`); return drift; } -export function mergeDetails(oldDetails,newDetails) { - var start = Math.max(oldDetails.startSN,newDetails.startSN)-newDetails.startSN, - end = Math.min(oldDetails.endSN,newDetails.endSN)-newDetails.startSN, - delta = newDetails.startSN - oldDetails.startSN, - oldfragments = oldDetails.fragments, - newfragments = newDetails.fragments, - ccOffset =0, - PTSFrag; +export function mergeDetails (oldDetails, newDetails) { + let start = Math.max(oldDetails.startSN, newDetails.startSN) - newDetails.startSN, + end = Math.min(oldDetails.endSN, newDetails.endSN) - newDetails.startSN, + delta = newDetails.startSN - oldDetails.startSN, + oldfragments = oldDetails.fragments, + newfragments = newDetails.fragments, + ccOffset = 0, + PTSFrag; // check if old/new playlists have fragments in common - if ( end < start) { - newDetails.PTSKnown = false; - return; - } - // loop through overlapping SN and update startPTS , cc, and duration if any found - for(var i = start ; i <= end ; i++) { - var oldFrag = oldfragments[delta+i], - newFrag = newfragments[i]; - if (newFrag && oldFrag) { - ccOffset = oldFrag.cc - newFrag.cc; - if (!isNaN(oldFrag.startPTS)) { - newFrag.start = newFrag.startPTS = oldFrag.startPTS; - newFrag.endPTS = oldFrag.endPTS; - newFrag.duration = oldFrag.duration; - newFrag.backtracked = oldFrag.backtracked; - newFrag.dropped = oldFrag.dropped; - PTSFrag = newFrag; - } + if (end < start) { + newDetails.PTSKnown = false; + return; + } + // loop through overlapping SN and update startPTS , cc, and duration if any found + for (var i = start; i <= end; i++) { + let oldFrag = oldfragments[delta + i], + newFrag = newfragments[i]; + if (newFrag && oldFrag) { + ccOffset = oldFrag.cc - newFrag.cc; + if (!isNaN(oldFrag.startPTS)) { + newFrag.start = newFrag.startPTS = oldFrag.startPTS; + newFrag.endPTS = oldFrag.endPTS; + newFrag.duration = oldFrag.duration; + newFrag.backtracked = oldFrag.backtracked; + newFrag.dropped = oldFrag.dropped; + PTSFrag = newFrag; } } + } - if(ccOffset) { - logger.log(`discontinuity sliding from playlist, take drift into account`); - for(i = 0 ; i < newfragments.length ; i++) { - newfragments[i].cc += ccOffset; - } - } + if (ccOffset) { + logger.log('discontinuity sliding from playlist, take drift into account'); + for (i = 0; i < newfragments.length; i++) + newfragments[i].cc += ccOffset; + } - // if at least one fragment contains PTS info, recompute PTS information for all fragments - if(PTSFrag) { - updateFragPTSDTS(newDetails,PTSFrag,PTSFrag.startPTS,PTSFrag.endPTS,PTSFrag.startDTS,PTSFrag.endDTS); - } else { - // ensure that delta is within oldfragments range - // also adjust sliding in case delta is 0 (we could have old=[50-60] and new=old=[50-61]) - // in that case we also need to adjust start offset of all fragments - if (delta >= 0 && delta < oldfragments.length) { - // adjust start by sliding offset - var sliding = oldfragments[delta].start; - for(i = 0 ; i < newfragments.length ; i++) { - newfragments[i].start += sliding; - } - } + // if at least one fragment contains PTS info, recompute PTS information for all fragments + if (PTSFrag) { + updateFragPTSDTS(newDetails, PTSFrag, PTSFrag.startPTS, PTSFrag.endPTS, PTSFrag.startDTS, PTSFrag.endDTS); + } else { + // ensure that delta is within oldfragments range + // also adjust sliding in case delta is 0 (we could have old=[50-60] and new=old=[50-61]) + // in that case we also need to adjust start offset of all fragments + if (delta >= 0 && delta < oldfragments.length) { + // adjust start by sliding offset + let sliding = oldfragments[delta].start; + for (i = 0; i < newfragments.length; i++) + newfragments[i].start += sliding; } - // if we are here, it means we have fragments overlapping between - // old and new level. reliable PTS info is thus relying on old level - newDetails.PTSKnown = oldDetails.PTSKnown; } + // if we are here, it means we have fragments overlapping between + // old and new level. reliable PTS info is thus relying on old level + newDetails.PTSKnown = oldDetails.PTSKnown; +} diff --git a/src/helper/mediakeys-helper.js b/src/helper/mediakeys-helper.js index fc0eb7384f0..4257a76bae3 100644 --- a/src/helper/mediakeys-helper.js +++ b/src/helper/mediakeys-helper.js @@ -1,10 +1,8 @@ -const requestMediaKeySystemAccess = (function() { - if (window.navigator && window.navigator.requestMediaKeySystemAccess) { +const requestMediaKeySystemAccess = (function () { + if (window.navigator && window.navigator.requestMediaKeySystemAccess) return window.navigator.requestMediaKeySystemAccess.bind(window.navigator); - } - else { + else return null; - } })(); export { diff --git a/src/helper/mediasource-helper.js b/src/helper/mediasource-helper.js index 2431fd32d84..db87094fdee 100644 --- a/src/helper/mediasource-helper.js +++ b/src/helper/mediasource-helper.js @@ -2,8 +2,7 @@ * MediaSource helper */ -export function getMediaSource() { - if (typeof window !== 'undefined') { +export function getMediaSource () { + if (typeof window !== 'undefined') return window.MediaSource || window.WebKitMediaSource; - } } diff --git a/src/hls.js b/src/hls.js index 2838b108925..6b4d8bfa6ca 100644 --- a/src/hls.js +++ b/src/hls.js @@ -9,14 +9,14 @@ import PlaylistLoader from './loader/playlist-loader'; import FragmentLoader from './loader/fragment-loader'; import KeyLoader from './loader/key-loader'; -import StreamController from './controller/stream-controller'; -import LevelController from './controller/level-controller'; +import StreamController from './controller/stream-controller'; +import LevelController from './controller/level-controller'; import ID3TrackController from './controller/id3-track-controller'; -import {isSupported} from './helper/is-supported'; -import {logger, enableLogs} from './utils/logger'; -import {hlsDefaultConfig} from './config'; -import {FragmentTracker} from './helper/fragment-tracker'; +import { isSupported } from './helper/is-supported'; +import { logger, enableLogs } from './utils/logger'; +import { hlsDefaultConfig } from './config'; +import { FragmentTracker } from './helper/fragment-tracker'; import HlsEvents from './events'; import EventEmitter from 'events'; @@ -30,56 +30,55 @@ require('string.prototype.endswith'); * @constructor */ export default class Hls { - /** * @type {string} */ - static get version() { + static get version () { return __VERSION__; } /** * @type {boolean} */ - static isSupported() { + static isSupported () { return isSupported(); } /** * @type {HlsEvents} */ - static get Events() { + static get Events () { return HlsEvents; } /** * @type {HlsErrorTypes} */ - static get ErrorTypes() { + static get ErrorTypes () { return ErrorTypes; } /** * @type {HlsErrorDetails} */ - static get ErrorDetails() { + static get ErrorDetails () { return ErrorDetails; } /** * @type {HlsConfig} */ - static get DefaultConfig() { - if(!Hls.defaultConfig) { + static get DefaultConfig () { + if (!Hls.defaultConfig) return hlsDefaultConfig; - } + return Hls.defaultConfig; } /** * @type {HlsConfig} */ - static set DefaultConfig(defaultConfig) { + static set DefaultConfig (defaultConfig) { Hls.defaultConfig = defaultConfig; } @@ -89,31 +88,28 @@ export default class Hls { * @constructs Hls * @param {HlsConfig} config */ - constructor(config = {}) { - var defaultConfig = Hls.DefaultConfig; + constructor (config = {}) { + let defaultConfig = Hls.DefaultConfig; - if ((config.liveSyncDurationCount || config.liveMaxLatencyDurationCount) && (config.liveSyncDuration || config.liveMaxLatencyDuration)) { + if ((config.liveSyncDurationCount || config.liveMaxLatencyDurationCount) && (config.liveSyncDuration || config.liveMaxLatencyDuration)) throw new Error('Illegal hls.js config: don\'t mix up liveSyncDurationCount/liveMaxLatencyDurationCount and liveSyncDuration/liveMaxLatencyDuration'); - } - for (var prop in defaultConfig) { - if (prop in config) { continue; } - config[prop] = defaultConfig[prop]; + for (let prop in defaultConfig) { + if (prop in config) continue; + config[prop] = defaultConfig[prop]; } - if (config.liveMaxLatencyDurationCount !== undefined && config.liveMaxLatencyDurationCount <= config.liveSyncDurationCount) { + if (config.liveMaxLatencyDurationCount !== undefined && config.liveMaxLatencyDurationCount <= config.liveSyncDurationCount) throw new Error('Illegal hls.js config: "liveMaxLatencyDurationCount" must be gt "liveSyncDurationCount"'); - } - if (config.liveMaxLatencyDuration !== undefined && (config.liveMaxLatencyDuration <= config.liveSyncDuration || config.liveSyncDuration === undefined)) { + if (config.liveMaxLatencyDuration !== undefined && (config.liveMaxLatencyDuration <= config.liveSyncDuration || config.liveSyncDuration === undefined)) throw new Error('Illegal hls.js config: "liveMaxLatencyDuration" must be gt "liveSyncDuration"'); - } enableLogs(config.debug); this.config = config; this._autoLevelCapping = -1; // observer setup - var observer = this.observer = new EventEmitter(); + let observer = this.observer = new EventEmitter(); observer.trigger = function trigger (event, ...data) { observer.emit(event, event, ...data); }; @@ -132,7 +128,7 @@ export default class Hls { */ const abrController = this.abrController = new config.abrController(this); - const bufferController = new config.bufferController(this); + const bufferController = new config.bufferController(this); const capLevelController = new config.capLevelController(this); const fpsController = new config.fpsController(this); const playListLoader = new PlaylistLoader(this); @@ -162,9 +158,8 @@ export default class Hls { * @var {ICoreComponent | Controller} */ let Controller = config.audioStreamController; - if (Controller) { + if (Controller) networkControllers.push(new Controller(this, fragmentTracker)); - } /** * @member {INetworkController[]} networkControllers @@ -222,9 +217,8 @@ export default class Hls { // optional subtitle controller [config.subtitleStreamController, config.timelineController].forEach(Controller => { - if (Controller) { + if (Controller) coreComponents.push(new Controller(this)); - } }); /** @@ -236,11 +230,11 @@ export default class Hls { /** * Dispose of the instance */ - destroy() { + destroy () { logger.log('destroy'); this.trigger(HlsEvents.DESTROYING); this.detachMedia(); - this.coreComponents.concat(this.networkControllers).forEach(component => {component.destroy();}); + this.coreComponents.concat(this.networkControllers).forEach(component => { component.destroy(); }); this.url = null; this.observer.removeAllListeners(); this._autoLevelCapping = -1; @@ -250,16 +244,16 @@ export default class Hls { * Attach a media element * @param {HTMLMediaElement} media */ - attachMedia(media) { + attachMedia (media) { logger.log('attachMedia'); this.media = media; - this.trigger(HlsEvents.MEDIA_ATTACHING, {media: media}); + this.trigger(HlsEvents.MEDIA_ATTACHING, { media: media }); } /** * Detach from the media */ - detachMedia() { + detachMedia () { logger.log('detachMedia'); this.trigger(HlsEvents.MEDIA_DETACHING); this.media = null; @@ -269,12 +263,12 @@ export default class Hls { * Set the source URL. Can be relative or absolute. * @param {string} url */ - loadSource(url) { + loadSource (url) { url = URLToolkit.buildAbsoluteURL(window.location.href, url, { alwaysNormalize: true }); logger.log(`loadSource:${url}`); this.url = url; // when attaching to a source URL, trigger a playlist load - this.trigger(HlsEvents.MANIFEST_LOADING, {url: url}); + this.trigger(HlsEvents.MANIFEST_LOADING, { url: url }); } /** @@ -284,23 +278,23 @@ export default class Hls { * @param {number} startPosition Set the start position to stream from * @default -1 None (from earliest point) */ - startLoad(startPosition = -1) { + startLoad (startPosition = -1) { logger.log(`startLoad(${startPosition})`); - this.networkControllers.forEach(controller => {controller.startLoad(startPosition);}); + this.networkControllers.forEach(controller => { controller.startLoad(startPosition); }); } /** * Stop loading of any stream data. */ - stopLoad() { + stopLoad () { logger.log('stopLoad'); - this.networkControllers.forEach(controller => {controller.stopLoad();}); + this.networkControllers.forEach(controller => { controller.stopLoad(); }); } /** * Swap through possible audio codecs in the stream (for example to switch from stereo to 5.1) */ - swapAudioCodec() { + swapAudioCodec () { logger.log('swapAudioCodec'); this.streamController.swapAudioCodec(); } @@ -311,9 +305,9 @@ export default class Hls { * * Automatic recovery of media-errors by this process is configurable. */ - recoverMediaError() { + recoverMediaError () { logger.log('recoverMediaError'); - var media = this.media; + let media = this.media; this.detachMedia(); this.attachMedia(media); } @@ -321,7 +315,7 @@ export default class Hls { /** * @type {QualityLevel[]} */ - get levels() { + get levels () { return this.levelController.levels; } @@ -329,7 +323,7 @@ export default class Hls { * Index of quality level currently played * @type {number} */ - get currentLevel() { + get currentLevel () { return this.streamController.currentLevel; } @@ -339,7 +333,7 @@ export default class Hls { * That means playback will interrupt at least shortly to re-buffer and re-sync eventually. * @type {number} -1 for automatic level selection */ - set currentLevel(newLevel) { + set currentLevel (newLevel) { logger.log(`set currentLevel:${newLevel}`); this.loadLevel = newLevel; this.streamController.immediateLevelSwitch(); @@ -349,7 +343,7 @@ export default class Hls { * Index of next quality level loaded as scheduled by stream controller. * @type {number} */ - get nextLevel() { + get nextLevel () { return this.streamController.nextLevel; } @@ -359,7 +353,7 @@ export default class Hls { * May abort current loading of data, and flush parts of buffer (outside currently played fragment region). * @type {number} -1 for automatic level selection */ - set nextLevel(newLevel) { + set nextLevel (newLevel) { logger.log(`set nextLevel:${newLevel}`); this.levelController.manualLevel = newLevel; this.streamController.nextLevelSwitch(); @@ -369,7 +363,7 @@ export default class Hls { * Return the quality level of the currently or last (of none is loaded currently) segment * @type {number} */ - get loadLevel() { + get loadLevel () { return this.levelController.level; } @@ -379,7 +373,7 @@ export default class Hls { * Thus the moment when the quality switch will appear in effect will only be after the already existing buffer. * @type {number} newLevel -1 for automatic level selection */ - set loadLevel(newLevel) { + set loadLevel (newLevel) { logger.log(`set loadLevel:${newLevel}`); this.levelController.manualLevel = newLevel; } @@ -388,7 +382,7 @@ export default class Hls { * get next quality level loaded * @type {number} */ - get nextLoadLevel() { + get nextLoadLevel () { return this.levelController.nextLoadLevel; } @@ -397,7 +391,7 @@ export default class Hls { * Same as `loadLevel` but will wait for next switch (until current loading is done). * @type {number} level */ - set nextLoadLevel(level) { + set nextLoadLevel (level) { this.levelController.nextLoadLevel = level; } @@ -406,7 +400,7 @@ export default class Hls { * falls back to index of first level referenced in manifest * @type {number} */ - get firstLevel() { + get firstLevel () { return Math.max(this.levelController.firstLevel, this.minAutoLevel); } @@ -414,7 +408,7 @@ export default class Hls { * Sets "first-level", see getter. * @type {number} */ - set firstLevel(newLevel) { + set firstLevel (newLevel) { logger.log(`set firstLevel:${newLevel}`); this.levelController.firstLevel = newLevel; } @@ -426,7 +420,7 @@ export default class Hls { * (determined from download of first segment) * @type {number} */ - get startLevel() { + get startLevel () { return this.levelController.startLevel; } @@ -437,13 +431,13 @@ export default class Hls { * (determined from download of first segment) * @type {number} newLevel */ - set startLevel(newLevel) { + set startLevel (newLevel) { logger.log(`set startLevel:${newLevel}`); const hls = this; // if not in automatic start level detection, ensure startLevel is greater than minAutoLevel - if (newLevel !== -1) { - newLevel = Math.max(newLevel,hls.minAutoLevel); - } + if (newLevel !== -1) + newLevel = Math.max(newLevel, hls.minAutoLevel); + hls.levelController.startLevel = newLevel; } @@ -451,7 +445,7 @@ export default class Hls { * Capping/max level value that should be used by automatic level selection algorithm (`ABRController`) * @type {number} */ - get autoLevelCapping() { + get autoLevelCapping () { return this._autoLevelCapping; } @@ -459,7 +453,7 @@ export default class Hls { * Capping/max level value that should be used by automatic level selection algorithm (`ABRController`) * @type {number} */ - set autoLevelCapping(newLevel) { + set autoLevelCapping (newLevel) { logger.log(`set autoLevelCapping:${newLevel}`); this._autoLevelCapping = newLevel; } @@ -468,16 +462,15 @@ export default class Hls { * True when automatic level selection enabled * @type {boolean} */ - get autoLevelEnabled() { + get autoLevelEnabled () { return (this.levelController.manualLevel === -1); } - /** * Level set manually (if any) * @type {number} */ - get manualLevel() { + get manualLevel () { return this.levelController.manualLevel; } @@ -485,13 +478,12 @@ export default class Hls { * min level selectable in auto mode according to config.minAutoBitrate * @type {number} */ - get minAutoLevel() { + get minAutoLevel () { let hls = this, levels = hls.levels, minAutoBitrate = hls.config.minAutoBitrate, len = levels ? levels.length : 0; for (let i = 0; i < len; i++) { - const levelNextBitrate = levels[i].realBitrate ? Math.max(levels[i].realBitrate,levels[i].bitrate) : levels[i].bitrate; - if (levelNextBitrate > minAutoBitrate) { + const levelNextBitrate = levels[i].realBitrate ? Math.max(levels[i].realBitrate, levels[i].bitrate) : levels[i].bitrate; + if (levelNextBitrate > minAutoBitrate) return i; - } } return 0; } @@ -500,16 +492,16 @@ export default class Hls { * max level selectable in auto mode according to autoLevelCapping * @type {number} */ - get maxAutoLevel() { + get maxAutoLevel () { const hls = this; const levels = hls.levels; const autoLevelCapping = hls.autoLevelCapping; let maxAutoLevel; - if (autoLevelCapping=== -1 && levels && levels.length) { + if (autoLevelCapping === -1 && levels && levels.length) maxAutoLevel = levels.length - 1; - } else { + else maxAutoLevel = autoLevelCapping; - } + return maxAutoLevel; } @@ -517,10 +509,10 @@ export default class Hls { * next automatically selected quality level * @type {number} */ - get nextAutoLevel() { + get nextAutoLevel () { const hls = this; // ensure next auto level is between min and max auto level - return Math.min(Math.max(hls.abrController.nextAutoLevel,hls.minAutoLevel),hls.maxAutoLevel); + return Math.min(Math.max(hls.abrController.nextAutoLevel, hls.minAutoLevel), hls.maxAutoLevel); } /** @@ -531,15 +523,15 @@ export default class Hls { * this value will be resetted to -1 by ABR controller. * @type {number} */ - set nextAutoLevel(nextLevel) { + set nextAutoLevel (nextLevel) { const hls = this; - hls.abrController.nextAutoLevel = Math.max(hls.minAutoLevel,nextLevel); + hls.abrController.nextAutoLevel = Math.max(hls.minAutoLevel, nextLevel); } /** * @type {AudioTrack[]} */ - get audioTracks() { + get audioTracks () { const audioTrackController = this.audioTrackController; return audioTrackController ? audioTrackController.audioTracks : []; } @@ -548,7 +540,7 @@ export default class Hls { * index of the selected audio track (index in audio track lists) * @type {number} */ - get audioTrack() { + get audioTrack () { const audioTrackController = this.audioTrackController; return audioTrackController ? audioTrackController.audioTrack : -1; } @@ -557,17 +549,16 @@ export default class Hls { * selects an audio track, based on its index in audio track lists * @type {number} */ - set audioTrack(audioTrackId) { + set audioTrack (audioTrackId) { const audioTrackController = this.audioTrackController; - if (audioTrackController) { + if (audioTrackController) audioTrackController.audioTrack = audioTrackId; - } } /** * @type {Seconds} */ - get liveSyncPosition() { + get liveSyncPosition () { return this.streamController.liveSyncPosition; } @@ -575,7 +566,7 @@ export default class Hls { * get alternate subtitle tracks list from playlist * @type {SubtitleTrack[]} */ - get subtitleTracks() { + get subtitleTracks () { const subtitleTrackController = this.subtitleTrackController; return subtitleTrackController ? subtitleTrackController.subtitleTracks : []; } @@ -584,7 +575,7 @@ export default class Hls { * index of the selected subtitle track (index in subtitle track lists) * @type {number} */ - get subtitleTrack() { + get subtitleTrack () { const subtitleTrackController = this.subtitleTrackController; return subtitleTrackController ? subtitleTrackController.subtitleTrack : -1; } @@ -593,17 +584,16 @@ export default class Hls { * select an subtitle track, based on its index in subtitle track lists * @type{number} */ - set subtitleTrack(subtitleTrackId) { + set subtitleTrack (subtitleTrackId) { const subtitleTrackController = this.subtitleTrackController; - if (subtitleTrackController) { + if (subtitleTrackController) subtitleTrackController.subtitleTrack = subtitleTrackId; - } } /** * @type {booelan} */ - get subtitleDisplay() { + get subtitleDisplay () { const subtitleTrackController = this.subtitleTrackController; return subtitleTrackController ? subtitleTrackController.subtitleDisplay : false; } @@ -612,10 +602,9 @@ export default class Hls { * Enable/disable subtitle display rendering * @type {boolean} */ - set subtitleDisplay(value) { + set subtitleDisplay (value) { const subtitleTrackController = this.subtitleTrackController; - if (subtitleTrackController) { + if (subtitleTrackController) subtitleTrackController.subtitleDisplay = value; - } } } diff --git a/src/loader/fragment-loader.js b/src/loader/fragment-loader.js index 5cfcc8aa0c9..50b10e43c27 100644 --- a/src/loader/fragment-loader.js +++ b/src/loader/fragment-loader.js @@ -4,36 +4,34 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {logger} from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { logger } from '../utils/logger'; class FragmentLoader extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.FRAG_LOADING); this.loaders = {}; } - destroy() { + destroy () { let loaders = this.loaders; for (let loaderName in loaders) { let loader = loaders[loaderName]; - if (loader) { + if (loader) loader.destroy(); - } } this.loaders = {}; super.destroy(); } - onFragLoading(data) { + onFragLoading (data) { const frag = data.frag, - type = frag.type, - loaders = this.loaders, - config = this.hls.config, - FragmentILoader = config.fLoader, - DefaultILoader = config.loader; + type = frag.type, + loaders = this.loaders, + config = this.hls.config, + FragmentILoader = config.fLoader, + DefaultILoader = config.loader; // reset fragment state frag.loaded = 0; @@ -45,14 +43,14 @@ class FragmentLoader extends EventHandler { } loader = loaders[type] = frag.loader = - !!config.fLoader ? new FragmentILoader(config) : new DefaultILoader(config); + config.fLoader ? new FragmentILoader(config) : new DefaultILoader(config); let loaderContext, loaderConfig, loaderCallbacks; - loaderContext = { url : frag.url, frag : frag, responseType : 'arraybuffer', progressData : false}; + loaderContext = { url: frag.url, frag: frag, responseType: 'arraybuffer', progressData: false }; let start = frag.byteRangeStartOffset, - end = frag.byteRangeEndOffset; + end = frag.byteRangeEndOffset; if (!isNaN(start) && !isNaN(end)) { loaderContext.rangeStart = start; @@ -61,7 +59,7 @@ class FragmentLoader extends EventHandler { loaderConfig = { timeout: config.fragLoadingTimeOut, - maxRetry: 0 , + maxRetry: 0, retryDelay: 0, maxRetryDelay: config.fragLoadingMaxRetryTimeout }; @@ -76,37 +74,37 @@ class FragmentLoader extends EventHandler { loader.load(loaderContext, loaderConfig, loaderCallbacks); } - loadsuccess(response, stats, context, networkDetails=null) { + loadsuccess (response, stats, context, networkDetails = null) { let payload = response.data, frag = context.frag; // detach fragment loader on load success frag.loader = undefined; this.loaders[frag.type] = undefined; - this.hls.trigger(Event.FRAG_LOADED, {payload: payload, frag: frag, stats: stats, networkDetails: networkDetails}); + this.hls.trigger(Event.FRAG_LOADED, { payload: payload, frag: frag, stats: stats, networkDetails: networkDetails }); } - loaderror(response, context, networkDetails=null) { + loaderror (response, context, networkDetails = null) { let loader = context.loader; - if (loader) { + if (loader) loader.abort(); - } + this.loaders[context.type] = undefined; - this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_ERROR, fatal: false, frag: context.frag, response: response, networkDetails: networkDetails}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_ERROR, fatal: false, frag: context.frag, response: response, networkDetails: networkDetails }); } - loadtimeout(stats, context, networkDetails=null) { + loadtimeout (stats, context, networkDetails = null) { let loader = context.loader; - if (loader) { + if (loader) loader.abort(); - } + this.loaders[context.type] = undefined; - this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_TIMEOUT, fatal: false, frag: context.frag, networkDetails: networkDetails}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.FRAG_LOAD_TIMEOUT, fatal: false, frag: context.frag, networkDetails: networkDetails }); } // data will be used for progressive parsing - loadprogress(stats, context, data, networkDetails=null) { // jshint ignore:line + loadprogress (stats, context, data, networkDetails = null) { // jshint ignore:line let frag = context.frag; frag.loaded = stats.loaded; - this.hls.trigger(Event.FRAG_LOAD_PROGRESS, {frag: frag, stats: stats, networkDetails: networkDetails}); + this.hls.trigger(Event.FRAG_LOAD_PROGRESS, { frag: frag, stats: stats, networkDetails: networkDetails }); } } diff --git a/src/loader/fragment.js b/src/loader/fragment.js index 059e4732fb7..aba0de784e1 100644 --- a/src/loader/fragment.js +++ b/src/loader/fragment.js @@ -4,8 +4,7 @@ import URLToolkit from 'url-toolkit'; import LevelKey from './level-key'; export default class Fragment { - - constructor() { + constructor () { this._url = null; this._byteRange = null; this._decryptdata = null; @@ -13,8 +12,8 @@ export default class Fragment { // Holds the types of data this fragment supports this._elementaryStreams = { - [Fragment.ElementaryStreamTypes.AUDIO] : false, - [Fragment.ElementaryStreamTypes.VIDEO] : false + [Fragment.ElementaryStreamTypes.AUDIO]: false, + [Fragment.ElementaryStreamTypes.VIDEO]: false }; } @@ -23,46 +22,44 @@ export default class Fragment { * * @enum */ - static get ElementaryStreamTypes() { + static get ElementaryStreamTypes () { return { AUDIO: 'audio', VIDEO: 'video' }; } - get url() { - if (!this._url && this.relurl) { + get url () { + if (!this._url && this.relurl) this._url = URLToolkit.buildAbsoluteURL(this.baseurl, this.relurl, { alwaysNormalize: true }); - } + return this._url; } - set url(value) { + set url (value) { this._url = value; } - get programDateTime() { - if (!this._programDateTime && this.rawProgramDateTime) { + get programDateTime () { + if (!this._programDateTime && this.rawProgramDateTime) this._programDateTime = new Date(Date.parse(this.rawProgramDateTime)); - } + return this._programDateTime; } - get byteRange() { - if (!this._byteRange && !this.rawByteRange) { + get byteRange () { + if (!this._byteRange && !this.rawByteRange) return []; - } - if (this._byteRange) { + if (this._byteRange) return this._byteRange; - } let byteRange = []; if (this.rawByteRange) { const params = this.rawByteRange.split('@', 2); if (params.length === 1) { const lastByteRangeEndOffset = this.lastByteRangeEndOffset; - byteRange[0] = lastByteRangeEndOffset ? lastByteRangeEndOffset : 0; + byteRange[0] = lastByteRangeEndOffset || 0; } else { byteRange[0] = parseInt(params[1]); } @@ -75,32 +72,32 @@ export default class Fragment { /** * @type {number} */ - get byteRangeStartOffset() { + get byteRangeStartOffset () { return this.byteRange[0]; } - get byteRangeEndOffset() { + get byteRangeEndOffset () { return this.byteRange[1]; } - get decryptdata() { - if (!this._decryptdata) { + get decryptdata () { + if (!this._decryptdata) this._decryptdata = this.fragmentDecryptdataFromLevelkey(this.levelkey, this.sn); - } + return this._decryptdata; } /** * @param {ElementaryStreamType} type */ - addElementaryStream(type) { + addElementaryStream (type) { this._elementaryStreams[type] = true; } /** * @param {ElementaryStreamType} type */ - hasElementaryStream(type) { + hasElementaryStream (type) { return this._elementaryStreams[type] === true; } @@ -108,12 +105,11 @@ export default class Fragment { * Utility method for parseLevelPlaylist to create an initialization vector for a given segment * @returns {Uint8Array} */ - createInitializationVector(segmentNumber) { - var uint8View = new Uint8Array(16); + createInitializationVector (segmentNumber) { + let uint8View = new Uint8Array(16); - for (var i = 12; i < 16; i++) { + for (let i = 12; i < 16; i++) uint8View[i] = (segmentNumber >> 8 * (15 - i)) & 0xff; - } return uint8View; } @@ -124,8 +120,8 @@ export default class Fragment { * @param segmentNumber - the fragment's segment number * @returns {*} - an object to be applied as a fragment's decryptdata */ - fragmentDecryptdataFromLevelkey(levelkey, segmentNumber) { - var decryptdata = levelkey; + fragmentDecryptdataFromLevelkey (levelkey, segmentNumber) { + let decryptdata = levelkey; if (levelkey && levelkey.method && levelkey.uri && !levelkey.iv) { decryptdata = new LevelKey(); diff --git a/src/loader/key-loader.js b/src/loader/key-loader.js index 7adad7925c7..9013850f525 100644 --- a/src/loader/key-loader.js +++ b/src/loader/key-loader.js @@ -4,86 +4,84 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {ErrorTypes, ErrorDetails} from '../errors'; -import {logger} from '../utils/logger'; +import { ErrorTypes, ErrorDetails } from '../errors'; +import { logger } from '../utils/logger'; class KeyLoader extends EventHandler { - - constructor(hls) { + constructor (hls) { super(hls, Event.KEY_LOADING); this.loaders = {}; this.decryptkey = null; this.decrypturl = null; } - destroy() { + destroy () { for (let loaderName in this.loaders) { let loader = this.loaders[loaderName]; - if (loader) { + if (loader) loader.destroy(); - } } this.loaders = {}; EventHandler.prototype.destroy.call(this); } - onKeyLoading(data) { + onKeyLoading (data) { let frag = data.frag, - type = frag.type, - loader = this.loaders[type], - decryptdata = frag.decryptdata, - uri = decryptdata.uri; - // if uri is different from previous one or if decrypt key not retrieved yet - if (uri !== this.decrypturl || this.decryptkey === null) { - let config = this.hls.config; + type = frag.type, + loader = this.loaders[type], + decryptdata = frag.decryptdata, + uri = decryptdata.uri; + // if uri is different from previous one or if decrypt key not retrieved yet + if (uri !== this.decrypturl || this.decryptkey === null) { + let config = this.hls.config; - if (loader) { - logger.warn(`abort previous key loader for type:${type}`); - loader.abort(); - } - frag.loader = this.loaders[type] = new config.loader(config); - this.decrypturl = uri; - this.decryptkey = null; - - let loaderContext, loaderConfig, loaderCallbacks; - loaderContext = { url : uri, frag : frag, responseType : 'arraybuffer'}; - loaderConfig = { timeout : config.fragLoadingTimeOut, maxRetry : config.fragLoadingMaxRetry , retryDelay : config.fragLoadingRetryDelay, maxRetryDelay : config.fragLoadingMaxRetryTimeout}; - loaderCallbacks = { onSuccess : this.loadsuccess.bind(this), onError :this.loaderror.bind(this), onTimeout : this.loadtimeout.bind(this)}; - frag.loader.load(loaderContext,loaderConfig,loaderCallbacks); - } else if (this.decryptkey) { - // we already loaded this key, return it - decryptdata.key = this.decryptkey; - this.hls.trigger(Event.KEY_LOADED, {frag: frag}); + if (loader) { + logger.warn(`abort previous key loader for type:${type}`); + loader.abort(); } + frag.loader = this.loaders[type] = new config.loader(config); + this.decrypturl = uri; + this.decryptkey = null; + + let loaderContext, loaderConfig, loaderCallbacks; + loaderContext = { url: uri, frag: frag, responseType: 'arraybuffer' }; + loaderConfig = { timeout: config.fragLoadingTimeOut, maxRetry: config.fragLoadingMaxRetry, retryDelay: config.fragLoadingRetryDelay, maxRetryDelay: config.fragLoadingMaxRetryTimeout }; + loaderCallbacks = { onSuccess: this.loadsuccess.bind(this), onError: this.loaderror.bind(this), onTimeout: this.loadtimeout.bind(this) }; + frag.loader.load(loaderContext, loaderConfig, loaderCallbacks); + } else if (this.decryptkey) { + // we already loaded this key, return it + decryptdata.key = this.decryptkey; + this.hls.trigger(Event.KEY_LOADED, { frag: frag }); + } } - loadsuccess(response, stats, context) { + loadsuccess (response, stats, context) { let frag = context.frag; this.decryptkey = frag.decryptdata.key = new Uint8Array(response.data); // detach fragment loader on load success frag.loader = undefined; this.loaders[frag.type] = undefined; - this.hls.trigger(Event.KEY_LOADED, {frag: frag}); + this.hls.trigger(Event.KEY_LOADED, { frag: frag }); } - loaderror(response, context) { + loaderror (response, context) { let frag = context.frag, - loader = frag.loader; - if (loader) { + loader = frag.loader; + if (loader) loader.abort(); - } + this.loaders[context.type] = undefined; - this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.KEY_LOAD_ERROR, fatal: false, frag: frag, response: response}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.KEY_LOAD_ERROR, fatal: false, frag: frag, response: response }); } - loadtimeout(stats, context) { + loadtimeout (stats, context) { let frag = context.frag, - loader = frag.loader; - if (loader) { + loader = frag.loader; + if (loader) loader.abort(); - } + this.loaders[context.type] = undefined; - this.hls.trigger(Event.ERROR, {type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.KEY_LOAD_TIMEOUT, fatal: false, frag: frag}); + this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.KEY_LOAD_TIMEOUT, fatal: false, frag: frag }); } } diff --git a/src/loader/level-key.js b/src/loader/level-key.js index 20a4892a581..76c721d7a78 100644 --- a/src/loader/level-key.js +++ b/src/loader/level-key.js @@ -1,18 +1,17 @@ import URLToolkit from 'url-toolkit'; export default class LevelKey { - - constructor() { + constructor () { this.method = null; this.key = null; this.iv = null; this._uri = null; } - get uri() { - if (!this._uri && this.reluri) { + get uri () { + if (!this._uri && this.reluri) this._uri = URLToolkit.buildAbsoluteURL(this.baseuri, this.reluri, { alwaysNormalize: true }); - } + return this._uri; } } diff --git a/src/loader/m3u8-parser.js b/src/loader/m3u8-parser.js index a6f81cbfd71..3b235f88fcf 100644 --- a/src/loader/m3u8-parser.js +++ b/src/loader/m3u8-parser.js @@ -5,8 +5,8 @@ import Fragment from './fragment'; import LevelKey from './level-key'; import AttrList from '../utils/attr-list'; -import {logger} from '../utils/logger'; -import {isCodecType} from '../utils/codecs'; +import { logger } from '../utils/logger'; +import { isCodecType } from '../utils/codecs'; /** * M3U8 parser @@ -19,35 +19,32 @@ const MASTER_PLAYLIST_MEDIA_REGEX = /#EXT-X-MEDIA:(.*)/g; const LEVEL_PLAYLIST_REGEX_FAST = new RegExp([ /#EXTINF:\s*(\d*(?:\.\d+)?)(?:,(.*)\s+)?/.source, // duration (#EXTINF:,), group 1 => duration, group 2 => title - /|(?!#)(\S+)/.source, // segment URI, group 3 => the URI (note newline is not eaten) - /|#EXT-X-BYTERANGE:*(.+)/.source, // next segment's byterange, group 4 => range spec (x@y) - /|#EXT-X-PROGRAM-DATE-TIME:(.+)/.source, // next segment's program date/time group 5 => the datetime spec - /|#.*/.source // All other non-segment oriented tags will match with all groups empty + /|(?!#)(\S+)/.source, // segment URI, group 3 => the URI (note newline is not eaten) + /|#EXT-X-BYTERANGE:*(.+)/.source, // next segment's byterange, group 4 => range spec (x@y) + /|#EXT-X-PROGRAM-DATE-TIME:(.+)/.source, // next segment's program date/time group 5 => the datetime spec + /|#.*/.source // All other non-segment oriented tags will match with all groups empty ].join(''), 'g'); const LEVEL_PLAYLIST_REGEX_SLOW = /(?:(?:#(EXTM3U))|(?:#EXT-X-(PLAYLIST-TYPE):(.+))|(?:#EXT-X-(MEDIA-SEQUENCE): *(\d+))|(?:#EXT-X-(TARGETDURATION): *(\d+))|(?:#EXT-X-(KEY):(.+))|(?:#EXT-X-(START):(.+))|(?:#EXT-X-(ENDLIST))|(?:#EXT-X-(DISCONTINUITY-SEQ)UENCE:(\d+))|(?:#EXT-X-(DIS)CONTINUITY))|(?:#EXT-X-(VERSION):(\d+))|(?:#EXT-X-(MAP):(.+))|(?:(#)(.*):(.*))|(?:(#)(.*))(?:.*)\r?\n?/; export default class M3U8Parser { - - static findGroup(groups, mediaGroupId) { - if (!groups) { + static findGroup (groups, mediaGroupId) { + if (!groups) return null; - } let matchingGroup = null; - for (let i = 0; i < groups.length; i ++) { + for (let i = 0; i < groups.length; i++) { const group = groups[i]; - if (group.id === mediaGroupId) { + if (group.id === mediaGroupId) matchingGroup = group; - } } return matchingGroup; } - static convertAVC1ToAVCOTI(codec) { - var result, avcdata = codec.split('.'); + static convertAVC1ToAVCOTI (codec) { + let result, avcdata = codec.split('.'); if (avcdata.length > 2) { result = avcdata.shift() + '.'; result += parseInt(avcdata.shift()).toString(16); @@ -58,15 +55,15 @@ export default class M3U8Parser { return result; } - static resolve(url, baseUrl) { + static resolve (url, baseUrl) { return URLToolkit.buildAbsoluteURL(baseUrl, url, { alwaysNormalize: true }); } - static parseMasterPlaylist(string, baseurl) { + static parseMasterPlaylist (string, baseurl) { let levels = [], result; MASTER_PLAYLIST_REGEX.lastIndex = 0; - function setCodecs(codecs, level) { + function setCodecs (codecs, level) { ['video', 'audio'].forEach((type) => { const filtered = codecs.filter((codec) => isCodecType(codec, type)); if (filtered.length) { @@ -83,14 +80,14 @@ export default class M3U8Parser { level.unknownCodecs = codecs; } - while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null){ + while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null) { const level = {}; - var attrs = level.attrs = new AttrList(result[1]); + let attrs = level.attrs = new AttrList(result[1]); level.url = M3U8Parser.resolve(result[2], baseurl); - var resolution = attrs.decimalResolution('RESOLUTION'); - if(resolution) { + let resolution = attrs.decimalResolution('RESOLUTION'); + if (resolution) { level.width = resolution.width; level.height = resolution.height; } @@ -99,16 +96,15 @@ export default class M3U8Parser { setCodecs([].concat((attrs.CODECS || '').split(/[ ,]+/)), level); - if (level.videoCodec && level.videoCodec.indexOf('avc1') !== -1) { + if (level.videoCodec && level.videoCodec.indexOf('avc1') !== -1) level.videoCodec = M3U8Parser.convertAVC1ToAVCOTI(level.videoCodec); - } levels.push(level); } return levels; } - static parseMasterPlaylistMedia(string, baseurl, type, audioGroups=[]) { + static parseMasterPlaylistMedia (string, baseurl, type, audioGroups = []) { let result; let medias = []; let id = 0; @@ -123,13 +119,13 @@ export default class M3U8Parser { media.default = (attrs.DEFAULT === 'YES'); media.autoselect = (attrs.AUTOSELECT === 'YES'); media.forced = (attrs.FORCED === 'YES'); - if (attrs.URI) { + if (attrs.URI) media.url = M3U8Parser.resolve(attrs.URI, baseurl); - } + media.lang = attrs.LANGUAGE; - if (!media.name) { - media.name = media.lang; - } + if (!media.name) + media.name = media.lang; + if (audioGroups.length) { const groupCodec = M3U8Parser.findGroup(audioGroups, media.groupId); media.audioCodec = groupCodec ? groupCodec.codec : audioGroups[0].codec; @@ -141,16 +137,16 @@ export default class M3U8Parser { return medias; } - static parseLevelPlaylist(string, baseurl, id, type) { - var currentSN = 0, - totalduration = 0, - level = {type: null, version: null, url: baseurl, fragments: [], live: true, startSN: 0}, - levelkey = new LevelKey(), - cc = 0, - prevFrag = null, - frag = new Fragment(), - result, - i; + static parseLevelPlaylist (string, baseurl, id, type) { + let currentSN = 0, + totalduration = 0, + level = { type: null, version: null, url: baseurl, fragments: [], live: true, startSN: 0 }, + levelkey = new LevelKey(), + cc = 0, + prevFrag = null, + frag = new Fragment(), + result, + i; LEVEL_PLAYLIST_REGEX_FAST.lastIndex = 0; @@ -160,8 +156,8 @@ export default class M3U8Parser { frag.duration = parseFloat(duration); // avoid sliced strings https://github.com/video-dev/hls.js/issues/939 const title = (' ' + result[2]).slice(1); - frag.title = title ? title : null; - frag.tagList.push(title ? [ 'INF',duration,title ] : [ 'INF',duration ]); + frag.title = title || null; + frag.tagList.push(title ? [ 'INF', duration, title ] : [ 'INF', duration ]); } else if (result[3]) { // url if (!isNaN(frag.duration)) { const sn = currentSN++; @@ -175,19 +171,19 @@ export default class M3U8Parser { // avoid sliced strings https://github.com/video-dev/hls.js/issues/939 frag.relurl = (' ' + result[3]).slice(1); - if(level.programDateTime){ - if(prevFrag){ - if(frag.rawProgramDateTime){//PDT discontinuity found - frag.pdt = Date.parse(frag.rawProgramDateTime); - } else {//Contiguous fragment - frag.pdt = prevFrag.pdt + (prevFrag.duration * 1000); - } - } else {//First fragment - frag.pdt = Date.parse(level.programDateTime); - } - frag.endPdt = frag.pdt + (frag.duration * 1000); - } - + if (level.programDateTime) { + if (prevFrag) { + if (frag.rawProgramDateTime) { // PDT discontinuity found + frag.pdt = Date.parse(frag.rawProgramDateTime); + } else { // Contiguous fragment + frag.pdt = prevFrag.pdt + (prevFrag.duration * 1000); + } + } else { // First fragment + frag.pdt = Date.parse(level.programDateTime); + } + frag.endPdt = frag.pdt + (frag.duration * 1000); + } + level.fragments.push(frag); prevFrag = frag; totalduration += frag.duration; @@ -198,109 +194,105 @@ export default class M3U8Parser { frag.rawByteRange = (' ' + result[4]).slice(1); if (prevFrag) { const lastByteRangeEndOffset = prevFrag.byteRangeEndOffset; - if (lastByteRangeEndOffset) { + if (lastByteRangeEndOffset) frag.lastByteRangeEndOffset = lastByteRangeEndOffset; - } } } else if (result[5]) { // PROGRAM-DATE-TIME // avoid sliced strings https://github.com/video-dev/hls.js/issues/939 frag.rawProgramDateTime = (' ' + result[5]).slice(1); frag.tagList.push(['PROGRAM-DATE-TIME', frag.rawProgramDateTime]); - if (level.programDateTime === undefined) { + if (level.programDateTime === undefined) level.programDateTime = new Date(new Date(Date.parse(result[5])) - 1000 * totalduration); - } } else { result = result[0].match(LEVEL_PLAYLIST_REGEX_SLOW); for (i = 1; i < result.length; i++) { - if (result[i] !== undefined) { + if (result[i] !== undefined) break; - } } // avoid sliced strings https://github.com/video-dev/hls.js/issues/939 - const value1 = (' ' + result[i+1]).slice(1); - const value2 = (' ' + result[i+2]).slice(1); + const value1 = (' ' + result[i + 1]).slice(1); + const value2 = (' ' + result[i + 2]).slice(1); switch (result[i]) { - case '#': - frag.tagList.push(value2 ? [ value1,value2 ] : [ value1 ]); - break; - case 'PLAYLIST-TYPE': - level.type = value1.toUpperCase(); - break; - case 'MEDIA-SEQUENCE': - currentSN = level.startSN = parseInt(value1); - break; - case 'TARGETDURATION': - level.targetduration = parseFloat(value1); - break; - case 'VERSION': - level.version = parseInt(value1); - break; - case 'EXTM3U': - break; - case 'ENDLIST': - level.live = false; - break; - case 'DIS': - cc++; - frag.tagList.push(['DIS']); - break; - case 'DISCONTINUITY-SEQ': - cc = parseInt(value1); - break; - case 'KEY': - // https://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.4.4 - var decryptparams = value1; - var keyAttrs = new AttrList(decryptparams); - var decryptmethod = keyAttrs.enumeratedString('METHOD'), - decrypturi = keyAttrs.URI, - decryptiv = keyAttrs.hexadecimalInteger('IV'); - if (decryptmethod) { - levelkey = new LevelKey(); - if ((decrypturi) && (['AES-128', 'SAMPLE-AES', 'SAMPLE-AES-CENC'].indexOf(decryptmethod) >= 0)) { - - levelkey.method = decryptmethod; - // URI to get the key - levelkey.baseuri = baseurl; - levelkey.reluri = decrypturi; - levelkey.key = null; - // Initialization Vector (IV) - levelkey.iv = decryptiv; - } - } - break; - case 'START': - let startParams = value1; - let startAttrs = new AttrList(startParams); - let startTimeOffset = startAttrs.decimalFloatingPoint('TIME-OFFSET'); - //TIME-OFFSET can be 0 - if ( !isNaN(startTimeOffset) ) { - level.startTimeOffset = startTimeOffset; + case '#': + frag.tagList.push(value2 ? [ value1, value2 ] : [ value1 ]); + break; + case 'PLAYLIST-TYPE': + level.type = value1.toUpperCase(); + break; + case 'MEDIA-SEQUENCE': + currentSN = level.startSN = parseInt(value1); + break; + case 'TARGETDURATION': + level.targetduration = parseFloat(value1); + break; + case 'VERSION': + level.version = parseInt(value1); + break; + case 'EXTM3U': + break; + case 'ENDLIST': + level.live = false; + break; + case 'DIS': + cc++; + frag.tagList.push(['DIS']); + break; + case 'DISCONTINUITY-SEQ': + cc = parseInt(value1); + break; + case 'KEY': + // https://tools.ietf.org/html/draft-pantos-http-live-streaming-08#section-3.4.4 + var decryptparams = value1; + var keyAttrs = new AttrList(decryptparams); + var decryptmethod = keyAttrs.enumeratedString('METHOD'), + decrypturi = keyAttrs.URI, + decryptiv = keyAttrs.hexadecimalInteger('IV'); + if (decryptmethod) { + levelkey = new LevelKey(); + if ((decrypturi) && (['AES-128', 'SAMPLE-AES', 'SAMPLE-AES-CENC'].indexOf(decryptmethod) >= 0)) { + levelkey.method = decryptmethod; + // URI to get the key + levelkey.baseuri = baseurl; + levelkey.reluri = decrypturi; + levelkey.key = null; + // Initialization Vector (IV) + levelkey.iv = decryptiv; } - break; - case 'MAP': - let mapAttrs = new AttrList(value1); - frag.relurl = mapAttrs.URI; - frag.rawByteRange = mapAttrs.BYTERANGE; - frag.baseurl = baseurl; - frag.level = id; - frag.type = type; - frag.sn = 'initSegment'; - level.initSegment = frag; - frag = new Fragment(); - break; - default: - logger.warn(`line parsed but not handled: ${result}`); - break; + } + break; + case 'START': + let startParams = value1; + let startAttrs = new AttrList(startParams); + let startTimeOffset = startAttrs.decimalFloatingPoint('TIME-OFFSET'); + // TIME-OFFSET can be 0 + if (!isNaN(startTimeOffset)) + level.startTimeOffset = startTimeOffset; + + break; + case 'MAP': + let mapAttrs = new AttrList(value1); + frag.relurl = mapAttrs.URI; + frag.rawByteRange = mapAttrs.BYTERANGE; + frag.baseurl = baseurl; + frag.level = id; + frag.type = type; + frag.sn = 'initSegment'; + level.initSegment = frag; + frag = new Fragment(); + break; + default: + logger.warn(`line parsed but not handled: ${result}`); + break; } } } frag = prevFrag; - //logger.log('found ' + level.fragments.length + ' fragments'); - if(frag && !frag.relurl) { + // logger.log('found ' + level.fragments.length + ' fragments'); + if (frag && !frag.relurl) { level.fragments.pop(); - totalduration-=frag.duration; + totalduration -= frag.duration; } level.totalduration = totalduration; level.averagetargetduration = totalduration / level.fragments.length; @@ -314,7 +306,7 @@ export default class M3U8Parser { // but this is to be able to handle SIDX. // FIXME: replace string test by a regex that matches // also `m4s` `m4a` `m4v` and other popular extensions - if(level.fragments.every((frag) => frag.relurl.endsWith('.mp4'))) { + if (level.fragments.every((frag) => frag.relurl.endsWith('.mp4'))) { logger.warn('MP4 fragments found but no init segment (probably no MAP, incomplete M3U8), trying to fetch SIDX'); frag = new Fragment(); diff --git a/src/loader/playlist-loader.js b/src/loader/playlist-loader.js index 26bd16ef525..4c35c4e404a 100644 --- a/src/loader/playlist-loader.js +++ b/src/loader/playlist-loader.js @@ -11,9 +11,9 @@ import Event from '../events'; import EventHandler from '../event-handler'; -import {ErrorTypes, ErrorDetails} from '../errors'; +import { ErrorTypes, ErrorDetails } from '../errors'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; import MP4Demuxer from '../demux/mp4demuxer'; import M3U8Parser from './m3u8-parser'; @@ -43,12 +43,11 @@ const LevelType = { * @constructor */ class PlaylistLoader extends EventHandler { - /** * @constructs * @param {Hls} hls */ - constructor(hls) { + constructor (hls) { super(hls, Event.MANIFEST_LOADING, Event.LEVEL_LOADING, @@ -58,11 +57,11 @@ class PlaylistLoader extends EventHandler { this.loaders = {}; } - static get ContextType() { + static get ContextType () { return ContextType; } - static get LevelType() { + static get LevelType () { return LevelType; } @@ -70,7 +69,7 @@ class PlaylistLoader extends EventHandler { * @param {ContextType} type * @returns {boolean} */ - static canHaveQualityLevels(type) { + static canHaveQualityLevels (type) { return (type !== ContextType.AUDIO_TRACK && type !== ContextType.SUBTITLE_TRACK); } @@ -80,10 +79,10 @@ class PlaylistLoader extends EventHandler { * @param {{type: ContextType}} context * @returns {LevelType} */ - static mapContextToLevelType(context) { - const {type} = context; + static mapContextToLevelType (context) { + const { type } = context; - switch(type) { + switch (type) { case ContextType.AUDIO_TRACK: return LevelType.AUDIO; case ContextType.SUBTITLE_TRACK: @@ -93,7 +92,7 @@ class PlaylistLoader extends EventHandler { } } - static getResponseUrl(response, context) { + static getResponseUrl (response, context) { let url = response.url; // responseURL not supported on some browsers (it is used to detect URL redirection) // data-uri mode also not supported (but no need to detect redirection) @@ -110,7 +109,7 @@ class PlaylistLoader extends EventHandler { * @param {object} context * @returns {XHRLoader} or other compatible configured overload */ - createInternalLoader(context) { + createInternalLoader (context) { const config = this.hls.config; const PLoader = config.pLoader; const Loader = config.loader; @@ -124,52 +123,51 @@ class PlaylistLoader extends EventHandler { return loader; } - getInternalLoader(context) { + getInternalLoader (context) { return this.loaders[context.type]; } - resetInternalLoader(contextType) { - if (this.loaders[contextType]) { + resetInternalLoader (contextType) { + if (this.loaders[contextType]) delete this.loaders[contextType]; - } } /** * Call `destroy` on all internal loader instances mapped (one per context type) */ - destroyInternalLoaders() { + destroyInternalLoaders () { for (let contextType in this.loaders) { let loader = this.loaders[contextType]; - if (loader) { + if (loader) loader.destroy(); - } + this.resetInternalLoader(contextType); } } - destroy() { + destroy () { this.destroyInternalLoaders(); super.destroy(); } - onManifestLoading(data) { - this.load(data.url, {type: ContextType.MANIFEST}); + onManifestLoading (data) { + this.load(data.url, { type: ContextType.MANIFEST }); } - onLevelLoading(data) { - this.load(data.url, {type: ContextType.LEVEL, level: data.level, id: data.id}); + onLevelLoading (data) { + this.load(data.url, { type: ContextType.LEVEL, level: data.level, id: data.id }); } - onAudioTrackLoading(data) { - this.load(data.url, {type: ContextType.AUDIO_TRACK, id: data.id}); + onAudioTrackLoading (data) { + this.load(data.url, { type: ContextType.AUDIO_TRACK, id: data.id }); } - onSubtitleTrackLoading(data) { - this.load(data.url, {type: ContextType.SUBTITLE_TRACK, id: data.id}); + onSubtitleTrackLoading (data) { + this.load(data.url, { type: ContextType.SUBTITLE_TRACK, id: data.id }); } - load(url, context) { + load (url, context) { const config = this.hls.config; // Check if a loader for this context already exists @@ -177,7 +175,7 @@ class PlaylistLoader extends EventHandler { if (loader) { const loaderContext = loader.context; if (loaderContext && loaderContext.url === url) { // same URL can't overlap - logger.trace(`playlist request ongoing`); + logger.trace('playlist request ongoing'); return false; } else { logger.warn(`aborting previous loader for type: ${context.type}`); @@ -185,13 +183,13 @@ class PlaylistLoader extends EventHandler { } } let maxRetry, - timeout, - retryDelay, - maxRetryDelay; + timeout, + retryDelay, + maxRetryDelay; // apply different configs for retries depending on // context (manifest, level, audio/subs playlist) - switch(context.type) { + switch (context.type) { case ContextType.MANIFEST: maxRetry = config.manifestLoadingMaxRetry; timeout = config.manifestLoadingTimeOut; @@ -238,8 +236,7 @@ class PlaylistLoader extends EventHandler { return true; } - loadsuccess(response, stats, context, networkDetails=null) { - + loadsuccess (response, stats, context, networkDetails = null) { if (context.isSidxRequest) { this._handleSidxRequest(response, context); this._handlePlaylistLoaded(response, stats, context, networkDetails); @@ -251,7 +248,7 @@ class PlaylistLoader extends EventHandler { const string = response.data; stats.tload = performance.now(); - //stats.mtime = new Date(target.getResponseHeader('Last-Modified')); + // stats.mtime = new Date(target.getResponseHeader('Last-Modified')); // Validate if it is an M3U8 at all if (string.indexOf('#EXTM3U') !== 0) { @@ -260,24 +257,21 @@ class PlaylistLoader extends EventHandler { } // Check if chunk-list or master - if (string.indexOf('#EXTINF:') > 0) { + if (string.indexOf('#EXTINF:') > 0) this._handleTrackOrLevelPlaylist(response, stats, context, networkDetails); - } else { + else this._handleMasterPlaylist(response, stats, context, networkDetails); - } - } - loaderror(response, context, networkDetails = null) { + loaderror (response, context, networkDetails = null) { this._handleNetworkError(context, networkDetails); } - loadtimeout(stats, context, networkDetails = null) { + loadtimeout (stats, context, networkDetails = null) { this._handleNetworkError(context, networkDetails); } - _handleMasterPlaylist(response, stats, context, networkDetails) { - + _handleMasterPlaylist (response, stats, context, networkDetails) { const hls = this.hls; const string = response.data; @@ -303,9 +297,8 @@ class PlaylistLoader extends EventHandler { // check if we have found an audio track embedded in main playlist (audio track without URI attribute) let embeddedAudioFound = false; audioTracks.forEach(audioTrack => { - if(!audioTrack.url) { + if (!audioTrack.url) embeddedAudioFound = true; - } }); // if no embedded audio track defined, but audio codec signaled in quality level, @@ -315,8 +308,8 @@ class PlaylistLoader extends EventHandler { if (embeddedAudioFound === false && levels[0].audioCodec && !levels[0].attrs.AUDIO) { logger.log('audio codec signaled in quality level, but no embedded audio track signaled, create one'); audioTracks.unshift({ - type : 'main', - name : 'main' + type: 'main', + name: 'main' }); } } @@ -329,14 +322,12 @@ class PlaylistLoader extends EventHandler { stats, networkDetails }); - } - _handleTrackOrLevelPlaylist(response, stats, context, networkDetails) { - + _handleTrackOrLevelPlaylist (response, stats, context, networkDetails) { const hls = this.hls; - const {id, level, type} = context; + const { id, level, type } = context; const url = PlaylistLoader.getResponseUrl(response, context); @@ -353,7 +344,6 @@ class PlaylistLoader extends EventHandler { // We fire the manifest-loaded event anyway with the parsed level-details // by creating a single-level structure for it. if (type === ContextType.MANIFEST) { - const singleLevel = { url, details: levelDetails @@ -361,12 +351,11 @@ class PlaylistLoader extends EventHandler { hls.trigger(Event.MANIFEST_LOADED, { levels: [singleLevel], - audioTracks : [], + audioTracks: [], url, stats, networkDetails }); - } // save parsing time @@ -388,33 +377,28 @@ class PlaylistLoader extends EventHandler { responseType: 'arraybuffer' }); return; - } // extend the context with the new levelDetails property context.levelDetails = levelDetails; this._handlePlaylistLoaded(response, stats, context, networkDetails); - } - _handleSidxRequest(response, context) { + _handleSidxRequest (response, context) { const sidxInfo = MP4Demuxer.parseSegmentIndex(new Uint8Array(response.data)); sidxInfo.references.forEach((segmentRef, index) => { - const segRefInfo = segmentRef.info; const frag = context.levelDetails.fragments[index]; - if(frag.byteRange.length === 0) { + if (frag.byteRange.length === 0) frag.rawByteRange = String(1 + segRefInfo.end - segRefInfo.start) + '@' + String(segRefInfo.start); - } - }); context.levelDetails.initSegment.rawByteRange = String(sidxInfo.moovEndOffset) + '@0'; } - _handleManifestParsingError(response, context, reason, networkDetails) { + _handleManifestParsingError (response, context, reason, networkDetails) { this.hls.trigger(Event.ERROR, { type: ErrorTypes.NETWORK_ERROR, details: ErrorDetails.MANIFEST_PARSING_ERROR, @@ -425,14 +409,13 @@ class PlaylistLoader extends EventHandler { }); } - _handleNetworkError(context, networkDetails) { - + _handleNetworkError (context, networkDetails) { let details; let fatal; const loader = this.getInternalLoader(context); - switch(context.type) { + switch (context.type) { case ContextType.MANIFEST: details = ErrorDetails.MANIFEST_LOAD_TIMEOUT; fatal = true; @@ -466,9 +449,8 @@ class PlaylistLoader extends EventHandler { }); } - _handlePlaylistLoaded(response, stats, context, networkDetails) { - - const {type, level, id, levelDetails} = context; + _handlePlaylistLoaded (response, stats, context, networkDetails) { + const { type, level, id, levelDetails } = context; if (!levelDetails.targetduration) { this._handleManifestParsingError(response, context, 'invalid target duration', networkDetails); @@ -477,7 +459,6 @@ class PlaylistLoader extends EventHandler { const canHaveLevels = PlaylistLoader.canHaveQualityLevels(context.type); if (canHaveLevels) { - this.hls.trigger(Event.LEVEL_LOADED, { details: levelDetails, level: level || 0, @@ -485,10 +466,8 @@ class PlaylistLoader extends EventHandler { stats, networkDetails }); - } else { - - switch(type) { + switch (type) { case ContextType.AUDIO_TRACK: this.hls.trigger(Event.AUDIO_TRACK_LOADED, { details: levelDetails, @@ -506,12 +485,8 @@ class PlaylistLoader extends EventHandler { }); break; } - } - } - } export default PlaylistLoader; - diff --git a/src/remux/dummy-remuxer.js b/src/remux/dummy-remuxer.js index 0b0b70e70d1..c9d512968dd 100644 --- a/src/remux/dummy-remuxer.js +++ b/src/remux/dummy-remuxer.js @@ -3,74 +3,71 @@ */ class DummyRemuxer { - constructor(observer) { + constructor (observer) { this.observer = observer; } - destroy() { + destroy () { } - resetInitSegment() { + resetInitSegment () { } - - resetTimeStamp() { + resetTimeStamp () { } - remux(audioTrack,videoTrack,id3Track,textTrack,timeOffset) { - this._remuxAACSamples(audioTrack,timeOffset); - this._remuxAVCSamples(videoTrack,timeOffset); - this._remuxID3Samples(id3Track,timeOffset); - this._remuxTextSamples(textTrack,timeOffset); + remux (audioTrack, videoTrack, id3Track, textTrack, timeOffset) { + this._remuxAACSamples(audioTrack, timeOffset); + this._remuxAVCSamples(videoTrack, timeOffset); + this._remuxID3Samples(id3Track, timeOffset); + this._remuxTextSamples(textTrack, timeOffset); } - _remuxAVCSamples(track, timeOffset) { - var avcSample, unit; + _remuxAVCSamples (track, timeOffset) { + let avcSample, unit; // loop through track.samples while (track.samples.length) { avcSample = track.samples.shift(); // loop through AVC sample NALUs - while (avcSample.units.length) { + while (avcSample.units.length) unit = avcSample.units.shift(); - } } - //please lint + // please lint timeOffset = timeOffset; } - _remuxAACSamples(track,timeOffset) { - var aacSample,unit; + _remuxAACSamples (track, timeOffset) { + let aacSample, unit; // loop through track.samples while (track.samples.length) { aacSample = track.samples.shift(); unit = aacSample.unit; } - //please lint + // please lint timeOffset = timeOffset; } - _remuxID3Samples(track,timeOffset) { - var id3Sample,unit; + _remuxID3Samples (track, timeOffset) { + let id3Sample, unit; // loop through track.samples while (track.samples.length) { id3Sample = track.samples.shift(); unit = id3Sample.unit; } - //please lint + // please lint timeOffset = timeOffset; } - _remuxTextSamples(track,timeOffset) { - var textSample,bytes; + _remuxTextSamples (track, timeOffset) { + let textSample, bytes; // loop through track.samples while (track.samples.length) { textSample = track.samples.shift(); bytes = textSample.bytes; } - //please lint + // please lint timeOffset = timeOffset; } } export default DummyRemuxer; - diff --git a/src/remux/mp4-generator.js b/src/remux/mp4-generator.js index 8c1137d74f5..b0839b4ebd0 100644 --- a/src/remux/mp4-generator.js +++ b/src/remux/mp4-generator.js @@ -2,12 +2,12 @@ * Generate MP4 Box */ -//import Hex from '../utils/hex'; +// import Hex from '../utils/hex'; const UINT32_MAX = Math.pow(2, 32) - 1; class MP4 { - static init() { + static init () { MP4.types = { avc1: [], // codingname avcC: [], @@ -47,7 +47,7 @@ class MP4 { smhd: [] }; - var i; + let i; for (i in MP4.types) { if (MP4.types.hasOwnProperty(i)) { MP4.types[i] = [ @@ -59,7 +59,7 @@ class MP4 { } } - var videoHdlr = new Uint8Array([ + let videoHdlr = new Uint8Array([ 0x00, // version 0 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x00, // pre_defined @@ -72,7 +72,7 @@ class MP4 { 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler' ]); - var audioHdlr = new Uint8Array([ + let audioHdlr = new Uint8Array([ 0x00, // version 0 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x00, // pre_defined @@ -90,7 +90,7 @@ class MP4 { 'audio': audioHdlr }; - var dref = new Uint8Array([ + let dref = new Uint8Array([ 0x00, // version 0 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x01, // entry_count @@ -100,7 +100,7 @@ class MP4 { 0x00, 0x00, 0x01 // entry_flags ]); - var stco = new Uint8Array([ + let stco = new Uint8Array([ 0x00, // version 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x00 // entry_count @@ -112,7 +112,7 @@ class MP4 { 0x00, // version 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x00, // sample_size - 0x00, 0x00, 0x00, 0x00, // sample_count + 0x00, 0x00, 0x00, 0x00 // sample_count ]); MP4.VMHD = new Uint8Array([ 0x00, // version @@ -134,30 +134,30 @@ class MP4 { 0x00, 0x00, 0x00, // flags 0x00, 0x00, 0x00, 0x01]);// entry_count - var majorBrand = new Uint8Array([105,115,111,109]); // isom - var avc1Brand = new Uint8Array([97,118,99,49]); // avc1 - var minorVersion = new Uint8Array([0, 0, 0, 1]); + let majorBrand = new Uint8Array([105, 115, 111, 109]); // isom + let avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1 + let minorVersion = new Uint8Array([0, 0, 0, 1]); MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand); MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref)); } - static box(type) { - var - payload = Array.prototype.slice.call(arguments, 1), - size = 8, - i = payload.length, - len = i, - result; + static box (type) { + let + payload = Array.prototype.slice.call(arguments, 1), + size = 8, + i = payload.length, + len = i, + result; // calculate the total size we need to allocate - while (i--) { + while (i--) size += payload[i].byteLength; - } + result = new Uint8Array(size); result[0] = (size >> 24) & 0xff; result[1] = (size >> 16) & 0xff; result[2] = (size >> 8) & 0xff; - result[3] = size & 0xff; + result[3] = size & 0xff; result.set(type, 4); // copy the payload into the result for (i = 0, size = 8; i < len; i++) { @@ -168,15 +168,15 @@ class MP4 { return result; } - static hdlr(type) { + static hdlr (type) { return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]); } - static mdat(data) { + static mdat (data) { return MP4.box(MP4.types.mdat, data); } - static mdhd(timescale, duration) { + static mdhd (timescale, duration) { duration *= timescale; const upperWordDuration = Math.floor(duration / (UINT32_MAX + 1)); const lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1)); @@ -187,78 +187,76 @@ class MP4 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // modification_time (timescale >> 24) & 0xFF, (timescale >> 16) & 0xFF, - (timescale >> 8) & 0xFF, + (timescale >> 8) & 0xFF, timescale & 0xFF, // timescale (upperWordDuration >> 24), (upperWordDuration >> 16) & 0xFF, - (upperWordDuration >> 8) & 0xFF, + (upperWordDuration >> 8) & 0xFF, upperWordDuration & 0xFF, (lowerWordDuration >> 24), (lowerWordDuration >> 16) & 0xFF, - (lowerWordDuration >> 8) & 0xFF, + (lowerWordDuration >> 8) & 0xFF, lowerWordDuration & 0xFF, 0x55, 0xc4, // 'und' language (undetermined) 0x00, 0x00 ])); } - static mdia(track) { + static mdia (track) { return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track)); } - static mfhd(sequenceNumber) { + static mfhd (sequenceNumber) { return MP4.box(MP4.types.mfhd, new Uint8Array([ 0x00, 0x00, 0x00, 0x00, // flags (sequenceNumber >> 24), (sequenceNumber >> 16) & 0xFF, - (sequenceNumber >> 8) & 0xFF, - sequenceNumber & 0xFF, // sequence_number + (sequenceNumber >> 8) & 0xFF, + sequenceNumber & 0xFF // sequence_number ])); } - static minf(track) { - if (track.type === 'audio') { + static minf (track) { + if (track.type === 'audio') return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track)); - } else { + else return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track)); - } } - static moof(sn, baseMediaDecodeTime, track) { - return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track,baseMediaDecodeTime)); + static moof (sn, baseMediaDecodeTime, track) { + return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime)); } -/** + /** * @param tracks... (optional) {array} the tracks associated with this movie */ - static moov(tracks) { - var + static moov (tracks) { + let i = tracks.length, boxes = []; - while (i--) { + while (i--) boxes[i] = MP4.trak(tracks[i]); - } return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(tracks[0].timescale, tracks[0].duration)].concat(boxes).concat(MP4.mvex(tracks))); } - static mvex(tracks) { - var + static mvex (tracks) { + let i = tracks.length, boxes = []; - while (i--) { + while (i--) boxes[i] = MP4.trex(tracks[i]); - } + return MP4.box.apply(null, [MP4.types.mvex].concat(boxes)); } - static mvhd(timescale,duration) { - duration*=timescale; + static mvhd (timescale, duration) { + duration *= timescale; const upperWordDuration = Math.floor(duration / (UINT32_MAX + 1)); const lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1)); - var + let bytes = new Uint8Array([ 0x01, // version 1 0x00, 0x00, 0x00, // flags @@ -266,15 +264,15 @@ class MP4 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, // modification_time (timescale >> 24) & 0xFF, (timescale >> 16) & 0xFF, - (timescale >> 8) & 0xFF, + (timescale >> 8) & 0xFF, timescale & 0xFF, // timescale (upperWordDuration >> 24), (upperWordDuration >> 16) & 0xFF, - (upperWordDuration >> 8) & 0xFF, + (upperWordDuration >> 8) & 0xFF, upperWordDuration & 0xFF, (lowerWordDuration >> 24), (lowerWordDuration >> 16) & 0xFF, - (lowerWordDuration >> 8) & 0xFF, + (lowerWordDuration >> 8) & 0xFF, lowerWordDuration & 0xFF, 0x00, 0x01, 0x00, 0x00, // 1.0 rate 0x01, 0x00, // 1.0 volume @@ -301,8 +299,8 @@ class MP4 { return MP4.box(MP4.types.mvhd, bytes); } - static sdtp(track) { - var + static sdtp (track) { + let samples = track.samples || [], bytes = new Uint8Array(4 + samples.length), flags, @@ -319,12 +317,12 @@ class MP4 { return MP4.box(MP4.types.sdtp, bytes); } - static stbl(track) { + static stbl (track) { return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO)); } - static avc1(track) { - var sps = [], pps = [], i, data, len; + static avc1 (track) { + let sps = [], pps = [], i, data, len; // assemble the SPSs for (i = 0; i < track.sps.length; i++) { @@ -344,92 +342,92 @@ class MP4 { pps = pps.concat(Array.prototype.slice.call(data)); } - var avcc = MP4.box(MP4.types.avcC, new Uint8Array([ - 0x01, // version - sps[3], // profile - sps[4], // profile compat - sps[5], // level - 0xfc | 3, // lengthSizeMinusOne, hard-coded to 4 bytes - 0xE0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets - ].concat(sps).concat([ - track.pps.length // numOfPictureParameterSets - ]).concat(pps))), // "PPS" - width = track.width, - height = track.height, - hSpacing = track.pixelRatio[0], - vSpacing = track.pixelRatio[1]; + let avcc = MP4.box(MP4.types.avcC, new Uint8Array([ + 0x01, // version + sps[3], // profile + sps[4], // profile compat + sps[5], // level + 0xfc | 3, // lengthSizeMinusOne, hard-coded to 4 bytes + 0xE0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets + ].concat(sps).concat([ + track.pps.length // numOfPictureParameterSets + ]).concat(pps))), // "PPS" + width = track.width, + height = track.height, + hSpacing = track.pixelRatio[0], + vSpacing = track.pixelRatio[1]; return MP4.box(MP4.types.avc1, new Uint8Array([ - 0x00, 0x00, 0x00, // reserved - 0x00, 0x00, 0x00, // reserved - 0x00, 0x01, // data_reference_index - 0x00, 0x00, // pre_defined - 0x00, 0x00, // reserved - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, // pre_defined - (width >> 8) & 0xFF, - width & 0xff, // width - (height >> 8) & 0xFF, - height & 0xff, // height - 0x00, 0x48, 0x00, 0x00, // horizresolution - 0x00, 0x48, 0x00, 0x00, // vertresolution - 0x00, 0x00, 0x00, 0x00, // reserved - 0x00, 0x01, // frame_count - 0x12, - 0x64, 0x61, 0x69, 0x6C, //dailymotion/hls.js - 0x79, 0x6D, 0x6F, 0x74, - 0x69, 0x6F, 0x6E, 0x2F, - 0x68, 0x6C, 0x73, 0x2E, - 0x6A, 0x73, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, // compressorname - 0x00, 0x18, // depth = 24 - 0x11, 0x11]), // pre_defined = -1 - avcc, - MP4.box(MP4.types.btrt, new Uint8Array([ - 0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB - 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate - 0x00, 0x2d, 0xc6, 0xc0])), // avgBitrate - MP4.box(MP4.types.pasp, new Uint8Array([ - (hSpacing >> 24), // hSpacing - (hSpacing >> 16) & 0xFF, - (hSpacing >> 8) & 0xFF, - hSpacing & 0xFF, - (vSpacing >> 24), // vSpacing - (vSpacing >> 16) & 0xFF, - (vSpacing >> 8) & 0xFF, - vSpacing & 0xFF])) - ); + 0x00, 0x00, 0x00, // reserved + 0x00, 0x00, 0x00, // reserved + 0x00, 0x01, // data_reference_index + 0x00, 0x00, // pre_defined + 0x00, 0x00, // reserved + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, // pre_defined + (width >> 8) & 0xFF, + width & 0xff, // width + (height >> 8) & 0xFF, + height & 0xff, // height + 0x00, 0x48, 0x00, 0x00, // horizresolution + 0x00, 0x48, 0x00, 0x00, // vertresolution + 0x00, 0x00, 0x00, 0x00, // reserved + 0x00, 0x01, // frame_count + 0x12, + 0x64, 0x61, 0x69, 0x6C, // dailymotion/hls.js + 0x79, 0x6D, 0x6F, 0x74, + 0x69, 0x6F, 0x6E, 0x2F, + 0x68, 0x6C, 0x73, 0x2E, + 0x6A, 0x73, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, // compressorname + 0x00, 0x18, // depth = 24 + 0x11, 0x11]), // pre_defined = -1 + avcc, + MP4.box(MP4.types.btrt, new Uint8Array([ + 0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB + 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate + 0x00, 0x2d, 0xc6, 0xc0])), // avgBitrate + MP4.box(MP4.types.pasp, new Uint8Array([ + (hSpacing >> 24), // hSpacing + (hSpacing >> 16) & 0xFF, + (hSpacing >> 8) & 0xFF, + hSpacing & 0xFF, + (vSpacing >> 24), // vSpacing + (vSpacing >> 16) & 0xFF, + (vSpacing >> 8) & 0xFF, + vSpacing & 0xFF])) + ); } - static esds(track) { - var configlen = track.config.length; + static esds (track) { + let configlen = track.config.length; return new Uint8Array([ 0x00, // version 0 0x00, 0x00, 0x00, // flags 0x03, // descriptor_type - 0x17+configlen, // length - 0x00, 0x01, //es_id + 0x17 + configlen, // length + 0x00, 0x01, // es_id 0x00, // stream_priority 0x04, // descriptor_type - 0x0f+configlen, // length - 0x40, //codec : mpeg4_audio + 0x0f + configlen, // length + 0x40, // codec : mpeg4_audio 0x15, // stream_type 0x00, 0x00, 0x00, // buffer_size 0x00, 0x00, 0x00, 0x00, // maxBitrate 0x00, 0x00, 0x00, 0x00, // avgBitrate 0x05 // descriptor_type - ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor + ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor } - static mp4a(track) { - var samplerate = track.samplerate; - return MP4.box(MP4.types.mp4a, new Uint8Array([ + static mp4a (track) { + let samplerate = track.samplerate; + return MP4.box(MP4.types.mp4a, new Uint8Array([ 0x00, 0x00, 0x00, // reserved 0x00, 0x00, 0x00, // reserved 0x00, 0x01, // data_reference_index @@ -441,12 +439,12 @@ class MP4 { (samplerate >> 8) & 0xFF, samplerate & 0xff, // 0x00, 0x00]), - MP4.box(MP4.types.esds, MP4.esds(track))); + MP4.box(MP4.types.esds, MP4.esds(track))); } - static mp3(track) { - var samplerate = track.samplerate; - return MP4.box(MP4.types['.mp3'], new Uint8Array([ + static mp3 (track) { + let samplerate = track.samplerate; + return MP4.box(MP4.types['.mp3'], new Uint8Array([ 0x00, 0x00, 0x00, // reserved 0x00, 0x00, 0x00, // reserved 0x00, 0x01, // data_reference_index @@ -460,24 +458,24 @@ class MP4 { 0x00, 0x00])); } - static stsd(track) { + static stsd (track) { if (track.type === 'audio') { - if (!track.isAAC && track.codec === 'mp3') { + if (!track.isAAC && track.codec === 'mp3') return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp3(track)); - } + return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track)); } else { return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track)); } } - static tkhd(track) { - var id = track.id, - duration = track.duration*track.timescale, - width = track.width, - height = track.height, - upperWordDuration = Math.floor(duration / (UINT32_MAX + 1)), - lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1)); + static tkhd (track) { + let id = track.id, + duration = track.duration * track.timescale, + width = track.width, + height = track.height, + upperWordDuration = Math.floor(duration / (UINT32_MAX + 1)), + lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1)); return MP4.box(MP4.types.tkhd, new Uint8Array([ 0x01, // version 1 0x00, 0x00, 0x07, // flags @@ -490,11 +488,11 @@ class MP4 { 0x00, 0x00, 0x00, 0x00, // reserved (upperWordDuration >> 24), (upperWordDuration >> 16) & 0xFF, - (upperWordDuration >> 8) & 0xFF, + (upperWordDuration >> 8) & 0xFF, upperWordDuration & 0xFF, (lowerWordDuration >> 24), (lowerWordDuration >> 16) & 0xFF, - (lowerWordDuration >> 8) & 0xFF, + (lowerWordDuration >> 8) & 0xFF, lowerWordDuration & 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved @@ -520,41 +518,41 @@ class MP4 { ])); } - static traf(track,baseMediaDecodeTime) { - var sampleDependencyTable = MP4.sdtp(track), - id = track.id, - upperWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1)), - lowerWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1)); + static traf (track, baseMediaDecodeTime) { + let sampleDependencyTable = MP4.sdtp(track), + id = track.id, + upperWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1)), + lowerWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1)); return MP4.box(MP4.types.traf, - MP4.box(MP4.types.tfhd, new Uint8Array([ - 0x00, // version 0 - 0x00, 0x00, 0x00, // flags - (id >> 24), - (id >> 16) & 0XFF, - (id >> 8) & 0XFF, - (id & 0xFF) // track_ID - ])), - MP4.box(MP4.types.tfdt, new Uint8Array([ - 0x01, // version 1 - 0x00, 0x00, 0x00, // flags - (upperWordBaseMediaDecodeTime >>24), - (upperWordBaseMediaDecodeTime >> 16) & 0XFF, - (upperWordBaseMediaDecodeTime >> 8) & 0XFF, - (upperWordBaseMediaDecodeTime & 0xFF), - (lowerWordBaseMediaDecodeTime >>24), - (lowerWordBaseMediaDecodeTime >> 16) & 0XFF, - (lowerWordBaseMediaDecodeTime >> 8) & 0XFF, - (lowerWordBaseMediaDecodeTime & 0xFF) - ])), - MP4.trun(track, - sampleDependencyTable.length + + MP4.box(MP4.types.tfhd, new Uint8Array([ + 0x00, // version 0 + 0x00, 0x00, 0x00, // flags + (id >> 24), + (id >> 16) & 0XFF, + (id >> 8) & 0XFF, + (id & 0xFF) // track_ID + ])), + MP4.box(MP4.types.tfdt, new Uint8Array([ + 0x01, // version 1 + 0x00, 0x00, 0x00, // flags + (upperWordBaseMediaDecodeTime >> 24), + (upperWordBaseMediaDecodeTime >> 16) & 0XFF, + (upperWordBaseMediaDecodeTime >> 8) & 0XFF, + (upperWordBaseMediaDecodeTime & 0xFF), + (lowerWordBaseMediaDecodeTime >> 24), + (lowerWordBaseMediaDecodeTime >> 16) & 0XFF, + (lowerWordBaseMediaDecodeTime >> 8) & 0XFF, + (lowerWordBaseMediaDecodeTime & 0xFF) + ])), + MP4.trun(track, + sampleDependencyTable.length + 16 + // tfhd 20 + // tfdt - 8 + // traf header + 8 + // traf header 16 + // mfhd - 8 + // moof header - 8), // mdat header - sampleDependencyTable); + 8 + // moof header + 8), // mdat header + sampleDependencyTable); } /** @@ -562,20 +560,20 @@ class MP4 { * @param track {object} a track definition * @return {Uint8Array} the track box */ - static trak(track) { + static trak (track) { track.duration = track.duration || 0xffffffff; return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track)); } - static trex(track) { - var id = track.id; + static trex (track) { + let id = track.id; return MP4.box(MP4.types.trex, new Uint8Array([ 0x00, // version 0 0x00, 0x00, 0x00, // flags - (id >> 24), - (id >> 16) & 0XFF, - (id >> 8) & 0XFF, - (id & 0xFF), // track_ID + (id >> 24), + (id >> 16) & 0XFF, + (id >> 8) & 0XFF, + (id & 0xFF), // track_ID 0x00, 0x00, 0x00, 0x01, // default_sample_description_index 0x00, 0x00, 0x00, 0x00, // default_sample_duration 0x00, 0x00, 0x00, 0x00, // default_sample_size @@ -583,12 +581,12 @@ class MP4 { ])); } - static trun(track, offset) { - var samples= track.samples || [], - len = samples.length, - arraylen = 12 + (16 * len), - array = new Uint8Array(arraylen), - i,sample,duration,size,flags,cts; + static trun (track, offset) { + let samples = track.samples || [], + len = samples.length, + arraylen = 12 + (16 * len), + array = new Uint8Array(arraylen), + i, sample, duration, size, flags, cts; offset += 8 + arraylen; array.set([ 0x00, // version 0 @@ -601,7 +599,7 @@ class MP4 { (offset >>> 16) & 0xFF, (offset >>> 8) & 0xFF, offset & 0xFF // data_offset - ],0); + ], 0); for (i = 0; i < len; i++) { sample = samples[i]; duration = sample.duration; @@ -628,16 +626,16 @@ class MP4 { (cts >>> 16) & 0xFF, (cts >>> 8) & 0xFF, cts & 0xFF // sample_composition_time_offset - ],12+16*i); + ], 12 + 16 * i); } return MP4.box(MP4.types.trun, array); } - static initSegment(tracks) { - if (!MP4.types) { + static initSegment (tracks) { + if (!MP4.types) MP4.init(); - } - var movie = MP4.moov(tracks), result; + + let movie = MP4.moov(tracks), result; result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength); result.set(MP4.FTYP); result.set(movie, MP4.FTYP.byteLength); diff --git a/src/remux/mp4-remuxer.js b/src/remux/mp4-remuxer.js index 126cf28b689..713fa257120 100644 --- a/src/remux/mp4-remuxer.js +++ b/src/remux/mp4-remuxer.js @@ -1,19 +1,18 @@ - /** +/** * fMP4 remuxer */ - import AAC from '../helper/aac'; import Event from '../events'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; import MP4 from '../remux/mp4-generator'; -import {ErrorTypes, ErrorDetails} from '../errors'; +import { ErrorTypes, ErrorDetails } from '../errors'; // 10 seconds const MAX_SILENT_FRAME_DURATION = 10 * 1000; class MP4Remuxer { - constructor(observer, config, typeSupported, vendor) { + constructor (observer, config, typeSupported, vendor) { this.observer = observer; this.config = config; this.typeSupported = typeSupported; @@ -22,22 +21,21 @@ class MP4Remuxer { this.ISGenerated = false; } - destroy() { + destroy () { } - resetTimeStamp(defaultTimeStamp) { + resetTimeStamp (defaultTimeStamp) { this._initPTS = this._initDTS = defaultTimeStamp; } - resetInitSegment() { + resetInitSegment () { this.ISGenerated = false; } - remux(audioTrack,videoTrack,id3Track,textTrack,timeOffset, contiguous,accurateTimeOffset) { + remux (audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset) { // generate Init Segment if needed - if (!this.ISGenerated) { - this.generateIS(audioTrack,videoTrack,timeOffset); - } + if (!this.ISGenerated) + this.generateIS(audioTrack, videoTrack, timeOffset); if (this.ISGenerated) { const nbAudioSamples = audioTrack.samples.length; @@ -49,76 +47,75 @@ class MP4Remuxer { // if first audio DTS is not aligned with first video DTS then we need to take that into account // when providing timeOffset to remuxAudio / remuxVideo. if we don't do that, there might be a permanent / small // drift between audio and video streams - let audiovideoDeltaDts = (audioTrack.samples[0].dts - videoTrack.samples[0].dts)/videoTrack.inputTimeScale; - audioTimeOffset += Math.max(0,audiovideoDeltaDts); - videoTimeOffset += Math.max(0,-audiovideoDeltaDts); + let audiovideoDeltaDts = (audioTrack.samples[0].dts - videoTrack.samples[0].dts) / videoTrack.inputTimeScale; + audioTimeOffset += Math.max(0, audiovideoDeltaDts); + videoTimeOffset += Math.max(0, -audiovideoDeltaDts); } // Purposefully remuxing audio before video, so that remuxVideo can use nextAudioPts, which is // calculated in remuxAudio. - //logger.log('nb AAC samples:' + audioTrack.samples.length); + // logger.log('nb AAC samples:' + audioTrack.samples.length); if (nbAudioSamples) { // if initSegment was generated without video samples, regenerate it again if (!audioTrack.timescale) { logger.warn('regenerate InitSegment as audio detected'); - this.generateIS(audioTrack,videoTrack,timeOffset); + this.generateIS(audioTrack, videoTrack, timeOffset); } - let audioData = this.remuxAudio(audioTrack,audioTimeOffset,contiguous,accurateTimeOffset); - //logger.log('nb AVC samples:' + videoTrack.samples.length); + let audioData = this.remuxAudio(audioTrack, audioTimeOffset, contiguous, accurateTimeOffset); + // logger.log('nb AVC samples:' + videoTrack.samples.length); if (nbVideoSamples) { let audioTrackLength; - if (audioData) { + if (audioData) audioTrackLength = audioData.endPTS - audioData.startPTS; - } + // if initSegment was generated without video samples, regenerate it again if (!videoTrack.timescale) { logger.warn('regenerate InitSegment as video detected'); - this.generateIS(audioTrack,videoTrack,timeOffset); + this.generateIS(audioTrack, videoTrack, timeOffset); } - this.remuxVideo(videoTrack,videoTimeOffset,contiguous,audioTrackLength, accurateTimeOffset); + this.remuxVideo(videoTrack, videoTimeOffset, contiguous, audioTrackLength, accurateTimeOffset); } } else { - //logger.log('nb AVC samples:' + videoTrack.samples.length); + // logger.log('nb AVC samples:' + videoTrack.samples.length); if (nbVideoSamples) { - let videoData = this.remuxVideo(videoTrack,videoTimeOffset,contiguous, 0, accurateTimeOffset); - if (videoData && audioTrack.codec) { + let videoData = this.remuxVideo(videoTrack, videoTimeOffset, contiguous, 0, accurateTimeOffset); + if (videoData && audioTrack.codec) this.remuxEmptyAudio(audioTrack, audioTimeOffset, contiguous, videoData); - } } } } - //logger.log('nb ID3 samples:' + audioTrack.samples.length); - if (id3Track.samples.length) { - this.remuxID3(id3Track,timeOffset); - } - //logger.log('nb ID3 samples:' + audioTrack.samples.length); - if (textTrack.samples.length) { - this.remuxText(textTrack,timeOffset); - } - //notify end of parsing + // logger.log('nb ID3 samples:' + audioTrack.samples.length); + if (id3Track.samples.length) + this.remuxID3(id3Track, timeOffset); + + // logger.log('nb ID3 samples:' + audioTrack.samples.length); + if (textTrack.samples.length) + this.remuxText(textTrack, timeOffset); + + // notify end of parsing this.observer.trigger(Event.FRAG_PARSED); } - generateIS(audioTrack,videoTrack,timeOffset) { - var observer = this.observer, - audioSamples = audioTrack.samples, - videoSamples = videoTrack.samples, - typeSupported = this.typeSupported, - container = 'audio/mp4', - tracks = {}, - data = { tracks : tracks }, - computePTSDTS = (this._initPTS === undefined), - initPTS, initDTS; - - if (computePTSDTS) { + generateIS (audioTrack, videoTrack, timeOffset) { + let observer = this.observer, + audioSamples = audioTrack.samples, + videoSamples = videoTrack.samples, + typeSupported = this.typeSupported, + container = 'audio/mp4', + tracks = {}, + data = { tracks: tracks }, + computePTSDTS = (this._initPTS === undefined), + initPTS, initDTS; + + if (computePTSDTS) initPTS = initDTS = Infinity; - } + if (audioTrack.config && audioSamples.length) { // let's use audio sampling rate as MP4 time scale. // rationale is that there is a integer nb of audio frames per audio sample (1024 for AAC) // using audio sampling rate here helps having an integer MP4 frame duration // this avoids potential rounding issue and AV sync issue audioTrack.timescale = audioTrack.samplerate; - logger.log (`audio sampling rate : ${audioTrack.samplerate}`); + logger.log(`audio sampling rate : ${audioTrack.samplerate}`); if (!audioTrack.isAAC) { if (typeSupported.mpeg) { // Chrome and Safari container = 'audio/mpeg'; @@ -128,11 +125,11 @@ class MP4Remuxer { } } tracks.audio = { - container : container, - codec : audioTrack.codec, - initSegment : !audioTrack.isAAC && typeSupported.mpeg ? new Uint8Array() : MP4.initSegment([audioTrack]), - metadata : { - channelCount : audioTrack.channelCount + container: container, + codec: audioTrack.codec, + initSegment: !audioTrack.isAAC && typeSupported.mpeg ? new Uint8Array() : MP4.initSegment([audioTrack]), + metadata: { + channelCount: audioTrack.channelCount } }; if (computePTSDTS) { @@ -147,68 +144,67 @@ class MP4Remuxer { const inputTimeScale = videoTrack.inputTimeScale; videoTrack.timescale = inputTimeScale; tracks.video = { - container : 'video/mp4', - codec : videoTrack.codec, - initSegment : MP4.initSegment([videoTrack]), - metadata : { - width : videoTrack.width, - height : videoTrack.height + container: 'video/mp4', + codec: videoTrack.codec, + initSegment: MP4.initSegment([videoTrack]), + metadata: { + width: videoTrack.width, + height: videoTrack.height } }; if (computePTSDTS) { - initPTS = Math.min(initPTS,videoSamples[0].pts - inputTimeScale * timeOffset); - initDTS = Math.min(initDTS,videoSamples[0].dts - inputTimeScale * timeOffset); - this.observer.trigger(Event.INIT_PTS_FOUND, { initPTS: initPTS}); + initPTS = Math.min(initPTS, videoSamples[0].pts - inputTimeScale * timeOffset); + initDTS = Math.min(initDTS, videoSamples[0].dts - inputTimeScale * timeOffset); + this.observer.trigger(Event.INIT_PTS_FOUND, { initPTS: initPTS }); } } - if(Object.keys(tracks).length) { - observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT,data); + if (Object.keys(tracks).length) { + observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT, data); this.ISGenerated = true; if (computePTSDTS) { this._initPTS = initPTS; this._initDTS = initDTS; } } else { - observer.trigger(Event.ERROR, {type : ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: false, reason: 'no audio/video samples found'}); + observer.trigger(Event.ERROR, { type: ErrorTypes.MEDIA_ERROR, details: ErrorDetails.FRAG_PARSING_ERROR, fatal: false, reason: 'no audio/video samples found' }); } } - remuxVideo(track, timeOffset, contiguous, audioTrackLength, accurateTimeOffset) { - var offset = 8, - timeScale = track.timescale, - mp4SampleDuration, - mdat, moof, - firstPTS, firstDTS, - nextDTS, - lastPTS, lastDTS, - inputSamples = track.samples, - outputSamples = [], - nbSamples = inputSamples.length, - ptsNormalize = this._PTSNormalize, - initDTS = this._initDTS; - - // for (let i = 0; i < track.samples.length; i++) { - // let avcSample = track.samples[i]; - // let units = avcSample.units; - // let unitsString = ''; - // for (let j = 0; j < units.length ; j++) { - // unitsString += units[j].type + ','; - // if (units[j].data.length < 500) { - // unitsString += Hex.hexDump(units[j].data); - // } - // } - // logger.log(avcSample.pts + '/' + avcSample.dts + ',' + unitsString + avcSample.units.length); - // } + remuxVideo (track, timeOffset, contiguous, audioTrackLength, accurateTimeOffset) { + let offset = 8, + timeScale = track.timescale, + mp4SampleDuration, + mdat, moof, + firstPTS, firstDTS, + nextDTS, + lastPTS, lastDTS, + inputSamples = track.samples, + outputSamples = [], + nbSamples = inputSamples.length, + ptsNormalize = this._PTSNormalize, + initDTS = this._initDTS; + + // for (let i = 0; i < track.samples.length; i++) { + // let avcSample = track.samples[i]; + // let units = avcSample.units; + // let unitsString = ''; + // for (let j = 0; j < units.length ; j++) { + // unitsString += units[j].type + ','; + // if (units[j].data.length < 500) { + // unitsString += Hex.hexDump(units[j].data); + // } + // } + // logger.log(avcSample.pts + '/' + avcSample.dts + ',' + unitsString + avcSample.units.length); + // } // if parsed fragment is contiguous with last one, let's use last DTS value as reference let nextAvcDts = this.nextAvcDts; const isSafari = this.isSafari; - - if(nbSamples === 0){ - return; - } + + if (nbSamples === 0) + return; // Safari does not like overlapping DTS on consecutive fragments. let's use nextAvcDts to overcome this if fragments are consecutive if (isSafari) { @@ -218,84 +214,82 @@ class MP4Remuxer { // - less than 100ms gaps between new time offset (if accurate) and next expected PTS OR // - less than 200 ms PTS gaps (timeScale/5) contiguous |= (inputSamples.length && nextAvcDts && - ((accurateTimeOffset && Math.abs(timeOffset-nextAvcDts/timeScale) < 0.1) || - Math.abs((inputSamples[0].pts-nextAvcDts-initDTS)) < timeScale/5) - ); + ((accurateTimeOffset && Math.abs(timeOffset - nextAvcDts / timeScale) < 0.1) || + Math.abs((inputSamples[0].pts - nextAvcDts - initDTS)) < timeScale / 5) + ); } if (!contiguous) { // if not contiguous, let's use target timeOffset - nextAvcDts = timeOffset*timeScale; + nextAvcDts = timeOffset * timeScale; } - // PTS is coded on 33bits, and can loop from -2^32 to 2^32 - // ptsNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value - inputSamples.forEach(function(sample) { - sample.pts = ptsNormalize(sample.pts-initDTS, nextAvcDts); - sample.dts = ptsNormalize(sample.dts-initDTS, nextAvcDts); + // PTS is coded on 33bits, and can loop from -2^32 to 2^32 + // ptsNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value + inputSamples.forEach(function (sample) { + sample.pts = ptsNormalize(sample.pts - initDTS, nextAvcDts); + sample.dts = ptsNormalize(sample.dts - initDTS, nextAvcDts); }); // sort video samples by DTS then PTS then demux id order - inputSamples.sort(function(a, b) { + inputSamples.sort(function (a, b) { const deltadts = a.dts - b.dts; const deltapts = a.pts - b.pts; - return deltadts ? deltadts : deltapts ? deltapts : (a.id - b.id); + return deltadts || (deltapts || (a.id - b.id)); }); // handle broken streams with PTS < DTS, tolerance up 200ms (18000 in 90kHz timescale) - let PTSDTSshift = inputSamples.reduce( (prev, curr) => Math.max(Math.min(prev,curr.pts-curr.dts),-18000),0); + let PTSDTSshift = inputSamples.reduce((prev, curr) => Math.max(Math.min(prev, curr.pts - curr.dts), -18000), 0); if (PTSDTSshift < 0) { - logger.warn(`PTS < DTS detected in video samples, shifting DTS by ${Math.round(PTSDTSshift/90)} ms to overcome this issue`); - for (let i = 0; i < inputSamples.length; i++) { + logger.warn(`PTS < DTS detected in video samples, shifting DTS by ${Math.round(PTSDTSshift / 90)} ms to overcome this issue`); + for (let i = 0; i < inputSamples.length; i++) inputSamples[i].dts += PTSDTSshift; - } } // compute first DTS and last DTS, normalize them against reference value let sample = inputSamples[0]; - firstDTS = Math.max(sample.dts,0); - firstPTS = Math.max(sample.pts,0); + firstDTS = Math.max(sample.dts, 0); + firstPTS = Math.max(sample.pts, 0); // check timestamp continuity accross consecutive fragments (this is to remove inter-fragment gap/hole) let delta = Math.round((firstDTS - nextAvcDts) / 90); // if fragment are contiguous, detect hole/overlapping between fragments if (contiguous) { if (delta) { - if (delta > 1) { + if (delta > 1) logger.log(`AVC:${delta} ms hole between fragments detected,filling it`); - } else if (delta < -1) { + else if (delta < -1) logger.log(`AVC:${(-delta)} ms overlapping between fragments detected`); - } + // remove hole/gap : set DTS to next expected DTS firstDTS = nextAvcDts; inputSamples[0].dts = firstDTS; // offset PTS as well, ensure that PTS is smaller or equal than new DTS firstPTS = Math.max(firstPTS - delta, nextAvcDts); inputSamples[0].pts = firstPTS; - logger.log(`Video/PTS/DTS adjusted: ${Math.round(firstPTS/90)}/${Math.round(firstDTS/90)},delta:${delta} ms`); + logger.log(`Video/PTS/DTS adjusted: ${Math.round(firstPTS / 90)}/${Math.round(firstDTS / 90)},delta:${delta} ms`); } } nextDTS = firstDTS; // compute lastPTS/lastDTS - sample = inputSamples[inputSamples.length-1]; - lastDTS = Math.max(sample.dts,0); - lastPTS = Math.max(sample.pts,0,lastDTS); + sample = inputSamples[inputSamples.length - 1]; + lastDTS = Math.max(sample.dts, 0); + lastPTS = Math.max(sample.pts, 0, lastDTS); - // on Safari let's signal the same sample duration for all samples - // sample duration (as expected by trun MP4 boxes), should be the delta between sample DTS - // set this constant duration as being the avg delta between consecutive DTS. - if (isSafari) { - mp4SampleDuration = Math.round((lastDTS-firstDTS)/(inputSamples.length-1)); - } + // on Safari let's signal the same sample duration for all samples + // sample duration (as expected by trun MP4 boxes), should be the delta between sample DTS + // set this constant duration as being the avg delta between consecutive DTS. + if (isSafari) + mp4SampleDuration = Math.round((lastDTS - firstDTS) / (inputSamples.length - 1)); let nbNalu = 0, naluLen = 0; - for (let i = 0 ; i < nbSamples; i++) { + for (let i = 0; i < nbSamples; i++) { // compute total/avc sample length and nb of NAL units let sample = inputSamples[i], units = sample.units, nbUnits = units.length, sampleLen = 0; - for (let j = 0; j < nbUnits; j++) { + for (let j = 0; j < nbUnits; j++) sampleLen += units[j].data.length; - } + naluLen += sampleLen; nbNalu += nbUnits; sample.length = sampleLen; @@ -303,13 +297,13 @@ class MP4Remuxer { // normalize PTS/DTS if (isSafari) { // sample DTS is computed using a constant decoding offset (mp4SampleDuration) between samples - sample.dts = firstDTS + i*mp4SampleDuration; + sample.dts = firstDTS + i * mp4SampleDuration; } else { // ensure sample monotonic DTS - sample.dts = Math.max(sample.dts,firstDTS); + sample.dts = Math.max(sample.dts, firstDTS); } // ensure that computed value is greater or equal than sample DTS - sample.pts = Math.max(sample.pts , sample.dts); + sample.pts = Math.max(sample.pts, sample.dts); } /* concatenate the video data and construct the mdat in place @@ -317,24 +311,24 @@ class MP4Remuxer { let mdatSize = naluLen + (4 * nbNalu) + 8; try { mdat = new Uint8Array(mdatSize); - } catch(err) { - this.observer.trigger(Event.ERROR, {type : ErrorTypes.MUX_ERROR, details: ErrorDetails.REMUX_ALLOC_ERROR, fatal: false, bytes : mdatSize, reason: `fail allocating video mdat ${mdatSize}`}); + } catch (err) { + this.observer.trigger(Event.ERROR, { type: ErrorTypes.MUX_ERROR, details: ErrorDetails.REMUX_ALLOC_ERROR, fatal: false, bytes: mdatSize, reason: `fail allocating video mdat ${mdatSize}` }); return; } let view = new DataView(mdat.buffer); view.setUint32(0, mdatSize); mdat.set(MP4.types.mdat, 4); - for (let i = 0 ; i < nbSamples; i++) { + for (let i = 0; i < nbSamples; i++) { let avcSample = inputSamples[i], - avcSampleUnits = avcSample.units, - mp4SampleLength = 0, - compositionTimeOffset; + avcSampleUnits = avcSample.units, + mp4SampleLength = 0, + compositionTimeOffset; // convert NALU bitstream to MP4 format (prepend NALU with size field) - for(let j = 0, nbUnits = avcSampleUnits.length; j < nbUnits ; j++) { + for (let j = 0, nbUnits = avcSampleUnits.length; j < nbUnits; j++) { let unit = avcSampleUnits[j], - unitData = unit.data, - unitDataLen = unit.data.byteLength; + unitData = unit.data, + unitDataLen = unit.data.byteLength; view.setUint32(offset, unitDataLen); offset += 4; mdat.set(unitData, offset); @@ -342,13 +336,13 @@ class MP4Remuxer { mp4SampleLength += 4 + unitDataLen; } - if(!isSafari) { + if (!isSafari) { // expected sample duration is the Decoding Timestamp diff of consecutive samples if (i < nbSamples - 1) { - mp4SampleDuration = inputSamples[i+1].dts - avcSample.dts; + mp4SampleDuration = inputSamples[i + 1].dts - avcSample.dts; } else { let config = this.config, - lastFrameDuration = avcSample.dts - inputSamples[i > 0 ? i-1 : i].dts; + lastFrameDuration = avcSample.dts - inputSamples[i > 0 ? i - 1 : i].dts; if (config.stretchShortVideoTrack) { // In some cases, a segment's audio track duration may exceed the video track duration. // Since we've already remuxed audio, and we know how long the audio track is, we look to @@ -356,16 +350,16 @@ class MP4Remuxer { // If so, playback would potentially get stuck, so we artificially inflate // the duration of the last frame to minimize any potential gap between segments. let maxBufferHole = config.maxBufferHole, - gapTolerance = Math.floor(maxBufferHole * timeScale), - deltaToFrameEnd = (audioTrackLength ? firstPTS + audioTrackLength * timeScale : this.nextAudioPts) - avcSample.pts; + gapTolerance = Math.floor(maxBufferHole * timeScale), + deltaToFrameEnd = (audioTrackLength ? firstPTS + audioTrackLength * timeScale : this.nextAudioPts) - avcSample.pts; if (deltaToFrameEnd > gapTolerance) { // We subtract lastFrameDuration from deltaToFrameEnd to try to prevent any video // frame overlap. maxBufferHole should be >> lastFrameDuration anyway. mp4SampleDuration = deltaToFrameEnd - lastFrameDuration; - if (mp4SampleDuration < 0) { + if (mp4SampleDuration < 0) mp4SampleDuration = lastFrameDuration; - } - logger.log(`It is approximately ${deltaToFrameEnd/90} ms to the next segment; using duration ${mp4SampleDuration/90} ms for the last video frame.`); + + logger.log(`It is approximately ${deltaToFrameEnd / 90} ms to the next segment; using duration ${mp4SampleDuration / 90} ms for the last video frame.`); } else { mp4SampleDuration = lastFrameDuration; } @@ -375,13 +369,13 @@ class MP4Remuxer { } compositionTimeOffset = Math.round(avcSample.pts - avcSample.dts); } else { - compositionTimeOffset = Math.max(0,mp4SampleDuration*Math.round((avcSample.pts - avcSample.dts)/mp4SampleDuration)); + compositionTimeOffset = Math.max(0, mp4SampleDuration * Math.round((avcSample.pts - avcSample.dts) / mp4SampleDuration)); } - //console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${avcSample.pts}/${avcSample.dts}/${initDTS}/${ptsnorm}/${dtsnorm}/${(avcSample.pts/4294967296).toFixed(3)}'); + // console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${avcSample.pts}/${avcSample.dts}/${initDTS}/${ptsnorm}/${dtsnorm}/${(avcSample.pts/4294967296).toFixed(3)}'); outputSamples.push({ size: mp4SampleLength, - // constant duration + // constant duration duration: mp4SampleDuration, cts: compositionTimeOffset, flags: { @@ -389,8 +383,8 @@ class MP4Remuxer { isDependedOn: 0, hasRedundancy: 0, degradPrio: 0, - dependsOn : avcSample.key ? 2 : 1, - isNonSync : avcSample.key ? 0 : 1 + dependsOn: avcSample.key ? 2 : 1, + isNonSync: avcSample.key ? 0 : 1 } }); } @@ -400,52 +394,52 @@ class MP4Remuxer { track.len = 0; track.nbNalu = 0; track.dropped = 0; - if(outputSamples.length && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { + if (outputSamples.length && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { let flags = outputSamples[0].flags; - // chrome workaround, mark first sample as being a Random Access Point to avoid sourcebuffer append issue - // https://code.google.com/p/chromium/issues/detail?id=229412 + // chrome workaround, mark first sample as being a Random Access Point to avoid sourcebuffer append issue + // https://code.google.com/p/chromium/issues/detail?id=229412 flags.dependsOn = 2; flags.isNonSync = 0; } track.samples = outputSamples; - moof = MP4.moof(track.sequenceNumber++, firstDTS , track); + moof = MP4.moof(track.sequenceNumber++, firstDTS, track); track.samples = []; let data = { data1: moof, data2: mdat, startPTS: firstPTS / timeScale, - endPTS: (lastPTS + mp4SampleDuration) / timeScale, + endPTS: (lastPTS + mp4SampleDuration) / timeScale, startDTS: firstDTS / timeScale, endDTS: this.nextAvcDts / timeScale, type: 'video', hasAudio: false, hasVideo: true, nb: outputSamples.length, - dropped : dropped + dropped: dropped }; this.observer.trigger(Event.FRAG_PARSING_DATA, data); return data; } - remuxAudio(track, timeOffset, contiguous,accurateTimeOffset) { + remuxAudio (track, timeOffset, contiguous, accurateTimeOffset) { const inputTimeScale = track.inputTimeScale, - mp4timeScale = track.timescale, - scaleFactor = inputTimeScale/mp4timeScale, - mp4SampleDuration = track.isAAC ? 1024 : 1152, - inputSampleDuration = mp4SampleDuration * scaleFactor, - ptsNormalize = this._PTSNormalize, - initDTS = this._initDTS, - rawMPEG = !track.isAAC && this.typeSupported.mpeg; - - var offset, - mp4Sample, - fillFrame, - mdat, moof, - firstPTS, lastPTS, - inputSamples = track.samples, - outputSamples = [], - nextAudioPts = this.nextAudioPts; + mp4timeScale = track.timescale, + scaleFactor = inputTimeScale / mp4timeScale, + mp4SampleDuration = track.isAAC ? 1024 : 1152, + inputSampleDuration = mp4SampleDuration * scaleFactor, + ptsNormalize = this._PTSNormalize, + initDTS = this._initDTS, + rawMPEG = !track.isAAC && this.typeSupported.mpeg; + + let offset, + mp4Sample, + fillFrame, + mdat, moof, + firstPTS, lastPTS, + inputSamples = track.samples, + outputSamples = [], + nextAudioPts = this.nextAudioPts; // for audio samples, also consider consecutive fragments as being contiguous (even if a level switch occurs), // for sake of clarity: @@ -456,27 +450,25 @@ class MP4Remuxer { // this helps ensuring audio continuity // and this also avoids audio glitches/cut when switching quality, or reporting wrong duration on first audio frame contiguous |= (inputSamples.length && nextAudioPts && - ((accurateTimeOffset && Math.abs(timeOffset-nextAudioPts/inputTimeScale) < 0.1) || - Math.abs((inputSamples[0].pts-nextAudioPts-initDTS)) < 20*inputSampleDuration) - ); - + ((accurateTimeOffset && Math.abs(timeOffset - nextAudioPts / inputTimeScale) < 0.1) || + Math.abs((inputSamples[0].pts - nextAudioPts - initDTS)) < 20 * inputSampleDuration) + ); // compute normalized PTS - inputSamples.forEach(function(sample) { - sample.pts = sample.dts = ptsNormalize(sample.pts - initDTS, timeOffset*inputTimeScale); + inputSamples.forEach(function (sample) { + sample.pts = sample.dts = ptsNormalize(sample.pts - initDTS, timeOffset * inputTimeScale); }); // filter out sample with negative PTS that are not playable anyway // if we don't remove these negative samples, they will shift all audio samples forward. // leading to audio overlap between current / next fragment - inputSamples = inputSamples.filter(function(sample) { + inputSamples = inputSamples.filter(function (sample) { return sample.pts >= 0; }); // in case all samples have negative PTS, and have been filtered out, return now - if (inputSamples.length === 0) { + if (inputSamples.length === 0) return; - } if (!contiguous) { if (!accurateTimeOffset) { @@ -484,7 +476,7 @@ class MP4Remuxer { nextAudioPts = inputSamples[0].pts; } else { // if timeOffset is accurate, let's use it as predicted next audio PTS - nextAudioPts = timeOffset*inputTimeScale; + nextAudioPts = timeOffset * inputTimeScale; } } @@ -494,39 +486,39 @@ class MP4Remuxer { // When possible, we inject a silent frame; when that's not possible, we duplicate the last // frame. - if ( track.isAAC) { + if (track.isAAC) { const maxAudioFramesDrift = this.config.maxAudioFramesDrift; - for (let i = 0, nextPts = nextAudioPts; i < inputSamples.length; ) { + for (let i = 0, nextPts = nextAudioPts; i < inputSamples.length;) { // First, let's see how far off this frame is from where we expect it to be var sample = inputSamples[i], delta; let pts = sample.pts; delta = pts - nextPts; - const duration = Math.abs(1000*delta/inputTimeScale); + const duration = Math.abs(1000 * delta / inputTimeScale); // If we're overlapping by more than a duration, drop this sample - if (delta <= -maxAudioFramesDrift*inputSampleDuration) { - logger.warn(`Dropping 1 audio frame @ ${(nextPts/inputTimeScale).toFixed(3)}s due to ${Math.round(duration)} ms overlap.`); + if (delta <= -maxAudioFramesDrift * inputSampleDuration) { + logger.warn(`Dropping 1 audio frame @ ${(nextPts / inputTimeScale).toFixed(3)}s due to ${Math.round(duration)} ms overlap.`); inputSamples.splice(i, 1); track.len -= sample.unit.length; // Don't touch nextPtsNorm or i - } + } // eslint-disable-line brace-style // Insert missing frames if: // 1: We're more than maxAudioFramesDrift frame away // 2: Not more than MAX_SILENT_FRAME_DURATION away // 3: currentTime (aka nextPtsNorm) is not 0 - else if (delta >= maxAudioFramesDrift*inputSampleDuration && duration < MAX_SILENT_FRAME_DURATION && nextPts) { - var missing = Math.round(delta / inputSampleDuration); - logger.warn(`Injecting ${missing} audio frame @ ${(nextPts/inputTimeScale).toFixed(3)}s due to ${Math.round(1000*delta/inputTimeScale)} ms gap.`); - for (var j = 0; j < missing; j++) { - let newStamp = Math.max(nextPts,0); - fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec,track.channelCount); + else if (delta >= maxAudioFramesDrift * inputSampleDuration && duration < MAX_SILENT_FRAME_DURATION && nextPts) { + let missing = Math.round(delta / inputSampleDuration); + logger.warn(`Injecting ${missing} audio frame @ ${(nextPts / inputTimeScale).toFixed(3)}s due to ${Math.round(1000 * delta / inputTimeScale)} ms gap.`); + for (let j = 0; j < missing; j++) { + let newStamp = Math.max(nextPts, 0); + fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount); if (!fillFrame) { logger.log('Unable to get silent frame for given audio codec; duplicating last frame instead.'); fillFrame = sample.unit.subarray(); } - inputSamples.splice(i, 0, {unit: fillFrame, pts: newStamp, dts: newStamp}); + inputSamples.splice(i, 0, { unit: fillFrame, pts: newStamp, dts: newStamp }); track.len += fillFrame.length; nextPts += inputSampleDuration; i++; @@ -539,7 +531,7 @@ class MP4Remuxer { } else { // Otherwise, just adjust pts if (Math.abs(delta) > (0.1 * inputSampleDuration)) { - //logger.log(`Invalid frame delta ${Math.round(delta + inputSampleDuration)} at PTS ${Math.round(pts / 90)} (should be ${Math.round(inputSampleDuration)}).`); + // logger.log(`Invalid frame delta ${Math.round(delta + inputSampleDuration)} at PTS ${Math.round(pts / 90)} (should be ${Math.round(inputSampleDuration)}).`); } sample.pts = sample.dts = nextPts; nextPts += inputSampleDuration; @@ -548,18 +540,17 @@ class MP4Remuxer { } } - - for (let j =0 , nbSamples = inputSamples.length; j < nbSamples ; j++) { + for (let j = 0, nbSamples = inputSamples.length; j < nbSamples; j++) { let audioSample = inputSamples[j]; let unit = audioSample.unit; let pts = audioSample.pts; - //logger.log(`Audio/PTS:${Math.round(pts/90)}`); + // logger.log(`Audio/PTS:${Math.round(pts/90)}`); // if not first sample if (lastPTS !== undefined) { mp4Sample.duration = Math.round((pts - lastPTS) / scaleFactor); } else { let delta = Math.round(1000 * (pts - nextAudioPts) / inputTimeScale), - numMissingFrames = 0; + numMissingFrames = 0; // if fragment are contiguous, detect hole/overlapping between fragments // contiguous fragments are consecutive fragments from same quality level (same level, new SN = old SN + 1) if (contiguous && track.isAAC) { @@ -569,16 +560,16 @@ class MP4Remuxer { numMissingFrames = Math.round((pts - nextAudioPts) / inputSampleDuration); logger.log(`${delta} ms hole between AAC samples detected,filling it`); if (numMissingFrames > 0) { - fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec,track.channelCount); - if (!fillFrame) { + fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount); + if (!fillFrame) fillFrame = unit.subarray(); - } + track.len += numMissingFrames * fillFrame.length; } // if we have frame overlap, overlapping for more than half a frame duraion } else if (delta < -12) { // drop overlapping audio frames... browser will deal with it - logger.log(`drop overlapping AAC sample, expected/parsed/delta:${(nextAudioPts/inputTimeScale).toFixed(3)}s/${(pts/inputTimeScale).toFixed(3)}s/${(-delta)}ms`); + logger.log(`drop overlapping AAC sample, expected/parsed/delta:${(nextAudioPts / inputTimeScale).toFixed(3)}s/${(pts / inputTimeScale).toFixed(3)}s/${(-delta)}ms`); track.len -= unit.byteLength; continue; } @@ -588,15 +579,15 @@ class MP4Remuxer { } // remember first PTS of our audioSamples firstPTS = pts; - if(track.len > 0) { + if (track.len > 0) { /* concatenate the audio data and construct the mdat in place (need 8 more bytes to fill length and mdat type) */ let mdatSize = rawMPEG ? track.len : track.len + 8; offset = rawMPEG ? 0 : 8; try { mdat = new Uint8Array(mdatSize); - } catch(err) { - this.observer.trigger(Event.ERROR, {type : ErrorTypes.MUX_ERROR, details: ErrorDetails.REMUX_ALLOC_ERROR, fatal: false, bytes : mdatSize, reason: `fail allocating audio mdat ${mdatSize}`}); + } catch (err) { + this.observer.trigger(Event.ERROR, { type: ErrorTypes.MUX_ERROR, details: ErrorDetails.REMUX_ALLOC_ERROR, fatal: false, bytes: mdatSize, reason: `fail allocating audio mdat ${mdatSize}` }); return; } if (!rawMPEG) { @@ -609,7 +600,7 @@ class MP4Remuxer { return; } for (let i = 0; i < numMissingFrames; i++) { - fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec,track.channelCount); + fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount); if (!fillFrame) { logger.log('Unable to get silent frame for given audio codec; duplicating this frame instead.'); fillFrame = unit.subarray(); @@ -625,7 +616,7 @@ class MP4Remuxer { isDependedOn: 0, hasRedundancy: 0, degradPrio: 0, - dependsOn: 1, + dependsOn: 1 } }; outputSamples.push(mp4Sample); @@ -634,7 +625,7 @@ class MP4Remuxer { mdat.set(unit, offset); let unitLen = unit.byteLength; offset += unitLen; - //console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${audioSample.pts}/${audioSample.dts}/${initDTS}/${ptsnorm}/${dtsnorm}/${(audioSample.pts/4294967296).toFixed(3)}'); + // console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${audioSample.pts}/${audioSample.dts}/${initDTS}/${ptsnorm}/${dtsnorm}/${(audioSample.pts/4294967296).toFixed(3)}'); mp4Sample = { size: unitLen, cts: 0, @@ -644,15 +635,15 @@ class MP4Remuxer { isDependedOn: 0, hasRedundancy: 0, degradPrio: 0, - dependsOn: 1, + dependsOn: 1 } }; outputSamples.push(mp4Sample); lastPTS = pts; } - var lastSampleDuration = 0; - var nbSamples = outputSamples.length; - //set last sample duration as being identical to previous sample + let lastSampleDuration = 0; + let nbSamples = outputSamples.length; + // set last sample duration as being identical to previous sample if (nbSamples >= 2) { lastSampleDuration = outputSamples[nbSamples - 2].duration; mp4Sample.duration = lastSampleDuration; @@ -660,14 +651,14 @@ class MP4Remuxer { if (nbSamples) { // next audio sample PTS should be equal to last sample PTS + duration this.nextAudioPts = nextAudioPts = lastPTS + scaleFactor * lastSampleDuration; - //logger.log('Audio/PTS/PTSend:' + audioSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0)); + // logger.log('Audio/PTS/PTSend:' + audioSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0)); track.len = 0; track.samples = outputSamples; - if (rawMPEG) { + if (rawMPEG) moof = new Uint8Array(); - } else { + else moof = MP4.moof(track.sequenceNumber++, firstPTS / scaleFactor, track); - } + track.samples = []; const start = firstPTS / inputTimeScale; const end = nextAudioPts / inputTimeScale; @@ -689,26 +680,26 @@ class MP4Remuxer { return null; } - remuxEmptyAudio(track, timeOffset, contiguous, videoData) { + remuxEmptyAudio (track, timeOffset, contiguous, videoData) { let inputTimeScale = track.inputTimeScale, - mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale, - scaleFactor = inputTimeScale/mp4timeScale, - nextAudioPts = this.nextAudioPts, + mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale, + scaleFactor = inputTimeScale / mp4timeScale, + nextAudioPts = this.nextAudioPts, - // sync with video's timestamp - startDTS = (nextAudioPts !== undefined ? nextAudioPts : videoData.startDTS * inputTimeScale) + this._initDTS, - endDTS = videoData.endDTS * inputTimeScale + this._initDTS, - // one sample's duration value - sampleDuration = 1024, - frameDuration = scaleFactor * sampleDuration, + // sync with video's timestamp + startDTS = (nextAudioPts !== undefined ? nextAudioPts : videoData.startDTS * inputTimeScale) + this._initDTS, + endDTS = videoData.endDTS * inputTimeScale + this._initDTS, + // one sample's duration value + sampleDuration = 1024, + frameDuration = scaleFactor * sampleDuration, - // samples count of this segment's duration - nbSamples = Math.ceil((endDTS - startDTS) / frameDuration), + // samples count of this segment's duration + nbSamples = Math.ceil((endDTS - startDTS) / frameDuration), - // silent frame - silentFrame = AAC.getSilentFrame(track.manifestCodec || track.codec,track.channelCount); + // silent frame + silentFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount); - logger.warn('remux empty Audio'); + logger.warn('remux empty Audio'); // Can't remux if we can't generate a silent frame... if (!silentFrame) { logger.trace('Unable to remuxEmptyAudio since we were unable to get a silent frame for given audio codec!'); @@ -716,9 +707,9 @@ class MP4Remuxer { } let samples = []; - for(var i = 0; i < nbSamples; i++) { - var stamp = startDTS + i * frameDuration; - samples.push({unit: silentFrame, pts: stamp, dts: stamp}); + for (let i = 0; i < nbSamples; i++) { + let stamp = startDTS + i * frameDuration; + samples.push({ unit: silentFrame, pts: stamp, dts: stamp }); track.len += silentFrame.length; } track.samples = samples; @@ -726,14 +717,14 @@ class MP4Remuxer { this.remuxAudio(track, timeOffset, contiguous); } - remuxID3(track,timeOffset) { - var length = track.samples.length, sample; + remuxID3 (track, timeOffset) { + let length = track.samples.length, sample; const inputTimeScale = track.inputTimeScale; const initPTS = this._initPTS; const initDTS = this._initDTS; // consume samples - if(length) { - for(var index = 0; index < length; index++) { + if (length) { + for (let index = 0; index < length; index++) { sample = track.samples[index]; // setting id3 pts, dts to relative time // using this._initPTS and this._initDTS to calculate relative time @@ -741,7 +732,7 @@ class MP4Remuxer { sample.dts = ((sample.dts - initDTS) / inputTimeScale); } this.observer.trigger(Event.FRAG_PARSING_METADATA, { - samples:track.samples + samples: track.samples }); } @@ -749,24 +740,24 @@ class MP4Remuxer { timeOffset = timeOffset; } - remuxText(track,timeOffset) { - track.samples.sort(function(a, b) { - return (a.pts-b.pts); + remuxText (track, timeOffset) { + track.samples.sort(function (a, b) { + return (a.pts - b.pts); }); - var length = track.samples.length, sample; + let length = track.samples.length, sample; const inputTimeScale = track.inputTimeScale; const initPTS = this._initPTS; // consume samples - if(length) { - for(var index = 0; index < length; index++) { + if (length) { + for (let index = 0; index < length; index++) { sample = track.samples[index]; // setting text pts, dts to relative time // using this._initPTS and this._initDTS to calculate relative time sample.pts = ((sample.pts - initPTS) / inputTimeScale); } this.observer.trigger(Event.FRAG_PARSING_USERDATA, { - samples:track.samples + samples: track.samples }); } @@ -774,11 +765,11 @@ class MP4Remuxer { timeOffset = timeOffset; } - _PTSNormalize(value, reference) { - var offset; - if (reference === undefined) { + _PTSNormalize (value, reference) { + let offset; + if (reference === undefined) return value; - } + if (reference < value) { // - 2^33 offset = -8589934592; @@ -789,12 +780,11 @@ class MP4Remuxer { /* PTS is 33bit (from 0 to 2^33 -1) if diff between value and reference is bigger than half of the amplitude (2^32) then it means that PTS looping occured. fill the gap */ - while (Math.abs(value - reference) > 4294967296) { - value += offset; - } + while (Math.abs(value - reference) > 4294967296) + value += offset; + return value; } - } export default MP4Remuxer; diff --git a/src/remux/passthrough-remuxer.js b/src/remux/passthrough-remuxer.js index c4debf771d6..b7db60e8f71 100644 --- a/src/remux/passthrough-remuxer.js +++ b/src/remux/passthrough-remuxer.js @@ -4,28 +4,28 @@ import Event from '../events'; class PassThroughRemuxer { - constructor(observer) { + constructor (observer) { this.observer = observer; } - destroy() { + destroy () { } - resetTimeStamp() { + resetTimeStamp () { } - resetInitSegment() { + resetInitSegment () { } - remux(audioTrack,videoTrack,id3Track,textTrack,timeOffset, contiguous,accurateTimeOffset,rawData) { - var observer = this.observer; - var streamType = ''; - if (audioTrack) { + remux (audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous, accurateTimeOffset, rawData) { + let observer = this.observer; + let streamType = ''; + if (audioTrack) streamType += 'audio'; - } - if (videoTrack) { + + if (videoTrack) streamType += 'video'; - } + observer.trigger(Event.FRAG_PARSING_DATA, { data1: rawData, startPTS: timeOffset, @@ -34,9 +34,9 @@ class PassThroughRemuxer { hasAudio: !!audioTrack, hasVideo: !!videoTrack, nb: 1, - dropped : 0 + dropped: 0 }); - //notify end of parsing + // notify end of parsing observer.trigger(Event.FRAG_PARSED); } } diff --git a/src/task-loop.js b/src/task-loop.js index 2bda2bfe002..3b8c7a9d9dc 100644 --- a/src/task-loop.js +++ b/src/task-loop.js @@ -1,8 +1,7 @@ import EventHandler from './event-handler'; export default class TaskLoop extends EventHandler { - - constructor(hls, ...events) { + constructor (hls, ...events) { super(hls, ...events); this._tickInterval = null; @@ -12,7 +11,7 @@ export default class TaskLoop extends EventHandler { /** * @override */ - destroy() { + destroy () { this.clearInterval(); super.destroy(); } @@ -20,7 +19,7 @@ export default class TaskLoop extends EventHandler { /** * @returns {boolean} */ - hasInterval() { + hasInterval () { return this._tickInterval !== null; } @@ -28,7 +27,7 @@ export default class TaskLoop extends EventHandler { * @param {number} millis Interval time (ms) * @returns {boolean} True when interval has been scheduled, false when already scheduled (no effect) */ - setInterval(millis) { + setInterval (millis) { if (!this._tickInterval) { this._tickInterval = setInterval(this.tick.bind(this, false), millis); return true; @@ -39,7 +38,7 @@ export default class TaskLoop extends EventHandler { /** * @returns {boolean} True when interval was cleared, false when none was set (no effect) */ - clearInterval() { + clearInterval () { if (this._tickInterval) { clearInterval(this._tickInterval); this._tickInterval = null; @@ -53,13 +52,13 @@ export default class TaskLoop extends EventHandler { * @param {Wether to force async} forceAsync * @returns {boolean} True when async, false when sync */ - tick() { + tick () { this._tickCallCount++; if (this._tickCallCount === 1) { this.doTick(); - if (this._tickCallCount > 1) { + if (this._tickCallCount > 1) setTimeout(this.tick.bind(this), 0); - } + this._tickCallCount = 0; } } @@ -68,8 +67,7 @@ export default class TaskLoop extends EventHandler { * For subclass to implement task logic * @abstract */ - doTick() { + doTick () { throw new Error('TaskLoop is abstract and `doLoop` must be implemented'); } - } diff --git a/src/utils/attr-list.js b/src/utils/attr-list.js index 9e96ee5e472..45cfd93ae14 100755 --- a/src/utils/attr-list.js +++ b/src/utils/attr-list.js @@ -1,85 +1,82 @@ -const DECIMAL_RESOLUTION_REGEX = /^(\d+)x(\d+)$/; -const ATTR_LIST_REGEX = /\s*(.+?)\s*=((?:\".*?\")|.*?)(?:,|$)/g; +const DECIMAL_RESOLUTION_REGEX = /^(\d+)x(\d+)$/; // eslint-disable-line no-useless-escape +const ATTR_LIST_REGEX = /\s*(.+?)\s*=((?:\".*?\")|.*?)(?:,|$)/g; // eslint-disable-line no-useless-escape // adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js class AttrList { - - constructor(attrs) { - if (typeof attrs === 'string') { + constructor (attrs) { + if (typeof attrs === 'string') attrs = AttrList.parseAttrList(attrs); - } - for(var attr in attrs){ - if(attrs.hasOwnProperty(attr)) { + + for (let attr in attrs) { + if (attrs.hasOwnProperty(attr)) this[attr] = attrs[attr]; - } } } - decimalInteger(attrName) { + decimalInteger (attrName) { const intValue = parseInt(this[attrName], 10); - if (intValue > Number.MAX_SAFE_INTEGER) { + if (intValue > Number.MAX_SAFE_INTEGER) return Infinity; - } + return intValue; } - hexadecimalInteger(attrName) { - if(this[attrName]) { + hexadecimalInteger (attrName) { + if (this[attrName]) { let stringValue = (this[attrName] || '0x').slice(2); stringValue = ((stringValue.length & 1) ? '0' : '') + stringValue; const value = new Uint8Array(stringValue.length / 2); - for (let i = 0; i < stringValue.length / 2; i++) { + for (let i = 0; i < stringValue.length / 2; i++) value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16); - } + return value; } else { return null; } } - hexadecimalIntegerAsNumber(attrName) { + hexadecimalIntegerAsNumber (attrName) { const intValue = parseInt(this[attrName], 16); - if (intValue > Number.MAX_SAFE_INTEGER) { + if (intValue > Number.MAX_SAFE_INTEGER) return Infinity; - } + return intValue; } - decimalFloatingPoint(attrName) { + decimalFloatingPoint (attrName) { return parseFloat(this[attrName]); } - enumeratedString(attrName) { + enumeratedString (attrName) { return this[attrName]; } - decimalResolution(attrName) { + decimalResolution (attrName) { const res = DECIMAL_RESOLUTION_REGEX.exec(this[attrName]); - if (res === null) { + if (res === null) return undefined; - } + return { width: parseInt(res[1], 10), height: parseInt(res[2], 10) }; } - static parseAttrList(input) { - var match, attrs = {}; + static parseAttrList (input) { + let match, attrs = {}; ATTR_LIST_REGEX.lastIndex = 0; while ((match = ATTR_LIST_REGEX.exec(input)) !== null) { - var value = match[2], quote = '"'; + let value = match[2], quote = '"'; if (value.indexOf(quote) === 0 && - value.lastIndexOf(quote) === (value.length-1)) { + value.lastIndexOf(quote) === (value.length - 1)) value = value.slice(1, -1); - } + attrs[match[1]] = value; } return attrs; } - } export default AttrList; diff --git a/src/utils/binary-search.js b/src/utils/binary-search.js index 05a71b11834..80ca66091f9 100644 --- a/src/utils/binary-search.js +++ b/src/utils/binary-search.js @@ -1,5 +1,5 @@ -var BinarySearch = { - /** +let BinarySearch = { + /** * Searches for an item in an array which matches a certain condition. * This requires the condition to only match one item in the array, * and for the array to be ordered. @@ -14,30 +14,27 @@ var BinarySearch = { * * @return {*} The object if it is found or null otherwise. */ - search: function(list, comparisonFunction) { - var minIndex = 0; - var maxIndex = list.length - 1; - var currentIndex = null; - var currentElement = null; - - while (minIndex <= maxIndex) { - currentIndex = (minIndex + maxIndex) / 2 | 0; - currentElement = list[currentIndex]; - - var comparisonResult = comparisonFunction(currentElement); - if (comparisonResult > 0) { - minIndex = currentIndex + 1; - } - else if (comparisonResult < 0) { - maxIndex = currentIndex - 1; - } - else { - return currentElement; - } - } - - return null; + search: function (list, comparisonFunction) { + let minIndex = 0; + let maxIndex = list.length - 1; + let currentIndex = null; + let currentElement = null; + + while (minIndex <= maxIndex) { + currentIndex = (minIndex + maxIndex) / 2 | 0; + currentElement = list[currentIndex]; + + let comparisonResult = comparisonFunction(currentElement); + if (comparisonResult > 0) + minIndex = currentIndex + 1; + else if (comparisonResult < 0) + maxIndex = currentIndex - 1; + else + return currentElement; } + + return null; + } }; export default BinarySearch; diff --git a/src/utils/cea-608-parser.js b/src/utils/cea-608-parser.js index 4dc48e7b5b4..1f69119c3e5 100644 --- a/src/utils/cea-608-parser.js +++ b/src/utils/cea-608-parser.js @@ -35,250 +35,245 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ - /** +/** * Exceptions from regular ASCII. CodePoints are mapped to UTF-16 codes */ -var specialCea608CharsCodes = { - 0x2a : 0xe1, // lowercase a, acute accent - 0x5c : 0xe9, // lowercase e, acute accent - 0x5e : 0xed, // lowercase i, acute accent - 0x5f : 0xf3, // lowercase o, acute accent - 0x60 : 0xfa, // lowercase u, acute accent - 0x7b : 0xe7, // lowercase c with cedilla - 0x7c : 0xf7, // division symbol - 0x7d : 0xd1, // uppercase N tilde - 0x7e : 0xf1, // lowercase n tilde - 0x7f : 0x2588, // Full block - // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS - // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F - // THIS MEANS THAT \x50 MUST BE ADDED TO THE VALUES - 0x80 : 0xae, // Registered symbol (R) - 0x81 : 0xb0, // degree sign - 0x82 : 0xbd, // 1/2 symbol - 0x83 : 0xbf, // Inverted (open) question mark - 0x84 : 0x2122, // Trademark symbol (TM) - 0x85 : 0xa2, // Cents symbol - 0x86 : 0xa3, // Pounds sterling - 0x87 : 0x266a, // Music 8'th note - 0x88 : 0xe0, // lowercase a, grave accent - 0x89 : 0x20, // transparent space (regular) - 0x8a : 0xe8, // lowercase e, grave accent - 0x8b : 0xe2, // lowercase a, circumflex accent - 0x8c : 0xea, // lowercase e, circumflex accent - 0x8d : 0xee, // lowercase i, circumflex accent - 0x8e : 0xf4, // lowercase o, circumflex accent - 0x8f : 0xfb, // lowercase u, circumflex accent - // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS - // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F - 0x90 : 0xc1, // capital letter A with acute - 0x91 : 0xc9, // capital letter E with acute - 0x92 : 0xd3, // capital letter O with acute - 0x93 : 0xda, // capital letter U with acute - 0x94 : 0xdc, // capital letter U with diaresis - 0x95 : 0xfc, // lowercase letter U with diaeresis - 0x96 : 0x2018, // opening single quote - 0x97 : 0xa1, // inverted exclamation mark - 0x98 : 0x2a, // asterisk - 0x99 : 0x2019, // closing single quote - 0x9a : 0x2501, // box drawings heavy horizontal - 0x9b : 0xa9, // copyright sign - 0x9c : 0x2120, // Service mark - 0x9d : 0x2022, // (round) bullet - 0x9e : 0x201c, // Left double quotation mark - 0x9f : 0x201d, // Right double quotation mark - 0xa0 : 0xc0, // uppercase A, grave accent - 0xa1 : 0xc2, // uppercase A, circumflex - 0xa2 : 0xc7, // uppercase C with cedilla - 0xa3 : 0xc8, // uppercase E, grave accent - 0xa4 : 0xca, // uppercase E, circumflex - 0xa5 : 0xcb, // capital letter E with diaresis - 0xa6 : 0xeb, // lowercase letter e with diaresis - 0xa7 : 0xce, // uppercase I, circumflex - 0xa8 : 0xcf, // uppercase I, with diaresis - 0xa9 : 0xef, // lowercase i, with diaresis - 0xaa : 0xd4, // uppercase O, circumflex - 0xab : 0xd9, // uppercase U, grave accent - 0xac : 0xf9, // lowercase u, grave accent - 0xad : 0xdb, // uppercase U, circumflex - 0xae : 0xab, // left-pointing double angle quotation mark - 0xaf : 0xbb, // right-pointing double angle quotation mark - // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS - // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F - 0xb0 : 0xc3, // Uppercase A, tilde - 0xb1 : 0xe3, // Lowercase a, tilde - 0xb2 : 0xcd, // Uppercase I, acute accent - 0xb3 : 0xcc, // Uppercase I, grave accent - 0xb4 : 0xec, // Lowercase i, grave accent - 0xb5 : 0xd2, // Uppercase O, grave accent - 0xb6 : 0xf2, // Lowercase o, grave accent - 0xb7 : 0xd5, // Uppercase O, tilde - 0xb8 : 0xf5, // Lowercase o, tilde - 0xb9 : 0x7b, // Open curly brace - 0xba : 0x7d, // Closing curly brace - 0xbb : 0x5c, // Backslash - 0xbc : 0x5e, // Caret - 0xbd : 0x5f, // Underscore - 0xbe : 0x7c, // Pipe (vertical line) - 0xbf : 0x223c, // Tilde operator - 0xc0 : 0xc4, // Uppercase A, umlaut - 0xc1 : 0xe4, // Lowercase A, umlaut - 0xc2 : 0xd6, // Uppercase O, umlaut - 0xc3 : 0xf6, // Lowercase o, umlaut - 0xc4 : 0xdf, // Esszett (sharp S) - 0xc5 : 0xa5, // Yen symbol - 0xc6 : 0xa4, // Generic currency sign - 0xc7 : 0x2503, // Box drawings heavy vertical - 0xc8 : 0xc5, // Uppercase A, ring - 0xc9 : 0xe5, // Lowercase A, ring - 0xca : 0xd8, // Uppercase O, stroke - 0xcb : 0xf8, // Lowercase o, strok - 0xcc : 0x250f, // Box drawings heavy down and right - 0xcd : 0x2513, // Box drawings heavy down and left - 0xce : 0x2517, // Box drawings heavy up and right - 0xcf : 0x251b // Box drawings heavy up and left +let specialCea608CharsCodes = { + 0x2a: 0xe1, // lowercase a, acute accent + 0x5c: 0xe9, // lowercase e, acute accent + 0x5e: 0xed, // lowercase i, acute accent + 0x5f: 0xf3, // lowercase o, acute accent + 0x60: 0xfa, // lowercase u, acute accent + 0x7b: 0xe7, // lowercase c with cedilla + 0x7c: 0xf7, // division symbol + 0x7d: 0xd1, // uppercase N tilde + 0x7e: 0xf1, // lowercase n tilde + 0x7f: 0x2588, // Full block + // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS + // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F + // THIS MEANS THAT \x50 MUST BE ADDED TO THE VALUES + 0x80: 0xae, // Registered symbol (R) + 0x81: 0xb0, // degree sign + 0x82: 0xbd, // 1/2 symbol + 0x83: 0xbf, // Inverted (open) question mark + 0x84: 0x2122, // Trademark symbol (TM) + 0x85: 0xa2, // Cents symbol + 0x86: 0xa3, // Pounds sterling + 0x87: 0x266a, // Music 8'th note + 0x88: 0xe0, // lowercase a, grave accent + 0x89: 0x20, // transparent space (regular) + 0x8a: 0xe8, // lowercase e, grave accent + 0x8b: 0xe2, // lowercase a, circumflex accent + 0x8c: 0xea, // lowercase e, circumflex accent + 0x8d: 0xee, // lowercase i, circumflex accent + 0x8e: 0xf4, // lowercase o, circumflex accent + 0x8f: 0xfb, // lowercase u, circumflex accent + // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS + // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F + 0x90: 0xc1, // capital letter A with acute + 0x91: 0xc9, // capital letter E with acute + 0x92: 0xd3, // capital letter O with acute + 0x93: 0xda, // capital letter U with acute + 0x94: 0xdc, // capital letter U with diaresis + 0x95: 0xfc, // lowercase letter U with diaeresis + 0x96: 0x2018, // opening single quote + 0x97: 0xa1, // inverted exclamation mark + 0x98: 0x2a, // asterisk + 0x99: 0x2019, // closing single quote + 0x9a: 0x2501, // box drawings heavy horizontal + 0x9b: 0xa9, // copyright sign + 0x9c: 0x2120, // Service mark + 0x9d: 0x2022, // (round) bullet + 0x9e: 0x201c, // Left double quotation mark + 0x9f: 0x201d, // Right double quotation mark + 0xa0: 0xc0, // uppercase A, grave accent + 0xa1: 0xc2, // uppercase A, circumflex + 0xa2: 0xc7, // uppercase C with cedilla + 0xa3: 0xc8, // uppercase E, grave accent + 0xa4: 0xca, // uppercase E, circumflex + 0xa5: 0xcb, // capital letter E with diaresis + 0xa6: 0xeb, // lowercase letter e with diaresis + 0xa7: 0xce, // uppercase I, circumflex + 0xa8: 0xcf, // uppercase I, with diaresis + 0xa9: 0xef, // lowercase i, with diaresis + 0xaa: 0xd4, // uppercase O, circumflex + 0xab: 0xd9, // uppercase U, grave accent + 0xac: 0xf9, // lowercase u, grave accent + 0xad: 0xdb, // uppercase U, circumflex + 0xae: 0xab, // left-pointing double angle quotation mark + 0xaf: 0xbb, // right-pointing double angle quotation mark + // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS + // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F + 0xb0: 0xc3, // Uppercase A, tilde + 0xb1: 0xe3, // Lowercase a, tilde + 0xb2: 0xcd, // Uppercase I, acute accent + 0xb3: 0xcc, // Uppercase I, grave accent + 0xb4: 0xec, // Lowercase i, grave accent + 0xb5: 0xd2, // Uppercase O, grave accent + 0xb6: 0xf2, // Lowercase o, grave accent + 0xb7: 0xd5, // Uppercase O, tilde + 0xb8: 0xf5, // Lowercase o, tilde + 0xb9: 0x7b, // Open curly brace + 0xba: 0x7d, // Closing curly brace + 0xbb: 0x5c, // Backslash + 0xbc: 0x5e, // Caret + 0xbd: 0x5f, // Underscore + 0xbe: 0x7c, // Pipe (vertical line) + 0xbf: 0x223c, // Tilde operator + 0xc0: 0xc4, // Uppercase A, umlaut + 0xc1: 0xe4, // Lowercase A, umlaut + 0xc2: 0xd6, // Uppercase O, umlaut + 0xc3: 0xf6, // Lowercase o, umlaut + 0xc4: 0xdf, // Esszett (sharp S) + 0xc5: 0xa5, // Yen symbol + 0xc6: 0xa4, // Generic currency sign + 0xc7: 0x2503, // Box drawings heavy vertical + 0xc8: 0xc5, // Uppercase A, ring + 0xc9: 0xe5, // Lowercase A, ring + 0xca: 0xd8, // Uppercase O, stroke + 0xcb: 0xf8, // Lowercase o, strok + 0xcc: 0x250f, // Box drawings heavy down and right + 0xcd: 0x2513, // Box drawings heavy down and left + 0xce: 0x2517, // Box drawings heavy up and right + 0xcf: 0x251b // Box drawings heavy up and left }; /** * Utils */ -var getCharForByte = function(byte) { - var charCode = byte; - if (specialCea608CharsCodes.hasOwnProperty(byte)) { - charCode = specialCea608CharsCodes[byte]; - } - return String.fromCharCode(charCode); +let getCharForByte = function (byte) { + let charCode = byte; + if (specialCea608CharsCodes.hasOwnProperty(byte)) + charCode = specialCea608CharsCodes[byte]; + + return String.fromCharCode(charCode); }; -var NR_ROWS = 15, - NR_COLS = 100; +let NR_ROWS = 15, + NR_COLS = 100; // Tables to look up row from PAC data -var rowsLowCh1 = {0x11 : 1, 0x12 : 3, 0x15 : 5, 0x16 : 7, 0x17 : 9, 0x10 : 11, 0x13 : 12, 0x14 : 14}; -var rowsHighCh1 = {0x11 : 2, 0x12 : 4, 0x15 : 6, 0x16 : 8, 0x17 : 10, 0x13 : 13, 0x14 : 15}; -var rowsLowCh2 = {0x19 : 1, 0x1A : 3, 0x1D : 5, 0x1E : 7, 0x1F : 9, 0x18 : 11, 0x1B : 12, 0x1C : 14}; -var rowsHighCh2 = {0x19 : 2, 0x1A : 4, 0x1D : 6, 0x1E : 8, 0x1F : 10, 0x1B : 13, 0x1C : 15}; +let rowsLowCh1 = { 0x11: 1, 0x12: 3, 0x15: 5, 0x16: 7, 0x17: 9, 0x10: 11, 0x13: 12, 0x14: 14 }; +let rowsHighCh1 = { 0x11: 2, 0x12: 4, 0x15: 6, 0x16: 8, 0x17: 10, 0x13: 13, 0x14: 15 }; +let rowsLowCh2 = { 0x19: 1, 0x1A: 3, 0x1D: 5, 0x1E: 7, 0x1F: 9, 0x18: 11, 0x1B: 12, 0x1C: 14 }; +let rowsHighCh2 = { 0x19: 2, 0x1A: 4, 0x1D: 6, 0x1E: 8, 0x1F: 10, 0x1B: 13, 0x1C: 15 }; -var backgroundColors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'black', 'transparent']; +let backgroundColors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'black', 'transparent']; /** * Simple logger class to be able to write with time-stamps and filter on level. */ -var logger = { - verboseFilter : {'DATA' : 3, 'DEBUG' : 3, 'INFO' : 2, 'WARNING' : 2, 'TEXT' : 1, 'ERROR' : 0}, - time : null, - verboseLevel : 0, // Only write errors - setTime : function(newTime) { - this.time = newTime; - }, - log : function(severity, msg) { - var minLevel = this.verboseFilter[severity]; - if (this.verboseLevel >= minLevel) { - console.log(this.time + ' [' + severity + '] ' + msg); - } - } +let logger = { + verboseFilter: { 'DATA': 3, 'DEBUG': 3, 'INFO': 2, 'WARNING': 2, 'TEXT': 1, 'ERROR': 0 }, + time: null, + verboseLevel: 0, // Only write errors + setTime: function (newTime) { + this.time = newTime; + }, + log: function (severity, msg) { + let minLevel = this.verboseFilter[severity]; + if (this.verboseLevel >= minLevel) + console.log(this.time + ' [' + severity + '] ' + msg); + } }; -var numArrayToHexArray = function(numArray) { - var hexArray = []; - for (var j = 0; j < numArray.length; j++) { - hexArray.push(numArray[j].toString(16)); - } - return hexArray; +let numArrayToHexArray = function (numArray) { + let hexArray = []; + for (let j = 0; j < numArray.length; j++) + hexArray.push(numArray[j].toString(16)); + + return hexArray; }; class PenState { - - constructor(foreground, underline, italics, background, flash) { - this.foreground = foreground || 'white'; - this.underline = underline || false; - this.italics = italics || false; - this.background = background || 'black'; - this.flash = flash || false; - } - - reset() { - this.foreground = 'white'; - this.underline = false; - this.italics = false; - this.background = 'black'; - this.flash = false; - } - - setStyles(styles) { - var attribs = ['foreground', 'underline', 'italics', 'background', 'flash']; - for (var i = 0 ; i < attribs.length; i++) { - var style = attribs[i]; - if (styles.hasOwnProperty(style)) { - this[style] = styles[style]; - } - } - } - - isDefault() { - return (this.foreground === 'white' && !this.underline && !this.italics && + constructor (foreground, underline, italics, background, flash) { + this.foreground = foreground || 'white'; + this.underline = underline || false; + this.italics = italics || false; + this.background = background || 'black'; + this.flash = flash || false; + } + + reset () { + this.foreground = 'white'; + this.underline = false; + this.italics = false; + this.background = 'black'; + this.flash = false; + } + + setStyles (styles) { + let attribs = ['foreground', 'underline', 'italics', 'background', 'flash']; + for (let i = 0; i < attribs.length; i++) { + let style = attribs[i]; + if (styles.hasOwnProperty(style)) + this[style] = styles[style]; + } + } + + isDefault () { + return (this.foreground === 'white' && !this.underline && !this.italics && this.background === 'black' && !this.flash); - } + } - equals(other) { - return ( (this.foreground === other.foreground) && + equals (other) { + return ((this.foreground === other.foreground) && (this.underline === other.underline) && (this.italics === other.italics) && (this.background === other.background) && - (this.flash === other.flash) ); - } - - copy(newPenState) { - this.foreground = newPenState.foreground; - this.underline = newPenState.underline; - this.italics = newPenState.italics; - this.background = newPenState.background; - this.flash = newPenState.flash; - } - - toString() { - return ('color=' + this.foreground + ', underline=' + this.underline + ', italics=' + this.italics + + (this.flash === other.flash)); + } + + copy (newPenState) { + this.foreground = newPenState.foreground; + this.underline = newPenState.underline; + this.italics = newPenState.italics; + this.background = newPenState.background; + this.flash = newPenState.flash; + } + + toString () { + return ('color=' + this.foreground + ', underline=' + this.underline + ', italics=' + this.italics + ', background=' + this.background + ', flash=' + this.flash); - } + } } /** * Unicode character with styling and background. * @constructor */ -class StyledUnicodeChar -{ - - constructor(uchar, foreground, underline, italics, background, flash) { - this.uchar = uchar || ' '; // unicode character - this.penState = new PenState(foreground, underline,italics, background, flash); - } - - reset() { - this.uchar = ' '; - this.penState.reset(); - } - - setChar(uchar, newPenState) { - this.uchar = uchar; - this.penState.copy(newPenState); - } - - setPenState(newPenState) { - this.penState.copy(newPenState); - } - - equals(other) { - return this.uchar === other.uchar && this.penState.equals(other.penState); - } - - copy(newChar) { - this.uchar = newChar.uchar; - this.penState.copy(newChar.penState); - } - - isEmpty() { - return this.uchar === ' ' && this.penState.isDefault(); - } +class StyledUnicodeChar { + constructor (uchar, foreground, underline, italics, background, flash) { + this.uchar = uchar || ' '; // unicode character + this.penState = new PenState(foreground, underline, italics, background, flash); + } + + reset () { + this.uchar = ' '; + this.penState.reset(); + } + + setChar (uchar, newPenState) { + this.uchar = uchar; + this.penState.copy(newPenState); + } + + setPenState (newPenState) { + this.penState.copy(newPenState); + } + + equals (other) { + return this.uchar === other.uchar && this.penState.equals(other.penState); + } + + copy (newChar) { + this.uchar = newChar.uchar; + this.penState.copy(newChar.penState); + } + + isEmpty () { + return this.uchar === ' ' && this.penState.isDefault(); + } } /** @@ -286,133 +281,129 @@ class StyledUnicodeChar * @constructor */ class Row { - constructor() { - this.chars = []; - for (var i = 0 ; i < NR_COLS ; i++) { - this.chars.push(new StyledUnicodeChar()); - } - this.pos = 0; - this.currPenState = new PenState(); - } - - equals(other) { - var equal = true; - for (var i = 0 ; i < NR_COLS; i ++) { - if (!this.chars[i].equals(other.chars[i])) { - equal = false; - break; - } - } - return equal; - } - - copy(other) { - for (var i = 0 ; i < NR_COLS; i ++) { - this.chars[i].copy(other.chars[i]); - } - } - - isEmpty() { - var empty = true; - for (var i = 0 ; i < NR_COLS; i ++) { - if (!this.chars[i].isEmpty()) { - empty = false; - break; - } - } - return empty; - } - - /** + constructor () { + this.chars = []; + for (let i = 0; i < NR_COLS; i++) + this.chars.push(new StyledUnicodeChar()); + + this.pos = 0; + this.currPenState = new PenState(); + } + + equals (other) { + let equal = true; + for (let i = 0; i < NR_COLS; i++) { + if (!this.chars[i].equals(other.chars[i])) { + equal = false; + break; + } + } + return equal; + } + + copy (other) { + for (let i = 0; i < NR_COLS; i++) + this.chars[i].copy(other.chars[i]); + } + + isEmpty () { + let empty = true; + for (let i = 0; i < NR_COLS; i++) { + if (!this.chars[i].isEmpty()) { + empty = false; + break; + } + } + return empty; + } + + /** * Set the cursor to a valid column. */ - setCursor(absPos) { - if (this.pos !== absPos) { - this.pos = absPos; - } - if (this.pos < 0) { - logger.log('ERROR', 'Negative cursor position ' + this.pos); - this.pos = 0; - } else if (this.pos > NR_COLS) { - logger.log('ERROR', 'Too large cursor position ' + this.pos); - this.pos = NR_COLS; - } + setCursor (absPos) { + if (this.pos !== absPos) + this.pos = absPos; + + if (this.pos < 0) { + logger.log('ERROR', 'Negative cursor position ' + this.pos); + this.pos = 0; + } else if (this.pos > NR_COLS) { + logger.log('ERROR', 'Too large cursor position ' + this.pos); + this.pos = NR_COLS; } + } - /** + /** * Move the cursor relative to current position. */ - moveCursor(relPos) { - var newPos = this.pos + relPos; - if (relPos > 1) { - for (var i = this.pos+1; i < newPos+1 ; i++) { - this.chars[i].setPenState(this.currPenState); - } - } - this.setCursor(newPos); + moveCursor (relPos) { + let newPos = this.pos + relPos; + if (relPos > 1) { + for (let i = this.pos + 1; i < newPos + 1; i++) + this.chars[i].setPenState(this.currPenState); } + this.setCursor(newPos); + } - /** + /** * Backspace, move one step back and clear character. */ - backSpace() { - this.moveCursor(-1); - this.chars[this.pos].setChar(' ', this.currPenState); - } - - insertChar(byte) { - if (byte >= 0x90) { //Extended char - this.backSpace(); - } - var char = getCharForByte(byte); - if (this.pos >= NR_COLS) { - logger.log('ERROR', 'Cannot insert ' + byte.toString(16) + + backSpace () { + this.moveCursor(-1); + this.chars[this.pos].setChar(' ', this.currPenState); + } + + insertChar (byte) { + if (byte >= 0x90) { // Extended char + this.backSpace(); + } + let char = getCharForByte(byte); + if (this.pos >= NR_COLS) { + logger.log('ERROR', 'Cannot insert ' + byte.toString(16) + ' (' + char + ') at position ' + this.pos + '. Skipping it!'); - return; - } - this.chars[this.pos].setChar(char, this.currPenState); - this.moveCursor(1); - } - - clearFromPos(startPos) { - var i; - for (i = startPos ; i < NR_COLS ; i++) { - this.chars[i].reset(); - } - } - - clear() { - this.clearFromPos(0); - this.pos = 0; - this.currPenState.reset(); - } - - clearToEndOfRow() { - this.clearFromPos(this.pos); - } - - getTextString() { - var chars = []; - var empty = true; - for (var i = 0 ; i < NR_COLS ; i++) { - var char = this.chars[i].uchar; - if (char !== ' ') { - empty = false; - } - chars.push(char); - } - if (empty) { - return ''; - } else { - return chars.join(''); - } - } - - setPenStyles(styles) { - this.currPenState.setStyles(styles); - var currChar = this.chars[this.pos]; - currChar.setPenState(this.currPenState); - } + return; + } + this.chars[this.pos].setChar(char, this.currPenState); + this.moveCursor(1); + } + + clearFromPos (startPos) { + let i; + for (i = startPos; i < NR_COLS; i++) + this.chars[i].reset(); + } + + clear () { + this.clearFromPos(0); + this.pos = 0; + this.currPenState.reset(); + } + + clearToEndOfRow () { + this.clearFromPos(this.pos); + } + + getTextString () { + let chars = []; + let empty = true; + for (let i = 0; i < NR_COLS; i++) { + let char = this.chars[i].uchar; + if (char !== ' ') + empty = false; + + chars.push(char); + } + if (empty) + return ''; + else + return chars.join(''); + } + + setPenStyles (styles) { + this.currPenState.setStyles(styles); + let currChar = this.chars[this.pos]; + currChar.setPenState(this.currPenState); + } } /** @@ -420,744 +411,721 @@ class Row { * @constructor */ class CaptionScreen { - - constructor() { - this.rows = []; - for (var i = 0 ; i < NR_ROWS; i++) { - this.rows.push(new Row()); // Note that we use zero-based numbering (0-14) - } - this.currRow = NR_ROWS - 1; - this.nrRollUpRows = null; - this.reset(); - } - - reset() { - for (var i = 0 ; i < NR_ROWS ; i++) { - this.rows[i].clear(); - } - this.currRow = NR_ROWS - 1; - } - - equals(other) { - var equal = true; - for (var i = 0 ; i < NR_ROWS ; i++) { - if (!this.rows[i].equals(other.rows[i])) { - equal = false; - break; - } - } - return equal; - } - - copy(other) { - for (var i = 0 ; i < NR_ROWS ; i++) { - this.rows[i].copy(other.rows[i]); - } - } - - isEmpty() { - var empty = true; - for (var i = 0 ; i < NR_ROWS ; i++) { - if (!this.rows[i].isEmpty()) { - empty = false; - break; - } - } - return empty; - } - - backSpace() { - var row = this.rows[this.currRow]; - row.backSpace(); - } - - clearToEndOfRow() { - var row = this.rows[this.currRow]; - row.clearToEndOfRow(); - } - - /** + constructor () { + this.rows = []; + for (let i = 0; i < NR_ROWS; i++) + this.rows.push(new Row()); // Note that we use zero-based numbering (0-14) + + this.currRow = NR_ROWS - 1; + this.nrRollUpRows = null; + this.reset(); + } + + reset () { + for (let i = 0; i < NR_ROWS; i++) + this.rows[i].clear(); + + this.currRow = NR_ROWS - 1; + } + + equals (other) { + let equal = true; + for (let i = 0; i < NR_ROWS; i++) { + if (!this.rows[i].equals(other.rows[i])) { + equal = false; + break; + } + } + return equal; + } + + copy (other) { + for (let i = 0; i < NR_ROWS; i++) + this.rows[i].copy(other.rows[i]); + } + + isEmpty () { + let empty = true; + for (let i = 0; i < NR_ROWS; i++) { + if (!this.rows[i].isEmpty()) { + empty = false; + break; + } + } + return empty; + } + + backSpace () { + let row = this.rows[this.currRow]; + row.backSpace(); + } + + clearToEndOfRow () { + let row = this.rows[this.currRow]; + row.clearToEndOfRow(); + } + + /** * Insert a character (without styling) in the current row. */ - insertChar(char) { - var row = this.rows[this.currRow]; - row.insertChar(char); - } - - setPen(styles) { - var row = this.rows[this.currRow]; - row.setPenStyles(styles); - } - - moveCursor(relPos) { - var row = this.rows[this.currRow]; - row.moveCursor(relPos); - } - - setCursor(absPos) { - logger.log('INFO', 'setCursor: ' + absPos); - var row = this.rows[this.currRow]; - row.setCursor(absPos); - } - - setPAC(pacData) { - logger.log('INFO', 'pacData = ' + JSON.stringify(pacData)); - var newRow = pacData.row - 1; - if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) { - newRow = this.nrRollUpRows-1; - } - - //Make sure this only affects Roll-up Captions by checking this.nrRollUpRows - if (this.nrRollUpRows && this.currRow !== newRow) { - //clear all rows first - for (let i = 0; i < NR_ROWS; i++) { - this.rows[i].clear(); - } - - //Copy this.nrRollUpRows rows from lastOutputScreen and place it in the newRow location - //topRowIndex - the start of rows to copy (inclusive index) - var topRowIndex = this.currRow + 1 - (this.nrRollUpRows); - //We only copy if the last position was already shown. - //We use the cueStartTime value to check this. - const lastOutputScreen = this.lastOutputScreen; - if (lastOutputScreen) { - var prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime; - if(prevLineTime && prevLineTime < logger.time) { - for (let i = 0; i < this.nrRollUpRows; i++) { - this.rows[newRow-this.nrRollUpRows+i+1].copy(lastOutputScreen.rows[topRowIndex+i]); - } - } - } + insertChar (char) { + let row = this.rows[this.currRow]; + row.insertChar(char); + } + + setPen (styles) { + let row = this.rows[this.currRow]; + row.setPenStyles(styles); + } + + moveCursor (relPos) { + let row = this.rows[this.currRow]; + row.moveCursor(relPos); + } + + setCursor (absPos) { + logger.log('INFO', 'setCursor: ' + absPos); + let row = this.rows[this.currRow]; + row.setCursor(absPos); + } + + setPAC (pacData) { + logger.log('INFO', 'pacData = ' + JSON.stringify(pacData)); + let newRow = pacData.row - 1; + if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) + newRow = this.nrRollUpRows - 1; + + // Make sure this only affects Roll-up Captions by checking this.nrRollUpRows + if (this.nrRollUpRows && this.currRow !== newRow) { + // clear all rows first + for (let i = 0; i < NR_ROWS; i++) + this.rows[i].clear(); + + // Copy this.nrRollUpRows rows from lastOutputScreen and place it in the newRow location + // topRowIndex - the start of rows to copy (inclusive index) + let topRowIndex = this.currRow + 1 - (this.nrRollUpRows); + // We only copy if the last position was already shown. + // We use the cueStartTime value to check this. + const lastOutputScreen = this.lastOutputScreen; + if (lastOutputScreen) { + let prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime; + if (prevLineTime && prevLineTime < logger.time) { + for (let i = 0; i < this.nrRollUpRows; i++) + this.rows[newRow - this.nrRollUpRows + i + 1].copy(lastOutputScreen.rows[topRowIndex + i]); } + } + } - this.currRow = newRow; - var row = this.rows[this.currRow]; - if (pacData.indent !== null) { - var indent = pacData.indent; - var prevPos = Math.max(indent-1, 0); - row.setCursor(pacData.indent); - pacData.color = row.chars[prevPos].penState.foreground; - } - var styles = {foreground : pacData.color, underline : pacData.underline, italics : pacData.italics, background : 'black', flash : false}; - this.setPen(styles); + this.currRow = newRow; + let row = this.rows[this.currRow]; + if (pacData.indent !== null) { + let indent = pacData.indent; + let prevPos = Math.max(indent - 1, 0); + row.setCursor(pacData.indent); + pacData.color = row.chars[prevPos].penState.foreground; } + let styles = { foreground: pacData.color, underline: pacData.underline, italics: pacData.italics, background: 'black', flash: false }; + this.setPen(styles); + } - /** + /** * Set background/extra foreground, but first do back_space, and then insert space (backwards compatibility). */ - setBkgData(bkgData) { - - logger.log('INFO', 'bkgData = ' + JSON.stringify(bkgData)); - this.backSpace(); - this.setPen(bkgData); - this.insertChar(0x20); //Space - } - - setRollUpRows(nrRows) { - this.nrRollUpRows = nrRows; - } - - rollUp() { - if (this.nrRollUpRows === null) { - logger.log('DEBUG', 'roll_up but nrRollUpRows not set yet'); - return; //Not properly setup - } - logger.log('TEXT', this.getDisplayText()); - var topRowIndex = this.currRow + 1 - this.nrRollUpRows; - var topRow = this.rows.splice(topRowIndex, 1)[0]; - topRow.clear(); - this.rows.splice(this.currRow, 0, topRow); - logger.log('INFO', 'Rolling up'); - //logger.log('TEXT', this.get_display_text()) - } - - /** + setBkgData (bkgData) { + logger.log('INFO', 'bkgData = ' + JSON.stringify(bkgData)); + this.backSpace(); + this.setPen(bkgData); + this.insertChar(0x20); // Space + } + + setRollUpRows (nrRows) { + this.nrRollUpRows = nrRows; + } + + rollUp () { + if (this.nrRollUpRows === null) { + logger.log('DEBUG', 'roll_up but nrRollUpRows not set yet'); + return; // Not properly setup + } + logger.log('TEXT', this.getDisplayText()); + let topRowIndex = this.currRow + 1 - this.nrRollUpRows; + let topRow = this.rows.splice(topRowIndex, 1)[0]; + topRow.clear(); + this.rows.splice(this.currRow, 0, topRow); + logger.log('INFO', 'Rolling up'); + // logger.log('TEXT', this.get_display_text()) + } + + /** * Get all non-empty rows with as unicode text. */ - getDisplayText(asOneRow) { - asOneRow = asOneRow || false; - var displayText = []; - var text = ''; - var rowNr = -1; - for (var i = 0 ; i < NR_ROWS ; i++) { - var rowText = this.rows[i].getTextString(); - if (rowText) { - rowNr = i+1; - if (asOneRow) { - displayText.push('Row ' + rowNr + ': \'' + rowText + '\''); - } else { - displayText.push(rowText.trim()); - } - } - } - if (displayText.length > 0) { - if (asOneRow) { - text = '[' + displayText.join(' | ') + ']'; - } else { - text = displayText.join('\n'); - } - } - return text; - } - - getTextAndFormat() { - return this.rows; - } + getDisplayText (asOneRow) { + asOneRow = asOneRow || false; + let displayText = []; + let text = ''; + let rowNr = -1; + for (let i = 0; i < NR_ROWS; i++) { + let rowText = this.rows[i].getTextString(); + if (rowText) { + rowNr = i + 1; + if (asOneRow) + displayText.push('Row ' + rowNr + ': \'' + rowText + '\''); + else + displayText.push(rowText.trim()); + } + } + if (displayText.length > 0) { + if (asOneRow) + text = '[' + displayText.join(' | ') + ']'; + else + text = displayText.join('\n'); + } + return text; + } + + getTextAndFormat () { + return this.rows; + } } -//var modes = ['MODE_ROLL-UP', 'MODE_POP-ON', 'MODE_PAINT-ON', 'MODE_TEXT']; - -class Cea608Channel -{ - constructor(channelNumber, outputFilter) { - - this.chNr = channelNumber; - this.outputFilter = outputFilter; - this.mode = null; - this.verbose = 0; - this.displayedMemory = new CaptionScreen(); - this.nonDisplayedMemory = new CaptionScreen(); - this.lastOutputScreen = new CaptionScreen(); - this.currRollUpRow = this.displayedMemory.rows[NR_ROWS-1]; - this.writeScreen = this.displayedMemory; - this.mode = null; - this.cueStartTime = null; // Keeps track of where a cue started. - } - - reset() { - this.mode = null; - this.displayedMemory.reset(); - this.nonDisplayedMemory.reset(); - this.lastOutputScreen.reset(); - this.currRollUpRow = this.displayedMemory.rows[NR_ROWS-1]; - this.writeScreen = this.displayedMemory; - this.mode = null; - this.cueStartTime = null; - this.lastCueEndTime = null; - } - - getHandler() { - return this.outputFilter; - } - - setHandler(newHandler) { - this.outputFilter = newHandler; - } - - setPAC(pacData) { - this.writeScreen.setPAC(pacData); - } - - setBkgData(bkgData) { - this.writeScreen.setBkgData(bkgData); - } - - setMode(newMode) { - if (newMode === this.mode) { - return; - } - this.mode = newMode; - logger.log('INFO', 'MODE=' + newMode); - if (this.mode === 'MODE_POP-ON') { - this.writeScreen = this.nonDisplayedMemory; - } else { - this.writeScreen = this.displayedMemory; - this.writeScreen.reset(); - } - if (this.mode !== 'MODE_ROLL-UP') { - this.displayedMemory.nrRollUpRows = null; - this.nonDisplayedMemory.nrRollUpRows = null; - } - this.mode = newMode; - } - - insertChars(chars) { - for (var i = 0 ; i < chars.length ; i++) { - this.writeScreen.insertChar(chars[i]); - } - var screen = this.writeScreen === this.displayedMemory ? 'DISP' : 'NON_DISP'; - logger.log('INFO', screen + ': ' + this.writeScreen.getDisplayText(true)); - if (this.mode === 'MODE_PAINT-ON' || this.mode === 'MODE_ROLL-UP') { - logger.log('TEXT', 'DISPLAYED: ' + this.displayedMemory.getDisplayText(true)); - this.outputDataUpdate(); - } - } - - ccRCL() { // Resume Caption Loading (switch mode to Pop On) - logger.log('INFO', 'RCL - Resume Caption Loading'); - this.setMode('MODE_POP-ON'); - } - - ccBS() { // BackSpace - logger.log('INFO', 'BS - BackSpace'); - if (this.mode === 'MODE_TEXT') { - return; - } - this.writeScreen.backSpace(); - if (this.writeScreen === this.displayedMemory) { - this.outputDataUpdate(); - } - } - - ccAOF() { // Reserved (formerly Alarm Off) - return; - } - - ccAON() { // Reserved (formerly Alarm On) - return; - } - - ccDER() { // Delete to End of Row - logger.log('INFO', 'DER- Delete to End of Row'); - this.writeScreen.clearToEndOfRow(); - this.outputDataUpdate(); - } - - ccRU(nrRows) { //Roll-Up Captions-2,3,or 4 Rows - logger.log('INFO', 'RU(' + nrRows +') - Roll Up'); - this.writeScreen = this.displayedMemory; - this.setMode('MODE_ROLL-UP'); - this.writeScreen.setRollUpRows(nrRows); - } - - ccFON() { //Flash On - logger.log('INFO', 'FON - Flash On'); - this.writeScreen.setPen({flash : true}); - } - - ccRDC() { // Resume Direct Captioning (switch mode to PaintOn) - logger.log('INFO', 'RDC - Resume Direct Captioning'); - this.setMode('MODE_PAINT-ON'); - } - - ccTR() { // Text Restart in text mode (not supported, however) - logger.log('INFO', 'TR'); - this.setMode('MODE_TEXT'); - } - - ccRTD() { // Resume Text Display in Text mode (not supported, however) - logger.log('INFO', 'RTD'); - this.setMode('MODE_TEXT'); - } - - ccEDM() { // Erase Displayed Memory - logger.log('INFO', 'EDM - Erase Displayed Memory'); - this.displayedMemory.reset(); - this.outputDataUpdate(true); - } - - ccCR() { // Carriage Return - logger.log('CR - Carriage Return'); - this.writeScreen.rollUp(); - this.outputDataUpdate(true); - } - - ccENM() { //Erase Non-Displayed Memory - logger.log('INFO', 'ENM - Erase Non-displayed Memory'); - this.nonDisplayedMemory.reset(); - } - - ccEOC() { //End of Caption (Flip Memories) - logger.log('INFO', 'EOC - End Of Caption'); - if (this.mode === 'MODE_POP-ON') { - var tmp = this.displayedMemory; - this.displayedMemory = this.nonDisplayedMemory; - this.nonDisplayedMemory = tmp; - this.writeScreen = this.nonDisplayedMemory; - logger.log('TEXT', 'DISP: ' + this.displayedMemory.getDisplayText()); - } - this.outputDataUpdate(true); - } - - ccTO(nrCols) { // Tab Offset 1,2, or 3 columns - logger.log('INFO', 'TO(' + nrCols + ') - Tab Offset'); - this.writeScreen.moveCursor(nrCols); - } - - ccMIDROW(secondByte) { // Parse MIDROW command - var styles = {flash : false}; - styles.underline = secondByte % 2 === 1; - styles.italics = secondByte >= 0x2e; - if (!styles.italics) { - var colorIndex = Math.floor(secondByte/2) - 0x10; - var colors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta']; - styles.foreground = colors[colorIndex]; - } else { - styles.foreground = 'white'; +// var modes = ['MODE_ROLL-UP', 'MODE_POP-ON', 'MODE_PAINT-ON', 'MODE_TEXT']; + +class Cea608Channel { + constructor (channelNumber, outputFilter) { + this.chNr = channelNumber; + this.outputFilter = outputFilter; + this.mode = null; + this.verbose = 0; + this.displayedMemory = new CaptionScreen(); + this.nonDisplayedMemory = new CaptionScreen(); + this.lastOutputScreen = new CaptionScreen(); + this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1]; + this.writeScreen = this.displayedMemory; + this.mode = null; + this.cueStartTime = null; // Keeps track of where a cue started. + } + + reset () { + this.mode = null; + this.displayedMemory.reset(); + this.nonDisplayedMemory.reset(); + this.lastOutputScreen.reset(); + this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1]; + this.writeScreen = this.displayedMemory; + this.mode = null; + this.cueStartTime = null; + this.lastCueEndTime = null; + } + + getHandler () { + return this.outputFilter; + } + + setHandler (newHandler) { + this.outputFilter = newHandler; + } + + setPAC (pacData) { + this.writeScreen.setPAC(pacData); + } + + setBkgData (bkgData) { + this.writeScreen.setBkgData(bkgData); + } + + setMode (newMode) { + if (newMode === this.mode) + return; + + this.mode = newMode; + logger.log('INFO', 'MODE=' + newMode); + if (this.mode === 'MODE_POP-ON') { + this.writeScreen = this.nonDisplayedMemory; + } else { + this.writeScreen = this.displayedMemory; + this.writeScreen.reset(); + } + if (this.mode !== 'MODE_ROLL-UP') { + this.displayedMemory.nrRollUpRows = null; + this.nonDisplayedMemory.nrRollUpRows = null; + } + this.mode = newMode; + } + + insertChars (chars) { + for (let i = 0; i < chars.length; i++) + this.writeScreen.insertChar(chars[i]); + + let screen = this.writeScreen === this.displayedMemory ? 'DISP' : 'NON_DISP'; + logger.log('INFO', screen + ': ' + this.writeScreen.getDisplayText(true)); + if (this.mode === 'MODE_PAINT-ON' || this.mode === 'MODE_ROLL-UP') { + logger.log('TEXT', 'DISPLAYED: ' + this.displayedMemory.getDisplayText(true)); + this.outputDataUpdate(); + } + } + + ccRCL () { // Resume Caption Loading (switch mode to Pop On) + logger.log('INFO', 'RCL - Resume Caption Loading'); + this.setMode('MODE_POP-ON'); + } + + ccBS () { // BackSpace + logger.log('INFO', 'BS - BackSpace'); + if (this.mode === 'MODE_TEXT') + return; + + this.writeScreen.backSpace(); + if (this.writeScreen === this.displayedMemory) + this.outputDataUpdate(); + } + + ccAOF () { // Reserved (formerly Alarm Off) + + } + + ccAON () { // Reserved (formerly Alarm On) + + } + + ccDER () { // Delete to End of Row + logger.log('INFO', 'DER- Delete to End of Row'); + this.writeScreen.clearToEndOfRow(); + this.outputDataUpdate(); + } + + ccRU (nrRows) { // Roll-Up Captions-2,3,or 4 Rows + logger.log('INFO', 'RU(' + nrRows + ') - Roll Up'); + this.writeScreen = this.displayedMemory; + this.setMode('MODE_ROLL-UP'); + this.writeScreen.setRollUpRows(nrRows); + } + + ccFON () { // Flash On + logger.log('INFO', 'FON - Flash On'); + this.writeScreen.setPen({ flash: true }); + } + + ccRDC () { // Resume Direct Captioning (switch mode to PaintOn) + logger.log('INFO', 'RDC - Resume Direct Captioning'); + this.setMode('MODE_PAINT-ON'); + } + + ccTR () { // Text Restart in text mode (not supported, however) + logger.log('INFO', 'TR'); + this.setMode('MODE_TEXT'); + } + + ccRTD () { // Resume Text Display in Text mode (not supported, however) + logger.log('INFO', 'RTD'); + this.setMode('MODE_TEXT'); + } + + ccEDM () { // Erase Displayed Memory + logger.log('INFO', 'EDM - Erase Displayed Memory'); + this.displayedMemory.reset(); + this.outputDataUpdate(true); + } + + ccCR () { // Carriage Return + logger.log('CR - Carriage Return'); + this.writeScreen.rollUp(); + this.outputDataUpdate(true); + } + + ccENM () { // Erase Non-Displayed Memory + logger.log('INFO', 'ENM - Erase Non-displayed Memory'); + this.nonDisplayedMemory.reset(); + } + + ccEOC () { // End of Caption (Flip Memories) + logger.log('INFO', 'EOC - End Of Caption'); + if (this.mode === 'MODE_POP-ON') { + let tmp = this.displayedMemory; + this.displayedMemory = this.nonDisplayedMemory; + this.nonDisplayedMemory = tmp; + this.writeScreen = this.nonDisplayedMemory; + logger.log('TEXT', 'DISP: ' + this.displayedMemory.getDisplayText()); + } + this.outputDataUpdate(true); + } + + ccTO (nrCols) { // Tab Offset 1,2, or 3 columns + logger.log('INFO', 'TO(' + nrCols + ') - Tab Offset'); + this.writeScreen.moveCursor(nrCols); + } + + ccMIDROW (secondByte) { // Parse MIDROW command + let styles = { flash: false }; + styles.underline = secondByte % 2 === 1; + styles.italics = secondByte >= 0x2e; + if (!styles.italics) { + let colorIndex = Math.floor(secondByte / 2) - 0x10; + let colors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta']; + styles.foreground = colors[colorIndex]; + } else { + styles.foreground = 'white'; + } + logger.log('INFO', 'MIDROW: ' + JSON.stringify(styles)); + this.writeScreen.setPen(styles); + } + + outputDataUpdate (dispatch = false) { + let t = logger.time; + if (t === null) + return; + + if (this.outputFilter) { + if (this.cueStartTime === null && !this.displayedMemory.isEmpty()) { // Start of a new cue + this.cueStartTime = t; + } else { + if (!this.displayedMemory.equals(this.lastOutputScreen)) { + if (this.outputFilter.newCue) { + this.outputFilter.newCue(this.cueStartTime, t, this.lastOutputScreen); + if (dispatch === true && this.outputFilter.dispatchCue) + this.outputFilter.dispatchCue(); + } + this.cueStartTime = this.displayedMemory.isEmpty() ? null : t; } - logger.log('INFO', 'MIDROW: ' + JSON.stringify(styles)); - this.writeScreen.setPen(styles); + } + this.lastOutputScreen.copy(this.displayedMemory); } + } - outputDataUpdate(dispatch = false) { - let t = logger.time; - if (t === null) { - return; - } - if (this.outputFilter) { - if (this.cueStartTime === null && !this.displayedMemory.isEmpty()) { // Start of a new cue - this.cueStartTime = t; - } else { - if (!this.displayedMemory.equals(this.lastOutputScreen)) { - if (this.outputFilter.newCue) { - this.outputFilter.newCue(this.cueStartTime, t, this.lastOutputScreen); - if (dispatch === true && this.outputFilter.dispatchCue) { - this.outputFilter.dispatchCue(); - } - } - this.cueStartTime = this.displayedMemory.isEmpty() ? null : t; - } - } - this.lastOutputScreen.copy(this.displayedMemory); - } - } + cueSplitAtTime (t) { + if (this.outputFilter) { + if (!this.displayedMemory.isEmpty()) { + if (this.outputFilter.newCue) + this.outputFilter.newCue(this.cueStartTime, t, this.displayedMemory); - cueSplitAtTime(t) { - if (this.outputFilter) { - if (!this.displayedMemory.isEmpty()) { - if (this.outputFilter.newCue) { - this.outputFilter.newCue(this.cueStartTime, t, this.displayedMemory); - } - this.cueStartTime = t; - } - } + this.cueStartTime = t; + } } + } } class Cea608Parser { - - constructor(field, out1, out2) { - this.field = field || 1; - this.outputs = [out1, out2]; - this.channels = [new Cea608Channel(1, out1), new Cea608Channel(2, out2)]; - this.currChNr = -1; // Will be 1 or 2 - this.lastCmdA = null; // First byte of last command - this.lastCmdB = null; // Second byte of last command - this.bufferedData = []; - this.startTime = null; - this.lastTime = null; - this.dataCounters = {'padding' : 0, 'char' : 0, 'cmd' : 0, 'other' : 0}; - } - - getHandler(index) { - return this.channels[index].getHandler(); - } - - setHandler(index, newHandler) { - this.channels[index].setHandler(newHandler); - } - - /** + constructor (field, out1, out2) { + this.field = field || 1; + this.outputs = [out1, out2]; + this.channels = [new Cea608Channel(1, out1), new Cea608Channel(2, out2)]; + this.currChNr = -1; // Will be 1 or 2 + this.lastCmdA = null; // First byte of last command + this.lastCmdB = null; // Second byte of last command + this.bufferedData = []; + this.startTime = null; + this.lastTime = null; + this.dataCounters = { 'padding': 0, 'char': 0, 'cmd': 0, 'other': 0 }; + } + + getHandler (index) { + return this.channels[index].getHandler(); + } + + setHandler (index, newHandler) { + this.channels[index].setHandler(newHandler); + } + + /** * Add data for time t in forms of list of bytes (unsigned ints). The bytes are treated as pairs. */ - addData(t, byteList) { - var cmdFound, a, b, - charsFound = false; - - this.lastTime = t; - logger.setTime(t); - - for (var i = 0 ; i < byteList.length ; i+=2) { - a = byteList[i] & 0x7f; - b = byteList[i+1] & 0x7f; - if (a === 0 && b === 0) { - this.dataCounters.padding += 2; - continue; - } else { - logger.log('DATA', '[' + numArrayToHexArray([byteList[i], byteList[i+1]]) +'] -> (' + numArrayToHexArray([a, b]) + ')'); - } - cmdFound = this.parseCmd(a, b); - if (!cmdFound) { - cmdFound = this.parseMidrow(a, b); - } - if (!cmdFound) { - cmdFound = this.parsePAC(a, b); - } - if (!cmdFound) { - cmdFound = this.parseBackgroundAttributes(a, b); - } - if (!cmdFound) { - charsFound = this.parseChars(a, b); - if (charsFound) { - if (this.currChNr && this.currChNr >=0) { - var channel = this.channels[this.currChNr-1]; - channel.insertChars(charsFound); - } else { - logger.log('WARNING', 'No channel found yet. TEXT-MODE?'); - } - } - } - if (cmdFound) { - this.dataCounters.cmd += 2; - } else if (charsFound) { - this.dataCounters.char += 2; - } else { - this.dataCounters.other += 2; - logger.log('WARNING', 'Couldn\'t parse cleaned data ' + numArrayToHexArray([a, b]) + - ' orig: ' + numArrayToHexArray([byteList[i], byteList[i+1]])); - } + addData (t, byteList) { + let cmdFound, a, b, + charsFound = false; + + this.lastTime = t; + logger.setTime(t); + + for (let i = 0; i < byteList.length; i += 2) { + a = byteList[i] & 0x7f; + b = byteList[i + 1] & 0x7f; + if (a === 0 && b === 0) { + this.dataCounters.padding += 2; + continue; + } else { + logger.log('DATA', '[' + numArrayToHexArray([byteList[i], byteList[i + 1]]) + '] -> (' + numArrayToHexArray([a, b]) + ')'); + } + cmdFound = this.parseCmd(a, b); + if (!cmdFound) + cmdFound = this.parseMidrow(a, b); + + if (!cmdFound) + cmdFound = this.parsePAC(a, b); + + if (!cmdFound) + cmdFound = this.parseBackgroundAttributes(a, b); + + if (!cmdFound) { + charsFound = this.parseChars(a, b); + if (charsFound) { + if (this.currChNr && this.currChNr >= 0) { + let channel = this.channels[this.currChNr - 1]; + channel.insertChars(charsFound); + } else { + logger.log('WARNING', 'No channel found yet. TEXT-MODE?'); + } } - } - - /** + } + if (cmdFound) { + this.dataCounters.cmd += 2; + } else if (charsFound) { + this.dataCounters.char += 2; + } else { + this.dataCounters.other += 2; + logger.log('WARNING', 'Couldn\'t parse cleaned data ' + numArrayToHexArray([a, b]) + + ' orig: ' + numArrayToHexArray([byteList[i], byteList[i + 1]])); + } + } + } + + /** * Parse Command. * @returns {Boolean} Tells if a command was found */ - parseCmd(a, b) { - var chNr = null; - - var cond1 = (a === 0x14 || a === 0x1C) && (0x20 <= b && b <= 0x2F); - var cond2 = (a === 0x17 || a === 0x1F) && (0x21 <= b && b <= 0x23); - if (!(cond1 || cond2)) { - return false; - } - - if (a === this.lastCmdA && b === this.lastCmdB) { - this.lastCmdA = null; - this.lastCmdB = null; // Repeated commands are dropped (once) - logger.log('DEBUG', 'Repeated command (' + numArrayToHexArray([a, b]) + ') is dropped'); - return true; - } - - if (a === 0x14 || a === 0x17) { - chNr = 1; - } else { - chNr = 2; // (a === 0x1C || a=== 0x1f) - } - - var channel = this.channels[chNr - 1]; - - if (a === 0x14 || a === 0x1C) { - if (b === 0x20) { - channel.ccRCL(); - } else if (b === 0x21) { - channel.ccBS(); - } else if (b === 0x22) { - channel.ccAOF(); - } else if (b === 0x23) { - channel.ccAON(); - } else if (b === 0x24) { - channel.ccDER(); - } else if (b === 0x25) { - channel.ccRU(2); - } else if (b === 0x26) { - channel.ccRU(3); - } else if (b === 0x27) { - channel.ccRU(4); - } else if (b === 0x28) { - channel.ccFON(); - } else if (b === 0x29) { - channel.ccRDC(); - } else if (b === 0x2A) { - channel.ccTR(); - } else if (b === 0x2B) { - channel.ccRTD(); - } else if (b === 0x2C) { - channel.ccEDM(); - } else if (b === 0x2D) { - channel.ccCR(); - } else if (b === 0x2E) { - channel.ccENM(); - } else if (b === 0x2F) { - channel.ccEOC(); - } - } else { //a == 0x17 || a == 0x1F - channel.ccTO(b - 0x20); - } - this.lastCmdA = a; - this.lastCmdB = b; - this.currChNr = chNr; - return true; - } - - /** + parseCmd (a, b) { + let chNr = null; + + let cond1 = (a === 0x14 || a === 0x1C) && (b >= 0x20 && b <= 0x2F); + let cond2 = (a === 0x17 || a === 0x1F) && (b >= 0x21 && b <= 0x23); + if (!(cond1 || cond2)) + return false; + + if (a === this.lastCmdA && b === this.lastCmdB) { + this.lastCmdA = null; + this.lastCmdB = null; // Repeated commands are dropped (once) + logger.log('DEBUG', 'Repeated command (' + numArrayToHexArray([a, b]) + ') is dropped'); + return true; + } + + if (a === 0x14 || a === 0x17) + chNr = 1; + else + chNr = 2; // (a === 0x1C || a=== 0x1f) + + let channel = this.channels[chNr - 1]; + + if (a === 0x14 || a === 0x1C) { + if (b === 0x20) + channel.ccRCL(); + else if (b === 0x21) + channel.ccBS(); + else if (b === 0x22) + channel.ccAOF(); + else if (b === 0x23) + channel.ccAON(); + else if (b === 0x24) + channel.ccDER(); + else if (b === 0x25) + channel.ccRU(2); + else if (b === 0x26) + channel.ccRU(3); + else if (b === 0x27) + channel.ccRU(4); + else if (b === 0x28) + channel.ccFON(); + else if (b === 0x29) + channel.ccRDC(); + else if (b === 0x2A) + channel.ccTR(); + else if (b === 0x2B) + channel.ccRTD(); + else if (b === 0x2C) + channel.ccEDM(); + else if (b === 0x2D) + channel.ccCR(); + else if (b === 0x2E) + channel.ccENM(); + else if (b === 0x2F) + channel.ccEOC(); + } else { // a == 0x17 || a == 0x1F + channel.ccTO(b - 0x20); + } + this.lastCmdA = a; + this.lastCmdB = b; + this.currChNr = chNr; + return true; + } + + /** * Parse midrow styling command * @returns {Boolean} */ - parseMidrow(a, b) { - var chNr = null; - - if ( ((a === 0x11) || (a === 0x19)) && 0x20 <= b && b <= 0x2f) { - if (a === 0x11) { - chNr = 1; - } else { - chNr = 2; - } - if (chNr !== this.currChNr) { - logger.log('ERROR', 'Mismatch channel in midrow parsing'); - return false; - } - var channel = this.channels[chNr-1]; - channel.ccMIDROW(b); - logger.log('DEBUG', 'MIDROW (' + numArrayToHexArray([a, b]) + ')'); - return true; - } + parseMidrow (a, b) { + let chNr = null; + + if (((a === 0x11) || (a === 0x19)) && b >= 0x20 && b <= 0x2f) { + if (a === 0x11) + chNr = 1; + else + chNr = 2; + + if (chNr !== this.currChNr) { + logger.log('ERROR', 'Mismatch channel in midrow parsing'); return false; - } - /** + } + let channel = this.channels[chNr - 1]; + channel.ccMIDROW(b); + logger.log('DEBUG', 'MIDROW (' + numArrayToHexArray([a, b]) + ')'); + return true; + } + return false; + } + /** * Parse Preable Access Codes (Table 53). * @returns {Boolean} Tells if PAC found */ - parsePAC(a, b) { - - var chNr = null; - var row = null; - - var case1 = ((0x11 <= a && a <= 0x17) || (0x19 <= a && a <= 0x1F)) && (0x40 <= b && b <= 0x7F); - var case2 = (a === 0x10 || a === 0x18) && (0x40 <= b && b <= 0x5F); - if (! (case1 || case2)) { - return false; - } - - if (a === this.lastCmdA && b === this.lastCmdB) { - this.lastCmdA = null; - this.lastCmdB = null; - return true; // Repeated commands are dropped (once) - } - - chNr = (a <= 0x17) ? 1 : 2; - - if (0x40 <= b && b <= 0x5F) { - row = (chNr === 1) ? rowsLowCh1[a] : rowsLowCh2[a]; - } else { // 0x60 <= b <= 0x7F - row = (chNr === 1) ? rowsHighCh1[a] : rowsHighCh2[a]; - } - var pacData = this.interpretPAC(row, b); - var channel = this.channels[chNr-1]; - channel.setPAC(pacData); - this.lastCmdA = a; - this.lastCmdB = b; - this.currChNr = chNr; - return true; - } - - /** + parsePAC (a, b) { + let chNr = null; + let row = null; + + let case1 = ((a >= 0x11 && a <= 0x17) || (a >= 0x19 && a <= 0x1F)) && (b >= 0x40 && b <= 0x7F); + let case2 = (a === 0x10 || a === 0x18) && (b >= 0x40 && b <= 0x5F); + if (!(case1 || case2)) + return false; + + if (a === this.lastCmdA && b === this.lastCmdB) { + this.lastCmdA = null; + this.lastCmdB = null; + return true; // Repeated commands are dropped (once) + } + + chNr = (a <= 0x17) ? 1 : 2; + + if (b >= 0x40 && b <= 0x5F) { + row = (chNr === 1) ? rowsLowCh1[a] : rowsLowCh2[a]; + } else { // 0x60 <= b <= 0x7F + row = (chNr === 1) ? rowsHighCh1[a] : rowsHighCh2[a]; + } + let pacData = this.interpretPAC(row, b); + let channel = this.channels[chNr - 1]; + channel.setPAC(pacData); + this.lastCmdA = a; + this.lastCmdB = b; + this.currChNr = chNr; + return true; + } + + /** * Interpret the second byte of the pac, and return the information. * @returns {Object} pacData with style parameters. */ - interpretPAC(row, byte) { - var pacIndex = byte; - var pacData = {color : null, italics : false, indent : null, underline : false, row : row}; - - if (byte > 0x5F) { - pacIndex = byte - 0x60; - } else { - pacIndex = byte - 0x40; - } - pacData.underline = (pacIndex & 1) === 1; - if (pacIndex <= 0xd) { - pacData.color = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'white'][Math.floor(pacIndex/2)]; - } else if (pacIndex <= 0xf) { - pacData.italics = true; - pacData.color = 'white'; - } else { - pacData.indent = (Math.floor((pacIndex-0x10)/2))*4; - } - return pacData; // Note that row has zero offset. The spec uses 1. - } - - /** + interpretPAC (row, byte) { + let pacIndex = byte; + let pacData = { color: null, italics: false, indent: null, underline: false, row: row }; + + if (byte > 0x5F) + pacIndex = byte - 0x60; + else + pacIndex = byte - 0x40; + + pacData.underline = (pacIndex & 1) === 1; + if (pacIndex <= 0xd) { + pacData.color = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'white'][Math.floor(pacIndex / 2)]; + } else if (pacIndex <= 0xf) { + pacData.italics = true; + pacData.color = 'white'; + } else { + pacData.indent = (Math.floor((pacIndex - 0x10) / 2)) * 4; + } + return pacData; // Note that row has zero offset. The spec uses 1. + } + + /** * Parse characters. * @returns An array with 1 to 2 codes corresponding to chars, if found. null otherwise. */ - parseChars(a, b) { - - var channelNr = null, - charCodes = null, - charCode1 = null; - - if (a >= 0x19) { - channelNr = 2; - charCode1 = a - 8; - } else { - channelNr = 1; - charCode1 = a; - } - if (0x11 <= charCode1 && charCode1 <= 0x13) { - // Special character - var oneCode = b; - if (charCode1 === 0x11) { - oneCode = b + 0x50; - } else if (charCode1 === 0x12) { - oneCode = b + 0x70; - } else { - oneCode = b + 0x90; - } - logger.log('INFO', 'Special char \'' + getCharForByte(oneCode) + '\' in channel ' + channelNr); - charCodes = [oneCode]; - } else if (0x20 <= a && a <= 0x7f) { - charCodes = (b === 0) ? [a] : [a, b]; - } - if (charCodes) { - var hexCodes = numArrayToHexArray(charCodes); - logger.log('DEBUG', 'Char codes = ' + hexCodes.join(',')); - this.lastCmdA = null; - this.lastCmdB = null; - } - return charCodes; - } - - /** + parseChars (a, b) { + let channelNr = null, + charCodes = null, + charCode1 = null; + + if (a >= 0x19) { + channelNr = 2; + charCode1 = a - 8; + } else { + channelNr = 1; + charCode1 = a; + } + if (charCode1 >= 0x11 && charCode1 <= 0x13) { + // Special character + let oneCode = b; + if (charCode1 === 0x11) + oneCode = b + 0x50; + else if (charCode1 === 0x12) + oneCode = b + 0x70; + else + oneCode = b + 0x90; + + logger.log('INFO', 'Special char \'' + getCharForByte(oneCode) + '\' in channel ' + channelNr); + charCodes = [oneCode]; + } else if (a >= 0x20 && a <= 0x7f) { + charCodes = (b === 0) ? [a] : [a, b]; + } + if (charCodes) { + let hexCodes = numArrayToHexArray(charCodes); + logger.log('DEBUG', 'Char codes = ' + hexCodes.join(',')); + this.lastCmdA = null; + this.lastCmdB = null; + } + return charCodes; + } + + /** * Parse extended background attributes as well as new foreground color black. * @returns{Boolean} Tells if background attributes are found */ - parseBackgroundAttributes(a, b) { - var bkgData, - index, - chNr, - channel; - - var case1 = (a === 0x10 || a === 0x18) && (0x20 <= b && b <= 0x2f); - var case2 = (a === 0x17 || a === 0x1f) && (0x2d <=b && b <= 0x2f); - if (!(case1 || case2)) { - return false; - } - bkgData = {}; - if (a === 0x10 || a === 0x18) { - index = Math.floor((b-0x20)/2); - bkgData.background = backgroundColors[index]; - if (b % 2 === 1) { - bkgData.background = bkgData.background + '_semi'; - } - } else if (b === 0x2d) { - bkgData.background = 'transparent'; - } else { - bkgData.foreground = 'black'; - if (b === 0x2f) { - bkgData.underline = true; - } - } - chNr = (a < 0x18) ? 1 : 2; - channel = this.channels[chNr-1]; - channel.setBkgData(bkgData); - this.lastCmdA = null; - this.lastCmdB = null; - return true; - } - - /** + parseBackgroundAttributes (a, b) { + let bkgData, + index, + chNr, + channel; + + let case1 = (a === 0x10 || a === 0x18) && (b >= 0x20 && b <= 0x2f); + let case2 = (a === 0x17 || a === 0x1f) && (b >= 0x2d && b <= 0x2f); + if (!(case1 || case2)) + return false; + + bkgData = {}; + if (a === 0x10 || a === 0x18) { + index = Math.floor((b - 0x20) / 2); + bkgData.background = backgroundColors[index]; + if (b % 2 === 1) + bkgData.background = bkgData.background + '_semi'; + } else if (b === 0x2d) { + bkgData.background = 'transparent'; + } else { + bkgData.foreground = 'black'; + if (b === 0x2f) + bkgData.underline = true; + } + chNr = (a < 0x18) ? 1 : 2; + channel = this.channels[chNr - 1]; + channel.setBkgData(bkgData); + this.lastCmdA = null; + this.lastCmdB = null; + return true; + } + + /** * Reset state of parser and its channels. */ - reset() { - for (var i=0 ; i < this.channels.length ; i++) { - if (this.channels[i]) { - this.channels[i].reset(); - } - } - this.lastCmdA = null; - this.lastCmdB = null; + reset () { + for (let i = 0; i < this.channels.length; i++) { + if (this.channels[i]) + this.channels[i].reset(); } + this.lastCmdA = null; + this.lastCmdB = null; + } - /** + /** * Trigger the generation of a cue, and the start of a new one if displayScreens are not empty. */ - cueSplitAtTime(t) { - for (var i=0 ; i < this.channels.length ; i++) { - if (this.channels[i]) { - this.channels[i].cueSplitAtTime(t); - } - } + cueSplitAtTime (t) { + for (let i = 0; i < this.channels.length; i++) { + if (this.channels[i]) + this.channels[i].cueSplitAtTime(t); } + } } export default Cea608Parser; diff --git a/src/utils/codecs.js b/src/utils/codecs.js index 87445430c59..4f620a560b1 100644 --- a/src/utils/codecs.js +++ b/src/utils/codecs.js @@ -1,75 +1,75 @@ // from http://mp4ra.org/codecs.html const sampleEntryCodesISO = { - audio: { - 'a3ds': true, - 'ac-3': true, - 'ac-4': true, - 'alac': true, - 'alaw': true, - 'dra1': true, - 'dts+': true, - 'dts-': true, - 'dtsc': true, - 'dtse': true, - 'dtsh': true, - 'ec-3': true, - 'enca': true, - 'g719': true, - 'g726': true, - 'm4ae': true, - 'mha1': true, - 'mha2': true, - 'mhm1': true, - 'mhm2': true, - 'mlpa': true, - 'mp4a': true, - 'raw ': true, - 'Opus': true, - 'samr': true, - 'sawb': true, - 'sawp': true, - 'sevc': true, - 'sqcp': true, - 'ssmv': true, - 'twos': true, - 'ulaw': true - }, - video: { - 'avc1': true, - 'avc2': true, - 'avc3': true, - 'avc4': true, - 'avcp': true, - 'drac': true, - 'dvav': true, - 'dvhe': true, - 'encv': true, - 'hev1': true, - 'hvc1': true, - 'mjp2': true, - 'mp4v': true, - 'mvc1': true, - 'mvc2': true, - 'mvc3': true, - 'mvc4': true, - 'resv': true, - 'rv60': true, - 's263': true, - 'svc1': true, - 'svc2': true, - 'vc-1': true, - 'vp08': true, - 'vp09': true - } + audio: { + 'a3ds': true, + 'ac-3': true, + 'ac-4': true, + 'alac': true, + 'alaw': true, + 'dra1': true, + 'dts+': true, + 'dts-': true, + 'dtsc': true, + 'dtse': true, + 'dtsh': true, + 'ec-3': true, + 'enca': true, + 'g719': true, + 'g726': true, + 'm4ae': true, + 'mha1': true, + 'mha2': true, + 'mhm1': true, + 'mhm2': true, + 'mlpa': true, + 'mp4a': true, + 'raw ': true, + 'Opus': true, + 'samr': true, + 'sawb': true, + 'sawp': true, + 'sevc': true, + 'sqcp': true, + 'ssmv': true, + 'twos': true, + 'ulaw': true + }, + video: { + 'avc1': true, + 'avc2': true, + 'avc3': true, + 'avc4': true, + 'avcp': true, + 'drac': true, + 'dvav': true, + 'dvhe': true, + 'encv': true, + 'hev1': true, + 'hvc1': true, + 'mjp2': true, + 'mp4v': true, + 'mvc1': true, + 'mvc2': true, + 'mvc3': true, + 'mvc4': true, + 'resv': true, + 'rv60': true, + 's263': true, + 'svc1': true, + 'svc2': true, + 'vc-1': true, + 'vp08': true, + 'vp09': true + } }; -function isCodecType(codec, type) { - const typeCodes = sampleEntryCodesISO[type]; - return !!typeCodes && typeCodes[codec.slice(0, 4)] === true; +function isCodecType (codec, type) { + const typeCodes = sampleEntryCodesISO[type]; + return !!typeCodes && typeCodes[codec.slice(0, 4)] === true; } -function isCodecSupportedInMp4(codec, type) { - return MediaSource.isTypeSupported(`${type || 'video'}/mp4;codecs="${codec}"`); +function isCodecSupportedInMp4 (codec, type) { + return MediaSource.isTypeSupported(`${type || 'video'}/mp4;codecs="${codec}"`); } export { isCodecType, isCodecSupportedInMp4 }; diff --git a/src/utils/cues.js b/src/utils/cues.js index 6f5c17c83b9..5b4e7993628 100644 --- a/src/utils/cues.js +++ b/src/utils/cues.js @@ -1,64 +1,49 @@ import { fixLineBreaks } from './vttparser'; -export function newCue(track, startTime, endTime, captionScreen) { - var row; - var cue; - var indenting; - var indent; - var text; - var VTTCue = window.VTTCue || window.TextTrackCue; - - for (var r=0; r<captionScreen.rows.length; r++) - { +export function newCue (track, startTime, endTime, captionScreen) { + let row; + let cue; + let indenting; + let indent; + let text; + let VTTCue = window.VTTCue || window.TextTrackCue; + + for (let r = 0; r < captionScreen.rows.length; r++) { row = captionScreen.rows[r]; indenting = true; indent = 0; text = ''; - if (!row.isEmpty()) - { - for (var c=0; c<row.chars.length; c++) - { - if (row.chars[c].uchar.match(/\s/) && indenting) - { + if (!row.isEmpty()) { + for (let c = 0; c < row.chars.length; c++) { + if (row.chars[c].uchar.match(/\s/) && indenting) { indent++; - } - else - { + } else { text += row.chars[c].uchar; indenting = false; } } - //To be used for cleaning-up orphaned roll-up captions + // To be used for cleaning-up orphaned roll-up captions row.cueStartTime = startTime; // Give a slight bump to the endTime if it's equal to startTime to avoid a SyntaxError in IE if (startTime === endTime) - { endTime += 0.0001; - } cue = new VTTCue(startTime, endTime, fixLineBreaks(text.trim())); if (indent >= 16) - { indent--; - } else - { indent++; - } // VTTCue.line get's flakey when using controls, so let's now include line 13&14 // also, drop line 1 since it's to close to the top if (navigator.userAgent.match(/Firefox\//)) - { cue.line = r + 1; - } else - { cue.line = (r > 7 ? r - 2 : r + 1); - } + cue.align = 'left'; // Clamp the position between 0 and 100 - if out of these bounds, Firefox throws an exception and captions break cue.position = Math.max(0, Math.min(100, 100 * (indent / 32) + (navigator.userAgent.match(/Firefox\//) ? 50 : 0))); @@ -66,4 +51,3 @@ export function newCue(track, startTime, endTime, captionScreen) { } } } - diff --git a/src/utils/discontinuities.js b/src/utils/discontinuities.js index fefa1ea743a..d6341af428a 100644 --- a/src/utils/discontinuities.js +++ b/src/utils/discontinuities.js @@ -1,7 +1,7 @@ import BinarySearch from './binary-search'; -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; -export function findFirstFragWithCC(fragments, cc) { +export function findFirstFragWithCC (fragments, cc) { let firstFrag = null; for (let i = 0; i < fragments.length; i += 1) { @@ -15,30 +15,28 @@ export function findFirstFragWithCC(fragments, cc) { return firstFrag; } -export function findFragWithCC(fragments, CC) { +export function findFragWithCC (fragments, CC) { return BinarySearch.search(fragments, (candidate) => { - if (candidate.cc < CC) { + if (candidate.cc < CC) return 1; - } else if (candidate.cc > CC) { + else if (candidate.cc > CC) return -1; - } else { + else return 0; - } }); } -export function shouldAlignOnDiscontinuities(lastFrag, lastLevel, details) { +export function shouldAlignOnDiscontinuities (lastFrag, lastLevel, details) { let shouldAlign = false; if (lastLevel && lastLevel.details && details) { - if (details.endCC > details.startCC || (lastFrag && lastFrag.cc < details.startCC)) { + if (details.endCC > details.startCC || (lastFrag && lastFrag.cc < details.startCC)) shouldAlign = true; - } } return shouldAlign; } // Find the first frag in the previous level which matches the CC of the first frag of the new level -export function findDiscontinuousReferenceFrag(prevDetails, curDetails) { +export function findDiscontinuousReferenceFrag (prevDetails, curDetails) { const prevFrags = prevDetails.fragments; const curFrags = curDetails.fragments; @@ -57,7 +55,7 @@ export function findDiscontinuousReferenceFrag(prevDetails, curDetails) { return prevStartFrag; } -export function adjustPts(sliding, details) { +export function adjustPts (sliding, details) { details.fragments.forEach((frag) => { if (frag) { let start = frag.start + sliding; @@ -71,7 +69,7 @@ export function adjustPts(sliding, details) { // If a change in CC is detected, the PTS can no longer be relied upon // Attempt to align the level by using the last level - find the last frag matching the current CC and use it's PTS // as a reference -export function alignDiscontinuities(lastFrag, lastLevel, details) { +export function alignDiscontinuities (lastFrag, lastLevel, details) { if (shouldAlignOnDiscontinuities(lastFrag, lastLevel, details)) { const referenceFrag = findDiscontinuousReferenceFrag(lastLevel.details, details); if (referenceFrag) { @@ -87,10 +85,10 @@ export function alignDiscontinuities(lastFrag, lastLevel, details) { let lastPDT = lastLevel.details.programDateTime; let newPDT = details.programDateTime; // date diff is in ms. frag.start is in seconds - let sliding = (newPDT - lastPDT)/1000 + lastLevel.details.fragments[0].start; + let sliding = (newPDT - lastPDT) / 1000 + lastLevel.details.fragments[0].start; if (!isNaN(sliding)) { logger.log(`adjusting PTS using programDateTime delta, sliding:${sliding.toFixed(3)}`); - adjustPts(sliding,details); + adjustPts(sliding, details); } } } diff --git a/src/utils/ewma-bandwidth-estimator.js b/src/utils/ewma-bandwidth-estimator.js index ef5076010bc..0f20f16e50a 100644 --- a/src/utils/ewma-bandwidth-estimator.js +++ b/src/utils/ewma-bandwidth-estimator.js @@ -8,10 +8,8 @@ import EWMA from '../utils/ewma'; - class EwmaBandWidthEstimator { - - constructor(hls,slow,fast,defaultEstimate) { + constructor (hls, slow, fast, defaultEstimate) { this.hls = hls; this.defaultEstimate_ = defaultEstimate; this.minWeight_ = 0.001; @@ -20,36 +18,34 @@ class EwmaBandWidthEstimator { this.fast_ = new EWMA(fast); } - sample(durationMs,numBytes) { + sample (durationMs, numBytes) { durationMs = Math.max(durationMs, this.minDelayMs_); - var bandwidth = 8000* numBytes / durationMs, - //console.log('instant bw:'+ Math.round(bandwidth)); - // we weight sample using loading duration.... - weight = durationMs / 1000; - this.fast_.sample(weight,bandwidth); - this.slow_.sample(weight,bandwidth); + let bandwidth = 8000 * numBytes / durationMs, + // console.log('instant bw:'+ Math.round(bandwidth)); + // we weight sample using loading duration.... + weight = durationMs / 1000; + this.fast_.sample(weight, bandwidth); + this.slow_.sample(weight, bandwidth); } - canEstimate() { + canEstimate () { let fast = this.fast_; return (fast && fast.getTotalWeight() >= this.minWeight_); } - - getEstimate() { + getEstimate () { if (this.canEstimate()) { - //console.log('slow estimate:'+ Math.round(this.slow_.getEstimate())); - //console.log('fast estimate:'+ Math.round(this.fast_.getEstimate())); + // console.log('slow estimate:'+ Math.round(this.slow_.getEstimate())); + // console.log('fast estimate:'+ Math.round(this.fast_.getEstimate())); // Take the minimum of these two estimates. This should have the effect of // adapting down quickly, but up more slowly. - return Math.min(this.fast_.getEstimate(),this.slow_.getEstimate()); + return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate()); } else { return this.defaultEstimate_; } } - destroy() { + destroy () { } } export default EwmaBandWidthEstimator; - diff --git a/src/utils/ewma.js b/src/utils/ewma.js index e54ce7ccc4a..063537ac398 100644 --- a/src/utils/ewma.js +++ b/src/utils/ewma.js @@ -5,28 +5,27 @@ */ class EWMA { - - // About half of the estimated value will be from the last |halfLife| samples by weight. - constructor(halfLife) { + // About half of the estimated value will be from the last |halfLife| samples by weight. + constructor (halfLife) { // Larger values of alpha expire historical data more slowly. this.alpha_ = halfLife ? Math.exp(Math.log(0.5) / halfLife) : 0; this.estimate_ = 0; this.totalWeight_ = 0; } - sample(weight,value) { - var adjAlpha = Math.pow(this.alpha_, weight); + sample (weight, value) { + let adjAlpha = Math.pow(this.alpha_, weight); this.estimate_ = value * (1 - adjAlpha) + adjAlpha * this.estimate_; this.totalWeight_ += weight; } - getTotalWeight() { + getTotalWeight () { return this.totalWeight_; } - getEstimate() { + getEstimate () { if (this.alpha_) { - var zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_); + let zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_); return this.estimate_ / zeroFactor; } else { return this.estimate_; diff --git a/src/utils/fetch-loader.js b/src/utils/fetch-loader.js index 76d182ce6c5..34991ec1743 100644 --- a/src/utils/fetch-loader.js +++ b/src/utils/fetch-loader.js @@ -6,16 +6,15 @@ */ class FetchLoader { - - constructor(config) { + constructor (config) { this.fetchSetup = config.fetchSetup; } - destroy() {} + destroy () {} - abort() {} + abort () {} - load(context, config, callbacks) { + load (context, config, callbacks) { let stats = { trequest: performance.now(), retry: 0 @@ -25,58 +24,53 @@ class FetchLoader { let request; const initParams = { - method: 'GET', - mode: 'cors', - credentials: 'same-origin' - }; + method: 'GET', + mode: 'cors', + credentials: 'same-origin' + }; const headersObj = {}; - if (context.rangeEnd) { + if (context.rangeEnd) headersObj['Range'] = 'bytes=' + context.rangeStart + '-' + String(context.rangeEnd - 1); /* jshint ignore:line */ - } initParams.headers = new Headers(headersObj); - if (this.fetchSetup) { - request = this.fetchSetup(context,initParams); - } else { - request = new Request(context.url,initParams); - } + if (this.fetchSetup) + request = this.fetchSetup(context, initParams); + else + request = new Request(context.url, initParams); - let fetchPromise = fetch(request,initParams); + let fetchPromise = fetch(request, initParams); // process fetchPromise - let responsePromise = fetchPromise.then(function(response) { + let responsePromise = fetchPromise.then(function (response) { if (response.ok) { - stats.tfirst = Math.max(stats.trequest,performance.now()); + stats.tfirst = Math.max(stats.trequest, performance.now()); targetURL = response.url; - if (context.responseType === 'arraybuffer') { + if (context.responseType === 'arraybuffer') return response.arrayBuffer(); - } else { + else return response.text(); - } } else { - callbacks.onError({text : 'fetch, bad network response'}, context); - return; + callbacks.onError({ text: 'fetch, bad network response' }, context); } - }).catch(function(error) { - callbacks.onError({text : error.message}, context); - return; + }).catch(function (error) { + callbacks.onError({ text: error.message }, context); }); // process response Promise - responsePromise.then(function(responseData) { + responsePromise.then(function (responseData) { if (responseData) { - stats.tload = Math.max(stats.tfirst,performance.now()); + stats.tload = Math.max(stats.tfirst, performance.now()); let len; - if (typeof responseData === 'string') { + if (typeof responseData === 'string') len = responseData.length; - } else { + else len = responseData.byteLength; - } + stats.loaded = stats.total = len; - let response = { url : targetURL, data : responseData}; - callbacks.onSuccess(response,stats,context); + let response = { url: targetURL, data: responseData }; + callbacks.onSuccess(response, stats, context); } }); } diff --git a/src/utils/hex.js b/src/utils/hex.js index d5247adb8a3..4f804908e6b 100644 --- a/src/utils/hex.js +++ b/src/utils/hex.js @@ -3,13 +3,13 @@ */ const Hex = { - hexDump : function(array) { - var i, str = ''; - for(i = 0; i < array.length; i++) { - var h = array[i].toString(16); - if (h.length < 2) { + hexDump: function (array) { + let i, str = ''; + for (i = 0; i < array.length; i++) { + let h = array[i].toString(16); + if (h.length < 2) h = '0' + h; - } + str += h; } return str; diff --git a/src/utils/logger.js b/src/utils/logger.js index f3a1ea600be..1e1d6867199 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -1,4 +1,4 @@ -function noop() {} +function noop () {} const fakeLogger = { trace: noop, @@ -11,9 +11,9 @@ const fakeLogger = { let exportedLogger = fakeLogger; -/*globals self: false */ +/* globals self: false */ -//let lastCallTime; +// let lastCallTime; // function formatMsgWithTimeInfo(type, msg) { // const now = Date.now(); // const diff = lastCallTime ? '+' + (now - lastCallTime) : '0'; @@ -22,35 +22,35 @@ let exportedLogger = fakeLogger; // return msg; // } -function formatMsg(type, msg) { - msg = '[' + type + '] > ' + msg; +function formatMsg (type, msg) { + msg = '[' + type + '] > ' + msg; return msg; } -function consolePrintFn(type) { +function consolePrintFn (type) { const func = self.console[type]; if (func) { - return function(...args) { - if(args[0]) { + return function (...args) { + if (args[0]) args[0] = formatMsg(type, args[0]); - } + func.apply(self.console, args); }; } return noop; } -function exportLoggerFunctions(debugConfig, ...functions) { - functions.forEach(function(type) { +function exportLoggerFunctions (debugConfig, ...functions) { + functions.forEach(function (type) { exportedLogger[type] = debugConfig[type] ? debugConfig[type].bind(debugConfig) : consolePrintFn(type); }); } -export var enableLogs = function(debugConfig) { +export var enableLogs = function (debugConfig) { if (debugConfig === true || typeof debugConfig === 'object') { exportLoggerFunctions(debugConfig, // Remove out from list here to hard-disable a log-level - //'trace', + // 'trace', 'debug', 'log', 'info', @@ -60,12 +60,11 @@ export var enableLogs = function(debugConfig) { // Some browsers don't allow to use bind on console object anyway // fallback to default if needed try { - exportedLogger.log(); + exportedLogger.log(); } catch (e) { exportedLogger = fakeLogger; } - } - else { + } else { exportedLogger = fakeLogger; } }; diff --git a/src/utils/output-filter.js b/src/utils/output-filter.js index c194c1c5cfe..b22d816e547 100644 --- a/src/utils/output-filter.js +++ b/src/utils/output-filter.js @@ -1,6 +1,5 @@ export default class OutputFilter { - - constructor(timelineController, track) { + constructor (timelineController, track) { this.timelineController = timelineController; this.track = track; this.startTime = null; @@ -8,18 +7,18 @@ export default class OutputFilter { this.screen = null; } - dispatchCue() { - if (this.startTime === null) { + dispatchCue () { + if (this.startTime === null) return; - } + this.timelineController.addCues('textTrack' + this.track, this.startTime, this.endTime, this.screen); this.startTime = null; } - newCue(startTime, endTime, screen) { - if (this.startTime === null || this.startTime > startTime) { + newCue (startTime, endTime, screen) { + if (this.startTime === null || this.startTime > startTime) this.startTime = startTime; - } + this.endTime = endTime; this.screen = screen; this.timelineController.createCaptionsTrack(this.track); diff --git a/src/utils/time-ranges.js b/src/utils/time-ranges.js index e34a102249e..a79c2ce8ee0 100644 --- a/src/utils/time-ranges.js +++ b/src/utils/time-ranges.js @@ -3,11 +3,11 @@ */ const TimeRanges = { - toString : function(r) { - var log = '', len = r.length; - for (var i=0; i<len; i++) { + toString: function (r) { + let log = '', len = r.length; + for (let i = 0; i < len; i++) log += '[' + r.start(i).toFixed(3) + ',' + r.end(i).toFixed(3) + ']'; - } + return log; } }; diff --git a/src/utils/vttcue.js b/src/utils/vttcue.js index 23c41fb2f5f..12a890bba12 100644 --- a/src/utils/vttcue.js +++ b/src/utils/vttcue.js @@ -14,18 +14,17 @@ * limitations under the License. */ -export default (function() { - if (typeof window !== 'undefined' && window.VTTCue) { +export default (function () { + if (typeof window !== 'undefined' && window.VTTCue) return window.VTTCue; - } - var autoKeyword = 'auto'; - var directionSetting = { + let autoKeyword = 'auto'; + let directionSetting = { '': true, lr: true, rl: true }; - var alignSetting = { + let alignSetting = { start: true, middle: true, end: true, @@ -33,49 +32,47 @@ export default (function() { right: true }; - function findDirectionSetting(value) { - if (typeof value !== 'string') { + function findDirectionSetting (value) { + if (typeof value !== 'string') return false; - } - var dir = directionSetting[value.toLowerCase()]; + + let dir = directionSetting[value.toLowerCase()]; return dir ? value.toLowerCase() : false; } - function findAlignSetting(value) { - if (typeof value !== 'string') { + function findAlignSetting (value) { + if (typeof value !== 'string') return false; - } - var align = alignSetting[value.toLowerCase()]; + + let align = alignSetting[value.toLowerCase()]; return align ? value.toLowerCase() : false; } - function extend(obj) { - var i = 1; + function extend (obj) { + let i = 1; for (; i < arguments.length; i++) { - var cobj = arguments[i]; - for (var p in cobj) { + let cobj = arguments[i]; + for (let p in cobj) obj[p] = cobj[p]; - } } return obj; } - function VTTCue(startTime, endTime, text) { - var cue = this; - var isIE8 = (function () { - if (typeof navigator === 'undefined') { + function VTTCue (startTime, endTime, text) { + let cue = this; + let isIE8 = (function () { + if (typeof navigator === 'undefined') return; - } + return (/MSIE\s8\.0/).test(navigator.userAgent); })(); - var baseObj = {}; + let baseObj = {}; - if (isIE8) { + if (isIE8) cue = document.createElement('custom'); - } else { + else baseObj.enumerable = true; - } /** * Shim implementation specific properties. These properties are not in @@ -92,20 +89,20 @@ export default (function() { * http://dev.w3.org/html5/webvtt/#vttcue-interface */ - var _id = ''; - var _pauseOnExit = false; - var _startTime = startTime; - var _endTime = endTime; - var _text = text; - var _region = null; - var _vertical = ''; - var _snapToLines = true; - var _line = 'auto'; - var _lineAlign = 'start'; - var _position = 50; - var _positionAlign = 'middle'; - var _size = 50; - var _align = 'middle'; + let _id = ''; + let _pauseOnExit = false; + let _startTime = startTime; + let _endTime = endTime; + let _text = text; + let _region = null; + let _vertical = ''; + let _snapToLines = true; + let _line = 'auto'; + let _lineAlign = 'start'; + let _position = 50; + let _positionAlign = 'middle'; + let _size = 50; + let _align = 'middle'; Object.defineProperty(cue, 'id', extend({}, baseObj, { get: function () { @@ -130,9 +127,9 @@ export default (function() { return _startTime; }, set: function (value) { - if (typeof value !== 'number') { + if (typeof value !== 'number') throw new TypeError('Start time must be set to a number.'); - } + _startTime = value; this.hasBeenReset = true; } @@ -143,9 +140,9 @@ export default (function() { return _endTime; }, set: function (value) { - if (typeof value !== 'number') { + if (typeof value !== 'number') throw new TypeError('End time must be set to a number.'); - } + _endTime = value; this.hasBeenReset = true; } @@ -176,11 +173,11 @@ export default (function() { return _vertical; }, set: function (value) { - var setting = findDirectionSetting(value); + let setting = findDirectionSetting(value); // Have to check for false because the setting an be an empty string. - if (setting === false) { + if (setting === false) throw new SyntaxError('An invalid or illegal string was specified.'); - } + _vertical = setting; this.hasBeenReset = true; } @@ -201,9 +198,9 @@ export default (function() { return _line; }, set: function (value) { - if (typeof value !== 'number' && value !== autoKeyword) { + if (typeof value !== 'number' && value !== autoKeyword) throw new SyntaxError('An invalid number or illegal string was specified.'); - } + _line = value; this.hasBeenReset = true; } @@ -214,10 +211,10 @@ export default (function() { return _lineAlign; }, set: function (value) { - var setting = findAlignSetting(value); - if (!setting) { + let setting = findAlignSetting(value); + if (!setting) throw new SyntaxError('An invalid or illegal string was specified.'); - } + _lineAlign = setting; this.hasBeenReset = true; } @@ -228,9 +225,9 @@ export default (function() { return _position; }, set: function (value) { - if (value < 0 || value > 100) { + if (value < 0 || value > 100) throw new Error('Position must be between 0 and 100.'); - } + _position = value; this.hasBeenReset = true; } @@ -241,10 +238,10 @@ export default (function() { return _positionAlign; }, set: function (value) { - var setting = findAlignSetting(value); - if (!setting) { + let setting = findAlignSetting(value); + if (!setting) throw new SyntaxError('An invalid or illegal string was specified.'); - } + _positionAlign = setting; this.hasBeenReset = true; } @@ -255,9 +252,9 @@ export default (function() { return _size; }, set: function (value) { - if (value < 0 || value > 100) { + if (value < 0 || value > 100) throw new Error('Size must be between 0 and 100.'); - } + _size = value; this.hasBeenReset = true; } @@ -268,10 +265,10 @@ export default (function() { return _align; }, set: function (value) { - var setting = findAlignSetting(value); - if (!setting) { + let setting = findAlignSetting(value); + if (!setting) throw new SyntaxError('An invalid or illegal string was specified.'); - } + _align = setting; this.hasBeenReset = true; } @@ -284,9 +281,8 @@ export default (function() { // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-display-state cue.displayState = undefined; - if (isIE8) { + if (isIE8) return cue; - } } /** @@ -295,7 +291,7 @@ export default (function() { VTTCue.prototype.getCueAsHTML = function () { // Assume WebVTT.convertCueToDOMTree is on the global. - var WebVTT = window.WebVTT; + let WebVTT = window.WebVTT; return WebVTT.convertCueToDOMTree(window, this.text); }; diff --git a/src/utils/vttparser.js b/src/utils/vttparser.js index 500fff99786..2743e193db0 100644 --- a/src/utils/vttparser.js +++ b/src/utils/vttparser.js @@ -4,40 +4,37 @@ import VTTCue from './vttcue'; -const StringDecoder = function StringDecoder() { +const StringDecoder = function StringDecoder () { return { - decode: function(data) { - if (!data) { + decode: function (data) { + if (!data) return ''; - } - if (typeof data !== 'string') { + + if (typeof data !== 'string') throw new Error('Error - expected string data.'); - } + return decodeURIComponent(encodeURIComponent(data)); - }, - }; + } }; +}; -function VTTParser() { - this.window = window; - this.state = 'INITIAL'; - this.buffer = ''; - this.decoder = new StringDecoder(); - this.regionList = []; +function VTTParser () { + this.window = window; + this.state = 'INITIAL'; + this.buffer = ''; + this.decoder = new StringDecoder(); + this.regionList = []; } - // Try to parse input as a time stamp. -function parseTimeStamp(input) { - - function computeSeconds(h, m, s, f) { +function parseTimeStamp (input) { + function computeSeconds (h, m, s, f) { return (h | 0) * 3600 + (m | 0) * 60 + (s | 0) + (f | 0) / 1000; } - var m = input.match(/^(\d+):(\d{2})(:\d{2})?\.(\d{3})/); - if (!m) { + let m = input.match(/^(\d+):(\d{2})(:\d{2})?\.(\d{3})/); + if (!m) return null; - } if (m[3]) { // Timestamp takes the form of [hours]:[minutes]:[seconds].[milliseconds] @@ -54,35 +51,34 @@ function parseTimeStamp(input) { // A settings object holds key/value pairs and will ignore anything but the first // assignment to a specific key. -function Settings() { +function Settings () { this.values = Object.create(null); } Settings.prototype = { // Only accept the first assignment to any key. - set: function(k, v) { - if (!this.get(k) && v !== '') { + set: function (k, v) { + if (!this.get(k) && v !== '') this.values[k] = v; - } }, // Return the value for a key, or a default value. // If 'defaultKey' is passed then 'dflt' is assumed to be an object with // a number of possible default values as properties where 'defaultKey' is // the key of the property that will be chosen; otherwise it's assumed to be // a single value. - get: function(k, dflt, defaultKey) { - if (defaultKey) { + get: function (k, dflt, defaultKey) { + if (defaultKey) return this.has(k) ? this.values[k] : dflt[defaultKey]; - } + return this.has(k) ? this.values[k] : dflt; }, // Check whether we have a value for a key. - has: function(k) { + has: function (k) { return k in this.values; }, // Accept a setting if its one of the given alternatives. - alt: function(k, v, a) { - for (var n = 0; n < a.length; ++n) { + alt: function (k, v, a) { + for (let n = 0; n < a.length; ++n) { if (v === a[n]) { this.set(k, v); break; @@ -90,14 +86,14 @@ Settings.prototype = { } }, // Accept a setting if its a valid (signed) integer. - integer: function(k, v) { + integer: function (k, v) { if (/^-?\d+$/.test(v)) { // integer this.set(k, parseInt(v, 10)); } }, // Accept a setting if its a valid percentage. - percent: function(k, v) { - var m; + percent: function (k, v) { + let m; if ((m = v.match(/^([\d]{1,3})(\.[\d]*)?%$/))) { v = parseFloat(v); if (v >= 0 && v <= 100) { @@ -111,91 +107,91 @@ Settings.prototype = { // Helper function to parse input into groups separated by 'groupDelim', and // interprete each group as a key/value pair separated by 'keyValueDelim'. -function parseOptions(input, callback, keyValueDelim, groupDelim) { - var groups = groupDelim ? input.split(groupDelim) : [input]; - for (var i in groups) { - if (typeof groups[i] !== 'string') { +function parseOptions (input, callback, keyValueDelim, groupDelim) { + let groups = groupDelim ? input.split(groupDelim) : [input]; + for (let i in groups) { + if (typeof groups[i] !== 'string') continue; - } - var kv = groups[i].split(keyValueDelim); - if (kv.length !== 2) { + + let kv = groups[i].split(keyValueDelim); + if (kv.length !== 2) continue; - } - var k = kv[0]; - var v = kv[1]; + + let k = kv[0]; + let v = kv[1]; callback(k, v); } } -var defaults = new VTTCue(0, 0, 0); +let defaults = new VTTCue(0, 0, 0); // 'middle' was changed to 'center' in the spec: https://github.com/w3c/webvtt/pull/244 // Safari doesn't yet support this change, but FF and Chrome do. -var center = defaults.align === 'middle' ? 'middle' : 'center'; +let center = defaults.align === 'middle' ? 'middle' : 'center'; -function parseCue(input, cue, regionList) { +function parseCue (input, cue, regionList) { // Remember the original input if we need to throw an error. - var oInput = input; + let oInput = input; // 4.1 WebVTT timestamp - function consumeTimeStamp() { - var ts = parseTimeStamp(input); - if (ts === null) { + function consumeTimeStamp () { + let ts = parseTimeStamp(input); + if (ts === null) throw new Error('Malformed timestamp: ' + oInput); - } + // Remove time stamp from input. input = input.replace(/^[^\sa-zA-Z-]+/, ''); return ts; } // 4.4.2 WebVTT cue settings - function consumeCueSettings(input, cue) { - var settings = new Settings(); + function consumeCueSettings (input, cue) { + let settings = new Settings(); - parseOptions(input, function(k, v) { + parseOptions(input, function (k, v) { switch (k) { - case 'region': - // Find the last region we parsed with the same region id. - for (var i = regionList.length - 1; i >= 0; i--) { - if (regionList[i].id === v) { - settings.set(k, regionList[i].region); - break; - } - } - break; - case 'vertical': - settings.alt(k, v, ['rl', 'lr']); - break; - case 'line': - var vals = v.split(','), - vals0 = vals[0]; - settings.integer(k, vals0); - if (settings.percent(k, vals0)) { - settings.set('snapToLines', false); - } - settings.alt(k, vals0, ['auto']); - if (vals.length === 2) { - settings.alt('lineAlign', vals[1], ['start', center, 'end']); - } - break; - case 'position': - vals = v.split(','); - settings.percent(k, vals[0]); - if (vals.length === 2) { - settings.alt('positionAlign', vals[1], ['start', center, 'end', 'line-left', 'line-right', 'auto']); + case 'region': + // Find the last region we parsed with the same region id. + for (let i = regionList.length - 1; i >= 0; i--) { + if (regionList[i].id === v) { + settings.set(k, regionList[i].region); + break; } - break; - case 'size': - settings.percent(k, v); - break; - case 'align': - settings.alt(k, v, ['start', center, 'end', 'left', 'right']); - break; + } + break; + case 'vertical': + settings.alt(k, v, ['rl', 'lr']); + break; + case 'line': + var vals = v.split(','), + vals0 = vals[0]; + settings.integer(k, vals0); + if (settings.percent(k, vals0)) + settings.set('snapToLines', false); + + settings.alt(k, vals0, ['auto']); + if (vals.length === 2) + settings.alt('lineAlign', vals[1], ['start', center, 'end']); + + break; + case 'position': + vals = v.split(','); + settings.percent(k, vals[0]); + if (vals.length === 2) + settings.alt('positionAlign', vals[1], ['start', center, 'end', 'line-left', 'line-right', 'auto']); + + break; + case 'size': + settings.percent(k, v); + break; + case 'align': + settings.alt(k, v, ['start', center, 'end', 'left', 'right']); + break; } }, /:/, /\s/); // Apply default values for any missing fields. cue.region = settings.get('region', null); cue.vertical = settings.get('vertical', ''); - var line = settings.get('line', 'auto'); + let line = settings.get('line', 'auto'); if (line === 'auto' && defaults.line === -1) { // set numeric line number for Safari line = -1; @@ -205,7 +201,7 @@ function parseCue(input, cue, regionList) { cue.snapToLines = settings.get('snapToLines', true); cue.size = settings.get('size', 100); cue.align = settings.get('align', center); - var position = settings.get('position', 'auto'); + let position = settings.get('position', 'auto'); if (position === 'auto' && defaults.position === 50) { // set numeric position for Safari position = cue.align === 'start' || cue.align === 'left' ? 0 : cue.align === 'end' || cue.align === 'right' ? 100 : 50; @@ -213,191 +209,186 @@ function parseCue(input, cue, regionList) { cue.position = position; } - function skipWhitespace() { + function skipWhitespace () { input = input.replace(/^\s+/, ''); } // 4.1 WebVTT cue timings. skipWhitespace(); - cue.startTime = consumeTimeStamp(); // (1) collect cue start time + cue.startTime = consumeTimeStamp(); // (1) collect cue start time skipWhitespace(); - if (input.substr(0, 3) !== '-->') { // (3) next characters must match '-->' + if (input.substr(0, 3) !== '-->') { // (3) next characters must match '-->' throw new Error('Malformed time stamp (time stamps must be separated by \'-->\'): ' + oInput); } input = input.substr(3); skipWhitespace(); - cue.endTime = consumeTimeStamp(); // (5) collect cue end time + cue.endTime = consumeTimeStamp(); // (5) collect cue end time // 4.1 WebVTT cue settings list. skipWhitespace(); consumeCueSettings(input, cue); } -function fixLineBreaks(input) { +function fixLineBreaks (input) { return input.replace(/<br(?: \/)?>/gi, '\n'); } VTTParser.prototype = { - parse: function(data) { - var self = this; + parse: function (data) { + let self = this; // If there is no data then we won't decode it, but will just try to parse // whatever is in buffer already. This may occur in circumstances, for // example when flush() is called. if (data) { // Try to decode the data that we received. - self.buffer += self.decoder.decode(data, {stream: true}); + self.buffer += self.decoder.decode(data, { stream: true }); } - function collectNextLine() { - var buffer = self.buffer; - var pos = 0; + function collectNextLine () { + let buffer = self.buffer; + let pos = 0; buffer = fixLineBreaks(buffer); - while (pos < buffer.length && buffer[pos] !== '\r' && buffer[pos] !== '\n') { + while (pos < buffer.length && buffer[pos] !== '\r' && buffer[pos] !== '\n') ++pos; - } - var line = buffer.substr(0, pos); + + let line = buffer.substr(0, pos); // Advance the buffer early in case we fail below. - if (buffer[pos] === '\r') { + if (buffer[pos] === '\r') ++pos; - } - if (buffer[pos] === '\n') { + + if (buffer[pos] === '\n') ++pos; - } + self.buffer = buffer.substr(pos); return line; } // 3.2 WebVTT metadata header syntax - function parseHeader(input) { - parseOptions(input, function(k, v) { + function parseHeader (input) { + parseOptions(input, function (k, v) { switch (k) { - case 'Region': - // 3.3 WebVTT region metadata header syntax - console.log('parse region', v); - //parseRegion(v); - break; + case 'Region': + // 3.3 WebVTT region metadata header syntax + console.log('parse region', v); + // parseRegion(v); + break; } }, /:/); } // 5.1 WebVTT file parsing. try { - var line; + let line; if (self.state === 'INITIAL') { // We can't start parsing until we have the first line. - if (!/\r\n|\n/.test(self.buffer)) { + if (!/\r\n|\n/.test(self.buffer)) return this; - } line = collectNextLine(); // strip of UTF-8 BOM if any // https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8 - var m = line.match(/^()?WEBVTT([ \t].*)?$/); - if (!m || !m[0]) { + let m = line.match(/^()?WEBVTT([ \t].*)?$/); + if (!m || !m[0]) throw new Error('Malformed WebVTT signature.'); - } self.state = 'HEADER'; } - var alreadyCollectedLine = false; + let alreadyCollectedLine = false; while (self.buffer) { // We can't parse a line until we have the full line. - if (!/\r\n|\n/.test(self.buffer)) { + if (!/\r\n|\n/.test(self.buffer)) return this; - } - if (!alreadyCollectedLine) { + if (!alreadyCollectedLine) line = collectNextLine(); - } else { + else alreadyCollectedLine = false; - } switch (self.state) { - case 'HEADER': - // 13-18 - Allow a header (metadata) under the WEBVTT line. - if (/:/.test(line)) { - parseHeader(line); - } else if (!line) { - // An empty line terminates the header and starts the body (cues). - self.state = 'ID'; - } + case 'HEADER': + // 13-18 - Allow a header (metadata) under the WEBVTT line. + if (/:/.test(line)) { + parseHeader(line); + } else if (!line) { + // An empty line terminates the header and starts the body (cues). + self.state = 'ID'; + } + continue; + case 'NOTE': + // Ignore NOTE blocks. + if (!line) + self.state = 'ID'; + + continue; + case 'ID': + // Check for the start of NOTE blocks. + if (/^NOTE($|[ \t])/.test(line)) { + self.state = 'NOTE'; + break; + } + // 19-29 - Allow any number of line terminators, then initialize new cue values. + if (!line) continue; - case 'NOTE': - // Ignore NOTE blocks. - if (!line) { - self.state = 'ID'; - } + + self.cue = new VTTCue(0, 0, ''); + self.state = 'CUE'; + // 30-39 - Check if self line contains an optional identifier or timing data. + if (line.indexOf('-->') === -1) { + self.cue.id = line; continue; - case 'ID': - // Check for the start of NOTE blocks. - if (/^NOTE($|[ \t])/.test(line)) { - self.state = 'NOTE'; - break; - } - // 19-29 - Allow any number of line terminators, then initialize new cue values. - if (!line) { - continue; - } - self.cue = new VTTCue(0, 0, ''); - self.state = 'CUE'; - // 30-39 - Check if self line contains an optional identifier or timing data. - if (line.indexOf('-->') === -1) { - self.cue.id = line; - continue; - } + } // Process line as start of a cue. - /*falls through*/ - case 'CUE': - // 40 - Collect cue timings and settings. - try { - parseCue(line, self.cue, self.regionList); - } catch (e) { - // In case of an error ignore rest of the cue. - self.cue = null; - self.state = 'BADCUE'; - continue; - } - self.state = 'CUETEXT'; + /* falls through */ + case 'CUE': + // 40 - Collect cue timings and settings. + try { + parseCue(line, self.cue, self.regionList); + } catch (e) { + // In case of an error ignore rest of the cue. + self.cue = null; + self.state = 'BADCUE'; continue; - case 'CUETEXT': - var hasSubstring = line.indexOf('-->') !== -1; - // 34 - If we have an empty line then report the cue. - // 35 - If we have the special substring '-->' then report the cue, - // but do not collect the line as we need to process the current - // one as a new cue. - if (!line || hasSubstring && (alreadyCollectedLine = true)) { - // We are done parsing self cue. - if (self.oncue) { - self.oncue(self.cue); - } - self.cue = null; - self.state = 'ID'; - continue; - } - if (self.cue.text) { - self.cue.text += '\n'; - } - self.cue.text += line; - continue; - case 'BADCUE': // BADCUE - // 54-62 - Collect and discard the remaining cue. - if (!line) { - self.state = 'ID'; - } + } + self.state = 'CUETEXT'; + continue; + case 'CUETEXT': + var hasSubstring = line.indexOf('-->') !== -1; + // 34 - If we have an empty line then report the cue. + // 35 - If we have the special substring '-->' then report the cue, + // but do not collect the line as we need to process the current + // one as a new cue. + if (!line || hasSubstring && (alreadyCollectedLine = true)) { + // We are done parsing self cue. + if (self.oncue) + self.oncue(self.cue); + + self.cue = null; + self.state = 'ID'; continue; + } + if (self.cue.text) + self.cue.text += '\n'; + + self.cue.text += line; + continue; + case 'BADCUE': // BADCUE + // 54-62 - Collect and discard the remaining cue. + if (!line) + self.state = 'ID'; + + continue; } } } catch (e) { - // If we are currently parsing a cue, report what we have. - if (self.state === 'CUETEXT' && self.cue && self.oncue) { + if (self.state === 'CUETEXT' && self.cue && self.oncue) self.oncue(self.cue); - } + self.cue = null; // Enter BADWEBVTT state if header was not parsed correctly otherwise // another exception occurred so enter BADCUE state. @@ -405,8 +396,8 @@ VTTParser.prototype = { } return this; }, - flush: function() { - var self = this; + flush: function () { + let self = this; try { // Finish decoding the stream. self.buffer += self.decoder.decode(); @@ -418,15 +409,14 @@ VTTParser.prototype = { // If we've flushed, parsed, and we're still on the INITIAL state then // that means we don't have enough of the stream to parse the first // line. - if (self.state === 'INITIAL') { + if (self.state === 'INITIAL') throw new Error('Malformed WebVTT signature.'); - } } catch (e) { throw e; } - if (self.onflush) { + if (self.onflush) self.onflush(); - } + return this; } }; diff --git a/src/utils/webvtt-parser.js b/src/utils/webvtt-parser.js index 029bb769db1..9b5a893f7a7 100644 --- a/src/utils/webvtt-parser.js +++ b/src/utils/webvtt-parser.js @@ -2,171 +2,166 @@ import VTTParser from './vttparser'; import { utf8ArrayToStr } from '../demux/id3'; // String.prototype.startsWith is not supported in IE11 -const startsWith = function(inputString, searchString, position) { +const startsWith = function (inputString, searchString, position) { return inputString.substr(position || 0, searchString.length) === searchString; }; -const cueString2millis = function(timeString) { - let ts = parseInt(timeString.substr(-3)); - let secs = parseInt(timeString.substr(-6,2)); - let mins = parseInt(timeString.substr(-9,2)); - let hours = timeString.length > 9 ? parseInt(timeString.substr(0, timeString.indexOf(':'))) : 0; +const cueString2millis = function (timeString) { + let ts = parseInt(timeString.substr(-3)); + let secs = parseInt(timeString.substr(-6, 2)); + let mins = parseInt(timeString.substr(-9, 2)); + let hours = timeString.length > 9 ? parseInt(timeString.substr(0, timeString.indexOf(':'))) : 0; - if (isNaN(ts) || isNaN(secs) || isNaN(mins) || isNaN(hours)) { - return -1; - } + if (isNaN(ts) || isNaN(secs) || isNaN(mins) || isNaN(hours)) + return -1; - ts += 1000 * secs; - ts += 60*1000 * mins; - ts += 60*60*1000 * hours; + ts += 1000 * secs; + ts += 60 * 1000 * mins; + ts += 60 * 60 * 1000 * hours; - return ts; + return ts; }; // From https://github.com/darkskyapp/string-hash -const hash = function(text) { - let hash = 5381; - let i = text.length; - while (i) { - hash = (hash * 33) ^ text.charCodeAt(--i); - } - return (hash >>> 0).toString(); -}; +const hash = function (text) { + let hash = 5381; + let i = text.length; + while (i) + hash = (hash * 33) ^ text.charCodeAt(--i); -const calculateOffset = function(vttCCs, cc, presentationTime) { - let currCC = vttCCs[cc]; - let prevCC = vttCCs[currCC.prevCC]; + return (hash >>> 0).toString(); +}; - // This is the first discontinuity or cues have been processed since the last discontinuity - // Offset = current discontinuity time - if (!prevCC || (!prevCC.new && currCC.new)) { - vttCCs.ccOffset = vttCCs.presentationOffset = currCC.start; - currCC.new = false; - return; - } - - // There have been discontinuities since cues were last parsed. - // Offset = time elapsed - while (prevCC && prevCC.new) { - vttCCs.ccOffset += currCC.start - prevCC.start; - currCC.new = false; - currCC = prevCC; - prevCC = vttCCs[currCC.prevCC]; - } - - vttCCs.presentationOffset = presentationTime; +const calculateOffset = function (vttCCs, cc, presentationTime) { + let currCC = vttCCs[cc]; + let prevCC = vttCCs[currCC.prevCC]; + + // This is the first discontinuity or cues have been processed since the last discontinuity + // Offset = current discontinuity time + if (!prevCC || (!prevCC.new && currCC.new)) { + vttCCs.ccOffset = vttCCs.presentationOffset = currCC.start; + currCC.new = false; + return; + } + + // There have been discontinuities since cues were last parsed. + // Offset = time elapsed + while (prevCC && prevCC.new) { + vttCCs.ccOffset += currCC.start - prevCC.start; + currCC.new = false; + currCC = prevCC; + prevCC = vttCCs[currCC.prevCC]; + } + + vttCCs.presentationOffset = presentationTime; }; const WebVTTParser = { - parse: function(vttByteArray, syncPTS, vttCCs, cc, callBack, errorCallBack) { - // Convert byteArray into string, replacing any somewhat exotic linefeeds with "\n", then split on that character. - let re = /\r\n|\n\r|\n|\r/g; - // Uint8Array.prototype.reduce is not implemented in IE11 - let vttLines = utf8ArrayToStr(new Uint8Array(vttByteArray)).trim().replace(re, '\n').split('\n'); - - let cueTime = '00:00.000'; - let mpegTs = 0; - let localTime = 0; - let presentationTime = 0; - let cues = []; - let parsingError; - let inHeader = true; - // let VTTCue = VTTCue || window.TextTrackCue; - - // Create parser object using VTTCue with TextTrackCue fallback on certain browsers. - let parser = new VTTParser(); - - parser.oncue = function(cue) { - // Adjust cue timing; clamp cues to start no earlier than - and drop cues that don't end after - 0 on timeline. - let currCC = vttCCs[cc]; - let cueOffset = vttCCs.ccOffset; - - // Update offsets for new discontinuities - if (currCC && currCC.new) { - if (localTime !== undefined) { - // When local time is provided, offset = discontinuity start time - local time - cueOffset = vttCCs.ccOffset = currCC.start; - } else { - calculateOffset(vttCCs, cc, presentationTime); - } - } - - if (presentationTime) { - // If we have MPEGTS, offset = presentation time + discontinuity offset - cueOffset = presentationTime + vttCCs.ccOffset - vttCCs.presentationOffset; - } - - cue.startTime += cueOffset - localTime; - cue.endTime += cueOffset - localTime; - - // Create a unique hash id for a cue based on start/end times and text. - // This helps timeline-controller to avoid showing repeated captions. - cue.id = hash(cue.startTime.toString()) + hash(cue.endTime.toString()) + hash(cue.text); - - // Fix encoding of special characters. TODO: Test with all sorts of weird characters. - cue.text = decodeURIComponent(encodeURIComponent(cue.text)); - if (cue.endTime > 0) { - cues.push(cue); - } - }; - - parser.onparsingerror = function(e) { - parsingError = e; - }; - - parser.onflush = function() { - if (parsingError && errorCallBack) { - errorCallBack(parsingError); - return; - } - callBack(cues); - }; - - // Go through contents line by line. - vttLines.forEach(line => { - if (inHeader) { - // Look for X-TIMESTAMP-MAP in header. - if (startsWith(line, 'X-TIMESTAMP-MAP=')) { - // Once found, no more are allowed anyway, so stop searching. - inHeader = false; - // Extract LOCAL and MPEGTS. - line.substr(16).split(',').forEach(timestamp => { - if (startsWith(timestamp, 'LOCAL:')) { - cueTime = timestamp.substr(6); - } else if (startsWith(timestamp, 'MPEGTS:')) { - mpegTs = parseInt(timestamp.substr(7)); - } - }); - try { - // Calculate subtitle offset in milliseconds. - // If sync PTS is less than zero, we have a 33-bit wraparound, which is fixed by adding 2^33 = 8589934592. - syncPTS = syncPTS < 0 ? syncPTS + 8589934592 : syncPTS; - // Adjust MPEGTS by sync PTS. - mpegTs -= syncPTS; - // Convert cue time to seconds - localTime = cueString2millis(cueTime) / 1000; - // Convert MPEGTS to seconds from 90kHz. - presentationTime = mpegTs / 90000; - - if (localTime === -1) { - parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); - } - } - catch(e) { - parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); - } - // Return without parsing X-TIMESTAMP-MAP line. - return; - } else if (line === '') { - inHeader = false; - } - } - // Parse line by default. - parser.parse(line+'\n'); - }); - - parser.flush(); - } + parse: function (vttByteArray, syncPTS, vttCCs, cc, callBack, errorCallBack) { + // Convert byteArray into string, replacing any somewhat exotic linefeeds with "\n", then split on that character. + let re = /\r\n|\n\r|\n|\r/g; + // Uint8Array.prototype.reduce is not implemented in IE11 + let vttLines = utf8ArrayToStr(new Uint8Array(vttByteArray)).trim().replace(re, '\n').split('\n'); + + let cueTime = '00:00.000'; + let mpegTs = 0; + let localTime = 0; + let presentationTime = 0; + let cues = []; + let parsingError; + let inHeader = true; + // let VTTCue = VTTCue || window.TextTrackCue; + + // Create parser object using VTTCue with TextTrackCue fallback on certain browsers. + let parser = new VTTParser(); + + parser.oncue = function (cue) { + // Adjust cue timing; clamp cues to start no earlier than - and drop cues that don't end after - 0 on timeline. + let currCC = vttCCs[cc]; + let cueOffset = vttCCs.ccOffset; + + // Update offsets for new discontinuities + if (currCC && currCC.new) { + if (localTime !== undefined) { + // When local time is provided, offset = discontinuity start time - local time + cueOffset = vttCCs.ccOffset = currCC.start; + } else { + calculateOffset(vttCCs, cc, presentationTime); + } + } + + if (presentationTime) { + // If we have MPEGTS, offset = presentation time + discontinuity offset + cueOffset = presentationTime + vttCCs.ccOffset - vttCCs.presentationOffset; + } + + cue.startTime += cueOffset - localTime; + cue.endTime += cueOffset - localTime; + + // Create a unique hash id for a cue based on start/end times and text. + // This helps timeline-controller to avoid showing repeated captions. + cue.id = hash(cue.startTime.toString()) + hash(cue.endTime.toString()) + hash(cue.text); + + // Fix encoding of special characters. TODO: Test with all sorts of weird characters. + cue.text = decodeURIComponent(encodeURIComponent(cue.text)); + if (cue.endTime > 0) + cues.push(cue); + }; + + parser.onparsingerror = function (e) { + parsingError = e; + }; + + parser.onflush = function () { + if (parsingError && errorCallBack) { + errorCallBack(parsingError); + return; + } + callBack(cues); + }; + + // Go through contents line by line. + vttLines.forEach(line => { + if (inHeader) { + // Look for X-TIMESTAMP-MAP in header. + if (startsWith(line, 'X-TIMESTAMP-MAP=')) { + // Once found, no more are allowed anyway, so stop searching. + inHeader = false; + // Extract LOCAL and MPEGTS. + line.substr(16).split(',').forEach(timestamp => { + if (startsWith(timestamp, 'LOCAL:')) + cueTime = timestamp.substr(6); + else if (startsWith(timestamp, 'MPEGTS:')) + mpegTs = parseInt(timestamp.substr(7)); + }); + try { + // Calculate subtitle offset in milliseconds. + // If sync PTS is less than zero, we have a 33-bit wraparound, which is fixed by adding 2^33 = 8589934592. + syncPTS = syncPTS < 0 ? syncPTS + 8589934592 : syncPTS; + // Adjust MPEGTS by sync PTS. + mpegTs -= syncPTS; + // Convert cue time to seconds + localTime = cueString2millis(cueTime) / 1000; + // Convert MPEGTS to seconds from 90kHz. + presentationTime = mpegTs / 90000; + + if (localTime === -1) + parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); + } catch (e) { + parsingError = new Error(`Malformed X-TIMESTAMP-MAP: ${line}`); + } + // Return without parsing X-TIMESTAMP-MAP line. + return; + } else if (line === '') { + inHeader = false; + } + } + // Parse line by default. + parser.parse(line + '\n'); + }); + + parser.flush(); + } }; export default WebVTTParser; diff --git a/src/utils/xhr-loader.js b/src/utils/xhr-loader.js index 8e9d2c2a205..ecc2cc44119 100644 --- a/src/utils/xhr-loader.js +++ b/src/utils/xhr-loader.js @@ -2,23 +2,21 @@ * XHR based logger */ -import {logger} from '../utils/logger'; +import { logger } from '../utils/logger'; class XhrLoader { - - constructor(config) { - if (config && config.xhrSetup) { + constructor (config) { + if (config && config.xhrSetup) this.xhrSetup = config.xhrSetup; - } } - destroy() { + destroy () { this.abort(); this.loader = null; } - abort() { - var loader = this.loader; + abort () { + let loader = this.loader; if (loader && loader.readyState !== 4) { this.stats.aborted = true; loader.abort(); @@ -30,19 +28,19 @@ class XhrLoader { this.retryTimeout = null; } - load(context, config, callbacks) { + load (context, config, callbacks) { this.context = context; this.config = config; this.callbacks = callbacks; - this.stats = {trequest: performance.now(), retry: 0}; + this.stats = { trequest: performance.now(), retry: 0 }; this.retryDelay = config.retryDelay; this.loadInternal(); } - loadInternal() { - var xhr, context = this.context; + loadInternal () { + let xhr, context = this.context; xhr = this.loader = new XMLHttpRequest(); - + let stats = this.stats; stats.tfirst = 0; stats.loaded = 0; @@ -59,18 +57,17 @@ class XhrLoader { xhrSetup(xhr, context.url); } } - if (!xhr.readyState) { + if (!xhr.readyState) xhr.open('GET', context.url, true); - } } catch (e) { // IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS - this.callbacks.onError({ code : xhr.status, text: e.message }, context, xhr); + this.callbacks.onError({ code: xhr.status, text: e.message }, context, xhr); return; } - if (context.rangeEnd) { - xhr.setRequestHeader('Range','bytes=' + context.rangeStart + '-' + (context.rangeEnd-1)); - } + if (context.rangeEnd) + xhr.setRequestHeader('Range', 'bytes=' + context.rangeStart + '-' + (context.rangeEnd - 1)); + xhr.onreadystatechange = this.readystatechange.bind(this); xhr.onprogress = this.loadprogress.bind(this); xhr.responseType = context.responseType; @@ -80,31 +77,30 @@ class XhrLoader { xhr.send(); } - readystatechange(event) { - var xhr = event.currentTarget, - readyState = xhr.readyState, - stats = this.stats, - context = this.context, - config = this.config; + readystatechange (event) { + let xhr = event.currentTarget, + readyState = xhr.readyState, + stats = this.stats, + context = this.context, + config = this.config; // don't proceed if xhr has been aborted - if (stats.aborted) { + if (stats.aborted) return; - } // >= HEADERS_RECEIVED - if (readyState >=2) { + if (readyState >= 2) { // clear xhr timeout and rearm it if readyState less than 4 window.clearTimeout(this.requestTimeout); - if (stats.tfirst === 0) { + if (stats.tfirst === 0) stats.tfirst = Math.max(performance.now(), stats.trequest); - } + if (readyState === 4) { let status = xhr.status; // http status between 200 to 299 are all successful - if (status >= 200 && status < 300) { - stats.tload = Math.max(stats.tfirst,performance.now()); - let data,len; + if (status >= 200 && status < 300) { + stats.tload = Math.max(stats.tfirst, performance.now()); + let data, len; if (context.responseType === 'arraybuffer') { data = xhr.response; len = data.byteLength; @@ -113,13 +109,13 @@ class XhrLoader { len = data.length; } stats.loaded = stats.total = len; - let response = { url : xhr.responseURL, data : data }; + let response = { url: xhr.responseURL, data: data }; this.callbacks.onSuccess(response, stats, context, xhr); } else { - // if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error + // if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error if (stats.retry >= config.maxRetry || (status >= 400 && status < 499)) { - logger.error(`${status} while loading ${context.url}` ); - this.callbacks.onError({ code : status, text : xhr.statusText}, context, xhr); + logger.error(`${status} while loading ${context.url}`); + this.callbacks.onError({ code: status, text: xhr.statusText }, context, xhr); } else { // retry logger.warn(`${status} while loading ${context.url}, retrying in ${this.retryDelay}...`); @@ -139,19 +135,19 @@ class XhrLoader { } } - loadtimeout() { - logger.warn(`timeout while loading ${this.context.url}` ); + loadtimeout () { + logger.warn(`timeout while loading ${this.context.url}`); this.callbacks.onTimeout(this.stats, this.context, null); } - loadprogress(event) { - var xhr = event.currentTarget, - stats = this.stats; + loadprogress (event) { + let xhr = event.currentTarget, + stats = this.stats; stats.loaded = event.loaded; - if (event.lengthComputable) { + if (event.lengthComputable) stats.total = event.total; - } + let onProgress = this.callbacks.onProgress; if (onProgress) { // third arg is to provide on progress data diff --git a/tests/functional/auto/hlsjs.html b/tests/functional/auto/hlsjs.html deleted file mode 100644 index f8477d3a6e9..00000000000 --- a/tests/functional/auto/hlsjs.html +++ /dev/null @@ -1,173 +0,0 @@ -<html> - <head> - <style> - body { - background-color: #cccccc; - } - - #log { - position: fixed; - top: 0; - bottom: 0; - left: 0; - right: 0; - margin: 0; - color: #ff0000; - overflow: hidden; - font-size: 20px; - z-index: 999; - } - - #log .inner { - position: absolute; - bottom: 0; - left: 0; - right: 0; - margin: 0; - padding: 0; - background-color: rgba(255, 255, 255, 0.75); - } - - #log .inner .line { - margin: 0; - padding: 3px 15px; - border-width: 1px; - border-color: #aaaaaa; - border-style: none; - border-bottom-style: solid; - white-space: pre-wrap; - } - </style> - <script src="/dist/hls.min.js"></script> - <script> - var video, hls, logString =''; - - // Object.assign polyfill - function objectAssign(target, firstSource) { - if (target === undefined || target === null) { - throw new TypeError('Cannot convert first argument to object'); - } - - var to = Object(target); - for (var i = 1; i < arguments.length; i++) { - var nextSource = arguments[i]; - if (nextSource === undefined || nextSource === null) { - continue; - } - - var keysArray = Object.keys(Object(nextSource)); - for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { - var nextKey = keysArray[nextIndex]; - var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); - if (desc !== undefined && desc.enumerable) { - to[nextKey] = nextSource[nextKey]; - } - } - } - return to; - } - - function startStream(streamUrl, config, callback) { - if (Hls.isSupported()) { - if (hls) { - callback({ code : 'hlsjsAlreadyInitialised', logs : logString}); - return; - } - video = document.getElementById('video'); - try { - hls = new Hls(objectAssign({}, config, {debug: true})); - console.log(navigator.userAgent); - hls.loadSource(streamUrl); - hls.attachMedia(video); - hls.on(Hls.Events.MANIFEST_PARSED, function() { - video.play(); - }); - hls.on(Hls.Events.ERROR, function(event, data) { - if (data.fatal) { - console.log('hlsjs fatal error :' + data.details); - if (data.details === Hls.ErrorDetails.INTERNAL_EXCEPTION) { - console.log('exception in :' + data.event); - console.log(data.err.stack ? JSON.stringify(data.err.stack) : data.err.message); - } - callback({ code : data.details, logs : logString}); - } - }); - video.onerror = function(event) { - console.log('video error, code :' + video.error.code); - callback({ code : 'video_error_' + video.error.code, logs : logString}); - }; - } catch(err) { - callback({ code : 'exception', logs : logString}); - } - } else { - callback({ code : 'notSupported', logs : logString}); - } - } - - function switchToLowestLevel(mode) { - switch(mode) { - case 'current': - hls.currentLevel = 0; - break; - case 'next': - hls.nextLevel = 0; - break; - case 'load': - default: - hls.loadLevel = 0; - break; - } - } - - function switchToHighestLevel() { - var highestLevel = hls.levels.length-1; - switch(mode) { - case 'current': - hls.currentLevel = highestLevel; - break; - case 'next': - hls.nextLevel = highestLevel; - break; - case 'load': - default: - hls.loadLevel = highestLevel; - break; - } - } - </script> - </head> - <body id="hlsjs-functional-tests"> - <video id="video"></video> - <div id="log"> - <div class="inner"></div> - </div> - <script> - (function() { - var methods = ["log", "debug", "info", "warn", "error"]; - methods.forEach(function(methodName) { - var original = console[methodName]; - if (!original) { - return; - } - console[methodName] = function() { - append(methodName, Array.prototype.slice.call(arguments).map(JSON.stringify).join(" ")); - return original.apply(this, arguments); - }; - }); - - var log = document.getElementById('log'); - var inner = log.getElementsByClassName('inner')[0]; - - function append(methodName, msg) { - var a = (new Date()).toISOString().replace("T", " ").replace("Z", "")+': '+msg; - var text = document.createTextNode(a); - var line = document.createElement('pre'); - line.className = "line line-"+methodName; - line.appendChild(text); - logString += a + '\n'; - inner.appendChild(line); - } - })(); - </script> - </body> - </html> diff --git a/tests/functional/auto/hlsjs.js b/tests/functional/auto/hlsjs.js deleted file mode 100644 index c5f44f5cc1e..00000000000 --- a/tests/functional/auto/hlsjs.js +++ /dev/null @@ -1,257 +0,0 @@ -var assert = require('assert'); -var webdriver = require('selenium-webdriver'); -// requiring this automatically adds the chromedriver binary to the PATH -var chromedriver = require('chromedriver'); -var HttpServer = require('http-server'); -var streams = require('../../test-streams'); - -function retry(cb, numAttempts, interval) { - numAttempts = numAttempts || 20; - interval = interval || 3000; - return new Promise(function(resolve, reject) { - var attempts = 0; - attempt(); - - function attempt() { - cb().then(function(res) { - resolve(res); - }).catch(function(e) { - if (++attempts >= numAttempts) { - // reject with the last error - reject(e); - } - else { - setTimeout(attempt, interval); - } - }); - } - }); -} - -var onTravis = !!process.env.TRAVIS; - -HttpServer.createServer({ - showDir: false, - autoIndex: false, - root: './', -}).listen(8000, '127.0.0.1'); - - -var browserConfig = {version : 'latest'}; -if (onTravis) { - var UA_VERSION = process.env.UA_VERSION; - if (UA_VERSION) { - browserConfig.version = UA_VERSION; - } - var UA = process.env.UA; - if (!UA) { - throw new Error('No test browser name.') - } - var OS = process.env.OS; - if (!OS) { - throw new Error('No test browser platform.') - } - browserConfig.name = UA; - browserConfig.platform = OS; -} -else { - browserConfig.name = "chrome"; -} -var browserDescription = browserConfig.name; -if (browserConfig.version) { - browserDescription += ' ('+browserConfig.version+')'; -} -if (browserConfig.platform) { - browserDescription += ', '+browserConfig.platform; -} - -describe('testing hls.js playback in the browser on "'+browserDescription+'"', function() { - beforeEach(function() { - var capabilities = { - name: '"'+stream.description+'" on "'+browserDescription+'"', - browserName: browserConfig.name, - platform: browserConfig.platform, - version: browserConfig.version, - commandTimeout: 90, - }; - if (onTravis) { - capabilities['tunnel-identifier'] = process.env.TRAVIS_JOB_NUMBER; - capabilities.build = 'HLSJS-'+process.env.TRAVIS_BUILD_NUMBER; - capabilities.username = process.env.SAUCE_USERNAME; - capabilities.accessKey = process.env.SAUCE_ACCESS_KEY; - capabilities.avoidProxy = true; - this.browser = new webdriver.Builder().usingServer('http://'+process.env.SAUCE_USERNAME+':'+process.env.SAUCE_ACCESS_KEY+'@ondemand.saucelabs.com:80/wd/hub'); - } - else { - this.browser = new webdriver.Builder(); - } - this.browser = this.browser.withCapabilities(capabilities).build(); - this.browser.manage().timeouts().setScriptTimeout(75000); - console.log("Retrieving web driver session..."); - return this.browser.getSession().then(function(session) { - console.log("Web driver session id: "+session.getId()); - if (onTravis) { - console.log("Job URL: https://saucelabs.com/jobs/"+session.getId()); - } - return retry(function() { - console.log("Loading test page..."); - return this.browser.get('http://127.0.0.1:8000/tests/functional/auto/hlsjs.html').then(function() { - // ensure that the page has loaded and we haven't got an error page - return this.browser.findElement(webdriver.By.css('body#hlsjs-functional-tests')).catch(function(e) { - console.log("CSS not found"); - this.browser.getPageSource().then(function(source){ - console.log(source); - return Promise.reject(e); - }); - }.bind(this)); - }.bind(this)); - }.bind(this)).then(function() { - console.log("Test page loaded."); - }); - }.bind(this), function(err) { - console.log('error while Retrieving browser session:' + err); - }); - }); - - afterEach(function() { - var browser = this.browser; - return browser.executeScript('return logString').then(function(return_value){ - console.log('travis_fold:start:debug_logs'); - console.log('logs'); - console.log(return_value); - console.log('travis_fold:end:debug_logs'); - console.log("Quitting browser..."); - return browser.quit().then(function() { - console.log("Browser quit."); - }); - }); - }); - - const testLoadedData = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - callback({ code : 'loadeddata', logs : logString}); - }; - }, url ,config).then(function(result) { - assert.strictEqual(result.code, 'loadeddata'); - }); - } - } - - const testSmoothSwitch = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - switchToHighestLevel('next'); - }; - window.setTimeout(function() { - callback({ code : video.readyState, logs : logString}); - }, 12000); - }, url ,config).then(function(result) { - assert.strictEqual(result.code, 4); - }); - } - } - - const testSeekOnLive = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - window.setTimeout(function() { video.currentTime = video.duration - 5;}, 5000); - }; - video.onseeked = function() { - callback({ code : 'seeked', logs : logString}); - }; - }, url ,config).then(function(result) { - assert.strictEqual(result.code, 'seeked'); - }); - } - } - - const testSeekOnVOD = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - window.setTimeout(function() { video.currentTime = video.duration - 5;}, 5000); - }; - video.onended = function() { - callback({ code : 'ended', logs : logString}); - }; - }, url ,config).then(function(result) { - assert.strictEqual(result.code, 'ended'); - }); - } - } - - const testSeekEndVOD = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - window.setTimeout(function() { video.currentTime = video.duration;}, 5000); - }; - video.onended = function() { - callback({ code : 'ended', logs : logString}); - }; - }, url ,config).then(function(result) { - assert.strictEqual(result.code, 'ended'); - }); - } - } - - const testIsPlayingVOD = function(url, config) { - return function() { - return this.browser.executeAsyncScript(function(url, config) { - var callback = arguments[arguments.length - 1]; - startStream(url, config, callback); - video.onloadeddata = function() { - let expectedPlaying = !(video.paused || // not playing when video is paused - video.ended || // not playing when video is ended - video.buffered.length === 0); // not playing if nothing buffered - let currentTime = video.currentTime; - if(expectedPlaying) { - window.setTimeout(function() { - console.log("video expected playing. [last currentTime/new currentTime]=[" + currentTime + "/" + video.currentTime + "]"); - callback({ playing : currentTime !== video.currentTime}); - }, 5000); - } else { - console.log("video not playing. [paused/ended/buffered.length]=[" + video.paused + "/" + video.ended + "/" + video.buffered.length + "]"); - callback({ playing : false }); - } - }; - }, url ,config).then(function(result) { - assert.strictEqual(result.playing, true); - }); - } - } - - for (var name in streams) { - var stream = streams[name]; - var url = stream.url; - var config = stream.config || {}; - if (!stream.blacklist_ua || stream.blacklist_ua.indexOf(browserConfig.name) === -1) { - it('should receive video loadeddata event for ' + stream.description, testLoadedData(url, config)); - if (stream.abr) { - it('should "smooth switch" to highest level and still play(readyState === 4) after 12s for ' + stream.description, testSmoothSwitch(url, config)); - } - - if (stream.live) { - it('should seek near the end and receive video seeked event for ' + stream.description, testSeekOnLive(url, config)); - } else { - it('should play ' + stream.description, testIsPlayingVOD(url, config)); - it('should seek 5s from end and receive video ended event for ' + stream.description, testSeekOnVOD(url, config)); - //it('should seek on end and receive video ended event for ' + stream.description, testSeekEndVOD(url)); - } - } - } -}); diff --git a/tests/functional/auto/index.html b/tests/functional/auto/index.html new file mode 100644 index 00000000000..62d727a587d --- /dev/null +++ b/tests/functional/auto/index.html @@ -0,0 +1,16 @@ +<html> + <head> + <link rel="stylesheet" href="style.css"> + <script src="/dist/hls.min.js"></script> + <script src="testbench.js"></script> + </head> + <body id="hlsjs-functional-tests"> + <video id="video"></video> + <div id="log"> + <div class="inner"></div> + </div> + <script> + setupConsoleLogRedirection(); + </script> + </body> + </html> diff --git a/tests/functional/auto/setup.js b/tests/functional/auto/setup.js new file mode 100644 index 00000000000..89285146bb4 --- /dev/null +++ b/tests/functional/auto/setup.js @@ -0,0 +1,257 @@ +let assert = require('assert'); +let webdriver = require('selenium-webdriver'); +// requiring this automatically adds the chromedriver binary to the PATH +let chromedriver = require('chromedriver'); +let HttpServer = require('http-server'); +let streams = require('../../test-streams'); + +function retry (cb, numAttempts, interval) { + numAttempts = numAttempts || 20; + interval = interval || 3000; + return new Promise(function (resolve, reject) { + let attempts = 0; + attempt(); + + function attempt () { + cb().then(function (res) { + resolve(res); + }).catch(function (e) { + if (++attempts >= numAttempts) { + // reject with the last error + reject(e); + } else { + setTimeout(attempt, interval); + } + }); + } + }); +} + +let onTravis = !!process.env.TRAVIS; + +HttpServer.createServer({ + showDir: false, + autoIndex: false, + root: './' +}).listen(8000, '127.0.0.1'); + +let browserConfig = { version: 'latest' }; +if (onTravis) { + let UA_VERSION = process.env.UA_VERSION; + if (UA_VERSION) + browserConfig.version = UA_VERSION; + + let UA = process.env.UA; + if (!UA) + throw new Error('No test browser name.'); + + let OS = process.env.OS; + if (!OS) + throw new Error('No test browser platform.'); + + browserConfig.name = UA; + browserConfig.platform = OS; +} else { + browserConfig.name = 'chrome'; +} +let browserDescription = browserConfig.name; +if (browserConfig.version) + browserDescription += ' (' + browserConfig.version + ')'; + +if (browserConfig.platform) + browserDescription += ', ' + browserConfig.platform; + +describe('testing hls.js playback in the browser on "' + browserDescription + '"', function () { + beforeEach(function () { + let capabilities = { + name: '"' + stream.description + '" on "' + browserDescription + '"', + browserName: browserConfig.name, + platform: browserConfig.platform, + version: browserConfig.version, + commandTimeout: 90 + }; + if (onTravis) { + capabilities['tunnel-identifier'] = process.env.TRAVIS_JOB_NUMBER; + capabilities.build = 'HLSJS-' + process.env.TRAVIS_BUILD_NUMBER; + capabilities.username = process.env.SAUCE_USERNAME; + capabilities.accessKey = process.env.SAUCE_ACCESS_KEY; + capabilities.avoidProxy = true; + this.browser = new webdriver.Builder().usingServer('http://' + process.env.SAUCE_USERNAME + ':' + process.env.SAUCE_ACCESS_KEY + '@ondemand.saucelabs.com:80/wd/hub'); + } else { + this.browser = new webdriver.Builder(); + } + this.browser = this.browser.withCapabilities(capabilities).build(); + this.browser.manage().timeouts().setScriptTimeout(75000); + console.log('Retrieving web driver session...'); + return this.browser.getSession().then(function (session) { + console.log('Web driver session id: ' + session.getId()); + if (onTravis) + console.log('Job URL: https://saucelabs.com/jobs/' + session.getId()); + + return retry(function () { + console.log('Loading test page...'); + return this.browser.get('http://127.0.0.1:8000/tests/functional/auto/hlsjs.html').then(function () { + // ensure that the page has loaded and we haven't got an error page + return this.browser.findElement(webdriver.By.css('body#hlsjs-functional-tests')).catch(function (e) { + console.log('CSS not found'); + this.browser.getPageSource().then(function (source) { + console.log(source); + return Promise.reject(e); + }); + }.bind(this)); + }.bind(this)); + }.bind(this)).then(function () { + console.log('Test page loaded.'); + }); + }.bind(this), function (err) { + console.log('error while Retrieving browser session:' + err); + }); + }); + + afterEach(function () { + let browser = this.browser; + return browser.executeScript('return logString').then(function (returnValue) { + console.log('travis_fold:start:debug_logs'); + console.log('logs'); + console.log(returnValue); + console.log('travis_fold:end:debug_logs'); + console.log('Quitting browser...'); + return browser.quit().then(function () { + console.log('Browser quit.'); + }); + }); + }); + + const testLoadedData = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + callback({ code: 'loadeddata', logs: window.logString }); + }; + }, url, config).then(function (result) { + assert.strictEqual(result.code, 'loadeddata'); + }); + }; + }; + + const testSmoothSwitch = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + window.switchToHighestLevel('next'); + }; + window.setTimeout(function () { + callback({ code: video.readyState, logs: window.logString }); + }, 12000); + }, url, config).then(function (result) { + assert.strictEqual(result.code, 4); + }); + }; + }; + + const testSeekOnLive = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + window.setTimeout(function () { video.currentTime = video.duration - 5; }, 5000); + }; + video.onseeked = function () { + callback({ code: 'seeked', logs: window.logString }); + }; + }, url, config).then(function (result) { + assert.strictEqual(result.code, 'seeked'); + }); + }; + }; + + const testSeekOnVOD = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + window.setTimeout(function () { video.currentTime = video.duration - 5; }, 5000); + }; + video.onended = function () { + callback({ code: 'ended', logs: window.logString }); + }; + }, url, config).then(function (result) { + assert.strictEqual(result.code, 'ended'); + }); + }; + }; + + const testSeekEndVOD = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + window.setTimeout(function () { video.currentTime = video.duration; }, 5000); + }; + video.onended = function () { + callback({ code: 'ended', logs: window.logString }); + }; + }, url, config).then(function (result) { + assert.strictEqual(result.code, 'ended'); + }); + }; + }; + + const testIsPlayingVOD = function (url, config) { + return function () { + return this.browser.executeAsyncScript(function (url, config) { + let callback = arguments[arguments.length - 1]; + window.startStream(url, config, callback); + const video = window.video; + video.onloadeddata = function () { + let expectedPlaying = !(video.paused || // not playing when video is paused + video.ended || // not playing when video is ended + video.buffered.length === 0); // not playing if nothing buffered + let currentTime = video.currentTime; + if (expectedPlaying) { + window.setTimeout(function () { + console.log('video expected playing. [last currentTime/new currentTime]=[' + currentTime + '/' + video.currentTime + ']'); + callback({ playing: currentTime !== video.currentTime }); + }, 5000); + } else { + console.log('video not playing. [paused/ended/buffered.length]=[' + video.paused + '/' + video.ended + '/' + video.buffered.length + ']'); + callback({ playing: false }); + } + }; + }, url, config).then(function (result) { + assert.strictEqual(result.playing, true); + }); + }; + }; + + for (let name in streams) { + var stream = streams[name]; + let url = stream.url; + let config = stream.config || {}; + if (!stream.blacklist_ua || stream.blacklist_ua.indexOf(browserConfig.name) === -1) { + it('should receive video loadeddata event for ' + stream.description, testLoadedData(url, config)); + if (stream.abr) + it('should "smooth switch" to highest level and still play(readyState === 4) after 12s for ' + stream.description, testSmoothSwitch(url, config)); + + if (stream.live) { + it('should seek near the end and receive video seeked event for ' + stream.description, testSeekOnLive(url, config)); + } else { + it('should play ' + stream.description, testIsPlayingVOD(url, config)); + it('should seek 5s from end and receive video ended event for ' + stream.description, testSeekOnVOD(url, config)); + // it('should seek on end and receive video ended event for ' + stream.description, testSeekEndVOD(url)); + } + } + } +}); diff --git a/tests/functional/auto/style.css b/tests/functional/auto/style.css new file mode 100644 index 00000000000..299ceb1096d --- /dev/null +++ b/tests/functional/auto/style.css @@ -0,0 +1,36 @@ +body { + background-color: #cccccc; +} + +#log { + position: fixed; + top: 0; + bottom: 0; + left: 0; + right: 0; + margin: 0; + color: #ff0000; + overflow: hidden; + font-size: 20px; + z-index: 999; +} + +#log .inner { + position: absolute; + bottom: 0; + left: 0; + right: 0; + margin: 0; + padding: 0; + background-color: rgba(255, 255, 255, 0.75); +} + +#log .inner .line { + margin: 0; + padding: 3px 15px; + border-width: 1px; + border-color: #aaaaaa; + border-style: none; + border-bottom-style: solid; + white-space: pre-wrap; +} diff --git a/tests/functional/auto/testbench.js b/tests/functional/auto/testbench.js new file mode 100644 index 00000000000..254b134c4ed --- /dev/null +++ b/tests/functional/auto/testbench.js @@ -0,0 +1,135 @@ +/* eslint-disable no-var */ + +// Browser environment state +var video; +var logString; +var hls; + +function setupConsoleLogRedirection () { + let log = document.getElementById('log'); + let inner = log.getElementsByClassName('inner')[0]; + + // append log message + function append (methodName, msg) { + let a = (new Date()).toISOString().replace('T', ' ').replace('Z', '') + ': ' + msg; + let text = document.createTextNode(a); + let line = document.createElement('pre'); + line.className = 'line line-' + methodName; + line.appendChild(text); + inner.appendChild(line); + + window.logString = logString += a + '\n'; + } + + // overload global window console methods + let methods = ['log', 'debug', 'info', 'warn', 'error']; + methods.forEach(function (methodName) { + let original = window.console[methodName]; + if (!original) + return; + + window.console[methodName] = function () { + append(methodName, Array.prototype.slice.call(arguments).map(JSON.stringify).join(' ')); + return original.apply(this, arguments); + }; + }); +} + +// Object.assign polyfill +function objectAssign (target, firstSource) { + if (target === undefined || target === null) + throw new TypeError('Cannot convert first argument to object'); + + let to = Object(target); + for (let i = 1; i < arguments.length; i++) { + let nextSource = arguments[i]; + if (nextSource === undefined || nextSource === null) + continue; + + let keysArray = Object.keys(Object(nextSource)); + for (let nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { + let nextKey = keysArray[nextIndex]; + let desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); + if (desc !== undefined && desc.enumerable) + to[nextKey] = nextSource[nextKey]; + } + } + return to; +} + +function startStream (streamUrl, config, callback) { + var Hls = window.Hls; + if (!Hls) + throw new Error('Hls not installed'); + + if (Hls.isSupported()) { + if (hls) { + callback({ code: 'hlsjsAlreadyInitialised', logs: logString }); + return; + } + window.video = video = document.getElementById('video'); + try { + window.hls = hls = new Hls(objectAssign({}, config, { debug: true })); + console.log(navigator.userAgent); + hls.loadSource(streamUrl); + hls.attachMedia(video); + hls.on(Hls.Events.MANIFEST_PARSED, function () { + video.play(); + }); + hls.on(Hls.Events.ERROR, function (event, data) { + if (data.fatal) { + console.log('hlsjs fatal error :' + data.details); + if (data.details === Hls.ErrorDetails.INTERNAL_EXCEPTION) { + console.log('exception in :' + data.event); + console.log(data.err.stack ? JSON.stringify(data.err.stack) : data.err.message); + } + callback({ code: data.details, logs: logString }); + } + }); + video.onerror = function (event) { + console.log('video error, code :' + video.error.code); + callback({ code: 'video_error_' + video.error.code, logs: logString }); + }; + } catch (err) { + callback({ code: 'exception', logs: logString }); + } + } else { + callback({ code: 'notSupported', logs: logString }); + } +} + +function switchToLowestLevel (mode) { + switch (mode) { + case 'current': + hls.currentLevel = 0; + break; + case 'next': + hls.nextLevel = 0; + break; + case 'load': + default: + hls.loadLevel = 0; + break; + } +} + +function switchToHighestLevel (mode) { + let highestLevel = hls.levels.length - 1; + switch (mode) { + case 'current': + hls.currentLevel = highestLevel; + break; + case 'next': + hls.nextLevel = highestLevel; + break; + case 'load': + default: + hls.loadLevel = highestLevel; + break; + } +} + +window.setupConsoleLogRedirection = setupConsoleLogRedirection; +window.startStream = startStream; +window.switchToHighestLevel = switchToHighestLevel; +window.switchToLowestLevel = switchToLowestLevel; diff --git a/tests/mocks/hls.mock.js b/tests/mocks/hls.mock.js index 7a5a66baff2..03b6fde61e3 100644 --- a/tests/mocks/hls.mock.js +++ b/tests/mocks/hls.mock.js @@ -13,14 +13,13 @@ const publicMethods = [ 'startLoad', 'stopLoad', 'swapAudioCodec', - 'recoverMediaError', + 'recoverMediaError' ]; export default class HlsMock { - // TODO: static properties - constructor(config) { + constructor (config) { // Mock arguments can at will override the default config // and have to specify things that are not in the default config this.config = Object.assign({}, Hls.DefaultConfig, config); @@ -34,9 +33,9 @@ export default class HlsMock { /** * Reset all spies */ - __reset__() { + __reset__ () { publicMethods.forEach((methodName) => { this[methodName].reset(); }); } -}; +} diff --git a/tests/test-streams.js b/tests/test-streams.js index e8a59873152..c72c668e133 100644 --- a/tests/test-streams.js +++ b/tests/test-streams.js @@ -7,14 +7,14 @@ * @param {string[]} [blacklist_ua] * @returns {{url: string, description: string, live: boolean, abr: boolean, blacklist_ua: string[]}} */ -function createTestStream(url, description, live = false, abr = true, blacklist_ua = []) { +function createTestStream (url, description, live = false, abr = true, blacklist_ua = []) { return { url, description, live, abr, blacklist_ua - } + }; } /** @@ -22,10 +22,10 @@ function createTestStream(url, description, live = false, abr = true, blacklist_ * @param {Object} [config] * @returns {{url: string, description: string, live: boolean, abr: boolean, blacklist_ua: string[]}} */ -function createTestStreamWithConfig(target, config) { - if (typeof target !== "object") { - throw new Error("target should be object"); - } +function createTestStreamWithConfig (target, config) { + if (typeof target !== 'object') + throw new Error('target should be object'); + const testStream = createTestStream(target.url, target.description, target.live, target.abr, target.blacklist_ua); testStream.config = config; @@ -35,112 +35,112 @@ function createTestStreamWithConfig(target, config) { module.exports = { bbb: createTestStreamWithConfig({ - url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8", - description: "Big Buck Bunny - adaptive qualities", - }, - { - // try to workaround test failing because of slow seek on Chrome/Win10 - nudgeMaxRetry: 5 - } + url: 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8', + description: 'Big Buck Bunny - adaptive qualities' + }, + { + // try to workaround test failing because of slow seek on Chrome/Win10 + nudgeMaxRetry: 5 + } ), bigBuckBunny480p: { - "url": "https://video-dev.github.io/streams/x36xhzz/url_6/193039199_mp4_h264_aac_hq_7.m3u8", - "description": "Big Buck Bunny - 480p only", - "live": false, - "abr": false, - "blacklist_ua": ["internet explorer"] + 'url': 'https://video-dev.github.io/streams/x36xhzz/url_6/193039199_mp4_h264_aac_hq_7.m3u8', + 'description': 'Big Buck Bunny - 480p only', + 'live': false, + 'abr': false, + 'blacklist_ua': ['internet explorer'] }, arte: { - "url": "https://video-dev.github.io/streams/test_001/stream.m3u8", - "description": "ARTE China,ABR", - "live": false, - "abr": true + 'url': 'https://video-dev.github.io/streams/test_001/stream.m3u8', + 'description': 'ARTE China,ABR', + 'live': false, + 'abr': true }, deltatreDAI: { - "url": "https://video-dev.github.io/streams/dai-discontinuity-deltatre/manifest.m3u8", - "description": "Ad-insertion in event stream", - "live": false, - "abr": false, - "blacklist_ua": ["internet explorer"] + 'url': 'https://video-dev.github.io/streams/dai-discontinuity-deltatre/manifest.m3u8', + 'description': 'Ad-insertion in event stream', + 'live': false, + 'abr': false, + 'blacklist_ua': ['internet explorer'] }, issue666: { - "url": "http://www.streambox.fr/playlists/cisq0gim60007xzvi505emlxx.m3u8", - "description": "hls.js/issues/666", - "live": false, - "abr": false, - "blacklist_ua": ["internet explorer"] + 'url': 'http://www.streambox.fr/playlists/cisq0gim60007xzvi505emlxx.m3u8', + 'description': 'hls.js/issues/666', + 'live': false, + 'abr': false, + 'blacklist_ua': ['internet explorer'] }, issue649: { - "url": "http://cdn3.screen9.com/media/c/W/cW87csHkxsgu5TV1qs78aA_auto_hls.m3u8?auth=qlUjeCtbVdtkDfZYrtveTIVUXX1yuSqgF8wfWabzKpX72r-d5upW88-FHuyRRdnZA_1PKRTGAtTt_6Z-aj22kw", - "description": "hls.js/issues/649", - "live": false, - "abr": false + 'url': 'http://cdn3.screen9.com/media/c/W/cW87csHkxsgu5TV1qs78aA_auto_hls.m3u8?auth=qlUjeCtbVdtkDfZYrtveTIVUXX1yuSqgF8wfWabzKpX72r-d5upW88-FHuyRRdnZA_1PKRTGAtTt_6Z-aj22kw', + 'description': 'hls.js/issues/649', + 'live': false, + 'abr': false }, closedCaptions: { - "url": "http://playertest.longtailvideo.com/adaptive/captions/playlist.m3u8", - "description": "CNN special report, with CC", - "live": false, - "abr": false, - "blacklist_ua": ["safari"] + 'url': 'http://playertest.longtailvideo.com/adaptive/captions/playlist.m3u8', + 'description': 'CNN special report, with CC', + 'live': false, + 'abr': false, + 'blacklist_ua': ['safari'] }, oceansAES: { - "url": "http://playertest.longtailvideo.com/adaptive/oceans_aes/oceans_aes.m3u8", - "description": "AES encrypted,ABR", - "live": false, - "abr": true + 'url': 'http://playertest.longtailvideo.com/adaptive/oceans_aes/oceans_aes.m3u8', + 'description': 'AES encrypted,ABR', + 'live': false, + 'abr': true }, bbbAES: { - "url": "http://streambox.fr/playlists/sample_aes/index.m3u8", - "description": "SAMPLE-AES encrypted", - "live": false, - "abr": false + 'url': 'http://streambox.fr/playlists/sample_aes/index.m3u8', + 'description': 'SAMPLE-AES encrypted', + 'live': false, + 'abr': false }, mp3Audio: { - "url": "https://player.webvideocore.net/CL1olYogIrDWvwqiIKK7eLBkzvO18gwo9ERMzsyXzwt_t-ya8ygf2kQBZww38JJT/8i4vvznv8408.m3u8", - "description": "MP3 VOD demo", - "live": false, - "abr": false, - "blacklist_ua": ["safari"] + 'url': 'https://player.webvideocore.net/CL1olYogIrDWvwqiIKK7eLBkzvO18gwo9ERMzsyXzwt_t-ya8ygf2kQBZww38JJT/8i4vvznv8408.m3u8', + 'description': 'MP3 VOD demo', + 'live': false, + 'abr': false, + 'blacklist_ua': ['safari'] }, mpegAudioOnly: { - "url": "https://pl.streamingvideoprovider.com/mp3-playlist/playlist.m3u8", - "description": "MPEG Audio Only demo", - "live": false, - "abr": false, - "blacklist_ua": ["internet explorer", "MicrosoftEdge", "safari", "firefox"] + 'url': 'https://pl.streamingvideoprovider.com/mp3-playlist/playlist.m3u8', + 'description': 'MPEG Audio Only demo', + 'live': false, + 'abr': false, + 'blacklist_ua': ['internet explorer', 'MicrosoftEdge', 'safari', 'firefox'] }, fmp4: { - "url": "https://storage.googleapis.com/shaka-demo-assets/angel-one-hls/hls.m3u8", - "description": "HLS fMP4 Angel-One multiple audio-tracks", - "live": false, - "abr": false, - "blacklist_ua": ["safari", "internet explorer"] + 'url': 'https://storage.googleapis.com/shaka-demo-assets/angel-one-hls/hls.m3u8', + 'description': 'HLS fMP4 Angel-One multiple audio-tracks', + 'live': false, + 'abr': false, + 'blacklist_ua': ['safari', 'internet explorer'] }, fmp4Bitmovin: { - "url": "https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s-fmp4/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8", - "description": "HLS fMP4 by Bitmovin", - "live": false, - "abr": true, - "blacklist_ua": ["safari", "internet explorer"] + 'url': 'https://bitdash-a.akamaihd.net/content/MI201109210084_1/m3u8s-fmp4/f08e80da-bf1d-4e3d-8899-f0f6155f6efa.m3u8', + 'description': 'HLS fMP4 by Bitmovin', + 'live': false, + 'abr': true, + 'blacklist_ua': ['safari', 'internet explorer'] }, offset_pts: { - "url": "https://video-dev.github.io/streams/pts_shift/master.m3u8", - "description": "DK Turntable, PTS shifted by 2.3s", - "live": false, - "abr": false + 'url': 'https://video-dev.github.io/streams/pts_shift/master.m3u8', + 'description': 'DK Turntable, PTS shifted by 2.3s', + 'live': false, + 'abr': false }, uspHLSAteam: createTestStream( - "http://demo.unified-streaming.com/video/ateam/ateam.ism/ateam.m3u8?session_id=27199", - "A-Team movie trailer - HLS by Unified Streaming Platform" + 'http://demo.unified-streaming.com/video/ateam/ateam.ism/ateam.m3u8?session_id=27199', + 'A-Team movie trailer - HLS by Unified Streaming Platform' ), angelOneShakaWidevine: createTestStreamWithConfig({ - url: "https://storage.googleapis.com/shaka-demo-assets/angel-one-widevine-hls/hls.m3u8", - description: "Shaka-packager Widevine DRM (EME) HLS-fMP4 - Angel One Demo", - blacklist_ua: ["firefox","safari", "internet explorer"] - }, - { - widevineLicenseUrl: "https://cwip-shaka-proxy.appspot.com/no_auth", - emeEnabled: true, - } + url: 'https://storage.googleapis.com/shaka-demo-assets/angel-one-widevine-hls/hls.m3u8', + description: 'Shaka-packager Widevine DRM (EME) HLS-fMP4 - Angel One Demo', + blacklist_ua: ['firefox', 'safari', 'internet explorer'] + }, + { + widevineLicenseUrl: 'https://cwip-shaka-proxy.appspot.com/no_auth', + emeEnabled: true + } ) }; diff --git a/tests/unit/controller/abr-controller.js b/tests/unit/controller/abr-controller.js index 85de4373175..494329e918e 100644 --- a/tests/unit/controller/abr-controller.js +++ b/tests/unit/controller/abr-controller.js @@ -4,21 +4,19 @@ import AbrController from '../../../src/controller/abr-controller'; import EwmaBandWidthEstimator from '../../../src/utils/ewma-bandwidth-estimator'; import Hls from '../../../src/hls'; - describe('AbrController', () => { - it('should return correct next auto level', () => { - var hls = new Hls({maxStarvationDelay : 4}); + let hls = new Hls({ maxStarvationDelay: 4 }); hls.levelController._levels = [ - {bitrate:105000, name : "144", details : {totalduration : 4, fragments : [ {} ] }}, - {bitrate:246440, name : "240", details : {totalduration : 10, fragments : [ {} ] }}, - {bitrate:460560, name : "380", details : {totalduration : 10, fragments : [ {} ] }}, - {bitrate:836280, name : "480", details : {totalduration : 10, fragments : [ {} ] }}, - {bitrate:2149280, name : "720", details : {totalduration : 10, fragments : [ {} ] }}, - {bitrate:6221600, name : "1080", details : {totalduration : 10, fragments : [ {} ] }} + { bitrate: 105000, name: '144', details: { totalduration: 4, fragments: [{}] } }, + { bitrate: 246440, name: '240', details: { totalduration: 10, fragments: [ {} ] } }, + { bitrate: 460560, name: '380', details: { totalduration: 10, fragments: [ {} ] } }, + { bitrate: 836280, name: '480', details: { totalduration: 10, fragments: [ {} ] } }, + { bitrate: 2149280, name: '720', details: { totalduration: 10, fragments: [ {} ] } }, + { bitrate: 6221600, name: '1080', details: { totalduration: 10, fragments: [ {} ] } } ]; - var abrController = new AbrController(hls); - abrController.bwEstimator = new EwmaBandWidthEstimator(hls,15,4,5e5); + let abrController = new AbrController(hls); + abrController.bwEstimator = new EwmaBandWidthEstimator(hls, 15, 4, 5e5); assert.equal(abrController.nextAutoLevel, 0); }); }); diff --git a/tests/unit/controller/cap-level-controller.js b/tests/unit/controller/cap-level-controller.js index 95eee383c06..21d542ce4bc 100644 --- a/tests/unit/controller/cap-level-controller.js +++ b/tests/unit/controller/cap-level-controller.js @@ -11,12 +11,12 @@ const levels = [ { width: 540, height: 540, - bandwidth: 2000, + bandwidth: 2000 }, { width: 540, height: 540, - bandwidth: 3000, + bandwidth: 3000 }, { width: 720, diff --git a/tests/unit/controller/eme-controller.js b/tests/unit/controller/eme-controller.js index 5340109600d..fc166e29c39 100644 --- a/tests/unit/controller/eme-controller.js +++ b/tests/unit/controller/eme-controller.js @@ -2,9 +2,9 @@ import EMEController from '../../../src/controller/eme-controller'; import assert from 'assert'; import HlsMock from '../../mocks/hls.mock'; import EventEmitter from 'events'; -import {ErrorTypes, ErrorDetails} from '../../../src/errors'; +import { ErrorTypes, ErrorDetails } from '../../../src/errors'; -const MediaMock = function() { +const MediaMock = function () { let media = new EventEmitter(); media.setMediaKeys = sinon.spy(); media.addEventListener = media.addListener.bind(media); @@ -23,14 +23,13 @@ const fakeLevels = [ let emeController; let media; -const setupEach = function(config) { +const setupEach = function (config) { media = new MediaMock(); emeController = new EMEController(new HlsMock(config)); -} +}; describe('EMEController', () => { - beforeEach(() => { setupEach(); }); @@ -38,54 +37,50 @@ describe('EMEController', () => { it('should be constructable with an unconfigured Hls.js instance', () => {}); it('should not do anything when `emeEnabled` is false (default)', () => { - let reqMediaKsAccessSpy = sinon.spy(); setupEach({ requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy }); - emeController.onMediaAttached({media}); - emeController.onManifestParsed({media}); + emeController.onMediaAttached({ media }); + emeController.onManifestParsed({ media }); media.setMediaKeys.callCount.should.be.equal(0); reqMediaKsAccessSpy.callCount.should.be.equal(0); }); it('should request keys when `emeEnabled` is true (but not set them)', (done) => { - - let reqMediaKsAccessSpy = sinon.spy(() => { - return Promise.resolve({ - // Media-keys mock - }) - }); - - setupEach({ - emeEnabled: true, - requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy + let reqMediaKsAccessSpy = sinon.spy(() => { + return Promise.resolve({ + // Media-keys mock }); + }); - emeController.onMediaAttached({media}); + setupEach({ + emeEnabled: true, + requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy + }); - media.setMediaKeys.callCount.should.be.equal(0); - reqMediaKsAccessSpy.callCount.should.be.equal(0); + emeController.onMediaAttached({ media }); - emeController.onManifestParsed({levels: fakeLevels}); + media.setMediaKeys.callCount.should.be.equal(0); + reqMediaKsAccessSpy.callCount.should.be.equal(0); - setTimeout(() => { - media.setMediaKeys.callCount.should.be.equal(0); - reqMediaKsAccessSpy.callCount.should.be.equal(1); - done(); - }, 0) + emeController.onManifestParsed({ levels: fakeLevels }); + setTimeout(() => { + media.setMediaKeys.callCount.should.be.equal(0); + reqMediaKsAccessSpy.callCount.should.be.equal(1); + done(); + }, 0); }); it('should trigger key system error when bad encrypted data is received', (done) => { - let reqMediaKsAccessSpy = sinon.spy(() => { return Promise.resolve({ // Media-keys mock - }) + }); }); setupEach({ @@ -94,12 +89,12 @@ describe('EMEController', () => { }); let badData = { - initDataType: "cenc", - initData: "bad data" + initDataType: 'cenc', + initData: 'bad data' }; - emeController.onMediaAttached({media}); - emeController.onManifestParsed({levels: fakeLevels}); + emeController.onMediaAttached({ media }); + emeController.onManifestParsed({ levels: fakeLevels }); media.emit('encrypted', badData); @@ -107,8 +102,6 @@ describe('EMEController', () => { assert.equal(emeController.hls.trigger.args[0][1].details, ErrorDetails.KEY_SYSTEM_NO_KEYS); assert.equal(emeController.hls.trigger.args[1][1].details, ErrorDetails.KEY_SYSTEM_NO_ACCESS); done(); - }, 0) - + }, 0); }); - -}) +}); diff --git a/tests/unit/controller/ewma-bandwidth-estimator.js b/tests/unit/controller/ewma-bandwidth-estimator.js index 2d4a5130be6..2ccd8835d70 100644 --- a/tests/unit/controller/ewma-bandwidth-estimator.js +++ b/tests/unit/controller/ewma-bandwidth-estimator.js @@ -3,47 +3,43 @@ const assert = require('assert'); import EwmaBandWidthEstimator from '../../../src/utils/ewma-bandwidth-estimator'; import Hls from '../../../src/hls'; - describe('EwmaBandWidthEstimator', () => { - it('returns default estimate if bw estimator not available yet', () => { - var defaultEstimate = 5e5; - var bwEstimator = new EwmaBandWidthEstimator(new Hls(),0,0,defaultEstimate); + let defaultEstimate = 5e5; + let bwEstimator = new EwmaBandWidthEstimator(new Hls(), 0, 0, defaultEstimate); assert.equal(bwEstimator.getEstimate(), 5e5); }); it('returns last bitrate is fast=slow=0', () => { - var defaultEstimate = 5e5; - var bwEstimator = new EwmaBandWidthEstimator(new Hls(),0,0,defaultEstimate); - bwEstimator.sample(8000,1000000); + let defaultEstimate = 5e5; + let bwEstimator = new EwmaBandWidthEstimator(new Hls(), 0, 0, defaultEstimate); + bwEstimator.sample(8000, 1000000); assert.equal(bwEstimator.getEstimate(), 1000000); - bwEstimator.sample(4000,1000000); + bwEstimator.sample(4000, 1000000); assert.equal(bwEstimator.getEstimate(), 2000000); - bwEstimator.sample(1000,1000000); + bwEstimator.sample(1000, 1000000); assert.equal(bwEstimator.getEstimate(), 8000000); }); it('returns correct value bitrate is slow=15,fast=4', () => { - var defaultEstimate = 5e5; - var bwEstimator = new EwmaBandWidthEstimator(new Hls(),15,4,defaultEstimate); - bwEstimator.sample(8000,1000000); + let defaultEstimate = 5e5; + let bwEstimator = new EwmaBandWidthEstimator(new Hls(), 15, 4, defaultEstimate); + bwEstimator.sample(8000, 1000000); assert.equal(bwEstimator.getEstimate(), 1000000); - bwEstimator.sample(4000,1000000); + bwEstimator.sample(4000, 1000000); assert.equal(Math.round(bwEstimator.getEstimate()), 1396480); - bwEstimator.sample(1000,1000000); + bwEstimator.sample(1000, 1000000); assert.equal(Math.round(bwEstimator.getEstimate()), 2056827); }); it('returns correct value bitrate is slow=9,fast=5', () => { - var defaultEstimate = 5e5; - var bwEstimator = new EwmaBandWidthEstimator(new Hls(),9,5,defaultEstimate); - bwEstimator.sample(8000,1000000); + let defaultEstimate = 5e5; + let bwEstimator = new EwmaBandWidthEstimator(new Hls(), 9, 5, defaultEstimate); + bwEstimator.sample(8000, 1000000); assert.equal(bwEstimator.getEstimate(), 1000000); - bwEstimator.sample(4000,1000000); + bwEstimator.sample(4000, 1000000); assert.equal(Math.round(bwEstimator.getEstimate()), 1439580); - bwEstimator.sample(1000,1000000); + bwEstimator.sample(1000, 1000000); assert.equal(Math.round(bwEstimator.getEstimate()), 2208342); }); - - }); diff --git a/tests/unit/controller/level-controller.js b/tests/unit/controller/level-controller.js index 16a8bb80697..e6f92ced394 100644 --- a/tests/unit/controller/level-controller.js +++ b/tests/unit/controller/level-controller.js @@ -4,90 +4,79 @@ const sinon = require('sinon'); import LevelController from '../../../src/controller/level-controller'; import HlsMock from '../../mocks/hls.mock'; import Event from '../../../src/events'; -import { ErrorTypes, ErrorDetails } from '../../../src/errors' - - +import { ErrorTypes, ErrorDetails } from '../../../src/errors'; describe('LevelController', () => { - let hls, levelController; - - beforeEach(() => { - hls = new HlsMock(); - levelController = new LevelController(hls); - }); - - afterEach(() => { - hls.destroy(); - hls = null; - levelController = null; - }); - - it('should trigger an error when no levels are found in the manifest', () => { - - levelController.onManifestLoaded({ - audioTracks: [], - levels: [], - networkDetails: "", - subtitles: [], - url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8" - }); - - assert.equal(levelController.hls.trigger.args[0][1].details, ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR); - - }); - - it('should trigger hlsManifestParsed when levels are found in the manifest', () => { - - let data = { - audioTracks: [], - levels: [ - { bitrate: 105000, name: "144", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 246440, name: "240", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 460560, name: "380", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 836280, name: "480", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 2149280, name: "720", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 6221600, name: "1080", details: { totalduration: 10, fragments: [{}] } } - ], - networkDetails: "", - subtitles: [], - stats: {}, - url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8" - }; - - levelController.onManifestLoaded(data); - - - assert.equal(levelController.hls.trigger.args[0][0], Event.MANIFEST_PARSED); - assert.equal(levelController.hls.trigger.args[0][1].levels.length, data.levels.length); - - - }); - - it('should trigger level switch when level is manually set', () => { - - let data = { - audioTracks: [], - levels: [ - { bitrate: 105000, name: "144", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 246440, name: "240", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 460560, name: "380", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 836280, name: "480", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 2149280, name: "720", details: { totalduration: 10, fragments: [{}] } }, - { bitrate: 6221600, name: "1080", details: { totalduration: 10, fragments: [{}] } } - ], - networkDetails: "", - subtitles: [], - url: "https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8" - }; - - let nextLevel = 1; - - levelController.onManifestLoaded(data); - levelController.level = nextLevel; - - assert.equal(levelController.hls.trigger.args[1][0],Event.LEVEL_SWITCH); - assert.equal(levelController.hls.trigger.args[1][1].level, nextLevel); - + let hls, levelController; + + beforeEach(() => { + hls = new HlsMock(); + levelController = new LevelController(hls); + }); + + afterEach(() => { + hls.destroy(); + hls = null; + levelController = null; + }); + + it('should trigger an error when no levels are found in the manifest', () => { + levelController.onManifestLoaded({ + audioTracks: [], + levels: [], + networkDetails: '', + subtitles: [], + url: 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8' }); + assert.equal(levelController.hls.trigger.args[0][1].details, ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR); + }); + + it('should trigger hlsManifestParsed when levels are found in the manifest', () => { + let data = { + audioTracks: [], + levels: [ + { bitrate: 105000, name: '144', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 246440, name: '240', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 460560, name: '380', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 836280, name: '480', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 2149280, name: '720', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 6221600, name: '1080', details: { totalduration: 10, fragments: [{}] } } + ], + networkDetails: '', + subtitles: [], + stats: {}, + url: 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8' + }; + + levelController.onManifestLoaded(data); + + assert.equal(levelController.hls.trigger.args[0][0], Event.MANIFEST_PARSED); + assert.equal(levelController.hls.trigger.args[0][1].levels.length, data.levels.length); + }); + + it('should trigger level switch when level is manually set', () => { + let data = { + audioTracks: [], + levels: [ + { bitrate: 105000, name: '144', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 246440, name: '240', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 460560, name: '380', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 836280, name: '480', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 2149280, name: '720', details: { totalduration: 10, fragments: [{}] } }, + { bitrate: 6221600, name: '1080', details: { totalduration: 10, fragments: [{}] } } + ], + networkDetails: '', + subtitles: [], + url: 'https://video-dev.github.io/streams/x36xhzz/x36xhzz.m3u8' + }; + + let nextLevel = 1; + + levelController.onManifestLoaded(data); + levelController.level = nextLevel; + + assert.equal(levelController.hls.trigger.args[1][0], Event.LEVEL_SWITCH); + assert.equal(levelController.hls.trigger.args[1][1].level, nextLevel); + }); }); diff --git a/tests/unit/controller/stream-controller.js b/tests/unit/controller/stream-controller.js index 5dcb13ac72e..fddb8991044 100644 --- a/tests/unit/controller/stream-controller.js +++ b/tests/unit/controller/stream-controller.js @@ -1,14 +1,12 @@ -import assert from "assert"; -import sinon from "sinon"; -import Hls from "../../../src/hls"; -import Event from "../../../src/events"; -import { FragmentTracker } from "../../../src/helper/fragment-tracker"; -import StreamController, { State } from "../../../src/controller/stream-controller"; -import M3U8Parser from "../../../src/loader/m3u8-parser"; - - -describe('StreamController tests', function() { - +import assert from 'assert'; +import sinon from 'sinon'; +import Hls from '../../../src/hls'; +import Event from '../../../src/events'; +import { FragmentTracker } from '../../../src/helper/fragment-tracker'; +import StreamController, { State } from '../../../src/controller/stream-controller'; +import M3U8Parser from '../../../src/loader/m3u8-parser'; + +describe('StreamController tests', function () { /** * Create StreamController instance with initial setting * @returns {{hls: Hls, streamController: StreamController}} @@ -27,8 +25,8 @@ describe('StreamController tests', function() { * @param {StreamController} streamController */ const assertStreamControllerStarted = (streamController) => { - assert.equal(streamController.hasInterval(), true, "StreamController should start interval"); - assert.notDeepEqual(streamController.state, State.STOPPED, "StreamController's state should not be STOPPED"); + assert.equal(streamController.hasInterval(), true, 'StreamController should start interval'); + assert.notDeepEqual(streamController.state, State.STOPPED, 'StreamController\'s state should not be STOPPED'); }; /** @@ -36,17 +34,17 @@ describe('StreamController tests', function() { * @param {StreamController} streamController */ const assertStreamControllerStopped = (streamController) => { - assert.equal(streamController.hasInterval(), false, "StreamController should stop interval"); - assert.equal(streamController.state, State.STOPPED, "StreamController's state should be STOPPED"); + assert.equal(streamController.hasInterval(), false, 'StreamController should stop interval'); + assert.equal(streamController.state, State.STOPPED, 'StreamController\'s state should be STOPPED'); }; - describe("StreamController", function() { - it("should be STOPPED when it is initialized", function() { + describe('StreamController', function () { + it('should be STOPPED when it is initialized', function () { const { streamController } = createStreamController(); assertStreamControllerStopped(streamController); }); - it("should trigger STREAM_STATE_TRANSITION when state is updated", function() { + it('should trigger STREAM_STATE_TRANSITION when state is updated', function () { const { hls, streamController } = createStreamController(); const spy = sinon.spy(); hls.on(Event.STREAM_STATE_TRANSITION, spy); @@ -54,7 +52,7 @@ describe('StreamController tests', function() { assert.deepEqual(spy.args[0][1], { previousState: State.STOPPED, nextState: State.ENDED }); }); - it("should not trigger STREAM_STATE_TRANSITION when state is not updated", function() { + it('should not trigger STREAM_STATE_TRANSITION when state is not updated', function () { const { hls, streamController } = createStreamController(); const spy = sinon.spy(); hls.on(Event.STREAM_STATE_TRANSITION, spy); @@ -63,13 +61,13 @@ describe('StreamController tests', function() { assert.equal(spy.called, false); }); - it("should not start when controller have not levels data", function() { + it('should not start when controller have not levels data', function () { const { streamController } = createStreamController(); streamController.startLoad(1); assertStreamControllerStopped(streamController); }); - it("should start when controller have levels data", function() { + it('should start when controller have levels data', function () { const { streamController } = createStreamController(); const manifest = `#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,RESOLUTION=848x360,NAME="480" @@ -82,179 +80,174 @@ describe('StreamController tests', function() { streamController.startLoad(1); assertStreamControllerStarted(streamController); streamController.stopLoad(); - assertStreamControllerStopped(streamController) + assertStreamControllerStopped(streamController); }); }); - describe('PDT vs SN tests for discontinuities with PDT', function() { - var fragPrevious = { - pdt : 1505502671523, - endPdt : 1505502676523, - duration : 5000, - level : 1, - start : 10000, - sn : 2, - cc : 0 - } - - var fragments = [ - { - pdt : 1505502661523, - endPdt : 1505502666523, - level : 2, - duration : 5000, - start : 0, - sn : 0, - cc : 0 - }, - //Discontinuity with PDT 1505502671523 - { - pdt : 1505502671523, - endPdt : 1505502676523, - level : 2, - duration : 5000, - start : 5000, - sn : 2, - cc : 1 - }, - { - pdt : 1505502676523, - endPdt : 1505502681523, - level : 2, - duration : 5000, - start : 10000, - sn : 3, - cc : 1 - }, - { - pdt : 1505502681523, - endPdt : 1505502686523, - level : 2, - duration : 5000, - start : 15000, - sn : 4, - cc : 1 - }, - { - pdt : 1505502686523, - endPdt : 1505502691523, - level : 2, - duration : 5000, - start : 20000, - sn : 5, - cc : 1 - } - ]; - - var fragLen = fragments.length; - var levelDetails ={ - startSN : fragments[0].sn, - endSN : fragments[fragments.length - 1].sn, - programDateTime : undefined //If this field is undefined SN search is used by default - }; - var bufferEnd = fragPrevious.start + fragPrevious.duration; - var end = fragments[fragments.length - 1].start + fragments[fragments.length - 1].duration; - - it('SN search choosing fragment after level loaded', function () { - var config = {}; - var hls = { - config : config, - on : function(){} - }; - - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragment(0, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails); - - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[3], "Expected sn 4, found sn segment " + resultSN); - - }); - - it('PDT search choosing fragment after level loaded', function () { - var config = {}; - var hls = { - config : config, - on : function(){} - }; - levelDetails.programDateTime = true;// If programDateTime contains a date then PDT is used (boolean used to mock) - - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragment(0, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails); - - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[2], "Expected sn 3, found sn segment " + resultSN); - - }); - - it('Unit test _findFragmentBySN', function () { - var config = { }; - var hls = { - config : config, - on : function(){} - }; - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragmentBySN(fragPrevious, fragments, bufferEnd, end); + describe('PDT vs SN tests for discontinuities with PDT', function () { + let fragPrevious = { + pdt: 1505502671523, + endPdt: 1505502676523, + duration: 5000, + level: 1, + start: 10000, + sn: 2, + cc: 0 + }; - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[3], "Expected sn 4, found sn segment " + resultSN); + let fragments = [ + { + pdt: 1505502661523, + endPdt: 1505502666523, + level: 2, + duration: 5000, + start: 0, + sn: 0, + cc: 0 + }, + // Discontinuity with PDT 1505502671523 + { + pdt: 1505502671523, + endPdt: 1505502676523, + level: 2, + duration: 5000, + start: 5000, + sn: 2, + cc: 1 + }, + { + pdt: 1505502676523, + endPdt: 1505502681523, + level: 2, + duration: 5000, + start: 10000, + sn: 3, + cc: 1 + }, + { + pdt: 1505502681523, + endPdt: 1505502686523, + level: 2, + duration: 5000, + start: 15000, + sn: 4, + cc: 1 + }, + { + pdt: 1505502686523, + endPdt: 1505502691523, + level: 2, + duration: 5000, + start: 20000, + sn: 5, + cc: 1 + } + ]; + + let fragLen = fragments.length; + let levelDetails = { + startSN: fragments[0].sn, + endSN: fragments[fragments.length - 1].sn, + programDateTime: undefined // If this field is undefined SN search is used by default + }; + let bufferEnd = fragPrevious.start + fragPrevious.duration; + let end = fragments[fragments.length - 1].start + fragments[fragments.length - 1].duration; - }); + it('SN search choosing fragment after level loaded', function () { + let config = {}; + let hls = { + config: config, + on: function () {} + }; - it('Unit test _findFragmentByPDT usual behaviour', function () { - var config = { }; - var hls = { - config : config, - on : function(){} - }; - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragmentByPDT(fragments, fragPrevious.endPdt + 1); + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragment(0, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails); - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[2], "Expected sn 3, found sn segment " + resultSN); + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[3], 'Expected sn 4, found sn segment ' + resultSN); + }); - }); + it('PDT search choosing fragment after level loaded', function () { + let config = {}; + let hls = { + config: config, + on: function () {} + }; + levelDetails.programDateTime = true;// If programDateTime contains a date then PDT is used (boolean used to mock) - it('Unit test _findFragmentByPDT beyond limits', function () { - var config = { }; - var hls = { - config : config, - on : function(){} - }; - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragmentByPDT(fragments, fragments[0].pdt - 1); - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, null, "Expected sn -1, found sn segment " + resultSN); + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragment(0, fragPrevious, fragLen, fragments, bufferEnd, end, levelDetails); - foundFragment = streamController._findFragmentByPDT(fragments, fragments[fragments.length - 1].endPdt + 1); - resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, null, "Expected sn -1, found sn segment " + resultSN); - }); + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[2], 'Expected sn 3, found sn segment ' + resultSN); + }); - it('Unit test _findFragmentByPDT at the beginning', function () { - var config = { }; - var hls = { - config : config, - on : function(){} - }; - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragmentByPDT(fragments, fragments[0].pdt); + it('Unit test _findFragmentBySN', function () { + let config = { }; + let hls = { + config: config, + on: function () {} + }; + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragmentBySN(fragPrevious, fragments, bufferEnd, end); + + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[3], 'Expected sn 4, found sn segment ' + resultSN); + }); - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[0], "Expected sn 1, found sn segment " + resultSN); - }); + it('Unit test _findFragmentByPDT usual behaviour', function () { + let config = { }; + let hls = { + config: config, + on: function () {} + }; + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragmentByPDT(fragments, fragPrevious.endPdt + 1); + + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[2], 'Expected sn 3, found sn segment ' + resultSN); + }); - it('Unit test _findFragmentByPDT for last segment', function () { - var config = { }; - var hls = { - config : config, - on : function(){} - }; - var streamController = new StreamController(hls); - var foundFragment = streamController._findFragmentByPDT(fragments, fragments[fragments.length - 1].pdt ); + it('Unit test _findFragmentByPDT beyond limits', function () { + let config = { }; + let hls = { + config: config, + on: function () {} + }; + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragmentByPDT(fragments, fragments[0].pdt - 1); + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, null, 'Expected sn -1, found sn segment ' + resultSN); + + foundFragment = streamController._findFragmentByPDT(fragments, fragments[fragments.length - 1].endPdt + 1); + resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, null, 'Expected sn -1, found sn segment ' + resultSN); + }); - var resultSN = foundFragment ? foundFragment.sn : -1; - assert.equal(foundFragment, fragments[4], "Expected sn 5, found sn segment " + resultSN); - }); - }); + it('Unit test _findFragmentByPDT at the beginning', function () { + let config = { }; + let hls = { + config: config, + on: function () {} + }; + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragmentByPDT(fragments, fragments[0].pdt); + + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[0], 'Expected sn 1, found sn segment ' + resultSN); + }); + it('Unit test _findFragmentByPDT for last segment', function () { + let config = { }; + let hls = { + config: config, + on: function () {} + }; + let streamController = new StreamController(hls); + let foundFragment = streamController._findFragmentByPDT(fragments, fragments[fragments.length - 1].pdt); + + let resultSN = foundFragment ? foundFragment.sn : -1; + assert.equal(foundFragment, fragments[4], 'Expected sn 5, found sn segment ' + resultSN); + }); + }); }); diff --git a/tests/unit/controller/subtitle-track-controller.js b/tests/unit/controller/subtitle-track-controller.js index 8b358c27548..98e21b8285e 100644 --- a/tests/unit/controller/subtitle-track-controller.js +++ b/tests/unit/controller/subtitle-track-controller.js @@ -4,7 +4,6 @@ import SubtitleTrackController from '../../../src/controller/subtitle-track-cont import Hls from '../../../src/hls'; describe('SubtitleTrackController', () => { - let subtitleTrackController; let videoElement; diff --git a/tests/unit/controller/timeline-controller.js b/tests/unit/controller/timeline-controller.js index 514bff11b0f..99880fcbcca 100644 --- a/tests/unit/controller/timeline-controller.js +++ b/tests/unit/controller/timeline-controller.js @@ -4,7 +4,6 @@ import TimelineController from '../../../src/controller/timeline-controller'; import Hls from '../../../src/hls'; describe('TimelineController', () => { - let timelineController; let hls; @@ -13,7 +12,6 @@ describe('TimelineController', () => { hls.config.enableWebVTT = true; timelineController = new TimelineController(hls); timelineController.media = document.createElement('video'); - }); it('should set default track to showing when displaySubtitles is true', () => { diff --git a/tests/unit/demuxer/demuxer.js b/tests/unit/demuxer/demuxer.js index 059d17ce32a..512a2fa2053 100644 --- a/tests/unit/demuxer/demuxer.js +++ b/tests/unit/demuxer/demuxer.js @@ -4,257 +4,247 @@ const sinon = require('sinon'); import Event from '../../../src/events.js'; import Demuxer from '../../../src/demux/demuxer.js'; -describe('Demuxer tests', function() { - +describe('Demuxer tests', function () { it('Demuxer constructor no worker', function () { - var config = { enableWorker : false }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - - assert.equal(demux.hls, hls, "Hls object created"); - assert.equal(demux.id, id, "Id has been set up"); - - assert.ok(demux.observer.trigger, "Observer trigger set up"); - assert.ok(demux.observer.off, "Observer off set up"); - assert.ok(demux.demuxer, "Demuxer set up"); + let config = { enableWorker: false }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + + assert.equal(demux.hls, hls, 'Hls object created'); + assert.equal(demux.id, id, 'Id has been set up'); + + assert.ok(demux.observer.trigger, 'Observer trigger set up'); + assert.ok(demux.observer.off, 'Observer off set up'); + assert.ok(demux.demuxer, 'Demuxer set up'); }); - + it('Demuxer constructor with worker', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - - assert.equal(demux.hls, hls, "Hls object created"); - assert.equal(demux.id, id, "Id has been set up"); - - assert.ok(demux.observer.trigger, "Observer trigger set up"); - assert.ok(demux.observer.off, "Observer off set up"); - assert.ok(demux.w, "Worker set up"); + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + + assert.equal(demux.hls, hls, 'Hls object created'); + assert.equal(demux.id, id, 'Id has been set up'); + + assert.ok(demux.observer.trigger, 'Observer trigger set up'); + assert.ok(demux.observer.off, 'Observer off set up'); + assert.ok(demux.w, 'Worker set up'); }); - + it('Destroy demuxer worker', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - demux.destroy(); - - assert.equal(demux.observer, null, "Observer destroyed"); - assert.equal(demux.demuxer, null, "Demuxer destroyed"); - assert.equal(demux.w, null, "Worker destroyed"); - }); - - + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + demux.destroy(); + + assert.equal(demux.observer, null, 'Observer destroyed'); + assert.equal(demux.demuxer, null, 'Demuxer destroyed'); + assert.equal(demux.w, null, 'Worker destroyed'); + }); + it('Destroy demuxer no worker', function () { - var config = { enableWorker : false }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - demux.destroy(); - - assert.equal(demux.observer, null, "Observer destroyed"); - assert.equal(demux.demuxer, null, "Demuxer destroyed"); - assert.equal(demux.w, null, "Worker destroyed"); - }); - + let config = { enableWorker: false }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + demux.destroy(); + + assert.equal(demux.observer, null, 'Observer destroyed'); + assert.equal(demux.demuxer, null, 'Demuxer destroyed'); + assert.equal(demux.w, null, 'Worker destroyed'); + }); + it('Push data to demuxer with worker', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - var currentFrag = { - cc : 100, - sn : 5, - level : 1 - } - //Config for push - demux.frag = currentFrag; - - var newFrag = { - decryptdata : {}, - cc : 100, - sn : 6, - level : 1, - startDTS : 1000, - start : undefined - } - var data = new ArrayBuffer(8), - initSegment = {}, - audioCodec = {}, - videoCodec = {}, - duration = {}, - accurateTimeOffset = {}, - defaultInitPTS = {}; - - - var stub = sinon.stub(demux.w, "postMessage").callsFake(function(obj1, obj2){ - assert.equal(obj1.cmd, 'demux', "cmd"); - assert.equal(obj1.data, data, "data"); - assert.equal(obj1.decryptdata, newFrag.decryptdata, "decryptdata"); - assert.equal(obj1.initSegment, initSegment, "initSegment"); - assert.equal(obj1.audioCodec, audioCodec, "audioCodec"); - assert.equal(obj1.videoCodec, videoCodec, "videoCodec"); - assert.equal(obj1.timeOffset, newFrag.startDTS, "timeOffset"); - assert.equal(obj1.discontinuity, false, "discontinuity"); - assert.equal(obj1.trackSwitch, false, "trackSwitch"); - assert.equal(obj1.contiguous, true, "contiguous"); - assert.equal(obj1.duration, duration, "duration"); - assert.equal(obj1.defaultInitPTS, defaultInitPTS, "defaultInitPTS"); - assert.equal(obj2[0], data, "ArrayBuffer"); - }); - - demux.push(data, initSegment, audioCodec, videoCodec, newFrag, duration,accurateTimeOffset,defaultInitPTS); - - assert.ok(stub.calledOnce, "postMessage called once"); - }); - + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + let currentFrag = { + cc: 100, + sn: 5, + level: 1 + }; + // Config for push + demux.frag = currentFrag; + + let newFrag = { + decryptdata: {}, + cc: 100, + sn: 6, + level: 1, + startDTS: 1000, + start: undefined + }; + let data = new ArrayBuffer(8), + initSegment = {}, + audioCodec = {}, + videoCodec = {}, + duration = {}, + accurateTimeOffset = {}, + defaultInitPTS = {}; + + let stub = sinon.stub(demux.w, 'postMessage').callsFake(function (obj1, obj2) { + assert.equal(obj1.cmd, 'demux', 'cmd'); + assert.equal(obj1.data, data, 'data'); + assert.equal(obj1.decryptdata, newFrag.decryptdata, 'decryptdata'); + assert.equal(obj1.initSegment, initSegment, 'initSegment'); + assert.equal(obj1.audioCodec, audioCodec, 'audioCodec'); + assert.equal(obj1.videoCodec, videoCodec, 'videoCodec'); + assert.equal(obj1.timeOffset, newFrag.startDTS, 'timeOffset'); + assert.equal(obj1.discontinuity, false, 'discontinuity'); + assert.equal(obj1.trackSwitch, false, 'trackSwitch'); + assert.equal(obj1.contiguous, true, 'contiguous'); + assert.equal(obj1.duration, duration, 'duration'); + assert.equal(obj1.defaultInitPTS, defaultInitPTS, 'defaultInitPTS'); + assert.equal(obj2[0], data, 'ArrayBuffer'); + }); + + demux.push(data, initSegment, audioCodec, videoCodec, newFrag, duration, accurateTimeOffset, defaultInitPTS); + + assert.ok(stub.calledOnce, 'postMessage called once'); + }); + it('Push data to demuxer with no worker', function () { - var config = { enableWorker : false }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - var currentFrag = { - cc : 100, - sn : 5, - level : 1 - } - //Config for push - demux.frag = currentFrag; - - var newFrag = { - decryptdata : {}, - cc : 200, - sn : 5, - level : 2, - startDTS : undefined, - start : 1000 - } - var data = {}, - initSegment = {}, - audioCodec = {}, - videoCodec = {}, - duration = {}, - accurateTimeOffset = {}, - defaultInitPTS = {}; - - - var stub = sinon.stub(demux.demuxer, "push").callsFake(function(obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13){ - assert.equal(obj1, data); - assert.equal(obj2, newFrag.decryptdata); - assert.equal(obj3, initSegment); - assert.equal(obj4, audioCodec); - assert.equal(obj5, videoCodec); - assert.equal(obj6, newFrag.start); - assert.equal(obj7, true); - assert.equal(obj8, true); - assert.equal(obj9, false); - assert.equal(obj10, duration); - assert.equal(obj11, accurateTimeOffset); - assert.equal(obj12, defaultInitPTS); - }); - - demux.push(data, initSegment, audioCodec, videoCodec, newFrag, duration,accurateTimeOffset,defaultInitPTS); - - - assert.ok(stub.calledWith(data, newFrag.decryptdata, initSegment, audioCodec, videoCodec, newFrag.start, true, true, false, duration, accurateTimeOffset, defaultInitPTS)); - - }); - - + let config = { enableWorker: false }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + let currentFrag = { + cc: 100, + sn: 5, + level: 1 + }; + // Config for push + demux.frag = currentFrag; + + let newFrag = { + decryptdata: {}, + cc: 200, + sn: 5, + level: 2, + startDTS: undefined, + start: 1000 + }; + let data = {}, + initSegment = {}, + audioCodec = {}, + videoCodec = {}, + duration = {}, + accurateTimeOffset = {}, + defaultInitPTS = {}; + + let stub = sinon.stub(demux.demuxer, 'push').callsFake(function (obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13) { + assert.equal(obj1, data); + assert.equal(obj2, newFrag.decryptdata); + assert.equal(obj3, initSegment); + assert.equal(obj4, audioCodec); + assert.equal(obj5, videoCodec); + assert.equal(obj6, newFrag.start); + assert.equal(obj7, true); + assert.equal(obj8, true); + assert.equal(obj9, false); + assert.equal(obj10, duration); + assert.equal(obj11, accurateTimeOffset); + assert.equal(obj12, defaultInitPTS); + }); + + demux.push(data, initSegment, audioCodec, videoCodec, newFrag, duration, accurateTimeOffset, defaultInitPTS); + + assert.ok(stub.calledWith(data, newFrag.decryptdata, initSegment, audioCodec, videoCodec, newFrag.start, true, true, false, duration, accurateTimeOffset, defaultInitPTS)); + }); + it('Sent worker generic message', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(event, data){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - demux.frag = {}; - - var evt = { - data : { - event : {}, - data : {} - } - } - - hls.trigger = function(event, data){ - assert.equal(event, evt.data.event); - assert.equal(data, evt.data.data); - assert.equal(demux.frag, evt.data.data.frag); - assert.equal(id, evt.data.data.id); - } - - demux.onWorkerMessage(evt); - - }); + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function (event, data) {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + demux.frag = {}; + + let evt = { + data: { + event: {}, + data: {} + } + }; + + hls.trigger = function (event, data) { + assert.equal(event, evt.data.event); + assert.equal(data, evt.data.data); + assert.equal(demux.frag, evt.data.data.frag); + assert.equal(id, evt.data.data.id); + }; + + demux.onWorkerMessage(evt); + }); it('Sent worker message type main', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(event, data){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - - var evt = { - data : { - event : 'init', - data : {} - } - } - - var spy = sinon.spy(URL, "revokeObjectURL"); - - demux.onWorkerMessage(evt); - - assert.ok(spy.calledOnce); - - }); - + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function (event, data) {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + + let evt = { + data: { + event: 'init', + data: {} + } + }; + + let spy = sinon.spy(URL, 'revokeObjectURL'); + + demux.onWorkerMessage(evt); + + assert.ok(spy.calledOnce); + }); + it('Sent worker message FRAG_PARSING_DATA', function () { - var config = { enableWorker : true }; //Option debug : true crashes mocha - var hls = { - trigger : function(){}, - config :config - }; - var id = 'main'; - var demux = new Demuxer(hls, id); - - var evt = { - data : { - event : Event.FRAG_PARSING_DATA, - data : {}, - data1 : {}, - data2 : {} - } - } - - demux.onWorkerMessage(evt); - - assert.ok(evt.data.data.data1); - assert.ok(evt.data.data.data2); - }); - + let config = { enableWorker: true }; // Option debug : true crashes mocha + let hls = { + trigger: function () {}, + config: config + }; + let id = 'main'; + let demux = new Demuxer(hls, id); + + let evt = { + data: { + event: Event.FRAG_PARSING_DATA, + data: {}, + data1: {}, + data2: {} + } + }; + + demux.onWorkerMessage(evt); + + assert.ok(evt.data.data.data1); + assert.ok(evt.data.data.data2); + }); }); diff --git a/tests/unit/dummy.js b/tests/unit/dummy.js index 46e2abfb595..78604f96599 100644 --- a/tests/unit/dummy.js +++ b/tests/unit/dummy.js @@ -1,8 +1,8 @@ -var assert = require("assert"); -describe('dummy test', function() { +let assert = require('assert'); +describe('dummy test', function () { describe('#1', function () { it('always passing', function () { - assert.equal(true,true); + assert.equal(true, true); }); }); }); diff --git a/tests/unit/events.js b/tests/unit/events.js index c0a67ff15f3..5da36ae8687 100644 --- a/tests/unit/events.js +++ b/tests/unit/events.js @@ -1,15 +1,15 @@ -var assert = require("assert"); +let assert = require('assert'); import Events from '../../src/events'; -function getAllCapsSnakeCaseToCamelCase(eventType) { - var eventValue = ""; - var previousWasUscore, nextChar; +function getAllCapsSnakeCaseToCamelCase (eventType) { + let eventValue = ''; + let previousWasUscore, nextChar; - for(var i=0;i<eventType.length;i++) { + for (let i = 0; i < eventType.length; i++) { nextChar = eventType.charAt(i); - if (i !== 0 && !previousWasUscore) { + if (i !== 0 && !previousWasUscore) nextChar = nextChar.toLowerCase(); - } + previousWasUscore = false; if (nextChar === '_') { previousWasUscore = true; @@ -20,19 +20,14 @@ function getAllCapsSnakeCaseToCamelCase(eventType) { return eventValue; } -describe('Events tests', function() { - +describe('Events tests', function () { describe('Events enumeration', function () { - - Object.keys(Events).forEach(function(event) { - + Object.keys(Events).forEach(function (event) { it('should have a value matching generics convention for event type: ' + event, function () { - var value = Events[event]; - var expected = 'hls' + getAllCapsSnakeCaseToCamelCase(event); + let value = Events[event]; + let expected = 'hls' + getAllCapsSnakeCaseToCamelCase(event); assert.equal(value, expected); }); - }); - }); }); diff --git a/tests/unit/helper/buffer-helper.js b/tests/unit/helper/buffer-helper.js index 8c0bc002bfc..c9226f9ebae 100644 --- a/tests/unit/helper/buffer-helper.js +++ b/tests/unit/helper/buffer-helper.js @@ -1,20 +1,20 @@ -const assert = require("assert"); +const assert = require('assert'); import BufferHelper from '../../../src/helper/buffer-helper'; -function createMockBuffer(buffered) { +function createMockBuffer (buffered) { return { start: i => (buffered.length > i) ? buffered[i].startPTS : null, end: i => (buffered.length > i) ? buffered[i].endPTS : null, - length: buffered.length, + length: buffered.length }; } -describe('BufferHelper', function() { - describe('isBuffered', function() { +describe('BufferHelper', function () { + describe('isBuffered', function () { // |////////|__________|////////////////| // 0 0.5 1 2.0 const media = { - get buffered() { + get buffered () { return createMockBuffer([ { startPTS: 0, @@ -23,39 +23,39 @@ describe('BufferHelper', function() { { startPTS: 1, endPTS: 2.0 - }, + } ]); } }; - it('should return true if media.buffered throw error', function() { + it('should return true if media.buffered throw error', function () { const invalidMedia = { - get buffered() { - throw new Error("InvalidStateError"); + get buffered () { + throw new Error('InvalidStateError'); } }; assert.equal(BufferHelper.isBuffered(invalidMedia, 0), false); }); - it('should return true if some media.buffered includes the position', function() { + it('should return true if some media.buffered includes the position', function () { assert.equal(BufferHelper.isBuffered(media, 0), true); assert.equal(BufferHelper.isBuffered(media, 0.1), true); assert.equal(BufferHelper.isBuffered(media, 0.5), true); assert.equal(BufferHelper.isBuffered(media, 1), true); assert.equal(BufferHelper.isBuffered(media, 2), true); }); - it('should return false if any media.buffered does not includes the position', function() { + it('should return false if any media.buffered does not includes the position', function () { assert.equal(BufferHelper.isBuffered(media, -0.1), false); assert.equal(BufferHelper.isBuffered(media, 0.51), false); assert.equal(BufferHelper.isBuffered(media, 0.9), false); assert.equal(BufferHelper.isBuffered(media, 2.1), false); }); }); - describe("bufferInfo", () => { - it("should return found buffer info when maxHoleDuration is 0", function() { + describe('bufferInfo', () => { + it('should return found buffer info when maxHoleDuration is 0', function () { // |////////|__________|////////////////| // 0 0.5 1 2.0 const media = { - get buffered() { + get buffered () { return createMockBuffer([ { startPTS: 0, @@ -64,7 +64,7 @@ describe('BufferHelper', function() { { startPTS: 1, endPTS: 2.0 - }, + } ]); } }; @@ -94,11 +94,11 @@ describe('BufferHelper', function() { nextStart: undefined }); }); - it("should return found buffer info when maxHoleDuration is 0.5", function() { + it('should return found buffer info when maxHoleDuration is 0.5', function () { // |////////|__________|////////////////| // 0 0.5 1 2.0 const media = { - get buffered() { + get buffered () { return createMockBuffer([ { startPTS: 0, @@ -107,7 +107,7 @@ describe('BufferHelper', function() { { startPTS: 1, endPTS: 2.0 - }, + } ]); } }; diff --git a/tests/unit/helper/fragment-tracker.js b/tests/unit/helper/fragment-tracker.js index 11cc4b4e69c..c37bce3d456 100644 --- a/tests/unit/helper/fragment-tracker.js +++ b/tests/unit/helper/fragment-tracker.js @@ -1,25 +1,25 @@ -import Event from "../../../src/events"; +import Event from '../../../src/events'; const assert = require('assert'); import Hls from '../../../src/hls'; -import {FragmentTracker, FragmentState} from '../../../src/helper/fragment-tracker'; +import { FragmentTracker, FragmentState } from '../../../src/helper/fragment-tracker'; import PlaylistLoader from '../../../src/loader/playlist-loader'; const LevelType = PlaylistLoader.LevelType; -function createMockBuffer(buffered) { +function createMockBuffer (buffered) { return { - start: i => (buffered.length > i) ? buffered[i].startPTS: null, - end : i => (buffered.length > i) ? buffered[i].endPTS: null, - length: buffered.length, + start: i => (buffered.length > i) ? buffered[i].startPTS : null, + end: i => (buffered.length > i) ? buffered[i].endPTS : null, + length: buffered.length }; } -function createMockFragment(data, types) { +function createMockFragment (data, types) { data._elementaryStreams = new Set(types); data.hasElementaryStream = (type) => { return data._elementaryStreams.has(type) === true; - } + }; return data; } @@ -28,7 +28,7 @@ function createMockFragment(data, types) { * @param {Hls} hls * @param {Fragment} fragment */ -function loadFragment(hls, fragment){ +function loadFragment (hls, fragment) { hls.trigger(Event.FRAG_LOADED, { frag: fragment }); } @@ -37,7 +37,7 @@ function loadFragment(hls, fragment){ * @param {Hls} hls * @param {Fragment} fragment */ -function loadFragmentAndBuffered(hls, fragment) { +function loadFragmentAndBuffered (hls, fragment) { loadFragment(hls, fragment); hls.trigger(Event.FRAG_BUFFERED, { frag: fragment }); } @@ -62,7 +62,7 @@ describe('FragmentTracker', () => { { startPTS: 0, endPTS: 0.5 - }, + } ]); timeRanges = {}; @@ -70,7 +70,7 @@ describe('FragmentTracker', () => { timeRanges['audio'] = buffered; hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); it('detects fragments that partially loaded', () => { // Get the partial fragment at a time @@ -93,7 +93,6 @@ describe('FragmentTracker', () => { hls = new Hls({}); fragmentTracker = new FragmentTracker(hls); - let addFragment = () => { fragment = createMockFragment({ startPTS: 0, @@ -116,7 +115,7 @@ describe('FragmentTracker', () => { { startPTS: 0, endPTS: 1 - }, + } ]); timeRanges = {}; @@ -124,7 +123,7 @@ describe('FragmentTracker', () => { timeRanges['audio'] = buffered; hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.OK); }); @@ -135,14 +134,14 @@ describe('FragmentTracker', () => { { startPTS: 0.5, endPTS: 2 - }, + } ]); timeRanges = {}; timeRanges['video'] = buffered; timeRanges['audio'] = buffered; hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.PARTIAL); }); @@ -153,14 +152,14 @@ describe('FragmentTracker', () => { { startPTS: 0.5, endPTS: 2 - }, + } ]); timeRanges = {}; timeRanges['video'] = buffered; timeRanges['audio'] = buffered; hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.PARTIAL); @@ -169,7 +168,7 @@ describe('FragmentTracker', () => { { startPTS: 0.75, endPTS: 2 - }, + } ]); timeRanges = {}; timeRanges['video'] = buffered; @@ -312,17 +311,17 @@ describe('FragmentTracker', () => { { startPTS: 0, endPTS: 2 - }, + } ]); timeRanges['audio'] = createMockBuffer([ { startPTS: 0.5, endPTS: 2 - }, + } ]); hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.PARTIAL); }); @@ -342,17 +341,17 @@ describe('FragmentTracker', () => { { startPTS: 0.5, endPTS: 2 - }, + } ]); timeRanges['audio'] = createMockBuffer([ { startPTS: 0, endPTS: 2 - }, + } ]); hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.PARTIAL); }); @@ -372,23 +371,23 @@ describe('FragmentTracker', () => { { startPTS: 0.5, endPTS: 2 - }, + } ]); timeRanges['audio'] = createMockBuffer([ { startPTS: 0, endPTS: 2 - }, + } ]); hls.trigger(Event.BUFFER_APPENDED, { timeRanges }); - hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id : 'main', frag: fragment }); + hls.trigger(Event.FRAG_BUFFERED, { stats: { aborted: true }, id: 'main', frag: fragment }); assert.strictEqual(fragmentTracker.getState(fragment), FragmentState.OK); }); }); - describe("removeFragment", () => { + describe('removeFragment', () => { /** @type {Hls} */ let hls; /** @type {FragmentTracker} */ @@ -397,7 +396,7 @@ describe('FragmentTracker', () => { hls = new Hls({}); fragmentTracker = new FragmentTracker(hls); }); - it("should remove fragment", () => { + it('should remove fragment', () => { const fragment = createMockFragment({ startPTS: 0, endPTS: 1, @@ -414,7 +413,7 @@ describe('FragmentTracker', () => { assert.strictEqual(fragmentTracker.hasFragment(fragment), false); }); }); - describe("removeAllFragments", () => { + describe('removeAllFragments', () => { /** @type {Hls} */ let hls; /** @type {FragmentTracker} */ @@ -423,7 +422,7 @@ describe('FragmentTracker', () => { hls = new Hls({}); fragmentTracker = new FragmentTracker(hls); }); - it("should remove all fragments", () => { + it('should remove all fragments', () => { const fragments = [ // 0-1 createMockFragment({ diff --git a/tests/unit/loader/playlist-loader.js b/tests/unit/loader/playlist-loader.js index 4e772b4a2b7..78cbbf3d14e 100644 --- a/tests/unit/loader/playlist-loader.js +++ b/tests/unit/loader/playlist-loader.js @@ -6,94 +6,93 @@ import M3U8Parser from '../../../src/loader/m3u8-parser'; describe('PlaylistLoader', () => { it('parses empty manifest returns empty array', () => { - assert.deepEqual(M3U8Parser.parseMasterPlaylist("", 'http://www.dailymotion.com'), []); + assert.deepEqual(M3U8Parser.parseMasterPlaylist('', 'http://www.dailymotion.com'), []); }); it('manifest with broken syntax returns empty array', () => { - var manifest = `#EXTXSTREAMINF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360,NAME="480" + let manifest = `#EXTXSTREAMINF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360,NAME="480" http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; assert.deepEqual(M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'), []); }); it('parses manifest with one level', () => { - var manifest = `#EXTM3U + let manifest = `#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360,NAME="480" http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; - var result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[0]['audioCodec'],'mp4a.40.2'); - assert.strictEqual(result[0]['videoCodec'],'avc1.64001f'); - assert.strictEqual(result[0]['width'],848); - assert.strictEqual(result[0]['height'],360); - assert.strictEqual(result[0]['name'],'480'); - assert.strictEqual(result[0]['url'],'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); + let result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[0]['audioCodec'], 'mp4a.40.2'); + assert.strictEqual(result[0]['videoCodec'], 'avc1.64001f'); + assert.strictEqual(result[0]['width'], 848); + assert.strictEqual(result[0]['height'], 360); + assert.strictEqual(result[0]['name'], '480'); + assert.strictEqual(result[0]['url'], 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); }); it('parses manifest without codecs', () => { - var manifest = `#EXTM3U + let manifest = `#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,RESOLUTION=848x360,NAME="480" http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; - var result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[0]['audioCodec'],undefined); - assert.strictEqual(result[0]['videoCodec'],undefined); - assert.strictEqual(result[0]['width'],848); - assert.strictEqual(result[0]['height'],360); - assert.strictEqual(result[0]['name'],'480'); - assert.strictEqual(result[0]['url'],'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); + let result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[0]['audioCodec'], undefined); + assert.strictEqual(result[0]['videoCodec'], undefined); + assert.strictEqual(result[0]['width'], 848); + assert.strictEqual(result[0]['height'], 360); + assert.strictEqual(result[0]['name'], '480'); + assert.strictEqual(result[0]['url'], 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); }); - it('does not care about the attribute order', () => { - var manifest = `#EXTM3U + let manifest = `#EXTM3U #EXT-X-STREAM-INF:NAME="480",PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360 http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; - var result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[0]['audioCodec'],'mp4a.40.2'); - assert.strictEqual(result[0]['videoCodec'],'avc1.64001f'); - assert.strictEqual(result[0]['width'],848); - assert.strictEqual(result[0]['height'],360); - assert.strictEqual(result[0]['name'],'480'); - assert.strictEqual(result[0]['url'],'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); + let result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[0]['audioCodec'], 'mp4a.40.2'); + assert.strictEqual(result[0]['videoCodec'], 'avc1.64001f'); + assert.strictEqual(result[0]['width'], 848); + assert.strictEqual(result[0]['height'], 360); + assert.strictEqual(result[0]['name'], '480'); + assert.strictEqual(result[0]['url'], 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); manifest = `#EXTM3U #EXT-X-STREAM-INF:NAME="480",RESOLUTION=848x360,PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f" http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[0]['audioCodec'],'mp4a.40.2'); - assert.strictEqual(result[0]['videoCodec'],'avc1.64001f'); - assert.strictEqual(result[0]['width'],848); - assert.strictEqual(result[0]['height'],360); - assert.strictEqual(result[0]['name'],'480'); - assert.strictEqual(result[0]['url'],'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[0]['audioCodec'], 'mp4a.40.2'); + assert.strictEqual(result[0]['videoCodec'], 'avc1.64001f'); + assert.strictEqual(result[0]['width'], 848); + assert.strictEqual(result[0]['height'], 360); + assert.strictEqual(result[0]['name'], '480'); + assert.strictEqual(result[0]['url'], 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); manifest = `#EXTM3U #EXT-X-STREAM-INF:CODECS="mp4a.40.2,avc1.64001f",NAME="480",RESOLUTION=848x360,PROGRAM-ID=1,BANDWIDTH=836280 http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core`; result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[0]['audioCodec'],'mp4a.40.2'); - assert.strictEqual(result[0]['videoCodec'],'avc1.64001f'); - assert.strictEqual(result[0]['width'],848); - assert.strictEqual(result[0]['height'],360); - assert.strictEqual(result[0]['name'],'480'); - assert.strictEqual(result[0]['url'],'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[0]['audioCodec'], 'mp4a.40.2'); + assert.strictEqual(result[0]['videoCodec'], 'avc1.64001f'); + assert.strictEqual(result[0]['width'], 848); + assert.strictEqual(result[0]['height'], 360); + assert.strictEqual(result[0]['name'], '480'); + assert.strictEqual(result[0]['url'], 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core'); }); it('parses manifest with 10 levels', () => { - var manifest = `#EXTM3U + let manifest = `#EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360,NAME="480" http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=836280,CODECS="mp4a.40.2,avc1.64001f",RESOLUTION=848x360,NAME="480" @@ -115,39 +114,39 @@ http://proxy-62.dailymotion.com/sec(2a991e17f08fcd94f95637a6dd718ddd)/video/107/ #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=6221600,CODECS="mp4a.40.2,avc1.640028",RESOLUTION=1920x816,NAME="1080" http://proxy-21.dailymotion.com/sec(2a991e17f08fcd94f95637a6dd718ddd)/video/107/282/158282701_mp4_h264_aac_fhd.m3u8#cell=core`; - var result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); - assert.strictEqual(result.length,10); - assert.strictEqual(result[0]['bitrate'],836280); - assert.strictEqual(result[1]['bitrate'],836280); - assert.strictEqual(result[2]['bitrate'],246440); - assert.strictEqual(result[3]['bitrate'],246440); - assert.strictEqual(result[4]['bitrate'],460560); - assert.strictEqual(result[5]['bitrate'],460560); - assert.strictEqual(result[6]['bitrate'],2149280); - assert.strictEqual(result[7]['bitrate'],2149280); - assert.strictEqual(result[8]['bitrate'],6221600); - assert.strictEqual(result[9]['bitrate'],6221600); + let result = M3U8Parser.parseMasterPlaylist(manifest, 'http://www.dailymotion.com'); + assert.strictEqual(result.length, 10); + assert.strictEqual(result[0]['bitrate'], 836280); + assert.strictEqual(result[1]['bitrate'], 836280); + assert.strictEqual(result[2]['bitrate'], 246440); + assert.strictEqual(result[3]['bitrate'], 246440); + assert.strictEqual(result[4]['bitrate'], 460560); + assert.strictEqual(result[5]['bitrate'], 460560); + assert.strictEqual(result[6]['bitrate'], 2149280); + assert.strictEqual(result[7]['bitrate'], 2149280); + assert.strictEqual(result[8]['bitrate'], 6221600); + assert.strictEqual(result[9]['bitrate'], 6221600); }); it('parses empty levels returns empty fragment array', () => { - var level = ""; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let level = ''; + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.fragments.length, 0); - assert.strictEqual(result.totalduration,0); + assert.strictEqual(result.totalduration, 0); }); it('level with 0 frag returns empty fragment array', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-TARGETDURATION:14`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.fragments.length, 0); - assert.strictEqual(result.totalduration,0); + assert.strictEqual(result.totalduration, 0); }); it('parse level with several fragments', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-TARGETDURATION:14 @@ -163,7 +162,7 @@ http://proxy-21.dailymotion.com/sec(2a991e17f08fcd94f95637a6dd718ddd)/video/107/ #EXTINF:3.880, /sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.ts #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.totalduration, 51.24); assert.strictEqual(result.startSN, 0); assert.strictEqual(result.version, 3); @@ -185,7 +184,7 @@ http://proxy-21.dailymotion.com/sec(2a991e17f08fcd94f95637a6dd718ddd)/video/107/ }); it('parse level with single char fragment URI', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-ALLOW-CACHE:NO #EXT-X-TARGETDURATION:2 #EXTINF:2, @@ -193,24 +192,24 @@ http://proxy-21.dailymotion.com/sec(2a991e17f08fcd94f95637a6dd718ddd)/video/107/ #EXTINF:2, 1 #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); - assert.strictEqual(result.totalduration, 4); - assert.strictEqual(result.startSN, 0); - assert.strictEqual(result.targetduration, 2); - assert.strictEqual(result.live, false); - assert.strictEqual(result.fragments.length, 2); - assert.strictEqual(result.fragments[0].cc, 0); - assert.strictEqual(result.fragments[0].duration, 2); - assert.strictEqual(result.fragments[0].sn, 0); - assert.strictEqual(result.fragments[0].relurl, '0'); - assert.strictEqual(result.fragments[1].cc, 0); - assert.strictEqual(result.fragments[1].duration, 2); - assert.strictEqual(result.fragments[1].sn, 1); - assert.strictEqual(result.fragments[1].relurl, '1'); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); + assert.strictEqual(result.totalduration, 4); + assert.strictEqual(result.startSN, 0); + assert.strictEqual(result.targetduration, 2); + assert.strictEqual(result.live, false); + assert.strictEqual(result.fragments.length, 2); + assert.strictEqual(result.fragments[0].cc, 0); + assert.strictEqual(result.fragments[0].duration, 2); + assert.strictEqual(result.fragments[0].sn, 0); + assert.strictEqual(result.fragments[0].relurl, '0'); + assert.strictEqual(result.fragments[1].cc, 0); + assert.strictEqual(result.fragments[1].duration, 2); + assert.strictEqual(result.fragments[1].sn, 1); + assert.strictEqual(result.fragments[1].relurl, '1'); }); it('parse level with EXTINF line without comma', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:6 @@ -227,7 +226,7 @@ chop/segment-4.ts chop/segment-5.ts #EXTINF:6.000000 #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.totalduration, 30); assert.strictEqual(result.startSN, 0); assert.strictEqual(result.version, 3); @@ -246,7 +245,7 @@ chop/segment-5.ts }); it('parse level with start time offset', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-TARGETDURATION:14 @@ -262,7 +261,7 @@ chop/segment-5.ts #EXTINF:3.880, /sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(5)/video/107/282/158282701_mp4_h264_aac_hq.ts #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.totalduration, 51.24); assert.strictEqual(result.startSN, 0); assert.strictEqual(result.targetduration, 14); @@ -271,7 +270,7 @@ chop/segment-5.ts }); it('parse AES encrypted URLs, with implicit IV', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:1 ## Created with Unified Streaming Platform(version=1.6.7) #EXT-X-MEDIA-SEQUENCE:1 @@ -285,7 +284,7 @@ oceans_aes-audio=65000-video=236000-2.ts #EXTINF:7,no desc oceans_aes-audio=65000-video=236000-3.ts #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://foo.com/adaptive/oceans_aes/oceans_aes.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://foo.com/adaptive/oceans_aes/oceans_aes.m3u8', 0); assert.strictEqual(result.totalduration, 25); assert.strictEqual(result.startSN, 1); assert.strictEqual(result.targetduration, 11); @@ -293,29 +292,28 @@ oceans_aes-audio=65000-video=236000-3.ts assert.strictEqual(result.fragments.length, 3); assert.strictEqual(result.fragments[0].cc, 0); assert.strictEqual(result.fragments[0].duration, 11); - assert.strictEqual(result.fragments[0].title, "no desc"); + assert.strictEqual(result.fragments[0].title, 'no desc'); assert.strictEqual(result.fragments[0].level, 0); assert.strictEqual(result.fragments[0].url, 'http://foo.com/adaptive/oceans_aes/oceans_aes-audio=65000-video=236000-1.ts'); assert.strictEqual(result.fragments[0].decryptdata.uri, 'http://foo.com/adaptive/oceans_aes/oceans.key'); assert.strictEqual(result.fragments[0].decryptdata.method, 'AES-128'); - var sn = 1; - var uint8View = new Uint8Array(16); - for (var i = 12; i < 16; i++) { - uint8View[i] = (sn >> 8*(15-i)) & 0xff; - } + let sn = 1; + let uint8View = new Uint8Array(16); + for (let i = 12; i < 16; i++) + uint8View[i] = (sn >> 8 * (15 - i)) & 0xff; + assert(bufferIsEqual(result.fragments[0].decryptdata.iv.buffer, uint8View.buffer)); sn = 3; uint8View = new Uint8Array(16); - for (var i = 12; i < 16; i++) { - uint8View[i] = (sn >> 8*(15-i)) & 0xff; - } + for (let i = 12; i < 16; i++) + uint8View[i] = (sn >> 8 * (15 - i)) & 0xff; + assert(bufferIsEqual(result.fragments[2].decryptdata.iv.buffer, uint8View.buffer)); }); - it('parse level with #EXT-X-BYTERANGE before #EXTINF', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:4 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:1 @@ -351,20 +349,20 @@ lo008ts #EXTINF:1000000, lo008ts`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8', 0); assert.strictEqual(result.fragments.length, 10); assert.strictEqual(result.fragments[0].url, 'http://dummy.com/lo007ts'); - assert.strictEqual(result.fragments[0].byteRangeStartOffset,803136); - assert.strictEqual(result.fragments[0].byteRangeEndOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeStartOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeEndOffset,1039452); + assert.strictEqual(result.fragments[0].byteRangeStartOffset, 803136); + assert.strictEqual(result.fragments[0].byteRangeEndOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeStartOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeEndOffset, 1039452); assert.strictEqual(result.fragments[9].url, 'http://dummy.com/lo008ts'); - assert.strictEqual(result.fragments[9].byteRangeStartOffset,684508); - assert.strictEqual(result.fragments[9].byteRangeEndOffset,817988); + assert.strictEqual(result.fragments[9].byteRangeStartOffset, 684508); + assert.strictEqual(result.fragments[9].byteRangeEndOffset, 817988); }); it('parse level with #EXT-X-BYTERANGE after #EXTINF', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:4 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:1 @@ -400,20 +398,20 @@ lo008ts #EXT-X-BYTERANGE:133480@684508 lo008ts`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8', 0); assert.strictEqual(result.fragments.length, 10); assert.strictEqual(result.fragments[0].url, 'http://dummy.com/lo007ts'); - assert.strictEqual(result.fragments[0].byteRangeStartOffset,803136); - assert.strictEqual(result.fragments[0].byteRangeEndOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeStartOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeEndOffset,1039452); + assert.strictEqual(result.fragments[0].byteRangeStartOffset, 803136); + assert.strictEqual(result.fragments[0].byteRangeEndOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeStartOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeEndOffset, 1039452); assert.strictEqual(result.fragments[9].url, 'http://dummy.com/lo008ts'); - assert.strictEqual(result.fragments[9].byteRangeStartOffset,684508); - assert.strictEqual(result.fragments[9].byteRangeEndOffset,817988); + assert.strictEqual(result.fragments[9].byteRangeStartOffset, 684508); + assert.strictEqual(result.fragments[9].byteRangeEndOffset, 817988); }); it('parse level with #EXT-X-BYTERANGE without offset', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:4 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:1 @@ -428,19 +426,19 @@ lo007ts #EXT-X-BYTERANGE:143068 lo007ts`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8', 0); assert.strictEqual(result.fragments.length, 3); assert.strictEqual(result.fragments[0].url, 'http://dummy.com/lo007ts'); - assert.strictEqual(result.fragments[0].byteRangeStartOffset,803136); - assert.strictEqual(result.fragments[0].byteRangeEndOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeStartOffset,943196); - assert.strictEqual(result.fragments[1].byteRangeEndOffset,1039452); - assert.strictEqual(result.fragments[2].byteRangeStartOffset,1039452); - assert.strictEqual(result.fragments[2].byteRangeEndOffset,1182520); + assert.strictEqual(result.fragments[0].byteRangeStartOffset, 803136); + assert.strictEqual(result.fragments[0].byteRangeEndOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeStartOffset, 943196); + assert.strictEqual(result.fragments[1].byteRangeEndOffset, 1039452); + assert.strictEqual(result.fragments[2].byteRangeStartOffset, 1039452); + assert.strictEqual(result.fragments[2].byteRangeEndOffset, 1182520); }); it('parses discontinuity and maintains continuity counter', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:10 @@ -458,15 +456,15 @@ lo007ts`; 0006.ts #EXT-X-ENDLIST `; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8', 0); assert.strictEqual(result.fragments.length, 5); assert.strictEqual(result.totalduration, 45); assert.strictEqual(result.fragments[2].cc, 0); - assert.strictEqual(result.fragments[3].cc, 1); //continuity counter should increase around discontinuity + assert.strictEqual(result.fragments[3].cc, 1); // continuity counter should increase around discontinuity }); it('parses correctly EXT-X-DISCONTINUITY-SEQUENCE and increases continuity counter', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:10 @@ -485,30 +483,30 @@ lo007ts`; 0006.ts #EXT-X-ENDLIST `; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8', 0); assert.strictEqual(result.fragments.length, 5); assert.strictEqual(result.totalduration, 45); assert.strictEqual(result.fragments[0].cc, 20); assert.strictEqual(result.fragments[2].cc, 20); - assert.strictEqual(result.fragments[3].cc, 21); //continuity counter should increase around discontinuity + assert.strictEqual(result.fragments[3].cc, 21); // continuity counter should increase around discontinuity }); it('parses manifest with one audio track', () => { - var manifest = `#EXTM3U + let manifest = `#EXTM3U #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="600k",LANGUAGE="eng",NAME="Audio",AUTOSELECT=YES,DEFAULT=YES,URI="/videos/ZakEbrahim_2014/audio/600k.m3u8?qr=true&preroll=Blank",BANDWIDTH=614400`; - var result = M3U8Parser.parseMasterPlaylistMedia(manifest, 'https://hls.ted.com/', 'AUDIO'); - assert.strictEqual(result.length,1); - assert.strictEqual(result[0]['autoselect'],true); - assert.strictEqual(result[0]['default'],true); - assert.strictEqual(result[0]['forced'],false); - assert.strictEqual(result[0]['groupId'],'600k'); - assert.strictEqual(result[0]['lang'],'eng'); - assert.strictEqual(result[0]['name'],'Audio'); - assert.strictEqual(result[0]['url'],'https://hls.ted.com/videos/ZakEbrahim_2014/audio/600k.m3u8?qr=true&preroll=Blank'); + let result = M3U8Parser.parseMasterPlaylistMedia(manifest, 'https://hls.ted.com/', 'AUDIO'); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0]['autoselect'], true); + assert.strictEqual(result[0]['default'], true); + assert.strictEqual(result[0]['forced'], false); + assert.strictEqual(result[0]['groupId'], '600k'); + assert.strictEqual(result[0]['lang'], 'eng'); + assert.strictEqual(result[0]['name'], 'Audio'); + assert.strictEqual(result[0]['url'], 'https://hls.ted.com/videos/ZakEbrahim_2014/audio/600k.m3u8?qr=true&preroll=Blank'); }); - //issue #425 - first fragment has null url and no decryptdata if EXT-X-KEY follows EXTINF + // issue #425 - first fragment has null url and no decryptdata if EXT-X-KEY follows EXTINF it('parse level with #EXT-X-KEY after #EXTINF', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 @@ -530,35 +528,35 @@ lo007ts`; 0007.ts #EXTINF:10, 0008.ts`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8', 0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.com/playlist.m3u8', 0); assert.strictEqual(result.fragments.length, 8); assert.strictEqual(result.totalduration, 80); - var fragdecryptdata, decryptdata = result.fragments[0].decryptdata, sn = 0; + let fragdecryptdata, decryptdata = result.fragments[0].decryptdata, sn = 0; result.fragments.forEach(function (fragment, idx) { sn = idx + 1; assert.strictEqual(fragment.url, 'http://dummy.com/000' + sn + '.ts'); - //decryptdata should persist across all fragments + // decryptdata should persist across all fragments fragdecryptdata = fragment.decryptdata; assert.strictEqual(fragdecryptdata.method, decryptdata.method); assert.strictEqual(fragdecryptdata.uri, decryptdata.uri); assert.strictEqual(fragdecryptdata.key, decryptdata.key); - //initialization vector is correctly generated since it wasn't declared in the playlist - var iv = fragdecryptdata.iv; + // initialization vector is correctly generated since it wasn't declared in the playlist + let iv = fragdecryptdata.iv; assert.strictEqual(iv[15], idx); - //hold this decrypt data to compare to the next fragment's decrypt data + // hold this decrypt data to compare to the next fragment's decrypt data decryptdata = fragment.decryptdata; }); }); - //PR #454 - Add support for custom tags in fragment object + // PR #454 - Add support for custom tags in fragment object it('return custom tags in fragment object', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:719926 @@ -584,23 +582,23 @@ http://dummy.url.com/hls/live/segment/segment_022916_164500865_719933.ts http://dummy.url.com/hls/live/segment/segment_022916_164500865_719934.ts #EXTINF:9.25, http://dummy.url.com/hls/live/segment/segment_022916_164500865_719935.ts`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.url.com/playlist.m3u8', 0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://dummy.url.com/playlist.m3u8', 0); assert.strictEqual(result.fragments.length, 10); assert.strictEqual(result.totalduration, 84.94); assert.strictEqual(result.targetduration, 10); assert.strictEqual(result.fragments[0].url, 'http://dummy.url.com/hls/live/segment/segment_022916_164500865_719926.ts'); - assert.strictEqual(result.fragments[0].tagList.length,1); - assert.strictEqual(result.fragments[2].tagList[0][0],'EXT-X-CUE-OUT'); - assert.strictEqual(result.fragments[2].tagList[0][1],'DURATION=150,BREAKID=0x0'); - assert.strictEqual(result.fragments[3].tagList[0][1],'0.50'); - assert.strictEqual(result.fragments[4].tagList.length,2); - assert.strictEqual(result.fragments[4].tagList[0][0],'EXT-X-CUE-IN'); - assert.strictEqual(result.fragments[7].tagList[0][0],'INF'); + assert.strictEqual(result.fragments[0].tagList.length, 1); + assert.strictEqual(result.fragments[2].tagList[0][0], 'EXT-X-CUE-OUT'); + assert.strictEqual(result.fragments[2].tagList[0][1], 'DURATION=150,BREAKID=0x0'); + assert.strictEqual(result.fragments[3].tagList[0][1], '0.50'); + assert.strictEqual(result.fragments[4].tagList.length, 2); + assert.strictEqual(result.fragments[4].tagList[0][0], 'EXT-X-CUE-IN'); + assert.strictEqual(result.fragments[7].tagList[0][0], 'INF'); assert.strictEqual(result.fragments[8].url, 'http://dummy.url.com/hls/live/segment/segment_022916_164500865_719934.ts'); }); it('parses playlists with #EXT-X-PROGRAM-DATE-TIME after #EXTINF before fragment URL', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:2 #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:69844067 @@ -614,9 +612,9 @@ Rollover38803/20160525T064049-01-69844068.ts #EXT-X-PROGRAM-DATE-TIME:2016-05-27T16:35:04Z Rollover38803/20160525T064049-01-69844069.ts `; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8', 0); assert.strictEqual(result.fragments.length, 3); - assert.strictEqual(result.programDateTime.getTime(),1464366884000); + assert.strictEqual(result.programDateTime.getTime(), 1464366884000); assert.strictEqual(result.totalduration, 30); assert.strictEqual(result.fragments[0].url, 'http://video.example.com/Rollover38803/20160525T064049-01-69844067.ts'); assert.strictEqual(result.fragments[0].programDateTime.getTime(), 1464366884000); @@ -627,20 +625,20 @@ Rollover38803/20160525T064049-01-69844069.ts }); it('parses #EXTINF without a leading digit', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-PLAYLIST-TYPE:VOD #EXT-X-TARGETDURATION:14 #EXTINF:.360, /sec(3ae40f708f79ca9471f52b86da76a3a8)/frag(1)/video/107/282/158282701_mp4_h264_aac_hq.ts #EXT-X-ENDLIST`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); assert.strictEqual(result.fragments.length, 1); assert.strictEqual(result.fragments[0].duration, 0.360); }); it('parses #EXT-X-MAP URI', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-TARGETDURATION:6 #EXT-X-VERSION:7 #EXT-X-MEDIA-SEQUENCE:1 @@ -650,15 +648,15 @@ Rollover38803/20160525T064049-01-69844069.ts #EXTINF:6.00600, #EXT-X-BYTERANGE:1543597@718 main.mp4`; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core',0); - assert.strictEqual(result.initSegment.url, "http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/main.mp4"); + let result = M3U8Parser.parseLevelPlaylist(level, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/158282701_mp4_h264_aac_hq.m3u8#cell=core', 0); + assert.strictEqual(result.initSegment.url, 'http://proxy-62.dailymotion.com/sec(3ae40f708f79ca9471f52b86da76a3a8)/video/107/282/main.mp4'); assert.strictEqual(result.initSegment.byteRangeStartOffset, 0); assert.strictEqual(result.initSegment.byteRangeEndOffset, 718); assert.strictEqual(result.initSegment.sn, 'initSegment'); }); it('if playlists contains #EXT-X-PROGRAM-DATE-TIME switching will be applied by PDT', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:2 #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:69844067 @@ -672,13 +670,13 @@ Rollover38803/20160525T064049-01-69844068.ts #EXT-X-PROGRAM-DATE-TIME:2016-05-27T16:35:04Z Rollover38803/20160525T064049-01-69844069.ts `; - var hls = {config : { }, on : function() { }}; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8',0); + let hls = { config: { }, on: function () { } }; + let result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8', 0); assert.ok(result.programDateTime); }); it('if playlists does NOT contain #EXT-X-PROGRAM-DATE-TIME switching will be applied by CC count', () => { - var level = `#EXTM3U + let level = `#EXTM3U #EXT-X-VERSION:2 #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:69844067 @@ -689,9 +687,8 @@ Rollover38803/20160525T064049-01-69844068.ts #EXTINF:10, no desc Rollover38803/20160525T064049-01-69844069.ts `; - var hls = {config : { }, on : function() { }}; - var result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8',0); + let hls = { config: { }, on: function () { } }; + let result = M3U8Parser.parseLevelPlaylist(level, 'http://video.example.com/disc.m3u8', 0); assert.strictEqual(result.programDateTime, undefined); }); - }); diff --git a/tests/unit/utils/attr-list.js b/tests/unit/utils/attr-list.js index 738fe4bbca8..dc0aa9fb363 100644 --- a/tests/unit/utils/attr-list.js +++ b/tests/unit/utils/attr-list.js @@ -11,7 +11,7 @@ describe('AttrList', () => { assert.deepEqual(new AttrList(undefined), {}); }); it('constructor() supports object argument', () => { - const obj = { VALUE: "42" }; + const obj = { VALUE: '42' }; const list = new AttrList(obj); assert.strictEqual(list.decimalInteger('VALUE'), 42); assert.strictEqual(Object.keys(list).length, 1); @@ -69,9 +69,9 @@ describe('AttrList', () => { assert.strictEqual(list.enumeratedString('ENUM2'), '42'); }); it('parses valid decimalResolution attribute', () => { - assert(deepStrictEqual(new AttrList('RES=400x200').decimalResolution('RES'), { width:400, height:200 })); - assert(deepStrictEqual(new AttrList('RES=0x0').decimalResolution('RES'), { width:0, height:0 })); - assert(deepStrictEqual(new AttrList('RES="400x200"').decimalResolution('RES'), { width:400, height:200 })); + assert(deepStrictEqual(new AttrList('RES=400x200').decimalResolution('RES'), { width: 400, height: 200 })); + assert(deepStrictEqual(new AttrList('RES=0x0').decimalResolution('RES'), { width: 0, height: 0 })); + assert(deepStrictEqual(new AttrList('RES="400x200"').decimalResolution('RES'), { width: 400, height: 200 })); }); it('handles invalid decimalResolution attribute', () => { assert(deepStrictEqual(new AttrList('RES=400x-200').decimalResolution('RES'), undefined)); @@ -90,7 +90,7 @@ describe('AttrList', () => { assert.strictEqual(list.decimalFloatingPoint('FLOAT'), 0.42); assert.strictEqual(list.STRING, 'hi'); assert.strictEqual(list.enumeratedString('ENUM'), 'OK'); - assert(deepStrictEqual(list.decimalResolution('RES'), { width:4, height:2 })); + assert(deepStrictEqual(list.decimalResolution('RES'), { width: 4, height: 2 })); assert.strictEqual(Object.keys(list).length, 6); }); @@ -117,8 +117,8 @@ describe('AttrList', () => { it('handles hexadecimalInteger conversions', () => { const list = new AttrList('HEX1=0x0123456789abcdef0123456789abcdef,HEX2=0x123,HEX3=0x0'); - assert(bufferIsEqual(list.hexadecimalInteger('HEX1').buffer, new Uint8Array([0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef]).buffer)); - assert(bufferIsEqual(list.hexadecimalInteger('HEX2').buffer, new Uint8Array([0x01,0x23]).buffer)); + assert(bufferIsEqual(list.hexadecimalInteger('HEX1').buffer, new Uint8Array([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]).buffer)); + assert(bufferIsEqual(list.hexadecimalInteger('HEX2').buffer, new Uint8Array([0x01, 0x23]).buffer)); assert(bufferIsEqual(list.hexadecimalInteger('HEX3').buffer, new Uint8Array([0x0]).buffer)); }); diff --git a/tests/unit/utils/binary-search.js b/tests/unit/utils/binary-search.js index a5136268433..7d38655f550 100644 --- a/tests/unit/utils/binary-search.js +++ b/tests/unit/utils/binary-search.js @@ -1,34 +1,33 @@ -var assert = require("assert"); +let assert = require('assert'); import BinarySearch from '../../../src/utils/binary-search'; -describe('binary search util', function() { +describe('binary search util', function () { describe('search helper', function () { - var list = null; - var buildComparisonFunction = function(itemToSearchFor) { - return function(candidate) { - if (candidate < itemToSearchFor) { + let list = null; + let buildComparisonFunction = function (itemToSearchFor) { + return function (candidate) { + if (candidate < itemToSearchFor) return 1; - } - else if (candidate > itemToSearchFor) { + else if (candidate > itemToSearchFor) return -1; - } + return 0; }; - } + }; - beforeEach(function() { + beforeEach(function () { list = [4, 8, 15, 16, 23, 42]; }); it('finds the element if it is present', function () { - for(var i=0; i<list.length; i++) { - var item = list[i]; - var foundItem = BinarySearch.search(list, buildComparisonFunction(item)); + for (let i = 0; i < list.length; i++) { + let item = list[i]; + let foundItem = BinarySearch.search(list, buildComparisonFunction(item)); assert.strictEqual(foundItem, item); } }); it('does not find the element if it is not present', function () { - var item = 1000; - var foundItem = BinarySearch.search(list, buildComparisonFunction(item)); + let item = 1000; + let foundItem = BinarySearch.search(list, buildComparisonFunction(item)); assert.strictEqual(foundItem, null); }); }); diff --git a/tests/unit/utils/discontinuities.js b/tests/unit/utils/discontinuities.js index cc73f3943a8..5de88e8f041 100644 --- a/tests/unit/utils/discontinuities.js +++ b/tests/unit/utils/discontinuities.js @@ -7,7 +7,7 @@ const mockReferenceFrag = { startPTS: 20, endPTS: 24, duration: 4, - cc: 0, + cc: 0 }; const mockFrags = [ @@ -16,7 +16,7 @@ const mockFrags = [ startPTS: 0, endPTS: 4, duration: 4, - cc: 0, + cc: 0 }, { start: 4, @@ -34,9 +34,8 @@ const mockFrags = [ } ]; - describe('level-helper', function () { - it ('adjusts level fragments with overlapping CC range using a reference fragment', function () { + it('adjusts level fragments with overlapping CC range using a reference fragment', function () { const details = { fragments: mockFrags.slice(0), PTSKnown: false @@ -70,21 +69,19 @@ describe('level-helper', function () { assert.equal(true, details.PTSKnown); }); - -it ('adjusts level fragments without overlapping CC range but with programDateTime info', function () { - - const lastFrag = { cc : 0 }; + it('adjusts level fragments without overlapping CC range but with programDateTime info', function () { + const lastFrag = { cc: 0 }; const lastLevel = { - details : { - PTSKnown : true, - programDateTime : new Date('2017-08-28 00:00:00'), - fragments : [ + details: { + PTSKnown: true, + programDateTime: new Date('2017-08-28 00:00:00'), + fragments: [ { start: 20, startPTS: 20, endPTS: 24, duration: 4, - cc: 0, + cc: 0 }, { start: 24, @@ -104,73 +101,72 @@ it ('adjusts level fragments without overlapping CC range but with programDateTi } }; - var details = { + let details = { fragments: [ - { - start: 0, - startPTS: 0, - endPTS: 4, - duration: 4, - cc: 2, - }, - { - start: 4, - startPTS: 4, - endPTS: 8, - duration: 4, - cc: 2 - }, - { - start: 8, - startPTS: 8, - endPTS: 16, - duration: 8, - cc: 3 - } - ], + { + start: 0, + startPTS: 0, + endPTS: 4, + duration: 4, + cc: 2 + }, + { + start: 4, + startPTS: 4, + endPTS: 8, + duration: 4, + cc: 2 + }, + { + start: 8, + startPTS: 8, + endPTS: 16, + duration: 8, + cc: 3 + } + ], PTSKnown: false, - programDateTime : new Date('2017-08-28 00:00:50'), - startCC : 2, - endCC : 3 + programDateTime: new Date('2017-08-28 00:00:50'), + startCC: 2, + endCC: 3 }; - var detailsExpected = { - fragments : [ - { - start: 70, - startPTS: 70, - endPTS: 74, - duration: 4, - cc: 2 - }, - { - start: 74, - startPTS: 74, - endPTS: 78, - duration: 4, - cc: 2 - }, - { - start: 78, - startPTS: 78, - endPTS: 86, - duration: 8, - cc: 3 - } - ], + let detailsExpected = { + fragments: [ + { + start: 70, + startPTS: 70, + endPTS: 74, + duration: 4, + cc: 2 + }, + { + start: 74, + startPTS: 74, + endPTS: 78, + duration: 4, + cc: 2 + }, + { + start: 78, + startPTS: 78, + endPTS: 86, + duration: 8, + cc: 3 + } + ], PTSKnown: true, - programDateTime : new Date('2017-08-28 00:00:50'), - startCC : 2, - endCC : 3 + programDateTime: new Date('2017-08-28 00:00:50'), + startCC: 2, + endCC: 3 }; - alignDiscontinuities(lastFrag,lastLevel,details); - assert.deepEqual(detailsExpected,details); + alignDiscontinuities(lastFrag, lastLevel, details); + assert.deepEqual(detailsExpected, details); }); - it('finds the first fragment in an array which matches the CC of the first fragment in another array', function () { const prevDetails = { - fragments: [mockReferenceFrag, { cc: 1 }] + fragments: [mockReferenceFrag, { cc: 1 }] }; const curDetails = { fragments: mockFrags diff --git a/tests/unit/utils/output-filter.js b/tests/unit/utils/output-filter.js index 6d0778ad55c..acf90e48f86 100644 --- a/tests/unit/utils/output-filter.js +++ b/tests/unit/utils/output-filter.js @@ -11,18 +11,18 @@ describe('OutputFilter', () => { lastCueArguments = { trackName, startTime, endTime, screen }; callCount++; }, - createCaptionsTrack: (track) => { + createCaptionsTrack: (track) => { captionsTrackCalled = true; }, getCallCount: () => callCount, getLastCueAdded: () => lastCueArguments, - didCaptionsTrackInvoke: () => captionsTrackCalled, - } - } + didCaptionsTrackInvoke: () => captionsTrackCalled + }; + }; let timelineController, outputFilter; - beforeEach(function() { + beforeEach(function () { timelineController = createMockTimelineController(); outputFilter = new OutputFilter(timelineController, 1); });