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_READER_H_ |
| 6 #define MEDIA_BLINK_MULTIBUFFER_READER_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/weak_ptr.h" |
| 16 #include "media/blink/multibuffer.h" |
| 17 |
| 18 namespace media { |
| 19 |
| 20 // Wrapper for MultiBuffer that offers a simple byte-reading |
| 21 // interface with prefetch. |
| 22 class MEDIA_EXPORT MultiBufferReader |
| 23 : NON_EXPORTED_BASE(public MultiBuffer::Reader) { |
| 24 public: |
| 25 // Note that |progress_callback| is guaranteed to be called if |
| 26 // a redirect happens and the url_data is updated. Otherwise |
| 27 // origin checks will become insecure. |
| 28 // Users probably want to call SetMaxBuffer & SetPreload after |
| 29 // creating the a MultiBufferReader. |
| 30 // The progress callback will be called when the "available range" |
| 31 // changes. (The number of bytes available for reading before and |
| 32 // after the current position.) The arguments for the progress |
| 33 // callback is the first byte available (from beginning of file) |
| 34 // and the last byte available + 1. Note that there may be other |
| 35 // regions of available data in the cache as well. |
| 36 MultiBufferReader(MultiBuffer* multibuffer, |
| 37 int64_t start, |
| 38 int64_t end, |
| 39 base::Callback<void(int64_t, int64_t)> progress_callback); |
| 40 |
| 41 ~MultiBufferReader() override; |
| 42 |
| 43 // Returns number of bytes available for reading. When the rest of the file |
| 44 // is available, the number returned will be greater than the number |
| 45 // or readable bytes. If an error occurs, -1 is returned. |
| 46 int64_t Available() const; |
| 47 |
| 48 // Seek to a different position. |
| 49 // If there is a pending Wait(), it will be cancelled. |
| 50 void Seek(int64_t pos); |
| 51 |
| 52 // Returns the current position. |
| 53 int64_t Tell() const { return pos_; } |
| 54 |
| 55 // Tries to read |len| bytes and advance position. |
| 56 // Returns number of bytes read. |
| 57 // If there is a pending Wait(), it will be cancelled. |
| 58 int64_t TryRead(unsigned char *data, int64_t len); |
| 59 |
| 60 // Wait until |len| bytes are available for reading. |
| 61 // Returns net::OK if |len| bytes are already available, otherwise it will |
| 62 // return net::ERR_IO_PENDING and call |cb| at some point later. |
| 63 // |len| must be smaller or equal to max_buffer_forward. |
| 64 int Wait(int64_t len, base::Closure cb); |
| 65 |
| 66 // Set how much data we try to preload into the cache ahead of our current |
| 67 // location. Normally, we preload until we have preload_high bytes, then |
| 68 // stop until we fall below preload_low bytes. Note that preload can be |
| 69 // set higher than max_buffer_forward, but the result is usually that |
| 70 // some blocks will be freed between the current position and the preload |
| 71 // position. |
| 72 void SetPreload(int64_t preload_high, int64_t preload_low); |
| 73 |
| 74 // Change how much data we pin to the cache. |
| 75 // The range [current_position - backward ... current_position + forward) |
| 76 // will be locked in the cache. Calling Wait() or TryRead() with values |
| 77 // larger than |forward| is not supported. |
| 78 void SetMaxBuffer(int64_t backward, int64_t forward); |
| 79 |
| 80 // Returns true if we are currently loading data. |
| 81 bool IsLoading() const; |
| 82 |
| 83 // Reader implementation. |
| 84 void NotifyAvailableRange( |
| 85 const Range<MultiBufferBlockId>& range) override; |
| 86 |
| 87 // Getters |
| 88 int64_t preload_high() const { return preload_high_; } |
| 89 int64_t preload_low() const { return preload_low_; } |
| 90 |
| 91 private: |
| 92 // Returns the block for a particular byte position. |
| 93 MultiBufferBlockId block(int64_t byte_pos) const; |
| 94 |
| 95 // Returns the block for a particular byte position, rounding up. |
| 96 MultiBufferBlockId block_ceil(int64_t byte_pos) const; |
| 97 |
| 98 // Check if wait operation can complete now. |
| 99 // Returns false if this object was destroyed while calling the |
| 100 // wait callback, true otherwise. |
| 101 void CheckWait(); |
| 102 |
| 103 // Increment preload position if data has been added to the buffer. |
| 104 // Returns false if this object was destroyed, true otherwise. |
| 105 void IncrementPreloadPos(); |
| 106 |
| 107 // Indirection function used to call callbacks. When we post a callback |
| 108 // we indirect it through a weak_ptr and this function to make sure we |
| 109 // don't call any callbacks after this object has been destroyed. |
| 110 void Call(const base::Closure& cb) const; |
| 111 |
| 112 // The multibuffer we're wrapping, not owned. |
| 113 MultiBuffer* multibuffer_; |
| 114 |
| 115 // We're not interested in reading past this position. |
| 116 int64_t end_; |
| 117 |
| 118 // Defer reading once we have this much data. |
| 119 int64_t preload_high_; |
| 120 // Stop deferring once we have this much data. |
| 121 int64_t preload_low_; |
| 122 |
| 123 // Pin this much data in the cache from the current position. |
| 124 int64_t max_buffer_forward_; |
| 125 int64_t max_buffer_backward_; |
| 126 |
| 127 // Current position in bytes. |
| 128 int64_t pos_; |
| 129 |
| 130 // [block(pos_)..preload_pos_) are known to be in the cache. |
| 131 // preload_pos_ is only allowed to point to a filled |
| 132 // cache position if it is equal to end_ or pos_+preload_. |
| 133 // This is a pointer to a slot in the cache, so the unit is |
| 134 // blocks. |
| 135 MultiBufferBlockId preload_pos_; |
| 136 |
| 137 // True if we've requested data from the cache by calling WaitFor(). |
| 138 bool loading_; |
| 139 |
| 140 // When Available() > current_wait_size_ we call cb_. |
| 141 int64_t current_wait_size_; |
| 142 base::Closure cb_; |
| 143 |
| 144 // Progress callback. |
| 145 base::Callback<void(int64_t, int64_t)> progress_callback_; |
| 146 |
| 147 base::WeakPtrFactory<MultiBufferReader> weak_factory_; |
| 148 }; |
| 149 |
| 150 } // namespace media |
| 151 |
| 152 #endif // MEDIA_BLINK_MULTIBUFFER_READER_H_ |
OLD | NEW |