OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/bufferqueue.h" | 11 #include "webrtc/base/bufferqueue.h" |
12 | 12 |
13 namespace rtc { | 13 namespace rtc { |
14 | 14 |
15 BufferQueue::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) { | 16 : capacity_(capacity), default_size_(default_size), |
17 owner_(Thread::Current()) { | |
18 } | |
19 | |
20 BufferQueue::BufferQueue(size_t capacity, size_t default_size, Thread* owner) | |
21 : capacity_(capacity), default_size_(default_size), owner_(owner) { | |
17 } | 22 } |
18 | 23 |
19 BufferQueue::~BufferQueue() { | 24 BufferQueue::~BufferQueue() { |
20 CritScope cs(&crit_); | 25 CritScope cs(&crit_); |
21 | 26 |
22 for (Buffer* buffer : queue_) { | 27 for (Buffer* buffer : queue_) { |
23 delete buffer; | 28 delete buffer; |
24 } | 29 } |
25 for (Buffer* buffer : free_list_) { | 30 for (Buffer* buffer : free_list_) { |
26 delete buffer; | 31 delete buffer; |
27 } | 32 } |
28 } | 33 } |
29 | 34 |
30 size_t BufferQueue::size() const { | 35 size_t BufferQueue::size() const { |
31 CritScope cs(&crit_); | 36 CritScope cs(&crit_); |
32 return queue_.size(); | 37 return queue_.size(); |
33 } | 38 } |
34 | 39 |
35 bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) { | 40 bool BufferQueue::ReadFront(void* buffer, size_t bytes, size_t* bytes_read) { |
36 CritScope cs(&crit_); | 41 CritScope cs(&crit_); |
37 if (queue_.empty()) { | 42 if (queue_.empty()) { |
38 return false; | 43 return false; |
39 } | 44 } |
40 | 45 |
46 bool was_writable = queue_.size() < capacity_; | |
41 Buffer* packet = queue_.front(); | 47 Buffer* packet = queue_.front(); |
42 queue_.pop_front(); | 48 queue_.pop_front(); |
43 | 49 |
44 size_t next_packet_size = packet->size(); | 50 size_t next_packet_size = packet->size(); |
45 if (bytes > next_packet_size) { | 51 if (bytes > next_packet_size) { |
46 bytes = next_packet_size; | 52 bytes = next_packet_size; |
47 } | 53 } |
48 | 54 |
49 memcpy(buffer, packet->data(), bytes); | 55 memcpy(buffer, packet->data(), bytes); |
50 if (bytes_read) { | 56 if (bytes_read) { |
51 *bytes_read = bytes; | 57 *bytes_read = bytes; |
52 } | 58 } |
53 free_list_.push_back(packet); | 59 free_list_.push_back(packet); |
60 if (!was_writable) { | |
61 PostEvent(owner_, BQ_WRITE); | |
62 } | |
54 return true; | 63 return true; |
55 } | 64 } |
56 | 65 |
57 bool BufferQueue::WriteBack(const void* buffer, size_t bytes, | 66 bool BufferQueue::WriteBack(const void* buffer, size_t bytes, |
58 size_t* bytes_written) { | 67 size_t* bytes_written) { |
59 CritScope cs(&crit_); | 68 CritScope cs(&crit_); |
60 if (queue_.size() == capacity_) { | 69 if (queue_.size() == capacity_) { |
61 return false; | 70 return false; |
62 } | 71 } |
63 | 72 |
73 bool was_readable = !queue_.empty(); | |
64 Buffer* packet; | 74 Buffer* packet; |
65 if (!free_list_.empty()) { | 75 if (!free_list_.empty()) { |
66 packet = free_list_.back(); | 76 packet = free_list_.back(); |
67 free_list_.pop_back(); | 77 free_list_.pop_back(); |
68 } else { | 78 } else { |
69 packet = new Buffer(bytes, default_size_); | 79 packet = new Buffer(bytes, default_size_); |
70 } | 80 } |
71 | 81 |
72 packet->SetData(static_cast<const uint8_t*>(buffer), bytes); | 82 packet->SetData(static_cast<const uint8_t*>(buffer), bytes); |
73 if (bytes_written) { | 83 if (bytes_written) { |
74 *bytes_written = bytes; | 84 *bytes_written = bytes; |
75 } | 85 } |
76 queue_.push_back(packet); | 86 queue_.push_back(packet); |
87 if (!was_readable) { | |
88 PostEvent(owner_, BQ_READ); | |
89 } | |
77 return true; | 90 return true; |
78 } | 91 } |
79 | 92 |
93 void BufferQueue::PostEvent(Thread* t, int events) { | |
94 t->Post(this, MSG_POST_EVENT, new BufferEventData(events)); | |
95 } | |
96 | |
97 void BufferQueue::PostEvent(int events) { | |
98 PostEvent(Thread::Current(), events); | |
99 } | |
100 | |
101 void BufferQueue::OnMessage(Message* msg) { | |
tommi
2015/11/13 08:38:12
It looks like we allow posting this notification t
| |
102 if (MSG_POST_EVENT == msg->message_id) { | |
103 BufferEventData* pe = static_cast<BufferEventData*>(msg->pdata); | |
104 SignalEvent(this, pe->events); | |
105 delete msg->pdata; | |
106 } | |
107 } | |
108 | |
80 } // namespace rtc | 109 } // namespace rtc |
OLD | NEW |