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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "rtc_base/buffer_queue.h" |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
ossu | b01c781 | 2016-02-24 09:05:56 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 18 | namespace rtc { |
| 19 | |
| 20 | BufferQueue::BufferQueue(size_t capacity, size_t default_size) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 21 | : capacity_(capacity), default_size_(default_size) {} |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 22 | |
| 23 | BufferQueue::~BufferQueue() { |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 24 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 25 | for (Buffer* buffer : queue_) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 26 | delete buffer; |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 27 | for (Buffer* buffer : free_list_) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 28 | delete buffer; |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | size_t BufferQueue::size() const { |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 32 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 33 | return queue_.size(); |
| 34 | } |
| 35 | |
guoweis | 4cc9f98 | 2016-02-24 19:10:06 | [diff] [blame] | 36 | void BufferQueue::Clear() { |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 37 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
guoweis | 4cc9f98 | 2016-02-24 19:10:06 | [diff] [blame] | 38 | while (!queue_.empty()) { |
| 39 | free_list_.push_back(queue_.front()); |
| 40 | queue_.pop_front(); |
| 41 | } |
| 42 | } |
| 43 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 44 | bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) { |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 45 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 46 | if (queue_.empty()) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 47 | return false; |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 48 | |
| 49 | Buffer* packet = queue_.front(); |
| 50 | queue_.pop_front(); |
| 51 | |
jbauch | e488a0d | 2015-11-19 13:17:58 | [diff] [blame] | 52 | bytes = std::min(bytes, packet->size()); |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 53 | memcpy(buffer, packet->data(), bytes); |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 54 | |
| 55 | if (bytes_read) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 56 | *bytes_read = bytes; |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 57 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 58 | free_list_.push_back(packet); |
| 59 | return true; |
| 60 | } |
| 61 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 62 | bool BufferQueue::WriteBack(const void* buffer, |
| 63 | size_t bytes, |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 64 | size_t* bytes_written) { |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 65 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 66 | if (queue_.size() == capacity_) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 67 | return false; |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 68 | |
| 69 | Buffer* packet; |
| 70 | if (!free_list_.empty()) { |
| 71 | packet = free_list_.back(); |
| 72 | free_list_.pop_back(); |
| 73 | } else { |
| 74 | packet = new Buffer(bytes, default_size_); |
| 75 | } |
| 76 | |
| 77 | packet->SetData(static_cast<const uint8_t*>(buffer), bytes); |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 78 | if (bytes_written) |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 79 | *bytes_written = bytes; |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 80 | |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 81 | queue_.push_back(packet); |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 82 | return true; |
| 83 | } |
| 84 | |
| 85 | } // namespace rtc |