blob: e8234611c3f24007ded5a01e5cfce76e32d37dab [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
kthelgason876222f2016-11-29 01:44:11 -080014#include <utility>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/optional.h"
17#include "api/video_codecs/video_encoder.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/utility/moving_average.h"
20#include "rtc_base/sequenced_task_checker.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000021
22namespace webrtc {
kthelgason876222f2016-11-29 01:44:11 -080023
sprangb1ca0732017-02-01 08:38:12 -080024// An interface for signaling requests to limit or increase the resolution or
25// framerate of the captured video stream.
26class AdaptationObserverInterface {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000027 public:
sprangb1ca0732017-02-01 08:38:12 -080028 // Indicates if the adaptation is due to overuse of the CPU resources, or if
29 // the quality of the encoded frames have dropped too low.
30 enum AdaptReason : size_t { kQuality = 0, kCpu = 1 };
kthelgason876222f2016-11-29 01:44:11 -080031 static const size_t kScaleReasonSize = 2;
sprangb1ca0732017-02-01 08:38:12 -080032 // Called to signal that we can handle larger or more frequent frames.
33 virtual void AdaptUp(AdaptReason reason) = 0;
34 // Called to signal that the source should reduce the resolution or framerate.
35 virtual void AdaptDown(AdaptReason reason) = 0;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000036
kthelgason876222f2016-11-29 01:44:11 -080037 protected:
sprangb1ca0732017-02-01 08:38:12 -080038 virtual ~AdaptationObserverInterface() {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000039};
40
kthelgason876222f2016-11-29 01:44:11 -080041// QualityScaler runs asynchronously and monitors QP values of encoded frames.
Niels Möllerd692ef92017-10-04 15:28:55 +020042// It holds a reference to an AdaptationObserverInterface implementation to
43// signal an intent to scale up or down.
kthelgason876222f2016-11-29 01:44:11 -080044class QualityScaler {
45 public:
Niels Möller225c7872018-02-22 15:03:53 +010046 // Construct a QualityScaler with given |thresholds| and |observer|.
kthelgason876222f2016-11-29 01:44:11 -080047 // This starts the quality scaler periodically checking what the average QP
48 // has been recently.
sprangb1ca0732017-02-01 08:38:12 -080049 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080050 VideoEncoder::QpThresholds thresholds);
51 virtual ~QualityScaler();
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020052 // Should be called each time the encoder drops a frame.
kthelgason876222f2016-11-29 01:44:11 -080053 void ReportDroppedFrame();
54 // Inform the QualityScaler of the last seen QP.
Åsa Persson04d5f1d2018-04-20 15:19:11 +020055 void ReportQp(int qp);
kthelgason876222f2016-11-29 01:44:11 -080056
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020057 // The following members declared protected for testing purposes.
kthelgason876222f2016-11-29 01:44:11 -080058 protected:
sprangb1ca0732017-02-01 08:38:12 -080059 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080060 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020061 int64_t sampling_period_ms);
kthelgason876222f2016-11-29 01:44:11 -080062
63 private:
Åsa Persson04d5f1d2018-04-20 15:19:11 +020064 class CheckQpTask;
65 void CheckQp();
kthelgason876222f2016-11-29 01:44:11 -080066 void ClearSamples();
Åsa Persson04d5f1d2018-04-20 15:19:11 +020067 void ReportQpLow();
68 void ReportQpHigh();
kthelgason876222f2016-11-29 01:44:11 -080069 int64_t GetSamplingPeriodMs() const;
70
Åsa Persson04d5f1d2018-04-20 15:19:11 +020071 CheckQpTask* check_qp_task_ RTC_GUARDED_BY(&task_checker_);
danilchap56359be2017-09-07 07:53:45 -070072 AdaptationObserverInterface* const observer_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080073 rtc::SequencedTaskChecker task_checker_;
74
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020075 const VideoEncoder::QpThresholds thresholds_;
kthelgason876222f2016-11-29 01:44:11 -080076 const int64_t sampling_period_ms_;
danilchap56359be2017-09-07 07:53:45 -070077 bool fast_rampup_ RTC_GUARDED_BY(&task_checker_);
78 MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_);
79 MovingAverage framedrop_percent_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080080};
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000081} // namespace webrtc
82
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020083#endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_