diff --git a/packager/hls/base/media_playlist.cc b/packager/hls/base/media_playlist.cc index e4df80067d..c6b9121db6 100644 --- a/packager/hls/base/media_playlist.cc +++ b/packager/hls/base/media_playlist.cc @@ -182,10 +182,9 @@ void MediaPlaylist::AddSegment(const std::string& file_name, if (segment_duration_seconds > longest_segment_duration_) longest_segment_duration_ = segment_duration_seconds; - total_duration_in_seconds_ += segment_duration_seconds; - total_segments_size_ += size; - ++total_num_segments_; - + const int kBitsInByte = 8; + const uint64_t bitrate = kBitsInByte * size / segment_duration_seconds; + max_bitrate_ = std::max(max_bitrate_, bitrate); entries_.push_back(new SegmentInfoEntry(file_name, segment_duration_seconds)); } @@ -313,12 +312,7 @@ bool MediaPlaylist::WriteToFile(media::File* file) { uint64_t MediaPlaylist::Bitrate() const { if (media_info_.has_bandwidth()) return media_info_.bandwidth(); - if (total_duration_in_seconds_ == 0.0) - return 0; - if (total_segments_size_ == 0) - return 0; - const int kBytesToBits = 8; - return total_segments_size_ * kBytesToBits / total_duration_in_seconds_; + return max_bitrate_; } double MediaPlaylist::GetLongestSegmentDuration() const { diff --git a/packager/hls/base/media_playlist.h b/packager/hls/base/media_playlist.h index e81f9829c4..d90d1ea964 100644 --- a/packager/hls/base/media_playlist.h +++ b/packager/hls/base/media_playlist.h @@ -132,8 +132,7 @@ class MediaPlaylist { virtual bool WriteToFile(media::File* file); /// If bitrate is specified in MediaInfo then it will use that value. - /// Otherwise, it is calculated from the duration and the size of the - /// segments added to this object. + /// Otherwise, returns the max bitrate. /// @return the bitrate (in bits per second) of this MediaPlaylist. virtual uint64_t Bitrate() const; @@ -165,10 +164,8 @@ class MediaPlaylist { double longest_segment_duration_ = 0.0; uint32_t time_scale_ = 0; - // The sum of the size of the segments listed in this playlist (in bytes). - uint64_t total_segments_size_ = 0; + uint64_t max_bitrate_ = 0; double total_duration_in_seconds_ = 0.0; - int total_num_segments_; // See SetTargetDuration() comments. bool target_duration_set_ = false; diff --git a/packager/hls/base/media_playlist_unittest.cc b/packager/hls/base/media_playlist_unittest.cc index fc73465668..792c5957ca 100644 --- a/packager/hls/base/media_playlist_unittest.cc +++ b/packager/hls/base/media_playlist_unittest.cc @@ -148,8 +148,8 @@ TEST_F(MediaPlaylistTest, GetBitrateFromSegments) { // 20 seconds, 5MB. media_playlist_.AddSegment("file2.ts", 1800000, 5000000); - // 200KB per second which is 1600K bits / sec. - EXPECT_EQ(1600000u, media_playlist_.Bitrate()); + // Max bitrate is 2000Kb/s. + EXPECT_EQ(2000000u, media_playlist_.Bitrate()); } TEST_F(MediaPlaylistTest, GetLongestSegmentDuration) {