blob: 48ff2e6d317d78208a6b330efb398add64e3df7c [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/bufferqueue.h"
Joachim Bauch6f2ef742015-05-21 17:52:01 +020012
ossub01c7812016-02-24 01:05:56 -080013#include <algorithm>
14
Joachim Bauch6f2ef742015-05-21 17:52:01 +020015namespace rtc {
16
17BufferQueue::BufferQueue(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 15:03:05 +020018 : capacity_(capacity), default_size_(default_size) {}
Joachim Bauch6f2ef742015-05-21 17:52:01 +020019
20BufferQueue::~BufferQueue() {
21 CritScope cs(&crit_);
22
23 for (Buffer* buffer : queue_) {
24 delete buffer;
25 }
26 for (Buffer* buffer : free_list_) {
27 delete buffer;
28 }
29}
30
31size_t BufferQueue::size() const {
32 CritScope cs(&crit_);
33 return queue_.size();
34}
35
guoweis4cc9f982016-02-24 11:10:06 -080036void BufferQueue::Clear() {
37 CritScope cs(&crit_);
38 while (!queue_.empty()) {
39 free_list_.push_back(queue_.front());
40 queue_.pop_front();
41 }
42}
43
Joachim Bauch6f2ef742015-05-21 17:52:01 +020044bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
45 CritScope cs(&crit_);
46 if (queue_.empty()) {
47 return false;
48 }
49
jbauche488a0d2015-11-19 05:17:58 -080050 bool was_writable = queue_.size() < capacity_;
Joachim Bauch6f2ef742015-05-21 17:52:01 +020051 Buffer* packet = queue_.front();
52 queue_.pop_front();
53
jbauche488a0d2015-11-19 05:17:58 -080054 bytes = std::min(bytes, packet->size());
Joachim Bauch6f2ef742015-05-21 17:52:01 +020055 memcpy(buffer, packet->data(), bytes);
56 if (bytes_read) {
57 *bytes_read = bytes;
58 }
59 free_list_.push_back(packet);
jbauche488a0d2015-11-19 05:17:58 -080060 if (!was_writable) {
61 NotifyWritableForTest();
62 }
Joachim Bauch6f2ef742015-05-21 17:52:01 +020063 return true;
64}
65
Yves Gerey665174f2018-06-19 15:03:05 +020066bool BufferQueue::WriteBack(const void* buffer,
67 size_t bytes,
Joachim Bauch6f2ef742015-05-21 17:52:01 +020068 size_t* bytes_written) {
69 CritScope cs(&crit_);
70 if (queue_.size() == capacity_) {
71 return false;
72 }
73
jbauche488a0d2015-11-19 05:17:58 -080074 bool was_readable = !queue_.empty();
Joachim Bauch6f2ef742015-05-21 17:52:01 +020075 Buffer* packet;
76 if (!free_list_.empty()) {
77 packet = free_list_.back();
78 free_list_.pop_back();
79 } else {
80 packet = new Buffer(bytes, default_size_);
81 }
82
83 packet->SetData(static_cast<const uint8_t*>(buffer), bytes);
84 if (bytes_written) {
85 *bytes_written = bytes;
86 }
87 queue_.push_back(packet);
jbauche488a0d2015-11-19 05:17:58 -080088 if (!was_readable) {
89 NotifyReadableForTest();
90 }
Joachim Bauch6f2ef742015-05-21 17:52:01 +020091 return true;
92}
93
94} // namespace rtc