OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /** | 7 /** |
8 * The ID of the video tag from which frames are captured. | 8 * The ID of the video tag from which frames are captured. |
9 * @private | 9 * @private |
10 */ | 10 */ |
11 var gVideoId = 'remote-view'; | 11 var gVideoId = 'remote-view'; |
12 | 12 |
13 /** | 13 /** |
14 * The ID of the canvas tag from which the frames are captured. | |
15 * @private | |
16 */ | |
17 var gCanvasId = 'remote-canvas'; | |
18 | |
19 /** | |
14 * Counts the number of frames that have been captured. Used in timeout | 20 * Counts the number of frames that have been captured. Used in timeout |
15 * adjustments. | 21 * adjustments. |
16 * @private | 22 * @private |
17 */ | 23 */ |
18 var gFrameCounter = 0; | 24 var gFrameCounter = 0; |
19 | 25 |
20 /** | 26 /** |
21 * The gStartOfTime when the capturing begins. Used for timeout adjustments. | 27 * The gStartOfTime when the capturing begins. Used for timeout adjustments. |
22 * @private | 28 * @private |
23 */ | 29 */ |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 * Upon load of the window opens the WebSocket to the PyWebSocket server. The | 77 * Upon load of the window opens the WebSocket to the PyWebSocket server. The |
72 * server should already be up and running. | 78 * server should already be up and running. |
73 */ | 79 */ |
74 window.onload = function() { | 80 window.onload = function() { |
75 tryOpeningWebSocket(); | 81 tryOpeningWebSocket(); |
76 } | 82 } |
77 | 83 |
78 /** | 84 /** |
79 * Starts the frame capturing. | 85 * Starts the frame capturing. |
80 * | 86 * |
81 * @param {Number} The width of the video/canvas area to be captured. | 87 * @param {!Object} The video tag from which the height and width parameters are |
82 * @param {Number} The height of the video area to be captured. | 88 to be extracted. |
83 * @param {Number} The height of the canvas where we put the video frames. | |
84 * @param {Number} The frame rate at which we would like to capture frames. | 89 * @param {Number} The frame rate at which we would like to capture frames. |
85 * @param {Number} The duration of the frame capture in seconds. | 90 * @param {Number} The duration of the frame capture in seconds. |
86 */ | 91 */ |
87 function startFrameCapture(width, height, canvas_height, frame_rate, duration){ | 92 function startFrameCapture(videoTag, frame_rate, duration) { |
88 gFrameCaptureInterval = 1000/frame_rate; | 93 gFrameCaptureInterval = 1000/frame_rate; |
89 gCaptureDuration = 1000 * duration; | 94 gCaptureDuration = 1000 * duration; |
95 var width = videoTag.videoWidth; | |
96 var height = videoTag.videoHeight; | |
97 | |
98 if (width == 0 || height == 0) { | |
99 throw failTest('Trying to capture from ' + videoTag.id + | |
100 ' but it is not playing any video.'); | |
kjellander_chromium
2014/04/30 10:49:42
nit: +3 indent
| |
101 } | |
102 | |
103 setCanvasDimensions(width, height); | |
90 | 104 |
91 console.log('Received width is: ' + width + ', received height is: ' + height | 105 console.log('Received width is: ' + width + ', received height is: ' + height |
92 + ', capture interval is: ' + gFrameCaptureInterval + | 106 + ', capture interval is: ' + gFrameCaptureInterval + |
93 ', duration is: ' + gCaptureDuration); | 107 ', Duration is: ' + gCaptureDuration); |
108 | |
94 gStartOfTime = new Date().getTime(); | 109 gStartOfTime = new Date().getTime(); |
95 setTimeout(function() { shoot(width, height, canvas_height); }, | 110 setTimeout(function() { shoot(width, height); }, |
96 gFrameCaptureInterval); | 111 gFrameCaptureInterval); |
97 } | 112 } |
98 | 113 |
99 /** | 114 /** |
115 * Sets the canvas dimensions. | |
116 * | |
117 * @param {Number} The width of the canvas. | |
118 * @param {Number} The height of the canvas. | |
119 */ | |
120 function setCanvasDimensions(width, height) { | |
kjellander_chromium
2014/04/30 10:49:42
Prefix with _ and mark as @private since this is o
kjellander_chromium
2014/04/30 10:49:42
IMO it would be nicer to pass the canvas Id as a p
| |
121 var canvas = document.getElementById(gCanvasId); | |
122 canvas.width = width; | |
123 canvas.height = height; | |
124 } | |
125 | |
126 /** | |
100 * Captures an image frame from the provided video element. | 127 * Captures an image frame from the provided video element. |
101 * | 128 * |
102 * @param {Video} video HTML5 video element from where the image frame will | 129 * @param {Video} video HTML5 video element from where the image frame will |
103 * be captured. | 130 * be captured. |
104 * @param {Number} The width of the video/canvas area to be captured. | 131 * @param {Number} The width of the video/canvas area to be captured. |
105 * @param {Number} The height of the video/canvas area to be captured. | 132 * @param {Number} The height of the video/canvas area to be captured. |
106 * | 133 * |
107 * @return {Canvas} | 134 * @return {Canvas} |
108 */ | 135 */ |
109 function capture(video, width, height) { | 136 function capture(video, width, height) { |
110 var canvas = document.getElementById('remote-canvas'); | 137 var canvas = document.getElementById(gCanvasId); |
111 var ctx = canvas.getContext('2d'); | 138 var ctx = canvas.getContext('2d'); |
112 ctx.drawImage(video, 0, 0, width, height); | 139 ctx.drawImage(video, 0, 0, width, height); |
113 return canvas; | 140 return canvas; |
114 } | 141 } |
115 | 142 |
116 /** | 143 /** |
117 * The function which is called at the end of every gFrameCaptureInterval. Gets | 144 * The function which is called at the end of every gFrameCaptureInterval. Gets |
118 * the current frame from the video and extracts the data from it. Then it saves | 145 * the current frame from the video and extracts the data from it. Then it saves |
119 * it in the frames array and adjusts the capture interval (timers in JavaScript | 146 * it in the frames array and adjusts the capture interval (timers in JavaScript |
120 * aren't precise). | 147 * aren't precise). |
121 * | 148 * |
122 * @param {Number} The width of the video/canvas area to be captured. | 149 * @param {Number} The width of the video/canvas area to be captured. |
123 * @param {Number} The height of the video area to be captured. | 150 * @param {Number} The height of the video area to be captured. |
124 * @param {Number} The height of the canvas where we put the video frames. | |
125 */ | 151 */ |
126 function shoot(width, height, canvas_height){ | 152 function shoot(width, height) { |
127 // The first two captured frames have big difference between the ideal time | 153 // The first two captured frames have big difference between the ideal time |
128 // interval between two frames and the real one. As a consequence this affects | 154 // interval between two frames and the real one. As a consequence this affects |
129 // enormously the interval adjustment for subsequent frames. That's why we | 155 // enormously the interval adjustment for subsequent frames. That's why we |
130 // have to reset the time after the first two frames and get rid of these two | 156 // have to reset the time after the first two frames and get rid of these two |
131 // frames. | 157 // frames. |
132 if (gFrameCounter == 1 && !gFrameIntervalAdjustment) { | 158 if (gFrameCounter == 1 && !gFrameIntervalAdjustment) { |
133 gStartOfTime = new Date().getTime(); | 159 gStartOfTime = new Date().getTime(); |
134 gFrameCounter = 0; | 160 gFrameCounter = 0; |
135 gFrameIntervalAdjustment = true; | 161 gFrameIntervalAdjustment = true; |
136 gFrames.pop(); | 162 gFrames.pop(); |
137 gFrames.pop(); | 163 gFrames.pop(); |
138 } | 164 } |
139 var video = document.getElementById(gVideoId); | 165 var video = document.getElementById(gVideoId); |
140 var canvas = capture(video, width, height); | 166 var canvas = capture(video, width, height); |
141 | 167 |
142 // Extract the data from the canvas. | 168 // Extract the data from the canvas. |
143 var ctx = canvas.getContext('2d'); | 169 var ctx = canvas.getContext('2d'); |
144 var img; | 170 // We capture the whole video frame. |
145 if (height == canvas_height) { | 171 var img = ctx.getImageData(0, 0, width, height); |
146 // We capture the whole video frame. | |
147 img = ctx.getImageData(0, 0, width, height); | |
148 } else { | |
149 // We capture only the barcode (canvas_height is the height of the barcode). | |
150 img = ctx.getImageData(0, 0, width, canvas_height); | |
151 } | |
152 gFrames.push(img.data.buffer); | 172 gFrames.push(img.data.buffer); |
153 gFrameCounter++; | 173 gFrameCounter++; |
154 | 174 |
155 // Adjust the timer. | 175 // Adjust the timer. |
156 var current_time = new Date().getTime(); | 176 var current_time = new Date().getTime(); |
157 var ideal_time = gFrameCounter*gFrameCaptureInterval; | 177 var ideal_time = gFrameCounter*gFrameCaptureInterval; |
158 var real_time_elapsed = current_time - gStartOfTime; | 178 var real_time_elapsed = current_time - gStartOfTime; |
159 var diff = real_time_elapsed - ideal_time; | 179 var diff = real_time_elapsed - ideal_time; |
160 | 180 |
161 if (real_time_elapsed < gCaptureDuration) { | 181 if (real_time_elapsed < gCaptureDuration) { |
162 // If duration isn't over shoot again | 182 // If duration isn't over shoot again |
163 setTimeout(function() { shoot(width, height, canvas_height); }, | 183 setTimeout(function() { shoot(width, height); }, |
164 gFrameCaptureInterval - diff); | 184 gFrameCaptureInterval - diff); |
165 } else { // Else reset gFrameCounter and send the frames | 185 } else { // Else reset gFrameCounter and send the frames |
166 dDoneFrameCapturing = true; | 186 dDoneFrameCapturing = true; |
167 gFrameCounter = 0; | 187 gFrameCounter = 0; |
168 clearPage_(); | 188 clearPage_(); |
169 prepareProgressBar_(); | 189 prepareProgressBar_(); |
170 sendFrames(); | 190 sendFrames(); |
171 } | 191 } |
172 } | 192 } |
173 | 193 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 /** | 285 /** |
266 * @private | 286 * @private |
267 */ | 287 */ |
268 function prepareProgressBar_() { | 288 function prepareProgressBar_() { |
269 document.body.innerHTML = | 289 document.body.innerHTML = |
270 '<html><body>' + | 290 '<html><body>' + |
271 '<p id="progress-bar" style="position: absolute; top: 50%; left: 40%;">' + | 291 '<p id="progress-bar" style="position: absolute; top: 50%; left: 40%;">' + |
272 'Preparing to send frames.</p>' + | 292 'Preparing to send frames.</p>' + |
273 '</body></html>'; | 293 '</body></html>'; |
274 } | 294 } |
OLD | NEW |