OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/seekable_buffer.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/stl_util-inl.h" |
| 11 |
| 12 namespace media { |
| 13 |
| 14 SeekableBuffer::SeekableBuffer(size_t backward_capacity, |
| 15 size_t forward_capacity) |
| 16 : current_buffer_offset_(0), |
| 17 backward_capacity_(backward_capacity), |
| 18 backward_bytes_(0), |
| 19 forward_capacity_(forward_capacity), |
| 20 forward_bytes_(0) { |
| 21 current_buffer_ = buffers_.begin(); |
| 22 } |
| 23 |
| 24 SeekableBuffer::~SeekableBuffer() { |
| 25 STLDeleteElements(&buffers_); |
| 26 } |
| 27 |
| 28 size_t SeekableBuffer::Read(size_t size, uint8* data) { |
| 29 DCHECK(data); |
| 30 return InternalRead(size, data); |
| 31 } |
| 32 |
| 33 bool SeekableBuffer::Append(size_t size, const uint8* data) { |
| 34 // Since the forward capacity is only used to check the criteria for buffer |
| 35 // full, we will always append data to the buffer. |
| 36 Buffer* buffer = new Buffer(size); |
| 37 memcpy(buffer->data.get(), data, size); |
| 38 buffers_.push_back(buffer); |
| 39 |
| 40 // After we have written the first buffer, update the |current_buffer_| to |
| 41 // point to it. |
| 42 if (current_buffer_ == buffers_.end()) { |
| 43 DCHECK_EQ(0, forward_bytes_); |
| 44 current_buffer_ = buffers_.begin(); |
| 45 } |
| 46 |
| 47 // Update the |forward_bytes_| counter since we have more bytes. |
| 48 forward_bytes_ += size; |
| 49 |
| 50 // Advise the user to stop append if the amount of forward bytes exceeds |
| 51 // the forward capacity. A false return value means the user should stop |
| 52 // appending more data to this buffer. |
| 53 if (forward_bytes_ >= forward_capacity_) |
| 54 return false; |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool SeekableBuffer::Seek(int32 offset) { |
| 59 if (offset > 0) |
| 60 return SeekForward(offset); |
| 61 else if (offset < 0) |
| 62 return SeekBackward(-offset); |
| 63 return true; |
| 64 } |
| 65 |
| 66 bool SeekableBuffer::SeekForward(size_t size) { |
| 67 // Perform seeking forward only if we have enough bytes in the queue. |
| 68 if (size > forward_bytes_) |
| 69 return false; |
| 70 |
| 71 // Do a read of |size| bytes. |
| 72 size_t taken = InternalRead(size, NULL); |
| 73 DCHECK_EQ(taken, size); |
| 74 return true; |
| 75 } |
| 76 |
| 77 bool SeekableBuffer::SeekBackward(size_t size) { |
| 78 if (size > backward_bytes_) |
| 79 return false; |
| 80 // Record the number of bytes taken. |
| 81 size_t taken = 0; |
| 82 // Loop until we taken enough bytes and rewind by the desired |size|. |
| 83 while (taken < size) { |
| 84 // |current_buffer_| can never be invalid when we are in this loop. It can |
| 85 // only be invalid before any data is appended, this case should be handled |
| 86 // by checks before we enter this loop. |
| 87 DCHECK(current_buffer_ != buffers_.end()); |
| 88 |
| 89 // We try to at most |size| bytes in the backward direction, we also have |
| 90 // to account for the offset we are in the current buffer, take the minimum |
| 91 // between the two to determine the amount of bytes to take from the |
| 92 // current buffer. |
| 93 size_t consumed = std::min(size - taken, current_buffer_offset_); |
| 94 |
| 95 // Decreases the offset in the current buffer since we are rewinding. |
| 96 current_buffer_offset_ -= consumed; |
| 97 |
| 98 // Increase the amount of bytes taken in the backward direction, this |
| 99 // determines when to stop the loop. |
| 100 taken += consumed; |
| 101 |
| 102 // Forward bytes increases, and backward bytes decreases by the amount |
| 103 // consumed in the current buffer. |
| 104 forward_bytes_ += consumed; |
| 105 backward_bytes_ -= consumed; |
| 106 DCHECK_GE(backward_bytes_, 0u); |
| 107 |
| 108 // The current buffer pointed by current iterator has been consumed, |
| 109 // move the iterator backward so it points to the previous buffer. |
| 110 if (current_buffer_offset_ == 0) { |
| 111 if (current_buffer_ == buffers_.begin()) |
| 112 break; |
| 113 // Move the iterator backward. |
| 114 --current_buffer_; |
| 115 // Set the offset into the current buffer to be the buffer size as we |
| 116 // are preparing for rewind for next iteration. |
| 117 current_buffer_offset_ = (*current_buffer_)->size; |
| 118 } |
| 119 } |
| 120 DCHECK_EQ(taken, size); |
| 121 return true; |
| 122 } |
| 123 |
| 124 void SeekableBuffer::EvictBackwardBuffers() { |
| 125 // Advances the iterator until we hit the current pointer. |
| 126 while (backward_bytes_ > backward_capacity_) { |
| 127 BufferQueue::iterator i = buffers_.begin(); |
| 128 if (i == current_buffer_) |
| 129 break; |
| 130 Buffer* buffer = *i; |
| 131 backward_bytes_ -= buffer->size; |
| 132 DCHECK_GE(backward_bytes_, 0u); |
| 133 |
| 134 delete *i; |
| 135 buffers_.erase(i); |
| 136 } |
| 137 } |
| 138 |
| 139 size_t SeekableBuffer::InternalRead(size_t size, uint8* data) { |
| 140 // Counts how many bytes are actually read from the buffer queue. |
| 141 size_t taken = 0; |
| 142 |
| 143 while (taken < size) { |
| 144 // |current_buffer_| is valid since the first time this buffer is appended |
| 145 // with data. |
| 146 if (current_buffer_ == buffers_.end()) { |
| 147 DCHECK_EQ(0, forward_bytes_); |
| 148 break; |
| 149 } |
| 150 Buffer* buffer = *current_buffer_; |
| 151 |
| 152 // Find the right amount to copy from the current buffer referenced by |
| 153 // |buffer|. We shall copy no more than |size| bytes in total and each |
| 154 // single step copied no more than the current buffer size. |
| 155 size_t copied = std::min(size - taken, |
| 156 buffer->size - current_buffer_offset_); |
| 157 |
| 158 // |data| is NULL if we are seeking forward, thus there's no need to copy. |
| 159 if (data) |
| 160 memcpy(data + taken, buffer->data.get() + current_buffer_offset_, copied); |
| 161 |
| 162 // Increase total number of bytes copied, which regulates when to end this |
| 163 // loop. |
| 164 taken += copied; |
| 165 |
| 166 // We have read |copied| bytes from the current buffer, advances the offset. |
| 167 current_buffer_offset_ += copied; |
| 168 |
| 169 // We have less forward bytes and more backward bytes, update these counters |
| 170 // by |copied|. |
| 171 forward_bytes_ -= copied; |
| 172 backward_bytes_ += copied; |
| 173 DCHECK_GE(forward_bytes_, 0u); |
| 174 |
| 175 // The buffer has been consumed. |
| 176 if (current_buffer_offset_ == buffer->size) { |
| 177 BufferQueue::iterator next = current_buffer_; |
| 178 ++next; |
| 179 // If we are at the last buffer, don't advance. |
| 180 if (next == buffers_.end()) |
| 181 break; |
| 182 |
| 183 // Advances the iterator. |
| 184 current_buffer_ = next; |
| 185 current_buffer_offset_ = 0; |
| 186 } |
| 187 } |
| 188 EvictBackwardBuffers(); |
| 189 return taken; |
| 190 } |
| 191 |
| 192 } // namespace media |
OLD | NEW |