If you had a look at the Step 2 of WebRTC codelabs, You might notice that they have created a loop between the caller and the callee.

A single device acts as both the local peer and the remote peer thereby doing both offering and answering parts. Though it does not have any practical usage, it is better to pass through this step to have a deep understanding of how stuffs work.

Have a look at the code below to figure out how it is implemented.

If you take a look at the above code, We have 3 methods.

start() method essentially creates the local audio and video source and adds them to a SurfaceViewRenderer (this is provided by WebRTC so that we can draw our video frames directly to this view).

hangup() method is just a simple piece of code that clears off all the PeerConnection instances

call() is where all the fun happens.

We create two peer connection instances (one for local peer and another for remote peer). Once those peers are created, we make the local peer create an Offer that is set as its Local Description and also the Remote Description of the remote peer.

peer connection instances (one for local peer and another for remote peer). Once those peers are created, we make the local peer create an that is set as its Local Description and also the Remote Description of the remote peer. We then make the remote peer to create answer which is set as its Local Description and the local peer’s Remote Description.

We also have the onIceCandidateReceived() method whose work is to set the Ice candidates received from one peer to another peer.

You can take a look at Step-2 folder of the Git repository for the full working code of Loopback peerconnection.

All is going well now. Our app will be able to show your face to you, just like we did in the first part. Except that, this time our app is showing the face through a peer connection.

We got the basics working. We have two peers which transfer the offer and answer SDPs along with the candidates and now they both can transfer the data. But this is not the real case. Real people call each other and not themselves. Extending this solution to a real use case is easy. We just need a medium to transfer the SDPs and some STUN and TURN setup.

This has become too large for a single post. See you soon(hopefully!) with the grand finale where you will have a working Peer-to-Peer enabled video calling app.

Do press the green heart to show some love! For more droid-y posts, follow Adventurous Android!