2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/byte_queue.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
// Default starting size for the queue.
|
|
|
|
enum { kDefaultQueueSize = 1024 };
|
|
|
|
|
|
|
|
ByteQueue::ByteQueue()
|
2014-09-30 21:52:21 +00:00
|
|
|
: buffer_(new uint8_t[kDefaultQueueSize]),
|
2013-09-24 01:35:40 +00:00
|
|
|
size_(kDefaultQueueSize),
|
|
|
|
offset_(0),
|
|
|
|
used_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteQueue::~ByteQueue() {}
|
|
|
|
|
|
|
|
void ByteQueue::Reset() {
|
|
|
|
offset_ = 0;
|
|
|
|
used_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void ByteQueue::Push(const uint8_t* data, int size) {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK(data);
|
|
|
|
DCHECK_GT(size, 0);
|
|
|
|
|
|
|
|
size_t size_needed = used_ + size;
|
|
|
|
|
|
|
|
// Check to see if we need a bigger buffer.
|
|
|
|
if (size_needed > size_) {
|
|
|
|
size_t new_size = 2 * size_;
|
|
|
|
while (size_needed > new_size && new_size > size_)
|
|
|
|
new_size *= 2;
|
|
|
|
|
|
|
|
// Sanity check to make sure we didn't overflow.
|
|
|
|
CHECK_GT(new_size, size_);
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> new_buffer(new uint8_t[new_size]);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Copy the data from the old buffer to the start of the new one.
|
|
|
|
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_) {
|
|
|
|
// The buffer is big enough, but we need to move the data in the queue.
|
|
|
|
memmove(buffer_.get(), front(), used_);
|
|
|
|
offset_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(front() + used_, data, size);
|
|
|
|
used_ += size;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void ByteQueue::Peek(const uint8_t** data, int* size) const {
|
2013-09-24 01:35:40 +00:00
|
|
|
DCHECK(data);
|
|
|
|
DCHECK(size);
|
|
|
|
*data = front();
|
|
|
|
*size = used_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ByteQueue::Pop(int count) {
|
|
|
|
DCHECK_LE(count, used_);
|
|
|
|
|
|
|
|
offset_ += count;
|
|
|
|
used_ -= count;
|
|
|
|
|
|
|
|
// Move the offset back to 0 if we have reached the end of the buffer.
|
|
|
|
if (offset_ == size_) {
|
|
|
|
DCHECK_EQ(used_, 0);
|
|
|
|
offset_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t* ByteQueue::front() const {
|
|
|
|
return buffer_.get() + offset_;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|