ParticipantItem.kt
7.1 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
/*
* Copyright 2024-2025 LiveKit, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:OptIn(ExperimentalCoroutinesApi::class)
package io.livekit.android.sample
import android.view.View
import com.github.ajalt.timberkt.Timber
import com.xwray.groupie.viewbinding.BindableItem
import com.xwray.groupie.viewbinding.GroupieViewHolder
import io.livekit.android.room.Room
import io.livekit.android.room.participant.ConnectionQuality
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.track.CameraPosition
import io.livekit.android.room.track.LocalVideoTrack
import io.livekit.android.room.track.Track
import io.livekit.android.room.track.VideoTrack
import io.livekit.android.sample.databinding.ParticipantItemBinding
import io.livekit.android.util.flow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
class ParticipantItem(
private val room: Room,
private val participant: Participant,
private val speakerView: Boolean = false,
) : BindableItem<ParticipantItemBinding>() {
private var boundVideoTrack: VideoTrack? = null
private var coroutineScope: CoroutineScope? = null
override fun initializeViewBinding(view: View): ParticipantItemBinding {
val binding = ParticipantItemBinding.bind(view)
room.initVideoRenderer(binding.renderer)
return binding
}
private fun ensureCoroutineScope() {
if (coroutineScope == null) {
coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
}
}
override fun bind(viewBinding: ParticipantItemBinding, position: Int) {
ensureCoroutineScope()
coroutineScope?.launch {
participant::identity.flow.collect { identity ->
viewBinding.identityText.text = identity?.value
}
}
coroutineScope?.launch {
participant::isSpeaking.flow.collect { isSpeaking ->
if (isSpeaking) {
showFocus(viewBinding)
} else {
hideFocus(viewBinding)
}
}
}
coroutineScope?.launch {
participant::isMicrophoneEnabled.flow
.collect { isMicEnabled ->
viewBinding.muteIndicator.visibility = if (isMicEnabled) View.VISIBLE else View.INVISIBLE
}
}
coroutineScope?.launch {
participant::connectionQuality.flow
.collect { quality ->
viewBinding.connectionQuality.visibility =
if (quality == ConnectionQuality.POOR) View.VISIBLE else View.INVISIBLE
}
}
// observe videoTracks changes.
val videoTrackPubFlow = participant::videoTrackPublications.flow
.map { participant to it }
.flatMapLatest { (participant, videoTracks) ->
// Prioritize any screenshare streams.
val trackPublication = participant.getTrackPublication(Track.Source.SCREEN_SHARE)
?: participant.getTrackPublication(Track.Source.CAMERA)
?: videoTracks.firstOrNull()?.first
flowOf(trackPublication)
}
coroutineScope?.launch {
val videoTrackFlow = videoTrackPubFlow
.flatMapLatestOrNull { pub -> pub::track.flow }
// Configure video view with track
launch {
videoTrackFlow.collectLatest { videoTrack ->
setupVideoIfNeeded(videoTrack as? VideoTrack, viewBinding)
}
}
// For local participants, mirror camera if using front camera.
if (participant == room.localParticipant) {
launch {
videoTrackFlow
.flatMapLatestOrNull { track -> (track as LocalVideoTrack)::options.flow }
.collectLatest { options ->
viewBinding.renderer.setMirror(options?.position == CameraPosition.FRONT)
}
}
}
}
// Handle muted changes
coroutineScope?.launch {
videoTrackPubFlow
.flatMapLatestOrNull { pub -> pub::muted.flow }
.collectLatest { muted ->
viewBinding.renderer.visibleOrInvisible(!(muted ?: true))
}
}
val existingTrack = getVideoTrack()
if (existingTrack != null) {
setupVideoIfNeeded(existingTrack, viewBinding)
}
}
private fun getVideoTrack(): VideoTrack? {
return participant.getTrackPublication(Track.Source.CAMERA)?.track as? VideoTrack
}
private fun setupVideoIfNeeded(videoTrack: VideoTrack?, viewBinding: ParticipantItemBinding) {
if (boundVideoTrack == videoTrack) {
return
}
boundVideoTrack?.removeRenderer(viewBinding.renderer)
boundVideoTrack = videoTrack
Timber.v { "adding renderer to $videoTrack" }
videoTrack?.addRenderer(viewBinding.renderer)
}
override fun unbind(viewHolder: GroupieViewHolder<ParticipantItemBinding>) {
coroutineScope?.cancel()
coroutineScope = null
super.unbind(viewHolder)
boundVideoTrack?.removeRenderer(viewHolder.binding.renderer)
boundVideoTrack = null
}
override fun getLayout(): Int =
if (speakerView) {
R.layout.speaker_view
} else {
R.layout.participant_item
}
}
private fun View.visibleOrGone(visible: Boolean) {
visibility = if (visible) {
View.VISIBLE
} else {
View.GONE
}
}
private fun View.visibleOrInvisible(visible: Boolean) {
visibility = if (visible) {
View.VISIBLE
} else {
View.INVISIBLE
}
}
private fun showFocus(binding: ParticipantItemBinding) {
binding.speakingIndicator.visibility = View.VISIBLE
}
private fun hideFocus(binding: ParticipantItemBinding) {
binding.speakingIndicator.visibility = View.INVISIBLE
}
private inline fun <T, R> Flow<T?>.flatMapLatestOrNull(
crossinline transform: suspend (value: T) -> Flow<R>,
): Flow<R?> {
return flatMapLatest {
if (it == null) {
flowOf(null)
} else {
transform(it)
}
}
}