blob: 09c6c4f73467ce9c89c4d04ad5e8ce3751b96cda [file] [log] [blame]
Joachim Bauch6f2ef742015-05-21 17:52:01 +02001/*
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 09:11:00 -080011#ifndef RTC_BASE_BUFFER_QUEUE_H_
12#define RTC_BASE_BUFFER_QUEUE_H_
Joachim Bauch6f2ef742015-05-21 17:52:01 +020013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <deque>
17#include <vector>
Joachim Bauch6f2ef742015-05-21 17:52:01 +020018
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/constructor_magic.h"
Mirko Bonadei20e4c802020-11-23 11:07:42 +010022#include "rtc_base/system/no_unique_address.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "rtc_base/thread_annotations.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
25namespace rtc {
26
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020027class BufferQueue final {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028 public:
29 // Creates a buffer queue with a given capacity and default buffer size.
30 BufferQueue(size_t capacity, size_t default_size);
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020031 ~BufferQueue();
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020032
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
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020048 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 }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057
58 private:
Mirko Bonadei20e4c802020-11-23 11:07:42 +010059 RTC_NO_UNIQUE_ADDRESS webrtc::SequenceChecker sequence_checker_;
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020060 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_);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020064
65 RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
66};
67
68} // namespace rtc
Joachim Bauch6f2ef742015-05-21 17:52:01 +020069
Steve Anton10542f22019-01-11 09:11:00 -080070#endif // RTC_BASE_BUFFER_QUEUE_H_