Default Calling
This section will provide information on how a complete calling workflow can be set up using CometChat. We've built the complete workflow to help your users make calls, receive calls as well as accept/reject calls.
Let us assume Alex to be the call initiator and Bob is the receiver.
- Alex initiates the call to Bob using the
initiateCall()
method. - Bob now has two choices:
- Accept the call from Alex using the
acceptCall()
method. - Reject the call from Alex using the
rejectCall("rejected")
method passing the status asrejected
.
- In the meantime, Alex has the option to cancel the call he initiated to Bob using the
rejectCall("cancelled")
method passing the status ascancelled
. - If Bob accepts the call from Alex, both Alex and Bob need to call the
startCall()
method. Alex in theonIncomingCallReceived()
method of theCallListener
and Bob in the success obtained from the call toacceptCall()
method and both will be connected to each other.
Initiate Call
The initiateCall()
method sends a call request to a user or a group.
- Swift
- Objective C
let receiverID = "UID"
let receiverType:CometChat.ReceiverType = .user OR.group
let callType: CometChat.CallType = .audio OR .video
let newCall = Call(receiverId: receiverID, callType: callType, receiverType: receiverType);
CometChat.initiateCall(call: newCall, onSuccess: { (ongoing_call) in
print("Call initiated successfully " + ongoing_call!.stringValue());
}) { (error) in
print("Call initialization failed with error: " + error!.errorDescription);
}
NSString *receiverID = @"UID";
Call *newCall = [[Call alloc]initWithReceiverId:receiverID callType:CallTypeVideo receiverType:ReceiverTypeUser];
[CometChat initiateCallWithCall:newCall onSuccess:^(Call * ongoing_call) {
NSLog(@"Call initiated successfully. %@",[ongoing_call stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"Call initialization failed with error: %@",[error ErrorDescription]);
}];
This method takes an object of the Call
class. The constructor for Call
class takes the following parameters:
Parameter | Description |
---|---|
receiverID | The UID or GUID of the recipient |
receiverType | The type of the receiver viz. - user - group |
callType | The type of the receiver viz. - audio - video |
Receive Calls
In order to receive all call
events, you must add protocol conformance CometChatCallDelegate
as Shown Below :
- Swift
- Objective C
extension ViewController: CometChatCallDelegate {
func onIncomingCallReceived(incomingCall: Call ? , error : CometChatException ? ) {
print(" Incoming call " + incomingCall!.stringValue());
}
func onOutgoingCallAccepted(acceptedCall: Call ? , error : CometChatException ? ) {
print("Outgoing call " + acceptedCall!.stringValue());
}
func onOutgoingCallRejected(rejectedCall: Call ? , error : CometChatException ? ) {
print("Rejected call " + rejectedCall!.stringValue());
}
func onIncomingCallCanceled(canceledCall: Call ? , error : CometChatException ? ) {
print("Cancelled call " + canceledCall!.stringValue());
}
}
@interface ViewController ()<CometChatCallDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[CometChat setCalldelegate:self];
}
- (void)onIncomingCallCanceledWithCanceledCall:(Call * _Nullable)canceledCall error:(CometChatException * _Nullable)error {
NSLog(@"Incoming call cancelled %@",[canceledCall stringValue]);
}
- (void)onIncomingCallReceivedWithIncomingCall:(Call * _Nullable)incomingCall error:(CometChatException * _Nullable)error {
NSLog(@"Incoming call %@",[incomingCall stringValue]);
}
- (void)onOutgoingCallAcceptedWithAcceptedCall:(Call * _Nullable)acceptedCall error:(CometChatException * _Nullable)error {
NSLog(@"Outgoing call accepted %@",[acceptedCall stringValue]);
}
- (void)onOutgoingCallRejectedWithRejectedCall:(Call * _Nullable)rejectedCall error:(CometChatException * _Nullable)error {
NSLog(@"Outgoing call rejected %@",[rejectedCall stringValue]);
}
@end
Do not forget to set your view controller as a CometChat delegate probably in viewDidLoad()
as CometChat.calldelegate = self
As mentioned in the Overview section, Once the call is initiated, there are three options that can be possible:
- The receiver of the call accepts the call.
- The receiver of the call rejects the call.
- The initiator of the call cancels the call.
Please find below how these three scenarios can be implemented:
Accept the incoming Call
Once you have received an incoming call from a user
or in any group
in onIncomingCallReceived(incomingCall:, error:)
, you need to accept the call using the acceptCall()
method.
- Swift
- Objective C
CometChat.acceptCall(sessionID: incomingCall!.sessionID, onSuccess: { (ongoing_call) in
print("Accepted Call. " + call!.stringValue());
}) { (error) in
print("Call accepting failed with error: " + error!.errorDescription);
}
[CometChat acceptCallWithSessionID:incomingCall.sessionID onSuccess:^(Call * ongoing_call) {
NSLog(@"Accepted call. %@",[ongoing_call stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"Call accepting failed with error: %@",[error ErrorDescription]);
}];
Reject the incoming Call
You can also choose to reject the incoming call once it is received using the rejectCall()
method.
- Swift
- Objective C
let status: CometChatConstants.callStatus = .rejected;
CometChat.rejectCall(sessionID: (incomingCall?.sessionID)!, status: status, onSuccess: { (rejeceted_call) in
print("Call rejected successfully. " + rejeceted_call!.stringValue());
}) { (error) in
print("Call rejection failed with error: " + error!.errorDescription);
}
[CometChat rejectCallWithSessionID:incomingCall.sessionID status:callStatusRejected onSuccess:^(Call * rejeceted_call) {
// Success
NSLog("Call rejected successfully %@ " , [rejeceted_call stringValue]);
} onError:^(CometChatException * error) {
// Error
NSLog("Call rejection failed with exception: %@" + [error ErrorDescription]);
}];
status | Reason for rejection of the call |
sessionID | The unique session ID available in the Call object |
Here the status needs to be set as CometChatConstants.callStatus.rejected
as the call is being rejected by the receiver of the call.
Cancel the Outgoing Call
In the case where the initiator wishes to cancel the call, use the same above rejectCall()
method and just pass the status to the rejectCall()
method as CometChatConstants.callStatus.cancelled
The possible values for the status can be one of the below:
status | interpretation | Enum available |
---|---|---|
rejected | The receiver rejects the call as it is received without accepting the call | .rejected |
cancelled | The initiator ends the call without the receiver accepting the call | .cancelled |
busy | The receiver rejects the call, as he/she is busy on another call | .busy |
Start the Call
Once the call request is sent and the receiver has accepted the call, both the initiator and the receiver need to call the startCall()
method.
You need to call this method for two scenarios:
- After you accept an incoming call i.e. in the
onSuccess()
callback of theacceptCall()
method. - After the recipient accepts an outgoing call i.e. in the
onOutgoingCallAccepted()
method of theCometChatCallDelegate
.
- Swift
- Objective C
CometChat.acceptCall(sessionID: incomingCall!.sessionId, onSuccess: { (ongoing_call) in
let callView = // a UIView you want to show the calling view in
let sessionID = ongoing_call.sessionID
let callSettings = CallSettings.CallSettingsBuilder(callView: callView, sessionId:sessionID).build()
CometChat.startCall(callSettings: callSettings, userJoined: { (joined_user) in
print("Joined User: " + joined_user.stringValue())
}, userLeft: { (left_user) in
print("Left User: " + left_user.stringValue())
}, userListUpdated: { (userList) in
print("userList \(userList)")
}, audioModesUpdated: { (audioList) in
print("audioList \(audioList)")
}, onUserMuted: {onUserMuted in
print("onUserMuted \(onUserMuted)")
}, onCallSwitchedToVideo: {callSwitchVideo in
print("callSwitchVideo \(callSwitchVideo)")
}, onRecordingStarted: {onRecordingStarted in
print("onRecordingStarted \(onRecordingStarted)")
}, onRecordingStopped: {onRecordingStopped in
print("onRecordingStopped \(onRecordingStopped)")
}, onError: { (error) in
print("Connection error with error: " + error.errorDescription);
}) { (ended_call) in
print("Call ended successfully. " + ended_call!.stringValue());
}
}) { (error) in
print("Accepting call failed with error:" + error.ErrorDescription);
}
- (void)onIncomingCallReceivedWithIncomingCall:(Call * _Nullable)incomingCall error:(CometChatException * _Nullable)error {
[CometChat acceptCallWithSessionID:incomingCall.sessionID onSuccess:^(Call * ongoing_call) {
// Call accepted successfully
UIView *callView = [UIView new]; //// a UIView you want to show the calling view in
[CometChat startCallWithSessionID:ongoing_call.sessionID inView:callView userJoined:^(User * joined_user) {
NSLog(@"Joined user: %@",[joined_user stringValue]);
} userLeft:^(User * left_user) {
NSLog(@"Left user %@",[left_user stringValue]);
} onError:^(CometChatException * error) {
NSLog(@"Error: %@",[error ErrorDescription]);
} callEnded:^(Call * call_ended) {
NSLog(@"Ended call: %@",[call_ended stringValue]);
}];
} onError:^(CometChatException * error) {
// Error
NSLog(@" error %@",[error ErrorDescription]);
}];
}
The current startCall()
method which takes the callView
, sessionId
, to load the call viewparameters directly, is deprecated and will be replaced in the future versions. It will continue to work on this version but we suggest moving to the new method as shown above using the object of the
CallSettings` class as the parameter.
Parameter | Description |
---|---|
view | The view that you want to show the calling view in. |
sessionID | The unique session ID available in the Call object |
The OngoingCallListener
listener provides you with the below callback methods:
Callback Method | Description |
---|---|
userJoined | This method is called when any other user joins the call. The user details can be obtained from the User object provided. |
userLeft | This method is called when a user leaves the call. The details of the user can be obtained from the provided User object. |
onError | This method is called when there is some error in establishing the call. |
callEnded | This method is called when the call is successfully ended. The call details can be obtained from the Call object provided. |
userListUpdated | This method is triggered every time a participant joins or leaves the call providing the list of users active in the call |
audioModesUpdated | This callback is triggered if any new audio output source is available or becomes unavailable. |
onUserMuted | This method is triggered when a user is muted in the ongoing call. |
onCallSwitchedToVideo | This callback is triggered when an audio call is converted into a video call. |
onRecordingStarted | This method is triggered when a user starts video recording. |
onRecordingStopped | This method is triggered when a user stops video recording |
Settings
The CallSettings
class is the most important class when it comes to the implementation of the Calling feature. This is the class that allows you to customize the overall calling experience. The properties for the call/conference can be set using the CallSettingsBuilder
class. This will eventually give you and object of the CallSettings
class which you can pass to the startCall()
method to start the call.
The mandatory parameters that are required to be present for any call/conference to work are:
- View - A View object in which the calling UI is loaded.
- sessionId - The unique session Id for the call/conference session.
The options available for customisation of calls are:
Setting | Description |
---|---|
enableDefaultLayout(boolean) | If set to true enables the default layout for handling the call operations. If set to false it hides the button layout and just displays the Call ViewDefault value = true |
showAudioModeButton(boolean) | If set to true it displays the AudioModeButton in Button Layout. if set to false it hides the AudioModeButton in Button LayoutDefault value = true |
showEndCallButton(boolean) | If set to true it displays the EndCallButton in Button Layout. if set to false it hides the EndCallButton in Button LayoutDefault value = true |
showPauseVideoButton(boolean) | If set to true it displays the PauseVideoButton in Button Layout. if set to false it hides the PauseVideoButton in Button LayoutDefault value = true |
showMuteAudioButton(boolean) | If set to true it displays the MuteAudioButton in Button Layout. if set to false it hides the MuteAudioButton in Button LayoutDefault value = true |
showSwitchCameraButton(boolean) | If set to true it displays the SwitchCameraButton in Button Layout. if set to false it hides the SwitchCameraButton in Button LayoutDefault value = true |
setMode(mode: CometChat.CallMode) | Currently, you can set 3 modes in CallDisplayModeEnum. MODE_DEFAULT MODE_SINGLE MODE_SPOTLIGHT when MODE_DEFAULT is set our default screen will be displayed when MODE_SINGLE is set the user will be able to see the opponents video only when MODE_SPOTLIGHT is set the user will be able to see members joined count along with their name and can also pin/unpin users |
setAudioOnlyCall(boolean) | If set to true, the call will be strictly an audio call. If ser to false, the call will be an audio-video call. Default value = false |
startWithAudioMuted(audioMuted: boolean) | This ensures the call is started with the audio muted if set to true |
startWithVideoMuted(videoMuted: boolean) | This ensures the call is started with the video paused if set to true |
setDefaultAudioMode(mode: CometChat.AudioMode) | This method can be used if you wish to start the call with a specific audio mode. The available options are 1. SPEAKER 2. EARPIECE 3. BLUETOOTH 4. HEADPHONES |
showSwitchToVideoCallButton(switchCall: boolean) | This method shows/hides the switch to video call button. If set to true it will display the switch to video call button. If set to false it will hide the switch to video call button. Default value = true |
setMainVideoAspectRatio(_ asceptRatio: CallSettings.AsceptRatio) | This method can be used to customize the main video container. You can read more about this here. |
enableVideoTileClick(_ flag: Bool) | This method can be used to enable/disable video tile click functionality in MODE_SPOTLIGHT mode. Default value = true |
enableVideoTileDrag(_ flag: Bool) | This method can be used to enable/disable video tile click functionality in MODE_SPOTLIGHT mode. Default value = true |
In case you wish to achieve a completely customised UI for the Calling experience, you can do so by embedding default UI buttons to the screen as per your requirement and then use the below methods to achieve different functionalities for the embedded buttons.
For the use case where you wish to align your own custom buttons and not use the default layout provided by CometChat, you can embed the buttons in your layout and use the below methods to perform the corresponding operations:
Switch Camera
Switch Camera
You can call switchCamera()
Method to toggle between camera sources.
- Swift
let callmanager = CallManager()
callmanager.switchCamera()
Mute Audio
Mute Audio
You can call muteAudio()
Method to mute your Audio Stream to the end-user.
- Swift
let callmanager = CallManager()
callmanager.muteAudio(mute: true) //toggle this value for unmuting the user's audio
If set to true
the Audio Stream is muted and if set tofalse
Audio Stream is transmitted.
Pause Video
You can call pauseVideo()
Method to pause Video Stream to the end-user.
- Swift
let callmanager = CallManager()
callmanager.pauseVideo(pauseVideo: true) //toggle this value for resuming the user's video
If set to true
the Video Stream is muted and if set tofalse
Video Stream is transmitted.
Audio Mode
You can call setAudioMode()
to set the output of the audio stream.
- Swift
let callmanager = CallManager()
callmanager.audioMode(setAudioMode: .EARPIECE)
The audio mode can have either one of the below 4 values:
- SPEAKER
- EARPIECE
- BLUETOOTH
- HEADPHONES
End Call
End Call is handled internally by CometChat when using the default UI provided. Once the user presses the end call button, CometChat internally triggers the action to end the call.
- Swift
var sessionID = "" //sessionId of the call
CometChat.endCall(sessionID: sessionID, onSuccess: { (call) in
print("Success \(String(describing: call?.description))")
}) { (error) in
print("Error \(String(describing: error?.errorDescription))")
}
Get list of available audio modes
This method will return the list of available audio modes. Based on the list received, you can set the audio mode for the ongoing call using the setAudioMode()
method.
- Swift
CometChat.getAudioModes { (modes) in
print("Available audio modes received \(modes)")
}
The AudioMode class has the below properties:
- mode - The mode can be either one of the below values:
- SPEAKER
- EARPIECE
- BLUETOOTH
- HEADPHONES
- isSelected - This is a boolean value that indicates if the audio mode is the active mode.
Active Call
CometChat provides a method to get the details of the call that is currently active. The CometChat.getActiveCall()
will return an object of the Call
containing the details of the ongoing call. If there is no ongoing call this method will return nil.
- Swift
var onGoingCall = CometChat.getActiveCall()
Call Participant Count
In order to get the number of participants in any call, you can use the getCallParticipantCount()
method. This method takes the session id of the call and returns the number of participants that are active in the call.
- Swift
let sessionID = "12345"
CometChat.getCallParticipantCount(sessionId: "", type: .DIRECT, onSucces: {
(count) in
print("Count \(count)")
}, onError: { (error) in
print("OnError \(error?.errorDescription)")
})