2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
12 #define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
14 #include "webrtc/modules/interface/module.h"
15 #include "webrtc/system_wrappers/interface/constructor_magic.h"
16 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
21 class CpuOveruseObserver;
22 class CriticalSectionWrapper;
25 // Limits on standard deviation for under/overuse.
27 const float kOveruseStdDevMs = 15.0f;
28 const float kNormalUseStdDevMs = 7.0f;
30 const float kOveruseStdDevMs = 22.0f;
31 const float kNormalUseStdDevMs = 12.0f;
33 const float kOveruseStdDevMs = 17.0f;
34 const float kNormalUseStdDevMs = 10.0f;
37 // TODO(pbos): Move this somewhere appropriate.
42 void AddSample(float sample_ms);
47 uint64_t Count() const;
50 float InitialMean() const;
51 float InitialVariance() const;
55 scoped_ptr<VCMExpFilter> filtered_samples_;
56 scoped_ptr<VCMExpFilter> filtered_variance_;
59 // Use to detect system overuse based on jitter in incoming frames.
60 class OveruseFrameDetector : public Module {
62 explicit OveruseFrameDetector(Clock* clock,
63 float normaluse_stddev_ms,
64 float overuse_stddev_ms);
65 ~OveruseFrameDetector();
67 // Registers an observer receiving overuse and underuse callbacks. Set
68 // 'observer' to NULL to disable callbacks.
69 void SetObserver(CpuOveruseObserver* observer);
71 // Called for each captured frame.
72 void FrameCaptured(int width, int height);
74 // Called when the processing of a captured frame is started.
75 void FrameProcessingStarted();
77 // Called for each encoded frame.
78 void FrameEncoded(int encode_time_ms);
81 // The last estimated jitter based on the incoming captured frames.
82 int last_capture_jitter_ms() const;
84 // Running average of reported encode time (FrameEncoded()).
85 // Only used for stats.
86 int AvgEncodeTimeMs() const;
88 // The average encode time divided by the average time difference between
89 // incoming captured frames.
90 // This variable is currently only used for statistics.
91 int EncodeUsagePercent() const;
93 // The current time delay between an incoming captured frame (FrameCaptured())
94 // until the frame is being processed (FrameProcessingStarted()).
95 // (Note: if a new frame is received before an old frame has been processed,
96 // the old frame is skipped).
97 // The delay is returned as the delay in ms per second.
98 // This variable is currently only used for statistics.
99 int AvgCaptureQueueDelayMsPerS() const;
100 int CaptureQueueDelayMsPerS() const;
102 // Implements Module.
103 virtual int32_t TimeUntilNextProcess() OVERRIDE;
104 virtual int32_t Process() OVERRIDE;
109 class CaptureQueueDelay;
112 bool IsUnderusing(int64_t time_now);
114 // Protecting all members.
115 scoped_ptr<CriticalSectionWrapper> crit_;
117 // Limits on standard deviation for under/overuse.
118 const float normaluse_stddev_ms_;
119 const float overuse_stddev_ms_;
121 // Observer getting overuse reports.
122 CpuOveruseObserver* observer_;
125 int64_t next_process_time_;
127 Statistics capture_deltas_;
128 int64_t last_capture_time_;
130 int64_t last_overuse_time_;
131 int checks_above_threshold_;
133 int64_t last_rampup_time_;
134 bool in_quick_rampup_;
135 int current_rampup_delay_ms_;
137 // Number of pixels of last captured frame.
140 int last_capture_jitter_ms_;
142 int64_t last_encode_sample_ms_;
143 scoped_ptr<EncodeTimeAvg> encode_time_;
144 scoped_ptr<EncodeUsage> encode_usage_;
146 scoped_ptr<CaptureQueueDelay> capture_queue_delay_;
148 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
151 } // namespace webrtc
153 #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_