blob: d2606c19e6950003e366a87856d308d9f20c6d4e [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
Peter Boström7623ce42015-12-09 12:13:30 +010011#ifndef WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_
12#define WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000013
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"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010020#include "webrtc/modules/include/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()
Peter Boström01f364e2016-01-07 16:38:25 +010041 : low_encode_usage_threshold_percent(55),
Peter Boström300eeb62015-05-12 16:51:11 +020042 high_encode_usage_threshold_percent(85),
Peter Boström300eeb62015-05-12 16:51:11 +020043 frame_timeout_interval_ms(1500),
44 min_frame_samples(120),
45 min_process_count(3),
46 high_threshold_consecutive_count(2) {}
47
Peter Boström300eeb62015-05-12 16:51:11 +020048 int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010049 int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
Peter Boström300eeb62015-05-12 16:51:11 +020050 // General settings.
51 int frame_timeout_interval_ms; // The maximum allowed interval between two
52 // frames before resetting estimations.
53 int min_frame_samples; // The minimum number of frames required.
54 int min_process_count; // The number of initial process times required before
55 // triggering an overuse/underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010056 int high_threshold_consecutive_count; // The number of consecutive checks
57 // above the high threshold before
58 // triggering an overuse.
Peter Boström300eeb62015-05-12 16:51:11 +020059};
60
61struct CpuOveruseMetrics {
asapersson1aa420b2015-12-07 03:12:22 -080062 CpuOveruseMetrics() : encode_usage_percent(-1) {}
Peter Boström300eeb62015-05-12 16:51:11 +020063
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010064 int encode_usage_percent; // Average encode time divided by the average time
65 // difference between incoming captured frames.
Peter Boström300eeb62015-05-12 16:51:11 +020066};
67
68class CpuOveruseMetricsObserver {
69 public:
70 virtual ~CpuOveruseMetricsObserver() {}
71 virtual void CpuOveruseMetricsUpdated(const CpuOveruseMetrics& metrics) = 0;
72};
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000073
pbos@webrtc.orga9575702013-08-30 17:16:32 +000074
Åsa Persson746210f2015-09-08 10:52:42 +020075// Use to detect system overuse based on the send-side processing time of
76// incoming frames.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000077class OveruseFrameDetector : public Module {
78 public:
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000079 OveruseFrameDetector(Clock* clock,
Peter Boström4b91bd02015-06-26 06:58:16 +020080 const CpuOveruseOptions& options,
81 CpuOveruseObserver* overuse_observer,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000082 CpuOveruseMetricsObserver* metrics_observer);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000083 ~OveruseFrameDetector();
84
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000085 // Called for each captured frame.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000086 void FrameCaptured(int width, int height, int64_t capture_time_ms);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000087
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000088 // Called for each sent frame.
89 void FrameSent(int64_t capture_time_ms);
90
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000091 // Only public for testing.
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000092 int LastProcessingTimeMs() const;
93 int FramesInQueue() const;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000094
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000095 // Implements Module.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000096 int64_t TimeUntilNextProcess() override;
97 int32_t Process() override;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000098
99 private:
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000100 class SendProcessingUsage;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000101 class FrameQueue;
102
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000103 void UpdateCpuOveruseMetrics() EXCLUSIVE_LOCKS_REQUIRED(crit_);
104
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000105 // TODO(asapersson): This method is only used on one thread, so it shouldn't
106 // need a guard.
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000107 void AddProcessingTime(int elapsed_ms) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org9e5b0342013-12-04 13:47:44 +0000108
asapersson74d85e12015-09-24 00:53:32 -0700109 // Only called on the processing thread.
110 bool IsOverusing(const CpuOveruseMetrics& metrics);
111 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000112
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000113 bool FrameTimeoutDetected(int64_t now) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
114 bool FrameSizeChanged(int num_pixels) const EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000115
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000116 void ResetAll(int num_pixels) EXCLUSIVE_LOCKS_REQUIRED(crit_);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000117
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000118 // Protecting all members except const and those that are only accessed on the
119 // processing thread.
120 // TODO(asapersson): See if we can reduce locking. As is, video frame
121 // processing contends with reading stats and the processing thread.
122 mutable rtc::CriticalSection crit_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000123
Peter Boström4b91bd02015-06-26 06:58:16 +0200124 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000125
Peter Boström4b91bd02015-06-26 06:58:16 +0200126 // Observer getting overuse reports.
127 CpuOveruseObserver* const observer_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000128
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000129 // Stats metrics.
130 CpuOveruseMetricsObserver* const metrics_observer_;
131 CpuOveruseMetrics metrics_ GUARDED_BY(crit_);
132
tommi@webrtc.orga907e012015-01-28 17:33:12 +0000133 Clock* const clock_;
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000134 int64_t num_process_times_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000135
asapersson@webrtc.orgcd621a82014-11-11 09:40:19 +0000136 int64_t last_capture_time_ GUARDED_BY(crit_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000137
asapersson74d85e12015-09-24 00:53:32 -0700138 // Number of pixels of last captured frame.
139 int num_pixels_ GUARDED_BY(crit_);
140
141 // These seven members are only accessed on the processing thread.
142 int64_t next_process_time_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000143 int64_t last_overuse_time_;
144 int checks_above_threshold_;
asapersson@webrtc.orgd9803072014-06-16 14:27:19 +0000145 int num_overuse_detections_;
pbos@webrtc.orga9575702013-08-30 17:16:32 +0000146 int64_t last_rampup_time_;
147 bool in_quick_rampup_;
148 int current_rampup_delay_ms_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000149
asapersson74d85e12015-09-24 00:53:32 -0700150 int64_t last_sample_time_ms_; // Only accessed by one thread.
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000151
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000152 // TODO(asapersson): Can these be regular members (avoid separate heap
153 // allocs)?
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000154 const rtc::scoped_ptr<SendProcessingUsage> usage_ GUARDED_BY(crit_);
155 const rtc::scoped_ptr<FrameQueue> frame_queue_ GUARDED_BY(crit_);
asapersson@webrtc.org44149392015-02-04 08:34:47 +0000156
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000157 rtc::ThreadChecker processing_thread_;
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000158
henrikg3c089d72015-09-16 05:37:44 -0700159 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000160};
161
162} // namespace webrtc
163
Peter Boström7623ce42015-12-09 12:13:30 +0100164#endif // WEBRTC_VIDEO_OVERUSE_FRAME_DETECTOR_H_