RemoteParticipant.kt
6.6 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
package io.livekit.android.room.participant
import com.github.ajalt.timberkt.Timber
import io.livekit.android.room.RTCClient
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
class RemoteParticipant(
val rtcClient: RTCClient,
sid: String, name: String? = null,
) : Participant(sid, name) {
/**
* @suppress
*/
constructor(rtcClient: RTCClient, info: LivekitModels.ParticipantInfo) : this(rtcClient, info.sid, info.identity) {
updateFromInfo(info)
}
private val coroutineScope = CloseableCoroutineScope(SupervisorJob())
fun getTrackPublication(sid: String): RemoteTrackPublication? = tracks[sid] as? RemoteTrackPublication
/**
* @suppress
*/
override fun updateFromInfo(info: LivekitModels.ParticipantInfo) {
val hadInfo = hasInfo
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)
newTrackPublications[trackSid] = publication
addTrackPublication(publication)
} else {
publication.updateFromInfo(trackInfo)
}
validTrackPublication[trackSid] = publication
}
if (hadInfo) {
for (publication in newTrackPublications.values) {
internalListener?.onTrackPublished(publication, this)
listener?.onTrackPublished(publication, this)
}
}
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, triesLeft: Int = 20) {
val publication = getTrackPublication(sid)
val track: Track = when (val kind = mediaTrack.kind()) {
KIND_AUDIO -> AudioTrack(rtcTrack = mediaTrack as AudioTrack, name = "")
KIND_VIDEO -> VideoTrack(rtcTrack = mediaTrack 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" }
internalListener?.onTrackSubscriptionFailed(sid, exception, this)
listener?.onTrackSubscriptionFailed(sid, exception, this)
} else {
coroutineScope.launch {
delay(150)
addSubscribedMediaTrack(mediaTrack, sid, triesLeft - 1)
}
}
return
}
publication.track = track
track.name = publication.name
track.sid = publication.sid
addTrackPublication(publication)
// TODO: how does mediatrack send ended event?
internalListener?.onTrackSubscribed(track, publication, this)
listener?.onTrackSubscribed(track, publication, this)
}
/**
* @suppress
*/
fun addSubscribedDataTrack(dataChannel: DataChannel, sid: String, name: String) {
val track = DataTrack(name, dataChannel)
var publication = getTrackPublication(sid)
if (publication == null) {
val trackInfo = LivekitModels.TrackInfo.newBuilder()
.setSid(sid)
.setName(name)
.setType(LivekitModels.TrackType.DATA)
.build()
publication = RemoteTrackPublication(info = trackInfo, participant = this)
addTrackPublication(publication)
if (hasInfo) {
internalListener?.onTrackPublished(publication, this)
listener?.onTrackPublished(publication, this)
}
}
publication.track = track
track.sid = publication.sid
dataChannel.registerObserver(object : DataChannel.Observer {
override fun onBufferedAmountChange(previousAmount: Long) {}
override fun onStateChange() {
val newState = dataChannel.state()
if (newState == DataChannel.State.CLOSED) {
publication.track = null
internalListener?.onTrackUnsubscribed(track, publication, this@RemoteParticipant)
listener?.onTrackUnsubscribed(track, publication, this@RemoteParticipant)
}
}
override fun onMessage(buffer: DataChannel.Buffer) {
internalListener?.onDataReceived(buffer.data, track, this@RemoteParticipant)
listener?.onDataReceived(buffer.data, track, this@RemoteParticipant)
}
})
internalListener?.onTrackSubscribed(track, publication, participant = this)
listener?.onTrackSubscribed(track, publication, this)
}
fun unpublishTrack(trackSid: String, sendUnpublish: Boolean = false) {
val publication = tracks.remove(trackSid) as? RemoteTrackPublication ?: return
when (publication.kind) {
LivekitModels.TrackType.AUDIO -> audioTracks.remove(trackSid)
LivekitModels.TrackType.VIDEO -> videoTracks.remove(trackSid)
LivekitModels.TrackType.DATA -> dataTracks.remove(trackSid)
else -> throw TrackException.InvalidTrackTypeException()
}
val track = publication.track
if (track != null) {
track.stop()
internalListener?.onTrackUnsubscribed(track, publication, this)
listener?.onTrackUnsubscribed(track, publication, this)
}
if (sendUnpublish) {
internalListener?.onTrackUnpublished(publication, this)
listener?.onTrackUnpublished(publication, this)
}
}
companion object {
private const val KIND_AUDIO = "audio"
private const val KIND_VIDEO = "video"
}
}