

# Amazon Kinesis Video Streams WebRTC SDK for Android
Android SDK

The following step-by-step instructions describe how to download, build, and run the Kinesis Video Streams with WebRTC SDK for Android and its corresponding samples.

**Note**  
Amazon Kinesis Video Streams doesn't support IPv6 addresses on Android. See more information about [disabling IPv6 on your Android device](https://www.cactusvpn.com/tutorials/how-to-disable-ipv6-on-android/).

## Download the SDK


To download the WebRTC SDK in Android, run the following command:

```
$ git clone https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-android.git
```

## Build the SDK


To build the WebRTC SDK in Android, complete the following steps:

1. Import the Android WebRTC SDK into the Android Studio integrated development environment (IDE) by opening `amazon-kinesis-video-streams-webrtc-sdk-android/build.gradle` with **Open as Project**. 

1. If you open the project for the first time, it automatically syncs. If not - initiate a sync. When you see a build error, choose to install any required SDKs by choosing **Install missing SDK package(s)**, then choose **Accept** and complete the install.

1. Configure Amazon Cognito (user pool and identity pool) settings. For details steps, see [Configure Amazon Cognito for the SDK](#build-sdk-android-cognito). This generates authentication and authorization settings required to build the Android WebRTC SDK.

1. In your Android IDE, open `awsconfiguration.json` (from `src/main/res/raw/`). The file looks like the following:

   ```
   {
     "Version": "1.0",
     "CredentialsProvider": {
       "CognitoIdentity": {
         "Default": {
           "PoolId": "REPLACE_ME",
           "Region": "REPLACE_ME"
         }
       }
     },
     "IdentityManager": {
       "Default": {}
     },
     "CognitoUserPool": {
       "Default": {
         "AppClientSecret": "REPLACE_ME",
         "AppClientId": "REPLACE_ME",
         "PoolId": "REPLACE_ME",
         "Region": "REPLACE_ME"
       }
     }
   }
   ```

   Update `awsconfiguration.json` with the values generated by running the steps in [Configure Amazon Cognito for the SDK](#build-sdk-android-cognito).

1. Make sure your Android device is connected to the computer where you're running the Android IDE. In the Android IDE, select the connected device and then build and run the WebRTC Android SDK.

   This step installs an app called `AWSKinesisVideoWebRTCDemoApp` on your Android device. Using this app, you can verify live WebRTC audio/video streaming between mobile, web and IoT device clients.

## Run the sample application


****

Complete the following steps:

1. On your Android device, open **AWSKinesisVideoWebRTCDemoApp** and log in using either a new (by creating it first) or an existing Amazon Cognito account.

1. In **AWSKinesisVideoWebRTCDemoApp**, navigate to the **Channel Configuration** page and either create a new signaling channel or choose an existing one.
**Note**  
Currently, using the sample application in this SDK, you can only run one signalling channel in **AWSKinesisVideoWebRTCDemoApp**.

1. Optional: choose a unique **Client Id** if you want to connect to this channel as a viewer. Client ID is required only if multiple viewers are connected to a channel. This helps channel's master identify respective viewers.

1. Choose the AWS Region and whether you want to send audio or video data, or both.

1. To verify peer-to-peer streaming, do any of the following:
**Note**  
Ensure that you specify the same signaling channel name, AWS region, viewer ID, and the AWS account ID on all clients that you're using in this demo.
   + Peer-to-peer streaming between two Android devices: master and viewer
     + Using procedures above, download, build, and run the Android WebRTC SDK on two Android devices. 
     + Open **AWSKinesisVideoWebRTCDemoApp** on one Android device in master mode (choose **START MASTER**) to start a new session (signaling channel).
**Note**  
Currently, there can only be one master for any given signaling channel.
     + Open **AWSKinesisVideoWebRTCDemoApp** on your second Android device in viewer mode to connect to the signaling channel (session) started in the step above (choose **START VIEWER**).

       Verify that the viewer can see master's audio/video data.
   + Peer-to-peer streaming between the embedded SDK master and an Android device viewer
     + Download, build, and run the [Amazon Kinesis Video Streams with WebRTC SDK in C for embedded devices](kvswebrtc-sdk-c.md) in master mode on a camera device. 
     + Using procedures above, download, build, and run the Android WebRTC SDK on an Android device. Open **AWSKinesisVideoWebRTCDemoApp** on this Android device in viewer mode and verify that the viewer can see the embedded SDK master's audio/video data.
   + Peer-to-peer streaming between Android device as master and web browser as viewer
     + Using procedures above, download, build, and run the Android WebRTC SDK on an Android device. Open **AWSKinesisVideoWebRTCDemoApp** on this Android device in master mode (choose **START MASTER**) to start a new session (signaling channel).
     + Download, build, and run the [Amazon Kinesis Video Streams with WebRTC SDK in JavaScript for web applications](kvswebrtc-sdk-js.md) as viewer and verify that the viewer can see the Android master's audio/video. 

## Configure Amazon Cognito for the SDK


### Prerequisites

+ We recommend [Android Studio](https://developer.android.com/studio/index.html) for examining, editing, and running the application code. We recommend using the latest stable version.
+ In the sample code, you provide Amazon Cognito credentials.

Follow these procedures to set up an Amazon Cognito user pool and identity pool.

### Set up a user pool


**To set up a user pool**

1. Sign in to the [Amazon Cognito console](https://console.aws.amazon.com/cognito/home) and verify the region is correct.

1. In the navigation on the left choose **User pools**.

1. In the **User pools** section, choose **Create user pool**.

1. Complete the following sections:

   1. **Step 1: Configure sign-in experience** - In the **Cognito user pool sign-in options** section, select the appropriate options.

      Select **Next**.

   1. **Step 2: Configure security requirements** - Select the appropriate options.

      Select **Next**.

   1. **Step 3: Configure sign-up experience** - Select the appropriate options.

      Select **Next**.

   1. **Step 4: Configure message delivery** - Select the appropriate options.

      In the **IAM role selection** field, select an existing role or create a new role.

      Select **Next**.

   1. **Step 5: Integrate your app** - Select the appropriate options.

      In the **Initial app client** field, choose **Confidential client**.

      Select **Next**.

   1. **Step 6: Review and create** - Review your selections from the previous sections, then choose **Create user pool**.

1. On the **User pools** page, select the pool that you just created.

   Copy the **User pool ID** and make note of this for later. In the `awsconfiguration.json` file, this is `CognitoUserPool.Default.PoolId`.

1. Select the **App integration** tab and go to the bottom of the page.

1. In the **App client list** section, choose the **App client name** you just created.

   Copy the **Client ID** and make note of this for later. In the `awsconfiguration.json` file, this is `CognitoUserPool.Default.AppClientId`.

1. Show the **Client secret** and make note of this for later. In the `awsconfiguration.json` file, this is `CognitoUserPool.Default.AppClientSecret`.

### Set up an identity pool


**To set up an identity pool**

1. Sign in to the [Amazon Cognito console](https://console.aws.amazon.com/cognito/home) and verify the region is correct.

1. In the navigation on the left choose **Identity pools**.

1. Choose **Create identity pool**.

1. Configure the identity pool.

   1. **Step 1: Configure identity pool trust** - Complete the following sections:
      + **User access** - Select **Authenticated access**
      + **Authenticated identity sources** - Select **Amazon Cognito user pool**

      Select **Next**.

   1. **Step 2: Configure permissions** - In the **Authenticated role** section, complete the following fields:
      + **IAM role** - Select **Create a new IAM role**
      + **IAM role name** - Enter a name and make note of it for a later step.

      Select **Next**.

   1. **Step 3: Connect identity providers** - In the **User pool details** section complete the following fields: 
      + **User pool ID** - Select the user pool you created earlier.
      + **App client ID** - Select the app client ID you created earlier.

      Select **Next**.

   1. **Step 4: Configure properties** - Type a name in the **Identity pool name** field.

      Select **Next**.

   1. **Step 5: Review and create** - Review your selections in each of the sections, then select **Create identity pool**.

1. On the **Identity pools** page, select your new identity pool.

   Copy the **Identity pool ID** and make note of this for later. In the `awsconfiguration.json` file, this is `CredentialsProvider.CognitoIdentity.Default.PoolId`.

1. Update the permissions for the IAM role.

   1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

   1. In the navigation on the left, choose **Roles**.

   1. Find and select the role you created above.
**Note**  
Use the search bar, if needed.

   1. Select the attached permissions policy.

      Select **Edit**.

   1. Select the **JSON** tab and replace the policy with the following:

      ```
      {
          "Version": "2012-10-17",		 	 	 
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "cognito-identity:*",
                      "kinesisvideo:*"
                  ],
                  "Resource": [
                      "*"
                  ]
              }
          ]
      }
      ```

      Select **Next**.

   1. Select the box next to **Set this new version as the default** if it isn't already selected.

      Select **Save changes**.