-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumerate_query.html
116 lines (103 loc) · 3.28 KB
/
enumerate_query.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
<!DOCTYPE html>
<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="query">Query!</button>
<button id="reload">Reload!</button>
<label><input type="checkbox" id="muted">Face-muted</label>
<div id="div"><br></div>
<script>
const console = {log: msg => div.innerHTML += `${msg}<br>`};
let unstopped = [];
(async () => {
try {
await dumpState("Initial: ", true);
} catch(e) {
console.log(e);
}
})();
async function dumpState(header = "", followup) {
const [cam, mic] = await Promise.all([
navigator.permissions.query({name: "camera"}),
navigator.permissions.query({name: "microphone"})
]);
console.log(`${header}camera = "${cam.state}", microphone = "${mic.state}"`);
if (followup) {
mic.onchange = () => console.log(`Changed from ${header}microphone = "${mic.state}"`);
cam.onchange = () => console.log(`Changed from ${header}camera = "${cam.state}"`);
}
}
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();
if (audio && oldvideo) {
stream.addTrack(oldvideo);
} else if (video && oldaudio) {
stream.addTrack(oldaudio);
}
}
video1.srcObject = stream;
unstopped.push(...video1.srcObject.getTracks());
await dumpState("After: ");
} catch (e) {
console.log(e);
}
}
async function gdm(constraints) {
try {
video2.srcObject = await navigator.mediaDevices.getDisplayMedia(constraints);
unstopped.push(...video2.srcObject.getTracks());
} catch (e) {
console.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) {
console.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();
console.log(devices.length + " devices.");
for (const {kind, label, deviceId, groupId} of devices) {
console.log(`${kind}: ${label} id=${deviceId} group=${groupId}`);
}
}
query.onclick = async () => {
try {
await dumpState("Query: ");
} catch(e) {
console.log(e);
}
}
reload.onclick = () => location.reload();
navigator.mediaDevices.ondevicechange = () => console.log("device change!");
</script>
</html>