| 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 #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 "media/base/data_buffer.h" | 10 #include "media/base/data_buffer.h" |
| 11 | 11 |
| 12 namespace media { | 12 namespace media { |
| 13 | 13 |
| 14 SeekableBuffer::SeekableBuffer(int backward_capacity, | 14 SeekableBuffer::SeekableBuffer(int backward_capacity, int forward_capacity) |
| 15 int forward_capacity) | |
| 16 : current_buffer_offset_(0), | 15 : current_buffer_offset_(0), |
| 17 backward_capacity_(backward_capacity), | 16 backward_capacity_(backward_capacity), |
| 18 backward_bytes_(0), | 17 backward_bytes_(0), |
| 19 forward_capacity_(forward_capacity), | 18 forward_capacity_(forward_capacity), |
| 20 forward_bytes_(0), | 19 forward_bytes_(0), |
| 21 current_time_(kNoTimestamp()) { | 20 current_time_(kNoTimestamp()) { |
| 22 current_buffer_ = buffers_.begin(); | 21 current_buffer_ = buffers_.begin(); |
| 23 } | 22 } |
| 24 | 23 |
| 25 SeekableBuffer::~SeekableBuffer() { | 24 SeekableBuffer::~SeekableBuffer() {} |
| 26 } | |
| 27 | 25 |
| 28 void SeekableBuffer::Clear() { | 26 void SeekableBuffer::Clear() { |
| 29 buffers_.clear(); | 27 buffers_.clear(); |
| 30 current_buffer_ = buffers_.begin(); | 28 current_buffer_ = buffers_.begin(); |
| 31 current_buffer_offset_ = 0; | 29 current_buffer_offset_ = 0; |
| 32 backward_bytes_ = 0; | 30 backward_bytes_ = 0; |
| 33 forward_bytes_ = 0; | 31 forward_bytes_ = 0; |
| 34 current_time_ = kNoTimestamp(); | 32 current_time_ = kNoTimestamp(); |
| 35 } | 33 } |
| 36 | 34 |
| 37 int SeekableBuffer::Read(uint8* data, int size) { | 35 int SeekableBuffer::Read(uint8* data, int size) { |
| 38 DCHECK(data); | 36 DCHECK(data); |
| 39 return InternalRead(data, size, true, 0); | 37 return InternalRead(data, size, true, 0); |
| 40 } | 38 } |
| 41 | 39 |
| 42 int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) { | 40 int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) { |
| 43 DCHECK(data); | 41 DCHECK(data); |
| 44 return InternalRead(data, size, false, forward_offset); | 42 return InternalRead(data, size, false, forward_offset); |
| 45 } | 43 } |
| 46 | 44 |
| 47 bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const { | 45 bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const { |
| 48 BufferQueue::iterator current_buffer = current_buffer_; | 46 BufferQueue::iterator current_buffer = current_buffer_; |
| 49 int current_buffer_offset = current_buffer_offset_; | 47 int current_buffer_offset = current_buffer_offset_; |
| 50 // Advance position if we are in the end of the current buffer. | 48 // Advance position if we are in the end of the current buffer. |
| 51 while (current_buffer != buffers_.end() && | 49 while (current_buffer != buffers_.end() && |
| 52 current_buffer_offset >= (*current_buffer)->GetDataSize()) { | 50 current_buffer_offset >= (*current_buffer)->get_data_size()) { |
| 53 ++current_buffer; | 51 ++current_buffer; |
| 54 current_buffer_offset = 0; | 52 current_buffer_offset = 0; |
| 55 } | 53 } |
| 56 if (current_buffer == buffers_.end()) | 54 if (current_buffer == buffers_.end()) return false; |
| 57 return false; | 55 *data = (*current_buffer)->get_data() + current_buffer_offset; |
| 58 *data = (*current_buffer)->GetData() + current_buffer_offset; | 56 *size = (*current_buffer)->get_data_size() - current_buffer_offset; |
| 59 *size = (*current_buffer)->GetDataSize() - current_buffer_offset; | |
| 60 return true; | 57 return true; |
| 61 } | 58 } |
| 62 | 59 |
| 63 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) { | 60 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) { |
| 64 if (buffers_.empty() && buffer_in->GetTimestamp() != kNoTimestamp()) { | 61 if (buffers_.empty() && buffer_in->get_timestamp() != kNoTimestamp()) { |
| 65 current_time_ = buffer_in->GetTimestamp(); | 62 current_time_ = buffer_in->get_timestamp(); |
| 66 } | 63 } |
| 67 | 64 |
| 68 // Since the forward capacity is only used to check the criteria for buffer | 65 // Since the forward capacity is only used to check the criteria for buffer |
| 69 // full, we always append data to the buffer. | 66 // full, we always append data to the buffer. |
| 70 buffers_.push_back(buffer_in); | 67 buffers_.push_back(buffer_in); |
| 71 | 68 |
| 72 // After we have written the first buffer, update |current_buffer_| to point | 69 // After we have written the first buffer, update |current_buffer_| to point |
| 73 // to it. | 70 // to it. |
| 74 if (current_buffer_ == buffers_.end()) { | 71 if (current_buffer_ == buffers_.end()) { |
| 75 DCHECK_EQ(0, forward_bytes_); | 72 DCHECK_EQ(0, forward_bytes_); |
| 76 current_buffer_ = buffers_.begin(); | 73 current_buffer_ = buffers_.begin(); |
| 77 } | 74 } |
| 78 | 75 |
| 79 // Update the |forward_bytes_| counter since we have more bytes. | 76 // Update the |forward_bytes_| counter since we have more bytes. |
| 80 forward_bytes_ += buffer_in->GetDataSize(); | 77 forward_bytes_ += buffer_in->get_data_size(); |
| 81 | 78 |
| 82 // Advise the user to stop append if the amount of forward bytes exceeds | 79 // Advise the user to stop append if the amount of forward bytes exceeds |
| 83 // the forward capacity. A false return value means the user should stop | 80 // the forward capacity. A false return value means the user should stop |
| 84 // appending more data to this buffer. | 81 // appending more data to this buffer. |
| 85 if (forward_bytes_ >= forward_capacity_) | 82 if (forward_bytes_ >= forward_capacity_) return false; |
| 86 return false; | |
| 87 return true; | 83 return true; |
| 88 } | 84 } |
| 89 | 85 |
| 90 bool SeekableBuffer::Append(const uint8* data, int size) { | 86 bool SeekableBuffer::Append(const uint8* data, int size) { |
| 91 if (size > 0) { | 87 if (size > 0) { |
| 92 scoped_refptr<DataBuffer> data_buffer = DataBuffer::CopyFrom(data, size); | 88 scoped_refptr<DataBuffer> data_buffer = DataBuffer::copy_from(data, size); |
| 93 return Append(data_buffer); | 89 return Append(data_buffer); |
| 94 } else { | 90 } else { |
| 95 // Return true if we have forward capacity. | 91 // Return true if we have forward capacity. |
| 96 return forward_bytes_ < forward_capacity_; | 92 return forward_bytes_ < forward_capacity_; |
| 97 } | 93 } |
| 98 } | 94 } |
| 99 | 95 |
| 100 bool SeekableBuffer::Seek(int32 offset) { | 96 bool SeekableBuffer::Seek(int32 offset) { |
| 101 if (offset > 0) | 97 if (offset > 0) |
| 102 return SeekForward(offset); | 98 return SeekForward(offset); |
| 103 else if (offset < 0) | 99 else if (offset < 0) |
| 104 return SeekBackward(-offset); | 100 return SeekBackward(-offset); |
| 105 return true; | 101 return true; |
| 106 } | 102 } |
| 107 | 103 |
| 108 bool SeekableBuffer::SeekForward(int size) { | 104 bool SeekableBuffer::SeekForward(int size) { |
| 109 // Perform seeking forward only if we have enough bytes in the queue. | 105 // Perform seeking forward only if we have enough bytes in the queue. |
| 110 if (size > forward_bytes_) | 106 if (size > forward_bytes_) return false; |
| 111 return false; | |
| 112 | 107 |
| 113 // Do a read of |size| bytes. | 108 // Do a read of |size| bytes. |
| 114 int taken = InternalRead(NULL, size, true, 0); | 109 int taken = InternalRead(NULL, size, true, 0); |
| 115 DCHECK_EQ(taken, size); | 110 DCHECK_EQ(taken, size); |
| 116 return true; | 111 return true; |
| 117 } | 112 } |
| 118 | 113 |
| 119 bool SeekableBuffer::SeekBackward(int size) { | 114 bool SeekableBuffer::SeekBackward(int size) { |
| 120 if (size > backward_bytes_) | 115 if (size > backward_bytes_) return false; |
| 121 return false; | |
| 122 // Record the number of bytes taken. | 116 // Record the number of bytes taken. |
| 123 int taken = 0; | 117 int taken = 0; |
| 124 // Loop until we taken enough bytes and rewind by the desired |size|. | 118 // Loop until we taken enough bytes and rewind by the desired |size|. |
| 125 while (taken < size) { | 119 while (taken < size) { |
| 126 // |current_buffer_| can never be invalid when we are in this loop. It can | 120 // |current_buffer_| can never be invalid when we are in this loop. It can |
| 127 // only be invalid before any data is appended. The invalid case should be | 121 // only be invalid before any data is appended. The invalid case should be |
| 128 // handled by checks before we enter this loop. | 122 // handled by checks before we enter this loop. |
| 129 DCHECK(current_buffer_ != buffers_.end()); | 123 DCHECK(current_buffer_ != buffers_.end()); |
| 130 | 124 |
| 131 // We try to consume at most |size| bytes in the backward direction. We also | 125 // We try to consume at most |size| bytes in the backward direction. We also |
| (...skipping 11 matching lines...) Expand all Loading... |
| 143 | 137 |
| 144 // Forward bytes increases and backward bytes decreases by the amount | 138 // Forward bytes increases and backward bytes decreases by the amount |
| 145 // consumed in the current buffer. | 139 // consumed in the current buffer. |
| 146 forward_bytes_ += consumed; | 140 forward_bytes_ += consumed; |
| 147 backward_bytes_ -= consumed; | 141 backward_bytes_ -= consumed; |
| 148 DCHECK_GE(backward_bytes_, 0); | 142 DCHECK_GE(backward_bytes_, 0); |
| 149 | 143 |
| 150 // The current buffer pointed by current iterator has been consumed. Move | 144 // The current buffer pointed by current iterator has been consumed. Move |
| 151 // the iterator backward so it points to the previous buffer. | 145 // the iterator backward so it points to the previous buffer. |
| 152 if (current_buffer_offset_ == 0) { | 146 if (current_buffer_offset_ == 0) { |
| 153 if (current_buffer_ == buffers_.begin()) | 147 if (current_buffer_ == buffers_.begin()) break; |
| 154 break; | |
| 155 // Move the iterator backward. | 148 // Move the iterator backward. |
| 156 --current_buffer_; | 149 --current_buffer_; |
| 157 // Set the offset into the current buffer to be the buffer size as we | 150 // Set the offset into the current buffer to be the buffer size as we |
| 158 // are preparing for rewind for next iteration. | 151 // are preparing for rewind for next iteration. |
| 159 current_buffer_offset_ = (*current_buffer_)->GetDataSize(); | 152 current_buffer_offset_ = (*current_buffer_)->get_data_size(); |
| 160 } | 153 } |
| 161 } | 154 } |
| 162 | 155 |
| 163 UpdateCurrentTime(current_buffer_, current_buffer_offset_); | 156 UpdateCurrentTime(current_buffer_, current_buffer_offset_); |
| 164 | 157 |
| 165 DCHECK_EQ(taken, size); | 158 DCHECK_EQ(taken, size); |
| 166 return true; | 159 return true; |
| 167 } | 160 } |
| 168 | 161 |
| 169 void SeekableBuffer::EvictBackwardBuffers() { | 162 void SeekableBuffer::EvictBackwardBuffers() { |
| 170 // Advances the iterator until we hit the current pointer. | 163 // Advances the iterator until we hit the current pointer. |
| 171 while (backward_bytes_ > backward_capacity_) { | 164 while (backward_bytes_ > backward_capacity_) { |
| 172 BufferQueue::iterator i = buffers_.begin(); | 165 BufferQueue::iterator i = buffers_.begin(); |
| 173 if (i == current_buffer_) | 166 if (i == current_buffer_) break; |
| 174 break; | |
| 175 scoped_refptr<DataBuffer> buffer = *i; | 167 scoped_refptr<DataBuffer> buffer = *i; |
| 176 backward_bytes_ -= buffer->GetDataSize(); | 168 backward_bytes_ -= buffer->get_data_size(); |
| 177 DCHECK_GE(backward_bytes_, 0); | 169 DCHECK_GE(backward_bytes_, 0); |
| 178 | 170 |
| 179 buffers_.erase(i); | 171 buffers_.erase(i); |
| 180 } | 172 } |
| 181 } | 173 } |
| 182 | 174 |
| 183 int SeekableBuffer::InternalRead(uint8* data, int size, | 175 int SeekableBuffer::InternalRead(uint8* data, int size, bool advance_position, |
| 184 bool advance_position, | |
| 185 int forward_offset) { | 176 int forward_offset) { |
| 186 // Counts how many bytes are actually read from the buffer queue. | 177 // Counts how many bytes are actually read from the buffer queue. |
| 187 int taken = 0; | 178 int taken = 0; |
| 188 | 179 |
| 189 BufferQueue::iterator current_buffer = current_buffer_; | 180 BufferQueue::iterator current_buffer = current_buffer_; |
| 190 int current_buffer_offset = current_buffer_offset_; | 181 int current_buffer_offset = current_buffer_offset_; |
| 191 | 182 |
| 192 int bytes_to_skip = forward_offset; | 183 int bytes_to_skip = forward_offset; |
| 193 while (taken < size) { | 184 while (taken < size) { |
| 194 // |current_buffer| is valid since the first time this buffer is appended | 185 // |current_buffer| is valid since the first time this buffer is appended |
| 195 // with data. | 186 // with data. |
| 196 if (current_buffer == buffers_.end()) | 187 if (current_buffer == buffers_.end()) break; |
| 197 break; | |
| 198 | 188 |
| 199 scoped_refptr<DataBuffer> buffer = *current_buffer; | 189 scoped_refptr<DataBuffer> buffer = *current_buffer; |
| 200 | 190 |
| 201 int remaining_bytes_in_buffer = | 191 int remaining_bytes_in_buffer = |
| 202 buffer->GetDataSize() - current_buffer_offset; | 192 buffer->get_data_size() - current_buffer_offset; |
| 203 | 193 |
| 204 if (bytes_to_skip == 0) { | 194 if (bytes_to_skip == 0) { |
| 205 // Find the right amount to copy from the current buffer referenced by | 195 // Find the right amount to copy from the current buffer referenced by |
| 206 // |buffer|. We shall copy no more than |size| bytes in total and each | 196 // |buffer|. We shall copy no more than |size| bytes in total and each |
| 207 // single step copied no more than the current buffer size. | 197 // single step copied no more than the current buffer size. |
| 208 int copied = std::min(size - taken, remaining_bytes_in_buffer); | 198 int copied = std::min(size - taken, remaining_bytes_in_buffer); |
| 209 | 199 |
| 210 // |data| is NULL if we are seeking forward, so there's no need to copy. | 200 // |data| is NULL if we are seeking forward, so there's no need to copy. |
| 211 if (data) | 201 if (data) |
| 212 memcpy(data + taken, buffer->GetData() + current_buffer_offset, copied); | 202 memcpy(data + taken, buffer->get_data() + current_buffer_offset, |
| 203 copied); |
| 213 | 204 |
| 214 // Increase total number of bytes copied, which regulates when to end this | 205 // Increase total number of bytes copied, which regulates when to end this |
| 215 // loop. | 206 // loop. |
| 216 taken += copied; | 207 taken += copied; |
| 217 | 208 |
| 218 // We have read |copied| bytes from the current buffer. Advances the | 209 // We have read |copied| bytes from the current buffer. Advances the |
| 219 // offset. | 210 // offset. |
| 220 current_buffer_offset += copied; | 211 current_buffer_offset += copied; |
| 221 } else { | 212 } else { |
| 222 int skipped = std::min(remaining_bytes_in_buffer, bytes_to_skip); | 213 int skipped = std::min(remaining_bytes_in_buffer, bytes_to_skip); |
| 223 current_buffer_offset += skipped; | 214 current_buffer_offset += skipped; |
| 224 bytes_to_skip -= skipped; | 215 bytes_to_skip -= skipped; |
| 225 } | 216 } |
| 226 | 217 |
| 227 // The buffer has been consumed. | 218 // The buffer has been consumed. |
| 228 if (current_buffer_offset == buffer->GetDataSize()) { | 219 if (current_buffer_offset == buffer->get_data_size()) { |
| 229 if (advance_position) { | 220 if (advance_position) { |
| 230 // Next buffer may not have timestamp, so we need to update current | 221 // Next buffer may not have timestamp, so we need to update current |
| 231 // timestamp before switching to the next buffer. | 222 // timestamp before switching to the next buffer. |
| 232 UpdateCurrentTime(current_buffer, current_buffer_offset); | 223 UpdateCurrentTime(current_buffer, current_buffer_offset); |
| 233 } | 224 } |
| 234 | 225 |
| 235 BufferQueue::iterator next = current_buffer; | 226 BufferQueue::iterator next = current_buffer; |
| 236 ++next; | 227 ++next; |
| 237 // If we are at the last buffer, don't advance. | 228 // If we are at the last buffer, don't advance. |
| 238 if (next == buffers_.end()) | 229 if (next == buffers_.end()) break; |
| 239 break; | |
| 240 | 230 |
| 241 // Advances the iterator. | 231 // Advances the iterator. |
| 242 current_buffer = next; | 232 current_buffer = next; |
| 243 current_buffer_offset = 0; | 233 current_buffer_offset = 0; |
| 244 } | 234 } |
| 245 } | 235 } |
| 246 | 236 |
| 247 if (advance_position) { | 237 if (advance_position) { |
| 248 // We have less forward bytes and more backward bytes. Updates these | 238 // We have less forward bytes and more backward bytes. Updates these |
| 249 // counters by |taken|. | 239 // counters by |taken|. |
| 250 forward_bytes_ -= taken; | 240 forward_bytes_ -= taken; |
| 251 backward_bytes_ += taken; | 241 backward_bytes_ += taken; |
| 252 DCHECK_GE(forward_bytes_, 0); | 242 DCHECK_GE(forward_bytes_, 0); |
| 253 DCHECK(current_buffer_ != buffers_.end() || forward_bytes_ == 0); | 243 DCHECK(current_buffer_ != buffers_.end() || forward_bytes_ == 0); |
| 254 | 244 |
| 255 current_buffer_ = current_buffer; | 245 current_buffer_ = current_buffer; |
| 256 current_buffer_offset_ = current_buffer_offset; | 246 current_buffer_offset_ = current_buffer_offset; |
| 257 | 247 |
| 258 UpdateCurrentTime(current_buffer_, current_buffer_offset_); | 248 UpdateCurrentTime(current_buffer_, current_buffer_offset_); |
| 259 EvictBackwardBuffers(); | 249 EvictBackwardBuffers(); |
| 260 } | 250 } |
| 261 | 251 |
| 262 return taken; | 252 return taken; |
| 263 } | 253 } |
| 264 | 254 |
| 265 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, | 255 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, |
| 266 int offset) { | 256 int offset) { |
| 267 // Garbage values are unavoidable, so this check will remain. | 257 // Garbage values are unavoidable, so this check will remain. |
| 268 if (buffer != buffers_.end() && (*buffer)->GetTimestamp() != kNoTimestamp()) { | 258 if (buffer != buffers_.end() && |
| 269 int64 time_offset = ((*buffer)->GetDuration().InMicroseconds() * | 259 (*buffer)->get_timestamp() != kNoTimestamp()) { |
| 270 offset) / (*buffer)->GetDataSize(); | 260 int64 time_offset = ((*buffer)->get_duration().InMicroseconds() * offset) / |
| 261 (*buffer)->get_data_size(); |
| 271 | 262 |
| 272 current_time_ = (*buffer)->GetTimestamp() + | 263 current_time_ = (*buffer)->get_timestamp() + |
| 273 base::TimeDelta::FromMicroseconds(time_offset); | 264 base::TimeDelta::FromMicroseconds(time_offset); |
| 274 } | 265 } |
| 275 } | 266 } |
| 276 | 267 |
| 277 } // namespace media | 268 } // namespace media |
| OLD | NEW |