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(WebcamDriver driver, WebcamDevice device) {
011
012 if (driver == null) {
013 throw new IllegalArgumentException("Webcam driver argument cannot be null");
014 }
015
016 this.sync = !driver.isThreadSafe();
017 this.device = device;
018 this.processor = WebcamProcessor.getInstance();
019 }
020
021 public WebcamDevice getDevice() {
022 return device;
023 }
024
025 /**
026 * Process task by processor thread.
027 *
028 * @throws InterruptedException when thread has been interrupted
029 */
030 public void process() throws InterruptedException {
031 if (sync) {
032 if (processor == null) {
033 throw new RuntimeException("Driver should be synchronized, but processor is null");
034 }
035 processor.process(this);
036 } else {
037 handle();
038 }
039 }
040
041 public Throwable getThrowable() {
042 return throwable;
043 }
044
045 public void setThrowable(Throwable t) {
046 this.throwable = t;
047 }
048
049 protected abstract void handle();
050 }