How to Use WebRTC SDK in Native Android App?

  • Provides peer to peer WebRTC communication between Android devices and browsers by using Ant Media server as a signalling 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.

Prerequisites for WebRTC Android SDK

WebRTC iOS and Android SDK’s 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 in 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.

Publish Stream from your Android

  • InMainActivity.java, you need to setwebRTCModeparameter toIWebRTCClient.MODE-PUBLISH
  • InMainActivity.java, set the stream id to anything else thenstream1i.estreamTest1:
  • TapStart Publishingbutton on the main screen. After the clickingStart Publishing, stream will be published on Ant Media Server.
  • 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 viahttps://your_domain:5443/WebRTCAppEE/player.html

Play Stream from your Android

  • Firstly, you need to setwebRTCModeparameter toIWebRTCClient.MODE_PLAYinMainActivity.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 same stream id in yourstreamIdparameter (You can quickly publish to the Ant Media Server viahttps://your_domain:5443/WebRTCAppEE). For our sample, stream id is still "streamTest1" in the image below. Then you just need to tapStart Playingbutton.

P2P Communication with your Android

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

Join Conference Room with your Android

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

Develop a WebRTC Android App

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

Creating Android Project

Open Android Studio and Create a New Android Project

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

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 plays 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

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

  • 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);
  • 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 Conference Room

Ant Media Server also supports ConferenceRoom feature. You need to initialize ConferenceManager.

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ant Media Server

Ant Media Server

Ant Media Server, open source software, supports publishing live streams with WebRTC and RTMP. It supports HLS(HTTP Live Streaming) and MP4 as well.