Source: lib/util/pssh.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.Pssh');
  18. goog.require('goog.asserts');
  19. goog.require('shaka.log');
  20. goog.require('shaka.util.Mp4Parser');
  21. goog.require('shaka.util.Uint8ArrayUtils');
  22. /**
  23. * Parse a PSSH box and extract the system IDs.
  24. *
  25. * @param {!Uint8Array} psshBox
  26. * @constructor
  27. * @struct
  28. * @throws {shaka.util.Error} if a PSSH box is truncated or contains a size
  29. * field over 53 bits.
  30. */
  31. shaka.util.Pssh = function(psshBox) {
  32. /**
  33. * In hex.
  34. * @type {!Array.<string>}
  35. */
  36. this.systemIds = [];
  37. /**
  38. * In hex.
  39. * @type {!Array.<string>}
  40. */
  41. this.cencKeyIds = [];
  42. /*
  43. * Array of tuples that define the startIndex + size for each
  44. * discrete pssh within |psshBox|
  45. * */
  46. this.dataBoundaries = [];
  47. new shaka.util.Mp4Parser()
  48. .fullBox('pssh', this.parseBox_.bind(this)).parse(psshBox.buffer);
  49. if (this.dataBoundaries.length == 0) {
  50. shaka.log.warning('No pssh box found!');
  51. }
  52. };
  53. /**
  54. * @param {!shakaExtern.ParsedBox} box
  55. * @private
  56. */
  57. shaka.util.Pssh.prototype.parseBox_ = function(box) {
  58. goog.asserts.assert(
  59. box.version != null,
  60. 'PSSH boxes are full boxes and must have a valid version');
  61. goog.asserts.assert(
  62. box.flags != null,
  63. 'PSSH boxes are full boxes and must have a valid flag');
  64. if (box.version > 1) {
  65. shaka.log.warning('Unrecognized PSSH version found!');
  66. return;
  67. }
  68. let systemId = shaka.util.Uint8ArrayUtils.toHex(box.reader.readBytes(16));
  69. let keyIds = [];
  70. if (box.version > 0) {
  71. let numKeyIds = box.reader.readUint32();
  72. for (let i = 0; i < numKeyIds; ++i) {
  73. let keyId = shaka.util.Uint8ArrayUtils.toHex(box.reader.readBytes(16));
  74. keyIds.push(keyId);
  75. }
  76. }
  77. let dataSize = box.reader.readUint32();
  78. box.reader.skip(dataSize); // Ignore the data section.
  79. // Now that everything has been succesfully parsed from this box,
  80. // update member variables.
  81. this.cencKeyIds.push.apply(this.cencKeyIds, keyIds);
  82. this.systemIds.push(systemId);
  83. this.dataBoundaries.push({
  84. start: box.start,
  85. end: box.start + box.size - 1
  86. });
  87. if (box.reader.getPosition() != box.reader.getLength()) {
  88. shaka.log.warning('Mismatch between box size and data size!');
  89. }
  90. };