OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 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 "webrtc/video/stats_counter.h" | |
12 | |
13 #include <algorithm> | |
14 | |
15 #include "webrtc/system_wrappers/include/clock.h" | |
16 | |
17 namespace webrtc { | |
18 | |
19 namespace { | |
20 // Periodic time interval for processing samples. | |
21 const int64_t kProcessIntervalMs = 2000; | |
22 } // namespace | |
23 | |
24 StatsCounter::AggStats::Stats::Stats() | |
25 : num_samples(0), min(-1), max(-1), average(-1) {} | |
26 | |
27 StatsCounter::AggStats::AggStats() : sum_(0) {} | |
28 | |
29 void StatsCounter::AggStats::Add(int sample) { | |
30 sum_ += sample; | |
31 ++stats_.num_samples; | |
32 if (stats_.num_samples == 1) { | |
33 stats_.min = sample; | |
34 stats_.max = sample; | |
35 } | |
36 stats_.min = std::min(sample, stats_.min); | |
37 stats_.max = std::max(sample, stats_.max); | |
38 } | |
39 | |
40 StatsCounter::AggStats::Stats StatsCounter::AggStats::ComputeStats() { | |
41 Compute(); | |
42 return stats_; | |
43 } | |
44 | |
45 void StatsCounter::AggStats::Compute() { | |
46 if (stats_.num_samples == 0) | |
47 return; | |
48 | |
49 stats_.average = (sum_ + (stats_.num_samples / 2)) / stats_.num_samples; | |
stefan-webrtc
2016/05/06 11:10:17
Remove parentheses around stats_.num_samples / 2
åsapersson
2016/05/09 14:36:50
Done.
| |
50 } | |
51 | |
52 // StatsCounter class. | |
53 StatsCounter::StatsCounter(Clock* clock, | |
54 bool include_empty_intervals, | |
55 StatsCounterObserver* observer) | |
56 : sum_(0), | |
57 num_samples_(0), | |
58 last_sum_(0), | |
59 clock_(clock), | |
60 include_empty_intervals_(include_empty_intervals), | |
61 observer_(observer), | |
62 last_process_time_ms_(-1) {} | |
63 | |
64 StatsCounter::AggStats::Stats StatsCounter::GetStats() { | |
65 return agg_stats_.ComputeStats(); | |
66 } | |
67 | |
68 bool StatsCounter::TimeToProcess() { | |
69 int64_t now = clock_->TimeInMilliseconds(); | |
70 if (last_process_time_ms_ == -1) | |
71 last_process_time_ms_ = now; | |
72 | |
73 int64_t diff_ms = now - last_process_time_ms_; | |
74 if (diff_ms < kProcessIntervalMs) | |
75 return false; | |
76 | |
77 // Advance number of complete kProcessIntervalMs that have passed. | |
78 int64_t num_intervals = diff_ms / kProcessIntervalMs; | |
79 last_process_time_ms_ += num_intervals * kProcessIntervalMs; | |
80 | |
81 // Add zero for intervals without samples. | |
82 if (include_empty_intervals_) { | |
83 for (int64_t i = 0; i < num_intervals - 1; ++i) { | |
84 agg_stats_.Add(0); | |
85 if (observer_) | |
86 observer_->OnMetricUpdated(0); | |
87 } | |
88 } | |
89 return true; | |
90 } | |
91 | |
92 void StatsCounter::Set(int sample) { | |
93 Process(); | |
94 ++num_samples_; | |
95 sum_ = sample; | |
96 } | |
97 | |
98 void StatsCounter::Add(int sample) { | |
99 Process(); | |
100 ++num_samples_; | |
101 sum_ += sample; | |
102 } | |
103 | |
104 void StatsCounter::Process() { | |
105 if (!TimeToProcess()) | |
106 return; | |
107 | |
108 int metric; | |
109 if (GetMetric(&metric)) { | |
110 agg_stats_.Add(metric); | |
111 if (observer_) | |
112 observer_->OnMetricUpdated(metric); | |
113 } | |
114 last_sum_ = sum_; | |
115 sum_ = 0; | |
116 num_samples_ = 0; | |
117 } | |
118 | |
119 // StatsCounter sub-classes. | |
120 AvgCounter::AvgCounter(Clock* clock, StatsCounterObserver* observer) | |
121 : StatsCounter(clock, | |
122 false, // |include_empty_intervals| | |
123 observer) {} | |
124 | |
125 void AvgCounter::Add(int sample) { | |
126 StatsCounter::Add(sample); | |
127 } | |
128 | |
129 bool AvgCounter::GetMetric(int* metric) const { | |
130 if (num_samples_ == 0) | |
131 return false; | |
132 *metric = (sum_ + (num_samples_ / 2)) / num_samples_; | |
stefan-webrtc
2016/05/06 11:10:17
Remove parentheses for the second term here and be
åsapersson
2016/05/09 14:36:50
Done.
| |
133 return true; | |
134 } | |
135 | |
136 PercentCounter::PercentCounter(Clock* clock, StatsCounterObserver* observer) | |
137 : StatsCounter(clock, | |
138 false, // |include_empty_intervals| | |
139 observer) {} | |
140 | |
141 void PercentCounter::Add(bool sample) { | |
142 StatsCounter::Add(sample ? 1 : 0); | |
143 } | |
144 | |
145 bool PercentCounter::GetMetric(int* metric) const { | |
146 if (num_samples_ == 0) | |
147 return false; | |
148 *metric = (sum_ * 100 + (num_samples_ / 2)) / num_samples_; | |
149 return true; | |
150 } | |
151 | |
152 PermilleCounter::PermilleCounter(Clock* clock, StatsCounterObserver* observer) | |
153 : StatsCounter(clock, | |
154 false, // |include_empty_intervals| | |
155 observer) {} | |
156 | |
157 void PermilleCounter::Add(bool sample) { | |
158 StatsCounter::Add(sample ? 1 : 0); | |
159 } | |
160 | |
161 bool PermilleCounter::GetMetric(int* metric) const { | |
162 if (num_samples_ == 0) | |
163 return false; | |
164 *metric = (sum_ * 1000 + (num_samples_ / 2)) / num_samples_; | |
165 return true; | |
166 } | |
167 | |
168 RateCounter::RateCounter(Clock* clock, StatsCounterObserver* observer) | |
169 : StatsCounter(clock, | |
170 true, // |include_empty_intervals| | |
171 observer) {} | |
172 | |
173 void RateCounter::Add(int sample) { | |
174 StatsCounter::Add(sample); | |
175 } | |
176 | |
177 bool RateCounter::GetMetric(int* metric) const { | |
178 if (num_samples_ == 0) | |
179 return false; | |
180 *metric = (sum_ * 1000 + (kProcessIntervalMs / 2)) / kProcessIntervalMs; | |
181 return true; | |
182 } | |
183 | |
184 RateAccCounter::RateAccCounter(Clock* clock, StatsCounterObserver* observer) | |
185 : StatsCounter(clock, | |
186 true, // |include_empty_intervals| | |
187 observer) {} | |
188 | |
189 void RateAccCounter::Set(int sample) { | |
190 StatsCounter::Set(sample); | |
191 } | |
192 | |
193 bool RateAccCounter::GetMetric(int* metric) const { | |
194 if (num_samples_ == 0 || last_sum_ > sum_) | |
195 return false; | |
196 *metric = ((sum_ - last_sum_) * 1000 + (kProcessIntervalMs / 2)) / | |
197 kProcessIntervalMs; | |
198 return true; | |
199 } | |
200 | |
201 } // namespace webrtc | |
OLD | NEW |