Skip to content

Commit

Permalink
Better multithreading support
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 22, 2012
1 parent 8e3f557 commit bd932f5
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 156 deletions.
Expand Up @@ -37,17 +37,21 @@ public class LtiCivilDevice implements WebcamDevice, CaptureObserver {
private Dimension size = null;
private Image image = null;
private CaptureStream stream = null;
private boolean open = false;
private boolean capturing = false;

private volatile boolean open = false;
private volatile boolean capturing = false;
private volatile boolean disposed = false;

public LtiCivilDevice(CaptureDeviceInfo cdi) {
this.cdi = cdi;
}

@Override
public String getName() {
return cdi.getDescription();
}

@Override
public Dimension[] getSizes() {

if (dimensions == null) {
Expand Down Expand Up @@ -76,6 +80,7 @@ public Dimension[] getSizes() {

Collections.sort(dimensions, new Comparator<Dimension>() {

@Override
public int compare(Dimension a, Dimension b) {
int apx = a.width * a.height;
int bpx = b.width * b.height;
Expand All @@ -93,26 +98,32 @@ public int compare(Dimension a, Dimension b) {
return dimensions.toArray(new Dimension[dimensions.size()]);
}

@Override
public BufferedImage getImage() {
if (!capturing) {
return null;
}
return AWTImageConverter.toBufferedImage(image);
}

@Override
public void onError(CaptureStream stream, CaptureException e) {
LOG.error("Exception in capture stream", e);
}

@Override
public void onNewImage(CaptureStream stream, Image image) {
this.image = image;
this.capturing = true;
}

@Override
public void open() {
if (open) {

if (open || disposed) {
return;
}

try {
stream = LtiCivilDriver.getCaptureSystem().openCaptureDeviceStream(cdi.getDeviceID());
stream.setVideoFormat(findFormat());
Expand Down Expand Up @@ -155,6 +166,7 @@ private VideoFormat findFormat() {
throw new RuntimeException("Cannot find RGB24 video format for size [" + size.width + "x" + size.height + "]");
}

@Override
public void close() {
if (!open) {
return;
Expand All @@ -167,11 +179,18 @@ public void close() {
}
}

@Override
public Dimension getSize() {
return size;
}

@Override
public void setSize(Dimension d) {
this.size = d;
}

@Override
public void dispose() {
disposed = true;
}
}
Expand Up @@ -20,11 +20,6 @@
*/
public class WebcamPlusMjpegExample {

static {
System.setProperty("http.proxyHost", "global.proxy.lucent.com");
System.setProperty("http.proxyPort", "8000");
}

/**
* Customized webcam driver.
*/
Expand Down
Expand Up @@ -184,9 +184,11 @@ public void stop() {
private IpCamAuth auth = null;
private IpCamHttpClient client = new IpCamHttpClient();
private PushImageReader pushReader = null;
private boolean open = false;
private boolean failOnError = false;

private volatile boolean open = false;
private volatile boolean disposed = false;

private Dimension[] sizes = null;
private Dimension size = null;

Expand Down Expand Up @@ -378,6 +380,10 @@ private BufferedImage getImagePullMode() {

@Override
public void open() {
if (disposed) {
LOG.warn("Device cannopt be open because it's already disposed");
return;
}
open = true;
}

Expand Down Expand Up @@ -423,4 +429,9 @@ public void resetAuth() {
public void setFailOnError(boolean failOnError) {
this.failOnError = failOnError;
}

@Override
public void dispose() {
disposed = true;
}
}
Expand Up @@ -19,9 +19,11 @@ public class JavaCvDevice implements WebcamDevice {

private int address = -1;
private String name = null;
private boolean open = false;
private FrameGrabber grabber = null;

private volatile boolean open = false;
private volatile boolean disposed = false;

public JavaCvDevice(int address) {
this.address = address;
}
Expand Down Expand Up @@ -66,7 +68,7 @@ public BufferedImage getImage() {
@Override
public void open() {

if (open) {
if (open || disposed) {
return;
}

Expand All @@ -79,12 +81,12 @@ public void open() {
grabber.start();
open = true;
} catch (com.googlecode.javacv.FrameGrabber.Exception e) {
dispose();
release();
throw new WebcamException(e);
}
}

private void dispose() {
private void release() {
if (grabber != null) {
try {
grabber.release();
Expand All @@ -109,4 +111,9 @@ public void close() {
dispose();
}
}

@Override
public void dispose() {
disposed = true;
}
}
Expand Up @@ -94,6 +94,7 @@ public void run() {
semaphore.release();
}

@Override
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof StartEvent) {
available = true;
Expand Down Expand Up @@ -121,6 +122,8 @@ public void controllerUpdate(ControllerEvent ce) {
*/
private volatile boolean started = false;

private volatile boolean disposed = false;

private PlayerStarter starter = null;
private Semaphore semaphore = new Semaphore(0, true);

Expand All @@ -142,6 +145,7 @@ public JmfDevice(CaptureDeviceInfo cdi) {
this.cdi = cdi;
}

@Override
public String getName() {
return cdi.getName();
}
Expand Down Expand Up @@ -229,6 +233,7 @@ private VideoFormat getLargestVideoFormat() {
return format;
}

@Override
public Dimension[] getSizes() {

if (dimensions == null) {
Expand All @@ -243,6 +248,7 @@ public Dimension[] getSizes() {

Collections.sort(dimensions, new Comparator<Dimension>() {

@Override
public int compare(Dimension a, Dimension b) {
int apx = a.width * a.height;
int bpx = b.width * b.height;
Expand All @@ -260,14 +266,17 @@ public int compare(Dimension a, Dimension b) {
return dimensions.toArray(new Dimension[dimensions.size()]);
}

@Override
public Dimension getSize() {
return dimension;
}

@Override
public void setSize(Dimension size) {
this.dimension = size;
}

@Override
public BufferedImage getImage() {

if (!open) {
Expand Down Expand Up @@ -295,8 +304,14 @@ public BufferedImage getImage() {
return buffered;
}

@Override
public void open() {

if (disposed) {
LOG.warn("Cannot open device because it's already disposed");
return;
}

if (open) {
LOG.debug("Opening webcam - already open!");
return;
Expand Down Expand Up @@ -344,6 +359,7 @@ public void open() {
open = started && available;
}

@Override
public void close() {

if (!started && !available && !open) {
Expand All @@ -366,4 +382,9 @@ public void close() {
}
}

@Override
public void dispose() {
disposed = true;
}

}
Expand Up @@ -34,7 +34,9 @@ public class OpenImajDevice implements WebcamDevice {
private Device device = null;
private VideoCapture capture = null;
private Dimension size = null;
private boolean open = false;

private volatile boolean open = false;
private volatile boolean disposed = false;

public OpenImajDevice(Device device) {
this.device = device;
Expand Down Expand Up @@ -77,6 +79,11 @@ public BufferedImage getImage() {
@Override
public void open() {

if (disposed) {
LOG.warn("Cannot open device because it's already disposed");
return;
}

if (open) {
return;
}
Expand Down Expand Up @@ -117,4 +124,9 @@ public void close() {

LOG.info("OpenIMAJ webcam device has been closed");
}

@Override
public void dispose() {
disposed = true;
}
}

0 comments on commit bd932f5

Please sign in to comment.