Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: webrtc/video/vie_encoder.cc

Issue 2630333002: Drop frames until specified bitrate is achieved. (Closed)
Patch Set: Only drop frames at beginning of call Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/video/vie_encoder.h ('k') | webrtc/video/vie_encoder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
45 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle 49 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle
46 // pipelining encoders better (multiple input frames before something comes 50 // pipelining encoders better (multiple input frames before something comes
47 // out). This should effectively turn off CPU adaptations for systems that 51 // out). This should effectively turn off CPU adaptations for systems that
48 // remotely cope with the load right now. 52 // remotely cope with the load right now.
49 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) { 53 CpuOveruseOptions GetCpuOveruseOptions(bool full_overuse_time) {
50 CpuOveruseOptions options; 54 CpuOveruseOptions options;
51 if (full_overuse_time) { 55 if (full_overuse_time) {
52 options.low_encode_usage_threshold_percent = 150; 56 options.low_encode_usage_threshold_percent = 150;
53 options.high_encode_usage_threshold_percent = 200; 57 options.high_encode_usage_threshold_percent = 200;
54 } 58 }
55 return options; 59 return options;
56 } 60 }
57 61
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
58 } // namespace 73 } // namespace
59 74
60 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { 75 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask {
61 public: 76 public:
62 ConfigureEncoderTask(ViEEncoder* vie_encoder, 77 ConfigureEncoderTask(ViEEncoder* vie_encoder,
63 VideoEncoderConfig config, 78 VideoEncoderConfig config,
64 size_t max_data_payload_length, 79 size_t max_data_payload_length,
65 bool nack_enabled) 80 bool nack_enabled)
66 : vie_encoder_(vie_encoder), 81 : vie_encoder_(vie_encoder),
67 config_(std::move(config)), 82 config_(std::move(config)),
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); 252 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy);
238 }; 253 };
239 254
240 ViEEncoder::ViEEncoder(uint32_t number_of_cores, 255 ViEEncoder::ViEEncoder(uint32_t number_of_cores,
241 SendStatisticsProxy* stats_proxy, 256 SendStatisticsProxy* stats_proxy,
242 const VideoSendStream::Config::EncoderSettings& settings, 257 const VideoSendStream::Config::EncoderSettings& settings,
243 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, 258 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
244 EncodedFrameObserver* encoder_timing) 259 EncodedFrameObserver* encoder_timing)
245 : shutdown_event_(true /* manual_reset */, false), 260 : shutdown_event_(true /* manual_reset */, false),
246 number_of_cores_(number_of_cores), 261 number_of_cores_(number_of_cores),
262 initial_rampup_(0),
247 source_proxy_(new VideoSourceProxy(this)), 263 source_proxy_(new VideoSourceProxy(this)),
248 sink_(nullptr), 264 sink_(nullptr),
249 settings_(settings), 265 settings_(settings),
250 codec_type_(PayloadNameToCodecType(settings.payload_name) 266 codec_type_(PayloadNameToCodecType(settings.payload_name)
251 .value_or(VideoCodecType::kVideoCodecUnknown)), 267 .value_or(VideoCodecType::kVideoCodecUnknown)),
252 video_sender_(Clock::GetRealTimeClock(), this, this), 268 video_sender_(Clock::GetRealTimeClock(), this, this),
253 overuse_detector_(Clock::GetRealTimeClock(), 269 overuse_detector_(Clock::GetRealTimeClock(),
254 GetCpuOveruseOptions(settings.full_overuse_time), 270 GetCpuOveruseOptions(settings.full_overuse_time),
255 this, 271 this,
256 encoder_timing, 272 encoder_timing,
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 }); 348 });
333 } 349 }
334 350
335 void ViEEncoder::SetSource( 351 void ViEEncoder::SetSource(
336 rtc::VideoSourceInterface<VideoFrame>* source, 352 rtc::VideoSourceInterface<VideoFrame>* source,
337 const VideoSendStream::DegradationPreference& degradation_preference) { 353 const VideoSendStream::DegradationPreference& degradation_preference) {
338 RTC_DCHECK_RUN_ON(&thread_checker_); 354 RTC_DCHECK_RUN_ON(&thread_checker_);
339 source_proxy_->SetSource(source, degradation_preference); 355 source_proxy_->SetSource(source, degradation_preference);
340 encoder_queue_.PostTask([this, degradation_preference] { 356 encoder_queue_.PostTask([this, degradation_preference] {
341 RTC_DCHECK_RUN_ON(&encoder_queue_); 357 RTC_DCHECK_RUN_ON(&encoder_queue_);
342 scaling_enabled_ = (degradation_preference != 358 scaling_enabled_ = (degradation_preference !=
perkj_webrtc 2017/01/24 12:58:23 Changing the source change scaling_enabled_ but do
kthelgason 2017/01/25 10:01:58 Acknowledged.
343 VideoSendStream::DegradationPreference::kMaintainResolution); 359 VideoSendStream::DegradationPreference::kMaintainResolution);
344 stats_proxy_->SetResolutionRestrictionStats( 360 stats_proxy_->SetResolutionRestrictionStats(
345 scaling_enabled_, scale_counter_[kCpu] > 0, scale_counter_[kQuality]); 361 scaling_enabled_, scale_counter_[kCpu] > 0, scale_counter_[kQuality]);
346 }); 362 });
347 } 363 }
348 364
349 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { 365 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) {
350 source_proxy_->SetWantsRotationApplied(rotation_applied); 366 source_proxy_->SetWantsRotationApplied(rotation_applied);
351 encoder_queue_.PostTask([this, sink] { 367 encoder_queue_.PostTask([this, sink] {
352 RTC_DCHECK_RUN_ON(&encoder_queue_); 368 RTC_DCHECK_RUN_ON(&encoder_queue_);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate)); 448 encoder_config_, rate_allocator_->GetPreferredBitrateBps(framerate));
433 } 449 }
434 450
435 pending_encoder_reconfiguration_ = false; 451 pending_encoder_reconfiguration_ = false;
436 452
437 sink_->OnEncoderConfigurationChanged( 453 sink_->OnEncoderConfigurationChanged(
438 std::move(streams), encoder_config_.min_transmit_bitrate_bps); 454 std::move(streams), encoder_config_.min_transmit_bitrate_bps);
439 455
440 const auto scaling_settings = settings_.encoder->GetScalingSettings(); 456 const auto scaling_settings = settings_.encoder->GetScalingSettings();
441 if (scaling_enabled_ && scaling_settings.enabled) { 457 if (scaling_enabled_ && scaling_settings.enabled) {
458 // Drop frames and scale down until desired quality is achieved.
442 if (scaling_settings.thresholds) { 459 if (scaling_settings.thresholds) {
443 quality_scaler_.reset( 460 quality_scaler_.reset(
perkj_webrtc 2017/01/24 12:58:22 quality scaler is only created when the codec is r
kthelgason 2017/01/25 10:01:58 Acknowledged.
444 new QualityScaler(this, *(scaling_settings.thresholds))); 461 new QualityScaler(this, *(scaling_settings.thresholds)));
445 } else { 462 } else {
446 quality_scaler_.reset(new QualityScaler(this, codec_type_)); 463 quality_scaler_.reset(new QualityScaler(this, codec_type_));
447 } 464 }
448 } else { 465 } else {
466 initial_rampup_ = kMaxInitialFramedrop;
perkj_webrtc 2017/01/24 12:58:23 This will not necessarily be called when scaling_e
kthelgason 2017/01/25 10:01:58 Acknowledged.
449 quality_scaler_.reset(nullptr); 467 quality_scaler_.reset(nullptr);
450 stats_proxy_->SetResolutionRestrictionStats( 468 stats_proxy_->SetResolutionRestrictionStats(
451 false, scale_counter_[kCpu] > 0, scale_counter_[kQuality]); 469 false, scale_counter_[kCpu] > 0, scale_counter_[kQuality]);
452 } 470 }
453 } 471 }
454 472
455 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { 473 void ViEEncoder::OnFrame(const VideoFrame& video_frame) {
456 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); 474 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_);
457 VideoFrame incoming_frame = video_frame; 475 VideoFrame incoming_frame = video_frame;
458 476
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 pending_encoder_reconfiguration_ = true; 556 pending_encoder_reconfiguration_ = true;
539 last_frame_info_ = rtc::Optional<VideoFrameInfo>( 557 last_frame_info_ = rtc::Optional<VideoFrameInfo>(
540 VideoFrameInfo(video_frame.width(), video_frame.height(), 558 VideoFrameInfo(video_frame.width(), video_frame.height(),
541 video_frame.rotation(), video_frame.is_texture())); 559 video_frame.rotation(), video_frame.is_texture()));
542 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" 560 LOG(LS_INFO) << "Video frame parameters changed: dimensions="
543 << last_frame_info_->width << "x" << last_frame_info_->height 561 << last_frame_info_->width << "x" << last_frame_info_->height
544 << ", rotation=" << last_frame_info_->rotation 562 << ", rotation=" << last_frame_info_->rotation
545 << ", texture=" << last_frame_info_->is_texture; 563 << ", texture=" << last_frame_info_->is_texture;
546 } 564 }
547 565
566 uint32_t max_pixel_count =
567 MaximumFrameSizeForBitrate(encoder_start_bitrate_bps_ / 1000);
568 if (initial_rampup_ < kMaxInitialFramedrop &&
perkj_webrtc 2017/01/24 12:58:22 I guess you will also need to check if (scaling_en
kthelgason 2017/01/25 10:01:58 Yes, this sucks.
569 video_frame.size() > max_pixel_count) {
perkj_webrtc 2017/01/24 12:58:23 nit: prefer video_frame_size() > MaximumFrameSizeF
kthelgason 2017/01/25 10:01:58 Done.
570 LOG(LS_INFO) << "Dropping frame. Too large for target bitrate.";
571 ScaleDown(kQuality);
572 ++initial_rampup_;
573 return;
574 }
575 initial_rampup_ = kMaxInitialFramedrop;
576
577
perkj_webrtc 2017/01/24 12:58:23 nit: remove extra empty line.
kthelgason 2017/01/25 10:01:58 Done.
548 int64_t now_ms = clock_->TimeInMilliseconds(); 578 int64_t now_ms = clock_->TimeInMilliseconds();
549 if (pending_encoder_reconfiguration_) { 579 if (pending_encoder_reconfiguration_) {
550 ReconfigureEncoder(); 580 ReconfigureEncoder();
551 } else if (!last_parameters_update_ms_ || 581 } else if (!last_parameters_update_ms_ ||
552 now_ms - *last_parameters_update_ms_ >= 582 now_ms - *last_parameters_update_ms_ >=
553 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) { 583 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) {
554 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), 584 video_sender_.UpdateChannelParemeters(rate_allocator_.get(),
555 bitrate_observer_); 585 bitrate_observer_);
556 } 586 }
557 last_parameters_update_ms_.emplace(now_ms); 587 last_parameters_update_ms_.emplace(now_ms);
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 724
695 if (stats_proxy_ && video_suspension_changed) { 725 if (stats_proxy_ && video_suspension_changed) {
696 LOG(LS_INFO) << "Video suspend state changed to: " 726 LOG(LS_INFO) << "Video suspend state changed to: "
697 << (video_is_suspended ? "suspended" : "not suspended"); 727 << (video_is_suspended ? "suspended" : "not suspended");
698 stats_proxy_->OnSuspendChange(video_is_suspended); 728 stats_proxy_->OnSuspendChange(video_is_suspended);
699 } 729 }
700 } 730 }
701 731
702 void ViEEncoder::ScaleDown(ScaleReason reason) { 732 void ViEEncoder::ScaleDown(ScaleReason reason) {
703 RTC_DCHECK_RUN_ON(&encoder_queue_); 733 RTC_DCHECK_RUN_ON(&encoder_queue_);
704 if (!scaling_enabled_) 734 if (!scaling_enabled_)
perkj_webrtc 2017/01/24 12:58:23 so if the source change scaling_enabled_ can chang
kthelgason 2017/01/25 10:01:58 This has now been changed such that the quality sc
705 return; 735 return;
706 // Request lower resolution if the current resolution is lower than last time 736 // Request lower resolution if the current resolution is lower than last time
707 // we asked for the resolution to be lowered. 737 // we asked for the resolution to be lowered.
708 int current_pixel_count = 738 int current_pixel_count =
709 last_frame_info_ ? last_frame_info_->pixel_count() : 0; 739 last_frame_info_ ? last_frame_info_->pixel_count() : 0;
710 if (max_pixel_count_ && current_pixel_count >= *max_pixel_count_) 740 if (max_pixel_count_ && current_pixel_count >= *max_pixel_count_)
711 return; 741 return;
712 switch (reason) { 742 switch (reason) {
713 case kQuality: 743 case kQuality:
714 stats_proxy_->OnQualityRestrictedResolutionChanged( 744 stats_proxy_->OnQualityRestrictedResolutionChanged(
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 --scale_counter_[reason]; 788 --scale_counter_[reason];
759 source_proxy_->RequestHigherResolutionThan(current_pixel_count); 789 source_proxy_->RequestHigherResolutionThan(current_pixel_count);
760 LOG(LS_INFO) << "Scaling up resolution."; 790 LOG(LS_INFO) << "Scaling up resolution.";
761 for (size_t i = 0; i < kScaleReasonSize; ++i) { 791 for (size_t i = 0; i < kScaleReasonSize; ++i) {
762 LOG(LS_INFO) << "Scaled " << scale_counter_[i] 792 LOG(LS_INFO) << "Scaled " << scale_counter_[i]
763 << " times for reason: " << (i ? "cpu" : "quality"); 793 << " times for reason: " << (i ? "cpu" : "quality");
764 } 794 }
765 } 795 }
766 796
767 } // namespace webrtc 797 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/vie_encoder.h ('k') | webrtc/video/vie_encoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698