blob: 4791cee2f50fcda115eaba8b99b0ad757e87b441 [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
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000014#include "webrtc/base/constructormagic.h"
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000015#include "webrtc/base/criticalsection.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
minyue@webrtc.org74aaf292014-07-16 21:28:26 +000017#include "webrtc/base/exp_filter.h"
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +000018#include "webrtc/base/thread_annotations.h"
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +000019#include "webrtc/base/thread_checker.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000020#include "webrtc/modules/interface/module.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000021
22namespace webrtc {
23
24class Clock;
Peter Boström300eeb62015-05-12 16:51:11 +020025
26// CpuOveruseObserver is called when a system overuse is detected and
27// VideoEngine cannot keep up the encoding frequency.
28class CpuOveruseObserver {
29 public:
30 // Called as soon as an overuse is detected.
31 virtual void OveruseDetected() = 0;
32 // Called periodically when the system is not overused any longer.
33 virtual void NormalUsage() = 0;
34
35 protected:
36 virtual ~CpuOveruseObserver() {}
37};
38
39struct CpuOveruseOptions {
40 CpuOveruseOptions()
41 : enable_capture_jitter_method(false),
42 low_capture_jitter_threshold_ms(20.0f),
43 high_capture_jitter_threshold_ms(30.0f),
44 enable_encode_usage_method(true),
45 low_encode_usage_threshold_percent(55),
46 high_encode_usage_threshold_percent(85),
47 low_encode_time_rsd_threshold(-1),
48 high_encode_time_rsd_threshold(-1),
49 enable_extended_processing_usage(true),
50 frame_timeout_interval_ms(1500),
51 min_frame_samples(120),
52 min_process_count(3),
53 high_threshold_consecutive_count(2) {}
54
55 // Method based on inter-arrival jitter of captured frames.
56 bool enable_capture_jitter_method;
57 float low_capture_jitter_threshold_ms; // Threshold for triggering underuse.
58 float high_capture_jitter_threshold_ms; // Threshold for triggering overuse.
59 // Method based on encode time of frames.
60 bool enable_encode_usage_method;
61 int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
62 int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
63 // TODO(asapersson): Remove options, not used.
64 int low_encode_time_rsd_threshold; // Additional threshold for triggering
65 // underuse (used in addition to
66 // threshold above if configured).
67 int high_encode_time_rsd_threshold; // Additional threshold for triggering
68 // overuse (used in addition to
69 // threshold above if configured).
70 bool enable_extended_processing_usage; // Include a larger time span (in
71 // addition to encode time) for
72 // measuring the processing time of a
73 // frame.
74 // General settings.
75 int frame_timeout_interval_ms; // The maximum allowed interval between two
76 // frames before resetting estimations.
77 int min_frame_samples; // The minimum number of frames required.
78 int min_process_count; // The number of initial process times required before
79 // triggering an overuse/underuse.
80 int high_threshold_consecutive_count; // The number of consecutive checks
81 // above the high threshold before
82 // triggering an overuse.
83
84 bool Equals(const CpuOveruseOptions& o) const {
85 return enable_capture_jitter_method == o.enable_capture_jitter_method &&
86 low_capture_jitter_threshold_ms == o.low_capture_jitter_threshold_ms &&
87 high_capture_jitter_threshold_ms ==
88 o.high_capture_jitter_threshold_ms &&
89 enable_encode_usage_method == o.enable_encode_usage_method &&
90 low_encode_usage_threshold_percent ==
91 o.low_encode_usage_threshold_percent &&
92 high_encode_usage_threshold_percent ==
93 o.high_encode_usage_threshold_percent &&
94 low_encode_time_rsd_threshold == o.low_encode_time_rsd_threshold &&
95 high_encode_time_rsd_threshold == o.high_encode_time_rsd_threshold &&
96 enable_extended_processing_usage ==
97 o.enable_extended_processing_usage &&
98 frame_timeout_interval_ms == o.frame_timeout_interval_ms &&
99 min_frame_samples == o.min_frame_samples &&
100 min_process_count == o.min_process_count &&
101 high_threshold_consecutive_count == o.high_threshold_consecutive_count;
102 }
103};
104
105struct CpuOveruseMetrics {
106 CpuOveruseMetrics()
107 : capture_jitter_ms(-1),
108 avg_encode_time_ms(-1),
Asa Perssoncddb3672015-07-16 08:08:03 +0200109 encode_usage_percent(-1) {}
Peter Boström300eeb62015-05-12 16:51:11 +0200110
111 int capture_jitter_ms; // The current estimated jitter in ms based on
112 // incoming captured frames.
113 int avg_encode_time_ms; // The average encode time in ms.
114 int encode_usage_percent; // The average encode time divided by the average
115 // time difference between incoming captured frames.
Peter Boström300eeb62015-05-12 16:51:11 +0200116};
117
118class CpuOveruseMetricsObserver {
119 public:
120 virtual ~CpuOveruseMetricsObserver() {}
121 virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0;
122};
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000123
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000124// TODO(pbos): Move this somewhere appropriate.
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000125class Statistics {
126 public:
Peter Boström4b91bd02015-06-26 06:58:16 +0200127 explicit Statistics(const CpuOveruseOptions& options);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000128
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000129 void AddSample(float sample_ms);
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000130 void Reset();
131
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000132 float Mean() const;
133 float StdDev() const;
134 uint64_t Count() const;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000135
136 private:
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000137 float InitialMean() const;
138 float InitialVariance() const;
139
140 float sum_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000141 uint64_t count_;
Peter Boström4b91bd02015-06-26 06:58:16 +0200142 const CpuOveruseOptions options_;
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000143 rtc::scoped_ptr<rtc::ExpFilter> filtered_samples_;
144 rtc::scoped_ptr<rtc::ExpFilter> filtered_variance_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000145};
146
147// Use to detect system overuse based on jitter in incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000148class OveruseFrameDetector : public Module {
149 public:
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000150 OveruseFrameDetector(Clock* clock,
Peter Boström4b91bd02015-06-26 06:58:16 +0200151 const CpuOveruseOptions& options,
152 CpuOveruseObserver* overuse_observer,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000153 CpuOveruseMetricsObserver* metrics_observer);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000154 ~OveruseFrameDetector();
155
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +0000156 // Called for each captured frame.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000157 void FrameCaptured(int width, int height, int64_t capture_time_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000158
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000159 // Called for each encoded frame.
asapersson@webrtc.orgc7ff8f92013-11-26 11:12:33 +0000160 void FrameEncoded(int encode_time_ms);
161
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000162 // Called for each sent frame.
163 void FrameSent(int64_t capture_time_ms);
164
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000165 // Only public for testing.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000166 int LastProcessingTimeMs() const;
167 int FramesInQueue() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000168
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000169 // Implements Module.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000170 int64_t TimeUntilNextProcess() override;
171 int32_t Process() override;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000172
173 private:
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000174 class EncodeTimeAvg;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000175 class SendProcessingUsage;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000176 class FrameQueue;
177
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000178 void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_);
179
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000180 // TODO(asapersson): This method is only used on one thread, so it shouldn't
181 // need a guard.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000182 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000183
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000184 // TODO(asapersson): This method is always called on the processing thread.
185 // If locking is required, consider doing that locking inside the
186 // implementation and reduce scope as much as possible. We should also
187 // see if we can avoid calling out to other methods while holding the lock.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000188 bool IsOverusing() EXCLUSIVE_LOCKS_REQUIRED(crit_);
189 bool IsUnderusing(int64_t time_now) EXCLUSIVE_LOCKS_REQUIRED(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000190
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000191 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
192 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000193
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000194 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000195
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000196 // Protecting all members except const and those that are only accessed on the
197 // processing thread.
198 // TODO(asapersson): See if we can reduce locking. As is, video frame
199 // processing contends with reading stats and the processing thread.
200 mutable rtc::CriticalSection crit_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000201
Peter Boström4b91bd02015-06-26 06:58:16 +0200202 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000203
Peter Boström4b91bd02015-06-26 06:58:16 +0200204 // Observer getting overuse reports.
205 CpuOveruseObserver* const observer_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000206
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000207 // Stats metrics.
208 CpuOveruseMetricsObserver* const metrics_observer_;
209 CpuOveruseMetrics metrics_ GUARDED_BY(crit_);
210
tommi@webrtc.orga907e012015-01-28 17:33:12 +0000211 Clock* const clock_;
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000212 int64_t next_process_time_; // Only accessed on the processing thread.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000213 int64_t num_process_times_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000214
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000215 Statistics capture_deltas_ GUARDED_BY(crit_);
216 int64_t last_capture_time_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000217
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000218 // These six members are only accessed on the processing thread.
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000219 int64_t last_overuse_time_;
220 int checks_above_threshold_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000221 int num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000222
223 int64_t last_rampup_time_;
224 bool in_quick_rampup_;
225 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000226
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000227 // Number of pixels of last captured frame.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000228 int num_pixels_ GUARDED_BY(crit_);
asapersson@webrtc.orge2af6222013-09-23 20:05:39 +0000229
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000230 int64_t last_encode_sample_ms_; // Only accessed by one thread.
231
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000232 // TODO(asapersson): Can these be regular members (avoid separate heap
233 // allocs)?
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000234 const rtc::scoped_ptr<EncodeTimeAvg> encode_time_ GUARDED_BY(crit_);
235 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_);
236 const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000237
238 int64_t last_sample_time_ms_; // Only accessed by one thread.
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000239
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000240 rtc::ThreadChecker processing_thread_;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000241
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000242 DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
243};
244
245} // namespace webrtc
246
247#endif // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_