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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 pixels_wanted >= sink_wants_.max_pixel_count) { | 230 pixels_wanted >= sink_wants_.max_pixel_count) { |
243 return false; | 231 return false; |
244 } | 232 } |
245 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; | 233 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; |
246 sink_wants_.max_pixel_count = pixels_wanted; | 234 sink_wants_.max_pixel_count = pixels_wanted; |
247 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 235 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
248 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 236 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
249 return true; | 237 return true; |
250 } | 238 } |
251 | 239 |
252 bool RequestFramerateLowerThan(int fps) { | 240 int RequestFramerateLowerThan(int fps) { |
253 // Called on the encoder task queue. | 241 // Called on the encoder task queue. |
254 // The input video frame rate will be scaled down to 2/3, rounding down. | 242 // The input video frame rate will be scaled down to 2/3, rounding down. |
255 return RestrictFramerate((fps * 2) / 3); | 243 int framerate_wanted = (fps * 2) / 3; |
244 return RestrictFramerate(framerate_wanted) ? framerate_wanted : -1; | |
256 } | 245 } |
257 | 246 |
258 bool RequestHigherResolutionThan(int pixel_count) { | 247 bool RequestHigherResolutionThan(int pixel_count) { |
259 // Called on the encoder task queue. | 248 // Called on the encoder task queue. |
260 rtc::CritScope lock(&crit_); | 249 rtc::CritScope lock(&crit_); |
261 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { | 250 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
262 // This can happen since |degradation_preference_| is set on libjingle's | 251 // This can happen since |degradation_preference_| is set on libjingle's |
263 // worker thread but the adaptation is done on the encoder task queue. | 252 // worker thread but the adaptation is done on the encoder task queue. |
264 return false; | 253 return false; |
265 } | 254 } |
(...skipping 16 matching lines...) Expand all Loading... | |
282 // not take a too large step up, we cap the requested pixel count to be at | 271 // 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. | 272 // most four time the current number of pixels. |
284 sink_wants_.target_pixel_count = | 273 sink_wants_.target_pixel_count = |
285 rtc::Optional<int>((pixel_count * 5) / 3); | 274 rtc::Optional<int>((pixel_count * 5) / 3); |
286 } | 275 } |
287 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; | 276 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; |
288 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); | 277 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWants()); |
289 return true; | 278 return true; |
290 } | 279 } |
291 | 280 |
292 bool RequestHigherFramerateThan(int fps) { | 281 // Request upgrade in framerate. Returns the new requested frame, or -1 if |
282 // no change requested. Note that maxint may be returned if limits due to | |
283 // adaptation requests are removed completely. In that case, consider | |
284 // |max_framerate_| to be the current limit (assuming the capturer complies). | |
285 int RequestHigherFramerateThan(int fps) { | |
293 // Called on the encoder task queue. | 286 // Called on the encoder task queue. |
294 // The input frame rate will be scaled up to the last step, with rounding. | 287 // The input frame rate will be scaled up to the last step, with rounding. |
295 int framerate_wanted = fps; | 288 int framerate_wanted = fps; |
296 if (fps != std::numeric_limits<int>::max()) | 289 if (fps != std::numeric_limits<int>::max()) |
297 framerate_wanted = (fps * 3) / 2; | 290 framerate_wanted = (fps * 3) / 2; |
298 | 291 |
299 return IncreaseFramerate(framerate_wanted); | 292 return IncreaseFramerate(framerate_wanted) ? framerate_wanted : -1; |
300 } | 293 } |
301 | 294 |
302 bool RestrictFramerate(int fps) { | 295 bool RestrictFramerate(int fps) { |
303 // Called on the encoder task queue. | 296 // Called on the encoder task queue. |
304 rtc::CritScope lock(&crit_); | 297 rtc::CritScope lock(&crit_); |
305 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) | 298 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
306 return false; | 299 return false; |
307 | 300 |
308 const int fps_wanted = std::max(kMinFramerateFps, fps); | 301 const int fps_wanted = std::max(kMinFramerateFps, fps); |
309 if (fps_wanted >= sink_wants_.max_framerate_fps) | 302 if (fps_wanted >= sink_wants_.max_framerate_fps) |
(...skipping 30 matching lines...) Expand all Loading... | |
340 GUARDED_BY(&crit_); | 333 GUARDED_BY(&crit_); |
341 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 334 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
342 | 335 |
343 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 336 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
344 }; | 337 }; |
345 | 338 |
346 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 339 ViEEncoder::ViEEncoder(uint32_t number_of_cores, |
347 SendStatisticsProxy* stats_proxy, | 340 SendStatisticsProxy* stats_proxy, |
348 const VideoSendStream::Config::EncoderSettings& settings, | 341 const VideoSendStream::Config::EncoderSettings& settings, |
349 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 342 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
350 EncodedFrameObserver* encoder_timing) | 343 EncodedFrameObserver* encoder_timing, |
344 std::unique_ptr<OveruseFrameDetector> overuse_detector) | |
351 : shutdown_event_(true /* manual_reset */, false), | 345 : shutdown_event_(true /* manual_reset */, false), |
352 number_of_cores_(number_of_cores), | 346 number_of_cores_(number_of_cores), |
353 initial_rampup_(0), | 347 initial_rampup_(0), |
354 source_proxy_(new VideoSourceProxy(this)), | 348 source_proxy_(new VideoSourceProxy(this)), |
355 sink_(nullptr), | 349 sink_(nullptr), |
356 settings_(settings), | 350 settings_(settings), |
357 codec_type_(PayloadNameToCodecType(settings.payload_name) | 351 codec_type_(PayloadNameToCodecType(settings.payload_name) |
358 .value_or(VideoCodecType::kVideoCodecUnknown)), | 352 .value_or(VideoCodecType::kVideoCodecUnknown)), |
359 video_sender_(Clock::GetRealTimeClock(), this, this), | 353 video_sender_(Clock::GetRealTimeClock(), this, this), |
360 overuse_detector_(GetCpuOveruseOptions(settings.full_overuse_time), | 354 overuse_detector_( |
361 this, | 355 overuse_detector.get() |
362 encoder_timing, | 356 ? overuse_detector.release() |
363 stats_proxy), | 357 : new OveruseFrameDetector( |
358 GetCpuOveruseOptions(settings.full_overuse_time), | |
359 this, | |
360 encoder_timing, | |
361 stats_proxy)), | |
364 stats_proxy_(stats_proxy), | 362 stats_proxy_(stats_proxy), |
365 pre_encode_callback_(pre_encode_callback), | 363 pre_encode_callback_(pre_encode_callback), |
366 module_process_thread_(nullptr), | 364 module_process_thread_(nullptr), |
365 max_framerate_(-1), | |
367 pending_encoder_reconfiguration_(false), | 366 pending_encoder_reconfiguration_(false), |
368 encoder_start_bitrate_bps_(0), | 367 encoder_start_bitrate_bps_(0), |
369 max_data_payload_length_(0), | 368 max_data_payload_length_(0), |
370 nack_enabled_(false), | 369 nack_enabled_(false), |
371 last_observed_bitrate_bps_(0), | 370 last_observed_bitrate_bps_(0), |
372 encoder_paused_and_dropped_frame_(false), | 371 encoder_paused_and_dropped_frame_(false), |
373 clock_(Clock::GetRealTimeClock()), | 372 clock_(Clock::GetRealTimeClock()), |
374 degradation_preference_( | 373 degradation_preference_( |
375 VideoSendStream::DegradationPreference::kDegradationDisabled), | 374 VideoSendStream::DegradationPreference::kDegradationDisabled), |
376 last_captured_timestamp_(0), | 375 last_captured_timestamp_(0), |
377 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - | 376 delta_ntp_internal_ms_(clock_->CurrentNtpInMilliseconds() - |
378 clock_->TimeInMilliseconds()), | 377 clock_->TimeInMilliseconds()), |
379 last_frame_log_ms_(clock_->TimeInMilliseconds()), | 378 last_frame_log_ms_(clock_->TimeInMilliseconds()), |
380 captured_frame_count_(0), | 379 captured_frame_count_(0), |
381 dropped_frame_count_(0), | 380 dropped_frame_count_(0), |
382 bitrate_observer_(nullptr), | 381 bitrate_observer_(nullptr), |
383 encoder_queue_("EncoderQueue") { | 382 encoder_queue_("EncoderQueue") { |
384 RTC_DCHECK(stats_proxy); | 383 RTC_DCHECK(stats_proxy); |
385 encoder_queue_.PostTask([this] { | 384 encoder_queue_.PostTask([this] { |
386 RTC_DCHECK_RUN_ON(&encoder_queue_); | 385 RTC_DCHECK_RUN_ON(&encoder_queue_); |
387 overuse_detector_.StartCheckForOveruse(); | 386 overuse_detector_->StartCheckForOveruse(); |
388 video_sender_.RegisterExternalEncoder( | 387 video_sender_.RegisterExternalEncoder( |
389 settings_.encoder, settings_.payload_type, settings_.internal_source); | 388 settings_.encoder, settings_.payload_type, settings_.internal_source); |
390 }); | 389 }); |
391 } | 390 } |
392 | 391 |
393 ViEEncoder::~ViEEncoder() { | 392 ViEEncoder::~ViEEncoder() { |
394 RTC_DCHECK_RUN_ON(&thread_checker_); | 393 RTC_DCHECK_RUN_ON(&thread_checker_); |
395 RTC_DCHECK(shutdown_event_.Wait(0)) | 394 RTC_DCHECK(shutdown_event_.Wait(0)) |
396 << "Must call ::Stop() before destruction."; | 395 << "Must call ::Stop() before destruction."; |
397 } | 396 } |
398 | 397 |
398 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | |
399 // pipelining encoders better (multiple input frames before something comes | |
400 // out). This should effectively turn off CPU adaptations for systems that | |
401 // remotely cope with the load right now. | |
402 CpuOveruseOptions ViEEncoder::GetCpuOveruseOptions(bool full_overuse_time) { | |
403 CpuOveruseOptions options; | |
404 if (full_overuse_time) { | |
405 options.low_encode_usage_threshold_percent = 150; | |
406 options.high_encode_usage_threshold_percent = 200; | |
407 } | |
408 return options; | |
409 } | |
410 | |
399 void ViEEncoder::Stop() { | 411 void ViEEncoder::Stop() { |
400 RTC_DCHECK_RUN_ON(&thread_checker_); | 412 RTC_DCHECK_RUN_ON(&thread_checker_); |
401 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 413 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
402 encoder_queue_.PostTask([this] { | 414 encoder_queue_.PostTask([this] { |
403 RTC_DCHECK_RUN_ON(&encoder_queue_); | 415 RTC_DCHECK_RUN_ON(&encoder_queue_); |
404 overuse_detector_.StopCheckForOveruse(); | 416 overuse_detector_->StopCheckForOveruse(); |
405 rate_allocator_.reset(); | 417 rate_allocator_.reset(); |
406 bitrate_observer_ = nullptr; | 418 bitrate_observer_ = nullptr; |
407 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 419 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
408 false); | 420 false); |
409 quality_scaler_ = nullptr; | 421 quality_scaler_ = nullptr; |
410 shutdown_event_.Set(); | 422 shutdown_event_.Set(); |
411 }); | 423 }); |
412 | 424 |
413 shutdown_event_.Wait(rtc::Event::kForever); | 425 shutdown_event_.Wait(rtc::Event::kForever); |
414 } | 426 } |
(...skipping 30 matching lines...) Expand all Loading... | |
445 RTC_DCHECK_RUN_ON(&encoder_queue_); | 457 RTC_DCHECK_RUN_ON(&encoder_queue_); |
446 if (degradation_preference_ != degradation_preference) { | 458 if (degradation_preference_ != degradation_preference) { |
447 // Reset adaptation state, so that we're not tricked into thinking there's | 459 // Reset adaptation state, so that we're not tricked into thinking there's |
448 // an already pending request of the same type. | 460 // an already pending request of the same type. |
449 last_adaptation_request_.reset(); | 461 last_adaptation_request_.reset(); |
450 } | 462 } |
451 degradation_preference_ = degradation_preference; | 463 degradation_preference_ = degradation_preference; |
452 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); | 464 bool allow_scaling = IsResolutionScalingEnabled(degradation_preference_); |
453 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; | 465 initial_rampup_ = allow_scaling ? 0 : kMaxInitialFramedrop; |
454 ConfigureQualityScaler(); | 466 ConfigureQualityScaler(); |
467 if (!IsFramerateScalingEnabled(degradation_preference) && | |
468 max_framerate_ != -1) { | |
469 // If frame rate scaling is no longer allowed, remove any potential | |
470 // allowance for longer frame intervals. | |
471 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); | |
472 } | |
455 }); | 473 }); |
456 } | 474 } |
457 | 475 |
458 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 476 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
459 source_proxy_->SetWantsRotationApplied(rotation_applied); | 477 source_proxy_->SetWantsRotationApplied(rotation_applied); |
460 encoder_queue_.PostTask([this, sink] { | 478 encoder_queue_.PostTask([this, sink] { |
461 RTC_DCHECK_RUN_ON(&encoder_queue_); | 479 RTC_DCHECK_RUN_ON(&encoder_queue_); |
462 sink_ = sink; | 480 sink_ = sink; |
463 }); | 481 }); |
464 } | 482 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
514 if (!VideoCodecInitializer::SetupCodec(encoder_config_, settings_, streams, | 532 if (!VideoCodecInitializer::SetupCodec(encoder_config_, settings_, streams, |
515 nack_enabled_, &codec, | 533 nack_enabled_, &codec, |
516 &rate_allocator_)) { | 534 &rate_allocator_)) { |
517 LOG(LS_ERROR) << "Failed to create encoder configuration."; | 535 LOG(LS_ERROR) << "Failed to create encoder configuration."; |
518 } | 536 } |
519 | 537 |
520 codec.startBitrate = | 538 codec.startBitrate = |
521 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); | 539 std::max(encoder_start_bitrate_bps_ / 1000, codec.minBitrate); |
522 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); | 540 codec.startBitrate = std::min(codec.startBitrate, codec.maxBitrate); |
523 codec.expect_encode_from_texture = last_frame_info_->is_texture; | 541 codec.expect_encode_from_texture = last_frame_info_->is_texture; |
542 max_framerate_ = codec.maxFramerate; | |
543 RTC_DCHECK_LE(max_framerate_, kMaxFramerateFps); | |
524 | 544 |
525 bool success = video_sender_.RegisterSendCodec( | 545 bool success = video_sender_.RegisterSendCodec( |
526 &codec, number_of_cores_, | 546 &codec, number_of_cores_, |
527 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; | 547 static_cast<uint32_t>(max_data_payload_length_)) == VCM_OK; |
528 if (!success) { | 548 if (!success) { |
529 LOG(LS_ERROR) << "Failed to configure encoder."; | 549 LOG(LS_ERROR) << "Failed to configure encoder."; |
530 rate_allocator_.reset(); | 550 rate_allocator_.reset(); |
531 } | 551 } |
532 | 552 |
533 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), | 553 video_sender_.UpdateChannelParemeters(rate_allocator_.get(), |
534 bitrate_observer_); | 554 bitrate_observer_); |
535 | 555 |
536 int framerate = stats_proxy_->GetSendFrameRate(); | 556 // Get the current actual framerate, as measured by the stats proxy. This is |
537 if (framerate == 0) | 557 // used to get the correct bitrate layer allocation. |
538 framerate = codec.maxFramerate; | 558 int current_framerate = stats_proxy_->GetSendFrameRate(); |
559 if (current_framerate == 0) | |
560 current_framerate = codec.maxFramerate; | |
539 stats_proxy_->OnEncoderReconfigured( | 561 stats_proxy_->OnEncoderReconfigured( |
540 encoder_config_, rate_allocator_.get() | 562 encoder_config_, |
541 ? rate_allocator_->GetPreferredBitrateBps(framerate) | 563 rate_allocator_.get() |
542 : codec.maxBitrate); | 564 ? rate_allocator_->GetPreferredBitrateBps(current_framerate) |
565 : codec.maxBitrate); | |
543 | 566 |
544 pending_encoder_reconfiguration_ = false; | 567 pending_encoder_reconfiguration_ = false; |
545 | 568 |
546 sink_->OnEncoderConfigurationChanged( | 569 sink_->OnEncoderConfigurationChanged( |
547 std::move(streams), encoder_config_.min_transmit_bitrate_bps); | 570 std::move(streams), encoder_config_.min_transmit_bitrate_bps); |
548 | 571 |
572 // Get the current target framerate, ie the maximum framerate as specified by | |
573 // the current codec configuration, or any limit imposed by cpu adaption in | |
574 // maintain-resolution or balanced mode. This is used to make sure overuse | |
575 // detection doesn't needlessly trigger in low and/or variable framerate | |
576 // scenarios. | |
577 int target_framerate = max_framerate_; | |
578 if (last_adaptation_request_ && | |
579 last_adaptation_request_->selected_framerate_fps_) { | |
åsapersson
2017/06/14 10:50:26
would it be better to get sink_wants_.max_framerat
sprang_webrtc
2017/06/14 13:41:28
Sure, I'll expose a getter that can be used.
| |
580 target_framerate = std::min( | |
581 target_framerate, *last_adaptation_request_->selected_framerate_fps_); | |
582 } | |
583 overuse_detector_->OnTargetFramerateUpdated(target_framerate); | |
584 | |
549 ConfigureQualityScaler(); | 585 ConfigureQualityScaler(); |
550 } | 586 } |
551 | 587 |
552 void ViEEncoder::ConfigureQualityScaler() { | 588 void ViEEncoder::ConfigureQualityScaler() { |
553 RTC_DCHECK_RUN_ON(&encoder_queue_); | 589 RTC_DCHECK_RUN_ON(&encoder_queue_); |
554 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 590 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
555 const bool quality_scaling_allowed = | 591 const bool quality_scaling_allowed = |
556 IsResolutionScalingEnabled(degradation_preference_) && | 592 IsResolutionScalingEnabled(degradation_preference_) && |
557 scaling_settings.enabled; | 593 scaling_settings.enabled; |
558 | 594 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
687 | 723 |
688 if (EncoderPaused()) { | 724 if (EncoderPaused()) { |
689 TraceFrameDropStart(); | 725 TraceFrameDropStart(); |
690 return; | 726 return; |
691 } | 727 } |
692 TraceFrameDropEnd(); | 728 TraceFrameDropEnd(); |
693 | 729 |
694 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 730 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
695 "Encode"); | 731 "Encode"); |
696 | 732 |
697 overuse_detector_.FrameCaptured(video_frame, time_when_posted_us); | 733 overuse_detector_->FrameCaptured(video_frame, time_when_posted_us); |
698 | 734 |
699 video_sender_.AddVideoFrame(video_frame, nullptr); | 735 video_sender_.AddVideoFrame(video_frame, nullptr); |
700 } | 736 } |
701 | 737 |
702 void ViEEncoder::SendKeyFrame() { | 738 void ViEEncoder::SendKeyFrame() { |
703 if (!encoder_queue_.IsCurrent()) { | 739 if (!encoder_queue_.IsCurrent()) { |
704 encoder_queue_.PostTask([this] { SendKeyFrame(); }); | 740 encoder_queue_.PostTask([this] { SendKeyFrame(); }); |
705 return; | 741 return; |
706 } | 742 } |
707 RTC_DCHECK_RUN_ON(&encoder_queue_); | 743 RTC_DCHECK_RUN_ON(&encoder_queue_); |
(...skipping 10 matching lines...) Expand all Loading... | |
718 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 754 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
719 | 755 |
720 EncodedImageCallback::Result result = | 756 EncodedImageCallback::Result result = |
721 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 757 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
722 | 758 |
723 int64_t time_sent_us = rtc::TimeMicros(); | 759 int64_t time_sent_us = rtc::TimeMicros(); |
724 uint32_t timestamp = encoded_image._timeStamp; | 760 uint32_t timestamp = encoded_image._timeStamp; |
725 const int qp = encoded_image.qp_; | 761 const int qp = encoded_image.qp_; |
726 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { | 762 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { |
727 RTC_DCHECK_RUN_ON(&encoder_queue_); | 763 RTC_DCHECK_RUN_ON(&encoder_queue_); |
728 overuse_detector_.FrameSent(timestamp, time_sent_us); | 764 overuse_detector_->FrameSent(timestamp, time_sent_us); |
729 if (quality_scaler_ && qp >= 0) | 765 if (quality_scaler_ && qp >= 0) |
730 quality_scaler_->ReportQP(qp); | 766 quality_scaler_->ReportQP(qp); |
731 }); | 767 }); |
732 | 768 |
733 return result; | 769 return result; |
734 } | 770 } |
735 | 771 |
736 void ViEEncoder::OnDroppedFrame() { | 772 void ViEEncoder::OnDroppedFrame() { |
737 encoder_queue_.PostTask([this] { | 773 encoder_queue_.PostTask([this] { |
738 RTC_DCHECK_RUN_ON(&encoder_queue_); | 774 RTC_DCHECK_RUN_ON(&encoder_queue_); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
789 LOG(LS_INFO) << "Video suspend state changed to: " | 825 LOG(LS_INFO) << "Video suspend state changed to: " |
790 << (video_is_suspended ? "suspended" : "not suspended"); | 826 << (video_is_suspended ? "suspended" : "not suspended"); |
791 stats_proxy_->OnSuspendChange(video_is_suspended); | 827 stats_proxy_->OnSuspendChange(video_is_suspended); |
792 } | 828 } |
793 } | 829 } |
794 | 830 |
795 void ViEEncoder::AdaptDown(AdaptReason reason) { | 831 void ViEEncoder::AdaptDown(AdaptReason reason) { |
796 RTC_DCHECK_RUN_ON(&encoder_queue_); | 832 RTC_DCHECK_RUN_ON(&encoder_queue_); |
797 AdaptationRequest adaptation_request = { | 833 AdaptationRequest adaptation_request = { |
798 last_frame_info_->pixel_count(), | 834 last_frame_info_->pixel_count(), |
799 stats_proxy_->GetStats().input_frame_rate, | 835 stats_proxy_->GetStats().input_frame_rate, rtc::Optional<int>(), |
800 AdaptationRequest::Mode::kAdaptDown}; | 836 AdaptationRequest::Mode::kAdaptDown}; |
801 | 837 |
802 bool downgrade_requested = | 838 bool downgrade_requested = |
803 last_adaptation_request_ && | 839 last_adaptation_request_ && |
804 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; | 840 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
805 | 841 |
806 int max_downgrades = 0; | 842 int max_downgrades = 0; |
807 switch (degradation_preference_) { | 843 switch (degradation_preference_) { |
808 case VideoSendStream::DegradationPreference::kBalanced: | 844 case VideoSendStream::DegradationPreference::kBalanced: |
809 FALLTHROUGH(); | 845 FALLTHROUGH(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
844 case VideoSendStream::DegradationPreference::kBalanced: | 880 case VideoSendStream::DegradationPreference::kBalanced: |
845 FALLTHROUGH(); | 881 FALLTHROUGH(); |
846 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 882 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
847 // Scale down resolution. | 883 // Scale down resolution. |
848 if (!source_proxy_->RequestResolutionLowerThan( | 884 if (!source_proxy_->RequestResolutionLowerThan( |
849 adaptation_request.input_pixel_count_)) { | 885 adaptation_request.input_pixel_count_)) { |
850 return; | 886 return; |
851 } | 887 } |
852 GetAdaptCounter().IncrementResolution(reason, 1); | 888 GetAdaptCounter().IncrementResolution(reason, 1); |
853 break; | 889 break; |
854 case VideoSendStream::DegradationPreference::kMaintainResolution: | 890 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
855 // Scale down framerate. | 891 // Scale down framerate. |
856 if (!source_proxy_->RequestFramerateLowerThan( | 892 const int requested_framerate = source_proxy_->RequestFramerateLowerThan( |
857 adaptation_request.framerate_fps_)) { | 893 adaptation_request.framerate_fps_); |
894 if (requested_framerate == -1) | |
858 return; | 895 return; |
859 } | 896 adaptation_request.selected_framerate_fps_.emplace(requested_framerate); |
897 overuse_detector_->OnTargetFramerateUpdated( | |
898 std::min(max_framerate_, requested_framerate)); | |
860 GetAdaptCounter().IncrementFramerate(reason, 1); | 899 GetAdaptCounter().IncrementFramerate(reason, 1); |
861 break; | 900 break; |
901 } | |
862 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 902 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
863 RTC_NOTREACHED(); | 903 RTC_NOTREACHED(); |
864 } | 904 } |
865 | 905 |
866 last_adaptation_request_.emplace(adaptation_request); | 906 last_adaptation_request_.emplace(adaptation_request); |
867 | 907 |
868 UpdateAdaptationStats(reason); | 908 UpdateAdaptationStats(reason); |
869 | 909 |
870 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); | 910 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); |
871 } | 911 } |
872 | 912 |
873 void ViEEncoder::AdaptUp(AdaptReason reason) { | 913 void ViEEncoder::AdaptUp(AdaptReason reason) { |
874 RTC_DCHECK_RUN_ON(&encoder_queue_); | 914 RTC_DCHECK_RUN_ON(&encoder_queue_); |
875 | 915 |
876 const AdaptCounter& adapt_counter = GetConstAdaptCounter(); | 916 const AdaptCounter& adapt_counter = GetConstAdaptCounter(); |
877 int num_downgrades = adapt_counter.TotalCount(reason); | 917 int num_downgrades = adapt_counter.TotalCount(reason); |
878 if (num_downgrades == 0) | 918 if (num_downgrades == 0) |
879 return; | 919 return; |
880 RTC_DCHECK_GT(num_downgrades, 0); | 920 RTC_DCHECK_GT(num_downgrades, 0); |
881 | 921 |
882 AdaptationRequest adaptation_request = { | 922 AdaptationRequest adaptation_request = { |
883 last_frame_info_->pixel_count(), | 923 last_frame_info_->pixel_count(), |
884 stats_proxy_->GetStats().input_frame_rate, | 924 stats_proxy_->GetStats().input_frame_rate, rtc::Optional<int>(), |
885 AdaptationRequest::Mode::kAdaptUp}; | 925 AdaptationRequest::Mode::kAdaptUp}; |
886 | 926 |
887 bool adapt_up_requested = | 927 bool adapt_up_requested = |
888 last_adaptation_request_ && | 928 last_adaptation_request_ && |
889 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; | 929 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptUp; |
890 | 930 |
891 switch (degradation_preference_) { | 931 switch (degradation_preference_) { |
892 case VideoSendStream::DegradationPreference::kBalanced: | 932 case VideoSendStream::DegradationPreference::kBalanced: |
893 FALLTHROUGH(); | 933 FALLTHROUGH(); |
894 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 934 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
(...skipping 28 matching lines...) Expand all Loading... | |
923 GetAdaptCounter().IncrementResolution(reason, -1); | 963 GetAdaptCounter().IncrementResolution(reason, -1); |
924 break; | 964 break; |
925 } | 965 } |
926 case VideoSendStream::DegradationPreference::kMaintainResolution: { | 966 case VideoSendStream::DegradationPreference::kMaintainResolution: { |
927 // Scale up framerate. | 967 // Scale up framerate. |
928 int fps = adaptation_request.framerate_fps_; | 968 int fps = adaptation_request.framerate_fps_; |
929 if (adapt_counter.FramerateCount() == 1) { | 969 if (adapt_counter.FramerateCount() == 1) { |
930 LOG(LS_INFO) << "Removing framerate down-scaling setting."; | 970 LOG(LS_INFO) << "Removing framerate down-scaling setting."; |
931 fps = std::numeric_limits<int>::max(); | 971 fps = std::numeric_limits<int>::max(); |
932 } | 972 } |
933 if (!source_proxy_->RequestHigherFramerateThan(fps)) | 973 |
974 const int requested_framerate = | |
975 source_proxy_->RequestHigherFramerateThan(fps); | |
976 if (requested_framerate == -1) { | |
977 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); | |
934 return; | 978 return; |
979 } | |
980 adaptation_request.selected_framerate_fps_.emplace(requested_framerate); | |
981 overuse_detector_->OnTargetFramerateUpdated( | |
982 std::min(max_framerate_, requested_framerate)); | |
935 GetAdaptCounter().IncrementFramerate(reason, -1); | 983 GetAdaptCounter().IncrementFramerate(reason, -1); |
936 break; | 984 break; |
937 } | 985 } |
938 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 986 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
939 RTC_NOTREACHED(); | 987 RTC_NOTREACHED(); |
940 } | 988 } |
941 | 989 |
942 last_adaptation_request_.emplace(adaptation_request); | 990 last_adaptation_request_.emplace(adaptation_request); |
943 | 991 |
944 UpdateAdaptationStats(reason); | 992 UpdateAdaptationStats(reason); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1051 std::string ViEEncoder::AdaptCounter::ToString( | 1099 std::string ViEEncoder::AdaptCounter::ToString( |
1052 const std::vector<int>& counters) const { | 1100 const std::vector<int>& counters) const { |
1053 std::stringstream ss; | 1101 std::stringstream ss; |
1054 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { | 1102 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
1055 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; | 1103 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
1056 } | 1104 } |
1057 return ss.str(); | 1105 return ss.str(); |
1058 } | 1106 } |
1059 | 1107 |
1060 } // namespace webrtc | 1108 } // namespace webrtc |
OLD | NEW |