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

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: added text ranges utility and unit test 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 Flush();
13 }
14
15 void TextRanges::Flush() {
16 curr_range_itr_ = range_map_.end();
17 }
18
19 bool TextRanges::AddCue(base::TimeDelta start_time) {
20 typedef RangeMap::iterator Itr;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: Please just use the full type below.
Matthew Heaney (Chromium) 2014/02/06 02:25:48 ack
21
22 if (curr_range_itr_ == range_map_.end()) {
23 // No is no active time range, so this is the first cue that
24 // follows a ssek.
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit:s/ssek/Reset()/
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
25
26 if (range_map_.empty()) {
27 NewRange(start_time);
28 return true;
29 }
30
31 const Itr first_itr = range_map_.begin();
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: Inline this since it is only used on the next
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
32
33 if (start_time < first_itr->first) {
34 NewRange(start_time);
35 return true;
36 }
37
38 const Itr itr = --Itr(range_map_.upper_bound(start_time));
39 DCHECK(start_time >= itr->first);
40
41 Range& range = itr->second;
42
43 if (start_time > range.last_time) {
44 NewRange(start_time);
45 return true;
46 }
47
48 if (start_time < range.last_time)
49 range.count = 0;
50 else
51 range.count = 1;
52
53 curr_range_itr_ = itr;
54 return false;
55 }
56
57 DCHECK(start_time >= curr_range_itr_->first);
58
59 Range& curr_range = curr_range_itr_->second;
60
61 if (start_time < curr_range.last_time) {
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 Please put the logic on lines 61 - 74 in a method
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
62 DCHECK_EQ(curr_range.count, 0);
63 return false;
64 }
65
66 if (start_time == curr_range.last_time) {
67 ++curr_range.count;
68
69 if (curr_range.count <= curr_range.max_count)
70 return false;
71
72 ++curr_range.max_count;
73 return true;
74 }
75
76 const Itr next_range_itr = ++Itr(curr_range_itr_);
77
78 if (next_range_itr != range_map_.end()) {
79 DCHECK(next_range_itr->first > curr_range.last_time);
80 DCHECK(start_time <= next_range_itr->first);
81
82 Range& next_range = next_range_itr->second;
83
84 if (start_time == next_range_itr->first) {
85 // We have walked off the curr range, and onto the next one.
86 // There is now no ambiguity about where the curr time range
87 // ends, and so we coalesce the curr and next ranges.
88
89 if (start_time < next_range.last_time)
90 next_range.count = 0;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: This logic is duplicated. It looks like a Res
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
91 else
92 next_range.count = 1;
93
94 curr_range = next_range;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: I think hiding the logic in this block in a v
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
95 range_map_.erase(next_range_itr);
96
97 return false;
98 }
99 }
100
101 // Either curr is the last range in the map, or there is a next
102 // range beyond the curr range, but its start time is ahead of this
103 // cue's start time. In either case, this cue becomes the new
104 // last_time for the curr range. Eventually we will see a cue whose
105 // time matches the start time of the next range, in which case we
106 // coalesce the curr and next ranges.
107
108 curr_range.last_time = start_time;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: Place these assignments in an SetLastTime() m
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
109 curr_range.count = 1;
110 curr_range.max_count = 1;
111
112 return true;
113 }
114
115 void TextRanges::NewRange(base::TimeDelta start_time) {
116 Range range;
117 range.last_time = start_time;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: Use the SetLastTime() method mentioned above
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
118 range.count = 1;
119 range.max_count = 1;
120
121 typedef RangeMap::iterator Itr;
acolwell GONE FROM CHROMIUM 2014/02/05 19:09:36 nit: Please remove the typdefs and inline since th
Matthew Heaney (Chromium) 2014/02/06 02:25:48 Done.
122 typedef std::pair<Itr, bool> Result;
123
124 Result result = range_map_.insert(std::make_pair(start_time, range));
125 DCHECK(result.second);
126
127 curr_range_itr_ = result.first;
128 }
129
130 int TextRanges::Present(base::TimeDelta start_time) const {
131 if (curr_range_itr_ == range_map_.end())
132 return -1;
133
134 if (start_time < curr_range_itr_->first)
135 return 0;
136
137 const Range& r = curr_range_itr_->second;
138
139 if (start_time <= r.last_time)
140 return 1;
141
142 return 0;
143 }
144
145 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698