Chromium Code Reviews| 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 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 | 37 |
| 38 // Time interval for logging frame counts. | 38 // Time interval for logging frame counts. |
| 39 const int64_t kFrameLogIntervalMs = 60000; | 39 const int64_t kFrameLogIntervalMs = 60000; |
| 40 | 40 |
| 41 // We will never ask for a resolution lower than this. | 41 // We will never ask for a resolution lower than this. |
| 42 // TODO(kthelgason): Lower this limit when better testing | 42 // TODO(kthelgason): Lower this limit when better testing |
| 43 // on MediaCodec and fallback implementations are in place. | 43 // on MediaCodec and fallback implementations are in place. |
| 44 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 | 44 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7206 |
| 45 const int kMinPixelsPerFrame = 320 * 180; | 45 const int kMinPixelsPerFrame = 320 * 180; |
| 46 const int kMinFramerateFps = 2; | 46 const int kMinFramerateFps = 2; |
| 47 const int kMaxFramerateFps = 120; | |
| 47 | 48 |
| 48 // The maximum number of frames to drop at beginning of stream | 49 // The maximum number of frames to drop at beginning of stream |
| 49 // to try and achieve desired bitrate. | 50 // to try and achieve desired bitrate. |
| 50 const int kMaxInitialFramedrop = 4; | 51 const int kMaxInitialFramedrop = 4; |
| 51 | 52 |
| 52 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | |
| 53 // pipelining encoders better (multiple input frames before something comes | |
| 54 // out). This should effectively turn off CPU adaptations for systems that | |
| 55 // remotely cope with the load right now. | |
| 56 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | |
| 57 CpuOveruseOptions options; | |
| 58 if (full_overuse_time) { | |
| 59 options.low_encode_usage_threshold_percent = 150; | |
| 60 options.high_encode_usage_threshold_percent = 200; | |
| 61 } | |
| 62 return options; | |
| 63 } | |
| 64 | |
| 65 uint32_t MaximumFrameSizeForBitrate(uint32_t kbps) { | 53 uint32_t MaximumFrameSizeForBitrate(uint32_t kbps) { |
| 66 if (kbps > 0) { | 54 if (kbps > 0) { |
| 67 if (kbps < 300 /* qvga */) { | 55 if (kbps < 300 /* qvga */) { |
| 68 return 320 * 240; | 56 return 320 * 240; |
| 69 } else if (kbps < 500 /* vga */) { | 57 } else if (kbps < 500 /* vga */) { |
| 70 return 640 * 480; | 58 return 640 * 480; |
| 71 } | 59 } |
| 72 } | 60 } |
| 73 return std::numeric_limits<uint32_t>::max(); | 61 return std::numeric_limits<uint32_t>::max(); |
| 74 } | 62 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 const VideoSendStream::DegradationPreference& degradation_preference) { | 165 const VideoSendStream::DegradationPreference& degradation_preference) { |
| 178 // Called on libjingle's worker thread. | 166 // Called on libjingle's worker thread. |
| 179 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 167 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
| 180 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 168 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
| 181 rtc::VideoSinkWants wants; | 169 rtc::VideoSinkWants wants; |
| 182 { | 170 { |
| 183 rtc::CritScope lock(&crit_); | 171 rtc::CritScope lock(&crit_); |
| 184 degradation_preference_ = degradation_preference; | 172 degradation_preference_ = degradation_preference; |
| 185 old_source = source_; | 173 old_source = source_; |
| 186 source_ = source; | 174 source_ = source; |
| 187 wants = GetActiveSinkWants(); | 175 wants = GetActiveSinkWantsInternal(); |
| 188 } | 176 } |
| 189 | 177 |
| 190 if (old_source != source && old_source != nullptr) { | 178 if (old_source != source && old_source != nullptr) { |
| 191 old_source->RemoveSink(vie_encoder_); | 179 old_source->RemoveSink(vie_encoder_); |
| 192 } | 180 } |
| 193 | 181 |
| 194 if (!source) { | 182 if (!source) { |
| 195 return; | 183 return; |
| 196 } | 184 } |
| 197 | 185 |
| 198 source->AddOrUpdateSink(vie_encoder_, wants); | 186 source->AddOrUpdateSink(vie_encoder_, wants); |
| 199 } | 187 } |
| 200 | 188 |
| 201 void SetWantsRotationApplied(bool rotation_applied) { | 189 void SetWantsRotationApplied(bool rotation_applied) { |
| 202 rtc::CritScope lock(&crit_); | 190 rtc::CritScope lock(&crit_); |
| 203 sink_wants_.rotation_applied = rotation_applied; | 191 sink_wants_.rotation_applied = rotation_applied; |
| 204 if (source_) | 192 if (source_) |
| 205 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 193 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); |
| 206 } | 194 } |
| 207 | 195 |
| 208 rtc::VideoSinkWants GetActiveSinkWants() EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 196 rtc::VideoSinkWants GetActiveSinkWants() { |
| 209 rtc::VideoSinkWants wants = sink_wants_; | 197 rtc::CritScope lock(&crit_); |
| 210 // Clear any constraints from the current sink wants that don't apply to | 198 return GetActiveSinkWantsInternal(); |
| 211 // the used degradation_preference. | |
| 212 switch (degradation_preference_) { | |
| 213 case VideoSendStream::DegradationPreference::kBalanced: | |
| 214 FALLTHROUGH(); | |
| 215 case VideoSendStream::DegradationPreference::kMaintainFramerate: | |
| 216 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 217 break; | |
| 218 case VideoSendStream::DegradationPreference::kMaintainResolution: | |
| 219 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 220 wants.target_pixel_count.reset(); | |
| 221 break; | |
| 222 case VideoSendStream::DegradationPreference::kDegradationDisabled: | |
| 223 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 224 wants.target_pixel_count.reset(); | |
| 225 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 226 } | |
| 227 return wants; | |
| 228 } | 199 } |
| 229 | 200 |
| 230 bool RequestResolutionLowerThan(int pixel_count) { | 201 bool RequestResolutionLowerThan(int pixel_count) { |
| 231 // Called on the encoder task queue. | 202 // Called on the encoder task queue. |
| 232 rtc::CritScope lock(&crit_); | 203 rtc::CritScope lock(&crit_); |
| 233 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { | 204 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
| 234 // This can happen since |degradation_preference_| is set on libjingle's | 205 // This can happen since |degradation_preference_| is set on libjingle's |
| 235 // worker thread but the adaptation is done on the encoder task queue. | 206 // worker thread but the adaptation is done on the encoder task queue. |
| 236 return false; | 207 return false; |
| 237 } | 208 } |
| 238 // The input video frame size will have a resolution less than or equal to | 209 // The input video frame size will have a resolution less than or equal to |
| 239 // |max_pixel_count| depending on how the source can scale the frame size. | 210 // |max_pixel_count| depending on how the source can scale the frame size. |
| 240 const int pixels_wanted = (pixel_count * 3) / 5; | 211 const int pixels_wanted = (pixel_count * 3) / 5; |
| 241 if (pixels_wanted < kMinPixelsPerFrame || | 212 if (pixels_wanted < kMinPixelsPerFrame || |
| 242 pixels_wanted >= sink_wants_.max_pixel_count) { | 213 pixels_wanted >= sink_wants_.max_pixel_count) { |
| 243 return false; | 214 return false; |
| 244 } | 215 } |
| 245 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; | 216 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; |
| 246 sink_wants_.max_pixel_count = pixels_wanted; | 217 sink_wants_.max_pixel_count = pixels_wanted; |
| 247 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 218 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
| 248 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 219 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); |
| 249 return true; | 220 return true; |
| 250 } | 221 } |
| 251 | 222 |
| 252 bool RequestFramerateLowerThan(int fps) { | 223 int RequestFramerateLowerThan(int fps) { |
| 253 // Called on the encoder task queue. | 224 // Called on the encoder task queue. |
| 254 // The input video frame rate will be scaled down to 2/3, rounding down. | 225 // The input video frame rate will be scaled down to 2/3, rounding down. |
| 255 return RestrictFramerate((fps * 2) / 3); | 226 int framerate_wanted = (fps * 2) / 3; |
| 227 return RestrictFramerate(framerate_wanted) ? framerate_wanted : -1; | |
| 256 } | 228 } |
| 257 | 229 |
| 258 bool RequestHigherResolutionThan(int pixel_count) { | 230 bool RequestHigherResolutionThan(int pixel_count) { |
| 259 // Called on the encoder task queue. | 231 // Called on the encoder task queue. |
| 260 rtc::CritScope lock(&crit_); | 232 rtc::CritScope lock(&crit_); |
| 261 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { | 233 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
| 262 // This can happen since |degradation_preference_| is set on libjingle's | 234 // This can happen since |degradation_preference_| is set on libjingle's |
| 263 // worker thread but the adaptation is done on the encoder task queue. | 235 // worker thread but the adaptation is done on the encoder task queue. |
| 264 return false; | 236 return false; |
| 265 } | 237 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 278 // On step down we request at most 3/5 the pixel count of the previous | 250 // On step down we request at most 3/5 the pixel count of the previous |
| 279 // resolution, so in order to take "one step up" we request a resolution | 251 // resolution, so in order to take "one step up" we request a resolution |
| 280 // as close as possible to 5/3 of the current resolution. The actual pixel | 252 // as close as possible to 5/3 of the current resolution. The actual pixel |
| 281 // count selected depends on the capabilities of the source. In order to | 253 // count selected depends on the capabilities of the source. In order to |
| 282 // not take a too large step up, we cap the requested pixel count to be at | 254 // not take a too large step up, we cap the requested pixel count to be at |
| 283 // most four time the current number of pixels. | 255 // most four time the current number of pixels. |
| 284 sink_wants_.target_pixel_count = | 256 sink_wants_.target_pixel_count = |
| 285 rtc::Optional<int>((pixel_count * 5) / 3); | 257 rtc::Optional<int>((pixel_count * 5) / 3); |
| 286 } | 258 } |
| 287 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; | 259 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; |
| 288 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 260 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); |
| 289 return true; | 261 return true; |
| 290 } | 262 } |
| 291 | 263 |
| 292 bool RequestHigherFramerateThan(int fps) { | 264 // Request upgrade in framerate. Returns the new requested frame, or -1 if |
| 265 // no change requested. Note that maxint may be returned if limits due to | |
| 266 // adaptation requests are removed completely. In that case, consider | |
| 267 // |max_framerate_| to be the current limit (assuming the capturer complies). | |
| 268 int RequestHigherFramerateThan(int fps) { | |
| 293 // Called on the encoder task queue. | 269 // Called on the encoder task queue. |
| 294 // The input frame rate will be scaled up to the last step, with rounding. | 270 // The input frame rate will be scaled up to the last step, with rounding. |
| 295 int framerate_wanted = fps; | 271 int framerate_wanted = fps; |
| 296 if (fps != std::numeric_limits<int>::max()) | 272 if (fps != std::numeric_limits<int>::max()) |
| 297 framerate_wanted = (fps * 3) / 2; | 273 framerate_wanted = (fps * 3) / 2; |
| 298 | 274 |
| 299 return IncreaseFramerate(framerate_wanted); | 275 return IncreaseFramerate(framerate_wanted) ? framerate_wanted : -1; |
| 276 } | |
| 277 | |
| 278 private: | |
| 279 rtc::VideoSinkWants GetActiveSinkWantsInternal() | |
| 280 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | |
| 281 rtc::VideoSinkWants wants = sink_wants_; | |
| 282 // Clear any constraints from the current sink wants that don't apply to | |
| 283 // the used degradation_preference. | |
| 284 switch (degradation_preference_) { | |
| 285 case VideoSendStream::DegradationPreference::kBalanced: | |
| 286 FALLTHROUGH(); | |
| 287 case VideoSendStream::DegradationPreference::kMaintainFramerate: | |
| 288 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 289 break; | |
| 290 case VideoSendStream::DegradationPreference::kMaintainResolution: | |
| 291 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 292 wants.target_pixel_count.reset(); | |
| 293 break; | |
| 294 case VideoSendStream::DegradationPreference::kDegradationDisabled: | |
| 295 wants.max_pixel_count = std::numeric_limits<int>::max(); | |
| 296 wants.target_pixel_count.reset(); | |
| 297 wants.max_framerate_fps = std::numeric_limits<int>::max(); | |
| 298 } | |
| 299 return wants; | |
| 300 } | 300 } |
| 301 | 301 |
| 302 bool RestrictFramerate(int fps) { | 302 bool RestrictFramerate(int fps) { |
| 303 // Called on the encoder task queue. | 303 // Called on the encoder task queue. |
| 304 rtc::CritScope lock(&crit_); | 304 rtc::CritScope lock(&crit_); |
| 305 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) | 305 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 306 return false; | 306 return false; |
| 307 | 307 |
| 308 const int fps_wanted = std::max(kMinFramerateFps, fps); | 308 const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 309 if (fps_wanted >= sink_wants_.max_framerate_fps) | 309 if (fps_wanted >= sink_wants_.max_framerate_fps) |
| 310 return false; | 310 return false; |
| 311 | 311 |
| 312 LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; | 312 LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; |
| 313 sink_wants_.max_framerate_fps = fps_wanted; | 313 sink_wants_.max_framerate_fps = fps_wanted; |
| 314 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 314 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); |
| 315 return true; | 315 return true; |
| 316 } | 316 } |
| 317 | 317 |
| 318 bool IncreaseFramerate(int fps) { | 318 bool IncreaseFramerate(int fps) { |
| 319 // Called on the encoder task queue. | 319 // Called on the encoder task queue. |
| 320 rtc::CritScope lock(&crit_); | 320 rtc::CritScope lock(&crit_); |
| 321 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) | 321 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 322 return false; | 322 return false; |
| 323 | 323 |
| 324 const int fps_wanted = std::max(kMinFramerateFps, fps); | 324 const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 325 if (fps_wanted <= sink_wants_.max_framerate_fps) | 325 if (fps_wanted <= sink_wants_.max_framerate_fps) |
| 326 return false; | 326 return false; |
| 327 | 327 |
| 328 LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; | 328 LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; |
| 329 sink_wants_.max_framerate_fps = fps_wanted; | 329 sink_wants_.max_framerate_fps = fps_wanted; |
| 330 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 330 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); |
| 331 return true; | 331 return true; |
| 332 } | 332 } |
| 333 | 333 |
| 334 private: | 334 private: |
| 335 rtc::CriticalSection crit_; | 335 rtc::CriticalSection crit_; |
| 336 rtc::SequencedTaskChecker main_checker_; | 336 rtc::SequencedTaskChecker main_checker_; |
| 337 ViEEncoder* const vie_encoder_; | 337 ViEEncoder* const vie_encoder_; |
| 338 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 338 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
| 339 VideoSendStream::DegradationPreference degradation_preference_ | 339 VideoSendStream::DegradationPreference degradation_preference_ |
| 340 GUARDED_BY(&crit_); | 340 GUARDED_BY(&crit_); |
| 341 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 341 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
| 342 | 342 |
| 343 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 343 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
| 344 }; | 344 }; |
| 345 | 345 |
| 346 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 346 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
| 347 SendStatisticsProxy* stats_proxy, | 347 SendStatisticsProxy* stats_proxy, |
| 348 const VideoSendStream::Config::EncoderSettings& settings, | 348 const VideoSendStream::Config::EncoderSettings& settings, |
| 349 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 349 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 350 EncodedFrameObserver* encoder_timing) | 350 EncodedFrameObserver* encoder_timing, |
| 351 std::unique_ptr<OveruseFrameDetector> overuse_detector) | |
| 351 : shutdown_event_(true /* manual_reset */, false), | 352 : shutdown_event_(true /* manual_reset */, false), |
| 352 number_of_cores_(number_of_cores), | 353 number_of_cores_(number_of_cores), |
| 353 initial_rampup_(0), | 354 initial_rampup_(0), |
| 354 source_proxy_(new VideoSourceProxy(this)), | 355 source_proxy_(new VideoSourceProxy(this)), |
| 355 sink_(nullptr), | 356 sink_(nullptr), |
| 356 settings_(settings), | 357 settings_(settings), |
| 357 codec_type_(PayloadNameToCodecType(settings.payload_name) | 358 codec_type_(PayloadNameToCodecType(settings.payload_name) |
| 358 .value_or(VideoCodecType::kVideoCodecUnknown)), | 359 .value_or(VideoCodecType::kVideoCodecUnknown)), |
| 359 video_sender_(Clock::GetRealTimeClock(), this, this), | 360 video_sender_(Clock::GetRealTimeClock(), this, this), |
| 360 overuse_detector_(GetCpuOveruseOptions(settings.full_overuse_time), | 361 overuse_detector_( |
| 361 this, | 362 overuse_detector.get() |
| 362 encoder_timing, | 363 ? overuse_detector.release() |
| 363 stats_proxy), | 364 : new OveruseFrameDetector( |
| 365 GetCpuOveruseOptions(settings.full_overuse_time), | |
| 366 this, | |
| 367 encoder_timing, | |
| 368 stats_proxy)), | |
| 364 stats_proxy_(stats_proxy), | 369 stats_proxy_(stats_proxy), |
| 365 pre_encode_callback_(pre_encode_callback), | 370 pre_encode_callback_(pre_encode_callback), |
| 366 module_process_thread_(nullptr), | 371 module_process_thread_(nullptr), |
| 372 max_framerate_(-1), | |
| 367 pending_encoder_reconfiguration_(false), | 373 pending_encoder_reconfiguration_(false), |
| 368 encoder_start_bitrate_bps_(0), | 374 encoder_start_bitrate_bps_(0), |
| 369 max_data_payload_length_(0), | 375 max_data_payload_length_(0), |
| 370 nack_enabled_(false), | 376 nack_enabled_(false), |
| 371 last_observed_bitrate_bps_(0), | 377 last_observed_bitrate_bps_(0), |
| 372 encoder_paused_and_dropped_frame_(false), | 378 encoder_paused_and_dropped_frame_(false), |
| 373 clock_(Clock::GetRealTimeClock()), | 379 clock_(Clock::GetRealTimeClock()), |
| 374 degradation_preference_( | 380 degradation_preference_( |
| 375 VideoSendStream::DegradationPreference::kDegradationDisabled), | 381 VideoSendStream::DegradationPreference::kDegradationDisabled), |
| 376 last_captured_timestamp_(0), | 382 last_captured_timestamp_(0), |
| 377 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 383 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
| 378 clock_->TimeInMilliseconds()), | 384 clock_->TimeInMilliseconds()), |
| 379 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 385 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
| 380 captured_frame_count_(0), | 386 captured_frame_count_(0), |
| 381 dropped_frame_count_(0), | 387 dropped_frame_count_(0), |
| 382 bitrate_observer_(nullptr), | 388 bitrate_observer_(nullptr), |
| 383 encoder_queue_("EncoderQueue") { | 389 encoder_queue_("EncoderQueue") { |
| 384 RTC_DCHECK(stats_proxy); | 390 RTC_DCHECK(stats_proxy); |
| 385 encoder_queue_.PostTask([this] { | 391 encoder_queue_.PostTask([this] { |
| 386 RTC_DCHECK_RUN_ON(&encoder_queue_); | 392 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 387 overuse_detector_.StartCheckForOveruse(); | 393 overuse_detector_->StartCheckForOveruse(); |
| 388 video_sender_.RegisterExternalEncoder( | 394 video_sender_.RegisterExternalEncoder( |
| 389 settings_.encoder, settings_.payload_type, settings_.internal_source); | 395 settings_.encoder, settings_.payload_type, settings_.internal_source); |
| 390 }); | 396 }); |
| 391 } | 397 } |
| 392 | 398 |
| 393 ViEEncoder::~ViEEncoder() { | 399 ViEEncoder::~ViEEncoder() { |
| 394 RTC_DCHECK_RUN_ON(&thread_checker_); | 400 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 395 RTC_DCHECK(shutdown_event_.Wait(0)) | 401 RTC_DCHECK(shutdown_event_.Wait(0)) |
| 396 << "Must call ::Stop() before destruction."; | 402 << "Must call ::Stop() before destruction."; |
| 397 } | 403 } |
| 398 | 404 |
| 405 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | |
| 406 // pipelining encoders better (multiple input frames before something comes | |
| 407 // out). This should effectively turn off CPU adaptations for systems that | |
| 408 // remotely cope with the load right now. | |
| 409 CpuOveruseOptions ViEEncoder::GetCpuOveruseOptions(bool full_overuse_time) { | |
| 410 CpuOveruseOptions options; | |
| 411 if (full_overuse_time) { | |
| 412 options.low_encode_usage_threshold_percent = 150; | |
| 413 options.high_encode_usage_threshold_percent = 200; | |
| 414 } | |
| 415 return options; | |
| 416 } | |
| 417 | |
| 399 void ViEEncoder::Stop() { | 418 void ViEEncoder::Stop() { |
| 400 RTC_DCHECK_RUN_ON(&thread_checker_); | 419 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 401 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 420 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
| 402 encoder_queue_.PostTask([this] { | 421 encoder_queue_.PostTask([this] { |
| 403 RTC_DCHECK_RUN_ON(&encoder_queue_); | 422 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 404 overuse_detector_.StopCheckForOveruse(); | 423 overuse_detector_->StopCheckForOveruse(); |
| 405 rate_allocator_.reset(); | 424 rate_allocator_.reset(); |
| 406 bitrate_observer_ = nullptr; | 425 bitrate_observer_ = nullptr; |
| 407 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 426 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
| 408 false); | 427 false); |
| 409 quality_scaler_ = nullptr; | 428 quality_scaler_ = nullptr; |
| 410 shutdown_event_.Set(); | 429 shutdown_event_.Set(); |
| 411 }); | 430 }); |
| 412 | 431 |
| 413 shutdown_event_.Wait(rtc::Event::kForever); | 432 shutdown_event_.Wait(rtc::Event::kForever); |
| 414 } | 433 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 445 RTC_DCHECK_RUN_ON(&encoder_queue_); | 464 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 446 if (degradation_preference_ != degradation_preference) { | 465 if (degradation_preference_ != degradation_preference) { |
| 447 // Reset adaptation state, so that we're not tricked into thinking there's | 466 // Reset adaptation state, so that we're not tricked into thinking there's |
| 448 // an already pending request of the same type. | 467 // an already pending request of the same type. |
| 449 last_adaptation_request_.reset(); | 468 last_adaptation_request_.reset(); |
| 450 } | 469 } |
| 451 degradation_preference_ = degradation_preference; | 470 degradation_preference_ = degradation_preference; |
| 452 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); | 471 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); |
| 453 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; | 472 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; |
| 454 ConfigureQualityScaler(); | 473 ConfigureQualityScaler(); |
| 474 if (!IsFramerateScalingEnabled(degradation_preference) && | |
| 475 max_framerate_ != -1) { | |
| 476 // If frame rate scaling is no longer allowed, remove any potential | |
| 477 // allowance for longer frame intervals. | |
| 478 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); | |
| 479 } | |
| 455 }); | 480 }); |
| 456 } | 481 } |
| 457 | 482 |
| 458 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 483 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
| 459 source_proxy_->SetWantsRotationApplied(rotation_applied); | 484 source_proxy_->SetWantsRotationApplied(rotation_applied); |
| 460 encoder_queue_.PostTask([this, sink] { | 485 encoder_queue_.PostTask([this, sink] { |
| 461 RTC_DCHECK_RUN_ON(&encoder_queue_); | 486 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 462 sink_ = sink; | 487 sink_ = sink; |
| 463 }); | 488 }); |
| 464 } | 489 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 if (!VideoCodecInitializer::SetupCodec(encoder_config_, settings_, streams, | 539 if (!VideoCodecInitializer::SetupCodec(encoder_config_, settings_, streams, |
| 515 nack_enabled_, &codec, | 540 nack_enabled_, &codec, |
| 516 &rate_allocator_)) { | 541 &rate_allocator_)) { |
| 517 LOG(LS_ERROR) << "Failed to create encoder configuration."; | 542 LOG(LS_ERROR) << "Failed to create encoder configuration."; |
| 518 } | 543 } |
| 519 | 544 |
| 520 codec.startBitrate = | 545 codec.startBitrate = |
| 521 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); | 546 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); |
| 522 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); | 547 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); |
| 523 codec.expect_encode_from_texture = last_frame_info_->is_texture; | 548 codec.expect_encode_from_texture = last_frame_info_->is_texture; |
| 549 max_framerate_ = codec.maxFramerate; | |
| 550 RTC_DCHECK_LE(max_framerate_, kMaxFramerateFps); | |
| 524 | 551 |
| 525 bool success = video_sender_.RegisterSendCodec( | 552 bool success = video_sender_.RegisterSendCodec( |
| 526 &codec, number_of_cores_, | 553 &codec, number_of_cores_, |
| 527 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; | 554 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; |
| 528 if (!success) { | 555 if (!success) { |
| 529 LOG(LS_ERROR) << "Failed to configure encoder."; | 556 LOG(LS_ERROR) << "Failed to configure encoder."; |
| 530 rate_allocator_.reset(); | 557 rate_allocator_.reset(); |
| 531 } | 558 } |
| 532 | 559 |
| 533 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), | 560 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), |
| 534 bitrate_observer_); | 561 bitrate_observer_); |
| 535 | 562 |
| 536 int framerate = stats_proxy_->GetSendFrameRate(); | 563 // Get the current actual framerate, as measured by the stats proxy. This is |
| 537 if (framerate == 0) | 564 // used to get the correct bitrate layer allocation. |
| 538 framerate = codec.maxFramerate; | 565 int current_framerate = stats_proxy_->GetSendFrameRate(); |
| 566 if (current_framerate == 0) | |
| 567 current_framerate = codec.maxFramerate; | |
| 539 stats_proxy_->OnEncoderReconfigured( | 568 stats_proxy_->OnEncoderReconfigured( |
| 540 encoder_config_, rate_allocator_.get() | 569 encoder_config_, |
| 541 ? rate_allocator_->GetPreferredBitrateBps(framerate) | 570 rate_allocator_.get() |
| 542 : codec.maxBitrate); | 571 ? rate_allocator_->GetPreferredBitrateBps(current_framerate) |
| 572 : codec.maxBitrate); | |
| 543 | 573 |
| 544 pending_encoder_reconfiguration_ = false; | 574 pending_encoder_reconfiguration_ = false; |
| 545 | 575 |
| 546 sink_->OnEncoderConfigurationChanged( | 576 sink_->OnEncoderConfigurationChanged( |
| 547 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 577 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
| 548 | 578 |
| 579 // Get the current target framerate, ie the maximum framerate as specified by | |
| 580 // the current codec configuration, or any limit imposed by cpu adaption in | |
| 581 // maintain-resolution or balanced mode. This is used to make sure overuse | |
| 582 // detection doesn't needlessly trigger in low and/or variable framerate | |
| 583 // scenarios. | |
| 584 int target_framerate = max_framerate_; | |
| 585 int current_fps_want = source_proxy_->GetActiveSinkWants().max_framerate_fps; | |
| 586 if (current_fps_want != std::numeric_limits<int>::max()) { | |
|
åsapersson
2017/06/14 14:46:22
remove if statement above?
sprang_webrtc
2017/06/14 15:29:56
Done.
| |
| 587 target_framerate = std::min(target_framerate, current_fps_want); | |
| 588 } | |
| 589 overuse_detector_->OnTargetFramerateUpdated(target_framerate); | |
| 590 | |
| 549 ConfigureQualityScaler(); | 591 ConfigureQualityScaler(); |
| 550 } | 592 } |
| 551 | 593 |
| 552 void ViEEncoder::ConfigureQualityScaler() { | 594 void ViEEncoder::ConfigureQualityScaler() { |
| 553 RTC_DCHECK_RUN_ON(&encoder_queue_); | 595 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 554 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 596 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
| 555 const bool quality_scaling_allowed = | 597 const bool quality_scaling_allowed = |
| 556 IsResolutionScalingEnabled(degradation_preference_) && | 598 IsResolutionScalingEnabled(degradation_preference_) && |
| 557 scaling_settings.enabled; | 599 scaling_settings.enabled; |
| 558 | 600 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 687 | 729 |
| 688 if (EncoderPaused()) { | 730 if (EncoderPaused()) { |
| 689 TraceFrameDropStart(); | 731 TraceFrameDropStart(); |
| 690 return; | 732 return; |
| 691 } | 733 } |
| 692 TraceFrameDropEnd(); | 734 TraceFrameDropEnd(); |
| 693 | 735 |
| 694 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 736 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
| 695 "Encode"); | 737 "Encode"); |
| 696 | 738 |
| 697 overuse_detector_.FrameCaptured(video_frame, time_when_posted_us); | 739 overuse_detector_->FrameCaptured(video_frame, time_when_posted_us); |
| 698 | 740 |
| 699 video_sender_.AddVideoFrame(video_frame, nullptr); | 741 video_sender_.AddVideoFrame(video_frame, nullptr); |
| 700 } | 742 } |
| 701 | 743 |
| 702 void ViEEncoder::SendKeyFrame() { | 744 void ViEEncoder::SendKeyFrame() { |
| 703 if (!encoder_queue_.IsCurrent()) { | 745 if (!encoder_queue_.IsCurrent()) { |
| 704 encoder_queue_.PostTask([this] { SendKeyFrame(); }); | 746 encoder_queue_.PostTask([this] { SendKeyFrame(); }); |
| 705 return; | 747 return; |
| 706 } | 748 } |
| 707 RTC_DCHECK_RUN_ON(&encoder_queue_); | 749 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 718 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 760 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
| 719 | 761 |
| 720 EncodedImageCallback::Result result = | 762 EncodedImageCallback::Result result = |
| 721 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 763 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
| 722 | 764 |
| 723 int64_t time_sent_us = rtc::TimeMicros(); | 765 int64_t time_sent_us = rtc::TimeMicros(); |
| 724 uint32_t timestamp = encoded_image._timeStamp; | 766 uint32_t timestamp = encoded_image._timeStamp; |
| 725 const int qp = encoded_image.qp_; | 767 const int qp = encoded_image.qp_; |
| 726 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { | 768 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { |
| 727 RTC_DCHECK_RUN_ON(&encoder_queue_); | 769 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 728 overuse_detector_.FrameSent(timestamp, time_sent_us); | 770 overuse_detector_->FrameSent(timestamp, time_sent_us); |
| 729 if (quality_scaler_ && qp >= 0) | 771 if (quality_scaler_ && qp >= 0) |
| 730 quality_scaler_->ReportQP(qp); | 772 quality_scaler_->ReportQP(qp); |
| 731 }); | 773 }); |
| 732 | 774 |
| 733 return result; | 775 return result; |
| 734 } | 776 } |
| 735 | 777 |
| 736 void ViEEncoder::OnDroppedFrame() { | 778 void ViEEncoder::OnDroppedFrame() { |
| 737 encoder_queue_.PostTask([this] { | 779 encoder_queue_.PostTask([this] { |
| 738 RTC_DCHECK_RUN_ON(&encoder_queue_); | 780 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 844 case VideoSendStream::DegradationPreference::kBalanced: | 886 case VideoSendStream::DegradationPreference::kBalanced: |
| 845 FALLTHROUGH(); | 887 FALLTHROUGH(); |
| 846 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 888 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 847 // Scale down resolution. | 889 // Scale down resolution. |
| 848 if (!source_proxy_->RequestResolutionLowerThan( | 890 if (!source_proxy_->RequestResolutionLowerThan( |
| 849 adaptation_request.input_pixel_count_)) { | 891 adaptation_request.input_pixel_count_)) { |
| 850 return; | 892 return; |
| 851 } | 893 } |
| 852 GetAdaptCounter().IncrementResolution(reason, 1); | 894 GetAdaptCounter().IncrementResolution(reason, 1); |
| 853 break; | 895 break; |
| 854 case VideoSendStream::DegradationPreference::kMaintainResolution: | 896 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
| 855 // Scale down framerate. | 897 // Scale down framerate. |
| 856 if (!source_proxy_->RequestFramerateLowerThan( | 898 const int requested_framerate = source_proxy_->RequestFramerateLowerThan( |
| 857 adaptation_request.framerate_fps_)) { | 899 adaptation_request.framerate_fps_); |
| 900 if (requested_framerate == -1) | |
| 858 return; | 901 return; |
| 859 } | 902 overuse_detector_->OnTargetFramerateUpdated( |
| 903 std::min(max_framerate_, requested_framerate)); | |
|
åsapersson
2017/06/14 14:46:22
can max_framerate_ be -1 here?
sprang_webrtc
2017/06/14 15:29:56
Only if AdaptDown() can be called before Reconfigu
| |
| 860 GetAdaptCounter().IncrementFramerate(reason, 1); | 904 GetAdaptCounter().IncrementFramerate(reason, 1); |
| 861 break; | 905 break; |
| 906 } | |
| 862 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 907 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 863 RTC_NOTREACHED(); | 908 RTC_NOTREACHED(); |
| 864 } | 909 } |
| 865 | 910 |
| 866 last_adaptation_request_.emplace(adaptation_request); | 911 last_adaptation_request_.emplace(adaptation_request); |
| 867 | 912 |
| 868 UpdateAdaptationStats(reason); | 913 UpdateAdaptationStats(reason); |
| 869 | 914 |
| 870 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); | 915 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); |
| 871 } | 916 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 923 GetAdaptCounter().IncrementResolution(reason, -1); | 968 GetAdaptCounter().IncrementResolution(reason, -1); |
| 924 break; | 969 break; |
| 925 } | 970 } |
| 926 case VideoSendStream::DegradationPreference::kMaintainResolution: { | 971 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
| 927 // Scale up framerate. | 972 // Scale up framerate. |
| 928 int fps = adaptation_request.framerate_fps_; | 973 int fps = adaptation_request.framerate_fps_; |
| 929 if (adapt_counter.FramerateCount() == 1) { | 974 if (adapt_counter.FramerateCount() == 1) { |
| 930 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 975 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
| 931 fps = std::numeric_limits<int>::max(); | 976 fps = std::numeric_limits<int>::max(); |
| 932 } | 977 } |
| 933 if (!source_proxy_->RequestHigherFramerateThan(fps)) | 978 |
| 979 const int requested_framerate = | |
| 980 source_proxy_->RequestHigherFramerateThan(fps); | |
| 981 if (requested_framerate == -1) { | |
| 982 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); | |
| 934 return; | 983 return; |
| 984 } | |
| 985 overuse_detector_->OnTargetFramerateUpdated( | |
| 986 std::min(max_framerate_, requested_framerate)); | |
| 935 GetAdaptCounter().IncrementFramerate(reason, -1); | 987 GetAdaptCounter().IncrementFramerate(reason, -1); |
| 936 break; | 988 break; |
| 937 } | 989 } |
| 938 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 990 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 939 RTC_NOTREACHED(); | 991 RTC_NOTREACHED(); |
| 940 } | 992 } |
| 941 | 993 |
| 942 last_adaptation_request_.emplace(adaptation_request); | 994 last_adaptation_request_.emplace(adaptation_request); |
| 943 | 995 |
| 944 UpdateAdaptationStats(reason); | 996 UpdateAdaptationStats(reason); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1051 std::string ViEEncoder::AdaptCounter::ToString( | 1103 std::string ViEEncoder::AdaptCounter::ToString( |
| 1052 const std::vector<int>& counters) const { | 1104 const std::vector<int>& counters) const { |
| 1053 std::stringstream ss; | 1105 std::stringstream ss; |
| 1054 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { | 1106 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
| 1055 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; | 1107 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
| 1056 } | 1108 } |
| 1057 return ss.str(); | 1109 return ss.str(); |
| 1058 } | 1110 } |
| 1059 | 1111 |
| 1060 } // namespace webrtc | 1112 } // namespace webrtc |
| OLD | NEW |