2018-07-03 19:39:50 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
let { Driver } = require( './driver' );
|
|
|
|
let http = require( 'http' );
|
|
|
|
let mjpegServer = require( 'mjpeg-server' );
|
|
|
|
let jpeg = require( 'jpeg-js' );
|
|
|
|
|
|
|
|
let mjpegReqHandler = undefined;
|
2018-07-03 20:34:21 +02:00
|
|
|
let httpServer = undefined;
|
2018-07-03 19:39:50 +02:00
|
|
|
|
|
|
|
class MotionJPEG extends Driver {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
2018-07-03 20:34:21 +02:00
|
|
|
httpServer = http.createServer( function( req, res ) {
|
2018-07-03 19:39:50 +02:00
|
|
|
mjpegReqHandler = mjpegServer.createReqHandler( req, res );
|
|
|
|
} ).listen( 8081, '0.0.0.0' );
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:34:21 +02:00
|
|
|
/**
|
|
|
|
* Writes the current buffer to the motionjpeg stream
|
|
|
|
*/
|
2018-07-03 19:39:50 +02:00
|
|
|
write( buffer = false ) {
|
|
|
|
if ( mjpegReqHandler === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = new Buffer( this.width * this.height * 4 );
|
|
|
|
|
|
|
|
let i = 0;
|
|
|
|
let size = this.getSize();
|
|
|
|
|
|
|
|
for ( let y = 0; y < size.height; y++ ) {
|
|
|
|
for ( let x = 0; x < size.width; x++ ) {
|
|
|
|
buffer[ i++ ] = this.matrix[ x ][ y ].r * this.matrix[ x ][ y ].a * this.brightness;
|
|
|
|
buffer[ i++ ] = this.matrix[ x ][ y ].g * this.matrix[ x ][ y ].a * this.brightness;
|
|
|
|
buffer[ i++ ] = this.matrix[ x ][ y ].b * this.matrix[ x ][ y ].a * this.brightness;
|
|
|
|
buffer[ i++ ] = 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 20:34:21 +02:00
|
|
|
// Buffor should be complete here
|
2018-07-03 19:39:50 +02:00
|
|
|
var imageData = {
|
|
|
|
data: buffer,
|
|
|
|
width: this.width,
|
|
|
|
height: this.height
|
|
|
|
};
|
|
|
|
|
2018-07-03 20:34:21 +02:00
|
|
|
var dt = jpeg.encode( imageData, 100 );
|
2018-07-03 19:39:50 +02:00
|
|
|
mjpegReqHandler.write( dt.data );
|
|
|
|
}
|
2018-07-03 20:34:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close all http server requests
|
|
|
|
*/
|
|
|
|
cleanup() {
|
|
|
|
if ( httpServer !== undefined ) {
|
|
|
|
httpServer.close();
|
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:39:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.MotionJPEG = MotionJPEG;
|