import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
+import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public @Nullable Ffmpeg ffmpegSnapshot = null;
public boolean streamingAutoFps = false;
public boolean motionDetected = false;
-
+ public Instant currentSnapshotTime = Instant.now();
private @Nullable ScheduledFuture<?> cameraConnectionJob = null;
private @Nullable ScheduledFuture<?> pollCameraJob = null;
private @Nullable ScheduledFuture<?> snapshotJob = null;
private @Nullable Bootstrap mainBootstrap;
- private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup();
+ private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup(1);
private FullHttpRequest putRequestWithBody = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, new HttpMethod("PUT"),
"");
private String gifFilename = "ipcamera";
basicAuth = "";
return false;
} else if (!basicAuth.isEmpty()) {
- // due to camera may have been sent multiple requests before the auth was set, this may trigger falsely.
+ // If the binding sends multiple requests before basicAuth was set, this may trigger falsely.
logger.warn("Camera is reporting your username and/or password is wrong.");
return false;
}
}
} finally {
lockCurrentSnapshot.unlock();
+ currentSnapshotTime = Instant.now();
}
if (updateImageChannel) {
}
public void startStreamServer() {
- if (servlet == null) {
- servlet = new CameraServlet(this, httpService);
- }
-
+ servlet = new CameraServlet(this, httpService);
updateState(CHANNEL_HLS_URL, new StringType("http://" + hostIp + ":" + SERVLET_PORT + "/ipcamera/"
+ getThing().getUID().getId() + "/ipcamera.m3u8"));
updateState(CHANNEL_IMAGE_URL, new StringType("http://" + hostIp + ":" + SERVLET_PORT + "/ipcamera/"
sendHttpGET(mjpegUri);
}
- void openChannel(Channel channel, String httpRequestURL) {
+ private void openChannel(Channel channel, String httpRequestURL) {
ChannelTracking tracker = channelTrackingMap.get(httpRequestURL);
if (tracker != null && !tracker.getReply().isEmpty()) {// We need to keep the stored reply
tracker.setChannel(channel);
if (localFuture != null) {
localFuture.cancel(false);
}
- if (thing.getThingTypeUID().getId().equals(INSTAR_THING)) {
- logger.debug("Setting up the Alarm Server settings in the camera now");
- sendHttpGET(
- "/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=1&cmd=setalarmserverattr&-as_index=3&-as_server="
- + hostIp + "&-as_port=" + SERVLET_PORT + "&-as_path=/ipcamera/"
- + getThing().getUID().getId()
- + "/instar&-as_queryattr1=&-as_queryval1=&-as_queryattr2=&-as_queryval2=&-as_queryattr3=&-as_queryval3=&-as_activequery=1&-as_auth=0&-as_query1=0&-as_query2=0&-as_query3=0");
- }
if (cameraConfig.getGifPreroll() > 0 || cameraConfig.getUpdateImageWhen().contains("1")) {
snapshotPolling = true;
snapshotJob = threadPool.scheduleWithFixedDelay(this::snapshotRunnable, 1000, cameraConfig.getPollTime(),
}
public byte[] getSnapshot() {
+ if (!isOnline) {
+ // Keep streams open when the camera goes offline so they dont stop.
+ return new byte[] { (byte) 0xff, (byte) 0xd8, (byte) 0xff, (byte) 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
+ 0x00, 0x01, 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, (byte) 0xff, (byte) 0xdb, 0x00, 0x43,
+ 0x00, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04,
+ 0x06, 0x04, 0x04, 0x04, 0x04, 0x04, 0x08, 0x06, 0x06, 0x05, 0x06, 0x09, 0x08, 0x0a, 0x0a, 0x09,
+ 0x08, 0x09, 0x09, 0x0a, 0x0c, 0x0f, 0x0c, 0x0a, 0x0b, 0x0e, 0x0b, 0x09, 0x09, 0x0d, 0x11, 0x0d,
+ 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x10, 0x0a, 0x0c, 0x12, 0x13, 0x12, 0x10, 0x13, 0x0f, 0x10, 0x10,
+ 0x10, (byte) 0xff, (byte) 0xc9, 0x00, 0x0b, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01, 0x01, 0x11, 0x00,
+ (byte) 0xff, (byte) 0xcc, 0x00, 0x06, 0x00, 0x10, 0x10, 0x05, (byte) 0xff, (byte) 0xda, 0x00, 0x08,
+ 0x01, 0x01, 0x00, 0x00, 0x3f, 0x00, (byte) 0xd2, (byte) 0xcf, 0x20, (byte) 0xff, (byte) 0xd9 };
+ }
if (!snapshotPolling && !ffmpegSnapshotGeneration) {
sendHttpGET(snapshotUri);
}
*
*/
void pollCameraRunnable() {
- // Snapshot should be first to keep consistent time between shots
- if (streamingAutoFps) {
- if (!snapshotPolling && !ffmpegSnapshotGeneration) {
- // Dont need to poll if creating from RTSP stream with FFmpeg or we are polling at full rate already.
- sendHttpGET(snapshotUri);
- }
- } else if (!snapshotUri.isEmpty() && !snapshotPolling) {// we need to check camera is still online.
+ if (!snapshotUri.isEmpty() && !snapshotPolling) {// we need to check camera is still online.
checkCameraConnection();
}
// NOTE: Use lowPriorityRequests if get request is not needed every poll.
@Override
public void initialize() {
cameraConfig = getConfigAs(CameraConfig.class);
+ threadPool = Executors.newScheduledThreadPool(4);
+ mainEventLoopGroup = new NioEventLoopGroup(3);
snapshotUri = getCorrectUrlFormat(cameraConfig.getSnapshotUrl());
mjpegUri = getCorrectUrlFormat(cameraConfig.getMjpegUrl());
rtspUri = cameraConfig.getFfmpegInput();
if (mjpegUri.isEmpty()) {
mjpegUri = "/mjpegstream.cgi?-chn=12";
}
+ sendHttpGET(
+ "/param.cgi?cmd=setmdalarm&-aname=server2&-switch=on&-interval=1&cmd=setalarmserverattr&-as_index=3&-as_server="
+ + hostIp + "&-as_port=" + SERVLET_PORT + "&-as_path=/ipcamera/"
+ + getThing().getUID().getId()
+ + "/instar&-as_queryattr1=&-as_queryval1=&-as_queryattr2=&-as_queryval2=&-as_queryattr3=&-as_queryval3=&-as_activequery=1&-as_auth=0&-as_query1=0&-as_query2=0&-as_query3=0");
break;
}
- // Onvif and Instar event handling need the host IP and the server started.
+ // for poll times 9 seconds and above don't display a warning about the Image channel.
+ if (9000 > cameraConfig.getPollTime() && cameraConfig.getUpdateImageWhen().contains("1")) {
+ logger.warn(
+ "The Image channel is set to update more often than 8 seconds. This is not recommended. The Image channel is best used only for higher poll times. See the readme file on how to display the cameras picture for best results or use a higher poll time.");
+ }
+ // ONVIF and Instar event handling need the server started before connecting.
startStreamServer();
+ tryConnecting();
+ }
+ private void tryConnecting() {
if (!thing.getThingTypeUID().getId().equals(GENERIC_THING)) {
onvifCamera = new OnvifConnection(this, cameraConfig.getIp() + ":" + cameraConfig.getOnvifPort(),
cameraConfig.getUser(), cameraConfig.getPassword());
// Only use ONVIF events if it is not an API camera.
onvifCamera.connect(thing.getThingTypeUID().getId().equals(ONVIF_THING));
}
-
- // for poll times 9 seconds and above don't display a warning about the Image channel.
- if (9000 > cameraConfig.getPollTime() && cameraConfig.getUpdateImageWhen().contains("1")) {
- logger.warn(
- "The Image channel is set to update more often than 8 seconds. This is not recommended. The Image channel is best used only for higher poll times. See the readme file on how to display the cameras picture for best results or use a higher poll time.");
- }
- // Waiting 3 seconds for ONVIF to discover the urls before running.
cameraConnectionJob = threadPool.scheduleWithFixedDelay(this::pollingCameraConnection, 4, 30, TimeUnit.SECONDS);
}
// What the camera needs to re-connect if the initialize() is not called.
private void resetAndRetryConnecting() {
- dispose();
- initialize();
+ offline();
+ tryConnecting();
}
- @Override
- public void dispose() {
+ private void offline() {
isOnline = false;
snapshotPolling = false;
Future<?> localFuture = pollCameraJob;
if (localFuture != null) {
localFuture.cancel(true);
+ localFuture = null;
}
localFuture = snapshotJob;
if (localFuture != null) {
localFuture.cancel(true);
+ localFuture = null;
}
localFuture = cameraConnectionJob;
if (localFuture != null) {
localFuture.cancel(true);
+ localFuture = null;
}
- threadPool.shutdown();
- threadPool = Executors.newScheduledThreadPool(4);
-
- groupTracker.listOfOnlineCameraHandlers.remove(this);
- groupTracker.listOfOnlineCameraUID.remove(getThing().getUID().getId());
- // inform all group handlers that this camera has gone offline
- for (IpCameraGroupHandler handle : groupTracker.listOfGroupHandlers) {
- handle.cameraOffline(this);
- }
- basicAuth = ""; // clear out stored Password hash
- useDigestAuth = false;
- openChannels.close();
-
Ffmpeg localFfmpeg = ffmpegHLS;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
- localFfmpeg = null;
+ ffmpegHLS = null;
}
localFfmpeg = ffmpegRecord;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
+ ffmpegRecord = null;
}
localFfmpeg = ffmpegGIF;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
+ ffmpegGIF = null;
}
localFfmpeg = ffmpegRtspHelper;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
+ ffmpegRtspHelper = null;
}
localFfmpeg = ffmpegMjpeg;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
+ ffmpegMjpeg = null;
}
localFfmpeg = ffmpegSnapshot;
if (localFfmpeg != null) {
localFfmpeg.stopConverting();
+ ffmpegSnapshot = null;
}
- channelTrackingMap.clear();
onvifCamera.disconnect();
+ openChannels.close();
+ }
+
+ @Override
+ public void dispose() {
+ offline();
+ CameraServlet localServlet = servlet;
+ if (localServlet != null) {
+ localServlet.dispose();
+ localServlet = null;
+ }
+ threadPool.shutdown();
+ // inform all group handlers that this camera has gone offline
+ groupTracker.listOfOnlineCameraHandlers.remove(this);
+ groupTracker.listOfOnlineCameraUID.remove(getThing().getUID().getId());
+ for (IpCameraGroupHandler handle : groupTracker.listOfGroupHandlers) {
+ handle.cameraOffline(this);
+ }
+ basicAuth = ""; // clear out stored Password hash
+ useDigestAuth = false;
+ mainEventLoopGroup.shutdownGracefully();
+ mainBootstrap = null;
+ channelTrackingMap.clear();
}
public String getWhiteList() {
import static org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.HLS_STARTUP_DELAY_MS;
import java.io.IOException;
+import java.time.Duration;
+import java.time.Instant;
+import javax.servlet.AsyncContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
+import org.openhab.binding.ipcamera.internal.ChannelTracking;
import org.openhab.binding.ipcamera.internal.Ffmpeg;
import org.openhab.binding.ipcamera.internal.InstarHandler;
import org.openhab.binding.ipcamera.internal.IpCameraBindingConstants.FFmpegFormat;
public class CameraServlet extends IpCameraServlet {
private static final long serialVersionUID = -134658667574L;
private final IpCameraHandler handler;
- private int autofpsStreamsOpen = 0;
- private int snapshotStreamsOpen = 0;
public OpenStreams openStreams = new OpenStreams();
+ private OpenStreams openSnapshotStreams = new OpenStreams();
+ private OpenStreams openAutoFpsStreams = new OpenStreams();
public CameraServlet(IpCameraHandler handler, HttpService httpService) {
super(handler, httpService);
sendFile(resp, pathInfo, "image/gif");
return;
case "/ipcamera.jpg":
- sendSnapshotImage(resp, "image/jpg", handler.getSnapshot());
+ // Use cached image if recent. Cameras can take > 1sec to send back a reply.
+ // Example an Image item/widget may have a 1 second refresh.
+ if (handler.ffmpegSnapshotGeneration
+ || Duration.between(handler.currentSnapshotTime, Instant.now()).toMillis() < 1200) {
+ sendSnapshotImage(resp, "image/jpg", handler.getSnapshot());
+ } else {
+ handler.getSnapshot();
+ final AsyncContext acontext = req.startAsync(req, resp);
+ acontext.start(new Runnable() {
+ @Override
+ public void run() {
+ Instant startTime = Instant.now();
+ do {
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ return;
+ }
+ } // 5 sec timeout OR a new snapshot comes back from camera
+ while (Duration.between(startTime, Instant.now()).toMillis() < 5000
+ && Duration.between(handler.currentSnapshotTime, Instant.now()).toMillis() > 1200);
+ sendSnapshotImage(resp, "image/jpg", handler.getSnapshot());
+ acontext.complete();
+ }
+ });
+ }
return;
case "/snapshots.mjpeg":
- req.getSession().setMaxInactiveInterval(0);
- snapshotStreamsOpen++;
handler.streamingSnapshotMjpeg = true;
handler.startSnapshotPolling();
StreamOutput output = new StreamOutput(resp);
+ openSnapshotStreams.addStream(output);
do {
try {
output.sendSnapshotBasedFrame(handler.getSnapshot());
Thread.sleep(1005);
} catch (InterruptedException | IOException e) {
// Never stop streaming until IOException. Occurs when browser stops the stream.
- snapshotStreamsOpen--;
- if (snapshotStreamsOpen == 0) {
+ openSnapshotStreams.removeStream(output);
+ if (openSnapshotStreams.isEmpty()) {
handler.streamingSnapshotMjpeg = false;
handler.stopSnapshotPolling();
logger.debug("All snapshots.mjpeg streams have stopped.");
}
} while (true);
case "/ipcamera.mjpeg":
- req.getSession().setMaxInactiveInterval(0);
if (handler.mjpegUri.isEmpty() || "ffmpeg".equals(handler.mjpegUri)) {
if (openStreams.isEmpty()) {
handler.setupFfmpegFormat(FFmpegFormat.MJPEG);
output = new StreamOutput(resp, handler.mjpegContentType);
openStreams.addStream(output);
} else {
- logger.debug("Not the first stream requested. Stream from camera already open");
+ ChannelTracking tracker = handler.channelTrackingMap.get(handler.mjpegUri);
+ if (tracker == null || !tracker.getChannel().isOpen()) {
+ logger.warn("Not the first stream requested but the stream from camera was closed");
+ handler.openCamerasStream();
+ }
output = new StreamOutput(resp, handler.mjpegContentType);
openStreams.addStream(output);
}
}
return;
}
- } while (true);
+ } while (!openStreams.isEmpty());
case "/autofps.mjpeg":
- req.getSession().setMaxInactiveInterval(0);
- autofpsStreamsOpen++;
handler.streamingAutoFps = true;
output = new StreamOutput(resp);
+ openAutoFpsStreams.addStream(output);
int counter = 0;
do {
try {
Thread.sleep(1000);
} catch (InterruptedException | IOException e) {
// Never stop streaming until IOException. Occurs when browser stops the stream.
- autofpsStreamsOpen--;
- if (autofpsStreamsOpen == 0) {
+ openAutoFpsStreams.removeStream(output);
+ if (openAutoFpsStreams.isEmpty()) {
handler.streamingAutoFps = false;
logger.debug("All autofps.mjpeg streams have stopped.");
}
@Override
public void dispose() {
openStreams.closeAllStreams();
+ openSnapshotStreams.closeAllStreams();
+ openAutoFpsStreams.closeAllStreams();
super.dispose();
}
}