Skip to content

Commit

Permalink
Add thread-safe support info in webcam drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Feb 6, 2013
1 parent c5955bf commit 7f54f05
Show file tree
Hide file tree
Showing 23 changed files with 228 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ protected static CaptureSystem getCaptureSystem() {
return system;
}

@Override
public List<WebcamDevice> getDevices() {

if (!initialized) {
Expand All @@ -72,4 +73,9 @@ public List<WebcamDevice> getDevices() {
return devices;
}

@Override
public boolean isThreadSafe() {
return false;
}

}
44 changes: 33 additions & 11 deletions webcam-capture-drivers/webcam-capture-driver-ipcam/.classpath
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="src" path="src/examples/java"/>
<classpathentry kind="src" path="src/examples/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/examples/java"/>
<classpathentry kind="src" path="src/examples/resources"/>
<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"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static void main(String[] args) throws MalformedURLException {
// available to be viewed online. Here in this example we are creating
// IP camera device working in PULL mode to request static JPEG images.

IpCamStorage storage = new IpCamStorage("src/examples/resources/cameras.xml");

Webcam.setDriver(new IpCamDriver(storage));

JFrame f = new JFrame("Dasding Studio Live IP Cameras");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(0, 3, 1, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public List<WebcamDevice> getDevices() {
public void register(IpCamDevice device) {
IpCamDeviceRegistry.register(device);
}

@Override
public boolean isThreadSafe() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public static void main(String[] args) {

}

@Override
public boolean isThreadSafe() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import javax.media.CaptureDeviceManager;
import javax.media.Format;

import com.github.sarxos.webcam.WebcamDriver;
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamDriver;


public class JmfDriver implements WebcamDriver {

private static List<WebcamDevice> devices = null;

@Override
public List<WebcamDevice> getDevices() {

if (devices == null) {
Expand All @@ -35,4 +36,9 @@ public List<WebcamDevice> getDevices() {

return devices;
}

@Override
public boolean isThreadSafe() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public List<WebcamDevice> getDevices() {
return webcamDevices;
}

@Override
public boolean isThreadSafe() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ public List<WebcamDevice> getDevices() {

return devices;
}

@Override
public boolean isThreadSafe() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


Expand All @@ -19,9 +17,7 @@ public class WebcamViewerExample extends JFrame implements Runnable, WebcamListe
private static final long serialVersionUID = -2831291292491395695L;

private Webcam webcam = null;

private JPanel wait = null;
private WebcamPanel view = null;
private WebcamPanel viewer = null;

@Override
public void run() {
Expand All @@ -30,31 +26,30 @@ public void run() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(this);

wait = new JPanel();
wait.add(new JLabel("Initialization"));
webcam = Webcam.getDefault();
if (webcam == null) {
System.out.println("No webcams found...");
System.exit(1);
}

webcam.setViewSize(WebcamResolution.VGA.getSize());
webcam.addWebcamListener(WebcamViewerExample.this);

setContentPane(wait);
viewer = new WebcamPanel(webcam, false);

setContentPane(viewer);
pack();
setVisible(true);

SwingUtilities.invokeLater(new Runnable() {
Thread t = new Thread() {

@Override
public void run() {

webcam = Webcam.getDefault();
if (webcam == null) {
System.out.println("No webcams found...");
System.exit(1);
}

view = new WebcamPanel(webcam);

webcam.addWebcamListener(WebcamViewerExample.this);
webcam.open();
viewer.start();
}
});
};
t.setDaemon(true);
t.start();
}

public static void main(String[] args) {
Expand All @@ -63,23 +58,21 @@ public static void main(String[] args) {

@Override
public void webcamOpen(WebcamEvent we) {
setContentPane(view);
pack();
System.out.println("webcam open");
}

@Override
public void webcamClosed(WebcamEvent we) {
setContentPane(wait);
pack();
System.out.println("webcam closed");
}

@Override
public void webcamDisposed(WebcamEvent we) {
System.out.println("webcam disposed");
}

@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}

@Override
Expand All @@ -101,12 +94,14 @@ public void windowDeactivated(WindowEvent e) {

@Override
public void windowDeiconified(WindowEvent e) {
view.resume();
System.out.println("webcam viewer resumed");
viewer.resume();
}

@Override
public void windowIconified(WindowEvent e) {
view.pause();
System.out.println("webcam viewer paused");
viewer.pause();
}

}
37 changes: 29 additions & 8 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sarxos.webcam.ds.WebcamProcessor;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDevice;
import com.github.sarxos.webcam.ds.buildin.WebcamDefaultDriver;
import com.github.sarxos.webcam.ds.cgt.WebcamCloseTask;
Expand Down Expand Up @@ -80,10 +79,12 @@ public void run() {
/**
* Webcam driver (LtiCivil, JMF, FMJ, JQT, OpenCV, VLCj, etc).
*/
private static volatile WebcamDriver driver = null;
private static WebcamDriver driver = null;

// TODO: create only if !WebcamDriver::isThreadSafe()
private static final WebcamProcessor processor = new WebcamProcessor();
/**
* Webcam tasks processor synchronize non-thread-safe operations.
*/
private static WebcamProcessor processor = null;

/**
* Webcam discovery service.
Expand Down Expand Up @@ -150,7 +151,7 @@ public void open() {
return;
}

WebcamOpenTask task = new WebcamOpenTask(processor, device, true);
WebcamOpenTask task = new WebcamOpenTask(this);
task.open(this);

Runtime.getRuntime().addShutdownHook(hook = new ShutdownHook(this));
Expand All @@ -166,7 +167,7 @@ public void close() {
return;
}

WebcamCloseTask task = new WebcamCloseTask(processor, device, true);
WebcamCloseTask task = new WebcamCloseTask(this);
task.close(this);

Runtime.getRuntime().removeShutdownHook(hook);
Expand Down Expand Up @@ -314,7 +315,7 @@ public BufferedImage getImage() {
}
}

WebcamReadBufferTask task = new WebcamReadBufferTask(processor, device, true);
WebcamReadBufferTask task = new WebcamReadBufferTask(this);
return task.getImage();
}

Expand Down Expand Up @@ -499,6 +500,10 @@ public static synchronized WebcamDriver getDriver() {
driver = new WebcamDefaultDriver();
}

if (!driver.isThreadSafe()) {
processor = new WebcamProcessor();
}

return driver;
}

Expand All @@ -519,6 +524,10 @@ public static synchronized void setDriver(WebcamDriver driver) {
resetDriver();

Webcam.driver = driver;

if (!driver.isThreadSafe()) {
processor = new WebcamProcessor();
}
}

/**
Expand Down Expand Up @@ -547,6 +556,9 @@ public static synchronized void setDriver(Class<? extends WebcamDriver> driverCl
throw new WebcamException(e);
}

if (!driver.isThreadSafe()) {
processor = new WebcamProcessor();
}
}

/**
Expand All @@ -565,6 +577,11 @@ public static void resetDriver() {
discovery.shutdown();
discovery = null;
}

if (processor != null) {
processor.shutdown();
processor = null;
}
}

/**
Expand Down Expand Up @@ -605,6 +622,10 @@ public WebcamDevice getDevice() {
return device;
}

protected WebcamProcessor getProcessor() {
return processor;
}

/**
* Completely dispose capture device. After this operation webcam cannot be
* used any more and full reinstantiation is required.
Expand All @@ -619,7 +640,7 @@ protected void dispose() {

LOG.info("Disposing webcam {}", getName());

WebcamDisposeTask task = new WebcamDisposeTask(processor, device, true);
WebcamDisposeTask task = new WebcamDisposeTask(this);
task.dispose();

WebcamEvent we = new WebcamEvent(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,16 @@ public List<WebcamDevice> getDevices() {
}
return all;
}

@Override
public boolean isThreadSafe() {
boolean safe = true;
for (WebcamDriver driver : drivers) {
safe &= driver.isThreadSafe();
if (!safe) {
break;
}
}
return safe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public interface WebcamDriver {
*
* @return List of webcam devices
*/
public List<WebcamDevice> getDevices();
List<WebcamDevice> getDevices();

/**
* Is driver thread-safe. Thread safe drivers operations does not have to be
* synchronized.
*
* @return True in case if driver is thread-safe, false otherwise
*/
boolean isThreadSafe();

}

0 comments on commit 7f54f05

Please sign in to comment.