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