| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 25 matching lines...) Expand all Loading... |
| 36 #include <list> | 36 #include <list> |
| 37 | 37 |
| 38 #include "base/basictypes.h" | 38 #include "base/basictypes.h" |
| 39 #include "base/lock.h" | 39 #include "base/lock.h" |
| 40 #include "base/scoped_ptr.h" | 40 #include "base/scoped_ptr.h" |
| 41 | 41 |
| 42 namespace media { | 42 namespace media { |
| 43 | 43 |
| 44 class SeekableBuffer { | 44 class SeekableBuffer { |
| 45 public: | 45 public: |
| 46 // Construct an instance with forward capacity of |forward_capacity| | 46 // Constructs an instance with |forward_capacity| and |backward_capacity|. |
| 47 // and backward capacity of |backward_capacity|. The values are in bytes. | 47 // The values are in bytes. |
| 48 SeekableBuffer(size_t backward_capacity, size_t forward_capacity); | 48 SeekableBuffer(size_t backward_capacity, size_t forward_capacity); |
| 49 | 49 |
| 50 ~SeekableBuffer(); | 50 ~SeekableBuffer(); |
| 51 | 51 |
| 52 // Read a maximum of |size| bytes into |buffer| from the current read | 52 // Reads a maximum of |size| bytes into |buffer| from the current read |
| 53 // position. Return the number of bytes read. | 53 // position. Returns the number of bytes read. |
| 54 // The current read position will advances by the amount of bytes read. If | 54 // The current read position will advance by the amount of bytes read. If |
| 55 // reading caused backward bytes to exceed backward_capacity(), an eviction | 55 // reading caused backward_bytes() to exceed backward_capacity(), an eviction |
| 56 // of backward buffer will be done internally. | 56 // of the backward buffer will be done internally. |
| 57 size_t Read(size_t size, uint8* buffer); | 57 size_t Read(size_t size, uint8* buffer); |
| 58 | 58 |
| 59 // Append |data| with |size| bytes to this buffer. If this buffer becomes full | 59 // Appends |data| with |size| bytes to this buffer. If this buffer becomes |
| 60 // or is already full then return false, otherwise true. | 60 // full or is already full then returns false, otherwise returns true. |
| 61 // Append operations are always successful, a return value of false only means | 61 // Append operations are always successful. A return value of false only means |
| 62 // that there's more forward bytes than the capacity, data is still in this | 62 // that forward_bytes() is greater than or equals to forward_capacity(). Data |
| 63 // buffer, but user is advised not to write any more. | 63 // appended is still in this buffer but user is advised not to write any more. |
| 64 bool Append(size_t size, const uint8* data); | 64 bool Append(size_t size, const uint8* data); |
| 65 | 65 |
| 66 // Move the read position by an offset of |offset| bytes. If |offset| is | 66 // Moves the read position by |offset| bytes. If |offset| is positive, the |
| 67 // positive, the current read position is moved forward. If negative, the | 67 // current read position is moved forward. If negative, the current read |
| 68 // current read position is moved backward. A zero |offset| value will keep | 68 // position is moved backward. A zero |offset| value will keep the current |
| 69 // the current read position stationary. | 69 // read position stationary. |
| 70 // If |offset| exceeds bytes buffered in either direction, reported by | 70 // If |offset| exceeds bytes buffered in either direction, reported by |
| 71 // forward_bytes() when seeking forward and backward_bytes() when seeking | 71 // forward_bytes() when seeking forward and backward_bytes() when seeking |
| 72 // backward, the seek operation will fail and return value will be false. | 72 // backward, the seek operation will fail and return value will be false. |
| 73 // If the seek operation fails, the current read position will not be updated. | 73 // If the seek operation fails, the current read position will not be updated. |
| 74 // If a forward seeking caused backward bytes to exceed backward_capacity(), | 74 // If a forward seeking caused backward_bytes() to exceed backward_capacity(), |
| 75 // this method call will cause an eviction of backward buffer. | 75 // this method call will cause an eviction of the backward buffer. |
| 76 bool Seek(int32 offset); | 76 bool Seek(int32 offset); |
| 77 | 77 |
| 78 // Returns the number of bytes buffered beyond the current read position. | 78 // Returns the number of bytes buffered beyond the current read position. |
| 79 size_t forward_bytes() const { return forward_bytes_; } | 79 size_t forward_bytes() const { return forward_bytes_; } |
| 80 | 80 |
| 81 // Returns the number of bytes buffered that precedes the current read | 81 // Returns the number of bytes buffered that precedes the current read |
| 82 // position. | 82 // position. |
| 83 size_t backward_bytes() const { return backward_bytes_; } | 83 size_t backward_bytes() const { return backward_bytes_; } |
| 84 | 84 |
| 85 // Returns the maximum number of bytes that should be kept in the forward | 85 // Returns the maximum number of bytes that should be kept in the forward |
| 86 // direction. | 86 // direction. |
| 87 size_t forward_capacity() const { return forward_capacity_; } | 87 size_t forward_capacity() const { return forward_capacity_; } |
| 88 | 88 |
| 89 // Returns the maximum number of bytes that should be kept in the backward | 89 // Returns the maximum number of bytes that should be kept in the backward |
| 90 // direction. | 90 // direction. |
| 91 size_t backward_capacity() const { return backward_capacity_; } | 91 size_t backward_capacity() const { return backward_capacity_; } |
| 92 | 92 |
| 93 private: | 93 private: |
| 94 // A structure that contains a block of data. |
| 95 struct Buffer { |
| 96 explicit Buffer(size_t len) : data(new uint8[len]), size(len) {} |
| 97 // Pointer to data. |
| 98 scoped_array<uint8> data; |
| 99 // Size of this block. |
| 100 size_t size; |
| 101 }; |
| 102 |
| 103 // Definition of the buffer queue. |
| 104 typedef std::list<Buffer*> BufferQueue; |
| 105 |
| 94 // A helper method to evict buffers in the backward direction until backward | 106 // A helper method to evict buffers in the backward direction until backward |
| 95 // bytes is within the backward capacity. | 107 // bytes is within the backward capacity. |
| 96 void EvictBackwardBuffers(); | 108 void EvictBackwardBuffers(); |
| 97 | 109 |
| 98 // An internal method shared by Read() and SeekForward() that actually does | 110 // An internal method shared by Read() and SeekForward() that actually does |
| 99 // reading. It reads a maximum of |size| bytes into |data|. Returns the number | 111 // reading. It reads a maximum of |size| bytes into |data|. Returns the number |
| 100 // of bytes read. The current read position will be moved forward by the | 112 // of bytes read. The current read position will be moved forward by the |
| 101 // number of bytes read. If |data| is NULL, only the current read position | 113 // number of bytes read. If |data| is NULL, only the current read position |
| 102 // will advance but no data will be copied. | 114 // will advance but no data will be copied. |
| 103 size_t InternalRead(size_t size, uint8* data); | 115 size_t InternalRead(size_t size, uint8* data); |
| 104 | 116 |
| 117 // A helper method that moves the current read position forward by |size| |
| 118 // bytes. |
| 119 // If the return value is true, the operation completed successfully. |
| 120 // If the return value is false, |size| is greater than forward_bytes() and |
| 121 // the seek operation failed. The current read position is not updated. |
| 105 bool SeekForward(size_t size); | 122 bool SeekForward(size_t size); |
| 106 | 123 |
| 124 // A helper method that moves the current read position backward by |size| |
| 125 // bytes. |
| 126 // If the return value is true, the operation completed successfully. |
| 127 // If the return value is false, |size| is greater than backward_bytes() and |
| 128 // the seek operation failed. The current read position is not updated. |
| 107 bool SeekBackward(size_t size); | 129 bool SeekBackward(size_t size); |
| 108 | 130 |
| 109 // A structure that contains a block of data. | |
| 110 struct Buffer { | |
| 111 explicit Buffer(size_t len) : data(new uint8[len]), size(len) {} | |
| 112 // Pointer to data. | |
| 113 scoped_array<uint8> data; | |
| 114 // Size of this block. | |
| 115 size_t size; | |
| 116 }; | |
| 117 | |
| 118 typedef std::list<Buffer*> BufferQueue; | |
| 119 BufferQueue::iterator current_buffer_; | 131 BufferQueue::iterator current_buffer_; |
| 120 BufferQueue buffers_; | 132 BufferQueue buffers_; |
| 121 size_t current_buffer_offset_; | 133 size_t current_buffer_offset_; |
| 122 | 134 |
| 123 size_t backward_capacity_; | 135 size_t backward_capacity_; |
| 124 size_t backward_bytes_; | 136 size_t backward_bytes_; |
| 125 | 137 |
| 126 size_t forward_capacity_; | 138 size_t forward_capacity_; |
| 127 size_t forward_bytes_; | 139 size_t forward_bytes_; |
| 128 }; | 140 }; |
| 129 | 141 |
| 130 } // namespace media | 142 } // namespace media |
| 131 | 143 |
| 132 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ | 144 #endif // MEDIA_BASE_SEEKABLE_BUFFER_H_ |
| OLD | NEW |