1 /*
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
13 namespace rtc {
14
BufferQueue(size_t capacity,size_t default_size)15 BufferQueue::BufferQueue(size_t capacity, size_t default_size)
16 : capacity_(capacity), default_size_(default_size) {
17 }
18
~BufferQueue()19 BufferQueue::~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
size() const30 size_t BufferQueue::size() const {
31 CritScope cs(&crit_);
32 return queue_.size();
33 }
34
ReadFront(void * buffer,size_t bytes,size_t * bytes_read)35 bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) {
36 CritScope cs(&crit_);
37 if (queue_.empty()) {
38 return false;
39 }
40
41 bool was_writable = queue_.size() < capacity_;
42 Buffer* packet = queue_.front();
43 queue_.pop_front();
44
45 bytes = std::min(bytes, packet->size());
46 memcpy(buffer, packet->data(), bytes);
47 if (bytes_read) {
48 *bytes_read = bytes;
49 }
50 free_list_.push_back(packet);
51 if (!was_writable) {
52 NotifyWritableForTest();
53 }
54 return true;
55 }
56
WriteBack(const void * buffer,size_t bytes,size_t * bytes_written)57 bool 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
64 bool was_readable = !queue_.empty();
65 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);
78 if (!was_readable) {
79 NotifyReadableForTest();
80 }
81 return true;
82 }
83
84 } // namespace rtc
85