You can use WebRTC facilities in Android Environment with the help of Native Android WebRTC SDK.

This SDK,  simplified and adapted version of famous AppRTC project, provides peer to peer WebRTC communication between Android devices and browsers by using Ant Media server as a signaling server. This document describes first how to run the Demo App and then gives more detailed information about SDK.

Run the Demo App

Prerequisites

In order to use WebRTC Android SDK, you need WebRTC Android SDK for sure and Ant Media Server Enterprise Edition. Please contact us at contact@antmedia.io.  We can provide WebRTC Android SDK and Enterprise Edition for trying or personal use.

Start the Ant Media Server Enterprise Edition

Extract the Android Media Server Enterprise Edition and start the server with start.sh command in the terminal

cd /path/to/ant-media-server
./start.sh

Open the WebRTC Android Demo in Android Studio

  • Open the project in Android Studio
  • Install the Android Demo App to your Device
  • Click Settings button on the right top

  • Scroll Down to Screen until you see Room server URL and click that item

  • Enter the Ant Media Server Enterprise Edition Server IP to the dialog and Click OK

 

  • Close the app and go to the App Permissions from System’s Application Manger and give all permissions to Android AppRTC App

  • Open the AppRTC app again and Enter a room name like “room1” and press phone icon

  • AppRTC should connect to the Ant Media Server and waits for the other peer to connect

  • The other peer should connect to same room in order to create a peer to peer connection.
    You may install the AppRTC to any other android device and connect to room1 or
    you may go to the http://SERVER_IP:5080/WebRTCAppEE/peer.html, write “room1” in the textbox and click “join”  on the web page.

  • Peer to Peer Connection should be established and you should see and talk to other peer.

More Information about Android WebRTC SDK

Take a look at the CallActivity class to see the sample usage of the Android SDK.

Modes

There are 3 types of broadcast scenarios, therefore, operating modes.

  • Peer Connection (Two nodes connect to each other, 1-1 connection)
  • Publish (One node publishes, 1-N connection)
  • Play (Other nodes play broadcast from publisher,1-N connection)

According to the projects or needs, the developer can implement one or all modes in their applications.

Configuration Parameters

In order to create WebSocket connection, two groups of parameters should be implemented successfully. These are minimal and mandatory configurations to establish a connection.
Except them, the developer can set other optional functional and GUI parameters.

Such as:

setCam2 (param); //boolean
setVideoResolution (param);
setVideoCodec (param);

Signaling Parameters

The first group of parameters is signaling parameters. These parameters consist of below variables;

public SignalingParameters(List iceServers, boolean initiator,
String clientId, String wssUrl, String wssPostUrl, 
SessionDescription offerSdp,List iceCandidates) {
this.iceServers = iceServers;
this.initiator = initiator;
this.clientId = clientId;
this.wssUrl = wssUrl;
this.wssPostUrl = wssPostUrl;
this.offerSdp = offerSdp;
this.iceCandidates = iceCandidates;
} 

In fact, the developer does not need to define any of these parameters. The SDK creates and defines required parameters for connection in the backend.

The only parameter that developer may need to define is Stun Server parameter. This parameter is set as “stun:stun.l.google.com:19302” by default, if it is needed to set this value, just call the below method:

appRtcClient.setStunServerUri(stunServerUri);

Connection Parameters

The second group of parameters is Connection Parameters which are;

  • streamId (Broadcast name)
  • url (WebSocket Url of Server)
  • mode (Operating Modes)

WebSocket server URLs may change according to communication medium, for example in secure environment, URL starts with “wss” instead of “ws” also port number changes to “8082” by default it is “8081”

Also “join”,”publish”,”play” mode values should be used for Peer, Publish and Play Operating modes.

WebRTCClient Implementation

First, you need to create a webRTCClient instance in your application:

 public WebRTCClient(boolean loopback, String roomId, IWebRTCListener webRTCListener, Context context) 
this(loopback, roomId, webRTCListener, context);   }
 webRTCClient = new WebRTCClient(loopback, roomId, this, Activity.this);

loopback: Should be false in the default application
roomId: This is stream id
this: IWebRTCListener object
Activity.this: Your application activity

Then, create UI elements in your activity.

 
SurfaceViewRenderer pipRenderer = findViewById(R.id.pip_video_view);  //This is for your own display
SurfaceViewRenderer fullscreenRenderer = findViewById(R.id.fullscreen_video_view);  //This is for your remote display
callFragment = new CallFragment();
hudFragment = new HudFragment();
webRTCClient.setVideoRenderers(pipRenderer, fullscreenRenderer);
callFragment.setCallEvents(webRTCClient);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.call_fragment_container, callFragment);
ft.add(R.id.hud_fragment_container, hudFragment);
ft.commit();

Finally, start the stream.

webRTCClient.startStream(url, roomId, mode);

url: WebSocket URL
roomId: Stream Id
mode: Peer, Publish or Play

We hope this tutorial will be helpful for you, if you have any question, just send an email to contact@antmedia.io 🙂

Categories: Tutorial

%d bloggers like this: