Shaka Packager SDK
Loading...
Searching...
No Matches
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 <absl/log/check.h>
8#include <absl/log/log.h>
9
10namespace shaka {
11namespace media {
12
13// Default starting size for the queue.
14enum { kDefaultQueueSize = 1024 };
15
16ByteQueue::ByteQueue()
17 : buffer_(new uint8_t[kDefaultQueueSize]),
18 size_(kDefaultQueueSize),
19 offset_(0),
20 used_(0) {}
21
22ByteQueue::~ByteQueue() {}
23
24void ByteQueue::Reset() {
25 offset_ = 0;
26 used_ = 0;
27}
28
29void 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
62void ByteQueue::Peek(const uint8_t** data, int* size) const {
63 DCHECK(data);
64 DCHECK(size);
65 *data = front();
66 *size = used_;
67}
68
69void 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
82uint8_t* ByteQueue::front() const {
83 return buffer_.get() + offset_;
84}
85
86} // namespace media
87} // namespace shaka
All the methods that are virtual are virtual for mocking.