Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled stream error in pipe: write EPIPE in Node.js

The idea is to serve screenshots of RTSP video stream with Express.js server. There is a continuously running spawned openRTSP process in flowing mode (it's stdout is consumed by another ffmpeg process):

function spawnProcesses (camera) {
  var openRTSP = spawn('openRTSP', ['-c', '-v', '-t', camera.rtsp_url]),
      encoder = spawn('ffmpeg', ['-i', 'pipe:', '-an', '-vcodec', 'libvpx', '-r', 10, '-f', 'webm', 'pipe:1']);

  openRTSP.stdout.pipe(encoder.stdin);

  openRTSP.on('close', function (code) {
    if (code !== 0) {
      console.log('Encoder process exited with code ' + code);
    }
  });

  encoder.on('close', function (code) {
    if (code !== 0) {
      console.log('Encoder process exited with code ' + code);
    }
  });

  return { rtsp: openRTSP, encoder: encoder };
}

...

camera.proc = spawnProcesses(camera);

There is an Express server with single route:

app.get('/cameras/:id.jpg', function(req, res){
  var camera = _.find(cameras, {id: parseInt(req.params.id, 10)});
  if (camera) {
    res.set({'Content-Type': 'image/jpeg'});
    var ffmpeg = spawn('ffmpeg', ['-i', 'pipe:', '-an', '-vframes', '1', '-s', '800x600', '-f', 'image2', 'pipe:1']);
    camera.proc.rtsp.stdout.pipe(ffmpeg.stdin);
    ffmpeg.stdout.pipe(res);
  } else {
    res.status(404).send('Not found');
  }
});

app.listen(3333);

When i request http://localhost:3333/cameras/1.jpg i get desired image, but from time to time app breaks with error:

stream.js:94
  throw er; // Unhandled stream error in pipe.
        ^
Error: write EPIPE
    at errnoException (net.js:901:11)
    at Object.afterWrite (net.js:718:19)

Strange thing is that sometimes it successfully streams image to res stream and closes child process without any error, but, sometimes, streams image and falls down.

I tried to create on('error', ...) event handlers on every possible stream, tried to change pipe(...) calls to on('data',...) constructions, but could not succeed.

My environment: node v0.10.22, OSX Mavericks 10.9.

UPDATE:

I wrapped spawn('ffmpeg',... block with try-catch:

app.get('/cameras/:id.jpg', function(req, res){
....
    try {
      var ffmpeg = spawn('ffmpeg', ['-i', 'pipe:', '-an', '-vframes', '1', '-s', '800x600', '-f', 'image2', 'pipe:1']);
      camera.proc.rtsp.stdout.pipe(ffmpeg.stdin);
      ffmpeg.stdout.pipe(res);
    } catch (e) {
      console.log("Gotcha!", e);
    }
....
});

... and this error disappeared, but log is silent, it doesn't catch any errors. What's wrong?

like image 416
Michael Romanenko Avatar asked Aug 16 '26 04:08

Michael Romanenko


1 Answers

EPIPE : A write on a pipe, socket, or FIFO for which there is no process to read the data

try to add an error handler then put .on('error', function(e) { ... }) before the call to .pipe() or alter the program to exit successfully in the case of a closed pipe

try :

process.stdout.on('error', function( err ) {
  if (err.code == "EPIPE") {
    process.exit(0);
  }
});
like image 111
fajar k Avatar answered Aug 18 '26 18:08

fajar k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!