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 const int64_t kFrameLogIntervalMs = 60000; | 37 const int64_t kFrameLogIntervalMs = 60000; |
38 // We will never ask for a resolution lower than this. | 38 // We will never ask for a resolution lower than this. |
39 #if defined(WEBRTC_ANDROID) | 39 #if defined(WEBRTC_ANDROID) |
40 // TODO(kthelgason): Lower this limit when better testing | 40 // TODO(kthelgason): Lower this limit when better testing |
41 // on MediaCodec and fallback implementations are in place. | 41 // on MediaCodec and fallback implementations are in place. |
42 const int kMinPixelsPerFrame = 320 * 180; | 42 const int kMinPixelsPerFrame = 320 * 180; |
43 #else | 43 #else |
44 const int kMinPixelsPerFrame = 120 * 90; | 44 const int kMinPixelsPerFrame = 120 * 90; |
45 #endif | 45 #endif |
46 | 46 |
| 47 // The maximum number of frames to drop at beginning of stream |
| 48 // to try and achieve desired bitrate. |
| 49 const int kMaxInitialFramedrop = 4; |
| 50 |
47 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 51 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
48 // pipelining encoders better (multiple input frames before something comes | 52 // pipelining encoders better (multiple input frames before something comes |
49 // out). This should effectively turn off CPU adaptations for systems that | 53 // out). This should effectively turn off CPU adaptations for systems that |
50 // remotely cope with the load right now. | 54 // remotely cope with the load right now. |
51 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { | 55 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { |
52 CpuOveruseOptions options; | 56 CpuOveruseOptions options; |
53 if (full_overuse_time) { | 57 if (full_overuse_time) { |
54 options.low_encode_usage_threshold_percent = 150; | 58 options.low_encode_usage_threshold_percent = 150; |
55 options.high_encode_usage_threshold_percent = 200; | 59 options.high_encode_usage_threshold_percent = 200; |
56 } | 60 } |
57 return options; | 61 return options; |
58 } | 62 } |
59 | 63 |
| 64 uint32_t MaximumFrameSizeForBitrate(uint32_t kbps) { |
| 65 if (kbps > 0) { |
| 66 if (kbps < 300 /* qvga */) { |
| 67 return 320 * 240; |
| 68 } else if (kbps < 500 /* vga */) { |
| 69 return 640 * 480; |
| 70 } |
| 71 } |
| 72 return std::numeric_limits<uint32_t>::max(); |
| 73 } |
| 74 |
60 } // namespace | 75 } // namespace |
61 | 76 |
62 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { | 77 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { |
63 public: | 78 public: |
64 ConfigureEncoderTask(ViEEncoder* vie_encoder, | 79 ConfigureEncoderTask(ViEEncoder* vie_encoder, |
65 VideoEncoderConfig config, | 80 VideoEncoderConfig config, |
66 size_t max_data_payload_length, | 81 size_t max_data_payload_length, |
67 bool nack_enabled) | 82 bool nack_enabled) |
68 : vie_encoder_(vie_encoder), | 83 : vie_encoder_(vie_encoder), |
69 config_(std::move(config)), | 84 config_(std::move(config)), |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 250 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
236 }; | 251 }; |
237 | 252 |
238 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 253 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
239 SendStatisticsProxy* stats_proxy, | 254 SendStatisticsProxy* stats_proxy, |
240 const VideoSendStream::Config::EncoderSettings& settings, | 255 const VideoSendStream::Config::EncoderSettings& settings, |
241 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 256 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
242 EncodedFrameObserver* encoder_timing) | 257 EncodedFrameObserver* encoder_timing) |
243 : shutdown_event_(true /* manual_reset */, false), | 258 : shutdown_event_(true /* manual_reset */, false), |
244 number_of_cores_(number_of_cores), | 259 number_of_cores_(number_of_cores), |
| 260 initial_rampup_(0), |
245 source_proxy_(new VideoSourceProxy(this)), | 261 source_proxy_(new VideoSourceProxy(this)), |
246 sink_(nullptr), | 262 sink_(nullptr), |
247 settings_(settings), | 263 settings_(settings), |
248 codec_type_(PayloadNameToCodecType(settings.payload_name) | 264 codec_type_(PayloadNameToCodecType(settings.payload_name) |
249 .value_or(VideoCodecType::kVideoCodecUnknown)), | 265 .value_or(VideoCodecType::kVideoCodecUnknown)), |
250 video_sender_(Clock::GetRealTimeClock(), this, this), | 266 video_sender_(Clock::GetRealTimeClock(), this, this), |
251 overuse_detector_(GetCpuOveruseOptions(settings.full_overuse_time), | 267 overuse_detector_(GetCpuOveruseOptions(settings.full_overuse_time), |
252 this, | 268 this, |
253 encoder_timing, | 269 encoder_timing, |
254 stats_proxy), | 270 stats_proxy), |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 348 |
333 void ViEEncoder::SetSource( | 349 void ViEEncoder::SetSource( |
334 rtc::VideoSourceInterface<VideoFrame>* source, | 350 rtc::VideoSourceInterface<VideoFrame>* source, |
335 const VideoSendStream::DegradationPreference& degradation_preference) { | 351 const VideoSendStream::DegradationPreference& degradation_preference) { |
336 RTC_DCHECK_RUN_ON(&thread_checker_); | 352 RTC_DCHECK_RUN_ON(&thread_checker_); |
337 source_proxy_->SetSource(source, degradation_preference); | 353 source_proxy_->SetSource(source, degradation_preference); |
338 encoder_queue_.PostTask([this, degradation_preference] { | 354 encoder_queue_.PostTask([this, degradation_preference] { |
339 RTC_DCHECK_RUN_ON(&encoder_queue_); | 355 RTC_DCHECK_RUN_ON(&encoder_queue_); |
340 | 356 |
341 degradation_preference_ = degradation_preference; | 357 degradation_preference_ = degradation_preference; |
342 stats_proxy_->SetResolutionRestrictionStats( | 358 initial_rampup_ = |
343 degradation_preference != | 359 degradation_preference_ != DegradationPreference::kMaintainResolution |
344 VideoSendStream::DegradationPreference::kMaintainResolution, | 360 ? 0 |
345 scale_counter_[kCpu] > 0, scale_counter_[kQuality]); | 361 : kMaxInitialFramedrop; |
| 362 ConfigureQualityScaler(); |
346 }); | 363 }); |
347 } | 364 } |
348 | 365 |
349 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 366 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
350 source_proxy_->SetWantsRotationApplied(rotation_applied); | 367 source_proxy_->SetWantsRotationApplied(rotation_applied); |
351 encoder_queue_.PostTask([this, sink] { | 368 encoder_queue_.PostTask([this, sink] { |
352 RTC_DCHECK_RUN_ON(&encoder_queue_); | 369 RTC_DCHECK_RUN_ON(&encoder_queue_); |
353 sink_ = sink; | 370 sink_ = sink; |
354 }); | 371 }); |
355 } | 372 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 framerate = codec.maxFramerate; | 447 framerate = codec.maxFramerate; |
431 stats_proxy_->OnEncoderReconfigured( | 448 stats_proxy_->OnEncoderReconfigured( |
432 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); | 449 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); |
433 } | 450 } |
434 | 451 |
435 pending_encoder_reconfiguration_ = false; | 452 pending_encoder_reconfiguration_ = false; |
436 | 453 |
437 sink_->OnEncoderConfigurationChanged( | 454 sink_->OnEncoderConfigurationChanged( |
438 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 455 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
439 | 456 |
| 457 ConfigureQualityScaler(); |
| 458 } |
| 459 |
| 460 void ViEEncoder::ConfigureQualityScaler() { |
| 461 RTC_DCHECK_RUN_ON(&encoder_queue_); |
440 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 462 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
441 if (degradation_preference_ != DegradationPreference::kMaintainResolution && | 463 const bool degradation_preference_allows_scaling = |
442 scaling_settings.enabled) { | 464 degradation_preference_ != DegradationPreference::kMaintainResolution; |
| 465 if (degradation_preference_allows_scaling && scaling_settings.enabled) { |
| 466 // Drop frames and scale down until desired quality is achieved. |
443 if (scaling_settings.thresholds) { | 467 if (scaling_settings.thresholds) { |
444 quality_scaler_.reset( | 468 quality_scaler_.reset( |
445 new QualityScaler(this, *(scaling_settings.thresholds))); | 469 new QualityScaler(this, *(scaling_settings.thresholds))); |
446 } else { | 470 } else { |
447 quality_scaler_.reset(new QualityScaler(this, codec_type_)); | 471 quality_scaler_.reset(new QualityScaler(this, codec_type_)); |
448 } | 472 } |
449 } else { | 473 } else { |
450 quality_scaler_.reset(nullptr); | 474 quality_scaler_.reset(nullptr); |
451 stats_proxy_->SetResolutionRestrictionStats( | |
452 false, scale_counter_[kCpu] > 0, scale_counter_[kQuality]); | |
453 } | 475 } |
| 476 stats_proxy_->SetResolutionRestrictionStats( |
| 477 degradation_preference_allows_scaling, scale_counter_[kCpu] > 0, |
| 478 scale_counter_[kQuality]); |
454 } | 479 } |
455 | 480 |
456 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 481 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { |
457 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 482 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
458 VideoFrame incoming_frame = video_frame; | 483 VideoFrame incoming_frame = video_frame; |
459 | 484 |
460 // Local time in webrtc time base. | 485 // Local time in webrtc time base. |
461 int64_t current_time_us = clock_->TimeInMicroseconds(); | 486 int64_t current_time_us = clock_->TimeInMicroseconds(); |
462 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; | 487 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; |
463 // TODO(nisse): This always overrides the incoming timestamp. Don't | 488 // TODO(nisse): This always overrides the incoming timestamp. Don't |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 pending_encoder_reconfiguration_ = true; | 567 pending_encoder_reconfiguration_ = true; |
543 last_frame_info_ = rtc::Optional<VideoFrameInfo>( | 568 last_frame_info_ = rtc::Optional<VideoFrameInfo>( |
544 VideoFrameInfo(video_frame.width(), video_frame.height(), | 569 VideoFrameInfo(video_frame.width(), video_frame.height(), |
545 video_frame.rotation(), video_frame.is_texture())); | 570 video_frame.rotation(), video_frame.is_texture())); |
546 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" | 571 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" |
547 << last_frame_info_->width << "x" << last_frame_info_->height | 572 << last_frame_info_->width << "x" << last_frame_info_->height |
548 << ", rotation=" << last_frame_info_->rotation | 573 << ", rotation=" << last_frame_info_->rotation |
549 << ", texture=" << last_frame_info_->is_texture; | 574 << ", texture=" << last_frame_info_->is_texture; |
550 } | 575 } |
551 | 576 |
| 577 if (initial_rampup_ < kMaxInitialFramedrop && |
| 578 video_frame.size() > |
| 579 MaximumFrameSizeForBitrate(encoder_start_bitrate_bps_ / 1000)) { |
| 580 LOG(LS_INFO) << "Dropping frame. Too large for target bitrate."; |
| 581 AdaptDown(kQuality); |
| 582 ++initial_rampup_; |
| 583 return; |
| 584 } |
| 585 initial_rampup_ = kMaxInitialFramedrop; |
| 586 |
552 int64_t now_ms = clock_->TimeInMilliseconds(); | 587 int64_t now_ms = clock_->TimeInMilliseconds(); |
553 if (pending_encoder_reconfiguration_) { | 588 if (pending_encoder_reconfiguration_) { |
554 ReconfigureEncoder(); | 589 ReconfigureEncoder(); |
555 } else if (!last_parameters_update_ms_ || | 590 } else if (!last_parameters_update_ms_ || |
556 now_ms - *last_parameters_update_ms_ >= | 591 now_ms - *last_parameters_update_ms_ >= |
557 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) { | 592 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) { |
558 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), | 593 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), |
559 bitrate_observer_); | 594 bitrate_observer_); |
560 } | 595 } |
561 last_parameters_update_ms_.emplace(now_ms); | 596 last_parameters_update_ms_.emplace(now_ms); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 --scale_counter_[reason]; | 799 --scale_counter_[reason]; |
765 source_proxy_->RequestHigherResolutionThan(current_pixel_count); | 800 source_proxy_->RequestHigherResolutionThan(current_pixel_count); |
766 LOG(LS_INFO) << "Scaling up resolution."; | 801 LOG(LS_INFO) << "Scaling up resolution."; |
767 for (size_t i = 0; i < kScaleReasonSize; ++i) { | 802 for (size_t i = 0; i < kScaleReasonSize; ++i) { |
768 LOG(LS_INFO) << "Scaled " << scale_counter_[i] | 803 LOG(LS_INFO) << "Scaled " << scale_counter_[i] |
769 << " times for reason: " << (i ? "cpu" : "quality"); | 804 << " times for reason: " << (i ? "cpu" : "quality"); |
770 } | 805 } |
771 } | 806 } |
772 | 807 |
773 } // namespace webrtc | 808 } // namespace webrtc |
OLD | NEW |