Try Live Demo

Ant Media Server has native WebRTC Android and iOS SDKs. You can use WebRTC facilities in the Android Platform with the help of Ant Media Server’s Native WebRTC Android SDK. In this blog post, features of Webrtc Android SDK will be presented with a sample Android project which comes bundled with the SDK.

Ant Media Server WebRTC Android SDK

WebRTC Android SDK has the following main features:

  • Provides peer-to-peer WebRTC communication between Android devices and browsers by using Ant Media server as a signaling server.
  • Could publish WebRTC stream which could be played by other Android devices and browsers (mobile or desktop).
  • Could play WebRTC stream which is published by other Android devices and browsers (mobile or desktop).
  • Could join conference room that is created in Ant Media Server.

How to Use WebRTC SDK in Native Android App

Prerequisites for WebRTC Android SDK

WebRTC iOS and WebRTC Android SDKs are free to download. You can access them through this link on antmedia.io. If you’re an enterprise user, it will be also available for you to download on your subscription page. Anyway, after you download the SDK, you can just unzip the file. You could also obtain a trial version of Ant Media Server Enterprise Edition from here.

Run the Sample WebRTC Android App

After downloading the SDK, open and run the sample project in Android Studio.

Open WebRTC SDK

Select your project’s build.gradle file path and Click to the OK button.

Android Studio dashboard

PS: You need to set SERVER_ADDRESS parameter in MainActivity.java:

SERVER_ADDRESS parameter in MainActivity.java

Publish Stream from your Android

  • In MainActivity.java, you need to set webRTCMode parameter to IWebRTCClient.MODE-PUBLISH
  • In MainActivity.java, set the stream id to anything else then stream1 i.e streamTest1:

Building webrtc android app

  • Tap Start Publishing button on the main screen. After the clicking Start Publishing, the stream will be published on Ant Media Server.publishing page of Ant media server native android sdk
  • Then it will start Publishing to your Ant Media Server. You can go to the web panel of Ant Media Server(http://server_ip:5080) and watch the stream there. You can also quickly play the stream via https://your_domain:5443/WebRTCAppEE/player.html

Play Stream from your Android

  • Firstly, you need to set webRTCMode parameter to IWebRTCClient.MODE_PLAY in MainActivity.java.
  • Playing stream on your Android is almost the same as publishing. Before playing, make sure that there is a stream that is already publishing to the server with the same stream id in your streamId parameter (You can quickly publish to the Ant Media Server via https://your_domain:5443/WebRTCAppEE). For our sample, the stream id is still “streamTest1” in the image below. Then you just need to tap Start Playing button.WebRTC publishing page in Android SDK

P2P Communication with your Android

WebRTC Android SDK also supports P2P communication. As you guess, just set webRTCMode parameter to IWebRTCClient.MODE_JOIN.

P2P publish

When there is another peer is connected to the same stream id via Android, iOS or Web, then P2P communication will be established and you can talk to each other. You can quickly connect to the same stream id via https://your_domain:5443/WebRTCAppEE/peer.html

Join to Conference Room with your Android

WebRTC Android SDK also supports the Conference Room feature. You just need to change the launcher activity to ConferenceActivity.java activity:

Building conferencing room in webrtc android sdk

When there are other streams connected to the same stream id via Android, iOS, or Web, then a Conference Room will be established and you can talk to each other. You can quickly connect to the same stream id via https://your_domain:5443/WebRTCAppEE/conference.html

WebRTC Conference room in streaming sdk

How to develop a WebRTC Android App

We highly recommend using the sample project to get started with your application. Nevertheless, it’s good to know the dependencies and how it works. So that we’re going to tell you how to create a WebRTC Android app from Scratch. Let’s build a WebRTC Android example app.

Creating an Android Project

Open Android Studio and Create a New Android Project

Just Click File > New > New Project . Choose Empty Activity in the next window:

Creating an Android Streaming App Project

Click the Next button and a window should open as shown below for the project details. Fill out the form:

Webrtc android sample app

Click Finish and complete creating the project.

Import WebRTC Android SDK as Module to Android Project

After creating the project. Let’s import the WebRTC Android SDK to the project. For doing that click File > New > Import Module . Choose the directory of the WebRTC Android SDK and click the Finish button. Importing Native WebRTC Android SDK

If the module is not included in the project, add the module name into settings.gradle file as shown in the image below. Import module in setting.gradle

Add dependency to Android Project App Module

Right-click app, choose Open Module Settingsand click the Dependencies tab. Then a window should appear as below. Click the + button at the bottom and choose `Module Dependency“

Adding dependency to Android Project App Module

Choose WebRTC Native Android SDK and click the OK button

Adding dependency to webrtc android framework

The CRITICAL thing about that is You need to import a Module as an API as shown in the image above. It will look like as in the image below after adding the dependency:

android studio project structure page

Prepare the App for Streaming
  • Create a MainActivity.java Class and add a Button to your activity main layout. This is just simple Android App development, we don’t give details here. You can get lots of tutorials about that on the Internet.
  • Add permissions in Manifest file.

Open the AndroidManifest.xml and add below permissions between application and manifesttag

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Set permissions for the App

Implement MainActivity onCreate function

Open the MainActivity.java and implement it as below. You should change SERVER_URL according to your Ant Media Server address. Secondly, the third parameter in the last line of the code below is IWebRTCClient.MODE_PUBLISH that publishes the stream to the server. You can use IWebRTCClient.MODE_PLAY for playing stream and IWebRTCClient.MODE_JOIN for P2P communication. If token control is enabled, you should define tokenId parameter.

   /**
     * Change this address with your Ant Media Server address
     */
    public static final String SERVER_ADDRESS = "serverdomain.com:5080";

    /**
     * Mode can Publish, Play or P2P
     */
    private String webRTCMode = IWebRTCClient.MODE_PLAY;

    public static final String SERVER_URL = "ws://"+ SERVER_ADDRESS +"/WebRTCAppEE/websocket";
    public static final String REST_URL = "http://"+SERVER_ADDRESS+"/WebRTCAppEE/rest/v2";
    private CallFragment callFragment;

    private WebRTCClient webRTCClient;

    private Button startStreamingButton;
    private String operationName = "";
    private Timer timer;
    private String streamId;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set window styles for fullscreen-window size. Needs to be done before
        // adding content.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        //getWindow().getDecorView().setSystemUiVisibility(getSystemUiVisibility());

        setContentView(R.layout.activity_main);
        SurfaceViewRenderer cameraViewRenderer = findViewById(R.id.camera_view_renderer);
        SurfaceViewRenderer pipViewRenderer = findViewById(R.id.pip_view_renderer);

        startStreamingButton = (Button)findViewById(R.id.start_streaming_button);

        // Check for mandatory permissions.
        for (String permission : CallActivity.MANDATORY_PERMISSIONS) {
            if (this.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission " + permission + " is not granted", Toast.LENGTH_SHORT).show();
                return;
            }
        }

        if (webRTCMode.equals(IWebRTCClient.MODE_PUBLISH)) {
            startStreamingButton.setText("Start Publishing");
            operationName = "Publishing";
        }
        else  if (webRTCMode.equals(IWebRTCClient.MODE_PLAY)) {
            startStreamingButton.setText("Start Playing");
            operationName = "Playing";
        }
        else if (webRTCMode.equals(IWebRTCClient.MODE_JOIN)) {
            startStreamingButton.setText("Start P2P");
            operationName = "P2P";
        }

        this.getIntent().putExtra(EXTRA_CAPTURETOTEXTURE_ENABLED, true);
        this.getIntent().putExtra(EXTRA_VIDEO_FPS, 30);
        this.getIntent().putExtra(EXTRA_VIDEO_BITRATE, 2500);
        this.getIntent().putExtra(EXTRA_CAPTURETOTEXTURE_ENABLED, true);

        webRTCClient = new WebRTCClient( this,this);

        //webRTCClient.setOpenFrontCamera(false);

        streamId = "stream1";
        String tokenId = "tokenId";

        webRTCClient.setVideoRenderers(pipViewRenderer, cameraViewRenderer);

       // this.getIntent().putExtra(CallActivity.EXTRA_VIDEO_FPS, 24);
        webRTCClient.init(SERVER_URL, streamId, webRTCMode, tokenId, this.getIntent());
    }

    public void startStreaming(View v) {
        if (!webRTCClient.isStreaming()) {
            ((Button)v).setText("Stop " + operationName);
            webRTCClient.startStream();
        }
        else {
            ((Button)v).setText("Start " + operationName);
            webRTCClient.stopStream();
        }
    }

Create activity_main.xml layout

Create an activity_main.xml layout file and add below codes.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/camera_view_renderer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <org.webrtc.SurfaceViewRenderer
        android:id="@+id/pip_view_renderer"
        android:layout_height="144dp"
        android:layout_width="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:id="@+id/start_streaming_button"
        android:onClick="startStreaming"
        android:layout_gravity="bottom|center"/>

    <FrameLayout
        android:id="@+id/call_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </FrameLayout>

How to Publish

We need to change some codes in onCreate. As a result, following code snippets just publish the stream on your server with streamId: ‘stream1’.

  • You need to set webRTCMode toIWebRTCClient.MODE_PUBLISH.
    private String webRTCMode = IWebRTCClient.MODE_PUBLISH;

How to Play

Playing a Stream is almost the same as publishing. We just need to change some codes in onCreate. As a result, the following code snippets just play the stream on your server with streamId: ‘stream1’. Make sure that, before you try to play, you need to publish a stream to your server with having stream id ‘stream1’

  • You need to set webRTCMode toIWebRTCClient.MODE_PLAY.
    private String webRTCMode = IWebRTCClient.MODE_PLAY;

How to use Data Channel in WebRTC Android SDK

Data channels can be used in Ant Media Server WebRTC Android SDK. In order to use Data Channel, make sure that it’s enabled both server-side and mobile.

Before initialization of webRTCClient you need to:

  • Set your Data Channel observer in the WebRTCClient object like this:
    webRTCClient.setDataChannelObserver(this);
  • Enable data channel communication by putting following key-value pair to your Intent before initialization of WebRTCClient with it:
    this.getIntent().putExtra(EXTRA_DATA_CHANNEL_ENABLED, true);

Then your Activity is ready to send and receive data.

  • To send data, call sendMessageViaDataChannel method of WebRTCClient and pass the raw data like this on click of a button:
    webRTCClient.sendMessageViaDataChannel(buf);

How to use Webrtc Data Channel

How to use the Conference Room in WebRTC Android SDK

Ant Media Server also supports ConferenceRoom feature in WebRTC Android SDK. You need to initialize ConferenceManager.

    private ConferenceManager conferenceManager;

    conferenceManager = new ConferenceManager(
                this,
                this,
                getIntent(),
                MainActivity.SERVER_URL,
                roomId,
                publishViewRenderer,
                playViewRenderers,
                streamId,
                this
     );

Check also -> https://github.com/ant-media/Ant-Media-Server/wiki/WebRTC-Conference-Call

We hope this tutorial will be helpful to understand how to use WebRTC Android SDK for you, if you have any questions, just send an email to contact@antmedia.io 🙂

You may also want to check out What is Transcoding? Why Is It Important for Streaming? and What is HLS Streaming Protocol? blog posts.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published.