pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |
| 13 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 14 | #include <utility> |
| 15 | |
kthelgason | 478681e | 2016-09-28 08:17:43 -0700 | [diff] [blame] | 16 | #include "webrtc/common_types.h" |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 17 | #include "webrtc/video_encoder.h" |
| 18 | #include "webrtc/base/optional.h" |
| 19 | #include "webrtc/base/sequenced_task_checker.h" |
kjellander@webrtc.org | b7ce964 | 2015-11-18 23:04:10 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/utility/moving_average.h" |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 23 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 24 | // An interface for signaling requests to limit or increase the resolution or |
| 25 | // framerate of the captured video stream. |
| 26 | class AdaptationObserverInterface { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 27 | public: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 28 | // 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 }; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 31 | static const size_t kScaleReasonSize = 2; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 32 | // 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.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 36 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 37 | protected: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 38 | virtual ~AdaptationObserverInterface() {} |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 41 | // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
| 42 | // It holds a reference to a ScalingObserverInterface implementation to signal |
| 43 | // an intent to scale up or down. |
| 44 | class QualityScaler { |
| 45 | public: |
| 46 | // Construct a QualityScaler with a given |observer|. |
| 47 | // This starts the quality scaler periodically checking what the average QP |
| 48 | // has been recently. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 49 | QualityScaler(AdaptationObserverInterface* observer, |
| 50 | VideoCodecType codec_type); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 51 | // If specific thresholds are desired these can be supplied as |thresholds|. |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 52 | QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 53 | VideoEncoder::QpThresholds thresholds); |
| 54 | virtual ~QualityScaler(); |
| 55 | // Should be called each time the encoder drops a frame |
| 56 | void ReportDroppedFrame(); |
| 57 | // Inform the QualityScaler of the last seen QP. |
| 58 | void ReportQP(int qp); |
| 59 | |
| 60 | // The following members declared protected for testing purposes |
| 61 | protected: |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 62 | QualityScaler(AdaptationObserverInterface* observer, |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 63 | VideoEncoder::QpThresholds thresholds, |
| 64 | int64_t sampling_period); |
| 65 | |
| 66 | private: |
| 67 | class CheckQPTask; |
| 68 | void CheckQP(); |
| 69 | void ClearSamples(); |
| 70 | void ReportQPLow(); |
| 71 | void ReportQPHigh(); |
| 72 | int64_t GetSamplingPeriodMs() const; |
| 73 | |
| 74 | CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame^] | 75 | AdaptationObserverInterface* const observer_ GUARDED_BY(&task_checker_); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 76 | rtc::SequencedTaskChecker task_checker_; |
| 77 | |
| 78 | const int64_t sampling_period_ms_; |
| 79 | bool fast_rampup_ GUARDED_BY(&task_checker_); |
| 80 | MovingAverage average_qp_ GUARDED_BY(&task_checker_); |
| 81 | MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_); |
| 82 | |
| 83 | VideoEncoder::QpThresholds thresholds_ GUARDED_BY(&task_checker_); |
| 84 | }; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 85 | } // namespace webrtc |
| 86 | |
| 87 | #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |