AVChatManager

Kind of class:public class
Package:com.smartfoxserver.v2.redbox
Inherits from:BaseAVManager < EventDispatcher
Version:1.0.0 for SmartFoxServer 2X
Author:The gotoAndPlay() Team
http://www.smartfoxserver.com
Classpath:com.smartfoxserver.v2.redbox.AVChatManager
File last modified:Thursday, 05 May 2011, 10:05:50
SmartFoxServer 2X RedBox Audio/Video Chat Manager.
This class is responsible for audio/video chat implementation by means of the connection to the Red5 server.
The AVChatManager handles the chat workflow (send request, accept or refuse it, establish or stop connection, etc.) and the live streaming to/from the Red5 server.

NOTE: in the provided examples, avChatMan always indicates an AVChatManager instance.
Usage:
  • The AVChatManager class is useful to create one-on-one audio/video chats. Three chat modes are supported: receive-only, send-only or send-and-receive.
    In send-only and receive-only modes, the a/v chat requests are distinct: a user can watch a friend without sending his own stream, and vice versa (just like in Windows Live Messenger, when you click the webcam icons near the users' pictures).
    In send-and-receive mode, a single request includes both sending the user's own stream and receiving the friend's stream.
    The following workflow is suggested (to make it simpler the send-and-receive mode is considered; for the other modes the flow is the same, but two separate requests are needed).
    1. The current user click on an interface element to send the a/v chat request to a friend; usually a button in a private chat window is used.
    2. An invitation is sent to the recipient user by means of the sendChatRequest method; of course the recipient must be connected to SmartFoxServer 2X (but not necessarily in the same Room of the requester), otherwise the RedBoxChatEvent.RECIPIENT_MISSING event is fired on the requester's client.
    3. On the recipient's client the RedBoxChatEvent.CHAT_REQUEST event is fired: an invitation to send and receive the a/v stream is displayed, together with the interface elements to accept or decline it.
      If the recipient refuses the invitation, the refuseChatRequest method is called, which causes the RedBoxChatEvent.CHAT_REFUSED event to be fired on the requester's client and the interface to be adjusted accordingly.
    4. The recipient accepts the invitation: the acceptChatRequest method is called, which causes the RedBoxChatEvent.CHAT_STARTED event to be fired on both the requester's and the recipient's clients.
    5. On both the requester's and the recipient's clients two Video objects are added to the stage to display the incoming stream and the user's own camera output.
    6. One of the two users involved in the chat clicks on an interface element to stop the a/v chat: the stopChat method is called and the RedBoxChatEvent.CHAT_STOPPED event is fired on the connected user's client, so that the Video objects can be removed from stage.
Events broadcasted to listeners:
  • RedBoxChatEvent with type: CHAT_REQUEST
    • Dispatched when an a/v chat request is received.
  • RedBoxChatEvent with type: RECIPIENT_MISSING
    • Dispatched when a chat request is sent, but the recipient is not available.
  • RedBoxChatEvent with type: DUPLICATE_REQUEST
    • Dispatched when a chat request is sent, but a mutual request has already been sent by the recipient.
  • RedBoxChatEvent with type: CHAT_REFUSED
    • Dispatched when an a/v chat request has been refused by the recipient.
  • RedBoxChatEvent with type: CHAT_STARTED
    • Dispatched when an a/v chat session is started, after the recipient accepted the requester's invitation.
  • RedBoxChatEvent with type: CHAT_STOPPED
    • Dispatched when an a/v chat session is stopped.

Summary


Constructor
  • AVChatManager (sfs:SmartFox, red5Ip:String, useRTMPT:Boolean = false, debug:Boolean = false)
    • AVChatManager contructor.
Constants
  • REQ_TYPE_SEND : String
    • Audio/video chat request type: "stream from requester to recipient".
  • REQ_TYPE_RECEIVE : String
    • Audio/video chat request type: "stream from recipient to requester".
  • REQ_TYPE_SEND_RECEIVE : String
    • Audio/video chat request type: "bi-directional stream".
Instance properties
Instance properties inherited from BaseAVManager
Instance methods
  • destroy : void
    • Destroy the AVChatManager instance.
  • getChatSession (sessionId:String) : ChatSession
    • Retrieve a ChatSession object instance.
  • sendChatRequest (type:String, recipientId:int, enableCamera:Boolean = true, enableMicrophone:Boolean = true) : ChatSession
    • Send a request to start an audio/video chat.
  • refuseChatRequest (sessionId:String) : void
    • Refuse an incoming request to start an audio/video chat.
  • acceptChatRequest (sessionId:String) : void
    • Accept an incoming request to start an audio/video chat.
  • stopChat (sessionId:String) : void
    • Stop an a/v chat session currently in progress.

Constructor

AVChatManager

public function AVChatManager (
sfs:SmartFox, red5Ip:String, useRTMPT:Boolean = false, debug:Boolean = false)

AVChatManager contructor.
Parameters:
sfs :
the SmartFox API main class instance.
red5Ip :
the Red5 server IP address (include the port number if the default one is not used).
useRTMPT:
connect to Red5 server using the HTTP-tunnelled RTMP protocol (optional, default is false); Red5 must be configured accordingly.
debug :
turn on the debug messages (optional, default is false).
Example:
  • The following example shows how to instantiate the AVChatManager class.
    var smartFox:SmartFox = new SmartFox();
    var red5IpAddress:String = "127.0.0.1";
    
    var avChatMan:AVChatManager = new AVChatManager(smartFox, red5IpAddress);

Constants

REQ_TYPE_RECEIVE

public static const REQ_TYPE_RECEIVE:String = "receive"
(read)

Audio/video chat request type: "stream from recipient to requester".
The requester would like to receive the recipient's live a/v stream.
See also:

REQ_TYPE_SEND

public static const REQ_TYPE_SEND:String = "send"
(read)

Audio/video chat request type: "stream from requester to recipient".
The requester would like to send his own live a/v stream to the recipient.
See also:

REQ_TYPE_SEND_RECEIVE

public static const REQ_TYPE_SEND_RECEIVE:String = "send&rcv"
(read)

Audio/video chat request type: "bi-directional stream".
The requester would like to establish a mutual live a/v connection.
See also:

Instance methods

acceptChatRequest

public function acceptChatRequest (
sessionId:String) : void

Accept an incoming request to start an audio/video chat.
Calling this method causes the RedBoxChatEvent.CHAT_STARTED event to be fired on both the requester and the recipient clients.
Parameters:
sessionId:
the id of the chat session request to be accepted (see ChatSession.id property).
Events broadcasted to listeners:
  • RedBoxChatEvent with type: CHAT_STARTED
    • Dispatched when an a/v chat session is started, after the recipient accepted the requester's invitation.
Throws:
Example:
  • The following example shows how to accept a chat request.
    bt_accept.addEventListener(MouseEvent.CLICK, onAcceptBtClick);
    
    // After receiving a chat request, its session id is saved and an "accept" button activated...
    
    function onAcceptBtClick(evt:MouseEvent):void
    {
        avChatMan.acceptChatRequest(chatSessionId);
    }

destroy

override public function destroy (
) : void

Destroy the AVChatManager instance.
Calling this method causes the interruption of all chat sessions currently in progress (if any) and the disconnection from Red5.
This method should always be called before deleting the AVChatManager instance.
Example:
  • The following example shows how to destroy the AVChatManager instance.
    avChatMan.destroy();
    avChatMan = null;

getChatSession

public function getChatSession (
sessionId:String) : ChatSession

Retrieve a ChatSession object instance.
Parameters:
sessionId:
the id of the chat session to be retrieved (see ChatSession.id property).
Returns:
Example:
  • The following example shows how to get a chat session.
    var chatData:ChatSession = avChatMan.getChatSession(sessionId);
    
    if (chatData != null)
        trace (chatData.toString());
See also:

refuseChatRequest

public function refuseChatRequest (
sessionId:String) : void

Refuse an incoming request to start an audio/video chat.
Calling this method causes the RedBoxChatEvent.CHAT_REFUSED event to be fired on the requester's client.
Parameters:
sessionId:
the id of the chat session request to be refused (see ChatSession.id property).
Events broadcasted to listeners:
  • RedBoxChatEvent with type: CHAT_REFUSED
    • Dispatched when an a/v chat request has been refused by the recipient.
Throws:
Example:
  • The following example shows how to refuse a chat request.
    bt_decline.addEventListener(MouseEvent.CLICK, onDeclineBtClick);
    
    // After receiving a chat request, its session id is saved and a "decline" button activated...
    
    function onDeclineBtClick(evt:MouseEvent):void
    {
        avChatMan.refuseChatRequest(chatSessionId);
    }

sendChatRequest

public function sendChatRequest (
type:String, recipientId:int, enableCamera:Boolean = true, enableMicrophone:Boolean = true) : ChatSession

Send a request to start an audio/video chat.
When this method is called, a "chat session" is created (see the ChatSession class description) and an invitation to start the a/v chat is sent to the selected user id, causing the RedBoxChatEvent.CHAT_REQUEST event to be fired on the recipient's client.
If the recipient is not available (for example he disconnects while the request is being sent), the RedBoxChatEvent.RECIPIENT_MISSING event is fired in response.
If the mutual request has already been sent by the recipient, the RedBoxChatEvent.DUPLICATE_REQUEST is fired in response.
Audio and video recording mode/quality should be set before calling this method. In order to alter these settings, please refer to the flash.media.Microphone and flash.media.Camera classes documentation.
Parameters:
type :
the request type; valid values are: REQ_TYPE_SEND, REQ_TYPE_RECEIVE and REQ_TYPE_SEND_RECEIVE.
recipientId :
the SmartFoxServer user id of the recipient.
enableCamera :
enable video live streaming; default value is true.
enableMicrophone:
enable audio live streaming; default value is true.
Returns:
  • The ChatSession object created, or null if the same request type has already been sent to the same recipient (and it's still pending or already accepted).
Events broadcasted to listeners:
  • RedBoxChatEvent with type: CHAT_REQUEST
    • Dispatched when an a/v chat request is received.
  • RedBoxChatEvent with type: RECIPIENT_MISSING
    • Dispatched when a chat request is sent, but the recipient is not available.
  • RedBoxChatEvent with type: DUPLICATE_REQUEST
    • Dispatched when a chat request is sent, but a mutual request has already been sent by the recipient.
Throws:
Example:
  • The following example shows how to send a request to start an a/v chat.
    avChatMan.addEventListener(RedBoxChatEvent.RECIPIENT_MISSING, onRecipientMissing);
    avChatMan.addEventListener(RedBoxChatEvent.DUPLICATE_REQUEST, onDuplicateRequest);
    
    avChatMan.sendChatRequest(AVChatManager.REQ_TYPE_SEND_RECEIVE, buddyId, true, true);
    
    function onRecipientMissing(evt:RedBoxChatEvent):void
    {
        trace ("Request '" + evt.params.chatSession.id + "' error: the recipient is not available!");
    }
    
    function onDuplicateRequest(evt:RedBoxChatEvent):void
    {
        trace ("Request '" + evt.params.chatSession.id + "' error: a mutual request has already been sent by the recipient!");
    }

stopChat

public function stopChat (
sessionId:String) : void

Stop an a/v chat session currently in progress.
Calling this method causes the RedBoxChatEvent.CHAT_STOPPED event to be fired on the connected user (mate) clients.
Parameters:
sessionId:
the id of the chat session to be stopped (see ChatSession.id property).
Events broadcasted to listeners:
  • RedBoxChatEvent with type: CHAT_STOPPED
    • Dispatched when an a/v chat session is stopped.
Example:
  • The following example shows how to stop a chat session.
    bt_stop.addEventListener(MouseEvent.CLICK, onStopBtClick);
    
    // After the chat session started, a "stop" button is activated...
    
    function onStopBtClick(evt:MouseEvent):void
    {
        avChatMan.stopChat(chatSessionId);
    }