OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <assert.h> | |
12 | |
13 #include "webrtc/modules/bitrate_controller/send_time_history.h" | |
14 | |
15 namespace webrtc { | |
16 | |
17 SendTimeHistory::SendTimeHistory(int64_t packet_age_limit) | |
18 : packet_age_limit_(packet_age_limit), oldest_sequence_number_(0) { | |
19 } | |
20 | |
21 SendTimeHistory::~SendTimeHistory() { | |
22 } | |
23 | |
24 void SendTimeHistory::Clear() { | |
25 history_.clear(); | |
26 } | |
27 | |
28 void SendTimeHistory::AddAndRemoveOldSendTimes(uint16_t sequence_number, | |
29 int64_t timestamp) { | |
30 EraseOld(timestamp - packet_age_limit_); | |
31 | |
32 if (history_.empty()) | |
33 oldest_sequence_number_ = sequence_number; | |
34 | |
35 history_[sequence_number] = timestamp; | |
36 } | |
37 | |
38 void SendTimeHistory::EraseOld(int64_t limit) { | |
39 while (!history_.empty()) { | |
40 auto it = history_.find(oldest_sequence_number_); | |
41 assert(it != history_.end()); | |
42 | |
43 if (it->second > limit) | |
44 return; // Oldest packet within age limit, return. | |
45 | |
46 // TODO(sprang): Warn if erasing (too many) old items? | |
47 history_.erase(it); | |
48 UpdateOldestSequenceNumber(); | |
49 } | |
50 } | |
51 | |
52 void SendTimeHistory::UpdateOldestSequenceNumber() { | |
53 // After removing an element from the map, update oldest_sequence_number_ to | |
54 // the element with the lowest sequence number higher than the previous | |
55 // value (there might be gaps). | |
56 if (history_.empty()) | |
57 return; | |
58 auto it = history_.upper_bound(oldest_sequence_number_); | |
59 if (it == history_.end()) { | |
60 // No element with higher sequence number than oldest_sequence_number_ | |
61 // 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. | |
63 it = history_.find(0); | |
64 if (it == history_.end()) | |
65 it = history_.upper_bound(0); | |
66 } | |
67 assert(it != history_.end()); | |
68 oldest_sequence_number_ = it->first; | |
69 } | |
70 | |
71 bool SendTimeHistory::GetSendTime(uint16_t sequence_number, | |
72 int64_t* timestamp, | |
73 bool remove) { | |
74 auto it = history_.find(sequence_number); | |
75 if (it == history_.end()) | |
76 return false; | |
77 *timestamp = it->second; | |
78 if (remove) { | |
79 history_.erase(it); | |
80 if (sequence_number == oldest_sequence_number_) | |
81 UpdateOldestSequenceNumber(); | |
82 } | |
83 return true; | |
84 } | |
85 | |
86 } // namespace webrtc | |
OLD | NEW |