# All files / src/loader fragment.js

92% Statements 46/50
80.65% Branches 25/31
71.43% Functions 10/14
92% Lines 46/50

    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

    132x
    132x
    132x
    132x
    132x
    132x

    132x

    264x

    25x
    22x

    25x

    46x
    6x

    40x
    16x

    24x
    24x
    24x
    24x
    2x
    2x

    22x

    24x
    24x

    24x

    13x

    33x

    21x
    10x

    21x

    10x
    1x

    9x

    9x

    10x

    10x
    40x

    10x

    10x

    10x
    10x
    10x
    10x
    10x
    10x

    10x

    import URLToolkit from 'url-toolkit';

    import LevelKey from './level-key';

    export default class Fragment {
      constructor () {
        this._url = null;
        this._byteRange = null;
        this._decryptdata = null;
        this.tagList = [];
        this.programDateTime = null;
        this.rawProgramDateTime = null;

        // Holds the types of data this fragment supports
        this._elementaryStreams = {
          [Fragment.ElementaryStreamTypes.AUDIO]: false,
          [Fragment.ElementaryStreamTypes.VIDEO]: false
        };
      }

      /**
       * `type` property for this._elementaryStreams
       *
       * @enum
       */
      static get ElementaryStreamTypes () {
        return {
          AUDIO: 'audio',
          VIDEO: 'video'
        };
      }

      get url () {
        if (!this._url && this.relurl) {
          this._url = URLToolkit.buildAbsoluteURL(this.baseurl, this.relurl, { alwaysNormalize: true });
        }

        return this._url;
      }

      set url (value) {
        this._url = value;
      }

      get byteRange () {
        if (!this._byteRange && !this.rawByteRange) {
          return [];
        }

        if (this._byteRange) {
          return this._byteRange;
        }

        let byteRange = [];
        Eif (this.rawByteRange) {
          const params = this.rawByteRange.split('@', 2);
          if (params.length === 1) {
            const lastByteRangeEndOffset = this.lastByteRangeEndOffset;
            byteRange[0] = lastByteRangeEndOffset || 0;
          } else {
            byteRange[0] = parseInt(params[1]);
          }
          byteRange[1] = parseInt(params[0]) + byteRange[0];
          this._byteRange = byteRange;
        }
        return byteRange;
      }

      /**
       * @type {number}
       */
      get byteRangeStartOffset () {
        return this.byteRange[0];
      }

      get byteRangeEndOffset () {
        return this.byteRange[1];
      }

      get decryptdata () {
        if (!this._decryptdata) {
          this._decryptdata = this.fragmentDecryptdataFromLevelkey(this.levelkey, this.sn);
        }

        return this._decryptdata;
      }

      get endProgramDateTime () {
        if (!Number.isFinite(this.programDateTime)) {
          return null;
        }

        let duration = !Number.isFinite(this.duration) ? 0 : this.duration;

        return this.programDateTime + (duration * 1000);
      }

      get encrypted () {
        return !!((this.decryptdata && this.decryptdata.uri !== null) && (this.decryptdata.key === null));
      }

      /**
       * @param {ElementaryStreamType} type
       */
      addElementaryStream (type) {
        this._elementaryStreams[type] = true;
      }

      /**
       * @param {ElementaryStreamType} type
       */
      hasElementaryStream (type) {
        return this._elementaryStreams[type] === true;
      }

      /**
       * Utility method for parseLevelPlaylist to create an initialization vector for a given segment
       * @returns {Uint8Array}
       */
      createInitializationVector (segmentNumber) {
        let uint8View = new Uint8Array(16);

        for (let i = 12; i < 16; i++) {
          uint8View[i] = (segmentNumber >> 8 * (15 - i)) & 0xff;
        }

        return uint8View;
      }

      /**
       * Utility method for parseLevelPlaylist to get a fragment's decryption data from the currently parsed encryption key data
       * @param levelkey - a playlist's encryption info
       * @param segmentNumber - the fragment's segment number
       * @returns {*} - an object to be applied as a fragment's decryptdata
       */
      fragmentDecryptdataFromLevelkey (levelkey, segmentNumber) {
        let decryptdata = levelkey;

        Eif (levelkey && levelkey.method && levelkey.uri && !levelkey.iv) {
          decryptdata = new LevelKey();
          decryptdata.method = levelkey.method;
          decryptdata.baseuri = levelkey.baseuri;
          decryptdata.reluri = levelkey.reluri;
          decryptdata.iv = this.createInitializationVector(segmentNumber);
        }

        return decryptdata;
      }
    }