Reference code snippet - How to initiate or accept Video Call
Sample Video call implementation.
Create an application class to initialize the SDK
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Rtc555Sdk.initializeLibrary(this);
}
}
Link the application class in AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rtcandroiddemo">
<application
android:name=".MyApplication"
...
>
...
</application>
</manifest>
Set SDK configuration after successful login
//After successful login
class VideoCallActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_call);
init();
}
private void init(){
Rtc555Sdk.setConfig(configObject(getApplication()));
}
private Rtc555Config configObject() {
Rtc555Config configObject = Rtc555Sdk.getInstance().getConfig();
configObject.routingId = <ROUTING_ID>; // Unique id for the user logged in 555 platform. (Identity manager response)
configObject.token = <555_JWT_TOKEN> // JWT token for accessing the 555 SDK/backend APIs. Token can be access from the 555 login response.
configObject.url = <EVM_URL>; // Event manager URL
configObject.domain = <DOMAIN> // Unique domain name
configObject.notificationManagerUrl = <NTM_URL> //(Optional) For mobile notifications from 555 Platform
return configObject;
}
}
Implement SDK Video callbacks
public class VideoCallActivity extends AppCompatActivity implements Rtc555Video.Rtc555VideoObserver {
...
private Boolean hasActiveCall = false;
private Rtc555Renderer localRenderer,remoteRenderer;
private FrameLayout localLayout, remoteLayout;
private String streamId;
private void init(){
...
Rtc555Video.createStream(this);
}
@Override
public void onRemoteStream(String streamId) {
remoteRenderer = new Rtc555Renderer(remoteLayout.getWidth(), remoteLayout.getHeight());
remoteRenderer.addStream(streamId, VideoCallActivity.this);
remoteLayout.addView(remoteRenderer.rootView);
}
@Override
public void onLocalStream(String streamId) {
this.streamId = streamId;
localRenderer = new Rtc555Renderer(localLayout.getWidth(), localLayout.getHeight());
localRenderer.addStream(streamId, VideoCallActivity.this);
localLayout.addView(localRenderer.rootView);
}
@Override
public void onStatus(CallStatus callStatus, String callId) {
switch (callStatus) {
case initializing:
Log.d(TAG, "Initializing..");
break;
case connecting:
Log.d(TAG, "Connecting..");
break;
case reconnecting:
Log.d(TAG, "Reconnecting..");
break;
case connected:
Log.d(TAG, "Active Call..");
hasActiveCall = true;
break;
case disconnected:
Log.d(TAG, "Disconnected..");
hasActiveCall = false;
break;
case hold:
Log.d(TAG, "Hold..");
break;
}
}
@Override
public void onError(Rtc555Exception errorInfo, String callId) {
Log.d(TAG,"Error: "+e.getMessage()+ "for callId: "+callId);
}
}
Initial Video call
private void initiateVideoCall(){
Rtc555Video.call(targetId, buildOutboundPayload(), streamId, this, new Rtc555Result() {
@Override
public void onSuccess(String callId) {
Log.d(TAG, "DIAL CALL SUCCESS AND CALLID IS " + callId);
}
@Override
public void onError(Rtc555Exception e) {
Log.d(TAG, "Dial Call Error: " + e.getMessage());
}
});
}
private String buildOutboundPayload() {
JSONObject userdata = new JSONObject();
try {
JSONObject data = new JSONObject();
data.put("cname", "username");
data.put("cid", "testuser@comcast.com");
data.put("isVideoOnly", true);
JSONObject notification = new JSONObject();
notification.put("type","video");
notification.put("topic", String.format("%s/%s", your_app_domain , "video"));
userdata.put("data", data);
userdata.put("notification", notification);
} catch (Exception e) {
e.printStackTrace();
}
Log.i(TAG, userdata.toString());
return userdata.toString();
}
Accept incoming video Call
public class VideoCallActivity extends AppCompatActivity implements Rtc555Video.Rtc555VideoObserver, Rtc555SdkObserver {
private void init(){
...
Rtc555Sdk.setNotificationObserver(this);
}
...
//SDK onNotification callback
@Override
public void onNotification(HashMap hashMap) {
String type = (String) hashMap.get("type");
switch (type){
case "notify":
Log.d(TAG, "xmpp notify");
//After user accepts incoming call invite, call SDK accept API with received payload
acceptCall(buildNotificationData(hashMap));
break;
case "cancel":
Log.d(TAG, "xmpp cancel");
break;
case "chat":
Log.d(TAG, "xmpp chat");
break;
default:
Log.d(TAG,"Failed to determine notification type");
}
}
@Override
public void onConnectionStateChange(String s) {
}
private void acceptCall(HashMap data){
Rtc555Video.accept(data, streamId, this, new Rtc555Result() {
@Override
public void onSuccess(String callId) {
Log.d(TAG, "START CALL SUCCESS AND CALLID IS " + callId);
}
@Override
public void onError(Rtc555Exception e) {
Log.d(TAG, "Accept Call Error: " + e.getMessage());
}
});
}
private HashMap buildNotificationData(HashMap data){
HashMap<String, String> notifyData = new HashMap();
notifyData.put("room_id", (String) data.get("roomid"));
notifyData.put("trace_id", (String) data.get("traceid"));
notifyData.put("room_token", (String) data.get("roomtoken"));
notifyData.put("room_token_expiry_time", (String) data.get("roomtokenexpirytime"));
notifyData.put("rtc_server", (String) data.get("rtcserver"));
notifyData.put("to_routing_id", (String) data.get("routingid"));
return notifyData;
}
}
Video on call features
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// User tapped on mute audio button
findViewById(R.id.your_mute_audio_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Video.audioMuteToggle(callId);
}
});
// User tapped on mute video button
findViewById(R.id.your_mute_video_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Video.videoMuteToggle(callId);
}
});
// User tapped on flip camera button
findViewById(R.id.your_flip_camera_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Video.flipCamera();
}
});
// User tapped on end call button
findViewById(R.id.your_hangup_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Rtc555Video.hangup(callId);
}
});
}
@Override
protected void onStop() {
super.onStop();
////No active video call then
if(!hasActiveCall)
Rtc555Sdk.cleanup();
}