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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
media / gpu / android / codec_allocator_unittest.cc [blame]
// Copyright 2016 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/gpu/android/codec_allocator.h"
#include <stdint.h>
#include <memory>
#include "base/android/build_info.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "media/base/android/mock_media_codec_bridge.h"
#include "media/base/subsample_entry.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Invoke;
using testing::NiceMock;
using testing::ReturnRef;
namespace media {
class CodecAllocatorTest : public testing::Test {
public:
CodecAllocatorTest() : allocator_thread_("AllocatorThread") {
// Don't start the clock at null.
tick_clock_.Advance(base::Seconds(1));
allocator_ = new CodecAllocator(
base::BindRepeating(&MockMediaCodecBridge::CreateVideoDecoder),
base::SequencedTaskRunner::GetCurrentDefault());
allocator_->tick_clock_ = &tick_clock_;
}
CodecAllocatorTest(const CodecAllocatorTest&) = delete;
CodecAllocatorTest& operator=(const CodecAllocatorTest&) = delete;
~CodecAllocatorTest() override {
if (allocator_thread_.IsRunning()) {
// Don't leave any threads hung, or this will hang too. It would be nice
// if we could let a unique ptr handle this, but the destructor is
// private. We also have to destroy it on the right thread.
allocator_thread_.task_runner()->PostTask(
FROM_HERE,
base::BindOnce([](CodecAllocator* allocator) { delete allocator; },
allocator_));
allocator_thread_.Stop();
return;
}
delete allocator_;
}
void CreateAllocatorOnAnotherThread() {
delete allocator_;
// Start a thread for the allocator. This would normally be the GPU main
// thread.
CHECK(allocator_thread_.Start());
allocator_ = new CodecAllocator(
base::BindRepeating(&MockMediaCodecBridge::CreateVideoDecoder),
allocator_thread_.task_runner());
allocator_->tick_clock_ = &tick_clock_;
}
void OnCodecCreatedInternal(base::OnceClosure quit_closure,
std::unique_ptr<MediaCodecBridge> codec) {
// This should always be called on the main thread, despite whatever thread
// the allocator happens to be running on.
ASSERT_TRUE(
task_environment_.GetMainThreadTaskRunner()->BelongsToCurrentThread());
last_created_codec_.reset(
reinterpret_cast<MockMediaCodecBridge*>(codec.release()));
OnCodecCreated(last_created_codec_->GetCodecType());
std::move(quit_closure).Run();
}
void OnCodecReleasedInternal(base::OnceClosure quit_closure) {
// This should always be called on the main thread, despite whatever thread
// the allocator happens to be running on.
ASSERT_TRUE(
task_environment_.GetMainThreadTaskRunner()->BelongsToCurrentThread());
OnCodecReleased();
std::move(quit_closure).Run();
}
bool IsPrimaryTaskRunnerLikelyHung() const {
CHECK(!allocator_thread_.IsRunning());
return allocator_->IsPrimaryTaskRunnerLikelyHung();
}
void VerifyOnPrimaryTaskRunner() {
ASSERT_TRUE(allocator_->primary_task_runner_->RunsTasksInCurrentSequence());
}
void VerifyOnSecondaryTaskRunner() {
ASSERT_TRUE(
allocator_->secondary_task_runner_->RunsTasksInCurrentSequence());
}
MOCK_METHOD1(OnCodecCreated, void(CodecType));
MOCK_METHOD0(OnCodecReleased, void());
// Allocate and return a config that allows any codec, and is suitable for
// hardware decode.
std::unique_ptr<VideoCodecConfig> CreateConfig() {
auto config = std::make_unique<VideoCodecConfig>();
config->codec_type = CodecType::kAny;
config->initial_expected_coded_size =
CodecAllocator::kMinHardwareResolution;
return config;
}
protected:
// So that we can get the thread's task runner.
base::test::TaskEnvironment task_environment_;
base::Thread allocator_thread_;
// The test params for |allocator_|.
base::SimpleTestTickClock tick_clock_;
// Allocators that we own. They are not unique_ptrs because the destructor is
// private and they need to be destructed on the right thread.
raw_ptr<CodecAllocator> allocator_ = nullptr;
std::unique_ptr<MockMediaCodecBridge> last_created_codec_;
};
TEST_F(CodecAllocatorTest, NormalCreation) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
auto config = CreateConfig();
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kAny));
run_loop.Run();
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, NormalSecureCreation) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
auto config = CreateConfig();
config->codec_type = CodecType::kSecure;
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kSecure));
run_loop.Run();
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, MultipleCreation) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
auto config = CreateConfig();
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), base::DoNothing()),
std::move(config));
// Advance some time, but not enough to trigger hang detection.
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
tick_clock_.Advance(base::Milliseconds(400));
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
auto config_secure = CreateConfig();
config_secure->codec_type = CodecType::kSecure;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config_secure));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kAny));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kSecure));
run_loop.Run();
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, MultipleRelease) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
base::RunLoop run_loop;
allocator_->ReleaseMediaCodec(
std::make_unique<MockMediaCodecBridge>(),
base::BindOnce(&CodecAllocatorTest::OnCodecReleasedInternal,
base::Unretained(this), base::DoNothing()));
// Advance some time, but not enough to trigger hang detection.
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
tick_clock_.Advance(base::Milliseconds(400));
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
allocator_->ReleaseMediaCodec(
std::make_unique<MockMediaCodecBridge>(),
base::BindOnce(&CodecAllocatorTest::OnCodecReleasedInternal,
base::Unretained(this), run_loop.QuitClosure()));
EXPECT_CALL(*this, OnCodecReleased()).Times(2);
run_loop.Run();
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, StalledReleaseCountsAsHung) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
// Release null codec, but don't pump message loop.
allocator_->ReleaseMediaCodec(std::make_unique<MockMediaCodecBridge>(),
base::DoNothing());
tick_clock_.Advance(base::Seconds(1));
ASSERT_TRUE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, StalledCreateCountsAsHung) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
// Create codec, but don't pump message loop.
auto config = CreateConfig();
config->codec_type = CodecType::kSecure;
allocator_->CreateMediaCodecAsync(base::DoNothing(), std::move(config));
tick_clock_.Advance(base::Seconds(1));
ASSERT_TRUE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, SecureCreationFailsWhenHung) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
// Release null codec, but don't pump message loop.
allocator_->ReleaseMediaCodec(std::make_unique<MockMediaCodecBridge>(),
base::DoNothing());
tick_clock_.Advance(base::Seconds(1));
ASSERT_TRUE(IsPrimaryTaskRunnerLikelyHung());
// Secure creation should fail since we're now using software codecs.
auto config = CreateConfig();
config->codec_type = CodecType::kSecure;
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(
[](base::OnceClosure quit_closure,
std::unique_ptr<MediaCodecBridge> codec) {
ASSERT_FALSE(codec);
std::move(quit_closure).Run();
},
run_loop.QuitClosure()),
std::move(config));
run_loop.Run();
// QuitClosure may run before the initial release processes, so RunUntilIdle
// here such that hung status is cleared.
task_environment_.RunUntilIdle();
// Running the loop should clear hung status.
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
TEST_F(CodecAllocatorTest, SoftwareCodecUsedWhenHung) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
// Release null codec, but don't pump message loop.
allocator_->ReleaseMediaCodec(std::make_unique<MockMediaCodecBridge>(),
base::DoNothing());
tick_clock_.Advance(base::Seconds(1));
ASSERT_TRUE(IsPrimaryTaskRunnerLikelyHung());
// Creation should fall back to software.
auto config = CreateConfig();
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kSoftware));
run_loop.Run();
// QuitClosure may run before the initial release processes, so RunUntilIdle
// here such that hung status is cleared.
task_environment_.RunUntilIdle();
// Running the loop should clear hung status.
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
// Verifies that software codecs are released on the secondary task runner when
// hung and that non-sw codecs are always released on the primary task runner.
TEST_F(CodecAllocatorTest, CodecReleasedOnRightTaskRunnerWhenHung) {
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
// Release null codec, but don't pump message loop.
allocator_->ReleaseMediaCodec(std::make_unique<MockMediaCodecBridge>(),
base::DoNothing());
tick_clock_.Advance(base::Seconds(1));
ASSERT_TRUE(IsPrimaryTaskRunnerLikelyHung());
// Release software codec, ensure it runs on secondary task runner.
auto config = CreateConfig();
config->codec_type = CodecType::kSoftware;
auto sw_codec = MockMediaCodecBridge::CreateVideoDecoder(*config);
reinterpret_cast<MockMediaCodecBridge*>(sw_codec.get())
->destruction_cb.ReplaceClosure(
base::BindOnce(&CodecAllocatorTest::VerifyOnSecondaryTaskRunner,
base::Unretained(this)));
allocator_->ReleaseMediaCodec(std::move(sw_codec), base::DoNothing());
// Release hardware codec, ensure it runs on primary task runner.
config->codec_type = CodecType::kAny;
auto hw_codec = MockMediaCodecBridge::CreateVideoDecoder(*config);
reinterpret_cast<MockMediaCodecBridge*>(hw_codec.get())
->destruction_cb.ReplaceClosure(
base::BindOnce(&CodecAllocatorTest::VerifyOnPrimaryTaskRunner,
base::Unretained(this)));
allocator_->ReleaseMediaCodec(std::move(hw_codec), base::DoNothing());
// Release secure (hardware) codec, ensure it runs on primary task runner.
config->codec_type = CodecType::kSecure;
auto secure_codec = MockMediaCodecBridge::CreateVideoDecoder(*config);
reinterpret_cast<MockMediaCodecBridge*>(secure_codec.get())
->destruction_cb.ReplaceClosure(
base::BindOnce(&CodecAllocatorTest::VerifyOnPrimaryTaskRunner,
base::Unretained(this)));
allocator_->ReleaseMediaCodec(std::move(secure_codec), base::DoNothing());
// QuitClosure may run before the initial release processes, so RunUntilIdle
// here such that hung status is cleared.
task_environment_.RunUntilIdle();
// Running the loop should clear hung status.
ASSERT_FALSE(IsPrimaryTaskRunnerLikelyHung());
}
// Make sure that allocating / freeing a codec on the allocator's thread
// completes, and doesn't DCHECK.
TEST_F(CodecAllocatorTest, AllocateAndDestroyCodecOnAllocatorThread) {
CreateAllocatorOnAnotherThread();
{
base::RunLoop run_loop;
auto config = CreateConfig();
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kAny));
run_loop.Run();
}
{
base::RunLoop run_loop;
allocator_->ReleaseMediaCodec(
std::move(last_created_codec_),
base::BindOnce(&CodecAllocatorTest::OnCodecReleasedInternal,
base::Unretained(this), run_loop.QuitClosure()));
EXPECT_CALL(*this, OnCodecReleased());
run_loop.Run();
}
}
TEST_F(CodecAllocatorTest, LowResolutionGetsSoftware) {
auto config = CreateConfig();
config->initial_expected_coded_size =
CodecAllocator::kMinHardwareResolution - gfx::Size(1, 1);
base::RunLoop run_loop;
allocator_->CreateMediaCodecAsync(
base::BindOnce(&CodecAllocatorTest::OnCodecCreatedInternal,
base::Unretained(this), run_loop.QuitClosure()),
std::move(config));
EXPECT_CALL(*this, OnCodecCreated(CodecType::kSoftware));
run_loop.Run();
}
} // namespace media