Skip to content

Commit

Permalink
Better exception handling in MJPEG, examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 14, 2012
1 parent c569a10 commit 399b754
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 35 deletions.
1 change: 1 addition & 0 deletions webcam-capture-driver-ipcam/.classpath
Expand Up @@ -17,6 +17,7 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/examples/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
@@ -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);

}

}
@@ -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);
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 399b754

Please sign in to comment.