-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-rtc-cam.js
89 lines (71 loc) · 2.1 KB
/
web-rtc-cam.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
(function webRtcCam(){
'use strict';
window.addEventListener("load", onWindowLoaded);
function onWindowLoaded() {
document.getElementById("readyButton").addEventListener('click', onReadyClick);
}
function onReadyClick() {
var constraints = {
audio: false,
video: {
width: 640,
height: 480,
facingMode: 'user'
}
};
try {
getUserMedia(constraints, setVideoSource, onError);
} catch(e) {
onError(e);
}
}
function getUserMedia(constraints, onLoad, onError) {
if(navigator.getUserMedia) {
return navigator.getUserMedia(constraints, onLoad, onError);
}
if(navigator.webkitGetUserMedia) {
// Chrome
return navigator.webkitGetUserMedia(constraints, onLoad, onError);
}
if(navigator.mozGetUserMedia) {
// Mozilla
return navigator.mozGetUserMedia(constraints, onLoad, onError);
}
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Edge
return navigator.mediaDevices.getUserMedia(constraints).then(onLoad).catch(onError);
}
console.error('getUserMedia', navigator.getUserMedia);
console.error('webkitGetUserMedia', navigator.webkitGetUserMedia);
console.error('mozGetUserMedia', navigator.mozGetUserMedia);
console.error('mediaDevices', navigator.mediaDevices);
throw 'Unable to determine how to get user media. Is your camera blocked from using this site?';
}
function setVideoSource(stream) {
var video = document.getElementById('videoCamera');
video.srcObject = stream;
}
function onError(error) {
var message = getErrorMessage(error);
var content = document.getElementById('content');
if (content === null) {
console.error(error);
alert(error);
return;
}
content.innerText = 'Error: ' + message;
}
function getErrorMessage(error) {
if(error.message) {
return error.message;
}
switch(error.name) {
case 'PermissionDeniedError':
return 'Permission to use a media device was denied by the user or the system.';
case 'NotFoundError':
return 'No media tracks of the type specified were found that satify the constraints specified.';
default:
return name;
}
}
})();