ParticipantItem.kt
5.2 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
/*
* Copyright 2023 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.
*/
package io.livekit.android.videoencodedecode
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import io.livekit.android.composesample.ui.theme.BlueMain
import io.livekit.android.composesample.ui.theme.NoVideoBackground
import io.livekit.android.room.Room
import io.livekit.android.room.participant.ConnectionQuality
import io.livekit.android.room.participant.Participant
import io.livekit.android.sample.common.R
import io.livekit.android.util.flow
/**
* Widget for displaying a participant.
*/
@Composable
fun ParticipantItem(
room: Room,
participant: Participant,
modifier: Modifier = Modifier,
isSpeaking: Boolean,
) {
val identity by participant::identity.flow.collectAsState()
val videoTracks by participant::videoTracks.flow.collectAsState()
val audioTracks by participant::audioTracks.flow.collectAsState()
val identityBarPadding = 4.dp
ConstraintLayout(
modifier = modifier.background(NoVideoBackground)
.run {
if (isSpeaking) {
border(2.dp, BlueMain)
} else {
this
}
}
) {
val (videoItem, identityBar, identityText, muteIndicator, connectionIndicator) = createRefs()
VideoItemTrackSelector(
room = room,
participant = participant,
modifier = Modifier.constrainAs(videoItem) {
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
}
)
Surface(
color = Color(0x80000000),
modifier = Modifier.constrainAs(identityBar) {
bottom.linkTo(parent.bottom)
start.linkTo(parent.start)
end.linkTo(parent.end)
width = Dimension.fillToConstraints
height = Dimension.value(30.dp)
}
) {}
Text(
text = identity ?: "",
color = Color.White,
modifier = Modifier.constrainAs(identityText) {
top.linkTo(identityBar.top)
bottom.linkTo(identityBar.bottom)
start.linkTo(identityBar.start, margin = identityBarPadding)
end.linkTo(muteIndicator.end, margin = 10.dp)
width = Dimension.fillToConstraints
height = Dimension.wrapContent
},
)
val isMuted = audioTracks.none { (pub) -> pub.track != null && !pub.muted }
if (isMuted) {
Icon(
painter = painterResource(id = R.drawable.outline_mic_off_24),
contentDescription = "",
tint = Color.Red,
modifier = Modifier.constrainAs(muteIndicator) {
top.linkTo(identityBar.top)
bottom.linkTo(identityBar.bottom)
end.linkTo(identityBar.end, margin = identityBarPadding)
}
)
}
val connectionQuality by participant::connectionQuality.flow.collectAsState()
val connectionIcon = when (connectionQuality) {
ConnectionQuality.EXCELLENT -> R.drawable.wifi_strength_4
ConnectionQuality.GOOD -> R.drawable.wifi_strength_3
ConnectionQuality.POOR -> R.drawable.wifi_strength_alert_outline
ConnectionQuality.UNKNOWN -> R.drawable.wifi_strength_alert_outline
}
if (connectionQuality == ConnectionQuality.POOR) {
Icon(
painter = painterResource(id = connectionIcon),
contentDescription = "",
tint = Color.Red,
modifier = Modifier
.constrainAs(connectionIndicator) {
top.linkTo(parent.top, margin = identityBarPadding)
end.linkTo(parent.end, margin = identityBarPadding)
}
.alpha(0.5f)
)
}
}
}