-
Notifications
You must be signed in to change notification settings - Fork 57
/
MainActivity.kt
139 lines (121 loc) · 4.93 KB
/
MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package me.amryousef.webrtc_demo
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.isGone
import io.ktor.util.KtorExperimentalAPI
import kotlinx.android.synthetic.main.activity_main.call_button
import kotlinx.android.synthetic.main.activity_main.local_view
import kotlinx.android.synthetic.main.activity_main.remote_view
import kotlinx.android.synthetic.main.activity_main.remote_view_loading
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.webrtc.IceCandidate
import org.webrtc.MediaStream
import org.webrtc.SessionDescription
@ExperimentalCoroutinesApi
@KtorExperimentalAPI
class MainActivity : AppCompatActivity() {
companion object {
private const val CAMERA_PERMISSION_REQUEST_CODE = 1
private const val CAMERA_PERMISSION = Manifest.permission.CAMERA
}
private lateinit var rtcClient: RTCClient
private lateinit var signallingClient: SignallingClient
private val sdpObserver = object : AppSdpObserver() {
override fun onCreateSuccess(p0: SessionDescription?) {
super.onCreateSuccess(p0)
signallingClient.send(p0)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkCameraPermission()
}
private fun checkCameraPermission() {
if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
requestCameraPermission()
} else {
onCameraPermissionGranted()
}
}
private fun onCameraPermissionGranted() {
rtcClient = RTCClient(
application,
object : PeerConnectionObserver() {
override fun onIceCandidate(p0: IceCandidate?) {
super.onIceCandidate(p0)
signallingClient.send(p0)
rtcClient.addIceCandidate(p0)
}
override fun onAddStream(p0: MediaStream?) {
super.onAddStream(p0)
p0?.videoTracks?.get(0)?.addSink(remote_view)
}
}
)
rtcClient.initSurfaceView(remote_view)
rtcClient.initSurfaceView(local_view)
rtcClient.startLocalVideoCapture(local_view)
signallingClient = SignallingClient(createSignallingClientListener())
call_button.setOnClickListener { rtcClient.call(sdpObserver) }
}
private fun createSignallingClientListener() = object : SignallingClientListener {
override fun onConnectionEstablished() {
call_button.isClickable = true
}
override fun onOfferReceived(description: SessionDescription) {
rtcClient.onRemoteSessionReceived(description)
rtcClient.answer(sdpObserver)
remote_view_loading.isGone = true
}
override fun onAnswerReceived(description: SessionDescription) {
rtcClient.onRemoteSessionReceived(description)
remote_view_loading.isGone = true
}
override fun onIceCandidateReceived(iceCandidate: IceCandidate) {
rtcClient.addIceCandidate(iceCandidate)
}
}
private fun requestCameraPermission(dialogShown: Boolean = false) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, CAMERA_PERMISSION) && !dialogShown) {
showPermissionRationaleDialog()
} else {
ActivityCompat.requestPermissions(this, arrayOf(CAMERA_PERMISSION), CAMERA_PERMISSION_REQUEST_CODE)
}
}
private fun showPermissionRationaleDialog() {
AlertDialog.Builder(this)
.setTitle("Camera Permission Required")
.setMessage("This app need the camera to function")
.setPositiveButton("Grant") { dialog, _ ->
dialog.dismiss()
requestCameraPermission(true)
}
.setNegativeButton("Deny") { dialog, _ ->
dialog.dismiss()
onCameraPermissionDenied()
}
.show()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
onCameraPermissionGranted()
} else {
onCameraPermissionDenied()
}
}
private fun onCameraPermissionDenied() {
Toast.makeText(this, "Camera Permission Denied", Toast.LENGTH_LONG).show()
}
override fun onDestroy() {
signallingClient.destroy()
super.onDestroy()
}
}