RemoteParticipant.kt
12.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package io.livekit.android.room.participant
import com.github.ajalt.timberkt.Timber
import io.livekit.android.room.track.*
import io.livekit.android.util.CloseableCoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import livekit.LivekitModels
import org.webrtc.AudioTrack
import org.webrtc.DataChannel
import org.webrtc.MediaStreamTrack
import org.webrtc.VideoTrack
import java.nio.ByteBuffer
class RemoteParticipant(
sid: Sid, name: String? = null
) : Participant(sid, name), RemoteDataTrack.Listener {
/**
* @suppress
*/
constructor(info: LivekitModels.ParticipantInfo) : this(Sid(info.sid), info.identity) {
updateFromInfo(info)
}
val remoteAudioTracks
get() = audioTracks.values.toList()
val remoteVideoTracks
get() = videoTracks.values.toList()
val remoteDataTracks
get() = dataTracks.values.toList()
var listener: Listener? = null
var participantInfo: LivekitModels.ParticipantInfo? = null
val hasInfo
get() = participantInfo != null
private val coroutineScope = CloseableCoroutineScope(SupervisorJob())
fun getTrackPublication(sid: Track.Sid): RemoteTrackPublication? =
tracks[sid] as? RemoteTrackPublication
/**
* @suppress
*/
fun updateFromInfo(info: LivekitModels.ParticipantInfo) {
val hadInfo = hasInfo
sid = Sid(info.sid)
name = info.identity
participantInfo = info
metadata = info.metadata
val validTrackPublication = mutableMapOf<Track.Sid, RemoteTrackPublication>()
val newTrackPublications = mutableMapOf<Track.Sid, RemoteTrackPublication>()
for (trackInfo in info.tracksList) {
val trackSid = Track.Sid(trackInfo.sid)
var publication = getTrackPublication(trackSid)
if (publication == null) {
publication = when (trackInfo.type) {
LivekitModels.TrackType.AUDIO -> RemoteAudioTrackPublication(trackInfo)
LivekitModels.TrackType.VIDEO -> RemoteVideoTrackPublication(trackInfo)
LivekitModels.TrackType.DATA -> RemoteDataTrackPublication(trackInfo)
LivekitModels.TrackType.UNRECOGNIZED -> throw TrackException.InvalidTrackTypeException()
null -> throw NullPointerException("trackInfo.type is null")
}
newTrackPublications[trackSid] = publication
addTrack(publication)
} else {
publication.updateFromInfo(trackInfo)
}
validTrackPublication[trackSid] = publication
}
if (hadInfo) {
for (publication in newTrackPublications.values) {
sendTrackPublishedEvent(publication)
}
}
val invalidKeys = tracks.keys - validTrackPublication.keys
for (invalidKey in invalidKeys) {
val publication = tracks[invalidKey] ?: continue
unpublishTrack(publication.trackSid, true)
}
}
/**
* @suppress
*/
fun addSubscribedMediaTrack(rtcTrack: MediaStreamTrack, sid: Track.Sid, triesLeft: Int = 20) {
val publication = getTrackPublication(sid)
val track: Track = when (val kind = rtcTrack.kind()) {
KIND_AUDIO -> RemoteAudioTrack(sid = sid, rtcTrack = rtcTrack as AudioTrack, name = "")
KIND_VIDEO -> RemoteVideoTrack(sid = sid, rtcTrack = rtcTrack as VideoTrack, name = "")
else -> throw TrackException.InvalidTrackTypeException("invalid track type: $kind")
}
if (publication == null) {
if (triesLeft == 0) {
val message = "Could not find published track with sid: $sid"
val exception = TrackException.InvalidTrackStateException(message)
Timber.e { "remote participant ${this.sid} --- $message" }
when (rtcTrack.kind()) {
KIND_AUDIO -> {
listener?.onFailToSubscribe(
audioTrack = track as RemoteAudioTrack,
exception = exception,
participant = this
)
}
KIND_VIDEO -> {
listener?.onFailToSubscribe(
videoTrack = track as RemoteVideoTrack,
exception = exception,
participant = this
)
}
}
} else {
coroutineScope.launch {
delay(150)
addSubscribedMediaTrack(rtcTrack, sid, triesLeft - 1)
}
}
return
}
val remoteTrack = track as RemoteTrack
publication.track = track
track.name = publication.trackName
remoteTrack.sid = publication.trackSid
when (publication) {
is RemoteAudioTrackPublication -> listener?.onSubscribe(publication, this)
is RemoteVideoTrackPublication -> listener?.onSubscribe(publication, this)
else -> throw TrackException.InvalidTrackTypeException()
}
}
/**
* @suppress
*/
fun addSubscribedDataTrack(rtcTrack: DataChannel, sid: Track.Sid, name: String) {
val track = RemoteDataTrack(sid, name, rtcTrack)
var publication = getTrackPublication(sid) as? RemoteDataTrackPublication
if (publication != null) {
publication.track = track
} else {
val trackInfo = LivekitModels.TrackInfo.newBuilder()
.setSid(sid.sid)
.setName(name)
.setType(LivekitModels.TrackType.DATA)
.build()
publication = RemoteDataTrackPublication(info = trackInfo, track = track)
addTrack(publication)
if (hasInfo) {
sendTrackPublishedEvent(publication)
}
}
rtcTrack.registerObserver(object : DataChannel.Observer {
override fun onBufferedAmountChange(previousAmount: Long) {}
override fun onStateChange() {
val newState = rtcTrack.state()
if (newState == DataChannel.State.CLOSED) {
listener?.onUnsubscribe(publication, this@RemoteParticipant)
}
}
override fun onMessage(buffer: DataChannel.Buffer) {
listener?.onReceive(buffer.data, publication, this@RemoteParticipant)
}
})
listener?.onSubscribe(dataTrack = publication, participant = this)
}
fun unpublishTrack(trackSid: Track.Sid, sendUnpublish: Boolean = false) {
val publication = tracks.remove(trackSid) ?: return
when (publication) {
is RemoteAudioTrackPublication -> audioTracks.remove(trackSid)
is RemoteVideoTrackPublication -> videoTracks.remove(trackSid)
is RemoteDataTrackPublication -> {
dataTracks.remove(trackSid)
publication.dataTrack?.rtcTrack?.unregisterObserver()
}
else -> throw TrackException.InvalidTrackTypeException()
}
if (publication.track != null) {
// TODO: need to stop track?
publication.track
sendTrackUnsubscribedEvent(publication)
}
if (sendUnpublish) {
sendTrackUnpublishedEvent(publication)
}
}
private fun sendTrackUnsubscribedEvent(publication: TrackPublication) {
when (publication) {
is RemoteAudioTrackPublication -> listener?.onUnsubscribe(publication, this)
is RemoteVideoTrackPublication -> listener?.onUnsubscribe(publication, this)
is RemoteDataTrackPublication -> listener?.onUnsubscribe(publication, this)
else -> throw TrackException.InvalidTrackTypeException()
}
}
private fun sendTrackUnpublishedEvent(publication: TrackPublication) {
when (publication) {
is RemoteAudioTrackPublication -> listener?.onUnpublish(publication, this)
is RemoteVideoTrackPublication -> listener?.onUnpublish(publication, this)
is RemoteDataTrackPublication -> listener?.onUnpublish(publication, this)
else -> throw TrackException.InvalidTrackTypeException()
}
}
private fun sendTrackPublishedEvent(publication: RemoteTrackPublication) {
when (publication) {
is RemoteAudioTrackPublication -> listener?.onPublish(publication, this)
is RemoteVideoTrackPublication -> listener?.onPublish(publication, this)
is RemoteDataTrackPublication -> listener?.onPublish(publication, this)
else -> throw TrackException.InvalidTrackTypeException()
}
}
override fun onReceiveString(message: String, dataTrack: DataTrack) {
TODO("Not yet implemented")
}
override fun onReceiveData(message: DataChannel.Buffer, dataTrack: DataTrack) {
TODO("Not yet implemented")
}
companion object {
private const val KIND_AUDIO = "audio"
private const val KIND_VIDEO = "video"
}
interface Listener {
fun onPublish(audioTrack: RemoteAudioTrackPublication, participant: RemoteParticipant) {}
fun onUnpublish(audioTrack: RemoteAudioTrackPublication, participant: RemoteParticipant) {}
fun onPublish(videoTrack: RemoteVideoTrackPublication, participant: RemoteParticipant) {}
fun onUnpublish(videoTrack: RemoteVideoTrackPublication, participant: RemoteParticipant) {}
fun onPublish(dataTrack: RemoteDataTrackPublication, participant: RemoteParticipant) {}
fun onUnpublish(dataTrack: RemoteDataTrackPublication, participant: RemoteParticipant) {}
fun onEnable(audioTrack: RemoteAudioTrackPublication, participant: RemoteParticipant) {}
fun onDisable(audioTrack: RemoteAudioTrackPublication, participant: RemoteParticipant) {}
fun onEnable(videoTrack: RemoteVideoTrackPublication, participant: RemoteParticipant) {}
fun onDisable(videoTrack: RemoteVideoTrackPublication, participant: RemoteParticipant) {}
fun onSubscribe(audioTrack: RemoteAudioTrackPublication, participant: RemoteParticipant) {}
fun onFailToSubscribe(
audioTrack: RemoteAudioTrack,
exception: Exception,
participant: RemoteParticipant
) {
}
fun onUnsubscribe(
audioTrack: RemoteAudioTrackPublication,
participant: RemoteParticipant
) {
}
fun onSubscribe(videoTrack: RemoteVideoTrackPublication, participant: RemoteParticipant) {}
fun onFailToSubscribe(
videoTrack: RemoteVideoTrack,
exception: Exception,
participant: RemoteParticipant
) {
}
fun onUnsubscribe(
videoTrack: RemoteVideoTrackPublication,
participant: RemoteParticipant
) {
}
fun onSubscribe(dataTrack: RemoteDataTrackPublication, participant: RemoteParticipant) {}
fun onFailToSubscribe(
dataTrack: RemoteDataTrackPublication,
exception: Exception,
participant: RemoteParticipant
) {
}
fun onUnsubscribe(dataTrack: RemoteDataTrackPublication, participant: RemoteParticipant) {}
fun onReceive(
data: ByteBuffer,
dataTrack: RemoteDataTrackPublication,
participant: RemoteParticipant
) {
}
//fun networkQualityDidChange(networkQualityLevel: NetworkQualityLevel, participant: remoteParticipant)
fun switchedOffVideo(track: RemoteVideoTrack, participant: RemoteParticipant) {}
fun switchedOnVideo(track: RemoteVideoTrack, participant: RemoteParticipant) {}
// fun onChangePublishPriority(videoTrack: RemoteVideoTrackPublication, priority: PublishPriority, participant: RemoteParticipant)
// fun onChangePublishPriority(audioTrack: RemoteAudioTrackPublication, priority: PublishPriority, participant: RemoteParticipant)
// fun onChangePublishPriority(dataTrack: RemoteDataTrackPublication, priority: PublishPriority, participant: RemoteParticipant)
}
}