Repository: amrfarid140/webrtc-android-codelab Branch: master Commit: c16bea358c3f Files: 41 Total size: 65.2 KB Directory structure: gitextract_11a2yhds/ ├── .gitignore ├── README.md ├── mobile/ │ ├── app/ │ │ ├── .gitignore │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src/ │ │ ├── androidTest/ │ │ │ └── java/ │ │ │ └── me/ │ │ │ └── amryousef/ │ │ │ └── webrtc_demo/ │ │ │ └── ExampleInstrumentedTest.kt │ │ ├── main/ │ │ │ ├── AndroidManifest.xml │ │ │ ├── java/ │ │ │ │ └── me/ │ │ │ │ └── amryousef/ │ │ │ │ └── webrtc_demo/ │ │ │ │ ├── AppSdpObserver.kt │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── PeerConnectionObserver.kt │ │ │ │ ├── RTCClient.kt │ │ │ │ ├── SignallingClient.kt │ │ │ │ └── SignallingClientListener.kt │ │ │ └── res/ │ │ │ ├── drawable/ │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── drawable-v24/ │ │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── layout/ │ │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26/ │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ └── values/ │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test/ │ │ └── java/ │ │ └── me/ │ │ └── amryousef/ │ │ └── webrtc_demo/ │ │ └── ExampleUnitTest.kt │ ├── build.gradle │ ├── gradle/ │ │ └── wrapper/ │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties │ ├── gradle.properties │ ├── gradlew │ ├── gradlew.bat │ ├── local.properties │ └── settings.gradle └── server/ ├── .gitignore ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── resources/ │ ├── application.conf │ └── logback.xml ├── settings.gradle └── src/ └── me/ └── amryousef/ └── Application.kt ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.iml .gradle /local.properties /.idea /.idea/navEditor.xml /.idea/assetWizardSettings.xml .DS_Store /build /captures .externalNativeBuild .cxx /.idea/ mobile/\.idea/caches/ mobile/\.idea/libraries/ mobile/\.idea/ ================================================ FILE: README.md ================================================ # WebRTC for Android This repo serves as a how-to guide for implementing basic video conferencing with WebRTC. It includes: - An Android mobile app - A bare-bones signalling server based on WebSocket built with Ktor. It's based on official WebRTC native library version **`1.0.27771`** --- ## What is WebRTC The [official](https://webrtc.org/) description "WebRTC is a free, open project that provides browsers and mobile applications with Real-Time Communications (RTC) capabilities via simple APIs. The WebRTC components have been optimised to best serve this purpose." Simply, it's a cross-platform API that allows developers to implement peer-to-peer real-time communication. Imagine an API that allows you to send voice, video and/or data (text, images...etc) across mobile apps and web apps. --- ## How does it work (The simple version) You have a signalling server that coordinates the initiation of the communication. Once the peer-to-peer connection is established, the signalling server is out of the equation. ![Simple Signalling architecture](./docs/simple_arch.png "Simple Signalling architecture") (A) prepares what's called SDP - [Session Description Protocol](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#SDP) - which we will call "offer". (A) sends this offer to the signalling server to request to be connected to (B). The signalling server then sends this "offer" to (B). (B) receives the offer and it will create an SDP of its own and send it back to the signalling server. We will call it "answer". The signalling server then send this "answer" to (A). A peer-to-peer connection is then created to exchange data. It's not magic. Something really important is happening is the background, both (A) & (B) are also exchanging public IP addresses. This is done through ICE - [Interactive Connectivity Establishment](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#ICE) - which uses a 3rd party server to fetch the public IP address. Those servers are known as [STUN](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#STUN) or [TURN](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Protocols#TURN). ![ICE exchange](./docs/ice.png "ICE exchange") (A) starts sending "ICE packets" from the moment it creates the "offer". Similarly, (B) starts sending the "answer". --- ## Getting a local video stream *Branch [step/local-video](https://github.com/amrfarid140/webrtc-android-codelab/tree/step/local-video)* Let's label the device we are working on as a "Peer". We need to setup its connection to start communication. WebRTC library has `PeerConnectionFactory` that creates the `PeerConnection` for you. However, we need to `initialize` and `configure` this factory first. ### Initialising `PeerConnectionFactory` First we need to say that we need to trace what's happening in the background then specify which features we want the Native library to turn on. In our case we want `H264` video format. ```java val options = PeerConnectionFactory.InitializationOptions.builder(context) .setEnableInternalTracer(true) .setFieldTrials("WebRTC-H264HighProfile/Enabled/") .createInitializationOptions() PeerConnectionFactory.initialize(options) ``` ### Configuring `PeerConnectionFactory` Now we can use `PeerConnectionFactory.Builder` to build an instance of `PeerConnectionFactory`. When building `PeerConnectionFactory` it's crucial to specify the video codecs you are using. In this sample, we will be using the default video codecs. In addition, we will be disabling encryption. ```java val rootEglBase: EglBase = EglBase.create() PeerConnectionFactory .builder() .setVideoDecoderFactory(DefaultVideoDecoderFactory(rootEglBase.eglBaseContext)) .setVideoEncoderFactory(DefaultVideoEncoderFactory(rootEglBase.eglBaseContext, true, true)) .setOptions(PeerConnectionFactory.Options().apply { disableEncryption = true disableNetworkMonitor = true }) .createPeerConnectionFactory() ``` ### Setting the video output Native WebRTC library relies on `SurfaceViewRenderer` view to output the video data. It's a `SurfaceView` that is setup to work will the callbacks of other WebRTC functionalities. ```xml ``` We will also need to mirror the video stream we are providing and enable hardware acceleration. ```java local_view.setMirror(true) local_view.setEnableHardwareScaler(true) local_view.init(rootEglBase.eglBaseContext, null) ``` ### Getting the video source The video source is simply the camera. Native WebRTC library has this handy helper - `Camera2Enumerator` - which allows use to fetch the front facing camera. ```java Camera2Enumerator(context).run { deviceNames.find { isFrontFacing(it) }?.let { createCapturer(it, null) } ?: throw IllegalStateException() } ``` once we have the front facing camera we can create a `VideoSource` from the `PeerConnectionFactory` and `VideoTrack` then we attached our `SurfaceViewRenderer` to the `VideoTrack` ```java // isScreencast=false val localVideoSource = peerConnectionFactory.createVideoSource(false) val surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().name, rootEglBase.eglBaseContext) (videoCapturer as VideoCapturer).initialize(surfaceTextureHelper, localVideoOutput.context, localVideoSource.capturerObserver) // width, height, frame per second videoCapturer.startCapture(320, 240, 60) val localVideoTrack = peerConnectionFactory.createVideoTrack(LOCAL_TRACK_ID, localVideoSource) localVideoTrack.addSink(local_view) ``` --- ## What's in the signalling server *Branch [step/remote-video](https://github.com/amrfarid140/webrtc-android-codelab/tree/step/remote-video)* For this sample our signalling server is just a `WebSocket` that forwards what it recieves. It's built using [Ktor](https://ktor.io/). The preffered way to run the server is - Get IntelliJ Idea - Import `/server` - Open `Application.kt` - Run `fun main()` This should run the server on port `8080`. You can change the port from `application.conf` file. [Ktor](https://ktor.io/) is also used as a client in the mobile app to send/receive data from `WebSocket`. Checkout this [file](https://github.com/amrfarid140/webrtc-android-codelab/blob/step/remote-video/mobile/app/src/main/java/me/amryousef/webrtc_demo/SignallingClient.kt) for implementation details. **Note that you will beed to change `HOST_ADDRESS` to match your IP address for your laptop** --- ## Creating `PeerConnection` The `PeerConnection` is what creates the "offer" and/or "answer". It's also responsible for syncing up ICE packets with other peers. That's why when creating the peer connection we pass an observer which will get ICE packets and the remote media stream (when ready). It also has other callbacks for the state of the peer connection but they are not our focus. ```java val stunServer = listOf( PeerConnection.IceServer.builder("stun:stun.l.google.com:19302") .createIceServer() ) val observer = object: PeerConnection.Observer { override fun onIceCandidate(p0: IceCandidate?) { super.onIceCandidate(p0) signallingClient.send(p0) rtcClient.addIceCandidate(p0) } override fun onAddStream(p0: MediaStream?) { super.onAddStream(p0) p0?.videoTracks?.get(0)?.addSink(remote_view) } } peerConnectionFactory.createPeerConnection( stunServer, observer ) ``` --- ## Starting a call Once you have a `PeerConnection` instance created, you can start a call by creating the offer and sending it to the signalling server. There are two things you need when creating the "offer". First is your constraint which sets what are you offering. For example video ``` val constraints = MediaConstraints().apply { mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) } ``` You also need SDP observer which get called when a session description is ready. It's worthy to mention at this point that Native WebRTC library has a callback-based API. ``` peerConnection.createOffer(object : SdpObserver { override fun onCreateSuccess(desc: SessionDescription?) { setLocalDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, desc) //TODO("Send it to your signalling server via WebSocket or other ways") } }, constraints) ``` You will also need to listen to your signalling server for responses. Once the other peer accepts the call, an "answer" SDP message is sent back via signalling server. Once you get this "answer", all you have to do is set it on the `PeerConnection` ```java peerConnection?.setRemoteDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, answerSdp) ``` Here's an [example](https://github.com/amrfarid140/webrtc-android-codelab/blob/65a22c1fc735cf00b42b4246148af8402089cbc7/mobile/app/src/main/java/me/amryousef/webrtc_demo/MainActivity.kt#L89) for the code sample. --- ## Accepting a call Similar to [Making a call](#call), you will need `PeerConnection` created. You also need to be listening to SDP message received from your signalling server. Once you get SDP message you like, and similar to [Making a call](#call), you will need to set your constraint. ``` val constraints = MediaConstraints().apply { mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) } ``` and you will have to set the remote SDP on your `PeerConnection` ```java peerConnection?.setRemoteDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, offerSdp) ``` Then create your SDP "answer" message ``` peerConnection.createAnswer(object : SdpObserver { override fun onCreateSuccess(desc: SessionDescription?) { setLocalDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, desc) //TODO("Send it to your signalling server via WebSocket or other ways") } }, constraints) ``` --- ## Running the sample First, You will need to checkout the [master branch](https://github.com/amrfarid140/webrtc-android-codelab/). There are two directories - mobile which contains a mobile app - server which contains a signalling server Second, Open `server` in IntelliJ Idea and run the server. Make sure it's running on port 8080. Third, Open `mobile` in Android Studio then navigate to `SignallingClient`. You find `HOST_ADDRESS`, change its value with your local IP address. Finally, use Android studio to install the application on two different devices then click the "call" button from one of them. If all goes well, a voice call should've started for you. ================================================ FILE: mobile/app/.gitignore ================================================ /build ================================================ FILE: mobile/app/build.gradle ================================================ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "me.amryousef.webrtc_demo" minSdkVersion 23 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } packagingOptions { exclude("META-INF/kotlinx-io.kotlin_module") exclude("META-INF/atomicfu.kotlin_module") exclude("META-INF/kotlinx-coroutines-io.kotlin_module") exclude("META-INF/kotlinx-coroutines-core.kotlin_module") } compileOptions { sourceCompatibility 1.8 targetCompatibility 1.8 } kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.core:core-ktx:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0-alpha06' //Ktor dependencies (you can retorfit instead) implementation("io.ktor:ktor-client-android:$ktor_version") implementation("io.ktor:ktor-client-websocket:$ktor_version") implementation("io.ktor:ktor-client-cio:$ktor_version") implementation("io.ktor:ktor-client-gson:$ktor_version") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1") //WebRTC dependency implementation("org.webrtc:google-webrtc:$webrtc_version") testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } ================================================ FILE: mobile/app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: mobile/app/src/androidTest/java/me/amryousef/webrtc_demo/ExampleInstrumentedTest.kt ================================================ package me.amryousef.webrtc_demo import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getInstrumentation().targetContext assertEquals("me.amryousef.webrtc_demo", appContext.packageName) } } ================================================ FILE: mobile/app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/AppSdpObserver.kt ================================================ package me.amryousef.webrtc_demo import org.webrtc.SdpObserver import org.webrtc.SessionDescription open class AppSdpObserver : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } } ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/MainActivity.kt ================================================ package me.amryousef.webrtc_demo import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.media.AudioManager import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.view.isGone import io.ktor.util.KtorExperimentalAPI import kotlinx.android.synthetic.main.activity_main.call_button import kotlinx.android.synthetic.main.activity_main.local_view import kotlinx.android.synthetic.main.activity_main.remote_view import kotlinx.android.synthetic.main.activity_main.remote_view_loading import kotlinx.coroutines.ExperimentalCoroutinesApi import org.webrtc.IceCandidate import org.webrtc.MediaStream import org.webrtc.SessionDescription @ExperimentalCoroutinesApi @KtorExperimentalAPI class MainActivity : AppCompatActivity() { companion object { private const val CAMERA_PERMISSION_REQUEST_CODE = 1 private const val CAMERA_PERMISSION = Manifest.permission.CAMERA } private lateinit var rtcClient: RTCClient private lateinit var signallingClient: SignallingClient private val sdpObserver = object : AppSdpObserver() { override fun onCreateSuccess(p0: SessionDescription?) { super.onCreateSuccess(p0) signallingClient.send(p0) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) checkCameraPermission() } private fun checkCameraPermission() { if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) { requestCameraPermission() } else { onCameraPermissionGranted() } } private fun onCameraPermissionGranted() { rtcClient = RTCClient( application, object : PeerConnectionObserver() { override fun onIceCandidate(p0: IceCandidate?) { super.onIceCandidate(p0) signallingClient.send(p0) rtcClient.addIceCandidate(p0) } override fun onAddStream(p0: MediaStream?) { super.onAddStream(p0) p0?.videoTracks?.get(0)?.addSink(remote_view) } } ) rtcClient.initSurfaceView(remote_view) rtcClient.initSurfaceView(local_view) rtcClient.startLocalVideoCapture(local_view) signallingClient = SignallingClient(createSignallingClientListener()) call_button.setOnClickListener { rtcClient.call(sdpObserver) } } private fun createSignallingClientListener() = object : SignallingClientListener { override fun onConnectionEstablished() { call_button.isClickable = true } override fun onOfferReceived(description: SessionDescription) { rtcClient.onRemoteSessionReceived(description) rtcClient.answer(sdpObserver) (getSystemService(Context.AUDIO_SERVICE) as AudioManager).apply { stopBluetoothSco() isBluetoothScoOn = false isSpeakerphoneOn = true } remote_view_loading.isGone = true } override fun onAnswerReceived(description: SessionDescription) { rtcClient.onRemoteSessionReceived(description) remote_view_loading.isGone = true } override fun onIceCandidateReceived(iceCandidate: IceCandidate) { rtcClient.addIceCandidate(iceCandidate) } } private fun requestCameraPermission(dialogShown: Boolean = false) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA_PERMISSION) && !dialogShown) { showPermissionRationaleDialog() } else { ActivityCompat.requestPermissions(this, arrayOf(CAMERA_PERMISSION), CAMERA_PERMISSION_REQUEST_CODE) } } private fun showPermissionRationaleDialog() { AlertDialog.Builder(this) .setTitle("Camera Permission Required") .setMessage("This app need the camera to function") .setPositiveButton("Grant") { dialog, _ -> dialog.dismiss() requestCameraPermission(true) } .setNegativeButton("Deny") { dialog, _ -> dialog.dismiss() onCameraPermissionDenied() } .show() } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { onCameraPermissionGranted() } else { onCameraPermissionDenied() } } private fun onCameraPermissionDenied() { Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_LONG).show() } override fun onDestroy() { signallingClient.destroy() super.onDestroy() } } ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/PeerConnectionObserver.kt ================================================ package me.amryousef.webrtc_demo import org.webrtc.DataChannel import org.webrtc.IceCandidate import org.webrtc.MediaStream import org.webrtc.PeerConnection import org.webrtc.RtpReceiver open class PeerConnectionObserver : PeerConnection.Observer { override fun onIceCandidate(p0: IceCandidate?) { } override fun onDataChannel(p0: DataChannel?) { } override fun onIceConnectionReceivingChange(p0: Boolean) { } override fun onIceConnectionChange(p0: PeerConnection.IceConnectionState?) { } override fun onIceGatheringChange(p0: PeerConnection.IceGatheringState?) { } override fun onAddStream(p0: MediaStream?) { } override fun onSignalingChange(p0: PeerConnection.SignalingState?) { } override fun onIceCandidatesRemoved(p0: Array?) { } override fun onRemoveStream(p0: MediaStream?) { } override fun onRenegotiationNeeded() { } override fun onAddTrack(p0: RtpReceiver?, p1: Array?) { } } ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/RTCClient.kt ================================================ package me.amryousef.webrtc_demo import android.app.Application import android.content.Context import org.webrtc.* import org.webrtc.voiceengine.WebRtcAudioUtils class RTCClient( context: Application, observer: PeerConnection.Observer ) { companion object { private const val LOCAL_TRACK_ID = "local_track" private const val LOCAL_STREAM_ID = "local_track" } private val rootEglBase: EglBase = EglBase.create() init { initPeerConnectionFactory(context) } private val iceServer = listOf( PeerConnection.IceServer.builder("stun:stun.l.google.com:19302") .createIceServer() ) private val peerConnectionFactory by lazy { buildPeerConnectionFactory() } private val videoCapturer by lazy { getVideoCapturer(context) } private val localVideoSource by lazy { peerConnectionFactory.createVideoSource(false) } private val peerConnection by lazy { buildPeerConnection(observer) } private val constraints = MediaConstraints().apply { mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true")) } private fun initPeerConnectionFactory(context: Application) { val options = PeerConnectionFactory.InitializationOptions.builder(context) .setEnableInternalTracer(true) .setFieldTrials("WebRTC-H264HighProfile/Enabled/") .createInitializationOptions() PeerConnectionFactory.initialize(options) } private fun buildPeerConnectionFactory(): PeerConnectionFactory { return PeerConnectionFactory .builder() .setVideoDecoderFactory(DefaultVideoDecoderFactory(rootEglBase.eglBaseContext)) .setVideoEncoderFactory(DefaultVideoEncoderFactory(rootEglBase.eglBaseContext, true, true)) .setOptions(PeerConnectionFactory.Options().apply { disableEncryption = true disableNetworkMonitor = true }) .createPeerConnectionFactory() } private fun buildPeerConnection(observer: PeerConnection.Observer) = peerConnectionFactory.createPeerConnection( iceServer, observer ) private fun getVideoCapturer(context: Context) = Camera2Enumerator(context).run { deviceNames.find { isFrontFacing(it) }?.let { createCapturer(it, null) } ?: throw IllegalStateException() } fun initSurfaceView(view: SurfaceViewRenderer) = view.run { setMirror(true) setEnableHardwareScaler(true) init(rootEglBase.eglBaseContext, null) } fun startLocalVideoCapture(localVideoOutput: SurfaceViewRenderer) { val surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().name, rootEglBase.eglBaseContext) (videoCapturer as VideoCapturer).initialize(surfaceTextureHelper, localVideoOutput.context, localVideoSource.capturerObserver) videoCapturer.startCapture(320, 240, 60) val localVideoTrack = peerConnectionFactory.createVideoTrack(LOCAL_TRACK_ID, localVideoSource) localVideoTrack.addSink(localVideoOutput) val localStream = peerConnectionFactory.createLocalMediaStream(LOCAL_STREAM_ID) localStream.addTrack(localVideoTrack) val audioSource = peerConnectionFactory.createAudioSource(MediaConstraints()) val audioTrack = peerConnectionFactory.createAudioTrack("audio_track", audioSource).apply { setEnabled(true) setVolume(100.0) } localStream.addTrack(audioTrack) peerConnection?.addStream(localStream) } private fun PeerConnection.call(sdpObserver: SdpObserver) { createOffer(object : SdpObserver by sdpObserver { override fun onCreateSuccess(desc: SessionDescription?) { setLocalDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, desc) sdpObserver.onCreateSuccess(desc) } }, constraints) } private fun PeerConnection.answer(sdpObserver: SdpObserver) { createAnswer(object : SdpObserver by sdpObserver { override fun onCreateSuccess(p0: SessionDescription?) { setLocalDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, p0) sdpObserver.onCreateSuccess(p0) } }, constraints) } fun call(sdpObserver: SdpObserver) = peerConnection?.call(sdpObserver) fun answer(sdpObserver: SdpObserver) = peerConnection?.answer(sdpObserver) fun onRemoteSessionReceived(sessionDescription: SessionDescription) { peerConnection?.setRemoteDescription(object : SdpObserver { override fun onSetFailure(p0: String?) { } override fun onSetSuccess() { } override fun onCreateSuccess(p0: SessionDescription?) { } override fun onCreateFailure(p0: String?) { } }, sessionDescription) } fun addIceCandidate(iceCandidate: IceCandidate?) { peerConnection?.addIceCandidate(iceCandidate) } } ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/SignallingClient.kt ================================================ package me.amryousef.webrtc_demo import android.util.Log import com.google.gson.Gson import com.google.gson.JsonObject import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.client.features.json.GsonSerializer import io.ktor.client.features.json.JsonFeature import io.ktor.client.features.websocket.WebSockets import io.ktor.client.features.websocket.ws import io.ktor.http.cio.websocket.Frame import io.ktor.http.cio.websocket.readText import io.ktor.util.KtorExperimentalAPI import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.Job import kotlinx.coroutines.channels.ConflatedBroadcastChannel import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.webrtc.IceCandidate import org.webrtc.SessionDescription @ExperimentalCoroutinesApi @KtorExperimentalAPI class SignallingClient( private val listener: SignallingClientListener ) : CoroutineScope { companion object { private const val HOST_ADDRESS = "192.168.0.12" } private val job = Job() private val gson = Gson() override val coroutineContext = Dispatchers.IO + job private val client = HttpClient(CIO) { install(WebSockets) install(JsonFeature) { serializer = GsonSerializer() } } private val sendChannel = ConflatedBroadcastChannel() init { connect() } private fun connect() = launch { client.ws(host = HOST_ADDRESS, port = 8080, path = "/connect") { listener.onConnectionEstablished() val sendData = sendChannel.openSubscription() try { while (true) { sendData.poll()?.let { Log.v(this@SignallingClient.javaClass.simpleName, "Sending: $it") outgoing.send(Frame.Text(it)) } incoming.poll()?.let { frame -> if (frame is Frame.Text) { val data = frame.readText() Log.v(this@SignallingClient.javaClass.simpleName, "Received: $data") val jsonObject = gson.fromJson(data, JsonObject::class.java) withContext(Dispatchers.Main) { if (jsonObject.has("serverUrl")) { listener.onIceCandidateReceived(gson.fromJson(jsonObject, IceCandidate::class.java)) } else if (jsonObject.has("type") && jsonObject.get("type").asString == "OFFER") { listener.onOfferReceived(gson.fromJson(jsonObject, SessionDescription::class.java)) } else if (jsonObject.has("type") && jsonObject.get("type").asString == "ANSWER") { listener.onAnswerReceived(gson.fromJson(jsonObject, SessionDescription::class.java)) } } } } } } catch (exception: Throwable) { Log.e("asd","asd",exception) } } } fun send(dataObject: Any?) = runBlocking { sendChannel.send(gson.toJson(dataObject)) } fun destroy() { client.close() job.complete() } } ================================================ FILE: mobile/app/src/main/java/me/amryousef/webrtc_demo/SignallingClientListener.kt ================================================ package me.amryousef.webrtc_demo import org.webrtc.IceCandidate import org.webrtc.SessionDescription interface SignallingClientListener { fun onConnectionEstablished() fun onOfferReceived(description: SessionDescription) fun onAnswerReceived(description: SessionDescription) fun onIceCandidateReceived(iceCandidate: IceCandidate) } ================================================ FILE: mobile/app/src/main/res/drawable/ic_launcher_background.xml ================================================ ================================================ FILE: mobile/app/src/main/res/drawable-v24/ic_launcher_foreground.xml ================================================ ================================================ FILE: mobile/app/src/main/res/layout/activity_main.xml ================================================ ================================================ FILE: mobile/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml ================================================ ================================================ FILE: mobile/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml ================================================ ================================================ FILE: mobile/app/src/main/res/values/colors.xml ================================================ #008577 #00574B #D81B60 ================================================ FILE: mobile/app/src/main/res/values/strings.xml ================================================ webrtc-demo ================================================ FILE: mobile/app/src/main/res/values/styles.xml ================================================