Twilio Android SDK to VidyoClient Android SDK

This guide walks you through how to migrate your existing Twilio Video implementation to Vidyo Android SDK. To complement this guide, we have also developed a sample app that can switch between Twilio SDK and Vidyo SDK simply using a build-time configuration. That app provides a handy “translation guide” between Twilio and VidyoClient Android SDK.

Install the SDK

Download and install the VidyoClient SDK from the Developer Portal. For example, if you're building using Gradle or Groovy, add this line to your app/build.gradle file:

implementation fileTree(dir: 'libs', include: ['*.aar'])

and locate VidyoClient.aar SDK file under /app/libs/:

VidyoClient SDK has several dependencies have to be also included:

/* Camera X: Required for Vidyo */
var cameraxVersion = "1.2.1"
implementation("androidx.camera:camera-camera2:$cameraxVersion")
implementation("androidx.camera:camera-lifecycle:$cameraxVersion")
implementation("androidx.camera:camera-extensions:$cameraxVersion")
implementation("androidx.concurrent:concurrent-futures-ktx:1.1.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")

If you like, you can also remove the Twilio SDK from your project with the following:

implementation 'com.twilio:video-android:<version>'

Set permissions

Ensure that you have all of the Android system permissions required by the VidyoClient SDK in your project (you can ignore permissions that you already have):

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

If your project uses proguard, add the following rules to your project:

-keep class com.vidyo.** { *; }
-dontwarn com.vidyo.**

Code Changes

The code snippets below illustrate the mappings between the Vidyo and Twilio SDKs. Low-level functionality may differ between the two platforms, so the mappings may not be perfectly accurate for all use cases.

Initialize the SDK

In order to use The VidyoClient SDK, you must first initialize the Core SDK:

ConnectorPkg.initialize();
ConnectorPkg.setApplicationUIContext(this);

This is usually done when your app starts, but you can place it wherever it is appropriate for your implementation.

Initialize Connector, SDK instance. Vidyo SDK automatically sets the audio, video, and dominant speaker parameters, so the user does not need to set these parameters explicitly:

vidyoConnector = new Connector(binding.vidyoVideoView, 
Connector.ConnectorViewStyle.VIDYO_CONNECTORVIEWSTYLE_Default, 8, logLevel, "", 0);

There is no equivalent of this in the Twilio Video SDK, so it is important to find the correct place in your app to add initialization logic.

Join session/room

Twilio uses JSON Web Tokens (JWT) to generate a token for users to join sessions. Vidyo follows a different model – a meeting room is created on a backend middleware (referred to as Vidyo Portal) which generates a meeting link, and users join the room through their browser by clicking on the meeting link.

Twilio requires an Access Token to join a meeting room.

View instructions here for how to generate an Access Token using the Twilio CLI.

Generating Access Tokens with the Twilio CLI is a quick way to get an Access Token for testing and development purposes.

val connectOptions = ConnectOptions.Builder(accessToken)
    .videoTracks(localVideoTracks)
    .audioTracks(localAudioTracks)
    .roomName(sessionId)
    .build()

Video.connect(context, connectOptions, roomListener)

Video

While Twilio requires additional configuration to set the quality of the video, Vidyo handles the video quality automatically based on network conditions and device capabilities. In low bandwidth conditions Vidyo prioritizes audio over video, and shared content video tiles over participant video. Vidyo also supports configuration options to limit maximum resolution of local or remote video.

Moreover, Vidyo has a Composite Renderer concept where you just have to pass a container ViewGroup reference e.g. binding.vidyoVideoView and VidyoClient will handle all the Local/Remote participant composition for you.

Rendering local video

Twilio has a concept of video and audio tracks:

localAudioTrack = LocalAudioTrack.create(this, true, LOCAL_AUDIO_TRACK_NAME);

cameraCapturerCompat = new CameraCapturerCompat(this, CameraCapturerCompat.Source.FRONT_CAMERA);
localVideoTrack = LocalVideoTrack.create(this,true, cameraCapturerCompat, LOCAL_VIDEO_TRACK_NAME);

localVideoTrack.addSink(primaryVideoView);

The biggest difference between the implementations is the amount of setup that is required for Twilio. While Twilio requires manually setting up a CameraCapturerCompat and LocalVideoTrack to specify your input sources, this is all handled internally within the VidyoClient SDK.

Turn the camera off

localVideoTrack.enable(false);

However, you also have to manually hide the renderer.

Render remote user video

Twilio uses onParticipantConnected and onVideoTrackSubscribed event listeners to determine which remote videos to render.

@Override
public void onParticipantConnected(@NonNull Room room, @NonNull RemoteParticipant remoteParticipant) {
    addRemoteParticipant(remoteParticipant);
}



@Override
public void onVideoTrackSubscribed(
                    @NonNull RemoteParticipant remoteParticipant,
                    @NonNull RemoteVideoTrackPublication remoteVideoTrackPublication, @NonNull RemoteVideoTrack remoteVideoTrack) {
    addRemoteParticipantVideo(remoteVideoTrack);
}



/*
* Set primary view as renderer for participant video track
*/
private void addRemoteParticipantVideo(VideoTrack videoTrack) {
    moveLocalVideoToThumbnailView();
    primaryVideoView.setMirror(false);
    videoTrack.addSink(primaryVideoView);
}

Mute/Unmute Microphone

Since Twilio API is track based, you have to disable the track individually:

localAudioTrack.enable(false);

Event Listeners

Twilio has event listeners for room and participant management, allowing you to detect changes that occur during a meeting. In Twilio there are multiple listeners depending on the area of functionality you are using. So, it may require some refactoring to consolidate everything into one place.

Room.Listener interface:
- onConnected
- onReconnecting
- onReconnected
- onConnectFailure
- onDisconnected
- onParticipantConnected
- onParticipantDisconnected
[...]
RemoteParticipant.Listener interface:
- onVideoTrackSubscribed
- onVideoTrackUnsubscribed
[...]

Leave session

To end the session, call the corresponding disconnect method:

room.disconnect();

Last updated