Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 #ifndef WEBRTC_VIDEO_STATS_COUNTER_H_ | 11 #ifndef WEBRTC_VIDEO_STATS_COUNTER_H_ |
| 12 #define WEBRTC_VIDEO_STATS_COUNTER_H_ | 12 #define WEBRTC_VIDEO_STATS_COUNTER_H_ |
| 13 | 13 |
| 14 #include <limits> | |
| 15 #include <map> | |
| 14 #include <memory> | 16 #include <memory> |
| 15 | 17 |
| 16 #include "webrtc/base/constructormagic.h" | 18 #include "webrtc/base/constructormagic.h" |
| 17 #include "webrtc/typedefs.h" | 19 #include "webrtc/typedefs.h" |
| 18 | 20 |
| 19 namespace webrtc { | 21 namespace webrtc { |
| 20 | 22 |
| 21 class AggregatedCounter; | 23 class AggregatedCounter; |
| 22 class Clock; | 24 class Clock; |
| 23 | 25 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 // counter.Add(7); | 69 // counter.Add(7); |
| 68 // counter.Add(3); // process interval passed -> GetMetric() avg:5 | 70 // counter.Add(3); // process interval passed -> GetMetric() avg:5 |
| 69 // counter.Add(10); | 71 // counter.Add(10); |
| 70 // counter.Add(20); // process interval passed -> GetMetric() avg:15 | 72 // counter.Add(20); // process interval passed -> GetMetric() avg:15 |
| 71 // AggregatedStats stats = counter.GetStats(); | 73 // AggregatedStats stats = counter.GetStats(); |
| 72 // stats: {min:4, max:15, avg:8} | 74 // stats: {min:4, max:15, avg:8} |
| 73 // | 75 // |
| 74 | 76 |
| 75 // Note: StatsCounter takes ownership of |observer|. | 77 // Note: StatsCounter takes ownership of |observer|. |
| 76 | 78 |
| 77 class StatsCounter { | 79 class StatsCounter { |
|
stefan-webrtc
2016/08/19 14:36:56
Would it be cleaner to have an StreamsStatsCounter
åsapersson
2016/08/23 09:06:02
Updated the code a bit, ptal. GetMetric is returni
| |
| 78 public: | 80 public: |
| 79 virtual ~StatsCounter(); | 81 virtual ~StatsCounter(); |
| 80 | 82 |
| 81 virtual bool GetMetric(int* metric) const = 0; | 83 virtual bool GetMetric(int* metric) const = 0; |
| 82 | 84 |
| 83 AggregatedStats GetStats(); | 85 AggregatedStats GetStats(); |
| 84 | 86 |
| 87 // Holds gathered samples within a process interval. | |
| 88 struct Samples { | |
| 89 void Add(int sample); | |
| 90 void Set(int sample); | |
| 91 void Reset(); | |
| 92 | |
| 93 int max_ = std::numeric_limits<int>::min(); | |
| 94 int64_t num_ = 0; | |
| 95 int64_t sum_ = 0; | |
| 96 int64_t last_sum_ = 0; | |
| 97 }; | |
| 98 | |
| 85 protected: | 99 protected: |
| 86 StatsCounter(Clock* clock, | 100 StatsCounter(Clock* clock, |
| 87 bool include_empty_intervals, | 101 bool include_empty_intervals, |
| 88 StatsCounterObserver* observer); | 102 StatsCounterObserver* observer); |
| 89 | 103 |
| 90 void Add(int sample); | 104 void Add(int sample); |
| 91 void Set(int sample); | 105 void Set(int sample, uint32_t ssrc); |
| 92 | 106 |
| 93 int max_; | 107 const bool include_empty_intervals_; |
| 94 int64_t sum_; | 108 std::map<uint32_t, Samples> samples_; // Gathered samples mapped by SSRC. |
| 95 int64_t num_samples_; | |
| 96 int64_t last_sum_; | |
| 97 | 109 |
| 98 private: | 110 private: |
| 99 bool TimeToProcess(); | 111 bool TimeToProcess(); |
| 100 void TryProcess(); | 112 void TryProcess(); |
| 101 | 113 |
| 102 Clock* const clock_; | 114 Clock* const clock_; |
| 103 const bool include_empty_intervals_; | |
| 104 const std::unique_ptr<StatsCounterObserver> observer_; | 115 const std::unique_ptr<StatsCounterObserver> observer_; |
| 105 const std::unique_ptr<AggregatedCounter> aggregated_counter_; | 116 const std::unique_ptr<AggregatedCounter> aggregated_counter_; |
| 106 int64_t last_process_time_ms_; | 117 int64_t last_process_time_ms_; |
| 107 }; | 118 }; |
| 108 | 119 |
| 109 // AvgCounter: average of samples | 120 // AvgCounter: average of samples |
| 110 // | 121 // |
| 111 // | * * * | * * | ... | 122 // | * * * | * * | ... |
| 112 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | | 123 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 113 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | | 124 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 // |<------ 2 sec ------->| | | 222 // |<------ 2 sec ------->| | |
| 212 // GetMetric | 8 / 2 | (13 - 8) / 2 | | 223 // GetMetric | 8 / 2 | (13 - 8) / 2 | |
| 213 // | 224 // |
| 214 class RateAccCounter : public StatsCounter { | 225 class RateAccCounter : public StatsCounter { |
| 215 public: | 226 public: |
| 216 RateAccCounter(Clock* clock, | 227 RateAccCounter(Clock* clock, |
| 217 StatsCounterObserver* observer, | 228 StatsCounterObserver* observer, |
| 218 bool include_empty_intervals); | 229 bool include_empty_intervals); |
| 219 ~RateAccCounter() override {} | 230 ~RateAccCounter() override {} |
| 220 | 231 |
| 221 void Set(int sample); | 232 void Set(int sample, uint32_t ssrc); |
| 222 | 233 |
| 223 private: | 234 private: |
| 224 bool GetMetric(int* metric) const override; | 235 bool GetMetric(int* metric) const override; |
| 225 | 236 |
| 226 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); | 237 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); |
| 227 }; | 238 }; |
| 228 | 239 |
| 229 } // namespace webrtc | 240 } // namespace webrtc |
| 230 | 241 |
| 231 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ | 242 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ |
| OLD | NEW |