P2P 视频文字聊天

package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.GroupSpecifier;
import flash.net.NetStream;
import flash.net.NetGroup;

import flash.media.Video;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.SoundCodec;
import flash.text.TextField;

[SWF(backgroundColor = 0x666666,width = 1003,height = 527)]

public class P2pBroad extends Sprite
{
private var server:String = "rtmfp://p2p.rtmfp.net/";
private var key:String = "9bb73c685f27383f07976dbb-60df5c2574da/vini123.com";
//private var key:String = "9bb73c685f27383f07976dbb-60df5c2574da";
                         

private var myNc;
private var myNs;
private var myGroup;
private var myName;
private var myType;

private var joinedGroup:Boolean = false;


private var myCam;
private var myMic;
private var vid;

public function P2pBroad():void
{
Init();
}
private function Init()
{
//bt=new Label();
bt.width = 80;
bt.height = 21;
var id = int(Math.random() * 120);
myName = "crazy" + id;
if (id%2==0)
{
myType = "tea";
bt.label = "发布视频";
}
else
{
myType = "stu";
bt.label = "观看视频";
}
bt.visible = false;
bt.x = stage.stageWidth - bt.width - 10;
bt.y = stage.stageHeight - bt.height - 10;
bt.addEventListener(MouseEvent.CLICK,clickHandler);

sendBt.width=80;
sendBt.height=21;
sendBt.label="发送信息"
sendBt.visible=false;
sendBt.addEventListener(MouseEvent.CLICK,sendHandler);
linkServer();
}

        private function sendHandler(e:MouseEvent)
{
sendMessage();
}
private function clickHandler(e:MouseEvent)
{
if (myType=="tea")
{
publishStream()
}
else
{
playStream();
}
sendBt.visible=true;
e.currentTarget.visible=false;
}



private function setupGroup()
{
//var groupspec = new GroupSpecifier(myName);
/*
GroupSpecifier类可以构造一个传递到NetStream和NetGroup的构造函数的加密groupspec字符串。
一个groupspec指定一个RTMFP P2P群组,包括功能、限制和成员授权等信息。
构造函数中的"myGroup/groupOne"为群组标识,可以随意字符串代替
*/

var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/groupTwo");

//设置群组中post群发方式是否可用
groupspec.postingEnabled = true;

//指定是否能通过ip多播方式交换更新关于组成员的信息。
groupspec.ipMulticastMemberUpdatesEnabled = true;

groupspec.multicastEnabled = true;

groupspec.serverChannelEnabled = true;

/*
将相关的群组加入某个IP地址并侦听相关的端口
Multicast的IP地址需要从不低于224开始,端口需高于1024 ————“224.0.0.0:1024”
为保证唯一性,一般设置高一些比较可靠:)
*/

    groupspec.addIPMulticastAddress("225.225.0.1:30303");

myNs = new NetStream(myNc,groupspec.groupspecWithAuthorizations());
myNs.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
myNs.client=this;
           
//在已成功的连接nc上创建一个群组
//groupspecWithAuthorizations:返回一个包含授权的加密的groupspec字符串, 传给NetGroup的构造函数。
myGroup = new NetGroup(myNc,groupspec.groupspecWithAuthorizations());
//为群组增加事件侦听
myGroup.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
}

private function publishStream()
{
myCam = Camera.getCamera();
if (myCam!=null)
{
myCam.setMode(320,240,10);
myCam.setQuality(0,80);
vid=new Video();
vid.width = 320;
vid.height = 240;
vid.x=500;
    vid.y=150;
vid.attachCamera(myCam);
myNs.attachCamera(myCam);
addChild(vid);
}
else
{
trace("你视频都没有,还想搞直播");
}
myMic = Microphone.getMicrophone();
if (myMic!=null)
{
myMic.codec = SoundCodec.SPEEX;
myMic.setSilenceLevel(0);
myMic.framesPerPacket = 1;
myMic.gain = 10;
myMic.rate = 45;
myNs.attachAudio(myMic);
}
else
{
trace("你的电脑连麦克风都木有么!");
}
myNs.publish("heihei");
}

private function playStream()
{
vid=new Video();
vid.width=320;
vid.height=240;
vid.x=500;
vid.y=150;
vid.attachNetStream(myNs);
myNs.bufferTime=2;
myNs.play("heihei");
var txt:TextField=new TextField();
txt.text="aaaaaaaaaaaa";
addChild(txt);
            addChild(vid);
}

private function sendMessage():void
{
var obj=new Object();
obj.sender = myGroup.convertPeerIDToGroupAddress(myNc.nearID);
obj.user = myName;
if(inTxt.text.length<=1)
{
return;
}
obj.text = inTxt.text; //"你是第" + int(Math.random() * 100) + "个猪";
trace(inTxt.text);
myGroup.post(obj);
receiveMessage(obj);
inTxt.text="";
}

private function receiveMessage(str:Object):void
{
trace("aaa:"+str.user);
write(str.user + ": " + str.text);
}
private function write(str)
{
//trace(str);
txt.appendText(str+"\n");
}
private function linkServer()
{
myNc=new NetConnection();
//myNc.ObjectEncoding = ObjectEncoding.AMF3;
myNc.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
myNc.connect(server+key);
//myNc.connect("rtmfp://stratus.adobe.com/HawkPrerelease-4e4efa13755c/FMSer.cn");
//myNc.connect("rtmfp://219.130.135.238/");
}
private function statusHandler(e:NetStatusEvent)
{
trace(e.info.code);
var code = e.info.code;
switch (code)
{
case "NetConnection.Connect.Success" :
    trace("aa:"+myNc.nearID)
setupGroup();
break;
case "NetGroup.Connect.Success" :
joinedGroup = true;
break;
case "NetStream.Connect.Success" :
bt.visible = true;
break;
case "NetGroup.Posting.Notify" :
receiveMessage(e.info.message);
break;
default :
break;
}
}
}
}

你可能感兴趣的:(视频)