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 "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 MultiBufferReader : public MultiBuffer::Reader { | |
21 public: | |
22 // Note that |progress_callback| is guaranteed to be called if | |
23 // a redirect happens and the url_data is updated. Otherwise | |
24 // origin checks will become insecure. | |
25 // Users probably want to call SetMaxBuffer & SetPreload after | |
26 // creating the a MultiBufferReader. | |
27 // The progress callback will be called when the "available range" | |
28 // changes. (The number of bytes available for reading before and | |
29 // after the current position.) The arguments for the progress | |
30 // callback is the first byte available (from beginning of file) | |
31 // and the last byte available + 1. Note that there may be other | |
32 // regions of available data in the cache as well. | |
33 MultiBufferReader(MultiBuffer* multibuffer, | |
34 MultiBufferUrlData url_data, | |
35 int64_t start, | |
36 int64_t end, | |
37 base::Callback<void(int64_t, int64_t)> progress_callback); | |
38 | |
39 ~MultiBufferReader() override; | |
40 | |
41 // Returns number of bytes available for reading. When the rest of the file | |
42 // is available, the number returned will be greater than the number | |
43 // or readable bytes. If an error occurs, -1 is returned. | |
44 int64_t Available() const; | |
45 | |
46 // Seek to a different position. | |
47 // If there is a pending Wait(), it will be cancelled. | |
48 void Seek(int64_t pos); | |
49 | |
50 // Returns the current position. | |
51 int64_t Tell() const { return pos_; } | |
52 | |
53 // Tries to read |len| bytes and advance position. | |
54 // Returns number of bytes read. | |
55 // If there is a pending Wait(), it will be cancelled. | |
56 int64_t TryRead(unsigned char *data, int64_t len); | |
57 | |
58 // Wait until |len| bytes are available for reading. | |
59 // Returns net::OK if |len| bytes are already available, otherwise it will | |
60 // return net::ERR_IO_PENDING and call |cb| at some point later. | |
61 // |len| must be smaller or equal to max_buffer_forward. | |
62 int Wait(int64_t len, base::Closure cb); | |
63 | |
64 // Set how much data we try to preload into the cache ahead of our current | |
65 // location. Normally, we preload until we have preload_high bytes, then | |
66 // stop until we fall below preload_low bytes. Note that preload can be | |
67 // set higher than max_buffer_forward, but the result is usually that | |
68 // some blocks will be freed between the current position and the preload | |
69 // position. | |
70 void SetPreload(int64_t preload_high, int64_t preload_low); | |
71 | |
72 // 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.
| |
73 void SetMaxBuffer(int64_t backward, int64_t forward); | |
74 | |
75 // Returns true if we are currently loading data. | |
76 bool IsLoading() const; | |
77 | |
78 // Reader implementation. | |
79 void NotifyAvailableRange( | |
80 const Range<MultiBufferBlockId>& range) override; | |
81 void UpdateUrlData(const MultiBufferUrlData& old_url_data, | |
82 const MultiBufferUrlData& new_url_data) override; | |
83 | |
84 // Getters | |
85 MultiBufferUrlData GetUrlData() const { return url_data_; } | |
86 int64_t preload_high() const { return preload_high_; } | |
87 int64_t preload_low() const { return preload_low_; } | |
88 | |
89 private: | |
90 // Returns the block for a particular byte position. | |
91 MultiBufferBlockId block(int64_t byte_pos) const; | |
92 | |
93 // Returns the block for a particular byte position, rounding up. | |
94 MultiBufferBlockId block_ceil(int64_t byte_pos) const; | |
95 | |
96 // Check if wait operation can complete now. | |
97 // Returns false if this object was destroyed while calling the | |
98 // wait callback, true otherwise. | |
99 bool CheckWait(); | |
100 | |
101 // Increment preload position if data has been added to the buffer. | |
102 // Returns false if this object was destroyed, true otherwise. | |
103 bool IncrementPreloadPos(); | |
104 | |
105 // The ID of the url we're accessing. | |
106 MultiBufferUrlData url_data_; | |
107 | |
108 // We're not interested in reading past this position. | |
109 int64_t end_; | |
110 | |
111 // Defer reading once we have this much data. | |
112 int64_t preload_high_; | |
113 // Stop deferring once we have this much data. | |
114 int64_t preload_low_; | |
115 | |
116 // Pin this much data in the cache from the current position. | |
117 int64_t max_buffer_forward_; | |
118 int64_t max_buffer_backward_; | |
119 | |
120 // Current position in bytes. | |
121 int64_t pos_; | |
122 | |
123 // [block(pos_)..preload_pos_) are known to be in the cache. | |
124 // preload_pos_ is only allowed to point to a filled | |
125 // cache position if it is equal to end_ or pos_+preload_. | |
126 // This is a pointer to a slot in the cache, so the unit is | |
127 // blocks. | |
128 MultiBufferBlockId preload_pos_; | |
129 | |
130 // True if we've requested data from the cache by calling WaitFor(). | |
131 bool loading_; | |
132 | |
133 // When Available() > current_wait_size_ we call cb_. | |
134 int64_t current_wait_size_; | |
135 base::Closure cb_; | |
136 | |
137 // Progress callback. | |
138 base::Callback<void(int64_t, int64_t)> progress_callback_; | |
139 | |
140 // Hack to let us detect if we've been deleted when calling a callback. | |
141 bool* clear_on_delete_; | |
142 | |
143 // The multibuffer we're wrapping, not owned. | |
144 MultiBuffer* multibuffer_; | |
145 }; | |
146 | |
147 } // namespace media | |
148 | |
149 #endif // MEDIA_BLINK_MULTIBUFFER_READER_H_ | |
OLD | NEW |