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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
media / mojo / clients / mojo_video_encoder_metrics_provider_unittest.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/mojo/clients/mojo_video_encoder_metrics_provider.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::InSequence;
namespace media {
constexpr uint64_t kEncoderId = 0u;
class MockMojomVideoEncoderMetricsProvider
: public mojom::VideoEncoderMetricsProvider {
public:
MockMojomVideoEncoderMetricsProvider() = default;
~MockMojomVideoEncoderMetricsProvider() override = default;
// mojom::VideoEncoderMetricsProvider implementation.
MOCK_METHOD(void,
Initialize,
(uint64_t,
mojom::VideoEncoderUseCase,
VideoCodecProfile,
const gfx::Size&,
bool,
SVCScalabilityMode),
(override));
MOCK_METHOD(void, SetEncodedFrameCount, (uint64_t, uint64_t), (override));
MOCK_METHOD(void,
SetError,
(uint64_t, const media::EncoderStatus&),
(override));
MOCK_METHOD(void, Complete, (uint64_t), (override));
};
class MojoVideoEncoderMetricsProviderTest : public ::testing::Test {
public:
MojoVideoEncoderMetricsProviderTest() = default;
static constexpr auto kUseCase = mojom::VideoEncoderUseCase::kMediaRecorder;
void SetUp() override {
mojo::PendingRemote<mojom::VideoEncoderMetricsProvider> pending_remote;
mojo_encoder_metrics_receiver_ = mojo::MakeSelfOwnedReceiver(
std::make_unique<MockMojomVideoEncoderMetricsProvider>(),
pending_remote.InitWithNewPipeAndPassReceiver());
mojo_encoder_metrics_provider_factory_ =
base::MakeRefCounted<MojoVideoEncoderMetricsProviderFactory>(
kUseCase, std::move(pending_remote));
mojo_encoder_metrics_provider_ = mojo_encoder_metrics_provider_factory_
->CreateVideoEncoderMetricsProvider();
}
void TearDown() override {
// The destruction of a mojo::SelfOwnedReceiver closes the bound message
// pipe but does not destroy the implementation object(s): this needs to
// happen manually by Close()ing it.
if (mojo_encoder_metrics_receiver_) {
mojo_encoder_metrics_receiver_->Close();
}
}
MockMojomVideoEncoderMetricsProvider* mock_mojo_receiver() {
return reinterpret_cast<MockMojomVideoEncoderMetricsProvider*>(
mojo_encoder_metrics_receiver_->impl());
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
// This member holds on to the mock implementation of the "service" side.
mojo::SelfOwnedReceiverRef<mojom::VideoEncoderMetricsProvider>
mojo_encoder_metrics_receiver_;
scoped_refptr<MojoVideoEncoderMetricsProviderFactory>
mojo_encoder_metrics_provider_factory_;
std::unique_ptr<VideoEncoderMetricsProvider> mojo_encoder_metrics_provider_;
};
TEST_F(MojoVideoEncoderMetricsProviderTest, CreateAndDestroy) {}
TEST_F(MojoVideoEncoderMetricsProviderTest, CreateAndBoundAndInitialize) {
constexpr auto kCodecProfile = media::VP9PROFILE_PROFILE0;
constexpr gfx::Size kEncodeSize(1920, 1080);
constexpr bool kIsHardwareEncoder = true;
constexpr auto kSVCMode = SVCScalabilityMode::kL1T3;
InSequence s;
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(kEncoderId, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
mojo_encoder_metrics_provider_->Initialize(kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode);
EXPECT_CALL(*mock_mojo_receiver(), Complete(kEncoderId));
mojo_encoder_metrics_provider_.reset();
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoVideoEncoderMetricsProviderTest,
CreateAndBoundAndInitializeSetEncodedFrameCount) {
constexpr auto kCodecProfile = media::VP9PROFILE_PROFILE0;
constexpr gfx::Size kEncodeSize(1920, 1080);
constexpr bool kIsHardwareEncoder = true;
constexpr auto kSVCMode = SVCScalabilityMode::kL1T3;
InSequence s;
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(kEncoderId, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
mojo_encoder_metrics_provider_->Initialize(kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode);
base::RunLoop().RunUntilIdle();
EXPECT_CALL(*mock_mojo_receiver(), SetEncodedFrameCount(kEncoderId, 1u));
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
base::RunLoop().RunUntilIdle();
for (size_t i = 0; i < 98; i++) {
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
}
base::RunLoop().RunUntilIdle();
EXPECT_CALL(*mock_mojo_receiver(), SetEncodedFrameCount(kEncoderId, 100u));
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
base::RunLoop().RunUntilIdle();
for (size_t i = 0; i < 99; i++) {
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
}
base::RunLoop().RunUntilIdle();
EXPECT_CALL(*mock_mojo_receiver(), SetEncodedFrameCount(kEncoderId, 200u));
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoVideoEncoderMetricsProviderTest,
CreateAndBoundAndInitializeAndSetError) {
constexpr auto kCodecProfile = media::VP9PROFILE_PROFILE0;
constexpr gfx::Size kEncodeSize(1920, 1080);
constexpr bool kIsHardwareEncoder = true;
constexpr auto kSVCMode = SVCScalabilityMode::kL1T3;
InSequence s;
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(kEncoderId, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
mojo_encoder_metrics_provider_->Initialize(kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode);
base::RunLoop().RunUntilIdle();
EXPECT_CALL(*mock_mojo_receiver(), SetEncodedFrameCount(kEncoderId, 1u));
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
base::RunLoop().RunUntilIdle();
const media::EncoderStatus kErrorStatus(
media::EncoderStatus::Codes::kEncoderFailedEncode, "Encoder failed");
mojo_encoder_metrics_provider_->SetError(kErrorStatus);
EXPECT_CALL(*mock_mojo_receiver(), SetError(kEncoderId, kErrorStatus));
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoVideoEncoderMetricsProviderTest, CreateAndSetError_NoCall) {
InSequence s;
const media::EncoderStatus kErrorStatus(
media::EncoderStatus::Codes::kEncoderFailedEncode, "Encoder failed");
mojo_encoder_metrics_provider_->SetError(kErrorStatus);
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoVideoEncoderMetricsProviderTest,
CreateAndIncrementEncodedFrameCount_NoCall) {
InSequence s;
mojo_encoder_metrics_provider_->IncrementEncodedFrameCount();
base::RunLoop().RunUntilIdle();
}
class MojoVideoEncoderMetricsProviderFactoryTest : public ::testing::Test {
public:
MojoVideoEncoderMetricsProviderFactoryTest() = default;
static constexpr auto kUseCase = mojom::VideoEncoderUseCase::kWebRTC;
void SetUp() override {
mojo::PendingRemote<mojom::VideoEncoderMetricsProvider> pending_remote;
mojo_encoder_metrics_receiver_ = mojo::MakeSelfOwnedReceiver(
std::make_unique<MockMojomVideoEncoderMetricsProvider>(),
pending_remote.InitWithNewPipeAndPassReceiver());
mojo_encoder_metrics_provider_factory_ =
base::MakeRefCounted<MojoVideoEncoderMetricsProviderFactory>(
mojom::VideoEncoderUseCase::kWebRTC, std::move(pending_remote));
}
void TearDown() override {
// The destruction of a mojo::SelfOwnedReceiver closes the bound message
// pipe but does not destroy the implementation object(s): this needs to
// happen manually by Close()ing it.
if (mojo_encoder_metrics_receiver_) {
mojo_encoder_metrics_receiver_->Close();
}
}
MockMojomVideoEncoderMetricsProvider* mock_mojo_receiver() {
return reinterpret_cast<MockMojomVideoEncoderMetricsProvider*>(
mojo_encoder_metrics_receiver_->impl());
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
// This member holds on to the mock implementation of the "service" side.
mojo::SelfOwnedReceiverRef<mojom::VideoEncoderMetricsProvider>
mojo_encoder_metrics_receiver_;
scoped_refptr<MojoVideoEncoderMetricsProviderFactory>
mojo_encoder_metrics_provider_factory_;
};
TEST_F(MojoVideoEncoderMetricsProviderFactoryTest, CreateMultipleEncoders) {
constexpr auto kCodecProfile = media::VP9PROFILE_PROFILE0;
constexpr gfx::Size kEncodeSize(1920, 1080);
constexpr bool kIsHardwareEncoder = true;
constexpr auto kSVCMode = SVCScalabilityMode::kL1T3;
auto metrics_provider0 = mojo_encoder_metrics_provider_factory_
->CreateVideoEncoderMetricsProvider();
auto metrics_provider1 = mojo_encoder_metrics_provider_factory_
->CreateVideoEncoderMetricsProvider();
InSequence s;
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(0u, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(1u, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
metrics_provider0->Initialize(kCodecProfile, kEncodeSize, kIsHardwareEncoder,
kSVCMode);
metrics_provider1->Initialize(kCodecProfile, kEncodeSize, kIsHardwareEncoder,
kSVCMode);
EXPECT_CALL(*mock_mojo_receiver(), Complete(0u));
EXPECT_CALL(*mock_mojo_receiver(), Complete(1u));
metrics_provider0.reset();
metrics_provider1.reset();
base::RunLoop().RunUntilIdle();
}
TEST_F(MojoVideoEncoderMetricsProviderFactoryTest,
CanOperateAfterDestroyingOtherMetricsProvider) {
constexpr auto kCodecProfile = media::VP9PROFILE_PROFILE0;
constexpr gfx::Size kEncodeSize(1920, 1080);
constexpr bool kIsHardwareEncoder = true;
constexpr auto kSVCMode = SVCScalabilityMode::kL1T3;
auto metrics_provider0 = mojo_encoder_metrics_provider_factory_
->CreateVideoEncoderMetricsProvider();
auto metrics_provider1 = mojo_encoder_metrics_provider_factory_
->CreateVideoEncoderMetricsProvider();
InSequence s;
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(0u, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
EXPECT_CALL(*mock_mojo_receiver(),
Initialize(1u, kUseCase, kCodecProfile, kEncodeSize,
kIsHardwareEncoder, kSVCMode));
metrics_provider0->Initialize(kCodecProfile, kEncodeSize, kIsHardwareEncoder,
kSVCMode);
metrics_provider1->Initialize(kCodecProfile, kEncodeSize, kIsHardwareEncoder,
kSVCMode);
EXPECT_CALL(*mock_mojo_receiver(), Complete(0u));
metrics_provider0.reset();
EXPECT_CALL(*mock_mojo_receiver(), SetEncodedFrameCount(1u, 1u));
metrics_provider1->IncrementEncodedFrameCount();
EXPECT_CALL(*mock_mojo_receiver(), Complete(1u));
metrics_provider1.reset();
base::RunLoop().RunUntilIdle();
}
} // namespace media