Shaka Packager SDK
byte_queue.cc
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "packager/media/base/byte_queue.h"
6 
7 #include "packager/base/logging.h"
8 
9 namespace shaka {
10 namespace media {
11 
12 // Default starting size for the queue.
13 enum { kDefaultQueueSize = 1024 };
14 
15 ByteQueue::ByteQueue()
16  : buffer_(new uint8_t[kDefaultQueueSize]),
17  size_(kDefaultQueueSize),
18  offset_(0),
19  used_(0) {
20 }
21 
22 ByteQueue::~ByteQueue() {}
23 
25  offset_ = 0;
26  used_ = 0;
27 }
28 
29 void ByteQueue::Push(const uint8_t* data, int size) {
30  DCHECK(data);
31 
32  size_t size_needed = used_ + size;
33 
34  // Check to see if we need a bigger buffer.
35  if (size_needed > size_) {
36  size_t new_size = 2 * size_;
37  while (size_needed > new_size && new_size > size_)
38  new_size *= 2;
39 
40  // Sanity check to make sure we didn't overflow.
41  CHECK_GT(new_size, size_);
42 
43  std::unique_ptr<uint8_t[]> new_buffer(new uint8_t[new_size]);
44 
45  // Copy the data from the old buffer to the start of the new one.
46  if (used_ > 0)
47  memcpy(new_buffer.get(), front(), used_);
48 
49  buffer_.reset(new_buffer.release());
50  size_ = new_size;
51  offset_ = 0;
52  } else if ((offset_ + used_ + size) > size_) {
53  // The buffer is big enough, but we need to move the data in the queue.
54  memmove(buffer_.get(), front(), used_);
55  offset_ = 0;
56  }
57 
58  memcpy(front() + used_, data, size);
59  used_ += size;
60 }
61 
62 void ByteQueue::Peek(const uint8_t** data, int* size) const {
63  DCHECK(data);
64  DCHECK(size);
65  *data = front();
66  *size = used_;
67 }
68 
69 void ByteQueue::Pop(int count) {
70  DCHECK_LE(count, used_);
71 
72  offset_ += count;
73  used_ -= count;
74 
75  // Move the offset back to 0 if we have reached the end of the buffer.
76  if (offset_ == size_) {
77  DCHECK_EQ(used_, 0);
78  offset_ = 0;
79  }
80 }
81 
82 uint8_t* ByteQueue::front() const {
83  return buffer_.get() + offset_;
84 }
85 
86 } // namespace media
87 } // namespace shaka
shaka::media::ByteQueue::Peek
void Peek(const uint8_t **data, int *size) const
Definition: byte_queue.cc:62
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::ByteQueue::Pop
void Pop(int count)
Definition: byte_queue.cc:69
shaka::media::ByteQueue::Push
void Push(const uint8_t *data, int size)
Append new bytes to the end of the queue.
Definition: byte_queue.cc:29
shaka::media::ByteQueue::Reset
void Reset()
Reset the queue to the empty state.
Definition: byte_queue.cc:24