Skip to content

Commit

Permalink
[ipcamera] Add a check to see if FFmpeg is frozen for mjpeg creation (o…
Browse files Browse the repository at this point in the history
…penhab#13896)

* check if ffmpegMjpeg is frozen

Signed-off-by: Matthew Skinner <matt@pcmus.com>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
Skinah authored and andrasU committed Jan 5, 2024
1 parent 20e95a3 commit 47824ff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
Expand Down Expand Up @@ -39,6 +39,7 @@
/**
* The {@link Ffmpeg} class is responsible for handling multiple ffmpeg conversions which are used for many tasks
*
*
* @author Matthew Skinner - Initial contribution
*/

Expand Down Expand Up @@ -81,15 +82,15 @@ public Ffmpeg(IpCameraHandler handle, FFmpegFormat format, String ffmpegLocation
public void setKeepAlive(int numberOfEightSeconds) {
// We poll every 8 seconds due to mjpeg stream requirement.
if (keepAlive == -1 && numberOfEightSeconds > 1) {
return; // When set to -1 this will not auto turn off stream.
return;// When set to -1 this will not auto turn off stream.
}
keepAlive = numberOfEightSeconds;
}

public void checkKeepAlive() {
if (keepAlive == 1) {
stopConverting();
} else if (keepAlive <= -1 && !isAlive()) {
} else if (keepAlive <= -1 && !getIsAlive()) {
logger.warn("HLS stream was not running, restarting it now.");
startConverting();
}
Expand Down Expand Up @@ -123,63 +124,63 @@ private void mp4Created() {
public void run() {
try {
process = Runtime.getRuntime().exec(commandArrayList.toArray(new String[commandArrayList.size()]));

InputStream errorStream = process.getErrorStream();
InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
BufferedReader bufferedReader = new BufferedReader(errorStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
logger.trace("{}", line);
switch (format) {
case RTSP_ALARMS:
if (line.contains("lavfi.")) {
// When the number of pixels that change are below the noise floor we need to look
// across frames to confirm it is motion and not noise.
if (countOfMotions < 10) { // Stop increasing otherwise it takes too long to go OFF
countOfMotions++;
}
if (countOfMotions > 9) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 4 && ipCameraHandler.motionThreshold.intValue() > 10) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 3 && ipCameraHandler.motionThreshold.intValue() > 15) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 2 && ipCameraHandler.motionThreshold.intValue() > 30) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 0 && ipCameraHandler.motionThreshold.intValue() > 89) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
countOfMotions = 4; // Used to debounce the Alarm.
}
} else if (line.contains("speed=")) {
if (countOfMotions > 0) {
if (ipCameraHandler.motionThreshold.intValue() > 89) {
countOfMotions--;
Process localProcess = process;
if (localProcess != null) {
InputStream errorStream = localProcess.getErrorStream();
InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
BufferedReader bufferedReader = new BufferedReader(errorStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
logger.trace("{}", line);
switch (format) {
case RTSP_ALARMS:
if (line.contains("lavfi.")) {
// When the number of pixels that change are below the noise floor we need to look
// across frames to confirm it is motion and not noise.
if (countOfMotions < 10) {// Stop increasing otherwise it takes too long to go OFF
countOfMotions++;
}
if (ipCameraHandler.motionThreshold.intValue() > 10) {
countOfMotions -= 2;
} else {
countOfMotions -= 4;
if (countOfMotions > 9) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 4 && ipCameraHandler.motionThreshold.intValue() > 10) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 3 && ipCameraHandler.motionThreshold.intValue() > 15) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 2 && ipCameraHandler.motionThreshold.intValue() > 30) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
} else if (countOfMotions > 0 && ipCameraHandler.motionThreshold.intValue() > 89) {
ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
countOfMotions = 4;// Used to debounce the Alarm.
}
if (countOfMotions <= 0) {
ipCameraHandler.noMotionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
countOfMotions = 0;
} else if (line.contains("speed=")) {
if (countOfMotions > 0) {
if (ipCameraHandler.motionThreshold.intValue() > 89) {
countOfMotions--;
}
if (ipCameraHandler.motionThreshold.intValue() > 10) {
countOfMotions -= 2;
} else {
countOfMotions -= 4;
}
if (countOfMotions <= 0) {
ipCameraHandler.noMotionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
countOfMotions = 0;
}
}
} else if (line.contains("silence_start")) {
ipCameraHandler.noAudioDetected();
} else if (line.contains("silence_end")) {
ipCameraHandler.audioDetected();
}
} else if (line.contains("silence_start")) {
ipCameraHandler.noAudioDetected();
} else if (line.contains("silence_end")) {
ipCameraHandler.audioDetected();
}
case MJPEG:
case SNAPSHOT:
notFrozen = true; // RTSP_ALARMS, MJPEG and SNAPSHOT all set this to true, no break.
break;
default:
break;
case MJPEG:
case SNAPSHOT:
notFrozen = true;// RTSP_ALARMS, MJPEG and SNAPSHOT all set this to true, no break.
break;
}
}
}
} catch (IOException e) {
logger.warn("An IO error occurred trying to start FFmpeg: {}", e.getMessage());
logger.warn("An IO error occured trying to start FFmpeg:{}", e.getMessage());
} finally {
switch (format) {
case GIF:
Expand All @@ -199,10 +200,10 @@ public void startConverting() {
if (!ipCameraFfmpegThread.isAlive()) {
ipCameraFfmpegThread = new IpCameraFfmpegThread();
if (!password.isEmpty()) {
logger.debug("Starting ffmpeg with this command now: {}",
logger.debug("Starting ffmpeg with this command now:{}",
ffmpegCommand.replaceAll(password, "********"));
} else {
logger.debug("Starting ffmpeg with this command now: {}", ffmpegCommand);
logger.debug("Starting ffmpeg with this command now:{}", ffmpegCommand);
}
ipCameraFfmpegThread.start();
if (format.equals(FFmpegFormat.HLS)) {
Expand All @@ -214,7 +215,7 @@ public void startConverting() {
}
}

public boolean isAlive() {
public boolean getIsAlive() {
Process localProcess = process;
if (localProcess != null) {
if (localProcess.isAlive() && notFrozen) {
Expand All @@ -227,7 +228,7 @@ public boolean isAlive() {

public void stopConverting() {
if (ipCameraFfmpegThread.isAlive()) {
logger.debug("Stopping ffmpeg {} now when keepalive is: {}", format, keepAlive);
logger.debug("Stopping ffmpeg {} now when keepalive is:{}", format, keepAlive);
Process localProcess = process;
if (localProcess != null) {
localProcess.destroyForcibly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object ms
&& content.contains("response=\"200\";")) {// new
ipCameraHandler.newInstarApi = true;
ipCameraHandler.logger.debug("Alarm server sucessfully setup for a 2k+ Instar camera");
if (ipCameraHandler.cameraConfig.getFfmpegInput().isEmpty()) {
ipCameraHandler.rtspUri = "rtsp://" + ipCameraHandler.cameraConfig.getIp()
+ "/livestream/12";
}
if (ipCameraHandler.cameraConfig.getMjpegUrl().isEmpty()) {
ipCameraHandler.mjpegUri = "/livestream/12?action=play&media=mjpeg";
}
if (ipCameraHandler.cameraConfig.getSnapshotUrl().isEmpty()) {
ipCameraHandler.snapshotUri = "/snap.cgi?chn=12";
}
} else if (requestUrl.startsWith("/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=1")
&& content.startsWith("[Succeed]set ok")) {
ipCameraHandler.newInstarApi = false;
Expand Down

0 comments on commit 47824ff

Please sign in to comment.