Chromium Code Reviews| 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 /** | |
| 20 * The height of the video elements. | |
| 21 * @private | |
| 22 */ | |
| 23 var gVideoHeight = 0; | |
| 24 | |
| 25 /** | |
| 26 * The width of the video elements. | |
| 27 * @private | |
| 28 */ | |
| 29 var gVideoWidth = 0; | |
| 30 | |
| 31 /** | |
| 32 * The height of the canvas. | |
| 33 * @private | |
| 34 */ | |
| 35 var gCanvasHeight = 0; | |
| 36 | |
| 37 /** | |
| 14 * Counts the number of frames that have been captured. Used in timeout | 38 * Counts the number of frames that have been captured. Used in timeout |
| 15 * adjustments. | 39 * adjustments. |
| 16 * @private | 40 * @private |
| 17 */ | 41 */ |
| 18 var gFrameCounter = 0; | 42 var gFrameCounter = 0; |
| 19 | 43 |
| 20 /** | 44 /** |
| 21 * The gStartOfTime when the capturing begins. Used for timeout adjustments. | 45 * The gStartOfTime when the capturing begins. Used for timeout adjustments. |
| 22 * @private | 46 * @private |
| 23 */ | 47 */ |
| (...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 | 95 * Upon load of the window opens the WebSocket to the PyWebSocket server. The |
| 72 * server should already be up and running. | 96 * server should already be up and running. |
| 73 */ | 97 */ |
| 74 window.onload = function() { | 98 window.onload = function() { |
| 75 tryOpeningWebSocket(); | 99 tryOpeningWebSocket(); |
| 76 } | 100 } |
| 77 | 101 |
| 78 /** | 102 /** |
| 79 * Starts the frame capturing. | 103 * Starts the frame capturing. |
| 80 * | 104 * |
| 81 * @param {Number} The width of the video/canvas area to be captured. | 105 * @param {String} The ID of the video tag from which the height and width |
| 82 * @param {Number} The height of the video area to be captured. | 106 * parameters are to be extracted |
|
phoglund_chromium
2014/04/28 08:51:43
Nit: End with .
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 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. | 107 * @param {Number} The frame rate at which we would like to capture frames. |
| 85 * @param {Number} The duration of the frame capture in seconds. | 108 * @param {Number} The duration of the frame capture in seconds. |
| 86 */ | 109 */ |
| 87 function startFrameCapture(width, height, canvas_height, frame_rate, duration){ | 110 function startFrameCapture(videoTagName, frame_rate, duration) { |
| 88 gFrameCaptureInterval = 1000/frame_rate; | 111 gFrameCaptureInterval = 1000/frame_rate; |
| 89 gCaptureDuration = 1000 * duration; | 112 gCaptureDuration = 1000 * duration; |
| 113 extractParameters(videoTagName); | |
|
phoglund_chromium
2014/04/28 08:51:43
Adding globals for height and width is a step back
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 90 | 114 |
| 91 console.log('Received width is: ' + width + ', received height is: ' + height | 115 console.log('Capture interval is: ' + gFrameCaptureInterval + |
| 92 + ', capture interval is: ' + gFrameCaptureInterval + | 116 ', Duration is: ' + gCaptureDuration); |
| 93 ', duration is: ' + gCaptureDuration); | |
| 94 gStartOfTime = new Date().getTime(); | 117 gStartOfTime = new Date().getTime(); |
| 95 setTimeout(function() { shoot(width, height, canvas_height); }, | 118 setTimeout(function() { shoot(); }, |
| 96 gFrameCaptureInterval); | 119 gFrameCaptureInterval); |
| 97 } | 120 } |
| 98 | 121 |
| 99 /** | 122 /** |
| 123 * Extracts the height and the width of the video tag streaming the video | |
| 124 * and sets those parameters to other tags. | |
| 125 * | |
| 126 * @param {String} The ID of the video tag from which the height and width | |
| 127 * parameters are to be extracted | |
| 128 */ | |
| 129 function extractParameters(videoTagName) { | |
| 130 var localView = document.getElementById(videoTagName); | |
| 131 gVideoHeight = localView.videoHeight; | |
| 132 gVideoWidth = localView.videoWidth; | |
| 133 // TODO(amogh.bihani): Change this when we have testcase where we capture only | |
| 134 // the bar code and not the whole frame. | |
| 135 gCanvasHeight = gVideoHeight; | |
| 136 | |
| 137 if (gVideoHeight == undefined || gVideoWidth == undefined || | |
|
phoglund_chromium
2014/04/28 08:51:43
This error checking for undefined will not really
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 138 gVideoHeight == 0 || gVideoWidth == 0) { | |
| 139 throw failTest('Could not extract video stream size'); | |
| 140 } | |
| 141 | |
| 142 console.log('Received height is: ' + gVideoHeight + ', received width is: ' + | |
| 143 gVideoWidth); | |
| 144 | |
| 145 var remoteView = document.getElementById(gVideoId); | |
|
phoglund_chromium
2014/04/28 08:51:43
This and 149-157 really seems to have no function
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 146 remoteView.height = gVideoHeight; | |
| 147 remoteView.width = gVideoWidth; | |
| 148 | |
| 149 var remoteCanvas = document.getElementById(gCanvasId); | |
|
phoglund_chromium
2014/04/28 08:51:43
I'm pretty sure you don't need to set the dimensio
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 150 remoteCanvas.height = gCanvasHeight; | |
| 151 remoteCanvas.width = gVideoWidth; | |
| 152 | |
| 153 var outputDiv = document.getElementById("output"); | |
| 154 outputDiv.style.display = "inline-block"; | |
| 155 outputDiv.style.position = "relative"; | |
| 156 outputDiv.style.height = gVideoHeight; | |
| 157 outputDiv.style.width = gVideoWidth; | |
| 158 } | |
| 159 | |
| 160 /** | |
| 100 * Captures an image frame from the provided video element. | 161 * Captures an image frame from the provided video element. |
| 101 * | 162 * |
| 102 * @param {Video} video HTML5 video element from where the image frame will | 163 * @param {Video} video HTML5 video element from where the image frame will |
| 103 * be captured. | 164 * be captured. |
| 104 * @param {Number} The width of the video/canvas area to be captured. | 165 * @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. | 166 * @param {Number} The height of the video/canvas area to be captured. |
| 106 * | 167 * |
| 107 * @return {Canvas} | 168 * @return {Canvas} |
| 108 */ | 169 */ |
| 109 function capture(video, width, height) { | 170 function capture(video, width, height) { |
| 110 var canvas = document.getElementById('remote-canvas'); | 171 var canvas = document.getElementById(gCanvasId); |
|
phoglund_chromium
2014/04/28 08:51:43
This is a good idea (we should get rid of the hard
| |
| 111 var ctx = canvas.getContext('2d'); | 172 var ctx = canvas.getContext('2d'); |
| 112 ctx.drawImage(video, 0, 0, width, height); | 173 ctx.drawImage(video, 0, 0, width, height); |
| 113 return canvas; | 174 return canvas; |
| 114 } | 175 } |
| 115 | 176 |
| 116 /** | 177 /** |
| 117 * The function which is called at the end of every gFrameCaptureInterval. Gets | 178 * 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 | 179 * 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 | 180 * it in the frames array and adjusts the capture interval (timers in JavaScript |
| 120 * aren't precise). | 181 * aren't precise). |
| 121 * | |
| 122 * @param {Number} The width of the video/canvas area to be captured. | |
| 123 * @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 */ | 182 */ |
| 126 function shoot(width, height, canvas_height){ | 183 function shoot() { |
|
phoglund_chromium
2014/04/28 08:51:43
Like I said above, keep the parameters for width a
amogh.bihani
2014/04/28 09:47:24
Done.
| |
| 127 // The first two captured frames have big difference between the ideal time | 184 // 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 | 185 // 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 | 186 // 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 | 187 // have to reset the time after the first two frames and get rid of these two |
| 131 // frames. | 188 // frames. |
| 132 if (gFrameCounter == 1 && !gFrameIntervalAdjustment) { | 189 if (gFrameCounter == 1 && !gFrameIntervalAdjustment) { |
| 133 gStartOfTime = new Date().getTime(); | 190 gStartOfTime = new Date().getTime(); |
| 134 gFrameCounter = 0; | 191 gFrameCounter = 0; |
| 135 gFrameIntervalAdjustment = true; | 192 gFrameIntervalAdjustment = true; |
| 136 gFrames.pop(); | 193 gFrames.pop(); |
| 137 gFrames.pop(); | 194 gFrames.pop(); |
| 138 } | 195 } |
| 139 var video = document.getElementById(gVideoId); | 196 var video = document.getElementById(gVideoId); |
| 140 var canvas = capture(video, width, height); | 197 var canvas = capture(video, gVideoWidth, gVideoHeight); |
| 141 | 198 |
| 142 // Extract the data from the canvas. | 199 // Extract the data from the canvas. |
| 143 var ctx = canvas.getContext('2d'); | 200 var ctx = canvas.getContext('2d'); |
| 144 var img; | 201 var img; |
| 145 if (height == canvas_height) { | 202 if (gVideoHeight == gCanvasHeight) { |
| 146 // We capture the whole video frame. | 203 // We capture the whole video frame. |
| 147 img = ctx.getImageData(0, 0, width, height); | 204 img = ctx.getImageData(0, 0, gVideoWidth, gVideoHeight); |
| 148 } else { | 205 } else { |
| 149 // We capture only the barcode (canvas_height is the height of the barcode). | 206 // We capture only the barcode (gCanvasHeight is the height of the barcode). |
| 150 img = ctx.getImageData(0, 0, width, canvas_height); | 207 img = ctx.getImageData(0, 0, gVideoWidth, gCanvasHeight); |
| 151 } | 208 } |
| 152 gFrames.push(img.data.buffer); | 209 gFrames.push(img.data.buffer); |
| 153 gFrameCounter++; | 210 gFrameCounter++; |
| 154 | 211 |
| 155 // Adjust the timer. | 212 // Adjust the timer. |
| 156 var current_time = new Date().getTime(); | 213 var current_time = new Date().getTime(); |
| 157 var ideal_time = gFrameCounter*gFrameCaptureInterval; | 214 var ideal_time = gFrameCounter*gFrameCaptureInterval; |
| 158 var real_time_elapsed = current_time - gStartOfTime; | 215 var real_time_elapsed = current_time - gStartOfTime; |
| 159 var diff = real_time_elapsed - ideal_time; | 216 var diff = real_time_elapsed - ideal_time; |
| 160 | 217 |
| 161 if (real_time_elapsed < gCaptureDuration) { | 218 if (real_time_elapsed < gCaptureDuration) { |
| 162 // If duration isn't over shoot again | 219 // If duration isn't over shoot again |
| 163 setTimeout(function() { shoot(width, height, canvas_height); }, | 220 setTimeout(function() { shoot(); }, |
| 164 gFrameCaptureInterval - diff); | 221 gFrameCaptureInterval - diff); |
| 165 } else { // Else reset gFrameCounter and send the frames | 222 } else { // Else reset gFrameCounter and send the frames |
| 166 dDoneFrameCapturing = true; | 223 dDoneFrameCapturing = true; |
| 167 gFrameCounter = 0; | 224 gFrameCounter = 0; |
| 168 clearPage_(); | 225 clearPage_(); |
| 169 prepareProgressBar_(); | 226 prepareProgressBar_(); |
| 170 sendFrames(); | 227 sendFrames(); |
| 171 } | 228 } |
| 172 } | 229 } |
| 173 | 230 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 265 /** | 322 /** |
| 266 * @private | 323 * @private |
| 267 */ | 324 */ |
| 268 function prepareProgressBar_() { | 325 function prepareProgressBar_() { |
| 269 document.body.innerHTML = | 326 document.body.innerHTML = |
| 270 '<html><body>' + | 327 '<html><body>' + |
| 271 '<p id="progress-bar" style="position: absolute; top: 50%; left: 40%;">' + | 328 '<p id="progress-bar" style="position: absolute; top: 50%; left: 40%;">' + |
| 272 'Preparing to send frames.</p>' + | 329 'Preparing to send frames.</p>' + |
| 273 '</body></html>'; | 330 '</body></html>'; |
| 274 } | 331 } |
| OLD | NEW |