001 package com.github.sarxos.webcam;
002
003 public abstract class WebcamTask {
004
005 private boolean sync = true;
006 private WebcamProcessor processor = null;
007 private WebcamDevice device = null;
008 private Throwable throwable = null;
009
010 public WebcamTask(boolean threadSafe, WebcamDevice device) {
011 this.sync = !threadSafe;
012 this.device = device;
013 this.processor = WebcamProcessor.getInstance();
014 }
015
016 public WebcamTask(WebcamDriver driver, WebcamDevice device) {
017 this(driver.isThreadSafe(), device);
018 }
019
020 public WebcamTask(WebcamDevice device) {
021 this(false, device);
022 }
023
024 public WebcamDevice getDevice() {
025 return device;
026 }
027
028 /**
029 * Process task by processor thread.
030 *
031 * @throws InterruptedException when thread has been interrupted
032 */
033 public void process() throws InterruptedException {
034 if (sync) {
035 if (processor == null) {
036 throw new RuntimeException("Driver should be synchronized, but processor is null");
037 }
038 processor.process(this);
039 } else {
040 handle();
041 }
042 }
043
044 public Throwable getThrowable() {
045 return throwable;
046 }
047
048 public void setThrowable(Throwable t) {
049 this.throwable = t;
050 }
051
052 protected abstract void handle();
053 }