Clean resource_adaptation_processor_unittest use of TaskQueueForTest

Use the current task queue in the test instead.

R=hbos@webrtc.org

Bug: None
Change-Id: I266796df3de675a808bcf2da08901411fadc86ae
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176414
Commit-Queue: Evan Shrubsole <eshr@google.com>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31439}
This commit is contained in:
Evan Shrubsole
2020-06-03 14:09:09 +02:00
committed by Commit Bot
parent aa40b89006
commit 4d75d9c04c

View File

@ -84,8 +84,7 @@ class VideoSourceRestrictionsListenerForTesting
class ResourceAdaptationProcessorTest : public ::testing::Test { class ResourceAdaptationProcessorTest : public ::testing::Test {
public: public:
ResourceAdaptationProcessorTest() ResourceAdaptationProcessorTest()
: resource_adaptation_queue_("ResourceAdaptationQueue"), : frame_rate_provider_(),
frame_rate_provider_(),
input_state_provider_(&frame_rate_provider_), input_state_provider_(&frame_rate_provider_),
resource_(FakeResource::Create("FakeResource")), resource_(FakeResource::Create("FakeResource")),
other_resource_(FakeResource::Create("OtherFakeResource")), other_resource_(FakeResource::Create("OtherFakeResource")),
@ -94,26 +93,17 @@ class ResourceAdaptationProcessorTest : public ::testing::Test {
processor_(std::make_unique<ResourceAdaptationProcessor>( processor_(std::make_unique<ResourceAdaptationProcessor>(
&input_state_provider_, &input_state_provider_,
/*encoder_stats_observer=*/&frame_rate_provider_)) { /*encoder_stats_observer=*/&frame_rate_provider_)) {
resource_adaptation_queue_.SendTask( processor_->SetResourceAdaptationQueue(TaskQueueBase::Current());
[this] { processor_->AddRestrictionsListener(&restrictions_listener_);
processor_->SetResourceAdaptationQueue( processor_->AddResource(resource_);
resource_adaptation_queue_.Get()); processor_->AddResource(other_resource_);
processor_->AddRestrictionsListener(&restrictions_listener_); processor_->AddAdaptationConstraint(&adaptation_constraint_);
processor_->AddResource(resource_); processor_->AddAdaptationListener(&adaptation_listener_);
processor_->AddResource(other_resource_);
processor_->AddAdaptationConstraint(&adaptation_constraint_);
processor_->AddAdaptationListener(&adaptation_listener_);
},
RTC_FROM_HERE);
} }
~ResourceAdaptationProcessorTest() override { ~ResourceAdaptationProcessorTest() override {
resource_adaptation_queue_.SendTask( if (processor_) {
[this] { DestroyProcessor();
if (processor_) { }
DestroyProcessor();
}
},
RTC_FROM_HERE);
} }
void SetInputStates(bool has_input, int fps, int frame_size) { void SetInputStates(bool has_input, int fps, int frame_size) {
@ -131,7 +121,6 @@ class ResourceAdaptationProcessorTest : public ::testing::Test {
} }
void DestroyProcessor() { void DestroyProcessor() {
RTC_DCHECK_RUN_ON(&resource_adaptation_queue_);
processor_->StopResourceAdaptation(); processor_->StopResourceAdaptation();
processor_->RemoveRestrictionsListener(&restrictions_listener_); processor_->RemoveRestrictionsListener(&restrictions_listener_);
processor_->RemoveResource(resource_); processor_->RemoveResource(resource_);
@ -142,7 +131,6 @@ class ResourceAdaptationProcessorTest : public ::testing::Test {
} }
protected: protected:
TaskQueueForTest resource_adaptation_queue_;
FakeFrameRateProvider frame_rate_provider_; FakeFrameRateProvider frame_rate_provider_;
VideoStreamInputStateProvider input_state_provider_; VideoStreamInputStateProvider input_state_provider_;
rtc::scoped_refptr<FakeResource> resource_; rtc::scoped_refptr<FakeResource> resource_;
@ -156,39 +144,31 @@ class ResourceAdaptationProcessorTest : public ::testing::Test {
} // namespace } // namespace
TEST_F(ResourceAdaptationProcessorTest, DisabledByDefault) { TEST_F(ResourceAdaptationProcessorTest, DisabledByDefault) {
resource_adaptation_queue_.SendTask( EXPECT_EQ(DegradationPreference::DISABLED,
[this] { processor_->degradation_preference());
EXPECT_EQ(DegradationPreference::DISABLED, EXPECT_EQ(DegradationPreference::DISABLED,
processor_->degradation_preference()); processor_->effective_degradation_preference());
EXPECT_EQ(DegradationPreference::DISABLED, SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->effective_degradation_preference()); processor_->StartResourceAdaptation();
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); // Adaptation does not happen when disabled.
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
// Adaptation does not happen when disabled. EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, InsufficientInput) { TEST_F(ResourceAdaptationProcessorTest, InsufficientInput) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); // Adaptation does not happen if input is insufficient.
processor_->StartResourceAdaptation(); // When frame size is missing (OnFrameSizeObserved not called yet).
// Adaptation does not happen if input is insufficient. input_state_provider_.OnHasInputChanged(true);
// When frame size is missing (OnFrameSizeObserved not called yet). resource_->SetUsageState(ResourceUsageState::kOveruse);
input_state_provider_.OnHasInputChanged(true); EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); // When "has input" is missing.
EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count()); SetInputStates(false, kDefaultFrameRate, kDefaultFrameSize);
// When "has input" is missing. resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(false, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); // Note: frame rate cannot be missing, if unset it is 0.
EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
// Note: frame rate cannot be missing, if unset it is 0.
},
RTC_FROM_HERE);
} }
// These tests verify that restrictions are applied, but not exactly how much // These tests verify that restrictions are applied, but not exactly how much
@ -197,285 +177,232 @@ TEST_F(ResourceAdaptationProcessorTest, InsufficientInput) {
// restrictions. For that, see video_stream_adapter_unittest.cc. // restrictions. For that, see video_stream_adapter_unittest.cc.
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
OveruseTriggersRestrictingResolutionInMaintainFrameRate) { OveruseTriggersRestrictingResolutionInMaintainFrameRate) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); EXPECT_TRUE(
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); restrictions_listener_.restrictions().max_pixels_per_frame().has_value());
EXPECT_TRUE(restrictions_listener_.restrictions()
.max_pixels_per_frame()
.has_value());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
OveruseTriggersRestrictingFrameRateInMaintainResolution) { OveruseTriggersRestrictingFrameRateInMaintainResolution) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_RESOLUTION);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_RESOLUTION); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); EXPECT_TRUE(
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); restrictions_listener_.restrictions().max_frame_rate().has_value());
EXPECT_TRUE(
restrictions_listener_.restrictions().max_frame_rate().has_value());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
OveruseTriggersRestrictingFrameRateAndResolutionInBalanced) { OveruseTriggersRestrictingFrameRateAndResolutionInBalanced) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(DegradationPreference::BALANCED);
[this] { processor_->StartResourceAdaptation();
processor_->SetDegradationPreference(DegradationPreference::BALANCED); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); // Adapting multiple times eventually resticts both frame rate and
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); // resolution. Exactly many times we need to adapt depends on
// Adapting multiple times eventually resticts both frame rate and // BalancedDegradationSettings, VideoStreamAdapter and default input
// resolution. Exactly many times we need to adapt depends on // states. This test requires it to be achieved within 4 adaptations.
// BalancedDegradationSettings, VideoStreamAdapter and default input for (size_t i = 0; i < 4; ++i) {
// states. This test requires it to be achieved within 4 adaptations. resource_->SetUsageState(ResourceUsageState::kOveruse);
for (size_t i = 0; i < 4; ++i) { EXPECT_EQ(i + 1, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(i + 1, restrictions_listener_.restrictions_updated_count()); }
RestrictSource(restrictions_listener_.restrictions()); EXPECT_TRUE(
} restrictions_listener_.restrictions().max_pixels_per_frame().has_value());
EXPECT_TRUE(restrictions_listener_.restrictions() EXPECT_TRUE(
.max_pixels_per_frame() restrictions_listener_.restrictions().max_frame_rate().has_value());
.has_value());
EXPECT_TRUE(
restrictions_listener_.restrictions().max_frame_rate().has_value());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, AwaitingPreviousAdaptation) { TEST_F(ResourceAdaptationProcessorTest, AwaitingPreviousAdaptation) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); // If we don't restrict the source then adaptation will not happen again
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); // due to "awaiting previous adaptation". This prevents "double-adapt".
// If we don't restrict the source then adaptation will not happen again resource_->SetUsageState(ResourceUsageState::kOveruse);
// due to "awaiting previous adaptation". This prevents "double-adapt". EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, CannotAdaptUpWhenUnrestricted) { TEST_F(ResourceAdaptationProcessorTest, CannotAdaptUpWhenUnrestricted) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kUnderuse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, UnderuseTakesUsBackToUnrestricted) { TEST_F(ResourceAdaptationProcessorTest, UnderuseTakesUsBackToUnrestricted) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); resource_->SetUsageState(ResourceUsageState::kUnderuse);
RestrictSource(restrictions_listener_.restrictions()); EXPECT_EQ(2u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kUnderuse); EXPECT_EQ(VideoSourceRestrictions(), restrictions_listener_.restrictions());
EXPECT_EQ(2u, restrictions_listener_.restrictions_updated_count());
EXPECT_EQ(VideoSourceRestrictions(),
restrictions_listener_.restrictions());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, ResourcesCanPreventAdaptingUp) { TEST_F(ResourceAdaptationProcessorTest, ResourcesCanPreventAdaptingUp) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); // Adapt down so that we can adapt up.
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); resource_->SetUsageState(ResourceUsageState::kOveruse);
// Adapt down so that we can adapt up. EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); // Adapting up is prevented.
RestrictSource(restrictions_listener_.restrictions()); adaptation_constraint_.set_is_adaptation_up_allowed(false);
// Adapting up is prevented. resource_->SetUsageState(ResourceUsageState::kUnderuse);
adaptation_constraint_.set_is_adaptation_up_allowed(false); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
ResourcesCanNotAdaptUpIfNeverAdaptedDown) { ResourcesCanNotAdaptUpIfNeverAdaptedDown) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
RestrictSource(restrictions_listener_.restrictions());
// Other resource signals under-use // Other resource signals under-use
other_resource_->SetUsageState(ResourceUsageState::kUnderuse); other_resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count()); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
ResourcesCanNotAdaptUpIfNotAdaptedDownAfterReset) { ResourcesCanNotAdaptUpIfNotAdaptedDownAfterReset) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ(1u, restrictions_listener_.restrictions_updated_count());
processor_->ResetVideoSourceRestrictions(); processor_->ResetVideoSourceRestrictions();
EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total());
other_resource_->SetUsageState(ResourceUsageState::kOveruse); other_resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
// resource_ did not overuse after we reset the restrictions, so adapt // resource_ did not overuse after we reset the restrictions, so adapt
// up should be disallowed. // up should be disallowed.
resource_->SetUsageState(ResourceUsageState::kUnderuse); resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
MultipleResourcesCanTriggerMultipleAdaptations) { MultipleResourcesCanTriggerMultipleAdaptations) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); other_resource_->SetUsageState(ResourceUsageState::kOveruse);
RestrictSource(restrictions_listener_.restrictions()); EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total());
other_resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total()); other_resource_->SetUsageState(ResourceUsageState::kOveruse);
RestrictSource(restrictions_listener_.restrictions()); EXPECT_EQ(3, restrictions_listener_.adaptation_counters().Total());
other_resource_->SetUsageState(ResourceUsageState::kOveruse); RestrictSource(restrictions_listener_.restrictions());
EXPECT_EQ(3, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions());
resource_->SetUsageState(ResourceUsageState::kUnderuse); resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
// Does not trigger adaptation since resource has no adaptations left. // Does not trigger adaptation since resource has no adaptations left.
resource_->SetUsageState(ResourceUsageState::kUnderuse); resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(2, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
other_resource_->SetUsageState(ResourceUsageState::kUnderuse); other_resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
other_resource_->SetUsageState(ResourceUsageState::kUnderuse); other_resource_->SetUsageState(ResourceUsageState::kUnderuse);
EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, AdaptingTriggersOnAdaptationApplied) { TEST_F(ResourceAdaptationProcessorTest, AdaptingTriggersOnAdaptationApplied) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); resource_->SetUsageState(ResourceUsageState::kOveruse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); EXPECT_EQ(1u, adaptation_listener_.num_adaptations_applied());
resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ(1u, adaptation_listener_.num_adaptations_applied());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
AdaptsDownWhenOtherResourceIsAlwaysUnderused) { AdaptsDownWhenOtherResourceIsAlwaysUnderused) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation(); other_resource_->SetUsageState(ResourceUsageState::kUnderuse);
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); // Does not trigger adapataion because there's no restriction.
other_resource_->SetUsageState(ResourceUsageState::kUnderuse); EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total());
// Does not trigger adapataion because there's no restriction.
EXPECT_EQ(0, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
resource_->SetUsageState(ResourceUsageState::kOveruse); resource_->SetUsageState(ResourceUsageState::kOveruse);
// Adapts down even if other resource asked for adapting up. // Adapts down even if other resource asked for adapting up.
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
other_resource_->SetUsageState(ResourceUsageState::kUnderuse); other_resource_->SetUsageState(ResourceUsageState::kUnderuse);
// Doesn't adapt up because adaptation is due to another resource. // Doesn't adapt up because adaptation is due to another resource.
EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total()); EXPECT_EQ(1, restrictions_listener_.adaptation_counters().Total());
RestrictSource(restrictions_listener_.restrictions()); RestrictSource(restrictions_listener_.restrictions());
},
RTC_FROM_HERE);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
TriggerOveruseNotOnAdaptationTaskQueue) { TriggerOveruseNotOnAdaptationTaskQueue) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation();
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); TaskQueueForTest resource_task_queue("ResourceTaskQueue");
}, resource_task_queue.PostTask(ToQueuedTask(
RTC_FROM_HERE); [&]() { resource_->SetUsageState(ResourceUsageState::kOveruse); }));
resource_->SetUsageState(ResourceUsageState::kOveruse);
EXPECT_EQ_WAIT(1u, restrictions_listener_.restrictions_updated_count(), EXPECT_EQ_WAIT(1u, restrictions_listener_.restrictions_updated_count(),
kDefaultTimeoutMs); kDefaultTimeoutMs);
} }
TEST_F(ResourceAdaptationProcessorTest, TEST_F(ResourceAdaptationProcessorTest,
DestroyProcessorWhileResourceListenerDelegateHasTaskInFlight) { DestroyProcessorWhileResourceListenerDelegateHasTaskInFlight) {
resource_adaptation_queue_.SendTask( processor_->SetDegradationPreference(
[this] { DegradationPreference::MAINTAIN_FRAMERATE);
processor_->SetDegradationPreference( processor_->StartResourceAdaptation();
DegradationPreference::MAINTAIN_FRAMERATE); SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize);
processor_->StartResourceAdaptation();
SetInputStates(true, kDefaultFrameRate, kDefaultFrameSize); // Wait for |resource_| to signal oversue first so we know that the delegate
}, // has passed it on to the processor's task queue.
RTC_FROM_HERE); rtc::Event resource_event;
// Block the destruction of the processor. This ensures that the adaptation TaskQueueForTest resource_task_queue("ResourceTaskQueue");
// queue is blocked until the ResourceListenerDelegate has had time to post resource_task_queue.PostTask(ToQueuedTask([&]() {
// its task. resource_->SetUsageState(ResourceUsageState::kOveruse);
rtc::Event destroy_processor_event; resource_event.Set();
resource_adaptation_queue_.PostTask([this, &destroy_processor_event] { }));
destroy_processor_event.Wait(rtc::Event::kForever);
DestroyProcessor(); EXPECT_TRUE(resource_event.Wait(kDefaultTimeoutMs));
}); // Now destroy the processor while handling the overuse is in flight.
resource_->SetUsageState(ResourceUsageState::kOveruse); DestroyProcessor();
// Unblock destruction and delegate task.
destroy_processor_event.Set();
resource_adaptation_queue_.WaitForPreviouslyPostedTasks();
// Because the processor was destroyed by the time the delegate's task ran, // Because the processor was destroyed by the time the delegate's task ran,
// the overuse signal must not have been handled. // the overuse signal must not have been handled.
EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count()); EXPECT_EQ(0u, restrictions_listener_.restrictions_updated_count());