Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,14 @@ public fun LivestreamPlayer(
var livestreamState by rememberSaveable { mutableStateOf(LivestreamState.INITIAL) }

LaunchedEffect(connection, endedAt, backstage) {
when (connection) {
is RealtimeConnection.Connected -> {
hasJoinedSuccessfully = true
livestreamState = when {
endedAt != null -> LivestreamState.ENDED
backstage -> LivestreamState.BACKSTAGE
else -> LivestreamState.LIVE
}
}

is RealtimeConnection.Failed -> livestreamState = LivestreamState.ERROR
is RealtimeConnection.InProgress -> livestreamState = LivestreamState.JOINING
else -> { }
}
if (connection is RealtimeConnection.Connected) hasJoinedSuccessfully = true
livestreamState = nextLivestreamState(
current = livestreamState,
connection = connection,
ended = endedAt != null,
backstage = backstage,
hasJoined = hasJoinedSuccessfully,
)
}

LivestreamPlayerImpl(
Expand All @@ -181,6 +175,30 @@ public fun LivestreamPlayer(
)
}

/**
* Returns the livestream state for the call's current [connection].
*
* An ended call leaves right away, so [ended] and a disconnect can arrive together. A joined
* viewer then still sees the ended state rather than staying on the live one.
*/
internal fun nextLivestreamState(
current: LivestreamState,
connection: RealtimeConnection,
ended: Boolean,
backstage: Boolean,
hasJoined: Boolean,
): LivestreamState = when (connection) {
is RealtimeConnection.Connected -> when {
ended -> LivestreamState.ENDED
backstage -> LivestreamState.BACKSTAGE
else -> LivestreamState.LIVE
}

is RealtimeConnection.Failed -> LivestreamState.ERROR
is RealtimeConnection.InProgress -> LivestreamState.JOINING
else -> if (ended && hasJoined) LivestreamState.ENDED else current
Comment thread
aleksandar-apostolov marked this conversation as resolved.
Resolved
}

@Composable
internal fun LivestreamPlayerImpl(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.compose.ui.components.livestream

import com.google.common.truth.Truth.assertThat
import io.getstream.video.android.compose.ui.components.livestream.state.LivestreamState
import io.getstream.video.android.core.RealtimeConnection
import org.junit.Test

internal class LivestreamStateTest {

@Test
fun `a call that ends and disconnects together shows the ended state`() {
val state = nextLivestreamState(
current = LivestreamState.LIVE,
connection = RealtimeConnection.Disconnected,
ended = true,
backstage = false,
hasJoined = true,
)

assertThat(state).isEqualTo(LivestreamState.ENDED)
}

@Test
fun `a disconnect without an end keeps the current state`() {
val state = nextLivestreamState(
current = LivestreamState.LIVE,
connection = RealtimeConnection.Disconnected,
ended = false,
backstage = false,
hasJoined = true,
)

assertThat(state).isEqualTo(LivestreamState.LIVE)
}

@Test
fun `an ended call the viewer never joined keeps the current state`() {
val state = nextLivestreamState(
current = LivestreamState.INITIAL,
connection = RealtimeConnection.Disconnected,
ended = true,
backstage = false,
hasJoined = false,
)

assertThat(state).isEqualTo(LivestreamState.INITIAL)
}

@Test
fun `a connected call maps to ended, backstage or live`() {
fun connected(ended: Boolean, backstage: Boolean) = nextLivestreamState(
current = LivestreamState.JOINING,
connection = RealtimeConnection.Connected,
ended = ended,
backstage = backstage,
hasJoined = true,
)

assertThat(connected(ended = true, backstage = true)).isEqualTo(LivestreamState.ENDED)
assertThat(connected(ended = false, backstage = true)).isEqualTo(LivestreamState.BACKSTAGE)
assertThat(connected(ended = false, backstage = false)).isEqualTo(LivestreamState.LIVE)
}

@Test
fun `joining and failing map to their states`() {
fun on(connection: RealtimeConnection) = nextLivestreamState(
current = LivestreamState.LIVE,
connection = connection,
ended = false,
backstage = false,
hasJoined = true,
)

assertThat(on(RealtimeConnection.InProgress)).isEqualTo(LivestreamState.JOINING)
assertThat(on(RealtimeConnection.Failed("boom"))).isEqualTo(LivestreamState.ERROR)
}
}
Loading