Skip to content

Commit

Permalink
Fix mp3 and aac demuxer streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Walch committed Sep 24, 2019
1 parent 5fa0d89 commit 0eb146f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
5 changes: 1 addition & 4 deletions src/demux/aacdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ class AACDemuxer implements Demuxer {
lastDataIndex = offset;
} else {
logger.log('Unable to parse AAC frame');
const partialData = data.slice(offset);

this.cachedData = appendUint8Array(this.cachedData, partialData);
offset += partialData.length;
offset = length;
}
} else if (ID3.canParse(data, offset)) {
id3Data = ID3.getID3Data(data, offset);
Expand Down
54 changes: 30 additions & 24 deletions src/demux/mp3demuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ import { dummyTrack } from './dummy-demuxed-track';
import { appendUint8Array } from '../utils/mp4-tools';

class MP3Demuxer implements Demuxer {
private observer: any;
private config: any;
private _audioTrack!: any;
private _id3Track!: DemuxedTrack;
private cachedData: Uint8Array = new Uint8Array();
private frameIndex: number = 0;
private cachedData: Uint8Array = new Uint8Array();
private initPTS: number | null = null;
static readonly minProbeByteLength: number = 4;

constructor (observer, config) {
this.observer = observer;
this.config = config;
}

resetInitSegment (audioCodec, videoCodec, duration) {
this._audioTrack = { container: 'audio/mpeg', type: 'audio', id: -1, sequenceNumber: 0, isAAC: false, samples: [], len: 0, manifestCodec: audioCodec, duration: duration, inputTimeScale: 90000 };
this._id3Track = {
Expand All @@ -36,25 +43,28 @@ class MP3Demuxer implements Demuxer {
}

static probe (data) {
if (!data) {
return false;
}

// check if data contains ID3 timestamp and MPEG sync word
let offset, length;
const id3Data = ID3.getID3Data(data, 0);
if (id3Data && ID3.getTimeStamp(id3Data) !== undefined) {
// Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
// Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
// More info http://www.mp3-tech.org/programmer/frame_header.html
for (offset = id3Data.length, length = Math.min(data.length - 1, offset + 100); offset < length; offset++) {
if (MpegAudio.probe(data, offset)) {
logger.log('MPEG Audio sync word found !');
return true;
}
// Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
// Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
// More info http://www.mp3-tech.org/programmer/frame_header.html
const id3Data = ID3.getID3Data(data, 0) || [];
let offset = id3Data.length;

for (let length = data.length; offset < length; offset++) {
if (MpegAudio.probe(data, offset)) {
logger.log('MPEG Audio sync word found !');
return true;
}
}
return false;
}

// feed incoming data to the front of the parsing pipeline
demux (data, timeOffset) {
demux (data, timeOffset): DemuxerResult {
if (this.cachedData.length) {
data = appendUint8Array(this.cachedData, data);
this.cachedData = new Uint8Array();
Expand All @@ -70,37 +80,33 @@ class MP3Demuxer implements Demuxer {
const length = data.length;

if (this.initPTS === null) {
this.initPTS = timestamp ? 90 * timestamp : timeOffset * 90000;
this.initPTS = Number.isFinite(timestamp) ? timestamp * 90 : timeOffset * 90000;
}

if (id3Data.length) {
id3Track.samples.push({ pts: this.initPTS, dts: this.initPTS, data: id3Data });
}

pts = this.initPTS;

while (offset < length) {
if (MpegAudio.canParse(data, offset)) {
const frame = MpegAudio.appendFrame(track, data, offset, this.initPTS, this.frameIndex);
if (frame) {
offset += frame.length;
pts = frame.sample.pts;
this.frameIndex++;
pts = frame.sample.pts;
offset += frame.length;
lastDataIndex = offset;
} else {
const partialData = data.slice(offset);

this.cachedData = appendUint8Array(this.cachedData, partialData);
offset += partialData.length;
offset = length;
}
} else if (ID3.canParse(data, offset)) {
id3Data = ID3.getID3Data(data, offset);
id3Track.samples.push({ pts: pts, dts: pts, data: id3Data });
offset += id3Data.length;
lastDataIndex = offset;
} else {
const partialData = data.slice(offset);

this.cachedData = appendUint8Array(this.cachedData, partialData);
offset += partialData.length;
offset++;
}
if (offset === length && lastDataIndex !== length) {
const partialData = data.slice(lastDataIndex);
Expand Down

0 comments on commit 0eb146f

Please sign in to comment.