blob: 74f7a502c0b910e61f5aaa8bc109e6334b8a918a [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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/bufferqueue.h"
Joachim Bauch6f2ef742015-05-21 15:52:0112
Yves Gerey988cc082018-10-23 10:03:0113#include <stdint.h>
14#include <string.h>
ossub01c7812016-02-24 09:05:5615#include <algorithm>
16
Joachim Bauch6f2ef742015-05-21 15:52:0117namespace rtc {
18
19BufferQueue::BufferQueue(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 13:03:0520 : capacity_(capacity), default_size_(default_size) {}
Joachim Bauch6f2ef742015-05-21 15:52:0121
22BufferQueue::~BufferQueue() {
23 CritScope cs(&crit_);
24
25 for (Buffer* buffer : queue_) {
26 delete buffer;
27 }
28 for (Buffer* buffer : free_list_) {
29 delete buffer;
30 }
31}
32
33size_t BufferQueue::size() const {
34 CritScope cs(&crit_);
35 return queue_.size();
36}
37
guoweis4cc9f982016-02-24 19:10:0638void BufferQueue::Clear() {
39 CritScope cs(&crit_);
40 while (!queue_.empty()) {
41 free_list_.push_back(queue_.front());
42 queue_.pop_front();
43 }
44}
45
Joachim Bauch6f2ef742015-05-21 15:52:0146bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
47 CritScope cs(&crit_);
48 if (queue_.empty()) {
49 return false;
50 }
51
jbauche488a0d2015-11-19 13:17:5852 bool was_writable = queue_.size() < capacity_;
Joachim Bauch6f2ef742015-05-21 15:52:0153 Buffer* packet = queue_.front();
54 queue_.pop_front();
55
jbauche488a0d2015-11-19 13:17:5856 bytes = std::min(bytes, packet->size());
Joachim Bauch6f2ef742015-05-21 15:52:0157 memcpy(buffer, packet->data(), bytes);
58 if (bytes_read) {
59 *bytes_read = bytes;
60 }
61 free_list_.push_back(packet);
jbauche488a0d2015-11-19 13:17:5862 if (!was_writable) {
63 NotifyWritableForTest();
64 }
Joachim Bauch6f2ef742015-05-21 15:52:0165 return true;
66}
67
Yves Gerey665174f2018-06-19 13:03:0568bool BufferQueue::WriteBack(const void* buffer,
69 size_t bytes,
Joachim Bauch6f2ef742015-05-21 15:52:0170 size_t* bytes_written) {
71 CritScope cs(&crit_);
72 if (queue_.size() == capacity_) {
73 return false;
74 }
75
jbauche488a0d2015-11-19 13:17:5876 bool was_readable = !queue_.empty();
Joachim Bauch6f2ef742015-05-21 15:52:0177 Buffer* packet;
78 if (!free_list_.empty()) {
79 packet = free_list_.back();
80 free_list_.pop_back();
81 } else {
82 packet = new Buffer(bytes, default_size_);
83 }
84
85 packet->SetData(static_cast<const uint8_t*>(buffer), bytes);
86 if (bytes_written) {
87 *bytes_written = bytes;
88 }
89 queue_.push_back(packet);
jbauche488a0d2015-11-19 13:17:5890 if (!was_readable) {
91 NotifyReadableForTest();
92 }
Joachim Bauch6f2ef742015-05-21 15:52:0193 return true;
94}
95
96} // namespace rtc