 Chromium Code Reviews
 Chromium Code Reviews Issue 2781433002:
  Reland of Add framerate to VideoSinkWants and ability to signal on overuse  (Closed)
    
  
    Issue 2781433002:
  Reland of Add framerate to VideoSinkWants and ability to signal on overuse  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 /* | 1 /* | 
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 
| 3 * | 3 * | 
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found | 
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may | 
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. | 
| 9 */ | 9 */ | 
| 10 | 10 | 
| 11 #include "webrtc/video/vie_encoder.h" | 11 #include "webrtc/video/vie_encoder.h" | 
| 12 | 12 | 
| 13 #include <algorithm> | 13 #include <algorithm> | 
| 14 #include <limits> | 14 #include <limits> | 
| 15 #include <numeric> | |
| 15 #include <utility> | 16 #include <utility> | 
| 16 | 17 | 
| 17 #include "webrtc/base/arraysize.h" | 18 #include "webrtc/base/arraysize.h" | 
| 18 #include "webrtc/base/checks.h" | 19 #include "webrtc/base/checks.h" | 
| 19 #include "webrtc/base/location.h" | 20 #include "webrtc/base/location.h" | 
| 20 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" | 
| 21 #include "webrtc/base/timeutils.h" | 22 #include "webrtc/base/timeutils.h" | 
| 22 #include "webrtc/base/trace_event.h" | 23 #include "webrtc/base/trace_event.h" | 
| 23 #include "webrtc/common_video/include/video_bitrate_allocator.h" | 24 #include "webrtc/common_video/include/video_bitrate_allocator.h" | 
| 24 #include "webrtc/modules/pacing/paced_sender.h" | 25 #include "webrtc/modules/pacing/paced_sender.h" | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 35 using DegradationPreference = VideoSendStream::DegradationPreference; | 36 using DegradationPreference = VideoSendStream::DegradationPreference; | 
| 36 | 37 | 
| 37 // Time interval for logging frame counts. | 38 // Time interval for logging frame counts. | 
| 38 const int64_t kFrameLogIntervalMs = 60000; | 39 const int64_t kFrameLogIntervalMs = 60000; | 
| 39 | 40 | 
| 40 // We will never ask for a resolution lower than this. | 41 // We will never ask for a resolution lower than this. | 
| 41 // TODO(kthelgason): Lower this limit when better testing | 42 // TODO(kthelgason): Lower this limit when better testing | 
| 42 // on MediaCodec and fallback implementations are in place. | 43 // on MediaCodec and fallback implementations are in place. | 
| 43 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 44 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 
| 44 const int kMinPixelsPerFrame = 320 * 180; | 45 const int kMinPixelsPerFrame = 320 * 180; | 
| 46 const int kMinFramerateFps = 2; | |
| 45 | 47 | 
| 46 // The maximum number of frames to drop at beginning of stream | 48 // The maximum number of frames to drop at beginning of stream | 
| 47 // to try and achieve desired bitrate. | 49 // to try and achieve desired bitrate. | 
| 48 const int kMaxInitialFramedrop = 4; | 50 const int kMaxInitialFramedrop = 4; | 
| 49 | 51 | 
| 50 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 52 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 
| 51 // pipelining encoders better (multiple input frames before something comes | 53 // pipelining encoders better (multiple input frames before something comes | 
| 52 // out). This should effectively turn off CPU adaptations for systems that | 54 // out). This should effectively turn off CPU adaptations for systems that | 
| 53 // remotely cope with the load right now. | 55 // remotely cope with the load right now. | 
| 54 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 56 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 }; | 145 }; | 
| 144 | 146 | 
| 145 // VideoSourceProxy is responsible ensuring thread safety between calls to | 147 // VideoSourceProxy is responsible ensuring thread safety between calls to | 
| 146 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a | 148 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a | 
| 147 // video capturer is connected to the encoder and the encoder task queue | 149 // video capturer is connected to the encoder and the encoder task queue | 
| 148 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 150 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 
| 149 class ViEEncoder::VideoSourceProxy { | 151 class ViEEncoder::VideoSourceProxy { | 
| 150 public: | 152 public: | 
| 151 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 153 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 
| 152 : vie_encoder_(vie_encoder), | 154 : vie_encoder_(vie_encoder), | 
| 153 degradation_preference_(DegradationPreference::kMaintainResolution), | 155 degradation_preference_(DegradationPreference::kDegradationDisabled), | 
| 154 source_(nullptr) {} | 156 source_(nullptr) {} | 
| 155 | 157 | 
| 156 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, | 158 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source, | 
| 157 const DegradationPreference& degradation_preference) { | 159 const DegradationPreference& degradation_preference) { | 
| 158 // Called on libjingle's worker thread. | 160 // Called on libjingle's worker thread. | 
| 159 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 161 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 
| 160 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 162 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 
| 161 rtc::VideoSinkWants wants; | 163 rtc::VideoSinkWants wants; | 
| 162 { | 164 { | 
| 163 rtc::CritScope lock(&crit_); | 165 rtc::CritScope lock(&crit_); | 
| 166 degradation_preference_ = degradation_preference; | |
| 164 old_source = source_; | 167 old_source = source_; | 
| 165 source_ = source; | 168 source_ = source; | 
| 166 degradation_preference_ = degradation_preference; | 169 wants = GetActiveSinkWants(); | 
| 167 wants = current_wants(); | |
| 168 } | 170 } | 
| 169 | 171 | 
| 170 if (old_source != source && old_source != nullptr) { | 172 if (old_source != source && old_source != nullptr) { | 
| 171 old_source->RemoveSink(vie_encoder_); | 173 old_source->RemoveSink(vie_encoder_); | 
| 172 } | 174 } | 
| 173 | 175 | 
| 174 if (!source) { | 176 if (!source) { | 
| 175 return; | 177 return; | 
| 176 } | 178 } | 
| 177 | 179 | 
| 178 source->AddOrUpdateSink(vie_encoder_, wants); | 180 source->AddOrUpdateSink(vie_encoder_, wants); | 
| 179 } | 181 } | 
| 180 | 182 | 
| 181 void SetWantsRotationApplied(bool rotation_applied) { | 183 void SetWantsRotationApplied(bool rotation_applied) { | 
| 182 rtc::CritScope lock(&crit_); | 184 rtc::CritScope lock(&crit_); | 
| 183 sink_wants_.rotation_applied = rotation_applied; | 185 sink_wants_.rotation_applied = rotation_applied; | 
| 184 disabled_scaling_sink_wants_.rotation_applied = rotation_applied; | 186 if (source_) | 
| 185 if (source_) { | 187 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 
| 186 source_->AddOrUpdateSink(vie_encoder_, current_wants()); | 188 } | 
| 189 | |
| 190 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | |
| 191 rtc::VideoSinkWants wants = sink_wants_; | |
| 192 // Clear any constraints from the current sink wants that don't apply to | |
| 193 // the used degradation_preference. | |
| 194 switch (degradation_preference_) { | |
| 195 case DegradationPreference::kBalanced: | |
| 196 FALLTHROUGH(); | |
| 197 case DegradationPreference::kMaintainFramerate: | |
| 198 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 199 break; | |
| 200 case DegradationPreference::kMaintainResolution: | |
| 201 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 202 wants.target_pixel_count.reset(); | |
| 203 break; | |
| 204 case DegradationPreference::kDegradationDisabled: | |
| 205 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 206 wants.target_pixel_count.reset(); | |
| 207 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 187 } | 208 } | 
| 209 return wants; | |
| 188 } | 210 } | 
| 189 | 211 | 
| 190 void RequestResolutionLowerThan(int pixel_count) { | 212 void RequestResolutionLowerThan(int pixel_count) { | 
| 191 // Called on the encoder task queue. | 213 // Called on the encoder task queue. | 
| 192 rtc::CritScope lock(&crit_); | 214 rtc::CritScope lock(&crit_); | 
| 193 if (!IsResolutionScalingEnabledLocked()) { | 215 if (!IsResolutionScalingEnabledLocked()) { | 
| 194 // This can happen since |degradation_preference_| is set on | 216 // This can happen since |degradation_preference_| is set on | 
| 195 // libjingle's worker thread but the adaptation is done on the encoder | 217 // libjingle's worker thread but the adaptation is done on the encoder | 
| 196 // task queue. | 218 // task queue. | 
| 197 return; | 219 return; | 
| 198 } | 220 } | 
| 199 // The input video frame size will have a resolution with less than or | 221 // The input video frame size will have a resolution with less than or | 
| 200 // equal to |max_pixel_count| depending on how the source can scale the | 222 // equal to |max_pixel_count| depending on how the source can scale the | 
| 201 // input frame size. | 223 // input frame size. | 
| 202 const int pixels_wanted = (pixel_count * 3) / 5; | 224 const int pixels_wanted = (pixel_count * 3) / 5; | 
| 203 if (pixels_wanted < kMinPixelsPerFrame) | 225 if (pixels_wanted < kMinPixelsPerFrame) | 
| 204 return; | 226 return; | 
| 205 sink_wants_.max_pixel_count = rtc::Optional<int>(pixels_wanted); | 227 sink_wants_.max_pixel_count = pixels_wanted; | 
| 206 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 228 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 
| 207 if (source_) | 229 if (source_) | 
| 208 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 230 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 
| 231 } | |
| 232 | |
| 233 void RequestFramerateLowerThan(int framerate_fps) { | |
| 234 // Called on the encoder task queue. | |
| 235 rtc::CritScope lock(&crit_); | |
| 236 if (!IsFramerateScalingEnabledLocked()) { | |
| 237 // This can happen since |degradation_preference_| is set on | |
| 238 // libjingle's worker thread but the adaptation is done on the encoder | |
| 239 // task queue. | |
| 240 return; | |
| 241 } | |
| 242 // The input video frame rate will be scaled down to 2/3 of input fps, | |
| 243 // rounding down. | |
| 244 const int framerate_wanted = | |
| 245 std::max(kMinFramerateFps, (framerate_fps * 2) / 3); | |
| 246 sink_wants_.max_framerate_fps = framerate_wanted; | |
| 247 if (source_) | |
| 248 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | |
| 209 } | 249 } | 
| 210 | 250 | 
| 211 void RequestHigherResolutionThan(int pixel_count) { | 251 void RequestHigherResolutionThan(int pixel_count) { | 
| 212 rtc::CritScope lock(&crit_); | 252 rtc::CritScope lock(&crit_); | 
| 213 if (!IsResolutionScalingEnabledLocked()) { | 253 if (!IsResolutionScalingEnabledLocked()) { | 
| 214 // This can happen since |degradation_preference_| is set on | 254 // This can happen since |degradation_preference_| is set on | 
| 215 // libjingle's worker thread but the adaptation is done on the encoder | 255 // libjingle's worker thread but the adaptation is done on the encoder | 
| 216 // task queue. | 256 // task queue. | 
| 217 return; | 257 return; | 
| 218 } | 258 } | 
| 219 // On step down we request at most 3/5 the pixel count of the previous | 259 | 
| 220 // resolution, so in order to take "one step up" we request a resolution as | 260 if (pixel_count == std::numeric_limits<int>::max()) { | 
| 221 // close as possible to 5/3 of the current resolution. The actual pixel | 261 // Remove any constraints. | 
| 222 // count selected depends on the capabilities of the source. In order to not | 262 sink_wants_.target_pixel_count.reset(); | 
| 223 // take a too large step up, we cap the requested pixel count to be at most | 263 sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); | 
| 224 // four time the current number of pixels. | 264 } else { | 
| 225 sink_wants_.target_pixel_count = rtc::Optional<int>((pixel_count * 5) / 3); | 265 // On step down we request at most 3/5 the pixel count of the previous | 
| 226 sink_wants_.max_pixel_count = rtc::Optional<int>(pixel_count * 4); | 266 // resolution, so in order to take "one step up" we request a resolution | 
| 267 // as close as possible to 5/3 of the current resolution. The actual pixel | |
| 268 // count selected depends on the capabilities of the source. In order to | |
| 269 // not take a too large step up, we cap the requested pixel count to be at | |
| 270 // most four time the current number of pixels. | |
| 271 sink_wants_.target_pixel_count = | |
| 272 rtc::Optional<int>((pixel_count * 5) / 3); | |
| 273 sink_wants_.max_pixel_count = pixel_count * 4; | |
| 274 } | |
| 227 if (source_) | 275 if (source_) | 
| 228 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 276 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 
| 277 } | |
| 278 | |
| 279 void RequestHigherFramerateThan(int framerate_fps) { | |
| 280 // Called on the encoder task queue. | |
| 281 rtc::CritScope lock(&crit_); | |
| 282 if (!IsFramerateScalingEnabledLocked()) { | |
| 283 // This can happen since |degradation_preference_| is set on | |
| 284 // libjingle's worker thread but the adaptation is done on the encoder | |
| 285 // task queue. | |
| 286 return; | |
| 287 } | |
| 288 if (framerate_fps == std::numeric_limits<int>::max()) { | |
| 289 // Remove any restrains. | |
| 290 sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 291 } else { | |
| 292 // The input video frame rate will be scaled up to the last step, with | |
| 293 // rounding. | |
| 294 const int framerate_wanted = (framerate_fps * 3) / 2; | |
| 295 sink_wants_.max_framerate_fps = framerate_wanted; | |
| 296 } | |
| 297 if (source_) | |
| 298 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | |
| 229 } | 299 } | 
| 230 | 300 | 
| 231 private: | 301 private: | 
| 232 bool IsResolutionScalingEnabledLocked() const | 302 bool IsResolutionScalingEnabledLocked() const | 
| 233 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 303 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 
| 234 return degradation_preference_ != | 304 return degradation_preference_ != | 
| 235 DegradationPreference::kMaintainResolution; | 305 DegradationPreference::kMaintainResolution; | 
| 236 } | 306 } | 
| 237 | 307 | 
| 238 const rtc::VideoSinkWants& current_wants() const | 308 bool IsFramerateScalingEnabledLocked() const | 
| 239 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 309 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 
| 240 return IsResolutionScalingEnabledLocked() ? sink_wants_ | 310 return degradation_preference_ == | 
| 241 : disabled_scaling_sink_wants_; | 311 DegradationPreference::kMaintainResolution; | 
| 242 } | 312 } | 
| 243 | 313 | 
| 244 rtc::CriticalSection crit_; | 314 rtc::CriticalSection crit_; | 
| 245 rtc::SequencedTaskChecker main_checker_; | 315 rtc::SequencedTaskChecker main_checker_; | 
| 246 ViEEncoder* const vie_encoder_; | 316 ViEEncoder* const vie_encoder_; | 
| 247 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 317 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 
| 248 rtc::VideoSinkWants disabled_scaling_sink_wants_ GUARDED_BY(&crit_); | |
| 249 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); | 318 DegradationPreference degradation_preference_ GUARDED_BY(&crit_); | 
| 250 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 319 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 
| 251 | 320 | 
| 252 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 321 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 
| 253 }; | 322 }; | 
| 254 | 323 | 
| 255 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 324 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 
| 256 SendStatisticsProxy* stats_proxy, | 325 SendStatisticsProxy* stats_proxy, | 
| 257 const VideoSendStream::Config::EncoderSettings& settings, | 326 const VideoSendStream::Config::EncoderSettings& settings, | 
| 258 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 327 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 
| (...skipping 14 matching lines...) Expand all Loading... | |
| 273 stats_proxy_(stats_proxy), | 342 stats_proxy_(stats_proxy), | 
| 274 pre_encode_callback_(pre_encode_callback), | 343 pre_encode_callback_(pre_encode_callback), | 
| 275 module_process_thread_(nullptr), | 344 module_process_thread_(nullptr), | 
| 276 pending_encoder_reconfiguration_(false), | 345 pending_encoder_reconfiguration_(false), | 
| 277 encoder_start_bitrate_bps_(0), | 346 encoder_start_bitrate_bps_(0), | 
| 278 max_data_payload_length_(0), | 347 max_data_payload_length_(0), | 
| 279 nack_enabled_(false), | 348 nack_enabled_(false), | 
| 280 last_observed_bitrate_bps_(0), | 349 last_observed_bitrate_bps_(0), | 
| 281 encoder_paused_and_dropped_frame_(false), | 350 encoder_paused_and_dropped_frame_(false), | 
| 282 clock_(Clock::GetRealTimeClock()), | 351 clock_(Clock::GetRealTimeClock()), | 
| 283 scale_counter_(kScaleReasonSize, 0), | 352 degradation_preference_(DegradationPreference::kDegradationDisabled), | 
| 284 degradation_preference_(DegradationPreference::kMaintainResolution), | |
| 285 last_captured_timestamp_(0), | 353 last_captured_timestamp_(0), | 
| 286 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 354 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 
| 287 clock_->TimeInMilliseconds()), | 355 clock_->TimeInMilliseconds()), | 
| 288 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 356 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 
| 289 captured_frame_count_(0), | 357 captured_frame_count_(0), | 
| 290 dropped_frame_count_(0), | 358 dropped_frame_count_(0), | 
| 291 bitrate_observer_(nullptr), | 359 bitrate_observer_(nullptr), | 
| 292 encoder_queue_("EncoderQueue") { | 360 encoder_queue_("EncoderQueue") { | 
| 293 RTC_DCHECK(stats_proxy); | 361 RTC_DCHECK(stats_proxy); | 
| 294 encoder_queue_.PostTask([this] { | 362 encoder_queue_.PostTask([this] { | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 345 }); | 413 }); | 
| 346 } | 414 } | 
| 347 | 415 | 
| 348 void ViEEncoder::SetSource( | 416 void ViEEncoder::SetSource( | 
| 349 rtc::VideoSourceInterface<VideoFrame>* source, | 417 rtc::VideoSourceInterface<VideoFrame>* source, | 
| 350 const VideoSendStream::DegradationPreference& degradation_preference) { | 418 const VideoSendStream::DegradationPreference& degradation_preference) { | 
| 351 RTC_DCHECK_RUN_ON(&thread_checker_); | 419 RTC_DCHECK_RUN_ON(&thread_checker_); | 
| 352 source_proxy_->SetSource(source, degradation_preference); | 420 source_proxy_->SetSource(source, degradation_preference); | 
| 353 encoder_queue_.PostTask([this, degradation_preference] { | 421 encoder_queue_.PostTask([this, degradation_preference] { | 
| 354 RTC_DCHECK_RUN_ON(&encoder_queue_); | 422 RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 355 | 423 if (degradation_preference_ != degradation_preference) { | 
| 424 // Reset adaptation state, so that we're not tricked into thinking there's | |
| 425 // an already pending request of the same type. | |
| 426 last_adaptation_request_.reset(); | |
| 427 } | |
| 356 degradation_preference_ = degradation_preference; | 428 degradation_preference_ = degradation_preference; | 
| 357 initial_rampup_ = | 429 initial_rampup_ = | 
| 358 degradation_preference_ != DegradationPreference::kMaintainResolution | 430 degradation_preference_ != DegradationPreference::kMaintainResolution | 
| 359 ? 0 | 431 ? 0 | 
| 360 : kMaxInitialFramedrop; | 432 : kMaxInitialFramedrop; | 
| 361 ConfigureQualityScaler(); | 433 ConfigureQualityScaler(); | 
| 362 }); | 434 }); | 
| 363 } | 435 } | 
| 364 | 436 | 
| 365 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 437 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 | 525 | 
| 454 ConfigureQualityScaler(); | 526 ConfigureQualityScaler(); | 
| 455 } | 527 } | 
| 456 | 528 | 
| 457 void ViEEncoder::ConfigureQualityScaler() { | 529 void ViEEncoder::ConfigureQualityScaler() { | 
| 458 RTC_DCHECK_RUN_ON(&encoder_queue_); | 530 RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 459 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 531 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 
| 460 const bool degradation_preference_allows_scaling = | 532 const bool degradation_preference_allows_scaling = | 
| 461 degradation_preference_ != DegradationPreference::kMaintainResolution; | 533 degradation_preference_ != DegradationPreference::kMaintainResolution; | 
| 462 | 534 | 
| 535 const std::vector<int>& scale_counters = GetScaleCounters(); | |
| 463 stats_proxy_->SetResolutionRestrictionStats( | 536 stats_proxy_->SetResolutionRestrictionStats( | 
| 464 degradation_preference_allows_scaling, scale_counter_[kCpu] > 0, | 537 degradation_preference_allows_scaling, scale_counters[kCpu] > 0, | 
| 465 scale_counter_[kQuality]); | 538 scale_counters[kQuality]); | 
| 466 | 539 | 
| 467 if (degradation_preference_allows_scaling && | 540 if (degradation_preference_allows_scaling && | 
| 468 scaling_settings.enabled) { | 541 scaling_settings.enabled) { | 
| 469 // Abort if quality scaler has already been configured. | 542 // Abort if quality scaler has already been configured. | 
| 470 if (quality_scaler_.get() != nullptr) | 543 if (quality_scaler_.get() != nullptr) | 
| 471 return; | 544 return; | 
| 472 // Drop frames and scale down until desired quality is achieved. | 545 // Drop frames and scale down until desired quality is achieved. | 
| 473 if (scaling_settings.thresholds) { | 546 if (scaling_settings.thresholds) { | 
| 474 quality_scaler_.reset( | 547 quality_scaler_.reset( | 
| 475 new QualityScaler(this, *(scaling_settings.thresholds))); | 548 new QualityScaler(this, *(scaling_settings.thresholds))); | 
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 701 | 774 | 
| 702 if (video_suspension_changed) { | 775 if (video_suspension_changed) { | 
| 703 LOG(LS_INFO) << "Video suspend state changed to: " | 776 LOG(LS_INFO) << "Video suspend state changed to: " | 
| 704 << (video_is_suspended ? "suspended" : "not suspended"); | 777 << (video_is_suspended ? "suspended" : "not suspended"); | 
| 705 stats_proxy_->OnSuspendChange(video_is_suspended); | 778 stats_proxy_->OnSuspendChange(video_is_suspended); | 
| 706 } | 779 } | 
| 707 } | 780 } | 
| 708 | 781 | 
| 709 void ViEEncoder::AdaptDown(AdaptReason reason) { | 782 void ViEEncoder::AdaptDown(AdaptReason reason) { | 
| 710 RTC_DCHECK_RUN_ON(&encoder_queue_); | 783 RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 711 if (degradation_preference_ != DegradationPreference::kBalanced) | 784 AdaptationRequest adaptation_request = { | 
| 712 return; | 785 last_frame_info_->pixel_count(), | 
| 713 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | 786 stats_proxy_->GetStats().input_frame_rate, | 
| 714 int current_pixel_count = last_frame_info_->pixel_count(); | 787 AdaptationRequest::Mode::kAdaptDown}; | 
| 715 if (last_adaptation_request_ && | 788 bool downgrade_requested = | 
| 716 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown && | 789 last_adaptation_request_ && | 
| 717 current_pixel_count >= last_adaptation_request_->input_pixel_count_) { | 790 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; | 
| 718 // Don't request lower resolution if the current resolution is not lower | 791 | 
| 719 // than the last time we asked for the resolution to be lowered. | 792 int max_downgrades = 0; | 
| 720 return; | 793 switch (degradation_preference_) { | 
| 794 case DegradationPreference::kBalanced: | |
| 795 FALLTHROUGH(); | |
| 796 case DegradationPreference::kMaintainFramerate: | |
| 797 max_downgrades = kMaxCpuResolutionDowngrades; | |
| 798 if (downgrade_requested && | |
| 799 adaptation_request.input_pixel_count_ >= | |
| 800 last_adaptation_request_->input_pixel_count_) { | |
| 801 // Don't request lower resolution if the current resolution is not | |
| 802 // lower than the last time we asked for the resolution to be lowered. | |
| 803 return; | |
| 804 } | |
| 805 break; | |
| 806 case DegradationPreference::kMaintainResolution: | |
| 807 max_downgrades = kMaxCpuFramerateDowngrades; | |
| 808 if (adaptation_request.framerate_fps_ <= 0 || | |
| 809 (downgrade_requested && | |
| 810 adaptation_request.framerate_fps_ < kMinFramerateFps)) { | |
| 811 // If no input fps estimate available, can't determine how to scale down | |
| 812 // framerate. Otherwise, don't request lower framerate if we don't have | |
| 813 // a valid frame rate. Since framerate, unlike resolution, is a measure | |
| 814 // we have to estimate, and can fluctuate naturally over time, don't | |
| 815 // make the same kind of limitations as for resolution, but trust the | |
| 816 // overuse detector to not trigger too often. | |
| 817 return; | |
| 818 } | |
| 819 break; | |
| 820 case DegradationPreference::kDegradationDisabled: | |
| 821 return; | |
| 721 } | 822 } | 
| 722 last_adaptation_request_.emplace(AdaptationRequest{ | 823 | 
| 723 current_pixel_count, AdaptationRequest::Mode::kAdaptDown}); | 824 last_adaptation_request_.emplace(adaptation_request); | 
| 825 const std::vector<int>& scale_counter = GetScaleCounters(); | |
| 724 | 826 | 
| 725 switch (reason) { | 827 switch (reason) { | 
| 726 case kQuality: | 828 case kQuality: | 
| 727 stats_proxy_->OnQualityRestrictedResolutionChanged( | 829 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter[reason] + | 
| 728 scale_counter_[reason] + 1); | 830 1); | 
| 729 break; | 831 break; | 
| 730 case kCpu: | 832 case kCpu: | 
| 731 if (scale_counter_[reason] >= kMaxCpuDowngrades) | 833 if (scale_counter[reason] >= max_downgrades) | 
| 732 return; | 834 return; | 
| 733 // Update stats accordingly. | 835 // Update stats accordingly. | 
| 734 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 836 stats_proxy_->OnCpuRestrictedResolutionChanged(true); | 
| 735 break; | 837 break; | 
| 736 } | 838 } | 
| 737 ++scale_counter_[reason]; | 839 | 
| 738 source_proxy_->RequestResolutionLowerThan(current_pixel_count); | 840 IncrementScaleCounter(reason, 1); | 
| 739 LOG(LS_INFO) << "Scaling down resolution."; | 841 | 
| 842 switch (degradation_preference_) { | |
| 843 case DegradationPreference::kBalanced: | |
| 844 FALLTHROUGH(); | |
| 845 case DegradationPreference::kMaintainFramerate: | |
| 846 source_proxy_->RequestResolutionLowerThan( | |
| 847 adaptation_request.input_pixel_count_); | |
| 848 LOG(LS_INFO) << "Scaling down resolution."; | |
| 849 break; | |
| 850 case DegradationPreference::kMaintainResolution: | |
| 851 source_proxy_->RequestFramerateLowerThan( | |
| 852 adaptation_request.framerate_fps_); | |
| 853 LOG(LS_INFO) << "Scaling down framerate."; | |
| 854 break; | |
| 855 case DegradationPreference::kDegradationDisabled: | |
| 856 RTC_NOTREACHED(); | |
| 857 } | |
| 858 | |
| 740 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 859 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 
| 741 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 860 LOG(LS_INFO) << "Scaled " << GetScaleCounters()[i] | 
| 742 << " times for reason: " << (i ? "cpu" : "quality"); | 861 << " times for reason: " << (i ? "cpu" : "quality"); | 
| 743 } | 862 } | 
| 744 } | 863 } | 
| 745 | 864 | 
| 746 void ViEEncoder::AdaptUp(AdaptReason reason) { | 865 void ViEEncoder::AdaptUp(AdaptReason reason) { | 
| 747 RTC_DCHECK_RUN_ON(&encoder_queue_); | 866 RTC_DCHECK_RUN_ON(&encoder_queue_); | 
| 748 if (scale_counter_[reason] == 0 || | 867 int scale_counter = GetScaleCounters()[reason]; | 
| 749 degradation_preference_ != DegradationPreference::kBalanced) { | 868 if (scale_counter == 0) | 
| 750 return; | 869 return; | 
| 870 RTC_DCHECK_GT(scale_counter, 0); | |
| 871 AdaptationRequest adaptation_request = { | |
| 872 last_frame_info_->pixel_count(), | |
| 873 stats_proxy_->GetStats().input_frame_rate, | |
| 874 AdaptationRequest::Mode::kAdaptUp}; | |
| 875 | |
| 876 bool adapt_up_requested = | |
| 877 last_adaptation_request_ && | |
| 878 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; | |
| 879 switch (degradation_preference_) { | |
| 880 case DegradationPreference::kBalanced: | |
| 881 FALLTHROUGH(); | |
| 882 case DegradationPreference::kMaintainFramerate: | |
| 883 if (adapt_up_requested && | |
| 884 adaptation_request.input_pixel_count_ <= | |
| 885 last_adaptation_request_->input_pixel_count_) { | |
| 886 // Don't request higher resolution if the current resolution is not | |
| 887 // higher than the last time we asked for the resolution to be higher. | |
| 888 return; | |
| 889 } | |
| 890 break; | |
| 891 case DegradationPreference::kMaintainResolution: | |
| 892 // TODO(sprang): Don't request higher framerate if we are already at | |
| 893 // max requested fps? | |
| 894 break; | |
| 895 case DegradationPreference::kDegradationDisabled: | |
| 896 return; | |
| 751 } | 897 } | 
| 752 // Only scale if resolution is higher than last time we requested higher | |
| 753 // resolution. | |
| 754 RTC_DCHECK(static_cast<bool>(last_frame_info_)); | |
| 755 int current_pixel_count = last_frame_info_->pixel_count(); | |
| 756 if (last_adaptation_request_ && | |
| 757 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp && | |
| 758 current_pixel_count <= last_adaptation_request_->input_pixel_count_) { | |
| 759 // Don't request higher resolution if the current resolution is not higher | |
| 760 // than the last time we asked for the resolution to be higher. | |
| 761 return; | |
| 762 } | |
| 763 last_adaptation_request_.emplace(AdaptationRequest{ | |
| 764 current_pixel_count, AdaptationRequest::Mode::kAdaptUp}); | |
| 
åsapersson
2017/03/29 08:59:41
Should last_adaptation_request_ be updated?
 
sprang_webrtc
2017/03/29 09:03:08
Yes? Not sure I understand the question.
 
sprang_webrtc
2017/03/30 09:15:40
You're right. 
Looking in the wrong spot...
 | |
| 765 | 898 | 
| 766 switch (reason) { | 899 switch (reason) { | 
| 767 case kQuality: | 900 case kQuality: | 
| 768 stats_proxy_->OnQualityRestrictedResolutionChanged( | 901 stats_proxy_->OnQualityRestrictedResolutionChanged(scale_counter - 1); | 
| 769 scale_counter_[reason] - 1); | |
| 770 break; | 902 break; | 
| 771 case kCpu: | 903 case kCpu: | 
| 772 // Update stats accordingly. | 904 // Update stats accordingly. | 
| 773 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter_[reason] > | 905 stats_proxy_->OnCpuRestrictedResolutionChanged(scale_counter > 1); | 
| 774 1); | |
| 775 break; | 906 break; | 
| 776 } | 907 } | 
| 777 --scale_counter_[reason]; | 908 | 
| 778 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 909 // Decrease counter of how many times we have scaled down, for this | 
| 779 LOG(LS_INFO) << "Scaling up resolution."; | 910 // degradation preference mode and reason. | 
| 911 IncrementScaleCounter(reason, -1); | |
| 912 | |
| 913 // Get a sum of how many times have scaled down, in total, for this | |
| 914 // degradation preference mode. If it is 0, remove any restraints. | |
| 915 const std::vector<int>& current_scale_counters = GetScaleCounters(); | |
| 916 const int scale_sum = std::accumulate(current_scale_counters.begin(), | |
| 917 current_scale_counters.end(), 0); | |
| 918 switch (degradation_preference_) { | |
| 919 case DegradationPreference::kBalanced: | |
| 920 FALLTHROUGH(); | |
| 921 case DegradationPreference::kMaintainFramerate: | |
| 922 if (scale_sum == 0) { | |
| 923 LOG(LS_INFO) << "Removing resolution down-scaling setting."; | |
| 924 source_proxy_->RequestHigherResolutionThan( | |
| 925 std::numeric_limits<int>::max()); | |
| 926 } else { | |
| 927 source_proxy_->RequestHigherResolutionThan( | |
| 928 adaptation_request.input_pixel_count_); | |
| 929 LOG(LS_INFO) << "Scaling up resolution."; | |
| 930 } | |
| 931 break; | |
| 932 case DegradationPreference::kMaintainResolution: | |
| 933 if (scale_sum == 0) { | |
| 934 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | |
| 935 source_proxy_->RequestHigherFramerateThan( | |
| 936 std::numeric_limits<int>::max()); | |
| 937 } else { | |
| 938 source_proxy_->RequestHigherFramerateThan( | |
| 939 adaptation_request.framerate_fps_); | |
| 940 LOG(LS_INFO) << "Scaling up framerate."; | |
| 941 } | |
| 942 break; | |
| 943 case DegradationPreference::kDegradationDisabled: | |
| 944 RTC_NOTREACHED(); | |
| 945 } | |
| 946 | |
| 780 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 947 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 
| 781 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 948 LOG(LS_INFO) << "Scaled " << current_scale_counters[i] | 
| 782 << " times for reason: " << (i ? "cpu" : "quality"); | 949 << " times for reason: " << (i ? "cpu" : "quality"); | 
| 783 } | 950 } | 
| 784 } | 951 } | 
| 785 | 952 | 
| 953 const std::vector<int>& ViEEncoder::GetScaleCounters() { | |
| 954 auto it = scale_counters_.find(degradation_preference_); | |
| 955 if (it == scale_counters_.end()) { | |
| 956 scale_counters_[degradation_preference_].resize(kScaleReasonSize); | |
| 957 return scale_counters_[degradation_preference_]; | |
| 958 } | |
| 959 return it->second; | |
| 960 } | |
| 961 | |
| 962 void ViEEncoder::IncrementScaleCounter(int reason, int delta) { | |
| 963 // Get the counters and validate. This may also lazily initialize the state. | |
| 964 const std::vector<int>& counter = GetScaleCounters(); | |
| 965 if (delta < 0) { | |
| 966 RTC_DCHECK_GE(counter[reason], delta); | |
| 967 } | |
| 968 scale_counters_[degradation_preference_][reason] += delta; | |
| 969 } | |
| 970 | |
| 786 } // namespace webrtc | 971 } // namespace webrtc | 
| OLD | NEW |