Skip to content

Commit

Permalink
Small changes in processor, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Feb 6, 2013
1 parent 7f54f05 commit 304243a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
23 changes: 3 additions & 20 deletions webcam-capture/src/main/java/com/github/sarxos/webcam/Webcam.java
Expand Up @@ -84,7 +84,7 @@ public void run() {
/**
* Webcam tasks processor synchronize non-thread-safe operations.
*/
private static WebcamProcessor processor = null;
private static WebcamProcessor processor = new WebcamProcessor();

/**
* Webcam discovery service.
Expand Down Expand Up @@ -500,10 +500,6 @@ public static synchronized WebcamDriver getDriver() {
driver = new WebcamDefaultDriver();
}

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

return driver;
}

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

Webcam.driver = driver;

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

/**
Expand Down Expand Up @@ -555,33 +547,24 @@ public static synchronized void setDriver(Class<? extends WebcamDriver> driverCl
} catch (IllegalAccessException e) {
throw new WebcamException(e);
}

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

/**
* Reset webcam driver.<br>
* <br>
* <b>This method is not thread-safe!</b>
*/
public static void resetDriver() {
public static synchronized void resetDriver() {

DRIVERS_LIST.clear();
DRIVERS_LIST.addAll(Arrays.asList(DRIVERS_DEFAULT));

driver = null;

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

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

/**
Expand Down
Expand Up @@ -36,8 +36,6 @@ private static final class AtomicProcessor implements Runnable {
private SynchronousQueue<WebcamTask> inbound = new SynchronousQueue<WebcamTask>(true);
private SynchronousQueue<WebcamTask> outbound = new SynchronousQueue<WebcamTask>(true);

private volatile boolean running = true;

/**
* Process task.
*
Expand All @@ -50,14 +48,9 @@ public WebcamTask process(WebcamTask task) throws InterruptedException {
return outbound.take();
}

public void stop() {
running = false;
}

@Override
public void run() {
running = true;
while (running) {
while (true) {
WebcamTask t = null;
try {
(t = inbound.take()).handle();
Expand Down Expand Up @@ -109,12 +102,4 @@ public void process(WebcamTask task) {
throw new WebcamException("Processing interrupted", e);
}
}

/**
* Stop processing thread.
*/
public void shutdown() {
processor.stop();
started.set(false);
}
}
Expand Up @@ -22,75 +22,109 @@ public class WebcamTest {

@Before
public void prepare() {
System.out.println(Thread.currentThread().getName() + ": Register dummy driver");
Webcam.registerDriver(DummyDriver.class);
}

@After
public void cleanup() {
System.out.println(Thread.currentThread().getName() + ": Reset driver");
Webcam.resetDriver();
}

@Test
public void test_getWebcams() {

System.out.println(Thread.currentThread().getName() + ": test_getWebcams() start");

List<Webcam> webcams = Webcam.getWebcams();
List<WebcamDevice> devices = DummyDriver.getInstance().getDevices();

Assert.assertTrue(webcams.size() > 0);
Assert.assertEquals(devices.size(), webcams.size());

System.out.println(Thread.currentThread().getName() + ": test_getWebcams() end");
}

@Test
public void test_getDefault() {

System.out.println(Thread.currentThread().getName() + ": test_getDefault() start");

List<Webcam> webcams = Webcam.getWebcams();
List<WebcamDevice> devices = DummyDriver.getInstance().getDevices();

Assert.assertNotNull(Webcam.getDefault());
Assert.assertSame(webcams.get(0), Webcam.getDefault());
Assert.assertSame(devices.get(0), Webcam.getDefault().getDevice());

System.out.println(Thread.currentThread().getName() + ": test_getDefault() end");
}

@Test
public void test_open() {

System.out.println(Thread.currentThread().getName() + ": test_open() start");

Webcam webcam = Webcam.getDefault();
webcam.open();

Assert.assertTrue(webcam.isOpen());
webcam.open();
Assert.assertTrue(webcam.isOpen());

System.out.println(Thread.currentThread().getName() + ": test_open() end");
}

@Test
public void test_close() {

System.out.println(Thread.currentThread().getName() + ": test_close() start");

Webcam webcam = Webcam.getDefault();
webcam.open();

Assert.assertSame(DummyDriver.class, Webcam.getDriver().getClass());

Assert.assertTrue(webcam.isOpen());
webcam.close();
Assert.assertFalse(webcam.isOpen());
webcam.close();
Assert.assertFalse(webcam.isOpen());

System.out.println(Thread.currentThread().getName() + ": test_close() end");
}

@Test
public void test_getImage() {

System.out.println(Thread.currentThread().getName() + ": test_getImage() start");

Webcam webcam = Webcam.getDefault();
webcam.open();

Assert.assertSame(DummyDriver.class, Webcam.getDriver().getClass());

Image image = webcam.getImage();

Assert.assertNotNull(image);

System.out.println(Thread.currentThread().getName() + ": test_getImage() end");
}

@Test
public void test_getSizes() {

System.out.println(Thread.currentThread().getName() + ": test_getSizes() start");

Dimension[] sizes = Webcam.getDefault().getViewSizes();

Assert.assertSame(DummyDriver.class, Webcam.getDriver().getClass());

Assert.assertNotNull(sizes);
Assert.assertEquals(2, sizes.length);

System.out.println(Thread.currentThread().getName() + ": test_getSizes() end");
}

@Test
Expand Down

0 comments on commit 304243a

Please sign in to comment.