<commit_before>package robowars.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Format;
import javax.media.MediaLocator;

import org.apache.log4j.Logger;

import com.lti.civil.CaptureException;
import com.lti.civil.CaptureSystem;
import com.lti.civil.impl.jni.NativeCaptureSystemFactory;

import net.sf.fmj.*;
import net.sf.fmj.media.RegistryDefaults;
import net.sf.fmj.media.cdp.GlobalCaptureDevicePlugger;
import net.sf.fmj.ui.FmjStudio;
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.utility.ClasspathChecker;

/**
 * Handles the streaming of live video and transmission of other camera
 * status messages to connected users. This class also stores a list of
 * all available camera, and manages the selection of a currently active
 * camera.
 */
public class MediaStreamer {
	/** The logger used by this class */
	private static Logger log = Logger.getLogger(MediaStreamer.class);
	
	/** 
	 * A list of all connected CameraControllers (each controller represents
	 * a separately connected USB webcam).
	 */
	private List<CameraController> cameras;
	
	/**
	 * The camera currently selected to stream video from.
	 */
	private CameraController selectedCamera;

	/**
	 * Generates a new MediaStreamer
	 */
	public MediaStreamer() 
	{
		RegistryDefaults.registerAll(RegistryDefaults.FMJ);
		cameras = new ArrayList<CameraController>();
		GlobalCaptureDevicePlugger.addCaptureDevices();
	}
	
	/**
	 * Testing function for using the LTI-Civil library (lower level than FMJ)
	 * Usage of the lower level library may be necessary to set video capture
	 * parameters (resolution, frame rate, etc.)
	 */
	public void ltiCivilTest() {
		NativeCaptureSystemFactory captureFactory = new NativeCaptureSystemFactory();
		try {
			CaptureSystem captureSystem = captureFactory.createCaptureSystem();
			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());

			}
		} catch (CaptureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Clears the existing camera controller list and re-detects connected
	 * capture devices. Note that the coordinate information for existing
	 * cameras will be lost.
	 * 
	 * TODO:	Keep coordinate information for devices which have been
	 * 			previously configured
	 */
	public void updateDeviceList() {
		// Store the old list of cameras, and generate a new list to store
		// cameras detected during this update
		List<CameraController> oldCameras = cameras;
		cameras = new ArrayList<CameraController>();

		// Iterate through all detected cameras, and generate a new camera controller
		// for those which did not exist in the previous camera list
		Vector<CaptureDeviceInfo> webcamListing = CaptureDeviceManager.getDeviceList(null);
		for(CaptureDeviceInfo info : webcamListing) {
			if(info.getLocator().toString().startsWith("civil")) {
				log.info("Found video device: " + info.getName());
				for(Format f : info.getFormats()) {
					log.info("\"" + info.getName() + "\" supports format: " + f.toString());
				}
				
				boolean foundExisting = false;
				for(CameraController c : oldCameras) {
					if(c.getMediaLocator() == info.getLocator()) {
						cameras.add(c);
						foundExisting = true;
						break;
					}
				}
				
				if(!foundExisting) {
					CameraController newCam = new CameraController(info);
					cameras.add(newCam);
				}
				
				// ------------- TESTING -----------------
				selectedCamera = cameras.get(0); // TODO: Set through UI, this is just for testing
				// ------------- TESTING -----------------
			}
		}
		
		// ------------- TESTING -----------------
		cameras.add(0, new CameraController(new CaptureDeviceInfo("Test Cam", 
				new MediaLocator("locator"), null)));
		selectedCamera = cameras.get(0); // TODO: Set through UI, this is just for testing
		// ------------- TESTING -----------------
		
		oldCameras.clear();
		oldCameras = null;
	}
	
	/**
	 * @return	The currently selected CameraController (or null if no
	 * 			camera has been selected)
	 */
	public CameraController getActiveCamera() {
		return selectedCamera;
	}
	
	/**
	 * Sets the active camera. The specified camera must be either null (video
	 * stream disabled), or a camera controller previously detected by the
	 * MediaStreamer instance.<commit_msg>Quick fix to ensure camera demo works for presentation.<commit_after>package robowars.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Format;
import javax.media.MediaLocator;

import org.apache.log4j.Logger;

import com.lti.civil.CaptureException;
import com.lti.civil.CaptureSystem;
import com.lti.civil.impl.jni.NativeCaptureSystemFactory;

import net.sf.fmj.*;
import net.sf.fmj.media.RegistryDefaults;
import net.sf.fmj.media.cdp.GlobalCaptureDevicePlugger;
import net.sf.fmj.ui.FmjStudio;
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.utility.ClasspathChecker;

/**
 * Handles the streaming of live video and transmission of other camera
 * status messages to connected users. This class also stores a list of
 * all available camera, and manages the selection of a currently active
 * camera.
 */
public class MediaStreamer {
	/** The logger used by this class */
	private static Logger log = Logger.getLogger(MediaStreamer.class);
	
	/** 
	 * A list of all connected CameraControllers (each controller represents
	 * a separately connected USB webcam).
	 */
	private List<CameraController> cameras;
	
	/**
	 * The camera currently selected to stream video from.
	 */
	private CameraController selectedCamera;

	/**
	 * Generates a new MediaStreamer
	 */
	public MediaStreamer() 
	{
		RegistryDefaults.registerAll(RegistryDefaults.FMJ);
		cameras = new ArrayList<CameraController>();
		GlobalCaptureDevicePlugger.addCaptureDevices();
	}
	
	/**
	 * Testing function for using the LTI-Civil library (lower level than FMJ)
	 * Usage of the lower level library may be necessary to set video capture
	 * parameters (resolution, frame rate, etc.)
	 */
	public void ltiCivilTest() {
		NativeCaptureSystemFactory captureFactory = new NativeCaptureSystemFactory();
		try {
			CaptureSystem captureSystem = captureFactory.createCaptureSystem();
			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());

			}
		} catch (CaptureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Clears the existing camera controller list and re-detects connected
	 * capture devices. Note that the coordinate information for existing
	 * cameras will be lost.
	 * 
	 * TODO:	Keep coordinate information for devices which have been
	 * 			previously configured
	 */
	public void updateDeviceList() {
		// Store the old list of cameras, and generate a new list to store
		// cameras detected during this update
		List<CameraController> oldCameras = cameras;
		cameras = new ArrayList<CameraController>();

		// Iterate through all detected cameras, and generate a new camera controller
		// for those which did not exist in the previous camera list
		Vector<CaptureDeviceInfo> webcamListing = CaptureDeviceManager.getDeviceList(null);
		for(CaptureDeviceInfo info : webcamListing) {
			if(info.getLocator().toString().startsWith("civil")) {
				log.info("Found video device: " + info.getName());
				for(Format f : info.getFormats()) {
					log.info("\"" + info.getName() + "\" supports format: " + f.toString());
				}
				
				boolean foundExisting = false;
				for(CameraController c : oldCameras) {
					if(c.getMediaLocator() == info.getLocator()) {
						cameras.add(c);
						foundExisting = true;
						break;
					}
				}
				
				if(!foundExisting) {
					CameraController newCam = new CameraController(info);
					cameras.add(newCam);
				}
				
				// ------------- TESTING -----------------
				selectedCamera = cameras.get(0); // TODO: Set through UI, this is just for testing
				// ------------- TESTING -----------------
			}
		}
		
		/*
		// ------------- TESTING -----------------
		cameras.add(0, new CameraController(new CaptureDeviceInfo("Test Cam", 
				new MediaLocator("locator"), null)));
		selectedCamera = cameras.get(0); // TODO: Set through UI, this is just for testing
		// ------------- TESTING -----------------
		*/
		
		oldCameras.clear();
		oldCameras = null;
	}
	
	/**
	 * @return	The currently selected CameraController (or null if no
	 * 			camera has been selected)
	 */
	public CameraController getActiveCamera() {
		return selectedCamera;
	}
	
	/**
	 * Sets the active camera. The specified camera must be either null (video
	 * stream disabled), or a camera controller previously detected by the
	 * MediaStreamer instance.
{'fraction_non_alphanumeric': 0.12316493955094991, 'fraction_numerical': 0.0008635578583765112, 'mean_word_length': 3.345684803001876, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '532491', 'n_tokens_mistral': 2910, 'n_tokens_neox': 2499, 'n_words': 769}