2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/offset_byte_queue.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cstdint>
|
2014-09-30 23:52:58 +00:00
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <glog/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 {
|
|
|
|
|
|
|
|
OffsetByteQueue::OffsetByteQueue() : buf_(NULL), size_(0), head_(0) {}
|
|
|
|
OffsetByteQueue::~OffsetByteQueue() {}
|
|
|
|
|
|
|
|
void OffsetByteQueue::Reset() {
|
|
|
|
queue_.Reset();
|
|
|
|
buf_ = NULL;
|
|
|
|
size_ = 0;
|
|
|
|
head_ = 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void OffsetByteQueue::Push(const uint8_t* buf, int size) {
|
2013-09-24 01:35:40 +00:00
|
|
|
queue_.Push(buf, size);
|
|
|
|
Sync();
|
|
|
|
DVLOG(4) << "Buffer pushed. head=" << head() << " tail=" << tail();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void OffsetByteQueue::Peek(const uint8_t** buf, int* size) {
|
2013-09-24 01:35:40 +00:00
|
|
|
*buf = size_ > 0 ? buf_ : NULL;
|
|
|
|
*size = size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OffsetByteQueue::Pop(int count) {
|
|
|
|
queue_.Pop(count);
|
|
|
|
head_ += count;
|
|
|
|
Sync();
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void OffsetByteQueue::PeekAt(int64_t offset, const uint8_t** buf, int* size) {
|
2013-09-24 01:35:40 +00:00
|
|
|
if (offset < head() || offset >= tail()) {
|
|
|
|
*buf = NULL;
|
|
|
|
*size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*buf = &buf_[offset - head()];
|
|
|
|
*size = tail() - offset;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
bool OffsetByteQueue::Trim(int64_t max_offset) {
|
2013-09-24 01:35:40 +00:00
|
|
|
if (max_offset < head_) return true;
|
|
|
|
if (max_offset > tail()) {
|
|
|
|
Pop(size_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Pop(max_offset - head_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OffsetByteQueue::Sync() {
|
|
|
|
queue_.Peek(&buf_, &size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|