Implement Voice and Video Calls
CallLib is an SDK built upon RTCLib that encapsulates additional voice/video call functionalities. It supports various one-to-one call scenarios, enabling flexible implementation of diverse calling features.
Prerequisites
-
Register a developer account. Upon successful registration, the Console automatically creates your first application with a default development environment App Key under the Singapore Data Center.
-
Obtain your application's App Key for the development environment. If not using the default app, refer to How to create an application and obtain corresponding App Key/Secret.
Note
Each application has two distinct App Keys for development and production environments respectively, with data isolation between them. Before official release, switch to the production environment App Key for final testing.
-
Complete Activating RTC Services. Ensure the Voice/Video Call service is enabled.
-
Complete Importing CallLib SDK.
Step 1: Permission Configuration
Android
-
Declare all required permissions in
AndroidManifest.xml
:<!-- Network permissions for RTC -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Camera capture -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Audio capture -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> -
For devices running Android 6.0 (API level 23) or higher, request
CAMERA
andRECORD_AUDIO
permissions at runtime when features are used. Refer to Android docs: Runtime Permissions and Requesting Permissions.
iOS
Add camera and microphone usage descriptions to Info.plist
:
<key>NSCameraUsageDescription</key>
<string>Camera access required for video calls</string>
<key>NSMicrophoneUsageDescription</key>
<string>Microphone access required for voice calls</string>
Step 2: Initialize with App Key
Since RTC signaling relies on RC's IM services, first initialize the IM SDK (rongcloud_im_wrapper_plugin). The initialization method accepts an App Key parameter and an optional RCIMIWEngineOptions
configuration. Initialize only once per app lifecycle unless changing App Keys.
RCIMIWEngineOptions options = RCIMIWEngineOptions.create();
RCIMIWEngine imEngine = await RCIMIWEngine.create(appKey, options);
Note
For detailed IM SDK initialization, refer to Initialization in IM SDK documentation.
Step 3: Call Event Handling
-
Set
onReceiveCall
inRCCallEngine
to handle incoming calls:engine?.onReceiveCall = (RCCallSession session) {
/// session: call entity
}; -
Monitor call state changes via
onCallDidMake
,onConnect
, andonDisconnect
:/// Outgoing call initiated
engine?.onCallDidMake = () {
};
/// Call established
engine?.onConnect = () {
};
/// Call terminated
engine?.onDisconnect = (RCCallDisconnectReason reason) {
/// reason: disconnect cause
};
Step 4: Obtain User Token
A Token is the authentication credential corresponding to a user ID, serving as the unique identifier in RC's system. Clients must establish an IM connection using a valid Token before accessing RTC features.
In production, obtain Tokens by having your app server call IM Server API User Registration.
For testing purposes, you may generate Tokens via the IM Server API Debugger in Console's Polaris developer tools.
Step 5: Connect to IM Service
After obtaining the IM engine instance, establish a TCP connection by calling connect
. We recommend initiating this during module initialization before RTC operations, and disconnecting via disconnect
or logout
upon exit.
-
Monitor connection status changes via
onConnectionStatusChanged
, which returns RCIMIWConnectionStatus:engine?.onConnectionStatusChanged = (RCIMIWConnectionStatus? status) {
//...
}; -
Connect using the obtained Token (simulating user ID '1'). Results are returned via RCIMIWConnectCallback:
RCIMIWConnectCallback? callback = RCIMIWConnectCallback(onDatabaseOpened: (int? code) {
//...
}, onConnected: (int? code, String? userId) {
//...
});
int? ret = await engine?.connect(token, timeout, callback:callback);
Note
- The SDK automatically retries connection up to 10 times (with intervals of 1, 2, 4...512 seconds) upon failure, and reattempts when network state changes.
- If the app is killed and relaunched via push notification, call
connect
again.- For detailed IM connection, refer to Connection.
Step 6: Initiate Call
After successful IM connection, initiate calls using startCall
:
/// Callee user ID
String targetId = 'UserB';
/// Call media type
RCCallMediaType mediaType = RCCallMediaType.audio_video;
engine?.startCall(targetId, mediaType);
Video calls default to front camera. Modify defaultCamera
in RCCallVideoConfig
pre-call. Enable beauty filters post-connection.
/// Set 1080p resolution and rear camera
RCCallVideoConfig config = RCCallVideoConfig.create(profile: RCCallVideoProfile.profile_1080_1920_high,
defaultCamera: RCCallCamera.back);
int ret = await Utils.engine!.setVideoConfig(config);
Step 7: Answer Call
Upon receiving onReceiveCall(RCCallSession session)
, accept the call:
engine?.accept();
Step 8: Offline Push Notifications
To receive call notifications when the app is terminated, integrate remote push. See Push Overview.