Source: lib/media/content_workarounds.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.media.ContentWorkarounds');
  7. goog.require('goog.asserts');
  8. goog.require('shaka.log');
  9. goog.require('shaka.util.BufferUtils');
  10. goog.require('shaka.util.Error');
  11. goog.require('shaka.util.Lazy');
  12. goog.require('shaka.util.Mp4Parser');
  13. goog.require('shaka.util.Platform');
  14. goog.require('shaka.util.Uint8ArrayUtils');
  15. /**
  16. * @summary
  17. * A collection of methods to work around content issues on various platforms.
  18. */
  19. shaka.media.ContentWorkarounds = class {
  20. /**
  21. * Transform the init segment into a new init segment buffer that indicates
  22. * encryption. If the init segment already indicates encryption, return the
  23. * original init segment.
  24. *
  25. * Should only be called for MP4 init segments, and only on platforms that
  26. * need this workaround.
  27. *
  28. * @param {!BufferSource} initSegmentBuffer
  29. * @param {?string} uri
  30. * @return {!Uint8Array}
  31. * @see https://github.com/shaka-project/shaka-player/issues/2759
  32. */
  33. static fakeEncryption(initSegmentBuffer, uri) {
  34. const ContentWorkarounds = shaka.media.ContentWorkarounds;
  35. const initSegment = shaka.util.BufferUtils.toUint8(initSegmentBuffer);
  36. let modifiedInitSegment = initSegment;
  37. let isEncrypted = false;
  38. /** @type {shaka.extern.ParsedBox} */
  39. let stsdBox;
  40. const ancestorBoxes = [];
  41. const onSimpleAncestorBox = (box) => {
  42. ancestorBoxes.push(box);
  43. shaka.util.Mp4Parser.children(box);
  44. };
  45. const onEncryptionMetadataBox = (box) => {
  46. isEncrypted = true;
  47. };
  48. // Multiplexed content could have multiple boxes that we need to modify.
  49. // Add to this array in order of box offset. This will be important later,
  50. // when we process the boxes.
  51. /** @type {!Array.<{box: shaka.extern.ParsedBox, newType: number}>} */
  52. const boxesToModify = [];
  53. new shaka.util.Mp4Parser()
  54. .box('moov', onSimpleAncestorBox)
  55. .box('trak', onSimpleAncestorBox)
  56. .box('mdia', onSimpleAncestorBox)
  57. .box('minf', onSimpleAncestorBox)
  58. .box('stbl', onSimpleAncestorBox)
  59. .fullBox('stsd', (box) => {
  60. stsdBox = box;
  61. ancestorBoxes.push(box);
  62. shaka.util.Mp4Parser.sampleDescription(box);
  63. })
  64. .fullBox('encv', onEncryptionMetadataBox)
  65. .fullBox('enca', onEncryptionMetadataBox)
  66. .fullBox('dvav', (box) => {
  67. boxesToModify.push({
  68. box,
  69. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  70. });
  71. })
  72. .fullBox('dva1', (box) => {
  73. boxesToModify.push({
  74. box,
  75. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  76. });
  77. })
  78. .fullBox('dvh1', (box) => {
  79. boxesToModify.push({
  80. box,
  81. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  82. });
  83. })
  84. .fullBox('dvhe', (box) => {
  85. boxesToModify.push({
  86. box,
  87. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  88. });
  89. })
  90. .fullBox('dvc1', (box) => {
  91. boxesToModify.push({
  92. box,
  93. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  94. });
  95. })
  96. .fullBox('dvi1', (box) => {
  97. boxesToModify.push({
  98. box,
  99. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  100. });
  101. })
  102. .fullBox('hev1', (box) => {
  103. boxesToModify.push({
  104. box,
  105. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  106. });
  107. })
  108. .fullBox('hvc1', (box) => {
  109. boxesToModify.push({
  110. box,
  111. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  112. });
  113. })
  114. .fullBox('avc1', (box) => {
  115. boxesToModify.push({
  116. box,
  117. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  118. });
  119. })
  120. .fullBox('avc3', (box) => {
  121. boxesToModify.push({
  122. box,
  123. newType: ContentWorkarounds.BOX_TYPE_ENCV_,
  124. });
  125. })
  126. .fullBox('ac-3', (box) => {
  127. boxesToModify.push({
  128. box,
  129. newType: ContentWorkarounds.BOX_TYPE_ENCA_,
  130. });
  131. })
  132. .fullBox('ec-3', (box) => {
  133. boxesToModify.push({
  134. box,
  135. newType: ContentWorkarounds.BOX_TYPE_ENCA_,
  136. });
  137. })
  138. .fullBox('ac-4', (box) => {
  139. boxesToModify.push({
  140. box,
  141. newType: ContentWorkarounds.BOX_TYPE_ENCA_,
  142. });
  143. })
  144. .fullBox('mp4a', (box) => {
  145. boxesToModify.push({
  146. box,
  147. newType: ContentWorkarounds.BOX_TYPE_ENCA_,
  148. });
  149. }).parse(initSegment);
  150. if (isEncrypted) {
  151. shaka.log.debug('Init segment already indicates encryption.');
  152. return initSegment;
  153. }
  154. if (boxesToModify.length == 0 || !stsdBox) {
  155. shaka.log.error('Failed to find boxes needed to fake encryption!');
  156. shaka.log.v2('Failed init segment (hex):',
  157. shaka.util.Uint8ArrayUtils.toHex(initSegment));
  158. throw new shaka.util.Error(
  159. shaka.util.Error.Severity.CRITICAL,
  160. shaka.util.Error.Category.MEDIA,
  161. shaka.util.Error.Code.CONTENT_TRANSFORMATION_FAILED,
  162. uri);
  163. }
  164. // Modify boxes in order from largest offset to smallest, so that earlier
  165. // boxes don't have their offsets changed before we process them.
  166. boxesToModify.reverse(); // in place!
  167. for (const workItem of boxesToModify) {
  168. const insertedBoxType =
  169. shaka.util.Mp4Parser.typeToString(workItem.newType);
  170. shaka.log.debug(`Inserting "${insertedBoxType}" box into init segment.`);
  171. modifiedInitSegment = ContentWorkarounds.insertEncryptionMetadata_(
  172. modifiedInitSegment, stsdBox, workItem.box, ancestorBoxes,
  173. workItem.newType);
  174. }
  175. // Edge Windows needs the unmodified init segment to be appended after the
  176. // patched one, otherwise video element throws following error:
  177. // CHUNK_DEMUXER_ERROR_APPEND_FAILED: Sample encryption info is not
  178. // available.
  179. if (shaka.util.Platform.isEdge() && shaka.util.Platform.isWindows()) {
  180. const doubleInitSegment = new Uint8Array(initSegment.byteLength +
  181. modifiedInitSegment.byteLength);
  182. doubleInitSegment.set(modifiedInitSegment);
  183. doubleInitSegment.set(initSegment, modifiedInitSegment.byteLength);
  184. return doubleInitSegment;
  185. }
  186. return modifiedInitSegment;
  187. }
  188. /**
  189. * Insert an encryption metadata box ("encv" or "enca" box) into the MP4 init
  190. * segment, based on the source box ("mp4a", "avc1", etc). Returns a new
  191. * buffer containing the modified init segment.
  192. *
  193. * @param {!Uint8Array} initSegment
  194. * @param {shaka.extern.ParsedBox} stsdBox
  195. * @param {shaka.extern.ParsedBox} sourceBox
  196. * @param {!Array.<shaka.extern.ParsedBox>} ancestorBoxes
  197. * @param {number} metadataBoxType
  198. * @return {!Uint8Array}
  199. * @private
  200. */
  201. static insertEncryptionMetadata_(
  202. initSegment, stsdBox, sourceBox, ancestorBoxes, metadataBoxType) {
  203. const ContentWorkarounds = shaka.media.ContentWorkarounds;
  204. const metadataBoxArray = ContentWorkarounds.createEncryptionMetadata_(
  205. initSegment, sourceBox, metadataBoxType);
  206. // Construct a new init segment array with room for the encryption metadata
  207. // box we're adding.
  208. const newInitSegment =
  209. new Uint8Array(initSegment.byteLength + metadataBoxArray.byteLength);
  210. // For Xbox One & Edge, we cut and insert at the start of the source box.
  211. // For other platforms, we cut and insert at the end of the source box. It's
  212. // not clear why this is necessary on Xbox One, but it seems to be evidence
  213. // of another bug in the firmware implementation of MediaSource & EME.
  214. const cutPoint =
  215. (shaka.util.Platform.isXboxOne() || shaka.util.Platform.isEdge()) ?
  216. sourceBox.start :
  217. sourceBox.start + sourceBox.size;
  218. // The data before the cut point will be copied to the same location as
  219. // before. The data after that will be appended after the added metadata
  220. // box.
  221. const beforeData = initSegment.subarray(0, cutPoint);
  222. const afterData = initSegment.subarray(cutPoint);
  223. newInitSegment.set(beforeData);
  224. newInitSegment.set(metadataBoxArray, cutPoint);
  225. newInitSegment.set(afterData, cutPoint + metadataBoxArray.byteLength);
  226. // The parents up the chain from the encryption metadata box need their
  227. // sizes adjusted to account for the added box. These offsets should not be
  228. // changed, because they should all be within the first section we copy.
  229. for (const box of ancestorBoxes) {
  230. goog.asserts.assert(box.start < cutPoint,
  231. 'Ancestor MP4 box found in the wrong location! ' +
  232. 'Modified init segment will not make sense!');
  233. ContentWorkarounds.updateBoxSize_(
  234. newInitSegment, box.start, box.size + metadataBoxArray.byteLength);
  235. }
  236. // Add one to the sample entries field of the "stsd" box. This is a 4-byte
  237. // field just past the box header.
  238. const stsdBoxView = shaka.util.BufferUtils.toDataView(
  239. newInitSegment, stsdBox.start);
  240. const stsdBoxHeaderSize = shaka.util.Mp4Parser.headerSize(stsdBox);
  241. const numEntries = stsdBoxView.getUint32(stsdBoxHeaderSize);
  242. stsdBoxView.setUint32(stsdBoxHeaderSize, numEntries + 1);
  243. return newInitSegment;
  244. }
  245. /**
  246. * Create an encryption metadata box ("encv" or "enca" box), based on the
  247. * source box ("mp4a", "avc1", etc). Returns a new buffer containing the
  248. * encryption metadata box.
  249. *
  250. * @param {!Uint8Array} initSegment
  251. * @param {shaka.extern.ParsedBox} sourceBox
  252. * @param {number} metadataBoxType
  253. * @return {!Uint8Array}
  254. * @private
  255. */
  256. static createEncryptionMetadata_(initSegment, sourceBox, metadataBoxType) {
  257. const ContentWorkarounds = shaka.media.ContentWorkarounds;
  258. const sinfBoxArray = ContentWorkarounds.CANNED_SINF_BOX_.value();
  259. // Create a subarray which points to the source box data.
  260. const sourceBoxArray = initSegment.subarray(
  261. /* start= */ sourceBox.start,
  262. /* end= */ sourceBox.start + sourceBox.size);
  263. // Create a view on the source box array.
  264. const sourceBoxView = shaka.util.BufferUtils.toDataView(sourceBoxArray);
  265. // Create an array to hold the new encryption metadata box, which is based
  266. // on the source box.
  267. const metadataBoxArray = new Uint8Array(
  268. sourceBox.size + sinfBoxArray.byteLength);
  269. // Copy the source box into the new array.
  270. metadataBoxArray.set(sourceBoxArray, /* targetOffset= */ 0);
  271. // Change the box type.
  272. const metadataBoxView = shaka.util.BufferUtils.toDataView(metadataBoxArray);
  273. metadataBoxView.setUint32(
  274. ContentWorkarounds.BOX_TYPE_OFFSET_, metadataBoxType);
  275. // Append the "sinf" box to the encryption metadata box.
  276. metadataBoxArray.set(sinfBoxArray, /* targetOffset= */ sourceBox.size);
  277. // Update the "sinf" box's format field (in the child "frma" box) to reflect
  278. // the format of the original source box.
  279. const sourceBoxType = sourceBoxView.getUint32(
  280. ContentWorkarounds.BOX_TYPE_OFFSET_);
  281. metadataBoxView.setUint32(
  282. sourceBox.size + ContentWorkarounds.CANNED_SINF_BOX_FORMAT_OFFSET_,
  283. sourceBoxType);
  284. // Now update the encryption metadata box size.
  285. ContentWorkarounds.updateBoxSize_(
  286. metadataBoxArray, /* boxStart= */ 0, metadataBoxArray.byteLength);
  287. return metadataBoxArray;
  288. }
  289. /**
  290. * Modify an MP4 box's size field in-place.
  291. *
  292. * @param {!Uint8Array} dataArray
  293. * @param {number} boxStart The start position of the box in dataArray.
  294. * @param {number} newBoxSize The new size of the box.
  295. * @private
  296. */
  297. static updateBoxSize_(dataArray, boxStart, newBoxSize) {
  298. const ContentWorkarounds = shaka.media.ContentWorkarounds;
  299. const boxView = shaka.util.BufferUtils.toDataView(dataArray, boxStart);
  300. const sizeField = boxView.getUint32(ContentWorkarounds.BOX_SIZE_OFFSET_);
  301. if (sizeField == 0) { // Means "the rest of the box".
  302. // No adjustment needed for this box.
  303. } else if (sizeField == 1) { // Means "use 64-bit size box".
  304. // Set the 64-bit int in two 32-bit parts.
  305. // The high bits should definitely be 0 in practice, but we're being
  306. // thorough here.
  307. boxView.setUint32(ContentWorkarounds.BOX_SIZE_64_OFFSET_,
  308. newBoxSize >> 32);
  309. boxView.setUint32(ContentWorkarounds.BOX_SIZE_64_OFFSET_ + 4,
  310. newBoxSize & 0xffffffff);
  311. } else { // Normal 32-bit size field.
  312. // Not checking the size of the value here, since a box larger than 4GB is
  313. // unrealistic.
  314. boxView.setUint32(ContentWorkarounds.BOX_SIZE_OFFSET_, newBoxSize);
  315. }
  316. }
  317. };
  318. /**
  319. * A canned "sinf" box for use when adding fake encryption metadata to init
  320. * segments.
  321. *
  322. * @const {!shaka.util.Lazy.<!Uint8Array>}
  323. * @private
  324. * @see https://github.com/shaka-project/shaka-player/issues/2759
  325. */
  326. shaka.media.ContentWorkarounds.CANNED_SINF_BOX_ =
  327. new shaka.util.Lazy(() => new Uint8Array([
  328. // sinf box
  329. // Size: 0x50 = 80
  330. 0x00, 0x00, 0x00, 0x50,
  331. // Type: sinf
  332. 0x73, 0x69, 0x6e, 0x66,
  333. // Children of sinf...
  334. // frma box
  335. // Size: 0x0c = 12
  336. 0x00, 0x00, 0x00, 0x0c,
  337. // Type: frma (child of sinf)
  338. 0x66, 0x72, 0x6d, 0x61,
  339. // Format: filled in later based on the source box ("avc1", "mp4a", etc)
  340. 0x00, 0x00, 0x00, 0x00,
  341. // end of frma box
  342. // schm box
  343. // Size: 0x14 = 20
  344. 0x00, 0x00, 0x00, 0x14,
  345. // Type: schm (child of sinf)
  346. 0x73, 0x63, 0x68, 0x6d,
  347. // Version: 0, Flags: 0
  348. 0x00, 0x00, 0x00, 0x00,
  349. // Scheme: cenc
  350. 0x63, 0x65, 0x6e, 0x63,
  351. // Scheme version: 1.0
  352. 0x00, 0x01, 0x00, 0x00,
  353. // end of schm box
  354. // schi box
  355. // Size: 0x28 = 40
  356. 0x00, 0x00, 0x00, 0x28,
  357. // Type: schi (child of sinf)
  358. 0x73, 0x63, 0x68, 0x69,
  359. // Children of schi...
  360. // tenc box
  361. // Size: 0x20 = 32
  362. 0x00, 0x00, 0x00, 0x20,
  363. // Type: tenc (child of schi)
  364. 0x74, 0x65, 0x6e, 0x63,
  365. // Version: 0, Flags: 0
  366. 0x00, 0x00, 0x00, 0x00,
  367. // Reserved fields
  368. 0x00, 0x00,
  369. // Default protected: true
  370. 0x01,
  371. // Default per-sample IV size: 8
  372. 0x08,
  373. // Default key ID: all zeros (dummy)
  374. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  375. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  376. // end of tenc box
  377. // end of schi box
  378. // end of sinf box
  379. ]));
  380. /**
  381. * The location of the format field in the "frma" box inside the canned "sinf"
  382. * box above.
  383. *
  384. * @const {number}
  385. * @private
  386. */
  387. shaka.media.ContentWorkarounds.CANNED_SINF_BOX_FORMAT_OFFSET_ = 0x10;
  388. /**
  389. * Offset to a box's size field.
  390. *
  391. * @const {number}
  392. * @private
  393. */
  394. shaka.media.ContentWorkarounds.BOX_SIZE_OFFSET_ = 0;
  395. /**
  396. * Offset to a box's type field.
  397. *
  398. * @const {number}
  399. * @private
  400. */
  401. shaka.media.ContentWorkarounds.BOX_TYPE_OFFSET_ = 4;
  402. /**
  403. * Offset to a box's 64-bit size field, if it has one.
  404. *
  405. * @const {number}
  406. * @private
  407. */
  408. shaka.media.ContentWorkarounds.BOX_SIZE_64_OFFSET_ = 8;
  409. /**
  410. * Box type for "encv".
  411. *
  412. * @const {number}
  413. * @private
  414. */
  415. shaka.media.ContentWorkarounds.BOX_TYPE_ENCV_ = 0x656e6376;
  416. /**
  417. * Box type for "enca".
  418. *
  419. * @const {number}
  420. * @private
  421. */
  422. shaka.media.ContentWorkarounds.BOX_TYPE_ENCA_ = 0x656e6361;