-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
78 lines (69 loc) · 3.13 KB
/
index.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
<!doctype html><html dir="ltr" lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0"> <title>user-media component demo</title> <script type="module" src="https://unpkg.com/@popul-ar/user-media@latest"></script> <!-- <script type="module" src="/build/user-media.esm.js"></script>
<script nomodule src="/build/user-media.js"></script> --> <style>html,
body {
margin: 0;
width: 100%;
height: 100%;
}
user-media {
position: absolute;
width: 100%;
height: 100%;
object-fit: contain;
}
#controls {
position: absolute;
top: 10px;
left: 10px;
z-index: 100;
}</style> </head> <body> <div id="controls"> <select name="device" id="device"> <option value="default">default</option> </select> <select name="resolution" id="resolution"> <option value="640:480">640:480</option> <option value="1280:720">1280:720</option> <option value="1920:1280">1920:1280</option> </select> <button id="stopStream">stopStream()</button> <p id="frame-counter">frame: 0</p> </div> <user-media audio="false" width="640" height="480" controls=""></user-media> <script>
document.addEventListener('DOMContentLoaded', () => {
const um = document.querySelector('user-media')
// stream state events
um.addEventListener('streamactive', async () => {
console.log('streamactive')
const settings = await um.getTrackSettings()
console.log('video track settings', settings)
})
um.addEventListener('streaminactive', () => console.log('streaminactive'))
// stop stream button
document.querySelector('#stopStream').addEventListener('click', () => um.stopStream())
// device selector
const deviceSelect = document.querySelector('#device')
um.getMediaDevices('videoinput').then(devices => {
devices.map(device => {
const option = document.createElement('option')
option.innerText = device.label
option.value = device.deviceId
deviceSelect.appendChild(option)
})
}).catch(err => console.error(err))
deviceSelect.addEventListener('change', (e) => {
um.videoDeviceId = e.target.value
})
// resolution selector
document.querySelector('#resolution').addEventListener('change', (e) => {
console.log(e.target.value)
const [w, h] = e.target.value.split(':')
um.width = w
um.height = h
})
// frame ticker
let frameNum = 0
const frameCounter = document.querySelector('#frame-counter')
um.addEventListener('frameupdate', (e) => {
// console.log('frameupdate', e.detail)
frameCounter.innerText = 'frame: ' + frameNum
frameNum++
})
})
</script> <script>
if ('serviceWorker' in navigator && location.protocol !== 'file:') {
// auto-unregister service worker during dev mode
navigator.serviceWorker.getRegistration().then(function(registration) {
if (registration) {
registration.unregister().then(function() { location.reload(true) });
}
});
}
</script></body></html>