This source file includes following definitions.
- used_
- Reset
- Push
- Peek
- Pop
- front
#include "media/base/byte_queue.h"
#include "base/logging.h"
namespace media {
enum { kDefaultQueueSize = 1024 };
ByteQueue::ByteQueue()
: buffer_(new uint8[kDefaultQueueSize]),
size_(kDefaultQueueSize),
offset_(0),
used_(0) {
}
ByteQueue::~ByteQueue() {}
void ByteQueue::Reset() {
offset_ = 0;
used_ = 0;
}
void ByteQueue::Push(const uint8* data, int size) {
DCHECK(data);
DCHECK_GT(size, 0);
size_t size_needed = used_ + size;
if (size_needed > size_) {
size_t new_size = 2 * size_;
while (size_needed > new_size && new_size > size_)
new_size *= 2;
CHECK_GT(new_size, size_);
scoped_ptr<uint8[]> new_buffer(new uint8[new_size]);
if (used_ > 0)
memcpy(new_buffer.get(), front(), used_);
buffer_.reset(new_buffer.release());
size_ = new_size;
offset_ = 0;
} else if ((offset_ + used_ + size) > size_) {
memmove(buffer_.get(), front(), used_);
offset_ = 0;
}
memcpy(front() + used_, data, size);
used_ += size;
}
void ByteQueue::Peek(const uint8** data, int* size) const {
DCHECK(data);
DCHECK(size);
*data = front();
*size = used_;
}
void ByteQueue::Pop(int count) {
DCHECK_LE(count, used_);
offset_ += count;
used_ -= count;
if (offset_ == size_) {
DCHECK_EQ(used_, 0);
offset_ = 0;
}
}
uint8* ByteQueue::front() const { return buffer_.get() + offset_; }
}