blob: c9f691cc072fc03ce710cb03b04550b1ec791f21 [file] [log] [blame]
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +00001/*
2 * Copyright (c) 2013 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_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
12#define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
13
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000014#include "webrtc/modules/interface/module.h"
15#include "webrtc/system_wrappers/interface/constructor_magic.h"
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +000017#include "webrtc/test/testsupport/gtest_prod_util.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000018
19namespace webrtc {
20
21class Clock;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000022class CpuOveruseObserver;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000023class CriticalSectionWrapper;
24class VCMExpFilter;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000025
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000026// Limits on standard deviation for under/overuse.
27#ifdef WEBRTC_LINUX
28const float kOveruseStdDevMs = 15.0f;
29const float kNormalUseStdDevMs = 7.0f;
30#elif WEBRTC_MAC
asapersson@webrtc.org8f690bc2014-02-13 14:43:18 +000031const float kOveruseStdDevMs = 24.0f;
32const float kNormalUseStdDevMs = 14.0f;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000033#else
34const float kOveruseStdDevMs = 17.0f;
35const float kNormalUseStdDevMs = 10.0f;
36#endif
37
38// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +000039class Statistics {
40 public:
41 Statistics();
42
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000043 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +000044 void Reset();
45
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000046 float Mean() const;
47 float StdDev() const;
48 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000049
50 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000051 float InitialMean() const;
52 float InitialVariance() const;
53
54 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000055 uint64_t count_;
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000056 scoped_ptr<VCMExpFilter> filtered_samples_;
57 scoped_ptr<VCMExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +000058};
59
60// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000061class OveruseFrameDetector : public Module {
62 public:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000063 explicit OveruseFrameDetector(Clock* clock,
64 float normaluse_stddev_ms,
65 float overuse_stddev_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000066 ~OveruseFrameDetector();
67
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000068 // Registers an observer receiving overuse and underuse callbacks. Set
69 // 'observer' to NULL to disable callbacks.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000070 void SetObserver(CpuOveruseObserver* observer);
71
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000072 // Called for each captured frame.
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +000073 void FrameCaptured(int width, int height);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000074
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000075 // Called when the processing of a captured frame is started.
76 void FrameProcessingStarted();
77
78 // Called for each encoded frame.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000079 void FrameEncoded(int encode_time_ms);
80
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000081 // Accessors.
82 // The last estimated jitter based on the incoming captured frames.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +000083 int last_capture_jitter_ms() const;
84
85 // Running average of reported encode time (FrameEncoded()).
86 // Only used for stats.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +000087 int AvgEncodeTimeMs() const;
88
89 // The average encode time divided by the average time difference between
90 // incoming captured frames.
91 // This variable is currently only used for statistics.
92 int EncodeUsagePercent() const;
93
94 // The current time delay between an incoming captured frame (FrameCaptured())
95 // until the frame is being processed (FrameProcessingStarted()).
96 // (Note: if a new frame is received before an old frame has been processed,
97 // the old frame is skipped).
98 // The delay is returned as the delay in ms per second.
99 // This variable is currently only used for statistics.
100 int AvgCaptureQueueDelayMsPerS() const;
101 int CaptureQueueDelayMsPerS() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000102
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000103 // Implements Module.
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000104 virtual int32_t TimeUntilNextProcess() OVERRIDE;
105 virtual int32_t Process() OVERRIDE;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000106
107 private:
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000108 FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, TriggerOveruse);
109 FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, OveruseAndRecover);
110 FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, DoubleOveruseAndRecover);
111 FRIEND_TEST_ALL_PREFIXES(
112 OveruseFrameDetectorTest, TriggerNormalUsageWithMinProcessCount);
113 FRIEND_TEST_ALL_PREFIXES(
114 OveruseFrameDetectorTest, ConstantOveruseGivesNoNormalUsage);
115 FRIEND_TEST_ALL_PREFIXES(OveruseFrameDetectorTest, LastCaptureJitter);
116
117 void set_min_process_count_before_reporting(int64_t count) {
118 min_process_count_before_reporting_ = count;
119 }
120
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000121 class EncodeTimeAvg;
122 class EncodeUsage;
123 class CaptureQueueDelay;
124
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000125 bool IsOverusing();
126 bool IsUnderusing(int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000127
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000128 bool DetectFrameTimeout(int64_t now) const;
129
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000130 // Protecting all members.
131 scoped_ptr<CriticalSectionWrapper> crit_;
132
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000133 // Limits on standard deviation for under/overuse.
134 const float normaluse_stddev_ms_;
135 const float overuse_stddev_ms_;
136
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000137 int64_t min_process_count_before_reporting_;
138
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000139 // Observer getting overuse reports.
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +0000140 CpuOveruseObserver* observer_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000141
142 Clock* clock_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000143 int64_t next_process_time_;
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000144 int64_t num_process_times_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000145
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000146 Statistics capture_deltas_;
147 int64_t last_capture_time_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000148
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000149 int64_t last_overuse_time_;
150 int checks_above_threshold_;
151
152 int64_t last_rampup_time_;
153 bool in_quick_rampup_;
154 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000155
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000156 // Number of pixels of last captured frame.
157 int num_pixels_;
158
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000159 int last_capture_jitter_ms_;
160
161 int64_t last_encode_sample_ms_;
162 scoped_ptr<EncodeTimeAvg> encode_time_;
163 scoped_ptr<EncodeUsage> encode_usage_;
164
165 scoped_ptr<CaptureQueueDelay> capture_queue_delay_;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000166
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000167 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
168};
169
170} // namespace webrtc
171
172#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_