Skip to content

Commit

Permalink
Add callback for ffmpeg process exit (homebridge-plugins#154)
Browse files Browse the repository at this point in the history
* Add callback for ffmpeg process exit
* Report error when ffmpeg fails
* Use SIGTERM to quit ffmpeg

* Force stop the stream controller when ffmpeg fails

* Change data object comparison from noob to pro

* Small fixes in README

* Make sure we use the right self

* Let there be let
  • Loading branch information
normen authored and KhaosT committed Mar 5, 2018
1 parent f11d890 commit 982e42a
Showing 2 changed files with 17 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ ffmpeg plugin for [Homebridge](https://github.com/nfarina/homebridge)
* `maxWidth` is the maximum width reported to HomeKit, default 1280
* `maxHeight` is the maximum height reported to HomeKit, default 720
* `maxFPS` is the maximum frame rate of the stream, default 10
* `maxBitrate` is the maximum frame rate of the stream in kbit/s, default 300
* `maxBitrate` is the maximum bit rate of the stream in kbit/s, default 300
* `vcodec` If you're running on a RPi with the omx version of ffmpeg installed, you can change to the hardware accelerated video codec with this option, default "libx264"
* `audio` can be set to true to enable audio streaming from camera. To use audio ffmpeg must be compiled with --enable-libfdk-aac, see https://github.com/KhaosT/homebridge-camera-ffmpeg/wiki, default false
* `packetSize` If audio or video is choppy try a smaller value, set to a multiple of 188, default 1316
@@ -54,6 +54,7 @@ ffmpeg plugin for [Homebridge](https://github.com/nfarina/homebridge)
"maxWidth": 1280,
"maxHeight": 720,
"maxFPS": 30,
"maxBitrate": 200,
"vcodec": "h264_omx",
"audio": true,
"packetSize": 188,
18 changes: 15 additions & 3 deletions ffmpeg.js
Original file line number Diff line number Diff line change
@@ -326,17 +326,29 @@ FFMPEG.prototype.handleStreamRequest = function(request) {
console.log(data.toString());
});
}
let self = this;
ffmpeg.on('close', (code) => {
if(code == null || code == 0 || code == 255){
self.log("Stopped streaming");
} else {
self.log("ERROR: FFmpeg exited with code " + code);
for(var i=0; i < self.streamControllers.length; i++){
var controller = self.streamControllers[i];
if(controller.sessionIdentifier === sessionID){
controller.forceStop();
}
}
}
});
this.ongoingSessions[sessionIdentifier] = ffmpeg;
}

delete this.pendingSessions[sessionIdentifier];
} else if (requestType == "stop") {
var ffmpegProcess = this.ongoingSessions[sessionIdentifier];
if (ffmpegProcess) {
ffmpegProcess.kill('SIGKILL');
this.log("Stopped ffmpeg");
ffmpegProcess.kill('SIGTERM');
}

delete this.ongoingSessions[sessionIdentifier];
}
}

0 comments on commit 982e42a

Please sign in to comment.