quality_scaler.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #ifndef MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
  11. #define MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <memory>
  15. #include "absl/types/optional.h"
  16. #include "api/scoped_refptr.h"
  17. #include "api/video_codecs/video_encoder.h"
  18. #include "rtc_base/experiments/quality_scaling_experiment.h"
  19. #include "rtc_base/numerics/moving_average.h"
  20. #include "rtc_base/ref_count.h"
  21. #include "rtc_base/ref_counted_object.h"
  22. #include "rtc_base/synchronization/sequence_checker.h"
  23. #include "rtc_base/system/no_unique_address.h"
  24. #include "rtc_base/task_queue.h"
  25. namespace webrtc {
  26. class QualityScalerQpUsageHandlerCallbackInterface;
  27. class QualityScalerQpUsageHandlerInterface;
  28. // QualityScaler runs asynchronously and monitors QP values of encoded frames.
  29. // It holds a reference to a QualityScalerQpUsageHandlerInterface implementation
  30. // to signal an overuse or underuse of QP (which indicate a desire to scale the
  31. // video stream down or up).
  32. class QualityScaler {
  33. public:
  34. // Construct a QualityScaler with given |thresholds| and |handler|.
  35. // This starts the quality scaler periodically checking what the average QP
  36. // has been recently.
  37. QualityScaler(QualityScalerQpUsageHandlerInterface* handler,
  38. VideoEncoder::QpThresholds thresholds);
  39. virtual ~QualityScaler();
  40. // Should be called each time a frame is dropped at encoding.
  41. void ReportDroppedFrameByMediaOpt();
  42. void ReportDroppedFrameByEncoder();
  43. // Inform the QualityScaler of the last seen QP.
  44. void ReportQp(int qp, int64_t time_sent_us);
  45. void SetQpThresholds(VideoEncoder::QpThresholds thresholds);
  46. bool QpFastFilterLow() const;
  47. // The following members declared protected for testing purposes.
  48. protected:
  49. QualityScaler(QualityScalerQpUsageHandlerInterface* handler,
  50. VideoEncoder::QpThresholds thresholds,
  51. int64_t sampling_period_ms);
  52. private:
  53. class QpSmoother;
  54. class CheckQpTask;
  55. class CheckQpTaskHandlerCallback;
  56. enum class CheckQpResult {
  57. kInsufficientSamples,
  58. kNormalQp,
  59. kHighQp,
  60. kLowQp,
  61. };
  62. // Starts checking for QP in a delayed task. When the resulting CheckQpTask
  63. // completes, it will invoke this method again, ensuring that we always
  64. // periodically check for QP. See CheckQpTask for more details. We never run
  65. // more than one CheckQpTask at a time.
  66. void StartNextCheckQpTask();
  67. CheckQpResult CheckQp() const;
  68. void ClearSamples();
  69. std::unique_ptr<CheckQpTask> pending_qp_task_ RTC_GUARDED_BY(&task_checker_);
  70. QualityScalerQpUsageHandlerInterface* const handler_
  71. RTC_GUARDED_BY(&task_checker_);
  72. RTC_NO_UNIQUE_ADDRESS SequenceChecker task_checker_;
  73. VideoEncoder::QpThresholds thresholds_ RTC_GUARDED_BY(&task_checker_);
  74. const int64_t sampling_period_ms_;
  75. bool fast_rampup_ RTC_GUARDED_BY(&task_checker_);
  76. rtc::MovingAverage average_qp_ RTC_GUARDED_BY(&task_checker_);
  77. rtc::MovingAverage framedrop_percent_media_opt_
  78. RTC_GUARDED_BY(&task_checker_);
  79. rtc::MovingAverage framedrop_percent_all_ RTC_GUARDED_BY(&task_checker_);
  80. // Used by QualityScalingExperiment.
  81. const bool experiment_enabled_;
  82. QualityScalingExperiment::Config config_ RTC_GUARDED_BY(&task_checker_);
  83. std::unique_ptr<QpSmoother> qp_smoother_high_ RTC_GUARDED_BY(&task_checker_);
  84. std::unique_ptr<QpSmoother> qp_smoother_low_ RTC_GUARDED_BY(&task_checker_);
  85. const size_t min_frames_needed_;
  86. const double initial_scale_factor_;
  87. const absl::optional<double> scale_factor_;
  88. };
  89. // Reacts to QP being too high or too low. For best quality, when QP is high it
  90. // is desired to decrease the resolution or frame rate of the stream and when QP
  91. // is low it is desired to increase the resolution or frame rate of the stream.
  92. // Whether to reconfigure the stream is ultimately up to the handler, which is
  93. // able to respond asynchronously.
  94. class QualityScalerQpUsageHandlerInterface {
  95. public:
  96. virtual ~QualityScalerQpUsageHandlerInterface();
  97. virtual void OnReportQpUsageHigh() = 0;
  98. virtual void OnReportQpUsageLow() = 0;
  99. };
  100. } // namespace webrtc
  101. #endif // MODULES_VIDEO_CODING_UTILITY_QUALITY_SCALER_H_