RemoteParticipant.kt
7.8 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
/*
* 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.room.participant
import io.livekit.android.events.ParticipantEvent
import io.livekit.android.room.SignalClient
import io.livekit.android.room.track.*
import io.livekit.android.util.CloseableCoroutineScope
import io.livekit.android.util.LKLog
import io.livekit.android.webrtc.RTCStatsGetter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import livekit.LivekitModels
import livekit.LivekitRtc
import org.webrtc.AudioTrack
import org.webrtc.MediaStreamTrack
import org.webrtc.RtpReceiver
import org.webrtc.VideoTrack
class RemoteParticipant(
sid: String,
identity: String? = null,
val signalClient: SignalClient,
private val ioDispatcher: CoroutineDispatcher,
defaultDispatcher: CoroutineDispatcher,
) : Participant(sid, identity, defaultDispatcher) {
/**
* Note: This constructor does not update all info due to event listener race conditions.
*
* Callers are responsible for calling through to [updateFromInfo] once ready.
*
* @suppress
*/
constructor(
info: LivekitModels.ParticipantInfo,
signalClient: SignalClient,
ioDispatcher: CoroutineDispatcher,
defaultDispatcher: CoroutineDispatcher,
) : this(
info.sid,
info.identity,
signalClient,
ioDispatcher,
defaultDispatcher
) {
super.updateFromInfo(info)
}
private val coroutineScope = CloseableCoroutineScope(defaultDispatcher + SupervisorJob())
fun getTrackPublication(sid: String): RemoteTrackPublication? = tracks[sid] as? RemoteTrackPublication
/**
* @suppress
*/
override fun updateFromInfo(info: LivekitModels.ParticipantInfo) {
super.updateFromInfo(info)
val validTrackPublication = mutableMapOf<String, RemoteTrackPublication>()
val newTrackPublications = mutableMapOf<String, RemoteTrackPublication>()
for (trackInfo in info.tracksList) {
val trackSid = trackInfo.sid
var publication = getTrackPublication(trackSid)
if (publication == null) {
publication = RemoteTrackPublication(
trackInfo,
participant = this,
ioDispatcher = ioDispatcher
)
newTrackPublications[trackSid] = publication
addTrackPublication(publication)
} else {
publication.updateFromInfo(trackInfo)
}
validTrackPublication[trackSid] = publication
}
for (publication in newTrackPublications.values) {
internalListener?.onTrackPublished(publication, this)
listener?.onTrackPublished(publication, this)
eventBus.postEvent(ParticipantEvent.TrackPublished(this, publication), scope)
}
val invalidKeys = tracks.keys - validTrackPublication.keys
for (invalidKey in invalidKeys) {
val publication = tracks[invalidKey] ?: continue
unpublishTrack(publication.sid, true)
}
}
/**
* @suppress
*/
fun addSubscribedMediaTrack(
mediaTrack: MediaStreamTrack,
sid: String,
statsGetter: RTCStatsGetter,
receiver: RtpReceiver,
autoManageVideo: Boolean = false,
triesLeft: Int = 20,
) {
val publication = getTrackPublication(sid)
// We may receive subscribed tracks before publications come in. Retry until then.
if (publication == null) {
if (triesLeft == 0) {
val message = "Could not find published track with sid: $sid"
val exception = TrackException.InvalidTrackStateException(message)
LKLog.e { "remote participant ${this.sid} --- $message" }
internalListener?.onTrackSubscriptionFailed(sid, exception, this)
listener?.onTrackSubscriptionFailed(sid, exception, this)
eventBus.postEvent(ParticipantEvent.TrackSubscriptionFailed(this, sid, exception), scope)
} else {
coroutineScope.launch {
delay(150)
addSubscribedMediaTrack(mediaTrack, sid, statsGetter, receiver = receiver, autoManageVideo, triesLeft - 1)
}
}
return
}
val track: Track = when (val kind = mediaTrack.kind()) {
KIND_AUDIO -> RemoteAudioTrack(rtcTrack = mediaTrack as AudioTrack, name = "", receiver = receiver)
KIND_VIDEO -> RemoteVideoTrack(
rtcTrack = mediaTrack as VideoTrack,
name = "",
autoManageVideo = autoManageVideo,
dispatcher = ioDispatcher,
receiver = receiver
)
else -> throw TrackException.InvalidTrackTypeException("invalid track type: $kind")
}
track.statsGetter = statsGetter
publication.track = track
publication.subscriptionAllowed = true
track.name = publication.name
track.sid = publication.sid
addTrackPublication(publication)
track.start()
// TODO: how does mediatrack send ended event?
internalListener?.onTrackSubscribed(track, publication, this)
listener?.onTrackSubscribed(track, publication, this)
eventBus.postEvent(ParticipantEvent.TrackSubscribed(this, track, publication), scope)
}
fun unpublishTrack(trackSid: String, sendUnpublish: Boolean = false) {
val publication = tracks[trackSid] as? RemoteTrackPublication ?: return
tracks = tracks.toMutableMap().apply { remove(trackSid) }
val track = publication.track
if (track != null) {
track.stop()
internalListener?.onTrackUnsubscribed(track, publication, this)
listener?.onTrackUnsubscribed(track, publication, this)
eventBus.postEvent(ParticipantEvent.TrackUnsubscribed(this, track, publication), scope)
}
if (sendUnpublish) {
internalListener?.onTrackUnpublished(publication, this)
listener?.onTrackUnpublished(publication, this)
eventBus.postEvent(ParticipantEvent.TrackUnpublished(this, publication), scope)
}
track?.dispose()
publication.track = null
}
internal fun onSubscriptionPermissionUpdate(subscriptionPermissionUpdate: LivekitRtc.SubscriptionPermissionUpdate) {
val pub = tracks[subscriptionPermissionUpdate.trackSid] as? RemoteTrackPublication ?: return
if (pub.subscriptionAllowed != subscriptionPermissionUpdate.allowed) {
pub.subscriptionAllowed = subscriptionPermissionUpdate.allowed
eventBus.postEvent(
ParticipantEvent.TrackSubscriptionPermissionChanged(this, pub, pub.subscriptionAllowed),
coroutineScope
)
}
}
// Internal methods just for posting events.
internal fun onDataReceived(data: ByteArray, topic: String?) {
listener?.onDataReceived(data, this)
eventBus.postEvent(ParticipantEvent.DataReceived(this, data, topic), scope)
}
companion object {
private const val KIND_AUDIO = "audio"
private const val KIND_VIDEO = "video"
}
}