blob: 24a9b04dc2422c107aa1806eac75f5116ec6b433 [file] [log] [blame]
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +08001/*
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 Chaof76cafb2019-04-01 13:54:10 +080011#ifndef RTC_BASE_BUFFER_QUEUE_H_
12#define RTC_BASE_BUFFER_QUEUE_H_
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080013
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080014#include <stddef.h>
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000015
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080016#include <deque>
17#include <vector>
18
19#include "rtc_base/buffer.h"
Hsin-Yu Chaof76cafb2019-04-01 13:54:10 +080020#include "rtc_base/constructor_magic.h"
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000021#include "rtc_base/synchronization/sequence_checker.h"
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080022#include "rtc_base/thread_annotations.h"
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080023
24namespace rtc {
25
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000026class BufferQueue final {
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080027 public:
28 // Creates a buffer queue with a given capacity and default buffer size.
29 BufferQueue(size_t capacity, size_t default_size);
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000030 ~BufferQueue();
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080031
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
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000047 bool is_writable() const {
48 RTC_DCHECK_RUN_ON(&sequence_checker_);
49 return queue_.size() < capacity_;
50 }
51
52 bool is_readable() const {
53 RTC_DCHECK_RUN_ON(&sequence_checker_);
54 return !queue_.empty();
55 }
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080056
57 private:
Hsin-Yu Chao8873da32020-11-30 07:50:42 +000058 webrtc::SequenceChecker sequence_checker_;
59 const size_t capacity_;
60 const size_t default_size_;
61 std::deque<Buffer*> queue_ RTC_GUARDED_BY(sequence_checker_);
62 std::vector<Buffer*> free_list_ RTC_GUARDED_BY(sequence_checker_);
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080063
64 RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
65};
66
67} // namespace rtc
68
Hsin-Yu Chaof76cafb2019-04-01 13:54:10 +080069#endif // RTC_BASE_BUFFER_QUEUE_H_