-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcamera-preview.js
196 lines (163 loc) · 5.44 KB
/
camera-preview.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import initWebrtc from './init-webrtc'
class CancelToken {
canceled = false
}
class CameraPreview {
constructor(previewContainer, tracker) {
this.container = previewContainer
this.videoElem = previewContainer.querySelector('video')
this.facing = null
this.switchButton = null
this.videoStream = null
this.tracker = tracker
this.initCancelToken = new CancelToken()
this.switchButtonListener = () => this.onSwitchCamera()
this.videoElem.addEventListener('playing', () => {
this.videoElem.classList.remove('paused')
})
this.loadFacing()
window.addEventListener('storage', evt => {
if (evt.key === 'cameraFacing') {
this.loadFacing()
}
})
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
this.stop()
return
}
this.initializeCamera()
})
let resizeTimeout = null
window.addEventListener('resize', evt => {
if (resizeTimeout) {
clearTimeout(resizeTimeout)
}
resizeTimeout = setTimeout(() => {
resizeTimeout = null
this.applyScaling()
}, 100)
})
}
loadFacing() {
const oldFacing = this.facing
this.facing = window.localStorage.getItem('cameraFacing') || 'user'
if (this.facing !== oldFacing) {
this.initializeCamera()
}
}
async initializeCamera(attempt = 0) {
const initTime = Date.now()
this.stop()
let timer = setTimeout(() => {
this.container.classList.remove('camera-enabled')
timer = null
}, 15000)
this.initCancelToken = new CancelToken()
let stream
try {
stream = await initWebrtc(this.videoElem, this.facing, this.initCancelToken)
} catch (err) {
if (timer) {
clearTimeout(timer)
timer = null
}
if (!this.initCancelToken.canceled && attempt < 2 && Date.now() - initTime < 200) {
// Chrome has a weird problem where if you try to do a getUserMedia request too early, it
// can return a MediaDeviceNotSupported error (even though nothing is wrong and permission
// has been granted). So we install a delay and retry a couple times to try and mitigate
// this
if (
(err.name && err.name === 'MediaDeviceNotSupported') ||
(err.message && err.message === 'MediaDeviceNotSupported')
) {
setTimeout(() => this.initializeCamera(attempt + 1), 200)
return
}
}
// TODO(tec27): display something to the user depending on error type
this.container.classList.remove('camera-enabled')
console.log('error initializing camera preview:')
console.dir(err)
this.tracker.onCameraError(err.name || err.message)
return
}
if (timer) {
clearTimeout(timer)
timer = null
}
this.container.classList.add('camera-enabled')
this.tracker.onCameraInitialized()
this.videoStream = stream
this.applyScaling()
this.updateSwitchButton()
}
applyScaling() {
if (!this.videoStream) {
return
}
const containerRect = this.videoElem.parentElement.getBoundingClientRect()
const { left, top, width, height } = calcImageDimens(
containerRect.width,
containerRect.height,
this.videoElem.videoWidth,
this.videoElem.videoHeight,
)
this.videoElem.style.left = `${left}px`
this.videoElem.style.top = `${top}px`
this.videoElem.style.width = `${width}px`
this.videoElem.style.height = `${height}px`
}
stop() {
this.initCancelToken.canceled = true
if (!this.videoStream) return
this.videoStream.stop()
this.videoStream = null
this.videoElem.classList.add('paused')
}
updateSwitchButton() {
if (this.switchButton) {
this.container.removeChild(this.switchButton)
this.switchButton = null
}
if (!this.videoStream.facing || !this.videoStream.hasFrontAndRear) return
this.switchButton = document.createElement('button')
this.switchButton.classList.add('switch-camera', 'shadow-1')
const otherCameraTitle = this.videoStream.facing === 'user' ? 'rear' : 'front'
this.switchButton.setAttribute('title', `Switch to ${otherCameraTitle} camera`)
this.switchButton.addEventListener('click', this.switchButtonListener)
if (this.videoStream.facing === 'user') {
this.switchButton.innerHTML = '<sc-svg-icon invert="true" icon="cameraRear"></sc-svg-icon>'
} else {
this.switchButton.innerHTML = '<sc-svg-icon invert="true" icon="cameraFront"></sc-svg-icon>'
}
this.container.appendChild(this.switchButton)
}
onSwitchCamera() {
this.facing = this.facing === 'user' ? 'environment' : 'user'
window.localStorage.setItem('cameraFacing', this.facing)
this.tracker.onCameraFacingChange(this.facing)
this.initializeCamera()
}
}
export default function createCameraPreview() {
return new CameraPreview(...arguments)
}
function calcImageDimens(targetWidth, targetHeight, sourceWidth, sourceHeight) {
let left = 0
let top = 0
let width = targetWidth
let height = targetHeight
const targetAspect = width / height
const actualAspect = sourceWidth / sourceHeight
if (targetAspect > actualAspect) {
// cut off the top/bottom
height = Math.round(width / actualAspect)
} else {
// cut off the left/right
width = Math.round(height * actualAspect)
}
left = -Math.floor((width - targetWidth) / 2)
top = -Math.floor((height - targetHeight) / 2)
return { left, top, width, height }
}