blob: 3cc9262a948090b9f7acf2173159d427887e690e [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"
20#include "rtc_base/numerics/exp_filter.h"
21#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.
38 int min_frame_samples; // The minimum number of frames required.
39 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
sprangfda496a2017-06-15 04:21:07 -070080 // 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);
86
mflodman@webrtc.orgd4412fe2013-07-31 16:42:21 +000087 // Called for each captured frame.
nissee0e3bdf2017-01-18 02:16:20 -080088 void FrameCaptured(const VideoFrame& frame, int64_t time_when_first_seen_us);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000089
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000090 // Called for each sent frame.
nissee0e3bdf2017-01-18 02:16:20 -080091 void FrameSent(uint32_t timestamp, int64_t time_sent_in_us);
asapersson@webrtc.orgb24d3352013-11-20 13:51:40 +000092
perkjd52063f2016-09-07 06:32:18 -070093 protected:
94 void CheckForOveruse(); // Protected for test purposes.
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +000095
96 private:
sprangc5d62e22017-04-02 23:53:04 -070097 class OverdoseInjector;
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +000098 class SendProcessingUsage;
perkjd52063f2016-09-07 06:32:18 -070099 class CheckOveruseTask;
Peter Boströme4499152016-02-05 11:13:28 +0100100 struct FrameTiming {
nissee0e3bdf2017-01-18 02:16:20 -0800101 FrameTiming(int64_t capture_time_us, uint32_t timestamp, int64_t now)
102 : capture_time_us(capture_time_us),
Peter Boströme4499152016-02-05 11:13:28 +0100103 timestamp(timestamp),
nissee0e3bdf2017-01-18 02:16:20 -0800104 capture_us(now),
105 last_send_us(-1) {}
106 int64_t capture_time_us;
Peter Boströme4499152016-02-05 11:13:28 +0100107 uint32_t timestamp;
nissee0e3bdf2017-01-18 02:16:20 -0800108 int64_t capture_us;
109 int64_t last_send_us;
Peter Boströme4499152016-02-05 11:13:28 +0100110 };
asapersson@webrtc.org9aed0022014-10-16 06:57:12 +0000111
perkjd52063f2016-09-07 06:32:18 -0700112 void EncodedFrameTimeMeasured(int encode_duration_ms);
asapersson74d85e12015-09-24 00:53:32 -0700113 bool IsOverusing(const CpuOveruseMetrics& metrics);
114 bool IsUnderusing(const CpuOveruseMetrics& metrics, int64_t time_now);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000115
perkjd52063f2016-09-07 06:32:18 -0700116 bool FrameTimeoutDetected(int64_t now) const;
117 bool FrameSizeChanged(int num_pixels) const;
asapersson@webrtc.org8a8c3ef2014-03-20 13:15:01 +0000118
perkjd52063f2016-09-07 06:32:18 -0700119 void ResetAll(int num_pixels);
asapersson@webrtc.orgb60346e2014-02-17 19:02:15 +0000120
sprangc5d62e22017-04-02 23:53:04 -0700121 static std::unique_ptr<SendProcessingUsage> CreateSendProcessingUsage(
122 const CpuOveruseOptions& options);
123
perkjd52063f2016-09-07 06:32:18 -0700124 rtc::SequencedTaskChecker task_checker_;
125 // Owned by the task queue from where StartCheckForOveruse is called.
126 CheckOveruseTask* check_overuse_task_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000127
Peter Boström4b91bd02015-06-26 06:58:16 +0200128 const CpuOveruseOptions options_;
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000129
Peter Boström4b91bd02015-06-26 06:58:16 +0200130 // Observer getting overuse reports.
sprangb1ca0732017-02-01 08:38:12 -0800131 AdaptationObserverInterface* const observer_;
Peter Boströme4499152016-02-05 11:13:28 +0100132 EncodedFrameObserver* const encoder_timing_;
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_);
141 int64_t last_processed_capture_time_us_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000142
asapersson74d85e12015-09-24 00:53:32 -0700143 // Number of pixels of last captured frame.
danilchapa37de392017-09-09 04:17:22 -0700144 int num_pixels_ RTC_GUARDED_BY(task_checker_);
145 int max_framerate_ RTC_GUARDED_BY(task_checker_);
146 int64_t last_overuse_time_ms_ RTC_GUARDED_BY(task_checker_);
147 int checks_above_threshold_ RTC_GUARDED_BY(task_checker_);
148 int num_overuse_detections_ RTC_GUARDED_BY(task_checker_);
149 int64_t last_rampup_time_ms_ RTC_GUARDED_BY(task_checker_);
150 bool in_quick_rampup_ RTC_GUARDED_BY(task_checker_);
151 int current_rampup_delay_ms_ RTC_GUARDED_BY(task_checker_);
mflodman@webrtc.orge6168f52013-06-26 11:23:01 +0000152
tommi@webrtc.org7a57f8f2015-02-08 18:27:46 +0000153 // TODO(asapersson): Can these be regular members (avoid separate heap
154 // allocs)?
danilchapa37de392017-09-09 04:17:22 -0700155 const std::unique_ptr<SendProcessingUsage> usage_
156 RTC_GUARDED_BY(task_checker_);
157 std::list<FrameTiming> frame_timing_ RTC_GUARDED_BY(task_checker_);
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200164#endif // VIDEO_OVERUSE_FRAME_DETECTOR_H_