| 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 |
| 22 | BitrateConstraint::BitrateConstraint() |
| 23 | : encoder_settings_(absl::nullopt), |
| 24 | encoder_target_bitrate_bps_(absl::nullopt) { |
| 25 | sequence_checker_.Detach(); |
| 26 | } |
| 27 |
| 28 | void BitrateConstraint::OnEncoderSettingsUpdated( |
| 29 | absl::optional<EncoderSettings> encoder_settings) { |
| 30 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 31 | encoder_settings_ = std::move(encoder_settings); |
| 32 | } |
| 33 |
| 34 | void 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). |
| 44 | bool 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 | encoder_settings_->video_codec())) { |
| 63 | // Resolution bitrate limits usage is restricted to singlecast. |
| 64 | return true; |
| 65 | } |
| 66 |
| 67 | absl::optional<int> current_frame_size_px = |
| 68 | input_state.single_active_stream_pixels(); |
| 69 | if (!current_frame_size_px.has_value()) { |
| 70 | return true; |
| 71 | } |
| 72 |
| 73 | absl::optional<VideoEncoder::ResolutionBitrateLimits> bitrate_limits = |
| 74 | encoder_settings_->encoder_info().GetEncoderBitrateLimitsForResolution( |
| 75 | // Need some sort of expected resulting pixels to be used |
| 76 | // instead of unrestricted. |
| 77 | GetHigherResolutionThan(*current_frame_size_px)); |
| 78 |
| 79 | if (bitrate_limits.has_value()) { |
| 80 | RTC_DCHECK_GE(bitrate_limits->frame_size_pixels, *current_frame_size_px); |
| 81 | return bitrate_bps >= |
| 82 | static_cast<uint32_t>(bitrate_limits->min_start_bitrate_bps); |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 |
| 88 | } // namespace webrtc |
| 89 |