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/base/constructormagic.h"
15 #include "webrtc/base/exp_filter.h"
16 #include "webrtc/modules/interface/module.h"
17 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
18 #include "webrtc/video_engine/include/vie_base.h"
23 class CpuOveruseObserver;
24 class CriticalSectionWrapper;
26 // TODO(pbos): Move this somewhere appropriate.
31 void AddSample(float sample_ms);
33 void SetOptions(const CpuOveruseOptions& options);
37 uint64_t Count() const;
40 float InitialMean() const;
41 float InitialVariance() const;
45 CpuOveruseOptions options_;
46 scoped_ptr<rtc::ExpFilter> filtered_samples_;
47 scoped_ptr<rtc::ExpFilter> filtered_variance_;
50 // Use to detect system overuse based on jitter in incoming frames.
51 class OveruseFrameDetector : public Module {
53 explicit OveruseFrameDetector(Clock* clock);
54 ~OveruseFrameDetector();
56 // Registers an observer receiving overuse and underuse callbacks. Set
57 // 'observer' to NULL to disable callbacks.
58 void SetObserver(CpuOveruseObserver* observer);
60 // Sets options for overuse detection.
61 void SetOptions(const CpuOveruseOptions& options);
63 // Called for each captured frame.
64 void FrameCaptured(int width, int height, int64_t capture_time_ms);
66 // Called when the processing of a captured frame is started.
67 void FrameProcessingStarted();
69 // Called for each encoded frame.
70 void FrameEncoded(int encode_time_ms);
72 // Called for each sent frame.
73 void FrameSent(int64_t capture_time_ms);
77 // Returns CpuOveruseMetrics where
78 // capture_jitter_ms: The estimated jitter based on incoming captured frames.
79 // avg_encode_time_ms: Running average of reported encode time
80 // (FrameEncoded()). Only used for stats.
81 // TODO(asapersson): Rename metric.
82 // encode_usage_percent: The average processing time of a frame on the
83 // send-side divided by the average time difference
84 // between incoming captured frames.
85 // capture_queue_delay_ms_per_s: The current time delay between an incoming
86 // captured frame (FrameCaptured()) until the
87 // frame is being processed
88 // (FrameProcessingStarted()). (Note: if a new
89 // frame is received before an old frame has
90 // been processed, the old frame is skipped).
91 // The delay is expressed in ms delay per sec.
92 // Only used for stats.
93 void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const;
95 // Only public for testing.
96 int CaptureQueueDelayMsPerS() const;
97 int LastProcessingTimeMs() const;
98 int FramesInQueue() const;
100 // Implements Module.
101 virtual int32_t TimeUntilNextProcess() OVERRIDE;
102 virtual int32_t Process() OVERRIDE;
106 class SendProcessingUsage;
107 class CaptureQueueDelay;
110 void AddProcessingTime(int elapsed_ms);
113 bool IsUnderusing(int64_t time_now);
115 bool FrameTimeoutDetected(int64_t now) const;
116 bool FrameSizeChanged(int num_pixels) const;
118 void ResetAll(int num_pixels);
120 // Protecting all members.
121 scoped_ptr<CriticalSectionWrapper> crit_;
123 // Observer getting overuse reports.
124 CpuOveruseObserver* observer_;
126 CpuOveruseOptions options_;
129 int64_t next_process_time_;
130 int64_t num_process_times_;
132 Statistics capture_deltas_;
133 int64_t last_capture_time_;
135 int64_t last_overuse_time_;
136 int checks_above_threshold_;
137 int num_overuse_detections_;
139 int64_t last_rampup_time_;
140 bool in_quick_rampup_;
141 int current_rampup_delay_ms_;
143 // Number of pixels of last captured frame.
146 int64_t last_encode_sample_ms_;
147 scoped_ptr<EncodeTimeAvg> encode_time_;
148 scoped_ptr<SendProcessingUsage> usage_;
149 scoped_ptr<FrameQueue> frame_queue_;
150 int64_t last_sample_time_ms_;
152 scoped_ptr<CaptureQueueDelay> capture_queue_delay_;
154 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
157 } // namespace webrtc
159 #endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_