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
media / gpu / vaapi / vaapi_video_decoder_unittest.cc [blame]
// Copyright 2024 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/vaapi/vaapi_video_decoder.h"
#if !BUILDFLAG(IS_CHROMEOS_ASH)
#error This file should only be built for Ash.
#endif
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/thread_pool.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "chromeos/components/cdm_factory_daemon/chromeos_cdm_context.h"
#include "media/base/media_util.h"
#include "media/base/mock_filters.h"
#include "media/base/mock_media_log.h"
#include "media/base/test_helpers.h"
#include "media/gpu/accelerated_video_decoder.h"
#include "media/gpu/vaapi/vaapi_video_decoder_delegate.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::test::RunClosure;
using ::testing::_;
using ::testing::ByMove;
using testing::InSequence;
using ::testing::Return;
using ::testing::StrictMock;
namespace media {
namespace {
constexpr std::string kEmptyData = "";
constexpr std::string kInvalidData = "ThisIsInvalidData";
constexpr uint8_t kEncodedData[] = {1, 2, 3};
constexpr gfx::Size kCodedSize(128, 128);
VideoDecoderConfig DefaultVideoDecoderConfig() {
const VideoDecoderConfig config(
media::VideoCodec::kVP8, VP8PROFILE_ANY,
VideoDecoderConfig::AlphaMode::kIsOpaque, VideoColorSpace(),
kNoTransformation, kCodedSize, gfx::Rect(kCodedSize), kCodedSize,
EmptyExtraData(), EncryptionScheme::kUnencrypted);
DCHECK(config.IsValidConfig());
return config;
}
} // namespace
MATCHER_P(MatchesStatusCode, status_code, "") {
return arg.code() == status_code;
}
scoped_refptr<DecoderBuffer> CreateDecoderBuffer(
const base::span<const uint8_t>& bitstream) {
scoped_refptr<DecoderBuffer> buffer = DecoderBuffer::CopyFrom(bitstream);
EXPECT_NE(buffer.get(), nullptr);
return buffer;
}
class MockVaapiWrapper : public VaapiWrapper {
public:
explicit MockVaapiWrapper(CodecMode mode)
: VaapiWrapper(VADisplayStateHandle(), mode) {}
private:
~MockVaapiWrapper() override = default;
};
class MockVideoDecoderMixinClient : public VideoDecoderMixin::Client {
public:
MOCK_METHOD(DmabufVideoFramePool*, GetVideoFramePool, (), (const, override));
MOCK_METHOD(void, PrepareChangeResolution, (), (override));
MOCK_METHOD(void, NotifyEstimatedMaxDecodeRequests, (int), (override));
MOCK_METHOD(CroStatus::Or<ImageProcessor::PixelLayoutCandidate>,
PickDecoderOutputFormat,
(const std::vector<ImageProcessor::PixelLayoutCandidate>&,
const gfx::Rect&,
const gfx::Size&,
std::optional<gfx::Size>,
size_t,
bool,
bool,
std::optional<DmabufVideoFramePool::CreateFrameCB>),
(override));
MOCK_METHOD(void, InitCallback, (DecoderStatus), ());
base::WeakPtrFactory<MockVideoDecoderMixinClient> weak_ptr_factory_{this};
};
class MockChromeOsCdmContext : public chromeos::ChromeOsCdmContext {
public:
MockChromeOsCdmContext() : chromeos::ChromeOsCdmContext() {}
~MockChromeOsCdmContext() override = default;
MOCK_METHOD3(GetHwKeyData,
void(const DecryptConfig*,
const std::vector<uint8_t>&,
chromeos::ChromeOsCdmContext::GetHwKeyDataCB));
MOCK_METHOD1(GetHwConfigData,
void(chromeos::ChromeOsCdmContext::GetHwConfigDataCB));
MOCK_METHOD1(GetScreenResolutions,
void(chromeos::ChromeOsCdmContext::GetScreenResolutionsCB));
MOCK_METHOD0(GetCdmContextRef, std::unique_ptr<CdmContextRef>());
MOCK_CONST_METHOD0(UsingArcCdm, bool());
MOCK_CONST_METHOD0(IsRemoteCdm, bool());
MOCK_METHOD2(AllocateSecureBuffer,
void(uint32_t,
chromeos::ChromeOsCdmContext::AllocateSecureBufferCB));
MOCK_METHOD4(ParseEncryptedSliceHeader,
void(uint64_t,
uint32_t,
const std::vector<uint8_t>&,
ParseEncryptedSliceHeaderCB));
};
class FakeCdmContextRef : public CdmContextRef {
public:
FakeCdmContextRef(CdmContext* cdm_context) : cdm_context_(cdm_context) {}
~FakeCdmContextRef() override = default;
CdmContext* GetCdmContext() override { return cdm_context_; }
private:
raw_ptr<CdmContext> cdm_context_;
};
class MockAcceleratedVideoDecoder : public AcceleratedVideoDecoder {
public:
MockAcceleratedVideoDecoder() = default;
~MockAcceleratedVideoDecoder() override = default;
MOCK_METHOD(void, SetStream, (int32_t, const DecoderBuffer&), (override));
MOCK_METHOD(bool, Flush, (), (override));
MOCK_METHOD(void, Reset, (), (override));
MOCK_METHOD(DecodeResult, Decode, (), (override));
MOCK_METHOD(gfx::Size, GetPicSize, (), (const, override));
MOCK_METHOD(gfx::Rect, GetVisibleRect, (), (const, override));
MOCK_METHOD(VideoCodecProfile, GetProfile, (), (const, override));
MOCK_METHOD(uint8_t, GetBitDepth, (), (const, override));
MOCK_METHOD(VideoChromaSampling, GetChromaSampling, (), (const, override));
MOCK_METHOD(VideoColorSpace, GetVideoColorSpace, (), (const, override));
MOCK_METHOD(std::optional<gfx::HDRMetadata>,
GetHDRMetadata,
(),
(const, override));
MOCK_METHOD(size_t, GetRequiredNumOfPictures, (), (const, override));
MOCK_METHOD(size_t, GetNumReferenceFrames, (), (const, override));
};
class VaapiVideoDecoderTest : public ::testing::Test {
public:
VaapiVideoDecoderTest() = default;
~VaapiVideoDecoderTest() override = default;
void SetUp() override {
mock_vaapi_wrapper_ =
base::MakeRefCounted<MockVaapiWrapper>(VaapiWrapper::kDecode);
mock_vaapi_wrapper_->sequence_checker_.DetachFromSequence();
ResetDecoder();
}
void ResetDecoder() {
auto mock_accelerated_video_decoder =
std::make_unique<StrictMock<MockAcceleratedVideoDecoder>>();
mock_accelerated_video_decoder_ = mock_accelerated_video_decoder.get();
decoder_ = VaapiVideoDecoder::Create(
std::make_unique<media::NullMediaLog>(),
base::SequencedTaskRunner::GetCurrentDefault(),
client_.weak_ptr_factory_.GetWeakPtr());
DCHECK_CALLED_ON_VALID_SEQUENCE(vaapi_decoder()->sequence_checker_);
vaapi_decoder()->vaapi_wrapper_ = mock_vaapi_wrapper_;
vaapi_decoder()->decoder_ = std::move(mock_accelerated_video_decoder);
}
void InitializeVaapiVideoDecoder(
const DecoderStatus::Codes& status_code,
const VideoDecoderConfig& config = DefaultVideoDecoderConfig(),
CdmContext* cdm_context = nullptr) {
base::RunLoop run_loop;
EXPECT_CALL(client_, InitCallback(MatchesStatusCode(status_code)))
.WillOnce(RunClosure(run_loop.QuitClosure()));
vaapi_decoder()->Initialize(
config, /*low_delay=*/false, /*cdm_context=*/cdm_context,
base::BindOnce(&MockVideoDecoderMixinClient::InitCallback,
client_.weak_ptr_factory_.GetWeakPtr()),
/*output_cb=*/base::DoNothing(),
/*waiting_cb*/ base::DoNothing());
ASSERT_NE(vaapi_decoder(), nullptr);
DCHECK_CALLED_ON_VALID_SEQUENCE(vaapi_decoder()->sequence_checker_);
ASSERT_NE(vaapi_decoder()->vaapi_wrapper_, nullptr);
ASSERT_NE(vaapi_decoder()->decoder_, nullptr);
run_loop.Run();
testing::Mock::VerifyAndClearExpectations(this);
}
void Decode(scoped_refptr<DecoderBuffer> buffer,
AcceleratedVideoDecoder::DecodeResult mock_decoder_result,
DecoderStatus::Codes vaapi_decoder_status) {
ASSERT_TRUE(mock_accelerated_video_decoder_);
{
InSequence sequence;
EXPECT_CALL(*mock_accelerated_video_decoder_, SetStream(_, _));
EXPECT_CALL(*mock_accelerated_video_decoder_, Decode())
.WillOnce(Return(mock_decoder_result));
EXPECT_CALL(*this,
OnDecodeCompleted(MatchesStatusCode(vaapi_decoder_status)));
}
vaapi_decoder()->Decode(
std::move(buffer),
base::BindOnce(&VaapiVideoDecoderTest::OnDecodeCompleted,
base::Unretained(this)));
task_environment_.RunUntilIdle();
}
VaapiVideoDecoder* vaapi_decoder() {
return reinterpret_cast<VaapiVideoDecoder*>(decoder_.get());
}
MOCK_METHOD(void, OnDecodeCompleted, (DecoderStatus), ());
MOCK_METHOD(void, OnResetDone, (), ());
MockCdmContext cdm_context_;
MockChromeOsCdmContext chromeos_cdm_context_;
media::CallbackRegistry<CdmContext::EventCB::RunType> event_callbacks_;
std::unique_ptr<VideoDecoderMixin> decoder_;
scoped_refptr<MockVaapiWrapper> mock_vaapi_wrapper_;
base::test::SingleThreadTaskEnvironment task_environment_;
MockVideoDecoderMixinClient client_;
raw_ptr<StrictMock<MockAcceleratedVideoDecoder>>
mock_accelerated_video_decoder_ = nullptr;
};
TEST_F(VaapiVideoDecoderTest, Initialize) {
InitializeVaapiVideoDecoder(DecoderStatus::Codes::kOk);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
}
// Verifies that Initialize() fails when trying to decode encrypted content with
// a missing CdmContext.
TEST_F(VaapiVideoDecoderTest,
InitializeFailsDueToMissingCdmContextForEncryptedContent) {
InitializeVaapiVideoDecoder(
DecoderStatus::Codes::kUnsupportedEncryptionMode,
VideoDecoderConfig(VideoCodec::kVP8, VP8PROFILE_ANY,
VideoDecoderConfig::AlphaMode::kIsOpaque,
VideoColorSpace(), kNoTransformation, kCodedSize,
gfx::Rect(kCodedSize), kCodedSize, EmptyExtraData(),
EncryptionScheme::kCenc));
}
// Verifies that Initialize() fails when trying to decode encrypted content with
// VP8 video codec as it's not supported by VA-API.
TEST_F(VaapiVideoDecoderTest, InitializeFailsDueToEncryptedContentForVP8) {
EXPECT_CALL(cdm_context_, GetChromeOsCdmContext())
.WillRepeatedly(Return(&chromeos_cdm_context_));
InitializeVaapiVideoDecoder(
DecoderStatus::Codes::kUnsupportedEncryptionMode,
VideoDecoderConfig(VideoCodec::kVP8, VP8PROFILE_ANY,
VideoDecoderConfig::AlphaMode::kIsOpaque,
VideoColorSpace(), kNoTransformation, kCodedSize,
gfx::Rect(kCodedSize), kCodedSize, EmptyExtraData(),
EncryptionScheme::kCenc),
&cdm_context_);
testing::Mock::VerifyAndClearExpectations(&chromeos_cdm_context_);
testing::Mock::VerifyAndClearExpectations(&cdm_context_);
}
// Verifies that Initialize() succeeds for VP9 encrypted content.
TEST_F(VaapiVideoDecoderTest, InitializeForVP9EncryptedContent) {
EXPECT_CALL(cdm_context_, GetChromeOsCdmContext())
.WillRepeatedly(Return(&chromeos_cdm_context_));
EXPECT_CALL(cdm_context_, RegisterEventCB(_))
.WillOnce([this](CdmContext::EventCB event_cb) {
return event_callbacks_.Register(std::move(event_cb));
});
EXPECT_CALL(chromeos_cdm_context_, GetCdmContextRef())
.WillOnce(
Return(ByMove(std::make_unique<FakeCdmContextRef>(&cdm_context_))));
EXPECT_CALL(chromeos_cdm_context_, IsRemoteCdm()).WillOnce(Return(false));
InitializeVaapiVideoDecoder(
DecoderStatus::Codes::kOk,
VideoDecoderConfig(VideoCodec::kVP9, VP9PROFILE_PROFILE0,
VideoDecoderConfig::AlphaMode::kIsOpaque,
VideoColorSpace(), kNoTransformation, kCodedSize,
gfx::Rect(kCodedSize), kCodedSize, EmptyExtraData(),
EncryptionScheme::kCenc),
&cdm_context_);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
testing::Mock::VerifyAndClearExpectations(&chromeos_cdm_context_);
testing::Mock::VerifyAndClearExpectations(&cdm_context_);
}
TEST_F(VaapiVideoDecoderTest, DecodeSucceeds) {
InitializeVaapiVideoDecoder(DecoderStatus::Codes::kOk);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
auto buffer = CreateDecoderBuffer(base::as_byte_span(kEncodedData));
Decode(buffer, AcceleratedVideoDecoder::DecodeResult::kRanOutOfStreamData,
DecoderStatus::Codes::kOk);
testing::Mock::VerifyAndClearExpectations(mock_accelerated_video_decoder_);
}
TEST_F(VaapiVideoDecoderTest, DecodeFails) {
InitializeVaapiVideoDecoder(DecoderStatus::Codes::kOk);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
auto buffer = CreateDecoderBuffer(base::as_byte_span(kInvalidData));
Decode(buffer, AcceleratedVideoDecoder::DecodeResult::kDecodeError,
DecoderStatus::Codes::kFailed);
testing::Mock::VerifyAndClearExpectations(mock_accelerated_video_decoder_);
}
// Verifies that kConfigChange event can be triggered correctly.
TEST_F(VaapiVideoDecoderTest, ConfigChange) {
InitializeVaapiVideoDecoder(DecoderStatus::Codes::kOk);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
auto buffer = CreateDecoderBuffer(base::as_byte_span(kEmptyData));
Decode(buffer, AcceleratedVideoDecoder::DecodeResult::kRanOutOfStreamData,
DecoderStatus::Codes::kOk);
buffer = CreateDecoderBuffer(kEncodedData);
EXPECT_CALL(client_, PrepareChangeResolution());
Decode(buffer, AcceleratedVideoDecoder::DecodeResult::kConfigChange,
DecoderStatus::Codes::kAborted);
testing::Mock::VerifyAndClearExpectations(mock_accelerated_video_decoder_);
}
// Verifies the Reset sequence.
TEST_F(VaapiVideoDecoderTest, Reset) {
InitializeVaapiVideoDecoder(DecoderStatus::Codes::kOk);
EXPECT_FALSE(vaapi_decoder()->NeedsTranscryption());
base::RunLoop run_loop;
EXPECT_CALL(*mock_accelerated_video_decoder_, Reset());
EXPECT_CALL(*this, OnResetDone())
.WillOnce(RunClosure(run_loop.QuitClosure()));
vaapi_decoder()->Reset(base::BindOnce(&VaapiVideoDecoderTest::OnResetDone,
base::Unretained(this)));
task_environment_.RunUntilIdle();
}
} // namespace media