<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFCFC, #FFFCFC]">

<mx:Script>
<![CDATA[

	import flash.net.NetConnection;
	import flash.external.*;
	import mx.core.FlexGlobals;
	import mx.utils.UIDUtil;

	[Bindable]
	private var netConnection:NetConnection = null;
	private var incomingNetStream:NetStream = null;
	private var outgoingNetStream:NetStream = null;

	private var mic:Microphone = null;
	private var myuid:String;
	
	public var recieverStream:String = "reciever";
	public var senderStream:String = "sender";
	public var rtmpUrl:String = "rtmp:/xmpp";

	public function windowCloseEvent():void 
	{
		netConnection.close();
	}

	private function init():void {
		simpleLabel.text="Waiting....."

		rtmpUrl = FlexGlobals.topLevelApplication.parameters.rtmpUrl;
		recieverStream = FlexGlobals.topLevelApplication.parameters.recieverStream;		
		senderStream = FlexGlobals.topLevelApplication.parameters.senderStream;		
		
		initMicrophone();
		myuid = UIDUtil.createUID();

		//NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;	
		netConnection = new NetConnection();
		//netConnection.objectEncoding = ObjectEncoding.AMF0;
		netConnection.client = this;
		netConnection.addEventListener( NetStatusEvent.NET_STATUS , netStatus );
		netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
		
		netConnection.connect(rtmpUrl);
		
		simpleLabel.text="Connecting....."
		
	}

	private function initMicrophone():void {
		//mic = Microphone.getMicrophone();
		mic = getMicrophone();		

		if(mic == null){
			trace("No available microphone");
		} else {
			mic.setUseEchoSuppression(true);
			mic.setLoopBack(false);
           		mic.setSilenceLevel(0,2000);			
			mic.gain = 80;
			mic.codec = SoundCodec.PCMU;			
			
			mic.addEventListener(ActivityEvent.ACTIVITY, micActivityHandler);
			mic.addEventListener(StatusEvent.STATUS, micStatusHandler);
		}
	}							

	private function getMicrophone() :Microphone 
	{
		var mic:Microphone = Microphone.getEnhancedMicrophone();
		var options:MicrophoneEnhancedOptions = new MicrophoneEnhancedOptions();
		options.mode = MicrophoneEnhancedMode.FULL_DUPLEX;
		options.nonLinearProcessing = true;
		mic.enhancedOptions = options;

		return mic;
	}

	private function micActivityHandler(event:ActivityEvent):void {
		//simpleLabel.text= "activityHandler: " + event;
	}


	private function micStatusHandler(event:StatusEvent):void {		
		switch(event.code) {

		case "Microphone.Muted":
			break;
		case "Microphone.Unmuted":
			break;
		default:
			trace("unknown micStatusHandler event: " + event);
		}
	}


	private function netStatus (evt:NetStatusEvent ):void {		    	
		switch(evt.info.code) {
		
		case "NetConnection.Connect.Success":
			simpleLabel.text="Connection success!"; 			
			connected(recieverStream, senderStream);				
			break;

		case "NetConnection.Connect.Failed":
			simpleLabel.text="Failed to connect";
			disconnected();
			break;

		case "NetConnection.Connect.Rejected":
			simpleLabel.text="Rejected";
			break;

		case "NetStream.Play.StreamNotFound":
			simpleLabel.text="No Stream";
			break;

		case "NetStream.Play.Failed":
			simpleLabel.text="Play error";			
			break;
			
		case "NetStream.Play.Start":		
			simpleLabel.text="Play start";
			
			break;
		case "NetStream.Play.Stop":			
			simpleLabel.text="Play stop";					
			break;
			
		case "NetStream.Buffer.Full":
			break;

		default:
			simpleLabel.text= evt.info.code ;
		}		    	

	}

        private function asyncErrorHandler(event:AsyncErrorEvent):void {
            // ignore AsyncErrorEvent events.
        }
        

        private function securityErrorHandler(event:SecurityErrorEvent):void {
            	simpleLabel.text= "securityErrorHandler: " + event;
        }
        
        
	public function disconnected():* {
		simpleLabel.text="Disconnected";
		
		incomingNetStream.play(false); 
		outgoingNetStream.attachAudio(null);
		outgoingNetStream.publish(null);

	}

	public function connected(publishName:String, playName:String):* {
		simpleLabel.text= "Connected: " + publishName;
		
		incomingNetStream = new NetStream(netConnection);
		incomingNetStream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
		incomingNetStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);

		outgoingNetStream = new NetStream(netConnection);
		outgoingNetStream.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
		outgoingNetStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);			
		outgoingNetStream.attachAudio(mic);

		incomingNetStream.play(playName); 
		outgoingNetStream.publish(publishName, "live"); 	
		
	}
		

]]>
</mx:Script>
<mx:Label x="5" y="220" id="simpleLabel" fontSize="10" text=""/>

</mx:Application>
