Chromium Code Reviews| Index: media/blink/multibuffer_reader.h |
| diff --git a/media/blink/multibuffer_reader.h b/media/blink/multibuffer_reader.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d933ed0a3c5c8881db513f54f5060fcdd67b11a8 |
| --- /dev/null |
| +++ b/media/blink/multibuffer_reader.h |
| @@ -0,0 +1,149 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MEDIA_BLINK_MULTIBUFFER_READER_H_ |
| +#define MEDIA_BLINK_MULTIBUFFER_READER_H_ |
| + |
| +#include <stdint.h> |
| + |
| +#include <limits> |
| +#include <map> |
| +#include <set> |
| + |
| +#include "media/blink/multibuffer.h" |
| + |
| +namespace media { |
| + |
| +// Wrapper for MultiBuffer that offers a simple byte-reading |
| +// interface with prefetch. |
| +class MultiBufferReader : public MultiBuffer::Reader { |
| + public: |
| + // Note that |progress_callback| is guaranteed to be called if |
| + // a redirect happens and the url_data is updated. Otherwise |
| + // origin checks will become insecure. |
| + // Users probably want to call SetMaxBuffer & SetPreload after |
| + // creating the a MultiBufferReader. |
| + // The progress callback will be called when the "available range" |
| + // changes. (The number of bytes available for reading before and |
| + // after the current position.) The arguments for the progress |
| + // callback is the first byte available (from beginning of file) |
| + // and the last byte available + 1. Note that there may be other |
| + // regions of available data in the cache as well. |
| + MultiBufferReader(MultiBuffer* multibuffer, |
| + MultiBufferUrlData url_data, |
| + int64_t start, |
| + int64_t end, |
| + base::Callback<void(int64_t, int64_t)> progress_callback); |
| + |
| + ~MultiBufferReader() override; |
| + |
| + // Returns number of bytes available for reading. When the rest of the file |
| + // is available, the number returned will be greater than the number |
| + // or readable bytes. If an error occurs, -1 is returned. |
| + int64_t Available() const; |
| + |
| + // Seek to a different position. |
| + // If there is a pending Wait(), it will be cancelled. |
| + void Seek(int64_t pos); |
| + |
| + // Returns the current position. |
| + int64_t Tell() const { return pos_; } |
| + |
| + // Tries to read |len| bytes and advance position. |
| + // Returns number of bytes read. |
| + // If there is a pending Wait(), it will be cancelled. |
| + int64_t TryRead(unsigned char *data, int64_t len); |
| + |
| + // Wait until |len| bytes are available for reading. |
| + // Returns net::OK if |len| bytes are already available, otherwise it will |
| + // return net::ERR_IO_PENDING and call |cb| at some point later. |
| + // |len| must be smaller or equal to max_buffer_forward. |
| + int Wait(int64_t len, base::Closure cb); |
| + |
| + // Set how much data we try to preload into the cache ahead of our current |
| + // location. Normally, we preload until we have preload_high bytes, then |
| + // stop until we fall below preload_low bytes. Note that preload can be |
| + // set higher than max_buffer_forward, but the result is usually that |
| + // some blocks will be freed between the current position and the preload |
| + // position. |
| + void SetPreload(int64_t preload_high, int64_t preload_low); |
| + |
| + // Adjusts max_buffer_forward and max_buffer_backsard. |
|
liberato (no reviews please)
2015/10/14 14:51:34
one will ask "what is max_buffer_*". might as wel
hubbe
2015/10/16 00:17:44
Comment updated.
|
| + void SetMaxBuffer(int64_t backward, int64_t forward); |
| + |
| + // Returns true if we are currently loading data. |
| + bool IsLoading() const; |
| + |
| + // Reader implementation. |
| + void NotifyAvailableRange( |
| + const Range<MultiBufferBlockId>& range) override; |
| + void UpdateUrlData(const MultiBufferUrlData& old_url_data, |
| + const MultiBufferUrlData& new_url_data) override; |
| + |
| + // Getters |
| + MultiBufferUrlData GetUrlData() const { return url_data_; } |
| + int64_t preload_high() const { return preload_high_; } |
| + int64_t preload_low() const { return preload_low_; } |
| + |
| + private: |
| + // Returns the block for a particular byte position. |
| + MultiBufferBlockId block(int64_t byte_pos) const; |
| + |
| + // Returns the block for a particular byte position, rounding up. |
| + MultiBufferBlockId block_ceil(int64_t byte_pos) const; |
| + |
| + // Check if wait operation can complete now. |
| + // Returns false if this object was destroyed while calling the |
| + // wait callback, true otherwise. |
| + bool CheckWait(); |
| + |
| + // Increment preload position if data has been added to the buffer. |
| + // Returns false if this object was destroyed, true otherwise. |
| + bool IncrementPreloadPos(); |
| + |
| + // The ID of the url we're accessing. |
| + MultiBufferUrlData url_data_; |
| + |
| + // We're not interested in reading past this position. |
| + int64_t end_; |
| + |
| + // Defer reading once we have this much data. |
| + int64_t preload_high_; |
| + // Stop deferring once we have this much data. |
| + int64_t preload_low_; |
| + |
| + // Pin this much data in the cache from the current position. |
| + int64_t max_buffer_forward_; |
| + int64_t max_buffer_backward_; |
| + |
| + // Current position in bytes. |
| + int64_t pos_; |
| + |
| + // [block(pos_)..preload_pos_) are known to be in the cache. |
| + // preload_pos_ is only allowed to point to a filled |
| + // cache position if it is equal to end_ or pos_+preload_. |
| + // This is a pointer to a slot in the cache, so the unit is |
| + // blocks. |
| + MultiBufferBlockId preload_pos_; |
| + |
| + // True if we've requested data from the cache by calling WaitFor(). |
| + bool loading_; |
| + |
| + // When Available() > current_wait_size_ we call cb_. |
| + int64_t current_wait_size_; |
| + base::Closure cb_; |
| + |
| + // Progress callback. |
| + base::Callback<void(int64_t, int64_t)> progress_callback_; |
| + |
| + // Hack to let us detect if we've been deleted when calling a callback. |
| + bool* clear_on_delete_; |
| + |
| + // The multibuffer we're wrapping, not owned. |
| + MultiBuffer* multibuffer_; |
| +}; |
| + |
| +} // namespace media |
| + |
| +#endif // MEDIA_BLINK_MULTIBUFFER_READER_H_ |