@@ -7,6 +7,8 @@ import type { DecryptData } from '../loader/level-key';
77import type { PassthroughTrack , UserdataSample } from '../types/demuxer' ;
88import type { ILogger } from '../utils/logger' ;
99
10+ type BoxDataOrUndefined = Uint8Array < ArrayBuffer > | undefined ;
11+
1012const UINT32_MAX = Math . pow ( 2 , 32 ) - 1 ;
1113const push = [ ] . push ;
1214
@@ -573,51 +575,74 @@ export function patchEncyptionData(
573575 }
574576 const keyId = decryptdata . keyId ;
575577 if ( keyId && decryptdata . isCommonEncryption ) {
576- const traks = findBox ( initSegment , [ 'moov' , 'trak' ] ) ;
577- traks . forEach ( ( trak ) => {
578- const stsd = findBox ( trak , [ 'mdia' , 'minf' , 'stbl' , 'stsd' ] ) [ 0 ] ;
579-
580- // skip the sample entry count
581- const sampleEntries = stsd . subarray ( 8 ) ;
582- let encBoxes = findBox ( sampleEntries , [ 'enca' ] ) ;
583- const isAudio = encBoxes . length > 0 ;
584- if ( ! isAudio ) {
585- encBoxes = findBox ( sampleEntries , [ 'encv' ] ) ;
578+ applyToTencBoxes ( initSegment , ( tenc , isAudio ) => {
579+ // Look for default key id (keyID offset is always 8 within the tenc box):
580+ const tencKeyId = tenc . subarray ( 8 , 24 ) ;
581+ if ( ! tencKeyId . some ( ( b ) => b !== 0 ) ) {
582+ logger . log (
583+ `[eme] Patching keyId in 'enc ${
584+ isAudio ? 'a' : 'v'
585+ } >sinf>>tenc' box: ${ arrayToHex ( tencKeyId ) } -> ${ arrayToHex ( keyId ) } ` ,
586+ ) ;
587+ tenc . set ( keyId , 8 ) ;
586588 }
587- encBoxes . forEach ( ( enc ) => {
588- const encBoxChildren = isAudio ? enc . subarray ( 28 ) : enc . subarray ( 78 ) ;
589- const sinfBoxes = findBox ( encBoxChildren , [ 'sinf' ] ) ;
590- sinfBoxes . forEach ( ( sinf ) => {
591- const tenc = parseSinf ( sinf ) ;
592- if ( tenc ) {
593- // Look for default key id (keyID offset is always 8 within the tenc box):
594- const tencKeyId = tenc . subarray ( 8 , 24 ) as Uint8Array < ArrayBuffer > ;
595- if ( ! tencKeyId . some ( ( b ) => b !== 0 ) ) {
596- logger . log (
597- `[eme] Patching keyId in 'enc${
598- isAudio ? 'a' : 'v'
599- } >sinf>>tenc' box: ${ arrayToHex ( tencKeyId ) } -> ${ arrayToHex (
600- keyId ,
601- ) } `,
602- ) ;
603- tenc . set ( keyId , 8 ) ;
604- }
605- }
606- } ) ;
607- } ) ;
608589 } ) ;
609590 }
610591}
611592
612- export function parseSinf ( sinf : Uint8Array ) : Uint8Array | null {
613- const schm = findBox ( sinf , [ 'schm' ] ) [ 0 ] ;
614- if ( schm as any ) {
593+ export function parseKeyIdsFromTenc (
594+ initSegment : Uint8Array < ArrayBuffer > ,
595+ ) : Uint8Array < ArrayBuffer > [ ] {
596+ const keyIds : Uint8Array < ArrayBuffer > [ ] = [ ] ;
597+ applyToTencBoxes ( initSegment , ( tenc ) => keyIds . push ( tenc . subarray ( 8 , 24 ) ) ) ;
598+ return keyIds ;
599+ }
600+
601+ function applyToTencBoxes (
602+ initSegment : Uint8Array < ArrayBuffer > ,
603+ predicate : ( tenc : Uint8Array < ArrayBuffer > , isAudio : boolean ) => void ,
604+ ) {
605+ const traks = findBox ( initSegment , [ 'moov' , 'trak' ] ) ;
606+ traks . forEach ( ( trak ) => {
607+ const stsd = findBox ( trak , [
608+ 'mdia' ,
609+ 'minf' ,
610+ 'stbl' ,
611+ 'stsd' ,
612+ ] ) [ 0 ] as BoxDataOrUndefined ;
613+ if ( ! stsd ) return ;
614+ const sampleEntries = stsd . subarray ( 8 ) ;
615+ let encBoxes = findBox ( sampleEntries , [ 'enca' ] ) ;
616+ const isAudio = encBoxes . length > 0 ;
617+ if ( ! isAudio ) {
618+ encBoxes = findBox ( sampleEntries , [ 'encv' ] ) ;
619+ }
620+ encBoxes . forEach ( ( enc ) => {
621+ const encBoxChildren = isAudio ? enc . subarray ( 28 ) : enc . subarray ( 78 ) ;
622+ const sinfBoxes = findBox ( encBoxChildren , [ 'sinf' ] ) ;
623+ sinfBoxes . forEach ( ( sinf ) => {
624+ const tenc = parseSinf ( sinf ) ;
625+ if ( tenc ) {
626+ predicate ( tenc , isAudio ) ;
627+ }
628+ } ) ;
629+ } ) ;
630+ } ) ;
631+ }
632+
633+ export function parseSinf ( sinf : Uint8Array ) : BoxDataOrUndefined {
634+ const schm = findBox ( sinf , [ 'schm' ] ) [ 0 ] as BoxDataOrUndefined ;
635+ if ( schm ) {
615636 const scheme = bin2str ( schm . subarray ( 4 , 8 ) ) ;
616637 if ( scheme === 'cbcs' || scheme === 'cenc' ) {
617- return findBox ( sinf , [ 'schi' , 'tenc' ] ) [ 0 ] ;
638+ const tenc = findBox ( sinf , [ 'schi' , 'tenc' ] ) [ 0 ] as BoxDataOrUndefined ;
639+ if ( tenc ) {
640+ return tenc ;
641+ }
642+ } else if ( scheme === 'cbc2' ) {
643+ /* no-op */
618644 }
619645 }
620- return null ;
621646}
622647
623648/*
0 commit comments