forked from anantn/webrtc-landing
-
Notifications
You must be signed in to change notification settings - Fork 22
/
enumerate.html
81 lines (71 loc) · 2.36 KB
/
enumerate.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
<html>
<meta charset=utf-8>
<video id="video1" height="120" autoplay></video>
<video id="video2" height="120" autoplay></video><br>
<button id="gum_audio">Start Mic!</button>
<button id="gum_video">Start Cam!</button>
<button id="gum_both">Start Both!</button>
<button id="gdm_video">Present!</button>
<button id="stopp">Stop!</button>
<button id="speakers">Switch speakers!</button>
<button id="list">Enumerate Devices!</button>
<button id="reload">Reload!</button>
<label><input type="checkbox" id="muted">Face-muted</label>
<div id="div"><br></div>
<script>
const log = msg => div.innerHTML += `${msg}<br>`;
let unstopped = [];
async function gum({audio, video}) {
try {
const stream = await navigator.mediaDevices.getUserMedia({audio, video});
if (video1.srcObject && !audio != !video) {
const [oldvideo] = video1.srcObject.getVideoTracks();
const [oldaudio] = video1.srcObject.getAudioTracks();
stream.addTrack(audio? oldvideo : oldaudio);
}
video1.srcObject = stream;
unstopped.push(...video1.srcObject.getTracks());
} catch (e) {
log(e);
}
}
async function gdm(constraints) {
try {
video2.srcObject = await navigator.mediaDevices.getDisplayMedia(constraints);
unstopped.push(...video2.srcObject.getTracks());
} catch (e) {
log(e);
}
}
gum_audio.onclick = () => gum({audio: true});
gum_video.onclick = () => gum({video: true});
gum_both.onclick = () => gum({video: true, audio: true});
gdm_video.onclick = () => gdm();
stopp.onclick = () => {
unstopped.forEach(track => track.stop());
unstopped = [];
};
speakers.onclick = async () => {
try {
const {deviceId, label} = await navigator.mediaDevices.selectAudioOutput();
console.log(`Switching speakers to ${label}`);
await video1.setSinkId(deviceId);
} catch (e) {
log(e);
}
}
muted.onclick = () => {
video1.srcObject.getTracks().forEach(track => track.enabled = !muted.checked);
video2.srcObject.getTracks().forEach(track => track.enabled = !muted.checked);
};
list.onclick = async () => {
const devices = await navigator.mediaDevices.enumerateDevices();
log(devices.length + " devices.");
for (const {kind, label, deviceId, groupId} of devices) {
log(`${kind}: ${label} id=${deviceId} group=${groupId}`);
}
}
reload.onclick = () => location.reload();
navigator.mediaDevices.ondevicechange = () => log("device change!");
</script>
</html>