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