Skip to content

Commit

Permalink
Build full data image before displaying the image instead of displayi…
Browse files Browse the repository at this point in the history
…ng an image that can be a chunk of the full image in some case
  • Loading branch information
Zombitch committed May 25, 2018
1 parent e43cf6d commit ecbf668
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion 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 @@ -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

15 comments on commit ecbf668

@igkvl
Copy link
Contributor

@igkvl igkvl commented on ecbf668 Aug 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend you to change code with Buffer, for instance:

const spawn = require('child_process').spawn
    , EventEmitter = require('events').EventEmitter
    , util = require('util')
    , Buffer = require('buffer').Buffer
    ;

var FFMpeg = function(options) {
...
    this.buff = new Buffer('');
...
}

 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('');
      }
  }

/*
  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,'binary').toString('base64'));
    self.emit('data', self.imageData);
    self.imageData = [];
      }else{
    self.imageData.push(data[idx]);
      }
  }
*/

for receiving images without color corruption.

Thank you for simple solution with ffmpeg.

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for your purpose! Does this solution helps to avoid blank frames? If it so, your commit is very helpful.

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if it so, can you make a PR?)

@igkvl
Copy link
Contributor

@igkvl igkvl commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, test of data.length removes blinks and Buffer removes image corruption,

@igkvl
Copy link
Contributor

@igkvl igkvl commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR - you mean some feedback ?

@Seikon
Copy link
Collaborator

@Seikon Seikon commented on ecbf668 Aug 15, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, пришли, пожалуйста пул реквест со своими исправлениями.Я с удовольствием смерджу. Не забудь себя вписать в соавторы в package.json

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Seikon you've done a lot of work, thank you. Can you add yourself as the contributor to the package.json ?

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Seikon also I thought that we can handle frames with buffer object, to prevent converting it to string, an I right?

@Seikon
Copy link
Collaborator

@Seikon Seikon commented on ecbf668 Aug 16, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thank you, I'll try to do this, but I also haven't possibility to test it
@igkvl can you help us?)

@igkvl
Copy link
Contributor

@igkvl igkvl commented on ecbf668 Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have an old OS. I just tried to start an explosive mixture x64 IIS8+iisnode+node+socket.io, it's not working, because iisnode.

But I found error with Buffer:

[DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I found error with Buffer:

Where? Buffer API changed a lot of time since node 0.8 version. I'll try to fix this warning, I've written this code a long time ago.

@igkvl
Copy link
Contributor

@igkvl igkvl commented on ecbf668 Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning was shown on windows with iis+iisnode

@agsh
Copy link
Owner

@agsh agsh commented on ecbf668 Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked at issnode, seems that they have node.js version >=6.0. Fixed it in package.json. Thanks.

Please sign in to comment.