/**
 * (./) CvPlayer.java, v0.1 - 20/03/09
 * (by) Douglas Edric Stanley & Cousot Stéphane
 * (cc) some right reserved
 *
 * Part of the Processing/Java OpenCV Libraries project, for the Atelier Hypermedia, art 
 * school of Aix-en-Provence, and for the Processing and Java community of course.
 *
 *
 * THIS LIBRARY (AND ALSO THIS FILE) IS RELEASED UNDER A CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
 * ‹ http://creativecommons.org/licenses/by/3.0/ ›
 */



// package name
package hypermedia.video;
 




/**
 * The OpenCV movie player.
 */
public class CvPlayer implements Runnable {
	
	
	
	
	/** A separate thread to attach movie events */
	private Thread thread = null;
	
	/** the current movie frame rate (frame per second) in milliseconds */
	int fps = 0;
	boolean repeat = false;
	
	/** 
	 * Movie image width 
	 * @see #height
	 */
	public int width = 0;
	
	/**
	 * Movie image height
	 * @see #width
	 */
	public int height = 0; 
	
	
	/** the attached OpenCV object */
	OpenCV opencv = null;
	
	/** the attached Processing applet or other Java object owner */
	Object parent = null;
	
	
	
	
	
	/**
	 * Create a new OpenCV movie player from the specified file name.
	 *
	 * @param opencv	the OpenCV parent object
	 * @param filename	the filepath of the movie file
	 */
	protected CvPlayer( OpenCV opencv, String filename ) {
		this( opencv, filename, -1, -1 );
	}
	
	/**
	 * Create a new OpenCV movie player from the specified file name, with the given frames 
	 * dimensions.
	 *
	 * @param opencv	the OpenCV parent object
	 * @param filename	the filepath of the movie file
	 * @param width		the width of the movie image
	 * @param height	the height of the movie image
	 */
	protected CvPlayer( OpenCV opencv, String filename, int width, int height ) {
		this( opencv, filename, -1, -1, -1 );
	}
	
	
	/**
	 * Create a new OpenCV movie player from the specified file name, with the given frames 
	 * dimensions.
	 *
	 * @param opencv	the OpenCV parent object
	 * @param filename	the filepath of the movie file
	 * @param width		the width of the movie image
	 * @param height	the height of the movie image
	 * @param framerate	the movie player framerate
	 */
	protected CvPlayer( OpenCV opencv, String filename, int width, int height, int framerate ) {
		
		// load the movie file first to retrieve (new) OpenCV dimensions
		opencv.movie( filename, width, height );
		
		this.opencv = opencv;
		this.parent	= opencv.parent;
		this.height = opencv.height;
		this.width	= opencv.width;
		//this.framerate = framerate;
	}
	
	
	
	
	/**
	 * Set this Video object to play the current movie continuously and start playing.
	 */
	public void loop() {
		if ( opencv!=null ) opencv.restore();
	}
	
	/**
	 * Set this Video object to play the current movie until the last frame then stop.
	 */
	public void noLoop() {
		if ( opencv!=null ) opencv.restore();
	}

	/**
	 * Return the required property from the current capture.
	 *
	 * @param which	either: MOVIE_WIDTH, MOVIE_HEIGHT, MOVIE_FPS, MOVIE_FRAME_COUNT, 
	 * MOVIE_MILLISECONDS, MOVIE_FRAMES, MOVIE_RATIO
	 */
	public native float property( int which );
	
	
	/**
	 * Attempt to play the current movie with the current frame rate.
	 * This method is called automatically and periodically by a separate executing thread. To start
	 * this execution use the <code>start()</code> method.
	 *
	 * @invisible
	 */
	public void run() {
		while( thread!=null && thread.isAlive() ) {
			try {
				Thread.sleep( 1000/fps );
			}
			catch( InterruptedException e ) {;}
		}
	}
	
}
