| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   61 // Flush the buffer. All packets in the buffer will be destroyed. |   61 // Flush the buffer. All packets in the buffer will be destroyed. | 
|   62 void PacketBuffer::Flush() { |   62 void PacketBuffer::Flush() { | 
|   63   DeleteAllPackets(&buffer_); |   63   DeleteAllPackets(&buffer_); | 
|   64 } |   64 } | 
|   65  |   65  | 
|   66 bool PacketBuffer::Empty() const { |   66 bool PacketBuffer::Empty() const { | 
|   67   return buffer_.empty(); |   67   return buffer_.empty(); | 
|   68 } |   68 } | 
|   69  |   69  | 
|   70 int PacketBuffer::InsertPacket(Packet* packet) { |   70 int PacketBuffer::InsertPacket(Packet* packet) { | 
|   71   if (!packet || packet->payload.empty()) { |   71   if (!packet || packet->empty()) { | 
|   72     if (packet) { |   72     if (packet) { | 
|   73       delete packet; |   73       delete packet; | 
|   74     } |   74     } | 
|   75     LOG(LS_WARNING) << "InsertPacket invalid packet"; |   75     LOG(LS_WARNING) << "InsertPacket invalid packet"; | 
|   76     return kInvalidPacket; |   76     return kInvalidPacket; | 
|   77   } |   77   } | 
|   78  |   78  | 
|   79   int return_val = kOK; |   79   int return_val = kOK; | 
|   80  |   80  | 
|   81   packet->waiting_time = tick_timer_->GetNewStopwatch(); |   81   packet->waiting_time = tick_timer_->GetNewStopwatch(); | 
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  202 } |  202 } | 
|  203  |  203  | 
|  204 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { |  204 Packet* PacketBuffer::GetNextPacket(size_t* discard_count) { | 
|  205   if (Empty()) { |  205   if (Empty()) { | 
|  206     // Buffer is empty. |  206     // Buffer is empty. | 
|  207     return NULL; |  207     return NULL; | 
|  208   } |  208   } | 
|  209  |  209  | 
|  210   Packet* packet = buffer_.front(); |  210   Packet* packet = buffer_.front(); | 
|  211   // Assert that the packet sanity checks in InsertPacket method works. |  211   // Assert that the packet sanity checks in InsertPacket method works. | 
|  212   assert(packet && !packet->payload.empty()); |  212   RTC_DCHECK(packet && !packet->empty()); | 
|  213   buffer_.pop_front(); |  213   buffer_.pop_front(); | 
|  214  |  214  | 
|  215   // Discard other packets with the same timestamp. These are duplicates or |  215   // Discard other packets with the same timestamp. These are duplicates or | 
|  216   // redundant payloads that should not be used. |  216   // redundant payloads that should not be used. | 
|  217   size_t discards = 0; |  217   size_t discards = 0; | 
|  218  |  218  | 
|  219   while (!Empty() && |  219   while (!Empty() && | 
|  220       buffer_.front()->header.timestamp == packet->header.timestamp) { |  220       buffer_.front()->header.timestamp == packet->header.timestamp) { | 
|  221     if (DiscardNextPacket() != kOK) { |  221     if (DiscardNextPacket() != kOK) { | 
|  222       assert(false);  // Must be ok by design. |  222       assert(false);  // Must be ok by design. | 
|  223     } |  223     } | 
|  224     ++discards; |  224     ++discards; | 
|  225   } |  225   } | 
|  226   // The way of inserting packet should not cause any packet discarding here. |  226   // The way of inserting packet should not cause any packet discarding here. | 
|  227   // TODO(minyue): remove |discard_count|. |  227   // TODO(minyue): remove |discard_count|. | 
|  228   assert(discards == 0); |  228   assert(discards == 0); | 
|  229   if (discard_count) |  229   if (discard_count) | 
|  230     *discard_count = discards; |  230     *discard_count = discards; | 
|  231  |  231  | 
|  232   return packet; |  232   return packet; | 
|  233 } |  233 } | 
|  234  |  234  | 
|  235 int PacketBuffer::DiscardNextPacket() { |  235 int PacketBuffer::DiscardNextPacket() { | 
|  236   if (Empty()) { |  236   if (Empty()) { | 
|  237     return kBufferEmpty; |  237     return kBufferEmpty; | 
|  238   } |  238   } | 
|  239   // Assert that the packet sanity checks in InsertPacket method works. |  239   // Assert that the packet sanity checks in InsertPacket method works. | 
|  240   assert(buffer_.front()); |  240   RTC_DCHECK(buffer_.front()); | 
|  241   assert(!buffer_.front()->payload.empty()); |  241   RTC_DCHECK(!buffer_.front()->empty()); | 
|  242   DeleteFirstPacket(&buffer_); |  242   DeleteFirstPacket(&buffer_); | 
|  243   return kOK; |  243   return kOK; | 
|  244 } |  244 } | 
|  245  |  245  | 
|  246 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, |  246 int PacketBuffer::DiscardOldPackets(uint32_t timestamp_limit, | 
|  247                                     uint32_t horizon_samples) { |  247                                     uint32_t horizon_samples) { | 
|  248   while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && |  248   while (!Empty() && timestamp_limit != buffer_.front()->header.timestamp && | 
|  249          IsObsoleteTimestamp(buffer_.front()->header.timestamp, |  249          IsObsoleteTimestamp(buffer_.front()->header.timestamp, | 
|  250                              timestamp_limit, |  250                              timestamp_limit, | 
|  251                              horizon_samples)) { |  251                              horizon_samples)) { | 
|  252     if (DiscardNextPacket() != kOK) { |  252     if (DiscardNextPacket() != kOK) { | 
|  253       assert(false);  // Must be ok by design. |  253       assert(false);  // Must be ok by design. | 
|  254     } |  254     } | 
|  255   } |  255   } | 
|  256   return 0; |  256   return 0; | 
|  257 } |  257 } | 
|  258  |  258  | 
|  259 int PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit) { |  259 int PacketBuffer::DiscardAllOldPackets(uint32_t timestamp_limit) { | 
|  260   return DiscardOldPackets(timestamp_limit, 0); |  260   return DiscardOldPackets(timestamp_limit, 0); | 
|  261 } |  261 } | 
|  262  |  262  | 
 |  263 void PacketBuffer::DiscardPacketsWithPayloadType(uint8_t payload_type) { | 
 |  264   for (auto it = buffer_.begin(); it != buffer_.end(); /* */) { | 
 |  265     Packet *packet = *it; | 
 |  266     if (packet->header.payloadType == payload_type) { | 
 |  267       delete packet; | 
 |  268       it = buffer_.erase(it); | 
 |  269     } else { | 
 |  270       ++it; | 
 |  271     } | 
 |  272   } | 
 |  273 } | 
 |  274  | 
|  263 size_t PacketBuffer::NumPacketsInBuffer() const { |  275 size_t PacketBuffer::NumPacketsInBuffer() const { | 
|  264   return buffer_.size(); |  276   return buffer_.size(); | 
|  265 } |  277 } | 
|  266  |  278  | 
|  267 size_t PacketBuffer::NumSamplesInBuffer(DecoderDatabase* decoder_database, |  279 size_t PacketBuffer::NumSamplesInBuffer(size_t last_decoded_length) const { | 
|  268                                         size_t last_decoded_length) const { |  | 
|  269   PacketList::const_iterator it; |  | 
|  270   size_t num_samples = 0; |  280   size_t num_samples = 0; | 
|  271   size_t last_duration = last_decoded_length; |  281   size_t last_duration = last_decoded_length; | 
|  272   for (it = buffer_.begin(); it != buffer_.end(); ++it) { |  282   for (Packet* packet : buffer_) { | 
|  273     Packet* packet = (*it); |  283     if (packet->frame) { | 
|  274     AudioDecoder* decoder = |  | 
|  275         decoder_database->GetDecoder(packet->header.payloadType); |  | 
|  276     if (decoder) { |  | 
|  277       if (!packet->primary) { |  284       if (!packet->primary) { | 
|  278         continue; |  285         continue; | 
|  279       } |  286       } | 
|  280       int duration = decoder->PacketDuration(packet->payload.data(), |  287       size_t duration = packet->frame->Duration(); | 
|  281                                              packet->payload.size()); |  288       if (duration > 0) { | 
|  282       if (duration >= 0) { |  | 
|  283         last_duration = duration;  // Save the most up-to-date (valid) duration. |  289         last_duration = duration;  // Save the most up-to-date (valid) duration. | 
|  284       } |  290       } | 
|  285     } |  291     } | 
|  286     num_samples += last_duration; |  292     num_samples += last_duration; | 
|  287   } |  293   } | 
|  288   return num_samples; |  294   return num_samples; | 
|  289 } |  295 } | 
|  290  |  296  | 
|  291 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { |  297 bool PacketBuffer::DeleteFirstPacket(PacketList* packet_list) { | 
|  292   if (packet_list->empty()) { |  298   if (packet_list->empty()) { | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|  303     // Continue while the list is not empty. |  309     // Continue while the list is not empty. | 
|  304   } |  310   } | 
|  305 } |  311 } | 
|  306  |  312  | 
|  307 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { |  313 void PacketBuffer::BufferStat(int* num_packets, int* max_num_packets) const { | 
|  308   *num_packets = static_cast<int>(buffer_.size()); |  314   *num_packets = static_cast<int>(buffer_.size()); | 
|  309   *max_num_packets = static_cast<int>(max_number_of_packets_); |  315   *max_num_packets = static_cast<int>(max_number_of_packets_); | 
|  310 } |  316 } | 
|  311  |  317  | 
|  312 }  // namespace webrtc |  318 }  // namespace webrtc | 
| OLD | NEW |