001    package com.github.sarxos.webcam.ds.civil;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.slf4j.Logger;
007    import org.slf4j.LoggerFactory;
008    
009    import com.github.sarxos.webcam.WebcamDevice;
010    import com.github.sarxos.webcam.WebcamDriver;
011    import com.lti.civil.CaptureDeviceInfo;
012    import com.lti.civil.CaptureException;
013    import com.lti.civil.CaptureSystem;
014    import com.lti.civil.CaptureSystemFactory;
015    import com.lti.civil.DefaultCaptureSystemFactorySingleton;
016    
017    
018    public class LtiCivilDriver implements WebcamDriver {
019    
020            // load civil DLL
021            static {
022                    LtiCivilLoader.load("civil");
023            }
024    
025            private static final Logger LOG = LoggerFactory.getLogger(LtiCivilDriver.class);
026    
027            private static List<WebcamDevice> devices = null;
028            private static CaptureSystemFactory factory = null;
029            private static CaptureSystem system = null;
030            private static boolean initialized = false;
031    
032            private static void initialize() {
033                    factory = DefaultCaptureSystemFactorySingleton.instance();
034                    try {
035                            system = factory.createCaptureSystem();
036                            system.init();
037                    } catch (UnsatisfiedLinkError e) {
038                            // ignore - it is already loaded
039                    } catch (CaptureException e) {
040                            LOG.error("Capture exception", e);
041                    }
042            }
043    
044            protected static CaptureSystem getCaptureSystem() {
045                    if (!initialized) {
046                            initialize();
047                    }
048                    return system;
049            }
050    
051            @Override
052            public List<WebcamDevice> getDevices() {
053    
054                    if (!initialized) {
055                            initialize();
056                    }
057    
058                    if (devices == null) {
059                            devices = new ArrayList<WebcamDevice>();
060                            try {
061    
062                                    @SuppressWarnings("unchecked")
063                                    List<CaptureDeviceInfo> infos = system.getCaptureDeviceInfoList();
064    
065                                    for (CaptureDeviceInfo cdi : infos) {
066                                            devices.add(new LtiCivilDevice(cdi));
067                                    }
068                            } catch (CaptureException e) {
069                                    e.printStackTrace();
070                            }
071                    }
072    
073                    return devices;
074            }
075    
076            @Override
077            public boolean isThreadSafe() {
078                    return false;
079            }
080    
081            @Override
082            public String toString() {
083                    return getClass().getSimpleName();
084            }
085    }