How can I set up WebRTC in Kotlin for Android Studio? I couldn't find a working solution. Please provide detailed info.
Asked
Active
Viewed 4,258 times
2
-
https://github.com/IhorKlimov/Android-WebRtc – Shweta Chauhan Apr 13 '20 at 12:20
2 Answers
4
Many of the examples online are using the old WebRTC api for android. There have been many changes in the past few years. The following example is in Java but it should be similar to Kotlin.
To start off with, you need to request permissions to camera and audio. Then perhaps set your views using findviewbyid, then add your ice servers to an array:
List<PeerConnection.IceServer> peerIceServers = new ArrayList<>();
peerIceServers.add(PeerConnection.IceServer.builder("stun:stun1.l.google.com:19302").createIceServer());
then initialize your peer connection factory.
DefaultVideoEncoderFactory defaultVideoEncoderFactory = new DefaultVideoEncoderFactory(eglBase.getEglBaseContext(), true, true);
DefaultVideoDecoderFactory defaultVideoDecoderFactory = new DefaultVideoDecoderFactory(eglBase.getEglBaseContext());
PeerConnectionFactory.InitializationOptions initializationOptions =
PeerConnectionFactory.InitializationOptions.builder(this)
.createInitializationOptions();
PeerConnectionFactory.initialize(initializationOptions);
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
factory = PeerConnectionFactory.builder()
.setVideoEncoderFactory(defaultVideoEncoderFactory)
.setVideoDecoderFactory(defaultVideoDecoderFactory)
.setOptions(options)
.createPeerConnectionFactory();
Then you can initialize camera and audio and your signalling client.
Looking at this example in Java may help:

Philip
- 638
- 1
- 8
- 22
0
It's been too late. Now, we have many tutorials for WebRTC android.
You need to follow below steps
- Create and initialize PeerConnectionFactory
- Create a VideoCapturer instance which uses the camera of the device
- Create a VideoSource from the Capturer Create a VideoTrack from the source
- Create a video renderer using a SurfaceViewRenderer view and add it to the VideoTrack instance
- Initialize Peer connections
Start streaming Video
private fun initializePeerConnectionFactory() { //Initialize PeerConnectionFactory globals. val initializationOptions = InitializationOptions.builder(this).createInitializationOptions() PeerConnectionFactory.initialize(initializationOptions) //Create a new PeerConnectionFactory instance - using Hardware encoder and decoder. val options = PeerConnectionFactory.Options() val defaultVideoEncoderFactory = DefaultVideoEncoderFactory(rootEglBase?.eglBaseContext, /* enableIntelVp8Encoder */true, /* enableH264HighProfile */true) val defaultVideoDecoderFactory = DefaultVideoDecoderFactory(rootEglBase?.eglBaseContext) factory = PeerConnectionFactory.builder() .setOptions(options) .setVideoEncoderFactory(defaultVideoEncoderFactory) .setVideoDecoderFactory(defaultVideoDecoderFactory) .createPeerConnectionFactory() }
Here, is full demo available but in Java - Example

Shweta Chauhan
- 6,739
- 6
- 37
- 57