# Twilio Android SDK to VidyoClient Android SDK

This guide walks you through how to migrate your existing [Twilio Video implementation](https://www.twilio.com/docs/video/android) to [Vidyo Android SDK](https://enghouse-vidyo.gitbook.io/vidyoplatform/android-integration). 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](https://enghouse-vidyo.gitbook.io/vidyoplatform/resources). For example, if you're building using Gradle or Groovy, add this line to your app/build.gradle file:

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

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

<figure><img src="https://3222273394-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FA1meIuKRQHExZTXqYpXl%2Fuploads%2FgHY2vCTn7OIIR339aZre%2Fimage.png?alt=media&#x26;token=76ce54cf-bcd6-406f-a91f-9e1885a428bc" alt=""><figcaption></figcaption></figure>

VidyoClient SDK has several dependencies have to be also included:

```java
/* 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:

```java
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):

```java
<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:

```java
-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:

```java
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:

```java
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

{% tabs %}
{% tab title="Twilio" %}
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](https://www.twilio.com/docs/video/tutorials/user-identity-access-tokens#generate-cli).

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

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

Video.connect(context, connectOptions, roomListener)
```

{% endtab %}

{% tab title="Vidyo" %}
Joining a meeting in Vidyo SDK requires the following:

1. URL of the VidyoPortal that owns/created the room.
2. Room name
3. Room pass code (optional). If this is set for a specific room, then users must enter the passcode to enter the room. This pass code is received as part of the meeting invitation.

```java
String portal = BuildConfig.VIDYO_PORTAL;
String displayName = BuildConfig.VIDYO_DISPLAY_NAME;
String roomKey = BuildConfig.VIDYO_ROOM;
String roomPin = BuildConfig.VIDYO_ROOM_PIN;

vidyoConnector.connectToRoomAsGuest(portal, displayName, roomKey, roomPin, this);
```

In a real production setting, the room creation/meeting link generation functionality is handled by a backend VidyoPlatform component referred to as VidyoPortal.

The VidyoPortal provides a web interface (and REST/SOAP APIs) for  creating meeting rooms, generating meeting links & moderating meetings.

Please refer to [Getting Started Guide](https://enghouse-vidyo.gitbook.io/vidyoplatform/getting-started) to see who to create a meeting room.
{% endtab %}
{% endtabs %}

### Video

While Twilio requires [additional configuration](https://www.twilio.com/docs/video/tutorials/developing-high-quality-video-applications) 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 <mark style="color:blue;">ViewGroup</mark> reference e.g. <mark style="color:blue;">binding.vidyoVideoView</mark> and VidyoClient will handle all the Local/Remote participant composition for you.

#### Rendering local video

{% tabs %}
{% tab title="Twilio" %}
Twilio has a concept of video and audio tracks:

```java
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);
```

{% endtab %}

{% tab title="Vidyo" %}
Vidyo has a <mark style="color:blue;">selectLocalCamera</mark> function to select which of the connected cameras to use in the call:

```java
vidyoConnector.selectLocalCamera(localCamera);
```

{% hint style="info" %}
Note: local camera list can be obtained from the: <mark style="color:blue;">onAdded(LocalCamera localCamera)</mark> device events callback.
{% endhint %}
{% endtab %}
{% endtabs %}

The biggest difference between the implementations is the amount of setup that is required for Twilio. While Twilio requires manually setting up a <mark style="color:blue;">CameraCapturerCompat</mark> and <mark style="color:blue;">LocalVideoTrack</mark> to specify your input sources, this is all handled internally within the VidyoClient SDK.

#### Turn the camera off

{% tabs %}
{% tab title="Twilio" %}

```java
localVideoTrack.enable(false);
```

However, you also have to manually hide the renderer.
{% endtab %}

{% tab title="Vidyo" %}
Vidyo’s implementation requires you to call a single API function to disable the local camera:

```java
vidyoConnector.setCameraPrivacy(true);
```

{% endtab %}
{% endtabs %}

#### Render remote user video

{% tabs %}
{% tab title="Twilio" %}
Twilio uses <mark style="color:blue;">onParticipantConnected</mark> and <mark style="color:blue;">onVideoTrackSubscribed</mark> event listeners to determine which remote videos to render.

```java
@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);
}
```

{% endtab %}

{% tab title="Vidyo" %}
Based on the Composite Renderer concept, Vidyo manages the video rendering part automatically, but the user can also register callbacks to listen to, for example, when a participant joins or leaves a room (later in this guide).
{% endtab %}
{% endtabs %}

### Mute/Unmute Microphone

{% tabs %}
{% tab title="Twilio" %}
Since Twilio API is track based, you have to disable the track individually:

```java
localAudioTrack.enable(false);
```

{% endtab %}

{% tab title="Vidyo" %}
Use the <mark style="color:blue;">setMicrophonePrivacy</mark> function to set the selected microphone state:

```java
vidyoConnector.setMicrophonePrivacy(true);
```

{% endtab %}
{% endtabs %}

### 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.

{% tabs %}
{% tab title="Twilio" %}

```java
Room.Listener interface:
- onConnected
- onReconnecting
- onReconnected
- onConnectFailure
- onDisconnected
- onParticipantConnected
- onParticipantDisconnected
[...]
```

```java
RemoteParticipant.Listener interface:
- onVideoTrackSubscribed
- onVideoTrackUnsubscribed
[...]
```

{% endtab %}

{% tab title="Vidyo" %}
Vidyo supports the concept of event listeners. Commonly used event listeners include:

* Connection Event Listener registered during conference join:

```java
Connector.IConnect interface: 

- onSuccess
- onDisconnected
- onFailure
```

* Device Event Listeners:

```java
vidyoConnector.registerLocalCameraEventListener(this);
```

```java
Connector.IRegisterLocalCameraEventListener interface:
- onLocalCameraAdded
- onLocalCameraRemoved
- onLocalCameraSelected
- onLocalCameraStateUpdated
```

```java
vidyoConnector.registerLocalMicrophoneEventListener(this);
```

```java
Connector.IRegisterLocalMicrophoneEventListener interface:
- onLocalMicrophoneAdded
- onLocalMicrophoneRemoved
- onLocalMicrophoneSelected
- onLocalMicrophoneStateUpdated
```

```java
vidyoConnector.registerLocalSpeakerEventListener(this);
```

```java
Connector.IRegisterLocalSpeakerEventListener interface:
- onLocalSpeakerAdded
- onLocalSpeakerRemoved
- onLocalSpeakerSelected
- onLocalSpeakerStateUpdated
```

* Participant Events Listener:

```java
vidyoConnector.registerParticipantEventListener(this);
```

```java
Connector.IRegisterParticipantEventListener interface: 
- onParticipantJoined
- onParticipantLeft
- onDynamicParticipantChanged
- onLoudestParticipantChanged
```

{% endtab %}
{% endtabs %}

### Leave session

To end the session, call the corresponding disconnect method:

{% tabs %}
{% tab title="Twilio" %}

```java
room.disconnect();
```

{% endtab %}

{% tab title="Vidyo" %}

```java
vidyoConnector.disconnect();
```

{% endtab %}
{% endtabs %}
