2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ipcamera.internal;
15 import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.*;
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
28 import org.eclipse.jdt.annotation.NonNullByDefault;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.FFmpegFormat;
31 import org.openhab.binding.ipcamera.internal.handler.IpCameraHandler;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link Ffmpeg} class is responsible for handling multiple ffmpeg conversions which are used for many tasks
41 * @author Matthew Skinner - Initial contribution
46 private final Logger logger = LoggerFactory.getLogger(getClass());
47 private IpCameraHandler ipCameraHandler;
48 private @Nullable Process process = null;
49 private String ffmpegCommand = "";
50 private FFmpegFormat format;
51 private List<String> commandArrayList = new ArrayList<String>();
52 private IpCameraFfmpegThread ipCameraFfmpegThread = new IpCameraFfmpegThread();
53 private int keepAlive = 8;
54 private boolean running = false;
55 private String password;
57 public Ffmpeg(IpCameraHandler handle, FFmpegFormat format, String ffmpegLocation, String inputArguments,
58 String input, String outArguments, String output, String username, String password) {
60 this.password = password;
61 ipCameraHandler = handle;
62 String altInput = input;
63 // Input can be snapshots not just rtsp or http
64 if (!password.isEmpty() && !input.contains("@") && input.contains("rtsp")) {
65 String credentials = username + ":" + password + "@";
66 // will not work for https: but currently binding does not use https
67 altInput = input.substring(0, 7) + credentials + input.substring(7);
69 if (inputArguments.isEmpty()) {
70 ffmpegCommand = "-i " + altInput + " " + outArguments + " " + output;
72 ffmpegCommand = inputArguments + " -i " + altInput + " " + outArguments + " " + output;
74 Collections.addAll(commandArrayList, ffmpegCommand.trim().split("\\s+"));
75 // ffmpegLocation may have a space in its folder
76 commandArrayList.add(0, ffmpegLocation);
79 public void setKeepAlive(int numberOfEightSeconds) {
80 // We poll every 8 seconds due to mjpeg stream requirement.
81 if (keepAlive == -1 && numberOfEightSeconds > 1) {
82 return;// When set to -1 this will not auto turn off stream.
84 keepAlive = numberOfEightSeconds;
87 public void checkKeepAlive() {
88 if (keepAlive <= -1) {
90 } else if (--keepAlive == 0) {
95 private class IpCameraFfmpegThread extends Thread {
96 private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
97 public int countOfMotions;
99 IpCameraFfmpegThread() {
103 private void gifCreated() {
104 // Without a small delay, Pushover sends no file 10% of time.
105 ipCameraHandler.setChannelState(CHANNEL_RECORDING_GIF, DecimalType.ZERO);
106 ipCameraHandler.setChannelState(CHANNEL_GIF_HISTORY_LENGTH,
107 new DecimalType(++ipCameraHandler.gifHistoryLength));
110 private void mp4Created() {
111 ipCameraHandler.setChannelState(CHANNEL_RECORDING_MP4, DecimalType.ZERO);
112 ipCameraHandler.setChannelState(CHANNEL_MP4_HISTORY_LENGTH,
113 new DecimalType(++ipCameraHandler.mp4HistoryLength));
119 process = Runtime.getRuntime().exec(commandArrayList.toArray(new String[commandArrayList.size()]));
120 Process localProcess = process;
121 if (localProcess != null) {
122 InputStream errorStream = localProcess.getErrorStream();
123 InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
124 BufferedReader bufferedReader = new BufferedReader(errorStreamReader);
126 while ((line = bufferedReader.readLine()) != null) {
127 if (format.equals(FFmpegFormat.RTSP_ALARMS)) {
128 logger.debug("{}", line);
129 if (line.contains("lavfi.")) {
130 if (countOfMotions == 4) {
131 ipCameraHandler.motionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
135 } else if (line.contains("speed=")) {
136 if (countOfMotions > 0) {
139 if (countOfMotions <= 0) {
140 ipCameraHandler.noMotionDetected(CHANNEL_FFMPEG_MOTION_ALARM);
143 } else if (line.contains("silence_start")) {
144 ipCameraHandler.noAudioDetected();
145 } else if (line.contains("silence_end")) {
146 ipCameraHandler.audioDetected();
149 logger.debug("{}", line);
153 } catch (IOException e) {
154 logger.warn("An error occured trying to process the messages from FFmpeg.");
158 threadPool.schedule(this::gifCreated, 800, TimeUnit.MILLISECONDS);
161 threadPool.schedule(this::mp4Created, 800, TimeUnit.MILLISECONDS);
170 public void startConverting() {
171 if (!ipCameraFfmpegThread.isAlive()) {
172 ipCameraFfmpegThread = new IpCameraFfmpegThread();
173 logger.debug("Starting ffmpeg with this command now:{}", ffmpegCommand.replaceAll(password, "********"));
174 ipCameraFfmpegThread.start();
176 if (format.equals(FFmpegFormat.HLS)) {
177 ipCameraHandler.setChannelState(CHANNEL_START_STREAM, OnOffType.ON);
180 if (keepAlive != -1) {
185 public boolean getIsAlive() {
189 public void stopConverting() {
190 if (ipCameraFfmpegThread.isAlive()) {
191 logger.debug("Stopping ffmpeg {} now when keepalive is:{}", format, keepAlive);
192 Process localProcess = process;
193 if (localProcess != null) {
194 localProcess.destroyForcibly();
197 if (format.equals(FFmpegFormat.HLS)) {
198 if (keepAlive == -1) {
199 logger.warn("HLS stopped when Stream should be running non stop, restarting HLS now.");
203 ipCameraHandler.setChannelState(CHANNEL_START_STREAM, OnOffType.OFF);