/
tsdemuxer.js
839 lines (797 loc) · 30.6 KB
/
tsdemuxer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/**
* A stream-based mp2ts to mp4 converter. This utility is used to
* deliver mp4s to a SourceBuffer on platforms that support native
* Media Source Extensions.
*/
import Event from '../events';
import ExpGolomb from './exp-golomb';
// import Hex from '../utils/hex';
import MP4 from '../remux/mp4-generator';
import observer from '../observer';
import {logger} from '../utils/logger';
import {ErrorTypes,ErrorDetails} from '../errors';
class TSDemuxer {
constructor() {
this.lastCC = 0;
}
switchLevel() {
this.pmtParsed = false;
this._pmtId = this._avcId = this._aacId = -1;
this._avcTrack = {type : 'video', sequenceNumber : 0};
this._aacTrack = {type : 'audio', sequenceNumber : 0};
this._avcSamples = [];
this._avcSamplesLength = 0;
this._avcSamplesNbNalu = 0;
this._aacSamples = [];
this._aacSamplesLength = 0;
this._initSegGenerated = false;
}
insertDiscontinuity() {
this.switchLevel();
this._initPTS = this._initDTS = undefined;
}
// feed incoming data to the front of the parsing pipeline
push(data,audioCodec, videoCodec,timeOffset,cc,level,duration) {
var avcData,aacData,start,len = data.length,stt,pid,atf,offset;
this.audioCodec = audioCodec;
this.videoCodec = videoCodec;
this.timeOffset = timeOffset;
this._duration = duration;
if(cc !== this.lastCC) {
logger.log(`discontinuity detected`);
this.insertDiscontinuity();
this.lastCC = cc;
} else if(level !== this.lastLevel) {
logger.log(`level switch detected`);
this.switchLevel();
this.lastLevel = level;
}
var pmtParsed=this.pmtParsed,avcId=this._avcId,aacId=this._aacId;
// loop through TS packets
for(start = 0; start < len ; start += 188) {
if(data[start] === 0x47) {
stt = !!(data[start+1] & 0x40);
// pid is a 13-bit field starting at the last bit of TS[1]
pid = ((data[start+1] & 0x1f) << 8) + data[start+2];
atf = (data[start+3] & 0x30) >> 4;
// if an adaption field is present, its length is specified by the fifth byte of the TS packet header.
if(atf > 1) {
offset = start+5+data[start+4];
// continue if there is only adaptation field
if(offset === (start+188)) {
continue;
}
} else {
offset = start+4;
}
if(pmtParsed) {
if(pid === avcId) {
if(stt) {
if(avcData) {
this._parseAVCPES(this._parsePES(avcData));
}
avcData = {data: [],size: 0};
}
avcData.data.push(data.subarray(offset,start+188));
avcData.size+=start+188-offset;
} else if(pid === aacId) {
if(stt) {
if(aacData) {
this._parseAACPES(this._parsePES(aacData));
}
aacData = {data: [],size: 0};
}
aacData.data.push(data.subarray(offset,start+188));
aacData.size+=start+188-offset;
}
} else {
if(stt) {
offset += data[offset] + 1;
}
if(pid === 0) {
this._parsePAT(data,offset);
} else if(pid === this._pmtId) {
this._parsePMT(data,offset);
pmtParsed = this.pmtParsed = true;
avcId = this._avcId;
aacId = this._aacId;
}
}
} else {
observer.trigger(Event.ERROR, { type : ErrorTypes.MEDIA_ERROR, details : ErrorDetails.FRAG_PARSING_ERROR, fatal:false, reason : 'TS packet did not start with 0x47'});
}
}
// parse last PES packet
if(avcData) {
this._parseAVCPES(this._parsePES(avcData));
}
if(aacData) {
this._parseAACPES(this._parsePES(aacData));
}
}
end() {
//logger.log('nb AVC samples:' + this._avcSamples.length);
if(this._avcSamples.length) {
this._flushAVCSamples();
}
//logger.log('nb AAC samples:' + this._aacSamples.length);
if(this._aacSamples.length) {
this._flushAACSamples();
}
//notify end of parsing
observer.trigger(Event.FRAG_PARSED);
}
destroy() {
this.switchLevel();
this._initPTS = this._initDTS = undefined;
this._duration = 0;
}
_parsePAT(data,offset) {
// skip the PSI header and parse the first PMT entry
this._pmtId = (data[offset+10] & 0x1F) << 8 | data[offset+11];
//logger.log('PMT PID:' + this._pmtId);
}
_parsePMT(data,offset) {
var sectionLength,tableEnd,programInfoLength,pid;
sectionLength = (data[offset+1] & 0x0f) << 8 | data[offset+2];
tableEnd = offset + 3 + sectionLength - 4;
// to determine where the table is, we have to figure out how
// long the program info descriptors are
programInfoLength = (data[offset+10] & 0x0f) << 8 | data[offset+11];
// advance the offset to the first entry in the mapping table
offset += 12 + programInfoLength;
while (offset < tableEnd) {
pid = (data[offset + 1] & 0x1F) << 8 | data[offset + 2];
switch(data[offset]) {
// ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio)
case 0x0f:
//logger.log('AAC PID:' + pid);
this._aacId = pid;
this._aacTrack.id = pid;
break;
// ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video)
case 0x1b:
//logger.log('AVC PID:' + pid);
this._avcId = pid;
this._avcTrack.id = pid;
break;
default:
logger.log('unkown stream type:' + data[offset]);
break;
}
// move to the next table entry
// skip past the elementary stream descriptors, if present
offset += ((data[offset + 3] & 0x0F) << 8 | data[offset + 4]) + 5;
}
}
_parsePES(stream) {
var i = 0,frag,pesFlags,pesPrefix,pesLen,pesHdrLen,pesData,pesPts,pesDts,payloadStartOffset;
//retrieve PTS/DTS from first fragment
frag = stream.data[0];
pesPrefix = (frag[0] << 16) + (frag[1] << 8) + frag[2];
if(pesPrefix === 1) {
pesLen = (frag[4] << 8) + frag[5];
pesFlags = frag[7];
if (pesFlags & 0xC0) {
/* PES header described here : http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
as PTS / DTS is 33 bit we cannot use bitwise operator in JS,
as Bitwise operators treat their operands as a sequence of 32 bits */
pesPts = (frag[9] & 0x0E)*536870912 // 1 << 29
+ (frag[10] & 0xFF)*4194304 // 1 << 22
+ (frag[11] & 0xFE)*16384 // 1 << 14
+ (frag[12] & 0xFF)*128 // 1 << 7
+ (frag[13] & 0xFE)/2;
// check if greater than 2^32 -1
if (pesPts > 4294967295) {
// decrement 2^33
pesPts -= 8589934592;
}
if (pesFlags & 0x40) {
pesDts = (frag[14] & 0x0E )*536870912 // 1 << 29
+ (frag[15] & 0xFF )*4194304 // 1 << 22
+ (frag[16] & 0xFE )*16384 // 1 << 14
+ (frag[17] & 0xFF )*128 // 1 << 7
+ (frag[18] & 0xFE )/2;
// check if greater than 2^32 -1
if (pesDts > 4294967295) {
// decrement 2^33
pesDts -= 8589934592;
}
} else {
pesDts = pesPts;
}
}
pesHdrLen = frag[8];
payloadStartOffset = pesHdrLen+9;
// trim PES header
stream.data[0] = stream.data[0].subarray(payloadStartOffset);
stream.size -= payloadStartOffset;
//reassemble PES packet
pesData = new Uint8Array(stream.size);
// reassemble the packet
while (stream.data.length) {
frag = stream.data.shift();
pesData.set(frag, i);
i += frag.byteLength;
}
return { data : pesData, pts : pesPts, dts : pesDts, len : pesLen};
} else {
return null;
}
}
_parseAVCPES(pes) {
var units,track = this._avcTrack,avcSample,key = false;
units = this._parseAVCNALu(pes.data);
//free pes.data to save up some memory
pes.data = null;
units.units.forEach(unit => {
switch(unit.type) {
//IDR
case 5:
key = true;
break;
//SPS
case 7:
if(!track.sps) {
var expGolombDecoder = new ExpGolomb(unit.data);
var config = expGolombDecoder.readSequenceParameterSet();
track.width = config.width;
track.height = config.height;
track.profileIdc = config.profileIdc;
track.profileCompatibility = config.profileCompatibility;
track.levelIdc = config.levelIdc;
track.sps = [unit.data];
track.duration = 90000*this._duration;
var codecarray = unit.data.subarray(1,4);
var codecstring = 'avc1.';
for(var i = 0; i < 3; i++) {
var h = codecarray[i].toString(16);
if (h.length < 2) {
h = '0' + h;
}
codecstring += h;
}
track.codec = codecstring;
}
break;
//PPS
case 8:
if(!track.pps) {
track.pps = [unit.data];
}
break;
default:
break;
}
});
//build sample from PES
// Annex B to MP4 conversion to be done
avcSample = { units : units, pts : pes.pts, dts : pes.dts , key : key};
this._avcSamples.push(avcSample);
this._avcSamplesLength += units.length;
this._avcSamplesNbNalu += units.units.length;
// generate Init Segment if needed
if(!this._initSegGenerated) {
this._generateInitSegment();
}
}
_flushAVCSamples() {
var view,i=8,avcSample,mp4Sample,mp4SampleLength,unit,track = this._avcTrack,
lastSampleDTS,mdat,moof,firstPTS,firstDTS,pts,dts,ptsnorm,dtsnorm,samples = [];
/* concatenate the video data and construct the mdat in place
(need 8 more bytes to fill length and mpdat type) */
mdat = new Uint8Array(this._avcSamplesLength + (4 * this._avcSamplesNbNalu)+8);
view = new DataView(mdat.buffer);
view.setUint32(0,mdat.byteLength);
mdat.set(MP4.types.mdat,4);
while(this._avcSamples.length) {
avcSample = this._avcSamples.shift();
mp4SampleLength = 0;
// convert NALU bitstream to MP4 format (prepend NALU with size field)
while(avcSample.units.units.length) {
unit = avcSample.units.units.shift();
view.setUint32(i, unit.data.byteLength);
i += 4;
mdat.set(unit.data, i);
i += unit.data.byteLength;
mp4SampleLength+=4+unit.data.byteLength;
}
pts = avcSample.pts - this._initDTS;
dts = avcSample.dts - this._initDTS;
//logger.log('Video/PTS/DTS:' + avcSample.pts + '/' + avcSample.dts);
if(lastSampleDTS !== undefined) {
ptsnorm = this._PTSNormalize(pts,lastSampleDTS);
dtsnorm = this._PTSNormalize(dts,lastSampleDTS);
mp4Sample.duration = dtsnorm - lastSampleDTS;
if(mp4Sample.duration < 0) {
//logger.log('invalid sample duration at PTS/DTS::' + avcSample.pts + '/' + avcSample.dts + ':' + mp4Sample.duration);
mp4Sample.duration = 0;
}
} else {
ptsnorm = this._PTSNormalize(pts,this.nextAvcPts);
dtsnorm = this._PTSNormalize(dts,this.nextAvcPts);
// check if fragments are contiguous (i.e. no missing frames between fragment)
if(this.nextAvcPts) {
var delta = Math.round((ptsnorm - this.nextAvcPts)/90),absdelta=Math.abs(delta);
//logger.log('absdelta/avcSample.pts:' + absdelta + '/' + avcSample.pts);
// if delta is less than 300 ms, next loaded fragment is assumed to be contiguous with last one
if(absdelta < 300) {
//logger.log('Video next PTS:' + this.nextAvcPts);
if(delta > 1) {
logger.log(`AVC:${delta} ms hole between fragments detected,filling it`);
} else if (delta < -1) {
logger.log(`AVC:${(-delta)} ms overlapping between fragments detected`);
}
// set PTS to next PTS
ptsnorm = this.nextAvcPts;
// offset DTS as well, ensure that DTS is smaller or equal than new PTS
dtsnorm = Math.max(dtsnorm-delta, this.lastAvcDts);
// logger.log('Video/PTS/DTS adjusted:' + avcSample.pts + '/' + avcSample.dts);
}
}
// remember first PTS of our avcSamples, ensure value is positive
firstPTS = Math.max(0,ptsnorm);
firstDTS = Math.max(0,dtsnorm);
}
console.log(`PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${avcSample.pts}/${avcSample.dts}/${this._initDTS}/${ptsnorm}/${dtsnorm}/${(avcSample.pts/4294967296).toFixed(3)}`);
mp4Sample = {
size: mp4SampleLength,
duration : 0,
cts: ptsnorm - dtsnorm,
flags: {
isLeading: 0,
isDependedOn: 0,
hasRedundancy: 0,
degradPrio: 0
}
};
if(avcSample.key === true) {
// the current sample is a key frame
mp4Sample.flags.dependsOn = 2;
mp4Sample.flags.isNonSync = 0;
} else {
mp4Sample.flags.dependsOn = 1;
mp4Sample.flags.isNonSync = 1;
}
samples.push(mp4Sample);
lastSampleDTS = dtsnorm;
}
if(samples.length >=2) {
mp4Sample.duration = samples[samples.length-2].duration;
}
this.lastAvcDts = dtsnorm;
// next AVC sample PTS should be equal to last sample PTS + duration
this.nextAvcPts = ptsnorm + mp4Sample.duration;
//logger.log('Video/lastAvcDts/nextAvcPts:' + this.lastAvcDts + '/' + this.nextAvcPts);
this._avcSamplesLength = 0;
this._avcSamplesNbNalu = 0;
track.samples = samples;
moof = MP4.moof(track.sequenceNumber++,firstDTS,track);
track.samples = [];
observer.trigger(Event.FRAG_PARSING_DATA,{
moof: moof,
mdat: mdat,
startPTS : firstPTS/90000,
endPTS : this.nextAvcPts/90000,
startDTS : firstDTS/90000,
endDTS : (dtsnorm + mp4Sample.duration)/90000,
type : 'video',
nb : samples.length
});
}
_parseAVCNALu(array) {
var i = 0,len = array.byteLength,value,overflow,state = 0;
var units = [], unit, unitType, lastUnitStart,lastUnitType,length = 0;
//logger.log('PES:' + Hex.hexDump(array));
while(i< len) {
value = array[i++];
// finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)
switch(state) {
case 0:
if(value === 0) {
state = 1;
}
break;
case 1:
if(value === 0) {
state = 2;
} else {
state = 0;
}
break;
case 2:
case 3:
if(value === 0) {
state = 3;
} else if(value === 1) {
unitType = array[i] & 0x1f;
//logger.log('find NALU @ offset:' + i + ',type:' + unitType);
if(lastUnitStart) {
unit = { data : array.subarray(lastUnitStart,i-state-1), type : lastUnitType};
length+=i-state-1-lastUnitStart;
//logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
units.push(unit);
} else {
// If NAL units are not starting right at the beginning of the PES packet, push preceding data into previous NAL unit.
overflow = i - state - 1;
if (overflow) {
//logger.log('first NALU found with overflow:' + overflow);
if(this._avcSamples.length) {
var lastavcSample = this._avcSamples[this._avcSamples.length-1];
var lastUnit = lastavcSample.units.units[lastavcSample.units.units.length-1];
var tmp = new Uint8Array(lastUnit.data.byteLength+overflow);
tmp.set(lastUnit.data,0);
tmp.set(array.subarray(0,overflow),lastUnit.data.byteLength);
lastUnit.data = tmp;
lastavcSample.units.length+=overflow;
this._avcSamplesLength+=overflow;
}
}
}
lastUnitStart = i;
lastUnitType = unitType;
if(unitType === 1 || unitType === 5) {
// OPTI !!! if IDR/NDR unit, consider it is last NALu
i = len;
}
state = 0;
} else {
state = 0;
}
break;
default:
break;
}
}
if(lastUnitStart) {
unit = { data : array.subarray(lastUnitStart,len), type : lastUnitType};
length+=len-lastUnitStart;
units.push(unit);
//logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
}
return { units : units , length : length};
}
_PTSNormalize(value,reference) {
var offset;
if (reference < value) {
// - 2^33
offset = -8589934592;
} else {
// + 2^33
offset = 8589934592;
}
/* PTS is 33bit (from 0 to 2^33 -1)
if diff between value and reference is bigger than half of the amplitude (2^32) then it means that
PTS looping occured. fill the gap */
while (Math.abs(value - reference) > 4294967296) {
value += offset;
}
return value;
}
_parseAACPES(pes) {
var track = this._aacTrack,aacSample,data = pes.data,config,adtsFrameSize,adtsStartOffset,adtsHeaderLen,stamp,nbSamples,len;
if(this.aacOverFlow) {
var tmp = new Uint8Array(this.aacOverFlow.byteLength+data.byteLength);
tmp.set(this.aacOverFlow,0);
tmp.set(data,this.aacOverFlow.byteLength);
data = tmp;
}
// look for ADTS header (0xFFFx)
for(adtsStartOffset = 0, len = data.length; adtsStartOffset<len-1; adtsStartOffset++) {
if((data[adtsStartOffset] === 0xff) && (data[adtsStartOffset+1] & 0xf0) === 0xf0) {
break;
}
}
// if ADTS header does not start straight from the beginning of the PES payload, raise an error
if(adtsStartOffset) {
var reason,fatal;
if(adtsStartOffset < len - 1) {
reason = `AAC PES did not start with ADTS header,offset:${adtsStartOffset}`;
fatal = false;
} else {
reason = `no ADTS header found in AAC PES`;
fatal = true;
}
observer.trigger(Event.ERROR, { type : ErrorTypes.MEDIA_ERROR, details : ErrorDetails.FRAG_PARSING_ERROR, fatal:fatal, reason : reason});
if(fatal) {
return;
}
}
if(!track.audiosamplerate) {
config = this._ADTStoAudioConfig(data,adtsStartOffset,this.audioCodec);
track.config = config.config;
track.audiosamplerate = config.samplerate;
track.channelCount = config.channelCount;
track.codec = config.codec;
track.duration = 90000*this._duration;
console.log(`parsed codec:${track.codec},rate:${config.samplerate},nb channel:${config.channelCount}`);
}
nbSamples = 0;
while((adtsStartOffset + 5) < len) {
// retrieve frame size
adtsFrameSize = ((data[adtsStartOffset+3] & 0x03) << 11);
// byte 4
adtsFrameSize |= (data[adtsStartOffset+4] << 3);
// byte 5
adtsFrameSize |= ((data[adtsStartOffset+5] & 0xE0) >>> 5);
adtsHeaderLen = (!!(data[adtsStartOffset+1] & 0x01) ? 7 : 9);
adtsFrameSize -= adtsHeaderLen;
stamp = Math.round(pes.pts + nbSamples*1024*90000/track.audiosamplerate);
//stamp = pes.pts;
//console.log('AAC frame, offset/length/pts:' + (adtsStartOffset+7) + '/' + adtsFrameSize + '/' + stamp.toFixed(0));
if(adtsStartOffset+adtsHeaderLen+adtsFrameSize <= len) {
aacSample = { unit : data.subarray(adtsStartOffset+adtsHeaderLen,adtsStartOffset+adtsHeaderLen+adtsFrameSize) , pts : stamp, dts : stamp};
this._aacSamples.push(aacSample);
this._aacSamplesLength += adtsFrameSize;
adtsStartOffset+=adtsFrameSize+adtsHeaderLen;
nbSamples++;
} else {
break;
}
}
if(!this._initSegGenerated) {
this._generateInitSegment();
}
if(adtsStartOffset < len) {
this.aacOverFlow = data.subarray(adtsStartOffset,len);
} else {
this.aacOverFlow = null;
}
}
_flushAACSamples() {
var view,i=8,aacSample,mp4Sample,unit,track = this._aacTrack,
lastSampleDTS,mdat,moof,firstPTS,firstDTS,pts,dts,ptsnorm,dtsnorm,samples = [];
/* concatenate the audio data and construct the mdat in place
(need 8 more bytes to fill length and mpdat type) */
mdat = new Uint8Array(this._aacSamplesLength+8);
view = new DataView(mdat.buffer);
view.setUint32(0,mdat.byteLength);
mdat.set(MP4.types.mdat,4);
while(this._aacSamples.length) {
aacSample = this._aacSamples.shift();
unit = aacSample.unit;
mdat.set(unit, i);
i += unit.byteLength;
pts = aacSample.pts - this._initDTS;
dts = aacSample.dts - this._initDTS;
//logger.log('Audio/PTS:' + aacSample.pts.toFixed(0));
if(lastSampleDTS !== undefined) {
ptsnorm = this._PTSNormalize(pts,lastSampleDTS);
dtsnorm = this._PTSNormalize(dts,lastSampleDTS);
// we use DTS to compute sample duration, but we use PTS to compute initPTS which is used to sync audio and video
mp4Sample.duration = dtsnorm - lastSampleDTS;
if(mp4Sample.duration < 0) {
//logger.log('invalid sample duration at PTS/DTS::' + avcSample.pts + '/' + avcSample.dts + ':' + mp4Sample.duration);
mp4Sample.duration = 0;
}
} else {
ptsnorm = this._PTSNormalize(pts,this.nextAacPts);
dtsnorm = this._PTSNormalize(dts,this.nextAacPts);
// check if fragments are contiguous (i.e. no missing frames between fragment)
if(this.nextAacPts && this.nextAacPts !== ptsnorm) {
//logger.log('Audio next PTS:' + this.nextAacPts);
var delta = Math.round((ptsnorm - this.nextAacPts)/90);
// if delta is less than 300 ms, next loaded fragment is assumed to be contiguous with last one
if(Math.abs(delta) > 1 && Math.abs(delta) < 300) {
if(delta > 0) {
logger.log(`AAC:${delta} ms hole between fragments detected,filling it`);
// set PTS to next PTS, and ensure PTS is greater or equal than last DTS
ptsnorm = Math.max(this.nextAacPts, this.lastAacDts);
dtsnorm = ptsnorm;
//logger.log('Audio/PTS/DTS adjusted:' + aacSample.pts + '/' + aacSample.dts);
} else {
logger.log(`AAC:${(-delta)} ms overlapping between fragments detected`);
}
}
}
// remember first PTS of our aacSamples, ensure value is positive
firstPTS = Math.max(0,ptsnorm);
firstDTS = Math.max(0,dtsnorm);
}
console.log(`PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${aacSample.pts}/${aacSample.dts}/${this._initDTS}/${ptsnorm}/${dtsnorm}/${(aacSample.pts/4294967296).toFixed(3)}`);
mp4Sample = {
size: unit.byteLength,
cts: 0,
duration:0,
flags: {
isLeading: 0,
isDependedOn: 0,
hasRedundancy: 0,
degradPrio: 0,
dependsOn : 1,
}
};
samples.push(mp4Sample);
lastSampleDTS = dtsnorm;
}
//set last sample duration as being identical to previous sample
if(samples.length >=2) {
mp4Sample.duration = samples[samples.length-2].duration;
}
this.lastAacDts = dtsnorm;
// next aac sample PTS should be equal to last sample PTS + duration
this.nextAacPts = ptsnorm + mp4Sample.duration;
//logger.log('Audio/PTS/PTSend:' + aacSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0));
this._aacSamplesLength = 0;
track.samples = samples;
moof = MP4.moof(track.sequenceNumber++,firstDTS,track);
track.samples = [];
observer.trigger(Event.FRAG_PARSING_DATA,{
moof: moof,
mdat: mdat,
startPTS : firstPTS/90000,
endPTS : this.nextAacPts/90000,
startDTS : firstDTS/90000,
endDTS : (dtsnorm + mp4Sample.duration)/90000,
type : 'audio',
nb : samples.length
});
}
_ADTStoAudioConfig(data,offset,audioCodec) {
var adtsObjectType, // :int
adtsSampleingIndex, // :int
adtsExtensionSampleingIndex, // :int
adtsChanelConfig, // :int
config,
userAgent = navigator.userAgent.toLowerCase(),
adtsSampleingRates = [
96000, 88200,
64000, 48000,
44100, 32000,
24000, 22050,
16000, 12000
];
// byte 2
adtsObjectType = ((data[offset+2] & 0xC0) >>> 6) + 1;
adtsSampleingIndex = ((data[offset+2] & 0x3C) >>> 2);
adtsChanelConfig = ((data[offset+2] & 0x01) << 2);
// byte 3
adtsChanelConfig |= ((data[offset+3] & 0xC0) >>> 6);
console.log(`manifest codec:${audioCodec},ADTS data:type:${adtsObjectType},sampleingIndex:${adtsSampleingIndex}[${adtsSampleingRates[adtsSampleingIndex]}kHz],channelConfig:${adtsChanelConfig}`);
// firefox: freq less than 24kHz = AAC SBR (HE-AAC)
if(userAgent.indexOf('firefox') !== -1) {
if(adtsSampleingIndex >=6) {
adtsObjectType = 5;
config = new Array(4);
// HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
// there is a factor 2 between frame sample rate and output sample rate
// multiply frequency by 2 (see table below, equivalent to substract 3)
adtsExtensionSampleingIndex = adtsSampleingIndex-3;
} else {
adtsObjectType = 2;
config = new Array(2);
adtsExtensionSampleingIndex = adtsSampleingIndex;
}
// Android : always use AAC
} else if(userAgent.indexOf('android') !== -1) {
adtsObjectType = 2;
config = new Array(2);
adtsExtensionSampleingIndex = adtsSampleingIndex;
} else {
/* for other browsers (chrome ...)
always force audio type to be HE-AAC SBR, as some browsers do not support audio codec switch properly (like Chrome ...)
*/
adtsObjectType = 5;
config = new Array(4);
// if (manifest codec is HE-AAC) OR (manifest codec not specified AND frequency less than 24kHz)
if((audioCodec && audioCodec.indexOf('mp4a.40.5') !==-1) || (!audioCodec && adtsSampleingIndex >=6)) {
// HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
// there is a factor 2 between frame sample rate and output sample rate
// multiply frequency by 2 (see table below, equivalent to substract 3)
adtsExtensionSampleingIndex = adtsSampleingIndex - 3;
} else {
// if (manifest codec is AAC) AND (frequency less than 24kHz OR nb channel is 1)
if(audioCodec && audioCodec.indexOf('mp4a.40.2') !==-1 && (adtsSampleingIndex >= 6 || adtsChanelConfig ===1)) {
adtsObjectType = 2;
config = new Array(2);
}
adtsExtensionSampleingIndex = adtsSampleingIndex;
}
}
/* refer to http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Audio_Specific_Config
ISO 14496-3 (AAC).pdf - Table 1.13 — Syntax of AudioSpecificConfig()
Audio Profile / Audio Object Type
0: Null
1: AAC Main
2: AAC LC (Low Complexity)
3: AAC SSR (Scalable Sample Rate)
4: AAC LTP (Long Term Prediction)
5: SBR (Spectral Band Replication)
6: AAC Scalable
sampling freq
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
13: Reserved
14: Reserved
15: frequency is written explictly
Channel Configurations
These are the channel configurations:
0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
*/
// audioObjectType = profile => profile, the MPEG-4 Audio Object Type minus 1
config[0] = adtsObjectType << 3;
// samplingFrequencyIndex
config[0] |= (adtsSampleingIndex & 0x0E) >> 1;
config[1] |= (adtsSampleingIndex & 0x01) << 7;
// channelConfiguration
config[1] |= adtsChanelConfig << 3;
if(adtsObjectType === 5) {
// adtsExtensionSampleingIndex
config[1] |= (adtsExtensionSampleingIndex & 0x0E) >> 1;
config[2] = (adtsExtensionSampleingIndex & 0x01) << 7;
// adtsObjectType (force to 2, chrome is checking that object type is less than 5 ???
// https://chromium.googlesource.com/chromium/src.git/+/master/media/formats/mp4/aac.cc
config[2] |= 2 << 2;
config[3] = 0;
}
return { config : config, samplerate : adtsSampleingRates[adtsSampleingIndex], channelCount : adtsChanelConfig, codec : ('mp4a.40.' + adtsObjectType)};
}
_generateInitSegment() {
if(this._avcId === -1) {
//audio only
if(this._aacTrack.config) {
observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT,{
audioMoov: MP4.initSegment([this._aacTrack]),
audioCodec : this._aacTrack.codec,
audioChannelCount : this._aacTrack.channelCount
});
this._initSegGenerated = true;
}
if(this._initPTS === undefined) {
// remember first PTS of this demuxing context
this._initPTS = this._aacSamples[0].pts - 90000*this.timeOffset;
this._initDTS = this._aacSamples[0].dts - 90000*this.timeOffset;
}
} else
if(this._aacId === -1) {
//video only
if(this._avcTrack.sps && this._avcTrack.pps) {
observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT,{
videoMoov: MP4.initSegment([this._avcTrack]),
videoCodec : this._avcTrack.codec,
videoWidth : this._avcTrack.width,
videoHeight : this._avcTrack.height
});
this._initSegGenerated = true;
if(this._initPTS === undefined) {
// remember first PTS of this demuxing context
this._initPTS = this._avcSamples[0].pts - 90000*this.timeOffset;
this._initDTS = this._avcSamples[0].dts - 90000*this.timeOffset;
}
}
} else {
//audio and video
if(this._aacTrack.config && this._avcTrack.sps && this._avcTrack.pps) {
observer.trigger(Event.FRAG_PARSING_INIT_SEGMENT,{
audioMoov: MP4.initSegment([this._aacTrack]),
audioCodec : this._aacTrack.codec,
audioChannelCount : this._aacTrack.channelCount,
videoMoov: MP4.initSegment([this._avcTrack]),
videoCodec : this._avcTrack.codec,
videoWidth : this._avcTrack.width,
videoHeight : this._avcTrack.height
});
this._initSegGenerated = true;
if(this._initPTS === undefined) {
// remember first PTS of this demuxing context
this._initPTS = Math.min(this._avcSamples[0].pts,this._aacSamples[0].pts) - 90000*this.timeOffset;
this._initDTS = Math.min(this._avcSamples[0].dts,this._aacSamples[0].dts) - 90000*this.timeOffset;
}
}
}
}
}
export default TSDemuxer;