Skip to content

Commit

Permalink
base audio demuxer: track PTS of last sample for ID3 frames (#5014)
Browse files Browse the repository at this point in the history
  • Loading branch information
jprjr authored and robwalch committed Nov 12, 2022
1 parent 2a607ff commit a93c41a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/demux/base-audio-demuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BaseAudioDemuxer implements Demuxer {
protected cachedData: Uint8Array | null = null;
protected basePTS: number | null = null;
protected initPTS: number | null = null;
protected lastPTS: number | null = null;

resetInitSegment(
initSegment: Uint8Array | undefined,
Expand All @@ -46,6 +47,7 @@ class BaseAudioDemuxer implements Demuxer {

resetContiguity(): void {
this.basePTS = null;
this.lastPTS = null;
this.frameIndex = 0;
}

Expand All @@ -69,7 +71,6 @@ class BaseAudioDemuxer implements Demuxer {
let id3Data: Uint8Array | undefined = ID3.getID3Data(data, 0);
let offset = id3Data ? id3Data.length : 0;
let lastDataIndex;
let pts;
const track = this._audioTrack;
const id3Track = this._id3Track;
const timestamp = id3Data ? ID3.getTimeStamp(id3Data) : undefined;
Expand All @@ -80,27 +81,30 @@ class BaseAudioDemuxer implements Demuxer {
(this.frameIndex === 0 && Number.isFinite(timestamp))
) {
this.basePTS = initPTSFn(timestamp, timeOffset, this.initPTS);
this.lastPTS = this.basePTS;
}

if (this.lastPTS === null) {
this.lastPTS = this.basePTS;
}

// more expressive than alternative: id3Data?.length
if (id3Data && id3Data.length > 0) {
id3Track.samples.push({
pts: this.basePTS,
dts: this.basePTS,
pts: this.lastPTS,
dts: this.lastPTS,
data: id3Data,
type: MetadataSchema.audioId3,
duration: Number.POSITIVE_INFINITY,
});
}

pts = this.basePTS;

while (offset < length) {
if (this.canParse(data, offset)) {
const frame = this.appendFrame(track, data, offset);
if (frame) {
this.frameIndex++;
pts = frame.sample.pts;
this.lastPTS = frame.sample.pts;
offset += frame.length;
lastDataIndex = offset;
} else {
Expand All @@ -110,8 +114,8 @@ class BaseAudioDemuxer implements Demuxer {
// after a ID3.canParse, a call to ID3.getID3Data *should* always returns some data
id3Data = ID3.getID3Data(data, offset)!;
id3Track.samples.push({
pts: pts,
dts: pts,
pts: this.lastPTS,
dts: this.lastPTS,
data: id3Data,
type: MetadataSchema.audioId3,
duration: Number.POSITIVE_INFINITY,
Expand Down

0 comments on commit a93c41a

Please sign in to comment.