Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ streamProject {
coverage {
includedModules = setOf(
"stream-video-android-core",
"stream-video-android-ui-core",
"stream-video-android-ui-compose",
)
sonarExclusions = listOf(
Expand Down
6 changes: 6 additions & 0 deletions stream-video-android-ui-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,10 @@ dependencies {
implementation(libs.androidx.lifecycle.runtime)

implementation(libs.stream.log)

testImplementation(libs.junit)
Comment thread
gpunto marked this conversation as resolved.
testImplementation(libs.mockk)
testImplementation(libs.turbine)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.kotlin.test.junit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ import io.getstream.video.android.ui.common.StreamCallActivity.Companion.callInt
import io.getstream.video.android.ui.common.models.StreamCallActivityException
import io.getstream.video.android.ui.common.permission.PermissionManager
import io.getstream.video.android.ui.common.util.StreamCallActivityDelicateApi
import io.getstream.video.android.ui.common.util.lastParticipantSignal
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -1478,41 +1477,41 @@ public abstract class StreamCallActivity : ComponentActivity(), ActivityCallOper
/**
* Processes participant leave events for the given [call].
*
* Observes [cachedCall.state.participants] and triggers [onLastParticipant] if only
* one or fewer participants remain. Debouncing is applied to handle quick network
* disconnect/reconnect scenarios.
* Observes [cachedCall.state.participants] together with the connection state and triggers
* [onLastParticipant] if only one or fewer participants remain while the connection is
* [RealtimeConnection.Connected]. Debouncing is applied to handle quick network
* disconnect/reconnect scenarios, and the check requires a connected state because the
* roster is unreliable while a join or reconnect is running and leaving would cancel it.
* See [lastParticipantSignal].
*
* @param call the active [Call] associated with the event.
* @param event the [VideoEvent] that triggered this processing, typically a [ParticipantLeftEvent]
* or [CallSessionParticipantLeftEvent].
*/
@OptIn(FlowPreview::class)
private fun processParticipantLeftEvent(call: Call, event: VideoEvent) {
/**
* - participantCountJob will be null when activity is newly created
* - participantCountJob will be inactive when activity is resumed with another call
*/
if (participantCountJob == null) {
participantCountJob = lifecycleScope.launch(supervisorJob) {
cachedCall.state.participants
/**
* A debounce is applied here to handle quick disconnect/reconnect scenarios
* caused by unstable network conditions. Without the debounce, other devices
* may receive a [ParticipantLeftEvent] prematurely, which could trigger
* unintended reactions in the call flow.
*/
.debounce(getParticipantUpdateDebounce(call))
.collect {
logger.d { "Participant left, remaining: ${it.size}" }
lastParticipantSignal(
participants = cachedCall.state.participants,
connection = cachedCall.state.connection,
Comment thread
gpunto marked this conversation as resolved.
debounceMs = getParticipantUpdateDebounce(call),
onEvaluated = { roster, connection ->
logger.d {
"Participant left, remaining: ${roster.size}, connection: $connection"
}
lifecycleScope.launch(Dispatchers.Default) {
it.forEachIndexed { i, v ->
roster.forEachIndexed { i, v ->
logger.d { "Participant [$i]=${v.name.value}" }
}
}
if (it.size <= 1) {
onLastParticipant(call)
}
}
},
).collect {
onLastParticipant(call)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2014-2026 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-video-android/blob/main/LICENSE
*
* 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.getstream.video.android.ui.common.util

import io.getstream.video.android.core.ParticipantState
import io.getstream.video.android.core.RealtimeConnection
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.runningFold

/**
* Emits the participant roster whenever the local user has become the last participant in the
* call while the connection is [RealtimeConnection.Connected].
*
* Roster changes are debounced by [debounceMs] to absorb quick disconnect/reconnect flaps.
* Emissions require a connected state because the roster is unreliable at any other point:
* during the initial join it is still being populated, and during a reconnect the rejoin
* removes the previous local participant record and remote participants of the failing SFU
* may not have rejoined yet. Acting on the roster then would leave the call and cancel the
* join or reconnect that is still running in the call scope. Terminal states need no signal
* from here: the reconnector leaves the call itself when retries are exhausted. The
* connection state is part of the combined stream, so the roster is re-evaluated once the
* connection settles and a genuine last-participant state still emits.
*
* The signal is a rising edge of the roster, not of the combined condition: becoming the last
* participant arms it, and it fires on the first connected evaluation after that. A connection
* transition with an unchanged roster therefore does not repeat the signal, while a remote
* participant joining and leaving again re-arms it and does signal a second time.
*
* @param participants the participant roster of the call.
* @param connection the realtime connection state of the call.
* @param debounceMs debounce applied to the combined stream before evaluation.
* @param onEvaluated invoked for every debounced evaluation, regardless of the outcome.
*/
@OptIn(FlowPreview::class)
internal fun lastParticipantSignal(
participants: Flow<List<ParticipantState>>,
connection: Flow<RealtimeConnection>,
debounceMs: Long,
onEvaluated: suspend (List<ParticipantState>, RealtimeConnection) -> Unit = { _, _ -> },
): Flow<List<ParticipantState>> =
combine(participants, connection) { roster, connectionState -> roster to connectionState }
.debounce(debounceMs)
.onEach { (roster, connectionState) -> onEvaluated(roster, connectionState) }
.runningFold(LastParticipantState()) { previous, (roster, connectionState) ->
val isLast = roster.size <= 1
// Arm on the rising edge of the roster and stay armed until a connected
// evaluation consumes it, so a signal found mid-reconnect is not lost.
val armed = when {
!isLast -> false
!previous.wasLast -> true
else -> previous.armed
}
val connected = connectionState is RealtimeConnection.Connected
LastParticipantState(
wasLast = isLast,
armed = armed && !connected,
signal = roster.takeIf { armed && connected },
)
}
.mapNotNull { it.signal }

/**
* Fold state of [lastParticipantSignal].
*
* @param wasLast whether the previous evaluation saw a last-participant roster.
* @param armed whether a last-participant roster is waiting for a connected evaluation.
* @param signal the roster to emit for this evaluation, or null when there is nothing to emit.
*/
private data class LastParticipantState(
val wasLast: Boolean = false,
val armed: Boolean = false,
val signal: List<ParticipantState>? = null,
)
Loading
Loading