From 1a52a9b7c09f677a40b13aa60b4da0959a72096f Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Wed, 23 May 2018 10:24:57 -0700 Subject: [PATCH] Fix potential incorrect target duration in iFrame Playlist This may happen if media segments do not start with iframes. Change-Id: Ie7b2d33a14405a5dcd071644f2fa3d5f9656dd07 --- packager/hls/base/media_playlist.cc | 14 +++++++++----- packager/hls/base/media_playlist_unittest.cc | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packager/hls/base/media_playlist.cc b/packager/hls/base/media_playlist.cc index 5811140b50..d7489cc551 100644 --- a/packager/hls/base/media_playlist.cc +++ b/packager/hls/base/media_playlist.cc @@ -521,8 +521,8 @@ void MediaPlaylist::AddSegmentInfoEntry(const std::string& segment_file_name, static_cast(start_time) / time_scale_; const double segment_duration_seconds = static_cast(duration) / time_scale_; - if (segment_duration_seconds > longest_segment_duration_) - longest_segment_duration_ = segment_duration_seconds; + longest_segment_duration_ = + std::max(longest_segment_duration_, segment_duration_seconds); const int kBitsInByte = 8; const uint64_t bitrate = kBitsInByte * size / segment_duration_seconds; @@ -539,15 +539,19 @@ void MediaPlaylist::AdjustLastSegmentInfoEntryDuration( if (time_scale_ == 0) return; - const double scaled_next_timestamp = + const double next_timestamp_seconds = static_cast(next_timestamp) / time_scale_; for (auto iter = entries_.rbegin(); iter != entries_.rend(); ++iter) { if (iter->get()->type() == HlsEntry::EntryType::kExtInf) { SegmentInfoEntry* segment_info = reinterpret_cast(iter->get()); - segment_info->set_duration(scaled_next_timestamp - - segment_info->start_time()); + + const double segment_duration_seconds = + next_timestamp_seconds - segment_info->start_time(); + segment_info->set_duration(segment_duration_seconds); + longest_segment_duration_ = + std::max(longest_segment_duration_, segment_duration_seconds); break; } } diff --git a/packager/hls/base/media_playlist_unittest.cc b/packager/hls/base/media_playlist_unittest.cc index c369cfd9fc..0fd653fc9c 100644 --- a/packager/hls/base/media_playlist_unittest.cc +++ b/packager/hls/base/media_playlist_unittest.cc @@ -811,7 +811,7 @@ TEST_F(IFrameMediaPlaylistTest, SingleSegment) { "#EXT-X-VERSION:6\n" "## Generated with https://github.com/google/shaka-packager version " "test\n" - "#EXT-X-TARGETDURATION:8\n" + "#EXT-X-TARGETDURATION:9\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXT-X-I-FRAMES-ONLY\n" "#EXT-X-MAP:URI=\"file.mp4\",BYTERANGE=\"501@0\"\n"