1対1音声通話
1対1音声通話を実装するサンプルコードです。
前提条件
開始する前に、次の作業が必要です。
- PlanetKitを初期化してください。
- 適切なアクセストークンを取得してください。
- 1対1通話フローにて、APIを使用するための全般的なプロセスを確認してください。
1対1音声通話実装時の考慮事項
受信者側で通話の通知を受けるには、通知用のシステムを実装するか、UWP(Universal Windows Platform)を利用してアプリケーションを実装後にプッシュ通知システムを連携する必要があります。
また、受信者にどのような情報を伝えるべきかを知っておく必要があります。アプリサーバーの役割でアプリケーションが転送すべきデータであるcc_param
について確認できます。
MakeCall()
、VerifyCall()
を呼び出してからは、SStartResult::EStartFailReason
で通話に問題はないか確認する必要があります。通話に失敗した場合は、その値に応じて適切な処理を行う必要があります。
動作する全体のソースコードは、デモアプリのソースコードで確認できます。
共通 - ICallEvent
実装
ICallEvent
は、コールセットアップ(call setup)に関するイベント処理用のインターフェースを提供する抽象クラスです。ICallEvent
を継承したクラスを作成し、関数をオーバーライドします。
class YourCallEventListener : public ICallEvent {
public:
// This is called after the call is connected on both sides.
// Write your own code in function.
void OnConnected(PlanetKitCallPtr pPlanetKitCall, CallConnectedParamPtr pConnectedParam) override;
// This is called after making a call on the caller side.
// Write your own code in function.
void OnWaitAnswer(PlanetKitCallPtr pPlanetKitCall) override;
// This is called after making a call on the callee side.
// Write your own code in function.
void OnVerified(PlanetKitCallPtr pPlanetKitCall, CallVerifiedParamPtr pVerifiedParam) override;
// This is called after the call is disconnected on both sides.
// Write your own code in function.
void OnDisconnected(PlanetKitCallPtr pPlanetKitCall, CallDisconnectedParamPtr pDisconnectedParam) override;
...
//
// Also, you should implement other override functions.
//
};
発信側 - 通話の作成
新たに通話を開始するには、適切なMakeCallParamPtr
引数を利用してMakeCall()
を呼び出します。
class YourApplication {
private :
// Prepare callback event listener instance.
YourCallEventListener m_yourCallEventListener;
// Prepare PlanetKitCall instance.
PlanetKit::PlanetKitCallPtr m_pCallPtr;
};
void YourApplication::MakeCallExample(const std::wstring& wstrAccessToken) {
// Prepare local user's ID and peer's ID.
std::wstring strMyUserId = "your user id";
std::wstring strMyServiceId = "your service id";
std::wstring strPeerUserId = "peer id";
std::wstring strPeerServiceId = "peer service id";
// Create myId and peerId.
PlanetKit::UserIdPtr pMyId = PlanetKit::UserId::Create(strMyUserId.c_str(), strMyServiceId.c_str());
PlanetKit::UserIdPtr pPeerId = PlanetKit::UserId::Create(strPeerUserId.c_str(), strPeerServiceId.c_str());
// Create MakeCallParam with Create API.
PlanetKit::MakeCallParamPtr pMakeCallParam = PlanetKit::MakeCallParam::CreateWithAccessToken(
pMyId,
pPeerId,
wstrAccessToken.c_str()
);
// Set required parameter.
pMakeCallParam->SetCallEvent(&m_yourCallEventListener);
// Now you have to create a PlanetKit::PlanetKitCall type object
// through the PlanetKit::PlanetKitManager::MakeCall() function.
// Please note that the PlanetKit::PlanetKitCall type object is the main call instance
// that controls call-related functions after the call setup completion.
PlanetKit::PlanetKitManagerPtr pPlanetKitManager = PlanetKit::PlanetKitManager::GetInstance();
PlanetKit::SStartResult sStartResult = pPlanetKitManager->MakeCall(pMakeCallParam, &m_pCallPtr);
if (sStartResult.bSuccess == false) {
LOG_ERROR("MakeCall() failed. Reason: " << sStartResult.reason);
}
}
受信側 - 通話通知の受信
Windowsアプリケーションでは、Universal Windows Platform(UWP)以外に適当なプッシュ通知システムはありません。したがって、Windows向けのPlanetKitを使用する際には、指定した受信者にcc_param
を転送するシステムを備える必要があります。
以下のコードは、次のような前提で作成しました。
- すべてのクライアントのアプリケーションは、特定のユーザーに新しい電話がかかってきたかどうかを知らせるために、周期的にアプリサーバーでの通話通知を確認します。
- 通知データは、JSON形式の文字列で渡します。
void YourApplication::UpdateNotificationFromServer(std::string& strCCParam) {
char strPushDataFromAppServer[2048];
// The app server can deliver cc_param with the desired protocol through the application channel.
// We assume the app server forwards cc_param in JSON string format in this example.
// The strPushDataFromAppServer is a JSON string containing cc_param received from the app server.
// strPushDataFromAppServer looks like this:
//
//{
// ...,
// "cc_param": "...",
// ...
//}
// We assume GetNotificationThroughApplicationChannel() reads JSON string including cc_param.
GetNotificationThroughApplicationChannel(strPushDataFromAppServer);
// We assume JSON-C open source library (https://github.com/json-c/json-c) is used to parse JSON strings.
json_object *pJsonObjectResponse = json_tokener_parse(strPushDataFromAppServer);
json_object *pJsonObjectCCParam = json_object_object_get(pJsonObjectResponse, "cc_param");
strCCParam = json_object_get_string(pJsonObjectCCParam);
return;
}
受信側 - 通話の受信
VerifyCallParam
を作成し、VerifyCall()
を呼び出して通話を受信します。
class YourApplication {
private :
// Prepare callback event listener instance.
YourCallEventListener m_yourCallEventListener;
// Prepare PlanetKitCall instance.
PlanetKit::PlanetKitCallPtr m_pCallPtr;
};
void YourApplication::VerifyCallExample(std::string& strCCParam) {
// Prepare for VerifyCall API's parameter.
PlanetKit::VerifyCallParamPtr pVerifyCallParam;
// Prepare my ID.
std::wstring strMyUserId = "your user id";
std::wstring strServiceId = "Your service id";
PlanetKit::UserIdPtr pMyId = PlanetKit::UserId::Create(strMyUserId.c_str(), strServiceId.c_str());
// Now you can create CCParamPtr with the PlanetKitManager instance.
PlanetKit::PlanetKitManagerPtr pPlanetKitManager = PlanetKit::PlanetKitManager::GetInstance();
PlanetKit::CCParamPtr pCCParam = pPlanetKitManager->CreateCCParam(strCCParam.c_str());
// Create VerifyCallParam.
pVerifyCallParam = PlanetKit::VerifyCallParam::Create(pMyId, pCCParam);
// Set the required parameter.
pVerifyCallParam->SetCallEvent(&m_yourCallEventListener);
// Now you have to create a PlanetKit::PlanetKitCall type object
// through the PlanetKit::PlanetKitManager::VerifyCall() function.
// Please note that the PlanetKit::PlanetKitCall type object is the main call instance
// that controls call-related functions after the call setup completion.
PlanetKit::PlanetKitCallPtr pPlanetKitCall;
PlanetKit::SStartResult sStartResult = pPlanetKitManager->VerifyCall(pVerifyCallParam, &m_pCallPtr);
if (sStartResult.bSuccess == false) {
LOG_ERROR("VerifyCall() failed. Reason: " << sStartResult.reason);
}
}
受信側 - 通話の応答
一般的に通話を受信した後、通話を受けるかどうかを決める時間が必要です。通話に応答するには、AcceptCall()
を呼び出します。
m_pCallPtr
変数は、VerifyCall()
で検証されたPlanetKitCall
のインスタンスです。
class YourApplication {
private :
// Prepare PlanetKitCall instance.
PlanetKit::PlanetKitCallPtr m_pCallPtr;
};
void YourApplication::AcceptCall(bool bPreparation) {
m_pCallPtr->AcceptCall(bPreparation);
}