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

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

Issue 2630333002: Drop frames until specified bitrate is achieved. (Closed)
Patch Set: Turn off quality scaling for screensharing in tests Created 3 years, 10 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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 !=
343 VideoSendStream::DegradationPreference::kMaintainResolution); 359 VideoSendStream::DegradationPreference::kMaintainResolution);
344 stats_proxy_->SetResolutionRestrictionStats( 360 initial_rampup_ = scaling_enabled_ ? 0 : kMaxInitialFramedrop;
345 scaling_enabled_, scale_counter_[kCpu] > 0, scale_counter_[kQuality]); 361 ConfigureQualityScaler();
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_);
353 sink_ = sink; 369 sink_ = sink;
354 }); 370 });
355 } 371 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
430 framerate = codec.maxFramerate; 446 framerate = codec.maxFramerate;
431 stats_proxy_->OnEncoderReconfigured( 447 stats_proxy_->OnEncoderReconfigured(
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
456 ConfigureQualityScaler();
457 }
458
459 void ViEEncoder::ConfigureQualityScaler() {
460 RTC_DCHECK_RUN_ON(&encoder_queue_);
440 const auto scaling_settings = settings_.encoder->GetScalingSettings(); 461 const auto scaling_settings = settings_.encoder->GetScalingSettings();
441 if (scaling_enabled_ && scaling_settings.enabled) { 462 if (scaling_enabled_ && scaling_settings.enabled) {
463 // Drop frames and scale down until desired quality is achieved.
442 if (scaling_settings.thresholds) { 464 if (scaling_settings.thresholds) {
443 quality_scaler_.reset( 465 quality_scaler_.reset(
444 new QualityScaler(this, *(scaling_settings.thresholds))); 466 new QualityScaler(this, *(scaling_settings.thresholds)));
445 } else { 467 } else {
446 quality_scaler_.reset(new QualityScaler(this, codec_type_)); 468 quality_scaler_.reset(new QualityScaler(this, codec_type_));
447 } 469 }
448 } else { 470 } else {
449 quality_scaler_.reset(nullptr); 471 quality_scaler_.reset(nullptr);
450 stats_proxy_->SetResolutionRestrictionStats(
451 false, scale_counter_[kCpu] > 0, scale_counter_[kQuality]);
452 } 472 }
473 stats_proxy_->SetResolutionRestrictionStats(
474 scaling_enabled_, scale_counter_[kCpu] > 0, scale_counter_[kQuality]);
453 } 475 }
454 476
455 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { 477 void ViEEncoder::OnFrame(const VideoFrame& video_frame) {
456 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); 478 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_);
457 VideoFrame incoming_frame = video_frame; 479 VideoFrame incoming_frame = video_frame;
458 480
459 // Local time in webrtc time base. 481 // Local time in webrtc time base.
460 int64_t current_time = clock_->TimeInMilliseconds(); 482 int64_t current_time = clock_->TimeInMilliseconds();
461 incoming_frame.set_render_time_ms(current_time); 483 incoming_frame.set_render_time_ms(current_time);
462 484
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 pending_encoder_reconfiguration_ = true; 560 pending_encoder_reconfiguration_ = true;
539 last_frame_info_ = rtc::Optional<VideoFrameInfo>( 561 last_frame_info_ = rtc::Optional<VideoFrameInfo>(
540 VideoFrameInfo(video_frame.width(), video_frame.height(), 562 VideoFrameInfo(video_frame.width(), video_frame.height(),
541 video_frame.rotation(), video_frame.is_texture())); 563 video_frame.rotation(), video_frame.is_texture()));
542 LOG(LS_INFO) << "Video frame parameters changed: dimensions=" 564 LOG(LS_INFO) << "Video frame parameters changed: dimensions="
543 << last_frame_info_->width << "x" << last_frame_info_->height 565 << last_frame_info_->width << "x" << last_frame_info_->height
544 << ", rotation=" << last_frame_info_->rotation 566 << ", rotation=" << last_frame_info_->rotation
545 << ", texture=" << last_frame_info_->is_texture; 567 << ", texture=" << last_frame_info_->is_texture;
546 } 568 }
547 569
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
548 int64_t now_ms = clock_->TimeInMilliseconds(); 580 int64_t now_ms = clock_->TimeInMilliseconds();
549 if (pending_encoder_reconfiguration_) { 581 if (pending_encoder_reconfiguration_) {
550 ReconfigureEncoder(); 582 ReconfigureEncoder();
551 } else if (!last_parameters_update_ms_ || 583 } else if (!last_parameters_update_ms_ ||
552 now_ms - *last_parameters_update_ms_ >= 584 now_ms - *last_parameters_update_ms_ >=
553 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) { 585 vcm::VCMProcessTimer::kDefaultProcessIntervalMs) {
554 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), 586 video_sender_.UpdateChannelParemeters(rate_allocator_.get(),
555 bitrate_observer_); 587 bitrate_observer_);
556 } 588 }
557 last_parameters_update_ms_.emplace(now_ms); 589 last_parameters_update_ms_.emplace(now_ms);
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 --scale_counter_[reason]; 790 --scale_counter_[reason];
759 source_proxy_->RequestHigherResolutionThan(current_pixel_count); 791 source_proxy_->RequestHigherResolutionThan(current_pixel_count);
760 LOG(LS_INFO) << "Scaling up resolution."; 792 LOG(LS_INFO) << "Scaling up resolution.";
761 for (size_t i = 0; i < kScaleReasonSize; ++i) { 793 for (size_t i = 0; i < kScaleReasonSize; ++i) {
762 LOG(LS_INFO) << "Scaled " << scale_counter_[i] 794 LOG(LS_INFO) << "Scaled " << scale_counter_[i]
763 << " times for reason: " << (i ? "cpu" : "quality"); 795 << " times for reason: " << (i ? "cpu" : "quality");
764 } 796 }
765 } 797 }
766 798
767 } // namespace webrtc 799 } // 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