Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support websocket as a capture destination #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"bugs": {
"url": "https://github.com/spite/ccapture.js/issues"
},
"homepage": "https://github.com/spite/ccapture.js#readme"
"homepage": "https://github.com/spite/ccapture.js#readme",
"dependencies": {
"ws": "^1.1.1"
}
}
52 changes: 52 additions & 0 deletions src/CCapture.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,57 @@ CCFFMpegServerEncoder.prototype.safeToProceed = function() {
return this.encoder.safeToProceed();
};


// http://www.smartjava.org/content/capture-canvas-and-webgl-output-video-using-websockets
// http://fhtr.blogspot.com.au/2014/02/saving-out-video-frames-from-webgl-app.html
function CCWebsocketServerEncoder( settings ) {

CCFrameEncoder.call( this, settings );

this.framerate = this.settings.framerate;
this.type = 'image/png';
this.extension = '.png';
this.fileExtension = this.extension;
this.stream = null;
this.mediaRecorder = null;
this.chunks = [];
this.count = 0;

settings.quality = ( settings.quality / 100 ) || .8;

this.encoder = new WebSocket("ws://localhost:8889/");

}

CCWebsocketServerEncoder.prototype = Object.create( CCFrameEncoder.prototype );

CCWebsocketServerEncoder.prototype.add = function( canvas ) {

canvas.toBlob( function( blob ) {
var fileReader = new FileReader();
fileReader.onload = function() {
frame = new Uint8Array( fileReader.result );
filename = pad( this.count ) + this.fileExtension;
this.encoder.send(frame);
this.count++;
this.step();
}.bind( this );
fileReader.readAsArrayBuffer(blob);

}.bind( this ), this.type )

}

CCWebsocketServerEncoder.prototype.save = function( callback ) {
// End the stream
this.mediaRecorder.stop();
// Close the socket
this.encoder.close()

this.stop();
}


/*
HTMLCanvasElement.captureStream()
*/
Expand Down Expand Up @@ -603,6 +654,7 @@ function CCapture( settings ) {
gif: CCGIFEncoder,
webm: CCWebMEncoder,
ffmpegserver: CCFFMpegServerEncoder,
websocketserver: CCWebsocketServerEncoder,
png: CCPNGEncoder,
jpg: CCJPEGEncoder,
'webm-mediarecorder': CCStreamEncoder
Expand Down
38 changes: 38 additions & 0 deletions utils/websocketserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// http://fhtr.blogspot.com.au/2014/02/saving-out-video-frames-from-webgl-app.html
// Adapted from this _WikiBooks OpenGL Programming Video Capture article_.
var port = 8889;
var ws = require('ws');
var fs = require('fs');
var frame = 0;
var path = "../../../output";

var WebSocketServer = require('ws').Server, wss = new WebSocketServer({ port: port });

wss.on('connection', function connection(ws) {
frame = 0;

var d = new Date();
var directory = path + "/" + d.toISOString();

fs.mkdir(path, '755', function() {});
fs.mkdir(directory, '755', function() {});

ws.on('message', function incoming(data, filename, x) {
console.log("filename")
console.log(filename)

base64Data = data.toString('base64');

// Allows for up to 1 hour of encoded data at 25fps.
var filename = ("00000" + frame).slice(-5)+".png";

fs.writeFile(directory + "/" + filename, base64Data, 'base64', function(err) {
if ( err ) console.log(err);
});

console.log('Wrote ' + filename);
frame++;
});
});

console.log('Server running at ws://127.0.0.1:'+port+'/');