Skip to main content
Version: 5.5

Group audio call

This page provides a code example for implementing a group audio call.

Prerequisites

Before you begin, you must do the following:

Considerations for implementing a group audio call

After calling joinConference(), you must check the reason property of the returned PlanetKitConferenceJoinResult.

  • If the reason is PlanetKitStartFailReason.none, it means success.
  • Otherwise, it means failure and you must take an appropriate action based on the reason.

Implement event delegates

Implement the event delegates to be used on the call.

  • PlanetKitConferenceDelegate is used to handle status change events of a group call.
  • PlanetKitMyMediaStatusDelegate is used to handle the local user's media status change events. You can update the local user's UI based on these events, such as when the mic becomes muted or unmuted, or when the audio description is updated.
extension ConferenceDelegateExample : PlanetKitConferenceDelegate {
func didConnect(_ conference: PlanetKitConference, connected: PlanetKitConferenceConnectedParam) {

// This is called when the call is connected.
// Write your own code here.

}

func didDisconnect(_ conference: PlanetKitConference, disconnected: PlanetKitDisconnectedParam) {

// This is called when the call is disconnected.
// Write your own code here.

}

func peerListDidUpdate(_ conference: PlanetKitConference, updated: PlanetKitConferencePeerListUpdateParam) {

// This is called when the list of peers is updated.
// Write your own code here.

}

//
// Also, you should implement other methods defined in the PlanetKitConferenceDelegate protocol.
//
...
}

extension MyMediaStatusDelegateExample: PlanetKitMyMediaStatusDelegate {
func didMuteMic(_ myMediaStatus: PlanetKitMyMediaStatus) {

// This is called when the local user's audio is muted.
// Write your own code here.

}

func didUnmuteMic(_ myMediaStatus: PlanetKitMyMediaStatus) {

// This is called when the local user's audio is unmuted.
// Write your own code here.

}

func didUpdateAudioDescription(_ myMediaStatus: PlanetKitMyMediaStatus, description: PlanetKitMyAudioDescription) {

// This is called when the local user's audio description is updated.
// Write your own code here.

}

//
// Also, you should implement other methods defined in the PlanetKitMyMediaStatusDelegate protocol.
//
...
}

Join a group call

The steps for joining a group call are as follows:

  1. Build a setting through PlanetKitJoinConferenceSettingBuilder.
  2. Create PlanetKitConferenceParam with PlanetKitConferenceDelegate.
  3. Call PlanetKitManager.shared.joinConference().
  4. Check the PlanetKitConferenceJoinResult.
class ParticipantExample
{
var conference: PlanetKitConference?
var myMediaStatusDelegate: MyMediaStatusDelegateExample
}

...

extension ParticipantExample
{
func joinConferenceExample(myId: String, myServiceId: String, roomId: String, roomServiceId: String, accessToken: String, delegate: PlanetKitConferenceDelegate) {
let myUserId = PlanetKitUserId(id: myId, serviceId: myServiceId)

let settings = try! PlanetKitJoinConferenceSettingBuilder().build()
let param = PlanetKitConferenceParam(myUserId: myUserId, roomId: roomId, roomServiceId: roomServiceId, displayName: nil, delegate: delegate, accessToken: accessToken)

let result = PlanetKitManager.shared.joinConference(param: param, settings: settings)

guard result.reason == .none else {
NSLog("Failed reason: result. \(result.reason)")
return
}

// The result.conference instance is a conference instance for calling other APIs from now on.
// You must keep this instance in your own context.
// In this example, the "conference" variable holds the instance.
conference = result.conference
}
}

Set the media status event delegate for the local user

Once the call is connected, add the media status event delegate for the local user.

extension ParticipantExample
{
func addMyMediaStatusHandlerExample() {
conference?.myMediaStatus.addHandler(myMediaStatusDelegate) {
// completion callback
}
}
}

Create peer control for each peer

When the peer list is updated, create PlanetKitPeerControl for newly added peers and register a PlanetKitPeerControlDelegate object. You can handle the status change of each peer using the PlanetKitPeerControlDelegate.

#if os(macOS)
typealias UIView = NSView
#endif

class YourPeerView: UIView {

...

var peerControl: PlanetKitPeerControl?
var peerVideoView: PlanetKitMTKView!

func setupPeerControl(for peer: PlanetKitConferencePeer, in conference: PlanetKitConference) {
guard let peerControl = conference.createPeerControl(peer: peer) else {
return
}

peerControl.register(self) { success in
// Write your own code here.
}
self.peerControl = peerControl
}
}

extension YourPeerView: PlanetKitPeerControlDelegate {

func didMuteMic(_ peerControl: PlanetKitPeerControl) {

// This is called when the local user's audio is muted.
// Write your own code here.

}

func didUnmuteMic(_ peerControl: PlanetKitPeerControl) {

// This is called when the local user's audio is unmuted.
// Write your own code here.

}

//
// Also, you should implement other methods defined in the PlanetKitPeerControlDelegate protocol.
//
...
}

Leave a group call

To leave a group call, call leaveConference().

extension ParticipantExample
{
func leaveConferenceExample() {
conference?.leaveConference()
}
}

Implement Apple CallKit in the application

Note

This step applies to iOS applications only.

CallKit is one of Apple's frameworks that provides the calling interface. PlanetKit SDK supports three options for CallKit: integration of user CallKit implementation, integration of PlanetKit internal implementation, and no integration.

By default, PlanetKit SDK joins a group call without CallKit integration. If you want to integrate your own implementation of CallKit, you must set type of PlanetKitCallKitSetting to user when joining a group call with PlanetKit SDK.

To implement CallKit in your application, do the following:

  1. Create a PlanetKitCallKitSetting instance and set type of PlanetKitCallKitSetting to user.

  2. Pass the instance to withCallKitSettingsKey() of PlanetKitJoinConferenceSettingBuilder and build settings.

    // Code for Step 1 and Step 2

    func createJoinConferenceSettingsWithUserCallKitExample() -> [String: Any] {
    let callKitSetting = PlanetKitCallKitSetting(type: .user, param: nil)

    var settingsBuilder = try! PlanetKitJoinConferenceSettingBuilder().withCallKitSettingsKey(setting: callKitSetting)

    let joinConferenceSettings = settingsBuilder.build()
    }
  3. On provider(_:didActivate:) of CXProviderDelegate, call notifyCallKitAudioActivation() of PlanetKitConference.

    // Code for Step 3

    extension YourCallKitHandler : CXProviderDelegate {
    ...

    func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    // PlanetKitConference instance created from PlanetKitManager.joinConference()
    conference.notifyCallKitAudioActivation()
    }

    ...
    }

For more information, see CallKit documentation.