Shaka Packager SDK
offset_byte_queue.cc
1 // Copyright (c) 2012 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/offset_byte_queue.h"
6 
7 #include <stdint.h>
8 
9 #include "packager/base/logging.h"
10 
11 namespace shaka {
12 namespace media {
13 
14 OffsetByteQueue::OffsetByteQueue() : buf_(NULL), size_(0), head_(0) {}
15 OffsetByteQueue::~OffsetByteQueue() {}
16 
17 void OffsetByteQueue::Reset() {
18  queue_.Reset();
19  buf_ = NULL;
20  size_ = 0;
21  head_ = 0;
22 }
23 
24 void OffsetByteQueue::Push(const uint8_t* buf, int size) {
25  queue_.Push(buf, size);
26  Sync();
27  DVLOG(4) << "Buffer pushed. head=" << head() << " tail=" << tail();
28 }
29 
30 void OffsetByteQueue::Peek(const uint8_t** buf, int* size) {
31  *buf = size_ > 0 ? buf_ : NULL;
32  *size = size_;
33 }
34 
35 void OffsetByteQueue::Pop(int count) {
36  queue_.Pop(count);
37  head_ += count;
38  Sync();
39 }
40 
41 void OffsetByteQueue::PeekAt(int64_t offset, const uint8_t** buf, int* size) {
42  if (offset < head() || offset >= tail()) {
43  *buf = NULL;
44  *size = 0;
45  return;
46  }
47  *buf = &buf_[offset - head()];
48  *size = tail() - offset;
49 }
50 
51 bool OffsetByteQueue::Trim(int64_t max_offset) {
52  if (max_offset < head_) return true;
53  if (max_offset > tail()) {
54  Pop(size_);
55  return false;
56  }
57  Pop(max_offset - head_);
58  return true;
59 }
60 
61 void OffsetByteQueue::Sync() {
62  queue_.Peek(&buf_, &size_);
63 }
64 
65 } // namespace media
66 } // namespace shaka
All the methods that are virtual are virtual for mocking.