blob: 7879e933c778033ddf14f9010df4d1ea0bad9470 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/buffer_queue.h"
Joachim Bauch6f2ef742015-05-21 17:52:01 +020012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stdint.h>
14#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
ossub01c7812016-02-24 01:05:56 -080016#include <algorithm>
17
Joachim Bauch6f2ef742015-05-21 17:52:01 +020018namespace rtc {
19
20BufferQueue::BufferQueue(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 15:03:05 +020021 : capacity_(capacity), default_size_(default_size) {}
Joachim Bauch6f2ef742015-05-21 17:52:01 +020022
23BufferQueue::~BufferQueue() {
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020024 RTC_DCHECK_RUN_ON(&sequence_checker_);
25 for (Buffer* buffer : queue_)
Joachim Bauch6f2ef742015-05-21 17:52:01 +020026 delete buffer;
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020027 for (Buffer* buffer : free_list_)
Joachim Bauch6f2ef742015-05-21 17:52:01 +020028 delete buffer;
Joachim Bauch6f2ef742015-05-21 17:52:01 +020029}
30
31size_t BufferQueue::size() const {
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020032 RTC_DCHECK_RUN_ON(&sequence_checker_);
Joachim Bauch6f2ef742015-05-21 17:52:01 +020033 return queue_.size();
34}
35
guoweis4cc9f982016-02-24 11:10:06 -080036void BufferQueue::Clear() {
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020037 RTC_DCHECK_RUN_ON(&sequence_checker_);
guoweis4cc9f982016-02-24 11:10:06 -080038 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) {
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020045 RTC_DCHECK_RUN_ON(&sequence_checker_);
46 if (queue_.empty())
Joachim Bauch6f2ef742015-05-21 17:52:01 +020047 return false;
Joachim Bauch6f2ef742015-05-21 17:52:01 +020048
49 Buffer* packet = queue_.front();
50 queue_.pop_front();
51
jbauche488a0d2015-11-19 05:17:58 -080052 bytes = std::min(bytes, packet->size());
Joachim Bauch6f2ef742015-05-21 17:52:01 +020053 memcpy(buffer, packet->data(), bytes);
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020054
55 if (bytes_read)
Joachim Bauch6f2ef742015-05-21 17:52:01 +020056 *bytes_read = bytes;
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020057
Joachim Bauch6f2ef742015-05-21 17:52:01 +020058 free_list_.push_back(packet);
59 return true;
60}
61
Yves Gerey665174f2018-06-19 15:03:05 +020062bool BufferQueue::WriteBack(const void* buffer,
63 size_t bytes,
Joachim Bauch6f2ef742015-05-21 17:52:01 +020064 size_t* bytes_written) {
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020065 RTC_DCHECK_RUN_ON(&sequence_checker_);
66 if (queue_.size() == capacity_)
Joachim Bauch6f2ef742015-05-21 17:52:01 +020067 return false;
Joachim Bauch6f2ef742015-05-21 17:52:01 +020068
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 13:04:01 +020078 if (bytes_written)
Joachim Bauch6f2ef742015-05-21 17:52:01 +020079 *bytes_written = bytes;
Tomas Gunnarssonb6bc09b2020-09-29 13:04:01 +020080
Joachim Bauch6f2ef742015-05-21 17:52:01 +020081 queue_.push_back(packet);
Joachim Bauch6f2ef742015-05-21 17:52:01 +020082 return true;
83}
84
85} // namespace rtc