0 Replies Latest reply on Jun 4, 2010 3:55 PM by Amosl

    Can someone please help me with my code

    Amosl Level 1

      Hi.  A while back I tried hacking together a bit of code to get a video chat working.  I sort of had things working but connections constantly fail and things were just so inconsistent.  Every once in a while things work.  Can anyone take a look at my code and help me out with the bugs.  Basically the connection is so inconsistent and I'm not sure where its failing.

       

      This code was pretty much hacked together from other code I found online:

       

       

       

       

      <?xml version="1.0" encoding="utf-8"?>

       


      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          applicationComplete="{init();}"
      >
         
         
          <mx:Script>
              <![CDATA[
                  import mx.core.UIComponent;
                  import mx.controls.Alert;
                  private var nc:NetConnection;
                  private var nc2:NetConnection;
                  private var rtmfpServer:String = 'rtmfp://stratus.adobe.com/enter-stratus-server-id-here';
                  private var sendNS:NetStream;
                  private var receiveNS:NetStream;
                  private var neerPeerID:String;
                 
                  private var cam:Camera;
                  private var mic:Microphone;
             
                  private function init():void {

       

       

       

                      initCamera();
                      initNetConnection();
                  }
             
                  private function initCamera():void {
                      if (Camera.names.length > 0) {
                          cam = Camera.getCamera();
                          cam.setMode(300, 300, 45);
                          cam.setQuality(0, 150);

       

                         
                         
                          my_video_display.attachCamera(cam);
                      }
                     
                      if (Microphone.names.length > 0) {
                 
                          mic = Microphone.getMicrophone();
                          mic.codec = SoundCodec.SPEEX;
                          mic.rate =44;
                         
                      }
                  }
                 
                  private function initNetConnection():void {
                      nc = new NetConnection();
                      nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
                      nc.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                      addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler)
                      nc.connect(rtmfpServer);
                      //trace(nc)
                     
                  }
                 
                  private function ioErrorHandler(event:IOErrorEvent):void {
                      trace("ioErrorHandler: " + event);
                  }

       

                 
                  private function netStatusEvent(event:NetStatusEvent):void {
                      trace('NetConnection 1 (Outstream) Status Event: ' + event.info.code);
                      if (event.info.code == 'NetConnection.Connect.Success') {
                          neerPeerID = nc.nearID;
                          farPeerId_text.text = neerPeerID;
                          initSendNetStream();
                         
                          add_contact_container.visible = true;
                         
                         
                      }
                  }
                 
                 
                  private function netStatusEvent2(event:NetStatusEvent):void {
                      trace('NetConnection 2 (Instream) Status Event:: ' + event.info.code);
                         
                     
                  }
                 
                 
                 
                  private function securityErrorHandler(event:SecurityErrorEvent):void {
                      Alert.show("securityErrorHandler: " + event);
                  }

       

                 
                  private function initSendNetStream():void {
                      sendNS = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
                      sendNS.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
                     
                      var clientObject:Object = new Object();
                      clientObject.onPeerConnect = function(ns:NetStream):Boolean {return true;}
                     
                      sendNS.client = clientObject;
                      sendNS.attachCamera(cam);
                      sendNS.attachAudio(mic);
                      sendNS.publish('video');
                      //trace(sendNS.info, "Sendns");
                  }
                 
                  private function addContact():void {
                      nc2 = new NetConnection();
                      nc2.addEventListener(NetStatusEvent.NET_STATUS, function (event:NetStatusEvent):void {
                      trace(event.info.code);
                          var receiveNS:NetStream = new NetStream(nc2, contact_peer_id_text.text);
                          receiveNS.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent2);
                          receiveNS.play('video');
                         
                         
                         
                          var video:Video = new Video();
                          video.attachNetStream(receiveNS);
                         
                          video.width=300;
                          video.height=300;
                 
                     
                          my_video_display2.addChild(video);
                         
                          //var uic:UIComponent = new UIComponent();
                          //uic.width = 800;
                          //uic.height = 500;
                          //uic.addChild(video);
                          //video_stack.addChild(uic);
                         
                     
                         
                          contact_peer_id_text.text = '';
                         
                      //trace(receiveNS.info)
                      });
                      nc2.connect(rtmfpServer);
                     
                  }
                 
                 
                  private function getinfo():void {
                 
                      trace("nc1 connected? "+ nc.connected);
                      trace(sendNS.close());
                  }
                 
                 
                  private function getinfo2():void {
                     
                      trace("nc2 connected? "+ nc2.connected);
                      trace(initSendNetStream());
                     
                      //trace(nc.connected);
                     
                     
                      //Alert.show(nc.farID);
                     
                  }
              ]]>
          </mx:Script>

       

         
         
          <mx:HBox id="video_stack" top="10" left="10">
              <mx:VBox>
                  <mx:VideoDisplay
                                   id="my_video_display" 
                                   width="300"
                                   height="300"/>
                 
                  <mx:VideoDisplay 
                                   id="my_video_display2"
                                   width="300"
                                   height="300"/>
                  <mx:HBox>
                      <mx:TextInput width="320" id="farPeerId_text" text="Your Peer ID is loading..."/>
                  </mx:HBox>
                  <mx:HBox id="add_contact_container" visible="true">
                      <mx:TextInput id="contact_peer_id_text" width="200"/>
                      <mx:Button label="Add contact" click="{addContact();}"/>   
                  </mx:HBox>
              </mx:VBox>
          </mx:HBox>
          <mx:Button label="Stop Sending" click="{getinfo();}"/>
          <mx:Button label="Start Sending" click="{getinfo2();}"/>
      </mx:Application>