├── .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 | 
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 | 
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 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/mobile/app/src/test/java/me/amryousef/webrtc_demo/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package me.amryousef.webrtc_demo
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/mobile/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.3.31'
5 | ext.webrtc_version = '1.0.27771'
6 | ext.ktor_version = '1.1.4'
7 | repositories {
8 | google()
9 | jcenter()
10 |
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:4.2.0-beta02'
14 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | google()
23 | jcenter()
24 |
25 | }
26 | }
27 |
28 | task clean(type: Delete) {
29 | delete rootProject.buildDir
30 | }
31 |
--------------------------------------------------------------------------------
/mobile/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/mobile/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/mobile/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/mobile/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun May 26 10:14:28 BST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https://services.gradle.org/distributions/gradle-6.7.1-all.zip
7 |
--------------------------------------------------------------------------------
/mobile/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/mobile/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/mobile/local.properties:
--------------------------------------------------------------------------------
1 | ## This file is automatically generated by Android Studio.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file should *NOT* be checked into Version Control Systems,
5 | # as it contains information specific to your local configuration.
6 | #
7 | # Location of the SDK. This is only used by Gradle.
8 | # For customization when using a Version Control System, please read the
9 | # header note.
10 | sdk.dir=/Users/amryousef/Library/Android/sdk
11 |
--------------------------------------------------------------------------------
/mobile/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='webrtc-demo'
3 |
--------------------------------------------------------------------------------
/server/.gitignore:
--------------------------------------------------------------------------------
1 | /.gradle
2 | /.idea
3 | /out
4 | /build
5 | *.iml
6 | *.ipr
7 | *.iws
8 | service_key.json
--------------------------------------------------------------------------------
/server/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | jcenter()
4 | }
5 |
6 | dependencies {
7 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8 | }
9 | }
10 |
11 | apply plugin: 'kotlin'
12 | apply plugin: 'application'
13 |
14 | group 'homedoor'
15 | version '0.0.1'
16 | mainClassName = "io.ktor.server.netty.EngineMain"
17 |
18 | sourceSets {
19 | main.kotlin.srcDirs = main.java.srcDirs = ['src']
20 | test.kotlin.srcDirs = test.java.srcDirs = ['test']
21 | main.resources.srcDirs = ['resources']
22 | test.resources.srcDirs = ['testresources']
23 | }
24 |
25 | repositories {
26 | mavenLocal()
27 | jcenter()
28 | maven { url 'https://kotlin.bintray.com/ktor' }
29 | }
30 |
31 | dependencies {
32 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
33 | compile "io.ktor:ktor-server-netty:$ktor_version"
34 | compile "ch.qos.logback:logback-classic:$logback_version"
35 | compile "io.ktor:ktor-metrics:$ktor_version"
36 | compile "io.ktor:ktor-server-core:$ktor_version"
37 | compile "io.ktor:ktor-websockets:$ktor_version"
38 | compile "io.ktor:ktor-gson:$ktor_version"
39 |
40 | testCompile "io.ktor:ktor-server-tests:$ktor_version"
41 | }
42 |
--------------------------------------------------------------------------------
/server/gradle.properties:
--------------------------------------------------------------------------------
1 | ktor_version=1.1.4
2 | kotlin.code.style=official
3 | kotlin_version=1.3.30
4 | logback_version=1.2.1
5 |
--------------------------------------------------------------------------------
/server/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/amrfarid140/webrtc-android-codelab/c16bea358c3fa98b445c2eebf6979681e2c61cc2/server/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/server/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/server/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/server/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/server/resources/application.conf:
--------------------------------------------------------------------------------
1 | ktor {
2 | deployment {
3 | port = 8080
4 | port = ${?PORT}
5 | }
6 | application {
7 | modules = [ me.amryousef.ApplicationKt.module ]
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/server/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | %d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/server/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = "homedoor"
2 |
--------------------------------------------------------------------------------
/server/src/me/amryousef/Application.kt:
--------------------------------------------------------------------------------
1 | package me.amryousef
2 |
3 | import io.ktor.application.Application
4 | import io.ktor.application.install
5 | import io.ktor.features.CallLogging
6 | import io.ktor.features.ContentNegotiation
7 | import io.ktor.features.DefaultHeaders
8 | import io.ktor.gson.gson
9 | import io.ktor.http.cio.websocket.*
10 | import io.ktor.routing.routing
11 | import io.ktor.websocket.WebSocketServerSession
12 | import io.ktor.websocket.WebSockets
13 | import io.ktor.websocket.webSocket
14 | import kotlinx.coroutines.ExperimentalCoroutinesApi
15 | import kotlinx.coroutines.channels.ConflatedBroadcastChannel
16 | import java.time.Duration
17 | import java.util.*
18 |
19 | fun main(args: Array): Unit = io.ktor.server.netty.EngineMain.main(args)
20 |
21 | @ExperimentalCoroutinesApi
22 | @Suppress("unused") // Referenced in application.conf
23 | @kotlin.jvm.JvmOverloads
24 | fun Application.module(testing: Boolean = false) {
25 | install(DefaultHeaders) {
26 | header("X-Engine", "Ktor") // will send this header with each response
27 | }
28 |
29 | install(CallLogging)
30 |
31 | install(WebSockets) {
32 | pingPeriod = Duration.ofSeconds(15)
33 | timeout = Duration.ofSeconds(15)
34 | maxFrameSize = Long.MAX_VALUE
35 | masking = false
36 | }
37 |
38 | install(ContentNegotiation) {
39 | gson {
40 | }
41 | }
42 |
43 | val connections = Collections.synchronizedMap(mutableMapOf())
44 |
45 | routing {
46 | webSocket(path = "/connect") {
47 | val id = UUID.randomUUID().toString()
48 | connections[id] = this
49 | println("Connected clients = ${connections.size}")
50 | try {
51 | for (data in incoming) {
52 | if (data is Frame.Text) {
53 | val clients = connections.filter { it.key != id }
54 | val text = data.readText()
55 | clients.forEach {
56 | println("Sending to:${it.key}")
57 | println("Sending $text")
58 | it.value.send(text)
59 | }
60 | }
61 | }
62 | } finally {
63 | connections.remove(id)
64 | }
65 | }
66 | }
67 | }
68 |
69 |
--------------------------------------------------------------------------------