Using Kurento Room Client with webRTC on Android

Posted By : Daljeet Singh | 14-Dec-2017

Kurento Room Client along with WebRTC helps us transfer data between two peers.Using the two libraries,it is possible to make video calls between two connected peers.Here are the basic steps involved in setting up the webrtc library with the kurento room client library:

 

1. First we add the webrtc dependency in the build.gradle of our app:

 

 

Dependencies {
compile 'fi.vtt.nubomedia:webrtcpeer-android:[1.1.2]'		
}

 

 

 

2. NBMWebRTCPeer is essential for setting up a WebRTC media session.So we make a class that implements NBMWebRTCPeer.Observer interface:

 

 

 
public class WebRTCApp implements NBMWebRTCPeer.Observer {
}

 

3. A configuration object has to be created before creating the NBMWebRTCPeer object:

 

 

mediaConfiguration = new NBMMediaConfiguration();

 

4. The NBMWebRTCPeer object is initialized by using the media configuration object created in the previous step:

 

 

NBMWebRTCPeer nbmWebRTCPeer = new NBMWebRTCPeer(mediaConfiguration, this,
localRender, myObserver)
nbmWebRTCPeer.initialize();

 

5. We can offer our video to the server by using the generateOffer method on our nbmWebRTCPeer instance:

 

 

nbmWebRTCPeer.generateOffer("local", true);

where the first parameter is the connectionid assigned to the local video connection while the second parameters tells server that we wish to offer our local media.

 

6. After the generation of the offer,a onLocalSdpOfferGenerated callback is triggered:

 

 

private boolean isVideoPublished = false;
public void onLocalSdpOfferGenerated(SessionDescription localSdpOffer)
{
    if (!isVideoPublished) {
	isVideoPublished = true;
    }else{
} 
}

 

 

 

The code to send(sendPublishVideo) or receive(sendReceiveVideoFrom) remote user video can be placed inside the onLocalSdpOfferGenerated block.

 

7. In order to deal with server signalling we can use the kurento-room-client-android library,which can be used by adding the following dependecy in the build.gradle of the app :

 

 

Dependencies{
compile 'fi.vtt.nubomedia:kurento-room-client-android:[1.1.2]'
}

 

8. The KurentoRoomAPI is the main class that is used for handling room connectivity between peers.Our following class can be used to implement the KurentoRoomAPI :

 

 

public class MainActivity extends Activity implements RoomListener {

    private LooperExecutor executor;
    private static KurentoRoomAPI kurentoRoomAPI;
@Override
public void onCreate(Bundle savedInstanceState) {
    executor = new LooperExecutor();
    executor.requestStart();
    String wsRoomUri = "RoomUrl";
    kurentoRoomAPI = new KurentoRoomAPI(executor, wsUri, this);
    kurentoRoomAPI.connectWebSocket();
}
    @Override
    public void onRoomResponse(RoomResponse response) {    }

    @Override
    public void onRoomError(RoomError error) {    }

    @Override
    public void onRoomNotification(RoomNotification notification) {    }

    @Override
    public void onRoomConnected() {
	kurentoRoomAPI.sendJoinRoom("Username", "RoomName", true, 123);
	//parameters are username,roomname,data channels should be enabled,index no to track response
    }
    @Override
    public void onRoomDisconnected() {    }
} 

 

 

 

The KurentoRoomAPI has been created and a websocket opened in the onCreate method of the class.After the connection of websocket,onRoomConnected is triggered,which helps the user join a room immediately.

 

9. As the webrtcpeer-android library does not contain information on rooms,method sendOnIceCandidate is used to bridge the kurento-room-client-android with it.We will use onIceCandidate method of our NBMWebRTCPeer to transfer information on how to connect to a client, which will be triggered each time the ICE(Interactive Connectivity Establishment) framework finds some local candidates.

 

 

boolean IceSent = false;

@Override
public void onIceCandidate(IceCandidate iceCandidate, NBMPeerConnection nbmPeerConnection) {
    if (!IceSent) {
        kurentoRoomAPI.sendOnIceCandidate("Username", iceCandidate.sdp, iceCandidate.sdpMid, Integer.toString(iceCandidate.sdpMLineIndex), 126);
        IceSent = true;
    } else {
        kurentoRoomAPI.sendOnIceCandidate("MyRoomPeer", iceCandidate.sdp, iceCandidate.sdpMid, Integer.toString(iceCandidate.sdpMLineIndex), 126);
    }
}

 

 

Here the first call to sendOnIceCandidate stores the creation of our own connectivity ticket,while the second call handles the peer's connectivity.

 

 

 

 

About Author

Author Image
Daljeet Singh

Daljeet has experience developing android applications across a range of domains such as Cryptocurrency, Travel & Hotel Booking, Video Streaming and e-commerce. In his free time, he can be found playing/watching a game of football or reading up on either

Request for Proposal

Name is required

Comment is required

Sending message..