blob: 7879e933c778033ddf14f9010df4d1ea0bad9470 [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
Steve Anton10542f22019-01-11 17:11:0011#include "rtc_base/buffer_queue.h"
Joachim Bauch6f2ef742015-05-21 15:52:0112
Yves Gerey988cc082018-10-23 10:03:0113#include <stdint.h>
14#include <string.h>
Jonas Olssona4d87372019-07-05 17:08:3315
ossub01c7812016-02-24 09:05:5616#include <algorithm>
17
Joachim Bauch6f2ef742015-05-21 15:52:0118namespace rtc {
19
20BufferQueue::BufferQueue(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 13:03:0521 : capacity_(capacity), default_size_(default_size) {}
Joachim Bauch6f2ef742015-05-21 15:52:0122
23BufferQueue::~BufferQueue() {
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0124 RTC_DCHECK_RUN_ON(&sequence_checker_);
25 for (Buffer* buffer : queue_)
Joachim Bauch6f2ef742015-05-21 15:52:0126 delete buffer;
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0127 for (Buffer* buffer : free_list_)
Joachim Bauch6f2ef742015-05-21 15:52:0128 delete buffer;
Joachim Bauch6f2ef742015-05-21 15:52:0129}
30
31size_t BufferQueue::size() const {
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0132 RTC_DCHECK_RUN_ON(&sequence_checker_);
Joachim Bauch6f2ef742015-05-21 15:52:0133 return queue_.size();
34}
35
guoweis4cc9f982016-02-24 19:10:0636void BufferQueue::Clear() {
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0137 RTC_DCHECK_RUN_ON(&sequence_checker_);
guoweis4cc9f982016-02-24 19:10:0638 while (!queue_.empty()) {
39 free_list_.push_back(queue_.front());
40 queue_.pop_front();
41 }
42}
43
Joachim Bauch6f2ef742015-05-21 15:52:0144bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0145 RTC_DCHECK_RUN_ON(&sequence_checker_);
46 if (queue_.empty())
Joachim Bauch6f2ef742015-05-21 15:52:0147 return false;
Joachim Bauch6f2ef742015-05-21 15:52:0148
49 Buffer* packet = queue_.front();
50 queue_.pop_front();
51
jbauche488a0d2015-11-19 13:17:5852 bytes = std::min(bytes, packet->size());
Joachim Bauch6f2ef742015-05-21 15:52:0153 memcpy(buffer, packet->data(), bytes);
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0154
55 if (bytes_read)
Joachim Bauch6f2ef742015-05-21 15:52:0156 *bytes_read = bytes;
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0157
Joachim Bauch6f2ef742015-05-21 15:52:0158 free_list_.push_back(packet);
59 return true;
60}
61
Yves Gerey665174f2018-06-19 13:03:0562bool BufferQueue::WriteBack(const void* buffer,
63 size_t bytes,
Joachim Bauch6f2ef742015-05-21 15:52:0164 size_t* bytes_written) {
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0165 RTC_DCHECK_RUN_ON(&sequence_checker_);
66 if (queue_.size() == capacity_)
Joachim Bauch6f2ef742015-05-21 15:52:0167 return false;
Joachim Bauch6f2ef742015-05-21 15:52:0168
69 Buffer* packet;
70 if (!free_list_.empty()) {
71 packet = free_list_.back();
72 free_list_.pop_back();
73 } else {
74 packet = new Buffer(bytes, default_size_);
75 }
76
77 packet->SetData(static_cast<const uint8_t*>(buffer), bytes);
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0178 if (bytes_written)
Joachim Bauch6f2ef742015-05-21 15:52:0179 *bytes_written = bytes;
Tomas Gunnarssonb6bc09b2020-09-29 11:04:0180
Joachim Bauch6f2ef742015-05-21 15:52:0181 queue_.push_back(packet);
Joachim Bauch6f2ef742015-05-21 15:52:0182 return true;
83}
84
85} // namespace rtc