Fix IndexOutOfBoundsException and remove Sleep. (#11089)
Signed-off-by: Matthew Skinner <matt@pcmus.com>
This commit is contained in:
@@ -768,6 +768,10 @@ public class IpCameraHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void openMjpegStream() {
|
||||||
|
sendHttpGET(mjpegUri);
|
||||||
|
}
|
||||||
|
|
||||||
// If start is true the CTX is added to the list to stream video to, false stops
|
// If start is true the CTX is added to the list to stream video to, false stops
|
||||||
// the stream.
|
// the stream.
|
||||||
public void setupMjpegStreaming(boolean start, ChannelHandlerContext ctx) {
|
public void setupMjpegStreaming(boolean start, ChannelHandlerContext ctx) {
|
||||||
@@ -777,13 +781,8 @@ public class IpCameraHandler extends BaseThingHandler {
|
|||||||
if (mjpegUri.isEmpty() || "ffmpeg".equals(mjpegUri)) {
|
if (mjpegUri.isEmpty() || "ffmpeg".equals(mjpegUri)) {
|
||||||
sendMjpegFirstPacket(ctx);
|
sendMjpegFirstPacket(ctx);
|
||||||
setupFfmpegFormat(FFmpegFormat.MJPEG);
|
setupFfmpegFormat(FFmpegFormat.MJPEG);
|
||||||
} else {
|
} else {// Delay fixes Dahua reboots when refreshing a mjpeg stream.
|
||||||
try {
|
threadPool.schedule(this::openMjpegStream, 500, TimeUnit.MILLISECONDS);
|
||||||
// fix Dahua reboots when refreshing a mjpeg stream.
|
|
||||||
TimeUnit.MILLISECONDS.sleep(500);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
sendHttpGET(mjpegUri);
|
|
||||||
}
|
}
|
||||||
} else if (ffmpegMjpeg != null) {// not first stream and we will use ffmpeg
|
} else if (ffmpegMjpeg != null) {// not first stream and we will use ffmpeg
|
||||||
sendMjpegFirstPacket(ctx);
|
sendMjpegFirstPacket(ctx);
|
||||||
@@ -1779,7 +1778,6 @@ public class IpCameraHandler extends BaseThingHandler {
|
|||||||
public void dispose() {
|
public void dispose() {
|
||||||
isOnline = false;
|
isOnline = false;
|
||||||
snapshotPolling = false;
|
snapshotPolling = false;
|
||||||
onvifCamera.disconnect();
|
|
||||||
Future<?> localFuture = pollCameraJob;
|
Future<?> localFuture = pollCameraJob;
|
||||||
if (localFuture != null) {
|
if (localFuture != null) {
|
||||||
localFuture.cancel(true);
|
localFuture.cancel(true);
|
||||||
@@ -1832,6 +1830,7 @@ public class IpCameraHandler extends BaseThingHandler {
|
|||||||
localFfmpeg.stopConverting();
|
localFfmpeg.stopConverting();
|
||||||
}
|
}
|
||||||
channelTrackingMap.clear();
|
channelTrackingMap.clear();
|
||||||
|
onvifCamera.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStreamServerHandler(StreamServerHandler streamServerHandler2) {
|
public void setStreamServerHandler(StreamServerHandler streamServerHandler2) {
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ import java.util.LinkedList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
@@ -110,6 +112,7 @@ public class OnvifConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
private ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(2);
|
||||||
private @Nullable Bootstrap bootstrap;
|
private @Nullable Bootstrap bootstrap;
|
||||||
private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup();
|
private EventLoopGroup mainEventLoopGroup = new NioEventLoopGroup();
|
||||||
private String ipAddress = "";
|
private String ipAddress = "";
|
||||||
@@ -162,7 +165,8 @@ public class OnvifConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String getXml(RequestType requestType) {
|
private String getXml(RequestType requestType) {
|
||||||
|
try {
|
||||||
switch (requestType) {
|
switch (requestType) {
|
||||||
case AbsoluteMove:
|
case AbsoluteMove:
|
||||||
return "<AbsoluteMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
|
return "<AbsoluteMove xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
|
||||||
@@ -286,6 +290,14 @@ public class OnvifConnection {
|
|||||||
return "<GetPresets xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
|
return "<GetPresets xmlns=\"http://www.onvif.org/ver20/ptz/wsdl\"><ProfileToken>"
|
||||||
+ mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetPresets>";
|
+ mediaProfileTokens.get(mediaProfileIndex) + "</ProfileToken></GetPresets>";
|
||||||
}
|
}
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
if (!isConnected) {
|
||||||
|
logger.debug("IndexOutOfBoundsException occured, camera is not connected via ONVIF: {}",
|
||||||
|
e.getMessage());
|
||||||
|
} else {
|
||||||
|
logger.debug("IndexOutOfBoundsException occured, {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
return "notfound";
|
return "notfound";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -805,6 +817,10 @@ public class OnvifConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void sendPTZRequest(RequestType requestType) {
|
public void sendPTZRequest(RequestType requestType) {
|
||||||
|
if (!isConnected) {
|
||||||
|
logger.debug("ONVIF was not connected when a PTZ request was made, connecting now");
|
||||||
|
connect(usingEvents);
|
||||||
|
}
|
||||||
sendOnvifRequest(requestBuilder(requestType, ptzXAddr));
|
sendOnvifRequest(requestBuilder(requestType, ptzXAddr));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -823,26 +839,28 @@ public class OnvifConnection {
|
|||||||
return isConnected;
|
return isConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
private void cleanup() {
|
||||||
if (usingEvents && isConnected) {
|
mainEventLoopGroup.shutdownGracefully();
|
||||||
sendOnvifRequest(requestBuilder(RequestType.Unsubscribe, subscriptionXAddr));
|
|
||||||
try {
|
|
||||||
Thread.sleep(500);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
isConnected = false;
|
isConnected = false;
|
||||||
presetTokens.clear();
|
|
||||||
mediaProfileTokens.clear();
|
|
||||||
if (!mainEventLoopGroup.isShutdown()) {
|
if (!mainEventLoopGroup.isShutdown()) {
|
||||||
try {
|
try {
|
||||||
mainEventLoopGroup.awaitTermination(3, TimeUnit.SECONDS);
|
mainEventLoopGroup.awaitTermination(3, TimeUnit.SECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.info("Onvif was not shutdown correctly due to being interrupted");
|
logger.warn("ONVIF was not cleanly shutdown, due to being interrupted");
|
||||||
} finally {
|
} finally {
|
||||||
|
logger.debug("Eventloop is shutdown:{}", mainEventLoopGroup.isShutdown());
|
||||||
mainEventLoopGroup = new NioEventLoopGroup();
|
mainEventLoopGroup = new NioEventLoopGroup();
|
||||||
bootstrap = null;
|
bootstrap = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
threadPool.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disconnect() {
|
||||||
|
if (usingEvents && isConnected && !mainEventLoopGroup.isShuttingDown()) {
|
||||||
|
sendOnvifRequest(requestBuilder(RequestType.Unsubscribe, subscriptionXAddr));
|
||||||
|
}
|
||||||
|
// Some cameras may continue to send event callbacks even when they cant reach a server.
|
||||||
|
threadPool.schedule(this::cleanup, 500, TimeUnit.MILLISECONDS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user