Skip to content

Commit

Permalink
Fixes IP Cameras Hanging
Browse files Browse the repository at this point in the history
 - 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. WPIRoboticsProjects#390

Closes WPIRoboticsProjects#390
Closes WPIRoboticsProjects#427
Closes WPIRoboticsProjects#404
Closes WPIRoboticsProjects#387
Closes WPIRoboticsProjects#436
  • Loading branch information
JLLeitschuh committed Jan 29, 2016
1 parent 34fd6be commit 4e0261f
Showing 1 changed file with 9 additions and 9 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, List<String>> headerfields;
private String boundryKey;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4e0261f

Please sign in to comment.