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

Side by Side Diff: media/blink/multibuffer_reader.h

Issue 1420883004: Multibuffer reader implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@media_cache
Patch Set: iterator fix + speed up random tests 3x Created 5 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « media/blink/media_blink.gyp ('k') | media/blink/multibuffer_reader.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // If |end| is not known, use -1.
38 MultiBufferReader(
39 MultiBuffer* multibuffer,
40 int64_t start,
41 int64_t end,
42 const base::Callback<void(int64_t, int64_t)>& progress_callback);
43
44 ~MultiBufferReader() override;
45
46 // Returns number of bytes available for reading. When the rest of the file
47 // is available, the number returned will be greater than the number
48 // or readable bytes. If an error occurs, -1 is returned.
49 int64_t Available() const;
50
51 // Seek to a different position.
52 // If there is a pending Wait(), it will be cancelled.
53 void Seek(int64_t pos);
54
55 // Returns the current position.
56 int64_t Tell() const { return pos_; }
57
58 // Tries to read |len| bytes and advance position.
59 // Returns number of bytes read.
60 // If there is a pending Wait(), it will be cancelled.
61 int64_t TryRead(uint8_t* data, int64_t len);
62
63 // Wait until |len| bytes are available for reading.
64 // Returns net::OK if |len| bytes are already available, otherwise it will
65 // return net::ERR_IO_PENDING and call |cb| at some point later.
66 // |len| must be smaller or equal to max_buffer_forward.
67 int Wait(int64_t len, const base::Closure& cb);
68
69 // Set how much data we try to preload into the cache ahead of our current
70 // location. Normally, we preload until we have preload_high bytes, then
71 // stop until we fall below preload_low bytes. Note that preload can be
72 // set higher than max_buffer_forward, but the result is usually that
73 // some blocks will be freed between the current position and the preload
74 // position.
75 void SetPreload(int64_t preload_high, int64_t preload_low);
76
77 // Change how much data we pin to the cache.
78 // The range [current_position - backward ... current_position + forward)
79 // will be locked in the cache. Calling Wait() or TryRead() with values
80 // larger than |forward| is not supported.
81 void SetMaxBuffer(int64_t backward, int64_t forward);
82
83 // Returns true if we are currently loading data.
84 bool IsLoading() const;
85
86 // Reader implementation.
87 void NotifyAvailableRange(const Interval<MultiBufferBlockId>& range) override;
88
89 // Getters
90 int64_t preload_high() const { return preload_high_; }
91 int64_t preload_low() const { return preload_low_; }
92
93 private:
94 // Returns the block for a particular byte position.
95 MultiBufferBlockId block(int64_t byte_pos) const {
96 return byte_pos >> multibuffer_->block_size_shift();
97 }
98
99 // Returns the block for a particular byte position, rounding up.
100 MultiBufferBlockId block_ceil(int64_t byte_pos) const {
101 return block(byte_pos + (1LL << multibuffer_->block_size_shift()) - 1);
102 }
103
104 // Check if wait operation can complete now.
105 void CheckWait();
106
107 // Recalculate preload_pos_ and update our entry in the multibuffer
108 // reader index. Also call CheckWait(). This function is basically
109 // called anything changes, like when we get more data or seek to
110 // a new position.
111 void UpdateInternalState();
112
113 // Indirection function used to call callbacks. When we post a callback
114 // we indirect it through a weak_ptr and this function to make sure we
115 // don't call any callbacks after this object has been destroyed.
116 void Call(const base::Closure& cb) const;
117
118 // The multibuffer we're wrapping, not owned.
119 MultiBuffer* multibuffer_;
120
121 // We're not interested in reading past this position.
122 int64_t end_;
123
124 // Defer reading once we have this much data.
125 int64_t preload_high_;
126 // Stop deferring once we have this much data.
127 int64_t preload_low_;
128
129 // Pin this much data in the cache from the current position.
130 int64_t max_buffer_forward_;
131 int64_t max_buffer_backward_;
132
133 // Current position in bytes.
134 int64_t pos_;
135
136 // [block(pos_)..preload_pos_) are known to be in the cache.
137 // preload_pos_ is only allowed to point to a filled
138 // cache position if it is equal to end_ or pos_+preload_.
139 // This is a pointer to a slot in the cache, so the unit is
140 // blocks.
141 MultiBufferBlockId preload_pos_;
142
143 // True if we've requested data from the cache by calling WaitFor().
144 bool loading_;
145
146 // When Available() > current_wait_size_ we call cb_.
147 int64_t current_wait_size_;
148 base::Closure cb_;
149
150 // Progress callback.
151 base::Callback<void(int64_t, int64_t)> progress_callback_;
152
153 base::WeakPtrFactory<MultiBufferReader> weak_factory_;
154 };
155
156 } // namespace media
157
158 #endif // MEDIA_BLINK_MULTIBUFFER_READER_H_
OLDNEW
« no previous file with comments | « media/blink/media_blink.gyp ('k') | media/blink/multibuffer_reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698