Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [92]

By Root 2588 0
onStatus);

}

}

Once the NetStream is connected, add a reference to the camera and the microphone and attach them to the stream. A Video object displays the camera feed. Finally, call the publish method and pass the name of your choice for the video session:

function onStatus(event:NetStatusEvent):void {

if (event.info.code == "NetStream.Connect.Success") {

var camera:Camera = Camera.getCamera();

var video:Video = new Video();

video.attachCamera(camera);

addChild(video);

outStream.attachAudio(Microphone.getMicrophone());

outStream.attachCamera(camera);

outStream.publish("remote video");

}

}

Recipients


The code for the peers receiving the video is similar, except for the few changes described next.

The incoming NetStream, used for the peers receiving the stream, must be the same GroupSpecifier as the publisher’s stream. The same stream cannot be used for sending and receiving:

var inStream:NetStream = new NetStream(connection,

groupSpec.groupspecWithAuthorizations());

inStream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

The recipient needs a Video object but no reference to the microphone and the camera. The play method is used to stream the video in:

var video:Video = new Video();

addChild(video);

inStream.play("remote video");

Sending and receiving data


Along with streams, NetStream can be used to send data. It is only an option for the publisher:

var object:Object = new Object();

object.type = "chat";

object.message = "hello";

outStream.send("onReceiveData", object);

To receive data, the incoming stream must assign a NetStream.client for callbacks. Note that the onReceiveData function matches the first parameter passed in the publisher send call:

inStream.client = this;

function onReceiveData(object:Object):void {

trace(object.type, object.message); // chat, hello

}

Closing a stream


Do not forget to remove the stream and its listener after it closes:

function onStatus(event:NetStatusEvent):void {

switch(event.info.code) {

case "NetStream.Connect.Closed" :

case "NetStream.Connect.Failed" :

onDisconnect();

break;

}

}

function onDisconnect():void {

stream.removeEventListener(NetStatusEvent.NET_STATUS, onStatus);

stream = null;

}

group.peerToPeerDisabled = false;

group.objectReplicationEnabled = true;

WARNING

Streaming does not pause when the application goes to the background. You need to monitor its pause and play functionality. This is important to avoid distracting audio playing over other applications and to save memory and battery life. Review Event.DEACTIVATE and Event.ACTIVATE, covered in Chapter 6.

End-to-End Stream


Another approach is for the publisher to send a separate stream to each receiver. This limits the number of users, but is the most efficient transmission with the lowest latency. No GroupSpecifier is needed for this mode of communication. In fact, this is no longer a group, but a one-to-one transfer or unidirectional NetStream channel.

Sending a peer-assisted stream


Set the connection parameter to NetStream.DIRECT_CONNECTIONS; the stream now has its bufferTime property set to 0 for maximum speed:

var outStream:NetStream =

new NetStream(connection, NetStream.DIRECT_CONNECTIONS);

outStream.bufferTime = 0;

outStream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);

var video:Video = new Video();

var camera:Camera = Camera.getCamera();

video.attachCamera(camera);

addChild(video);

outStream.attachAudio(Microphone.getMicrophone());

outStream.attachCamera(camera);

outStream.publish("privateVideo");

When first connected, every peer is assigned a unique 256-bit peerID. Cirrus uses it to match it to your IP address and port number when other peers want to communicate with you, as in this example. nearID represents you:

var myPeerID:String

function onStatus(event:NetStatusEvent):void {

if (event.info.code == "NetConnection.Connect.Success) {

myPeerID = connection.nearID;

trace(myPeerID);

// 02024ab55a7284ad9d9d4586dd2dc8d2fa1b207e53118d93a34abc946836fa4

}

}

The receivers need the peerID

Return Main Page Previous Page Next Page

®Online Book Reader