From 33792ca2daae5d3fdcd7a8f72ddb3d2455db2ece Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Wed, 22 Aug 2018 13:34:48 -0700 Subject: [PATCH] Workarounds TS contents with dts moving backwards Negative duration is not allowed, so set the duration of that sample to an arbitrary small value in case it is needed to decode future samples. Issue #451. Change-Id: I9250d71d163f769ea2657d56e108b6dbd583de67 --- packager/media/formats/mp2t/es_parser_h26x.cc | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packager/media/formats/mp2t/es_parser_h26x.cc b/packager/media/formats/mp2t/es_parser_h26x.cc index eefff70ddb..9588fa7d67 100644 --- a/packager/media/formats/mp2t/es_parser_h26x.cc +++ b/packager/media/formats/mp2t/es_parser_h26x.cc @@ -304,10 +304,20 @@ bool EsParserH26x::EmitFrame(int64_t access_unit_pos, media_sample->set_dts(current_timing_desc.dts); media_sample->set_pts(current_timing_desc.pts); if (pending_sample_) { - DCHECK_GT(media_sample->dts(), pending_sample_->dts()); - pending_sample_duration_ = media_sample->dts() - pending_sample_->dts(); - pending_sample_->set_duration(pending_sample_duration_); - emit_sample_cb_.Run(pid(), pending_sample_); + if (media_sample->dts() <= pending_sample_->dts()) { + LOG(WARNING) << "[MPEG-2 TS] PID " << pid() << " dts " + << media_sample->dts() + << " less than or equal to previous dts " + << pending_sample_->dts(); + // Keep the sample but adjust the sample duration to a very small value, + // in case that the sample is still needed for the decoding afterwards. + const int64_t kArbitrarySmallDuration = 0.001 * kMpeg2Timescale; // 1ms. + pending_sample_->set_duration(kArbitrarySmallDuration); + } else { + pending_sample_duration_ = media_sample->dts() - pending_sample_->dts(); + pending_sample_->set_duration(pending_sample_duration_); + } + emit_sample_cb_.Run(pid(), std::move(pending_sample_)); } pending_sample_ = media_sample;