blob: b018e160a1815a14f1188db32df692820b33847c [file] [log] [blame]
Joachim Bauch6f2ef742015-05-21 15:52:011/*
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 Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_BUFFER_QUEUE_H_
12#define RTC_BASE_BUFFER_QUEUE_H_
Joachim Bauch6f2ef742015-05-21 15:52:0113
Yves Gerey988cc082018-10-23 10:03:0114#include <stddef.h>
Jonas Olssona4d87372019-07-05 17:08:3315
Henrik Kjellanderec78f1c2017-06-29 05:52:5016#include <deque>
17#include <vector>
Joachim Bauch6f2ef742015-05-21 15:52:0118
Artem Titovd15a5752021-02-10 13:31:2419#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/buffer.h"
Mirko Bonadei20e4c802020-11-23 10:07:4221#include "rtc_base/system/no_unique_address.h"
Yves Gerey988cc082018-10-23 10:03:0122#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5023
24namespace rtc {
25
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0126class BufferQueue final {
Henrik Kjellanderec78f1c2017-06-29 05:52:5027 public:
28 // Creates a buffer queue with a given capacity and default buffer size.
29 BufferQueue(size_t capacity, size_t default_size);
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0130 ~BufferQueue();
Henrik Kjellanderec78f1c2017-06-29 05:52:5031
Byoungchan Lee14af7622022-01-11 20:24:5832 BufferQueue(const BufferQueue&) = delete;
33 BufferQueue& operator=(const BufferQueue&) = delete;
34
Henrik Kjellanderec78f1c2017-06-29 05:52:5035 // Return number of queued buffers.
36 size_t size() const;
37
Artem Titov96e3b992021-07-26 14:03:1438 // Clear the BufferQueue by moving all Buffers from `queue_` to `free_list_`.
Henrik Kjellanderec78f1c2017-06-29 05:52:5039 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 Gunnarssonb6bc09b2020-09-29 11:04:0150 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 Kjellanderec78f1c2017-06-29 05:52:5059
60 private:
Mirko Bonadei20e4c802020-11-23 10:07:4261 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0162 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 Kjellanderec78f1c2017-06-29 05:52:5066};
67
68} // namespace rtc
Joachim Bauch6f2ef742015-05-21 15:52:0169
Steve Anton10542f22019-01-11 17:11:0070#endif // RTC_BASE_BUFFER_QUEUE_H_