| 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 <assert.h> |
| 12 | 12 |
| 13 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" | 13 #include "webrtc/modules/remote_bitrate_estimator/include/send_time_history.h" |
| 14 | 14 |
| 15 namespace webrtc { | 15 namespace webrtc { |
| 16 | 16 |
| 17 SendTimeHistory::SendTimeHistory(int64_t packet_age_limit) | 17 SendTimeHistory::SendTimeHistory(int64_t packet_age_limit) |
| 18 : packet_age_limit_(packet_age_limit), oldest_sequence_number_(0) { | 18 : packet_age_limit_(packet_age_limit), oldest_sequence_number_(0) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 SendTimeHistory::~SendTimeHistory() { | 21 SendTimeHistory::~SendTimeHistory() { |
| 22 } | 22 } |
| 23 | 23 |
| 24 void SendTimeHistory::Clear() { | 24 void SendTimeHistory::Clear() { |
| 25 history_.clear(); | 25 history_.clear(); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void SendTimeHistory::AddAndRemoveOldSendTimes(uint16_t sequence_number, | 28 void SendTimeHistory::AddAndRemoveOld(const PacketInfo& packet) { |
| 29 int64_t timestamp) { | 29 EraseOld(packet.send_time_ms - packet_age_limit_); |
| 30 EraseOld(timestamp - packet_age_limit_); | |
| 31 | 30 |
| 32 if (history_.empty()) | 31 if (history_.empty()) |
| 33 oldest_sequence_number_ = sequence_number; | 32 oldest_sequence_number_ = packet.sequence_number; |
| 34 | 33 |
| 35 history_[sequence_number] = timestamp; | 34 history_.insert( |
| 35 std::pair<uint16_t, PacketInfo>(packet.sequence_number, packet)); |
| 36 } |
| 37 |
| 38 bool SendTimeHistory::UpdateSendTime(uint16_t sequence_number, |
| 39 int64_t send_time_ms) { |
| 40 auto it = history_.find(sequence_number); |
| 41 if (it == history_.end()) |
| 42 return false; |
| 43 it->second.send_time_ms = send_time_ms; |
| 44 return true; |
| 36 } | 45 } |
| 37 | 46 |
| 38 void SendTimeHistory::EraseOld(int64_t limit) { | 47 void SendTimeHistory::EraseOld(int64_t limit) { |
| 39 while (!history_.empty()) { | 48 while (!history_.empty()) { |
| 40 auto it = history_.find(oldest_sequence_number_); | 49 auto it = history_.find(oldest_sequence_number_); |
| 41 assert(it != history_.end()); | 50 assert(it != history_.end()); |
| 42 | 51 |
| 43 if (it->second > limit) | 52 if (it->second.send_time_ms > limit) |
| 44 return; // Oldest packet within age limit, return. | 53 return; // Oldest packet within age limit, return. |
| 45 | 54 |
| 46 // TODO(sprang): Warn if erasing (too many) old items? | 55 // TODO(sprang): Warn if erasing (too many) old items? |
| 47 history_.erase(it); | 56 history_.erase(it); |
| 48 UpdateOldestSequenceNumber(); | 57 UpdateOldestSequenceNumber(); |
| 49 } | 58 } |
| 50 } | 59 } |
| 51 | 60 |
| 52 void SendTimeHistory::UpdateOldestSequenceNumber() { | 61 void SendTimeHistory::UpdateOldestSequenceNumber() { |
| 53 // After removing an element from the map, update oldest_sequence_number_ to | 62 // After removing an element from the map, update oldest_sequence_number_ to |
| 54 // the element with the lowest sequence number higher than the previous | 63 // the element with the lowest sequence number higher than the previous |
| 55 // value (there might be gaps). | 64 // value (there might be gaps). |
| 56 if (history_.empty()) | 65 if (history_.empty()) |
| 57 return; | 66 return; |
| 58 auto it = history_.upper_bound(oldest_sequence_number_); | 67 auto it = history_.upper_bound(oldest_sequence_number_); |
| 59 if (it == history_.end()) { | 68 if (it == history_.end()) { |
| 60 // No element with higher sequence number than oldest_sequence_number_ | 69 // No element with higher sequence number than oldest_sequence_number_ |
| 61 // found, check wrap around. Note that history_.upper_bound(0) will not | 70 // found, check wrap around. Note that history_.upper_bound(0) will not |
| 62 // find 0 even if it is there, need to explicitly check for 0. | 71 // find 0 even if it is there, need to explicitly check for 0. |
| 63 it = history_.find(0); | 72 it = history_.find(0); |
| 64 if (it == history_.end()) | 73 if (it == history_.end()) |
| 65 it = history_.upper_bound(0); | 74 it = history_.upper_bound(0); |
| 66 } | 75 } |
| 67 assert(it != history_.end()); | 76 assert(it != history_.end()); |
| 68 oldest_sequence_number_ = it->first; | 77 oldest_sequence_number_ = it->first; |
| 69 } | 78 } |
| 70 | 79 |
| 71 bool SendTimeHistory::GetSendTime(uint16_t sequence_number, | 80 bool SendTimeHistory::GetInfo(PacketInfo* packet, bool remove) { |
| 72 int64_t* timestamp, | 81 auto it = history_.find(packet->sequence_number); |
| 73 bool remove) { | |
| 74 auto it = history_.find(sequence_number); | |
| 75 if (it == history_.end()) | 82 if (it == history_.end()) |
| 76 return false; | 83 return false; |
| 77 *timestamp = it->second; | 84 int64_t receive_time = packet->arrival_time_ms; |
| 85 *packet = it->second; |
| 86 packet->arrival_time_ms = receive_time; |
| 78 if (remove) { | 87 if (remove) { |
| 79 history_.erase(it); | 88 history_.erase(it); |
| 80 if (sequence_number == oldest_sequence_number_) | 89 if (packet->sequence_number == oldest_sequence_number_) |
| 81 UpdateOldestSequenceNumber(); | 90 UpdateOldestSequenceNumber(); |
| 82 } | 91 } |
| 83 return true; | 92 return true; |
| 84 } | 93 } |
| 85 | 94 |
| 86 } // namespace webrtc | 95 } // namespace webrtc |
| OLD | NEW |