@clappr/hlsjs-playback 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -178,7 +178,7 @@ var HlsjsPlayback = /*#__PURE__*/function (_HTML5Video) {
178
178
  key: "supportedVersion",
179
179
  get: function get() {
180
180
  return {
181
- min: "0.16.2"
181
+ min: "0.16.3"
182
182
  };
183
183
  }
184
184
  }, {
@@ -181,7 +181,7 @@
181
181
  key: "supportedVersion",
182
182
  get: function get() {
183
183
  return {
184
- min: "0.16.2"
184
+ min: "0.16.3"
185
185
  };
186
186
  }
187
187
  }, {
@@ -1,2 +1,2 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@clappr/core"),require("hls.js")):"function"==typeof define&&define.amd?define(["@clappr/core","hls.js"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).HlsjsPlayback=e(t.Clappr,t.Hls)}(this,function(t,e){"use strict";function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=Array(e);r<e;r++)i[r]=t[r];return i}function i(t,e,r){return e=o(e),function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,l()?Reflect.construct(e,r||[],o(t).constructor):e.apply(t,r))}function n(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,d(i.key),i)}}function a(t,e,r){return(e=d(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function s(){return s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,r){var i=function(t,e){for(;!{}.hasOwnProperty.call(t,e)&&null!==(t=o(t)););return t}(t,e);if(i){var n=Object.getOwnPropertyDescriptor(i,e);return n.get?n.get.call(arguments.length<3?t:r):n.value}},s.apply(null,arguments)}function o(t){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},o(t)}function l(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(l=function(){return!!t})()}function u(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,i)}return r}function h(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?u(Object(r),!0).forEach(function(e){a(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):u(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}function _(t,e,r,i){var n=s(o(t.prototype),e,r);return 2&i&&"function"==typeof n?function(t){return n.apply(r,t)}:n}function p(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return r(t,e);var i={}.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?r(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function d(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.call(t,e);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof e?e:e+""}if(!e)throw new Error("@clappr/hlsjs-playback requires hls.js (^1) to be loaded before it");var f=t.Utils.now,y=t.Utils.listContainsIgnoreCase;t.Events.register("PLAYBACK_FRAGMENT_CHANGED"),t.Events.register("PLAYBACK_FRAGMENT_PARSING_METADATA");var v=function(r){function a(){var t;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,a);for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return(t=i(this,a,[].concat(r))).options.hlsPlayback=h(h({},t.defaultOptions),t.options.hlsPlayback),t._timeUpdateThrottleDelay=200,t._timeUpdateFiringRate=.2,t._durationChangeMinOffset=.5,t._setInitialState(),t}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&c(t,e)}(a,r),s=a,l=[{key:"HLSJS",get:function(){return e}}],(o=[{key:"name",get:function(){return"hls"}},{key:"supportedVersion",get:function(){return{min:"0.16.2"}}},{key:"levels",get:function(){return this._levels||[]}},{key:"currentLevel",get:function(){return null===this._currentLevel||void 0===this._currentLevel?-1:this._currentLevel},set:function(e){this._currentLevel=e,this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH_START),this.options.playback.hlsUseNextLevel?this._hls.nextLevel=this._currentLevel:this._hls.currentLevel=this._currentLevel}},{key:"isReady",get:function(){return this._isReadyState}},{key:"latency",get:function(){return this._hls.latency}},{key:"liveSyncPosition",get:function(){return this._hls.liveSyncPosition}},{key:"currentProgramDateTime",get:function(){return this._hls.playingDate}},{key:"_startTime",get:function(){return this._playbackType===t.Playback.LIVE&&"EVENT"!==this._playlistType?this._extrapolatedStartTime:this._playableRegionStartTime}},{key:"_now",get:function(){return f()}},{key:"_extrapolatedStartTime",get:function(){if(!this._localStartTimeCorrelation)return this._playableRegionStartTime;var t=this._localStartTimeCorrelation,e=this._now-t.local,r=(t.remote+e)/1e3;return Math.min(r,this._playableRegionStartTime+this._extrapolatedWindowDuration)}},{key:"_extrapolatedEndTime",get:function(){var t=this._playableRegionStartTime+this._playableRegionDuration;if(!this._localEndTimeCorrelation)return t;var e=this._localEndTimeCorrelation,r=this._now-e.local,i=(e.remote+r)/1e3;return Math.max(t-this._extrapolatedWindowDuration,Math.min(i,t))}},{key:"_duration",get:function(){return this._extrapolatedEndTime-this._startTime}},{key:"_extrapolatedWindowDuration",get:function(){return null===this._segmentTargetDuration?0:this._extrapolatedWindowNumSegments*this._segmentTargetDuration}},{key:"bandwidthEstimate",get:function(){return this._hls&&this._hls.bandwidthEstimate}},{key:"defaultOptions",get:function(){return{preload:!0}}},{key:"customListeners",get:function(){return this.options.hlsPlayback&&this.options.hlsPlayback.customListeners||[]}},{key:"sourceMedia",get:function(){return this.options.src}},{key:"currentTimestamp",get:function(){return this._currentFragment?(this._currentFragment.programDateTime+1e3*(this.el.currentTime-this._currentFragment.start))/1e3:null}},{key:"_setInitialState",value:function(){var e;this._minDvrSize=void 0===this.options.hlsMinimumDvrSize?60:this.options.hlsMinimumDvrSize,this._extrapolatedWindowNumSegments=this.options.playback&&void 0!==this.options.playback.extrapolatedWindowNumSegments?this.options.playback.extrapolatedWindowNumSegments:2,this._playbackType=t.Playback.VOD,this._lastTimeUpdate={current:0,total:0,firstFragDateTime:0},this._lastTimeUpdateFiredTime=0,this._lastDuration=null,this._playableRegionStartTime=0,this._localStartTimeCorrelation=null,this._localEndTimeCorrelation=null,this._playableRegionDuration=0,this._programDateTime=0,this._durationExcludesAfterLiveSyncPoint=!1,this._segmentTargetDuration=null,this._playlistType=null,this._recoverAttemptsRemaining=null!==(e=this.options.hlsRecoverAttempts)&&void 0!==e?e:16}},{key:"_setup",value:function(){this._destroyHLSInstance(),this._createHLSInstance(),this._listenHLSEvents(),this._attachHLSMedia()}},{key:"_destroyHLSInstance",value:function(){this._hls&&(this._manifestLoading=!1,this._ccIsSetup=!1,this._ccTracksUpdated=!1,this._setInitialState(),this._hls.destroy(),this._hls=null)}},{key:"_createHLSInstance",value:function(){var t=h({},this.options.playback.hlsjsConfig);this._hls=new e(t)}},{key:"_attachHLSMedia",value:function(){this._hls&&this._hls.attachMedia(this.el)}},{key:"_listenHLSEvents",value:function(){var t=this;this._hls&&(this._hls.once(e.Events.MEDIA_ATTACHED,function(){t.options.hlsPlayback.preload&&t._hls.loadSource(t.options.src)}),this._hls.on(e.Events.MANIFEST_LOADING,function(){t._manifestLoading=!0}),this._hls.on(e.Events.LEVEL_LOADED,function(e,r){return t._updatePlaybackType(e,r)}),this._hls.on(e.Events.LEVEL_UPDATED,function(e,r){return t._onLevelUpdated(e,r)}),this._hls.on(e.Events.LEVEL_SWITCHED,function(e,r){return t._onLevelSwitch(e,r)}),this._hls.on(e.Events.FRAG_CHANGED,function(e,r){return t._onFragmentChanged(e,r)}),this._hls.on(e.Events.FRAG_LOADED,function(e,r){return t._onFragmentLoaded(e,r)}),this._hls.on(e.Events.FRAG_BUFFERED,function(e,r){return t._onFragmentBuffered(e,r)}),this._hls.on(e.Events.FRAG_PARSING_METADATA,function(e,r){return t._onFragmentParsingMetadata(e,r)}),this._hls.on(e.Events.ERROR,function(e,r){return t._onHLSJSError(e,r)}),this._hls.on(e.Events.SUBTITLE_TRACK_LOADED,function(e,r){return t._onSubtitleLoaded(e,r)}),this._hls.on(e.Events.SUBTITLE_TRACKS_UPDATED,function(){return t._ccTracksUpdated=!0}),this.bindCustomListeners())}},{key:"bindCustomListeners",value:function(){var t=this;this.customListeners.forEach(function(e){var r=e.eventName,i=e.once?"once":"on";r&&t._hls["".concat(i)](r,e.callback)})}},{key:"unbindCustomListeners",value:function(){var t=this;this.customListeners.forEach(function(e){var r=e.eventName;r&&t._hls.off(r,e.callback)})}},{key:"_onFragmentParsingMetadata",value:function(e,r){this.trigger(t.Events.Custom.PLAYBACK_FRAGMENT_PARSING_METADATA,{evt:e,data:r})}},{key:"render",value:function(){return this._ready(),_(a,"render",this,3)([])}},{key:"_ready",value:function(){this._isReadyState||(!this._hls&&this._setup(),this._isReadyState=!0,this.trigger(t.Events.PLAYBACK_READY,this.name))}},{key:"_recover",value:function(e,r,i){if(this._recoveredDecodingError)if(this._recoveredAudioCodecError){t.Log.error("hlsjs: failed to recover",{evt:e,data:r}),i.level=t.PlayerError.Levels.FATAL;var n=this.createError(i);this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop()}else this._recoveredAudioCodecError=!0,this._hls.swapAudioCodec(),this._hls.recoverMediaError(),this.play();else this._recoveredDecodingError=!0,this._hls.recoverMediaError(),this.play()}},{key:"_setupSrc",value:function(t){}},{key:"_startTimeUpdateTimer",value:function(){var t=this;this._timeUpdateTimer||(this._timeUpdateTimer=setInterval(function(){t._onDurationChange(),t._onTimeUpdate()},100))}},{key:"_stopTimeUpdateTimer",value:function(){this._timeUpdateTimer&&(clearInterval(this._timeUpdateTimer),this._timeUpdateTimer=null)}},{key:"getProgramDateTime",value:function(){return this._programDateTime}},{key:"getDuration",value:function(){return this._duration}},{key:"getFrameRate",value:function(){if(this._hls&&this._hls.levels&&this._hls.currentLevel>=0){var t=this._hls.levels[this._hls.currentLevel];return t&&t.frameRate?t.frameRate:null}return null}},{key:"getCurrentTime",value:function(){return Math.max(0,this.el.currentTime-this._startTime)}},{key:"getStartTimeOffset",value:function(){return this._startTime}},{key:"seekPercentage",value:function(t){this.seek(this._duration*(t/100))}},{key:"seek",value:function(e){e<0&&(t.Log.warn("Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point."),e=this.getDuration()),e+=this._startTime,this.el.currentTime=e}},{key:"seekToLivePoint",value:function(){this.seek(this.getDuration())}},{key:"_updateSettings",value:function(){this._playbackType===t.Playback.VOD?this.settings.left=["playpause","position","duration"]:this.dvrEnabled?this.settings.left=["playpause"]:this.settings.left=["playstop"],this.settings.seekEnabled=this.isSeekEnabled(),this.trigger(t.Events.PLAYBACK_SETTINGSUPDATE)}},{key:"_onHLSJSError",value:function(r,i){var n,a={code:"".concat(i.type,"_").concat(i.details),description:"".concat(this.name," error: type: ").concat(i.type,", details: ").concat(i.details),raw:i};if(i.response&&(a.description+=", response: ".concat(JSON.stringify(i.response))),i.fatal)if(this._recoverAttemptsRemaining>0)switch(this._recoverAttemptsRemaining-=1,i.type){case e.ErrorTypes.NETWORK_ERROR:switch(i.details){case e.ErrorDetails.MANIFEST_LOAD_ERROR:case e.ErrorDetails.MANIFEST_LOAD_TIMEOUT:case e.ErrorDetails.MANIFEST_PARSING_ERROR:case e.ErrorDetails.LEVEL_LOAD_ERROR:case e.ErrorDetails.LEVEL_LOAD_TIMEOUT:t.Log.error("hlsjs: unrecoverable network fatal error.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop();break;default:t.Log.warn("hlsjs: trying to recover from network error.",{evt:r,data:i}),a.level=t.PlayerError.Levels.WARN,this._hls.startLoad()}break;case e.ErrorTypes.MEDIA_ERROR:t.Log.warn("hlsjs: trying to recover from media error.",{evt:r,data:i}),a.level=t.PlayerError.Levels.WARN,this._recover(r,i,a);break;default:t.Log.error("hlsjs: could not recover from error.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop()}else t.Log.error("hlsjs: could not recover from error after maximum number of attempts.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop();else{if(this.options.playback.triggerFatalErrorOnResourceDenied&&this._keyIsDenied(i))return t.Log.error("hlsjs: could not load decrypt key.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),void this.stop();a.level=t.PlayerError.Levels.WARN,t.Log.warn("hlsjs: non-fatal error occurred",{evt:r,data:i})}}},{key:"_keyIsDenied",value:function(t){return t.type===e.ErrorTypes.NETWORK_ERROR&&t.details===e.ErrorDetails.KEY_LOAD_ERROR&&t.response&&t.response.code>=400}},{key:"_onTimeUpdate",value:function(){var e={current:this.getCurrentTime(),total:this.getDuration(),firstFragDateTime:this.getProgramDateTime()};this._shouldThrottleTimeUpdate(e)||(this._lastTimeUpdate=e,this._lastTimeUpdateFiredTime=this._now,this.trigger(t.Events.PLAYBACK_TIMEUPDATE,e,this.name))}},{key:"_shouldThrottleTimeUpdate",value:function(t){var e=Math.abs(t.current-this._lastTimeUpdate.current)<this._timeUpdateFiringRate,r=Math.abs(t.total-this._lastTimeUpdate.total)<this._durationChangeMinOffset,i=t.firstFragDateTime===this._lastTimeUpdate.firstFragDateTime,n=e&&r&&i,a=this._now-this._lastTimeUpdateFiredTime<this._timeUpdateThrottleDelay;return n&&a}},{key:"_onDurationChange",value:function(){var t=this.getDuration();Math.abs(this._lastDuration-t)<this._durationChangeMinOffset||(this._lastDuration=t,_(a,"_onDurationChange",this,3)([]))}},{key:"_onProgress",value:function(){if(this.el.buffered.length){for(var e=[],r=0,i=0;i<this.el.buffered.length;i++)e=[].concat(p(e),[{start:Math.max(0,this.el.buffered.start(i)-this._playableRegionStartTime),end:Math.max(0,this.el.buffered.end(i)-this._playableRegionStartTime)}]),this.el.currentTime>=e[i].start&&this.el.currentTime<=e[i].end&&(r=i);var n={start:e[r].start,current:e[r].end,total:this.getDuration()};this.trigger(t.Events.PLAYBACK_PROGRESS,n,e)}}},{key:"load",value:function(t){this._stopTimeUpdateTimer(),this.options.src=t,this._setup()}},{key:"play",value:function(){!this._hls&&this._setup(),!this._manifestLoading&&!this.options.hlsPlayback.preload&&this._hls.loadSource(this.options.src),_(a,"play",this,3)([]),this._startTimeUpdateTimer()}},{key:"pause",value:function(){this._hls&&this.el.pause()}},{key:"stop",value:function(){this._stopTimeUpdateTimer(),this._hls&&_(a,"stop",this,3)([]),this._destroyHLSInstance()}},{key:"destroy",value:function(){this._stopTimeUpdateTimer(),this._destroyHLSInstance(),_(a,"destroy",this,3)([])}},{key:"_updatePlaybackType",value:function(e,r){this._playbackType=r.details.live?t.Playback.LIVE:t.Playback.VOD,this._onLevelUpdated(e,r),this._ccTracksUpdated&&this._playbackType===t.Playback.LIVE&&this.hasClosedCaptionsTracks&&this._onSubtitleLoaded()}},{key:"_fillLevels",value:function(){this._levels=this._hls.levels.map(function(t,e){return{id:e,level:t,label:"".concat(t.bitrate/1e3,"Kbps")}}),this.trigger(t.Events.PLAYBACK_LEVELS_AVAILABLE,this._levels)}},{key:"_onLevelUpdated",value:function(r,i){this._segmentTargetDuration=i.details.targetduration,this._playlistType=i.details.type||null;var n=!1,a=!1,s=i.details.fragments,o=this._playableRegionStartTime,l=this._playableRegionDuration;if(0!==s.length){if(s[0].rawProgramDateTime&&(this._programDateTime=s[0].rawProgramDateTime),this._playableRegionStartTime!==s[0].start&&(n=!0,this._playableRegionStartTime=s[0].start),n)if(this._localStartTimeCorrelation){var u=this._localStartTimeCorrelation,h=this._now-u.local,c=(u.remote+h)/1e3;c<s[0].start?this._localStartTimeCorrelation={local:this._now,remote:1e3*s[0].start}:c>o+this._extrapolatedWindowDuration&&(this._localStartTimeCorrelation={local:this._now,remote:1e3*Math.max(s[0].start,o+this._extrapolatedWindowDuration)})}else this._localStartTimeCorrelation={local:this._now,remote:1e3*(s[0].start+this._extrapolatedWindowDuration/2)};var _=i.details.totalduration;if(this._playbackType===t.Playback.LIVE){var p=i.details.targetduration*((this.options.playback.hlsjsConfig||{}).liveSyncDurationCount||e.DefaultConfig.liveSyncDurationCount);p<=_?(_-=p,this._durationExcludesAfterLiveSyncPoint=!0):this._durationExcludesAfterLiveSyncPoint=!1}_!==this._playableRegionDuration&&(a=!0,this._playableRegionDuration=_);var d=s[0].start+_,f=o+l;if(d!==f)if(this._localEndTimeCorrelation){var y=this._localEndTimeCorrelation,v=this._now-y.local,m=(y.remote+v)/1e3;m>d?this._localEndTimeCorrelation={local:this._now,remote:1e3*d}:m<d-this._extrapolatedWindowDuration?this._localEndTimeCorrelation={local:this._now,remote:1e3*(d-this._extrapolatedWindowDuration)}:m>f&&(this._localEndTimeCorrelation={local:this._now,remote:1e3*f})}else this._localEndTimeCorrelation={local:this._now,remote:1e3*d};a&&this._onDurationChange(),n&&this._onProgress()}}},{key:"_onFragmentChanged",value:function(e,r){this._currentFragment=r.frag,this.trigger(t.Events.Custom.PLAYBACK_FRAGMENT_CHANGED,r)}},{key:"_onFragmentLoaded",value:function(e,r){this.trigger(t.Events.PLAYBACK_FRAGMENT_LOADED,r)}},{key:"_onFragmentBuffered",value:function(e,r){this.trigger(t.Events.PLAYBACK_FRAGMENT_BUFFERED,r)}},{key:"_onSubtitleLoaded",value:function(){if(!this._ccIsSetup){this.trigger(t.Events.PLAYBACK_SUBTITLE_AVAILABLE);var e=this._playbackType===t.Playback.LIVE?-1:this.closedCaptionsTrackId;this.closedCaptionsTrackId=e,this._ccIsSetup=!0}}},{key:"_onLevelSwitch",value:function(e,r){this.levels.length||this._fillLevels(),this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH_END),this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH,r);var i=this._hls.levels[r.level];i&&(this.highDefinition=i.height>=720||i.bitrate/1e3>=2e3,this.trigger(t.Events.PLAYBACK_HIGHDEFINITIONUPDATE,this.highDefinition),this.trigger(t.Events.PLAYBACK_BITRATE,{height:i.height,width:i.width,bandwidth:i.bitrate,bitrate:i.bitrate,level:r.level}))}},{key:"dvrEnabled",get:function(){return this._durationExcludesAfterLiveSyncPoint&&this._duration>=this._minDvrSize&&this.getPlaybackType()===t.Playback.LIVE}},{key:"getPlaybackType",value:function(){return this._playbackType}},{key:"isSeekEnabled",value:function(){return this._playbackType===t.Playback.VOD||this.dvrEnabled}}])&&n(s.prototype,o),l&&n(s,l),Object.defineProperty(s,"prototype",{writable:!1}),s;var s,o,l}(t.HTML5Video);return v.canPlay=function(t,r){var i=t.split("?")[0].match(/.*\.(.*)$/)||[],n=i.length>1&&"m3u8"===i[1].toLowerCase()||y(r,["application/vnd.apple.mpegurl","application/x-mpegURL"]);return!(!e.isSupported()||!n)},v});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@clappr/core"),require("hls.js")):"function"==typeof define&&define.amd?define(["@clappr/core","hls.js"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).HlsjsPlayback=e(t.Clappr,t.Hls)}(this,function(t,e){"use strict";function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,i=Array(e);r<e;r++)i[r]=t[r];return i}function i(t,e,r){return e=o(e),function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,l()?Reflect.construct(e,r||[],o(t).constructor):e.apply(t,r))}function n(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,d(i.key),i)}}function a(t,e,r){return(e=d(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function s(){return s="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,r){var i=function(t,e){for(;!{}.hasOwnProperty.call(t,e)&&null!==(t=o(t)););return t}(t,e);if(i){var n=Object.getOwnPropertyDescriptor(i,e);return n.get?n.get.call(arguments.length<3?t:r):n.value}},s.apply(null,arguments)}function o(t){return o=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},o(t)}function l(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(l=function(){return!!t})()}function u(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,i)}return r}function h(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?u(Object(r),!0).forEach(function(e){a(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):u(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function c(t,e){return c=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},c(t,e)}function _(t,e,r,i){var n=s(o(t.prototype),e,r);return 2&i&&"function"==typeof n?function(t){return n.apply(r,t)}:n}function p(t){return function(t){if(Array.isArray(t))return r(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,e){if(t){if("string"==typeof t)return r(t,e);var i={}.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?r(t,e):void 0}}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function d(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var i=r.call(t,e);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof e?e:e+""}if(!e)throw new Error("@clappr/hlsjs-playback requires hls.js (^1) to be loaded before it");var f=t.Utils.now,y=t.Utils.listContainsIgnoreCase;t.Events.register("PLAYBACK_FRAGMENT_CHANGED"),t.Events.register("PLAYBACK_FRAGMENT_PARSING_METADATA");var v=function(r){function a(){var t;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,a);for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return(t=i(this,a,[].concat(r))).options.hlsPlayback=h(h({},t.defaultOptions),t.options.hlsPlayback),t._timeUpdateThrottleDelay=200,t._timeUpdateFiringRate=.2,t._durationChangeMinOffset=.5,t._setInitialState(),t}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&c(t,e)}(a,r),s=a,l=[{key:"HLSJS",get:function(){return e}}],(o=[{key:"name",get:function(){return"hls"}},{key:"supportedVersion",get:function(){return{min:"0.16.3"}}},{key:"levels",get:function(){return this._levels||[]}},{key:"currentLevel",get:function(){return null===this._currentLevel||void 0===this._currentLevel?-1:this._currentLevel},set:function(e){this._currentLevel=e,this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH_START),this.options.playback.hlsUseNextLevel?this._hls.nextLevel=this._currentLevel:this._hls.currentLevel=this._currentLevel}},{key:"isReady",get:function(){return this._isReadyState}},{key:"latency",get:function(){return this._hls.latency}},{key:"liveSyncPosition",get:function(){return this._hls.liveSyncPosition}},{key:"currentProgramDateTime",get:function(){return this._hls.playingDate}},{key:"_startTime",get:function(){return this._playbackType===t.Playback.LIVE&&"EVENT"!==this._playlistType?this._extrapolatedStartTime:this._playableRegionStartTime}},{key:"_now",get:function(){return f()}},{key:"_extrapolatedStartTime",get:function(){if(!this._localStartTimeCorrelation)return this._playableRegionStartTime;var t=this._localStartTimeCorrelation,e=this._now-t.local,r=(t.remote+e)/1e3;return Math.min(r,this._playableRegionStartTime+this._extrapolatedWindowDuration)}},{key:"_extrapolatedEndTime",get:function(){var t=this._playableRegionStartTime+this._playableRegionDuration;if(!this._localEndTimeCorrelation)return t;var e=this._localEndTimeCorrelation,r=this._now-e.local,i=(e.remote+r)/1e3;return Math.max(t-this._extrapolatedWindowDuration,Math.min(i,t))}},{key:"_duration",get:function(){return this._extrapolatedEndTime-this._startTime}},{key:"_extrapolatedWindowDuration",get:function(){return null===this._segmentTargetDuration?0:this._extrapolatedWindowNumSegments*this._segmentTargetDuration}},{key:"bandwidthEstimate",get:function(){return this._hls&&this._hls.bandwidthEstimate}},{key:"defaultOptions",get:function(){return{preload:!0}}},{key:"customListeners",get:function(){return this.options.hlsPlayback&&this.options.hlsPlayback.customListeners||[]}},{key:"sourceMedia",get:function(){return this.options.src}},{key:"currentTimestamp",get:function(){return this._currentFragment?(this._currentFragment.programDateTime+1e3*(this.el.currentTime-this._currentFragment.start))/1e3:null}},{key:"_setInitialState",value:function(){var e;this._minDvrSize=void 0===this.options.hlsMinimumDvrSize?60:this.options.hlsMinimumDvrSize,this._extrapolatedWindowNumSegments=this.options.playback&&void 0!==this.options.playback.extrapolatedWindowNumSegments?this.options.playback.extrapolatedWindowNumSegments:2,this._playbackType=t.Playback.VOD,this._lastTimeUpdate={current:0,total:0,firstFragDateTime:0},this._lastTimeUpdateFiredTime=0,this._lastDuration=null,this._playableRegionStartTime=0,this._localStartTimeCorrelation=null,this._localEndTimeCorrelation=null,this._playableRegionDuration=0,this._programDateTime=0,this._durationExcludesAfterLiveSyncPoint=!1,this._segmentTargetDuration=null,this._playlistType=null,this._recoverAttemptsRemaining=null!==(e=this.options.hlsRecoverAttempts)&&void 0!==e?e:16}},{key:"_setup",value:function(){this._destroyHLSInstance(),this._createHLSInstance(),this._listenHLSEvents(),this._attachHLSMedia()}},{key:"_destroyHLSInstance",value:function(){this._hls&&(this._manifestLoading=!1,this._ccIsSetup=!1,this._ccTracksUpdated=!1,this._setInitialState(),this._hls.destroy(),this._hls=null)}},{key:"_createHLSInstance",value:function(){var t=h({},this.options.playback.hlsjsConfig);this._hls=new e(t)}},{key:"_attachHLSMedia",value:function(){this._hls&&this._hls.attachMedia(this.el)}},{key:"_listenHLSEvents",value:function(){var t=this;this._hls&&(this._hls.once(e.Events.MEDIA_ATTACHED,function(){t.options.hlsPlayback.preload&&t._hls.loadSource(t.options.src)}),this._hls.on(e.Events.MANIFEST_LOADING,function(){t._manifestLoading=!0}),this._hls.on(e.Events.LEVEL_LOADED,function(e,r){return t._updatePlaybackType(e,r)}),this._hls.on(e.Events.LEVEL_UPDATED,function(e,r){return t._onLevelUpdated(e,r)}),this._hls.on(e.Events.LEVEL_SWITCHED,function(e,r){return t._onLevelSwitch(e,r)}),this._hls.on(e.Events.FRAG_CHANGED,function(e,r){return t._onFragmentChanged(e,r)}),this._hls.on(e.Events.FRAG_LOADED,function(e,r){return t._onFragmentLoaded(e,r)}),this._hls.on(e.Events.FRAG_BUFFERED,function(e,r){return t._onFragmentBuffered(e,r)}),this._hls.on(e.Events.FRAG_PARSING_METADATA,function(e,r){return t._onFragmentParsingMetadata(e,r)}),this._hls.on(e.Events.ERROR,function(e,r){return t._onHLSJSError(e,r)}),this._hls.on(e.Events.SUBTITLE_TRACK_LOADED,function(e,r){return t._onSubtitleLoaded(e,r)}),this._hls.on(e.Events.SUBTITLE_TRACKS_UPDATED,function(){return t._ccTracksUpdated=!0}),this.bindCustomListeners())}},{key:"bindCustomListeners",value:function(){var t=this;this.customListeners.forEach(function(e){var r=e.eventName,i=e.once?"once":"on";r&&t._hls["".concat(i)](r,e.callback)})}},{key:"unbindCustomListeners",value:function(){var t=this;this.customListeners.forEach(function(e){var r=e.eventName;r&&t._hls.off(r,e.callback)})}},{key:"_onFragmentParsingMetadata",value:function(e,r){this.trigger(t.Events.Custom.PLAYBACK_FRAGMENT_PARSING_METADATA,{evt:e,data:r})}},{key:"render",value:function(){return this._ready(),_(a,"render",this,3)([])}},{key:"_ready",value:function(){this._isReadyState||(!this._hls&&this._setup(),this._isReadyState=!0,this.trigger(t.Events.PLAYBACK_READY,this.name))}},{key:"_recover",value:function(e,r,i){if(this._recoveredDecodingError)if(this._recoveredAudioCodecError){t.Log.error("hlsjs: failed to recover",{evt:e,data:r}),i.level=t.PlayerError.Levels.FATAL;var n=this.createError(i);this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop()}else this._recoveredAudioCodecError=!0,this._hls.swapAudioCodec(),this._hls.recoverMediaError(),this.play();else this._recoveredDecodingError=!0,this._hls.recoverMediaError(),this.play()}},{key:"_setupSrc",value:function(t){}},{key:"_startTimeUpdateTimer",value:function(){var t=this;this._timeUpdateTimer||(this._timeUpdateTimer=setInterval(function(){t._onDurationChange(),t._onTimeUpdate()},100))}},{key:"_stopTimeUpdateTimer",value:function(){this._timeUpdateTimer&&(clearInterval(this._timeUpdateTimer),this._timeUpdateTimer=null)}},{key:"getProgramDateTime",value:function(){return this._programDateTime}},{key:"getDuration",value:function(){return this._duration}},{key:"getFrameRate",value:function(){if(this._hls&&this._hls.levels&&this._hls.currentLevel>=0){var t=this._hls.levels[this._hls.currentLevel];return t&&t.frameRate?t.frameRate:null}return null}},{key:"getCurrentTime",value:function(){return Math.max(0,this.el.currentTime-this._startTime)}},{key:"getStartTimeOffset",value:function(){return this._startTime}},{key:"seekPercentage",value:function(t){this.seek(this._duration*(t/100))}},{key:"seek",value:function(e){e<0&&(t.Log.warn("Attempt to seek to a negative time. Resetting to live point. Use seekToLivePoint() to seek to the live point."),e=this.getDuration()),e+=this._startTime,this.el.currentTime=e}},{key:"seekToLivePoint",value:function(){this.seek(this.getDuration())}},{key:"_updateSettings",value:function(){this._playbackType===t.Playback.VOD?this.settings.left=["playpause","position","duration"]:this.dvrEnabled?this.settings.left=["playpause"]:this.settings.left=["playstop"],this.settings.seekEnabled=this.isSeekEnabled(),this.trigger(t.Events.PLAYBACK_SETTINGSUPDATE)}},{key:"_onHLSJSError",value:function(r,i){var n,a={code:"".concat(i.type,"_").concat(i.details),description:"".concat(this.name," error: type: ").concat(i.type,", details: ").concat(i.details),raw:i};if(i.response&&(a.description+=", response: ".concat(JSON.stringify(i.response))),i.fatal)if(this._recoverAttemptsRemaining>0)switch(this._recoverAttemptsRemaining-=1,i.type){case e.ErrorTypes.NETWORK_ERROR:switch(i.details){case e.ErrorDetails.MANIFEST_LOAD_ERROR:case e.ErrorDetails.MANIFEST_LOAD_TIMEOUT:case e.ErrorDetails.MANIFEST_PARSING_ERROR:case e.ErrorDetails.LEVEL_LOAD_ERROR:case e.ErrorDetails.LEVEL_LOAD_TIMEOUT:t.Log.error("hlsjs: unrecoverable network fatal error.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop();break;default:t.Log.warn("hlsjs: trying to recover from network error.",{evt:r,data:i}),a.level=t.PlayerError.Levels.WARN,this._hls.startLoad()}break;case e.ErrorTypes.MEDIA_ERROR:t.Log.warn("hlsjs: trying to recover from media error.",{evt:r,data:i}),a.level=t.PlayerError.Levels.WARN,this._recover(r,i,a);break;default:t.Log.error("hlsjs: could not recover from error.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop()}else t.Log.error("hlsjs: could not recover from error after maximum number of attempts.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),this.stop();else{if(this.options.playback.triggerFatalErrorOnResourceDenied&&this._keyIsDenied(i))return t.Log.error("hlsjs: could not load decrypt key.",{evt:r,data:i}),n=this.createError(a),this.trigger(t.Events.PLAYBACK_ERROR,n),void this.stop();a.level=t.PlayerError.Levels.WARN,t.Log.warn("hlsjs: non-fatal error occurred",{evt:r,data:i})}}},{key:"_keyIsDenied",value:function(t){return t.type===e.ErrorTypes.NETWORK_ERROR&&t.details===e.ErrorDetails.KEY_LOAD_ERROR&&t.response&&t.response.code>=400}},{key:"_onTimeUpdate",value:function(){var e={current:this.getCurrentTime(),total:this.getDuration(),firstFragDateTime:this.getProgramDateTime()};this._shouldThrottleTimeUpdate(e)||(this._lastTimeUpdate=e,this._lastTimeUpdateFiredTime=this._now,this.trigger(t.Events.PLAYBACK_TIMEUPDATE,e,this.name))}},{key:"_shouldThrottleTimeUpdate",value:function(t){var e=Math.abs(t.current-this._lastTimeUpdate.current)<this._timeUpdateFiringRate,r=Math.abs(t.total-this._lastTimeUpdate.total)<this._durationChangeMinOffset,i=t.firstFragDateTime===this._lastTimeUpdate.firstFragDateTime,n=e&&r&&i,a=this._now-this._lastTimeUpdateFiredTime<this._timeUpdateThrottleDelay;return n&&a}},{key:"_onDurationChange",value:function(){var t=this.getDuration();Math.abs(this._lastDuration-t)<this._durationChangeMinOffset||(this._lastDuration=t,_(a,"_onDurationChange",this,3)([]))}},{key:"_onProgress",value:function(){if(this.el.buffered.length){for(var e=[],r=0,i=0;i<this.el.buffered.length;i++)e=[].concat(p(e),[{start:Math.max(0,this.el.buffered.start(i)-this._playableRegionStartTime),end:Math.max(0,this.el.buffered.end(i)-this._playableRegionStartTime)}]),this.el.currentTime>=e[i].start&&this.el.currentTime<=e[i].end&&(r=i);var n={start:e[r].start,current:e[r].end,total:this.getDuration()};this.trigger(t.Events.PLAYBACK_PROGRESS,n,e)}}},{key:"load",value:function(t){this._stopTimeUpdateTimer(),this.options.src=t,this._setup()}},{key:"play",value:function(){!this._hls&&this._setup(),!this._manifestLoading&&!this.options.hlsPlayback.preload&&this._hls.loadSource(this.options.src),_(a,"play",this,3)([]),this._startTimeUpdateTimer()}},{key:"pause",value:function(){this._hls&&this.el.pause()}},{key:"stop",value:function(){this._stopTimeUpdateTimer(),this._hls&&_(a,"stop",this,3)([]),this._destroyHLSInstance()}},{key:"destroy",value:function(){this._stopTimeUpdateTimer(),this._destroyHLSInstance(),_(a,"destroy",this,3)([])}},{key:"_updatePlaybackType",value:function(e,r){this._playbackType=r.details.live?t.Playback.LIVE:t.Playback.VOD,this._onLevelUpdated(e,r),this._ccTracksUpdated&&this._playbackType===t.Playback.LIVE&&this.hasClosedCaptionsTracks&&this._onSubtitleLoaded()}},{key:"_fillLevels",value:function(){this._levels=this._hls.levels.map(function(t,e){return{id:e,level:t,label:"".concat(t.bitrate/1e3,"Kbps")}}),this.trigger(t.Events.PLAYBACK_LEVELS_AVAILABLE,this._levels)}},{key:"_onLevelUpdated",value:function(r,i){this._segmentTargetDuration=i.details.targetduration,this._playlistType=i.details.type||null;var n=!1,a=!1,s=i.details.fragments,o=this._playableRegionStartTime,l=this._playableRegionDuration;if(0!==s.length){if(s[0].rawProgramDateTime&&(this._programDateTime=s[0].rawProgramDateTime),this._playableRegionStartTime!==s[0].start&&(n=!0,this._playableRegionStartTime=s[0].start),n)if(this._localStartTimeCorrelation){var u=this._localStartTimeCorrelation,h=this._now-u.local,c=(u.remote+h)/1e3;c<s[0].start?this._localStartTimeCorrelation={local:this._now,remote:1e3*s[0].start}:c>o+this._extrapolatedWindowDuration&&(this._localStartTimeCorrelation={local:this._now,remote:1e3*Math.max(s[0].start,o+this._extrapolatedWindowDuration)})}else this._localStartTimeCorrelation={local:this._now,remote:1e3*(s[0].start+this._extrapolatedWindowDuration/2)};var _=i.details.totalduration;if(this._playbackType===t.Playback.LIVE){var p=i.details.targetduration*((this.options.playback.hlsjsConfig||{}).liveSyncDurationCount||e.DefaultConfig.liveSyncDurationCount);p<=_?(_-=p,this._durationExcludesAfterLiveSyncPoint=!0):this._durationExcludesAfterLiveSyncPoint=!1}_!==this._playableRegionDuration&&(a=!0,this._playableRegionDuration=_);var d=s[0].start+_,f=o+l;if(d!==f)if(this._localEndTimeCorrelation){var y=this._localEndTimeCorrelation,v=this._now-y.local,m=(y.remote+v)/1e3;m>d?this._localEndTimeCorrelation={local:this._now,remote:1e3*d}:m<d-this._extrapolatedWindowDuration?this._localEndTimeCorrelation={local:this._now,remote:1e3*(d-this._extrapolatedWindowDuration)}:m>f&&(this._localEndTimeCorrelation={local:this._now,remote:1e3*f})}else this._localEndTimeCorrelation={local:this._now,remote:1e3*d};a&&this._onDurationChange(),n&&this._onProgress()}}},{key:"_onFragmentChanged",value:function(e,r){this._currentFragment=r.frag,this.trigger(t.Events.Custom.PLAYBACK_FRAGMENT_CHANGED,r)}},{key:"_onFragmentLoaded",value:function(e,r){this.trigger(t.Events.PLAYBACK_FRAGMENT_LOADED,r)}},{key:"_onFragmentBuffered",value:function(e,r){this.trigger(t.Events.PLAYBACK_FRAGMENT_BUFFERED,r)}},{key:"_onSubtitleLoaded",value:function(){if(!this._ccIsSetup){this.trigger(t.Events.PLAYBACK_SUBTITLE_AVAILABLE);var e=this._playbackType===t.Playback.LIVE?-1:this.closedCaptionsTrackId;this.closedCaptionsTrackId=e,this._ccIsSetup=!0}}},{key:"_onLevelSwitch",value:function(e,r){this.levels.length||this._fillLevels(),this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH_END),this.trigger(t.Events.PLAYBACK_LEVEL_SWITCH,r);var i=this._hls.levels[r.level];i&&(this.highDefinition=i.height>=720||i.bitrate/1e3>=2e3,this.trigger(t.Events.PLAYBACK_HIGHDEFINITIONUPDATE,this.highDefinition),this.trigger(t.Events.PLAYBACK_BITRATE,{height:i.height,width:i.width,bandwidth:i.bitrate,bitrate:i.bitrate,level:r.level}))}},{key:"dvrEnabled",get:function(){return this._durationExcludesAfterLiveSyncPoint&&this._duration>=this._minDvrSize&&this.getPlaybackType()===t.Playback.LIVE}},{key:"getPlaybackType",value:function(){return this._playbackType}},{key:"isSeekEnabled",value:function(){return this._playbackType===t.Playback.VOD||this.dvrEnabled}}])&&n(s.prototype,o),l&&n(s,l),Object.defineProperty(s,"prototype",{writable:!1}),s;var s,o,l}(t.HTML5Video);return v.canPlay=function(t,r){var i=t.split("?")[0].match(/.*\.(.*)$/)||[],n=i.length>1&&"m3u8"===i[1].toLowerCase()||y(r,["application/vnd.apple.mpegurl","application/x-mpegURL"]);return!(!e.isSupported()||!n)},v});
2
2
  //# sourceMappingURL=hlsjs-playback.min.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clappr/hlsjs-playback",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "HLS Playback based on hls.js",
5
5
  "main": "./dist/hlsjs-playback.js",
6
6
  "module": "./dist/hlsjs-playback.esm.js",
@@ -41,7 +41,7 @@
41
41
  "hls.js": "^1"
42
42
  },
43
43
  "devDependencies": {
44
- "@clappr/core": "^0.16.2",
45
- "hls.js": "~1.6.17"
44
+ "@clappr/core": "^0.16.3",
45
+ "hls.js": "^1"
46
46
  }
47
47
  }
package/src/hls.test.js CHANGED
@@ -442,9 +442,7 @@ describe('HlsjsPlayback', () => {
442
442
  }
443
443
  const playback = new HlsjsPlayback({ src: 'http://clappr.io/foo.m3u8' })
444
444
  playback.el.currentTime = 5
445
- playback._setup()
446
- playback.unbindCustomListeners()
447
- playback._hls.trigger(HLSJS.Events.FRAG_CHANGED, fragmentMock)
445
+ playback._onFragmentChanged(HLSJS.Events.FRAG_CHANGED, fragmentMock)
448
446
  expect(playback.currentTimestamp).toBe(1556663045) // 'Tue Apr 30 2019 19:24:05'
449
447
  })
450
448
 
@@ -455,6 +453,46 @@ describe('HlsjsPlayback', () => {
455
453
  })
456
454
  })
457
455
 
456
+ describe('level and fragment events', () => {
457
+ test('_onLevelSwitch fills levels and reports bitrate', () => {
458
+ const playback = createPlayback()
459
+ playback._hls = {
460
+ levels: [{ height: 1080, width: 1920, bitrate: 4000000 }]
461
+ }
462
+ const onSwitch = jest.fn()
463
+ const onHd = jest.fn()
464
+ const onBitrate = jest.fn()
465
+ playback.on(Events.PLAYBACK_LEVEL_SWITCH, onSwitch)
466
+ playback.on(Events.PLAYBACK_HIGHDEFINITIONUPDATE, onHd)
467
+ playback.on(Events.PLAYBACK_BITRATE, onBitrate)
468
+
469
+ playback._onLevelSwitch(HLSJS.Events.LEVEL_SWITCHED, { level: 0 })
470
+
471
+ expect(playback.levels).toHaveLength(1)
472
+ expect(playback.highDefinition).toBe(true)
473
+ expect(onSwitch).toHaveBeenCalled()
474
+ expect(onHd).toHaveBeenCalledWith(true)
475
+ expect(onBitrate).toHaveBeenCalledWith(
476
+ expect.objectContaining({ height: 1080, bitrate: 4000000, level: 0 })
477
+ )
478
+ })
479
+
480
+ test('_onFragmentLoaded and _onFragmentBuffered forward playback events', () => {
481
+ const playback = createPlayback()
482
+ const onLoaded = jest.fn()
483
+ const onBuffered = jest.fn()
484
+ playback.on(Events.PLAYBACK_FRAGMENT_LOADED, onLoaded)
485
+ playback.on(Events.PLAYBACK_FRAGMENT_BUFFERED, onBuffered)
486
+ const data = { frag: { sn: 1 } }
487
+
488
+ playback._onFragmentLoaded(HLSJS.Events.FRAG_LOADED, data)
489
+ playback._onFragmentBuffered(HLSJS.Events.FRAG_BUFFERED, data)
490
+
491
+ expect(onLoaded).toHaveBeenCalledWith(data)
492
+ expect(onBuffered).toHaveBeenCalledWith(data)
493
+ })
494
+ })
495
+
458
496
  describe('DVR time model', () => {
459
497
  test('without correlation duration equals playable region duration', () => {
460
498
  const playback = createPlayback()