From 887f2e614de7f75ada3f1cf6ba3af1db17e86e60 Mon Sep 17 00:00:00 2001 From: Jacob Trimble Date: Tue, 20 Aug 2019 14:24:24 -0700 Subject: [PATCH] Make Uint8ArrayUtils accept BufferSource. Change-Id: Ia22b4535f4a6cbc156022299d732b27602c677d3 --- lib/media/drm_engine.js | 3 +- lib/polyfill/patchedmediakeys_webkit.js | 9 ++--- lib/util/uint8array_utils.js | 35 ++++++++++--------- .../dash_parser_content_protection_unit.js | 3 +- test/test/util/canned_idb.js | 2 +- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/lib/media/drm_engine.js b/lib/media/drm_engine.js index 2626af7f8..0bf02d228 100644 --- a/lib/media/drm_engine.js +++ b/lib/media/drm_engine.js @@ -1314,8 +1314,7 @@ shaka.media.DrmEngine = class { hasExpiredKeys = true; } - const keyIdHex = shaka.util.Uint8ArrayUtils.toHex( - shaka.util.BufferUtils.toUint8(keyId)); + const keyIdHex = shaka.util.Uint8ArrayUtils.toHex(keyId); this.keyStatusByKeyId_.set(keyIdHex, status); }); diff --git a/lib/polyfill/patchedmediakeys_webkit.js b/lib/polyfill/patchedmediakeys_webkit.js index 45b69f2cf..090c9735f 100644 --- a/lib/polyfill/patchedmediakeys_webkit.js +++ b/lib/polyfill/patchedmediakeys_webkit.js @@ -680,13 +680,8 @@ shaka.polyfill.PatchedMediaKeysWebkit.MediaKeySession = // Persisting the initial license. // Prefix the init data with a tag to indicate persistence. const prefix = StringUtils.toUTF8('PERSISTENT|'); - const result = - new Uint8Array(prefix.byteLength + initData.byteLength); - result.set(shaka.util.BufferUtils.toUint8(prefix), 0); - result.set( - shaka.util.BufferUtils.toUint8(initData), - prefix.byteLength); - mangledInitData = result; + mangledInitData = + shaka.util.Uint8ArrayUtils.concat(prefix, initData); } else { // Loading a stored license. // Prefix the init data (which is really a session ID) with a tag diff --git a/lib/util/uint8array_utils.js b/lib/util/uint8array_utils.js index 676d24cdd..320623706 100644 --- a/lib/util/uint8array_utils.js +++ b/lib/util/uint8array_utils.js @@ -17,6 +17,7 @@ goog.provide('shaka.util.Uint8ArrayUtils'); +goog.require('shaka.util.BufferUtils'); goog.require('shaka.util.Iterables'); goog.require('shaka.util.StringUtils'); @@ -27,29 +28,30 @@ goog.require('shaka.util.StringUtils'); */ shaka.util.Uint8ArrayUtils = class { /** - * Convert a Uint8Array to a base64 string. The output will be standard + * Convert a buffer to a base64 string. The output will be standard * alphabet as opposed to base64url safe alphabet. - * @param {!Uint8Array} u8Arr + * @param {BufferSource} data * @return {string} * @export */ - static toStandardBase64(u8Arr) { - const bytes = shaka.util.StringUtils.fromCharCode(u8Arr); + static toStandardBase64(data) { + const bytes = shaka.util.StringUtils.fromCharCode( + shaka.util.BufferUtils.toUint8(data)); return btoa(bytes); } /** - * Convert a Uint8Array to a base64 string. The output will always use the + * Convert a buffer to a base64 string. The output will always use the * alternate encoding/alphabet also known as "base64url". - * @param {!Uint8Array} arr + * @param {BufferSource} data * @param {boolean=} padding If true, pad the output with equals signs. * Defaults to true. * @return {string} * @export */ - static toBase64(arr, padding) { + static toBase64(data, padding) { padding = (padding == undefined) ? true : padding; - const base64 = shaka.util.Uint8ArrayUtils.toStandardBase64(arr) + const base64 = shaka.util.Uint8ArrayUtils.toStandardBase64(data) .replace(/\+/g, '-').replace(/\//g, '_'); return padding ? base64 : base64.replace(/[=]*$/, ''); } @@ -91,12 +93,13 @@ shaka.util.Uint8ArrayUtils = class { /** - * Convert a Uint8Array to a hex string. - * @param {!Uint8Array} arr + * Convert a buffer to a hex string. + * @param {BufferSource} data * @return {string} * @export */ - static toHex(arr) { + static toHex(data) { + const arr = shaka.util.BufferUtils.toUint8(data); let hex = ''; for (let value of arr) { value = value.toString(16); @@ -110,22 +113,22 @@ shaka.util.Uint8ArrayUtils = class { /** - * Concatenate Uint8Arrays. - * @param {...!Uint8Array} varArgs + * Concatenate buffers. + * @param {...BufferSource} varArgs * @return {!Uint8Array} * @export */ static concat(...varArgs) { let totalLength = 0; for (const arr of varArgs) { - totalLength += arr.length; + totalLength += arr.byteLength; } const result = new Uint8Array(totalLength); let offset = 0; for (const arr of varArgs) { - result.set(arr, offset); - offset += arr.length; + result.set(shaka.util.BufferUtils.toUint8(arr), offset); + offset += arr.byteLength; } return result; } diff --git a/test/dash/dash_parser_content_protection_unit.js b/test/dash/dash_parser_content_protection_unit.js index a50136260..0c45c3cc7 100644 --- a/test/dash/dash_parser_content_protection_unit.js +++ b/test/dash/dash_parser_content_protection_unit.js @@ -812,8 +812,7 @@ describe('In-manifest PlayReady and Widevine', () => { // value ].concat(laurlCodes)); - const encodedPrObject = - shaka.util.Uint8ArrayUtils.toBase64(new Uint8Array(prBytes.buffer)); + const encodedPrObject = shaka.util.Uint8ArrayUtils.toBase64(prBytes); const input = { init: null, keyId: null, diff --git a/test/test/util/canned_idb.js b/test/test/util/canned_idb.js index 11f07cabf..101e6d5c4 100644 --- a/test/test/util/canned_idb.js +++ b/test/test/util/canned_idb.js @@ -136,7 +136,7 @@ shaka.test.CannedIDB = class { if (dummyArrayBuffers) { data = ''; } else { - data = shaka.util.Uint8ArrayUtils.toBase64(new Uint8Array(value)); + data = shaka.util.Uint8ArrayUtils.toBase64(value); } return { __type__: 'ArrayBuffer',