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