This source file includes following definitions.
- current_time_
- Clear
- Read
- Peek
- GetCurrentChunk
- Append
- Append
- SeekForward
- SeekBackward
- EvictBackwardBuffers
- InternalRead
- UpdateCurrentTime
#include "media/base/seekable_buffer.h"
#include <algorithm>
#include "base/logging.h"
#include "media/base/data_buffer.h"
namespace media {
SeekableBuffer::SeekableBuffer(int backward_capacity, int forward_capacity)
: current_buffer_offset_(0),
backward_capacity_(backward_capacity),
backward_bytes_(0),
forward_capacity_(forward_capacity),
forward_bytes_(0),
current_time_(kNoTimestamp()) {
current_buffer_ = buffers_.begin();
}
SeekableBuffer::~SeekableBuffer() {
}
void SeekableBuffer::Clear() {
buffers_.clear();
current_buffer_ = buffers_.begin();
current_buffer_offset_ = 0;
backward_bytes_ = 0;
forward_bytes_ = 0;
current_time_ = kNoTimestamp();
}
int SeekableBuffer::Read(uint8* data, int size) {
DCHECK(data);
return InternalRead(data, size, true, 0);
}
int SeekableBuffer::Peek(uint8* data, int size, int forward_offset) {
DCHECK(data);
return InternalRead(data, size, false, forward_offset);
}
bool SeekableBuffer::GetCurrentChunk(const uint8** data, int* size) const {
BufferQueue::iterator current_buffer = current_buffer_;
int current_buffer_offset = current_buffer_offset_;
while (current_buffer != buffers_.end() &&
current_buffer_offset >= (*current_buffer)->data_size()) {
++current_buffer;
current_buffer_offset = 0;
}
if (current_buffer == buffers_.end())
return false;
*data = (*current_buffer)->data() + current_buffer_offset;
*size = (*current_buffer)->data_size() - current_buffer_offset;
return true;
}
bool SeekableBuffer::Append(const scoped_refptr<DataBuffer>& buffer_in) {
if (buffers_.empty() && buffer_in->timestamp() != kNoTimestamp()) {
current_time_ = buffer_in->timestamp();
}
buffers_.push_back(buffer_in);
if (current_buffer_ == buffers_.end()) {
DCHECK_EQ(0, forward_bytes_);
current_buffer_ = buffers_.begin();
}
forward_bytes_ += buffer_in->data_size();
if (forward_bytes_ >= forward_capacity_)
return false;
return true;
}
bool SeekableBuffer::Append(const uint8* data, int size) {
if (size > 0) {
scoped_refptr<DataBuffer> data_buffer = DataBuffer::CopyFrom(data, size);
return Append(data_buffer);
} else {
return forward_bytes_ < forward_capacity_;
}
}
bool SeekableBuffer::Seek(int32 offset) {
if (offset > 0)
return SeekForward(offset);
else if (offset < 0)
return SeekBackward(-offset);
return true;
}
bool SeekableBuffer::SeekForward(int size) {
if (size > forward_bytes_)
return false;
int taken = InternalRead(NULL, size, true, 0);
DCHECK_EQ(taken, size);
return true;
}
bool SeekableBuffer::SeekBackward(int size) {
if (size > backward_bytes_)
return false;
int taken = 0;
while (taken < size) {
DCHECK(current_buffer_ != buffers_.end());
int consumed = std::min(size - taken, current_buffer_offset_);
current_buffer_offset_ -= consumed;
taken += consumed;
forward_bytes_ += consumed;
backward_bytes_ -= consumed;
DCHECK_GE(backward_bytes_, 0);
if (current_buffer_offset_ == 0) {
if (current_buffer_ == buffers_.begin())
break;
--current_buffer_;
current_buffer_offset_ = (*current_buffer_)->data_size();
}
}
UpdateCurrentTime(current_buffer_, current_buffer_offset_);
DCHECK_EQ(taken, size);
return true;
}
void SeekableBuffer::EvictBackwardBuffers() {
while (backward_bytes_ > backward_capacity_) {
BufferQueue::iterator i = buffers_.begin();
if (i == current_buffer_)
break;
scoped_refptr<DataBuffer> buffer = *i;
backward_bytes_ -= buffer->data_size();
DCHECK_GE(backward_bytes_, 0);
buffers_.erase(i);
}
}
int SeekableBuffer::InternalRead(uint8* data, int size,
bool advance_position,
int forward_offset) {
int taken = 0;
BufferQueue::iterator current_buffer = current_buffer_;
int current_buffer_offset = current_buffer_offset_;
int bytes_to_skip = forward_offset;
while (taken < size) {
if (current_buffer == buffers_.end())
break;
scoped_refptr<DataBuffer> buffer = *current_buffer;
int remaining_bytes_in_buffer =
buffer->data_size() - current_buffer_offset;
if (bytes_to_skip == 0) {
int copied = std::min(size - taken, remaining_bytes_in_buffer);
if (data)
memcpy(data + taken, buffer->data() + current_buffer_offset, copied);
taken += copied;
current_buffer_offset += copied;
} else {
int skipped = std::min(remaining_bytes_in_buffer, bytes_to_skip);
current_buffer_offset += skipped;
bytes_to_skip -= skipped;
}
if (current_buffer_offset == buffer->data_size()) {
if (advance_position) {
UpdateCurrentTime(current_buffer, current_buffer_offset);
}
BufferQueue::iterator next = current_buffer;
++next;
if (next == buffers_.end())
break;
current_buffer = next;
current_buffer_offset = 0;
}
}
if (advance_position) {
forward_bytes_ -= taken;
backward_bytes_ += taken;
DCHECK_GE(forward_bytes_, 0);
DCHECK(current_buffer_ != buffers_.end() || forward_bytes_ == 0);
current_buffer_ = current_buffer;
current_buffer_offset_ = current_buffer_offset;
UpdateCurrentTime(current_buffer_, current_buffer_offset_);
EvictBackwardBuffers();
}
return taken;
}
void SeekableBuffer::UpdateCurrentTime(BufferQueue::iterator buffer,
int offset) {
if (buffer != buffers_.end() &&
(*buffer)->timestamp() != kNoTimestamp()) {
int64 time_offset = ((*buffer)->duration().InMicroseconds() * offset) /
(*buffer)->data_size();
current_time_ = (*buffer)->timestamp() +
base::TimeDelta::FromMicroseconds(time_offset);
}
}
}