RemoteTrackPublication.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
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
/*
* 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.track
import io.livekit.android.dagger.InjectionNames
import io.livekit.android.events.TrackEvent
import io.livekit.android.events.collect
import io.livekit.android.room.participant.RemoteParticipant
import io.livekit.android.util.debounce
import io.livekit.android.util.invoke
import kotlinx.coroutines.*
import livekit.LivekitModels
import javax.inject.Named
class RemoteTrackPublication(
info: LivekitModels.TrackInfo,
track: Track? = null,
participant: RemoteParticipant,
@Named(InjectionNames.DISPATCHER_IO)
private val ioDispatcher: CoroutineDispatcher,
) : TrackPublication(info, track, participant) {
@OptIn(FlowPreview::class)
override var track: Track?
get() = super.track
set(value) {
if (value != super.track) {
trackJob?.cancel()
trackJob = null
}
super.track = value
if (value != null) {
trackJob = CoroutineScope(ioDispatcher).launch {
value.events.collect {
when (it) {
is TrackEvent.VisibilityChanged -> handleVisibilityChanged(it.isVisible)
is TrackEvent.VideoDimensionsChanged -> handleVideoDimensionsChanged(it.newDimensions)
is TrackEvent.StreamStateChanged -> handleStreamStateChanged(it)
}
}
}
if (value is RemoteVideoTrack && value.autoManageVideo) {
handleVideoDimensionsChanged(value.lastDimensions)
handleVisibilityChanged(value.lastVisibility)
}
}
}
private var trackJob: Job? = null
private var unsubscribed: Boolean = false
private var disabled: Boolean = false
private var videoQuality: LivekitModels.VideoQuality? = LivekitModels.VideoQuality.HIGH
private var videoDimensions: Track.Dimensions? = null
private var fps: Int? = null
var subscriptionAllowed: Boolean = true
internal set
val isAutoManaged: Boolean
get() = (track as? RemoteVideoTrack)?.autoManageVideo ?: false
/**
* Returns true if track is subscribed, and ready for playback
*
* @see [subscriptionStatus]
*/
override val subscribed: Boolean
get() {
if (unsubscribed || !subscriptionAllowed) {
return false
}
return super.subscribed
}
val subscriptionStatus: SubscriptionStatus
get() {
return if (!unsubscribed || track == null) {
SubscriptionStatus.UNSUBSCRIBED
} else if (!subscriptionAllowed) {
SubscriptionStatus.SUBSCRIBED_AND_NOT_ALLOWED
} else {
SubscriptionStatus.SUBSCRIBED
}
}
override var muted: Boolean
get() = super.muted
set(v) {
if (super.muted == v) {
return
}
super.muted = v
val participant = this.participant.get() as? RemoteParticipant ?: return
if (v) {
participant.onTrackMuted(this)
} else {
participant.onTrackUnmuted(this)
}
}
/**
* Subscribe or unsubscribe from this track
*/
fun setSubscribed(subscribed: Boolean) {
unsubscribed = !subscribed
val participant = this.participant.get() as? RemoteParticipant ?: return
val participantTracks = with(LivekitModels.ParticipantTracks.newBuilder()) {
participantSid = participant.sid
addTrackSids(sid)
build()
}
participant.signalClient.sendUpdateSubscription(!unsubscribed, participantTracks)
}
/**
* disable server from sending down data for this track
*
* this is useful when the participant is off screen, you may disable streaming down their
* video to reduce bandwidth requirements
*/
fun setEnabled(enabled: Boolean) {
if (isAutoManaged || !subscribed || enabled == !disabled) {
return
}
disabled = !enabled
sendUpdateTrackSettings.invoke()
}
/**
* For tracks that support simulcasting, directly adjust subscribed quality
*
* This indicates the highest quality the client can accept. If network bandwidth does not
* allow, server will automatically reduce quality to optimize for uninterrupted video.
*
* Will override previous calls to [setVideoDimensions].
*/
fun setVideoQuality(quality: LivekitModels.VideoQuality) {
if (isAutoManaged ||
!subscribed ||
quality == videoQuality ||
track !is VideoTrack
) {
return
}
videoQuality = quality
videoDimensions = null
sendUpdateTrackSettings.invoke()
}
/**
* Update the dimensions that the server will use for determining the video quality to send down.
*
* Will override previous calls to [setVideoQuality].
*/
fun setVideoDimensions(dimensions: Track.Dimensions) {
if (isAutoManaged ||
!subscribed ||
videoDimensions == dimensions ||
track !is VideoTrack
) {
return
}
videoQuality = null
videoDimensions = dimensions
sendUpdateTrackSettings.invoke()
}
/**
* Update the fps that the server will use for determining the video quality to send down.
*/
fun setVideoFps(fps: Int?) {
if (isAutoManaged ||
!subscribed ||
this.fps == fps ||
track !is VideoTrack
) {
return
}
this.fps = fps
sendUpdateTrackSettings.invoke()
}
private fun handleVisibilityChanged(isVisible: Boolean) {
disabled = !isVisible
sendUpdateTrackSettings.invoke()
}
private fun handleVideoDimensionsChanged(newDimensions: Track.Dimensions) {
videoDimensions = newDimensions
sendUpdateTrackSettings.invoke()
}
private fun handleStreamStateChanged(trackEvent: TrackEvent.StreamStateChanged) {
participant.get()?.onTrackStreamStateChanged(trackEvent)
}
// Debounce just in case multiple settings get changed at once.
internal val sendUpdateTrackSettings = debounce<Unit, Unit>(100L, CoroutineScope(ioDispatcher)) {
sendUpdateTrackSettingsImpl()
}
private fun sendUpdateTrackSettingsImpl() {
val participant = this.participant.get() as? RemoteParticipant ?: return
val rtcTrack = track?.rtcTrack
if (rtcTrack is org.webrtc.VideoTrack) {
rtcTrack.setShouldReceive(!disabled)
}
participant.signalClient.sendUpdateTrackSettings(
sid,
disabled,
videoDimensions,
videoQuality,
fps,
)
}
enum class SubscriptionStatus {
/**
* Has a valid track, receiving data.
*/
SUBSCRIBED,
/**
* Has a track, but no data will be received due to permissions.
*/
SUBSCRIBED_AND_NOT_ALLOWED,
/**
* Not subscribed.
*/
UNSUBSCRIBED,
}
}