blob: f7ebdb2b0d9db5d3a5fdfaf35bc5173eadebfedd [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef VIDEO_OVERUSE_FRAME_DETECTOR_H_
12#define VIDEO_OVERUSE_FRAME_DETECTOR_H_
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000013
Peter Boströme4499152016-02-05 11:13:28 +010014#include <list>
kwiberg27f982b2016-03-01 11:52:33 -080015#include <memory>
Peter Boströme4499152016-02-05 11:13:28 +010016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/optional.h"
18#include "modules/video_coding/utility/quality_scaler.h"
19#include "rtc_base/constructormagic.h"
Niels Möller7dc26b72017-12-06 10:27:48 +010020#include "rtc_base/numerics/exp_filter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/sequenced_task_checker.h"
22#include "rtc_base/task_queue.h"
23#include "rtc_base/thread_annotations.h"
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000024
25namespace webrtc {
26
Peter Boströme4499152016-02-05 11:13:28 +010027class VideoFrame;
Peter Boström300eeb62015-05-12 16:51:11 +020028
Peter Boström300eeb62015-05-12 16:51:11 +020029struct CpuOveruseOptions {
torbjorng448468d2016-02-10 08:11:57 -080030 CpuOveruseOptions();
Peter Boström300eeb62015-05-12 16:51:11 +020031
Peter Boström300eeb62015-05-12 16:51:11 +020032 int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010033 int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
Peter Boström300eeb62015-05-12 16:51:11 +020034 // General settings.
35 int frame_timeout_interval_ms; // The maximum allowed interval between two
36 // frames before resetting estimations.
Niels Möller7dc26b72017-12-06 10:27:48 +010037 int min_frame_samples; // The minimum number of frames required.
Peter Boström300eeb62015-05-12 16:51:11 +020038 int min_process_count; // The number of initial process times required before
39 // triggering an overuse/underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010040 int high_threshold_consecutive_count; // The number of consecutive checks
41 // above the high threshold before
42 // triggering an overuse.
Niels Möller83dbeac2017-12-14 16:39:44 +010043 // New estimator enabled if this is set non-zero.
44 int filter_time_ms; // Time constant for averaging
Peter Boström300eeb62015-05-12 16:51:11 +020045};
46
47struct CpuOveruseMetrics {
asapersson1aa420b2015-12-07 03:12:22 -080048 CpuOveruseMetrics() : encode_usage_percent(-1) {}
Peter Boström300eeb62015-05-12 16:51:11 +020049
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010050 int encode_usage_percent; // Average encode time divided by the average time
51 // difference between incoming captured frames.
Peter Boström300eeb62015-05-12 16:51:11 +020052};
53
54class CpuOveruseMetricsObserver {
55 public:
56 virtual ~CpuOveruseMetricsObserver() {}
Peter Boströme4499152016-02-05 11:13:28 +010057 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms,
58 const CpuOveruseMetrics& metrics) = 0;
Peter Boström300eeb62015-05-12 16:51:11 +020059};
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000060
Åsa Persson746210f2015-09-08 10:52:42 +020061// Use to detect system overuse based on the send-side processing time of
perkjd52063f2016-09-07 06:32:18 -070062// incoming frames. All methods must be called on a single task queue but it can
63// be created and destroyed on an arbitrary thread.
64// OveruseFrameDetector::StartCheckForOveruse must be called to periodically
65// check for overuse.
66class OveruseFrameDetector {
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000067 public:
nissee0e3bdf2017-01-18 02:16:20 -080068 OveruseFrameDetector(const CpuOveruseOptions& options,
sprangb1ca0732017-02-01 08:38:12 -080069 AdaptationObserverInterface* overuse_observer,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000070 CpuOveruseMetricsObserver* metrics_observer);
sprangfda496a2017-06-15 04:21:07 -070071 virtual ~OveruseFrameDetector();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000072
perkjd52063f2016-09-07 06:32:18 -070073 // Start to periodically check for overuse.
74 void StartCheckForOveruse();
75
76 // StopCheckForOveruse must be called before destruction if
77 // StartCheckForOveruse has been called.
78 void StopCheckForOveruse();
79
Niels Möller7dc26b72017-12-06 10:27:48 +010080 // Defines the current maximum framerate targeted by the capturer. This is
81 // used to make sure the encode usage percent doesn't drop unduly if the
82 // capturer has quiet periods (for instance caused by screen capturers with
83 // variable capture rate depending on content updates), otherwise we might
84 // experience adaptation toggling.
85 virtual void OnTargetFramerateUpdated(int framerate_fps);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000086
Niels Möller7dc26b72017-12-06 10:27:48 +010087 // Called for each captured frame.
88 void FrameCaptured(const VideoFrame& frame, int64_t time_when_first_seen_us);
89
90 // Called for each sent frame.
Niels Möller83dbeac2017-12-14 16:39:44 +010091 void FrameSent(uint32_t timestamp,
92 int64_t time_sent_in_us,
93 int64_t capture_time_us,
94 rtc::Optional<int> encode_duration_us);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000095
Niels Möller904f8692017-12-07 11:22:39 +010096 // Interface for cpu load estimation. Intended for internal use only.
97 class ProcessingUsage {
98 public:
99 virtual void Reset() = 0;
100 virtual void SetMaxSampleDiffMs(float diff_ms) = 0;
Niels Möllere08cf3a2017-12-07 15:23:58 +0100101 virtual void FrameCaptured(const VideoFrame& frame,
102 int64_t time_when_first_seen_us,
103 int64_t last_capture_time_us) = 0;
104 // Returns encode_time in us, if there's a new measurement.
Niels Möller83dbeac2017-12-14 16:39:44 +0100105 virtual rtc::Optional<int> FrameSent(
106 // These two argument used by old estimator.
107 uint32_t timestamp,
108 int64_t time_sent_in_us,
109 // And these two by the new estimator.
110 int64_t capture_time_us,
111 rtc::Optional<int> encode_duration_us) = 0;
Niels Möllere08cf3a2017-12-07 15:23:58 +0100112
Niels Möller904f8692017-12-07 11:22:39 +0100113 virtual int Value() = 0;
114 virtual ~ProcessingUsage() = default;
115 };
116
perkjd52063f2016-09-07 06:32:18 -0700117 protected:
118 void CheckForOveruse(); // Protected for test purposes.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000119
120 private:
perkjd52063f2016-09-07 06:32:18 -0700121 class CheckOveruseTask;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000122
perkjd52063f2016-09-07 06:32:18 -0700123 void EncodedFrameTimeMeasured(int encode_duration_ms);
asapersson74d85e12015-09-24 00:53:32 -0700124 bool IsOverusing(const CpuOveruseMetrics& metrics);
125 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000126
perkjd52063f2016-09-07 06:32:18 -0700127 bool FrameTimeoutDetected(int64_t now) const;
128 bool FrameSizeChanged(int num_pixels) const;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000129
Niels Möller7dc26b72017-12-06 10:27:48 +0100130 void ResetAll(int num_pixels);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000131
Niels Möller904f8692017-12-07 11:22:39 +0100132 static std::unique_ptr<ProcessingUsage> CreateProcessingUsage(
Niels Möller6b642f72017-12-08 14:11:14 +0100133 const CpuOveruseOptions& options);
sprangc5d62e22017-04-02 23:53:04 -0700134
perkjd52063f2016-09-07 06:32:18 -0700135 rtc::SequencedTaskChecker task_checker_;
136 // Owned by the task queue from where StartCheckForOveruse is called.
137 CheckOveruseTask* check_overuse_task_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000138
Peter Boström4b91bd02015-06-26 06:58:16 +0200139 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000140
Peter Boström4b91bd02015-06-26 06:58:16 +0200141 // Observer getting overuse reports.
sprangb1ca0732017-02-01 08:38:12 -0800142 AdaptationObserverInterface* const observer_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000143
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000144 // Stats metrics.
145 CpuOveruseMetricsObserver* const metrics_observer_;
danilchapa37de392017-09-09 04:17:22 -0700146 rtc::Optional<CpuOveruseMetrics> metrics_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000147
danilchapa37de392017-09-09 04:17:22 -0700148 int64_t num_process_times_ RTC_GUARDED_BY(task_checker_);
perkjd52063f2016-09-07 06:32:18 -0700149
danilchapa37de392017-09-09 04:17:22 -0700150 int64_t last_capture_time_us_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000151
asapersson74d85e12015-09-24 00:53:32 -0700152 // Number of pixels of last captured frame.
danilchapa37de392017-09-09 04:17:22 -0700153 int num_pixels_ RTC_GUARDED_BY(task_checker_);
Niels Möller7dc26b72017-12-06 10:27:48 +0100154 int max_framerate_ RTC_GUARDED_BY(task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700155 int64_t last_overuse_time_ms_ RTC_GUARDED_BY(task_checker_);
156 int checks_above_threshold_ RTC_GUARDED_BY(task_checker_);
157 int num_overuse_detections_ RTC_GUARDED_BY(task_checker_);
158 int64_t last_rampup_time_ms_ RTC_GUARDED_BY(task_checker_);
159 bool in_quick_rampup_ RTC_GUARDED_BY(task_checker_);
160 int current_rampup_delay_ms_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000161
Niels Möller904f8692017-12-07 11:22:39 +0100162 const std::unique_ptr<ProcessingUsage> usage_
163 RTC_PT_GUARDED_BY(task_checker_);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000164
henrikg3c089d72015-09-16 05:37:44 -0700165 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000166};
167
168} // namespace webrtc
169
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200170#endif // VIDEO_OVERUSE_FRAME_DETECTOR_H_