forked from mozilla/webrtc-landing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgum_test.html
302 lines (274 loc) · 8.68 KB
/
gum_test.html
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
<!DOCTYPE html>
<html>
<head>
<title>gUM Test Page</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" charset="utf-8">
<link href="https://fonts.googleapis.com/css?family=Gentium+Basic:400,700" rel="stylesheet" type="text/css">
<style>
#main {
display: block;
margin: 0px auto;
text-align: center
}
#content {
display: inline-block;
}
#frames {
display: inline-block;
max-width: 180px;
vertical-align: top;
}
#startbuttons {
display: block;
}
#stopbuttons {
display: none;
}
#message {
font-size: 24px;
font-family: "Gentium Basic", serif;
}
h1 {
font-size: 40px;
font-family: "Gentium Basic", serif;
}
input {
font-size: 28px;
font-family: "Gentium Basic", serif;
}
p {
color: green;
}
p.error {
color: red;
}
input[type=number] {
width: 65px;
}
</style>
</head>
<body>
<div id="main">
<h2><b>getUserMedia / getDisplayMedia Test Page</b></h2>
<p><a href="https://mozilla.github.io/webrtc-landing">Main webrtc demo page</a></p>
<div id="gum_warn" style="display: none">
<font color="red">
<h4>getUserMedia is missing!</h4>
This page only works when loaded loaded <a href="https://mozilla.github.io/webrtc-landing/gum_test.html">via HTTPS</a> as getUserMedia() can only be accessed via HTTPS.<br><br>
</font>
</div>
<div id="startbuttons">
<input value="Camera" onclick="startVideo();" type="button">
<input value="Microphone" onclick="startAudio();" type="button">
<input value="Camera & microphone" onclick="startAudioVideo();" type="button">
<input value="Screen capture" onclick="startScreen();" type="button">
<input value="Tab capture" onclick="startTab();" type="button">
<br>
<input id="fps_range" min="0" max="120" value="0" type="range">
<input id="fps_number" min="0" max="120" value="0" type="number">
<label for="fps_number">FPS desired (0 for default)</label>
</div>
<div id="images">
<div id="content"></div>
<div id="controls"></div>
<div id="frames"></div>
<div id="fps"></div>
</div>
<div id="message"></div>
<div style="display: none;" id="stopbuttons">
<input value="Stop" onclick="stopMedia();" type="button">
<input value="Pause/Play" onclick="pauseMedia();" type="button">
<input id="snapshot" value="Snapshot" onclick="startSnapshot();" type="button">
</div>
</div>
<script type="application/javascript">
if (!navigator.mediaDevices.getUserMedia) {
document.getElementById("gum_warn").style.display = "none";
}
const video = document.createElement("video");
video.setAttribute("width", 640);
video.setAttribute("height", 480);
const muted = document.createElement("input");
muted.type = "checkbox";
muted.checked = true;
muted.onclick = () => { video.muted = muted.checked; };
const mute = document.createElement("label");
mute.innerHTML = "Volume mute";
mute.insertBefore(muted, mute.firstChild);
const snapshots = [];
const audio = document.createElement("audio");
audio.setAttribute("controls", true);
const start = document.getElementById("startbuttons");
const stop = document.getElementById("stopbuttons");
const message = document.getElementById("message");
const content = document.getElementById("content");
const frames = document.getElementById("frames");
const snapshot = document.getElementById("snapshot");
const fps = document.getElementById("fps");
const fps_range = document.getElementById("fps_range");
const fps_number = document.getElementById("fps_number");
fps_range.oninput = () => fps_number.value = fps_range.value;
fps_number.oninput = () => fps_range.value = fps_number.value;
let saved_stream = null;
let capturing = false;
let running = false;
let fps_now = 0;
let fps_total = 0;
let last_fps_time;
let first_fps_time;
let last_fps_frames;
let first_fps_frames;
let fps_constraint;
function startScreen() {
startMedia("getDisplayMedia", {video: true});
}
function startTab() {
startMedia("getDisplayMedia",
{
video: { mediaSource: "browser",
scrollWithPage: true },
});
}
function startAudioVideo() {
startMedia("getUserMedia", {video: true, audio: true});
}
function startAudio() {
startMedia("getUserMedia", {audio: true});
}
function startVideo() {
startMedia("getUserMedia", {video: true});
}
function stopMedia() {
if (video.srcObject) {
for (const track of video.srcObject.getTracks()) {
track.stop();
}
video.srcObject = null;
content.removeChild(video);
if (mute.parentNode) {
controls.removeChild(mute);
}
fps.innerHTML = "";
stopbuttons.removeChild(snapshot);
snapshot.value = "Snapshot";
frames.innerHTML = '';
capturing = false;
} else if (audio.srcObject) {
for (const track of audio.srcObject.getTracks()) {
track.stop();
}
audio.srcObject = null;
content.removeChild(audio);
}
saved_stream = null;
stop.style.display = "none";
start.style.display = "block";
running = false;
}
function pauseMedia() {
if (saved_stream) {
if (saved_stream.getVideoTracks().length) {
video.srcObject = saved_stream;
video.play();
} else {
audio.srcObject = saved_stream;
audio.play();
}
saved_stream = null;
} else {
if (video.srcObject) {
video.pause();
saved_stream = video.srcObject;
video.srcObject = null;
} else if (audio.srcObject) {
audio.pause();
saved_stream = audio.srcObject;
audio.srcObject = null;
}
}
}
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
async function startMedia(gum, constraints) {
if (constraints.video) {
const frameRate = +fps_number.value || undefined;
constraints.video = Object.assign(constraints.video, {frameRate});
}
stop.style.display = "block";
start.style.display = "none";
try {
const stream = await navigator.mediaDevices[gum](constraints);
message.innerHTML = "<p>Success!</p>";
if (stream.getVideoTracks().length) {
content.appendChild(video);
if (stream.getAudioTracks().length) {
controls.appendChild(mute);
video.muted = muted.checked;
}
video.srcObject = stream;
video.play();
frames.innerHTML = '';
stopbuttons.appendChild(snapshot);
first_fps_time = last_fps_time = new Date();
first_fps_frames = last_fps_frames = video.mozPaintedFrames;
fps.innerHTML="FPS now: 0.0 , entire capture: 0.0";
setTimeout(get_fps, 2000);
} else {
content.appendChild(audio);
audio.srcObject = stream;
audio.play();
}
running = true;
} catch (err) {
if (location.protocol != "https:") {
message.innerHTML = `<p class='error'>${err}</p>
<p>getUserMedia and getDisplayMedia now require the site be loaded from an https URL</p>
<p>Reloading page in https in 10 seconds</p>`;
await wait(10000);
window.location.href = `https:${window.location.href.substring(window.location.protocol.length)}`;
} else {
message.innerHTML = `<p class='error'>${err}</p>`;
}
stopMedia();
}
}
function get_fps() {
if (running) {
const now = new Date();
const frames = video.mozPaintedFrames;
fps_now = (frames - last_fps_frames)/((now - last_fps_time)/1000);
fps_total = (frames - first_fps_frames)/((now - first_fps_time)/1000);
fps.innerHTML= `FPS now: ${fps_now.toFixed(1)} , entire capture: ${fps_total.toFixed(1)}`;
last_fps_time = now;
last_fps_frames = frames;
setTimeout(get_fps, 1000);
}
}
function startSnapshot() {
capturing = !capturing;
if (capturing) {
captureImage();
snapshot.value = "Stop Snapshot";
} else {
snapshot.value = "Snapshot";
}
}
function captureImage() {
if (video.srcObject && capturing) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = video.videoWidth / 4;
canvas.height = video.videoHeight / 4;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
if (snapshots.unshift(canvas) > 4) {
snapshots.length = 4;
}
frames.innerHTML = '';
for (const snapshot of snapshots) {
frames.appendChild(snapshot);
}
setTimeout(captureImage, 2000);
}
}
</script>
</body>
</html>