blob: 63f5182509f62204be01115798b90f1e0a8e2560 [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
11#ifndef RTC_BASE_BUFFERQUEUE_H_
12#define RTC_BASE_BUFFERQUEUE_H_
13
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080014#include <stddef.h>
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080015#include <deque>
16#include <vector>
17
18#include "rtc_base/buffer.h"
19#include "rtc_base/constructormagic.h"
20#include "rtc_base/criticalsection.h"
Hsin-Yu Chao640d48b2018-11-09 16:51:50 +080021#include "rtc_base/thread_annotations.h"
Hsin-Yu Chao47ed2092018-03-17 16:50:45 +080022
23namespace rtc {
24
25class BufferQueue {
26 public:
27 // Creates a buffer queue with a given capacity and default buffer size.
28 BufferQueue(size_t capacity, size_t default_size);
29 virtual ~BufferQueue();
30
31 // Return number of queued buffers.
32 size_t size() const;
33
34 // Clear the BufferQueue by moving all Buffers from |queue_| to |free_list_|.
35 void Clear();
36
37 // ReadFront will only read one buffer at a time and will truncate buffers
38 // that don't fit in the passed memory.
39 // Returns true unless no data could be returned.
40 bool ReadFront(void* data, size_t bytes, size_t* bytes_read);
41
42 // WriteBack always writes either the complete memory or nothing.
43 // Returns true unless no data could be written.
44 bool WriteBack(const void* data, size_t bytes, size_t* bytes_written);
45
46 protected:
47 // These methods are called when the state of the queue changes.
48 virtual void NotifyReadableForTest() {}
49 virtual void NotifyWritableForTest() {}
50
51 private:
52 size_t capacity_;
53 size_t default_size_;
54 CriticalSection crit_;
55 std::deque<Buffer*> queue_ RTC_GUARDED_BY(crit_);
56 std::vector<Buffer*> free_list_ RTC_GUARDED_BY(crit_);
57
58 RTC_DISALLOW_COPY_AND_ASSIGN(BufferQueue);
59};
60
61} // namespace rtc
62
63#endif // RTC_BASE_BUFFERQUEUE_H_