diff --git a/webcam-capture-driver-ipcam/.classpath b/webcam-capture-driver-ipcam/.classpath
index c29db95a..e147e777 100644
--- a/webcam-capture-driver-ipcam/.classpath
+++ b/webcam-capture-driver-ipcam/.classpath
@@ -17,6 +17,7 @@
+
diff --git a/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/JpegExample.java b/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/JpegExample.java
new file mode 100644
index 00000000..a7689708
--- /dev/null
+++ b/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/JpegExample.java
@@ -0,0 +1,39 @@
+package com.github.sarxos.webcam.ds.ipcam;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.swing.JFrame;
+
+import com.github.sarxos.webcam.Webcam;
+import com.github.sarxos.webcam.WebcamPanel;
+
+
+public class JpegExample {
+
+ public static void main(String[] args) throws MalformedURLException {
+
+ // Dasding is a radio in Germany. They have few network cameras
+ // available to be viewed online. Here in this example we are creating
+ // IP camera device working in PULL mode to request static JPEG images.
+
+ String address = "http://www.dasding.de/ext/webcam/webcam770.php?cam=1";
+ IpCamDevice livecam = new IpCamDevice("dasding", new URL(address), IpCamMode.PULL);
+
+ IpCamDriver driver = new IpCamDriver();
+ driver.register(livecam);
+
+ Webcam.setDriver(driver);
+
+ WebcamPanel panel = new WebcamPanel(Webcam.getDefault());
+ panel.setFPS(0.2); // 1 frame per 5 seconds
+
+ JFrame f = new JFrame("Dasding Studio Live IP Camera");
+ f.add(panel);
+ f.pack();
+ f.setVisible(true);
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+ }
+
+}
diff --git a/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/MjpegExample.java b/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/MjpegExample.java
new file mode 100644
index 00000000..34a88aff
--- /dev/null
+++ b/webcam-capture-driver-ipcam/src/examples/java/com/github/sarxos/webcam/ds/ipcam/MjpegExample.java
@@ -0,0 +1,38 @@
+package com.github.sarxos.webcam.ds.ipcam;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.swing.JFrame;
+
+import com.github.sarxos.webcam.Webcam;
+import com.github.sarxos.webcam.WebcamPanel;
+
+
+/**
+ * Example of how to stream MJPEG with Webcam Capture.
+ *
+ * @author Bartosz Firyn (SarXos)
+ */
+public class MjpegExample {
+
+ public static void main(String[] args) throws MalformedURLException {
+
+ String address = "http://88.37.116.138/mjpg/video.mjpg ";
+ IpCamDevice livecam = new IpCamDevice("Lignano Beach", new URL(address), IpCamMode.PUSH);
+
+ IpCamDriver driver = new IpCamDriver();
+ driver.register(livecam);
+
+ Webcam.setDriver(driver);
+
+ WebcamPanel panel = new WebcamPanel(Webcam.getWebcams().get(0));
+ panel.setFPS(1);
+
+ JFrame f = new JFrame("Live Views From Lignano Beach (Italy)");
+ f.add(panel);
+ f.pack();
+ f.setVisible(true);
+ f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ }
+}
diff --git a/webcam-capture-driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java b/webcam-capture-driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java
index c43dc6d2..3ddc8cf5 100644
--- a/webcam-capture-driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java
+++ b/webcam-capture-driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java
@@ -2,6 +2,7 @@
import java.awt.Dimension;
import java.awt.image.BufferedImage;
+import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@@ -50,13 +51,50 @@ private final class PushImageReader implements Runnable {
private BufferedImage image = null;
private boolean running = true;
private WebcamException exception = null;
+ private HttpGet get = null;
+ private URI uri = null;
- public PushImageReader(InputStream is) {
- stream = new IpCamMJPEGStream(is);
+ public PushImageReader(URI uri) {
+ this.uri = uri;
+ stream = new IpCamMJPEGStream(requestStream(uri));
+ }
+
+ private InputStream requestStream(URI uri) {
+
+ BasicHttpContext context = new BasicHttpContext();
+
+ IpCamAuth auth = getAuth();
+ if (auth != null) {
+ AuthCache cache = new BasicAuthCache();
+ cache.put(new HttpHost(uri.getHost()), new BasicScheme());
+ context.setAttribute(ClientContext.AUTH_CACHE, cache);
+ }
+
+ try {
+ get = new HttpGet(uri);
+
+ HttpResponse respone = client.execute(get, context);
+ HttpEntity entity = respone.getEntity();
+
+ Header ct = entity.getContentType();
+ if (ct == null) {
+ throw new WebcamException("Content Type header is missing");
+ }
+
+ if (ct.getValue().startsWith("image/")) {
+ throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL");
+ }
+
+ return entity.getContent();
+
+ } catch (Exception e) {
+ throw new WebcamException("Cannot download image", e);
+ }
}
@Override
public void run() {
+
while (running) {
if (stream.isClosed()) {
@@ -81,9 +119,27 @@ public void run() {
// case when someone manually closed stream, do not log
// exception, this is normal behavior
if (stream.isClosed()) {
+ LOG.debug("Stream already closed, returning");
return;
}
+ if (e instanceof EOFException) {
+
+ LOG.debug("EOF detected, recreating MJPEG stream");
+
+ get.releaseConnection();
+
+ try {
+ stream.close();
+ } catch (IOException ioe) {
+ throw new WebcamException(ioe);
+ }
+
+ stream = new IpCamMJPEGStream(requestStream(uri));
+
+ continue;
+ }
+
LOG.error("Cannot read MJPEG frame", e);
if (failOnError) {
@@ -238,46 +294,14 @@ private BufferedImage getImagePushMode() {
synchronized (this) {
- InputStream is = null;
-
URI uri = null;
-
try {
uri = getURL().toURI();
} catch (URISyntaxException e) {
throw new WebcamException(String.format("Incorrect URI syntax '%s'", uri), e);
}
- BasicHttpContext context = new BasicHttpContext();
-
- IpCamAuth auth = getAuth();
- if (auth != null) {
- AuthCache cache = new BasicAuthCache();
- cache.put(new HttpHost(uri.getHost()), new BasicScheme());
- context.setAttribute(ClientContext.AUTH_CACHE, cache);
- }
-
- try {
- HttpGet get = new HttpGet(uri);
- HttpResponse respone = client.execute(get, context);
- HttpEntity entity = respone.getEntity();
-
- Header ct = entity.getContentType();
- if (ct == null) {
- throw new WebcamException("Content Type header is missing");
- }
-
- if (ct.getValue().startsWith("image/")) {
- throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL");
- }
-
- is = entity.getContent();
-
- } catch (Exception e) {
- throw new WebcamException("Cannot download image", e);
- }
-
- pushReader = new PushImageReader(is);
+ pushReader = new PushImageReader(uri);
// TODO: change to executor