@@ -98,14 +98,24 @@ class TSDemuxer implements Demuxer {
98
98
return syncOffset !== - 1 ;
99
99
}
100
100
101
- static syncOffset ( data : Uint8Array ) {
101
+ static syncOffset ( data : Uint8Array ) : number {
102
102
const scanwindow =
103
- Math . min ( PACKET_LENGTH * 5 , data . length - PACKET_LENGTH * 2 ) + 1 ;
103
+ Math . min ( PACKET_LENGTH * 5 , data . length - PACKET_LENGTH ) + 1 ;
104
104
let i = 0 ;
105
105
while ( i < scanwindow ) {
106
106
// a TS init segment should contain at least 2 TS packets: PAT and PMT, each starting with 0x47
107
- if ( data [ i ] === 0x47 && data [ i + PACKET_LENGTH ] === 0x47 ) {
108
- return i ;
107
+ let foundPat = false ;
108
+ for ( let j = 0 ; j < scanwindow ; j += PACKET_LENGTH ) {
109
+ if ( data [ j ] === 0x47 ) {
110
+ if ( ! foundPat && parsePID ( data , j ) === 0 ) {
111
+ foundPat = true ;
112
+ }
113
+ if ( foundPat && j + PACKET_LENGTH > scanwindow ) {
114
+ return i ;
115
+ }
116
+ } else {
117
+ break ;
118
+ }
109
119
}
110
120
i ++ ;
111
121
}
@@ -245,8 +255,7 @@ class TSDemuxer implements Demuxer {
245
255
for ( let start = syncOffset ; start < len ; start += PACKET_LENGTH ) {
246
256
if ( data [ start ] === 0x47 ) {
247
257
const stt = ! ! ( data [ start + 1 ] & 0x40 ) ;
248
- // pid is a 13-bit field starting at the last bit of TS[1]
249
- const pid = ( ( data [ start + 1 ] & 0x1f ) << 8 ) + data [ start + 2 ] ;
258
+ const pid = parsePID ( data , start ) ;
250
259
const atf = ( data [ start + 3 ] & 0x30 ) >> 4 ;
251
260
252
261
// if an adaption field is present, its length is specified by the fifth byte of the TS packet header.
@@ -312,6 +321,7 @@ class TSDemuxer implements Demuxer {
312
321
}
313
322
314
323
pmtId = this . _pmtId = parsePAT ( data , offset ) ;
324
+ // logger.log('PMT PID:' + this._pmtId);
315
325
break ;
316
326
case pmtId : {
317
327
if ( stt ) {
@@ -355,7 +365,7 @@ class TSDemuxer implements Demuxer {
355
365
pmtParsed = this . pmtParsed = true ;
356
366
break ;
357
367
}
358
- case 17 :
368
+ case 0x11 :
359
369
case 0x1fff :
360
370
break ;
361
371
default :
@@ -988,13 +998,22 @@ function createAVCSample(
988
998
} ;
989
999
}
990
1000
991
- function parsePAT ( data , offset ) {
1001
+ function parsePID ( data : Uint8Array , offset : number ) : number {
1002
+ // pid is a 13-bit field starting at the last bit of TS[1]
1003
+ return ( ( data [ offset + 1 ] & 0x1f ) << 8 ) + data [ offset + 2 ] ;
1004
+ }
1005
+
1006
+ function parsePAT ( data : Uint8Array , offset : number ) : number {
992
1007
// skip the PSI header and parse the first PMT entry
993
1008
return ( ( data [ offset + 10 ] & 0x1f ) << 8 ) | data [ offset + 11 ] ;
994
- // logger.log('PMT PID:' + this._pmtId);
995
1009
}
996
1010
997
- function parsePMT ( data , offset , typeSupported , isSampleAes ) {
1011
+ function parsePMT (
1012
+ data : Uint8Array ,
1013
+ offset : number ,
1014
+ typeSupported : TypeSupported ,
1015
+ isSampleAes : boolean
1016
+ ) {
998
1017
const result = { audio : - 1 , avc : - 1 , id3 : - 1 , segmentCodec : 'aac' } ;
999
1018
const sectionLength = ( ( data [ offset + 1 ] & 0x0f ) << 8 ) | data [ offset + 2 ] ;
1000
1019
const tableEnd = offset + 3 + sectionLength - 4 ;
@@ -1005,7 +1024,7 @@ function parsePMT(data, offset, typeSupported, isSampleAes) {
1005
1024
// advance the offset to the first entry in the mapping table
1006
1025
offset += 12 + programInfoLength ;
1007
1026
while ( offset < tableEnd ) {
1008
- const pid = ( ( data [ offset + 1 ] & 0x1f ) << 8 ) | data [ offset + 2 ] ;
1027
+ const pid = parsePID ( data , offset ) ;
1009
1028
switch ( data [ offset ] ) {
1010
1029
case 0xcf : // SAMPLE-AES AAC
1011
1030
if ( ! isSampleAes ) {
0 commit comments