├── .gitignore ├── README.md ├── docs ├── caller.gif ├── ice.png ├── receiver.gif └── simple_arch.png ├── mobile ├── .idea │ └── vcs.xml ├── 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-v24 │ │ │ └── ic_launcher_foreground.xml │ │ │ ├── drawable │ │ │ └── ic_launcher_background.xml │ │ │ ├── layout │ │ │ └── activity_main.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── test │ │ └── java │ │ └── me │ │ └── amryousef │ │ └── webrtc_demo │ │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties └── settings.gradle └── server ├── .gitignore ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── resources ├── application.conf └── logback.xml ├── settings.gradle └── src └── me └── amryousef └── Application.kt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | /.idea/navEditor.xml 6 | /.idea/assetWizardSettings.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | .cxx 12 | /.idea/ 13 | 14 | mobile/\.idea/caches/ 15 | 16 | mobile/\.idea/libraries/ 17 | 18 | mobile/\.idea/ 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebRTC for Android 2 | 3 | This repo serves as a how-to guide for implementing basic video conferencing with WebRTC. It includes: 4 | 5 | - An Android mobile app 6 | - A bare-bones signalling server based on WebSocket built with Ktor. 7 | 8 | It's based on official WebRTC native library version **`1.0.27771`** 9 | 10 | --- 11 | ## What is WebRTC 12 | 13 | The [official](https://webrtc.org/) description 14 | 15 | "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." 16 | 17 | 18 | Simply, it's a cross-platform API that allows developers to implement peer-to-peer real-time communication. 19 | 20 | Imagine an API that allows you to send voice, video and/or data (text, images...etc) across mobile apps and web apps. 21 | 22 | --- 23 | ## How does it work (The simple version) 24 | 25 | 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. 26 | 27 | ![Simple Signalling architecture](./docs/simple_arch.png "Simple Signalling architecture") 28 | 29 | (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". 30 | 31 | (A) sends this offer to the signalling server to request to be connected to (B). 32 | 33 | The signalling server then sends this "offer" to (B). 34 | 35 | (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". 36 | 37 | The signalling server then send this "answer" to (A). 38 | 39 | A peer-to-peer connection is then created to exchange data. 40 | 41 | 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). 42 | 43 | ![ICE exchange](./docs/ice.png "ICE exchange") 44 | 45 | (A) starts sending "ICE packets" from the moment it creates the "offer". Similarly, (B) starts sending the "answer". 46 | 47 | --- 48 | 49 | ## Getting a local video stream 50 | 51 | *Branch [step/local-video](https://github.com/amrfarid140/webrtc-android-codelab/tree/step/local-video)* 52 | 53 | Let's label the device we are working on as a "Peer". We need to setup its connection to start communication. 54 | 55 | WebRTC library has `PeerConnectionFactory` that creates the `PeerConnection` for you. However, we need to `initialize` and `configure` this factory first. 56 | 57 | ### Initialising `PeerConnectionFactory` 58 | 59 | 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. 60 | 61 | ```java 62 | val options = PeerConnectionFactory.InitializationOptions.builder(context) 63 | .setEnableInternalTracer(true) 64 | .setFieldTrials("WebRTC-H264HighProfile/Enabled/") 65 | .createInitializationOptions() 66 | PeerConnectionFactory.initialize(options) 67 | ``` 68 | 69 | ### Configuring `PeerConnectionFactory` 70 | 71 | Now we can use `PeerConnectionFactory.Builder` to build an instance of `PeerConnectionFactory`. 72 | 73 | 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. 74 | 75 | ```java 76 | val rootEglBase: EglBase = EglBase.create() 77 | PeerConnectionFactory 78 | .builder() 79 | .setVideoDecoderFactory(DefaultVideoDecoderFactory(rootEglBase.eglBaseContext)) 80 | .setVideoEncoderFactory(DefaultVideoEncoderFactory(rootEglBase.eglBaseContext, true, true)) 81 | .setOptions(PeerConnectionFactory.Options().apply { 82 | disableEncryption = true 83 | disableNetworkMonitor = true 84 | }) 85 | .createPeerConnectionFactory() 86 | ``` 87 | 88 | ### Setting the video output 89 | 90 | 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. 91 | 92 | ```xml 93 | 94 | 100 | 101 | 109 | 110 | 111 | ``` 112 | 113 | We will also need to mirror the video stream we are providing and enable hardware acceleration. 114 | 115 | ```java 116 | local_view.setMirror(true) 117 | local_view.setEnableHardwareScaler(true) 118 | local_view.init(rootEglBase.eglBaseContext, null) 119 | ``` 120 | 121 | ### Getting the video source 122 | 123 | The video source is simply the camera. Native WebRTC library has this handy helper - `Camera2Enumerator` - which allows use to fetch the front facing camera. 124 | 125 | ```java 126 | Camera2Enumerator(context).run { 127 | deviceNames.find { 128 | isFrontFacing(it) 129 | }?.let { 130 | createCapturer(it, null) 131 | } ?: throw IllegalStateException() 132 | } 133 | ``` 134 | 135 | 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` 136 | 137 | ```java 138 | // isScreencast=false 139 | val localVideoSource = peerConnectionFactory.createVideoSource(false) 140 | 141 | val surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().name, rootEglBase.eglBaseContext) 142 | (videoCapturer as VideoCapturer).initialize(surfaceTextureHelper, localVideoOutput.context, localVideoSource.capturerObserver) 143 | 144 | // width, height, frame per second 145 | videoCapturer.startCapture(320, 240, 60) 146 | 147 | val localVideoTrack = peerConnectionFactory.createVideoTrack(LOCAL_TRACK_ID, localVideoSource) 148 | 149 | localVideoTrack.addSink(local_view) 150 | ``` 151 | 152 | --- 153 | 154 | ## What's in the signalling server 155 | *Branch [step/remote-video](https://github.com/amrfarid140/webrtc-android-codelab/tree/step/remote-video)* 156 | 157 | For this sample our signalling server is just a `WebSocket` that forwards what it recieves. It's built using [Ktor](https://ktor.io/). 158 | 159 | The preffered way to run the server is 160 | 161 | - Get IntelliJ Idea 162 | - Import `/server` 163 | - Open `Application.kt` 164 | - Run `fun main()` 165 | 166 | This should run the server on port `8080`. You can change the port from `application.conf` file. 167 | 168 | [Ktor](https://ktor.io/) is also used as a client in the mobile app to send/receive data from `WebSocket`. 169 | 170 | 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. 171 | 172 | **Note that you will beed to change `HOST_ADDRESS` to match your IP address for your laptop** 173 | 174 | --- 175 | 176 | ## Creating `PeerConnection` 177 | 178 | The `PeerConnection` is what creates the "offer" and/or "answer". It's also responsible for syncing up ICE packets with other peers. 179 | 180 | 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. 181 | 182 | ```java 183 | val stunServer = listOf( 184 | PeerConnection.IceServer.builder("stun:stun.l.google.com:19302") 185 | .createIceServer() 186 | ) 187 | 188 | val observer = object: PeerConnection.Observer { 189 | override fun onIceCandidate(p0: IceCandidate?) { 190 | super.onIceCandidate(p0) 191 | signallingClient.send(p0) 192 | rtcClient.addIceCandidate(p0) 193 | } 194 | 195 | override fun onAddStream(p0: MediaStream?) { 196 | super.onAddStream(p0) 197 | p0?.videoTracks?.get(0)?.addSink(remote_view) 198 | } 199 | } 200 | peerConnectionFactory.createPeerConnection( 201 | stunServer, 202 | observer 203 | ) 204 | ``` 205 | 206 | --- 207 | 208 | ## Starting a call 209 | 210 | 211 | Once you have a `PeerConnection` instance created, you can start a call by creating the offer and sending it to the signalling server. 212 | 213 | There are two things you need when creating the "offer". First is your constraint which sets what are you offering. For example video 214 | 215 | ``` 216 | val constraints = MediaConstraints().apply { 217 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) 218 | } 219 | ``` 220 | 221 | 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. 222 | 223 | ``` 224 | peerConnection.createOffer(object : SdpObserver { 225 | override fun onCreateSuccess(desc: SessionDescription?) { 226 | 227 | setLocalDescription(object : SdpObserver { 228 | override fun onSetFailure(p0: String?) { 229 | } 230 | 231 | override fun onSetSuccess() { 232 | } 233 | 234 | override fun onCreateSuccess(p0: SessionDescription?) { 235 | } 236 | 237 | override fun onCreateFailure(p0: String?) { 238 | } 239 | }, desc) 240 | 241 | //TODO("Send it to your signalling server via WebSocket or other ways") 242 | } 243 | }, constraints) 244 | ``` 245 | 246 | 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` 247 | 248 | ```java 249 | peerConnection?.setRemoteDescription(object : SdpObserver { 250 | override fun onSetFailure(p0: String?) { 251 | } 252 | 253 | override fun onSetSuccess() { 254 | } 255 | 256 | override fun onCreateSuccess(p0: SessionDescription?) { 257 | } 258 | 259 | override fun onCreateFailure(p0: String?) { 260 | } 261 | }, answerSdp) 262 | ``` 263 | 264 | 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. 265 | 266 | --- 267 | 268 | ## Accepting a call 269 | 270 | 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. 271 | 272 | Once you get SDP message you like, and similar to [Making a call](#call), you will need to set your constraint. 273 | 274 | ``` 275 | val constraints = MediaConstraints().apply { 276 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) 277 | } 278 | ``` 279 | 280 | and you will have to set the remote SDP on your `PeerConnection` 281 | 282 | ```java 283 | peerConnection?.setRemoteDescription(object : SdpObserver { 284 | override fun onSetFailure(p0: String?) { 285 | } 286 | 287 | override fun onSetSuccess() { 288 | } 289 | 290 | override fun onCreateSuccess(p0: SessionDescription?) { 291 | } 292 | 293 | override fun onCreateFailure(p0: String?) { 294 | } 295 | }, offerSdp) 296 | ``` 297 | 298 | Then create your SDP "answer" message 299 | 300 | ``` 301 | peerConnection.createAnswer(object : SdpObserver { 302 | override fun onCreateSuccess(desc: SessionDescription?) { 303 | 304 | setLocalDescription(object : SdpObserver { 305 | override fun onSetFailure(p0: String?) { 306 | } 307 | 308 | override fun onSetSuccess() { 309 | } 310 | 311 | override fun onCreateSuccess(p0: SessionDescription?) { 312 | } 313 | 314 | override fun onCreateFailure(p0: String?) { 315 | } 316 | }, desc) 317 | 318 | //TODO("Send it to your signalling server via WebSocket or other ways") 319 | } 320 | }, constraints) 321 | ``` 322 | --- 323 | 324 | ## Running the sample 325 | 326 | 327 | First, You will need to checkout the [master branch](https://github.com/amrfarid140/webrtc-android-codelab/). There are two directories 328 | - mobile which contains a mobile app 329 | - server which contains a signalling server 330 | 331 | Second, Open `server` in IntelliJ Idea and run the server. Make sure it's running on port 8080. 332 | 333 | Third, Open `mobile` in Android Studio then navigate to `SignallingClient`. You find `HOST_ADDRESS`, change its value with your local IP address. 334 | 335 | 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. 336 | 337 | 343 | 344 | 345 | 346 | -------------------------------------------------------------------------------- /docs/caller.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/docs/caller.gif -------------------------------------------------------------------------------- /docs/ice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/docs/ice.png -------------------------------------------------------------------------------- /docs/receiver.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/docs/receiver.gif -------------------------------------------------------------------------------- /docs/simple_arch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/docs/simple_arch.png -------------------------------------------------------------------------------- /mobile/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /mobile/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mobile/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | android { 8 | compileSdkVersion 28 9 | defaultConfig { 10 | applicationId "me.amryousef.webrtc_demo" 11 | minSdkVersion 23 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName "1.0" 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | packagingOptions { 25 | exclude("META-INF/kotlinx-io.kotlin_module") 26 | exclude("META-INF/atomicfu.kotlin_module") 27 | exclude("META-INF/kotlinx-coroutines-io.kotlin_module") 28 | exclude("META-INF/kotlinx-coroutines-core.kotlin_module") 29 | } 30 | 31 | compileOptions { 32 | sourceCompatibility 1.8 33 | targetCompatibility 1.8 34 | } 35 | 36 | kotlinOptions { 37 | jvmTarget = JavaVersion.VERSION_1_8.toString() 38 | } 39 | } 40 | 41 | dependencies { 42 | implementation fileTree(dir: 'libs', include: ['*.jar']) 43 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 44 | implementation 'androidx.appcompat:appcompat:1.0.2' 45 | implementation 'androidx.core:core-ktx:1.0.2' 46 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 47 | implementation 'com.google.android.material:material:1.1.0-alpha06' 48 | 49 | //Ktor dependencies (you can retorfit instead) 50 | implementation("io.ktor:ktor-client-android:$ktor_version") 51 | implementation("io.ktor:ktor-client-websocket:$ktor_version") 52 | implementation("io.ktor:ktor-client-cio:$ktor_version") 53 | implementation("io.ktor:ktor-client-gson:$ktor_version") 54 | 55 | implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1") 56 | 57 | //WebRTC dependency 58 | implementation("org.webrtc:google-webrtc:$webrtc_version") 59 | 60 | testImplementation 'junit:junit:4.12' 61 | androidTestImplementation 'androidx.test.ext:junit:1.1.0' 62 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 63 | } 64 | -------------------------------------------------------------------------------- /mobile/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /mobile/app/src/androidTest/java/me/amryousef/webrtc_demo/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("me.amryousef.webrtc_demo", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /mobile/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/AppSdpObserver.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import org.webrtc.SdpObserver 4 | import org.webrtc.SessionDescription 5 | 6 | open class AppSdpObserver : SdpObserver { 7 | override fun onSetFailure(p0: String?) { 8 | } 9 | 10 | override fun onSetSuccess() { 11 | } 12 | 13 | override fun onCreateSuccess(p0: SessionDescription?) { 14 | } 15 | 16 | override fun onCreateFailure(p0: String?) { 17 | } 18 | } -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import android.Manifest 4 | import android.content.Context 5 | import android.content.pm.PackageManager 6 | import android.media.AudioManager 7 | import android.os.Bundle 8 | import android.widget.Toast 9 | import androidx.appcompat.app.AlertDialog 10 | import androidx.appcompat.app.AppCompatActivity 11 | import androidx.core.app.ActivityCompat 12 | import androidx.core.content.ContextCompat 13 | import androidx.core.view.isGone 14 | import io.ktor.util.KtorExperimentalAPI 15 | import kotlinx.android.synthetic.main.activity_main.call_button 16 | import kotlinx.android.synthetic.main.activity_main.local_view 17 | import kotlinx.android.synthetic.main.activity_main.remote_view 18 | import kotlinx.android.synthetic.main.activity_main.remote_view_loading 19 | import kotlinx.coroutines.ExperimentalCoroutinesApi 20 | import org.webrtc.IceCandidate 21 | import org.webrtc.MediaStream 22 | import org.webrtc.SessionDescription 23 | 24 | @ExperimentalCoroutinesApi 25 | @KtorExperimentalAPI 26 | class MainActivity : AppCompatActivity() { 27 | 28 | companion object { 29 | private const val CAMERA_PERMISSION_REQUEST_CODE = 1 30 | private const val CAMERA_PERMISSION = Manifest.permission.CAMERA 31 | } 32 | 33 | private lateinit var rtcClient: RTCClient 34 | private lateinit var signallingClient: SignallingClient 35 | 36 | private val sdpObserver = object : AppSdpObserver() { 37 | override fun onCreateSuccess(p0: SessionDescription?) { 38 | super.onCreateSuccess(p0) 39 | signallingClient.send(p0) 40 | } 41 | } 42 | 43 | override fun onCreate(savedInstanceState: Bundle?) { 44 | super.onCreate(savedInstanceState) 45 | setContentView(R.layout.activity_main) 46 | checkCameraPermission() 47 | } 48 | 49 | private fun checkCameraPermission() { 50 | if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) { 51 | requestCameraPermission() 52 | } else { 53 | onCameraPermissionGranted() 54 | } 55 | } 56 | 57 | private fun onCameraPermissionGranted() { 58 | rtcClient = RTCClient( 59 | application, 60 | object : PeerConnectionObserver() { 61 | override fun onIceCandidate(p0: IceCandidate?) { 62 | super.onIceCandidate(p0) 63 | signallingClient.send(p0) 64 | rtcClient.addIceCandidate(p0) 65 | } 66 | 67 | override fun onAddStream(p0: MediaStream?) { 68 | super.onAddStream(p0) 69 | p0?.videoTracks?.get(0)?.addSink(remote_view) 70 | } 71 | } 72 | ) 73 | rtcClient.initSurfaceView(remote_view) 74 | rtcClient.initSurfaceView(local_view) 75 | rtcClient.startLocalVideoCapture(local_view) 76 | signallingClient = SignallingClient(createSignallingClientListener()) 77 | call_button.setOnClickListener { rtcClient.call(sdpObserver) } 78 | } 79 | 80 | private fun createSignallingClientListener() = object : SignallingClientListener { 81 | override fun onConnectionEstablished() { 82 | call_button.isClickable = true 83 | } 84 | 85 | override fun onOfferReceived(description: SessionDescription) { 86 | rtcClient.onRemoteSessionReceived(description) 87 | rtcClient.answer(sdpObserver) 88 | (getSystemService(Context.AUDIO_SERVICE) as AudioManager).apply { 89 | stopBluetoothSco() 90 | isBluetoothScoOn = false 91 | isSpeakerphoneOn = true 92 | } 93 | remote_view_loading.isGone = true 94 | } 95 | 96 | override fun onAnswerReceived(description: SessionDescription) { 97 | rtcClient.onRemoteSessionReceived(description) 98 | remote_view_loading.isGone = true 99 | } 100 | 101 | override fun onIceCandidateReceived(iceCandidate: IceCandidate) { 102 | rtcClient.addIceCandidate(iceCandidate) 103 | } 104 | } 105 | 106 | private fun requestCameraPermission(dialogShown: Boolean = false) { 107 | if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA_PERMISSION) && !dialogShown) { 108 | showPermissionRationaleDialog() 109 | } else { 110 | ActivityCompat.requestPermissions(this, arrayOf(CAMERA_PERMISSION), CAMERA_PERMISSION_REQUEST_CODE) 111 | } 112 | } 113 | 114 | private fun showPermissionRationaleDialog() { 115 | AlertDialog.Builder(this) 116 | .setTitle("Camera Permission Required") 117 | .setMessage("This app need the camera to function") 118 | .setPositiveButton("Grant") { dialog, _ -> 119 | dialog.dismiss() 120 | requestCameraPermission(true) 121 | } 122 | .setNegativeButton("Deny") { dialog, _ -> 123 | dialog.dismiss() 124 | onCameraPermissionDenied() 125 | } 126 | .show() 127 | } 128 | 129 | override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { 130 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 131 | if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) { 132 | onCameraPermissionGranted() 133 | } else { 134 | onCameraPermissionDenied() 135 | } 136 | } 137 | 138 | private fun onCameraPermissionDenied() { 139 | Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_LONG).show() 140 | } 141 | 142 | override fun onDestroy() { 143 | signallingClient.destroy() 144 | super.onDestroy() 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/PeerConnectionObserver.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import org.webrtc.DataChannel 4 | import org.webrtc.IceCandidate 5 | import org.webrtc.MediaStream 6 | import org.webrtc.PeerConnection 7 | import org.webrtc.RtpReceiver 8 | 9 | open class PeerConnectionObserver : PeerConnection.Observer { 10 | override fun onIceCandidate(p0: IceCandidate?) { 11 | } 12 | 13 | override fun onDataChannel(p0: DataChannel?) { 14 | } 15 | 16 | override fun onIceConnectionReceivingChange(p0: Boolean) { 17 | } 18 | 19 | override fun onIceConnectionChange(p0: PeerConnection.IceConnectionState?) { 20 | } 21 | 22 | override fun onIceGatheringChange(p0: PeerConnection.IceGatheringState?) { 23 | } 24 | 25 | override fun onAddStream(p0: MediaStream?) { 26 | } 27 | 28 | override fun onSignalingChange(p0: PeerConnection.SignalingState?) { 29 | } 30 | 31 | override fun onIceCandidatesRemoved(p0: Array?) { 32 | } 33 | 34 | override fun onRemoveStream(p0: MediaStream?) { 35 | } 36 | 37 | override fun onRenegotiationNeeded() { 38 | } 39 | 40 | override fun onAddTrack(p0: RtpReceiver?, p1: Array?) { 41 | } 42 | } -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/RTCClient.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import android.app.Application 4 | import android.content.Context 5 | import org.webrtc.* 6 | import org.webrtc.voiceengine.WebRtcAudioUtils 7 | 8 | class RTCClient( 9 | context: Application, 10 | observer: PeerConnection.Observer 11 | ) { 12 | 13 | companion object { 14 | private const val LOCAL_TRACK_ID = "local_track" 15 | private const val LOCAL_STREAM_ID = "local_track" 16 | } 17 | 18 | private val rootEglBase: EglBase = EglBase.create() 19 | 20 | init { 21 | initPeerConnectionFactory(context) 22 | } 23 | 24 | private val iceServer = listOf( 25 | PeerConnection.IceServer.builder("stun:stun.l.google.com:19302") 26 | .createIceServer() 27 | ) 28 | 29 | private val peerConnectionFactory by lazy { buildPeerConnectionFactory() } 30 | private val videoCapturer by lazy { getVideoCapturer(context) } 31 | private val localVideoSource by lazy { peerConnectionFactory.createVideoSource(false) } 32 | private val peerConnection by lazy { buildPeerConnection(observer) } 33 | private val constraints = MediaConstraints().apply { 34 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true")) 35 | mandatory.add(MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true")) 36 | } 37 | 38 | private fun initPeerConnectionFactory(context: Application) { 39 | val options = PeerConnectionFactory.InitializationOptions.builder(context) 40 | .setEnableInternalTracer(true) 41 | .setFieldTrials("WebRTC-H264HighProfile/Enabled/") 42 | .createInitializationOptions() 43 | PeerConnectionFactory.initialize(options) 44 | } 45 | 46 | private fun buildPeerConnectionFactory(): PeerConnectionFactory { 47 | return PeerConnectionFactory 48 | .builder() 49 | .setVideoDecoderFactory(DefaultVideoDecoderFactory(rootEglBase.eglBaseContext)) 50 | .setVideoEncoderFactory(DefaultVideoEncoderFactory(rootEglBase.eglBaseContext, true, true)) 51 | .setOptions(PeerConnectionFactory.Options().apply { 52 | disableEncryption = true 53 | disableNetworkMonitor = true 54 | }) 55 | .createPeerConnectionFactory() 56 | } 57 | 58 | private fun buildPeerConnection(observer: PeerConnection.Observer) = peerConnectionFactory.createPeerConnection( 59 | iceServer, 60 | observer 61 | ) 62 | 63 | private fun getVideoCapturer(context: Context) = 64 | Camera2Enumerator(context).run { 65 | deviceNames.find { 66 | isFrontFacing(it) 67 | }?.let { 68 | createCapturer(it, null) 69 | } ?: throw IllegalStateException() 70 | } 71 | 72 | fun initSurfaceView(view: SurfaceViewRenderer) = view.run { 73 | setMirror(true) 74 | setEnableHardwareScaler(true) 75 | init(rootEglBase.eglBaseContext, null) 76 | } 77 | 78 | fun startLocalVideoCapture(localVideoOutput: SurfaceViewRenderer) { 79 | val surfaceTextureHelper = SurfaceTextureHelper.create(Thread.currentThread().name, rootEglBase.eglBaseContext) 80 | (videoCapturer as VideoCapturer).initialize(surfaceTextureHelper, localVideoOutput.context, localVideoSource.capturerObserver) 81 | videoCapturer.startCapture(320, 240, 60) 82 | val localVideoTrack = peerConnectionFactory.createVideoTrack(LOCAL_TRACK_ID, localVideoSource) 83 | localVideoTrack.addSink(localVideoOutput) 84 | val localStream = peerConnectionFactory.createLocalMediaStream(LOCAL_STREAM_ID) 85 | localStream.addTrack(localVideoTrack) 86 | val audioSource = peerConnectionFactory.createAudioSource(MediaConstraints()) 87 | val audioTrack = peerConnectionFactory.createAudioTrack("audio_track", audioSource).apply { 88 | setEnabled(true) 89 | setVolume(100.0) 90 | } 91 | localStream.addTrack(audioTrack) 92 | peerConnection?.addStream(localStream) 93 | } 94 | 95 | private fun PeerConnection.call(sdpObserver: SdpObserver) { 96 | createOffer(object : SdpObserver by sdpObserver { 97 | override fun onCreateSuccess(desc: SessionDescription?) { 98 | 99 | setLocalDescription(object : SdpObserver { 100 | override fun onSetFailure(p0: String?) { 101 | } 102 | 103 | override fun onSetSuccess() { 104 | } 105 | 106 | override fun onCreateSuccess(p0: SessionDescription?) { 107 | } 108 | 109 | override fun onCreateFailure(p0: String?) { 110 | } 111 | }, desc) 112 | sdpObserver.onCreateSuccess(desc) 113 | } 114 | }, constraints) 115 | } 116 | 117 | private fun PeerConnection.answer(sdpObserver: SdpObserver) { 118 | createAnswer(object : SdpObserver by sdpObserver { 119 | override fun onCreateSuccess(p0: SessionDescription?) { 120 | setLocalDescription(object : SdpObserver { 121 | override fun onSetFailure(p0: String?) { 122 | } 123 | 124 | override fun onSetSuccess() { 125 | } 126 | 127 | override fun onCreateSuccess(p0: SessionDescription?) { 128 | } 129 | 130 | override fun onCreateFailure(p0: String?) { 131 | } 132 | }, p0) 133 | sdpObserver.onCreateSuccess(p0) 134 | } 135 | }, constraints) 136 | } 137 | 138 | fun call(sdpObserver: SdpObserver) = peerConnection?.call(sdpObserver) 139 | 140 | fun answer(sdpObserver: SdpObserver) = peerConnection?.answer(sdpObserver) 141 | 142 | fun onRemoteSessionReceived(sessionDescription: SessionDescription) { 143 | peerConnection?.setRemoteDescription(object : SdpObserver { 144 | override fun onSetFailure(p0: String?) { 145 | } 146 | 147 | override fun onSetSuccess() { 148 | } 149 | 150 | override fun onCreateSuccess(p0: SessionDescription?) { 151 | } 152 | 153 | override fun onCreateFailure(p0: String?) { 154 | } 155 | }, sessionDescription) 156 | } 157 | 158 | fun addIceCandidate(iceCandidate: IceCandidate?) { 159 | peerConnection?.addIceCandidate(iceCandidate) 160 | } 161 | } -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/SignallingClient.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import android.util.Log 4 | import com.google.gson.Gson 5 | import com.google.gson.JsonObject 6 | import io.ktor.client.HttpClient 7 | import io.ktor.client.engine.cio.CIO 8 | import io.ktor.client.features.json.GsonSerializer 9 | import io.ktor.client.features.json.JsonFeature 10 | import io.ktor.client.features.websocket.WebSockets 11 | import io.ktor.client.features.websocket.ws 12 | import io.ktor.http.cio.websocket.Frame 13 | import io.ktor.http.cio.websocket.readText 14 | import io.ktor.util.KtorExperimentalAPI 15 | import kotlinx.coroutines.CoroutineScope 16 | import kotlinx.coroutines.Dispatchers 17 | import kotlinx.coroutines.ExperimentalCoroutinesApi 18 | import kotlinx.coroutines.Job 19 | import kotlinx.coroutines.channels.ConflatedBroadcastChannel 20 | import kotlinx.coroutines.launch 21 | import kotlinx.coroutines.runBlocking 22 | import kotlinx.coroutines.withContext 23 | import org.webrtc.IceCandidate 24 | import org.webrtc.SessionDescription 25 | 26 | @ExperimentalCoroutinesApi 27 | @KtorExperimentalAPI 28 | class SignallingClient( 29 | private val listener: SignallingClientListener 30 | ) : CoroutineScope { 31 | 32 | companion object { 33 | private const val HOST_ADDRESS = "192.168.0.12" 34 | } 35 | 36 | private val job = Job() 37 | 38 | private val gson = Gson() 39 | 40 | override val coroutineContext = Dispatchers.IO + job 41 | 42 | private val client = HttpClient(CIO) { 43 | install(WebSockets) 44 | install(JsonFeature) { 45 | serializer = GsonSerializer() 46 | } 47 | } 48 | 49 | private val sendChannel = ConflatedBroadcastChannel() 50 | 51 | init { 52 | connect() 53 | } 54 | 55 | private fun connect() = launch { 56 | client.ws(host = HOST_ADDRESS, port = 8080, path = "/connect") { 57 | listener.onConnectionEstablished() 58 | val sendData = sendChannel.openSubscription() 59 | try { 60 | while (true) { 61 | 62 | sendData.poll()?.let { 63 | Log.v(this@SignallingClient.javaClass.simpleName, "Sending: $it") 64 | outgoing.send(Frame.Text(it)) 65 | } 66 | incoming.poll()?.let { frame -> 67 | if (frame is Frame.Text) { 68 | val data = frame.readText() 69 | Log.v(this@SignallingClient.javaClass.simpleName, "Received: $data") 70 | val jsonObject = gson.fromJson(data, JsonObject::class.java) 71 | withContext(Dispatchers.Main) { 72 | if (jsonObject.has("serverUrl")) { 73 | listener.onIceCandidateReceived(gson.fromJson(jsonObject, IceCandidate::class.java)) 74 | } else if (jsonObject.has("type") && jsonObject.get("type").asString == "OFFER") { 75 | listener.onOfferReceived(gson.fromJson(jsonObject, SessionDescription::class.java)) 76 | } else if (jsonObject.has("type") && jsonObject.get("type").asString == "ANSWER") { 77 | listener.onAnswerReceived(gson.fromJson(jsonObject, SessionDescription::class.java)) 78 | } 79 | } 80 | } 81 | } 82 | } 83 | } catch (exception: Throwable) { 84 | Log.e("asd","asd",exception) 85 | } 86 | } 87 | } 88 | 89 | fun send(dataObject: Any?) = runBlocking { 90 | sendChannel.send(gson.toJson(dataObject)) 91 | } 92 | 93 | fun destroy() { 94 | client.close() 95 | job.complete() 96 | } 97 | } -------------------------------------------------------------------------------- /mobile/app/src/main/java/me/amryousef/webrtc_demo/SignallingClientListener.kt: -------------------------------------------------------------------------------- 1 | package me.amryousef.webrtc_demo 2 | 3 | import org.webrtc.IceCandidate 4 | import org.webrtc.SessionDescription 5 | 6 | interface SignallingClientListener { 7 | fun onConnectionEstablished() 8 | fun onOfferReceived(description: SessionDescription) 9 | fun onAnswerReceived(description: SessionDescription) 10 | fun onIceCandidateReceived(iceCandidate: IceCandidate) 11 | } -------------------------------------------------------------------------------- /mobile/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 26 | 27 | 36 | 37 | 46 | 47 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /mobile/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | 7 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | webrtc-demo 3 | 4 | -------------------------------------------------------------------------------- /mobile/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |