/
video-frame.any.js
99 lines (77 loc) · 3.16 KB
/
video-frame.any.js
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
// META: global=window,dedicatedworker
// META: script=/webcodecs/utils.js
test(t => {
let image = makeImageBitmap(32, 16);
let frame = new VideoFrame(image, {timestamp: 10});
assert_equals(frame.timestamp, 10, "timestamp");
assert_equals(frame.duration, null, "duration");
assert_equals(frame.cropWidth, 32, "cropWidth");
assert_equals(frame.cropHeight, 16, "cropHeight");
assert_equals(frame.cropWidth, 32, "displayWidth");
assert_equals(frame.cropHeight, 16, "displayHeight");
frame.close();
}, 'Test we can construct a VideoFrame.');
test(t => {
let image = makeImageBitmap(1, 1);
let frame = new VideoFrame(image, {timestamp: 10});
assert_equals(frame.cropWidth, 1, "cropWidth");
assert_equals(frame.cropHeight, 1, "cropHeight");
assert_equals(frame.cropWidth, 1, "displayWidth");
assert_equals(frame.cropHeight, 1, "displayHeight");
frame.close();
}, 'Test we can construct an odd-sized VideoFrame.');
test(t => {
let image = makeImageBitmap(32, 16);
let frame = new VideoFrame(image, {timestamp: 0});
// TODO(sandersd): This would be more clear as RGBA, but conversion has
// not be specified (or implemented) yet.
if (frame.format !== "I420") {
return;
}
assert_equals(frame.planes.length, 3, "number of planes");
// Validate Y plane metadata.
let yPlane = frame.planes[0];
let yStride = yPlane.stride;
let yRows = yPlane.rows;
let yLength = yPlane.length;
// Required minimums to contain the visible data.
assert_greater_than_equal(yRows, 16, "Y plane rows");
assert_greater_than_equal(yStride, 32, "Y plane stride");
assert_greater_than_equal(yLength, 32 * 16, "Y plane length");
// Not required by spec, but sets limit at 50% padding per dimension.
assert_less_than_equal(yRows, 32, "Y plane rows");
assert_less_than_equal(yStride, 64, "Y plane stride");
assert_less_than_equal(yLength, 32 * 64, "Y plane length");
// Validate Y plane data.
let buffer = new ArrayBuffer(yLength);
let view = new Uint8Array(buffer);
frame.planes[0].readInto(view);
// TODO(sandersd): This probably needs to be fuzzy unless we can make
// guarantees about the color space.
assert_equals(view[0], 94, "Y value at (0, 0)");
frame.close();
}, 'Test we can read planar data from a VideoFrame.');
test(t => {
let image = makeImageBitmap(32, 16);
let frame = new VideoFrame(image, {timestamp: 0});
// TODO(sandersd): This would be more clear as RGBA, but conversion has
// not be specified (or implemented) yet.
if (frame.format !== "I420") {
return;
}
assert_equals(frame.planes.length, 3, "number of planes");
// Attempt to read Y plane data, but close the frame first.
let yPlane = frame.planes[0];
let yLength = yPlane.length;
frame.close();
let buffer = new ArrayBuffer(yLength);
let view = new Uint8Array(buffer);
assert_throws_dom("InvalidStateError", () => yPlane.readInto(view));
}, 'Test we cannot read planar data from a closed VideoFrame.');
test(t => {
let image = makeImageBitmap(32, 16);
image.close();
assert_throws_dom("InvalidStateError", () => {
let frame = new VideoFrame(image, {timestamp: 10});
})
}, 'Test constructing VideoFrames from closed ImageBitmap throws.');