/** * Re-detects all camera devices connected to the system. Returns without performing any action if * a media stream is currently being served. */ public synchronized void updateDeviceList() { if (currentStream != null) { log.error("Attempted to update capture list while serving a stream."); return; } // Store the old list of cameras, and generate a new list to store // cameras detected during this update List<CameraController> oldCameras = cameras; CameraController oldActiveCam = getActiveCamera(); cameras = new ArrayList<CameraController>(); // Detect all connected cameras, and generate a new CameraController // for those which were not present during the last update of the device // list NativeCaptureSystemFactory captureFactory = new NativeCaptureSystemFactory(); try { CaptureSystem captureSystem = captureFactory.createCaptureSystem(); captureSystem.init(); List<com.lti.civil.CaptureDeviceInfo> devices = captureSystem.getCaptureDeviceInfoList(); for (com.lti.civil.CaptureDeviceInfo device : devices) { log.info( "LTI-Civil found device: " + device.getDescription() + " - " + device.getDeviceID()); // Add the previously existing camera controller for the detected // device, or create a new one of no controller is available boolean foundExisting = false; for (CameraController c : oldCameras) { if (c.getDeviceId().equals(device.getDeviceID())) { cameras.add(c); foundExisting = true; break; } } if (!foundExisting) { CameraController newCam = new CameraController(device); cameras.add(newCam); } } if (cameras.isEmpty()) { selectedCamera = null; } else { if (cameras.contains(oldActiveCam)) { setActiveCamera(oldActiveCam); } else { // Note: Defaults to first detected camera if no active // camera was previously selected setActiveCamera(cameras.get(0)); } } } catch (CaptureException e) { e.printStackTrace(); } }
/** Starts streaming of the selected camera if the required resolution is supported. */ public synchronized void playStream() { if (getActiveCamera() != null) { CaptureStream stream = getActiveCamera().getCaptureStream(); if (stream != null) { try { boolean gotValidFormat = false; for (VideoFormat v : stream.enumVideoFormats()) { if (v.getWidth() == VIDEO_WIDTH && v.getHeight() == VIDEO_HEIGHT) { stream.setVideoFormat(v); log.info( "Selected capture format: " + v.getWidth() + "x" + v.getHeight() + " - " + v.getFPS() + " fps - " + v.getFormatType() + "(" + VideoFormat.RGB24 + " RGB24, " + VideoFormat.RGB32 + " RGB32)"); gotValidFormat = true; break; } } if (!gotValidFormat) { log.error( "Selected capture device does not support the required resolution: " + VIDEO_WIDTH + " x " + VIDEO_HEIGHT + " px"); } stream.setObserver(observer); stream.start(); currentStream = stream; } catch (CaptureException e) { log.error("Attempt to play capture stream failed."); e.printStackTrace(); } } } }
/** Stops the currently active media stream (does nothing if no stream is active) */ public synchronized void stopStream() { if (currentStream != null) { try { currentStream.stop(); } catch (CaptureException e) { log.error("Attempt to stop capture stream failed."); e.printStackTrace(); } finally { currentStream.setObserver(null); try { currentStream.dispose(); } catch (CaptureException e) { log.error("Attempt to dispose capture stream failed."); e.printStackTrace(); } currentStream = null; } } }
@Override /** Logs errors in the capture stream */ public void onError(CaptureStream stream, CaptureException err) { log.error("Error in capture stream:\n" + err.getMessage()); }