OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_BLINK_MULTIBUFFER_H_ | 5 #ifndef MEDIA_BLINK_MULTIBUFFER_H_ |
6 #define MEDIA_BLINK_MULTIBUFFER_H_ | 6 #define MEDIA_BLINK_MULTIBUFFER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 #include <map> | 11 #include <map> |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "media/base/data_buffer.h" | 19 #include "media/base/data_buffer.h" |
20 #include "media/blink/interval_map.h" | 20 #include "media/blink/interval_map.h" |
21 #include "media/blink/lru.h" | 21 #include "media/blink/lru.h" |
22 #include "media/blink/media_blink_export.h" | 22 #include "media/blink/media_blink_export.h" |
23 | 23 |
24 namespace media { | 24 namespace media { |
25 | 25 |
26 typedef int32_t MultiBufferBlockId; | 26 typedef int32_t MultiBufferBlockId; |
xhwang
2015/11/19 23:34:17
Add a comment?
hubbe
2015/11/20 23:24:22
Done.
| |
27 class MultiBuffer; | 27 class MultiBuffer; |
28 typedef std::pair<MultiBuffer*, MultiBufferBlockId> MultiBufferGlobalBlockId; | 28 typedef std::pair<MultiBuffer*, MultiBufferBlockId> MultiBufferGlobalBlockId; |
29 | 29 |
30 } // namespace media | 30 } // namespace media |
31 | 31 |
32 namespace BASE_HASH_NAMESPACE { | 32 namespace BASE_HASH_NAMESPACE { |
33 | 33 |
34 template <> | 34 template <> |
35 struct hash<media::MultiBufferGlobalBlockId> { | 35 struct hash<media::MultiBufferGlobalBlockId> { |
36 std::size_t operator()(const media::MultiBufferGlobalBlockId& key) const { | 36 std::size_t operator()(const media::MultiBufferGlobalBlockId& key) const { |
(...skipping 28 matching lines...) Expand all Loading... | |
65 // When new readers are added, new data providers are created if | 65 // When new readers are added, new data providers are created if |
66 // the new reader doesn't fall into the look-ahead region of | 66 // the new reader doesn't fall into the look-ahead region of |
67 // an existing data provider. | 67 // an existing data provider. |
68 | 68 |
69 // This is the size of the look-ahead region. | 69 // This is the size of the look-ahead region. |
70 const int kMaxWaitForWriterOffset = 5; | 70 const int kMaxWaitForWriterOffset = 5; |
71 | 71 |
72 // This is the size of the look-behind region. | 72 // This is the size of the look-behind region. |
73 const int kMaxWaitForReaderOffset = 50; | 73 const int kMaxWaitForReaderOffset = 50; |
74 | 74 |
75 class MultiBuffer; | 75 class MultiBuffer; |
xhwang
2015/11/19 23:34:17
not needed?
hubbe
2015/11/20 23:24:22
Done.
| |
76 | 76 |
77 // MultiBuffers are multi-reader multi-writer cache/buffers with | 77 // MultiBuffers are multi-reader multi-writer cache/buffers with |
78 // prefetching and pinning. Data is stored internally in ref-counted | 78 // prefetching and pinning. Data is stored internally in ref-counted |
79 // blocks of identical size. |block_size_shift| is log2 of the block | 79 // blocks of identical size. |block_size_shift| is log2 of the block |
80 // size. | 80 // size. |
81 // | 81 // |
82 // Users should inherit this class and implement CreateWriter(). | 82 // Users should inherit this class and implement CreateWriter(). |
83 // TODO(hubbe): Make the multibuffer respond to memory pressure. | 83 // TODO(hubbe): Make the multibuffer respond to memory pressure. |
84 class MEDIA_BLINK_EXPORT MultiBuffer { | 84 class MEDIA_BLINK_EXPORT MultiBuffer { |
85 public: | 85 public: |
(...skipping 26 matching lines...) Expand all Loading... | |
112 // Returns true if one (or more) blocks are | 112 // Returns true if one (or more) blocks are |
113 // availble to read. | 113 // availble to read. |
114 virtual bool Available() const = 0; | 114 virtual bool Available() const = 0; |
115 | 115 |
116 // Returns the next block. Only valid if Available() | 116 // Returns the next block. Only valid if Available() |
117 // returns true. Last block might be of a smaller size | 117 // returns true. Last block might be of a smaller size |
118 // and after the last block we will get an end-of-stream | 118 // and after the last block we will get an end-of-stream |
119 // DataBuffer. | 119 // DataBuffer. |
120 virtual scoped_refptr<DataBuffer> Read() = 0; | 120 virtual scoped_refptr<DataBuffer> Read() = 0; |
121 | 121 |
122 // |cb| is called every time Available() becomes true. | |
123 virtual void SetAvailableCallback(const base::Closure& cb) = 0; | |
124 | |
125 // Ask the data provider to stop giving us data. | 122 // Ask the data provider to stop giving us data. |
126 // It's ok if the effect is not immediate. | 123 // It's ok if the effect is not immediate. |
127 virtual void SetDeferred(bool deferred) = 0; | 124 virtual void SetDeferred(bool deferred) = 0; |
128 }; | 125 }; |
129 | 126 |
130 // Multibuffers use a global shared LRU to free memory. | 127 // Multibuffers use a global shared LRU to free memory. |
131 // This effectively means that recently used multibuffers can | 128 // This effectively means that recently used multibuffers can |
132 // borrow memory from less recently used ones. | 129 // borrow memory from less recently used ones. |
133 class MEDIA_BLINK_EXPORT GlobalLRU : public base::RefCounted<GlobalLRU> { | 130 class MEDIA_BLINK_EXPORT GlobalLRU : public base::RefCounted<GlobalLRU> { |
134 public: | 131 public: |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 // function for applying multiple changes to the pinned ranges. | 219 // function for applying multiple changes to the pinned ranges. |
223 void PinRanges(const IntervalMap<BlockId, int32_t>& ranges); | 220 void PinRanges(const IntervalMap<BlockId, int32_t>& ranges); |
224 | 221 |
225 // Increment max cache size by |size| (counted in blocks). | 222 // Increment max cache size by |size| (counted in blocks). |
226 void IncrementMaxSize(int32_t size); | 223 void IncrementMaxSize(int32_t size); |
227 | 224 |
228 // Caller takes ownership of 'provider', cache will | 225 // Caller takes ownership of 'provider', cache will |
229 // not call it anymore. | 226 // not call it anymore. |
230 scoped_ptr<DataProvider> RemoveProvider(DataProvider* provider); | 227 scoped_ptr<DataProvider> RemoveProvider(DataProvider* provider); |
231 | 228 |
232 // Add a writer to this cache. Cache takes ownership and | 229 // Add a writer to this cache. Cache takes ownership, and may |
233 // may choose to destroy it. | 230 // destroy |provider| later. (Not during this call.) |
234 void AddProvider(scoped_ptr<DataProvider> provider); | 231 void AddProvider(scoped_ptr<DataProvider> provider); |
235 | 232 |
236 // Transfer all data from |other| to this. | 233 // Transfer all data from |other| to this. |
237 void MergeFrom(MultiBuffer* other); | 234 void MergeFrom(MultiBuffer* other); |
238 | 235 |
239 // Accessors. | 236 // Accessors. |
240 const DataMap& map() const { return data_; } | 237 const DataMap& map() const { return data_; } |
241 int32_t block_size_shift() const { return block_size_shift_; } | 238 int32_t block_size_shift() const { return block_size_shift_; } |
242 | 239 |
240 // Callback which notifies us that a data provider has | |
241 // some data for us. Also called when it might be apprperiate | |
242 // for a provider in a deferred state to wake up. | |
243 void DataProviderEvent(DataProvider* provider); | |
xhwang
2015/11/19 23:34:17
In media/ callbacks are often called OnXxx, not su
hubbe
2015/11/20 23:24:23
OnDataProviderEvent() seems ok, can't think of som
| |
244 | |
243 protected: | 245 protected: |
244 // Create a new writer at |pos| and return it. | 246 // Create a new writer at |pos| and return it. |
245 // Users needs to implemement this method. | 247 // Users needs to implemement this method. |
246 virtual DataProvider* CreateWriter(const BlockId& pos) = 0; | 248 virtual DataProvider* CreateWriter(const BlockId& pos) = 0; |
xhwang
2015/11/19 23:34:17
Who owns the created Writer? If the caller owns it
hubbe
2015/11/20 23:24:22
Done.
| |
247 | 249 |
248 virtual bool RangeSupported() const = 0; | 250 virtual bool RangeSupported() const = 0; |
251 virtual void OnEmpty(); | |
249 | 252 |
250 private: | 253 private: |
251 // For testing. | 254 // For testing. |
252 friend class TestMultiBuffer; | 255 friend class TestMultiBuffer; |
253 | 256 |
254 enum ProviderState { | 257 enum ProviderState { |
255 ProviderStateDead, | 258 ProviderStateDead, |
256 ProviderStateDefer, | 259 ProviderStateDefer, |
257 ProviderStateLoad | 260 ProviderStateLoad |
258 }; | 261 }; |
(...skipping 10 matching lines...) Expand all Loading... | |
269 | 272 |
270 // Returns true if a writer at |pos| is colliding with | 273 // Returns true if a writer at |pos| is colliding with |
271 // output of another writer. | 274 // output of another writer. |
272 bool ProviderCollision(const BlockId& pos) const; | 275 bool ProviderCollision(const BlockId& pos) const; |
273 | 276 |
274 // Call NotifyAvailableRange(new_range) on all readers waiting | 277 // Call NotifyAvailableRange(new_range) on all readers waiting |
275 // for a block in |observer_range| | 278 // for a block in |observer_range| |
276 void NotifyAvailableRange(const Interval<MultiBufferBlockId>& observer_range, | 279 void NotifyAvailableRange(const Interval<MultiBufferBlockId>& observer_range, |
277 const Interval<MultiBufferBlockId>& new_range); | 280 const Interval<MultiBufferBlockId>& new_range); |
278 | 281 |
279 // Callback which notifies us that a data provider has | |
280 // some data for us. Also called when it might be apprperiate | |
281 // for a provider in a deferred state to wake up. | |
282 void DataProviderEvent(DataProvider* provider); | |
283 | |
284 // Max number of blocks. | 282 // Max number of blocks. |
285 int64_t max_size_; | 283 int64_t max_size_; |
286 | 284 |
287 // log2 of block size. | 285 // log2 of block size. |
288 int32_t block_size_shift_; | 286 int32_t block_size_shift_; |
289 | 287 |
290 // Stores the actual data. | 288 // Stores the actual data. |
291 DataMap data_; | 289 DataMap data_; |
292 | 290 |
293 // Keeps track of readers waiting for data. | 291 // Keeps track of readers waiting for data. |
(...skipping 14 matching lines...) Expand all Loading... | |
308 | 306 |
309 // present_[block] should be 1 for all blocks that are present | 307 // present_[block] should be 1 for all blocks that are present |
310 // and 0 for all blocks that are not. Used to quickly figure out | 308 // and 0 for all blocks that are not. Used to quickly figure out |
311 // ranges of available/unavailable blocks without iterating. | 309 // ranges of available/unavailable blocks without iterating. |
312 IntervalMap<BlockId, int32_t> present_; | 310 IntervalMap<BlockId, int32_t> present_; |
313 }; | 311 }; |
314 | 312 |
315 } // namespace media | 313 } // namespace media |
316 | 314 |
317 #endif // MEDIA_BLINK_MULTIBUFFER_H_ | 315 #endif // MEDIA_BLINK_MULTIBUFFER_H_ |
OLD | NEW |