blob: 6af13ac338dcd01a38097e11d30dc4ec71837e61 [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.
Peter Boström300eeb62015-05-12 16:51:11 +020043};
44
45struct CpuOveruseMetrics {
asapersson1aa420b2015-12-07 03:12:22 -080046 CpuOveruseMetrics() : encode_usage_percent(-1) {}
Peter Boström300eeb62015-05-12 16:51:11 +020047
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010048 int encode_usage_percent; // Average encode time divided by the average time
49 // difference between incoming captured frames.
Peter Boström300eeb62015-05-12 16:51:11 +020050};
51
52class CpuOveruseMetricsObserver {
53 public:
54 virtual ~CpuOveruseMetricsObserver() {}
Peter Boströme4499152016-02-05 11:13:28 +010055 virtual void OnEncodedFrameTimeMeasured(int encode_duration_ms,
56 const CpuOveruseMetrics& metrics) = 0;
Peter Boström300eeb62015-05-12 16:51:11 +020057};
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000058
Åsa Persson746210f2015-09-08 10:52:42 +020059// Use to detect system overuse based on the send-side processing time of
perkjd52063f2016-09-07 06:32:18 -070060// incoming frames. All methods must be called on a single task queue but it can
61// be created and destroyed on an arbitrary thread.
62// OveruseFrameDetector::StartCheckForOveruse must be called to periodically
63// check for overuse.
64class OveruseFrameDetector {
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000065 public:
nissee0e3bdf2017-01-18 02:16:20 -080066 OveruseFrameDetector(const CpuOveruseOptions& options,
sprangb1ca0732017-02-01 08:38:12 -080067 AdaptationObserverInterface* overuse_observer,
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +000068 CpuOveruseMetricsObserver* metrics_observer);
sprangfda496a2017-06-15 04:21:07 -070069 virtual ~OveruseFrameDetector();
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000070
perkjd52063f2016-09-07 06:32:18 -070071 // Start to periodically check for overuse.
72 void StartCheckForOveruse();
73
74 // StopCheckForOveruse must be called before destruction if
75 // StartCheckForOveruse has been called.
76 void StopCheckForOveruse();
77
Niels Möller7dc26b72017-12-06 10:27:48 +010078 // Defines the current maximum framerate targeted by the capturer. This is
79 // used to make sure the encode usage percent doesn't drop unduly if the
80 // capturer has quiet periods (for instance caused by screen capturers with
81 // variable capture rate depending on content updates), otherwise we might
82 // experience adaptation toggling.
83 virtual void OnTargetFramerateUpdated(int framerate_fps);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000084
Niels Möller7dc26b72017-12-06 10:27:48 +010085 // Called for each captured frame.
86 void FrameCaptured(const VideoFrame& frame, int64_t time_when_first_seen_us);
87
88 // Called for each sent frame.
89 void FrameSent(uint32_t timestamp, int64_t time_sent_in_us);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000090
Niels Möller904f8692017-12-07 11:22:39 +010091 // Interface for cpu load estimation. Intended for internal use only.
92 class ProcessingUsage {
93 public:
94 virtual void Reset() = 0;
95 virtual void SetMaxSampleDiffMs(float diff_ms) = 0;
Niels Möllere08cf3a2017-12-07 15:23:58 +010096 virtual void FrameCaptured(const VideoFrame& frame,
97 int64_t time_when_first_seen_us,
98 int64_t last_capture_time_us) = 0;
99 // Returns encode_time in us, if there's a new measurement.
100 virtual rtc::Optional<int> FrameSent(uint32_t timestamp,
101 int64_t time_sent_in_us) = 0;
102
Niels Möller904f8692017-12-07 11:22:39 +0100103 virtual int Value() = 0;
104 virtual ~ProcessingUsage() = default;
105 };
106
perkjd52063f2016-09-07 06:32:18 -0700107 protected:
108 void CheckForOveruse(); // Protected for test purposes.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000109
110 private:
perkjd52063f2016-09-07 06:32:18 -0700111 class CheckOveruseTask;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000112
perkjd52063f2016-09-07 06:32:18 -0700113 void EncodedFrameTimeMeasured(int encode_duration_ms);
asapersson74d85e12015-09-24 00:53:32 -0700114 bool IsOverusing(const CpuOveruseMetrics& metrics);
115 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000116
perkjd52063f2016-09-07 06:32:18 -0700117 bool FrameTimeoutDetected(int64_t now) const;
118 bool FrameSizeChanged(int num_pixels) const;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000119
Niels Möller7dc26b72017-12-06 10:27:48 +0100120 void ResetAll(int num_pixels);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000121
Niels Möller904f8692017-12-07 11:22:39 +0100122 static std::unique_ptr<ProcessingUsage> CreateProcessingUsage(
Niels Möller6b642f72017-12-08 14:11:14 +0100123 const CpuOveruseOptions& options);
sprangc5d62e22017-04-02 23:53:04 -0700124
perkjd52063f2016-09-07 06:32:18 -0700125 rtc::SequencedTaskChecker task_checker_;
126 // Owned by the task queue from where StartCheckForOveruse is called.
127 CheckOveruseTask* check_overuse_task_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000128
Peter Boström4b91bd02015-06-26 06:58:16 +0200129 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000130
Peter Boström4b91bd02015-06-26 06:58:16 +0200131 // Observer getting overuse reports.
sprangb1ca0732017-02-01 08:38:12 -0800132 AdaptationObserverInterface* const observer_;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000133
pbos@webrtc.org3e6e2712015-02-26 12:19:31 +0000134 // Stats metrics.
135 CpuOveruseMetricsObserver* const metrics_observer_;
danilchapa37de392017-09-09 04:17:22 -0700136 rtc::Optional<CpuOveruseMetrics> metrics_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000137
danilchapa37de392017-09-09 04:17:22 -0700138 int64_t num_process_times_ RTC_GUARDED_BY(task_checker_);
perkjd52063f2016-09-07 06:32:18 -0700139
danilchapa37de392017-09-09 04:17:22 -0700140 int64_t last_capture_time_us_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000141
asapersson74d85e12015-09-24 00:53:32 -0700142 // Number of pixels of last captured frame.
danilchapa37de392017-09-09 04:17:22 -0700143 int num_pixels_ RTC_GUARDED_BY(task_checker_);
Niels Möller7dc26b72017-12-06 10:27:48 +0100144 int max_framerate_ RTC_GUARDED_BY(task_checker_);
danilchapa37de392017-09-09 04:17:22 -0700145 int64_t last_overuse_time_ms_ RTC_GUARDED_BY(task_checker_);
146 int checks_above_threshold_ RTC_GUARDED_BY(task_checker_);
147 int num_overuse_detections_ RTC_GUARDED_BY(task_checker_);
148 int64_t last_rampup_time_ms_ RTC_GUARDED_BY(task_checker_);
149 bool in_quick_rampup_ RTC_GUARDED_BY(task_checker_);
150 int current_rampup_delay_ms_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000151
Niels Möller904f8692017-12-07 11:22:39 +0100152 const std::unique_ptr<ProcessingUsage> usage_
153 RTC_PT_GUARDED_BY(task_checker_);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +0000154
henrikg3c089d72015-09-16 05:37:44 -0700155 RTC_DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000156};
157
158} // namespace webrtc
159
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200160#endif // VIDEO_OVERUSE_FRAME_DETECTOR_H_