1 package { 2 import flash.display.MovieClip; 3 import flash.net.NetConnection; 4 import flash.events.NetStatusEvent; 5 import flash.events.MouseEvent; 6 import flash.events.AsyncErrorEvent; 7 import flash.net.NetStream; 8 import flash.media.Video; 9 import flash.media.Camera; 10 import flash.media.Microphone; 11 import fl.controls.Button; 12 import fl.controls.Label; 13 import fl.controls.TextArea; 14 15 16 public class Metadata extends MovieClip { 17 private var nc:NetConnection; 18 private var ns:NetStream; 19 private var nsPlayer:NetStream; 20 private var vid:Video; 21 private var vidPlayer:Video; 22 private var cam:Camera; 23 private var mic:Microphone; 24 private var clearBtn:Button; 25 private var startPlaybackBtn:Button; 26 private var outgoingLbl:Label; 27 private var incomingLbl:Label; 28 private var myMetadata:Object; 29 private var outputWindow:TextArea; 30 31 public function Metadata(){ 32 setupUI(); 33 nc = new NetConnection(); 34 nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 35 nc.connect("rtmp://localhost/publishlive"); 36 } 37 38 /* 39 * Clear the MetaData associated with the stream 40 */ 41 private function clearHandler(event:MouseEvent):void { 42 if (ns){ 43 trace("Clearing MetaData"); 44 ns.send("@clearDataFrame", "onMetaData"); 45 } 46 } 47 48 private function startHandler(event:MouseEvent):void { 49 displayPlaybackVideo(); 50 } 51 52 private function onNetStatus(event:NetStatusEvent):void { 53 trace(event.target + ": " + event.info.code); 54 switch (event.info.code) 55 { 56 case "NetConnection.Connect.Success": 57 publishCamera(); 58 displayPublishingVideo(); 59 break; 60 } 61 } 62 63 private function asyncErrorHandler(event:AsyncErrorEvent):void { 64 trace(event.text); 65 } 66 67 private function sendMetadata():void { 68 trace("sendMetaData() called") 69 myMetadata = new Object(); 70 myMetadata.customProp = "This message is sent by @setDataFrame."; 71 ns.send("@setDataFrame", "onMetaData", myMetadata); 72 } 73 74 private function publishCamera():void { 75 cam = Camera.getCamera(); 76 mic = Microphone.getMicrophone(); 77 ns = new NetStream(nc); 78 ns.client = this; 79 ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 80 ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 81 ns.attachCamera(cam); 82 ns.attachAudio(mic); 83 ns.publish("myCamera", "record"); 84 sendMetadata(); 85 86 } 87 88 private function displayPublishingVideo():void { 89 vid = new Video(cam.width, cam.height); 90 vid.x = 10; 91 vid.y = 10; 92 vid.attachCamera(cam); 93 addChild(vid); 94 } 95 96 private function displayPlaybackVideo():void { 97 nsPlayer = new NetStream(nc); 98 nsPlayer.client = this; 99 nsPlayer.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 100 nsPlayer.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); 101 nsPlayer.play("myCamera", 1); 102 vidPlayer = new Video(cam.width, cam.height); 103 vidPlayer.x = cam.width + 100; 104 vidPlayer.y = 10; 105 vidPlayer.attachNetStream(nsPlayer); 106 addChild(vidPlayer); 107 } 108 109 private function setupUI():void { 110 outputWindow = new TextArea(); 111 outputWindow.move(250, 175); 112 outputWindow.width = 250; 113 outputWindow.height = 150; 114 115 outgoingLbl = new Label(); 116 incomingLbl = new Label(); 117 outgoingLbl.width = 150; 118 incomingLbl.width = 150; 119 outgoingLbl.text = "Publishing Stream"; 120 incomingLbl.text = "Playback Stream"; 121 outgoingLbl.move(30, 150); 122 incomingLbl.move(300, 150); 123 124 startPlaybackBtn = new Button(); 125 startPlaybackBtn.width = 150; 126 startPlaybackBtn.move(250, 345) 127 startPlaybackBtn.label = "View Live Event"; 128 startPlaybackBtn.addEventListener(MouseEvent.CLICK, startHandler); 129 130 clearBtn = new Button(); 131 clearBtn.width = 100; 132 clearBtn.move(135,345); 133 clearBtn.label = "Clear Metadata"; 134 clearBtn.addEventListener(MouseEvent.CLICK, clearHandler); 135 136 addChild(clearBtn); 137 addChild(outgoingLbl); 138 addChild(incomingLbl); 139 addChild(startPlaybackBtn); 140 addChild(outputWindow); 141 } 142 143 public function onMetaData(info:Object):void { 144 var key:String; 145 for (key in info){ 146 outputWindow.appendText(key + ": " + info[key] + " "); 147 } 148 } 149 public function onTimeCoordInfo(info:Object):void{ 150 for (var key:String in info){ 151 trace(key + ": " + info[key] + " "); 152 } 153 } 154 } 155 }