Skip to content

Commit

Permalink
Merge pull request #25 from Zombitch/master
Browse files Browse the repository at this point in the history
Replace updateFirst by update & Wait for full data image in order to display it
  • Loading branch information
agsh authored May 26, 2018
2 parents 34a68c0 + ecbf668 commit f17ffbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion example/server-canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ io.on('connection', function(socket) {

app.get('/', function (req, res) {
res.sendFile(__dirname + '/index-canvas.html');
});
});
22 changes: 20 additions & 2 deletions lib/rtsp-ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var FFMpeg = function(options) {
this.resolution = options.resolution;
this.quality = (options.quality === undefined || options.quality === "") ? 3 : options.quality;
this.arguments = options.arguments || [];
this.imageData = []; // Store the entire data image into this variable. This attribute is replaced each time a full image is received from the stream.

this.on('newListener', newListener.bind(this));
this.on('removeListener', removeListener.bind(this));
Expand Down Expand Up @@ -73,7 +74,7 @@ FFMpeg.prototype._args = function() {
//, '-vf', 'fps=25'
//, '-b:v', '32k'
'-f', 'image2'
, '-updatefirst', '1'
, '-update', '1'
, '-'
]
, this.resolution ? ['-s', this.resolution] : []);
Expand All @@ -83,8 +84,25 @@ FFMpeg.prototype._args = function() {
* Start ffmpeg spawn process
*/
FFMpeg.prototype.start = function() {
var self = this;
this.child = spawn(FFMpeg.cmd, this._args());
this.child.stdout.on('data', this.emit.bind(this, 'data'));
this.child.stdout.on('data', function(data){
//The image can be composed of one or multiple chunk when receiving stream data.
//Store all bytes into an array until we meet flag "FF D9" that mean it's the end of the image then we can send all data in order to display the full image.
for(var idx = 0; idx < data.length-1;idx++){
offset = data[idx].toString(16);
offset2 = data[idx+1].toString(16);

if(offset == "ff" && offset2 == "d9"){
self.imageData.push(data[idx]);
self.imageData.push(data[idx+1]);
self.emit('data', Buffer.from(self.imageData));
self.imageData = [];
}else{
self.imageData.push(data[idx]);
}
}
});
this.child.stderr.on('data', function(data) {
throw new Error(data);
});
Expand Down

0 comments on commit f17ffbf

Please sign in to comment.