CallViewModel.kt
9.5 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package io.livekit.android.sample
import android.app.Application
import android.content.Intent
import android.media.projection.MediaProjectionManager
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.github.ajalt.timberkt.Timber
import io.livekit.android.LiveKit
import io.livekit.android.LiveKitOverrides
import io.livekit.android.RoomOptions
import io.livekit.android.audio.AudioSwitchHandler
import io.livekit.android.events.RoomEvent
import io.livekit.android.events.collect
import io.livekit.android.room.Room
import io.livekit.android.room.participant.LocalParticipant
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.participant.RemoteParticipant
import io.livekit.android.room.track.*
import io.livekit.android.util.flow
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import livekit.LivekitRtc
class CallViewModel(
val url: String,
val token: String,
application: Application
) : AndroidViewModel(application) {
val audioHandler = AudioSwitchHandler(application)
val room = LiveKit.create(
appContext = application,
options = RoomOptions(adaptiveStream = true, dynacast = true),
overrides = LiveKitOverrides(
audioHandler = audioHandler
)
)
val participants = room::remoteParticipants.flow
.map { remoteParticipants ->
listOf<Participant>(room.localParticipant) +
remoteParticipants
.keys
.sortedBy { it }
.mapNotNull { remoteParticipants[it] }
}
private val mutableError = MutableStateFlow<Throwable?>(null)
val error = mutableError.hide()
private val mutablePrimarySpeaker = MutableStateFlow<Participant?>(null)
val primarySpeaker: StateFlow<Participant?> = mutablePrimarySpeaker
val activeSpeakers = room::activeSpeakers.flow
private var localScreencastTrack: LocalScreencastVideoTrack? = null
// Controls
private val mutableMicEnabled = MutableLiveData(true)
val micEnabled = mutableMicEnabled.hide()
private val mutableCameraEnabled = MutableLiveData(true)
val cameraEnabled = mutableCameraEnabled.hide()
private val mutableFlipVideoButtonEnabled = MutableLiveData(true)
val flipButtonVideoEnabled = mutableFlipVideoButtonEnabled.hide()
private val mutableScreencastEnabled = MutableLiveData(false)
val screenshareEnabled = mutableScreencastEnabled.hide()
// Emits a string whenever a data message is received.
private val mutableDataReceived = MutableSharedFlow<String>()
val dataReceived = mutableDataReceived
// Whether other participants are allowed to subscribe to this participant's tracks.
private val mutablePermissionAllowed = MutableStateFlow(true)
val permissionAllowed = mutablePermissionAllowed.hide()
init {
viewModelScope.launch {
// Collect any errors.
launch {
error.collect { Timber.e(it) }
}
// Handle any changes in speakers.
launch {
combine(participants, activeSpeakers) { participants, speakers -> participants to speakers }
.collect { (participantsList, speakers) ->
handlePrimarySpeaker(
participantsList,
speakers,
room
)
}
}
launch {
// Handle room events.
room.events.collect {
when (it) {
is RoomEvent.FailedToConnect -> mutableError.value = it.error
is RoomEvent.DataReceived -> {
val identity = it.participant.identity ?: ""
val message = it.data.toString(Charsets.UTF_8)
mutableDataReceived.emit("$identity: $message")
}
else -> {}
}
}
}
connectToRoom()
}
}
private suspend fun connectToRoom() {
try {
room.connect(
url = url,
token = token,
)
// Create and publish audio/video tracks
val localParticipant = room.localParticipant
localParticipant.setMicrophoneEnabled(true)
mutableMicEnabled.postValue(localParticipant.isMicrophoneEnabled())
localParticipant.setCameraEnabled(true)
mutableCameraEnabled.postValue(localParticipant.isCameraEnabled())
// Update the speaker
handlePrimarySpeaker(emptyList(), emptyList(), room)
} catch (e: Throwable) {
mutableError.value = e
}
}
private fun handlePrimarySpeaker(participantsList: List<Participant>, speakers: List<Participant>, room: Room?) {
var speaker = mutablePrimarySpeaker.value
// If speaker is local participant (due to defaults),
// attempt to find another remote speaker to replace with.
if (speaker is LocalParticipant) {
val remoteSpeaker = participantsList
.filterIsInstance<RemoteParticipant>() // Try not to display local participant as speaker.
.firstOrNull()
if (remoteSpeaker != null) {
speaker = remoteSpeaker
}
}
// If previous primary speaker leaves
if (!participantsList.contains(speaker)) {
// Default to another person in room, or local participant.
speaker = participantsList.filterIsInstance<RemoteParticipant>()
.firstOrNull()
?: room?.localParticipant
}
if (speakers.isNotEmpty() && !speakers.contains(speaker)) {
val remoteSpeaker = speakers
.filterIsInstance<RemoteParticipant>() // Try not to display local participant as speaker.
.firstOrNull()
if (remoteSpeaker != null) {
speaker = remoteSpeaker
}
}
mutablePrimarySpeaker.value = speaker
}
/**
* Start a screen capture with the result intent from
* [MediaProjectionManager.createScreenCaptureIntent]
*/
fun startScreenCapture(mediaProjectionPermissionResultData: Intent) {
val localParticipant = room.localParticipant
viewModelScope.launch {
val screencastTrack =
localParticipant.createScreencastTrack(mediaProjectionPermissionResultData = mediaProjectionPermissionResultData)
localParticipant.publishVideoTrack(
screencastTrack
)
// Must start the foreground prior to startCapture.
screencastTrack.startForegroundService(null, null)
screencastTrack.startCapture()
this@CallViewModel.localScreencastTrack = screencastTrack
mutableScreencastEnabled.postValue(screencastTrack.enabled)
}
}
fun stopScreenCapture() {
viewModelScope.launch {
localScreencastTrack?.let { localScreencastVideoTrack ->
localScreencastVideoTrack.stop()
room.localParticipant.unpublishTrack(localScreencastVideoTrack)
mutableScreencastEnabled.postValue(localScreencastTrack?.enabled ?: false)
}
}
}
override fun onCleared() {
super.onCleared()
room.disconnect()
}
fun setMicEnabled(enabled: Boolean) {
viewModelScope.launch {
room.localParticipant.setMicrophoneEnabled(enabled)
mutableMicEnabled.postValue(enabled)
}
}
fun setCameraEnabled(enabled: Boolean) {
viewModelScope.launch {
room.localParticipant.setCameraEnabled(enabled)
mutableCameraEnabled.postValue(enabled)
}
}
fun flipCamera() {
val videoTrack = room.localParticipant.getTrackPublication(Track.Source.CAMERA)
?.track as? LocalVideoTrack
?: return
val newOptions = when (videoTrack.options.position) {
CameraPosition.FRONT -> LocalVideoTrackOptions(position = CameraPosition.BACK)
CameraPosition.BACK -> LocalVideoTrackOptions(position = CameraPosition.FRONT)
else -> LocalVideoTrackOptions()
}
videoTrack.restartTrack(newOptions)
}
fun dismissError() {
mutableError.value = null
}
fun sendData(message: String) {
viewModelScope.launch {
room.localParticipant.publishData(message.toByteArray(Charsets.UTF_8))
}
}
fun toggleSubscriptionPermissions() {
mutablePermissionAllowed.value = !mutablePermissionAllowed.value
room.localParticipant.setTrackSubscriptionPermissions(mutablePermissionAllowed.value)
}
// Debug functions
fun simulateMigration() {
room.sendSimulateScenario(
LivekitRtc.SimulateScenario.newBuilder()
.setMigration(true)
.build()
)
}
fun reconnect() {
Timber.e { "Reconnecting." }
mutablePrimarySpeaker.value = null
room.disconnect()
viewModelScope.launch {
connectToRoom()
}
}
}
private fun <T> LiveData<T>.hide(): LiveData<T> = this
private fun <T> MutableStateFlow<T>.hide(): StateFlow<T> = this
private fun <T> Flow<T>.hide(): Flow<T> = this