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

Side by Side Diff: media/base/ranges.h

Issue 10704142: Merge 143765 - Ensure media's buffered ranges always have a range that includes currentTime. (Closed) Base URL: svn://svn.chromium.org/chrome/branches/1180/src/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « media/base/pipeline_unittest.cc ('k') | media/filters/chunk_demuxer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BASE_RANGES_H_ 5 #ifndef MEDIA_BASE_RANGES_H_
6 #define MEDIA_BASE_RANGES_H_ 6 #define MEDIA_BASE_RANGES_H_
7 7
8 #include <ostream> 8 #include <ostream>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/time.h"
13 #include "media/base/media_export.h" 14 #include "media/base/media_export.h"
14 15
15 namespace media { 16 namespace media {
16 17
17 // Ranges allows holding an ordered list of ranges of [start,end) intervals. 18 // Ranges allows holding an ordered list of ranges of [start,end) intervals.
18 // The canonical example use-case is holding the list of ranges of buffered 19 // The canonical example use-case is holding the list of ranges of buffered
19 // bytes or times in a <video> tag. 20 // bytes or times in a <video> tag.
20 template<class T> // Endpoint type; typically a base::TimeDelta or an int64. 21 template<class T> // Endpoint type; typically a base::TimeDelta or an int64.
21 class Ranges { 22 class Ranges {
22 public: 23 public:
23 // Allow copy & assign. 24 // Allow copy & assign.
24 25
25 // Add (start,end) to this object, coallescing overlaps as appropriate. 26 // Add (start,end) to this object, coallescing overlaps as appropriate.
26 // Returns the number of stored ranges, post coallescing. 27 // Returns the number of stored ranges, post coallescing.
27 size_t Add(T start, T end); 28 size_t Add(T start, T end);
28 29
29 // Return the number of disjoint ranges. 30 // Return the number of disjoint ranges.
30 size_t size() const; 31 size_t size() const;
31 32
32 // Return the "i"'th range's start & end (0-based). 33 // Return the "i"'th range's start & end (0-based).
33 T start(int i) const; 34 T start(int i) const;
34 T end(int i) const; 35 T end(int i) const;
35 36
36 // Clear all ranges. 37 // Clear all ranges.
37 void clear(); 38 void clear();
38 39
40 // Computes the intersection between this range and |other|.
41 Ranges<T> IntersectionWith(const Ranges<T>& other);
42
39 private: 43 private:
44 // Wrapper around DCHECK_LT allowing comparisons of operator<<'able T's.
45 void DCheckLT(const T& lhs, const T& rhs) const;
46
40 // Disjoint, in increasing order of start. 47 // Disjoint, in increasing order of start.
41 std::vector<std::pair<T, T> > ranges_; 48 std::vector<std::pair<T, T> > ranges_;
42 }; 49 };
43 50
44 ////////////////////////////////////////////////////////////////////// 51 //////////////////////////////////////////////////////////////////////
45 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!! 52 // EVERYTHING BELOW HERE IS IMPLEMENTATION DETAIL!!
46 ////////////////////////////////////////////////////////////////////// 53 //////////////////////////////////////////////////////////////////////
47 54
48 template<class T> 55 template<class T>
49 size_t Ranges<T>::Add(T start, T end) { 56 size_t Ranges<T>::Add(T start, T end) {
50 if (start == end) // Nothing to be done with empty ranges. 57 if (start == end) // Nothing to be done with empty ranges.
51 return ranges_.size(); 58 return ranges_.size();
52 59
53 DCHECK(start < end); 60 DCheckLT(start, end);
54 size_t i; 61 size_t i;
55 // Walk along the array of ranges until |start| is no longer larger than the 62 // Walk along the array of ranges until |start| is no longer larger than the
56 // current interval's end. 63 // current interval's end.
57 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) { 64 for (i = 0; i < ranges_.size() && ranges_[i].second < start; ++i) {
58 // Empty body 65 // Empty body
59 } 66 }
60 67
61 // Now we know |start| belongs in the i'th slot. 68 // Now we know |start| belongs in the i'th slot.
62 // If i is the end of the range, append new range and done. 69 // If i is the end of the range, append new range and done.
63 if (i == ranges_.size()) { 70 if (i == ranges_.size()) {
(...skipping 24 matching lines...) Expand all
88 // original loop went too far. 95 // original loop went too far.
89 while ((i + 1) < ranges_.size() && 96 while ((i + 1) < ranges_.size() &&
90 ranges_[i + 1].first <= ranges_[i].second) { 97 ranges_[i + 1].first <= ranges_[i].second) {
91 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second); 98 ranges_[i].second = std::max(ranges_[i].second, ranges_[i + 1].second);
92 ranges_.erase(ranges_.begin() + i + 1); 99 ranges_.erase(ranges_.begin() + i + 1);
93 } 100 }
94 101
95 return ranges_.size(); 102 return ranges_.size();
96 } 103 }
97 104
105 template<>
106 void Ranges<base::TimeDelta>::DCheckLT(const base::TimeDelta& lhs,
107 const base::TimeDelta& rhs) const;
108
109 template<class T>
110 void Ranges<T>::DCheckLT(const T& lhs, const T& rhs) const {
111 DCHECK_LT(lhs, rhs);
112 }
113
98 template<class T> 114 template<class T>
99 size_t Ranges<T>::size() const { 115 size_t Ranges<T>::size() const {
100 return ranges_.size(); 116 return ranges_.size();
101 } 117 }
102 118
103 template<class T> 119 template<class T>
104 T Ranges<T>::start(int i) const { 120 T Ranges<T>::start(int i) const {
105 return ranges_[i].first; 121 return ranges_[i].first;
106 } 122 }
107 123
108 template<class T> 124 template<class T>
109 T Ranges<T>::end(int i) const { 125 T Ranges<T>::end(int i) const {
110 return ranges_[i].second; 126 return ranges_[i].second;
111 } 127 }
112 128
113 template<class T> 129 template<class T>
114 void Ranges<T>::clear() { 130 void Ranges<T>::clear() {
115 ranges_.clear(); 131 ranges_.clear();
116 } 132 }
117 133
134 template<class T>
135 Ranges<T> Ranges<T>::IntersectionWith(const Ranges<T>& other) {
136 Ranges<T> result;
137
138 size_t i = 0;
139 size_t j = 0;
140
141 while (i < size() && j < other.size()) {
142 T max_start = std::max(start(i), other.start(j));
143 T min_end = std::min(end(i), other.end(j));
144
145 // Add an intersection range to the result if the ranges overlap.
146 if (max_start < min_end)
147 result.Add(max_start, min_end);
148
149 if (end(i) < other.end(j))
150 ++i;
151 else
152 ++j;
153 }
154
155 return result;
156 }
157
118 } // namespace media 158 } // namespace media
119 159
120 #endif // MEDIA_BASE_RANGES_H_ 160 #endif // MEDIA_BASE_RANGES_H_
OLDNEW
« no previous file with comments | « media/base/pipeline_unittest.cc ('k') | media/filters/chunk_demuxer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698