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/bitrate_constraint.h" 12 13 #include <utility> 14 #include <vector> 15 16 #include "api/sequence_checker.h" 17 #include "call/adaptation/video_stream_adapter.h" 18 #include "video/adaptation/video_stream_encoder_resource_manager.h" 19 20 namespace webrtc { 21 BitrateConstraint()22BitrateConstraint::BitrateConstraint() 23 : encoder_settings_(absl::nullopt), 24 encoder_target_bitrate_bps_(absl::nullopt) { 25 sequence_checker_.Detach(); 26 } 27 OnEncoderSettingsUpdated(absl::optional<EncoderSettings> encoder_settings)28void BitrateConstraint::OnEncoderSettingsUpdated( 29 absl::optional<EncoderSettings> encoder_settings) { 30 RTC_DCHECK_RUN_ON(&sequence_checker_); 31 encoder_settings_ = std::move(encoder_settings); 32 } 33 OnEncoderTargetBitrateUpdated(absl::optional<uint32_t> encoder_target_bitrate_bps)34void BitrateConstraint::OnEncoderTargetBitrateUpdated( 35 absl::optional<uint32_t> encoder_target_bitrate_bps) { 36 RTC_DCHECK_RUN_ON(&sequence_checker_); 37 encoder_target_bitrate_bps_ = std::move(encoder_target_bitrate_bps); 38 } 39 40 // Checks if resolution is allowed to adapt up based on the current bitrate and 41 // ResolutionBitrateLimits.min_start_bitrate_bps for the next higher resolution. 42 // Bitrate limits usage is restricted to a single active stream/layer (e.g. when 43 // quality scaling is enabled). IsAdaptationUpAllowed(const VideoStreamInputState & input_state,const VideoSourceRestrictions & restrictions_before,const VideoSourceRestrictions & restrictions_after) const44bool BitrateConstraint::IsAdaptationUpAllowed( 45 const VideoStreamInputState& input_state, 46 const VideoSourceRestrictions& restrictions_before, 47 const VideoSourceRestrictions& restrictions_after) const { 48 RTC_DCHECK_RUN_ON(&sequence_checker_); 49 // Make sure bitrate limits are not violated. 50 if (DidIncreaseResolution(restrictions_before, restrictions_after)) { 51 if (!encoder_settings_.has_value()) { 52 return true; 53 } 54 55 uint32_t bitrate_bps = encoder_target_bitrate_bps_.value_or(0); 56 if (bitrate_bps == 0) { 57 return true; 58 } 59 60 if (VideoStreamEncoderResourceManager::IsSimulcastOrMultipleSpatialLayers( 61 encoder_settings_->encoder_config())) { 62 // Resolution bitrate limits usage is restricted to singlecast. 63 return true; 64 } 65 66 absl::optional<int> current_frame_size_px = 67 input_state.single_active_stream_pixels(); 68 if (!current_frame_size_px.has_value()) { 69 return true; 70 } 71 72 absl::optional<VideoEncoder::ResolutionBitrateLimits> bitrate_limits = 73 encoder_settings_->encoder_info().GetEncoderBitrateLimitsForResolution( 74 // Need some sort of expected resulting pixels to be used 75 // instead of unrestricted. 76 GetHigherResolutionThan(*current_frame_size_px)); 77 78 if (bitrate_limits.has_value()) { 79 RTC_DCHECK_GE(bitrate_limits->frame_size_pixels, *current_frame_size_px); 80 return bitrate_bps >= 81 static_cast<uint32_t>(bitrate_limits->min_start_bitrate_bps); 82 } 83 } 84 return true; 85 } 86 87 } // namespace webrtc 88