| 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), packet_age_limit_ms_(packet_age_limit_ms) {} |
| 19 packet_age_limit_(packet_age_limit), | |
| 20 oldest_sequence_number_(0) {} | |
| 21 | 21 |
| 22 SendTimeHistory::~SendTimeHistory() { | 22 SendTimeHistory::~SendTimeHistory() {} |
| 23 } | |
| 24 | 23 |
| 25 void SendTimeHistory::Clear() { | 24 void SendTimeHistory::Clear() { |
| 26 history_.clear(); | 25 history_.clear(); |
| 27 } | 26 } |
| 28 | 27 |
| 29 void SendTimeHistory::AddAndRemoveOld(uint16_t sequence_number, | 28 void SendTimeHistory::AddAndRemoveOld(uint16_t sequence_number, |
| 30 size_t length, | 29 size_t payload_size, |
| 31 int probe_cluster_id) { | 30 int probe_cluster_id) { |
| 32 EraseOld(); | 31 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 32 // Remove old. |
| 33 while (!history_.empty() && |
| 34 now_ms - history_.begin()->second.creation_time_ms > |
| 35 packet_age_limit_ms_) { |
| 36 // TODO(sprang): Warn if erasing (too many) old items? |
| 37 history_.erase(history_.begin()); |
| 38 } |
| 33 | 39 |
| 34 if (history_.empty()) | 40 // Add new. |
| 35 oldest_sequence_number_ = sequence_number; | 41 int64_t unwrapped_seq_num = seq_num_unwrapper_.Unwrap(sequence_number); |
| 36 | 42 int64_t creation_time_ms = now_ms; |
| 37 history_.insert(std::pair<uint16_t, PacketInfo>( | 43 constexpr int64_t kNoArrivalTimeMs = -1; // Arrival time is ignored. |
| 38 sequence_number, PacketInfo(clock_->TimeInMilliseconds(), 0, -1, | 44 constexpr int64_t kNoSendTimeMs = -1; // Send time is set by OnSentPacket. |
| 39 sequence_number, length, probe_cluster_id))); | 45 history_.insert(std::make_pair( |
| 46 unwrapped_seq_num, |
| 47 PacketInfo(creation_time_ms, kNoArrivalTimeMs, kNoSendTimeMs, |
| 48 sequence_number, payload_size, probe_cluster_id))); |
| 40 } | 49 } |
| 41 | 50 |
| 42 bool SendTimeHistory::OnSentPacket(uint16_t sequence_number, | 51 bool SendTimeHistory::OnSentPacket(uint16_t sequence_number, |
| 43 int64_t send_time_ms) { | 52 int64_t send_time_ms) { |
| 44 auto it = history_.find(sequence_number); | 53 int64_t unwrapped_seq_num = seq_num_unwrapper_.Unwrap(sequence_number); |
| 54 auto it = history_.find(unwrapped_seq_num); |
| 45 if (it == history_.end()) | 55 if (it == history_.end()) |
| 46 return false; | 56 return false; |
| 47 it->second.send_time_ms = send_time_ms; | 57 it->second.send_time_ms = send_time_ms; |
| 48 return true; | 58 return true; |
| 49 } | 59 } |
| 50 | 60 |
| 51 void SendTimeHistory::EraseOld() { | 61 bool SendTimeHistory::GetInfo(PacketInfo* packet_info, bool remove) { |
| 52 while (!history_.empty()) { | 62 RTC_DCHECK(packet_info); |
| 53 auto it = history_.find(oldest_sequence_number_); | 63 int64_t unwrapped_seq_num = |
| 54 assert(it != history_.end()); | 64 seq_num_unwrapper_.Unwrap(packet_info->sequence_number); |
| 55 | 65 auto it = history_.find(unwrapped_seq_num); |
| 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 } | |
| 66 | |
| 67 void SendTimeHistory::UpdateOldestSequenceNumber() { | |
| 68 // After removing an element from the map, update oldest_sequence_number_ to | |
| 69 // the element with the lowest sequence number higher than the previous | |
| 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()) | 66 if (it == history_.end()) |
| 89 return false; | 67 return false; |
| 90 int64_t receive_time = packet->arrival_time_ms; | 68 |
| 91 *packet = it->second; | 69 // Save arrival_time not to overwrite it. |
| 92 packet->arrival_time_ms = receive_time; | 70 int64_t arrival_time_ms = packet_info->arrival_time_ms; |
| 93 if (remove) { | 71 *packet_info = it->second; |
| 72 packet_info->arrival_time_ms = arrival_time_ms; |
| 73 |
| 74 if (remove) |
| 94 history_.erase(it); | 75 history_.erase(it); |
| 95 if (packet->sequence_number == oldest_sequence_number_) | |
| 96 UpdateOldestSequenceNumber(); | |
| 97 } | |
| 98 return true; | 76 return true; |
| 99 } | 77 } |
| 100 | 78 |
| 101 } // namespace webrtc | 79 } // namespace webrtc |
| OLD | NEW |