diff --git a/src/controller/eme-controller.ts b/src/controller/eme-controller.ts index e09f0216c4f..5c906835b32 100644 --- a/src/controller/eme-controller.ts +++ b/src/controller/eme-controller.ts @@ -35,8 +35,8 @@ import { requestMediaKeySystemAccess, } from '../utils/mediakeys-helper'; import { KeySystemFormats } from '../utils/mediakeys-helper'; -import { bin2str, parseMultiPssh, parseSinf } from '../utils/mp4-tools'; -import { base64Decode } from '../utils/numeric-encoding-utils'; +import { parseMultiPssh, parseSinf } from '../utils/mp4-tools'; +import { base64Decode, bin2str } from '../utils/numeric-encoding-utils'; import { stringify } from '../utils/safe-json-stringify'; import { strToUtf8array } from '../utils/utf8-utils'; import type { EMEControllerConfig, HlsConfig, LoadPolicy } from '../config'; @@ -1364,10 +1364,7 @@ class EMEController extends Logger implements ComponentAPI { // actual license request) and any HttpHeader elements (sent as request // headers). // For PlayReady CDMs, we need to dig the Challenge out of the XML. - const xmlString = String.fromCharCode.apply( - null, - new Uint16Array(licenseChallenge.buffer), - ); + const xmlString = bin2str(new Uint16Array(licenseChallenge.buffer)); // eslint-disable-next-line no-restricted-syntax if (!xmlString.includes('PlayReadyKeyMessage')) { // This does not appear to be a wrapped message as on Edge. Some diff --git a/src/controller/fragment-finders.ts b/src/controller/fragment-finders.ts index 73cc6418d86..84fae582f95 100644 --- a/src/controller/fragment-finders.ts +++ b/src/controller/fragment-finders.ts @@ -205,21 +205,6 @@ export function pdtWithinToleranceTest( return endProgramDateTime - candidateLookupTolerance > pdtBufferEnd; } -export function findFragWithCC( - fragments: MediaFragment[], - cc: number, -): MediaFragment | null { - return BinarySearch.search(fragments, (candidate) => { - if (candidate.cc < cc) { - return 1; - } else if (candidate.cc > cc) { - return -1; - } else { - return 0; - } - }); -} - export function findNearestWithCC( details: LevelDetails | undefined, cc: number, diff --git a/src/demux/video/exp-golomb.ts b/src/demux/video/exp-golomb.ts index 57d38c0eb31..7d229afa087 100644 --- a/src/demux/video/exp-golomb.ts +++ b/src/demux/video/exp-golomb.ts @@ -138,16 +138,6 @@ class ExpGolomb { readUByte(): number { return this.readBits(8); } - - // ():int - readUShort(): number { - return this.readBits(16); - } - - // ():int - readUInt(): number { - return this.readBits(32); - } } export default ExpGolomb; diff --git a/src/demux/video/hevc-video-parser.ts b/src/demux/video/hevc-video-parser.ts index 16528f24fac..b547cf2086b 100644 --- a/src/demux/video/hevc-video-parser.ts +++ b/src/demux/video/hevc-video-parser.ts @@ -1,6 +1,7 @@ import BaseVideoParser from './base-video-parser'; import ExpGolomb from './exp-golomb'; import { parseSEIMessageFromNALu } from '../../utils/mp4-tools'; +import { bin2str } from '../../utils/numeric-encoding-utils'; import type { DemuxedUserdataTrack, DemuxedVideoTrack, @@ -764,10 +765,7 @@ class HevcVideoParser extends BaseVideoParser { matchSPS(sps1: Uint8Array, sps2: Uint8Array): boolean { // compare without headers and VPS related params - return ( - String.fromCharCode.apply(null, sps1).substr(3) === - String.fromCharCode.apply(null, sps2).substr(3) - ); + return bin2str(sps1).substring(3) === bin2str(sps2).substring(3); } } diff --git a/src/utils/chunker.ts b/src/utils/chunker.ts deleted file mode 100644 index 4182faafab8..00000000000 --- a/src/utils/chunker.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { appendUint8Array } from './mp4-tools'; - -export default class Chunker { - private chunkSize: number; - public cache: Uint8Array | null = null; - constructor(chunkSize = Math.pow(2, 19)) { - this.chunkSize = chunkSize; - } - - public push(data: Uint8Array): Array { - const { cache, chunkSize } = this; - const result: Array = []; - - let temp: Uint8Array | null = null; - if (cache?.length) { - temp = appendUint8Array(cache, data); - this.cache = null; - } else { - temp = data; - } - - if (temp.length < chunkSize) { - this.cache = temp; - return result; - } - - if (temp.length > chunkSize) { - let offset = 0; - const len = temp.length; - while (offset < len - chunkSize) { - result.push(temp.slice(offset, offset + chunkSize)); - offset += chunkSize; - } - this.cache = temp.slice(offset); - } else { - result.push(temp); - } - - return result; - } -} diff --git a/src/utils/hex.ts b/src/utils/hex.ts index 2bc66b55132..79f7286ade8 100644 --- a/src/utils/hex.ts +++ b/src/utils/hex.ts @@ -24,9 +24,3 @@ export function hexToArrayBuffer(str: string): ArrayBuffer { .split(' '), ).buffer; } - -const Hex = { - hexDump: arrayToHex, -}; - -export default Hex; diff --git a/src/utils/level-helper.ts b/src/utils/level-helper.ts index 096fd76c8d9..7c7399e73c8 100644 --- a/src/utils/level-helper.ts +++ b/src/utils/level-helper.ts @@ -19,16 +19,6 @@ type FragmentIntersection = ( ) => void; type PartIntersection = (oldPart: Part, newPart: Part) => void; -export function updatePTS( - fragments: MediaFragment[], - fromIdx: number, - toIdx: number, -): void { - const fragFrom = fragments[fromIdx]; - const fragTo = fragments[toIdx]; - updateFromToPTS(fragFrom, fragTo); -} - function updateFromToPTS(fragFrom: MediaFragment, fragTo: MediaFragment) { const fragToPTS = fragTo.startPTS as number; // if we know startPTS[toIdx] diff --git a/src/utils/mediakeys-helper.ts b/src/utils/mediakeys-helper.ts index 5d9f5e2f64e..d64ecf9e6c2 100755 --- a/src/utils/mediakeys-helper.ts +++ b/src/utils/mediakeys-helper.ts @@ -1,6 +1,6 @@ import { optionalSelf } from './global'; import { changeEndianness } from './keysystem-util'; -import { base64Decode } from './numeric-encoding-utils'; +import { base64Decode, bin2str } from './numeric-encoding-utils'; import type { DRMSystemOptions, EMEControllerConfig } from '../config'; /** @@ -184,7 +184,7 @@ export function parsePlayReadyWRM(keyBytes: Uint8Array) { keyBytes.byteOffset, keyBytes.byteLength / 2, ); - const keyByteStr = String.fromCharCode.apply(null, Array.from(keyBytesUtf16)); + const keyByteStr = bin2str(keyBytesUtf16); // Parse Playready WRMHeader XML const xmlKeyBytes = keyByteStr.substring( diff --git a/src/utils/mp4-tools.ts b/src/utils/mp4-tools.ts index b871c47b20b..3de9c2dc8df 100644 --- a/src/utils/mp4-tools.ts +++ b/src/utils/mp4-tools.ts @@ -1,5 +1,6 @@ import { utf8ArrayToStr } from '@svta/cml-id3'; import { arrayToHex } from './hex'; +import { bin2str } from './numeric-encoding-utils'; import { ElementaryStreamTypes } from '../loader/fragment'; import { logger } from '../utils/logger'; import type { ChunkMetadata } from '../hls'; @@ -72,28 +73,24 @@ export const RemuxerTrackIdConfig = { text: 4, }; -export function bin2str(data: Uint8Array): string { - return String.fromCharCode.apply(null, data); -} - -export function readUint16(buffer: Uint8Array, offset: number): number { +function readUint16(buffer: Uint8Array, offset: number): number { const val = (buffer[offset] << 8) | buffer[offset + 1]; return val < 0 ? 65536 + val : val; } -export function readUint32(buffer: Uint8Array, offset: number): number { +function readUint32(buffer: Uint8Array, offset: number): number { const val = readSint32(buffer, offset); return val < 0 ? 4294967296 + val : val; } -export function readUint64(buffer: Uint8Array, offset: number) { +function readUint64(buffer: Uint8Array, offset: number) { let result = readUint32(buffer, offset); result *= Math.pow(2, 32); result += readUint32(buffer, offset + 4); return result; } -export function readSint32(buffer: Uint8Array, offset: number): number { +function readSint32(buffer: Uint8Array, offset: number): number { return ( (buffer[offset] << 24) | (buffer[offset + 1] << 16) | diff --git a/src/utils/numeric-encoding-utils.ts b/src/utils/numeric-encoding-utils.ts index c87eb4d31b5..368d9b559a7 100644 --- a/src/utils/numeric-encoding-utils.ts +++ b/src/utils/numeric-encoding-utils.ts @@ -1,26 +1,7 @@ -export function base64ToBase64Url(base64encodedStr: string): string { - return base64encodedStr - .replace(/\+/g, '-') - .replace(/\//g, '_') - .replace(/=+$/, ''); -} - -export function strToBase64Encode(str: string): string { - return btoa(str); -} - -export function base64DecodeToStr(str: string): string { - return atob(str); -} - -export function base64Encode(input: Uint8Array): string { - return btoa(String.fromCharCode(...input)); -} - -export function base64UrlEncode(input: Uint8Array): string { - return base64ToBase64Url(base64Encode(input)); -} - export function base64Decode(base64encodedStr: string) { return Uint8Array.from(atob(base64encodedStr), (c) => c.charCodeAt(0)); } + +export function bin2str(data: number[] | Uint8Array | Uint16Array): string { + return String.fromCharCode.apply(null, data); +}