Skip to content

Commit

Permalink
Ensure that cam.capture() can be called more than once for an instance.
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 21, 2017
1 parent 03ca47e commit 24a1b41
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
22 changes: 13 additions & 9 deletions lib/camera.js
Expand Up @@ -123,8 +123,10 @@ class Camera extends Stream {
]);
},
stop() {
state.process.kill('SIGTERM');
state.process = null;
if (state.process) {
state.process.kill('SIGTERM');
state.process = null;
}
}
};

Expand Down Expand Up @@ -170,20 +172,22 @@ class Camera extends Stream {
state.stream = null;
this.stream();
});
}

let incoming = state.stream.pipe(new MjpegConsumer());

incoming.on('data', frame => state.frame = frame);
incoming.on('error', () => this.stream());
incoming.pipe(this);
let incoming = state.stream.pipe(new MjpegConsumer());

incoming.on('data', frame => state.frame = frame);
incoming.on('error', () => {
state.stream = null;
this.stream();
});
incoming.pipe(this);
}
return this;
}

capture() {
let cs = new CaptureStream(this);

this.stream();
this.once('data', data => {
cs.push(data);
cs.push(null);
Expand Down
33 changes: 32 additions & 1 deletion test/unit/camera.js
Expand Up @@ -68,7 +68,7 @@ exports['av.Camera'] = {
});

cam.on('stop', () => {
test.equal(this.write.lastCall.args[0], buffer);
test.ok(this.write.lastCall.args[0].equals(buffer));
test.equal(this.write.callCount, 1);
// This is null because we bypassed the frame setting
// mechanism below when `cam.emit('data', buffer);`
Expand All @@ -80,6 +80,37 @@ exports['av.Camera'] = {
cam.emit('data', buffer);
},

captureMultiple(test) {
test.expect(8);

var buffer = new Buffer([0]);
var cam = new av.Camera();
var writable = new Writable();

this.sandbox.spy(cam, 'capture');
this.sandbox.spy(cam, 'stream');

cam.on('stop', () => {
test.ok(buffer.equals(buffer));
buffer.writeInt8(cam.capture.callCount, 0);

if (cam.capture.callCount === 4) {
test.equal(cam.stream.callCount, 4);
test.done();
} else {
let cs = cam.capture();
cam.emit('data', buffer);

test.equal(cs.read(1).readUInt8(0), cam.capture.callCount - 1);

cs.pipe(writable);
}
});

cam.capture().pipe(writable);
cam.emit('data', buffer);
},

spawned(test) {
test.expect(3);

Expand Down

0 comments on commit 24a1b41

Please sign in to comment.