npmtest-shaka-player (v0.0.1)

Code coverage report for node-npmtest-shaka-player/node_modules/shaka-player/lib/util/pssh.js

Statements: 2.44% (1 / 41)      Branches: 0% (0 / 10)      Functions: 0% (0 / 1)      Lines: 2.44% (1 / 41)      Ignored: none     

All files » node-npmtest-shaka-player/node_modules/shaka-player/lib/util/ » pssh.js
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                                  2                                                                                                                                                                                                        
/**
 * @license
 * Copyright 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
goog.provide('shaka.util.Pssh');
 
goog.require('shaka.log');
goog.require('shaka.util.DataViewReader');
goog.require('shaka.util.Mp4Parser');
goog.require('shaka.util.Uint8ArrayUtils');
 
 
 
/**
 * Parse a PSSH box and extract the system IDs.
 *
 * @param {!Uint8Array} psshBox
 * @constructor
 * @struct
 * @throws {shaka.util.Error} if a PSSH box is truncated or contains a size
 *   field over 53 bits.
 */
shaka.util.Pssh = function(psshBox) {
  /**
   * In hex.
   * @type {!Array.<string>}
   */
  this.systemIds = [];
 
  /**
   * In hex.
   * @type {!Array.<string>}
   */
  this.cencKeyIds = [];
 
  /*
  * Array of tuples that define the startIndex + size for each
  * discrete pssh within |psshBox|
  * */
  this.dataBoundaries = [];
 
  // Parse the PSSH box.
  var reader = new shaka.util.DataViewReader(
      new DataView(psshBox.buffer),
      shaka.util.DataViewReader.Endianness.BIG_ENDIAN);
 
  var psshBoxFound = false;
 
  // There could be multiple boxes concatenated together.
  while (reader.hasMoreData()) {
    var size = shaka.util.Mp4Parser.findBox(shaka.util.Pssh.BOX_TYPE, reader);
    if (size == shaka.util.Mp4Parser.BOX_NOT_FOUND)
      break;
 
    // a file can have several pssh boxes, mark that at least
    // one was found
    psshBoxFound = true;
    var startPosition = reader.getPosition() - 8;
    var version = reader.readUint8();
    if (version > 1) {
      shaka.log.warning('Unrecognized PSSH version found!');
      reader.skip(size - (reader.getPosition() - startPosition));
      continue;
    }
 
    reader.skip(3);  // Skip flags.
 
    var systemId = shaka.util.Uint8ArrayUtils.toHex(reader.readBytes(16));
    var keyIds = [];
    if (version > 0) {
      var numKeyIds = reader.readUint32();
      for (var i = 0; i < numKeyIds; ++i) {
        var keyId = shaka.util.Uint8ArrayUtils.toHex(reader.readBytes(16));
        keyIds.push(keyId);
      }
    }
 
    var dataSize = reader.readUint32();
    reader.skip(dataSize);  // Ignore the data section.
 
    // Now that everything has been succesfully parsed from this box,
    // update member variables.
    this.cencKeyIds.push.apply(this.cencKeyIds, keyIds);
    this.systemIds.push(systemId);
    this.dataBoundaries.push({
      start: startPosition,
      end: reader.getPosition() - 1 }
    );
 
    if (reader.getPosition() != startPosition + size) {
      shaka.log.warning('Mismatch between box size and data size!');
      reader.skip(size - (reader.getPosition() - startPosition));
    }
  }
 
  if (!psshBoxFound)
    shaka.log.warning('No pssh box found!');
};
 
 
/** @const {number} */
shaka.util.Pssh.BOX_TYPE = 0x70737368;