actionscript p2p chat - 如何传输视频

时间:2013-04-26 22:45:20

标签: video actionscript chat p2p

再次是我!我尝试使用actionscript开发我的聊天!我取得了一些成功!这是我的代码:

动作:

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

             

        private const SERVER_ADDRESS:String = "rtmfp://p2p.rtmfp.net/850ecff774ce98e0c0c765f3-29ce3314ba14/";
        private var nc:NetConnection;
        private var ss:NetStream; 
        private var rs:NetStream;
        private var myPeerID:String; 
        private var recvStreams:Object = new Object();
        private var sendStreams:Object = new Object();
        private var recvVideos:ArrayCollection;
        private var myVideo:Video;
        private var microphone:Microphone;
        //private var uics:ArrayCollection;

        private function init():void {
            Security.allowDomain("*");
            myVideo=getVideoBox();
            ExternalInterface.addCallback("initConnection",initConnection);
            ExternalInterface.addCallback("sendSomeData",sendSomeData);
            ExternalInterface.addCallback("initReceiveStream",initReceiveStream);
            ExternalInterface.call("p2pStartInit");
        }

        private function getVideoBox(width:int=320,height:int=240,x:int=0,y:int=0):Video{
            var video:Video = new Video(width,height);
            video.x = x == 0 ? -width/2 : x;
            video.y = y == 0 ? -height/2 : y;
            var uic:UIComponent = new UIComponent();
            uic.addChild(video);
            addElement(uic);
            return video;
        }

        private function initConnection():void{
            nc = new NetConnection();
            nc.maxPeerConnections = 1000;
            nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
            nc.connect(SERVER_ADDRESS);
        }

        private function setVolume(s:NetStream,level:Number=0.8):NetStream{
            s.soundTransform = new SoundTransform(level);
            return s;
        }

        private function setMicrophoneLevel(level:Number = 0.8):void{
            microphone.soundTransform = new SoundTransform(level);
        }

        private function setupCamera(width:int=320,height:int=240):void{
            var cam:Camera = Camera.getCamera();
            cam.setMode(width, height, 25);
            cam.addEventListener(StatusEvent.STATUS, camHandler);
            ss.attachCamera(cam);
            myVideo.attachCamera(cam);                  
            microphone = Microphone.getMicrophone();
            setMicrophoneLevel();
            ss.attachAudio(microphone);
        }

        private function camHandler(e:StatusEvent):void{
            if(e.level == "status" && e.code == "Camera.Muted")ExternalInterface.call("p2pError", "allowcamaccess");
        }

        public function ncStatus(event:NetStatusEvent):void{
            ExternalInterface.call("p2pError",event.info.code);
            if(event.info.code == "NetConnection.Connect.Success"){
                myPeerID = nc.nearID;
                initSendStream();
                ExternalInterface.call("alert",nc.nearID);
            }
        }

        private function initSendStream():void{
            ss = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
            setupCamera();
            ss.publish('chat');
            var client:Object = new Object();
            client.onPeerConnect = function(subscriber:NetStream):Boolean {
                if(!sendStreams[subscriber.farID])sendStreams[subscriber.farID] = subscriber;
                if(!recvStreams[subscriber.farID])initReceiveStream(subscriber.farID);
                if(!recvVideos[subscriber.farID]){
                    try{
                        recvVideos[subscriber.farID] = getVideoBox();//truoble somewhere here
                        recvVideos[subscriber.farID].attachNetStream(subscriber);//truoble somewhere here
                    }
                    catch(e:Error){ExternalInterface.call("tester", e); }
                }
                return true;
            }
            ss.client = client;
        }

        private function initReceiveStream(peerID:String):void{
            if(peerID){
                rs = new NetStream(nc,peerID);
                rs.play('chat');
                var client:Object = new Object();
                client.receiveSomeData = receiveSomeData;
                rs.client = client;
                var peer:Object = new Object();
                peer.stream = rs;
                recvStreams[peerID] = peer;
            }
        }

        private function sendSomeData(str:String,farIds:String):void{
            if(str!=null&&str!=""){
                str = str.replace(/(^[\r\n\t\s]+)|([\r\n\t\s]$)/g,"");
                farIds = farIds == null ? "" : farIds.replace(/[^a-z0-9;]/gi,"");
                if(farIds!=""){
                    var farId:Array = farIds.split(";");
                    for(var i:int;i<farId.length;i++){
                        if(farId[i]&&sendStreams[farId[i]]){
                            sendStreams[farId[i]].send('receiveSomeData', str, myPeerID);
                        }
                    }
                }
                else{
                    for(var id:String in sendStreams){
                        sendStreams[id].send('receiveSomeData', str, myPeerID);
                    }
                }
            }
        }

        private function receiveSomeData(str:String, farId:String):void{
            ExternalInterface.call("receiveSomeData", str, farId);
        }
    ]]>
</mx:Script>

的javascript:

    var name='myflash';

function getP2p(){
    if(navigator.appName.indexOf("Microsoft")!=-1)return window[name];
    else return document[name];
}

function p2pStartInit(){
    try{getP2p().initConnection()}
    catch(e){p2pError('flasherror')}
}

function initReceiveStream(p2pId){
    try{getP2p().initReceiveStream(p2pId)}
    catch(e){p2pError(e)}
}

function sendSomeData(str,ids){
    try{getP2p().sendSomeData(str,ids)}
    catch(e){p2pError(e)}
}

function p2pError(err){
    alert(err)
}

function receiveSomeData(str,id){
    alert('Received:'+str+'/'+id)
}

function tester(p,hf){
    var err=getProps(p,hf)
    alert(err.substr(0,err.length-1))
}
function getProps(obj,hf,n,t){
    if(!n)n='\n';
    if(!t)t=''
    if(t.match(/&nbsp;/))t+='&nbsp;&nbsp;&nbsp;'
    else t+='\t'
    //if(!hf)hf=1
    var s='';
    for(var i in obj){
        if(hf&&typeof obj[i]=='object')s+=t+i+':'+getProps(obj[i],hf,'',t)+n
        else s+=t+i+':'+obj[i]+','+n;
    }
    s='{'+n+s+t.substr(0,t.length-1)+'},';
    return s;
}
function getRandom(min,max){
    var r=Math.random();
    if(r==0)r=1;
    return Math.floor(r*(max-min)+min)
}

如果我在initSendStream函数中删除这些部分,一切正常:

recvVideos[subscriber.farID] = getVideoBox();
recvVideos[subscriber.farID].attachNetStream(subscriber);

我可以向任何我想要的同伴发送消息,我可以从我的网络摄像头看到视频。但是当我向任何需要发送视频的人发送消息时,我的问题就开始了。

如果我使用这两行代码编译应用程序,一切正常,直到我尝试连接到另一个对等方。这里出现错误#1009,视频不会在对等体之间传输。我无法理解我做错了什么。

还有另一个问题,可能是解决方案可能隐藏在这里。它一直出现。我在两个浏览器中测试这个聊天 - mozilla和opera。如果首先在Opera中加载应用程序的页面,那么我可以在Opera中看到来自我的摄像头的视频,而我无法在mozilla中看到它。如果首先加载mozilla页面,那么我可以在mozilla中看到视频,我无法在Opera中看到视频。为什么会这样?它真的让我困惑!

所以大家请告诉我的代码有什么问题!

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

完成!错误发生在initReceiveStream函数中。没有rs.receiveVideo(true);当我发现recvVideos正确的方式是私人var recvVideos:对象=新对象();