blob: 76508bcd2c814b42403b08b51ca87c13f9332477 [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 EncodedFrameObserver;
28class VideoFrame;
Peter Boström300eeb62015-05-12 16:51:11 +020029
Peter Boström300eeb62015-05-12 16:51:11 +020030struct CpuOveruseOptions {
torbjorng448468d2016-02-10 08:11:57 -080031 CpuOveruseOptions();
Peter Boström300eeb62015-05-12 16:51:11 +020032
Peter Boström300eeb62015-05-12 16:51:11 +020033 int low_encode_usage_threshold_percent; // Threshold for triggering underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010034 int high_encode_usage_threshold_percent; // Threshold for triggering overuse.
Peter Boström300eeb62015-05-12 16:51:11 +020035 // General settings.
36 int frame_timeout_interval_ms; // The maximum allowed interval between two
37 // frames before resetting estimations.
Niels Möller7dc26b72017-12-06 10:27:48 +010038 int min_frame_samples; // The minimum number of frames required.
Peter Boström300eeb62015-05-12 16:51:11 +020039 int min_process_count; // The number of initial process times required before
40 // triggering an overuse/underuse.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010041 int high_threshold_consecutive_count; // The number of consecutive checks
42 // above the high threshold before
43 // triggering an overuse.
Peter Boström300eeb62015-05-12 16:51:11 +020044};
45
46struct CpuOveruseMetrics {
asapersson1aa420b2015-12-07 03:12:22 -080047 CpuOveruseMetrics() : encode_usage_percent(-1) {}
Peter Boström300eeb62015-05-12 16:51:11 +020048
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010049 int encode_usage_percent; // Average encode time divided by the average time
50 // difference between incoming captured frames.
Peter Boström300eeb62015-05-12 16:51:11 +020051};
52
53class CpuOveruseMetricsObserver {
54 public:
55 virtual ~CpuOveruseMetricsObserver() {}
Peter Boströme4499152016-02-05 11:13:28 +010056 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms,
57 const CpuOveruseMetrics& metrics) = 0;
Peter Boström300eeb62015-05-12 16:51:11 +020058};
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000059
Åsa Persson746210f2015-09-08 10:52:42 +020060// Use to detect system overuse based on the send-side processing time of
perkjd52063f2016-09-07 06:32:18 -070061// incoming frames. All methods must be called on a single task queue but it can
62// be created and destroyed on an arbitrary thread.
63// OveruseFrameDetector::StartCheckForOveruse must be called to periodically
64// check for overuse.
65class OveruseFrameDetector {
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000066 public:
nissee0e3bdf2017-01-18 02:16:20 -080067 OveruseFrameDetector(const CpuOveruseOptions& options,
sprangb1ca0732017-02-01 08:38:12 -080068 AdaptationObserverInterface* overuse_observer,
Peter Boströme4499152016-02-05 11:13:28 +010069 EncodedFrameObserver* encoder_timing_,
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.
91 void FrameSent(uint32_t timestamp, int64_t time_sent_in_us);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000092
Niels Möller904f8692017-12-07 11:22:39 +010093 // Interface for cpu load estimation. Intended for internal use only.
94 class ProcessingUsage {
95 public:
96 virtual void Reset() = 0;
97 virtual void SetMaxSampleDiffMs(float diff_ms) = 0;
Niels Möllere08cf3a2017-12-07 15:23:58 +010098 virtual void FrameCaptured(const VideoFrame& frame,
99 int64_t time_when_first_seen_us,
100 int64_t last_capture_time_us) = 0;
101 // Returns encode_time in us, if there's a new measurement.
102 virtual rtc::Optional<int> FrameSent(uint32_t timestamp,
103 int64_t time_sent_in_us) = 0;
104
Niels Möller904f8692017-12-07 11:22:39 +0100105 virtual int Value() = 0;
106 virtual ~ProcessingUsage() = default;
107 };
108
perkjd52063f2016-09-07 06:32:18 -0700109 protected:
110 void CheckForOveruse(); // Protected for test purposes.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000111
112 private:
perkjd52063f2016-09-07 06:32:18 -0700113 class CheckOveruseTask;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000114
perkjd52063f2016-09-07 06:32:18 -0700115 void EncodedFrameTimeMeasured(int encode_duration_ms);
asapersson74d85e12015-09-24 00:53:32 -0700116 bool IsOverusing(const CpuOveruseMetrics& metrics);
117 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000118
perkjd52063f2016-09-07 06:32:18 -0700119 bool FrameTimeoutDetected(int64_t now) const;
120 bool FrameSizeChanged(int num_pixels) const;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000121
Niels Möller7dc26b72017-12-06 10:27:48 +0100122 void ResetAll(int num_pixels);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000123
Niels Möller904f8692017-12-07 11:22:39 +0100124 static std::unique_ptr<ProcessingUsage> CreateProcessingUsage(
Niels Möllere08cf3a2017-12-07 15:23:58 +0100125 const CpuOveruseOptions& options,
126 EncodedFrameObserver* encoder_timing);
sprangc5d62e22017-04-02 23:53:04 -0700127
perkjd52063f2016-09-07 06:32:18 -0700128 rtc::SequencedTaskChecker task_checker_;
129 // Owned by the task queue from where StartCheckForOveruse is called.
130 CheckOveruseTask* check_overuse_task_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000131
Peter Boström4b91bd02015-06-26 06:58:16 +0200132 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000133
Peter Boström4b91bd02015-06-26 06:58:16 +0200134 // Observer getting overuse reports.
sprangb1ca0732017-02-01 08:38:12 -0800135 AdaptationObserverInterface* const observer_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000136
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000137 // Stats metrics.
138 CpuOveruseMetricsObserver* const metrics_observer_;
danilchapa37de392017-09-09 04:17:22 -0700139 rtc::Optional<CpuOveruseMetrics> metrics_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000140
danilchapa37de392017-09-09 04:17:22 -0700141 int64_t num_process_times_ RTC_GUARDED_BY(task_checker_);
perkjd52063f2016-09-07 06:32:18 -0700142
danilchapa37de392017-09-09 04:17:22 -0700143 int64_t last_capture_time_us_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000144
asapersson74d85e12015-09-24 00:53:32 -0700145 // Number of pixels of last captured frame.
danilchapa37de392017-09-09 04:17:22 -0700146 int num_pixels_ RTC_GUARDED_BY(task_checker_);
Niels Möller7dc26b72017-12-06 10:27:48 +0100147 int max_framerate_ RTC_GUARDED_BY(task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700148 int64_t last_overuse_time_ms_ RTC_GUARDED_BY(task_checker_);
149 int checks_above_threshold_ RTC_GUARDED_BY(task_checker_);
150 int num_overuse_detections_ RTC_GUARDED_BY(task_checker_);
151 int64_t last_rampup_time_ms_ RTC_GUARDED_BY(task_checker_);
152 bool in_quick_rampup_ RTC_GUARDED_BY(task_checker_);
153 int current_rampup_delay_ms_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000154
Niels Möller904f8692017-12-07 11:22:39 +0100155 const std::unique_ptr<ProcessingUsage> usage_
156 RTC_PT_GUARDED_BY(task_checker_);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000157
henrikg3c089d72015-09-16 05:37:44 -0700158 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000159};
160
161} // namespace webrtc
162
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200163#endif // VIDEO_OVERUSE_FRAME_DETECTOR_H_