// Path: src/main/java/au/edu/jcu/v4l4j/exceptions/StateException.java
// 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);
// 	}
// }
// 
// Path: src/main/java/au/edu/jcu/v4l4j/exceptions/V4L4JException.java
// 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);
// 	}
// }


import java.util.concurrent.ThreadFactory;
import au.edu.jcu.v4l4j.exceptions.StateException;
import au.edu.jcu.v4l4j.exceptions.V4L4JException;
/*
* Copyright (C) 2011 Gilles Gigan (gilles.gigan@gmail.com)
*
* 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;




/**
 * PushSource instances create their own thread which polls
 * a frame grabber and notify the 
 * {@link CaptureCallback} object given in the constructor
 * each time a new frame is available.
 * @author gilles
 *
 */
class PushSource implements Runnable {
	private CaptureCallback 		callback;
	private AbstractGrabber			frameGrabber;
	private Thread					thread;
	private ThreadFactory			threadFactory;
	
	private int						state;
	private static final int		STATE_STOPPED = 0;
	private static final int		STATE_RUNNING = 1;
	private static final int		STATE_ABOUT_TO_STOP = 2;
	
	/**
	 * This method builds a new <code>PushSource</code> instance
	 * which will obtain frames from the given frame grabber and
	 * pass them to the given callback object.
	 * @param grabber the {@link FrameGrabber} instance on which
	 * this push source will repeatedly call {@link FrameGrabber#getVideoFrame()}.
	 * @param callback an object implementing the {@link CaptureCallback}
	 * interface to which the frames will be delivered through the 
	 * {@link CaptureCallback#nextFrame(VideoFrame)}.
	 */
	public PushSource(AbstractGrabber grabber, CaptureCallback callback, ThreadFactory factory) {
		if ((grabber == null) || (callback == null))
			throw new NullPointerException("the frame grabber and callback cannot be null");
		
		this.callback = callback;
		frameGrabber = grabber;
		threadFactory = factory;
		state = STATE_STOPPED;
	}
	
	/**
	 * This method instructs this source to start the capture and to push
	 * to the captured frame to the  
	 * {@link CaptureCallback}. 
	 * @throws StateException if the capture has already been started
	 */
// Path: src/main/java/au/edu/jcu/v4l4j/exceptions/StateException.java
// 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);
// 	}
// }
// 
// Path: src/main/java/au/edu/jcu/v4l4j/exceptions/V4L4JException.java
// 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);
// 	}
// }


// Path: src/main/java/au/edu/jcu/v4l4j/PushSource.java
import java.util.concurrent.ThreadFactory;
import au.edu.jcu.v4l4j.exceptions.StateException;
import au.edu.jcu.v4l4j.exceptions.V4L4JException;

/*
* Copyright (C) 2011 Gilles Gigan (gilles.gigan@gmail.com)
*
* 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;




/**
 * PushSource instances create their own thread which polls
 * a frame grabber and notify the 
 * {@link CaptureCallback} object given in the constructor
 * each time a new frame is available.
 * @author gilles
 *
 */
class PushSource implements Runnable {
	private CaptureCallback 		callback;
	private AbstractGrabber			frameGrabber;
	private Thread					thread;
	private ThreadFactory			threadFactory;
	
	private int						state;
	private static final int		STATE_STOPPED = 0;
	private static final int		STATE_RUNNING = 1;
	private static final int		STATE_ABOUT_TO_STOP = 2;
	
	/**
	 * This method builds a new <code>PushSource</code> instance
	 * which will obtain frames from the given frame grabber and
	 * pass them to the given callback object.
	 * @param grabber the {@link FrameGrabber} instance on which
	 * this push source will repeatedly call {@link FrameGrabber#getVideoFrame()}.
	 * @param callback an object implementing the {@link CaptureCallback}
	 * interface to which the frames will be delivered through the 
	 * {@link CaptureCallback#nextFrame(VideoFrame)}.
	 */
	public PushSource(AbstractGrabber grabber, CaptureCallback callback, ThreadFactory factory) {
		if ((grabber == null) || (callback == null))
			throw new NullPointerException("the frame grabber and callback cannot be null");
		
		this.callback = callback;
		frameGrabber = grabber;
		threadFactory = factory;
		state = STATE_STOPPED;
	}
	
	/**
	 * This method instructs this source to start the capture and to push
	 * to the captured frame to the  
	 * {@link CaptureCallback}. 
	 * @throws StateException if the capture has already been started
	 */
	public synchronized final long startCapture() throws V4L4JException{