Skip to content

Commit

Permalink
Merge pull request #27 from igkvl/master
Browse files Browse the repository at this point in the history
Update rtsp-ffmpeg
  • Loading branch information
agsh authored Aug 16, 2018
2 parents 0e0b12a + 93eacce commit 08a8566
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions lib/rtsp-ffmpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const spawn = require('child_process').spawn
, EventEmitter = require('events').EventEmitter
, util = require('util')
, Buffer = require('buffer').Buffer
;

/**
Expand All @@ -27,7 +28,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.buff = new Buffer(''); // 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 @@ -87,21 +88,19 @@ FFMpeg.prototype.start = function() {
var self = this;
this.child = spawn(FFMpeg.cmd, this._args());
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]);
}
}
//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.
if (data.length >1) {
self.buff = Buffer.concat([self.buff, data]);

offset = data[data.length-2].toString(16);
offset2 = data[data.length-1].toString(16);

if(offset == "ff" && offset2 == "d9") {
self.emit('data', self.buff);
self.buff = new Buffer('');
}
}
});
this.child.stderr.on('data', function(data) {
throw new Error(data);
Expand Down

0 comments on commit 08a8566

Please sign in to comment.