-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample4.html
124 lines (107 loc) · 3.65 KB
/
sample4.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
117
118
119
120
121
122
123
124
<html>
<head>
<body>
<div id="container">
<video autoplay="true" id='video'>
</video>
<video autoplay></video>
<!--<p><a href="#" onclick="init()">init</a></p>-->
<p><a href="#" onclick="record()">record</a></p>
<p><a href="#" onclick="stop()">stop</a></p>
<p><a href="#" onclick="load()">load</a></p>
<p><a href="#" onclick="snapshot()">snapshot</a></p>
</div>
<script>
var errorElement = document.querySelector('#errorMsg');
var video = document.querySelector('video');
var constraints = window.constraints = {
audio: true,
video: true
};
function handleSuccess(stream) {
var videoTracks = stream.getVideoTracks();
console.log('Got stream with constraints:', constraints);
console.log('Using video device: ' + videoTracks[0].label);
stream.oninactive = function() {
console.log('Stream inactive');
};
window.stream = stream; // make variable available to browser console
video.srcObject = stream;
}
function handleError(error) {
if (error.name === 'ConstraintNotSatisfiedError') {
errorMsg('The resolution ' + constraints.video.width.exact + 'x' +
constraints.video.width.exact + ' px is not supported by your device.');
} else if (error.name === 'PermissionDeniedError') {
errorMsg('Permissions have not been granted to use your camera and ' +
'microphone, you need to allow the page access to your devices in ' +
'order for the demo to work.');
}
errorMsg('getUserMedia error: ' + error.name, error);
}
function errorMsg(msg, error) {
errorElement.innerHTML += '<p>' + msg + '</p>';
if (typeof error !== 'undefined') {
console.error(error);
}
}
navigator.mediaDevices.getUserMedia(constraints).
then(handleSuccess).catch(handleError);
var video = document.getElementsByTagName('video')[0],
canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
frames = [],
task = null;
function init() {
console.log('init');
try {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
} catch (e) {
window.alert('Your browser does not support WebVideo, try Google Chrome');
}
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true}, function (stream) {
console.log('stream', stream);
video.src = window.URL.createObjectURL(stream);
}, function (e) {
window.alert('Please enable your webcam to begin recording');
});
} else {
window.alert('Your browser does not support recording, try Google Chrome');
}
}
function captureFrame(time) {
task = requestAnimationFrame(captureFrame);
context.drawImage(video, 0, 0, 320, 240);
frames.push(canvas.toDataURL('image/webp', 1)); // image/jpeg is faster
};
function record() {
console.log('record', video.src);
frames = [];
task = requestAnimationFrame(captureFrame);
}
function stop() {
console.log('stop', frames.length);
cancelAnimationFrame(task);
}
function load() {
video.src = window.URL.createObjectURL(Whammy.fromImageArray(frames, 1000 / 60));
}
function snapshot() {
if (localMediaStream) {
ctx.drawImage(video, 0, 0);
// "image/webp" works in Chrome.
// Other browsers will fall back to image/png.
document.querySelector('img').src = canvas.toDataURL('image/webp');
}
}
video.addEventListener('click', snapshot, false);
// Not showing vendor prefixes or code that works cross-browser.
navigator.getUserMedia({video: true}, function(stream) {
video.src = window.URL.createObjectURL(stream);
localMediaStream = stream;
}, errorCallback);
</script>
</body>
</head>
</html>