Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: webrtc/rtc_base/moving_max_counter.h

Issue 2995143002: Report max interframe delay over window insdead of interframe delay sum (Closed)
Patch Set: Implement more Kwiberg@ comments Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/rtc_base/BUILD.gn ('k') | webrtc/rtc_base/moving_max_counter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2017 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 #ifndef WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_
12 #define WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_
13
14 #include <stdint.h>
15
16 #include <deque>
17 #include <limits>
18 #include <utility>
19
20 #include "webrtc/rtc_base/checks.h"
21 #include "webrtc/rtc_base/constructormagic.h"
22 #include "webrtc/rtc_base/optional.h"
23
24 namespace rtc {
25
26 // Implements moving max: can add samples to it and calculate maximum over some
27 // fixed moving window.
28 //
29 // Window size is configured at constructor.
30 // Samples can be added with |Add()| and max over current window is returned by
31 // |MovingMax|. |current_time_ms| in successive calls to Add and MovingMax
32 // should never decrease as if it's a wallclock time.
33 // Not thread-safe.
kwiberg-webrtc 2017/08/23 09:32:14 Now that the mutable members are gone and you don'
ilnik 2017/08/23 09:57:56 Done.
34 template <class T>
35 class MovingMaxCounter {
36 public:
37 explicit MovingMaxCounter(int64_t window_length_ms);
38 // Advances the current time, and adds a new sample. The new current time must
39 // be at least as large as the old current time.
40 void Add(const T& sample, int64_t current_time_ms);
41 // Advances the current time, and returns the maximum sample in the time
42 // window ending at the current time. The new current time must be at least as
43 // large as the old current time.
44 rtc::Optional<T> Max(int64_t current_time_ms);
45 void Reset();
46
47 private:
48 // Throws out obsolete samples.
49 void RollWindow(int64_t new_time_ms);
50 const int64_t window_length_ms_;
51 // Samples in the window are stored as pairs <time, sample> in this deque in
52 // chronological order. Samples what can't be maximum in any window are
53 // thrown out - deque always contains non-increasing sequence of samples.
kwiberg-webrtc 2017/08/23 09:32:14 I made an attempt to rephrase this: "This deque s
ilnik 2017/08/23 09:57:56 Done.
54 std::deque<std::pair<int64_t, T>> samples_;
55 #if RTC_DCHECK_IS_ON
56 int64_t last_call_time_ms_ = std::numeric_limits<int64_t>::min();
57 #endif
58 RTC_DISALLOW_COPY_AND_ASSIGN(MovingMaxCounter);
59 };
60
61 template <class T>
62 MovingMaxCounter<T>::MovingMaxCounter(int64_t window_length_ms)
63 : window_length_ms_(window_length_ms) {}
64
65 template <class T>
66 void MovingMaxCounter<T>::Add(const T& sample, int64_t current_time_ms) {
67 #if RTC_DCHECK_IS_ON
68 RTC_DCHECK_GE(current_time_ms, last_call_time_ms_);
69 last_call_time_ms_ = current_time_ms;
70 #endif
71 RollWindow(current_time_ms);
kwiberg-webrtc 2017/08/23 09:32:14 Hmm. You repeat lines 67-71 exactly below in the M
ilnik 2017/08/23 09:57:56 Done.
72 // Remove samples what will never be maximum in any window: newly added sample
kwiberg-webrtc 2017/08/23 09:32:14 samples that will never
ilnik 2017/08/23 09:57:56 Done.
73 // will always be in all windows the previous samples are. Thus, smaller
74 // samples could be removed. This will maintain the invariant - deque contains
75 // non-increasing sequence of values.
76 while (!samples_.empty() && samples_.back().second < sample) {
77 samples_.pop_back();
78 }
kwiberg-webrtc 2017/08/23 09:32:13 Hmm. Why not samples_.back().second <= sample
ilnik 2017/08/23 09:57:56 You are right. It doesn't affect the results, but
79 // Add the new sample but only if there's no existing sample at the same time.
80 // Due to checks above, already existing element will be larger and new sample
81 // will never be a maximum in any window.
82 if (samples_.empty() || samples_.back().first < current_time_ms) {
83 samples_.emplace_back(std::make_pair(current_time_ms, sample));
84 }
85 }
86
87 template <class T>
88 rtc::Optional<T> MovingMaxCounter<T>::Max(int64_t current_time_ms) {
89 #if RTC_DCHECK_IS_ON
90 RTC_DCHECK_GE(current_time_ms, last_call_time_ms_);
91 last_call_time_ms_ = current_time_ms;
92 #endif
93 RollWindow(current_time_ms);
94 rtc::Optional<T> res;
95 if (!samples_.empty()) {
96 res.emplace(samples_.front().second);
97 }
98 return res;
99 }
100
101 template <class T>
102 void MovingMaxCounter<T>::Reset() {
103 samples_.clear();
104 }
105
106 template <class T>
107 void MovingMaxCounter<T>::RollWindow(int64_t new_time_ms) {
108 const int64_t window_begin_ms = new_time_ms - window_length_ms_;
109 auto it = samples_.begin();
110 while (it != samples_.end() && it->first < window_begin_ms) {
111 ++it;
112 }
113 samples_.erase(samples_.begin(), it);
114 }
115
116 } // namespace rtc
117
118 #endif // WEBRTC_RTC_BASE_MOVING_MAX_COUNTER_H_
OLDNEW
« no previous file with comments | « webrtc/rtc_base/BUILD.gn ('k') | webrtc/rtc_base/moving_max_counter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698