Video Calling using WebView and PeerJS on Android

Sk Ahron
ProAndroidDev
Published in
4 min readDec 8, 2022

--

In this digital world, we want to stay connected with our friends and family.

The demand for video calling apps is increasing day by day.

we can build apps to stay connected using WebRTC.

The flow of the Article:

  1. What are WebRTC and PeerJS?
  2. Basics about WebRTC.
  3. Developing Video Calling App using Peer.js and WebViews in android.

What are WebRTC and PeerJS?

WebRTC stands for Web Real-Time Communication. WebRTC technology helps you to exchange video, audio, and arbitrary data with peers. WebRTC is supported in Web Browsers and native devices for native devices, you need to add webrtc libraries.

PeerJS is simply a wrapper around the browser’s webrtc implementation and using this we can add video and audio streaming support very easily. It abstracts all the hard tasks which we have to handle. We will be using WebViews for our purpose to call javascript code and rendering HTML.

Basics about WebRTC.

WebRTC works by establishing connections between peer to peer by using a set of protocols.

  1. Signaling: Signaling process helps set up and control the communication session. Signaling uses SDP(Session Description Protocol) protocol which is in plain text format that contains data about. a) Locations of peers(IP address of the peers). b) media type
  2. Connecting: In WebRTC, connections happen in a Peer-to-Peer manner not a client-server. It could be tough to connect two peers may be because of incompatibilities. This connection problem can be solved by using ICE, TURN, and STUN servers.
    a). ICE stands for Interactive Connectivity Establishment. It helps to find the optimal path to connect two peers so that data transfer can happen optimally.
    b). STUN stands for Session Traversal Utilities for NAT. It is a standard method used by NAT traversal. WebRTC client requests the STUN server to get the Public IP address.
    c). TURN stands for Traversal Using Relays around NAT. When it is not possible to use STUN. then services have to relay media streams through this server.
  3. Securing Data Exchange: Webrtc ensures security. It encrypts the data which is exchanged between peers. It uses two protocols SRTP and DTLS.
    a). SRTP stands for Secure Real-Time Transport Protocol. It is a secure version of RTP. The protocol provides encryption, confidentiality, message authentication, and replay protection to your transmitted audio and video traffic.
    b). DTLS stands for Datagram Transport Layer Security. DTLS is a protocol used to secure datagram-based communications. Ordered Delivery is not guaranteed and it has less latency.
  4. Exchanging Data: Using WebRTC we can transfer data, audio, and video streams. We use RTP and RTCP protocols for data transfer.
    a). RTP stands for the real-time transport protocol. It helps for real-time transmission of media streams.
    b). RTCP stands for real-time control protocol. The messages that can control the transmission and quality of data as well as also allow the recipients so they can send feedback to the source. A protocol designed for this purpose is RTCP.

Developing Video Calling App using Peer.js and WebViews in android.

Let’s start working on it.

Here is the GitHub link.

Step 1: Create a new Android Project.

Step 2: You need to add 4 files to your android project

Here is the link.

This is the place where you have to add files.

VideoCallDemo/app/src/main/assets/

Step 3: You need to add these permissions in Manifest.xml.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

I am using this dependency for handling permissions in jetpack compose.

implementation "com.google.accompanist:accompanist-permissions:0.27.1"

Step 4: We will create an XML file to use webView.

Now, we will add our XML file to our composable.

Step 5: Now, create UI for the mic, video, and end call.

Step 6: Now, the VideoCallCompose

Now, as you can see there is a method we are calling callJavaScriptFunction() using this we will call js methods like toggleAudio, toggleVideo, startCall, and disconnect.

We are loading the call.html file in webView.

You need to pass uniqueId to init() javascript method. you need to call startCall() javascript method and pass the uniqueId of the remote peer for that you need to store the uniqueId in a backend and fetch that id from there.

For simplicity purposes. I will not store it in the backend as the main purpose is to show how to use webView and peer.js in android.

Using the UI.

First, click on Create Room. It will generate a unique Id.

Now, on the second phone in the text field put that peer id and click Join Room.

I have not added any validation in the UI. So try adding the correct id.

Now, Join/Create a Room.

As You can see I have connected with two devices and video calling is working.

Thank You For Reading.

--

--

Hi, I am Shaik Ahron. I am enthusiastic about mobile development. I am open to freelancing, contract roles, and full-time opportunities.