Skip to content

Commit

Permalink
Redesign some synchronization features
Browse files Browse the repository at this point in the history
refs #26
  • Loading branch information
sarxos committed Feb 3, 2013
1 parent 1183329 commit 6ee5530
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 77 deletions.
Expand Up @@ -18,9 +18,6 @@
*/
public class DetectMotionExample extends JFrame implements Runnable {

/**
*
*/
private static final long serialVersionUID = -585739158170333370L;

private static final int INTERVAL = 100; // ms
Expand Down
@@ -0,0 +1,43 @@
package com.github.sarxos.webcam;

import java.util.concurrent.atomic.AtomicInteger;


public class ConcurrentThreadsExample {

private static AtomicInteger counter = new AtomicInteger(0);

private static final class Capture extends Thread {

private static final AtomicInteger number = new AtomicInteger(0);

public Capture() {
super("capture-" + number.incrementAndGet());
}

@Override
public void run() {
while (true) {
Webcam.getDefault().getImage();
int value = counter.incrementAndGet();
if (value != 0 && value % 10 == 0) {
System.out.println(Thread.currentThread().getName() + ": Frames captured: " + value);
}
}
}
}

public static void main(String[] args) {

/**
* This example will start several concurrent threads which use single
* webcam instance.
*/

int n = Runtime.getRuntime().availableProcessors() * 4;
for (int i = 0; i < n; i++) {
System.out.println("Thread: " + i);
new Capture().start();
}
}
}
@@ -0,0 +1,45 @@
package com.github.sarxos.webcam;

import org.bridj.Pointer;

import com.github.sarxos.webcam.ds.buildin.natives.Device;
import com.github.sarxos.webcam.ds.buildin.natives.DeviceList;
import com.github.sarxos.webcam.ds.buildin.natives.OpenIMAJGrabber;


public class PureDefaultDeviceExample {

public static void main(String[] args) {

/**
* This example show how to use native OpenIMAJ API to capture raw bytes
* data as byte[] array. It also calculates current FPS.
*/

OpenIMAJGrabber grabber = new OpenIMAJGrabber();

Device device = null;
Pointer<DeviceList> devices = grabber.getVideoDevices();
for (Device d : devices.get().asArrayList()) {
device = d;
break;
}

grabber.startSession(320, 240, 30, Pointer.pointerTo(device));

long t1 = System.currentTimeMillis() / 1000;

int n = 100;
int i = 0;
do {
grabber.nextFrame();
grabber.getImage().getBytes(320 * 240 * 3); // byte[]
} while (++i < n);

long t2 = System.currentTimeMillis() / 1000;

System.out.println("FPS: " + ((double) n / (t2 - t1)));

grabber.stopSession();
}
}
27 changes: 16 additions & 11 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -73,12 +73,12 @@ public void run() {
/**
* Webcam driver (LtiCivil, JMF, FMJ, JQT, OpenCV, VLCj, etc).
*/
private static WebcamDriver driver = null;
private static volatile WebcamDriver driver = null;

/**
* Webcam discovery service.
*/
private static WebcamDiscoveryService discovery = null;
private static volatile WebcamDiscoveryService discovery = null;

/**
* Is automated deallocation on TERM signal enabled.
Expand Down Expand Up @@ -331,13 +331,14 @@ public BufferedImage getImage() {
return null;
}

synchronized (this) {
if (!open) {
LOG.debug("Try to get image on closed webcam, opening it automatically");
if (!open) {
LOG.debug("Try to get image on closed webcam, opening it automatically");
synchronized (this) {
open();
}
return device.getImage();
}

return device.getImage();
}

/**
Expand Down Expand Up @@ -382,7 +383,7 @@ public static List<Webcam> getWebcams(long timeout) throws TimeoutException, Web
* @throws TimeoutException when timeout has been exceeded
* @throws WebcamException when something is wrong
*/
public static List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {
public static synchronized List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException, WebcamException {

WebcamDiscoveryService discovery = getDiscoveryService();
List<Webcam> webcams = discovery.getWebcams(timeout, tunit);
Expand All @@ -402,6 +403,7 @@ public static List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws Timeo
* @see Webcam#getWebcams()
*/
public static Webcam getDefault() throws WebcamException {

try {
return getDefault(Long.MAX_VALUE);
} catch (TimeoutException e) {
Expand Down Expand Up @@ -507,7 +509,7 @@ public boolean removeWebcamListener(WebcamListener l) {
*
* @return Webcam driver
*/
public static WebcamDriver getDriver() {
public static synchronized WebcamDriver getDriver() {

if (driver == null) {
driver = WebcamDriverUtils.findDriver(DRIVERS_LIST, DRIVERS_CLASS_LIST);
Expand All @@ -529,11 +531,14 @@ public static WebcamDriver getDriver() {
* @param driver new webcam driver to be used (e.g. LtiCivil, JFM, FMJ, QTJ)
* @throws IllegalArgumentException when argument is null
*/
public static void setDriver(WebcamDriver driver) {
public static synchronized void setDriver(WebcamDriver driver) {

if (driver == null) {
throw new IllegalArgumentException("Webcam driver cannot be null!");
}

resetDriver();

Webcam.driver = driver;
}

Expand All @@ -547,7 +552,7 @@ public static void setDriver(WebcamDriver driver) {
* @param driver new video driver class to use
* @throws IllegalArgumentException when argument is null
*/
public static void setDriver(Class<? extends WebcamDriver> driverClass) {
public static synchronized void setDriver(Class<? extends WebcamDriver> driverClass) {

resetDriver();

Expand Down Expand Up @@ -718,7 +723,7 @@ public static boolean removeDiscoveryListener(WebcamDiscoveryListener l) {
*
* @return Discovery service
*/
public static WebcamDiscoveryService getDiscoveryService() {
public static synchronized WebcamDiscoveryService getDiscoveryService() {
if (discovery == null) {
discovery = new WebcamDiscoveryService(getDriver());
}
Expand Down
Expand Up @@ -78,11 +78,7 @@ private static List<WebcamDevice> getDevices(List<Webcam> webcams) {
return devices;
}

public List<Webcam> getWebcams() throws TimeoutException {
return getWebcams(Webcam.getDiscoveryTimeout(), TimeUnit.MILLISECONDS);
}

public List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException {
public synchronized List<Webcam> getWebcams(long timeout, TimeUnit tunit) throws TimeoutException {

if (timeout < 0) {
throw new IllegalArgumentException("Timeout cannot be negative");
Expand Down Expand Up @@ -162,7 +158,7 @@ public void run() {
List<WebcamDevice> tmpold = null;

try {
tmpold = getDevices(getWebcams(Webcam.getDiscoveryTimeout(), TimeUnit.MILLISECONDS));
tmpold = getDevices(getWebcams(Long.MAX_VALUE, TimeUnit.MILLISECONDS));
} catch (TimeoutException e) {
throw new WebcamException(e);
}
Expand Down

0 comments on commit 6ee5530

Please sign in to comment.