Joachim Bauch | 6f2ef74 | 2015-05-21 17:52:01 +0200 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_BUFFER_QUEUE_H_ |
| 12 | #define RTC_BASE_BUFFER_QUEUE_H_ |
Joachim Bauch | 6f2ef74 | 2015-05-21 17:52:01 +0200 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 16 | #include <deque> |
| 17 | #include <vector> |
Joachim Bauch | 6f2ef74 | 2015-05-21 17:52:01 +0200 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "rtc_base/constructor_magic.h" |
| 21 | #include "rtc_base/critical_section.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "rtc_base/thread_annotations.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | class BufferQueue { |
| 27 | public: |
| 28 | // Creates a buffer queue with a given capacity and default buffer size. |
| 29 | BufferQueue(size_t capacity, size_t default_size); |
| 30 | virtual ~BufferQueue(); |
| 31 | |
| 32 | // Return number of queued buffers. |
| 33 | size_t size() const; |
| 34 | |
| 35 | // Clear the BufferQueue by moving all Buffers from |queue_| to |free_list_|. |
| 36 | void Clear(); |
| 37 | |
| 38 | // ReadFront will only read one buffer at a time and will truncate buffers |
| 39 | // that don't fit in the passed memory. |
| 40 | // Returns true unless no data could be returned. |
| 41 | bool ReadFront(void* data, size_t bytes, size_t* bytes_read); |
| 42 | |
| 43 | // WriteBack always writes either the complete memory or nothing. |
| 44 | // Returns true unless no data could be written. |
| 45 | bool WriteBack(const void* data, size_t bytes, size_t* bytes_written); |
| 46 | |
| 47 | protected: |
| 48 | // These methods are called when the state of the queue changes. |
| 49 | virtual void NotifyReadableForTest() {} |
| 50 | virtual void NotifyWritableForTest() {} |
| 51 | |
| 52 | private: |
| 53 | size_t capacity_; |
| 54 | size_t default_size_; |
| 55 | CriticalSection crit_; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 56 | std::deque<Buffer*> queue_ RTC_GUARDED_BY(crit_); |
| 57 | std::vector<Buffer*> free_list_ RTC_GUARDED_BY(crit_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | |
| 59 | RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue); |
| 60 | }; |
| 61 | |
| 62 | } // namespace rtc |
Joachim Bauch | 6f2ef74 | 2015-05-21 17:52:01 +0200 | [diff] [blame] | 63 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 64 | #endif // RTC_BASE_BUFFER_QUEUE_H_ |