Skip to content

Commit

Permalink
Add rememberParticipantTrackReferences state function (#8)
Browse files Browse the repository at this point in the history
* Add rememberParticipantTrackReferences state function

Also redo rememberTrackReferences to be tied to participant's track publications' flow to be more robust

* Calculate track references directly off flow values

* Calculate participant track references directly off flow values
  • Loading branch information
davidliu committed Mar 25, 2024
1 parent d317074 commit 23501cd
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright 2024 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.compose.state

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import io.livekit.android.compose.local.ParticipantLocal
import io.livekit.android.compose.local.RoomLocal
import io.livekit.android.compose.local.requireParticipant
import io.livekit.android.compose.local.requireRoom
import io.livekit.android.compose.types.TrackReference
import io.livekit.android.room.Room
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.track.Track
import io.livekit.android.util.flow
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.mapLatest

/**
* Returns an array of TrackReferences for a participant depending the sources provided.
*
* @param sources The sources of the tracks to provide. Defaults to camera and screen share tracks.
* @param participantIdentity The identity of the participant.
* @param usePlaceholders A set of sources to provide placeholders for.
* A placeholder will provide a TrackReference for participants that don't
* yet have a track published for that source. Defaults to no placeholders.
* @param passedRoom The room to use on, or [RoomLocal] if null.
* @param onlySubscribed If true, only return tracks that have been subscribed. Defaults to true.
*/
@Composable
fun rememberParticipantTrackReferences(
sources: List<Track.Source>,
participantIdentity: Participant.Identity,
passedRoom: Room? = null,
usePlaceholders: Set<Track.Source> = emptySet(),
onlySubscribed: Boolean = true,
): List<TrackReference> {
val room = requireRoom(passedRoom)
val participant = room.getParticipantByIdentity(participantIdentity)

return rememberParticipantTrackReferences(
sources = sources,
passedParticipant = participant,
usePlaceholders = usePlaceholders,
onlySubscribed = onlySubscribed
)
}

/**
* Returns an array of TrackReferences for a participant depending the sources provided.
*
* @param sources The sources of the tracks to provide. Defaults to camera and screen share tracks.
* @param usePlaceholders A set of sources to provide placeholders for.
* A placeholder will provide a TrackReference for participants that don't
* yet have a track published for that source. Defaults to no placeholders.
* @param passedParticipant The participant to use on, or [ParticipantLocal] if null/not passed.
* @param onlySubscribed If true, only return tracks that have been subscribed. Defaults to true.
*/
@Composable
fun rememberParticipantTrackReferences(
sources: List<Track.Source> = listOf(
Track.Source.CAMERA,
Track.Source.SCREEN_SHARE
),
usePlaceholders: Set<Track.Source> = emptySet(),
passedParticipant: Participant? = null,
onlySubscribed: Boolean = true,
): List<TrackReference> {
val participant = requireParticipant(passedParticipant)

return participantTrackReferencesFlow(
participant = participant,
sources = sources,
usePlaceholders = usePlaceholders,
onlySubscribed = onlySubscribed
)
.collectAsState(initial = participant.getTrackReferencesBySource(sources, usePlaceholders, onlySubscribed))
.value
}

/**
* A flow of the TrackReferences/placeholders for a participant.
*
* @see rememberTracks
*/
@OptIn(ExperimentalCoroutinesApi::class)
internal fun participantTrackReferencesFlow(
participant: Participant,
sources: List<Track.Source>,
usePlaceholders: Set<Track.Source> = emptySet(),
onlySubscribed: Boolean = true,
): Flow<List<TrackReference>> {
return participant::trackPublications.flow
.mapLatest { trackPubs ->
calculateTrackReferences(
participant = participant,
trackPublications = trackPubs.values,
sources = sources,
usePlaceholders = usePlaceholders,
onlySubscribed = onlySubscribed,
)
}
}

/**
* @see rememberParticipantTrackReferences
*/
fun Participant.getTrackReferencesBySource(
sources: List<Track.Source>,
usePlaceholders: Set<Track.Source> = emptySet(),
onlySubscribed: Boolean = true
): List<TrackReference> {
return calculateTrackReferences(
participant = this,
trackPublications = this.trackPublications.values,
sources = sources,
usePlaceholders = usePlaceholders,
onlySubscribed = onlySubscribed,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ import androidx.compose.runtime.collectAsState
import io.livekit.android.compose.local.RoomLocal
import io.livekit.android.compose.local.requireRoom
import io.livekit.android.compose.types.TrackReference
import io.livekit.android.events.RoomEvent
import io.livekit.android.room.Room
import io.livekit.android.room.participant.Participant
import io.livekit.android.room.track.Track
import io.livekit.android.room.track.TrackPublication
import io.livekit.android.util.flow
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.mapLatest

/**
* Returns an array of TrackReferences depending the sources provided.
*
* @param sources The sources of the tracks to provide. Defaults to all tracks.
* @param sources The sources of the tracks to provide. Defaults to camera and screen share tracks.
* @param usePlaceholders A set of sources to provide placeholders for.
* A placeholder will provide a TrackReference for participants that don't
* yet have a track published for that source. Defaults to no placeholders.
* @param passedRoom The room to use on, or [RoomLocal] if null.
* @param updateOn Room events to listen to. Defaults to all events.
* @param passedRoom The room to use on, or [RoomLocal] if null/not passed.
* @param onlySubscribed If true, only return tracks that have been subscribed. Defaults to true.
*/
@Composable
fun rememberTracks(
sources: List<Track.Source> = listOf(
Track.Source.CAMERA,
Track.Source.MICROPHONE,
Track.Source.SCREEN_SHARE,
Track.Source.UNKNOWN
Track.Source.SCREEN_SHARE
),
usePlaceholders: Set<Track.Source> = emptySet(),
passedRoom: Room? = null,
updateOn: Set<Class<RoomEvent>>? = null,
onlySubscribed: Boolean = true,
): List<TrackReference> {
val room = requireRoom(passedRoom)
Expand All @@ -58,7 +58,6 @@ fun rememberTracks(
room = room,
sources = sources,
usePlaceholders = usePlaceholders,
updateOn = updateOn,
onlySubscribed = onlySubscribed
)
.collectAsState(initial = room.getTrackReferences(sources, usePlaceholders, onlySubscribed))
Expand All @@ -70,16 +69,41 @@ fun rememberTracks(
*
* @see rememberTracks
*/
fun trackReferencesFlow(
@OptIn(ExperimentalCoroutinesApi::class)
internal fun trackReferencesFlow(
room: Room,
sources: List<Track.Source>,
usePlaceholders: Set<Track.Source> = emptySet(),
updateOn: Set<Class<RoomEvent>>? = null,
onlySubscribed: Boolean = true,
): Flow<List<TrackReference>> {
return room.events.events
.filter { updateOn == null || updateOn.contains(it::class.java) }
.map { room.getTrackReferences(sources, usePlaceholders, onlySubscribed) }
return room::remoteParticipants.flow
.flatMapLatest { remoteParticipants ->
val allParticipants = listOf(room.localParticipant).plus(remoteParticipants.values)

// Get flows of all the participant's trackPublications.
// Although we don't use the trackPublications directly here,
// We tie into the flow so we can be sensitive to its changes,
// due to participants being mutable.
val participantToTrackPubFlows = allParticipants.map { participant ->
participant::trackPublications.flow
.mapLatest { trackPublications ->
participant to trackPublications
}
}

// Flat map each participant into to track references.
return@flatMapLatest combine(participantToTrackPubFlows) { participantToTrackPubList ->
participantToTrackPubList.flatMap { (participant, trackPubs) ->
calculateTrackReferences(
participant = participant,
trackPublications = trackPubs.values,
sources = sources,
usePlaceholders = usePlaceholders,
onlySubscribed = onlySubscribed
)
}
}
}
}

/**
Expand All @@ -92,31 +116,44 @@ fun Room.getTrackReferences(
): List<TrackReference> {
val allParticipants = listOf(localParticipant).plus(remoteParticipants.values)
return allParticipants.flatMap { participant ->
sources.map { source ->
var tracks = participant.trackPublications.values.mapNotNull { trackPub ->
if (trackPub.source == source &&
(!onlySubscribed || trackPub.subscribed)
) {
TrackReference(
participant = participant,
publication = trackPub,
source = trackPub.source
)
} else {
null
}
}
if (tracks.isEmpty() && usePlaceholders.contains(source)) {
// Add placeholder
tracks = listOf(
TrackReference(
participant = participant,
publication = null,
source = source,
)
participant.getTrackReferencesBySource(sources, usePlaceholders, onlySubscribed)
}
}

internal fun calculateTrackReferences(
participant: Participant,
trackPublications: Collection<TrackPublication>,
sources: List<Track.Source>,
usePlaceholders: Set<Track.Source> = emptySet(),
onlySubscribed: Boolean = true
): List<TrackReference> {
return sources.flatMap { source ->
// Get all tracks for source
var tracks = trackPublications.mapNotNull { trackPub ->
if (trackPub.source == source &&
(!onlySubscribed || trackPub.subscribed)
) {
TrackReference(
participant = participant,
publication = trackPub,
source = trackPub.source
)
} else {
null
}
return@flatMap tracks
}

// If no tracks exist for source, create a placeholder.
if (tracks.isEmpty() && usePlaceholders.contains(source)) {
// Add placeholder
tracks = listOf(
TrackReference(
participant = participant,
publication = null,
source = source,
)
)
}
tracks
}
}

0 comments on commit 23501cd

Please sign in to comment.