1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/*
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "video/adaptation/pixel_limit_resource.h"
#include <memory>
#include <utility>
#include "absl/functional/any_invocable.h"
#include "api/units/timestamp.h"
#include "call/adaptation/test/fake_video_stream_input_state_provider.h"
#include "call/adaptation/test/mock_resource_listener.h"
#include "call/adaptation/video_stream_adapter.h"
#include "rtc_base/task_queue_for_test.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/time_controller/simulated_time_controller.h"
using testing::_;
namespace webrtc {
namespace {
constexpr TimeDelta kResourceUsageCheckIntervalMs = TimeDelta::Seconds(5);
} // namespace
class PixelLimitResourceTest : public ::testing::Test {
public:
PixelLimitResourceTest()
: time_controller_(Timestamp::Micros(1234)),
task_queue_(time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
"TestQueue",
TaskQueueFactory::Priority::NORMAL)),
input_state_provider_() {}
void SetCurrentPixels(int current_pixels) {
input_state_provider_.SetInputState(current_pixels, 30, current_pixels);
}
void RunTaskOnTaskQueue(absl::AnyInvocable<void() &&> task) {
task_queue_->PostTask(std::move(task));
time_controller_.AdvanceTime(TimeDelta::Zero());
}
protected:
// Posted tasks, including repeated tasks, are executed when simulated time is
// advanced by time_controller_.AdvanceTime().
GlobalSimulatedTimeController time_controller_;
std::unique_ptr<TaskQueueBase, TaskQueueDeleter> task_queue_;
FakeVideoStreamInputStateProvider input_state_provider_;
};
TEST_F(PixelLimitResourceTest, ResourceIsSilentByDefault) {
// Because our mock is strick, the test would fail if
// OnResourceUsageStateMeasured() is invoked.
testing::StrictMock<MockResourceListener> resource_listener;
RunTaskOnTaskQueue([&]() {
rtc::scoped_refptr<PixelLimitResource> pixel_limit_resource =
PixelLimitResource::Create(task_queue_.get(), &input_state_provider_);
pixel_limit_resource->SetResourceListener(&resource_listener);
// Set a current pixel count.
SetCurrentPixels(1280 * 720);
// Advance a significant amount of time.
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs * 10);
pixel_limit_resource->SetResourceListener(nullptr);
});
}
TEST_F(PixelLimitResourceTest,
OveruseIsReportedWhileCurrentPixelsIsGreaterThanMaxPixels) {
constexpr int kMaxPixels = 640 * 480;
testing::StrictMock<MockResourceListener> resource_listener;
RunTaskOnTaskQueue([&]() {
rtc::scoped_refptr<PixelLimitResource> pixel_limit_resource =
PixelLimitResource::Create(task_queue_.get(), &input_state_provider_);
pixel_limit_resource->SetResourceListener(&resource_listener);
time_controller_.AdvanceTime(TimeDelta::Zero());
pixel_limit_resource->SetMaxPixels(kMaxPixels);
SetCurrentPixels(kMaxPixels + 1);
EXPECT_CALL(resource_listener,
OnResourceUsageStateMeasured(_, ResourceUsageState::kOveruse))
.Times(1);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs);
// As long as the current pixels has not updated, the overuse signal is
// repeated at a fixed interval.
EXPECT_CALL(resource_listener,
OnResourceUsageStateMeasured(_, ResourceUsageState::kOveruse))
.Times(3);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs * 3);
// When the overuse signal has resulted in a lower resolution, the overuse
// signals stops.
SetCurrentPixels(kMaxPixels);
EXPECT_CALL(resource_listener, OnResourceUsageStateMeasured(_, _)).Times(0);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs * 3);
pixel_limit_resource->SetResourceListener(nullptr);
});
}
TEST_F(PixelLimitResourceTest,
UnderuseIsReportedWhileCurrentPixelsIsLessThanMinPixels) {
constexpr int kMaxPixels = 640 * 480;
const int kMinPixels = GetLowerResolutionThan(kMaxPixels);
testing::StrictMock<MockResourceListener> resource_listener;
RunTaskOnTaskQueue([&]() {
rtc::scoped_refptr<PixelLimitResource> pixel_limit_resource =
PixelLimitResource::Create(task_queue_.get(), &input_state_provider_);
pixel_limit_resource->SetResourceListener(&resource_listener);
time_controller_.AdvanceTime(TimeDelta::Zero());
pixel_limit_resource->SetMaxPixels(kMaxPixels);
SetCurrentPixels(kMinPixels - 1);
EXPECT_CALL(resource_listener,
OnResourceUsageStateMeasured(_, ResourceUsageState::kUnderuse))
.Times(1);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs);
// As long as the current pixels has not updated, the underuse signal is
// repeated at a fixed interval.
EXPECT_CALL(resource_listener,
OnResourceUsageStateMeasured(_, ResourceUsageState::kUnderuse))
.Times(3);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs * 3);
// When the underuse signal has resulted in a higher resolution, the
// underuse signals stops.
SetCurrentPixels(kMinPixels);
EXPECT_CALL(resource_listener, OnResourceUsageStateMeasured(_, _)).Times(0);
time_controller_.AdvanceTime(kResourceUsageCheckIntervalMs * 3);
pixel_limit_resource->SetResourceListener(nullptr);
});
}
} // namespace webrtc
|