Skip to content

Commit

Permalink
tsdemuxer: if PES does not contain PTS/DTS, use last PES PTS/DTS instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume du Pontavice committed Jan 30, 2019
1 parent dcb3db6 commit cebe2ff
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/demux/tsdemuxer.js
Expand Up @@ -184,7 +184,7 @@ class TSDemuxer {
switch (pid) {
case avcId:
if (stt) {
if (avcData && (pes = parsePES(avcData)) && pes.pts !== undefined) {
if (avcData && (pes = parsePES(avcData))) {
parseAVCPES(pes, false);
}

Expand All @@ -197,7 +197,7 @@ class TSDemuxer {
break;
case audioId:
if (stt) {
if (audioData && (pes = parsePES(audioData)) && pes.pts !== undefined) {
if (audioData && (pes = parsePES(audioData))) {
if (audioTrack.isAAC) {
parseAACPES(pes);
} else {
Expand All @@ -213,7 +213,7 @@ class TSDemuxer {
break;
case id3Id:
if (stt) {
if (id3Data && (pes = parsePES(id3Data)) && pes.pts !== undefined) {
if (id3Data && (pes = parsePES(id3Data))) {
parseID3PES(pes);
}

Expand Down Expand Up @@ -279,15 +279,15 @@ class TSDemuxer {
}
}
// try to parse last PES packets
if (avcData && (pes = parsePES(avcData)) && pes.pts !== undefined) {
if (avcData && (pes = parsePES(avcData))) {
parseAVCPES(pes, true);
avcTrack.pesData = null;
} else {
// either avcData null or PES truncated, keep it for next frag parsing
avcTrack.pesData = avcData;
}

if (audioData && (pes = parsePES(audioData)) && pes.pts !== undefined) {
if (audioData && (pes = parsePES(audioData))) {
if (audioTrack.isAAC) {
parseAACPES(pes);
} else {
Expand All @@ -304,7 +304,7 @@ class TSDemuxer {
audioTrack.pesData = audioData;
}

if (id3Data && (pes = parsePES(id3Data)) && pes.pts !== undefined) {
if (id3Data && (pes = parsePES(id3Data))) {
parseID3PES(pes);
id3Track.pesData = null;
} else {
Expand Down Expand Up @@ -534,6 +534,18 @@ class TSDemuxer {
if (avcSample.units.length && avcSample.frame) {
const samples = avcTrack.samples;
const nbSamples = samples.length;
// if sample does not have PTS/DTS, patch with last sample PTS/DTS
if (isNaN(avcSample.pts)) {
if (nbSamples) {
const lastSample = samples[nbSamples - 1];
avcSample.pts = lastSample.pts;
avcSample.dts = lastSample.dts;
} else {
// dropping samples, no timestamp found
avcTrack.dropped++;
return;
}
}
// only push AVC sample if starting with a keyframe is not mandatory OR
// if keyframe already found in this fragment OR
// keyframe found in last fragment (track.sps) AND
Expand Down

0 comments on commit cebe2ff

Please sign in to comment.