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

Side by Side 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: one more compile fix 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 unified diff | Download patch
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 "media/blink/multibuffer.h"
15
16 namespace media {
17
18 // Wrapper for MultiBuffer that offers a simple byte-reading
19 // interface with prefetch.
20 class MEDIA_EXPORT MultiBufferReader
21 : NON_EXPORTED_BASE(public MultiBuffer::Reader) {
22 public:
23 // Note that |progress_callback| is guaranteed to be called if
24 // a redirect happens and the url_data is updated. Otherwise
25 // origin checks will become insecure.
26 // Users probably want to call SetMaxBuffer & SetPreload after
27 // creating the a MultiBufferReader.
28 // The progress callback will be called when the "available range"
29 // changes. (The number of bytes available for reading before and
30 // after the current position.) The arguments for the progress
31 // callback is the first byte available (from beginning of file)
32 // and the last byte available + 1. Note that there may be other
33 // regions of available data in the cache as well.
34 MultiBufferReader(MultiBuffer* multibuffer,
35 MultiBufferUrlData url_data,
36 int64_t start,
37 int64_t end,
38 base::Callback<void(int64_t, int64_t)> progress_callback);
39
40 ~MultiBufferReader() override;
41
42 // Returns number of bytes available for reading. When the rest of the file
43 // is available, the number returned will be greater than the number
44 // or readable bytes. If an error occurs, -1 is returned.
45 int64_t Available() const;
46
47 // Seek to a different position.
48 // If there is a pending Wait(), it will be cancelled.
49 void Seek(int64_t pos);
50
51 // Returns the current position.
52 int64_t Tell() const { return pos_; }
53
54 // Tries to read |len| bytes and advance position.
55 // Returns number of bytes read.
56 // If there is a pending Wait(), it will be cancelled.
57 int64_t TryRead(unsigned char *data, int64_t len);
58
59 // Wait until |len| bytes are available for reading.
60 // Returns net::OK if |len| bytes are already available, otherwise it will
61 // return net::ERR_IO_PENDING and call |cb| at some point later.
62 // |len| must be smaller or equal to max_buffer_forward.
63 int Wait(int64_t len, base::Closure cb);
64
65 // Set how much data we try to preload into the cache ahead of our current
66 // location. Normally, we preload until we have preload_high bytes, then
67 // stop until we fall below preload_low bytes. Note that preload can be
68 // set higher than max_buffer_forward, but the result is usually that
69 // some blocks will be freed between the current position and the preload
70 // position.
71 void SetPreload(int64_t preload_high, int64_t preload_low);
72
73 // Change how much data we pin to the cache.
74 // The range [current_position - backward ... current_position + forward)
75 // will be locked in the cache. Calling Wait() or TryRead() with values
76 // larger than |forward| is not supported.
77 void SetMaxBuffer(int64_t backward, int64_t forward);
78
79 // Returns true if we are currently loading data.
80 bool IsLoading() const;
81
82 // Reader implementation.
83 void NotifyAvailableRange(
84 const Range<MultiBufferBlockId>& range) override;
85 void UpdateUrlData(const MultiBufferUrlData& old_url_data,
86 const MultiBufferUrlData& new_url_data) override;
87
88 // Getters
89 MultiBufferUrlData GetUrlData() const { return url_data_; }
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
97 // Returns the block for a particular byte position, rounding up.
98 MultiBufferBlockId block_ceil(int64_t byte_pos) const;
99
100 // Check if wait operation can complete now.
101 // Returns false if this object was destroyed while calling the
102 // wait callback, true otherwise.
103 bool CheckWait();
104
105 // Increment preload position if data has been added to the buffer.
106 // Returns false if this object was destroyed, true otherwise.
107 bool IncrementPreloadPos();
108
109 // The multibuffer we're wrapping, not owned.
110 MultiBuffer* multibuffer_;
111
112 // The ID of the url we're accessing.
113 MultiBufferUrlData url_data_;
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 // Hack to let us detect if we've been deleted when calling a callback.
148 bool* clear_on_delete_;
149 };
150
151 } // namespace media
152
153 #endif // MEDIA_BLINK_MULTIBUFFER_READER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698