001package com.github.sarxos.webcam.ds.civil; 002 003import java.util.ArrayList; 004import java.util.List; 005import java.util.concurrent.atomic.AtomicBoolean; 006 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010import com.github.sarxos.webcam.WebcamDevice; 011import com.github.sarxos.webcam.WebcamDriver; 012import com.github.sarxos.webcam.WebcamException; 013import com.lti.civil.CaptureDeviceInfo; 014import com.lti.civil.CaptureException; 015import com.lti.civil.CaptureSystem; 016import com.lti.civil.CaptureSystemFactory; 017import com.lti.civil.DefaultCaptureSystemFactorySingleton; 018 019 020public class LtiCivilDriver implements WebcamDriver { 021 022 // load civil DLL 023 static { 024 025 } 026 027 private static final Logger LOG = LoggerFactory.getLogger(LtiCivilDriver.class); 028 029 private static CaptureSystemFactory factory = null; 030 private static CaptureSystem system = null; 031 032 private static volatile boolean ready = false; 033 private static final AtomicBoolean INIT = new AtomicBoolean(false); 034 035 private static void initialize() { 036 037 if (!INIT.compareAndSet(false, true)) { 038 return; 039 } 040 041 LtiCivilLoader.load("civil"); 042 043 factory = DefaultCaptureSystemFactorySingleton.instance(); 044 045 try { 046 system = factory.createCaptureSystem(); 047 system.init(); 048 049 ready = true; 050 051 } catch (UnsatisfiedLinkError e) { 052 // ignore - it is already loaded 053 LOG.debug("Library already loaded"); 054 } catch (CaptureException e) { 055 throw new WebcamException(e); 056 } 057 } 058 059 protected static CaptureSystem getCaptureSystem() { 060 initialize(); 061 return system; 062 } 063 064 @Override 065 public List<WebcamDevice> getDevices() { 066 067 initialize(); 068 069 int i = 0; 070 while (!ready) { 071 072 try { 073 Thread.sleep(100); 074 } catch (InterruptedException e) { 075 return null; 076 } 077 078 // wait max 10 seconds for driver to be ready 079 if (i++ > 100) { 080 throw new RuntimeException("Cannot get devices because capture driver has not become ready for 10 seconds"); 081 } 082 } 083 084 List<WebcamDevice> devices = new ArrayList<WebcamDevice>(); 085 086 try { 087 for (Object cdi : system.getCaptureDeviceInfoList()) { 088 devices.add(new LtiCivilDevice((CaptureDeviceInfo) cdi)); 089 } 090 } catch (CaptureException e) { 091 throw new WebcamException(e); 092 } 093 094 return devices; 095 } 096 097 @Override 098 public boolean isThreadSafe() { 099 return false; 100 } 101 102 @Override 103 public String toString() { 104 return getClass().getSimpleName(); 105 } 106}