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