Share Coding

Tutorials, Problems, Stuffs …

Peer To Peer Live Video Streaming on Actionscript 3.0 With OpenRTMFP/Cumulus Server

To read this post, you need to finish H.264 Live Streaming first and know how to have a live streaming in Actionscript 3.0.
The target is to have a high quality(H.264) and high performance(Peer to peer) Web meeting.
This post only contain actionscript code at publisher and player. You must set up a OpenRTMFP/Cumulus Server.

Follow this post https://sharecoding.wordpress.com/2012/05/22/install-openrtmfpcumulus-to-centos-6-2/ to start a server and use this post to modify your H.264 Live Streaming project.

Web Meeting Publisher & Player

import flash.net.NetGroup;

 

var userid:String;
var groupname:String;

// Receive Flash Parameters from web page
userid = LoaderInfo(this.root.loaderInfo).parameters["userid"];
groupname = LoaderInfo(this.root.loaderInfo).parameters["groupname"];
if (userid==""||userid==null) {
	userid = "456";
}
if (groupname==""||groupname==null) {
	groupname = "swfever";
}

 

nc = new NetConnection(); 
nc.objectEncoding= ObjectEncoding.AMF3;
nc.maxPeerConnections=8; 
nc.addEventListener( NetStatusEvent.NET_STATUS, netStatus ); 
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); 
nc.connect("rtmfp://255.255.255.255:2105/");// Port must be setted at server

 

case "NetConnection.Connect.Success" : // In your netStatu listener
	var groupSpec:GroupSpecifier = new GroupSpecifier(groupname); 
	groupSpec.peerToPeerDisabled = false; 
	groupSpec.serverChannelEnabled=true; 
	groupSpec.ipMulticastMemberUpdatesEnabled=true; 
	groupSpec.multicastEnabled=true; 
	groupSpec.postingEnabled=true;
	var groupId:String = groupSpec.groupspecWithAuthorizations(); 
	netGroup = new NetGroup(nc,groupId); 
	netGroup.addEventListener(NetStatusEvent.NET_STATUS, netGroup_onNetStatus); 
	publish(groupId);
break;

 

// Suggestted to combine NetConnection/NetStream/NetGroup netstate listener
// NetStream Listener will receive all of the event of NetGroupStateEvent, because NetState event is his super class
function netGroup_onNetStatus(e:NetStatusEvent) {	
	trace(e.info.code);
	if (e.info.code == "NetGroup.Neighbor.Connect") {
		var msg = {nearid:nc.nearID};
		netGroup.post(msg);
	} else if (e.info.code == "NetGroup.Neighbor.Disconnect") {
		trace("NetGroup Neighbor Count = " + netGroup.neighborCount);
	} else if (e.info.code == "NetGroup.SendTo.Notify") {
		trace("From: "+e.info.from);
	} else if(e.info.code == "NetGroup.MulticastStream.PublishNotify"){
		trace(e.info.name);
	}
}

 

function publish(groupId:String):void {
	if (nc.connected) {
		nc.client = this;
		in_ns = new NetStream(nc,groupId);
		in_ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
		in_ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
		in_ns.inBufferSeek = false;
		if (cam != null) {
			in_ns.attachCamera(cam);
			h264Settings = new H264VideoStreamSettings();
			h264Settings.setProfileLevel(H264Profile.BASELINE, H264Level.LEVEL_3);
			h264Settings.setQuality(camBandWidth,camQualityPersentage);
			h264Settings.setKeyFrameInterval(camKeyFrameInterval);
			h264Settings.setMode(width, height, camFPS);
			in_ns.videoStreamSettings = h264Settings;

			panel.txtCamStatus.htmlText = "<font color='#33CC00'>ON</font>";
		}
		if (mic != null) {
			in_ns.attachAudio(mic);
			panel.txtMicStatus.htmlText = "<font color='#33CC00'>ON</font>";
		}

		soundControler.volume = 0;
		in_ns.soundTransform = soundControler;
		in_ns.publish("testStreamName", "live"); // Stream Name Must be same
	}
}

 
Client and Publisher use the same code.
You may add this code to client after NetGroup has been create

netGroup = new NetGroup(nc,groupId);
netGroup.receiveMode = NetGroupReceiveMode.NEAREST;

 
Concept
In a normal Live Streaming, you identify user by stream name which is created by your web page.
1. Now, you need to create a group name by web page.
2. Tell GroupSpecifier the group name.
3. Tell NetStream the groupSpec.groupspecWithAuthorizations().
4. Stream Name must the same in publisher and client.
5. Client has the same stream name but different group could not see others.

Advertisements

3 responses to “Peer To Peer Live Video Streaming on Actionscript 3.0 With OpenRTMFP/Cumulus Server

  1. Graig 2013/07/03 at 5:42 am

    Wenn man mit dem wohnmobil reise unterwegs schon Probleme mit der Khlanlage beim
    Fahrzeug, lsst sich eine Fahrt in die nchstgelegene Werkstatt manchmal
    nicht vermeiden.

  2. ibsenleo 2014/02/16 at 10:41 am

    hi! i used your code and works ok! is publishing, but now i dont know how to recieve the streamings published in that group, and attach them to vid. i mean, how to capture

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: