Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: media/base/text_ranges.cc

Issue 151103005: TextRenderer only pushes new cues downstream (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: incorporate aaron's comments of 2014/02/05 Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 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.
66 // There is now no ambiguity about where the curr time range
67 // 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.
68
69 Merge(curr_range, next_range_itr);
70 return false;
71 }
72 }
73
74 // 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.
75 // range beyond the 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 the 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 curr 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 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.
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 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.
113 last_time = start_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 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698