blob: 1ac57abc0cad425d132a0e1b5f69a438f13eab88 [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
11#include "webrtc/base/bufferqueue.h"
12
13namespace rtc {
14
15BufferQueue::BufferQueue(size_t capacity, size_t default_size)
16 : capacity_(capacity), default_size_(default_size) {
17}
18
19BufferQueue::~BufferQueue() {
20 CritScope cs(&crit_);
21
22 for (Buffer* buffer : queue_) {
23 delete buffer;
24 }
25 for (Buffer* buffer : free_list_) {
26 delete buffer;
27 }
28}
29
30size_t BufferQueue::size() const {
31 CritScope cs(&crit_);
32 return queue_.size();
33}
34
35bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
36 CritScope cs(&crit_);
37 if (queue_.empty()) {
38 return false;
39 }
40
jbauche488a0d2015-11-19 05:17:58 -080041 bool was_writable = queue_.size() < capacity_;
Joachim Bauch6f2ef742015-05-21 17:52:01 +020042 Buffer* packet = queue_.front();
43 queue_.pop_front();
44
jbauche488a0d2015-11-19 05:17:58 -080045 bytes = std::min(bytes, packet->size());
Joachim Bauch6f2ef742015-05-21 17:52:01 +020046 memcpy(buffer, packet->data(), bytes);
47 if (bytes_read) {
48 *bytes_read = bytes;
49 }
50 free_list_.push_back(packet);
jbauche488a0d2015-11-19 05:17:58 -080051 if (!was_writable) {
52 NotifyWritableForTest();
53 }
Joachim Bauch6f2ef742015-05-21 17:52:01 +020054 return true;
55}
56
57bool BufferQueue::WriteBack(const void* buffer, size_t bytes,
58 size_t* bytes_written) {
59 CritScope cs(&crit_);
60 if (queue_.size() == capacity_) {
61 return false;
62 }
63
jbauche488a0d2015-11-19 05:17:58 -080064 bool was_readable = !queue_.empty();
Joachim Bauch6f2ef742015-05-21 17:52:01 +020065 Buffer* packet;
66 if (!free_list_.empty()) {
67 packet = free_list_.back();
68 free_list_.pop_back();
69 } else {
70 packet = new Buffer(bytes, default_size_);
71 }
72
73 packet->SetData(static_cast<const uint8_t*>(buffer), bytes);
74 if (bytes_written) {
75 *bytes_written = bytes;
76 }
77 queue_.push_back(packet);
jbauche488a0d2015-11-19 05:17:58 -080078 if (!was_readable) {
79 NotifyReadableForTest();
80 }
Joachim Bauch6f2ef742015-05-21 17:52:01 +020081 return true;
82}
83
84} // namespace rtc