-
Notifications
You must be signed in to change notification settings - Fork 1
/
enumdemo4.html
80 lines (69 loc) · 2.04 KB
/
enumdemo4.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
<!DOCTYPE html>
<html>
<head>
<title>enumeration demo</title>
</head>
<body>
Devices:
<ol id="devicelist"></ol>
<button id="refresh" onclick="refreshDeviceList();">Refresh device list</button><br>
<script>
var device_list = null;
var devchangecount = 0;
window.onload = onLoad;
async function requestPermission() {
try {
let stream = await navigator.mediaDevices.getUserMedia({audio:true});
stream.getTracks()[0].stop();
stream = await navigator.mediaDevices.getUserMedia({video:true});
stream.getTracks()[0].stop();
} catch(e) {
console.err("FAILED: " + e.name + ", " + JSON.stringify(e));
}
}
function onLoad() {
device_list = document.getElementById("devicelist");
requestPermission().then(() => refreshDeviceList(true,device_list));
if ("ondevicechange" in navigator.mediaDevices) {
console.log("Adding event listener");
navigator.mediaDevices.ondevicechange = function() { console.log('GOT DEVICECHANGE EVENT ' + devchangecount++); refreshDeviceList(false); };
}
}
function addItem(strItem) {
var item = document.createElement("li");
item.innerHTML = strItem;
device_list.appendChild(item);
}
function clearItems() {
while (device_list.firstChild ){
device_list.removeChild(device_list.firstChild );
}
}
let enumcounter = 0;
let enumresultcounter=0;
function refreshDeviceList() {
if (!navigator.mediaDevices) {
addItem('navigator.mediaDevices not supported');
return;
}
if (!navigator.mediaDevices.enumerateDevices) {
addItem('enumerateDevices not supported');
return;
}
console.log("ISSUING ENUMERATION " + enumcounter++);
navigator.mediaDevices.enumerateDevices().then( function(infos) {
console.log("GOT ENUMERATION RESULTS " + enumresultcounter++);
clearItems();
for (let i = 0; i < infos.length; i++) {
addItem(infos[i].kind + ' - ' +
infos[i].deviceId + ' - "' +
infos[i].label + '" - ' +
infos[i].groupId);
}
}, function(msg) {
alert('Something wrong: ' + msg);
});
}
</script>
</body>
</html>