| 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 #include "media/base/timestamp_constants.h" | 11 #include "media/base/timestamp_constants.h" |
| 12 | 12 |
| 13 namespace media { | 13 namespace media { |
| 14 | 14 |
| 15 SeekableBuffer::SeekableBuffer(int backward_capacity, int forward_capacity) | 15 SeekableBuffer::SeekableBuffer(int backward_capacity, int forward_capacity) |
| 16 : current_buffer_offset_(0), | 16 : current_buffer_offset_(0), |
| 17 backward_capacity_(backward_capacity), | 17 backward_capacity_(backward_capacity), |
| 18 backward_bytes_(0), | 18 backward_bytes_(0), |
| 19 forward_capacity_(forward_capacity), | 19 forward_capacity_(forward_capacity), |
| 20 forward_bytes_(0), | 20 forward_bytes_(0), |
| 21 current_time_(kNoTimestamp()) { | 21 current_time_(kNoTimestamp) { |
| 22 current_buffer_ = buffers_.begin(); | 22 current_buffer_ = buffers_.begin(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 SeekableBuffer::~SeekableBuffer() { | 25 SeekableBuffer::~SeekableBuffer() { |
| 26 } | 26 } |
| 27 | 27 |
| 28 void SeekableBuffer::Clear() { | 28 void SeekableBuffer::Clear() { |
| 29 buffers_.clear(); | 29 buffers_.clear(); |
| 30 current_buffer_ = buffers_.begin(); | 30 current_buffer_ = buffers_.begin(); |
| 31 current_buffer_offset_ = 0; | 31 current_buffer_offset_ = 0; |
| 32 backward_bytes_ = 0; | 32 backward_bytes_ = 0; |
| 33 forward_bytes_ = 0; | 33 forward_bytes_ = 0; |
| 34 current_time_ = kNoTimestamp(); | 34 current_time_ = kNoTimestamp; |
| 35 } | 35 } |
| 36 | 36 |
| 37 int SeekableBuffer::Read(uint8_t* data, int size) { | 37 int SeekableBuffer::Read(uint8_t* data, int size) { |
| 38 DCHECK(data); | 38 DCHECK(data); |
| 39 return InternalRead(data, size, true, 0); | 39 return InternalRead(data, size, true, 0); |
| 40 } | 40 } |
| 41 | 41 |
| 42 int SeekableBuffer::Peek(uint8_t* data, int size, int forward_offset) { | 42 int SeekableBuffer::Peek(uint8_t* data, int size, int forward_offset) { |
| 43 DCHECK(data); | 43 DCHECK(data); |
| 44 return InternalRead(data, size, false, forward_offset); | 44 return InternalRead(data, size, false, forward_offset); |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool SeekableBuffer::GetCurrentChunk(const uint8_t** data, int* size) const { | 47 bool SeekableBuffer::GetCurrentChunk(const uint8_t** data, int* size) const { |
| 48 BufferQueue::iterator current_buffer = current_buffer_; | 48 BufferQueue::iterator current_buffer = current_buffer_; |
| 49 int current_buffer_offset = current_buffer_offset_; | 49 int current_buffer_offset = current_buffer_offset_; |
| 50 // Advance position if we are in the end of the current buffer. | 50 // Advance position if we are in the end of the current buffer. |
| 51 while (current_buffer != buffers_.end() && | 51 while (current_buffer != buffers_.end() && |
| 52 current_buffer_offset >= (*current_buffer)->data_size()) { | 52 current_buffer_offset >= (*current_buffer)->data_size()) { |
| 53 ++current_buffer; | 53 ++current_buffer; |
| 54 current_buffer_offset = 0; | 54 current_buffer_offset = 0; |
| 55 } | 55 } |
| 56 if (current_buffer == buffers_.end()) | 56 if (current_buffer == buffers_.end()) |
| 57 return false; | 57 return false; |
| 58 *data = (*current_buffer)->data() + current_buffer_offset; | 58 *data = (*current_buffer)->data() + current_buffer_offset; |
| 59 *size = (*current_buffer)->data_size() - current_buffer_offset; | 59 *size = (*current_buffer)->data_size() - current_buffer_offset; |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 | 62 |
| 63 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) { | 63 bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) { |
| 64 if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp()) { | 64 if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp) { |
| 65 current_time_ = buffer_in->timestamp(); | 65 current_time_ = buffer_in->timestamp(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 // Since the forward capacity is only used to check the criteria for buffer | 68 // Since the forward capacity is only used to check the criteria for buffer |
| 69 // full, we always append data to the buffer. | 69 // full, we always append data to the buffer. |
| 70 buffers_.push_back(buffer_in); | 70 buffers_.push_back(buffer_in); |
| 71 | 71 |
| 72 // After we have written the first buffer, update |current_buffer_| to point | 72 // After we have written the first buffer, update |current_buffer_| to point |
| 73 // to it. | 73 // to it. |
| 74 if (current_buffer_ == buffers_.end()) { | 74 if (current_buffer_ == buffers_.end()) { |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 UpdateCurrentTime(current_buffer_, current_buffer_offset_); | 259 UpdateCurrentTime(current_buffer_, current_buffer_offset_); |
| 260 EvictBackwardBuffers(); | 260 EvictBackwardBuffers(); |
| 261 } | 261 } |
| 262 | 262 |
| 263 return taken; | 263 return taken; |
| 264 } | 264 } |
| 265 | 265 |
| 266 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, | 266 void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer, |
| 267 int offset) { | 267 int offset) { |
| 268 // Garbage values are unavoidable, so this check will remain. | 268 // Garbage values are unavoidable, so this check will remain. |
| 269 if (buffer != buffers_.end() && | 269 if (buffer != buffers_.end() && (*buffer)->timestamp() != kNoTimestamp) { |
| 270 (*buffer)->timestamp() != kNoTimestamp()) { | |
| 271 int64_t time_offset = ((*buffer)->duration().InMicroseconds() * offset) / | 270 int64_t time_offset = ((*buffer)->duration().InMicroseconds() * offset) / |
| 272 (*buffer)->data_size(); | 271 (*buffer)->data_size(); |
| 273 | 272 |
| 274 current_time_ = (*buffer)->timestamp() + | 273 current_time_ = (*buffer)->timestamp() + |
| 275 base::TimeDelta::FromMicroseconds(time_offset); | 274 base::TimeDelta::FromMicroseconds(time_offset); |
| 276 } | 275 } |
| 277 } | 276 } |
| 278 | 277 |
| 279 } // namespace media | 278 } // namespace media |
| OLD | NEW |