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
content / test / data / gpu / vc / webgpu_videos_mxn.html [blame]
<!--
Copyright 2021 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>
<head>
<title>WebGpu MxN Video playbacks</title>
<style>
#message {
position: absolute;
width: 1600px;
height: 900px;
top: 200px;
left: 400px;
}
</style>
<script src="../third_party/stats-js/stats.js"></script>
<script src="video_utils.js"></script>
<script src="webgpu_video.js"></script>
<script>
const _defaultRows = 7;
const _defaultColumns = 7;
const _totalVideoWidth = 1600;
const _totalVideoHeight = 900;
let hasUIOnTop = true;
let hasFPSOnTop = true;
let useImportTextureApi = true;
let capUIFPS = true;
let useLargeSizeVideo = false;
let enableBackPressureWorkaround = true;
let codec = 'vp9';
let useLocalCamera = false;
async function startMxNVideos() {
const container = document.getElementById('container');
const p = document.getElementById('message');
// Initialize WebGpu
const gpuSetting = await webGpuInit(_totalVideoWidth, _totalVideoHeight);
if (!gpuSetting) {
p.innerHTML = "Webgpu not supported!" + "<br />" + "<br />" +
"Did you forget to add command line switch --enable-unsafe-webgpu?" +
"<br />" +
"Are you running on the latest Canary or Chromium build?"
return;
}
// Get the UI rendering options from the string.
const uiOption = parsedString['ui'];
if (uiOption === 'none') {
hasUIOnTop = false;
}
const fpsOption = parsedString['fps'];
if (fpsOption === 'none') {
hasFPSOnTop = false;
}
const api = parsedString['import_texture_api']
if (api === '0') {
useImportTextureApi = false;
} else if (api === '1') {
useImportTextureApi = true;
}
codecString = parsedString['codec']
if (codecString === 'vp8') {
codec = 'vp8';
} else if (codecString !== 'vp9' && codecString !== undefined) {
console.warn('Unsupported video codec format! Switch to default VP9.');
}
const capFPSOption = parsedString['cap_ui_fps']
if (capFPSOption === '0') {
capUIFPS = false;
} else if (capFPSOption === '1') {
capUIFPS = true;
}
const useLargeSizeVideoOption = parsedString['use_large_size_video']
if (useLargeSizeVideoOption === '0') {
useLargeSizeVideo = false;
} else if (useLargeSizeVideoOption === '1') {
useLargeSizeVideo = true;
}
const backPressureWorkaroundOption = parsedString['back_pressure_workaround']
if (backPressureWorkaroundOption === '0') {
enableBackPressureWorkaround = false;
} else if (backPressureWorkaroundOption === '1') {
enableBackPressureWorkaround = true;
}
const useLocalCameraOption = parsedString['use_local_camera'];
if (useLocalCameraOption === '0') {
useLocalCamera = false;
} else if (useLocalCameraOption === '1') {
useLocalCamera = true;
}
// Get the number of video rows and columns from the string.
let videoRows = parsedString['rows'];
let videoColumns = parsedString['columns'];
if (videoRows === undefined) {
videoRows = _defaultRows;
}
if (videoColumns === undefined) {
videoColumns = _defaultColumns;
}
// Limite the number of videos to 20x20.
// The video will not load when the number is too big.
const maxColRow = Math.max(videoRows, videoColumns);
if (maxColRow > 20) {
p.innerHTML = "Cannot support videos more than 20 x 20 !" + "<br />" +
"Please change the number of rows/columns.";
return;
}
// Calculate the video onscreen size
const videoWidth = _totalVideoWidth / maxColRow;
const videoHeight = _totalVideoHeight / maxColRow;
// Create MxN videos and a small video (size = 89x50) at the upper right
// corner to similate the one from the local camera.
p.innerHTML = "Uploading videos...";
const videos = [];
const videoCount = videoRows * videoColumns;
for (let i = 0; i < videoCount + 1; i++) {
const video = document.createElement('video');
video.id = i;
video.loop = true;
video.autoplay = true;
video.muted = true;
video.display = "none";
video.src = GetVideoSource(videoCount, i, codec, useLargeSizeVideo);
video.width = videoWidth;
video.height = videoHeight;
video.crossorigin = "anonymous";
videos.push(video);
}
// For the small video at the upper right corner.
videos[videoRows * videoColumns].width = videoWidth / 3;
videos[videoRows * videoColumns].height = videoHeight / 3;
if (useLocalCamera) {
const video = videos[videoRows * videoColumns];
video.src = '';
const constraints = {
audio: false,
video: { width: 640, height: 360 }
};
await navigator.mediaDevices.getUserMedia(constraints)
.then((mediaStream) => {
video.srcObject = mediaStream;
video.onloadedmetadata = () => {
video.play();
};
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
} else {
// Use the 640x360 source to simulate the local camera.
videos[videoRows * videoColumns].src = GetVideoSource(1, 1, codec);
}
await Promise.all(videos.map(video => video.play()));
p.remove();
// Simulate video playback by WebGpu rendering.
webGpuDrawVideoFrames(gpuSetting, videos, videoRows, videoColumns,
hasUIOnTop, hasFPSOnTop, useImportTextureApi, capUIFPS, enableBackPressureWorkaround);
}
</script>
</head>
<body>
<div id="container" style="position:absolute; top:0px; left:0px">
<p id="message"></p>
</div>
<script>startMxNVideos();</script>
</body>
</html>