2015-12-16 00:30:14 -05:00
|
|
|
/**
|
|
|
|
* AAC demuxer
|
|
|
|
*/
|
2016-01-13 15:58:12 -05:00
|
|
|
import ADTS from './adts';
|
2015-12-16 00:30:14 -05:00
|
|
|
import {logger} from '../utils/logger';
|
|
|
|
import ID3 from '../demux/id3';
|
|
|
|
|
|
|
|
class AACDemuxer {
|
|
|
|
|
|
|
|
constructor(observer,remuxerClass) {
|
|
|
|
this.observer = observer;
|
|
|
|
this.remuxerClass = remuxerClass;
|
|
|
|
this.remuxer = new this.remuxerClass(observer);
|
|
|
|
this._aacTrack = {type: 'audio', id :-1, sequenceNumber: 0, samples : [], len : 0};
|
|
|
|
}
|
|
|
|
|
|
|
|
static probe(data) {
|
|
|
|
// check if data contains ID3 timestamp and ADTS sync worc
|
2016-02-03 18:00:01 -05:00
|
|
|
var id3 = new ID3(data), offset,len;
|
2015-12-16 00:30:14 -05:00
|
|
|
if(id3.hasTimeStamp) {
|
|
|
|
// look for ADTS header (0xFFFx)
|
2016-02-03 18:00:01 -05:00
|
|
|
for (offset = id3.length, len = data.length; offset < len - 1; offset++) {
|
|
|
|
if ((data[offset] === 0xff) && (data[offset+1] & 0xf0) === 0xf0) {
|
2015-12-16 00:30:14 -05:00
|
|
|
//logger.log('ADTS sync word found !');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// feed incoming data to the front of the parsing pipeline
|
|
|
|
push(data, audioCodec, videoCodec, timeOffset, cc, level, sn, duration) {
|
2016-01-13 15:58:12 -05:00
|
|
|
var track = this._aacTrack,
|
|
|
|
id3 = new ID3(data),
|
|
|
|
pts = 90*id3.timeStamp,
|
2016-02-03 18:00:01 -05:00
|
|
|
config, frameLength, frameDuration, frameIndex, offset, headerLength, stamp, len, aacSample;
|
2015-12-16 00:30:14 -05:00
|
|
|
// look for ADTS header (0xFFFx)
|
2016-02-03 18:00:01 -05:00
|
|
|
for (offset = id3.length, len = data.length; offset < len - 1; offset++) {
|
|
|
|
if ((data[offset] === 0xff) && (data[offset+1] & 0xf0) === 0xf0) {
|
2015-12-16 00:30:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!track.audiosamplerate) {
|
2016-02-03 18:00:01 -05:00
|
|
|
config = ADTS.getAudioConfig(this.observer,data, offset, audioCodec);
|
2015-12-16 00:30:14 -05:00
|
|
|
track.config = config.config;
|
|
|
|
track.audiosamplerate = config.samplerate;
|
|
|
|
track.channelCount = config.channelCount;
|
|
|
|
track.codec = config.codec;
|
2016-02-03 18:00:01 -05:00
|
|
|
track.timescale = config.samplerate;
|
|
|
|
track.duration = config.samplerate * duration;
|
2015-12-16 00:30:14 -05:00
|
|
|
logger.log(`parsed codec:${track.codec},rate:${config.samplerate},nb channel:${config.channelCount}`);
|
|
|
|
}
|
2016-02-03 18:00:01 -05:00
|
|
|
frameIndex = 0;
|
|
|
|
frameDuration = 1024 * 90000 / track.audiosamplerate;
|
|
|
|
while ((offset + 5) < len) {
|
|
|
|
// The protection skip bit tells us if we have 2 bytes of CRC data at the end of the ADTS header
|
|
|
|
headerLength = (!!(data[offset + 1] & 0x01) ? 7 : 9);
|
2015-12-16 00:30:14 -05:00
|
|
|
// retrieve frame size
|
2016-02-03 18:00:01 -05:00
|
|
|
frameLength = ((data[offset + 3] & 0x03) << 11) |
|
|
|
|
(data[offset + 4] << 3) |
|
|
|
|
((data[offset + 5] & 0xE0) >>> 5);
|
|
|
|
frameLength -= headerLength;
|
2015-12-16 00:30:14 -05:00
|
|
|
//stamp = pes.pts;
|
2016-02-03 18:00:01 -05:00
|
|
|
|
|
|
|
if ((frameLength > 0) && ((offset + headerLength + frameLength) <= len)) {
|
|
|
|
stamp = pts + frameIndex * frameDuration;
|
|
|
|
//logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}/${(stamp/90).toFixed(0)}`);
|
|
|
|
aacSample = {unit: data.subarray(offset + headerLength, offset + headerLength + frameLength), pts: stamp, dts: stamp};
|
2015-12-16 00:30:14 -05:00
|
|
|
track.samples.push(aacSample);
|
2016-02-03 18:00:01 -05:00
|
|
|
track.len += frameLength;
|
|
|
|
offset += frameLength + headerLength;
|
|
|
|
frameIndex++;
|
2015-12-16 00:30:14 -05:00
|
|
|
// look for ADTS header (0xFFFx)
|
2016-02-03 18:00:01 -05:00
|
|
|
for ( ; offset < (len - 1); offset++) {
|
|
|
|
if ((data[offset] === 0xff) && ((data[offset + 1] & 0xf0) === 0xf0)) {
|
2015-12-16 00:30:14 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 12:46:01 -05:00
|
|
|
this.remuxer.remux(this._aacTrack,{samples : []}, {samples : [ { pts: pts, dts : pts, unit : id3.payload} ]}, timeOffset);
|
2015-12-16 00:30:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AACDemuxer;
|