<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   minWidth="500" minHeight="350" creationComplete="init()">

	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>

	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.core.FlexGlobals;
			private var streamer:String;
			private var file:String;
			private var camera:Camera;
			private var microphone:Microphone;
			private var connection:NetConnection;
			private var stream:NetStream;
			private var h264Settings:H264VideoStreamSettings;

			private function toggleFeedListener(event:MouseEvent):void {
				if(toggleFeed.label == 'Start Feed') {
					toggleFeed.label = 'Stop Feed';
					stream.publish(file, 'live');
					videoDisplay.attachCamera(camera);
					toggleVideo.enabled = true;
					toggleAudio.enabled = true;
				} else {
					toggleFeed.label = 'Start Feed';
					stream.close();
					videoDisplay.attachCamera(null);
					toggleVideo.enabled = false;
					toggleAudio.enabled = false;
				}
			}

			private function toggleVideoListener(event:MouseEvent):void {
				if(toggleVideo.label == 'Start Video') {
					toggleVideo.label = 'Stop Video';
					videoDisplay.attachCamera(camera);
					stream.attachCamera(camera);
				} else {
					toggleVideo.label = 'Start Video';
					videoDisplay.attachCamera(null);
					stream.attachCamera(null);
				}
			}

			private function toggleAudioListener(event:MouseEvent):void {
				if(toggleAudio.label == 'Start Audio') {
					toggleAudio.label = 'Stop Audio';
					stream.attachAudio(microphone);
				} else {
					toggleAudio.label = 'Start Audio';
					stream.attachAudio(null);
				}
			}

			private function initListeners():void {
				toggleFeed.addEventListener(MouseEvent.CLICK, toggleFeedListener);
				toggleVideo.addEventListener(MouseEvent.CLICK, toggleVideoListener);
				toggleAudio.addEventListener(MouseEvent.CLICK, toggleAudioListener);
			}

			private function netStatusHander(event:NetStatusEvent):void {
				switch(event.info.code) {
					case 'NetConnection.Connect.Success':
						stream = new NetStream(connection);
						stream.attachCamera(camera);
						stream.attachAudio(microphone);
						h264Settings = new H264VideoStreamSettings();
						h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_1_2);
						stream.videoStreamSettings = h264Settings;
						break;
				}
			}

			private function init():void {
				streamer = FlexGlobals.topLevelApplication.parameters.streamer;
				file = FlexGlobals.topLevelApplication.parameters.file;
				if(file == null) {
					Alert.show('Missing flashvars: file');
					return;
				}
				if(streamer == null) {
					Alert.show('Missing flashvars: streamer');
					return;
				}
				initListeners();

				camera = Camera.getCamera();
				microphone = Microphone.getMicrophone();
				microphone.setSilenceLevel(0);
				microphone.codec = "Speex";
				microphone.encodeQuality = 6;
				camera.setMode(704, 576, 25);
				camera.setQuality(131072, 70);
				connection = new NetConnection();
				connection.connect(streamer);
				connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHander);

			}
		]]>
	</fx:Script>
	<s:Panel x="0" y="0" width="100%" height="100%" title="RTMP Publisher">
		<mx:VideoDisplay width="100%" height="100%" id="videoDisplay">
		</mx:VideoDisplay>
		<s:controlBarContent>
			<s:Button label="Start Feed" id="toggleFeed"></s:Button>
			<s:Spacer width="100%" height="100%"/>
			<s:Button label="Stop Video" id="toggleVideo" enabled="false"></s:Button>
			<s:Button label="Stop Audio" id="toggleAudio" enabled="false"></s:Button>
		</s:controlBarContent>
	</s:Panel>
</s:Application>
