# All files / src/loader fragment.js

93.62% Statements 44/47
90.32% Branches 28/31
78.57% Functions 11/14
93.62% Lines 44/47

    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

    111x
    111x
    111x
    111x

    111x

    222x

    25x
    22x

    25x

    3x
    3x

    3x

    46x
    6x

    40x
    16x

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

    22x

    24x
    24x

    24x

    13x

    33x

    30x
    11x

    30x

    4x

    10x

    10x
    40x

    10x

    11x

    11x
    10x
    10x
    10x
    10x
    10x

    11x

    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 = [];

        // 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 programDateTime () {
        Eif (!this._programDateTime && this.rawProgramDateTime) {
          this._programDateTime = new Date(Date.parse(this.rawProgramDateTime));
        }

        return this._programDateTime;
      }

      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 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;

        if (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;
      }
    }