blob: c2f5b212e8f8bb280665dba35f6ad76486c9ff61 [file] [log] [blame]
pbos@webrtc.orga0d78272014-09-12 11:51:47 +00001/*
2 * Copyright (c) 2014 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 MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
12#define MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Åsa Perssona945aee2018-04-24 16:53:25 +020016#include <memory>
kthelgason876222f2016-11-29 01:44:11 -080017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/video_codecs/video_encoder.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020019#include "rtc_base/experiments/quality_scaling_experiment.h"
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010020#include "rtc_base/numerics/moving_average.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/sequenced_task_checker.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010022#include "rtc_base/task_utils/repeating_task.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000023
24namespace webrtc {
kthelgason876222f2016-11-29 01:44:11 -080025
sprangb1ca0732017-02-01 08:38:12 -080026// An interface for signaling requests to limit or increase the resolution or
27// framerate of the captured video stream.
28class AdaptationObserverInterface {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000029 public:
sprangb1ca0732017-02-01 08:38:12 -080030 // Indicates if the adaptation is due to overuse of the CPU resources, or if
31 // the quality of the encoded frames have dropped too low.
32 enum AdaptReason : size_t { kQuality = 0, kCpu = 1 };
kthelgason876222f2016-11-29 01:44:11 -080033 static const size_t kScaleReasonSize = 2;
sprangb1ca0732017-02-01 08:38:12 -080034 // Called to signal that we can handle larger or more frequent frames.
35 virtual void AdaptUp(AdaptReason reason) = 0;
36 // Called to signal that the source should reduce the resolution or framerate.
37 virtual void AdaptDown(AdaptReason reason) = 0;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000038
kthelgason876222f2016-11-29 01:44:11 -080039 protected:
sprangb1ca0732017-02-01 08:38:12 -080040 virtual ~AdaptationObserverInterface() {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000041};
42
kthelgason876222f2016-11-29 01:44:11 -080043// QualityScaler runs asynchronously and monitors QP values of encoded frames.
Niels Möllerd692ef92017-10-04 15:28:55 +020044// It holds a reference to an AdaptationObserverInterface implementation to
45// signal an intent to scale up or down.
kthelgason876222f2016-11-29 01:44:11 -080046class QualityScaler {
47 public:
Niels Möller225c7872018-02-22 15:03:53 +010048 // Construct a QualityScaler with given |thresholds| and |observer|.
kthelgason876222f2016-11-29 01:44:11 -080049 // This starts the quality scaler periodically checking what the average QP
50 // has been recently.
sprangb1ca0732017-02-01 08:38:12 -080051 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080052 VideoEncoder::QpThresholds thresholds);
53 virtual ~QualityScaler();
Åsa Perssona945aee2018-04-24 16:53:25 +020054 // Should be called each time a frame is dropped at encoding.
55 void ReportDroppedFrameByMediaOpt();
56 void ReportDroppedFrameByEncoder();
kthelgason876222f2016-11-29 01:44:11 -080057 // Inform the QualityScaler of the last seen QP.
Sebastian Janssonb6789402019-03-01 15:40:49 +010058 void ReportQp(int qp, int64_t time_sent_us);
kthelgason876222f2016-11-29 01:44:11 -080059
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020060 // The following members declared protected for testing purposes.
kthelgason876222f2016-11-29 01:44:11 -080061 protected:
sprangb1ca0732017-02-01 08:38:12 -080062 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080063 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020064 int64_t sampling_period_ms);
kthelgason876222f2016-11-29 01:44:11 -080065
66 private:
Åsa Perssona945aee2018-04-24 16:53:25 +020067 class QpSmoother;
Yves Gerey3e707812018-11-28 16:47:49 +010068
Åsa Persson04d5f1d2018-04-20 15:19:11 +020069 void CheckQp();
kthelgason876222f2016-11-29 01:44:11 -080070 void ClearSamples();
Åsa Persson04d5f1d2018-04-20 15:19:11 +020071 void ReportQpLow();
72 void ReportQpHigh();
kthelgason876222f2016-11-29 01:44:11 -080073 int64_t GetSamplingPeriodMs() const;
74
Sebastian Janssonecb68972019-01-18 10:30:54 +010075 RepeatingTaskHandle check_qp_task_ RTC_GUARDED_BY(&task_checker_);
danilchap56359be2017-09-07 07:53:45 -070076 AdaptationObserverInterface* const observer_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080077 rtc::SequencedTaskChecker task_checker_;
78
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020079 const VideoEncoder::QpThresholds thresholds_;
kthelgason876222f2016-11-29 01:44:11 -080080 const int64_t sampling_period_ms_;
danilchap56359be2017-09-07 07:53:45 -070081 bool fast_rampup_ RTC_GUARDED_BY(&task_checker_);
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010082 rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_);
83 rtc::MovingAverage framedrop_percent_media_opt_
84 RTC_GUARDED_BY(&task_checker_);
85 rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +020086
87 // Used by QualityScalingExperiment.
88 const bool experiment_enabled_;
89 QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_);
90 std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_);
91 std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_);
92 bool observed_enough_frames_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080093};
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000094} // namespace webrtc
95
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020096#endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_