# All files / src hls.js

34.97% Statements 57/163
33.33% Branches 23/69
13.46% Functions 7/52
33.97% Lines 53/156

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402

    5x
    5x

    5x

    5x

    5x
    320x
    319x

    5x

    5x

    5x
    5x
    5x

    5x
    5x

    5x

    5x
    5x
    5x

    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x

    5x
    5x
    5x

    5x
    5x

    5x

    5x

    5x
    5x

    5x
    5x

    5x
    10x

    5x

    3x

    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x

     /**
     * HLS interface
     */
    import URLToolkit from 'url-toolkit';
    import Event from './events';
    import {ErrorTypes, ErrorDetails} from './errors';
    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 ID3TrackController from './controller/id3-track-controller';

    import {getMediaSource} from './helper/mediasource-helper';
    import {logger, enableLogs} from './utils/logger';
    import EventEmitter from 'events';
    import {hlsDefaultConfig} from './config';

    export default class Hls {
      static get version() {
        return __VERSION__;
      }
      static isSupported() {
        const mediaSource = getMediaSource();
        const sourceBuffer = window.SourceBuffer || window.WebKitSourceBuffer;
        const isTypeSupported = mediaSource &&
                                typeof mediaSource.isTypeSupported === 'function' &&
                                mediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"');

        // if SourceBuffer is exposed ensure its API is valid
        // safari and old version of Chrome doe not expose SourceBuffer globally so checking SourceBuffer.prototype is impossible
        const sourceBufferValidAPI = !sourceBuffer ||
                                     (sourceBuffer.prototype &&
                                     typeof sourceBuffer.prototype.appendBuffer === 'function' &&
                                     typeof sourceBuffer.prototype.remove === 'function');
        return isTypeSupported && sourceBufferValidAPI;
      }

      static get Events() {
        return Event;
      }

      static get ErrorTypes() {
        return ErrorTypes;
      }

      static get ErrorDetails() {
        return ErrorDetails;
      }

      static get DefaultConfig() {
        Eif(!Hls.defaultConfig) {
          return hlsDefaultConfig;
        }
        return Hls.defaultConfig;
      }

      static set DefaultConfig(defaultConfig) {
        Hls.defaultConfig = defaultConfig;
      }

      constructor(config = {}) {
        var defaultConfig = Hls.DefaultConfig;

        Iif ((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];
        }

        Iif (config.liveMaxLatencyDurationCount !== undefined && config.liveMaxLatencyDurationCount <= config.liveSyncDurationCount) {
          throw new Error('Illegal hls.js config: "liveMaxLatencyDurationCount" must be gt "liveSyncDurationCount"');
        }

        Iif (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();
        observer.trigger = function trigger (event, ...data) {
          observer.emit(event, event, ...data);
        };

        observer.off = function off (event, ...data) {
          observer.removeListener(event, ...data);
        };
        this.on = observer.on.bind(observer);
        this.off = observer.off.bind(observer);
        this.trigger = observer.trigger.bind(observer);

        // core controllers and network loaders
        const abrController = this.abrController = new config.abrController(this);
        const bufferController  = new config.bufferController(this);
        const capLevelController = new config.capLevelController(this);
        const fpsController = new config.fpsController(this);
        const playListLoader = new PlaylistLoader(this);
        const fragmentLoader = new FragmentLoader(this);
        const keyLoader = new KeyLoader(this);
        const id3TrackController = new ID3TrackController(this);

        // network controllers
        const levelController = this.levelController = new LevelController(this);
        const streamController = this.streamController = new StreamController(this);
        let networkControllers = [levelController, streamController];

        // optional audio stream controller
        let Controller = config.audioStreamController;
        Iif (Controller) {
          networkControllers.push(new Controller(this));
        }
        this.networkControllers = networkControllers;

        let coreComponents = [ playListLoader, fragmentLoader, keyLoader, abrController, bufferController, capLevelController, fpsController, id3TrackController ];

        // optional audio track and subtitle controller
        Controller = config.audioTrackController;
        Iif (Controller) {
          let audioTrackController = new Controller(this);
          this.audioTrackController = audioTrackController;
          coreComponents.push(audioTrackController);
        }

        Controller = config.subtitleTrackController;
        Iif (Controller) {
          let subtitleTrackController = new Controller(this);
          this.subtitleTrackController = subtitleTrackController;
          coreComponents.push(subtitleTrackController);
        }

        // optional subtitle controller
        [config.subtitleStreamController, config.timelineController].forEach(Controller => {
          Iif (Controller) {
            coreComponents.push(new Controller(this));
          }
        });
        this.coreComponents = coreComponents;
      }

      destroy() {
        logger.log('destroy');
        this.trigger(Event.DESTROYING);
        this.detachMedia();
        this.coreComponents.concat(this.networkControllers).forEach(component => {component.destroy();});
        this.url = null;
        this.observer.removeAllListeners();
        this._autoLevelCapping = -1;
      }

      attachMedia(media) {
        logger.log('attachMedia');
        this.media = media;
        this.trigger(Event.MEDIA_ATTACHING, {media: media});
      }

      detachMedia() {
        logger.log('detachMedia');
        this.trigger(Event.MEDIA_DETACHING);
        this.media = null;
      }

      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(Event.MANIFEST_LOADING, {url: url});
      }

      startLoad(startPosition=-1) {
        logger.log(`startLoad(${startPosition})`);
        this.networkControllers.forEach(controller => {controller.startLoad(startPosition);});
      }

      stopLoad() {
        logger.log('stopLoad');
        this.networkControllers.forEach(controller => {controller.stopLoad();});
      }

      swapAudioCodec() {
        logger.log('swapAudioCodec');
        this.streamController.swapAudioCodec();
      }

      recoverMediaError() {
        logger.log('recoverMediaError');
        var media = this.media;
        this.detachMedia();
        this.attachMedia(media);
      }

      /** Return all quality levels **/
      get levels() {
        return this.levelController.levels;
      }

      /** Return current playback quality level **/
      get currentLevel() {
        return this.streamController.currentLevel;
      }

      /* set quality level immediately (-1 for automatic level selection) */
      set currentLevel(newLevel) {
        logger.log(`set currentLevel:${newLevel}`);
        this.loadLevel = newLevel;
        this.streamController.immediateLevelSwitch();
      }

      /** Return next playback quality level (quality level of next fragment) **/
      get nextLevel() {
        return this.streamController.nextLevel;
      }

      /* set quality level for next fragment (-1 for automatic level selection) */
      set nextLevel(newLevel) {
        logger.log(`set nextLevel:${newLevel}`);
        this.levelController.manualLevel = newLevel;
        this.streamController.nextLevelSwitch();
      }

      /** Return the quality level of current/last loaded fragment **/
      get loadLevel() {
        return this.levelController.level;
      }

      /* set quality level for current/next loaded fragment (-1 for automatic level selection) */
      set loadLevel(newLevel) {
        logger.log(`set loadLevel:${newLevel}`);
        this.levelController.manualLevel = newLevel;
      }

      /** Return the quality level of next loaded fragment **/
      get nextLoadLevel() {
        return this.levelController.nextLoadLevel;
      }

      /** set quality level of next loaded fragment **/
      set nextLoadLevel(level) {
        this.levelController.nextLoadLevel = level;
      }

      /** Return first level (index of first level referenced in manifest)
      **/
      get firstLevel() {
        return Math.max(this.levelController.firstLevel, this.minAutoLevel);
      }

      /** set first level (index of first level referenced in manifest)
      **/
      set firstLevel(newLevel) {
        logger.log(`set firstLevel:${newLevel}`);
        this.levelController.firstLevel = newLevel;
      }

      /** Return start level (level of first fragment that will be played back)
          if not overrided by user, first level appearing in manifest will be used as start level
          if -1 : automatic start level selection, playback will start from level matching download bandwidth (determined from download of first segment)
      **/
      get startLevel() {
        return this.levelController.startLevel;
      }

      /** set  start level (level of first fragment that will be played back)
          if not overrided by user, first level appearing in manifest will be used as start level
          if -1 : automatic start level selection, playback will start from level matching download bandwidth (determined from download of first segment)
      **/
      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);
        }
        hls.levelController.startLevel = newLevel;
      }

      /** Return the capping/max level value that could be used by automatic level selection algorithm **/
      get autoLevelCapping() {
        return this._autoLevelCapping;
      }

      /** set the capping/max level value that could be used by automatic level selection algorithm **/
      set autoLevelCapping(newLevel) {
        logger.log(`set autoLevelCapping:${newLevel}`);
        this._autoLevelCapping = newLevel;
      }

      /* check if we are in automatic level selection mode */
      get autoLevelEnabled() {
        return (this.levelController.manualLevel === -1);
      }

      /* return manual level */
      get manualLevel() {
        return this.levelController.manualLevel;
      }

      /* return min level selectable in auto mode according to config.minAutoBitrate */
      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;
          Eif (levelNextBitrate > minAutoBitrate) {
            return i;
          }
        }
        return 0;
      }

      /* return max level selectable in auto mode according to autoLevelCapping */
      get maxAutoLevel() {
        const hls = this;
        const levels = hls.levels;
        const autoLevelCapping = hls.autoLevelCapping;
        let maxAutoLevel;
        Eif (autoLevelCapping=== -1 && levels && levels.length) {
          maxAutoLevel = levels.length - 1;
        } else {
          maxAutoLevel = autoLevelCapping;
        }
        return maxAutoLevel;
      }

      // return next auto level
      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);
      }

      // this setter is used to force next auto level
      // this is useful to force a switch down in auto mode : in case of load error on level N, hls.js can set nextAutoLevel to N-1 for example)
      // forced value is valid for one fragment. upon succesful frag loading at forced level, this value will be resetted to -1 by ABR controller
      set nextAutoLevel(nextLevel) {
        const hls = this;
        hls.abrController.nextAutoLevel = Math.max(hls.minAutoLevel,nextLevel);
      }

      /** get alternate audio tracks list from playlist **/
      get audioTracks() {
        const audioTrackController = this.audioTrackController;
        return audioTrackController ? audioTrackController.audioTracks : [];
      }

      /** get index of the selected audio track (index in audio track lists) **/
      get audioTrack() {
        const audioTrackController = this.audioTrackController;
        return audioTrackController ? audioTrackController.audioTrack : -1;
      }

      /** select an audio track, based on its index in audio track lists**/
      set audioTrack(audioTrackId) {
        const audioTrackController = this.audioTrackController;
        if (audioTrackController) {
          audioTrackController.audioTrack = audioTrackId;
        }
      }

      get liveSyncPosition() {
        return this.streamController.liveSyncPosition;
      }

      /** get alternate subtitle tracks list from playlist **/
      get subtitleTracks() {
        const subtitleTrackController = this.subtitleTrackController;
        return subtitleTrackController ? subtitleTrackController.subtitleTracks : [];
      }

      /** get index of the selected subtitle track (index in subtitle track lists) **/
      get subtitleTrack() {
        const subtitleTrackController = this.subtitleTrackController;
        return subtitleTrackController ? subtitleTrackController.subtitleTrack : -1;
      }

      /** select an subtitle track, based on its index in subtitle track lists**/
      set subtitleTrack(subtitleTrackId) {
        const subtitleTrackController = this.subtitleTrackController;
        if (subtitleTrackController) {
          subtitleTrackController.subtitleTrack = subtitleTrackId;
        }
      }

      get subtitleDisplay() {
        const subtitleTrackController = this.subtitleTrackController;
        return subtitleTrackController ? subtitleTrackController.subtitleDisplay : false;
      }

      set subtitleDisplay(value) {
        const subtitleTrackController = this.subtitleTrackController;
        if (subtitleTrackController) {
          subtitleTrackController.subtitleDisplay = value;
        }
      }
    }