グループ音声通話
グループ音声通話を実装するサンプルコードです。
前提条件
開始する前に、次の作業が必要です。
- PlanetKitを初期化してください。
- 適切なアクセストークンを取得してください。
- グループ通話フローにて、APIを使用するための全般的なプロセスを確認してください。
グループ音声通話実装時の考慮事項
joinConference()
を呼び出した後、返却されたPlanetKitConferenceJoinResult
のreason
プロパティを確認する必要があります。
reason
がPlanetKitStartFailReason.none
の場合、成功を意味します。- それ以外は失敗を意味し、
reason
に応じて適切に処理する必要があります。
イベントデリゲートの実装
通話で使用されるイベントデリゲート(delegate)を実装します。
PlanetKitConferenceDelegate
は、グループ通話のステータス変更イベントを処理するのに使用されます。PlanetKitMyMediaStatusDelegate
は、ローカルユーザーのメディアステータス変更イベントを処理するのに使用されます。マイクがミュートもしくはミュート解除された場合、またはオーディオの説明がアップデートされた場合など、イベントに基づいてローカルユーザーのUIをアップデートできます。
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.
//
...
}
グループ通話参加
グループ通話に参加するための手順は次のとおりです。
PlanetKitJoinConferenceSettingBuilder
を通じて設定情報を作成します。PlanetKitConferenceDelegate
を含めてPlanetKitConferenceParam
を作成します。PlanetKitManager.shared.joinConference()
を呼び出します。PlanetKitConferenceJoinResult
を確認してください。
PlanetKit 6.0からiOSアプリケーションでは、通話設定を作成する時にPlanetKitCallKitSetting
にCallKitタイプを必ず指定する必要があるため、グループ通話に参加するためのコードがiOSとmacOSの間で異なります。CallKitタイプの指定に関する詳細は、アプリケーションでCallKitタイプ設定を参照してください。
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)
// Configure CallKitSetting
let callKitSetting = createCallKitSetting()
var settingsBuilder = PlanetKitJoinConferenceSettingBuilder().withCallKitSettingsKey(setting: callKitSetting)
let settings = try! settingsBuilder.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
}
}
ローカルユーザーに対するメディアステータスのイベントデリゲート設定
通話が接続されると、ローカルユーザーに対するメディアステータスのイベントデリゲートを設定します。
extension ParticipantExample
{
func addMyMediaStatusHandlerExample() {
conference?.myMediaStatus.addHandler(myMediaStatusDelegate) {
// completion callback
}
}
}
各ピアに対するピアコントロールの作成
ピアリストが更新されたら、新たに追加されたピアに対してPlanetKitPeerControl
を作成し、PlanetKitPeerControlDelegate
オブジェクトを登録してください。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.
//
...
}
グループ通話退出
グループ通話から退出するには、leaveConference()
を呼び出します。
extension ParticipantExample
{
func leaveConferenceExample() {
conference?.leaveConference()
}
}
アプリケーションでCallKitタイプ設定
CallKitは、通話インスタンスを提供するAppleのフレームワークです。PlanetKit SDKはCallKit連動のために以下3つのオプションを提供します。
- ユーザー定義のCallKit実装
- PlanetKit内部のCallKit実装
- CallKitを連動しない
PlanetKit SDKを使用して通話を作成または受信する際、PlanetKitCallKitSetting
にCallKitタイプを指定する必要があります。
PlanetKitCallKitType
がuser
またはplanetkit
に設定された場合、PlanetKitはマイクインジケーターの制御をCallKitに委任することに注意してください。
PlanetKitCallKitType
の設定例
func createCallKitSetting() -> PlanetKitCallKitSetting {
// Select the appropriate CallKit integration option from the examples below
// when integrating the user CallKit implementation.
let callKitSetting = PlanetKitCallKitSetting(type: .user, param: nil)
// when using PlanetKit internal CallKit implementation.
let callkitParam = PlanetKitCallKitParam(appName: "Example app", callerName: "caller name", isVideo: true, ringtoneSound: nil, icon: "example icon", addCallToList: true, supportsHolding: true)
let callKitSetting = PlanetKitCallKitSetting(type: .planetkit, param: callkitParam)
// when CallKit is not used in the app.
let callKitSetting = PlanetKitCallKitSetting(type: .none, param: nil)
return callKitSetting
}
アプリケーションでApple CallKitを実装
次の例は、ユーザー定義のCallKit実装をPlanetKitに連動させる方法についてのガイドです。
ユーザー定義のCallKit実装を連動するとき、CallKitとPlanetKitの間でミュート状態などの通話状態を同期化することが重要です。
-
PlanetKitCallKitSetting
インスタンスを作成し、PlanetKitCallKitSetting
のtype
をuser
に設定します。 -
PlanetKitJoinConferenceSettingBuilder
のwithCallKitSettingsKey()
にインスタンスを引き渡し、設定情報を作成してください。// Code for Step 1 and Step 2
func createJoinConferenceSettingsWithUserCallKitExample() -> [String: Any] {
let callKitSetting = PlanetKitCallKitSetting(type: .user, param: nil)
var settingsBuilder = PlanetKitJoinConferenceSettingBuilder().withCallKitSettingsKey(setting: callKitSetting)
let joinConferenceSettings = try! settingsBuilder.build()
return joinConferenceSettings
} -
CallKit動作を処理するためのCallKitハンドラーを実装します。
conference
変数はグループ通話に参加する時に取得したPlanetKitConference
インスタンスです。class YourCallKitHandler {
static let shared = YourCallKitHandler()
private var provider: CXProvider
private var callController: CXCallController
private var conference: PlanetKitConference
...
// Implement initialization and other features to handle CallKit operations.
} -
CXProviderDelegate
のprovider(_:didActivate:)
でPlanetKitConference
のnotifyCallKitAudioActivation()
を呼び出します。extension YourCallKitHandler : CXProviderDelegate {
...
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
conference.notifyCallKitAudioActivation()
}
// Implement other delegate functions to handle other CXCallActions
...
} -
CallKitで
CXCallAction
が実行される時に関連PlanetKitConference
関数を呼び出します。extension YourCallKitHandler : CXProviderDelegate {
...
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
conference.leaveConference()
action.fulfill()
}
func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
conference.muteMyAudio(action.isMuted) { success in
guard success else {
action.fail()
return
}
action.fulfill()
}
}
func provider(_ provider: CXProvider, perform action: CXSetHeldCallAction) {
if action.isOnHold {
conference.hold(reason: nil) { success in
guard success else {
action.fail()
return
}
action.fulfill()
}
}
else {
conference.unhold() { success in
guard success else {
action.fail()
return
}
action.fulfill()
}
}
}
// Implement other delegate functions to handle other CXCallActions
...
} -
CallKitとPlanetKitを同期化するためにCallKitを通じて関連
CXCallAction
を呼び出します。extension YourCallViewController {
@IBAction func muteCall(_ sender: UIButton) {
conference.muteMyAudio() { success in
if success {
YourCallKitHandler.shared.muteCall()
}
}
}
@IBAction func holdCall(_sender: UIButton) {
conference.hold(reason: nil) { success in
if success {
YourCallKitHandler.shared.holdCall()
}
}
}
}
extension YourCallKitHandler {
func muteCall() {
let action = CXSetMutedCallAction(call: call.uuid , muted: true)
callController.requestTransaction(with: action) { error in
NSLog("\(error)")
}
}
func holdCall() {
let action = CXSetHeldCallAction(call: call.uuid, onHold: true)
callController.requestTransaction(with: action) { error in
NSLog("\(error)")
}
}
}
詳細は、CallKitドキュメントを参照してください。