blob: d6fd59913973a1df49b1a465cbffd538acf18b7a [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Åsa Perssona945aee2018-04-24 16:53:25 +020017#include <memory>
kthelgason876222f2016-11-29 01:44:11 -080018
Åsa Persson517678c2019-05-06 14:17:35 +020019#include "absl/types/optional.h"
Evan Shrubsolece0a11d2020-04-16 11:36:55 +020020#include "api/video/video_adaptation_reason.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/video_codecs/video_encoder.h"
Åsa Perssona945aee2018-04-24 16:53:25 +020022#include "rtc_base/experiments/quality_scaling_experiment.h"
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010023#include "rtc_base/numerics/moving_average.h"
Sebastian Janssonb55015e2019-04-09 13:44:04 +020024#include "rtc_base/synchronization/sequence_checker.h"
Sebastian Janssoncda86dd2019-03-11 17:26:36 +010025#include "rtc_base/task_queue.h"
Sebastian Janssonecb68972019-01-18 10:30:54 +010026#include "rtc_base/task_utils/repeating_task.h"
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000027
28namespace webrtc {
kthelgason876222f2016-11-29 01:44:11 -080029
sprangb1ca0732017-02-01 08:38:12 -080030// An interface for signaling requests to limit or increase the resolution or
31// framerate of the captured video stream.
Henrik Boström48258ac2020-02-06 12:49:57 +010032// TODO(hbos): Can we remove AdaptationObserverInterface in favor of
33// ResourceUsageListener? If we need to adapt that is because of resource usage.
34// A multi-stream and multi-resource aware solution needs to sparate the notion
35// of being resource constrained from the decision to downgrade a specific
36// stream.
sprangb1ca0732017-02-01 08:38:12 -080037class AdaptationObserverInterface {
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000038 public:
sprangb1ca0732017-02-01 08:38:12 -080039 // Called to signal that we can handle larger or more frequent frames.
Evan Shrubsolece0a11d2020-04-16 11:36:55 +020040 virtual void AdaptUp(VideoAdaptationReason reason) = 0;
sprangb1ca0732017-02-01 08:38:12 -080041 // Called to signal that the source should reduce the resolution or framerate.
Åsa Perssonf5e5d252019-08-16 17:24:59 +020042 // Returns false if a downgrade was requested but the request did not result
43 // in a new limiting resolution or fps.
Evan Shrubsolece0a11d2020-04-16 11:36:55 +020044 virtual bool AdaptDown(VideoAdaptationReason reason) = 0;
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000045
kthelgason876222f2016-11-29 01:44:11 -080046 protected:
sprangb1ca0732017-02-01 08:38:12 -080047 virtual ~AdaptationObserverInterface() {}
pbos@webrtc.orga0d78272014-09-12 11:51:47 +000048};
49
kthelgason876222f2016-11-29 01:44:11 -080050// QualityScaler runs asynchronously and monitors QP values of encoded frames.
Niels Möllerd692ef92017-10-04 15:28:55 +020051// It holds a reference to an AdaptationObserverInterface implementation to
52// signal an intent to scale up or down.
kthelgason876222f2016-11-29 01:44:11 -080053class QualityScaler {
54 public:
Niels Möller225c7872018-02-22 15:03:53 +010055 // Construct a QualityScaler with given |thresholds| and |observer|.
kthelgason876222f2016-11-29 01:44:11 -080056 // This starts the quality scaler periodically checking what the average QP
57 // has been recently.
Evan Shrubsole73a5e912020-01-28 15:10:32 +010058 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080059 VideoEncoder::QpThresholds thresholds);
60 virtual ~QualityScaler();
Åsa Perssona945aee2018-04-24 16:53:25 +020061 // Should be called each time a frame is dropped at encoding.
62 void ReportDroppedFrameByMediaOpt();
63 void ReportDroppedFrameByEncoder();
kthelgason876222f2016-11-29 01:44:11 -080064 // Inform the QualityScaler of the last seen QP.
Sebastian Janssonb6789402019-03-01 15:40:49 +010065 void ReportQp(int qp, int64_t time_sent_us);
kthelgason876222f2016-11-29 01:44:11 -080066
Åsa Persson12314192019-06-20 15:45:07 +020067 void SetQpThresholds(VideoEncoder::QpThresholds thresholds);
Åsa Perssone644a032019-11-08 15:56:00 +010068 bool QpFastFilterLow() const;
Åsa Persson12314192019-06-20 15:45:07 +020069
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020070 // The following members declared protected for testing purposes.
kthelgason876222f2016-11-29 01:44:11 -080071 protected:
Evan Shrubsole73a5e912020-01-28 15:10:32 +010072 QualityScaler(AdaptationObserverInterface* observer,
kthelgason876222f2016-11-29 01:44:11 -080073 VideoEncoder::QpThresholds thresholds,
Åsa Persson0ad2d8a2018-04-19 11:06:11 +020074 int64_t sampling_period_ms);
kthelgason876222f2016-11-29 01:44:11 -080075
76 private:
Åsa Perssona945aee2018-04-24 16:53:25 +020077 class QpSmoother;
Yves Gerey3e707812018-11-28 16:47:49 +010078
Åsa Persson04d5f1d2018-04-20 15:19:11 +020079 void CheckQp();
kthelgason876222f2016-11-29 01:44:11 -080080 void ClearSamples();
Åsa Persson04d5f1d2018-04-20 15:19:11 +020081 void ReportQpLow();
82 void ReportQpHigh();
kthelgason876222f2016-11-29 01:44:11 -080083 int64_t GetSamplingPeriodMs() const;
84
Sebastian Janssonecb68972019-01-18 10:30:54 +010085 RepeatingTaskHandle check_qp_task_ RTC_GUARDED_BY(&task_checker_);
danilchap56359be2017-09-07 07:53:45 -070086 AdaptationObserverInterface* const observer_ RTC_GUARDED_BY(&task_checker_);
Sebastian Janssonb55015e2019-04-09 13:44:04 +020087 SequenceChecker task_checker_;
kthelgason876222f2016-11-29 01:44:11 -080088
Åsa Persson12314192019-06-20 15:45:07 +020089 VideoEncoder::QpThresholds thresholds_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -080090 const int64_t sampling_period_ms_;
danilchap56359be2017-09-07 07:53:45 -070091 bool fast_rampup_ RTC_GUARDED_BY(&task_checker_);
Ilya Nikolaevskiy26341992018-11-05 12:55:18 +010092 rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_);
93 rtc::MovingAverage framedrop_percent_media_opt_
94 RTC_GUARDED_BY(&task_checker_);
95 rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_);
Åsa Perssona945aee2018-04-24 16:53:25 +020096
97 // Used by QualityScalingExperiment.
98 const bool experiment_enabled_;
99 QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_);
100 std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_);
101 std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_);
102 bool observed_enough_frames_ RTC_GUARDED_BY(&task_checker_);
Åsa Persson517678c2019-05-06 14:17:35 +0200103
104 const size_t min_frames_needed_;
105 const double initial_scale_factor_;
106 const absl::optional<double> scale_factor_;
Åsa Perssonf5e5d252019-08-16 17:24:59 +0200107 bool adapt_called_ RTC_GUARDED_BY(&task_checker_);
108 bool adapt_failed_ RTC_GUARDED_BY(&task_checker_);
kthelgason876222f2016-11-29 01:44:11 -0800109};
pbos@webrtc.orga0d78272014-09-12 11:51:47 +0000110} // namespace webrtc
111
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200112#endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_