Skip to content

Commit 399b754

Browse files
committed
Better exception handling in MJPEG, examples
1 parent c569a10 commit 399b754

File tree

4 files changed

+137
-35
lines changed

4 files changed

+137
-35
lines changed

webcam-capture-driver-ipcam/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<attribute name="maven.pomderived" value="true"/>
1818
</attributes>
1919
</classpathentry>
20+
<classpathentry kind="src" path="src/examples/java"/>
2021
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
2122
<attributes>
2223
<attribute name="maven.pomderived" value="true"/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.sarxos.webcam.ds.ipcam;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
6+
import javax.swing.JFrame;
7+
8+
import com.github.sarxos.webcam.Webcam;
9+
import com.github.sarxos.webcam.WebcamPanel;
10+
11+
12+
public class JpegExample {
13+
14+
public static void main(String[] args) throws MalformedURLException {
15+
16+
// Dasding is a radio in Germany. They have few network cameras
17+
// available to be viewed online. Here in this example we are creating
18+
// IP camera device working in PULL mode to request static JPEG images.
19+
20+
String address = "http://www.dasding.de/ext/webcam/webcam770.php?cam=1";
21+
IpCamDevice livecam = new IpCamDevice("dasding", new URL(address), IpCamMode.PULL);
22+
23+
IpCamDriver driver = new IpCamDriver();
24+
driver.register(livecam);
25+
26+
Webcam.setDriver(driver);
27+
28+
WebcamPanel panel = new WebcamPanel(Webcam.getDefault());
29+
panel.setFPS(0.2); // 1 frame per 5 seconds
30+
31+
JFrame f = new JFrame("Dasding Studio Live IP Camera");
32+
f.add(panel);
33+
f.pack();
34+
f.setVisible(true);
35+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
36+
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.sarxos.webcam.ds.ipcam;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
6+
import javax.swing.JFrame;
7+
8+
import com.github.sarxos.webcam.Webcam;
9+
import com.github.sarxos.webcam.WebcamPanel;
10+
11+
12+
/**
13+
* Example of how to stream MJPEG with Webcam Capture.
14+
*
15+
* @author Bartosz Firyn (SarXos)
16+
*/
17+
public class MjpegExample {
18+
19+
public static void main(String[] args) throws MalformedURLException {
20+
21+
String address = "http://88.37.116.138/mjpg/video.mjpg ";
22+
IpCamDevice livecam = new IpCamDevice("Lignano Beach", new URL(address), IpCamMode.PUSH);
23+
24+
IpCamDriver driver = new IpCamDriver();
25+
driver.register(livecam);
26+
27+
Webcam.setDriver(driver);
28+
29+
WebcamPanel panel = new WebcamPanel(Webcam.getWebcams().get(0));
30+
panel.setFPS(1);
31+
32+
JFrame f = new JFrame("Live Views From Lignano Beach (Italy)");
33+
f.add(panel);
34+
f.pack();
35+
f.setVisible(true);
36+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37+
}
38+
}

webcam-capture-driver-ipcam/src/main/java/com/github/sarxos/webcam/ds/ipcam/IpCamDevice.java

+59-35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.awt.Dimension;
44
import java.awt.image.BufferedImage;
5+
import java.io.EOFException;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import java.net.MalformedURLException;
@@ -50,13 +51,50 @@ private final class PushImageReader implements Runnable {
5051
private BufferedImage image = null;
5152
private boolean running = true;
5253
private WebcamException exception = null;
54+
private HttpGet get = null;
55+
private URI uri = null;
5356

54-
public PushImageReader(InputStream is) {
55-
stream = new IpCamMJPEGStream(is);
57+
public PushImageReader(URI uri) {
58+
this.uri = uri;
59+
stream = new IpCamMJPEGStream(requestStream(uri));
60+
}
61+
62+
private InputStream requestStream(URI uri) {
63+
64+
BasicHttpContext context = new BasicHttpContext();
65+
66+
IpCamAuth auth = getAuth();
67+
if (auth != null) {
68+
AuthCache cache = new BasicAuthCache();
69+
cache.put(new HttpHost(uri.getHost()), new BasicScheme());
70+
context.setAttribute(ClientContext.AUTH_CACHE, cache);
71+
}
72+
73+
try {
74+
get = new HttpGet(uri);
75+
76+
HttpResponse respone = client.execute(get, context);
77+
HttpEntity entity = respone.getEntity();
78+
79+
Header ct = entity.getContentType();
80+
if (ct == null) {
81+
throw new WebcamException("Content Type header is missing");
82+
}
83+
84+
if (ct.getValue().startsWith("image/")) {
85+
throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL");
86+
}
87+
88+
return entity.getContent();
89+
90+
} catch (Exception e) {
91+
throw new WebcamException("Cannot download image", e);
92+
}
5693
}
5794

5895
@Override
5996
public void run() {
97+
6098
while (running) {
6199

62100
if (stream.isClosed()) {
@@ -81,9 +119,27 @@ public void run() {
81119
// case when someone manually closed stream, do not log
82120
// exception, this is normal behavior
83121
if (stream.isClosed()) {
122+
LOG.debug("Stream already closed, returning");
84123
return;
85124
}
86125

126+
if (e instanceof EOFException) {
127+
128+
LOG.debug("EOF detected, recreating MJPEG stream");
129+
130+
get.releaseConnection();
131+
132+
try {
133+
stream.close();
134+
} catch (IOException ioe) {
135+
throw new WebcamException(ioe);
136+
}
137+
138+
stream = new IpCamMJPEGStream(requestStream(uri));
139+
140+
continue;
141+
}
142+
87143
LOG.error("Cannot read MJPEG frame", e);
88144

89145
if (failOnError) {
@@ -238,46 +294,14 @@ private BufferedImage getImagePushMode() {
238294

239295
synchronized (this) {
240296

241-
InputStream is = null;
242-
243297
URI uri = null;
244-
245298
try {
246299
uri = getURL().toURI();
247300
} catch (URISyntaxException e) {
248301
throw new WebcamException(String.format("Incorrect URI syntax '%s'", uri), e);
249302
}
250303

251-
BasicHttpContext context = new BasicHttpContext();
252-
253-
IpCamAuth auth = getAuth();
254-
if (auth != null) {
255-
AuthCache cache = new BasicAuthCache();
256-
cache.put(new HttpHost(uri.getHost()), new BasicScheme());
257-
context.setAttribute(ClientContext.AUTH_CACHE, cache);
258-
}
259-
260-
try {
261-
HttpGet get = new HttpGet(uri);
262-
HttpResponse respone = client.execute(get, context);
263-
HttpEntity entity = respone.getEntity();
264-
265-
Header ct = entity.getContentType();
266-
if (ct == null) {
267-
throw new WebcamException("Content Type header is missing");
268-
}
269-
270-
if (ct.getValue().startsWith("image/")) {
271-
throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL");
272-
}
273-
274-
is = entity.getContent();
275-
276-
} catch (Exception e) {
277-
throw new WebcamException("Cannot download image", e);
278-
}
279-
280-
pushReader = new PushImageReader(is);
304+
pushReader = new PushImageReader(uri);
281305

282306
// TODO: change to executor
283307

0 commit comments

Comments
 (0)