Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 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/video_stream_adapter.h" |
| 12 | |
| 13 | #include <algorithm> |
| 14 | #include <limits> |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 15 | #include <utility> |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 16 | |
| 17 | #include "absl/types/optional.h" |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 18 | #include "api/video_codecs/video_encoder.h" |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 19 | #include "rtc_base/constructor_magic.h" |
| 20 | #include "rtc_base/logging.h" |
| 21 | #include "rtc_base/numerics/safe_conversions.h" |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 25 | const int kMinFrameRateFps = 2; |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 26 | |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 27 | namespace { |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 28 | |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 29 | int MinPixelsPerFrame(const absl::optional<EncoderSettings>& encoder_settings) { |
| 30 | return encoder_settings.has_value() |
| 31 | ? encoder_settings->encoder_info() |
| 32 | .scaling_settings.min_pixels_per_frame |
| 33 | : kDefaultMinPixelsPerFrame; |
| 34 | } |
| 35 | |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 36 | // Generate suggested higher and lower frame rates and resolutions, to be |
| 37 | // applied to the VideoSourceRestrictor. These are used in "maintain-resolution" |
| 38 | // and "maintain-framerate". The "balanced" degradation preference also makes |
| 39 | // use of BalancedDegradationPreference when generating suggestions. The |
| 40 | // VideoSourceRestrictor decidedes whether or not a proposed adaptation is |
| 41 | // valid. |
| 42 | |
| 43 | // For frame rate, the steps we take are 2/3 (down) and 3/2 (up). |
| 44 | int GetLowerFrameRateThan(int fps) { |
| 45 | RTC_DCHECK(fps != std::numeric_limits<int>::max()); |
| 46 | return (fps * 2) / 3; |
| 47 | } |
| 48 | // TODO(hbos): Use absl::optional<> instead? |
| 49 | int GetHigherFrameRateThan(int fps) { |
| 50 | return fps != std::numeric_limits<int>::max() |
| 51 | ? (fps * 3) / 2 |
| 52 | : std::numeric_limits<int>::max(); |
| 53 | } |
| 54 | |
| 55 | // For resolution, the steps we take are 3/5 (down) and 5/3 (up). |
| 56 | // Notice the asymmetry of which restriction property is set depending on if |
| 57 | // we are adapting up or down: |
| 58 | // - VideoSourceRestrictor::DecreaseResolution() sets the max_pixels_per_frame() |
| 59 | // to the desired target and target_pixels_per_frame() to null. |
| 60 | // - VideoSourceRestrictor::IncreaseResolutionTo() sets the |
| 61 | // target_pixels_per_frame() to the desired target, and max_pixels_per_frame() |
| 62 | // is set according to VideoSourceRestrictor::GetIncreasedMaxPixelsWanted(). |
| 63 | int GetLowerResolutionThan(int pixel_count) { |
| 64 | RTC_DCHECK(pixel_count != std::numeric_limits<int>::max()); |
| 65 | return (pixel_count * 3) / 5; |
| 66 | } |
| 67 | // TODO(hbos): Use absl::optional<> instead? |
| 68 | int GetHigherResolutionThan(int pixel_count) { |
| 69 | return pixel_count != std::numeric_limits<int>::max() |
| 70 | ? (pixel_count * 5) / 3 |
| 71 | : std::numeric_limits<int>::max(); |
| 72 | } |
| 73 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 74 | // One of the conditions used in VideoStreamAdapter::GetAdaptationUp(). |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 75 | // TODO(hbos): Whether or not we can adapt up due to encoder settings and |
| 76 | // bitrate should be expressed as a bandwidth-related Resource. |
| 77 | bool CanAdaptUpResolution( |
| 78 | const absl::optional<EncoderSettings>& encoder_settings, |
| 79 | absl::optional<uint32_t> encoder_target_bitrate_bps, |
| 80 | int input_pixels) { |
| 81 | uint32_t bitrate_bps = encoder_target_bitrate_bps.value_or(0); |
| 82 | absl::optional<VideoEncoder::ResolutionBitrateLimits> bitrate_limits = |
| 83 | encoder_settings.has_value() |
| 84 | ? encoder_settings->encoder_info() |
| 85 | .GetEncoderBitrateLimitsForResolution( |
| 86 | GetHigherResolutionThan(input_pixels)) |
| 87 | : absl::nullopt; |
| 88 | if (!bitrate_limits.has_value() || bitrate_bps == 0) { |
| 89 | return true; // No limit configured or bitrate provided. |
| 90 | } |
| 91 | RTC_DCHECK_GE(bitrate_limits->frame_size_pixels, input_pixels); |
| 92 | return bitrate_bps >= |
| 93 | static_cast<uint32_t>(bitrate_limits->min_start_bitrate_bps); |
| 94 | } |
| 95 | |
| 96 | } // namespace |
| 97 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 98 | Adaptation::Step::Step(StepType type, int target) |
| 99 | : type(type), target(target) {} |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 100 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 101 | Adaptation::Adaptation(int validation_id, Step step) |
| 102 | : validation_id_(validation_id), |
| 103 | status_(Status::kValid), |
| 104 | step_(std::move(step)), |
| 105 | min_pixel_limit_reached_(false) {} |
| 106 | |
| 107 | Adaptation::Adaptation(int validation_id, |
| 108 | Step step, |
| 109 | bool min_pixel_limit_reached) |
| 110 | : validation_id_(validation_id), |
| 111 | status_(Status::kValid), |
| 112 | step_(std::move(step)), |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 113 | min_pixel_limit_reached_(min_pixel_limit_reached) {} |
| 114 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 115 | Adaptation::Adaptation(int validation_id, Status invalid_status) |
| 116 | : validation_id_(validation_id), |
| 117 | status_(invalid_status), |
| 118 | step_(absl::nullopt), |
| 119 | min_pixel_limit_reached_(false) { |
| 120 | RTC_DCHECK_NE(status_, Status::kValid); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 123 | Adaptation::Adaptation(int validation_id, |
| 124 | Status invalid_status, |
| 125 | bool min_pixel_limit_reached) |
| 126 | : validation_id_(validation_id), |
| 127 | status_(invalid_status), |
| 128 | step_(absl::nullopt), |
| 129 | min_pixel_limit_reached_(min_pixel_limit_reached) { |
| 130 | RTC_DCHECK_NE(status_, Status::kValid); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 133 | Adaptation::Status Adaptation::status() const { |
| 134 | return status_; |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 137 | bool Adaptation::min_pixel_limit_reached() const { |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 138 | return min_pixel_limit_reached_; |
| 139 | } |
| 140 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 141 | const Adaptation::Step& Adaptation::step() const { |
| 142 | RTC_DCHECK_EQ(status_, Status::kValid); |
| 143 | return step_.value(); |
| 144 | } |
| 145 | |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 146 | // VideoSourceRestrictor is responsible for keeping track of current |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 147 | // VideoSourceRestrictions. |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 148 | class VideoStreamAdapter::VideoSourceRestrictor { |
| 149 | public: |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 150 | VideoSourceRestrictor() {} |
| 151 | |
| 152 | VideoSourceRestrictions source_restrictions() const { |
| 153 | return source_restrictions_; |
| 154 | } |
| 155 | const AdaptationCounters& adaptation_counters() const { return adaptations_; } |
| 156 | void ClearRestrictions() { |
| 157 | source_restrictions_ = VideoSourceRestrictions(); |
| 158 | adaptations_ = AdaptationCounters(); |
| 159 | } |
| 160 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 161 | void SetMinPixelsPerFrame(int min_pixels_per_frame) { |
| 162 | min_pixels_per_frame_ = min_pixels_per_frame; |
| 163 | } |
| 164 | |
| 165 | bool CanDecreaseResolutionTo(int target_pixels) { |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 166 | int max_pixels_per_frame = rtc::dchecked_cast<int>( |
| 167 | source_restrictions_.max_pixels_per_frame().value_or( |
| 168 | std::numeric_limits<int>::max())); |
| 169 | return target_pixels < max_pixels_per_frame && |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 170 | target_pixels >= min_pixels_per_frame_; |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | bool CanIncreaseResolutionTo(int target_pixels) { |
| 174 | int max_pixels_wanted = GetIncreasedMaxPixelsWanted(target_pixels); |
| 175 | int max_pixels_per_frame = rtc::dchecked_cast<int>( |
| 176 | source_restrictions_.max_pixels_per_frame().value_or( |
| 177 | std::numeric_limits<int>::max())); |
| 178 | return max_pixels_wanted > max_pixels_per_frame; |
| 179 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 180 | |
| 181 | bool CanDecreaseFrameRateTo(int max_frame_rate) { |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 182 | const int fps_wanted = std::max(kMinFrameRateFps, max_frame_rate); |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 183 | return fps_wanted < rtc::dchecked_cast<int>( |
| 184 | source_restrictions_.max_frame_rate().value_or( |
| 185 | std::numeric_limits<int>::max())); |
| 186 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 187 | |
| 188 | bool CanIncreaseFrameRateTo(int max_frame_rate) { |
| 189 | return max_frame_rate > rtc::dchecked_cast<int>( |
| 190 | source_restrictions_.max_frame_rate().value_or( |
| 191 | std::numeric_limits<int>::max())); |
| 192 | } |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 193 | |
| 194 | void ApplyAdaptationStep( |
| 195 | const Adaptation::Step& step, |
| 196 | DegradationPreference effective_degradation_preference) { |
| 197 | switch (step.type) { |
| 198 | case Adaptation::StepType::kIncreaseResolution: |
| 199 | IncreaseResolutionTo(step.target); |
| 200 | break; |
| 201 | case Adaptation::StepType::kDecreaseResolution: |
| 202 | DecreaseResolutionTo(step.target); |
| 203 | break; |
| 204 | case Adaptation::StepType::kIncreaseFrameRate: |
| 205 | IncreaseFrameRateTo(step.target); |
| 206 | // TODO(https://crbug.com/webrtc/11222): Don't adapt in two steps. |
| 207 | // GetAdaptationUp() should tell us the correct value, but BALANCED |
| 208 | // logic in DecrementFramerate() makes it hard to predict whether this |
| 209 | // will be the last step. Remove the dependency on |
| 210 | // adaptation_counters(). |
| 211 | if (effective_degradation_preference == |
| 212 | DegradationPreference::BALANCED && |
| 213 | adaptation_counters().fps_adaptations == 0 && |
| 214 | step.target != std::numeric_limits<int>::max()) { |
| 215 | RTC_LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 216 | IncreaseFrameRateTo(std::numeric_limits<int>::max()); |
| 217 | } |
| 218 | break; |
| 219 | case Adaptation::StepType::kDecreaseFrameRate: |
| 220 | DecreaseFrameRateTo(step.target); |
| 221 | break; |
| 222 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | private: |
| 226 | static int GetIncreasedMaxPixelsWanted(int target_pixels) { |
| 227 | if (target_pixels == std::numeric_limits<int>::max()) |
| 228 | return std::numeric_limits<int>::max(); |
| 229 | // When we decrease resolution, we go down to at most 3/5 of current pixels. |
| 230 | // Thus to increase resolution, we need 3/5 to get back to where we started. |
| 231 | // When going up, the desired max_pixels_per_frame() has to be significantly |
| 232 | // higher than the target because the source's native resolutions might not |
| 233 | // match the target. We pick 12/5 of the target. |
| 234 | // |
| 235 | // (This value was historically 4 times the old target, which is (3/5)*4 of |
| 236 | // the new target - or 12/5 - assuming the target is adjusted according to |
| 237 | // the above steps.) |
| 238 | RTC_DCHECK(target_pixels != std::numeric_limits<int>::max()); |
| 239 | return (target_pixels * 12) / 5; |
| 240 | } |
| 241 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 242 | void DecreaseResolutionTo(int target_pixels) { |
| 243 | RTC_DCHECK(CanDecreaseResolutionTo(target_pixels)); |
| 244 | RTC_LOG(LS_INFO) << "Scaling down resolution, max pixels: " |
| 245 | << target_pixels; |
| 246 | source_restrictions_.set_max_pixels_per_frame( |
| 247 | target_pixels != std::numeric_limits<int>::max() |
| 248 | ? absl::optional<size_t>(target_pixels) |
| 249 | : absl::nullopt); |
| 250 | source_restrictions_.set_target_pixels_per_frame(absl::nullopt); |
| 251 | ++adaptations_.resolution_adaptations; |
| 252 | } |
| 253 | |
| 254 | void IncreaseResolutionTo(int target_pixels) { |
| 255 | RTC_DCHECK(CanIncreaseResolutionTo(target_pixels)); |
| 256 | int max_pixels_wanted = GetIncreasedMaxPixelsWanted(target_pixels); |
| 257 | RTC_LOG(LS_INFO) << "Scaling up resolution, max pixels: " |
| 258 | << max_pixels_wanted; |
| 259 | source_restrictions_.set_max_pixels_per_frame( |
| 260 | max_pixels_wanted != std::numeric_limits<int>::max() |
| 261 | ? absl::optional<size_t>(max_pixels_wanted) |
| 262 | : absl::nullopt); |
| 263 | source_restrictions_.set_target_pixels_per_frame( |
| 264 | max_pixels_wanted != std::numeric_limits<int>::max() |
| 265 | ? absl::optional<size_t>(target_pixels) |
| 266 | : absl::nullopt); |
| 267 | --adaptations_.resolution_adaptations; |
| 268 | RTC_DCHECK_GE(adaptations_.resolution_adaptations, 0); |
| 269 | } |
| 270 | |
| 271 | void DecreaseFrameRateTo(int max_frame_rate) { |
| 272 | RTC_DCHECK(CanDecreaseFrameRateTo(max_frame_rate)); |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 273 | max_frame_rate = std::max(kMinFrameRateFps, max_frame_rate); |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 274 | RTC_LOG(LS_INFO) << "Scaling down framerate: " << max_frame_rate; |
| 275 | source_restrictions_.set_max_frame_rate( |
| 276 | max_frame_rate != std::numeric_limits<int>::max() |
| 277 | ? absl::optional<double>(max_frame_rate) |
| 278 | : absl::nullopt); |
| 279 | ++adaptations_.fps_adaptations; |
| 280 | } |
| 281 | |
| 282 | void IncreaseFrameRateTo(int max_frame_rate) { |
| 283 | RTC_DCHECK(CanIncreaseFrameRateTo(max_frame_rate)); |
| 284 | RTC_LOG(LS_INFO) << "Scaling up framerate: " << max_frame_rate; |
| 285 | source_restrictions_.set_max_frame_rate( |
| 286 | max_frame_rate != std::numeric_limits<int>::max() |
| 287 | ? absl::optional<double>(max_frame_rate) |
| 288 | : absl::nullopt); |
| 289 | --adaptations_.fps_adaptations; |
| 290 | RTC_DCHECK_GE(adaptations_.fps_adaptations, 0); |
| 291 | } |
| 292 | |
| 293 | // Needed by CanDecreaseResolutionTo(). |
| 294 | int min_pixels_per_frame_ = 0; |
| 295 | // Current State. |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 296 | VideoSourceRestrictions source_restrictions_; |
| 297 | AdaptationCounters adaptations_; |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 298 | }; |
| 299 | |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 300 | // static |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 301 | VideoStreamAdapter::AdaptationRequest::Mode |
| 302 | VideoStreamAdapter::AdaptationRequest::GetModeFromAdaptationAction( |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 303 | Adaptation::StepType step_type) { |
| 304 | switch (step_type) { |
| 305 | case Adaptation::StepType::kIncreaseResolution: |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 306 | return AdaptationRequest::Mode::kAdaptUp; |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 307 | case Adaptation::StepType::kDecreaseResolution: |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 308 | return AdaptationRequest::Mode::kAdaptDown; |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 309 | case Adaptation::StepType::kIncreaseFrameRate: |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 310 | return AdaptationRequest::Mode::kAdaptUp; |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 311 | case Adaptation::StepType::kDecreaseFrameRate: |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 312 | return AdaptationRequest::Mode::kAdaptDown; |
| 313 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | VideoStreamAdapter::VideoStreamAdapter() |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 317 | : source_restrictor_(std::make_unique<VideoSourceRestrictor>()), |
| 318 | balanced_settings_(), |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 319 | adaptation_validation_id_(0), |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 320 | degradation_preference_(DegradationPreference::DISABLED), |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 321 | input_mode_(VideoInputMode::kNoVideo), |
| 322 | input_pixels_(0), |
| 323 | input_fps_(0), |
| 324 | encoder_settings_(absl::nullopt), |
| 325 | encoder_target_bitrate_bps_(absl::nullopt), |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 326 | last_adaptation_request_(absl::nullopt) {} |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 327 | |
| 328 | VideoStreamAdapter::~VideoStreamAdapter() {} |
| 329 | |
| 330 | VideoSourceRestrictions VideoStreamAdapter::source_restrictions() const { |
| 331 | return source_restrictor_->source_restrictions(); |
| 332 | } |
| 333 | |
| 334 | const AdaptationCounters& VideoStreamAdapter::adaptation_counters() const { |
| 335 | return source_restrictor_->adaptation_counters(); |
| 336 | } |
| 337 | |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 338 | const BalancedDegradationSettings& VideoStreamAdapter::balanced_settings() |
| 339 | const { |
| 340 | return balanced_settings_; |
| 341 | } |
| 342 | |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 343 | void VideoStreamAdapter::ClearRestrictions() { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 344 | // Invalidate any previously returned Adaptation. |
| 345 | ++adaptation_validation_id_; |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 346 | source_restrictor_->ClearRestrictions(); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 347 | last_adaptation_request_.reset(); |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 350 | VideoStreamAdapter::SetDegradationPreferenceResult |
| 351 | VideoStreamAdapter::SetDegradationPreference( |
| 352 | DegradationPreference degradation_preference) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 353 | if (degradation_preference_ == degradation_preference) |
| 354 | return SetDegradationPreferenceResult::kRestrictionsNotCleared; |
| 355 | // Invalidate any previously returned Adaptation. |
| 356 | ++adaptation_validation_id_; |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 357 | bool did_clear = false; |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 358 | if (degradation_preference == DegradationPreference::BALANCED || |
| 359 | degradation_preference_ == DegradationPreference::BALANCED) { |
| 360 | ClearRestrictions(); |
| 361 | did_clear = true; |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 362 | } |
| 363 | degradation_preference_ = degradation_preference; |
| 364 | return did_clear ? SetDegradationPreferenceResult::kRestrictionsCleared |
| 365 | : SetDegradationPreferenceResult::kRestrictionsNotCleared; |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 368 | void VideoStreamAdapter::SetInput( |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 369 | VideoInputMode input_mode, |
| 370 | int input_pixels, |
| 371 | int input_fps, |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 372 | absl::optional<EncoderSettings> encoder_settings, |
| 373 | absl::optional<uint32_t> encoder_target_bitrate_bps) { |
| 374 | // Invalidate any previously returned Adaptation. |
| 375 | ++adaptation_validation_id_; |
| 376 | input_mode_ = input_mode; |
| 377 | input_pixels_ = input_pixels; |
| 378 | input_fps_ = input_fps; |
| 379 | encoder_settings_ = encoder_settings; |
| 380 | encoder_target_bitrate_bps_ = encoder_target_bitrate_bps; |
| 381 | source_restrictor_->SetMinPixelsPerFrame( |
| 382 | MinPixelsPerFrame(encoder_settings_)); |
| 383 | } |
| 384 | |
| 385 | Adaptation VideoStreamAdapter::GetAdaptationUp( |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 386 | AdaptationObserverInterface::AdaptReason reason) const { |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 387 | // Don't adapt if we don't have sufficient input. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 388 | if (input_mode_ == VideoInputMode::kNoVideo) { |
| 389 | return Adaptation(adaptation_validation_id_, |
| 390 | Adaptation::Status::kInsufficientInput); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 391 | } |
| 392 | // Don't adapt if we're awaiting a previous adaptation to have an effect. |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 393 | bool last_adaptation_was_up = |
| 394 | last_adaptation_request_ && |
| 395 | last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
| 396 | if (last_adaptation_was_up && |
| 397 | degradation_preference_ == DegradationPreference::MAINTAIN_FRAMERATE && |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 398 | input_pixels_ <= last_adaptation_request_->input_pixel_count_) { |
| 399 | return Adaptation(adaptation_validation_id_, |
| 400 | Adaptation::Status::kAwaitingPreviousAdaptation); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 401 | } |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 402 | // Don't adapt if BalancedDegradationSettings applies and determines this will |
| 403 | // exceed bitrate constraints. |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 404 | if (reason == AdaptationObserverInterface::AdaptReason::kQuality && |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 405 | EffectiveDegradationPreference() == DegradationPreference::BALANCED && |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 406 | !balanced_settings_.CanAdaptUp( |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 407 | GetVideoCodecTypeOrGeneric(encoder_settings_), input_pixels_, |
| 408 | encoder_target_bitrate_bps_.value_or(0))) { |
| 409 | return Adaptation(adaptation_validation_id_, |
| 410 | Adaptation::Status::kIsBitrateConstrained); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 413 | // Maybe propose targets based on degradation preference. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 414 | switch (EffectiveDegradationPreference()) { |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 415 | case DegradationPreference::BALANCED: { |
| 416 | // Attempt to increase target frame rate. |
| 417 | int target_fps = balanced_settings_.MaxFps( |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 418 | GetVideoCodecTypeOrGeneric(encoder_settings_), input_pixels_); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 419 | if (source_restrictor_->CanIncreaseFrameRateTo(target_fps)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 420 | return Adaptation( |
| 421 | adaptation_validation_id_, |
| 422 | Adaptation::Step(Adaptation::StepType::kIncreaseFrameRate, |
| 423 | target_fps)); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 424 | } |
| 425 | // Fall-through to maybe-adapting resolution, unless |balanced_settings_| |
| 426 | // forbids it based on bitrate. |
| 427 | if (reason == AdaptationObserverInterface::AdaptReason::kQuality && |
| 428 | !balanced_settings_.CanAdaptUpResolution( |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 429 | GetVideoCodecTypeOrGeneric(encoder_settings_), input_pixels_, |
| 430 | encoder_target_bitrate_bps_.value_or(0))) { |
| 431 | return Adaptation(adaptation_validation_id_, |
| 432 | Adaptation::Status::kIsBitrateConstrained); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 433 | } |
| 434 | // Scale up resolution. |
| 435 | ABSL_FALLTHROUGH_INTENDED; |
| 436 | } |
| 437 | case DegradationPreference::MAINTAIN_FRAMERATE: { |
| 438 | // Don't adapt resolution if CanAdaptUpResolution() forbids it based on |
| 439 | // bitrate and limits specified by encoder capabilities. |
| 440 | if (reason == AdaptationObserverInterface::AdaptReason::kQuality && |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 441 | !CanAdaptUpResolution(encoder_settings_, encoder_target_bitrate_bps_, |
| 442 | input_pixels_)) { |
| 443 | return Adaptation(adaptation_validation_id_, |
| 444 | Adaptation::Status::kIsBitrateConstrained); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 445 | } |
| 446 | // Attempt to increase pixel count. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 447 | int target_pixels = input_pixels_; |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 448 | if (source_restrictor_->adaptation_counters().resolution_adaptations == |
| 449 | 1) { |
| 450 | RTC_LOG(LS_INFO) << "Removing resolution down-scaling setting."; |
| 451 | target_pixels = std::numeric_limits<int>::max(); |
| 452 | } |
| 453 | target_pixels = GetHigherResolutionThan(target_pixels); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 454 | if (!source_restrictor_->CanIncreaseResolutionTo(target_pixels)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 455 | return Adaptation(adaptation_validation_id_, |
| 456 | Adaptation::Status::kLimitReached); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 457 | } |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 458 | return Adaptation( |
| 459 | adaptation_validation_id_, |
| 460 | Adaptation::Step(Adaptation::StepType::kIncreaseResolution, |
| 461 | target_pixels)); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 462 | } |
| 463 | case DegradationPreference::MAINTAIN_RESOLUTION: { |
| 464 | // Scale up framerate. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 465 | int target_fps = input_fps_; |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 466 | if (source_restrictor_->adaptation_counters().fps_adaptations == 1) { |
| 467 | RTC_LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 468 | target_fps = std::numeric_limits<int>::max(); |
| 469 | } |
| 470 | target_fps = GetHigherFrameRateThan(target_fps); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 471 | if (!source_restrictor_->CanIncreaseFrameRateTo(target_fps)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 472 | return Adaptation(adaptation_validation_id_, |
| 473 | Adaptation::Status::kLimitReached); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 474 | } |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 475 | return Adaptation( |
| 476 | adaptation_validation_id_, |
| 477 | Adaptation::Step(Adaptation::StepType::kIncreaseFrameRate, |
| 478 | target_fps)); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 479 | } |
| 480 | case DegradationPreference::DISABLED: |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 481 | return Adaptation(adaptation_validation_id_, |
| 482 | Adaptation::Status::kAdaptationDisabled); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 483 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 486 | Adaptation VideoStreamAdapter::GetAdaptationDown() const { |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 487 | // Don't adapt if we don't have sufficient input or adaptation is disabled. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 488 | if (input_mode_ == VideoInputMode::kNoVideo) { |
| 489 | return Adaptation(adaptation_validation_id_, |
| 490 | Adaptation::Status::kInsufficientInput); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 491 | } |
| 492 | if (degradation_preference_ == DegradationPreference::DISABLED) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 493 | return Adaptation(adaptation_validation_id_, |
| 494 | Adaptation::Status::kAdaptationDisabled); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 495 | } |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 496 | bool last_adaptation_was_down = |
| 497 | last_adaptation_request_ && |
| 498 | last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 499 | if (EffectiveDegradationPreference() == |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 500 | DegradationPreference::MAINTAIN_RESOLUTION) { |
| 501 | // TODO(hbos): This usage of |last_adaptation_was_down| looks like a mistake |
| 502 | // - delete it. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 503 | if (input_fps_ <= 0 || |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 504 | (last_adaptation_was_down && input_fps_ < kMinFrameRateFps)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 505 | return Adaptation(adaptation_validation_id_, |
| 506 | Adaptation::Status::kInsufficientInput); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 507 | } |
| 508 | } |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 509 | // Don't adapt if we're awaiting a previous adaptation to have an effect. |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 510 | if (last_adaptation_was_down && |
| 511 | degradation_preference_ == DegradationPreference::MAINTAIN_FRAMERATE && |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 512 | input_pixels_ >= last_adaptation_request_->input_pixel_count_) { |
| 513 | return Adaptation(adaptation_validation_id_, |
| 514 | Adaptation::Status::kAwaitingPreviousAdaptation); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 515 | } |
| 516 | |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 517 | // Maybe propose targets based on degradation preference. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 518 | switch (EffectiveDegradationPreference()) { |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 519 | case DegradationPreference::BALANCED: { |
| 520 | // Try scale down framerate, if lower. |
| 521 | int target_fps = balanced_settings_.MinFps( |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 522 | GetVideoCodecTypeOrGeneric(encoder_settings_), input_pixels_); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 523 | if (source_restrictor_->CanDecreaseFrameRateTo(target_fps)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 524 | return Adaptation( |
| 525 | adaptation_validation_id_, |
| 526 | Adaptation::Step(Adaptation::StepType::kDecreaseFrameRate, |
| 527 | target_fps)); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 528 | } |
| 529 | // Scale down resolution. |
| 530 | ABSL_FALLTHROUGH_INTENDED; |
| 531 | } |
| 532 | case DegradationPreference::MAINTAIN_FRAMERATE: { |
| 533 | // Scale down resolution. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 534 | int target_pixels = GetLowerResolutionThan(input_pixels_); |
| 535 | bool min_pixel_limit_reached = |
| 536 | target_pixels < MinPixelsPerFrame(encoder_settings_); |
| 537 | if (!source_restrictor_->CanDecreaseResolutionTo(target_pixels)) { |
| 538 | return Adaptation(adaptation_validation_id_, |
| 539 | Adaptation::Status::kLimitReached, |
| 540 | min_pixel_limit_reached); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 541 | } |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 542 | return Adaptation( |
| 543 | adaptation_validation_id_, |
| 544 | Adaptation::Step(Adaptation::StepType::kDecreaseResolution, |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 545 | target_pixels), |
| 546 | min_pixel_limit_reached); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 547 | } |
| 548 | case DegradationPreference::MAINTAIN_RESOLUTION: { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 549 | int target_fps = GetLowerFrameRateThan(input_fps_); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 550 | if (!source_restrictor_->CanDecreaseFrameRateTo(target_fps)) { |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 551 | return Adaptation(adaptation_validation_id_, |
| 552 | Adaptation::Status::kLimitReached); |
Henrik Boström | 3453149 | 2020-03-12 12:41:19 +0100 | [diff] [blame] | 553 | } |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 554 | return Adaptation( |
| 555 | adaptation_validation_id_, |
| 556 | Adaptation::Step(Adaptation::StepType::kDecreaseFrameRate, |
| 557 | target_fps)); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 558 | } |
| 559 | case DegradationPreference::DISABLED: |
| 560 | RTC_NOTREACHED(); |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 561 | return Adaptation(adaptation_validation_id_, |
| 562 | Adaptation::Status::kAdaptationDisabled); |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 563 | } |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 564 | } |
| 565 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 566 | VideoSourceRestrictions VideoStreamAdapter::PeekNextRestrictions( |
| 567 | const Adaptation& adaptation) const { |
Henrik Boström | 4c07605 | 2020-03-18 10:09:59 +0100 | [diff] [blame^] | 568 | RTC_DCHECK_EQ(adaptation.validation_id_, adaptation_validation_id_); |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 569 | if (adaptation.status() != Adaptation::Status::kValid) |
| 570 | return source_restrictor_->source_restrictions(); |
| 571 | VideoSourceRestrictor restrictor_copy = *source_restrictor_; |
| 572 | restrictor_copy.ApplyAdaptationStep(adaptation.step(), |
| 573 | EffectiveDegradationPreference()); |
| 574 | return restrictor_copy.source_restrictions(); |
| 575 | } |
| 576 | |
| 577 | ResourceListenerResponse VideoStreamAdapter::ApplyAdaptation( |
| 578 | const Adaptation& adaptation) { |
| 579 | RTC_DCHECK_EQ(adaptation.validation_id_, adaptation_validation_id_); |
| 580 | if (adaptation.status() != Adaptation::Status::kValid) { |
| 581 | return ResourceListenerResponse::kNothing; |
| 582 | } |
Henrik Boström | b0f2e0c | 2020-03-06 13:32:03 +0100 | [diff] [blame] | 583 | // Remember the input pixels and fps of this adaptation. Used to avoid |
| 584 | // adapting again before this adaptation has had an effect. |
| 585 | last_adaptation_request_.emplace(AdaptationRequest{ |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 586 | input_pixels_, input_fps_, |
| 587 | AdaptationRequest::GetModeFromAdaptationAction(adaptation.step().type)}); |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 588 | // Adapt! |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 589 | source_restrictor_->ApplyAdaptationStep(adaptation.step(), |
| 590 | EffectiveDegradationPreference()); |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 591 | // In BALANCED, if requested FPS is higher or close to input FPS to the target |
| 592 | // we tell the QualityScaler to increase its frequency. |
| 593 | // TODO(hbos): Don't have QualityScaler-specific logic here. If the |
| 594 | // QualityScaler wants to add special logic depending on what effects |
| 595 | // adaptation had, it should listen to changes to the VideoSourceRestrictions |
| 596 | // instead. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 597 | if (EffectiveDegradationPreference() == DegradationPreference::BALANCED && |
| 598 | adaptation.step().type == Adaptation::StepType::kDecreaseFrameRate) { |
| 599 | absl::optional<int> min_diff = balanced_settings_.MinFpsDiff(input_pixels_); |
| 600 | if (min_diff && input_fps_ > 0) { |
| 601 | int fps_diff = input_fps_ - adaptation.step().target; |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 602 | if (fps_diff < min_diff.value()) { |
| 603 | return ResourceListenerResponse::kQualityScalerShouldIncreaseFrequency; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | return ResourceListenerResponse::kNothing; |
| 608 | } |
| 609 | |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 610 | DegradationPreference VideoStreamAdapter::EffectiveDegradationPreference() |
| 611 | const { |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 612 | // Balanced mode for screenshare works via automatic animation detection: |
| 613 | // Resolution is capped for fullscreen animated content. |
| 614 | // Adapatation is done only via framerate downgrade. |
| 615 | // Thus effective degradation preference is MAINTAIN_RESOLUTION. |
Henrik Boström | 453953c | 2020-03-14 10:53:57 +0100 | [diff] [blame] | 616 | return (input_mode_ == VideoInputMode::kScreenshareVideo && |
Henrik Boström | 8d9f750 | 2020-03-06 14:51:34 +0100 | [diff] [blame] | 617 | degradation_preference_ == DegradationPreference::BALANCED) |
| 618 | ? DegradationPreference::MAINTAIN_RESOLUTION |
| 619 | : degradation_preference_; |
Henrik Boström | efbec9a | 2020-03-06 10:41:25 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | } // namespace webrtc |