2
2
* highly optimized TS demuxer:
3
3
* parse PAT, PMT
4
4
* extract PES packet from audio and video PIDs
5
- * extract AVC/H264 NAL units and AAC/ADTS samples from PES packet
5
+ * extract AVC/H264 (or HEVC/H265) NAL units and AAC/ADTS samples from PES packet
6
6
* trigger the remuxer upon parsing completion
7
7
* it also tries to workaround as best as it can audio codec switch (HE-AAC to AAC and vice versa), without having to restart the MediaSource.
8
8
* it also controls the remuxing process :
@@ -14,6 +14,7 @@ import * as MpegAudio from './audio/mpegaudio';
14
14
import * as AC3 from './audio/ac3-demuxer' ;
15
15
import BaseVideoParser from './video/base-video-parser' ;
16
16
import AvcVideoParser from './video/avc-video-parser' ;
17
+ import HevcVideoParser from './video/hevc-video-parser' ;
17
18
import SampleAesDecrypter from './sample-aes' ;
18
19
import { Events } from '../events' ;
19
20
import { appendUint8Array , RemuxerTrackIdConfig } from '../utils/mp4-tools' ;
@@ -70,7 +71,7 @@ class TSDemuxer implements Demuxer {
70
71
private _txtTrack ?: DemuxedUserdataTrack ;
71
72
private aacOverFlow : AudioFrame | null = null ;
72
73
private remainderData : Uint8Array | null = null ;
73
- private videoParser : BaseVideoParser ;
74
+ private videoParser : BaseVideoParser | null ;
74
75
75
76
constructor (
76
77
observer : HlsEventEmitter ,
@@ -80,7 +81,7 @@ class TSDemuxer implements Demuxer {
80
81
this . observer = observer ;
81
82
this . config = config ;
82
83
this . typeSupported = typeSupported ;
83
- this . videoParser = new AvcVideoParser ( ) ;
84
+ this . videoParser = null ;
84
85
}
85
86
86
87
static probe ( data : Uint8Array ) {
@@ -288,13 +289,25 @@ class TSDemuxer implements Demuxer {
288
289
case videoPid :
289
290
if ( stt ) {
290
291
if ( videoData && ( pes = parsePES ( videoData ) ) ) {
291
- this . videoParser . parsePES (
292
- videoTrack ,
293
- textTrack ,
294
- pes ,
295
- false ,
296
- this . _duration ,
297
- ) ;
292
+ if ( this . videoParser === null ) {
293
+ switch ( videoTrack . segmentCodec ) {
294
+ case 'avc' :
295
+ this . videoParser = new AvcVideoParser ( ) ;
296
+ break ;
297
+ case 'hevc' :
298
+ this . videoParser = new HevcVideoParser ( ) ;
299
+ break ;
300
+ }
301
+ }
302
+ if ( this . videoParser !== null ) {
303
+ this . videoParser . parsePES (
304
+ videoTrack ,
305
+ textTrack ,
306
+ pes ,
307
+ false ,
308
+ this . _duration ,
309
+ ) ;
310
+ }
298
311
}
299
312
300
313
videoData = { data : [ ] , size : 0 } ;
@@ -466,14 +479,26 @@ class TSDemuxer implements Demuxer {
466
479
// try to parse last PES packets
467
480
let pes : PES | null ;
468
481
if ( videoData && ( pes = parsePES ( videoData ) ) ) {
469
- this . videoParser . parsePES (
470
- videoTrack as DemuxedVideoTrack ,
471
- textTrack as DemuxedUserdataTrack ,
472
- pes ,
473
- true ,
474
- this . _duration ,
475
- ) ;
476
- videoTrack . pesData = null ;
482
+ if ( this . videoParser === null ) {
483
+ switch ( videoTrack . segmentCodec ) {
484
+ case 'avc' :
485
+ this . videoParser = new AvcVideoParser ( ) ;
486
+ break ;
487
+ case 'hevc' :
488
+ this . videoParser = new HevcVideoParser ( ) ;
489
+ break ;
490
+ }
491
+ }
492
+ if ( this . videoParser !== null ) {
493
+ this . videoParser . parsePES (
494
+ videoTrack as DemuxedVideoTrack ,
495
+ textTrack as DemuxedUserdataTrack ,
496
+ pes ,
497
+ true ,
498
+ this . _duration ,
499
+ ) ;
500
+ videoTrack . pesData = null ;
501
+ }
477
502
} else {
478
503
// either avcData null or PES truncated, keep it for next frag parsing
479
504
videoTrack . pesData = videoData ;
@@ -870,8 +895,13 @@ function parsePMT(
870
895
case 0x87 :
871
896
logger . warn ( 'Unsupported EC-3 in M2TS found' ) ;
872
897
break ;
873
- case 0x24 :
874
- logger . warn ( 'Unsupported HEVC in M2TS found' ) ;
898
+
899
+ case 0x24 : // ITU-T Rec. H.265 and ISO/IEC 23008-2 (HEVC)
900
+ if ( result . videoPid === - 1 ) {
901
+ result . videoPid = pid ;
902
+ result . segmentVideoCodec = 'hevc' ;
903
+ logger . log ( 'HEVC in M2TS found' ) ;
904
+ }
875
905
break ;
876
906
877
907
default :
0 commit comments