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.
5 #include "media/base/byte_queue.h"
7 #include "base/logging.h"
11 // Default starting size for the queue.
12 enum { kDefaultQueueSize
= 1024 };
14 ByteQueue::ByteQueue()
15 : buffer_(new uint8
[kDefaultQueueSize
]),
16 size_(kDefaultQueueSize
),
21 ByteQueue::~ByteQueue() {}
23 void ByteQueue::Reset() {
28 void ByteQueue::Push(const uint8
* data
, int size
) {
32 size_t size_needed
= used_
+ size
;
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_
)
40 // Sanity check to make sure we didn't overflow.
41 CHECK_GT(new_size
, size_
);
43 scoped_ptr
<uint8
[]> new_buffer(new uint8
[new_size
]);
45 // Copy the data from the old buffer to the start of the new one.
47 memcpy(new_buffer
.get(), front(), used_
);
49 buffer_
.reset(new_buffer
.release());
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_
);
58 memcpy(front() + used_
, data
, size
);
62 void ByteQueue::Peek(const uint8
** data
, int* size
) const {
69 void ByteQueue::Pop(int count
) {
70 DCHECK_LE(count
, used_
);
75 // Move the offset back to 0 if we have reached the end of the buffer.
76 if (offset_
== size_
) {
82 uint8
* ByteQueue::front() const { return buffer_
.get() + offset_
; }