Created
April 20, 2015 04:27
-
-
Save darktrojan/b1257f73e6491aefbc9c to your computer and use it in GitHub Desktop.
Video pipeline with Worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title></title> | |
</head> | |
<body> | |
<video id="video" width="480" height="360"></video> | |
<canvas id="canvas1" width="480" height="360"></canvas> | |
<canvas id="canvas2" width="480" height="360"></canvas> | |
<canvas id="canvas3" width="480" height="360"></canvas> | |
<script type="application/javascript;version=1.8"> | |
for (let element of document.querySelectorAll("[id]")) { | |
window[element.id] = element; | |
} | |
let {width, height} = canvas1; | |
let context1 = canvas1.getContext("2d"); | |
let context2 = canvas2.getContext("2d"); | |
let context3 = canvas3.getContext("2d"); | |
let worker1 = new Worker("worker1.js"); | |
worker1.onmessage = function({data}) { | |
context2.putImageData(new ImageData(new Uint8ClampedArray(data), width, height), 0, 0); | |
worker2.postMessage(data, [data]); | |
}; | |
let worker2 = new Worker("worker2.js"); | |
worker2.onmessage = function({data}) { | |
context3.putImageData(new ImageData(new Uint8ClampedArray(data), width, height), 0, 0); | |
}; | |
navigator.mozGetUserMedia({video: true}, (stream) => { | |
video.mozSrcObject = stream; | |
video.play(); | |
video.onplaying = function() { | |
let count = 0; | |
let interval = setInterval(function() { | |
context1.drawImage(video, 0, 0, width, height); | |
let frame = context1.getImageData(0, 0, width, height); | |
let data = frame.data.buffer; | |
worker1.postMessage(data, [data]); | |
if (++count == 500) { | |
clearInterval(interval); | |
video.pause(); | |
stream.stop(); | |
} | |
}, 40); | |
}; | |
}, console.error.bind(console)); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = function({data}) { | |
var u8 = new Uint8ClampedArray(data); | |
for (var i = 0; i < u8.length;) { | |
var r = i++; | |
var g = i++; | |
var b = i++; | |
var a = i++; | |
var average = (u8[r] + u8[g] + u8[b]) / 3; | |
u8[r] = average; | |
u8[g] = average; | |
u8[b] = average; | |
} | |
postMessage(data, [data]); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onmessage = function({data}) { | |
var u8 = new Uint8ClampedArray(data); | |
for (var i = 0; i < u8.length;) { | |
var r = i++; | |
var g = i++; | |
var b = i++; | |
var a = i++; | |
u8[r] *= 1.2; | |
u8[g] *= 1.2; | |
u8[b] *= 0.8; | |
} | |
postMessage(data, [data]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment