Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [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 | |
Hsin-Yu Chao | f76cafb | 2019-04-01 13:54:10 +0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_BUFFER_QUEUE_H_ |
| 12 | #define RTC_BASE_BUFFER_QUEUE_H_ |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 13 | |
Hsin-Yu Chao | 640d48b | 2018-11-09 16:51:50 +0800 | [diff] [blame] | 14 | #include <stddef.h> |
Hsin-Yu Chao | 8873da3 | 2020-11-30 07:50:42 +0000 | [diff] [blame] | 15 | |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 16 | #include <deque> |
| 17 | #include <vector> |
| 18 | |
Per Ã…hgren | aa5a43f | 2021-03-30 08:29:43 +0200 | [diff] [blame^] | 19 | #include "api/sequence_checker.h" |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 20 | #include "rtc_base/buffer.h" |
Hsin-Yu Chao | f76cafb | 2019-04-01 13:54:10 +0800 | [diff] [blame] | 21 | #include "rtc_base/constructor_magic.h" |
Hsin-Yu Chao | e2b0512 | 2021-02-03 09:52:37 +0000 | [diff] [blame] | 22 | #include "rtc_base/system/no_unique_address.h" |
Hsin-Yu Chao | 640d48b | 2018-11-09 16:51:50 +0800 | [diff] [blame] | 23 | #include "rtc_base/thread_annotations.h" |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 24 | |
| 25 | namespace rtc { |
| 26 | |
Hsin-Yu Chao | 8873da3 | 2020-11-30 07:50:42 +0000 | [diff] [blame] | 27 | class BufferQueue final { |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 28 | public: |
| 29 | // Creates a buffer queue with a given capacity and default buffer size. |
| 30 | BufferQueue(size_t capacity, size_t default_size); |
Hsin-Yu Chao | 8873da3 | 2020-11-30 07:50:42 +0000 | [diff] [blame] | 31 | ~BufferQueue(); |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 32 | |
| 33 | // Return number of queued buffers. |
| 34 | size_t size() const; |
| 35 | |
| 36 | // Clear the BufferQueue by moving all Buffers from |queue_| to |free_list_|. |
| 37 | void Clear(); |
| 38 | |
| 39 | // ReadFront will only read one buffer at a time and will truncate buffers |
| 40 | // that don't fit in the passed memory. |
| 41 | // Returns true unless no data could be returned. |
| 42 | bool ReadFront(void* data, size_t bytes, size_t* bytes_read); |
| 43 | |
| 44 | // WriteBack always writes either the complete memory or nothing. |
| 45 | // Returns true unless no data could be written. |
| 46 | bool WriteBack(const void* data, size_t bytes, size_t* bytes_written); |
| 47 | |
Hsin-Yu Chao | 8873da3 | 2020-11-30 07:50:42 +0000 | [diff] [blame] | 48 | bool is_writable() const { |
| 49 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 50 | return queue_.size() < capacity_; |
| 51 | } |
| 52 | |
| 53 | bool is_readable() const { |
| 54 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 55 | return !queue_.empty(); |
| 56 | } |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 57 | |
| 58 | private: |
Hsin-Yu Chao | e2b0512 | 2021-02-03 09:52:37 +0000 | [diff] [blame] | 59 | RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_; |
Hsin-Yu Chao | 8873da3 | 2020-11-30 07:50:42 +0000 | [diff] [blame] | 60 | const size_t capacity_; |
| 61 | const size_t default_size_; |
| 62 | std::deque<Buffer*> queue_ RTC_GUARDED_BY(sequence_checker_); |
| 63 | std::vector<Buffer*> free_list_ RTC_GUARDED_BY(sequence_checker_); |
Hsin-Yu Chao | 47ed209 | 2018-03-17 16:50:45 +0800 | [diff] [blame] | 64 | |
| 65 | RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue); |
| 66 | }; |
| 67 | |
| 68 | } // namespace rtc |
| 69 | |
Hsin-Yu Chao | f76cafb | 2019-04-01 13:54:10 +0800 | [diff] [blame] | 70 | #endif // RTC_BASE_BUFFER_QUEUE_H_ |