quality_scaler_resource.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2020 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 VIDEO_ADAPTATION_QUALITY_SCALER_RESOURCE_H_
  11. #define VIDEO_ADAPTATION_QUALITY_SCALER_RESOURCE_H_
  12. #include <memory>
  13. #include <queue>
  14. #include <string>
  15. #include "absl/types/optional.h"
  16. #include "api/scoped_refptr.h"
  17. #include "api/video/video_adaptation_reason.h"
  18. #include "api/video_codecs/video_encoder.h"
  19. #include "call/adaptation/adaptation_listener.h"
  20. #include "call/adaptation/resource_adaptation_processor_interface.h"
  21. #include "modules/video_coding/utility/quality_scaler.h"
  22. #include "rtc_base/critical_section.h"
  23. #include "rtc_base/ref_counted_object.h"
  24. #include "rtc_base/task_queue.h"
  25. #include "video/adaptation/video_stream_encoder_resource.h"
  26. namespace webrtc {
  27. // Handles interaction with the QualityScaler.
  28. class QualityScalerResource : public VideoStreamEncoderResource,
  29. public AdaptationListener,
  30. public QualityScalerQpUsageHandlerInterface {
  31. public:
  32. static rtc::scoped_refptr<QualityScalerResource> Create();
  33. QualityScalerResource();
  34. ~QualityScalerResource() override;
  35. void SetAdaptationProcessor(
  36. ResourceAdaptationProcessorInterface* adaptation_processor);
  37. bool is_started() const;
  38. void StartCheckForOveruse(VideoEncoder::QpThresholds qp_thresholds);
  39. void StopCheckForOveruse();
  40. void SetQpThresholds(VideoEncoder::QpThresholds qp_thresholds);
  41. bool QpFastFilterLow();
  42. void OnEncodeCompleted(const EncodedImage& encoded_image,
  43. int64_t time_sent_in_us);
  44. void OnFrameDropped(EncodedImageCallback::DropReason reason);
  45. // QualityScalerQpUsageHandlerInterface implementation.
  46. void OnReportQpUsageHigh(
  47. rtc::scoped_refptr<QualityScalerQpUsageHandlerCallbackInterface> callback)
  48. override;
  49. void OnReportQpUsageLow(
  50. rtc::scoped_refptr<QualityScalerQpUsageHandlerCallbackInterface> callback)
  51. override;
  52. // AdaptationListener implementation.
  53. void OnAdaptationApplied(
  54. const VideoStreamInputState& input_state,
  55. const VideoSourceRestrictions& restrictions_before,
  56. const VideoSourceRestrictions& restrictions_after,
  57. rtc::scoped_refptr<Resource> reason_resource) override;
  58. private:
  59. size_t QueuePendingCallback(
  60. rtc::scoped_refptr<QualityScalerQpUsageHandlerCallbackInterface>
  61. callback);
  62. void HandlePendingCallback(size_t callback_id, bool clear_qp_samples);
  63. void AbortPendingCallbacks();
  64. // Members accessed on the encoder queue.
  65. std::unique_ptr<QualityScaler> quality_scaler_
  66. RTC_GUARDED_BY(encoder_queue());
  67. // The timestamp of the last time we reported underuse because this resource
  68. // was disabled in order to prevent getting stuck with QP adaptations. Used to
  69. // make sure underuse reporting is not too spammy.
  70. absl::optional<int64_t> last_underuse_due_to_disabled_timestamp_ms_
  71. RTC_GUARDED_BY(encoder_queue());
  72. // Every OnReportQpUsageHigh/Low() operation has a callback that MUST be
  73. // invoked on the encoder_queue(). Because usage measurements are reported on
  74. // the encoder_queue() but handled by the processor on the the
  75. // resource_adaptation_queue_(), handling a measurement entails a task queue
  76. // "ping" round-trip. Multiple callbacks in-flight is thus possible.
  77. size_t num_handled_callbacks_ RTC_GUARDED_BY(encoder_queue());
  78. std::queue<rtc::scoped_refptr<QualityScalerQpUsageHandlerCallbackInterface>>
  79. pending_callbacks_ RTC_GUARDED_BY(encoder_queue());
  80. // Members accessed on the adaptation queue.
  81. ResourceAdaptationProcessorInterface* adaptation_processor_
  82. RTC_GUARDED_BY(resource_adaptation_queue());
  83. bool clear_qp_samples_ RTC_GUARDED_BY(resource_adaptation_queue());
  84. };
  85. } // namespace webrtc
  86. #endif // VIDEO_ADAPTATION_QUALITY_SCALER_RESOURCE_H_