OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/text_ranges.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace media { | |
10 | |
11 TextRanges::TextRanges() { | |
12 Reset(); | |
13 } | |
14 | |
15 void TextRanges::Reset() { | |
16 curr_range_itr_ = range_map_.end(); | |
17 } | |
18 | |
19 bool TextRanges::AddCue(base::TimeDelta start_time) { | |
20 typedef RangeMap::iterator Itr; | |
21 | |
22 if (curr_range_itr_ == range_map_.end()) { | |
23 // There is no active time range, so this is the first AddCue() | |
24 // attempt that follows a Reset(). | |
25 | |
26 if (range_map_.empty()) { | |
27 NewRange(start_time); | |
28 return true; | |
29 } | |
30 | |
31 if (start_time < range_map_.begin()->first) { | |
32 NewRange(start_time); | |
33 return true; | |
34 } | |
35 | |
36 const Itr itr = --Itr(range_map_.upper_bound(start_time)); | |
37 DCHECK(start_time >= itr->first); | |
38 | |
39 Range& range = itr->second; | |
40 | |
41 if (start_time > range.last_time()) { | |
42 NewRange(start_time); | |
43 return true; | |
44 } | |
45 | |
46 range.ResetCount(start_time); | |
47 curr_range_itr_ = itr; | |
48 return false; | |
49 } | |
50 | |
51 DCHECK(start_time >= curr_range_itr_->first); | |
52 | |
53 Range& curr_range = curr_range_itr_->second; | |
54 | |
55 if (start_time <= curr_range.last_time()) | |
56 return curr_range.AddCue(start_time); | |
57 | |
58 const Itr next_range_itr = ++Itr(curr_range_itr_); | |
59 | |
60 if (next_range_itr != range_map_.end()) { | |
61 DCHECK(next_range_itr->first > curr_range.last_time()); | |
62 DCHECK(start_time <= next_range_itr->first); | |
63 | |
64 if (start_time == next_range_itr->first) { | |
65 // We have walked off the currrent range, and onto the next one. | |
acolwell GONE FROM CHROMIUM
2014/02/07 18:30:52
nit: s/currrent/current/
Matthew Heaney (Chromium)
2014/02/07 18:49:07
Done.
| |
66 // There is now no ambiguity about where the current time range | |
67 // ends, and so we coalesce the current and next ranges. | |
68 | |
69 Merge(curr_range, next_range_itr); | |
70 return false; | |
71 } | |
72 } | |
73 | |
74 // Either curr_range is the last range in the map, or there is a | |
acolwell GONE FROM CHROMIUM
2014/02/07 18:30:52
nit: s/curr_range/|curr_range|/ here and elsewhere
Matthew Heaney (Chromium)
2014/02/07 18:49:07
Done.
| |
75 // next range beyond curr_range, but its start time is ahead of this | |
76 // cue's start time. In either case, this cue becomes the new | |
77 // last_time for curr_range. Eventually we will see a cue whose | |
78 // time matches the start time of the next range, in which case we | |
79 // coalesce the current and next ranges. | |
80 | |
81 curr_range.SetLastTime(start_time); | |
82 return true; | |
83 } | |
84 | |
85 size_t TextRanges::RangeCountForTesting() const { | |
86 return range_map_.size(); | |
87 } | |
88 | |
89 void TextRanges::NewRange(base::TimeDelta start_time) { | |
90 Range range; | |
91 range.SetLastTime(start_time); | |
92 | |
93 std::pair<RangeMap::iterator, bool> result = | |
94 range_map_.insert(std::make_pair(start_time, range)); | |
95 DCHECK(result.second); | |
96 | |
97 curr_range_itr_ = result.first; | |
98 } | |
99 | |
100 void TextRanges::Merge( | |
101 Range& curr_range, | |
102 const RangeMap::iterator& next_range_itr) { | |
103 curr_range = next_range_itr->second; | |
104 curr_range.ResetCount(next_range_itr->first); | |
105 range_map_.erase(next_range_itr); | |
106 } | |
107 | |
108 void TextRanges::Range::ResetCount(base::TimeDelta start_time) { | |
109 count_ = (start_time < last_time_) ? 0 : 1; | |
110 } | |
111 | |
112 void TextRanges::Range::SetLastTime(base::TimeDelta last_time) { | |
113 last_time_ = last_time; | |
114 count_ = 1; | |
115 max_count_ = 1; | |
116 } | |
117 | |
118 bool TextRanges::Range::AddCue(base::TimeDelta start_time) { | |
119 if (start_time < last_time_) { | |
120 DCHECK_EQ(count_, 0); | |
121 return false; | |
122 } | |
123 | |
124 DCHECK(start_time == last_time_); | |
125 | |
126 ++count_; | |
127 if (count_ <= max_count_) | |
128 return false; | |
129 | |
130 ++max_count_; | |
131 return true; | |
132 } | |
133 | |
134 base::TimeDelta TextRanges::Range::last_time() const { | |
135 return last_time_; | |
136 } | |
137 | |
138 } // namespace media | |
OLD | NEW |