| 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 27 matching lines...) Expand all Loading... |
| 38 #include "base/basictypes.h" | 38 #include "base/basictypes.h" |
| 39 #include "base/memory/ref_counted.h" | 39 #include "base/memory/ref_counted.h" |
| 40 #include "media/base/buffers.h" | 40 #include "media/base/buffers.h" |
| 41 | 41 |
| 42 namespace media { | 42 namespace media { |
| 43 | 43 |
| 44 class MEDIA_EXPORT SeekableBuffer { | 44 class MEDIA_EXPORT SeekableBuffer { |
| 45 public: | 45 public: |
| 46 // Constructs an instance with |forward_capacity| and |backward_capacity|. | 46 // Constructs an instance with |forward_capacity| and |backward_capacity|. |
| 47 // The values are in bytes. | 47 // The values are in bytes. |
| 48 SeekableBuffer(size_t backward_capacity, size_t forward_capacity); | 48 SeekableBuffer(int backward_capacity, int forward_capacity); |
| 49 | 49 |
| 50 ~SeekableBuffer(); | 50 ~SeekableBuffer(); |
| 51 | 51 |
| 52 // Clears the buffer queue. | 52 // Clears the buffer queue. |
| 53 void Clear(); | 53 void Clear(); |
| 54 | 54 |
| 55 // Reads a maximum of |size| bytes into |data| from the current read | 55 // Reads a maximum of |size| bytes into |data| from the current read |
| 56 // position. Returns the number of bytes read. | 56 // position. Returns the number of bytes read. |
| 57 // The current read position will advance by the amount of bytes read. If | 57 // The current read position will advance by the amount of bytes read. If |
| 58 // reading caused backward_bytes() to exceed backward_capacity(), an eviction | 58 // reading caused backward_bytes() to exceed backward_capacity(), an eviction |
| 59 // of the backward buffer will be done internally. | 59 // of the backward buffer will be done internally. |
| 60 size_t Read(uint8* data, size_t size); | 60 int Read(uint8* data, int size); |
| 61 | 61 |
| 62 // Copies up to |size| bytes from current position to |data|. Returns | 62 // Copies up to |size| bytes from current position to |data|. Returns |
| 63 // number of bytes copied. Doesn't advance current position. Optionally | 63 // number of bytes copied. Doesn't advance current position. Optionally |
| 64 // starts at a |forward_offset| from current position. | 64 // starts at a |forward_offset| from current position. |
| 65 size_t Peek(uint8* data, size_t size) { return Peek(data, size, 0); } | 65 int Peek(uint8* data, int size) { return Peek(data, size, 0); } |
| 66 size_t Peek(uint8* data, size_t size, size_t forward_offset); | 66 int Peek(uint8* data, int size, int forward_offset); |
| 67 | 67 |
| 68 // Returns pointer to the current chunk of data that is being consumed. | 68 // Returns pointer to the current chunk of data that is being consumed. |
| 69 // If there is no data left in the buffer false is returned, otherwise | 69 // If there is no data left in the buffer false is returned, otherwise |
| 70 // true is returned and |data| and |size| are updated. The returned | 70 // true is returned and |data| and |size| are updated. The returned |
| 71 // |data| value becomes invalid when Read(), Append() or Seek() | 71 // |data| value becomes invalid when Read(), Append() or Seek() |
| 72 // are called. | 72 // are called. |
| 73 bool GetCurrentChunk(const uint8** data, size_t* size) const; | 73 bool GetCurrentChunk(const uint8** data, int* size) const; |
| 74 | 74 |
| 75 // Appends |buffer_in| to this buffer. Returns false if forward_bytes() is | 75 // Appends |buffer_in| to this buffer. Returns false if forward_bytes() is |
| 76 // greater than or equals to forward_capacity(), true otherwise. The data | 76 // greater than or equals to forward_capacity(), true otherwise. The data |
| 77 // is added to the buffer in any case. | 77 // is added to the buffer in any case. |
| 78 bool Append(Buffer* buffer_in); | 78 bool Append(Buffer* buffer_in); |
| 79 | 79 |
| 80 // Appends |size| bytes of |data| to the buffer. Result is the same | 80 // Appends |size| bytes of |data| to the buffer. Result is the same |
| 81 // as for Append(Buffer*). | 81 // as for Append(Buffer*). |
| 82 bool Append(const uint8* data, size_t size); | 82 bool Append(const uint8* data, int size); |
| 83 | 83 |
| 84 // Moves the read position by |offset| bytes. If |offset| is positive, the | 84 // Moves the read position by |offset| bytes. If |offset| is positive, the |
| 85 // current read position is moved forward. If negative, the current read | 85 // current read position is moved forward. If negative, the current read |
| 86 // position is moved backward. A zero |offset| value will keep the current | 86 // position is moved backward. A zero |offset| value will keep the current |
| 87 // read position stationary. | 87 // read position stationary. |
| 88 // If |offset| exceeds bytes buffered in either direction, reported by | 88 // If |offset| exceeds bytes buffered in either direction, reported by |
| 89 // forward_bytes() when seeking forward and backward_bytes() when seeking | 89 // forward_bytes() when seeking forward and backward_bytes() when seeking |
| 90 // backward, the seek operation will fail and return value will be false. | 90 // backward, the seek operation will fail and return value will be false. |
| 91 // If the seek operation fails, the current read position will not be updated. | 91 // If the seek operation fails, the current read position will not be updated. |
| 92 // If a forward seeking caused backward_bytes() to exceed backward_capacity(), | 92 // If a forward seeking caused backward_bytes() to exceed backward_capacity(), |
| 93 // this method call will cause an eviction of the backward buffer. | 93 // this method call will cause an eviction of the backward buffer. |
| 94 bool Seek(int32 offset); | 94 bool Seek(int32 offset); |
| 95 | 95 |
| 96 // Returns the number of bytes buffered beyond the current read position. | 96 // Returns the number of bytes buffered beyond the current read position. |
| 97 size_t forward_bytes() const { return forward_bytes_; } | 97 int forward_bytes() const { return forward_bytes_; } |
| 98 | 98 |
| 99 // Returns the number of bytes buffered that precedes the current read | 99 // Returns the number of bytes buffered that precedes the current read |
| 100 // position. | 100 // position. |
| 101 size_t backward_bytes() const { return backward_bytes_; } | 101 int backward_bytes() const { return backward_bytes_; } |
| 102 | 102 |
| 103 // Sets the forward_capacity to |new_forward_capacity| bytes. | 103 // Sets the forward_capacity to |new_forward_capacity| bytes. |
| 104 void set_forward_capacity(size_t new_forward_capacity) { | 104 void set_forward_capacity(int new_forward_capacity) { |
| 105 forward_capacity_ = new_forward_capacity; | 105 forward_capacity_ = new_forward_capacity; |
| 106 } | 106 } |
| 107 | 107 |
| 108 // Sets the backward_capacity to |new_backward_capacity| bytes. | 108 // Sets the backward_capacity to |new_backward_capacity| bytes. |
| 109 void set_backward_capacity(size_t new_backward_capacity) { | 109 void set_backward_capacity(int new_backward_capacity) { |
| 110 backward_capacity_ = new_backward_capacity; | 110 backward_capacity_ = new_backward_capacity; |
| 111 } | 111 } |
| 112 | 112 |
| 113 // Returns the maximum number of bytes that should be kept in the forward | 113 // Returns the maximum number of bytes that should be kept in the forward |
| 114 // direction. | 114 // direction. |
| 115 size_t forward_capacity() const { return forward_capacity_; } | 115 int forward_capacity() const { return forward_capacity_; } |
| 116 | 116 |
| 117 // Returns the maximum number of bytes that should be kept in the backward | 117 // Returns the maximum number of bytes that should be kept in the backward |
| 118 // direction. | 118 // direction. |
| 119 size_t backward_capacity() const { return backward_capacity_; } | 119 int backward_capacity() const { return backward_capacity_; } |
| 120 | 120 |
| 121 // Returns the current timestamp, taking into account current offset. The | 121 // Returns the current timestamp, taking into account current offset. The |
| 122 // value calculated based on the timestamp of the current buffer. If | 122 // value calculated based on the timestamp of the current buffer. If |
| 123 // timestamp for the current buffer is set to 0 or the data was added with | 123 // timestamp for the current buffer is set to 0 or the data was added with |
| 124 // Append(const uint*, size_t), then returns value that corresponds to the | 124 // Append(const uint*, int), then returns value that corresponds to the |
| 125 // last position in a buffer that had timestamp set. | 125 // last position in a buffer that had timestamp set. |
| 126 // kNoTimestamp() is returned if no buffers we read from had timestamp set. | 126 // kNoTimestamp() is returned if no buffers we read from had timestamp set. |
| 127 base::TimeDelta current_time() const { return current_time_; } | 127 base::TimeDelta current_time() const { return current_time_; } |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 // Definition of the buffer queue. | 130 // Definition of the buffer queue. |
| 131 typedef std::list<scoped_refptr<Buffer> > BufferQueue; | 131 typedef std::list<scoped_refptr<Buffer> > BufferQueue; |
| 132 | 132 |
| 133 // A helper method to evict buffers in the backward direction until backward | 133 // A helper method to evict buffers in the backward direction until backward |
| 134 // bytes is within the backward capacity. | 134 // bytes is within the backward capacity. |
| 135 void EvictBackwardBuffers(); | 135 void EvictBackwardBuffers(); |
| 136 | 136 |
| 137 // An internal method shared by Read() and SeekForward() that actually does | 137 // An internal method shared by Read() and SeekForward() that actually does |
| 138 // reading. It reads a maximum of |size| bytes into |data|. Returns the number | 138 // reading. It reads a maximum of |size| bytes into |data|. Returns the number |
| 139 // of bytes read. The current read position will be moved forward by the | 139 // of bytes read. The current read position will be moved forward by the |
| 140 // number of bytes read. If |data| is NULL, only the current read position | 140 // number of bytes read. If |data| is NULL, only the current read position |
| 141 // will advance but no data will be copied. | 141 // will advance but no data will be copied. |
| 142 size_t InternalRead( | 142 int InternalRead( |
| 143 uint8* data, size_t size, bool advance_position, size_t forward_offset); | 143 uint8* data, int size, bool advance_position, int forward_offset); |
| 144 | 144 |
| 145 // A helper method that moves the current read position forward by |size| | 145 // A helper method that moves the current read position forward by |size| |
| 146 // bytes. | 146 // bytes. |
| 147 // If the return value is true, the operation completed successfully. | 147 // If the return value is true, the operation completed successfully. |
| 148 // If the return value is false, |size| is greater than forward_bytes() and | 148 // If the return value is false, |size| is greater than forward_bytes() and |
| 149 // the seek operation failed. The current read position is not updated. | 149 // the seek operation failed. The current read position is not updated. |
| 150 bool SeekForward(size_t size); | 150 bool SeekForward(int size); |
| 151 | 151 |
| 152 // A helper method that moves the current read position backward by |size| | 152 // A helper method that moves the current read position backward by |size| |
| 153 // bytes. | 153 // bytes. |
| 154 // If the return value is true, the operation completed successfully. | 154 // If the return value is true, the operation completed successfully. |
| 155 // If the return value is false, |size| is greater than backward_bytes() and | 155 // If the return value is false, |size| is greater than backward_bytes() and |
| 156 // the seek operation failed. The current read position is not updated. | 156 // the seek operation failed. The current read position is not updated. |
| 157 bool SeekBackward(size_t size); | 157 bool SeekBackward(int size); |
| 158 | 158 |
| 159 // Updates |current_time_| with the time that corresponds to the | 159 // Updates |current_time_| with the time that corresponds to the |
| 160 // specified position in the buffer. | 160 // specified position in the buffer. |
| 161 void UpdateCurrentTime(BufferQueue::iterator buffer, size_t offset); | 161 void UpdateCurrentTime(BufferQueue::iterator buffer, int offset); |
| 162 | 162 |
| 163 BufferQueue::iterator current_buffer_; | 163 BufferQueue::iterator current_buffer_; |
| 164 BufferQueue buffers_; | 164 BufferQueue buffers_; |
| 165 size_t current_buffer_offset_; | 165 int current_buffer_offset_; |
| 166 | 166 |
| 167 size_t backward_capacity_; | 167 int backward_capacity_; |
| 168 size_t backward_bytes_; | 168 int backward_bytes_; |
| 169 | 169 |
| 170 size_t forward_capacity_; | 170 int forward_capacity_; |
| 171 size_t forward_bytes_; | 171 int forward_bytes_; |
| 172 | 172 |
| 173 // Keeps track of the most recent time we've seen in case the |buffers_| is | 173 // Keeps track of the most recent time we've seen in case the |buffers_| is |
| 174 // empty when our owner asks what time it is. | 174 // empty when our owner asks what time it is. |
| 175 base::TimeDelta current_time_; | 175 base::TimeDelta current_time_; |
| 176 | 176 |
| 177 DISALLOW_COPY_AND_ASSIGN(SeekableBuffer); | 177 DISALLOW_COPY_AND_ASSIGN(SeekableBuffer); |
| 178 }; | 178 }; |
| 179 | 179 |
| 180 } // namespace media | 180 } // namespace media |
| 181 | 181 |
| 182 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ | 182 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ |
| OLD | NEW |