From 4e0261fa1b8d8b0921f91aff7b42eb2d3b2bf76a Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 29 Jan 2016 18:05:34 -0500 Subject: [PATCH] Fixes IP Cameras Hanging - The IPCameraFrameGrabber was stuck in an infinte loop waiting for the rest of the image to arrive. This was fixed by using a DataInputStream instead - An unexpected end of the stream would cause a null pointer exception. Now throws an EOFException. #390 Closes #390 Closes #427 Closes #404 Closes #387 Closes #436 --- .../core/sources/IPCameraFrameGrabber.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/edu/wpi/grip/core/sources/IPCameraFrameGrabber.java b/core/src/main/java/edu/wpi/grip/core/sources/IPCameraFrameGrabber.java index 536895cb61..23a39320eb 100644 --- a/core/src/main/java/edu/wpi/grip/core/sources/IPCameraFrameGrabber.java +++ b/core/src/main/java/edu/wpi/grip/core/sources/IPCameraFrameGrabber.java @@ -33,8 +33,9 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.EOFException; import java.io.IOException; -import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; @@ -72,7 +73,7 @@ public static void tryLoad() throws Exception { private URL url; private URLConnection connection; - private InputStream input; + private DataInputStream input; private byte[] pixelBuffer = new byte[1024]; private Map> headerfields; private String boundryKey; @@ -99,7 +100,7 @@ public void start() throws Exception { } } } - input = connection.getInputStream(); + input = new DataInputStream(connection.getInputStream()); } catch (IOException e) { // Make sure we rethrow the IO exception https://github.com/bytedeco/javacv/pull/300 throw new Exception(e.getMessage(), e); @@ -169,9 +170,9 @@ byte[] readImage() throws IOException { } } // find embedded jpeg in stream - String subheader = sb.toString(); + final String subheader = sb.toString(); //log.debug(subheader); - int contentLength = -1; + // if (boundryKey == null) // { // Yay! - server was nice and sent content length @@ -180,18 +181,17 @@ byte[] readImage() throws IOException { if (c0 < 0) { //log.info("no content length returning null"); - return null; + throw new EOFException("The camera stream ended unexpectedly"); } c0 += 16; - contentLength = Integer.parseInt(subheader.substring(c0, c1).trim()); + final int contentLength = Integer.parseInt(subheader.substring(c0, c1).trim()); //log.debug("Content-Length: " + contentLength); // adaptive size - careful - don't want a 2G jpeg ensureBufferCapacity(contentLength); - while (input.available() < contentLength) ; - input.read(pixelBuffer, 0, contentLength); + input.readFully(pixelBuffer, 0, contentLength); input.read();// \r input.read();// \n input.read();// \r