This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
233 lines (197 loc) · 8.86 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Mirror</title>
</head>
<style>
#content{
position: absolute;
height: 98%;
width: 98%
}
#stats{
position: absolute;
left: 2px;
z-index: 3
}
video{
position: absolute;
height: 100%;
z-index: 2;
}
canvas{
position: absolute;
z-index: 1;
}
</style>
<body>
<div id="content">
<div id="stats"></div>
<video id="vidme" autoplay hidden></video>
<canvas id="mirror"></canvas>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script>
$(document).ready( function(){
var video = $('#vidme')[0]; //use var so we only call jQuery selectors once
var w = 1280, //video width
h = 720, //video width
r=30; //framerate: default to 30
var vidRatio = 1280/720; //use a 16:9 ratio
var writeMirror; //global var for our mirror function
//Get the local video with getUserMedia
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
//Make sure getUserMedia is available
if(!navigator.getUserMedia){
$("#content").innerHTML(":( Your browser does not support getUserMedia")
}
else{
//ask for 720p resolution
var constraints = {
audio: false, //no need for audio
video: {
mandatory: {
minWidth: w,
minHeight: h
}
}
};
function onSuccess(stream) {
window.stream = stream; // stream available to console
video.src = window.URL.createObjectURL(stream);
video.play();
console.log("showing video");
//get the returned video dimensions once the video starts playing
//question: is "playing" event from stream than vid? Some other event?
video.addEventListener("playing", getDimensions());
}
function onFail(error){
//To DO: on fail due to camera constraints try again with a lower res
$('#content').innerText("Sorry - you have an error: " + error);
console.log(error);
}
console.log("requested video size is: " + w + " x " + h );
navigator.getUserMedia(constraints, onSuccess, onFail);
}
//use vars so we only call jQuery selectors once
var mirrorCanvas = $('#mirror')[0];
var canvasContext = $("#mirror")[0].getContext('2d');
var area= $('#content')[0]; //
//Sometimes need to wait for dimensions to show
function getDimensions(){
var timer = 10;
var waitForDimensions = window.setInterval(function(){
//see if the video dimensions are available
if(video.videoWidth*video.videoHeight>0) {
clearInterval(waitForDimensions);
vidRatio = video.videoWidth/video.videoHeight;
console.log("after waiting " + timer + " ms, actual video size is: " + video.videoWidth + " x " + video.videoHeight);
startCanvas();
//start calculating stats once the video is started
calculateStats();
}
//If not, wait another 10ms
else {
//console.log("No video dimensions after " + timer + " ms");
timer+=10;
}
}, 10);
}
//Canvas stuff
function startCanvas(){
mirrorCanvas.setAttribute("height", area.offsetHeight ); //use the whole height of 'content' div
mirrorCanvas.setAttribute("width", area.offsetHeight * vidRatio); //scale the width accordingly
canvasContext.translate(area.offsetHeight * vidRatio,0); //move the drawing cursor to the right edge
canvasContext.scale(-1,1); //flip the image horizontally
drawMirror(r); //start at the default rate
}
//function to draw the mirror at the specified number of times a second
function drawMirror(rate){
writeMirror = window.setInterval( function(){
if ( $("#mirror").is(":hidden") ) return; //don't draw if the canvas is hidden
canvasContext.drawImage(video, 0,0,area.offsetHeight * vidRatio, area.offsetHeight );
}, 1/rate);
}
//resize the canvas is the window is changed
$(window).resize(function(){
mirrorCanvas.setAttribute("height", area.offsetHeight );
mirrorCanvas.setAttribute("width", area.offsetHeight * vidRatio);
canvasContext.translate(area.offsetHeight * vidRatio,0); //move the drawing cursor to the right edge
canvasContext.scale(-1,1); //flip the image horizontally
});
//Change the mirror mode on click
$(document).click(function(){
$('#vidme').toggle();
$('#mirror').toggle();
console.log("toggling outputs");
});
//modified from: http://git.chromium.org/gitweb/?p=chromium.git;a=blob;f=chrome/test/data/media/html/media_stat_perf.html
function calculateStats() {
var decodedFrames = 0,
droppedFrames = 0,
decodedFPS = [],
droppedFPS = [],
startTime = new Date().getTime(),
deltaTime = 0;
var decodedFPSavg = 0,
droppedFPSavg = 0,
currentDecodedFPS= 0,
currentDroppedFPS=0;
window.setInterval(function(){
if (video.readyState <= HTMLMediaElement.HAVE_CURRENT_DATA ||
video.paused || video.ended ) {
console.log("No video to get stats on");
return;
}
else if (!video.webkitDecodedFrameCount){
console.log("Video FPS calcs not supported");
video.removeEventListener("playing", handler);
return;
}
else{
var currentTime = new Date().getTime();
deltaTime = (currentTime - startTime) / 1000;
startTime = currentTime;
// Calculate decoded frames per sec.
var fps = (video.webkitDecodedFrameCount - decodedFrames) / deltaTime;
decodedFrames = video.webkitDecodedFrameCount;
decodedFPS.push(fps);
// Calculate dropped frames per sec.
fps = (video.webkitDroppedFrameCount - droppedFrames) / deltaTime;
droppedFrames = video.webkitDroppedFrameCount;
droppedFPS.push(fps);
//calculate averages
decodedFPSavg = decodedFPS.average();
droppedFPSavg = droppedFPS.average();
currentDecodedFPS = Math.round(decodedFPS.slice(-1)[0]);
currentDroppedFPS = Math.round(droppedFPS.slice(-1)[0]);
//write the results to a table
$("#stats")[0].innerHTML =
"Decoded frames: " + decodedFrames + " Avg: " + decodedFPS.average().toFixed() + " fps. Current:" + currentDecodedFPS + " fps <br>" +
"Dropped frames: " + droppedFrames + " Avg: " + droppedFPS.average().toFixed() + " fps. Current:" + currentDroppedFPS +" fps <br>" +
"Total frames: " + (decodedFrames + droppedFrames) + " Avg: " + (decodedFPSavg + droppedFPSavg).toFixed() + " fps. Current:" + (currentDecodedFPS + currentDroppedFPS) +" fps <br>";
//Check if the current framerate has changed
if (r!=Math.round(currentDecodedFPS)){
clearInterval(mirror);
//set the mirrorCanvas update rate to whatever the decoded framerate is
drawMirror(Math.round(currentDecodedFPS));
}
}
}, 1000);
}
});
</script>
<script>
//Added this to show averages
//source: http://stackoverflow.com/a/18234568/2186163
Array.prototype.average = function () {
var sum = 0, j = 0;
for (var i = 0; i < this.length, isFinite(this[i]); i++) {
sum += parseFloat(this[i]); ++j;
}
return j ? sum / j : 0;
};
</script>
</body>
</html>