Skip to content

Commit

Permalink
Under some conditions webcam API completely hangs, fixes #249
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Aug 8, 2014
1 parent 6e4923b commit 0b826cf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ public class WebcamProcessor {

private static final Logger LOG = LoggerFactory.getLogger(WebcamProcessor.class);

/**
* Thread doing supersync processing.
*
* @author sarxos
*/
public static final class ProcessorThread extends Thread {

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

public ProcessorThread(Runnable r) {
super(r, String.format("atomic-processor-%d", N.incrementAndGet()));
}
}

/**
* Thread factory for processor.
*
* @author Bartosz Firyn (SarXos)
*/
private static final class ProcessorThreadFactory implements ThreadFactory {

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

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, String.format("atomic-processor-%d", N.incrementAndGet()));
Thread t = new ProcessorThread(r);
t.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
t.setDaemon(true);
return t;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.github.sarxos.webcam;



public abstract class WebcamTask {

private boolean sync = true;
private boolean doSync = true;
private WebcamProcessor processor = null;
private WebcamDevice device = null;
private Throwable throwable = null;

public WebcamTask(boolean threadSafe, WebcamDevice device) {
this.sync = !threadSafe;
this.doSync = !threadSafe;
this.device = device;
this.processor = WebcamProcessor.getInstance();
}
Expand All @@ -31,13 +33,20 @@ public WebcamDevice getDevice() {
* @throws InterruptedException when thread has been interrupted
*/
public void process() throws InterruptedException {
if (sync) {
if (processor == null) {
throw new RuntimeException("Driver should be synchronized, but processor is null");
}
processor.process(this);
} else {

boolean alreadyInSync = Thread.currentThread() instanceof WebcamProcessor.ProcessorThread;

if (alreadyInSync) {
handle();
} else {
if (doSync) {
if (processor == null) {
throw new RuntimeException("Driver should be synchronized, but processor is null");
}
processor.process(this);
} else {
handle();
}
}
}

Expand Down

0 comments on commit 0b826cf

Please sign in to comment.