OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // SeekableBuffer to support backward and forward seeking in a buffer for | 5 // SeekableBuffer to support backward and forward seeking in a buffer for |
6 // reading a media data source. | 6 // reading a media data source. |
7 // | 7 // |
8 // In order to support backward and forward seeking, this class buffers data in | 8 // In order to support backward and forward seeking, this class buffers data in |
9 // both backward and forward directions, the current read position can be reset | 9 // both backward and forward directions, the current read position can be reset |
10 // to anywhere in the buffered data. | 10 // to anywhere in the buffered data. |
(...skipping 17 matching lines...) Expand all Loading... |
28 // media files downloaded from network we cannot afford losing data, we can | 28 // media files downloaded from network we cannot afford losing data, we can |
29 // only advise a halt of further writing to this buffer. | 29 // only advise a halt of further writing to this buffer. |
30 // This class is not inherently thread-safe. Concurrent access must be | 30 // This class is not inherently thread-safe. Concurrent access must be |
31 // externally serialized. | 31 // externally serialized. |
32 | 32 |
33 #ifndef MEDIA_BASE_SEEKABLE_BUFFER_H_ | 33 #ifndef MEDIA_BASE_SEEKABLE_BUFFER_H_ |
34 #define MEDIA_BASE_SEEKABLE_BUFFER_H_ | 34 #define MEDIA_BASE_SEEKABLE_BUFFER_H_ |
35 | 35 |
36 #include <list> | 36 #include <list> |
37 | 37 |
38 #include "base/basictypes.h" | |
39 #include "base/memory/ref_counted.h" | 38 #include "base/memory/ref_counted.h" |
40 #include "base/time/time.h" | 39 #include "base/time/time.h" |
41 #include "media/base/media_export.h" | 40 #include "media/base/media_export.h" |
42 | 41 |
43 namespace media { | 42 namespace media { |
44 | 43 |
45 class DataBuffer; | 44 class DataBuffer; |
46 | 45 |
47 class MEDIA_EXPORT SeekableBuffer { | 46 class MEDIA_EXPORT SeekableBuffer { |
48 public: | 47 public: |
49 // Constructs an instance with |forward_capacity| and |backward_capacity|. | 48 // Constructs an instance with |forward_capacity| and |backward_capacity|. |
50 // The values are in bytes. | 49 // The values are in bytes. |
51 SeekableBuffer(int backward_capacity, int forward_capacity); | 50 SeekableBuffer(int backward_capacity, int forward_capacity); |
52 | 51 |
53 ~SeekableBuffer(); | 52 ~SeekableBuffer(); |
54 | 53 |
55 // Clears the buffer queue. | 54 // Clears the buffer queue. |
56 void Clear(); | 55 void Clear(); |
57 | 56 |
58 // Reads a maximum of |size| bytes into |data| from the current read | 57 // Reads a maximum of |size| bytes into |data| from the current read |
59 // position. Returns the number of bytes read. | 58 // position. Returns the number of bytes read. |
60 // The current read position will advance by the amount of bytes read. If | 59 // The current read position will advance by the amount of bytes read. If |
61 // reading caused backward_bytes() to exceed backward_capacity(), an eviction | 60 // reading caused backward_bytes() to exceed backward_capacity(), an eviction |
62 // of the backward buffer will be done internally. | 61 // of the backward buffer will be done internally. |
63 int Read(uint8* data, int size); | 62 int Read(uint8_t* data, int size); |
64 | 63 |
65 // Copies up to |size| bytes from current position to |data|. Returns | 64 // Copies up to |size| bytes from current position to |data|. Returns |
66 // number of bytes copied. Doesn't advance current position. Optionally | 65 // number of bytes copied. Doesn't advance current position. Optionally |
67 // starts at a |forward_offset| from current position. | 66 // starts at a |forward_offset| from current position. |
68 int Peek(uint8* data, int size) { return Peek(data, size, 0); } | 67 int Peek(uint8_t* data, int size) { return Peek(data, size, 0); } |
69 int Peek(uint8* data, int size, int forward_offset); | 68 int Peek(uint8_t* data, int size, int forward_offset); |
70 | 69 |
71 // Returns pointer to the current chunk of data that is being consumed. | 70 // Returns pointer to the current chunk of data that is being consumed. |
72 // If there is no data left in the buffer false is returned, otherwise | 71 // If there is no data left in the buffer false is returned, otherwise |
73 // true is returned and |data| and |size| are updated. The returned | 72 // true is returned and |data| and |size| are updated. The returned |
74 // |data| value becomes invalid when Read(), Append() or Seek() | 73 // |data| value becomes invalid when Read(), Append() or Seek() |
75 // are called. | 74 // are called. |
76 bool GetCurrentChunk(const uint8** data, int* size) const; | 75 bool GetCurrentChunk(const uint8_t** data, int* size) const; |
77 | 76 |
78 // Appends |buffer_in| to this buffer. Returns false if forward_bytes() is | 77 // Appends |buffer_in| to this buffer. Returns false if forward_bytes() is |
79 // greater than or equals to forward_capacity(), true otherwise. The data | 78 // greater than or equals to forward_capacity(), true otherwise. The data |
80 // is added to the buffer in any case. | 79 // is added to the buffer in any case. |
81 bool Append(const scoped_refptr<DataBuffer>& buffer_in); | 80 bool Append(const scoped_refptr<DataBuffer>& buffer_in); |
82 | 81 |
83 // Appends |size| bytes of |data| to the buffer. Result is the same | 82 // Appends |size| bytes of |data| to the buffer. Result is the same |
84 // as for Append(Buffer*). | 83 // as for Append(Buffer*). |
85 bool Append(const uint8* data, int size); | 84 bool Append(const uint8_t* data, int size); |
86 | 85 |
87 // Moves the read position by |offset| bytes. If |offset| is positive, the | 86 // Moves the read position by |offset| bytes. If |offset| is positive, the |
88 // current read position is moved forward. If negative, the current read | 87 // current read position is moved forward. If negative, the current read |
89 // position is moved backward. A zero |offset| value will keep the current | 88 // position is moved backward. A zero |offset| value will keep the current |
90 // read position stationary. | 89 // read position stationary. |
91 // If |offset| exceeds bytes buffered in either direction, reported by | 90 // If |offset| exceeds bytes buffered in either direction, reported by |
92 // forward_bytes() when seeking forward and backward_bytes() when seeking | 91 // forward_bytes() when seeking forward and backward_bytes() when seeking |
93 // backward, the seek operation will fail and return value will be false. | 92 // backward, the seek operation will fail and return value will be false. |
94 // If the seek operation fails, the current read position will not be updated. | 93 // If the seek operation fails, the current read position will not be updated. |
95 // If a forward seeking caused backward_bytes() to exceed backward_capacity(), | 94 // If a forward seeking caused backward_bytes() to exceed backward_capacity(), |
96 // this method call will cause an eviction of the backward buffer. | 95 // this method call will cause an eviction of the backward buffer. |
97 bool Seek(int32 offset); | 96 bool Seek(int32_t offset); |
98 | 97 |
99 // Returns the number of bytes buffered beyond the current read position. | 98 // Returns the number of bytes buffered beyond the current read position. |
100 int forward_bytes() const { return forward_bytes_; } | 99 int forward_bytes() const { return forward_bytes_; } |
101 | 100 |
102 // Returns the number of bytes buffered that precedes the current read | 101 // Returns the number of bytes buffered that precedes the current read |
103 // position. | 102 // position. |
104 int backward_bytes() const { return backward_bytes_; } | 103 int backward_bytes() const { return backward_bytes_; } |
105 | 104 |
106 // Sets the forward_capacity to |new_forward_capacity| bytes. | 105 // Sets the forward_capacity to |new_forward_capacity| bytes. |
107 void set_forward_capacity(int new_forward_capacity) { | 106 void set_forward_capacity(int new_forward_capacity) { |
(...skipping 27 matching lines...) Expand all Loading... |
135 | 134 |
136 // A helper method to evict buffers in the backward direction until backward | 135 // A helper method to evict buffers in the backward direction until backward |
137 // bytes is within the backward capacity. | 136 // bytes is within the backward capacity. |
138 void EvictBackwardBuffers(); | 137 void EvictBackwardBuffers(); |
139 | 138 |
140 // An internal method shared by Read() and SeekForward() that actually does | 139 // An internal method shared by Read() and SeekForward() that actually does |
141 // reading. It reads a maximum of |size| bytes into |data|. Returns the number | 140 // reading. It reads a maximum of |size| bytes into |data|. Returns the number |
142 // of bytes read. The current read position will be moved forward by the | 141 // of bytes read. The current read position will be moved forward by the |
143 // number of bytes read. If |data| is NULL, only the current read position | 142 // number of bytes read. If |data| is NULL, only the current read position |
144 // will advance but no data will be copied. | 143 // will advance but no data will be copied. |
145 int InternalRead( | 144 int InternalRead(uint8_t* data, |
146 uint8* data, int size, bool advance_position, int forward_offset); | 145 int size, |
| 146 bool advance_position, |
| 147 int forward_offset); |
147 | 148 |
148 // A helper method that moves the current read position forward by |size| | 149 // A helper method that moves the current read position forward by |size| |
149 // bytes. | 150 // bytes. |
150 // If the return value is true, the operation completed successfully. | 151 // If the return value is true, the operation completed successfully. |
151 // If the return value is false, |size| is greater than forward_bytes() and | 152 // If the return value is false, |size| is greater than forward_bytes() and |
152 // the seek operation failed. The current read position is not updated. | 153 // the seek operation failed. The current read position is not updated. |
153 bool SeekForward(int size); | 154 bool SeekForward(int size); |
154 | 155 |
155 // A helper method that moves the current read position backward by |size| | 156 // A helper method that moves the current read position backward by |size| |
156 // bytes. | 157 // bytes. |
(...skipping 19 matching lines...) Expand all Loading... |
176 // Keeps track of the most recent time we've seen in case the |buffers_| is | 177 // Keeps track of the most recent time we've seen in case the |buffers_| is |
177 // empty when our owner asks what time it is. | 178 // empty when our owner asks what time it is. |
178 base::TimeDelta current_time_; | 179 base::TimeDelta current_time_; |
179 | 180 |
180 DISALLOW_COPY_AND_ASSIGN(SeekableBuffer); | 181 DISALLOW_COPY_AND_ASSIGN(SeekableBuffer); |
181 }; | 182 }; |
182 | 183 |
183 } // namespace media | 184 } // namespace media |
184 | 185 |
185 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ | 186 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ |
OLD | NEW |