Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This example is a two-directional video chat using a client Flash application executed as a simple swf file.
The example demonstrates operation of a Flash video chat that allows two-directional video communication to the same example for iOS, Android or Web SDK.

The screenshot displays operation of the Flash video chat.

...

1. During initialization the application gets access to the camera and the microphone. line 65

Code Block
languagejsactionscript3
themeRDark
cam = Camera.getCamera();
localDisplay.attachCamera(cam);
mic = Microphone.getEnhancedMicrophone();
remoteDisplayHolder.addChild(remoteDisplay);

...

If connection to the server is successful, the joinRoom() method is invoked to join to the room.

Code Block
languagejsactionscript3
themeRDark
				session = new Session(url, username);
				session.on(SessionStatus.FAILED, function():void{
 					setStatus(sessionStatus, SessionStatus.FAILED);
					onLeft();
				}).on(SessionStatus.DISCONNECTED, function():void {
 					setStatus(sessionStatus, SessionStatus.DISCONNECTED);
					onLeft();
				}).on(SessionStatus.ESTABLISHED, function():void {
 					setStatus(sessionStatus, SessionStatus.ESTABLISHED);
					joinRoom();
				});
				session.connect();


3. While the participant joins the room, the reactions to various events in the room are added. line 150

JOINED - a new participant has joined the room
LEFT - a participant has left the room
PUBLISHED - a participant has published a video stream
FAILED - an error occurred wile communicating with the room
MESSAGE - an inbound message from a participant in the room

Code Block
languagejsactionscript3
themeRDark
				session.join(this.roomName).on(RoomStatus.STATE, function(room:Room):void{
 					var participants:Array = room.getParticipants();
					setInviteAddress(room);
					if (participants.length > 0) {
 						var chatState:String = "participants: ";
 						for (var i:Number = 0; i < participants.length; i++) {
 							installParticipant(participants[i]);
 							chatState += participants[i].getName();
 							if (i != participants.length - 1) {
 								chatState += ",";
 							}
 						}
 						addMessage("chat", chatState);
					} else {
 						addMessage("chat", " room is empty");
					}
					publishLocalMedia(room);
					onJoined(room);
				}).on(RoomStatus.JOINED, function(participant:Participant):void{
 					installParticipant(participant);
					addMessage(participant.getName(), "joined");
				}).on(RoomStatus.LEFT, function(participant:Participant):void{
 					removeParticipant();
					addMessage(participant.getName(), "left");
				}).on(RoomStatus.PUBLISHED, function(participant:Participant):void{
 					playParticipantsStream(participant);
				}).on(RoomStatus.FAILED, function(room:Room, info:Object):void{
 					failedInfo.text = info.info;
					session.disconnect();
				}).on(RoomStatus.MESSAGE, function(message:Object):void{
 					addMessage(message.from.getName(), message.text);
				});


4. Publishing the video stream from the web camera to the WCS server. line 232

Code Block
languagejsactionscript3
themeRDark
private function publishLocalMedia(room:Room):void {
    var stream:NetStream = room.publish(mic, cam);
    stream.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent):void{
        Logger.info("handlePublishStreamStatus: "+event.info.code);
        switch (event.info.code) {
            case "NetStream.Publish.BadName":
                setStatus(streamStatus, "FAILED");
                onMediaStopped(room);
            break;
            case "NetStream.Unpublish.Success":
                setStatus(streamStatus, "UNPUBLISHED");
                onMediaStopped(room);
            break;
            case "NetStream.Publish.Start":
                setStatus(streamStatus, "PUBLISHING");
                onMediaPublished(stream);
            break;
        }
    });
}


5. Playing the stream of another participant. line 207

Code Block
languagejsactionscript3
themeRDark
privatefunctionplayParticipantsStreamprivate function playParticipantsStream(p:Participant):void
{
    var varstreamstream:NetStream = p.play();
    if (stream != null) {
        remoteDisplay.attachNetStream(stream);
        stream.addEventListener(NetStatusEvent.NET_STATUS, function(event:NetStatusEvent):void{
            Logger.info("handlePlayStreamStatus: "+event.info.code);
            switch (event.info.code) {
                case "NetStream.Video.DimensionChange":
 varres                   var res:Object = downScaleToFitSize(remoteDisplay.videoWidth, remoteDisplay.videoHeight, display.width, display.height);
                    remoteDisplay.width = res.w;
                    remoteDisplay.height = res.h;
                    remoteDisplayHolder.width = res.w;
                    remoteDisplayHolder.height = res.h;
                break;
                case "NetStream.Play.UnpublishNotify":
                case "NetStream.Play.Stop":
                    remoteDisplay.clear();
                break;
            }
        });
    }
}