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 | #ifndef RTC_BASE_BUFFER_QUEUE_H_ |
| 12 | #define RTC_BASE_BUFFER_QUEUE_H_ |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 16 | #include <deque> |
| 17 | #include <vector> |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 18 | |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 19 | #include "api/sequence_checker.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 20 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 20e4c80 | 2020-11-23 10:07:42 | [diff] [blame] | 21 | #include "rtc_base/system/no_unique_address.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 22 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 26 | class BufferQueue final { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 27 | public: |
| 28 | // Creates a buffer queue with a given capacity and default buffer size. |
| 29 | BufferQueue(size_t capacity, size_t default_size); |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 30 | ~BufferQueue(); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 31 | |
Byoungchan Lee | 14af762 | 2022-01-11 20:24:58 | [diff] [blame] | 32 | BufferQueue(const BufferQueue&) = delete; |
| 33 | BufferQueue& operator=(const BufferQueue&) = delete; |
| 34 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 35 | // Return number of queued buffers. |
| 36 | size_t size() const; |
| 37 | |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 38 | // Clear the BufferQueue by moving all Buffers from `queue_` to `free_list_`. |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 39 | void Clear(); |
| 40 | |
| 41 | // ReadFront will only read one buffer at a time and will truncate buffers |
| 42 | // that don't fit in the passed memory. |
| 43 | // Returns true unless no data could be returned. |
| 44 | bool ReadFront(void* data, size_t bytes, size_t* bytes_read); |
| 45 | |
| 46 | // WriteBack always writes either the complete memory or nothing. |
| 47 | // Returns true unless no data could be written. |
| 48 | bool WriteBack(const void* data, size_t bytes, size_t* bytes_written); |
| 49 | |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 50 | bool is_writable() const { |
| 51 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 52 | return queue_.size() < capacity_; |
| 53 | } |
| 54 | |
| 55 | bool is_readable() const { |
| 56 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 57 | return !queue_.empty(); |
| 58 | } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 59 | |
| 60 | private: |
Mirko Bonadei | 20e4c80 | 2020-11-23 10:07:42 | [diff] [blame] | 61 | RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_; |
Tomas Gunnarsson | b6bc09b | 2020-09-29 11:04:01 | [diff] [blame] | 62 | const size_t capacity_; |
| 63 | const size_t default_size_; |
| 64 | std::deque<Buffer*> queue_ RTC_GUARDED_BY(sequence_checker_); |
| 65 | std::vector<Buffer*> free_list_ RTC_GUARDED_BY(sequence_checker_); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace rtc |
Joachim Bauch | 6f2ef74 | 2015-05-21 15:52:01 | [diff] [blame] | 69 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 70 | #endif // RTC_BASE_BUFFER_QUEUE_H_ |