Skip to content

Commit

Permalink
Camera: further streaming optimizations
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Mar 23, 2016
1 parent 7bff719 commit 2515876
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 13 deletions.
61 changes: 56 additions & 5 deletions lib/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var Readable = require('stream').Readable;
var priv = new Map();

function scale(value, fromLow, fromHigh, toLow, toHigh) {
return (value - fromLow) * (toHigh - toLow) /
(fromHigh - fromLow) + toLow;
return ((value - fromLow) * (toHigh - toLow) /
(fromHigh - fromLow) + toLow) | 0;
}

function constrain(value, lower, upper) {
Expand Down Expand Up @@ -113,27 +113,41 @@ Camera.prototype.capture = function(options) {
state.output = cameraDefaults.output;
}

if (state.stream && state.quality > 0.9) {
state.quality = 0.9;
}

var args = [
// Overwrite: yes
'-y',
// Logging: fatal
'-v', 'fatal',
'-r', '30',

// Input...
'-f', 'v4l2',
'-r', 4,
'-s', `${state.width}x${state.height}`,
'-i', state.path,

// Output...
'-s', `${state.width}x${state.height}`,
'-q:v', scale(state.quality, 0, 1, 30, 1),
];

if (state.stream) {
// args.splice(2, 0, '-f', 'v4l2');

args.push(
// '-r', '30',
// '-r', 4,
// '-i', '/dev/video0',
// '-y',
// '-s', '320x240',
'-f', 'MJPEG',
// '-f', 'h264',
'-b:v', '64k',
'-r', '30',
// '-maxrate', '64k',
// '-movflags', '+faststart',
'-r', 4,
'pipe:1'
);
} else {
Expand All @@ -155,13 +169,26 @@ Camera.prototype.capture = function(options) {
}
}


if (state.process && state.isStreaming) {
return this;
}

state.process = cp.spawn('ffmpeg', args);

if (state.stream) {
state.isStreaming = true;
}

state.process.stdout.on('data', (data) => {
if (!data) {
return;
}

if (data && !data.length) {
return;
}

if (state.isStreaming) {
cs.push(data);
this.emit('data', data);
Expand All @@ -175,6 +202,14 @@ Camera.prototype.capture = function(options) {

state.isStreaming = false;

if (!buffer) {
return;
}

if (buffer && !buffer.length) {
return;
}

if (error) {
this.emit('error', error);
} else {
Expand Down Expand Up @@ -208,6 +243,22 @@ Camera.prototype.stream = function() {
});
};

Camera.prototype.stop = function() {
var state = priv.get(this);
if (!state.isStreaming) {
return this;
}
state.isStreaming = false;

if (state.process) {
state.process.kill('SIGTERM');
state.process = null;
}
this.emit('stop');
return this;
};


module.exports = Camera;

if (global.IS_TEST_ENV) {
Expand Down
35 changes: 27 additions & 8 deletions test/unit/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ exports['av.Camera'] = {
'-y',
'-v',
'fatal',
'-f',
'v4l2',
'-r',
'30',
4,
'-s',
'320x240',
'-i',
'/dev/video0',
'-s',
Expand Down Expand Up @@ -127,8 +131,12 @@ exports['av.Camera'] = {
'-y',
'-v',
'fatal',
'-f',
'v4l2',
'-r',
'30',
4,
'-s',
'320x240',
'-i',
'/dev/video0',
'-s',
Expand Down Expand Up @@ -160,8 +168,12 @@ exports['av.Camera'] = {
'-y',
'-v',
'fatal',
'-f',
'v4l2',
'-r',
'30',
4,
'-s',
'320x240',
'-i',
'/dev/video0',
'-s',
Expand Down Expand Up @@ -193,8 +205,12 @@ exports['av.Camera'] = {
'-y',
'-v',
'fatal',
'-f',
'v4l2',
'-r',
'30',
4,
'-s',
'320x240',
'-i',
'/dev/video0',
'-s',
Expand Down Expand Up @@ -253,27 +269,30 @@ exports['av.Camera'] = {

test.equal(s instanceof CaptureStream, true);
test.equal(s instanceof Readable, true);

test.equal(this.spawn.callCount, 1);
test.equal(this.spawn.lastCall.args[0], 'ffmpeg');
test.deepEqual(this.spawn.lastCall.args[1], [
'-y',
'-v',
'fatal',
'-f',
'v4l2',
'-r',
'30',
4,
'-s',
'320x240',
'-i',
'/dev/video0',
'-s',
'320x240',
'-q:v',
1,
3,
'-f',
'MJPEG',
'-b:v',
'64k',
'-r',
'30',
4,
'pipe:1',
]);

Expand Down

0 comments on commit 2515876

Please sign in to comment.