public static class DiscreteInterval {	
	/**
	 * The numerator for this discrete frame interval.
	 */
	public final int numerator;
		
	/**
	 * The denominator for this discrete frame interval.
	 */
	public final int denominator;
		
	DiscreteInterval(int n, int d){
		numerator = n;
		denominator = d;
	}
		
	/**
	 * This method returns the numerator of this frame interval.
	 * @return the numerator of the frame interval
	 */
	public int getNum(){
		return numerator;
	}
		
	/**
	 * This method returns the denominator of this frame interval.
	 * @return the denominator of this frame interval
	 */
	public int getDenom(){
		return denominator;
	}
		
	@Override
	public String toString(){
		return numerator+"/"+denominator;
	}
		
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + denominator;
		result = prime * result + numerator;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof DiscreteInterval))
			return false;
		DiscreteInterval other = (DiscreteInterval) obj;
		if (denominator != other.denominator)
			return false;
		if (numerator != other.numerator)
			return false;
		return true;
	}
} public class CaptureChannelException extends V4L4JException {

	private static final long serialVersionUID = -3338859321078232443L;

	public CaptureChannelException(String message) {
		super(message);
	}

	public CaptureChannelException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public CaptureChannelException(Throwable throwable) {
		super(throwable);
	}
} public class ImageFormatException extends V4L4JException {

	private static final long serialVersionUID = -3338859321078232443L;

	public ImageFormatException(String message) {
		super(message);
	}

	public ImageFormatException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public ImageFormatException(Throwable throwable) {
		super(throwable);
	}
} public class InitialisationException extends V4L4JException {

	private static final long serialVersionUID = -3338859321078232443L;

	public InitialisationException(String message) {
		super(message);
	}

	public InitialisationException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public InitialisationException(Throwable throwable) {
		super(throwable);
	}
} public class InvalidValue extends RuntimeException {
	private static final long serialVersionUID = -3506474708008499397L;

	public InvalidValue(String message) {
		super(message);
	}

	public InvalidValue(String message, Throwable throwable) {
		super(message, throwable);
	}

	public InvalidValue(Throwable throwable) {
		super(throwable);
	}
} public class NoTunerException extends V4L4JException {

	private static final long serialVersionUID = -4596596557974047977L;

	public NoTunerException(String message) {
		super(message);
	}

	public NoTunerException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public NoTunerException(Throwable throwable) {
		super(throwable);
	}

} public class StateException extends RuntimeException {

	private static final long serialVersionUID = 1351714754008657462L;

	public StateException(String message) {
		super(message);
	}

	public StateException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public StateException(Throwable throwable) {
		super(throwable);
	}
} public class UnsupportedMethod extends RuntimeException {
	private static final long serialVersionUID = -8801339441741012577L;

	public UnsupportedMethod(String message) {
		super(message);
	}

	public UnsupportedMethod(String message, Throwable throwable) {
		super(message, throwable);
	}

	public UnsupportedMethod(Throwable throwable) {
		super(throwable);
	}
} public class V4L4JException extends Exception {

	private static final long serialVersionUID = -8247407640809375121L;
	public V4L4JException(String message) {
		    super(message);
		  }
	public V4L4JException(String message, Throwable throwable) {
		super(message, throwable);
	}
	public V4L4JException(Throwable throwable) {
		super(throwable);
	}
} public class VideoStandardException extends V4L4JException {

	private static final long serialVersionUID = -3338859321078232443L;

	public VideoStandardException(String message) {
		super(message);
	}

	public VideoStandardException(String message, Throwable throwable) {
		super(message, throwable);
	}

	public VideoStandardException(Throwable throwable) {
		super(throwable);
	}
}
import au.edu.jcu.v4l4j.exceptions.InitialisationException;
import au.edu.jcu.v4l4j.exceptions.InvalidValue;
import au.edu.jcu.v4l4j.exceptions.NoTunerException;
import au.edu.jcu.v4l4j.exceptions.StateException;
import au.edu.jcu.v4l4j.exceptions.UnsupportedMethod;
import au.edu.jcu.v4l4j.exceptions.V4L4JException;
import au.edu.jcu.v4l4j.exceptions.VideoStandardException;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.Vector;
import au.edu.jcu.v4l4j.FrameInterval.DiscreteInterval;
import au.edu.jcu.v4l4j.exceptions.CaptureChannelException;
import au.edu.jcu.v4l4j.exceptions.ImageFormatException;
/*
 * Copyright (C) 2007-2008 Gilles Gigan (gilles.gigan@gmail.com)
 * eResearch Centre, James Cook University (eresearch.jcu.edu.au)
 *
 * This program was developed as part of the ARCHER project
 * (Australian Research Enabling Environment) funded by a   
 * Systemic Infrastructure Initiative (SII) grant and supported by the Australian
 * Department of Innovation, Industry, Science and Research
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public  License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package au.edu.jcu.v4l4j;




/**
 * This abstract class implements the core functionalities found in all frame 
 * grabbers, initialisation, starting the capture, capturing frames, stopping
 * the capture and releasing resources. It must be subclassed.<br>
 * Subclasses must implement {@link FrameGrabber#getImageFormat()} to return the
 * correct image format used for capture. {@link #init()} may also be overridden
 * if required.
 * @author gilles 
 */

abstract class AbstractGrabber implements FrameGrabber {
	protected final static int RAW_GRABBER = 0;
	protected final static int JPEG_GRABBER = 1;
	protected final static int RGB24_GRABBER = 2;
	protected final static int BGR24_GRABBER = 3;
	protected final static int YUV_GRABBER = 4;
	protected final static int YVU_GRABBER = 5;

	protected DeviceInfo 				dInfo;
	private int 						width;
	private int 						height;
	private int 						channel;
	private int 						standard;
	protected int 						nbV4LBuffers;
	protected Vector<BaseVideoFrame> 	videoFrames;
	private Vector<BaseVideoFrame> 		availableVideoFrames;
	protected State 					state;
	protected int 						format;
	private Tuner 						tuner;
	private int 						type;
	private long						lastCapturedFrameSequence;	// update v4l4j_FrameGrabber.c if
	private long						lastCapturedFrameTimeuSec;	// these three names are changed
	private int							lastCapturedFrameBufferIndex;//
	private PushSource					pushSource;
	private ThreadFactory				threadFactory;

	/*
	 * JNI returns a long (which is really a pointer) when a device is allocated
	 * for use. This field is read-only (!!!) 
	 */
	protected long object;

	static {
		try {
			System.loadLibrary("v4l4j");
		} catch (UnsatisfiedLinkError e) {
			System.err.println("Cant load v4l4j JNI library");
			throw e;
		}
	}


	private native int doInit(long o, int numBuffers, int w, int h, int ch, int std,
			int requestedFormat, int output)
	throws V4L4JException;

	private native void start(long o) throws V4L4JException;
	/**
	 * This method sets a new value for the JPEG quality
	 * @param o the struct v4l4_device
	 * @param i the new value
	 * @throws V4L4JException if the JPEG quality is disabled because of the
	 * type of this frame grabber (not {@link #JPEG_GRABBER}).
	 */
	protected native void setQuality(long o, int i);
	private native int getBufferSize(long o);
	private native int enqueueBuffer(long o, int index);
	private native int fillBuffer(long o, byte array[]) throws V4L4JException;
	private native void stop(long o);
	private native void doRelease(long o);
	private native void doSetFrameIntv(long o, int n, int d) throws InvalidValue;