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,
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).- 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.
- 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.
- 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. - 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.
- 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.
- 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.
Instance methods inherited from BaseAVManager
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.
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.
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.
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.
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:
- NoAVConnectionException if the connection to Red5 is not available.
- InvalidChatSessionIdException if the passed session id is unknown or the chat session is not in a ChatSession.STATUS_PENDING status.
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.
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;
Overrides:
getChatSession
Retrieve a ChatSession object instance.
Parameters:
sessionId:
the id of the chat session to be retrieved (see ChatSession.id property).
Returns:
- The ChatSession object.
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.
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:
- InvalidChatSessionIdException if the passed session id is unknown or the chat session is not in a ChatSession.STATUS_PENDING status.
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.
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 :
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:
- NoAVConnectionException if the connection to Red5 is not available.
- InvalidParamsException if both enableCamera and enableMicrophone parameters are set to
false
. - BadRequestException if the wrong request type is passed when calling this method.
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!"); }
See also:
- ChatSession
- RedBoxChatEvent.CHAT_REQUEST
- RedBoxChatEvent.RECIPIENT_MISSING
- RedBoxChatEvent.DUPLICATE_REQUEST
- NoAVConnectionException
- InvalidParamsException
- BadRequestException
- flash.media.Camera
- flash.media.Microphone
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.
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); }
See also: