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