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
404
media / mojo / mojom / video_frame_mojom_traits_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/mojo/mojom/video_frame_mojom_traits.h"
#include <algorithm>
#include <array>
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/memory/writable_shared_memory_region.h"
#include "base/numerics/safe_conversions.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/mailbox_holder.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "media/base/color_plane_layout.h"
#include "media/base/video_frame.h"
#include "media/base/video_frame_layout.h"
#include "media/mojo/mojom/traits_test_service.test-mojom.h"
#include "media/video/fake_gpu_memory_buffer.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/system/buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
namespace media {
namespace {
class VideoFrameStructTraitsTest : public testing::Test,
public media::mojom::TraitsTestService {
public:
VideoFrameStructTraitsTest() = default;
VideoFrameStructTraitsTest(const VideoFrameStructTraitsTest&) = delete;
VideoFrameStructTraitsTest& operator=(const VideoFrameStructTraitsTest&) =
delete;
protected:
mojo::Remote<mojom::TraitsTestService> GetTraitsTestRemote() {
mojo::Remote<mojom::TraitsTestService> remote;
traits_test_receivers_.Add(this, remote.BindNewPipeAndPassReceiver());
return remote;
}
bool RoundTrip(scoped_refptr<VideoFrame>* frame) {
scoped_refptr<VideoFrame> input = std::move(*frame);
mojo::Remote<mojom::TraitsTestService> remote = GetTraitsTestRemote();
return remote->EchoVideoFrame(std::move(input), frame);
}
private:
void EchoVideoFrame(const scoped_refptr<VideoFrame>& f,
EchoVideoFrameCallback callback) override {
// Touch all data in the received frame to ensure that it is valid.
if (f && f->IsMappable()) {
base::MD5Context md5_context;
base::MD5Init(&md5_context);
VideoFrame::HashFrameForTesting(&md5_context, *f);
}
std::move(callback).Run(f);
}
base::test::TaskEnvironment task_environment_;
mojo::ReceiverSet<TraitsTestService> traits_test_receivers_;
};
} // namespace
TEST_F(VideoFrameStructTraitsTest, Null) {
scoped_refptr<VideoFrame> frame;
ASSERT_TRUE(RoundTrip(&frame));
EXPECT_FALSE(frame);
}
TEST_F(VideoFrameStructTraitsTest, EOS) {
scoped_refptr<VideoFrame> frame = VideoFrame::CreateEOSFrame();
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
EXPECT_TRUE(frame->metadata().end_of_stream);
}
TEST_F(VideoFrameStructTraitsTest, MappableVideoFrame) {
constexpr VideoFrame::StorageType storage_types[] = {
VideoFrame::STORAGE_SHMEM,
VideoFrame::STORAGE_OWNED_MEMORY,
VideoFrame::STORAGE_UNOWNED_MEMORY,
};
constexpr VideoPixelFormat formats[] = {PIXEL_FORMAT_I420, PIXEL_FORMAT_NV12};
constexpr gfx::Size kCodedSize(100, 100);
constexpr gfx::Rect kVisibleRect(kCodedSize);
constexpr gfx::Size kNaturalSize = kCodedSize;
constexpr double kFrameRate = 42.0;
constexpr base::TimeDelta kTimestamp = base::Seconds(100);
for (auto format : formats) {
for (auto storage_type : storage_types) {
scoped_refptr<media::VideoFrame> frame;
base::MappedReadOnlyRegion region;
if (storage_type == VideoFrame::STORAGE_OWNED_MEMORY) {
frame = media::VideoFrame::CreateFrame(format, kCodedSize, kVisibleRect,
kNaturalSize, kTimestamp);
} else {
std::vector<size_t> strides =
VideoFrame::ComputeStrides(format, kCodedSize);
size_t aggregate_size = 0;
std::array<size_t, 3> sizes = {};
for (size_t i = 0; i < strides.size(); ++i) {
sizes[i] = media::VideoFrame::Rows(i, format, kCodedSize.height()) *
strides[i];
aggregate_size += sizes[i];
}
region = base::ReadOnlySharedMemoryRegion::Create(aggregate_size);
ASSERT_TRUE(region.IsValid());
std::array<uint8_t*, 3> data = {};
data[0] = const_cast<uint8_t*>(region.mapping.GetMemoryAs<uint8_t>());
for (size_t i = 1; i < strides.size(); ++i) {
data[i] = data[i - 1] + sizes[i - 1];
}
if (format == PIXEL_FORMAT_I420) {
frame = media::VideoFrame::WrapExternalYuvData(
format, kCodedSize, kVisibleRect, kNaturalSize, strides[0],
strides[1], strides[2], data[0], data[1], data[2], kTimestamp);
} else {
frame = media::VideoFrame::WrapExternalYuvData(
format, kCodedSize, kVisibleRect, kNaturalSize, strides[0],
strides[1], data[0], data[1], kTimestamp);
}
if (storage_type == VideoFrame::STORAGE_SHMEM)
frame->BackWithSharedMemory(®ion.region);
}
ASSERT_TRUE(frame);
frame->metadata().frame_rate = kFrameRate;
ASSERT_EQ(frame->storage_type(), storage_type);
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
EXPECT_FALSE(frame->metadata().end_of_stream);
EXPECT_EQ(frame->format(), format);
EXPECT_EQ(*frame->metadata().frame_rate, kFrameRate);
EXPECT_EQ(frame->coded_size(), kCodedSize);
EXPECT_EQ(frame->visible_rect(), kVisibleRect);
EXPECT_EQ(frame->natural_size(), kNaturalSize);
EXPECT_EQ(frame->timestamp(), kTimestamp);
ASSERT_EQ(frame->storage_type(), VideoFrame::STORAGE_SHMEM);
EXPECT_TRUE(frame->shm_region()->IsValid());
}
}
}
TEST_F(VideoFrameStructTraitsTest, InterleavedPlanes) {
constexpr VideoFrame::StorageType storage_type = VideoFrame::STORAGE_SHMEM;
constexpr VideoPixelFormat format = PIXEL_FORMAT_I420;
constexpr gfx::Size kCodedSize(100, 100);
constexpr gfx::Rect kVisibleRect(kCodedSize);
constexpr gfx::Size kNaturalSize = kCodedSize;
constexpr base::TimeDelta kTimestamp;
scoped_refptr<media::VideoFrame> frame;
std::vector<size_t> strides = VideoFrame::ComputeStrides(format, kCodedSize);
ASSERT_EQ(strides[1], strides[2]);
size_t aggregate_size = 0;
std::array<size_t, 3> sizes = {};
for (size_t i = 0; i < strides.size(); ++i) {
sizes[i] =
media::VideoFrame::Rows(i, format, kCodedSize.height()) * strides[i];
aggregate_size += sizes[i];
}
auto region = base::WritableSharedMemoryRegion::Create(aggregate_size);
ASSERT_TRUE(region.IsValid());
auto mapping = region.MapAt(0, aggregate_size);
auto [y_plane, uv_plane] =
mapping.GetMemoryAsSpan<uint8_t>().split_at(sizes[0]);
std::ranges::fill(y_plane, 1);
// Setup memory layout where U and V planes occupy the same space, but have
// interleaving U and V rows. This is achieved by doubling the stride.
size_t normal_stride = strides[1];
size_t uv_stride = normal_stride * 2;
int yu_rows = media::VideoFrame::Rows(1, format, kCodedSize.height());
auto uv_plane2 = uv_plane; // Loop below is destructive.
for (int i = 0; i < yu_rows; ++i) {
const auto [u, v] = uv_plane2.take_first(uv_stride).split_at(normal_stride);
std::ranges::fill(u, 2);
std::ranges::fill(v, 3);
}
frame = media::VideoFrame::WrapExternalYuvData(
format, kCodedSize, kVisibleRect, kNaturalSize, strides[0], uv_stride,
uv_stride, y_plane.data(), uv_plane.data(),
uv_plane.subspan(normal_stride).data(), kTimestamp);
auto ro_region =
base::WritableSharedMemoryRegion::ConvertToReadOnly(std::move(region));
frame->BackWithSharedMemory(&ro_region);
EXPECT_TRUE(frame);
EXPECT_EQ(frame->storage_type(), storage_type);
EXPECT_TRUE(RoundTrip(&frame));
EXPECT_TRUE(frame);
EXPECT_EQ(frame->format(), format);
EXPECT_EQ(frame->coded_size(), kCodedSize);
auto plane_1 = frame->GetVisiblePlaneData(1);
auto plane_2 = frame->GetVisiblePlaneData(2);
// Bytes between the visible edge and the full stride are not considered part
// of the visible plane, and may not be accessible through the above spans.
const size_t row_bytes_1 =
VideoFrame::RowBytes(1, format, kCodedSize.width());
const size_t row_bytes_2 =
VideoFrame::RowBytes(2, format, kCodedSize.width());
for (int i = 0; i < yu_rows; ++i) {
const auto [u, v] = uv_plane.take_first(uv_stride).split_at(normal_stride);
EXPECT_EQ(plane_1.subspan(i * frame->stride(1), row_bytes_1),
u.first(row_bytes_1));
EXPECT_EQ(plane_2.subspan(i * frame->stride(2), row_bytes_2),
v.first(row_bytes_2));
}
}
TEST_F(VideoFrameStructTraitsTest, InvalidOffsets) {
constexpr auto kFormat = PIXEL_FORMAT_I420;
// This test works by patching the outgoing mojo message, so choose a size
// that's two primes to try and maximize the uniqueness of the values we're
// scanning for in the message.
constexpr gfx::Size kSize(127, 149);
auto strides = VideoFrame::ComputeStrides(kFormat, kSize);
size_t aggregate_size = 0;
std::array<size_t, 3> sizes = {};
for (size_t i = 0; i < strides.size(); ++i) {
sizes[i] = VideoFrame::Rows(i, kFormat, kSize.height()) * strides[i];
aggregate_size += sizes[i];
}
auto region = base::ReadOnlySharedMemoryRegion::Create(aggregate_size);
ASSERT_TRUE(region.IsValid());
std::array<uint8_t*, 3> data = {};
data[0] = const_cast<uint8_t*>(region.mapping.GetMemoryAs<uint8_t>());
for (size_t i = 1; i < strides.size(); ++i) {
data[i] = data[i - 1] + sizes[i];
}
auto frame = VideoFrame::WrapExternalYuvData(
kFormat, kSize, gfx::Rect(kSize), kSize, strides[0], strides[1],
strides[2], data[0], data[1], data[2], base::Seconds(1));
ASSERT_TRUE(frame);
frame->BackWithSharedMemory(®ion.region);
auto message = mojom::VideoFrame::SerializeAsMessage(&frame);
// Scan for the offsets array in the message body. It will start with an
// array header and then have the three offsets matching our frame.
base::span<uint32_t> body(
reinterpret_cast<uint32_t*>(message.mutable_payload()),
message.payload_num_bytes() / sizeof(uint32_t));
std::vector<uint32_t> offsets = {
static_cast<uint32_t>(data[0] - data[0]), // offsets[0]
static_cast<uint32_t>(data[1] - data[0]), // offsets[1]
static_cast<uint32_t>(data[2] - data[0]), // offsets[2]
};
bool patched_offsets = false;
for (size_t i = 0; i + 3 < body.size(); ++i) {
if (body[i] == offsets[0] && body[i + 1] == offsets[1] &&
body[i + 2] == offsets[2]) {
body[i + 1] = 0xc01db33f;
patched_offsets = true;
break;
}
}
ASSERT_TRUE(patched_offsets);
// Required to pass base deserialize checks.
mojo::ScopedMessageHandle handle = message.TakeMojoMessage();
message = mojo::Message::CreateFromMessageHandle(&handle);
// Ensure deserialization fails instead of crashing.
scoped_refptr<VideoFrame> new_frame;
EXPECT_FALSE(mojom::VideoFrame::DeserializeFromMessage(std::move(message),
&new_frame));
}
TEST_F(VideoFrameStructTraitsTest, HoleVideoFrame) {
base::UnguessableToken overlay_plane_id = base::UnguessableToken::Create();
scoped_refptr<VideoFrame> frame = VideoFrame::CreateVideoHoleFrame(
overlay_plane_id, gfx::Size(200, 100), base::Seconds(100));
// Saves the VideoFrame metadata from the created frame. The test should not
// assume these have any particular value.
const VideoFrame::StorageType storage_type = frame->storage_type();
const VideoPixelFormat format = frame->format();
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
EXPECT_FALSE(frame->metadata().end_of_stream);
EXPECT_EQ(frame->storage_type(), storage_type);
EXPECT_EQ(frame->format(), format);
EXPECT_EQ(frame->natural_size(), gfx::Size(200, 100));
EXPECT_EQ(frame->timestamp(), base::Seconds(100));
ASSERT_TRUE(frame->metadata().tracking_token.has_value());
ASSERT_EQ(*frame->metadata().tracking_token, overlay_plane_id);
}
TEST_F(VideoFrameStructTraitsTest, TrackingTokenVideoFrame) {
base::UnguessableToken tracking_token = base::UnguessableToken::Create();
scoped_refptr<VideoFrame> frame = VideoFrame::WrapTrackingToken(
PIXEL_FORMAT_ARGB, tracking_token, gfx::Size(100, 100),
gfx::Rect(10, 10, 80, 80), gfx::Size(200, 100), base::Seconds(100));
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
EXPECT_FALSE(frame->metadata().end_of_stream);
EXPECT_EQ(frame->format(), PIXEL_FORMAT_ARGB);
EXPECT_EQ(frame->coded_size(), gfx::Size(100, 100));
EXPECT_EQ(frame->visible_rect(), gfx::Rect(10, 10, 80, 80));
EXPECT_EQ(frame->natural_size(), gfx::Size(200, 100));
EXPECT_EQ(frame->timestamp(), base::Seconds(100));
ASSERT_TRUE(frame->metadata().tracking_token.has_value());
ASSERT_EQ(*frame->metadata().tracking_token, tracking_token);
}
TEST_F(VideoFrameStructTraitsTest, SharedImageVideoFrame) {
scoped_refptr<gpu::ClientSharedImage> shared_image =
gpu::ClientSharedImage::CreateForTesting();
scoped_refptr<VideoFrame> frame = VideoFrame::WrapSharedImage(
PIXEL_FORMAT_ARGB, shared_image, gpu::SyncToken(),
VideoFrame::ReleaseMailboxCB(), gfx::Size(100, 100),
gfx::Rect(10, 10, 80, 80), gfx::Size(200, 100), base::Seconds(100));
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
EXPECT_FALSE(frame->metadata().end_of_stream);
EXPECT_EQ(frame->format(), PIXEL_FORMAT_ARGB);
EXPECT_EQ(frame->coded_size(), gfx::Size(100, 100));
EXPECT_EQ(frame->visible_rect(), gfx::Rect(10, 10, 80, 80));
EXPECT_EQ(frame->natural_size(), gfx::Size(200, 100));
EXPECT_EQ(frame->timestamp(), base::Seconds(100));
ASSERT_TRUE(frame->HasSharedImage());
ASSERT_EQ(frame->shared_image()->mailbox(), shared_image->mailbox());
}
// BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) because
// media::FakeGpuMemoryBuffer supports NativePixmapHandle backed
// GpuMemoryBufferHandle only. !BUILDFLAG(IS_OZONE) so as to force
// GpuMemoryBufferSupport to select gfx::ClientNativePixmapFactoryDmabuf for
// gfx::ClientNativePixmapFactory.
// TODO(crbug.com/40286368): Allow this test without !BUILDFLAG(IS_OZONE)
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !BUILDFLAG(IS_OZONE)
TEST_F(VideoFrameStructTraitsTest, GpuMemoryBufferSharedImageVideoFrame) {
gfx::Size coded_size = gfx::Size(256, 256);
gfx::Rect visible_rect(coded_size);
auto timestamp = base::Milliseconds(1);
std::unique_ptr<gfx::GpuMemoryBuffer> gmb =
std::make_unique<FakeGpuMemoryBuffer>(
coded_size, gfx::BufferFormat::YUV_420_BIPLANAR);
gfx::BufferFormat expected_gmb_format = gmb->GetFormat();
gfx::Size expected_gmb_size = gmb->GetSize();
scoped_refptr<gpu::ClientSharedImage> shared_image =
gpu::ClientSharedImage::CreateForTesting();
auto frame = VideoFrame::WrapExternalGpuMemoryBuffer(
visible_rect, visible_rect.size(), std::move(gmb), shared_image,
gpu::SyncToken(), base::NullCallback(), timestamp);
ASSERT_TRUE(RoundTrip(&frame));
ASSERT_TRUE(frame);
ASSERT_EQ(frame->storage_type(), VideoFrame::STORAGE_GPU_MEMORY_BUFFER);
EXPECT_TRUE(frame->HasMappableGpuBuffer());
EXPECT_FALSE(frame->metadata().end_of_stream);
EXPECT_EQ(frame->format(), PIXEL_FORMAT_NV12);
EXPECT_EQ(frame->coded_size(), coded_size);
EXPECT_EQ(frame->visible_rect(), visible_rect);
EXPECT_EQ(frame->natural_size(), visible_rect.size());
EXPECT_EQ(frame->timestamp(), timestamp);
ASSERT_TRUE(frame->HasSharedImage());
EXPECT_EQ(frame->mailbox_holder(0).mailbox, shared_image->mailbox());
EXPECT_EQ(frame->GetGpuMemoryBufferForTesting()->GetFormat(),
expected_gmb_format);
EXPECT_EQ(frame->GetGpuMemoryBufferForTesting()->GetSize(),
expected_gmb_size);
}
#endif // (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) &&
// !BUILDFLAG(IS_OZONE)
} // namespace media