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 | |
| 24 | // An interface for a class that receives scale up/down requests. |
| 25 | class ScalingObserverInterface { |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 26 | public: |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 27 | enum ScaleReason : size_t { kQuality = 0, kCpu = 1 }; |
| 28 | static const size_t kScaleReasonSize = 2; |
| 29 | // Called to signal that we can handle larger frames. |
| 30 | virtual void ScaleUp(ScaleReason reason) = 0; |
| 31 | // Called to signal that encoder to scale down. |
| 32 | virtual void ScaleDown(ScaleReason reason) = 0; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 33 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 34 | protected: |
| 35 | virtual ~ScalingObserverInterface() {} |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 38 | // QualityScaler runs asynchronously and monitors QP values of encoded frames. |
| 39 | // It holds a reference to a ScalingObserverInterface implementation to signal |
| 40 | // an intent to scale up or down. |
| 41 | class QualityScaler { |
| 42 | public: |
| 43 | // Construct a QualityScaler with a given |observer|. |
| 44 | // This starts the quality scaler periodically checking what the average QP |
| 45 | // has been recently. |
| 46 | QualityScaler(ScalingObserverInterface* observer, VideoCodecType codec_type); |
| 47 | // If specific thresholds are desired these can be supplied as |thresholds|. |
| 48 | QualityScaler(ScalingObserverInterface* observer, |
| 49 | VideoEncoder::QpThresholds thresholds); |
| 50 | virtual ~QualityScaler(); |
| 51 | // Should be called each time the encoder drops a frame |
| 52 | void ReportDroppedFrame(); |
| 53 | // Inform the QualityScaler of the last seen QP. |
| 54 | void ReportQP(int qp); |
| 55 | |
| 56 | // The following members declared protected for testing purposes |
| 57 | protected: |
| 58 | QualityScaler(ScalingObserverInterface* observer, |
| 59 | VideoEncoder::QpThresholds thresholds, |
| 60 | int64_t sampling_period); |
| 61 | |
| 62 | private: |
| 63 | class CheckQPTask; |
| 64 | void CheckQP(); |
| 65 | void ClearSamples(); |
| 66 | void ReportQPLow(); |
| 67 | void ReportQPHigh(); |
| 68 | int64_t GetSamplingPeriodMs() const; |
| 69 | |
| 70 | CheckQPTask* check_qp_task_ GUARDED_BY(&task_checker_); |
| 71 | ScalingObserverInterface* const observer_ GUARDED_BY(&task_checker_); |
| 72 | rtc::SequencedTaskChecker task_checker_; |
| 73 | |
| 74 | const int64_t sampling_period_ms_; |
| 75 | bool fast_rampup_ GUARDED_BY(&task_checker_); |
| 76 | MovingAverage average_qp_ GUARDED_BY(&task_checker_); |
| 77 | MovingAverage framedrop_percent_ GUARDED_BY(&task_checker_); |
| 78 | |
| 79 | VideoEncoder::QpThresholds thresholds_ GUARDED_BY(&task_checker_); |
| 80 | }; |
pbos@webrtc.org | a0d7827 | 2014-09-12 11:51:47 +0000 | [diff] [blame] | 81 | } // namespace webrtc |
| 82 | |
| 83 | #endif // WEBRTC_MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_ |