actionscript p2p 聊天 - 如何传输视频

Posted

技术标签:

【中文标题】actionscript p2p 聊天 - 如何传输视频【英文标题】:actionscript p2p chat - how to transmit video 【发布时间】:2013-04-21 05:02:03 【问题描述】:

又是我!我尝试使用 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()
    trygetP2p().initConnection()
    catch(e)p2pError('flasherror')


function initReceiveStream(p2pId)
    trygetP2p().initReceiveStream(p2pId)
    catch(e)p2pError(e)


function sendSomeData(str,ids)
    trygetP2p().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】:

完成!错误出现在 initReceiveStream 函数中。没有 rs.receiveVideo(true);当我删除 recvVideos 时,正确的方法是 private var recvVideos:Object = new Object();

【讨论】:

以上是关于actionscript p2p 聊天 - 如何传输视频的主要内容,如果未能解决你的问题,请参考以下文章

python 简单p2p聊天程序

android webrtc 多人网状P2P视频聊天

java基于P2P的聊天和文件传输实例

pyqt5实现简单的 p2p聊天

端游下载的时候现在很多都是采用HTTP传输,为什么不是P2P传输?

c#基于udp实现的p2p语音聊天工具