Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(357)

Unified Diff: media/blink/multibuffer_reader.h

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..99529ecd01e37d43341d22a1260188f660e87028
--- /dev/null
+++ b/media/blink/multibuffer_reader.h
@@ -0,0 +1,152 @@
+// 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 "base/callback.h"
+#include "base/memory/weak_ptr.h"
+#include "media/blink/multibuffer.h"
+
+namespace media {
+
+// Wrapper for MultiBuffer that offers a simple byte-reading
+// interface with prefetch.
+class MEDIA_EXPORT MultiBufferReader
+ : NON_EXPORTED_BASE(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,
+ 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);
+
+ // Change how much data we pin to the cache.
+ // The range [current_position - backward ... current_position + forward)
+ // will be locked in the cache. Calling Wait() or TryRead() with values
+ // larger than |forward| is not supported.
+ 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;
+
+ // Getters
+ 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.
+ void CheckWait();
+
+ // Increment preload position if data has been added to the buffer.
+ // Returns false if this object was destroyed, true otherwise.
+ void IncrementPreloadPos();
+
+ // Indirection function used to call callbacks. When we post a callback
+ // we indirect it through a weak_ptr and this function to make sure we
+ // don't call any callbacks after this object has been destroyed.
+ void Call(const base::Closure& cb) const;
+
+ // The multibuffer we're wrapping, not owned.
+ MultiBuffer* multibuffer_;
+
+ // 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_;
+
+ base::WeakPtrFactory<MultiBufferReader> weak_factory_;
+};
+
+} // namespace media
+
+#endif // MEDIA_BLINK_MULTIBUFFER_READER_H_

Powered by Google App Engine
This is Rietveld 408576698