-
Notifications
You must be signed in to change notification settings - Fork 0
/
gum_several.html
125 lines (111 loc) · 3.22 KB
/
gum_several.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
<html>
<meta charset=utf-8>
<div style="display:inline-block;">
<video id="video1" autoplay controls style="width:160px;height:120px;"></video>
<br/>
<button id="start1Btn" onclick="start1()">Request Cam 1</button>
<br/>
<button id="stop1Btn" onclick="stop1()" disabled>Stop Cam 1</button>
</div>
<div style="display:inline-block;">
<video id="video2" autoplay controls style="width:160px;height:120px;"></video>
<br/>
<button id="start2Btn" onclick="start2()">Request Cam 2</button>
<br/>
<button id="stop2Btn" onclick="stop2()" disabled>Stop Cam 2</button>
</div>
<div style="display:inline-block;">
<video id="video3" autoplay controls style="width:160px;height:120px;"></video>
<br/>
<button id="start3Btn" onclick="start3()">Request any</button>
<br/>
<button id="stop3Btn" onclick="stop3()" disabled>Stop same</button>
</div>
<div id="div"></div>
<script>
const console = {log: msg => div.innerHTML += `${msg}<br>`,
error: e => console.log(`${e.name}: ${e.message}`)};
let deviceId1, deviceId2;
async function start(video, startBtn, stopBtn, deviceId) {
if (video.srcObject) {
console.error("video1 already has a stream !");
return;
}
const stream = await navigator.mediaDevices.getUserMedia({
video: {deviceId: deviceId? {exact: deviceId} : undefined }
});
if (video.srcObject) {
console.error("video1 already has a stream !");
for (let track of stream.getTracks()) {
track.stop();
}
return;
}
video.srcObject = stream;
if (!deviceId1) {
deviceId1 = deviceId2 = stream.getVideoTracks()[0].getSettings().deviceId;
}
if (deviceId2 == deviceId1) {
const devices = await navigator.mediaDevices.enumerateDevices();
for (const {deviceId} of devices.filter(({kind}) => kind == "videoinput")) {
if (deviceId != deviceId1) {
deviceId2 = deviceId;
break;
}
}
}
startBtn.setAttribute("disabled", "");
stopBtn.removeAttribute("disabled");
}
function stop(video, startBtn, stopBtn) {
if (!video.srcObject) {
console.error("video1 has no stream !");
return;
}
for (let track of video.srcObject.getTracks()) {
track.stop();
}
video.srcObject = null;
startBtn.removeAttribute("disabled");
stopBtn.setAttribute("disabled", "");
}
function start1() {
return start(
document.getElementById("video1"),
document.getElementById("start1Btn"),
document.getElementById("stop1Btn"),
deviceId1);
}
function stop1() {
return stop(
document.getElementById("video1"),
document.getElementById("start1Btn"),
document.getElementById("stop1Btn"));
}
function start2() {
return start(
document.getElementById("video2"),
document.getElementById("start2Btn"),
document.getElementById("stop2Btn"),
deviceId2);
}
function stop2() {
return stop(
document.getElementById("video2"),
document.getElementById("start2Btn"),
document.getElementById("stop2Btn"));
}
function start3() {
return start(
document.getElementById("video3"),
document.getElementById("start3Btn"),
document.getElementById("stop3Btn"));
}
function stop3() {
return stop(
document.getElementById("video3"),
document.getElementById("start3Btn"),
document.getElementById("stop3Btn"));
}
</script>
</html>