OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 #ifndef MEDIA_BLINK_MULTIBUFFER_H_ | |
6 #define MEDIA_BLINK_MULTIBUFFER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <limits> | |
11 #include <map> | |
12 #include <set> | |
13 | |
14 #include "base/callback.h" | |
15 #include "base/memory/ref_counted.h" | |
16 #include "media/base/data_buffer.h" | |
17 #include "media/blink/lru.h" | |
18 #include "media/blink/rangemap.h" | |
19 #include "media/blink/url_index.h" | |
20 | |
21 namespace media { | |
22 | |
23 typedef scoped_refptr<UrlData> MultiBufferUrlData; | |
24 typedef int32_t MultiBufferBlockNum; | |
25 | |
26 const int kMaxFreesPerAdd = 10; | |
DaleCurtis
2015/10/19 21:45:25
Needs comment.
hubbe
2015/10/20 00:31:39
Done.
| |
27 | |
28 // There is a simple logic for creating, destroying and deferring | |
29 // data providers. Every data provider has a look-ahead region and | |
30 // a look-behind region. If there are readers in the look-ahead | |
31 // region, we keep reading. If not, but there are readers in the | |
32 // look-behind region, we defer. If there are no readers in either | |
33 // region, we destroy the data provider. | |
34 | |
35 // When new readers are added, new data providers are created if | |
36 // the new reader doesn't fall into the look-ahead region of | |
37 // an existing data provider. | |
38 | |
39 // This is the size of the look-ahead region. | |
40 const int kMaxWaitForWriterOffset = 5; | |
41 | |
42 // This is the size of the look-behind region. | |
43 const int kMaxWaitForReaderOffset = 50; | |
44 | |
45 #define kUnknownUrlData nullptr | |
46 | |
47 // This is the key for the multibuffer cache, it's made up of an | |
48 // refcounted UrlData pointer and a block ID. | |
49 // Uninitialized MultiBufferBlockIds will have a url equal to | |
50 // kUnknownUrlData. We also use kUnknownUrlData together with | |
51 // a block id of maxint/minint to represent the maximum and | |
52 // minimum possible MultiBufferBlockId. Examples, in sorted | |
53 // order: | |
54 // { url=kUnknownUrlData, block_num=-maxint } // minimum possible value | |
55 // { url=kUnknownUrlData, block_num=0 } // uninitialized | |
56 // { url=valid, block_num=1 } // valid value | |
57 // { url=valid, block_num=10 } // valid value | |
58 // { url=kUnknownUrlData, block_num=maxint } // maximum possible value | |
59 class MEDIA_EXPORT MultiBufferBlockId { | |
60 public: | |
61 MultiBufferBlockId(); | |
62 MultiBufferBlockId(const MultiBufferBlockId& block_id); | |
63 MultiBufferBlockId(MultiBufferUrlData url_data, | |
64 MultiBufferBlockNum block_num); | |
65 ~MultiBufferBlockId(); | |
66 bool SameUrl(const MultiBufferBlockId& other) const { | |
67 return url_data_ == other.url_data_; | |
68 } | |
69 bool operator<(const MultiBufferBlockId& other) const { | |
DaleCurtis
2015/10/19 21:45:25
Chrome style typically frowns on using operator ov
hubbe
2015/10/20 00:31:39
Not that I know of.
What I have is similar to a st
DaleCurtis
2015/10/20 00:49:43
Reducing to only the operators you need to overrid
| |
70 if (SameUrl(other)) { | |
71 return block_num_ < other.block_num_; | |
72 } | |
73 return url_data_cmp() < other.url_data_cmp(); | |
74 } | |
75 bool operator>(const MultiBufferBlockId& other) const { | |
76 if (SameUrl(other)) { | |
77 return block_num_ > other.block_num_; | |
78 } | |
79 return url_data_cmp() > other.url_data_cmp(); | |
80 } | |
81 bool operator<=(const MultiBufferBlockId& other) const { | |
82 if (SameUrl(other)) { | |
83 return block_num_ <= other.block_num_; | |
84 } | |
85 return url_data_cmp() <= other.url_data_cmp(); | |
86 } | |
87 bool operator>=(const MultiBufferBlockId& other) const { | |
88 if (SameUrl(other)) { | |
89 return block_num_ >= other.block_num_; | |
90 } | |
91 return url_data_cmp() >= other.url_data_cmp(); | |
92 } | |
93 bool operator==(const MultiBufferBlockId& other) const { | |
94 return SameUrl(other) && block_num_ == other.block_num_; | |
95 } | |
96 bool operator!=(const MultiBufferBlockId& other) const { | |
97 return !SameUrl(other) || block_num_ != other.block_num_; | |
98 } | |
99 int64_t operator-(const MultiBufferBlockId& other) const { | |
100 if (SameUrl(other)) { | |
101 return block_num_ - other.block_num_; | |
102 } | |
103 // Blocks from different URLs are "infinitely" distant. | |
104 return std::numeric_limits<int64_t>::max(); | |
105 } | |
106 MultiBufferBlockId operator-(int32_t n) const { | |
107 return MultiBufferBlockId(url_data_, block_num_ - n); | |
108 } | |
109 MultiBufferBlockId operator+(int32_t n) const { | |
110 return MultiBufferBlockId(url_data_, block_num_ + n); | |
111 } | |
112 | |
113 void operator++() { | |
114 block_num_++; | |
115 } | |
116 | |
117 void operator--() { | |
118 block_num_--; | |
119 } | |
120 | |
121 MultiBufferUrlData url_data() const { return url_data_; } | |
122 MultiBufferBlockNum block_num() const { return block_num_; } | |
123 | |
124 private: | |
125 // We represent the minimum block ID as {0, 0} and the | |
126 // maximum as {0, max_int64}. This helper function lets | |
127 // us compare the URLs and get the sort order right. | |
128 uint64_t url_data_cmp() const { | |
129 DCHECK_GE(sizeof(uint64_t), sizeof(UrlData*)); | |
130 if (url_data_) return reinterpret_cast<uint64_t>(url_data_.get()); | |
131 if (block_num_ == std::numeric_limits<MultiBufferBlockNum>::max()) | |
132 return std::numeric_limits<uint64_t>::max(); | |
133 return 0; | |
134 } | |
135 MultiBufferUrlData url_data_; | |
136 MultiBufferBlockNum block_num_; | |
137 }; | |
138 | |
139 } // namespace media | |
140 | |
141 namespace std { | |
DaleCurtis
2015/10/19 21:45:25
Forbidden via style guide.
http://google-stylegui
hubbe
2015/10/20 00:31:39
I see a section that say not to forward-declare an
DaleCurtis
2015/10/20 00:49:44
The ostream operator is just for printing, right?
| |
142 MEDIA_EXPORT ostream& operator<<( | |
143 ostream& o, const media::MultiBufferBlockId& id); | |
144 | |
145 template<> | |
146 class numeric_limits<media::MultiBufferBlockId> { | |
147 public: | |
148 static media::MultiBufferBlockId min() { | |
149 return media::MultiBufferBlockId( | |
150 kUnknownUrlData, | |
151 numeric_limits<media::MultiBufferBlockNum>::min()); | |
152 } | |
153 | |
154 static media::MultiBufferBlockId max() { | |
155 return media::MultiBufferBlockId( | |
156 kUnknownUrlData, | |
157 numeric_limits<media::MultiBufferBlockNum>::max()); | |
158 } | |
159 }; | |
160 | |
161 } // namespace std | |
162 | |
163 namespace media { | |
164 | |
165 // MultiBuffers are multi-reader multi-writer cache/buffers with | |
166 // prefetching and pinning. Data is stored internally in ref-counted | |
167 // blocks of identical size. |block_size_shift| is log2 of the block | |
168 // size. | |
169 // | |
170 // Users should inherit this class and implement CreateWriter(). | |
171 // TODO(hubbe): Make the multibuffer respond to memory pressure. | |
172 class MEDIA_EXPORT MultiBuffer { | |
173 public: | |
174 | |
175 // Interface for clients wishing to read data out of this cache. | |
176 class Reader { | |
177 public: | |
178 Reader() {} | |
179 virtual ~Reader() {} | |
180 // Tell the reader that we have a new UrlData. There may be several | |
181 // reasons for this: | |
182 // o We encountered a redirect. | |
183 // o An error occured, in this case the new_url_data will be | |
184 // kUnknownUrlData | |
185 // o We discovered a better UrlData. This happens when expired data | |
186 // exists in the cache, but the expired data was found to be still | |
187 // valid after sending the first request. | |
188 // In most cases, the reader will simply update it's internal position | |
189 // and keep reading. (This usually involves calling | |
190 // RemoveReader(old position), AddReader(new position)), but in the case of | |
191 // an error, the reader will either retry or give up and go away. | |
192 virtual void UpdateUrlData(const MultiBufferUrlData& old_url_data, | |
193 const MultiBufferUrlData& new_url_data) = 0; | |
194 | |
195 // Notifies the reader that the range of available blocks has changed. | |
196 // The reader must call MultiBuffer::Observe() to activate this callback. | |
197 virtual void NotifyAvailableRange( | |
198 const Range<MultiBufferBlockId>& range) = 0; | |
199 private: | |
200 DISALLOW_COPY_AND_ASSIGN(Reader); | |
201 }; | |
202 | |
203 // DataProvider is the interface that MultiBuffer | |
204 // uses to get data into the cache. | |
205 class DataProvider { | |
206 public: | |
207 virtual ~DataProvider() {} | |
208 | |
209 // Returns the block number that is to be returned | |
210 // by the next Read() call. | |
211 virtual MultiBufferBlockId Tell() const = 0; | |
212 | |
213 // Returns true if one (or more) blocks are | |
214 // availble to read. | |
215 virtual bool Available() const = 0; | |
216 | |
217 // Returns the next block. Only valid if Available() | |
218 // returns true. Last block might be of a smaller size | |
219 // and after the last block we will get an end-of-stream | |
220 // DataBuffer. | |
221 virtual scoped_refptr<DataBuffer> Read() = 0; | |
222 | |
223 // |cb| is called every time Available() becomes true. | |
224 virtual void SetAvailableCallback(const base::Closure& cb) = 0; | |
225 | |
226 // Ask the data provider to stop giving us data. | |
227 // It's ok if the effect is not immediate. | |
228 virtual void SetDeferred(bool deferred) = 0; | |
229 }; | |
230 | |
231 explicit MultiBuffer(int32_t block_size_shift); | |
232 virtual ~MultiBuffer(); | |
233 | |
234 // Identifies a block in the cache. | |
235 // Block numbers can be calculated from byte positions as: | |
236 // block_num = byte_pos >> block_size_shift | |
237 typedef MultiBufferBlockId BlockId; | |
238 typedef std::map<BlockId, scoped_refptr<DataBuffer> > DataMap; | |
239 | |
240 // Registers a reader at the given position. | |
241 // If the cache does not already contain |pos|, it will activate | |
242 // or create data providers to make sure that the block becomes | |
243 // available soon. If |pos| is already in the cache, no action is | |
244 // taken, it simply lets the cache know that this reader is likely | |
245 // to read pos+1, pos+2.. soon. | |
246 // | |
247 // Registered readers will be notified when the available range | |
248 // at their position changes. The available range at |pos| is a range | |
249 // from A to B where: A <= |pos|, B >= |pos| and all blocks in [A..B) | |
250 // are present in the cache. When this changes, we will call | |
251 // NotifyAvailableRange() on the reader. | |
252 void AddReader(const BlockId& pos, Reader* reader); | |
253 | |
254 // Unregister a reader at block |pos|. | |
255 // Often followed by a call to AddReader(pos + 1, ...); | |
256 // Idempotent. | |
257 void RemoveReader(const BlockId& pos, Reader* reader); | |
258 | |
259 // Immediately remove writers at or before |pos| if nobody needs them. | |
260 // Note that we can't really do this in StopWaitFor(), because it's very | |
261 // likely that StopWaitFor() is immediately followed by a call to WaitFor(). | |
262 // It is also a bad idea to wait for the writers to clean themselves up when | |
263 // they try to provide unwanted data to the cache. Besides the obvoius | |
264 // inefficiency, it will also cause the http_cache to bypass the disk/memory | |
265 // cache if we have multiple simultaneous requests going against the same | |
266 // url. | |
267 void CleanupWriters(const BlockId& pos); | |
268 | |
269 // Returns true if block |pos| is available in the cache. | |
270 bool Contains(const BlockId& pos) const; | |
271 | |
272 // Returns the next unavailable block at or after |pos|. | |
273 BlockId FindNextUnavailable(const BlockId& pos) const; | |
274 | |
275 // Change the pin count for a range of data blocks. | |
276 // Note that blocks do not have to be present in the | |
277 // cache to be pinned. | |
278 // Examples: | |
279 // Pin block 3, 4 & 5: PinRange(3, 6, 1); | |
280 // Unpin block 4 & 5: PinRange(4, 6, -1); | |
281 void PinRange(const BlockId& from, const BlockId& to, int32_t howmuch); | |
282 | |
283 // Calls PinRange for each range in |ranges|, convenience | |
284 // function for applying multiple changes to the pinned ranges. | |
285 void PinRanges(const RangeMap<BlockId, int32_t>& ranges); | |
286 | |
287 // Increment max cache size by |size| (counted in blocks). | |
288 void IncrementMaxSize(int32_t size); | |
289 | |
290 // Caller takes ownership of 'provider', cache will | |
291 // not call it anymore. | |
292 scoped_ptr<DataProvider> RemoveProvider(DataProvider* provider); | |
293 | |
294 // Add a writer to this cache. Cache takes ownership and | |
295 // may choose to destroy it. | |
296 void AddProvider(scoped_ptr<DataProvider> provider); | |
297 | |
298 // Tell all reader workin on |old_url_data| about a new url. | |
299 // See Reader::UpdateUrlData for more info. | |
300 void UpdateUrlData(const MultiBufferUrlData& old_url_data, | |
301 const MultiBufferUrlData& new_url_data); | |
302 | |
303 // Accessors. | |
304 const DataMap& map() const { return data_; } | |
305 int32_t block_size_shift() const { return block_size_shift_; } | |
306 | |
307 protected: | |
308 // Create a new writer at |pos| and return it. | |
309 // Users needs to implemement this method. | |
310 virtual DataProvider* CreateWriter(const BlockId& pos) = 0; | |
311 | |
312 private: | |
313 // For testing. | |
314 friend class TestMultiBuffer; | |
315 | |
316 enum ProviderState { | |
317 ProviderStateDead, | |
318 ProviderStateDefer, | |
319 ProviderStateLoad | |
320 }; | |
321 | |
322 // Figure out what state a writer at |pos| should be in. | |
323 ProviderState SuggestProviderState(const BlockId& pos) const; | |
324 | |
325 // Returns true if a writer at |pos| is colliding with | |
326 // output of another writer. | |
327 bool ProviderCollision(const BlockId& pos) const; | |
328 | |
329 // Call NotifyAvailableRange(new_range) on all readers waiting | |
330 // for a block in |observer_range| | |
331 void NotifyAvailableRange( | |
332 const Range<MultiBufferBlockId>& observer_range, | |
333 const Range<MultiBufferBlockId>& new_range); | |
334 | |
335 // Callback which notifies us that a data provider has | |
336 // some data for us. Also called when it might be apprperiate | |
337 // for a provider in a deferred state to wake up. | |
338 void DataProviderEvent(DataProvider *provider); | |
339 | |
340 // Free elements from cache if needed and possible. | |
341 // Don't free more than |max_to_free| blocks. | |
342 // Virtual for testing purposes. | |
343 virtual void Prune(size_t max_to_free); | |
344 | |
345 // Max number of blocks. | |
346 int64_t max_size_; | |
347 | |
348 // log2 of block size. | |
349 int32_t block_size_shift_; | |
350 | |
351 // Stores the actual data. | |
352 DataMap data_; | |
353 | |
354 // Keeps track of readers waiting for data. | |
355 std::map<MultiBufferBlockId, std::set<Reader*> > readers_; | |
356 | |
357 // Keeps track of writers by their position. | |
358 // The writers are owned by this class. | |
359 std::map<BlockId, DataProvider*> writer_index_; | |
360 | |
361 // The LRU should contain all blocks which are not pinned. | |
362 LRU<BlockId> lru_; | |
363 | |
364 // Keeps track of what blocks are pinned. If block p is pinned, | |
365 // then pinned_[p] > 0. Pinned blocks cannot be freed and should not | |
366 // be present in |lru_|. | |
367 RangeMap<BlockId, int32_t> pinned_; | |
368 | |
369 // present_[block] should be 1 for all blocks that are present | |
370 // and 0 for all blocks that are not. Used to quickly figure out | |
371 // ranges of available blocks without iterating. | |
372 RangeMap<BlockId, int32_t> present_; | |
373 }; | |
374 | |
375 } // namespace media | |
376 | |
377 #endif // MEDIA_BLINK_MULTIBUFFER_H_ | |
OLD | NEW |