Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 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 <assert.h> | 11 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
| 12 | 12 |
| 13 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 13 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | |
| 15 #include "webrtc/system_wrappers/include/clock.h" | |
| 14 | 16 |
| 15 namespace webrtc { | 17 namespace webrtc { |
| 16 | 18 |
| 17 SendTimeHistory::SendTimeHistory(Clock* clock, int64_t packet_age_limit) | 19 SendTimeHistory::SendTimeHistory(Clock* clock, int64_t packet_age_limit_ms) |
| 18 : clock_(clock), | 20 : clock_(clock), |
| 19 packet_age_limit_(packet_age_limit), | 21 packet_age_limit_ms_(packet_age_limit_ms), |
| 20 oldest_sequence_number_(0) {} | 22 oldest_packet_info_(history_.end()) {} |
| 21 | 23 |
| 22 SendTimeHistory::~SendTimeHistory() { | 24 SendTimeHistory::~SendTimeHistory() {} |
| 23 } | |
| 24 | 25 |
| 25 void SendTimeHistory::Clear() { | 26 void SendTimeHistory::Clear() { |
| 26 history_.clear(); | 27 history_.clear(); |
| 28 oldest_packet_info_ = history_.end(); | |
| 27 } | 29 } |
| 28 | 30 |
| 29 void SendTimeHistory::AddAndRemoveOld(uint16_t sequence_number, | 31 void SendTimeHistory::AddAndRemoveOld(uint16_t sequence_number, |
| 30 size_t length, | 32 size_t payload_size, |
| 31 int probe_cluster_id) { | 33 int probe_cluster_id) { |
| 32 EraseOld(); | 34 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 35 // Remove old. | |
| 36 while (!history_.empty() && | |
|
philipel
2016/06/21 09:48:30
Is the packet with the oldest sequence number also
danilchap
2016/06/21 11:45:30
might not be the case when packets arrive out of o
philipel
2016/06/21 12:07:14
Acknowledged.
| |
| 37 now_ms - oldest_packet_info_->second.creation_time_ms > | |
| 38 packet_age_limit_ms_) { | |
| 39 // TODO(sprang): Warn if erasing (too many) old items? | |
| 40 EraseOldestPacketInfo(); | |
|
philipel
2016/06/21 09:48:30
Remove 'EraseOldestPacketInfo' and just do 'histor
danilchap
2016/06/21 11:45:30
Done.
| |
| 41 } | |
| 33 | 42 |
| 34 if (history_.empty()) | 43 RTC_DCHECK_EQ(history_.empty(), oldest_packet_info_ == history_.end()); |
| 35 oldest_sequence_number_ = sequence_number; | |
| 36 | 44 |
| 45 // Add new. | |
| 46 int64_t creation_time_ms = now_ms; | |
| 47 constexpr int64_t kNoArrivalTimeMs = 0; // Arrival time is ignored. | |
| 48 constexpr int64_t kNoSendTimeMs = -1; // Send time is set by OnSentPacket. | |
| 37 history_.insert(std::pair<uint16_t, PacketInfo>( | 49 history_.insert(std::pair<uint16_t, PacketInfo>( |
| 38 sequence_number, PacketInfo(clock_->TimeInMilliseconds(), 0, -1, | 50 sequence_number, |
| 39 sequence_number, length, probe_cluster_id))); | 51 PacketInfo(creation_time_ms, kNoArrivalTimeMs, kNoSendTimeMs, |
| 52 sequence_number, payload_size, probe_cluster_id))); | |
| 53 | |
| 54 // Set as oldest if it was the first packet info. | |
| 55 if (oldest_packet_info_ == history_.end()) | |
| 56 oldest_packet_info_ = history_.begin(); | |
| 40 } | 57 } |
| 41 | 58 |
| 42 bool SendTimeHistory::OnSentPacket(uint16_t sequence_number, | 59 bool SendTimeHistory::OnSentPacket(uint16_t sequence_number, |
| 43 int64_t send_time_ms) { | 60 int64_t send_time_ms) { |
| 44 auto it = history_.find(sequence_number); | 61 auto it = history_.find(sequence_number); |
| 45 if (it == history_.end()) | 62 if (it == history_.end()) |
| 46 return false; | 63 return false; |
| 47 it->second.send_time_ms = send_time_ms; | 64 it->second.send_time_ms = send_time_ms; |
| 48 return true; | 65 return true; |
| 49 } | 66 } |
| 50 | 67 |
| 51 void SendTimeHistory::EraseOld() { | 68 void SendTimeHistory::EraseOldestPacketInfo() { |
| 52 while (!history_.empty()) { | 69 RTC_DCHECK(oldest_packet_info_ != history_.end()); |
| 53 auto it = history_.find(oldest_sequence_number_); | 70 history_.erase(oldest_packet_info_++); |
| 54 assert(it != history_.end()); | 71 if (oldest_packet_info_ == history_.end()) |
| 55 | 72 oldest_packet_info_ = history_.begin(); // Wrap around. |
| 56 if (clock_->TimeInMilliseconds() - it->second.creation_time_ms <= | |
| 57 packet_age_limit_) { | |
| 58 return; // Oldest packet within age limit, return. | |
| 59 } | |
| 60 | |
| 61 // TODO(sprang): Warn if erasing (too many) old items? | |
| 62 history_.erase(it); | |
| 63 UpdateOldestSequenceNumber(); | |
| 64 } | |
| 65 } | 73 } |
| 66 | 74 |
| 67 void SendTimeHistory::UpdateOldestSequenceNumber() { | 75 bool SendTimeHistory::GetInfo(PacketInfo* packet_info, bool remove) { |
| 68 // After removing an element from the map, update oldest_sequence_number_ to | 76 RTC_DCHECK(packet_info); |
| 69 // the element with the lowest sequence number higher than the previous | 77 auto it = history_.find(packet_info->sequence_number); |
| 70 // value (there might be gaps). | |
| 71 if (history_.empty()) | |
| 72 return; | |
| 73 auto it = history_.upper_bound(oldest_sequence_number_); | |
| 74 if (it == history_.end()) { | |
| 75 // No element with higher sequence number than oldest_sequence_number_ | |
| 76 // found, check wrap around. Note that history_.upper_bound(0) will not | |
| 77 // find 0 even if it is there, need to explicitly check for 0. | |
| 78 it = history_.find(0); | |
| 79 if (it == history_.end()) | |
| 80 it = history_.upper_bound(0); | |
| 81 } | |
| 82 assert(it != history_.end()); | |
| 83 oldest_sequence_number_ = it->first; | |
| 84 } | |
| 85 | |
| 86 bool SendTimeHistory::GetInfo(PacketInfo* packet, bool remove) { | |
| 87 auto it = history_.find(packet->sequence_number); | |
| 88 if (it == history_.end()) | 78 if (it == history_.end()) |
| 89 return false; | 79 return false; |
| 90 int64_t receive_time = packet->arrival_time_ms; | 80 |
| 91 *packet = it->second; | 81 // Save arrival_time not to overwrite it. |
| 92 packet->arrival_time_ms = receive_time; | 82 int64_t arrival_time_ms = packet_info->arrival_time_ms; |
| 83 *packet_info = it->second; | |
| 84 packet_info->arrival_time_ms = arrival_time_ms; | |
| 85 | |
| 93 if (remove) { | 86 if (remove) { |
| 94 history_.erase(it); | 87 if (it == oldest_packet_info_) |
| 95 if (packet->sequence_number == oldest_sequence_number_) | 88 EraseOldestPacketInfo(); |
| 96 UpdateOldestSequenceNumber(); | 89 else |
| 90 history_.erase(it); | |
| 97 } | 91 } |
| 98 return true; | 92 return true; |
| 99 } | 93 } |
| 100 | 94 |
| 101 } // namespace webrtc | 95 } // namespace webrtc |
| OLD | NEW |