OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 // Flush the buffer. All packets in the buffer will be destroyed. | 50 // Flush the buffer. All packets in the buffer will be destroyed. |
51 void PacketBuffer::Flush() { | 51 void PacketBuffer::Flush() { |
52 DeleteAllPackets(&buffer_); | 52 DeleteAllPackets(&buffer_); |
53 } | 53 } |
54 | 54 |
55 bool PacketBuffer::Empty() const { | 55 bool PacketBuffer::Empty() const { |
56 return buffer_.empty(); | 56 return buffer_.empty(); |
57 } | 57 } |
58 | 58 |
59 int PacketBuffer::InsertPacket(Packet* packet) { | 59 int PacketBuffer::InsertPacket(Packet* packet) { |
60 if (!packet || !packet->payload) { | 60 if (!packet || packet->payload.empty()) { |
61 if (packet) { | 61 if (packet) { |
62 delete packet; | 62 delete packet; |
63 } | 63 } |
64 LOG(LS_WARNING) << "InsertPacket invalid packet"; | 64 LOG(LS_WARNING) << "InsertPacket invalid packet"; |
65 return kInvalidPacket; | 65 return kInvalidPacket; |
66 } | 66 } |
67 | 67 |
68 int return_val = kOK; | 68 int return_val = kOK; |
69 | 69 |
70 packet->waiting_time = tick_timer_->GetNewStopwatch(); | 70 packet->waiting_time = tick_timer_->GetNewStopwatch(); |
(...skipping 10 matching lines...) Expand all Loading... |
81 // likely case is that the new packet should be near the end of the list. | 81 // likely case is that the new packet should be near the end of the list. |
82 PacketList::reverse_iterator rit = std::find_if( | 82 PacketList::reverse_iterator rit = std::find_if( |
83 buffer_.rbegin(), buffer_.rend(), | 83 buffer_.rbegin(), buffer_.rend(), |
84 NewTimestampIsLarger(packet)); | 84 NewTimestampIsLarger(packet)); |
85 | 85 |
86 // The new packet is to be inserted to the right of |rit|. If it has the same | 86 // The new packet is to be inserted to the right of |rit|. If it has the same |
87 // timestamp as |rit|, which has a higher priority, do not insert the new | 87 // timestamp as |rit|, which has a higher priority, do not insert the new |
88 // packet to list. | 88 // packet to list. |
89 if (rit != buffer_.rend() && | 89 if (rit != buffer_.rend() && |
90 packet->header.timestamp == (*rit)->header.timestamp) { | 90 packet->header.timestamp == (*rit)->header.timestamp) { |
91 delete [] packet->payload; | |
92 delete packet; | 91 delete packet; |
93 return return_val; | 92 return return_val; |
94 } | 93 } |
95 | 94 |
96 // The new packet is to be inserted to the left of |it|. If it has the same | 95 // The new packet is to be inserted to the left of |it|. If it has the same |
97 // timestamp as |it|, which has a lower priority, replace |it| with the new | 96 // timestamp as |it|, which has a lower priority, replace |it| with the new |
98 // packet. | 97 // packet. |
99 PacketList::iterator it = rit.base(); | 98 PacketList::iterator it = rit.base(); |
100 if (it != buffer_.end() && | 99 if (it != buffer_.end() && |
101 packet->header.timestamp == (*it)->header.timestamp) { | 100 packet->header.timestamp == (*it)->header.timestamp) { |
102 delete [] (*it)->payload; | |
103 delete *it; | 101 delete *it; |
104 it = buffer_.erase(it); | 102 it = buffer_.erase(it); |
105 } | 103 } |
106 buffer_.insert(it, packet); // Insert the packet at that position. | 104 buffer_.insert(it, packet); // Insert the packet at that position. |
107 | 105 |
108 return return_val; | 106 return return_val; |
109 } | 107 } |
110 | 108 |
111 int PacketBuffer::InsertPacketList(PacketList* packet_list, | 109 int PacketBuffer::InsertPacketList(PacketList* packet_list, |
112 const DecoderDatabase& decoder_database, | 110 const DecoderDatabase& decoder_database, |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } | 184 } |
187 | 185 |
188 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { | 186 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { |
189 if (Empty()) { | 187 if (Empty()) { |
190 // Buffer is empty. | 188 // Buffer is empty. |
191 return NULL; | 189 return NULL; |
192 } | 190 } |
193 | 191 |
194 Packet* packet = buffer_.front(); | 192 Packet* packet = buffer_.front(); |
195 // Assert that the packet sanity checks in InsertPacket method works. | 193 // Assert that the packet sanity checks in InsertPacket method works. |
196 assert(packet && packet->payload); | 194 assert(packet && !packet->payload.empty()); |
197 buffer_.pop_front(); | 195 buffer_.pop_front(); |
198 | 196 |
199 // Discard other packets with the same timestamp. These are duplicates or | 197 // Discard other packets with the same timestamp. These are duplicates or |
200 // redundant payloads that should not be used. | 198 // redundant payloads that should not be used. |
201 size_t discards = 0; | 199 size_t discards = 0; |
202 | 200 |
203 while (!Empty() && | 201 while (!Empty() && |
204 buffer_.front()->header.timestamp == packet->header.timestamp) { | 202 buffer_.front()->header.timestamp == packet->header.timestamp) { |
205 if (DiscardNextPacket() != kOK) { | 203 if (DiscardNextPacket() != kOK) { |
206 assert(false); // Must be ok by design. | 204 assert(false); // Must be ok by design. |
207 } | 205 } |
208 ++discards; | 206 ++discards; |
209 } | 207 } |
210 // The way of inserting packet should not cause any packet discarding here. | 208 // The way of inserting packet should not cause any packet discarding here. |
211 // TODO(minyue): remove |discard_count|. | 209 // TODO(minyue): remove |discard_count|. |
212 assert(discards == 0); | 210 assert(discards == 0); |
213 if (discard_count) | 211 if (discard_count) |
214 *discard_count = discards; | 212 *discard_count = discards; |
215 | 213 |
216 return packet; | 214 return packet; |
217 } | 215 } |
218 | 216 |
219 int PacketBuffer::DiscardNextPacket() { | 217 int PacketBuffer::DiscardNextPacket() { |
220 if (Empty()) { | 218 if (Empty()) { |
221 return kBufferEmpty; | 219 return kBufferEmpty; |
222 } | 220 } |
223 // Assert that the packet sanity checks in InsertPacket method works. | 221 // Assert that the packet sanity checks in InsertPacket method works. |
224 assert(buffer_.front()); | 222 assert(buffer_.front()); |
225 assert(buffer_.front()->payload); | 223 assert(!buffer_.front()->payload.empty()); |
226 DeleteFirstPacket(&buffer_); | 224 DeleteFirstPacket(&buffer_); |
227 return kOK; | 225 return kOK; |
228 } | 226 } |
229 | 227 |
230 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, | 228 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |
231 uint32_t horizon_samples) { | 229 uint32_t horizon_samples) { |
232 while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && | 230 while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && |
233 IsObsoleteTimestamp(buffer_.front()->header.timestamp, | 231 IsObsoleteTimestamp(buffer_.front()->header.timestamp, |
234 timestamp_limit, | 232 timestamp_limit, |
235 horizon_samples)) { | 233 horizon_samples)) { |
(...skipping 18 matching lines...) Expand all Loading... |
254 size_t num_samples = 0; | 252 size_t num_samples = 0; |
255 size_t last_duration = last_decoded_length; | 253 size_t last_duration = last_decoded_length; |
256 for (it = buffer_.begin(); it != buffer_.end(); ++it) { | 254 for (it = buffer_.begin(); it != buffer_.end(); ++it) { |
257 Packet* packet = (*it); | 255 Packet* packet = (*it); |
258 AudioDecoder* decoder = | 256 AudioDecoder* decoder = |
259 decoder_database->GetDecoder(packet->header.payloadType); | 257 decoder_database->GetDecoder(packet->header.payloadType); |
260 if (decoder && !packet->sync_packet) { | 258 if (decoder && !packet->sync_packet) { |
261 if (!packet->primary) { | 259 if (!packet->primary) { |
262 continue; | 260 continue; |
263 } | 261 } |
264 int duration = | 262 int duration = decoder->PacketDuration(packet->payload.data(), |
265 decoder->PacketDuration(packet->payload, packet->payload_length); | 263 packet->payload.size()); |
266 if (duration >= 0) { | 264 if (duration >= 0) { |
267 last_duration = duration; // Save the most up-to-date (valid) duration. | 265 last_duration = duration; // Save the most up-to-date (valid) duration. |
268 } | 266 } |
269 } | 267 } |
270 num_samples += last_duration; | 268 num_samples += last_duration; |
271 } | 269 } |
272 return num_samples; | 270 return num_samples; |
273 } | 271 } |
274 | 272 |
275 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { | 273 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { |
276 if (packet_list->empty()) { | 274 if (packet_list->empty()) { |
277 return false; | 275 return false; |
278 } | 276 } |
279 Packet* first_packet = packet_list->front(); | 277 Packet* first_packet = packet_list->front(); |
280 delete [] first_packet->payload; | |
281 delete first_packet; | 278 delete first_packet; |
282 packet_list->pop_front(); | 279 packet_list->pop_front(); |
283 return true; | 280 return true; |
284 } | 281 } |
285 | 282 |
286 void PacketBuffer::DeleteAllPackets(PacketList* packet_list) { | 283 void PacketBuffer::DeleteAllPackets(PacketList* packet_list) { |
287 while (DeleteFirstPacket(packet_list)) { | 284 while (DeleteFirstPacket(packet_list)) { |
288 // Continue while the list is not empty. | 285 // Continue while the list is not empty. |
289 } | 286 } |
290 } | 287 } |
291 | 288 |
292 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { | 289 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |
293 *num_packets = static_cast<int>(buffer_.size()); | 290 *num_packets = static_cast<int>(buffer_.size()); |
294 *max_num_packets = static_cast<int>(max_number_of_packets_); | 291 *max_num_packets = static_cast<int>(max_number_of_packets_); |
295 } | 292 } |
296 | 293 |
297 } // namespace webrtc | 294 } // namespace webrtc |
OLD | NEW |