Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/bufferqueue.h" |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 12 | |
ossu | b01c781 | 2016-02-24 09:05:56 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 15 | namespace rtc { |
| 16 | |
| 17 | BufferQueue::BufferQueue(size_t capacity, size_t default_size) |
| 18 | : capacity_(capacity), default_size_(default_size) { |
| 19 | } |
| 20 | |
| 21 | BufferQueue::~BufferQueue() { |
| 22 | CritScope cs(&crit_); |
| 23 | |
| 24 | for (Buffer* buffer : queue_) { |
| 25 | delete buffer; |
| 26 | } |
| 27 | for (Buffer* buffer : free_list_) { |
| 28 | delete buffer; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | size_t BufferQueue::size() const { |
| 33 | CritScope cs(&crit_); |
| 34 | return queue_.size(); |
| 35 | } |
| 36 | |
guoweis | 4cc9f98 | 2016-02-24 19:10:06 | [diff] [blame] | 37 | void BufferQueue::Clear() { |
| 38 | CritScope cs(&crit_); |
| 39 | while (!queue_.empty()) { |
| 40 | free_list_.push_back(queue_.front()); |
| 41 | queue_.pop_front(); |
| 42 | } |
| 43 | } |
| 44 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 45 | bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) { |
| 46 | CritScope cs(&crit_); |
| 47 | if (queue_.empty()) { |
| 48 | return false; |
| 49 | } |
| 50 | |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 51 | bool was_writable = queue_.size() < capacity_; |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 52 | Buffer* packet = queue_.front(); |
| 53 | queue_.pop_front(); |
| 54 | |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 55 | bytes = std::min(bytes, packet->size()); |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 56 | memcpy(buffer, packet->data(), bytes); |
| 57 | if (bytes_read) { |
| 58 | *bytes_read = bytes; |
| 59 | } |
| 60 | free_list_.push_back(packet); |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 61 | if (!was_writable) { |
| 62 | NotifyWritableForTest(); |
| 63 | } |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
| 67 | bool BufferQueue::WriteBack(const void* buffer, size_t bytes, |
| 68 | size_t* bytes_written) { |
| 69 | CritScope cs(&crit_); |
| 70 | if (queue_.size() == capacity_) { |
| 71 | return false; |
| 72 | } |
| 73 | |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 74 | bool was_readable = !queue_.empty(); |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 75 | Buffer* packet; |
| 76 | if (!free_list_.empty()) { |
| 77 | packet = free_list_.back(); |
| 78 | free_list_.pop_back(); |
| 79 | } else { |
| 80 | packet = new Buffer(bytes, default_size_); |
| 81 | } |
| 82 | |
| 83 | packet->SetData(static_cast<const uint8_t*>(buffer), bytes); |
| 84 | if (bytes_written) { |
| 85 | *bytes_written = bytes; |
| 86 | } |
| 87 | queue_.push_back(packet); |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 88 | if (!was_readable) { |
| 89 | NotifyReadableForTest(); |
| 90 | } |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 91 | return true; |
| 92 | } |
| 93 | |
| 94 | } // namespace rtc |