Chromium Code Reviews| Index: media/base/text_ranges.cc |
| diff --git a/media/base/text_ranges.cc b/media/base/text_ranges.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7eab0accf50473fb1f76d6c961d524f8bd35c488 |
| --- /dev/null |
| +++ b/media/base/text_ranges.cc |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/base/text_ranges.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace media { |
| + |
| +TextRanges::TextRanges() { |
| + Reset(); |
| +} |
| + |
| +void TextRanges::Reset() { |
| + curr_range_itr_ = range_map_.end(); |
| +} |
| + |
| +bool TextRanges::AddCue(base::TimeDelta start_time) { |
| + typedef RangeMap::iterator Itr; |
| + |
| + if (curr_range_itr_ == range_map_.end()) { |
| + // There is no active time range, so this is the first AddCue() |
| + // attempt that follows a Reset(). |
| + |
| + if (range_map_.empty()) { |
| + NewRange(start_time); |
| + return true; |
| + } |
| + |
| + if (start_time < range_map_.begin()->first) { |
| + NewRange(start_time); |
| + return true; |
| + } |
| + |
| + const Itr itr = --Itr(range_map_.upper_bound(start_time)); |
| + DCHECK(start_time >= itr->first); |
| + |
| + Range& range = itr->second; |
| + |
| + if (start_time > range.last_time) { |
| + NewRange(start_time); |
| + return true; |
| + } |
| + |
| + range.ResetCount(start_time); |
| + curr_range_itr_ = itr; |
| + return false; |
| + } |
| + |
| + DCHECK(start_time >= curr_range_itr_->first); |
| + |
| + Range& curr_range = curr_range_itr_->second; |
| + |
| + if (start_time <= curr_range.last_time) |
| + return curr_range.AddCue(start_time); |
| + |
| + const Itr next_range_itr = ++Itr(curr_range_itr_); |
| + |
| + if (next_range_itr != range_map_.end()) { |
| + DCHECK(next_range_itr->first > curr_range.last_time); |
| + DCHECK(start_time <= next_range_itr->first); |
| + |
| + if (start_time == next_range_itr->first) { |
| + // We have walked off the curr range, and onto the next one. |
|
acolwell GONE FROM CHROMIUM
2014/02/07 01:44:18
nit:s/curr range/current range/ or s/curr range/|c
Matthew Heaney (Chromium)
2014/02/07 03:24:35
Done.
|
| + // There is now no ambiguity about where the curr time range |
| + // ends, and so we coalesce the curr and next ranges. |
|
acolwell GONE FROM CHROMIUM
2014/02/07 01:44:18
ditto. Either use "current" or the actual variable
Matthew Heaney (Chromium)
2014/02/07 03:24:35
Done.
|
| + |
| + Merge(curr_range, next_range_itr); |
| + return false; |
| + } |
| + } |
| + |
| + // Either curr is the last range in the map, or there is a next |
|
acolwell GONE FROM CHROMIUM
2014/02/07 01:44:18
ditto for this comment as well.
Matthew Heaney (Chromium)
2014/02/07 03:24:35
Done.
|
| + // range beyond the curr range, but its start time is ahead of this |
| + // cue's start time. In either case, this cue becomes the new |
| + // last_time for the curr range. Eventually we will see a cue whose |
| + // time matches the start time of the next range, in which case we |
| + // coalesce the curr and next ranges. |
| + |
| + curr_range.SetLastTime(start_time); |
| + return true; |
| +} |
| + |
| +size_t TextRanges::RangeCountForTesting() const { |
| + return range_map_.size(); |
| +} |
| + |
| +void TextRanges::NewRange(base::TimeDelta start_time) { |
| + Range range; |
| + range.SetLastTime(start_time); |
| + |
| + std::pair<RangeMap::iterator, bool> result = |
| + range_map_.insert(std::make_pair(start_time, range)); |
| + DCHECK(result.second); |
| + |
| + curr_range_itr_ = result.first; |
| +} |
| + |
| +void TextRanges::Merge( |
| + Range& curr_range, |
| + RangeMap::iterator next_range_itr) { |
|
acolwell GONE FROM CHROMIUM
2014/02/07 01:44:18
nit: make const&
Matthew Heaney (Chromium)
2014/02/07 03:24:35
Done.
|
| + curr_range = next_range_itr->second; |
| + curr_range.ResetCount(next_range_itr->first); |
| + range_map_.erase(next_range_itr); |
| +} |
| + |
| +void TextRanges::Range::ResetCount(base::TimeDelta start_time) { |
| + count = (start_time < last_time) ? 0 : 1; |
| +} |
| + |
| +void TextRanges::Range::SetLastTime(base::TimeDelta start_time) { |
|
acolwell GONE FROM CHROMIUM
2014/02/07 01:44:18
nit: s/start_time/last_time/
Matthew Heaney (Chromium)
2014/02/07 03:24:35
Done.
|
| + last_time = start_time; |
| + count = 1; |
| + max_count = 1; |
| +} |
| + |
| +bool TextRanges::Range::AddCue(base::TimeDelta start_time) { |
| + if (start_time < last_time) { |
| + DCHECK_EQ(count, 0); |
| + return false; |
| + } |
| + |
| + DCHECK(start_time == last_time); |
| + |
| + ++count; |
| + if (count <= max_count) |
| + return false; |
| + |
| + ++max_count; |
| + return true; |
| +} |
| + |
| +} // namespace media |