/* ***************************************************************************+
 * ITX package (cnrg.itx) for telephony application programming.              *
 * Copyright (c) 1999  Cornell University, Ithaca NY                          *
 *                                                                            *
 * This program is free software; you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public Liense as published by        *
 * the Free Software Foundation, either version 2 of the License, or (at      * 
 * your option) any later version.                                            *
 *                                                                            *
 * The ITX package 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.                                                          * 
 *                                                                            *
 * A copy of the license is distributed with this package.  Look in the docs  *
 * directory, filename GPL.                                                   *
 *                                                                            * 
 * Contact information:                                                       *
 * Donna Bergmark                                                             *
 * 484 Rhodes Hall                                                            *
 * Cornell University                                                         *
 * Ithaca, NY 14853-3801                                                      *
 * bergmark@cs.cornell.edu                                                    *
 ******************************************************************************/


package cnrg.itx.datax.jaudio;

import java.util.*;

public class JAudioIn 
{
	//////////////////////////////////////////////////////////////////////
	//																	//
	//				 CLASS MEMBER VARIABLES								//
	//																	//
	//////////////////////////////////////////////////////////////////////

	protected JAudioDevice device;
	protected Queue blockQueue;

	//////////////////////////////////////////////////////////////////////
	//																	//
	//					  CLASS METHODS									//
	//																	//
	//////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////
	//
	//	JAudioIn::JAudioIn()
	//
	//	Description:
	//		JAudioIn constructor.
	//	
	//	Parameters:
	//		device:  The JAudioDevice to which this JAudio belongs.
	//
	//	Returns:
	//		Nothing.
	//
	//////////////////////////////////////////////////////////////////////
	protected JAudioIn(JAudioDevice device) 
	{
		this.device = device;
		blockQueue = new Queue();
	}

	//////////////////////////////////////////////////////////////////////
	//
	//	JAudioIn::start() / stop()
	//
	//	Description:
	//		Starts and stops the JAudioIn.
	//	
	//	Parameters:
	//		None.
	//
	//	Returns:
	//		Nothing.
	//
	//////////////////////////////////////////////////////////////////////
	public void start() throws JAudioException 
	{
		startN();
		device.checkError();
	}
	public void stop() throws JAudioException 
	{
		stopN();
		device.checkError();
	}

	//////////////////////////////////////////////////////////////////////
	//
	//	JAudioIn::addBlock()
	//
	//	Description:
	//		Adds a JAudioBlock to the input queue.
	//	
	//	Parameters:
	//		block:	The JAudioBlock to add to the input queue.
	//
	//	Returns:
	//		Nothing.
	//
	//////////////////////////////////////////////////////////////////////
	public void addBlock(JAudioBlock block) throws JAudioException 
	{
		if (!block.grabUsedInLock()) 
		{
			throw new JAudioException("Block is already being used, can't do input");
		}

		// Add the block to the block queue (to keep GC happy) and send 
		// the block to the device
		blockQueue.Push(block);
		addBlockN(block);
		device.checkError();
	}

	//////////////////////////////////////////////////////////////////////
	//																	//
	//					  CLASS HELPER METHODS							//
	//																	//
	//////////////////////////////////////////////////////////////////////

	// All native methods
	private native void startN();
	private native void addBlockN(JAudioBlock b);
	private native void stopN();
}
