OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "media/base/buffer_queue.h" |
| 6 |
| 7 #include "media/base/buffers.h" |
| 8 |
| 9 namespace media { |
| 10 |
| 11 BufferQueue::BufferQueue() |
| 12 : data_offset_(0), |
| 13 size_in_bytes_(0) { |
| 14 } |
| 15 |
| 16 BufferQueue::~BufferQueue() { |
| 17 } |
| 18 |
| 19 void BufferQueue::Consume(size_t bytes_to_be_consumed) { |
| 20 // Make sure user isn't trying to consume more than we have. |
| 21 DCHECK(size_in_bytes_ >= bytes_to_be_consumed); |
| 22 |
| 23 // As we have enough data to consume, adjust |size_in_bytes_|. |
| 24 size_in_bytes_ -= bytes_to_be_consumed; |
| 25 |
| 26 // Now consume them. |
| 27 while (bytes_to_be_consumed > 0) { |
| 28 // Calculate number of usable bytes in the front of the |queue_|. |
| 29 size_t front_remaining = queue_.front()->GetDataSize() - data_offset_; |
| 30 |
| 31 // If there is enough data in our first buffer to advance into it, do so. |
| 32 // Otherwise, advance into the queue. |
| 33 if (front_remaining > bytes_to_be_consumed) { |
| 34 data_offset_ += bytes_to_be_consumed; |
| 35 bytes_to_be_consumed = 0; |
| 36 } else { |
| 37 data_offset_ = 0; |
| 38 queue_.pop_front(); |
| 39 bytes_to_be_consumed -= front_remaining; |
| 40 } |
| 41 } |
| 42 } |
| 43 |
| 44 size_t BufferQueue::Copy(uint8* dest, size_t bytes) { |
| 45 if (bytes == 0) |
| 46 return 0; |
| 47 |
| 48 DCHECK(!queue_.empty()); |
| 49 |
| 50 size_t current_remaining = 0; |
| 51 const uint8* current = NULL; |
| 52 size_t copied = 0; |
| 53 |
| 54 for (size_t i = 0; i < queue_.size() && bytes > 0; ++i) { |
| 55 // Calculate number of usable bytes in the front of the |queue_|. Special |
| 56 // case for front due to |data_offset_|. |
| 57 if (i == 0) { |
| 58 current_remaining = queue_.front()->GetDataSize() - data_offset_; |
| 59 current = queue_.front()->GetData() + data_offset_; |
| 60 } else { |
| 61 current_remaining = queue_[i]->GetDataSize(); |
| 62 current = queue_[i]->GetData(); |
| 63 } |
| 64 |
| 65 // Prevent writing over the end of the buffer. |
| 66 if (current_remaining > bytes) |
| 67 current_remaining = bytes; |
| 68 |
| 69 memcpy(dest + copied, current, current_remaining); |
| 70 |
| 71 // Modify counts and pointers. |
| 72 copied += current_remaining; |
| 73 bytes -= current_remaining; |
| 74 } |
| 75 return copied; |
| 76 } |
| 77 |
| 78 void BufferQueue::Enqueue(Buffer* buffer_in) { |
| 79 queue_.push_back(buffer_in); |
| 80 size_in_bytes_ += buffer_in->GetDataSize(); |
| 81 } |
| 82 |
| 83 void BufferQueue::Clear() { |
| 84 queue_.clear(); |
| 85 size_in_bytes_ = 0; |
| 86 data_offset_ = 0; |
| 87 } |
| 88 |
| 89 bool BufferQueue::IsEmpty() { |
| 90 // Since we keep track of the number of bytes, this is easier than calling |
| 91 // into |queue_|. |
| 92 return size_in_bytes_ == 0; |
| 93 } |
| 94 |
| 95 size_t BufferQueue::SizeInBytes() { |
| 96 return size_in_bytes_; |
| 97 } |
| 98 |
| 99 } // namespace media |
OLD | NEW |