xref: /aosp_15_r20/external/webrtc/video/adaptation/balanced_constraint.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include "video/adaptation/balanced_constraint.h"
12 
13 #include <string>
14 #include <utility>
15 
16 #include "api/sequence_checker.h"
17 
18 namespace webrtc {
19 
BalancedConstraint(DegradationPreferenceProvider * degradation_preference_provider,const FieldTrialsView & field_trials)20 BalancedConstraint::BalancedConstraint(
21     DegradationPreferenceProvider* degradation_preference_provider,
22     const FieldTrialsView& field_trials)
23     : encoder_target_bitrate_bps_(absl::nullopt),
24       balanced_settings_(field_trials),
25       degradation_preference_provider_(degradation_preference_provider) {
26   RTC_DCHECK(degradation_preference_provider_);
27   sequence_checker_.Detach();
28 }
29 
OnEncoderTargetBitrateUpdated(absl::optional<uint32_t> encoder_target_bitrate_bps)30 void BalancedConstraint::OnEncoderTargetBitrateUpdated(
31     absl::optional<uint32_t> encoder_target_bitrate_bps) {
32   RTC_DCHECK_RUN_ON(&sequence_checker_);
33   encoder_target_bitrate_bps_ = std::move(encoder_target_bitrate_bps);
34 }
35 
IsAdaptationUpAllowed(const VideoStreamInputState & input_state,const VideoSourceRestrictions & restrictions_before,const VideoSourceRestrictions & restrictions_after) const36 bool BalancedConstraint::IsAdaptationUpAllowed(
37     const VideoStreamInputState& input_state,
38     const VideoSourceRestrictions& restrictions_before,
39     const VideoSourceRestrictions& restrictions_after) const {
40   RTC_DCHECK_RUN_ON(&sequence_checker_);
41   // Don't adapt if BalancedDegradationSettings applies and determines this will
42   // exceed bitrate constraints.
43   if (degradation_preference_provider_->degradation_preference() ==
44       DegradationPreference::BALANCED) {
45     int frame_size_pixels = input_state.single_active_stream_pixels().value_or(
46         input_state.frame_size_pixels().value());
47     if (!balanced_settings_.CanAdaptUp(
48             input_state.video_codec_type(), frame_size_pixels,
49             encoder_target_bitrate_bps_.value_or(0))) {
50       return false;
51     }
52     if (DidIncreaseResolution(restrictions_before, restrictions_after) &&
53         !balanced_settings_.CanAdaptUpResolution(
54             input_state.video_codec_type(), frame_size_pixels,
55             encoder_target_bitrate_bps_.value_or(0))) {
56       return false;
57     }
58   }
59   return true;
60 }
61 
62 }  // namespace webrtc
63