Skip to content

Commit

Permalink
fix to hex string
Browse files Browse the repository at this point in the history
  • Loading branch information
adrums86 committed Dec 12, 2023
1 parent f0cbf43 commit f4dca90
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import logger from './util/logger';
import {merge, createTimeRanges} from './util/vjs-compat';
import { addMetadata, createMetadataTrackIfNotExists, addDateRangeMetadata } from './util/text-tracks';
import ContentSteeringController from './content-steering-controller';
import { bufferToHexString } from './util/string.js';

const ABORT_EARLY_EXCLUSION_SECONDS = 10;

Expand Down Expand Up @@ -2421,14 +2422,14 @@ export class PlaylistController extends videojs.EventTarget {
}

/**
* Adds a keystatus to the keystatus map, tries to convert to string with a TextDecoder if necessary.
* Adds a keystatus to the keystatus map, tries to convert to string if necessary.
*
* @param {any} keyId the keyId to add a status for
* @param {string} status the status of the keyId
*/
addKeyStatus_(keyId, status) {
const isString = typeof keyId === 'string';
const keyIdHexString = isString ? keyId : new TextDecoder().decode(keyId);
const keyIdHexString = isString ? keyId : bufferToHexString(keyId);

// 32 digit keyId hex string.
this.keyStatusMap_.set(keyIdHexString.slice(0, 32), status);
Expand Down
5 changes: 5 additions & 0 deletions src/util/string.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export const uint8ToUtf8 = (uintArray) =>
decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));

export const bufferToHexString = (buffer) => {
const uInt8Buffer = new Uint8Array(buffer);

return Array.from(uInt8Buffer).map((byte) => byte.toString(16).padStart(2, '0')).join('');
};
11 changes: 6 additions & 5 deletions test/playlist-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4866,21 +4866,21 @@ QUnit.test('can pass or select a playlist for fastQualityChange', function(asser

QUnit.test('addKeyStatus_ adds keyId and status to the Map', function(assert) {
// string keyId
const keyIdEncoded = new TextEncoder().encode('a6fcfb2a857c4227adb5a5f51aa27632');
const keyIdString = 'a6fcfb2a857c4227adb5a5f51aa27632';
const status = 'usable';
const pc = this.playlistController;
let excludeNonUsablePlaylistsByKeyIdCalled = 0;

pc.addKeyStatus_ = (id, st) => {
assert.equal(id, keyIdEncoded, 'addKeyStatus_ called with expected keyId');
assert.equal(id, keyIdString, 'addKeyStatus_ called with expected keyId');
assert.equal(st, status, 'addKeyStatus_ called with expected status');
};

pc.excludeNonUsablePlaylistsByKeyId_ = () => {
excludeNonUsablePlaylistsByKeyIdCalled++;
};

pc.updatePlaylistByKeyStatus(keyIdEncoded, status);
pc.updatePlaylistByKeyStatus(keyIdString, status);
assert.equal(excludeNonUsablePlaylistsByKeyIdCalled, 1, 'excludeNonUsablePlaylistsByKeyIdCalled called once');
});

Expand All @@ -4896,8 +4896,9 @@ QUnit.test('addKeyStatus_ adds keyId and status to the Map', function(assert) {
assert.equal(pc.keyStatusMap_.get(keyId), status, 'keyId has expected status');

// test a non-string keyId
const keyId2 = 'd0bebaf8a3cb4c52bae03d20a71e3df3';
const keyIdArrayBuffer = new TextEncoder().encode(keyId2);
const keyId2 = '1f1574301da34f8bbcc82f4c2d1551b8';
const keyIdArrayBuffer = new Uint8Array([31, 21, 116, 48, 29, 163,
79, 139, 188, 200, 47, 76, 45, 21, 81, 184]).buffer;

pc.addKeyStatus_(keyIdArrayBuffer, 'usable');

Expand Down
2 changes: 1 addition & 1 deletion test/videojs-http-streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4913,7 +4913,7 @@ QUnit.test('eme handles keystatuschange where status is usable', function(assert

const excludes = [];
let updatePlaylistByKeyStatusCalled = 0;
const keyIdEncoded = new TextEncoder().encode('303E3FF1CAC36019B9265CBFF45C82F2');
const keyIdEncoded = '303E3FF1CAC36019B9265CBFF45C82F2';

this.player.tech_.vhs.playlistController_.updatePlaylistByKeyStatus = (keyId, status) => {
updatePlaylistByKeyStatusCalled++;
Expand Down

0 comments on commit f4dca90

Please sign in to comment.