diff --git a/webrtc/video_engine/include/vie_base.h b/webrtc/video_engine/include/vie_base.h index 486eca63d..0a528cb12 100644 --- a/webrtc/video_engine/include/vie_base.h +++ b/webrtc/video_engine/include/vie_base.h @@ -69,8 +69,8 @@ struct CpuOveruseOptions { enable_encode_usage_method(false), low_encode_usage_threshold_percent(60), high_encode_usage_threshold_percent(90), - low_encode_time_rsd_threshold(0), - high_encode_time_rsd_threshold(0), + low_encode_time_rsd_threshold(-1), + high_encode_time_rsd_threshold(-1), frame_timeout_interval_ms(1500), min_frame_samples(120), min_process_count(3), @@ -84,8 +84,12 @@ struct CpuOveruseOptions { bool enable_encode_usage_method; int low_encode_usage_threshold_percent; // Threshold for triggering underuse. int high_encode_usage_threshold_percent; // Threshold for triggering overuse. - int low_encode_time_rsd_threshold; // Threshold for triggering underuse. - int high_encode_time_rsd_threshold; // Threshold for triggering overuse. + int low_encode_time_rsd_threshold; // Additional threshold for triggering + // underuse (used in addition to + // threshold above if configured). + int high_encode_time_rsd_threshold; // Additional threshold for triggering + // overuse (used in addition to + // threshold above if configured). // General settings. int frame_timeout_interval_ms; // The maximum allowed interval between two // frames before resetting estimations. diff --git a/webrtc/video_engine/overuse_frame_detector.cc b/webrtc/video_engine/overuse_frame_detector.cc index 4a9510337..ac9519f32 100644 --- a/webrtc/video_engine/overuse_frame_detector.cc +++ b/webrtc/video_engine/overuse_frame_detector.cc @@ -38,7 +38,7 @@ const float kWeightFactorMean = 0.98f; const int kQuickRampUpDelayMs = 10 * 1000; // Delay between rampup attempts. Initially uses standard, scales up to max. const int kStandardRampUpDelayMs = 30 * 1000; -const int kMaxRampUpDelayMs = 120 * 1000; +const int kMaxRampUpDelayMs = 240 * 1000; // Expontential back-off factor, to prevent annoying up-down behaviour. const double kRampUpBackoffFactor = 2.0; @@ -126,7 +126,7 @@ class OveruseFrameDetector::EncodeTimeAvg { filtered_encode_time_ms_->Apply(exp, encode_time_ms); } - int filtered_encode_time_ms() const { + int Value() const { return static_cast(filtered_encode_time_ms_->Value() + 0.5); } @@ -175,7 +175,7 @@ class OveruseFrameDetector::EncodeUsage { filtered_encode_time_ms_->Apply(exp, encode_time_ms); } - int UsageInPercent() const { + int Value() const { if (count_ < static_cast(options_.min_frame_samples)) { return static_cast(InitialUsageInPercent() + 0.5f); } @@ -186,6 +186,7 @@ class OveruseFrameDetector::EncodeUsage { return static_cast(encode_usage_percent + 0.5); } + private: float InitialUsageInPercent() const { // Start in between the underuse and overuse threshold. return (options_.low_encode_usage_threshold_percent + @@ -196,7 +197,6 @@ class OveruseFrameDetector::EncodeUsage { return InitialUsageInPercent() * kInitialSampleDiffMs / 100; } - private: const float kWeightFactorFrameDiff; const float kWeightFactorEncodeTime; const float kInitialSampleDiffMs; @@ -295,13 +295,13 @@ class OveruseFrameDetector::EncodeTimeRsd { return static_cast(filtered_rsd_->Value() + 0.5); } + private: float InitialValue() const { // Start in between the underuse and overuse threshold. - return (options_.low_encode_time_rsd_threshold + - options_.high_encode_time_rsd_threshold) / 2.0f; + return std::max(((options_.low_encode_time_rsd_threshold + + options_.high_encode_time_rsd_threshold) / 2.0f), 0.0f); } - private: const float kWeightFactor; uint32_t count_; // Number of encode samples since last reset. CpuOveruseOptions options_; @@ -358,7 +358,7 @@ class OveruseFrameDetector::CaptureQueueDelay { return delay_ms_; } - int filtered_delay_ms_per_s() const { + int Value() const { return static_cast(filtered_delay_ms_per_s_->Value() + 0.5); } @@ -419,11 +419,10 @@ void OveruseFrameDetector::GetCpuOveruseMetrics( CpuOveruseMetrics* metrics) const { CriticalSectionScoped cs(crit_.get()); metrics->capture_jitter_ms = static_cast(capture_deltas_.StdDev() + 0.5); - metrics->avg_encode_time_ms = encode_time_->filtered_encode_time_ms(); + metrics->avg_encode_time_ms = encode_time_->Value(); metrics->encode_rsd = encode_rsd_->Value(); - metrics->encode_usage_percent = encode_usage_->UsageInPercent(); - metrics->capture_queue_delay_ms_per_s = - capture_queue_delay_->filtered_delay_ms_per_s(); + metrics->encode_usage_percent = encode_usage_->Value(); + metrics->capture_queue_delay_ms_per_s = capture_queue_delay_->Value(); } int32_t OveruseFrameDetector::TimeUntilNextProcess() { @@ -544,7 +543,7 @@ int32_t OveruseFrameDetector::Process() { in_quick_rampup_ ? kQuickRampUpDelayMs : current_rampup_delay_ms_; LOG(LS_INFO) << " Frame stats: capture avg: " << capture_deltas_.Mean() << " capture stddev " << capture_deltas_.StdDev() - << " encode usage " << encode_usage_->UsageInPercent() + << " encode usage " << encode_usage_->Value() << " encode rsd " << encode_rsd_->Value() << " rampup delay " << rampup_delay; return 0; @@ -556,8 +555,14 @@ bool OveruseFrameDetector::IsOverusing() { overusing = capture_deltas_.StdDev() >= options_.high_capture_jitter_threshold_ms; } else if (options_.enable_encode_usage_method) { - overusing = encode_usage_->UsageInPercent() >= - options_.high_encode_usage_threshold_percent; + bool encode_usage_overuse = + encode_usage_->Value() >= options_.high_encode_usage_threshold_percent; + bool encode_rsd_overuse = false; + if (options_.high_encode_time_rsd_threshold > 0) { + encode_rsd_overuse = + (encode_rsd_->Value() >= options_.high_encode_time_rsd_threshold); + } + overusing = encode_usage_overuse || encode_rsd_overuse; } if (overusing) { @@ -578,8 +583,14 @@ bool OveruseFrameDetector::IsUnderusing(int64_t time_now) { underusing = capture_deltas_.StdDev() < options_.low_capture_jitter_threshold_ms; } else if (options_.enable_encode_usage_method) { - underusing = encode_usage_->UsageInPercent() < - options_.low_encode_usage_threshold_percent; + bool encode_usage_underuse = + encode_usage_->Value() < options_.low_encode_usage_threshold_percent; + bool encode_rsd_underuse = true; + if (options_.low_encode_time_rsd_threshold > 0) { + encode_rsd_underuse = + (encode_rsd_->Value() < options_.low_encode_time_rsd_threshold); + } + underusing = encode_usage_underuse && encode_rsd_underuse; } return underusing; } diff --git a/webrtc/video_engine/overuse_frame_detector_unittest.cc b/webrtc/video_engine/overuse_frame_detector_unittest.cc index 2d7116f86..4e5d4bda2 100644 --- a/webrtc/video_engine/overuse_frame_detector_unittest.cc +++ b/webrtc/video_engine/overuse_frame_detector_unittest.cc @@ -71,6 +71,12 @@ class OveruseFrameDetectorTest : public ::testing::Test { options_.high_encode_usage_threshold_percent) / 2.0f) + 0.5; } + int InitialEncodeRsd() { + return std::max( + ((options_.low_encode_time_rsd_threshold + + options_.high_encode_time_rsd_threshold) / 2.0f) + 0.5f, 0.0f); + } + void InsertFramesWithInterval( size_t num_frames, int interval_ms, int width, int height) { while (num_frames-- > 0) { @@ -111,7 +117,19 @@ class OveruseFrameDetectorTest : public ::testing::Test { } } - void TriggerNormalUsageWithEncodeUsage() { + void TriggerOveruseWithEncodeRsd(int num_times) { + const int kEncodeTimeMs1 = 10; + const int kEncodeTimeMs2 = 25; + for (int i = 0; i < num_times; ++i) { + InsertAndEncodeFramesWithInterval( + 200, kFrameInterval33ms, kWidth, kHeight, kEncodeTimeMs1); + InsertAndEncodeFramesWithInterval( + 10, kFrameInterval33ms, kWidth, kHeight, kEncodeTimeMs2); + overuse_detector_->Process(); + } + } + + void TriggerNormalUsageWithEncodeTime() { const int kEncodeTimeMs = 5; InsertAndEncodeFramesWithInterval( 1000, kFrameInterval33ms, kWidth, kHeight, kEncodeTimeMs); @@ -136,25 +154,37 @@ class OveruseFrameDetectorTest : public ::testing::Test { return metrics.encode_usage_percent; } + int EncodeRsd() { + CpuOveruseMetrics metrics; + overuse_detector_->GetCpuOveruseMetrics(&metrics); + return metrics.encode_rsd; + } + CpuOveruseOptions options_; scoped_ptr clock_; scoped_ptr observer_; scoped_ptr overuse_detector_; }; +// enable_capture_jitter_method = true; +// CaptureJitterMs() > high_capture_jitter_threshold_ms => overuse. +// CaptureJitterMs() < low_capture_jitter_threshold_ms => underuse. TEST_F(OveruseFrameDetectorTest, TriggerOveruse) { + // capture_jitter > high => overuse EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); TriggerOveruse(options_.high_threshold_consecutive_count); } TEST_F(OveruseFrameDetectorTest, OveruseAndRecover) { + // capture_jitter > high => overuse EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); TriggerOveruse(options_.high_threshold_consecutive_count); + // capture_jitter < low => underuse EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(testing::AtLeast(1)); TriggerNormalUsage(); } -TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverNoObserver) { +TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverWithNoObserver) { overuse_detector_->SetObserver(NULL); EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(0); TriggerOveruse(options_.high_threshold_consecutive_count); @@ -162,8 +192,9 @@ TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverNoObserver) { TriggerNormalUsage(); } -TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverDisabled) { +TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverWithMethodDisabled) { options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = false; overuse_detector_->SetOptions(options_); EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(0); TriggerOveruse(options_.high_threshold_consecutive_count); @@ -221,6 +252,7 @@ TEST_F(OveruseFrameDetectorTest, GetCpuOveruseMetrics) { EXPECT_GT(metrics.avg_encode_time_ms, 0); EXPECT_GT(metrics.encode_usage_percent, 0); EXPECT_GE(metrics.capture_queue_delay_ms_per_s, 0); + EXPECT_GE(metrics.encode_rsd, 0); } TEST_F(OveruseFrameDetectorTest, CaptureJitter) { @@ -332,7 +364,7 @@ TEST_F(OveruseFrameDetectorTest, EncodedUsage) { const int kEncodeTimeMs = 5; InsertAndEncodeFramesWithInterval( 1000, kFrameInterval33ms, kWidth, kHeight, kEncodeTimeMs); - EXPECT_EQ(15, EncodeUsagePercent()); + EXPECT_EQ(kEncodeTimeMs * 100 / kFrameInterval33ms, EncodeUsagePercent()); } TEST_F(OveruseFrameDetectorTest, EncodeUsageResetAfterChangingThreshold) { @@ -345,10 +377,14 @@ TEST_F(OveruseFrameDetectorTest, EncodeUsageResetAfterChangingThreshold) { EXPECT_EQ(InitialEncodeUsage(), EncodeUsagePercent()); } +// enable_encode_usage_method = true; +// EncodeUsagePercent() > high_encode_usage_threshold_percent => overuse. +// EncodeUsagePercent() < low_encode_usage_threshold_percent => underuse. TEST_F(OveruseFrameDetectorTest, TriggerOveruseWithEncodeUsage) { options_.enable_capture_jitter_method = false; options_.enable_encode_usage_method = true; overuse_detector_->SetOptions(options_); + // usage > high => overuse EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); TriggerOveruseWithEncodeUsage(options_.high_threshold_consecutive_count); } @@ -357,9 +393,94 @@ TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverWithEncodeUsage) { options_.enable_capture_jitter_method = false; options_.enable_encode_usage_method = true; overuse_detector_->SetOptions(options_); + // usage > high => overuse EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); TriggerOveruseWithEncodeUsage(options_.high_threshold_consecutive_count); + // usage < low => underuse EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(testing::AtLeast(1)); - TriggerNormalUsageWithEncodeUsage(); + TriggerNormalUsageWithEncodeTime(); +} + +TEST_F(OveruseFrameDetectorTest, + OveruseAndRecoverWithEncodeUsageMethodDisabled) { + options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = false; + overuse_detector_->SetOptions(options_); + // usage > high => overuse + EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(0); + TriggerOveruseWithEncodeUsage(options_.high_threshold_consecutive_count); + // usage < low => underuse + EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(0); + TriggerNormalUsageWithEncodeTime(); +} + +TEST_F(OveruseFrameDetectorTest, EncodeRsdResetAfterChangingThreshold) { + EXPECT_EQ(InitialEncodeRsd(), EncodeRsd()); + options_.high_encode_time_rsd_threshold = 100; + overuse_detector_->SetOptions(options_); + EXPECT_EQ(InitialEncodeRsd(), EncodeRsd()); + options_.low_encode_time_rsd_threshold = 20; + overuse_detector_->SetOptions(options_); + EXPECT_EQ(InitialEncodeRsd(), EncodeRsd()); +} + +// enable_encode_usage_method = true; +// low/high_encode_time_rsd_threshold >= 0 +// EncodeUsagePercent() > high_encode_usage_threshold_percent || +// EncodeRsd() > high_encode_time_rsd_threshold => overuse. +// EncodeUsagePercent() < low_encode_usage_threshold_percent && +// EncodeRsd() < low_encode_time_rsd_threshold => underuse. +TEST_F(OveruseFrameDetectorTest, TriggerOveruseWithEncodeRsd) { + options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = true; + options_.high_encode_time_rsd_threshold = 80; + overuse_detector_->SetOptions(options_); + // rsd > high, usage < high => overuse + EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); + TriggerOveruseWithEncodeRsd(options_.high_threshold_consecutive_count); + EXPECT_LT(EncodeUsagePercent(), options_.high_encode_usage_threshold_percent); +} + +TEST_F(OveruseFrameDetectorTest, OveruseAndRecoverWithEncodeRsd) { + options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = true; + options_.low_encode_time_rsd_threshold = 20; + options_.high_encode_time_rsd_threshold = 80; + overuse_detector_->SetOptions(options_); + // rsd > high, usage < high => overuse + EXPECT_CALL(*(observer_.get()), OveruseDetected()).Times(1); + TriggerOveruseWithEncodeRsd(options_.high_threshold_consecutive_count); + EXPECT_LT(EncodeUsagePercent(), options_.high_encode_usage_threshold_percent); + // rsd < low, usage < low => underuse + EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(testing::AtLeast(1)); + TriggerNormalUsageWithEncodeTime(); +} + +TEST_F(OveruseFrameDetectorTest, NoUnderuseWithEncodeRsd_UsageGtLowThreshold) { + options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = true; + options_.low_encode_usage_threshold_percent = 1; + options_.low_encode_time_rsd_threshold = 20; + options_.high_encode_time_rsd_threshold = 90; + overuse_detector_->SetOptions(options_); + // rsd < low, usage > low => no underuse + EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(0); + TriggerNormalUsageWithEncodeTime(); + EXPECT_LT(EncodeRsd(), options_.low_encode_time_rsd_threshold); + EXPECT_GT(EncodeUsagePercent(), options_.low_encode_usage_threshold_percent); +} + +TEST_F(OveruseFrameDetectorTest, NoUnderuseWithEncodeRsd_RsdGtLowThreshold) { + options_.enable_capture_jitter_method = false; + options_.enable_encode_usage_method = true; + options_.low_encode_usage_threshold_percent = 20; + options_.low_encode_time_rsd_threshold = 1; + options_.high_encode_time_rsd_threshold = 90; + overuse_detector_->SetOptions(options_); + // rsd > low, usage < low => no underuse + EXPECT_CALL(*(observer_.get()), NormalUsage()).Times(0); + TriggerNormalUsageWithEncodeTime(); + EXPECT_GT(EncodeRsd(), options_.low_encode_time_rsd_threshold); + EXPECT_LT(EncodeUsagePercent(), options_.low_encode_usage_threshold_percent); } } // namespace webrtc