From f188d7f5da82dd10441cd61d3359ad5f724d891d Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Mon, 2 Nov 2026 13:59:29 +0530 Subject: [PATCH 01/10] refactor(core): add EmptyRtcSession, an RtcSession without an SFU connection RtcSession becomes open and gains an internal initializeRtc flag. When the flag is false, the session never creates the SFU connection or any peer connection and leaves the shared peer connection factory untouched. EmptyRtcSession uses it and turns every public member into a no-op. cleanup() no longer assumes the SFU connection module exists. Prepares for joining a call without WebRTC media (VID-1543). No behaviour change for existing sessions. Co-authored-by: Cursor --- .../api/stream-video-android-core.api | 24 +-- .../android/core/call/EmptyRtcSession.kt | 113 ++++++++++ .../video/android/core/call/RtcSession.kt | 47 ++-- .../android/core/rtc/EmptyRtcSessionTest.kt | 200 ++++++++++++++++++ 4 files changed, 355 insertions(+), 29 deletions(-) create mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt create mode 100644 stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/EmptyRtcSessionTest.kt diff --git a/stream-video-android-core/api/stream-video-android-core.api b/stream-video-android-core/api/stream-video-android-core.api index 11a3bb2fb10..185ec1876bc 100644 --- a/stream-video-android-core/api/stream-video-android-core.api +++ b/stream-video-android-core/api/stream-video-android-core.api @@ -10125,21 +10125,21 @@ public final class io/getstream/video/android/core/call/CallType$Livestream : io public fun getSortPreset ()Lio/getstream/video/android/core/sorting/SortPreset; } -public final class io/getstream/video/android/core/call/RtcSession { +public class io/getstream/video/android/core/call/RtcSession { public final fun cleanup ()V - public final fun connect (Lstream/video/sfu/event/ReconnectDetails;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun connect (Lstream/video/sfu/event/ReconnectDetails;Ljava/util/List;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun connect$default (Lio/getstream/video/android/core/call/RtcSession;Lstream/video/sfu/event/ReconnectDetails;Ljava/util/List;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; - public final fun getPublisherStats (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun getPublisherStats (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getSfuSocketState ()Lkotlinx/coroutines/flow/StateFlow; - public final fun getSubscriberStats (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun handleEvent (Lio/getstream/android/video/generated/models/VideoEvent;)V - public final fun handleIceTrickle (Lio/getstream/video/android/core/events/ICETrickleEvent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun handleSubscriberOffer (Lio/getstream/video/android/core/events/SubscriberOfferEvent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun reconnect (ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun requestPublisherIceRestart (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun requestSubscriberIceRestart (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public final fun setScreenShareTrack ()V - public final fun updateTrackDimensions (Ljava/lang/String;Lstream/video/sfu/models/TrackType;ZLstream/video/sfu/models/VideoDimension;Ljava/lang/String;)V + public fun getSubscriberStats (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun handleEvent (Lio/getstream/android/video/generated/models/VideoEvent;)V + public fun handleIceTrickle (Lio/getstream/video/android/core/events/ICETrickleEvent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun handleSubscriberOffer (Lio/getstream/video/android/core/events/SubscriberOfferEvent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun reconnect (ZLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun requestPublisherIceRestart (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun requestSubscriberIceRestart (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun setScreenShareTrack ()V + public fun updateTrackDimensions (Ljava/lang/String;Lstream/video/sfu/models/TrackType;ZLstream/video/sfu/models/VideoDimension;Ljava/lang/String;)V public static synthetic fun updateTrackDimensions$default (Lio/getstream/video/android/core/call/RtcSession;Ljava/lang/String;Lstream/video/sfu/models/TrackType;ZLstream/video/sfu/models/VideoDimension;Ljava/lang/String;ILjava/lang/Object;)V } diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt new file mode 100644 index 00000000000..37a6f247e6f --- /dev/null +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt @@ -0,0 +1,113 @@ +/* + * 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.core.call + +import androidx.lifecycle.Lifecycle +import io.getstream.android.video.generated.models.VideoEvent +import io.getstream.result.Error +import io.getstream.result.Result +import io.getstream.result.Result.Failure +import io.getstream.video.android.core.Call +import io.getstream.video.android.core.StreamVideo +import io.getstream.video.android.core.StreamVideoClient +import io.getstream.video.android.core.analytics.call.observer.SfuAnalytics +import io.getstream.video.android.core.call.components.CallSessionManager +import io.getstream.video.android.core.call.stats.model.RtcStatsReport +import io.getstream.video.android.core.events.ICETrickleEvent +import io.getstream.video.android.core.events.SubscriberOfferEvent +import stream.video.sfu.event.ReconnectDetails +import stream.video.sfu.models.PublishOption +import stream.video.sfu.models.TrackType +import stream.video.sfu.models.VideoDimension +import stream.video.sfu.signal.ICERestartResponse + +/** + * An [RtcSession] with no SFU connection behind it, for a call that is joined without WebRTC + * media. + * + * It never creates the SFU connection or any peer connection and leaves the shared peer + * connection factory untouched. Every public member is a no-op: stats are `null` and ICE + * restarts fail. It must not be installed as the active session in [CallSessionManager], since + * the reconnect, stats and rendering paths read that session as a live SFU connection. + */ +internal class EmptyRtcSession( + client: StreamVideo, + call: Call, + sessionManager: CallSessionManager, + sessionId: String, + apiKey: String, + lifecycle: Lifecycle, + sfuAnalytics: SfuAnalytics, + clientImpl: StreamVideoClient = client as StreamVideoClient, +) : RtcSession( + client = client, + powerManager = null, + call = call, + sessionManager = sessionManager, + sessionId = sessionId, + apiKey = apiKey, + lifecycle = lifecycle, + sfuUrl = "", + sfuWsUrl = "", + sfuToken = "", + sfuName = "", + remoteIceServers = emptyList(), + clientImpl = clientImpl, + sfuAnalytics = sfuAnalytics, + initializeRtc = false, +) { + + override suspend fun reconnect(forceRestart: Boolean) = Unit + + @Deprecated( + message = "Use connectInternal() which returns SfuConnectionResult instead of throwing.", + replaceWith = ReplaceWith("connectInternal(reconnectDetails, options)"), + ) + override suspend fun connect( + reconnectDetails: ReconnectDetails?, + options: List?, + ) = Unit + + override fun setScreenShareTrack() = Unit + + override fun handleEvent(event: VideoEvent) = Unit + + override suspend fun handleIceTrickle(event: ICETrickleEvent) = Unit + + override suspend fun handleSubscriberOffer(offerEvent: SubscriberOfferEvent) = Unit + + override suspend fun getPublisherStats(): RtcStatsReport? = null + + override suspend fun getSubscriberStats(): RtcStatsReport? = null + + override suspend fun requestSubscriberIceRestart(): Result = + noSfuConnection() + + override suspend fun requestPublisherIceRestart(): Result = + noSfuConnection() + + override fun updateTrackDimensions( + sessionId: String, + trackType: TrackType, + visible: Boolean, + dimensions: VideoDimension, + viewportId: String, + ) = Unit + + private fun noSfuConnection(): Result = + Failure(Error.GenericError("EmptyRtcSession has no SFU connection")) +} diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/RtcSession.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/RtcSession.kt index 0f390165cee..b049766c263 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/RtcSession.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/RtcSession.kt @@ -257,7 +257,7 @@ internal sealed class FastReconnectResult { data class Failed(val error: Exception) : FastReconnectResult() } -public class RtcSession internal constructor( +public open class RtcSession internal constructor( client: StreamVideo, private val sessionCounter: Int = 0, private val powerManager: PowerManager?, @@ -327,6 +327,11 @@ public class RtcSession internal constructor( sfuAnalytics = sfuAnalytics, ) }, + /** + * When false, the session never creates the SFU connection or any peer connection, and + * leaves the shared peer connection factory untouched. Only [EmptyRtcSession] passes false. + */ + initializeRtc: Boolean = true, ) { private val muteStateSyncJobs = TrackKeyedJobs() @@ -718,6 +723,12 @@ public class RtcSession internal constructor( "SDK hasn't been initialised yet - can't start a RtcSession", ) } + if (initializeRtc) { + setUpRtc() + } + } + + private fun setUpRtc() { logger.i { " #sfu; #track; no args" } traceE2EEConfiguration() @@ -976,7 +987,7 @@ public class RtcSession internal constructor( * @param forceRestart - set to true if you want to force restart both ICE connections * regardless of their current connection status (even if they are CONNECTED) */ - suspend fun reconnect(forceRestart: Boolean) { + public open suspend fun reconnect(forceRestart: Boolean) { // ice restart val subscriberAsync = rtcSessionScope.async { subscriber.value?.let { @@ -1010,7 +1021,7 @@ public class RtcSession internal constructor( message = "Use connectInternal() which returns SfuConnectionResult instead of throwing.", replaceWith = ReplaceWith("connectInternal(reconnectDetails, options)"), ) - suspend fun connect( + public open suspend fun connect( reconnectDetails: ReconnectDetails? = null, options: List? = null, ) { @@ -1305,7 +1316,7 @@ public class RtcSession internal constructor( return } - fun setScreenShareTrack() { + public open fun setScreenShareTrack() { setLocalTrack( TrackType.TRACK_TYPE_SCREEN_SHARE, VideoTrack( @@ -1349,11 +1360,13 @@ public class RtcSession internal constructor( fun cleanup() = atomicCleanup { logger.i { "[cleanup] #sfu; #track; no args" } - coroutineScope.launch { - serialProcessor.submit("cleanupSfuConnections") { - sfuConnectionModule.socketConnection.disconnect() - sfuConnectionMigrationModule?.socketConnection?.disconnect() - Unit + if (::sfuConnectionModule.isInitialized) { + coroutineScope.launch { + serialProcessor.submit("cleanupSfuConnections") { + sfuConnectionModule.socketConnection.disconnect() + sfuConnectionMigrationModule?.socketConnection?.disconnect() + Unit + } } } sfuConnectionMigrationModule = null @@ -1663,7 +1676,7 @@ public class RtcSession internal constructor( logger.d { "[setVideoSubscriptions] #sfu; #track; useDefaults: $useDefaults" } } - fun handleEvent(event: VideoEvent) { + public open fun handleEvent(event: VideoEvent) { logger.i { "[rtc handleEvent] #sfu; event: $event" } if (event is SfuDataEvent) { coroutineScope.launch { @@ -1923,7 +1936,7 @@ public class RtcSession internal constructor( /** * Triggered whenever we receive new ice candidate from the SFU */ - suspend fun handleIceTrickle(event: ICETrickleEvent) { + public open suspend fun handleIceTrickle(event: ICETrickleEvent) { if (event.peerType == PeerType.PEER_TYPE_PUBLISHER_UNSPECIFIED && publisher.value == null && sfuConnectionModule.socketConnection.state().value is SfuSocketState.Connected) { logger.v { "[handleIceTrickle] #sfu; #${event.peerType.stringify()}; publisher is null, adding to pending" @@ -1961,7 +1974,7 @@ public class RtcSession internal constructor( * - Sets the local description * - Sends the answer back to the SFU */ - suspend fun handleSubscriberOffer(offerEvent: SubscriberOfferEvent) { + public open suspend fun handleSubscriberOffer(offerEvent: SubscriberOfferEvent) { logger.d { "[handleSubscriberOffer] #sfu; #subscriber; event: $offerEvent" } if (subscriber.value == null) { subscriberPendingEvents.add(offerEvent) @@ -1977,14 +1990,14 @@ public class RtcSession internal constructor( /** * @return [StateFlow] that holds [RtcStatsReport] that the publisher exposes. */ - suspend fun getPublisherStats(): RtcStatsReport? { + public open suspend fun getPublisherStats(): RtcStatsReport? { return publisher.value?.getStats() } /** * @return [StateFlow] that holds [RTCStatsReport] that the subscriber exposes. */ - suspend fun getSubscriberStats(): RtcStatsReport? { + public open suspend fun getSubscriberStats(): RtcStatsReport? { return subscriber.value?.getStats() } @@ -2108,7 +2121,7 @@ public class RtcSession internal constructor( } // share what size and which participants we're looking at - suspend fun requestSubscriberIceRestart(): Result = + public open suspend fun requestSubscriberIceRestart(): Result = subscriber.value?.restartIce() ?: Failure( Error.ThrowableError( "Subscriber is null", @@ -2116,7 +2129,7 @@ public class RtcSession internal constructor( ), ) - suspend fun requestPublisherIceRestart(): Result = safeApiCall { + public open suspend fun requestPublisherIceRestart(): Result = safeApiCall { val request = ICERestartRequest( session_id = sessionId, peer_type = PeerType.PEER_TYPE_PUBLISHER_UNSPECIFIED, @@ -2145,7 +2158,7 @@ public class RtcSession internal constructor( // sets display track visibility @Synchronized - fun updateTrackDimensions( + public open fun updateTrackDimensions( sessionId: String, trackType: TrackType, visible: Boolean, diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/EmptyRtcSessionTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/EmptyRtcSessionTest.kt new file mode 100644 index 00000000000..a2daa21664f --- /dev/null +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/EmptyRtcSessionTest.kt @@ -0,0 +1,200 @@ +/* + * 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.core.rtc + +import androidx.lifecycle.Lifecycle +import com.google.common.truth.Truth.assertThat +import io.getstream.result.Result.Failure +import io.getstream.video.android.core.Call +import io.getstream.video.android.core.CallState +import io.getstream.video.android.core.StreamVideo +import io.getstream.video.android.core.StreamVideoClient +import io.getstream.video.android.core.analytics.call.observer.SfuAnalytics +import io.getstream.video.android.core.base.DispatcherRule +import io.getstream.video.android.core.call.EmptyRtcSession +import io.getstream.video.android.core.call.RtcSession +import io.getstream.video.android.core.call.components.CallSessionManager +import io.getstream.video.android.core.call.connection.StreamPeerConnectionFactory +import io.getstream.video.android.core.call.scope.ScopeProvider +import io.getstream.video.android.core.events.ICETrickleEvent +import io.getstream.video.android.core.events.SubscriberOfferEvent +import io.getstream.video.android.core.internal.module.SfuConnectionModule +import io.getstream.video.android.core.socket.sfu.SfuSocketConnection +import io.getstream.video.android.core.socket.sfu.state.SfuSocketState +import io.mockk.every +import io.mockk.mockk +import io.mockk.unmockkAll +import io.mockk.verify +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import stream.video.sfu.models.PeerType +import stream.video.sfu.models.TrackType + +class EmptyRtcSessionTest { + + @get:Rule + val dispatcherRule = DispatcherRule() + + private val testScope = TestScope(StandardTestDispatcher()) + + private lateinit var mockCall: Call + private lateinit var mockLifecycle: Lifecycle + private lateinit var mockVideoClient: StreamVideoClient + private lateinit var mockStreamVideo: StreamVideo + private lateinit var peerConnectionFactory: StreamPeerConnectionFactory + + @Before + fun setup() { + mockStreamVideo = mockk(relaxed = true) + mockVideoClient = mockk(relaxed = true) + mockLifecycle = mockk(relaxed = true) + peerConnectionFactory = mockk(relaxed = true) + val callState = mockk(relaxed = true) + val scopeProvider = mockk { + every { getCoroutineScope(any()) } returns testScope + every { getRtcSessionScope(any(), any()) } returns testScope + } + mockCall = mockk(relaxed = true) + every { mockCall.state } returns callState + every { mockCall.scope } returns testScope + every { mockCall.scopeProvider } returns scopeProvider + every { mockCall.peerConnectionFactory } returns peerConnectionFactory + StreamVideo.install(mockStreamVideo) + } + + @After + fun tearDown() { + StreamVideo.removeClient() + unmockkAll() + } + + private fun createEmptySession() = EmptyRtcSession( + client = mockStreamVideo, + call = mockCall, + sessionManager = CallSessionManager(), + sessionId = "test-session-id", + apiKey = "test-api-key", + lifecycle = mockLifecycle, + sfuAnalytics = SfuAnalytics.getFakeSfuAnalytics(), + clientImpl = mockVideoClient, + ) + + @Test + fun `empty session never touches the peer connection factory or the lifecycle`() = + testScope.runTest { + val session = createEmptySession() + advanceUntilIdle() + + assertThat(session.subscriber.value).isNull() + assertThat(session.publisher.value).isNull() + assertThat(session.sfuSocketState.value) + .isEqualTo(SfuSocketState.Disconnected.Stopped) + verify(exactly = 0) { mockCall.peerConnectionFactory } + verify(exactly = 0) { mockLifecycle.addObserver(any()) } + } + + @Test + fun `empty session public methods are safe no-ops`() = testScope.runTest { + val session: RtcSession = createEmptySession() + + session.reconnect(forceRestart = true) + @Suppress("DEPRECATION") + session.connect() + session.setScreenShareTrack() + session.handleEvent(mockk(relaxed = true)) + session.handleIceTrickle( + ICETrickleEvent(candidate = "{}", peerType = PeerType.PEER_TYPE_PUBLISHER_UNSPECIFIED), + ) + session.handleSubscriberOffer(mockk(relaxed = true)) + session.updateTrackDimensions("other-session", TrackType.TRACK_TYPE_VIDEO, visible = true) + advanceUntilIdle() + + assertThat(session.getPublisherStats()).isNull() + assertThat(session.getSubscriberStats()).isNull() + assertThat(session.requestSubscriberIceRestart()).isInstanceOf(Failure::class.java) + assertThat(session.requestPublisherIceRestart()).isInstanceOf(Failure::class.java) + assertThat(session.subscriberPendingEvents).isEmpty() + assertThat(session.publisherPendingEvents).isEmpty() + verify(exactly = 0) { mockCall.peerConnectionFactory } + verify(exactly = 0) { mockCall.mediaManager } + } + + @Test + fun `empty session cleanup succeeds without an SFU connection and can repeat`() = + testScope.runTest { + val session = createEmptySession() + + session.cleanup() + session.cleanup() + advanceUntilIdle() + + assertThat(session.subscriber.value).isNull() + verify(exactly = 0) { mockCall.peerConnectionFactory } + } + + @Test + fun `regular session still creates the SFU connection and the subscriber`() = runTest { + every { mockCall.state.participants } returns MutableStateFlow(emptyList()) + every { mockCall.state.remoteParticipants } returns MutableStateFlow(emptyList()) + every { mockCall.state.ownCapabilities } returns MutableStateFlow(emptyList()) + var moduleCreations = 0 + val socket = mockk(relaxed = true) { + every { state() } returns MutableStateFlow(SfuSocketState.Disconnected.Stopped) + every { errors() } returns MutableSharedFlow() + every { events() } returns MutableSharedFlow() + } + val module = mockk(relaxed = true) { + every { socketConnection } returns socket + } + + val session = RtcSession( + client = mockStreamVideo, + powerManager = null, + call = mockCall, + sessionManager = CallSessionManager(), + sessionId = "test-session-id", + apiKey = "test-api-key", + lifecycle = mockLifecycle, + sfuUrl = "https://test-sfu.stream.com", + sfuWsUrl = "wss://test-sfu.stream.com", + sfuToken = "fake-sfu-token", + sfuName = "sfu-edge-1", + remoteIceServers = emptyList(), + clientImpl = mockVideoClient, + coroutineScope = testScope, + rtcSessionScope = testScope, + sfuConnectionModuleProvider = { + moduleCreations++ + module + }, + sfuAnalytics = SfuAnalytics.getFakeSfuAnalytics(), + ) + testScope.advanceUntilIdle() + + assertThat(moduleCreations).isEqualTo(1) + assertThat(session.subscriber.value).isNotNull() + verify(atLeast = 1) { mockCall.peerConnectionFactory } + } +} From d69a2a63b98dbc7367c5010595c4380c01ead224 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Mon, 2 Nov 2026 14:45:01 +0530 Subject: [PATCH 02/10] refactor(core): log ignored calls on EmptyRtcSession Co-authored-by: Cursor --- .../android/core/call/EmptyRtcSession.kt | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt index 37a6f247e6f..679a520426f 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt @@ -18,6 +18,7 @@ package io.getstream.video.android.core.call import androidx.lifecycle.Lifecycle import io.getstream.android.video.generated.models.VideoEvent +import io.getstream.log.taggedLogger import io.getstream.result.Error import io.getstream.result.Result import io.getstream.result.Result.Failure @@ -40,8 +41,8 @@ import stream.video.sfu.signal.ICERestartResponse * media. * * It never creates the SFU connection or any peer connection and leaves the shared peer - * connection factory untouched. Every public member is a no-op: stats are `null` and ICE - * restarts fail. It must not be installed as the active session in [CallSessionManager], since + * connection factory untouched. Every public member is a no-op that logs a warning: stats are + * `null` and ICE restarts fail. It must not be installed as the active session in [CallSessionManager], since * the reconnect, stats and rendering paths read that session as a live SFU connection. */ internal class EmptyRtcSession( @@ -71,7 +72,11 @@ internal class EmptyRtcSession( initializeRtc = false, ) { - override suspend fun reconnect(forceRestart: Boolean) = Unit + private val logger by taggedLogger("Video:EmptyRtcSession") + + override suspend fun reconnect(forceRestart: Boolean) { + logIgnored("reconnect") + } @Deprecated( message = "Use connectInternal() which returns SfuConnectionResult instead of throwing.", @@ -80,25 +85,45 @@ internal class EmptyRtcSession( override suspend fun connect( reconnectDetails: ReconnectDetails?, options: List?, - ) = Unit + ) { + logIgnored("connect") + } - override fun setScreenShareTrack() = Unit + override fun setScreenShareTrack() { + logIgnored("setScreenShareTrack") + } - override fun handleEvent(event: VideoEvent) = Unit + override fun handleEvent(event: VideoEvent) { + logIgnored("handleEvent") + } - override suspend fun handleIceTrickle(event: ICETrickleEvent) = Unit + override suspend fun handleIceTrickle(event: ICETrickleEvent) { + logIgnored("handleIceTrickle") + } - override suspend fun handleSubscriberOffer(offerEvent: SubscriberOfferEvent) = Unit + override suspend fun handleSubscriberOffer(offerEvent: SubscriberOfferEvent) { + logIgnored("handleSubscriberOffer") + } - override suspend fun getPublisherStats(): RtcStatsReport? = null + override suspend fun getPublisherStats(): RtcStatsReport? { + logIgnored("getPublisherStats") + return null + } - override suspend fun getSubscriberStats(): RtcStatsReport? = null + override suspend fun getSubscriberStats(): RtcStatsReport? { + logIgnored("getSubscriberStats") + return null + } - override suspend fun requestSubscriberIceRestart(): Result = - noSfuConnection() + override suspend fun requestSubscriberIceRestart(): Result { + logIgnored("requestSubscriberIceRestart") + return noSfuConnection() + } - override suspend fun requestPublisherIceRestart(): Result = - noSfuConnection() + override suspend fun requestPublisherIceRestart(): Result { + logIgnored("requestPublisherIceRestart") + return noSfuConnection() + } override fun updateTrackDimensions( sessionId: String, @@ -106,7 +131,13 @@ internal class EmptyRtcSession( visible: Boolean, dimensions: VideoDimension, viewportId: String, - ) = Unit + ) { + logIgnored("updateTrackDimensions") + } + + private fun logIgnored(function: String) { + logger.w { "[$function] Ignored: EmptyRtcSession has no SFU connection" } + } private fun noSfuConnection(): Result = Failure(Error.GenericError("EmptyRtcSession has no SFU connection")) From e42ea3a302594ab69d7c7ccf2a0537c4d8574507 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Mon, 2 Nov 2026 19:07:30 +0530 Subject: [PATCH 03/10] refactor(core): reject EmptyRtcSession as the active session Co-authored-by: Cursor --- .../video/android/core/call/EmptyRtcSession.kt | 4 ++-- .../core/call/components/CallSessionManager.kt | 15 ++++++++++++++- .../call/components/CallSessionManagerTest.kt | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt index 679a520426f..2a12869d961 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/EmptyRtcSession.kt @@ -42,8 +42,8 @@ import stream.video.sfu.signal.ICERestartResponse * * It never creates the SFU connection or any peer connection and leaves the shared peer * connection factory untouched. Every public member is a no-op that logs a warning: stats are - * `null` and ICE restarts fail. It must not be installed as the active session in [CallSessionManager], since - * the reconnect, stats and rendering paths read that session as a live SFU connection. + * `null` and ICE restarts fail. [CallSessionManager.setActiveSession] refuses it, since the + * reconnect, stats and rendering paths read the active session as a live SFU connection. */ internal class EmptyRtcSession( client: StreamVideo, diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt index 62ed3d49938..abe9d5f43af 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt @@ -16,6 +16,8 @@ package io.getstream.video.android.core.call.components +import io.getstream.log.taggedLogger +import io.getstream.video.android.core.call.EmptyRtcSession import io.getstream.video.android.core.call.RtcSession import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -32,6 +34,8 @@ import java.util.concurrent.ConcurrentHashMap * lifecycle collaborators a single source of truth to depend on. */ internal class CallSessionManager() { + private val logger by taggedLogger("Video:CallSessionManager") + private val _session: MutableStateFlow = MutableStateFlow(null) /** @@ -41,8 +45,17 @@ internal class CallSessionManager() { */ val session: StateFlow = _session.asStateFlow() - /** Sole write path for [session]: swaps in a new RTC session or clears it with `null`. */ + /** + * Sole write path for [session]: swaps in a new RTC session or clears it with `null`. + * + * An [EmptyRtcSession] is rejected and the current session is kept: the reconnect, leave and + * stats paths read the active session as a live SFU connection, which it does not have. + */ fun setActiveSession(rtcSession: RtcSession?) { + if (rtcSession is EmptyRtcSession) { + logger.e { "[setActiveSession] Rejected: EmptyRtcSession must not be the active session" } + return + } _session.value = rtcSession } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallSessionManagerTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallSessionManagerTest.kt index 23be4570bd3..d73d88eab9a 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallSessionManagerTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallSessionManagerTest.kt @@ -17,6 +17,7 @@ package io.getstream.video.android.core.call.components import com.google.common.truth.Truth.assertThat +import io.getstream.video.android.core.call.EmptyRtcSession import io.getstream.video.android.core.call.RtcSession import io.mockk.mockk import org.junit.Test @@ -43,6 +44,22 @@ class CallSessionManagerTest { assertThat(manager.session.value).isNull() } + @Test + fun `setActiveSession rejects an EmptyRtcSession and keeps the current session`() { + val manager = manager() + + manager.setActiveSession(mockk(relaxed = true)) + assertThat(manager.session.value).isNull() + + val session = mockk(relaxed = true) + manager.setActiveSession(session) + manager.setActiveSession(mockk(relaxed = true)) + assertThat(manager.session.value).isSameInstanceAs(session) + + manager.setActiveSession(null) + assertThat(manager.session.value).isNull() + } + @Test fun `session and unified session ids are non-blank uuids and distinct`() { val manager = manager() From d53640264744f1ab014c2050abe477cf6ab8c0fb Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Mon, 2 Nov 2026 19:25:52 +0530 Subject: [PATCH 04/10] feat(core): join with HLS playback when the backend grants it --- .../api/stream-video-android-core.api | 165 +++++++++++++++++- .../generated/infrastructure/Serializer.kt | 3 + .../video/generated/models/JoinCallRequest.kt | 40 ++++- .../generated/models/JoinCallResponse.kt | 5 +- .../generated/models/PlaybackResponse.kt | 113 ++++++++++++ .../io/getstream/video/android/core/Call.kt | 12 ++ .../getstream/video/android/core/CallState.kt | 16 +- .../video/android/core/StreamVideoClient.kt | 2 + .../core/call/components/CallApiClient.kt | 5 + .../call/components/CallJoinCoordinator.kt | 49 +++++- .../call/components/CallLifecycleManager.kt | 9 + .../call/components/CallSessionManager.kt | 7 + .../video/android/core/model/PlaybackType.kt | 37 ++++ .../components/CallJoinCoordinatorTest.kt | 107 +++++++++++- .../components/CallLifecycleManagerTest.kt | 90 ++++++++++ .../core/model/PlaybackSerializationTest.kt | 65 +++++++ .../core/rtc/JoinRecoverableFailureTest.kt | 4 + 17 files changed, 714 insertions(+), 15 deletions(-) create mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/PlaybackResponse.kt create mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/model/PlaybackType.kt create mode 100644 stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt create mode 100644 stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/model/PlaybackSerializationTest.kt diff --git a/stream-video-android-core/api/stream-video-android-core.api b/stream-video-android-core/api/stream-video-android-core.api index 185ec1876bc..7d9d8e65df9 100644 --- a/stream-video-android-core/api/stream-video-android-core.api +++ b/stream-video-android-core/api/stream-video-android-core.api @@ -4203,11 +4203,13 @@ public final class io/getstream/android/video/generated/models/IngressVideoLayer } public final class io/getstream/android/video/generated/models/JoinCallRequest { - public fun (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;)V - public synthetic fun (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer;Ljava/util/List;)V + public synthetic fun (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Ljava/lang/String; public final fun component10 ()Ljava/util/List; public final fun component11 ()Lio/getstream/android/video/generated/models/CallRequest; + public final fun component12 ()Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer; + public final fun component13 ()Ljava/util/List; public final fun component2 ()Ljava/lang/Boolean; public final fun component3 ()Ljava/lang/Boolean; public final fun component4 ()Ljava/lang/Boolean; @@ -4216,8 +4218,8 @@ public final class io/getstream/android/video/generated/models/JoinCallRequest { public final fun component7 ()Ljava/lang/Boolean; public final fun component8 ()Ljava/lang/Boolean; public final fun component9 ()Ljava/lang/Boolean; - public final fun copy (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;)Lio/getstream/android/video/generated/models/JoinCallRequest; - public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/JoinCallRequest;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/JoinCallRequest; + public final fun copy (Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer;Ljava/util/List;)Lio/getstream/android/video/generated/models/JoinCallRequest; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/JoinCallRequest;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Integer;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/util/List;Lio/getstream/android/video/generated/models/CallRequest;Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer;Ljava/util/List;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/JoinCallRequest; public fun equals (Ljava/lang/Object;)Z public final fun getCreate ()Ljava/lang/Boolean; public final fun getData ()Lio/getstream/android/video/generated/models/CallRequest; @@ -4228,15 +4230,51 @@ public final class io/getstream/android/video/generated/models/JoinCallRequest { public final fun getMigratingFrom ()Ljava/lang/String; public final fun getMigratingFromList ()Ljava/util/List; public final fun getNotify ()Ljava/lang/Boolean; + public final fun getPlaybackCapabilities ()Ljava/util/List; + public final fun getPrefer ()Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer; public final fun getRing ()Ljava/lang/Boolean; public final fun getVideo ()Ljava/lang/Boolean; public fun hashCode ()I public fun toString ()Ljava/lang/String; } +public abstract class io/getstream/android/video/generated/models/JoinCallRequest$Prefer { + public static final field Companion Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer$Companion; + public synthetic fun (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getValue ()Ljava/lang/String; + public fun toString ()Ljava/lang/String; +} + +public final class io/getstream/android/video/generated/models/JoinCallRequest$Prefer$Companion { + public final fun fromString (Ljava/lang/String;)Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer; +} + +public final class io/getstream/android/video/generated/models/JoinCallRequest$Prefer$Hls : io/getstream/android/video/generated/models/JoinCallRequest$Prefer { + public static final field INSTANCE Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer$Hls; +} + +public final class io/getstream/android/video/generated/models/JoinCallRequest$Prefer$PreferAdapter : com/squareup/moshi/JsonAdapter { + public fun ()V + public fun fromJson (Lcom/squareup/moshi/JsonReader;)Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer; + public synthetic fun fromJson (Lcom/squareup/moshi/JsonReader;)Ljava/lang/Object; + public fun toJson (Lcom/squareup/moshi/JsonWriter;Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer;)V + public synthetic fun toJson (Lcom/squareup/moshi/JsonWriter;Ljava/lang/Object;)V +} + +public final class io/getstream/android/video/generated/models/JoinCallRequest$Prefer$Unknown : io/getstream/android/video/generated/models/JoinCallRequest$Prefer { + public fun (Ljava/lang/String;)V + public final fun component1 ()Ljava/lang/String; + public final fun copy (Ljava/lang/String;)Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer$Unknown; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer$Unknown;Ljava/lang/String;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/JoinCallRequest$Prefer$Unknown; + public fun equals (Ljava/lang/Object;)Z + public final fun getUnknownValue ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class io/getstream/android/video/generated/models/JoinCallResponse { - public fun (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;)V - public synthetic fun (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;Lio/getstream/android/video/generated/models/PlaybackResponse;)V + public synthetic fun (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;Lio/getstream/android/video/generated/models/PlaybackResponse;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun component1 ()Z public final fun component2 ()Ljava/lang/String; public final fun component3 ()Ljava/util/List; @@ -4245,8 +4283,9 @@ public final class io/getstream/android/video/generated/models/JoinCallResponse public final fun component6 ()Lio/getstream/android/video/generated/models/Credentials; public final fun component7 ()Lio/getstream/android/video/generated/models/StatsOptions; public final fun component8 ()Lio/getstream/android/video/generated/models/MemberResponse; - public final fun copy (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;)Lio/getstream/android/video/generated/models/JoinCallResponse; - public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/JoinCallResponse;ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/JoinCallResponse; + public final fun component9 ()Lio/getstream/android/video/generated/models/PlaybackResponse; + public final fun copy (ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;Lio/getstream/android/video/generated/models/PlaybackResponse;)Lio/getstream/android/video/generated/models/JoinCallResponse; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/JoinCallResponse;ZLjava/lang/String;Ljava/util/List;Ljava/util/List;Lio/getstream/android/video/generated/models/CallResponse;Lio/getstream/android/video/generated/models/Credentials;Lio/getstream/android/video/generated/models/StatsOptions;Lio/getstream/android/video/generated/models/MemberResponse;Lio/getstream/android/video/generated/models/PlaybackResponse;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/JoinCallResponse; public fun equals (Ljava/lang/Object;)Z public final fun getCall ()Lio/getstream/android/video/generated/models/CallResponse; public final fun getCreated ()Z @@ -4255,6 +4294,7 @@ public final class io/getstream/android/video/generated/models/JoinCallResponse public final fun getMembers ()Ljava/util/List; public final fun getMembership ()Lio/getstream/android/video/generated/models/MemberResponse; public final fun getOwnCapabilities ()Ljava/util/List; + public final fun getPlayback ()Lio/getstream/android/video/generated/models/PlaybackResponse; public final fun getStatsOptions ()Lio/getstream/android/video/generated/models/StatsOptions; public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -5190,6 +5230,93 @@ public final class io/getstream/android/video/generated/models/PinResponse { public fun toString ()Ljava/lang/String; } +public final class io/getstream/android/video/generated/models/PlaybackResponse { + public fun (Ljava/lang/String;Lio/getstream/android/video/generated/models/PlaybackResponse$Reason;Lio/getstream/android/video/generated/models/PlaybackResponse$Type;)V + public final fun component1 ()Ljava/lang/String; + public final fun component2 ()Lio/getstream/android/video/generated/models/PlaybackResponse$Reason; + public final fun component3 ()Lio/getstream/android/video/generated/models/PlaybackResponse$Type; + public final fun copy (Ljava/lang/String;Lio/getstream/android/video/generated/models/PlaybackResponse$Reason;Lio/getstream/android/video/generated/models/PlaybackResponse$Type;)Lio/getstream/android/video/generated/models/PlaybackResponse; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/PlaybackResponse;Ljava/lang/String;Lio/getstream/android/video/generated/models/PlaybackResponse$Reason;Lio/getstream/android/video/generated/models/PlaybackResponse$Type;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/PlaybackResponse; + public fun equals (Ljava/lang/Object;)Z + public final fun getPlaylistUrl ()Ljava/lang/String; + public final fun getReason ()Lio/getstream/android/video/generated/models/PlaybackResponse$Reason; + public final fun getType ()Lio/getstream/android/video/generated/models/PlaybackResponse$Type; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public abstract class io/getstream/android/video/generated/models/PlaybackResponse$Reason { + public static final field Companion Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Companion; + public synthetic fun (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getValue ()Ljava/lang/String; + public fun toString ()Ljava/lang/String; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Reason$Companion { + public final fun fromString (Ljava/lang/String;)Lio/getstream/android/video/generated/models/PlaybackResponse$Reason; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Reason$Hint : io/getstream/android/video/generated/models/PlaybackResponse$Reason { + public static final field INSTANCE Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Hint; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Reason$Percentage : io/getstream/android/video/generated/models/PlaybackResponse$Reason { + public static final field INSTANCE Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Percentage; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Reason$ReasonAdapter : com/squareup/moshi/JsonAdapter { + public fun ()V + public fun fromJson (Lcom/squareup/moshi/JsonReader;)Lio/getstream/android/video/generated/models/PlaybackResponse$Reason; + public synthetic fun fromJson (Lcom/squareup/moshi/JsonReader;)Ljava/lang/Object; + public fun toJson (Lcom/squareup/moshi/JsonWriter;Lio/getstream/android/video/generated/models/PlaybackResponse$Reason;)V + public synthetic fun toJson (Lcom/squareup/moshi/JsonWriter;Ljava/lang/Object;)V +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Reason$Unknown : io/getstream/android/video/generated/models/PlaybackResponse$Reason { + public fun (Ljava/lang/String;)V + public final fun component1 ()Ljava/lang/String; + public final fun copy (Ljava/lang/String;)Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Unknown; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Unknown;Ljava/lang/String;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/PlaybackResponse$Reason$Unknown; + public fun equals (Ljava/lang/Object;)Z + public final fun getUnknownValue ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public abstract class io/getstream/android/video/generated/models/PlaybackResponse$Type { + public static final field Companion Lio/getstream/android/video/generated/models/PlaybackResponse$Type$Companion; + public synthetic fun (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getValue ()Ljava/lang/String; + public fun toString ()Ljava/lang/String; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Type$Companion { + public final fun fromString (Ljava/lang/String;)Lio/getstream/android/video/generated/models/PlaybackResponse$Type; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Type$Hls : io/getstream/android/video/generated/models/PlaybackResponse$Type { + public static final field INSTANCE Lio/getstream/android/video/generated/models/PlaybackResponse$Type$Hls; +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Type$TypeAdapter : com/squareup/moshi/JsonAdapter { + public fun ()V + public fun fromJson (Lcom/squareup/moshi/JsonReader;)Lio/getstream/android/video/generated/models/PlaybackResponse$Type; + public synthetic fun fromJson (Lcom/squareup/moshi/JsonReader;)Ljava/lang/Object; + public fun toJson (Lcom/squareup/moshi/JsonWriter;Lio/getstream/android/video/generated/models/PlaybackResponse$Type;)V + public synthetic fun toJson (Lcom/squareup/moshi/JsonWriter;Ljava/lang/Object;)V +} + +public final class io/getstream/android/video/generated/models/PlaybackResponse$Type$Unknown : io/getstream/android/video/generated/models/PlaybackResponse$Type { + public fun (Ljava/lang/String;)V + public final fun component1 ()Ljava/lang/String; + public final fun copy (Ljava/lang/String;)Lio/getstream/android/video/generated/models/PlaybackResponse$Type$Unknown; + public static synthetic fun copy$default (Lio/getstream/android/video/generated/models/PlaybackResponse$Type$Unknown;Ljava/lang/String;ILjava/lang/Object;)Lio/getstream/android/video/generated/models/PlaybackResponse$Type$Unknown; + public fun equals (Ljava/lang/Object;)Z + public final fun getUnknownValue ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class io/getstream/android/video/generated/models/PublishedTrackFlags { public fun (ZZZZ)V public final fun component1 ()Z @@ -9190,6 +9317,7 @@ public final class io/getstream/video/android/core/CallState { public final fun getParticipants ()Lkotlinx/coroutines/flow/StateFlow; public final fun getPermissionRequests ()Lkotlinx/coroutines/flow/StateFlow; public final fun getPinnedParticipants ()Lkotlinx/coroutines/flow/StateFlow; + public final fun getPlaybackType ()Lkotlinx/coroutines/flow/StateFlow; public final fun getRawRecording ()Lkotlinx/coroutines/flow/StateFlow; public final fun getReactions ()Lkotlinx/coroutines/flow/StateFlow; public final fun getRecording ()Lkotlinx/coroutines/flow/StateFlow; @@ -13454,6 +13582,27 @@ public final class io/getstream/video/android/core/model/NetworkQuality$UnSpecif public fun toString ()Ljava/lang/String; } +public abstract interface class io/getstream/video/android/core/model/PlaybackType { +} + +public final class io/getstream/video/android/core/model/PlaybackType$Hls : io/getstream/video/android/core/model/PlaybackType { + public fun (Ljava/lang/String;)V + public final fun component1 ()Ljava/lang/String; + public final fun copy (Ljava/lang/String;)Lio/getstream/video/android/core/model/PlaybackType$Hls; + public static synthetic fun copy$default (Lio/getstream/video/android/core/model/PlaybackType$Hls;Ljava/lang/String;ILjava/lang/Object;)Lio/getstream/video/android/core/model/PlaybackType$Hls; + public fun equals (Ljava/lang/Object;)Z + public final fun getPlaylistUrl ()Ljava/lang/String; + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + +public final class io/getstream/video/android/core/model/PlaybackType$WebRtc : io/getstream/video/android/core/model/PlaybackType { + public static final field INSTANCE Lio/getstream/video/android/core/model/PlaybackType$WebRtc; + public fun equals (Ljava/lang/Object;)Z + public fun hashCode ()I + public fun toString ()Ljava/lang/String; +} + public final class io/getstream/video/android/core/model/PreferredVideoPublishOptions { public fun ()V public fun (Lio/getstream/video/android/core/model/VideoCodec;Ljava/lang/Integer;Ljava/lang/Integer;)V diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/infrastructure/Serializer.kt b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/infrastructure/Serializer.kt index cb79219f98e..8e6c263b394 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/infrastructure/Serializer.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/infrastructure/Serializer.kt @@ -45,9 +45,12 @@ object Serializer { .add(io.getstream.android.video.generated.models.IngressAudioEncodingOptionsRequest.Channels.ChannelsAdapter()) .add(io.getstream.android.video.generated.models.IngressSourceRequest.Fps.FpsAdapter()) .add(io.getstream.android.video.generated.models.IngressVideoLayerRequest.Codec.CodecAdapter()) + .add(io.getstream.android.video.generated.models.JoinCallRequest.Prefer.PreferAdapter()) .add(io.getstream.android.video.generated.models.LayoutSettingsRequest.Name.NameAdapter()) .add(io.getstream.android.video.generated.models.NoiseCancellationSettings.Mode.ModeAdapter()) .add(io.getstream.android.video.generated.models.OwnCapability.OwnCapabilityAdapter()) + .add(io.getstream.android.video.generated.models.PlaybackResponse.Reason.ReasonAdapter()) + .add(io.getstream.android.video.generated.models.PlaybackResponse.Type.TypeAdapter()) .add(io.getstream.android.video.generated.models.RTMPBroadcastRequest.Quality.QualityAdapter()) .add(io.getstream.android.video.generated.models.RTMPSettingsRequest.Quality.QualityAdapter()) .add(io.getstream.android.video.generated.models.RawRecordingSettingsRequest.Mode.ModeAdapter()) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallRequest.kt b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallRequest.kt index 70c56b0e5b7..b09039528c0 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallRequest.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallRequest.kt @@ -70,5 +70,43 @@ data class JoinCallRequest ( val migratingFromList: kotlin.collections.List? = emptyList(), @Json(name = "data") - val data: io.getstream.android.video.generated.models.CallRequest? = null + val data: io.getstream.android.video.generated.models.CallRequest? = null, + + @Json(name = "prefer") + val prefer: Prefer? = null, + + @Json(name = "playback_capabilities") + val playbackCapabilities: kotlin.collections.List? = emptyList() ) +{ + + /** + * Prefer Enum + */ + sealed class Prefer(val value: kotlin.String) { + override fun toString(): String = value + + companion object { + fun fromString(s: kotlin.String): Prefer = when (s) { + "hls" -> Hls + else -> Unknown(s) + } + } + object Hls : Prefer("hls") + data class Unknown(val unknownValue: kotlin.String) : Prefer(unknownValue) + + + class PreferAdapter : JsonAdapter() { + @FromJson + override fun fromJson(reader: JsonReader): Prefer? { + val s = reader.nextString() ?: return null + return Prefer.fromString(s) + } + + @ToJson + override fun toJson(writer: JsonWriter, value: Prefer?) { + writer.value(value?.value) + } + } + } +} diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallResponse.kt b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallResponse.kt index e6f62d923db..49cbef79c3e 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallResponse.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/JoinCallResponse.kt @@ -61,5 +61,8 @@ data class JoinCallResponse ( val statsOptions: io.getstream.android.video.generated.models.StatsOptions, @Json(name = "membership") - val membership: io.getstream.android.video.generated.models.MemberResponse? = null + val membership: io.getstream.android.video.generated.models.MemberResponse? = null, + + @Json(name = "playback") + val playback: io.getstream.android.video.generated.models.PlaybackResponse? = null ) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/PlaybackResponse.kt b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/PlaybackResponse.kt new file mode 100644 index 00000000000..d341a402e5e --- /dev/null +++ b/stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/PlaybackResponse.kt @@ -0,0 +1,113 @@ +/* + * 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. + */ + +@file:Suppress( + "ArrayInDataClass", + "EnumEntryName", + "RemoveRedundantQualifierName", + "UnusedImport" +) + +package io.getstream.android.video.generated.models + +import kotlin.collections.List +import kotlin.collections.Map +import kotlin.collections.* +import kotlin.io.* +import com.squareup.moshi.FromJson +import com.squareup.moshi.Json +import com.squareup.moshi.JsonAdapter +import com.squareup.moshi.JsonReader +import com.squareup.moshi.JsonWriter +import com.squareup.moshi.ToJson + +/** + * How the client should play the call when it is not given SFU credentials + */ + +data class PlaybackResponse ( + @Json(name = "playlist_url") + val playlistUrl: kotlin.String, + + @Json(name = "reason") + val reason: Reason, + + @Json(name = "type") + val type: Type +) +{ + + /** + * Reason Enum + */ + sealed class Reason(val value: kotlin.String) { + override fun toString(): String = value + + companion object { + fun fromString(s: kotlin.String): Reason = when (s) { + "hint" -> Hint + "percentage" -> Percentage + else -> Unknown(s) + } + } + object Hint : Reason("hint") + object Percentage : Reason("percentage") + data class Unknown(val unknownValue: kotlin.String) : Reason(unknownValue) + + + class ReasonAdapter : JsonAdapter() { + @FromJson + override fun fromJson(reader: JsonReader): Reason? { + val s = reader.nextString() ?: return null + return Reason.fromString(s) + } + + @ToJson + override fun toJson(writer: JsonWriter, value: Reason?) { + writer.value(value?.value) + } + } + } + /** + * Type Enum + */ + sealed class Type(val value: kotlin.String) { + override fun toString(): String = value + + companion object { + fun fromString(s: kotlin.String): Type = when (s) { + "hls" -> Hls + else -> Unknown(s) + } + } + object Hls : Type("hls") + data class Unknown(val unknownValue: kotlin.String) : Type(unknownValue) + + + class TypeAdapter : JsonAdapter() { + @FromJson + override fun fromJson(reader: JsonReader): Type? { + val s = reader.nextString() ?: return null + return Type.fromString(s) + } + + @ToJson + override fun toJson(writer: JsonWriter, value: Type?) { + writer.value(value?.value) + } + } + } +} diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt index 956b846c778..ea30e6354e7 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt @@ -54,6 +54,7 @@ import io.getstream.log.taggedLogger import io.getstream.result.Result import io.getstream.video.android.core.analytics.call.CallAnalytics import io.getstream.video.android.core.analytics.call.observer.model.JoinAnalyticsModel +import io.getstream.video.android.core.call.EmptyRtcSession import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.audio.InputAudioFilter import io.getstream.video.android.core.call.components.CallApiClient @@ -539,6 +540,17 @@ public class Call( clientImpl.permissionCheck .checkAndroidPermissionsGroup(clientImpl.context, this@Call).first }, + createPlaybackSession = { + EmptyRtcSession( + client = clientImpl, + call = this, + sessionManager = sessionManager, + sessionId = sessionManager.sessionId, + apiKey = clientImpl.apiKey, + lifecycle = clientImpl.coordinatorConnectionModule.lifecycle, + sfuAnalytics = callAnalytics.sfuAnalytics, + ) + }, consumeJoinSource = { consumeJoinSource() }, ) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/CallState.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/CallState.kt index c50ada4db77..a39e41fb17f 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/CallState.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/CallState.kt @@ -107,6 +107,7 @@ import io.getstream.video.android.core.events.TrackUnpublishedEvent import io.getstream.video.android.core.internal.InternalStreamVideoApi import io.getstream.video.android.core.model.Ingress import io.getstream.video.android.core.model.NetworkQuality +import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.model.RTMP import io.getstream.video.android.core.model.Reaction import io.getstream.video.android.core.model.RejectReason @@ -276,6 +277,18 @@ public class CallState( it is RealtimeConnection.Reconnecting } + internal val _playbackType = MutableStateFlow(PlaybackType.WebRtc) + + /** + * How this client receives the call's media. The backend decides on join: a viewer can be + * served the call's HLS broadcast instead of joining the SFU, in which case this is + * [PlaybackType.Hls] and participants carry no video or audio tracks. Back to + * [PlaybackType.WebRtc] after leaving. + * + * Beta: HLS delivery is enabled per call type by Stream and may change. + */ + public val playbackType: StateFlow = _playbackType + private val internalParticipants = ConcurrentHashMap() private val _participants = MutableStateFlow>(emptyMap()) @@ -1614,9 +1627,10 @@ public class CallState( // participant counts. Coordinator session snapshots (counts_updated, participant_joined/left, // and any event carrying a CallSessionResponse) carry a smaller, stale view at scale and // must NOT clobber the SFU value — that produces wild fluctuations in livestreams. + // An HLS viewer has no SFU, so the coordinator stays its only source. if (sfuHealthCheckEvent != null) { _participantCounts.value = sfuHealthCheckEvent.participantCount - } else if (session != null && !isInCall()) { + } else if (session != null && (!isInCall() || _playbackType.value is PlaybackType.Hls)) { val byRoleCount = session.participantsCountByRole.values.sum() val nonAnonymous = maxOf(byRoleCount, session.participants.size) _participantCounts.value = ParticipantCount( diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt index 616891596cb..83fabc187ed 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt @@ -865,6 +865,7 @@ internal class StreamVideoClient internal constructor( migratingFromList: List? = null, hintHighScaleLivestreamPublisher: Boolean? = null, e2ee: Boolean? = null, + playbackCapabilities: List? = null, ): Result { val joinCallRequest = JoinCallRequest( create = create, @@ -882,6 +883,7 @@ internal class StreamVideoClient internal constructor( migratingFrom = migratingFrom, migratingFromList = migratingFromList, hintHighScaleLivestreamPublisher = hintHighScaleLivestreamPublisher, + playbackCapabilities = playbackCapabilities, ) val result = apiCall { diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt index 463b80c8e4d..64f70033acd 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt @@ -90,6 +90,9 @@ internal class CallApiClient( /** * Executes the coordinator join request. Shared by the join flow and the reconnect flow * (rejoin / migrate), which is why it lives here rather than in either of them. + * + * Only the first join passes [playbackCapabilities]. Rejoin and migrate recover an SFU + * session, so they must always get SFU credentials back. */ suspend fun joinRequest( create: CreateCallOptions? = null, @@ -101,6 +104,7 @@ internal class CallApiClient( hintHighScaleLivestreamPublisher: Boolean? = null, joinAnalyticsModel: JoinAnalyticsModel, e2ee: Boolean? = null, + playbackCapabilities: List? = null, ): Result { val migratingFromList = migratingFromList ?: sessionManager.failedSfuIdsSnapshot().takeIf { it.isNotEmpty() } @@ -129,6 +133,7 @@ internal class CallApiClient( migratingFromList = migratingFromList, hintHighScaleLivestreamPublisher = hintHighScaleLivestreamPublisher, e2ee = requestE2ee, + playbackCapabilities = playbackCapabilities, ) result.onSuccess { callAnalytics.joinAnalytics.onJoinRequestSuccess( diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index 8aa9cf82ec5..e0ffd96561d 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -17,6 +17,7 @@ package io.getstream.video.android.core.call.components import io.getstream.android.video.generated.models.JoinCallResponse +import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.android.video.generated.models.RingCallRequest import io.getstream.log.taggedLogger import io.getstream.result.Error @@ -38,6 +39,7 @@ import io.getstream.video.android.core.analytics.reporting.model.AnalyticsCallAb import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectFailureCause import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.model.toIceServer import io.getstream.video.android.core.ringing.RingJoinSource import io.getstream.video.android.core.utils.StreamRefCountedSingleFlightProcessor @@ -70,11 +72,16 @@ internal class CallJoinCoordinator( private val sessionMonitor: SessionMonitor, private val callRegistry: ClientCallRegistry, private val hasRequiredPermissions: () -> Boolean, + // Builds the EmptyRtcSession returned when the backend serves HLS playback. + private val createPlaybackSession: () -> RtcSession, // Reads and clears the source of a ring-driven join; see Call.consumeJoinSource. private val consumeJoinSource: () -> RingJoinSource? = { null }, + // Declares HLS playback support on the first join. Off until the UI can render HLS. + private val hlsPlaybackSupported: Boolean = false, ) { private companion object { const val JOIN_FLIGHT_KEY = "join" + const val PLAYBACK_HLS = "hls" } private val logger by taggedLogger("Call:JoinCoordinator:$type:$id") @@ -222,7 +229,7 @@ internal class CallJoinCoordinator( ): Result { // Subsequent join() calls while a session is live return that session instead of // building a second one. [joinInternal] repeats the same check for direct callers. - sessionManager.session.value?.let { existing -> + joinedSession()?.let { existing -> logger.w { "[join] Call already joined — returning existing session" } existing.sfuTracer.trace("join-already-joined", "join() while session already live") return Success(existing) @@ -277,6 +284,10 @@ internal class CallJoinCoordinator( JoinAnalyticsModel(retryCount, JoinReason.FirstAttempt, joinSource), ) if (result is Success) { + // An HLS viewer publishes nothing, so local capture stays as it is. + if (state._playbackType.value is PlaybackType.Hls) { + return result + } // we initialise the camera, mic and other according to local + backend settings // only when the call is joined to make sure we don't switch and override // the settings during a call. @@ -382,7 +393,7 @@ internal class CallJoinCoordinator( // Gate before any teardown: cancelSfuObservers() would leave the live session without // its SFU event subscription, and only monitorSession() (further down, on the new-session // path) restores it. - sessionManager.session.value?.let { existing -> + joinedSession()?.let { existing -> logger.i { "[joinInternal] Call already joined — returning existing session" } existing.sfuTracer.trace( "join-already-joined", @@ -425,11 +436,15 @@ internal class CallJoinCoordinator( // The coordinator rejects a join whose flag disagrees with the call. This is // the first join; rejoin / migrate omit it and CallApiClient reads the manager. e2ee = state.e2eeEnabled.value, + playbackCapabilities = if (hlsPlaybackSupported) listOf(PLAYBACK_HLS) else null, ) if (result !is Success) { return result as Failure } + result.value.playback?.let { playback -> + return joinPlayback(playback) + } val sfuToken = result.value.credentials.token val sfuUrl = result.value.credentials.server.url val sfuWsUrl = result.value.credentials.server.wsEndpoint @@ -468,6 +483,36 @@ internal class CallJoinCoordinator( } } + private fun joinedSession(): RtcSession? = + sessionManager.session.value ?: sessionManager.playbackSession + + /** + * Completes a join the backend served as HLS playback. No SFU connection is made, so the + * session monitor (SFU events, network-driven reconnects) and the stats reporter never start. + * The returned session is held as [CallSessionManager.playbackSession], never as the active + * session. + */ + private fun joinPlayback(playback: PlaybackResponse): Result { + if (!hlsPlaybackSupported || + playback.type != PlaybackResponse.Type.Hls || + playback.playlistUrl.isBlank() + ) { + logger.e { + "[joinInternal] Unsupported playback (type=${playback.type}, " + + "hlsSupported=$hlsPlaybackSupported, hasUrl=${playback.playlistUrl.isNotBlank()})" + } + return Failure(Error.GenericError("Unsupported playback type: ${playback.type}")) + } + logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } + val session = createPlaybackSession() + sessionManager.playbackSession = session + state._playbackType.value = PlaybackType.Hls(playback.playlistUrl) + state._connection.value = RealtimeConnection.Joined(session) + callRegistry.markActive() + state._connection.value = RealtimeConnection.Connected + return Success(session) + } + private suspend fun completeJoinAfterSessionInstall( localSession: RtcSession, joinResponse: JoinCallResponse, diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt index f39b171a42f..14a54e5237a 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt @@ -25,6 +25,7 @@ import io.getstream.video.android.core.SdkCause import io.getstream.video.android.core.StreamVideoClient import io.getstream.video.android.core.analytics.call.CallAnalytics import io.getstream.video.android.core.call.scope.ScopeProvider +import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.utils.AtomicUnitCall import io.getstream.video.android.core.utils.safeCall import kotlinx.coroutines.launch @@ -93,6 +94,7 @@ internal class CallLifecycleManager( callAnalytics.stopObservers() sessionMonitor().cancelSfuObservers() state._connection.value = RealtimeConnection.Disconnected + clearPlayback() logger.v { "[leave] #ringing; call_id = $id" } if (isDestroyed) { logger.w { "[leave] #ringing; Call already destroyed, ignoring" } @@ -144,10 +146,17 @@ internal class CallLifecycleManager( statsReporter.stop() media.cleanup() // TODO Rahul, Verify Later: need to check which call has owned the media at the moment(probably use active call) sessionManager.setActiveSession(null) + clearPlayback() // Cleanup the call's scope provider scopeProvider.cleanup() } + private fun clearPlayback() { + sessionManager.playbackSession?.cleanup() + sessionManager.playbackSession = null + state._playbackType.value = PlaybackType.WebRtc + } + /** Stops the ICE and connectivity monitors. */ private fun stopConnectionMonitors() { iceMonitor.stop() diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt index abe9d5f43af..122bc4bc10a 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt @@ -59,6 +59,13 @@ internal class CallSessionManager() { _session.value = rtcSession } + /** + * The [EmptyRtcSession] returned by `join()` when the backend served HLS playback. Held + * apart from [session] so a repeated `join()` returns it instead of joining again. + */ + @Volatile + var playbackSession: RtcSession? = null + var sessionId = UUID.randomUUID().toString() val unifiedSessionId = UUID.randomUUID().toString() diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/model/PlaybackType.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/model/PlaybackType.kt new file mode 100644 index 00000000000..ea3a9f39fec --- /dev/null +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/model/PlaybackType.kt @@ -0,0 +1,37 @@ +/* + * 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.core.model + +import androidx.compose.runtime.Stable + +/** + * How this client receives the call's media, as decided by the backend when the call is joined. + * + * Beta: HLS delivery is enabled per call type by Stream and may change. + */ +@Stable +public sealed interface PlaybackType { + + /** Media comes from the SFU over WebRTC. The default, and the only mode for publishers. */ + public data object WebRtc : PlaybackType + + /** + * Media comes from the call's HLS broadcast at [playlistUrl]. There is no SFU connection, so + * participants have no video or audio tracks and the client cannot publish. + */ + public data class Hls(val playlistUrl: String) : PlaybackType +} diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index 362987da59f..28f0831b71b 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -19,6 +19,7 @@ package io.getstream.video.android.core.call.components import com.google.common.truth.Truth.assertThat import io.getstream.android.video.generated.models.CallSettingsResponse import io.getstream.android.video.generated.models.JoinCallResponse +import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.android.video.generated.models.RingCallRequest import io.getstream.android.video.generated.models.RingCallResponse import io.getstream.result.Error @@ -36,6 +37,7 @@ import io.getstream.video.android.core.base.DispatcherRule import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectFailureCause import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -67,6 +69,10 @@ import kotlin.test.assertFailsWith */ class CallJoinCoordinatorTest { + private companion object { + const val HLS_URL = "https://hls.example.com/playlist.m3u8" + } + @get:Rule val dispatcherRule = DispatcherRule() @@ -88,6 +94,9 @@ class CallJoinCoordinatorTest { private lateinit var connectionFlow: MutableStateFlow private lateinit var mockSession: RtcSession private lateinit var mockJoinResponse: JoinCallResponse + private lateinit var playbackTypeFlow: MutableStateFlow + private lateinit var playbackSession: RtcSession + private var heldPlaybackSession: RtcSession? = null @Before fun setup() { @@ -114,6 +123,16 @@ class CallJoinCoordinatorTest { every { state.settings } returns MutableStateFlow(null) every { state.e2eeEnabled } returns MutableStateFlow(false) coEvery { clientImpl.getCachedLocation() } returns Success("test-location") + + playbackTypeFlow = MutableStateFlow(PlaybackType.WebRtc) + playbackSession = mockk(relaxed = true) + heldPlaybackSession = null + every { state._playbackType } returns playbackTypeFlow + every { mockJoinResponse.playback } returns null + every { sessionManager.playbackSession } answers { heldPlaybackSession } + every { sessionManager.playbackSession = any() } answers { + heldPlaybackSession = firstArg() + } } @After @@ -121,7 +140,7 @@ class CallJoinCoordinatorTest { unmockkAll() } - private fun coordinator() = CallJoinCoordinator( + private fun coordinator(hlsPlaybackSupported: Boolean = false) = CallJoinCoordinator( clientImpl = clientImpl, state = state, callAnalytics = callAnalytics, @@ -137,16 +156,27 @@ class CallJoinCoordinatorTest { sessionMonitor = sessionMonitor, callRegistry = callRegistry, hasRequiredPermissions = { true }, + createPlaybackSession = { playbackSession }, + hlsPlaybackSupported = hlsPlaybackSupported, ) private fun stubJoinCall(result: io.getstream.result.Result) { coEvery { apiClient.joinRequest( - any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), ) } returns result } + private fun stubHlsPlayback() { + every { mockJoinResponse.playback } returns PlaybackResponse( + playlistUrl = HLS_URL, + reason = PlaybackResponse.Reason.Percentage, + type = PlaybackResponse.Type.Hls, + ) + stubJoinCall(Success(mockJoinResponse)) + } + @Test fun `join succeeds and returns the connected session`() = runTest(testDispatcher) { stubJoinCall(Success(mockJoinResponse)) @@ -735,4 +765,77 @@ class CallJoinCoordinatorTest { assertThat(result).isInstanceOf(Failure::class.java) coVerify { lifecycle.leave(any()) } } + + @Test + fun `first join declares HLS playback only when supported`() = runTest(testDispatcher) { + stubJoinCall(Success(mockJoinResponse)) + coEvery { mockSession.connectInternal() } returns SfuConnectionResult.Success + + coordinator(hlsPlaybackSupported = false).join() + advanceUntilIdle() + coVerify { + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), null) + } + + sessionFlow.value = null + coordinator(hlsPlaybackSupported = true).join() + advanceUntilIdle() + coVerify { + apiClient.joinRequest( + any(), any(), any(), any(), any(), any(), any(), any(), any(), + listOf( + "hls", + ), + ) + } + } + + @Test + fun `HLS playback joins without an SFU connection`() = runTest(testDispatcher) { + stubHlsPlayback() + + val result = coordinator(hlsPlaybackSupported = true).join() + advanceUntilIdle() + + assertThat((result as Success).value).isSameInstanceAs(playbackSession) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) + assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) + assertThat(heldPlaybackSession).isSameInstanceAs(playbackSession) + assertThat(sessionFlow.value).isNull() + verify { callRegistry.markActive() } + coVerify(exactly = 0) { mockSession.connectInternal() } + verify(exactly = 0) { sessionMonitor.monitorSession(any()) } + verify(exactly = 0) { media.updateMediaManagerFromSettings(any()) } + } + + @Test + fun `join after HLS playback returns the playback session`() = runTest(testDispatcher) { + stubHlsPlayback() + val coordinator = coordinator(hlsPlaybackSupported = true) + + coordinator.join() + advanceUntilIdle() + val second = coordinator.join() + advanceUntilIdle() + + assertThat((second as Success).value).isSameInstanceAs(playbackSession) + coVerify(exactly = 1) { + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) + } + } + + @Test + fun `HLS playback fails the join when HLS support is off`() = runTest(testDispatcher) { + stubHlsPlayback() + + val result = coordinator(hlsPlaybackSupported = false).join() + advanceUntilIdle() + + assertThat(result).isInstanceOf(Failure::class.java) + assertThat(connectionFlow.value).isInstanceOf(RealtimeConnection.Failed::class.java) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + assertThat(heldPlaybackSession).isNull() + verify(exactly = 0) { callRegistry.markActive() } + coVerify(exactly = 0) { mockSession.connectInternal() } + } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt new file mode 100644 index 00000000000..3bc3877e87e --- /dev/null +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt @@ -0,0 +1,90 @@ +/* + * 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.core.call.components + +import com.google.common.truth.Truth.assertThat +import io.getstream.video.android.core.CallState +import io.getstream.video.android.core.RealtimeConnection +import io.getstream.video.android.core.StreamVideoClient +import io.getstream.video.android.core.call.RtcSession +import io.getstream.video.android.core.model.PlaybackType +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.runTest +import org.junit.Before +import org.junit.Test + +class CallLifecycleManagerTest { + + private val testDispatcher = StandardTestDispatcher() + private val testScope = TestScope(testDispatcher) + + private lateinit var state: CallState + private lateinit var sessionManager: CallSessionManager + private lateinit var playbackTypeFlow: MutableStateFlow + + @Before + fun setup() { + state = mockk(relaxed = true) + sessionManager = CallSessionManager() + playbackTypeFlow = MutableStateFlow(PlaybackType.Hls("https://hls.example.com/playlist.m3u8")) + every { state._connection } returns MutableStateFlow(RealtimeConnection.Connected) + every { state._playbackType } returns playbackTypeFlow + } + + private fun manager(): CallLifecycleManager { + val clientImpl = mockk(relaxed = true) + every { clientImpl.scope } returns testScope + return CallLifecycleManager( + clientImpl = clientImpl, + sessionManager = sessionManager, + scopeProvider = mockk(relaxed = true), + callRegistry = mockk(relaxed = true), + shutDownJobs = {}, + state = state, + callAnalytics = mockk(relaxed = true), + statsReporter = mockk(relaxed = true), + media = mockk(relaxed = true), + iceMonitor = mockk(relaxed = true), + sessionMonitor = { mockk(relaxed = true) }, + connectivityMonitor = { mockk(relaxed = true) }, + type = "livestream", + id = "test-call", + ) + } + + @Test + fun `leave after HLS playback cleans up the playback session and resets the playback type`() = + runTest(testDispatcher) { + val playbackSession = mockk(relaxed = true) + sessionManager.playbackSession = playbackSession + + manager().leave("user") + advanceUntilIdle() + + assertThat(sessionManager.playbackSession).isNull() + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + verify { playbackSession.cleanup() } + coVerify(exactly = 0) { playbackSession.sendLeaveEvent(any()) } + } +} diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/model/PlaybackSerializationTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/model/PlaybackSerializationTest.kt new file mode 100644 index 00000000000..c3fea617b97 --- /dev/null +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/model/PlaybackSerializationTest.kt @@ -0,0 +1,65 @@ +/* + * 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.core.model + +import com.google.common.truth.Truth.assertThat +import io.getstream.android.video.generated.infrastructure.Serializer +import io.getstream.android.video.generated.models.JoinCallRequest +import io.getstream.android.video.generated.models.PlaybackResponse +import org.junit.Test + +class PlaybackSerializationTest { + + @Test + fun `join request sends playback capabilities and prefer`() { + val json = Serializer.moshi.adapter(JoinCallRequest::class.java).toJson( + JoinCallRequest( + location = "AMS", + prefer = JoinCallRequest.Prefer.Hls, + playbackCapabilities = listOf("hls"), + ), + ) + + assertThat(json).contains("\"playback_capabilities\":[\"hls\"]") + assertThat(json).contains("\"prefer\":\"hls\"") + } + + @Test + fun `playback response parses the HLS grant`() { + val playback = Serializer.moshi.adapter(PlaybackResponse::class.java).fromJson( + """{"type":"hls","playlist_url":"https://hls.example.com/playlist.m3u8","reason":"percentage"}""", + ) + + assertThat(playback).isEqualTo( + PlaybackResponse( + playlistUrl = "https://hls.example.com/playlist.m3u8", + reason = PlaybackResponse.Reason.Percentage, + type = PlaybackResponse.Type.Hls, + ), + ) + } + + @Test + fun `playback response keeps unknown types and reasons`() { + val playback = Serializer.moshi.adapter(PlaybackResponse::class.java).fromJson( + """{"type":"dash","playlist_url":"u","reason":"threshold"}""", + ) + + assertThat(playback?.type).isEqualTo(PlaybackResponse.Type.Unknown("dash")) + assertThat(playback?.reason).isEqualTo(PlaybackResponse.Reason.Unknown("threshold")) + } +} diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt index 6db4f7018c7..77299de293a 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt @@ -40,6 +40,7 @@ import io.getstream.video.android.core.call.components.CallSessionManager import io.getstream.video.android.core.call.components.ClientCallRegistry import io.getstream.video.android.core.call.components.RtcSessionFactory import io.getstream.video.android.core.call.components.SessionMonitor +import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -101,6 +102,8 @@ class JoinRecoverableFailureTest { every { state.connection } returns connectionFlow every { state.settings } returns MutableStateFlow(null) every { state.e2eeEnabled } returns MutableStateFlow(false) + every { state._playbackType } returns MutableStateFlow(PlaybackType.WebRtc) + every { mockJoinResponse.playback } returns null coEvery { clientImpl.getCachedLocation() } returns Success("test-location") coEvery { apiClient.joinRequest( @@ -130,6 +133,7 @@ class JoinRecoverableFailureTest { sessionMonitor = mockk(relaxed = true), callRegistry = mockk(relaxed = true), hasRequiredPermissions = { true }, + createPlaybackSession = { mockk(relaxed = true) }, ) private suspend fun join() = coordinator().joinInternal( From de6226ad4952c112120de95798bb56d60fe3d591 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Mon, 2 Nov 2026 23:15:25 +0530 Subject: [PATCH 05/10] feat(core): declare HLS on every join and move to HLS on rejoin or migrate --- .../io/getstream/video/android/core/Call.kt | 26 ++-- .../core/call/components/CallApiClient.kt | 8 +- .../call/components/CallJoinCoordinator.kt | 14 +- .../core/call/components/CallReconnector.kt | 41 ++++++ .../core/call/components/HlsPlayback.kt | 23 ++++ .../core/call/components/SessionMonitor.kt | 12 ++ .../core/call/components/CallApiClientTest.kt | 48 ++++++- .../components/CallJoinCoordinatorTest.kt | 46 ++----- .../call/components/CallReconnectorTest.kt | 124 +++++++++++++++++- .../video/android/core/e2ee/CallE2EETest.kt | 4 +- 10 files changed, 278 insertions(+), 68 deletions(-) create mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt index ea30e6354e7..5a149eb6502 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt @@ -210,6 +210,19 @@ public class Call( ) } + /** Creates the [EmptyRtcSession] that join / rejoin / migrate return for HLS playback. */ + private val playbackSessionFactory: () -> RtcSession = { + EmptyRtcSession( + client = clientImpl, + call = this, + sessionManager = sessionManager, + sessionId = sessionManager.sessionId, + apiKey = clientImpl.apiKey, + lifecycle = clientImpl.coordinatorConnectionModule.lifecycle, + sfuAnalytics = callAnalytics.sfuAnalytics, + ) + } + /** * The call's registration in the client-level ringing / active / telecom registries. Every * operation needs this instance, which the extracted components deliberately don't hold. @@ -425,6 +438,7 @@ public class Call( state = state, callAnalytics = callAnalytics, sessionMonitor = { sessionMonitor }, + createPlaybackSession = playbackSessionFactory, type = type, id = id, ) @@ -540,17 +554,7 @@ public class Call( clientImpl.permissionCheck .checkAndroidPermissionsGroup(clientImpl.context, this@Call).first }, - createPlaybackSession = { - EmptyRtcSession( - client = clientImpl, - call = this, - sessionManager = sessionManager, - sessionId = sessionManager.sessionId, - apiKey = clientImpl.apiKey, - lifecycle = clientImpl.coordinatorConnectionModule.lifecycle, - sfuAnalytics = callAnalytics.sfuAnalytics, - ) - }, + createPlaybackSession = playbackSessionFactory, consumeJoinSource = { consumeJoinSource() }, ) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt index 64f70033acd..8794020dcb4 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt @@ -30,6 +30,7 @@ import io.getstream.android.video.generated.models.ListTranscriptionsResponse import io.getstream.android.video.generated.models.MemberRequest import io.getstream.android.video.generated.models.MuteUsersResponse import io.getstream.android.video.generated.models.PinResponse +import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.android.video.generated.models.RejectCallResponse import io.getstream.android.video.generated.models.RingCallRequest import io.getstream.android.video.generated.models.RingCallResponse @@ -91,8 +92,8 @@ internal class CallApiClient( * Executes the coordinator join request. Shared by the join flow and the reconnect flow * (rejoin / migrate), which is why it lives here rather than in either of them. * - * Only the first join passes [playbackCapabilities]. Rejoin and migrate recover an SFU - * session, so they must always get SFU credentials back. + * Every request declares HLS playback support, so the coordinator may answer any of them + * with [JoinCallResponse.playback] instead of SFU credentials. */ suspend fun joinRequest( create: CreateCallOptions? = null, @@ -104,7 +105,6 @@ internal class CallApiClient( hintHighScaleLivestreamPublisher: Boolean? = null, joinAnalyticsModel: JoinAnalyticsModel, e2ee: Boolean? = null, - playbackCapabilities: List? = null, ): Result { val migratingFromList = migratingFromList ?: sessionManager.failedSfuIdsSnapshot().takeIf { it.isNotEmpty() } @@ -133,7 +133,7 @@ internal class CallApiClient( migratingFromList = migratingFromList, hintHighScaleLivestreamPublisher = hintHighScaleLivestreamPublisher, e2ee = requestE2ee, - playbackCapabilities = playbackCapabilities, + playbackCapabilities = listOf(PlaybackResponse.Type.Hls.value), ) result.onSuccess { callAnalytics.joinAnalytics.onJoinRequestSuccess( diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index e0ffd96561d..22cd7220825 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -76,12 +76,9 @@ internal class CallJoinCoordinator( private val createPlaybackSession: () -> RtcSession, // Reads and clears the source of a ring-driven join; see Call.consumeJoinSource. private val consumeJoinSource: () -> RingJoinSource? = { null }, - // Declares HLS playback support on the first join. Off until the UI can render HLS. - private val hlsPlaybackSupported: Boolean = false, ) { private companion object { const val JOIN_FLIGHT_KEY = "join" - const val PLAYBACK_HLS = "hls" } private val logger by taggedLogger("Call:JoinCoordinator:$type:$id") @@ -436,7 +433,6 @@ internal class CallJoinCoordinator( // The coordinator rejects a join whose flag disagrees with the call. This is // the first join; rejoin / migrate omit it and CallApiClient reads the manager. e2ee = state.e2eeEnabled.value, - playbackCapabilities = if (hlsPlaybackSupported) listOf(PLAYBACK_HLS) else null, ) if (result !is Success) { @@ -493,20 +489,18 @@ internal class CallJoinCoordinator( * session. */ private fun joinPlayback(playback: PlaybackResponse): Result { - if (!hlsPlaybackSupported || - playback.type != PlaybackResponse.Type.Hls || - playback.playlistUrl.isBlank() - ) { + val playlistUrl = playback.hlsPlaylistUrlOrNull() + if (playlistUrl == null) { logger.e { "[joinInternal] Unsupported playback (type=${playback.type}, " + - "hlsSupported=$hlsPlaybackSupported, hasUrl=${playback.playlistUrl.isNotBlank()})" + "hasUrl=${playback.playlistUrl.isNotBlank()})" } return Failure(Error.GenericError("Unsupported playback type: ${playback.type}")) } logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } val session = createPlaybackSession() sessionManager.playbackSession = session - state._playbackType.value = PlaybackType.Hls(playback.playlistUrl) + state._playbackType.value = PlaybackType.Hls(playlistUrl) state._connection.value = RealtimeConnection.Joined(session) callRegistry.markActive() state._connection.value = RealtimeConnection.Connected diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt index b51d8549c9e..ff36ec6f31e 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt @@ -16,6 +16,7 @@ package io.getstream.video.android.core.call.components +import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.log.taggedLogger import io.getstream.result.Result.Success import io.getstream.video.android.core.BackendCause @@ -29,7 +30,9 @@ import io.getstream.video.android.core.analytics.call.observer.model.JoinAnalyti import io.getstream.video.android.core.analytics.call.observer.model.JoinReason import io.getstream.video.android.core.analytics.reporting.model.AnalyticsCallAbortReason import io.getstream.video.android.core.call.FastReconnectResult +import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.model.toIceServer import kotlinx.coroutines.delay import kotlinx.coroutines.sync.Mutex @@ -79,6 +82,8 @@ internal class CallReconnector( // Lazy provider: the session monitor is built from the connectivity monitor, which takes // this component, so it can only be resolved after construction. private val sessionMonitor: () -> SessionMonitor, + // Builds the EmptyRtcSession used when a rejoin or migrate is answered with HLS playback. + private val createPlaybackSession: () -> RtcSession, private val type: String, private val id: String, ) { @@ -128,6 +133,11 @@ internal class CallReconnector( } return } + // HLS playback has no SFU connection to recover; the player handles its own retries. + if (sessionManager.playbackSession != null) { + logger.d { "[reconnect] HLS playback has no SFU connection — skipping ($reason)" } + return + } // Use tryLock so concurrent triggers (stateJob, NetworkStateListener, // SfuSocket errors) don't queue up. If a reconnect loop is already @@ -395,6 +405,9 @@ internal class CallReconnector( Exception("Failed to get join response: ${joinResponse.errorOrNull()}"), ) } + joinResponse.value.playback?.let { playback -> + return switchToPlayback(oldSession, playback) + } val cred = joinResponse.value.credentials val currentOptions = oldSession.publisher.value?.currentOptions() @@ -465,6 +478,9 @@ internal class CallReconnector( ), ) } + joinResponse.value.playback?.let { playback -> + return switchToPlayback(oldSession, playback) + } val cred = joinResponse.value.credentials val currentOptions = oldSession.publisher.value?.currentOptions() @@ -512,6 +528,31 @@ internal class CallReconnector( } } + /** + * Moves a viewer from the SFU to HLS playback when a rejoin or migrate is answered with an + * HLS grant. The old session leaves the SFU, SFU monitoring stops, and participants (whose + * tracks came from the SFU) are cleared. An unplayable grant is terminal: the stable + * backend bucket would answer every retry the same way. + */ + private suspend fun switchToPlayback( + oldSession: RtcSession, + playback: PlaybackResponse, + ): ReconnectOutcome { + val playlistUrl = playback.hlsPlaylistUrlOrNull() + ?: return ReconnectOutcome.PreconditionNotMet( + "Unsupported playback type: ${playback.type}", + ) + logger.i { "[reconnect] Moving to HLS playback (reason=${playback.reason})" } + sessionMonitor().stopMonitoring() + oldSession.prepareRejoin("playback-hls") + sessionManager.setActiveSession(null) + state.clearParticipants() + sessionManager.playbackSession = createPlaybackSession() + state._playbackType.value = PlaybackType.Hls(playlistUrl) + state._connection.value = RealtimeConnection.Connected + return ReconnectOutcome.Success + } + suspend fun fastReconnect(reason: String = "unknown") { reconnect(WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_FAST, reason) } diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt new file mode 100644 index 00000000000..6c57a275695 --- /dev/null +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt @@ -0,0 +1,23 @@ +/* + * 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.core.call.components + +import io.getstream.android.video.generated.models.PlaybackResponse + +/** The playlist URL of an HLS grant, or `null` when the grant can't be played. */ +internal fun PlaybackResponse.hlsPlaylistUrlOrNull(): String? = + playlistUrl.takeIf { type == PlaybackResponse.Type.Hls && it.isNotBlank() } diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt index 452d52914fc..05556fd3690 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt @@ -85,4 +85,16 @@ internal class SessionMonitor( iceMonitor.start() connectivityMonitor.subscribe() } + + /** + * Stops everything [monitorSession] started, for a call that moves from the SFU to HLS + * playback. Network changes must no longer trigger SFU reconnects. + */ + fun stopMonitoring() { + cancelSfuObservers() + statsReporter.stop() + iceMonitor.stop() + connectivityMonitor.cancelLeaveTimeout() + connectivityMonitor.unsubscribe() + } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt index efacc24d47a..0b839b716df 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt @@ -335,7 +335,7 @@ class CallApiClientTest { coEvery { clientImpl.joinCall( any(), any(), any(), any(), any(), any(), any(), any(), - any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), ) } returns Result.Failure(io.getstream.result.Error.GenericError("boom")) @@ -348,7 +348,7 @@ class CallApiClientTest { coVerify { clientImpl.joinCall( any(), any(), any(), any(), any(), any(), any(), any(), - any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), ) } } @@ -374,7 +374,7 @@ class CallApiClientTest { coEvery { clientImpl.joinCall( any(), any(), any(), any(), any(), any(), any(), any(), - any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), ) } returns Result.Failure(io.getstream.result.Error.GenericError("boom")) @@ -400,6 +400,48 @@ class CallApiClientTest { migratingFromList = any(), hintHighScaleLivestreamPublisher = any(), e2ee = true, + playbackCapabilities = any(), + ) + } + } + + @Test + fun `joinRequest declares HLS playback support on every join`() = runTest(testDispatcher) { + coEvery { + clientImpl.joinCall( + any(), any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), + ) + } returns Result.Failure(io.getstream.result.Error.GenericError("boom")) + + apiClient.joinRequest( + location = "test-location", + joinAnalyticsModel = JoinAnalyticsModel(0, JoinReason.FirstAttempt), + ) + apiClient.joinRequest( + location = "test-location", + migratingFrom = "sfu-1", + joinAnalyticsModel = JoinAnalyticsModel(1, JoinReason.Migrate), + ) + + coVerify(exactly = 2) { + clientImpl.joinCall( + type = any(), + id = any(), + create = any(), + members = any(), + custom = any(), + settingsOverride = any(), + startsAt = any(), + team = any(), + ring = any(), + notify = any(), + location = any(), + migratingFrom = any(), + migratingFromList = any(), + hintHighScaleLivestreamPublisher = any(), + e2ee = any(), + playbackCapabilities = listOf("hls"), ) } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index 28f0831b71b..5a3daaba1ae 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -140,7 +140,7 @@ class CallJoinCoordinatorTest { unmockkAll() } - private fun coordinator(hlsPlaybackSupported: Boolean = false) = CallJoinCoordinator( + private fun coordinator() = CallJoinCoordinator( clientImpl = clientImpl, state = state, callAnalytics = callAnalytics, @@ -157,13 +157,12 @@ class CallJoinCoordinatorTest { callRegistry = callRegistry, hasRequiredPermissions = { true }, createPlaybackSession = { playbackSession }, - hlsPlaybackSupported = hlsPlaybackSupported, ) private fun stubJoinCall(result: io.getstream.result.Result) { coEvery { apiClient.joinRequest( - any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), any(), ) } returns result } @@ -766,35 +765,11 @@ class CallJoinCoordinatorTest { coVerify { lifecycle.leave(any()) } } - @Test - fun `first join declares HLS playback only when supported`() = runTest(testDispatcher) { - stubJoinCall(Success(mockJoinResponse)) - coEvery { mockSession.connectInternal() } returns SfuConnectionResult.Success - - coordinator(hlsPlaybackSupported = false).join() - advanceUntilIdle() - coVerify { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), null) - } - - sessionFlow.value = null - coordinator(hlsPlaybackSupported = true).join() - advanceUntilIdle() - coVerify { - apiClient.joinRequest( - any(), any(), any(), any(), any(), any(), any(), any(), any(), - listOf( - "hls", - ), - ) - } - } - @Test fun `HLS playback joins without an SFU connection`() = runTest(testDispatcher) { stubHlsPlayback() - val result = coordinator(hlsPlaybackSupported = true).join() + val result = coordinator().join() advanceUntilIdle() assertThat((result as Success).value).isSameInstanceAs(playbackSession) @@ -811,7 +786,7 @@ class CallJoinCoordinatorTest { @Test fun `join after HLS playback returns the playback session`() = runTest(testDispatcher) { stubHlsPlayback() - val coordinator = coordinator(hlsPlaybackSupported = true) + val coordinator = coordinator() coordinator.join() advanceUntilIdle() @@ -820,15 +795,20 @@ class CallJoinCoordinatorTest { assertThat((second as Success).value).isSameInstanceAs(playbackSession) coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) } } @Test - fun `HLS playback fails the join when HLS support is off`() = runTest(testDispatcher) { - stubHlsPlayback() + fun `an unplayable playback grant fails the join`() = runTest(testDispatcher) { + every { mockJoinResponse.playback } returns PlaybackResponse( + playlistUrl = HLS_URL, + reason = PlaybackResponse.Reason.Percentage, + type = PlaybackResponse.Type.Unknown("dash"), + ) + stubJoinCall(Success(mockJoinResponse)) - val result = coordinator(hlsPlaybackSupported = false).join() + val result = coordinator().join() advanceUntilIdle() assertThat(result).isInstanceOf(Failure::class.java) diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt index f4b9e57ff82..d31510155d8 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt @@ -18,6 +18,7 @@ package io.getstream.video.android.core.call.components import com.google.common.truth.Truth.assertThat import io.getstream.android.video.generated.models.JoinCallResponse +import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.result.Result.Success import io.getstream.video.android.core.CallLeaveReason import io.getstream.video.android.core.CallState @@ -28,6 +29,7 @@ import io.getstream.video.android.core.analytics.call.CallAnalytics import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectionResult import io.getstream.video.android.core.call.connection.Publisher +import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -48,6 +50,10 @@ import stream.video.sfu.models.WebsocketReconnectStrategy */ class CallReconnectorTest { + private companion object { + const val HLS_URL = "https://hls.example.com/playlist.m3u8" + } + private val testDispatcher = StandardTestDispatcher() private lateinit var clientImpl: StreamVideoClient @@ -59,6 +65,8 @@ class CallReconnectorTest { private lateinit var apiClient: CallApiClient private lateinit var callAnalytics: CallAnalytics private lateinit var sessionFactory: RtcSessionFactory + private lateinit var playbackTypeFlow: MutableStateFlow + private lateinit var playbackSession: RtcSession @Before fun setup() { @@ -83,6 +91,9 @@ class CallReconnectorTest { } returns true every { state.connection } returns connectionFlow every { state._connection } returns connectionFlow + playbackTypeFlow = MutableStateFlow(PlaybackType.WebRtc) + playbackSession = mockk(relaxed = true) + every { state._playbackType } returns playbackTypeFlow } private fun reconnector() = CallReconnector( @@ -94,6 +105,7 @@ class CallReconnectorTest { state = state, callAnalytics = callAnalytics, sessionMonitor = { sessionMonitor }, + createPlaybackSession = { playbackSession }, type = "default", id = "call-id", ) @@ -178,7 +190,7 @@ class CallReconnectorTest { fun `rejoin swaps in the new session and monitors it on success`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = mockk(relaxed = true) + val joinResponse = sfuJoinResponse() prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success @@ -196,7 +208,7 @@ class CallReconnectorTest { fun `rejoin failures are retried until the attempts are exhausted`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = mockk(relaxed = true) + val joinResponse = sfuJoinResponse() prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Failure( Exception("rejoin failed"), @@ -218,7 +230,7 @@ class CallReconnectorTest { ) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = mockk(relaxed = true) + val joinResponse = sfuJoinResponse() prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success @@ -237,7 +249,7 @@ class CallReconnectorTest { fun `rejoin leaves the call once the rejoin budget is spent`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) + prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) repeat(RejoinRateLimiter.MAX_ATTEMPTS) { sessionManager.rejoinRateLimiter.tryRegister() } reconnector().reconnect( @@ -258,7 +270,7 @@ class CallReconnectorTest { fun `migrate counts against the rejoin budget`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) + prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) repeat(RejoinRateLimiter.MAX_ATTEMPTS) { sessionManager.rejoinRateLimiter.tryRegister() } reconnector().reconnect( @@ -295,7 +307,7 @@ class CallReconnectorTest { fun `a rejoin with budget left still issues a join request`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) + prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success repeat(RejoinRateLimiter.MAX_ATTEMPTS - 1) { sessionManager.rejoinRateLimiter.tryRegister() @@ -311,6 +323,106 @@ class CallReconnectorTest { verify(exactly = 0) { lifecycle.leave(any()) } } + @Test + fun `rejoin answered with HLS moves the viewer to HLS playback`() = runTest(testDispatcher) { + val oldSession = mockk(relaxed = true) + prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), hlsJoinResponse()) + + reconnector().reconnect( + WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, + "rejoin", + ) + advanceUntilIdle() + + assertHlsPlayback(oldSession) + } + + @Test + fun `migrate answered with HLS moves the viewer to HLS playback`() = runTest(testDispatcher) { + val oldSession = mockk(relaxed = true) + prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), hlsJoinResponse()) + + reconnector().reconnect( + WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_MIGRATE, + "migrate", + ) + advanceUntilIdle() + + assertHlsPlayback(oldSession) + verify(exactly = 0) { oldSession.enterMigration() } + } + + @Test + fun `an unplayable grant on rejoin gives up without leaving the SFU session`() = runTest( + testDispatcher, + ) { + val oldSession = mockk(relaxed = true) + val joinResponse = mockk(relaxed = true) + every { joinResponse.playback } returns PlaybackResponse( + playlistUrl = "", + reason = PlaybackResponse.Reason.Percentage, + type = PlaybackResponse.Type.Hls, + ) + prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), joinResponse) + + reconnector().reconnect( + WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, + "rejoin", + ) + advanceUntilIdle() + + assertThat(connectionFlow.value) + .isInstanceOf(RealtimeConnection.ReconnectingFailed::class.java) + assertThat(sessionManager.playbackSession).isNull() + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + verify(exactly = 0) { sessionMonitor.stopMonitoring() } + coVerify(exactly = 0) { oldSession.prepareRejoin(any()) } + } + + @Test + fun `reconnect is skipped during HLS playback`() = runTest(testDispatcher) { + sessionManager.playbackSession = mockk(relaxed = true) + + reconnector().reconnect( + WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, + "network", + ) + advanceUntilIdle() + + coVerify(exactly = 0) { + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any()) + } + verify(exactly = 0) { lifecycle.leave(any()) } + assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Reconnecting) + } + + private fun sfuJoinResponse(): JoinCallResponse = mockk(relaxed = true).also { + every { it.playback } returns null + } + + private fun hlsJoinResponse(): JoinCallResponse = mockk(relaxed = true).also { + every { it.playback } returns PlaybackResponse( + playlistUrl = HLS_URL, + reason = PlaybackResponse.Reason.Percentage, + type = PlaybackResponse.Type.Hls, + ) + } + + private fun assertHlsPlayback(oldSession: RtcSession) { + assertThat(sessionManager.session.value).isNull() + assertThat(sessionManager.playbackSession).isSameInstanceAs(playbackSession) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) + assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) + verify { sessionMonitor.stopMonitoring() } + coVerify { oldSession.prepareRejoin(any()) } + verify { state.clearParticipants() } + verify( + exactly = 0, + ) { sessionFactory.create(any(), any(), any(), any(), any(), any(), any()) } + verify(exactly = 0) { sessionMonitor.monitorSession(any()) } + verify(exactly = 0) { lifecycle.leave(any()) } + } + /** * Wires up the shared happy-path state a rejoin/migrate needs: a resolvable location, * an existing (old) session, a stubbed join request and an injected new session. diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/e2ee/CallE2EETest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/e2ee/CallE2EETest.kt index 81c3bbdd5c0..05f9c74650e 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/e2ee/CallE2EETest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/e2ee/CallE2EETest.kt @@ -97,7 +97,7 @@ class CallE2EETest { coEvery { mockClientImpl.joinCall( any(), any(), any(), any(), any(), any(), any(), any(), - any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), ) } returns Result.Failure(Error.GenericError("not under test")) @@ -196,6 +196,7 @@ class CallE2EETest { migratingFromList = any(), hintHighScaleLivestreamPublisher = any(), e2ee = true, + playbackCapabilities = any(), ) } } @@ -224,6 +225,7 @@ class CallE2EETest { migratingFromList = any(), hintHighScaleLivestreamPublisher = any(), e2ee = false, + playbackCapabilities = any(), ) } } From f07c821795e5644260479890b84c24258252462e Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Tue, 3 Nov 2026 18:00:59 +0530 Subject: [PATCH 06/10] refactor(core): declare HLS playback on the first join only --- .../io/getstream/video/android/core/Call.kt | 3 +- .../core/call/components/CallApiClient.kt | 8 +- .../call/components/CallJoinCoordinator.kt | 5 +- .../core/call/components/CallReconnector.kt | 36 ------ .../core/call/components/HlsPlayback.kt | 23 ---- .../core/call/components/SessionMonitor.kt | 12 -- .../core/call/components/CallApiClientTest.kt | 48 ++++---- .../components/CallJoinCoordinatorTest.kt | 52 ++++++--- .../call/components/CallReconnectorTest.kt | 107 +----------------- .../core/rtc/JoinRecoverableFailureTest.kt | 2 +- 10 files changed, 81 insertions(+), 215 deletions(-) delete mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt index 5a149eb6502..1c105d16149 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt @@ -210,7 +210,7 @@ public class Call( ) } - /** Creates the [EmptyRtcSession] that join / rejoin / migrate return for HLS playback. */ + /** Creates the [EmptyRtcSession] that the first join returns for HLS playback. */ private val playbackSessionFactory: () -> RtcSession = { EmptyRtcSession( client = clientImpl, @@ -438,7 +438,6 @@ public class Call( state = state, callAnalytics = callAnalytics, sessionMonitor = { sessionMonitor }, - createPlaybackSession = playbackSessionFactory, type = type, id = id, ) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt index 8794020dcb4..64f70033acd 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallApiClient.kt @@ -30,7 +30,6 @@ import io.getstream.android.video.generated.models.ListTranscriptionsResponse import io.getstream.android.video.generated.models.MemberRequest import io.getstream.android.video.generated.models.MuteUsersResponse import io.getstream.android.video.generated.models.PinResponse -import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.android.video.generated.models.RejectCallResponse import io.getstream.android.video.generated.models.RingCallRequest import io.getstream.android.video.generated.models.RingCallResponse @@ -92,8 +91,8 @@ internal class CallApiClient( * Executes the coordinator join request. Shared by the join flow and the reconnect flow * (rejoin / migrate), which is why it lives here rather than in either of them. * - * Every request declares HLS playback support, so the coordinator may answer any of them - * with [JoinCallResponse.playback] instead of SFU credentials. + * Only the first join passes [playbackCapabilities]. Rejoin and migrate recover an SFU + * session, so they must always get SFU credentials back. */ suspend fun joinRequest( create: CreateCallOptions? = null, @@ -105,6 +104,7 @@ internal class CallApiClient( hintHighScaleLivestreamPublisher: Boolean? = null, joinAnalyticsModel: JoinAnalyticsModel, e2ee: Boolean? = null, + playbackCapabilities: List? = null, ): Result { val migratingFromList = migratingFromList ?: sessionManager.failedSfuIdsSnapshot().takeIf { it.isNotEmpty() } @@ -133,7 +133,7 @@ internal class CallApiClient( migratingFromList = migratingFromList, hintHighScaleLivestreamPublisher = hintHighScaleLivestreamPublisher, e2ee = requestE2ee, - playbackCapabilities = listOf(PlaybackResponse.Type.Hls.value), + playbackCapabilities = playbackCapabilities, ) result.onSuccess { callAnalytics.joinAnalytics.onJoinRequestSuccess( diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index 22cd7220825..a9f4c5ee11f 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -433,6 +433,7 @@ internal class CallJoinCoordinator( // The coordinator rejects a join whose flag disagrees with the call. This is // the first join; rejoin / migrate omit it and CallApiClient reads the manager. e2ee = state.e2eeEnabled.value, + playbackCapabilities = listOf(PlaybackResponse.Type.Hls.value), ) if (result !is Success) { @@ -489,8 +490,8 @@ internal class CallJoinCoordinator( * session. */ private fun joinPlayback(playback: PlaybackResponse): Result { - val playlistUrl = playback.hlsPlaylistUrlOrNull() - if (playlistUrl == null) { + val playlistUrl = playback.playlistUrl + if (playback.type != PlaybackResponse.Type.Hls || playlistUrl.isBlank()) { logger.e { "[joinInternal] Unsupported playback (type=${playback.type}, " + "hasUrl=${playback.playlistUrl.isNotBlank()})" diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt index ff36ec6f31e..2eb07df2623 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallReconnector.kt @@ -16,7 +16,6 @@ package io.getstream.video.android.core.call.components -import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.log.taggedLogger import io.getstream.result.Result.Success import io.getstream.video.android.core.BackendCause @@ -30,9 +29,7 @@ import io.getstream.video.android.core.analytics.call.observer.model.JoinAnalyti import io.getstream.video.android.core.analytics.call.observer.model.JoinReason import io.getstream.video.android.core.analytics.reporting.model.AnalyticsCallAbortReason import io.getstream.video.android.core.call.FastReconnectResult -import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectionResult -import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.model.toIceServer import kotlinx.coroutines.delay import kotlinx.coroutines.sync.Mutex @@ -82,8 +79,6 @@ internal class CallReconnector( // Lazy provider: the session monitor is built from the connectivity monitor, which takes // this component, so it can only be resolved after construction. private val sessionMonitor: () -> SessionMonitor, - // Builds the EmptyRtcSession used when a rejoin or migrate is answered with HLS playback. - private val createPlaybackSession: () -> RtcSession, private val type: String, private val id: String, ) { @@ -405,9 +400,6 @@ internal class CallReconnector( Exception("Failed to get join response: ${joinResponse.errorOrNull()}"), ) } - joinResponse.value.playback?.let { playback -> - return switchToPlayback(oldSession, playback) - } val cred = joinResponse.value.credentials val currentOptions = oldSession.publisher.value?.currentOptions() @@ -478,9 +470,6 @@ internal class CallReconnector( ), ) } - joinResponse.value.playback?.let { playback -> - return switchToPlayback(oldSession, playback) - } val cred = joinResponse.value.credentials val currentOptions = oldSession.publisher.value?.currentOptions() @@ -528,31 +517,6 @@ internal class CallReconnector( } } - /** - * Moves a viewer from the SFU to HLS playback when a rejoin or migrate is answered with an - * HLS grant. The old session leaves the SFU, SFU monitoring stops, and participants (whose - * tracks came from the SFU) are cleared. An unplayable grant is terminal: the stable - * backend bucket would answer every retry the same way. - */ - private suspend fun switchToPlayback( - oldSession: RtcSession, - playback: PlaybackResponse, - ): ReconnectOutcome { - val playlistUrl = playback.hlsPlaylistUrlOrNull() - ?: return ReconnectOutcome.PreconditionNotMet( - "Unsupported playback type: ${playback.type}", - ) - logger.i { "[reconnect] Moving to HLS playback (reason=${playback.reason})" } - sessionMonitor().stopMonitoring() - oldSession.prepareRejoin("playback-hls") - sessionManager.setActiveSession(null) - state.clearParticipants() - sessionManager.playbackSession = createPlaybackSession() - state._playbackType.value = PlaybackType.Hls(playlistUrl) - state._connection.value = RealtimeConnection.Connected - return ReconnectOutcome.Success - } - suspend fun fastReconnect(reason: String = "unknown") { reconnect(WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_FAST, reason) } diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt deleted file mode 100644 index 6c57a275695..00000000000 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/HlsPlayback.kt +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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.core.call.components - -import io.getstream.android.video.generated.models.PlaybackResponse - -/** The playlist URL of an HLS grant, or `null` when the grant can't be played. */ -internal fun PlaybackResponse.hlsPlaylistUrlOrNull(): String? = - playlistUrl.takeIf { type == PlaybackResponse.Type.Hls && it.isNotBlank() } diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt index 05556fd3690..452d52914fc 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/SessionMonitor.kt @@ -85,16 +85,4 @@ internal class SessionMonitor( iceMonitor.start() connectivityMonitor.subscribe() } - - /** - * Stops everything [monitorSession] started, for a call that moves from the SFU to HLS - * playback. Network changes must no longer trigger SFU reconnects. - */ - fun stopMonitoring() { - cancelSfuObservers() - statsReporter.stop() - iceMonitor.stop() - connectivityMonitor.cancelLeaveTimeout() - connectivityMonitor.unsubscribe() - } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt index 0b839b716df..6b673084250 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallApiClientTest.kt @@ -33,6 +33,7 @@ import io.getstream.video.android.core.analytics.call.observer.model.JoinReason import io.getstream.video.android.core.model.RejectReason import io.getstream.video.android.core.model.SortField import io.getstream.video.android.core.recording.RecordingType +import io.mockk.MockKMatcherScope import io.mockk.coEvery import io.mockk.coVerify import io.mockk.mockk @@ -406,7 +407,9 @@ class CallApiClientTest { } @Test - fun `joinRequest declares HLS playback support on every join`() = runTest(testDispatcher) { + fun `joinRequest forwards playback capabilities only when the caller passes them`() = runTest( + testDispatcher, + ) { coEvery { clientImpl.joinCall( any(), any(), any(), any(), any(), any(), any(), any(), @@ -417,6 +420,7 @@ class CallApiClientTest { apiClient.joinRequest( location = "test-location", joinAnalyticsModel = JoinAnalyticsModel(0, JoinReason.FirstAttempt), + playbackCapabilities = listOf("hls"), ) apiClient.joinRequest( location = "test-location", @@ -424,25 +428,27 @@ class CallApiClientTest { joinAnalyticsModel = JoinAnalyticsModel(1, JoinReason.Migrate), ) - coVerify(exactly = 2) { - clientImpl.joinCall( - type = any(), - id = any(), - create = any(), - members = any(), - custom = any(), - settingsOverride = any(), - startsAt = any(), - team = any(), - ring = any(), - notify = any(), - location = any(), - migratingFrom = any(), - migratingFromList = any(), - hintHighScaleLivestreamPublisher = any(), - e2ee = any(), - playbackCapabilities = listOf("hls"), - ) - } + coVerify(exactly = 1) { joinCallWithCapabilities(listOf("hls")) } + coVerify(exactly = 1) { joinCallWithCapabilities(null) } } + + private suspend fun MockKMatcherScope.joinCallWithCapabilities(capabilities: List?) = + clientImpl.joinCall( + type = any(), + id = any(), + create = any(), + members = any(), + custom = any(), + settingsOverride = any(), + startsAt = any(), + team = any(), + ring = any(), + notify = any(), + location = any(), + migratingFrom = any(), + migratingFromList = any(), + hintHighScaleLivestreamPublisher = any(), + e2ee = any(), + playbackCapabilities = capabilities, + ) } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index 5a3daaba1ae..62cd51a22e6 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -162,7 +162,7 @@ class CallJoinCoordinatorTest { private fun stubJoinCall(result: io.getstream.result.Result) { coEvery { apiClient.joinRequest( - any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), ) } returns result } @@ -223,6 +223,31 @@ class CallJoinCoordinatorTest { hintHighScaleLivestreamPublisher = any(), joinAnalyticsModel = any(), e2ee = true, + playbackCapabilities = any(), + ) + } + } + + @Test + fun `first join declares HLS playback support`() = runTest(testDispatcher) { + stubJoinCall(Success(mockJoinResponse)) + coEvery { mockSession.connectInternal() } returns SfuConnectionResult.Success + + coordinator().join() + advanceUntilIdle() + + coVerify { + apiClient.joinRequest( + create = any(), + location = any(), + migratingFrom = any(), + migratingFromList = any(), + ring = any(), + notify = any(), + hintHighScaleLivestreamPublisher = any(), + joinAnalyticsModel = any(), + e2ee = any(), + playbackCapabilities = listOf("hls"), ) } } @@ -269,6 +294,7 @@ class CallJoinCoordinatorTest { any(), any(), any(), + any(), ) } } @@ -291,7 +317,7 @@ class CallJoinCoordinatorTest { verify(exactly = 0) { sessionManager.setActiveSession(null) } verify(exactly = 1) { callAnalytics.joinAnalytics.onJoinFunctionStart() } coVerify(exactly = 0) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } // The single outer gate must also stop a second RtcSession from being installed. verify(exactly = 0) { sessionManager.setActiveSession(mockSession) } @@ -309,7 +335,7 @@ class CallJoinCoordinatorTest { val joinAnalytics = callAnalytics.joinAnalytics verify(exactly = 1) { joinAnalytics.onJoinFunctionStart() } coVerify(exactly = 0) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } } @@ -328,7 +354,7 @@ class CallJoinCoordinatorTest { assertThat(result).isInstanceOf(Success::class.java) assertThat((result as Success).value).isSameInstanceAs(existing) coVerify(exactly = 0) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } verify { existing.sfuTracer.trace("join-already-joined", any()) } // The guard must not tear down the live session's SFU observers: nothing on this path @@ -388,7 +414,7 @@ class CallJoinCoordinatorTest { assertThat(results.map { (it as Success).value }.distinct()).hasSize(1) // One join request and one SFU connect for five callers. coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } coVerify(exactly = 1) { mockSession.connectInternal() } verify(exactly = 1) { sessionManager.setActiveSession(mockSession) } @@ -498,7 +524,7 @@ class CallJoinCoordinatorTest { verify(exactly = 1) { state.callJoinInterceptor = interceptor } coVerify(exactly = 0) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } guestGate.complete(Unit) @@ -523,7 +549,7 @@ class CallJoinCoordinatorTest { advanceUntilIdle() coVerify(exactly = 2) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } } @@ -556,7 +582,7 @@ class CallJoinCoordinatorTest { assertThat(secondResult).isInstanceOf(Success::class.java) coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } coVerify(exactly = 1) { mockSession.connectInternal() } } @@ -565,7 +591,7 @@ class CallJoinCoordinatorTest { fun `cancelling the last waiter does not abort the shared join`() = runTest(testDispatcher) { val joinRequestGate = CompletableDeferred() coEvery { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } coAnswers { joinRequestGate.await() Success(mockJoinResponse) @@ -597,7 +623,7 @@ class CallJoinCoordinatorTest { assertThat(retry).isInstanceOf(Success::class.java) assertThat((retry as Success).value).isSameInstanceAs(mockSession) coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } } @@ -607,7 +633,7 @@ class CallJoinCoordinatorTest { ) { val joinRequestGate = CompletableDeferred() coEvery { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } coAnswers { joinRequestGate.await() Success(mockJoinResponse) @@ -661,7 +687,7 @@ class CallJoinCoordinatorTest { assertThat(retry).isInstanceOf(Success::class.java) assertThat((retry as Success).value).isSameInstanceAs(mockSession) coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } } @@ -795,7 +821,7 @@ class CallJoinCoordinatorTest { assertThat((second as Success).value).isSameInstanceAs(playbackSession) coVerify(exactly = 1) { - apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any()) + apiClient.joinRequest(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt index d31510155d8..1fd533a1442 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallReconnectorTest.kt @@ -18,7 +18,6 @@ package io.getstream.video.android.core.call.components import com.google.common.truth.Truth.assertThat import io.getstream.android.video.generated.models.JoinCallResponse -import io.getstream.android.video.generated.models.PlaybackResponse import io.getstream.result.Result.Success import io.getstream.video.android.core.CallLeaveReason import io.getstream.video.android.core.CallState @@ -29,7 +28,6 @@ import io.getstream.video.android.core.analytics.call.CallAnalytics import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectionResult import io.getstream.video.android.core.call.connection.Publisher -import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery import io.mockk.coVerify import io.mockk.every @@ -50,10 +48,6 @@ import stream.video.sfu.models.WebsocketReconnectStrategy */ class CallReconnectorTest { - private companion object { - const val HLS_URL = "https://hls.example.com/playlist.m3u8" - } - private val testDispatcher = StandardTestDispatcher() private lateinit var clientImpl: StreamVideoClient @@ -65,8 +59,6 @@ class CallReconnectorTest { private lateinit var apiClient: CallApiClient private lateinit var callAnalytics: CallAnalytics private lateinit var sessionFactory: RtcSessionFactory - private lateinit var playbackTypeFlow: MutableStateFlow - private lateinit var playbackSession: RtcSession @Before fun setup() { @@ -91,9 +83,6 @@ class CallReconnectorTest { } returns true every { state.connection } returns connectionFlow every { state._connection } returns connectionFlow - playbackTypeFlow = MutableStateFlow(PlaybackType.WebRtc) - playbackSession = mockk(relaxed = true) - every { state._playbackType } returns playbackTypeFlow } private fun reconnector() = CallReconnector( @@ -105,7 +94,6 @@ class CallReconnectorTest { state = state, callAnalytics = callAnalytics, sessionMonitor = { sessionMonitor }, - createPlaybackSession = { playbackSession }, type = "default", id = "call-id", ) @@ -190,7 +178,7 @@ class CallReconnectorTest { fun `rejoin swaps in the new session and monitors it on success`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = sfuJoinResponse() + val joinResponse = mockk(relaxed = true) prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success @@ -208,7 +196,7 @@ class CallReconnectorTest { fun `rejoin failures are retried until the attempts are exhausted`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = sfuJoinResponse() + val joinResponse = mockk(relaxed = true) prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Failure( Exception("rejoin failed"), @@ -230,7 +218,7 @@ class CallReconnectorTest { ) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - val joinResponse = sfuJoinResponse() + val joinResponse = mockk(relaxed = true) prepareRejoinOrMigrate(oldSession, newSession, joinResponse) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success @@ -249,7 +237,7 @@ class CallReconnectorTest { fun `rejoin leaves the call once the rejoin budget is spent`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) + prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) repeat(RejoinRateLimiter.MAX_ATTEMPTS) { sessionManager.rejoinRateLimiter.tryRegister() } reconnector().reconnect( @@ -270,7 +258,7 @@ class CallReconnectorTest { fun `migrate counts against the rejoin budget`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) + prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) repeat(RejoinRateLimiter.MAX_ATTEMPTS) { sessionManager.rejoinRateLimiter.tryRegister() } reconnector().reconnect( @@ -307,7 +295,7 @@ class CallReconnectorTest { fun `a rejoin with budget left still issues a join request`() = runTest(testDispatcher) { val oldSession = mockk(relaxed = true) val newSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, newSession, sfuJoinResponse()) + prepareRejoinOrMigrate(oldSession, newSession, mockk(relaxed = true)) coEvery { newSession.connectInternal(any(), any()) } returns SfuConnectionResult.Success repeat(RejoinRateLimiter.MAX_ATTEMPTS - 1) { sessionManager.rejoinRateLimiter.tryRegister() @@ -323,62 +311,6 @@ class CallReconnectorTest { verify(exactly = 0) { lifecycle.leave(any()) } } - @Test - fun `rejoin answered with HLS moves the viewer to HLS playback`() = runTest(testDispatcher) { - val oldSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), hlsJoinResponse()) - - reconnector().reconnect( - WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, - "rejoin", - ) - advanceUntilIdle() - - assertHlsPlayback(oldSession) - } - - @Test - fun `migrate answered with HLS moves the viewer to HLS playback`() = runTest(testDispatcher) { - val oldSession = mockk(relaxed = true) - prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), hlsJoinResponse()) - - reconnector().reconnect( - WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_MIGRATE, - "migrate", - ) - advanceUntilIdle() - - assertHlsPlayback(oldSession) - verify(exactly = 0) { oldSession.enterMigration() } - } - - @Test - fun `an unplayable grant on rejoin gives up without leaving the SFU session`() = runTest( - testDispatcher, - ) { - val oldSession = mockk(relaxed = true) - val joinResponse = mockk(relaxed = true) - every { joinResponse.playback } returns PlaybackResponse( - playlistUrl = "", - reason = PlaybackResponse.Reason.Percentage, - type = PlaybackResponse.Type.Hls, - ) - prepareRejoinOrMigrate(oldSession, mockk(relaxed = true), joinResponse) - - reconnector().reconnect( - WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, - "rejoin", - ) - advanceUntilIdle() - - assertThat(connectionFlow.value) - .isInstanceOf(RealtimeConnection.ReconnectingFailed::class.java) - assertThat(sessionManager.playbackSession).isNull() - assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) - verify(exactly = 0) { sessionMonitor.stopMonitoring() } - coVerify(exactly = 0) { oldSession.prepareRejoin(any()) } - } - @Test fun `reconnect is skipped during HLS playback`() = runTest(testDispatcher) { sessionManager.playbackSession = mockk(relaxed = true) @@ -396,33 +328,6 @@ class CallReconnectorTest { assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Reconnecting) } - private fun sfuJoinResponse(): JoinCallResponse = mockk(relaxed = true).also { - every { it.playback } returns null - } - - private fun hlsJoinResponse(): JoinCallResponse = mockk(relaxed = true).also { - every { it.playback } returns PlaybackResponse( - playlistUrl = HLS_URL, - reason = PlaybackResponse.Reason.Percentage, - type = PlaybackResponse.Type.Hls, - ) - } - - private fun assertHlsPlayback(oldSession: RtcSession) { - assertThat(sessionManager.session.value).isNull() - assertThat(sessionManager.playbackSession).isSameInstanceAs(playbackSession) - assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) - assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) - verify { sessionMonitor.stopMonitoring() } - coVerify { oldSession.prepareRejoin(any()) } - verify { state.clearParticipants() } - verify( - exactly = 0, - ) { sessionFactory.create(any(), any(), any(), any(), any(), any(), any()) } - verify(exactly = 0) { sessionMonitor.monitorSession(any()) } - verify(exactly = 0) { lifecycle.leave(any()) } - } - /** * Wires up the shared happy-path state a rejoin/migrate needs: a resolvable location, * an existing (old) session, a stubbed join request and an injected new session. diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt index 77299de293a..abe376d6748 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/rtc/JoinRecoverableFailureTest.kt @@ -107,7 +107,7 @@ class JoinRecoverableFailureTest { coEvery { clientImpl.getCachedLocation() } returns Success("test-location") coEvery { apiClient.joinRequest( - any(), any(), any(), any(), any(), any(), any(), any(), any(), + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), ) } returns Success(mockJoinResponse) } From a7b72d4499a5c1279555c26ed4f052524f4e6ec2 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Wed, 18 Nov 2026 18:08:30 +0530 Subject: [PATCH 07/10] fix(core): discard an HLS grant that arrives after leave started --- .../call/components/CallJoinCoordinator.kt | 33 ++++++++--- .../call/components/CallLifecycleManager.kt | 26 +++++++- .../android/core/utils/AtomicUnitCall.kt | 3 + .../components/CallJoinCoordinatorTest.kt | 59 +++++++++++++++++++ .../components/CallLifecycleManagerTest.kt | 14 +++++ .../android/core/utils/AtomicUnitCallTest.kt | 14 +++++ 6 files changed, 139 insertions(+), 10 deletions(-) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index a9f4c5ee11f..973a6123022 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -300,6 +300,12 @@ internal class CallJoinCoordinator( return result } if (result is Failure) { + // Leave wins over the join that was in flight. Setting Failed here would + // overwrite the Disconnected state leave just published. + if (lifecycle.hasLeaveStarted()) { + logger.i { "[join] Leave started during join — discarding the result" } + return result + } sessionManager.setActiveSession(null) logger.e { "Join failed with error $result" } if (isPermanentError(result.value)) { @@ -498,14 +504,25 @@ internal class CallJoinCoordinator( } return Failure(Error.GenericError("Unsupported playback type: ${playback.type}")) } - logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } - val session = createPlaybackSession() - sessionManager.playbackSession = session - state._playbackType.value = PlaybackType.Hls(playlistUrl) - state._connection.value = RealtimeConnection.Joined(session) - callRegistry.markActive() - state._connection.value = RealtimeConnection.Connected - return Success(session) + // The grant is installed below without suspending, so a leave that started while + // joinRequest was in flight cannot cancel it. The lock keeps that leave from landing + // between the check and the writes. + return lifecycle.withLeaveLock { + if (lifecycle.hasLeaveStarted()) { + logger.w { "[joinPlayback] Leave already started — discarding HLS grant" } + return@withLeaveLock Failure( + Error.GenericError("Call left before HLS playback started"), + ) + } + logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } + val session = createPlaybackSession() + sessionManager.playbackSession = session + state._playbackType.value = PlaybackType.Hls(playlistUrl) + state._connection.value = RealtimeConnection.Joined(session) + callRegistry.markActive() + state._connection.value = RealtimeConnection.Connected + Success(session) + } } private suspend fun completeJoinAfterSessionInstall( diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt index 14a54e5237a..4aed848214b 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt @@ -62,9 +62,17 @@ internal class CallLifecycleManager( private val cid = "$type:$id" - // Atomic controls + // Atomic controls. Replaced by [resetLeaveGuard], read from the join path. + @Volatile private var atomicLeave = AtomicUnitCall() + /** + * Orders a leave against an HLS grant being installed. [AtomicUnitCall] flips its flag before + * the leave body runs, so the body takes this lock first: it then either waits for an install + * already in progress and cleans it up, or runs before the install, which then sees the leave. + */ + private val leaveLock = Any() + /** Call has been left and the object is cleaned up and destroyed. */ var isDestroyed = false @@ -76,9 +84,21 @@ internal class CallLifecycleManager( /** Resets the leave guard so a fresh join can run after a previous leave. */ fun resetLeaveGuard() { - atomicLeave = AtomicUnitCall() + synchronized(leaveLock) { atomicLeave = AtomicUnitCall() } } + /** + * True from the start of [leave] until the next [resetLeaveGuard]. [isDestroyed] cannot + * stand in for it: that flag is never cleared, so a later join would look left forever. + */ + fun hasLeaveStarted(): Boolean = atomicLeave.hasBeenCalled + + /** + * Runs [block] without interleaving it with [leave]. Playback install uses this: it does not + * suspend, so cancelling the call scope cannot stop it from running after leave has started. + */ + fun withLeaveLock(block: () -> T): T = synchronized(leaveLock, block) + fun leave(reason: CallLeaveReason) { logger.d { "[leave] #ringing; call_cid:$cid" } internalLeave(reason) @@ -90,6 +110,8 @@ internal class CallLifecycleManager( } private fun internalLeave(reason: CallLeaveReason) = atomicLeave { + // Barrier only, see [leaveLock]: waits out a grant that is mid-install. + synchronized(leaveLock) { } stopConnectionMonitors() callAnalytics.stopObservers() sessionMonitor().cancelSfuObservers() diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/utils/AtomicUnitCall.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/utils/AtomicUnitCall.kt index 6c08f610469..828760f6b8d 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/utils/AtomicUnitCall.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/utils/AtomicUnitCall.kt @@ -24,6 +24,9 @@ import java.util.concurrent.atomic.AtomicBoolean internal class AtomicUnitCall { private var called = AtomicBoolean(false) + /** True once [invoke] has been called, even while its block is still running. */ + val hasBeenCalled: Boolean get() = called.get() + /** * Executes the given block of code only once, no matter how many consecutive calls are made. */ diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index 62cd51a22e6..8714e4b6427 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -37,6 +37,7 @@ import io.getstream.video.android.core.base.DispatcherRule import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectFailureCause import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.call.scope.ScopeProvider import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery import io.mockk.coVerify @@ -133,6 +134,10 @@ class CallJoinCoordinatorTest { every { sessionManager.playbackSession = any() } answers { heldPlaybackSession = firstArg() } + every { lifecycle.hasLeaveStarted() } returns false + every { lifecycle.withLeaveLock(any<() -> Any?>()) } answers { + firstArg<() -> Any?>().invoke() + } } @After @@ -844,4 +849,58 @@ class CallJoinCoordinatorTest { verify(exactly = 0) { callRegistry.markActive() } coVerify(exactly = 0) { mockSession.connectInternal() } } + + @Test + fun `an HLS grant that arrives after leave does not activate the call`() = runTest( + testDispatcher, + ) { + val sessions = CallSessionManager() + sessionManager = sessions + lifecycle = CallLifecycleManager( + clientImpl = clientImpl, + sessionManager = sessions, + scopeProvider = mockk(relaxed = true), + callRegistry = callRegistry, + shutDownJobs = {}, + state = state, + callAnalytics = callAnalytics, + statsReporter = mockk(relaxed = true), + media = media, + iceMonitor = mockk(relaxed = true), + sessionMonitor = { sessionMonitor }, + connectivityMonitor = { mockk(relaxed = true) }, + type = "default", + id = "test-call", + ) + every { clientImpl.scope } returns testScope + stubHlsPlayback() + var requests = 0 + coEvery { + apiClient.joinRequest( + any(), any(), any(), any(), any(), any(), any(), any(), any(), any(), + ) + } coAnswers { + requests += 1 + if (requests == 1) lifecycle.leave("user") + Success(mockJoinResponse) + } + + val coordinator = coordinator() + val first = coordinator.join() + + assertThat(first).isInstanceOf(Failure::class.java) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + assertThat(sessions.playbackSession).isNull() + assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Disconnected) + verify(exactly = 0) { callRegistry.markActive() } + + advanceUntilIdle() + val second = coordinator.join() + advanceUntilIdle() + + assertThat((second as Success).value).isSameInstanceAs(playbackSession) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) + assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) + verify(exactly = 1) { callRegistry.markActive() } + } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt index 3bc3877e87e..5126a46cb2d 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt @@ -87,4 +87,18 @@ class CallLifecycleManagerTest { verify { playbackSession.cleanup() } coVerify(exactly = 0) { playbackSession.sendLeaveEvent(any()) } } + + @Test + fun `leave marks the call left until the next join resets the guard`() = runTest( + testDispatcher, + ) { + val manager = manager() + + assertThat(manager.hasLeaveStarted()).isFalse() + manager.leave("user") + assertThat(manager.hasLeaveStarted()).isTrue() + + manager.resetLeaveGuard() + assertThat(manager.hasLeaveStarted()).isFalse() + } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/utils/AtomicUnitCallTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/utils/AtomicUnitCallTest.kt index 9f4392337da..3f536ad3b6b 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/utils/AtomicUnitCallTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/utils/AtomicUnitCallTest.kt @@ -16,6 +16,8 @@ package io.getstream.video.android.core.utils +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test @@ -43,4 +45,16 @@ class AtomicUnitCallTest { } assert(executed == 1) } + + @Test + fun `hasBeenCalled is true while the block is still running`() { + val atomicUnitCall = AtomicUnitCall() + assertFalse(atomicUnitCall.hasBeenCalled) + + var duringBlock: Boolean? = null + atomicUnitCall { duringBlock = atomicUnitCall.hasBeenCalled } + + assertEquals(true, duringBlock) + assertTrue(atomicUnitCall.hasBeenCalled) + } } From 225b7fdb078bc7fdba093d3b6eab0986ebf67c2c Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Thu, 19 Nov 2026 19:49:18 +0530 Subject: [PATCH 08/10] feat(core): own the HLS player per call behind an opt-in factory Media3 adds about 2 MB, and few apps need HLS, so the player moves to a separate artifact. Core keeps the contract: HlsPlayer and HlsPlayerFactory, registered through StreamVideoBuilder.hlsPlayerFactory. The first join only declares HLS support when a factory is registered, so an app without one is never served HLS. An HLS join creates the call's player next to the playback session; leave releases it. A player that cannot be created fails the join and installs nothing. --- .../api/stream-video-android-core.api | 16 +++- .../io/getstream/video/android/core/Call.kt | 15 ++++ .../video/android/core/StreamVideoBuilder.kt | 7 ++ .../video/android/core/StreamVideoClient.kt | 2 + .../call/components/CallJoinCoordinator.kt | 29 +++++-- .../call/components/CallLifecycleManager.kt | 5 +- .../call/components/CallSessionManager.kt | 4 + .../video/android/core/call/hls/HlsPlayer.kt | 77 +++++++++++++++++++ .../components/CallJoinCoordinatorTest.kt | 69 ++++++++++++++++- .../components/CallLifecycleManagerTest.kt | 7 +- 10 files changed, 220 insertions(+), 11 deletions(-) create mode 100644 stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt diff --git a/stream-video-android-core/api/stream-video-android-core.api b/stream-video-android-core/api/stream-video-android-core.api index c98734fe9c2..b578f52c023 100644 --- a/stream-video-android-core/api/stream-video-android-core.api +++ b/stream-video-android-core/api/stream-video-android-core.api @@ -13176,7 +13176,8 @@ public final class io/getstream/video/android/core/StreamVideoBuilder { public fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;Z)V public fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;ZZ)V public fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;ZZLio/getstream/video/android/core/ringing/RingStatePollingConfig;)V - public synthetic fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;ZZLio/getstream/video/android/core/ringing/RingStatePollingConfig;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;ZZLio/getstream/video/android/core/ringing/RingStatePollingConfig;Lio/getstream/video/android/core/call/hls/HlsPlayerFactory;)V + public synthetic fun (Landroid/content/Context;Ljava/lang/String;Lio/getstream/video/android/core/GEO;Lio/getstream/video/android/model/User;Ljava/lang/String;Lkotlin/jvm/functions/Function2;Lio/getstream/video/android/core/socket/common/token/TokenProvider;Lio/getstream/video/android/core/logging/LoggingLevel;Lio/getstream/video/android/core/notifications/NotificationConfig;Lkotlin/jvm/functions/Function1;JZLjava/lang/String;ZLio/getstream/video/android/core/notifications/internal/service/CallServiceConfig;Lio/getstream/video/android/core/notifications/internal/service/CallServiceConfigRegistry;Ljava/lang/String;Lio/getstream/video/android/core/sounds/Sounds;Lio/getstream/video/android/core/sounds/RingingCallVibrationConfig;ZLio/getstream/video/android/core/permission/android/StreamPermissionCheck;ILjava/lang/String;Lorg/webrtc/ManagedAudioProcessingFactory;JZZZLio/getstream/video/android/core/notifications/internal/telecom/TelecomConfig;ZZLio/getstream/video/android/core/ringing/RingStatePollingConfig;Lio/getstream/video/android/core/call/hls/HlsPlayerFactory;IILkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun build ()Lio/getstream/video/android/core/StreamVideo; } @@ -13461,6 +13462,19 @@ public final class io/getstream/video/android/core/call/connection/StreamPeerCon public final fun toggleAudioProcessing ()Z } +public abstract interface class io/getstream/video/android/core/call/hls/HlsPlayer { + public abstract fun attach (Landroid/view/SurfaceView;)V + public abstract fun detach (Landroid/view/SurfaceView;)V + public abstract fun getPlaylistUrl ()Ljava/lang/String; + public abstract fun getVideoAspectRatio ()Lkotlinx/coroutines/flow/StateFlow; + public abstract fun release ()V + public abstract fun setAudioEnabled (Z)V +} + +public abstract interface class io/getstream/video/android/core/call/hls/HlsPlayerFactory { + public abstract fun create (Landroid/content/Context;Ljava/lang/String;)Lio/getstream/video/android/core/call/hls/HlsPlayer; +} + public final class io/getstream/video/android/core/call/signal/socket/RTCEventMapper { public static final field INSTANCE Lio/getstream/video/android/core/call/signal/socket/RTCEventMapper; public final fun mapEvent (Lstream/video/sfu/event/SfuEvent;)Lio/getstream/video/android/core/events/SfuDataEvent; diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt index 31b4fd43f95..888d069508f 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt @@ -74,6 +74,7 @@ import io.getstream.video.android.core.call.components.NoiseCancellationPolicy import io.getstream.video.android.core.call.components.RtcSessionFactory import io.getstream.video.android.core.call.components.SessionMonitor import io.getstream.video.android.core.call.connection.StreamPeerConnectionFactory +import io.getstream.video.android.core.call.hls.HlsPlayer import io.getstream.video.android.core.call.scope.ScopeProvider import io.getstream.video.android.core.call.scope.ScopeProviderImpl import io.getstream.video.android.core.call.video.VideoFilter @@ -170,6 +171,14 @@ public class Call( // is also internal, so it cannot appear in a public signature at all. internal val session: StateFlow get() = sessionManager.session + /** + * Plays the call's HLS broadcast. Set while [CallState.playbackType] is + * [io.getstream.video.android.core.model.PlaybackType.Hls], null otherwise. Read by the HLS + * renderer in `stream-video-android-hls`. + */ + @InternalStreamVideoApi + public val hlsPlayer: StateFlow get() = sessionManager.hlsPlayer + var sessionId: String get() = sessionManager.sessionId set(value) { @@ -553,6 +562,12 @@ public class Call( }, createPlaybackSession = playbackSessionFactory, consumeJoinSource = { consumeJoinSource() }, + createHlsPlayer = clientImpl.hlsPlayerFactory?.let { factory -> + { + playlistUrl -> + factory.create(clientImpl.context, playlistUrl) + } + }, ) internal var reconnectDeadlineMillis: Int diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt index 2c3a23a2555..1dcc301d04d 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoBuilder.kt @@ -24,6 +24,7 @@ import io.getstream.log.AndroidStreamLogger import io.getstream.log.StreamLog import io.getstream.log.streamLog import io.getstream.video.android.core.call.CallType +import io.getstream.video.android.core.call.hls.HlsPlayerFactory import io.getstream.video.android.core.internal.InternalStreamVideoApi import io.getstream.video.android.core.internal.module.CoordinatorConnectionModule import io.getstream.video.android.core.logging.LoggingLevel @@ -112,6 +113,10 @@ import java.net.ConnectException * @property rejectCallWhenBusy Automatically rejects incoming calls when the user is already in another call. * When enabled, the SDK suppresses incoming call notifications. * CallRingEvent will not be propagated if there is an active or ongoing ringing call. + * @property hlsPlayerFactory Enables HLS playback for viewers. When set, a join tells the backend + * that this client can play HLS, and a call served that way plays through the player it + * builds. Null by default: every call joins over WebRTC. The `stream-video-android-hls` + * artifact provides `ExoHlsPlayerFactory`. Beta. * * @see build * @see ClientState.connection @@ -170,6 +175,7 @@ public class StreamVideoBuilder @JvmOverloads constructor( private val connectOnInit: Boolean = true, private val rejectCallWhenBusy: Boolean = false, private val ringStatePolling: RingStatePollingConfig? = RingStatePollingConfig(), + private val hlsPlayerFactory: HlsPlayerFactory? = null, ) { private val context: Context = context.applicationContext private val scope = UserScope(ClientScope()) @@ -330,6 +336,7 @@ public class StreamVideoBuilder @JvmOverloads constructor( }, tokenRepository = tokenRepository, rejectCallWhenBusy = rejectCallWhenBusy, + hlsPlayerFactory = hlsPlayerFactory, ) if (user.type == UserType.Guest) { diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt index 83fabc187ed..714fde5f867 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/StreamVideoClient.kt @@ -87,6 +87,7 @@ import io.getstream.result.Result.Success import io.getstream.video.android.core.analytics.AnalyticsModule import io.getstream.video.android.core.audio.AudioExecutionContext import io.getstream.video.android.core.call.CallBusyHandler +import io.getstream.video.android.core.call.hls.HlsPlayerFactory import io.getstream.video.android.core.errors.VideoErrorCode import io.getstream.video.android.core.events.VideoEventListener import io.getstream.video.android.core.filter.Filters @@ -201,6 +202,7 @@ internal class StreamVideoClient internal constructor( internal val enableStereoForSubscriber: Boolean = true, internal val telecomConfig: TelecomConfig? = null, internal val rejectCallWhenBusy: Boolean = false, + internal val hlsPlayerFactory: HlsPlayerFactory? = null, internal val analytics: AnalyticsModule = AnalyticsModule.getDefault( coordinatorConnectionModule.api, scope, diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index 973a6123022..eaf4f691aa3 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -39,6 +39,7 @@ import io.getstream.video.android.core.analytics.reporting.model.AnalyticsCallAb import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectFailureCause import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.call.hls.HlsPlayer import io.getstream.video.android.core.model.PlaybackType import io.getstream.video.android.core.model.toIceServer import io.getstream.video.android.core.ringing.RingJoinSource @@ -76,6 +77,9 @@ internal class CallJoinCoordinator( private val createPlaybackSession: () -> RtcSession, // Reads and clears the source of a ring-driven join; see Call.consumeJoinSource. private val consumeJoinSource: () -> RingJoinSource? = { null }, + // Builds the call's HLS player from a playlist URL. Null when the app registered no + // HlsPlayerFactory: the join then never asks for HLS. + private val createHlsPlayer: ((playlistUrl: String) -> HlsPlayer)? = null, ) { private companion object { const val JOIN_FLIGHT_KEY = "join" @@ -439,7 +443,11 @@ internal class CallJoinCoordinator( // The coordinator rejects a join whose flag disagrees with the call. This is // the first join; rejoin / migrate omit it and CallApiClient reads the manager. e2ee = state.e2eeEnabled.value, - playbackCapabilities = listOf(PlaybackResponse.Type.Hls.value), + playbackCapabilities = createHlsPlayer?.let { + listOf( + PlaybackResponse.Type.Hls.value, + ) + }, ) if (result !is Success) { @@ -493,14 +501,15 @@ internal class CallJoinCoordinator( * Completes a join the backend served as HLS playback. No SFU connection is made, so the * session monitor (SFU events, network-driven reconnects) and the stats reporter never start. * The returned session is held as [CallSessionManager.playbackSession], never as the active - * session. + * session, and the call's [HlsPlayer] as [CallSessionManager.hlsPlayer]. */ private fun joinPlayback(playback: PlaybackResponse): Result { val playlistUrl = playback.playlistUrl - if (playback.type != PlaybackResponse.Type.Hls || playlistUrl.isBlank()) { + val createPlayer = createHlsPlayer + if (playback.type != PlaybackResponse.Type.Hls || playlistUrl.isBlank() || createPlayer == null) { logger.e { "[joinInternal] Unsupported playback (type=${playback.type}, " + - "hasUrl=${playback.playlistUrl.isNotBlank()})" + "hasUrl=${playback.playlistUrl.isNotBlank()}, hasPlayer=${createPlayer != null})" } return Failure(Error.GenericError("Unsupported playback type: ${playback.type}")) } @@ -514,9 +523,19 @@ internal class CallJoinCoordinator( Error.GenericError("Call left before HLS playback started"), ) } - logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } val session = createPlaybackSession() + val player = try { + createPlayer(playlistUrl) + } catch (e: Exception) { + logger.e(e) { "[joinPlayback] Could not create the HLS player" } + session.cleanup() + return@withLeaveLock Failure( + Error.ThrowableError("Could not create the HLS player", e), + ) + } + logger.i { "[joinInternal] Joined with HLS playback (reason=${playback.reason})" } sessionManager.playbackSession = session + sessionManager.hlsPlayer.value = player state._playbackType.value = PlaybackType.Hls(playlistUrl) state._connection.value = RealtimeConnection.Joined(session) callRegistry.markActive() diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt index 4aed848214b..3e7f80c51a2 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManager.kt @@ -174,9 +174,12 @@ internal class CallLifecycleManager( } private fun clearPlayback() { + val player = sessionManager.hlsPlayer.value + sessionManager.hlsPlayer.value = null + state._playbackType.value = PlaybackType.WebRtc sessionManager.playbackSession?.cleanup() sessionManager.playbackSession = null - state._playbackType.value = PlaybackType.WebRtc + player?.release() } /** Stops the ICE and connectivity monitors. */ diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt index 122bc4bc10a..932f7dd3cdf 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallSessionManager.kt @@ -19,6 +19,7 @@ package io.getstream.video.android.core.call.components import io.getstream.log.taggedLogger import io.getstream.video.android.core.call.EmptyRtcSession import io.getstream.video.android.core.call.RtcSession +import io.getstream.video.android.core.call.hls.HlsPlayer import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow @@ -66,6 +67,9 @@ internal class CallSessionManager() { @Volatile var playbackSession: RtcSession? = null + /** The call's HLS player. Set and cleared together with [playbackSession]. */ + val hlsPlayer = MutableStateFlow(null) + var sessionId = UUID.randomUUID().toString() val unifiedSessionId = UUID.randomUUID().toString() diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt new file mode 100644 index 00000000000..e5d7ca35a67 --- /dev/null +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt @@ -0,0 +1,77 @@ +/* + * 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.core.call.hls + +import android.content.Context +import android.view.SurfaceView +import io.getstream.video.android.core.Call +import io.getstream.video.android.core.StreamVideoBuilder +import kotlinx.coroutines.flow.StateFlow + +/** + * Plays the HLS broadcast of one [Call]. + * + * The call owns its player: one is created when the backend serves the call as HLS, and it is + * released when the call is left. A renderer only shows and hides it with [attach] and [detach], + * so a renderer that is recreated, for example on rotation, resumes the same player. + * + * The player draws to one surface at a time: showing the same call in two renderers at once is + * not supported. + * + * Beta: HLS delivery is enabled per call type by Stream and may change. + */ +public interface HlsPlayer { + + public val playlistUrl: String + + /** + * Display width divided by height of the decoded video, or null until the first frame size is + * known. A [SurfaceView] stretches frames to its own bounds, so the UI sizes the surface with it. + */ + public val videoAspectRatio: StateFlow + + /** Draws the broadcast on [surfaceView] and plays it. Main thread only. */ + public fun attach(surfaceView: SurfaceView) + + /** + * Stops drawing on [surfaceView] and pauses. The player keeps its buffer, so a later [attach] + * resumes without loading the stream again. Main thread only. + */ + public fun detach(surfaceView: SurfaceView) + + /** Mutes the broadcast audio when [enabled] is false. Playback continues either way. */ + public fun setAudioEnabled(enabled: Boolean) + + /** Stops playback for good. Called by the SDK when the call is left, from any thread. */ + public fun release() +} + +/** + * Builds the [HlsPlayer] for a call the backend serves as HLS. Pass one to + * [StreamVideoBuilder] to enable HLS playback: without it the SDK never asks the backend for HLS, + * and every call joins over WebRTC. + * + * The `stream-video-android-hls` artifact provides the ExoPlayer implementation. + */ +public fun interface HlsPlayerFactory { + + /** + * Called from a background thread while the call is joining, so it must return quickly and + * must not touch a thread-bound player yet. + */ + public fun create(context: Context, playlistUrl: String): HlsPlayer +} diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index 8714e4b6427..eb79890393d 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -37,6 +37,7 @@ import io.getstream.video.android.core.base.DispatcherRule import io.getstream.video.android.core.call.RtcSession import io.getstream.video.android.core.call.SfuConnectFailureCause import io.getstream.video.android.core.call.SfuConnectionResult +import io.getstream.video.android.core.call.hls.HlsPlayer import io.getstream.video.android.core.call.scope.ScopeProvider import io.getstream.video.android.core.model.PlaybackType import io.mockk.coEvery @@ -98,6 +99,8 @@ class CallJoinCoordinatorTest { private lateinit var playbackTypeFlow: MutableStateFlow private lateinit var playbackSession: RtcSession private var heldPlaybackSession: RtcSession? = null + private lateinit var hlsPlayer: HlsPlayer + private lateinit var hlsPlayerFlow: MutableStateFlow @Before fun setup() { @@ -134,6 +137,9 @@ class CallJoinCoordinatorTest { every { sessionManager.playbackSession = any() } answers { heldPlaybackSession = firstArg() } + hlsPlayer = mockk(relaxed = true) + hlsPlayerFlow = MutableStateFlow(null) + every { sessionManager.hlsPlayer } returns hlsPlayerFlow every { lifecycle.hasLeaveStarted() } returns false every { lifecycle.withLeaveLock(any<() -> Any?>()) } answers { firstArg<() -> Any?>().invoke() @@ -145,7 +151,9 @@ class CallJoinCoordinatorTest { unmockkAll() } - private fun coordinator() = CallJoinCoordinator( + private fun coordinator( + createHlsPlayer: ((String) -> HlsPlayer)? = { hlsPlayer }, + ) = CallJoinCoordinator( clientImpl = clientImpl, state = state, callAnalytics = callAnalytics, @@ -162,6 +170,7 @@ class CallJoinCoordinatorTest { callRegistry = callRegistry, hasRequiredPermissions = { true }, createPlaybackSession = { playbackSession }, + createHlsPlayer = createHlsPlayer, ) private fun stubJoinCall(result: io.getstream.result.Result) { @@ -234,13 +243,32 @@ class CallJoinCoordinatorTest { } @Test - fun `first join declares HLS playback support`() = runTest(testDispatcher) { + fun `first join declares HLS playback when a player factory is registered`() = runTest( + testDispatcher, + ) { stubJoinCall(Success(mockJoinResponse)) coEvery { mockSession.connectInternal() } returns SfuConnectionResult.Success coordinator().join() advanceUntilIdle() + verifyPlaybackCapabilities(listOf("hls")) + } + + @Test + fun `first join does not declare HLS playback without a player factory`() = runTest( + testDispatcher, + ) { + stubJoinCall(Success(mockJoinResponse)) + coEvery { mockSession.connectInternal() } returns SfuConnectionResult.Success + + coordinator(createHlsPlayer = null).join() + advanceUntilIdle() + + verifyPlaybackCapabilities(null) + } + + private fun verifyPlaybackCapabilities(expected: List?) { coVerify { apiClient.joinRequest( create = any(), @@ -252,7 +280,7 @@ class CallJoinCoordinatorTest { hintHighScaleLivestreamPublisher = any(), joinAnalyticsModel = any(), e2ee = any(), - playbackCapabilities = listOf("hls"), + playbackCapabilities = expected, ) } } @@ -807,6 +835,7 @@ class CallJoinCoordinatorTest { assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) assertThat(heldPlaybackSession).isSameInstanceAs(playbackSession) + assertThat(hlsPlayerFlow.value).isSameInstanceAs(hlsPlayer) assertThat(sessionFlow.value).isNull() verify { callRegistry.markActive() } coVerify(exactly = 0) { mockSession.connectInternal() } @@ -846,10 +875,42 @@ class CallJoinCoordinatorTest { assertThat(connectionFlow.value).isInstanceOf(RealtimeConnection.Failed::class.java) assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) assertThat(heldPlaybackSession).isNull() + assertThat(hlsPlayerFlow.value).isNull() verify(exactly = 0) { callRegistry.markActive() } coVerify(exactly = 0) { mockSession.connectInternal() } } + @Test + fun `an HLS grant without a player factory fails the join`() = runTest(testDispatcher) { + stubHlsPlayback() + + val result = coordinator(createHlsPlayer = null).join() + advanceUntilIdle() + + assertThat(result).isInstanceOf(Failure::class.java) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + assertThat(heldPlaybackSession).isNull() + verify(exactly = 0) { callRegistry.markActive() } + } + + @Test + fun `a player that cannot be created fails the HLS join without installing it`() = runTest( + testDispatcher, + ) { + stubHlsPlayback() + + val result = coordinator(createHlsPlayer = { error("no player") }).join() + advanceUntilIdle() + + assertThat(result).isInstanceOf(Failure::class.java) + assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) + assertThat(heldPlaybackSession).isNull() + assertThat(hlsPlayerFlow.value).isNull() + assertThat(connectionFlow.value).isInstanceOf(RealtimeConnection.Failed::class.java) + verify { playbackSession.cleanup() } + verify(exactly = 0) { callRegistry.markActive() } + } + @Test fun `an HLS grant that arrives after leave does not activate the call`() = runTest( testDispatcher, @@ -891,6 +952,7 @@ class CallJoinCoordinatorTest { assertThat(first).isInstanceOf(Failure::class.java) assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) assertThat(sessions.playbackSession).isNull() + assertThat(sessions.hlsPlayer.value).isNull() assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Disconnected) verify(exactly = 0) { callRegistry.markActive() } @@ -900,6 +962,7 @@ class CallJoinCoordinatorTest { assertThat((second as Success).value).isSameInstanceAs(playbackSession) assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.Hls(HLS_URL)) + assertThat(sessions.hlsPlayer.value).isSameInstanceAs(hlsPlayer) assertThat(connectionFlow.value).isEqualTo(RealtimeConnection.Connected) verify(exactly = 1) { callRegistry.markActive() } } diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt index 5126a46cb2d..1241d8b7a73 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallLifecycleManagerTest.kt @@ -21,6 +21,7 @@ import io.getstream.video.android.core.CallState import io.getstream.video.android.core.RealtimeConnection import io.getstream.video.android.core.StreamVideoClient import io.getstream.video.android.core.call.RtcSession +import io.getstream.video.android.core.call.hls.HlsPlayer import io.getstream.video.android.core.model.PlaybackType import io.mockk.coVerify import io.mockk.every @@ -74,17 +75,21 @@ class CallLifecycleManagerTest { } @Test - fun `leave after HLS playback cleans up the playback session and resets the playback type`() = + fun `leave after HLS playback releases the player and resets the playback type`() = runTest(testDispatcher) { val playbackSession = mockk(relaxed = true) + val player = mockk(relaxed = true) sessionManager.playbackSession = playbackSession + sessionManager.hlsPlayer.value = player manager().leave("user") advanceUntilIdle() assertThat(sessionManager.playbackSession).isNull() + assertThat(sessionManager.hlsPlayer.value).isNull() assertThat(playbackTypeFlow.value).isEqualTo(PlaybackType.WebRtc) verify { playbackSession.cleanup() } + verify(exactly = 1) { player.release() } coVerify(exactly = 0) { playbackSession.sendLeaveEvent(any()) } } From cd869013bc2a0ce43599a0126bdac961843d6196 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Thu, 19 Nov 2026 21:04:27 +0530 Subject: [PATCH 09/10] docs(core): HLS playback continues while no renderer is shown --- .../video/android/core/call/hls/HlsPlayer.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt index e5d7ca35a67..984886aa200 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt @@ -26,8 +26,9 @@ import kotlinx.coroutines.flow.StateFlow * Plays the HLS broadcast of one [Call]. * * The call owns its player: one is created when the backend serves the call as HLS, and it is - * released when the call is left. A renderer only shows and hides it with [attach] and [detach], - * so a renderer that is recreated, for example on rotation, resumes the same player. + * released when the call is left. A renderer only shows and hides the video with [attach] and + * [detach]. Playback, including audio, continues while no renderer is shown, the same as a WebRTC + * call, and a renderer that is recreated, for example on rotation, shows the same player. * * The player draws to one surface at a time: showing the same call in two renderers at once is * not supported. @@ -44,12 +45,12 @@ public interface HlsPlayer { */ public val videoAspectRatio: StateFlow - /** Draws the broadcast on [surfaceView] and plays it. Main thread only. */ + /** Draws the broadcast on [surfaceView], starting playback if needed. Main thread only. */ public fun attach(surfaceView: SurfaceView) /** - * Stops drawing on [surfaceView] and pauses. The player keeps its buffer, so a later [attach] - * resumes without loading the stream again. Main thread only. + * Stops drawing on [surfaceView]. Playback continues until [release]. Ignored if another + * surface was attached since. Main thread only. */ public fun detach(surfaceView: SurfaceView) From 5df65fc9b6d60420f309b4cfe750bc029adffdd2 Mon Sep 17 00:00:00 2001 From: pratimmallick Date: Fri, 20 Nov 2026 15:20:18 +0530 Subject: [PATCH 10/10] fix(core): read the HLS player factory at join, not when building a Call --- .../kotlin/io/getstream/video/android/core/Call.kt | 10 ++++++---- .../core/call/components/CallJoinCoordinator.kt | 10 +++++----- .../getstream/video/android/core/call/hls/HlsPlayer.kt | 6 +++--- .../core/call/components/CallJoinCoordinatorTest.kt | 2 +- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt index 888d069508f..d556972b0fd 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/Call.kt @@ -562,10 +562,12 @@ public class Call( }, createPlaybackSession = playbackSessionFactory, consumeJoinSource = { consumeJoinSource() }, - createHlsPlayer = clientImpl.hlsPlayerFactory?.let { factory -> - { - playlistUrl -> - factory.create(clientImpl.context, playlistUrl) + hlsPlayerFactory = { + clientImpl.hlsPlayerFactory?.let { factory -> + { + playlistUrl -> + factory.create(clientImpl.context, playlistUrl) + } } }, ) diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt index eaf4f691aa3..4a15b7c4347 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinator.kt @@ -77,9 +77,9 @@ internal class CallJoinCoordinator( private val createPlaybackSession: () -> RtcSession, // Reads and clears the source of a ring-driven join; see Call.consumeJoinSource. private val consumeJoinSource: () -> RingJoinSource? = { null }, - // Builds the call's HLS player from a playlist URL. Null when the app registered no - // HlsPlayerFactory: the join then never asks for HLS. - private val createHlsPlayer: ((playlistUrl: String) -> HlsPlayer)? = null, + // Returns the builder of the call's HLS player from a playlist URL, read at join time. Null + // when the app registered no HlsPlayerFactory: the join then never asks for HLS. + private val hlsPlayerFactory: () -> ((playlistUrl: String) -> HlsPlayer)? = { null }, ) { private companion object { const val JOIN_FLIGHT_KEY = "join" @@ -443,7 +443,7 @@ internal class CallJoinCoordinator( // The coordinator rejects a join whose flag disagrees with the call. This is // the first join; rejoin / migrate omit it and CallApiClient reads the manager. e2ee = state.e2eeEnabled.value, - playbackCapabilities = createHlsPlayer?.let { + playbackCapabilities = hlsPlayerFactory()?.let { listOf( PlaybackResponse.Type.Hls.value, ) @@ -505,7 +505,7 @@ internal class CallJoinCoordinator( */ private fun joinPlayback(playback: PlaybackResponse): Result { val playlistUrl = playback.playlistUrl - val createPlayer = createHlsPlayer + val createPlayer = hlsPlayerFactory() if (playback.type != PlaybackResponse.Type.Hls || playlistUrl.isBlank() || createPlayer == null) { logger.e { "[joinInternal] Unsupported playback (type=${playback.type}, " + diff --git a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt index 984886aa200..24d93ea5001 100644 --- a/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt +++ b/stream-video-android-core/src/main/kotlin/io/getstream/video/android/core/call/hls/HlsPlayer.kt @@ -26,9 +26,9 @@ import kotlinx.coroutines.flow.StateFlow * Plays the HLS broadcast of one [Call]. * * The call owns its player: one is created when the backend serves the call as HLS, and it is - * released when the call is left. A renderer only shows and hides the video with [attach] and - * [detach]. Playback, including audio, continues while no renderer is shown, the same as a WebRTC - * call, and a renderer that is recreated, for example on rotation, shows the same player. + * released when the call is left. Joining alone plays nothing: playback, including audio, starts + * at the first [attach]. From then on it continues while no renderer is shown, the same as a + * WebRTC call, and a renderer that is recreated, for example on rotation, shows the same player. * * The player draws to one surface at a time: showing the same call in two renderers at once is * not supported. diff --git a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt index eb79890393d..31ae437ac76 100644 --- a/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt +++ b/stream-video-android-core/src/test/kotlin/io/getstream/video/android/core/call/components/CallJoinCoordinatorTest.kt @@ -170,7 +170,7 @@ class CallJoinCoordinatorTest { callRegistry = callRegistry, hasRequiredPermissions = { true }, createPlaybackSession = { playbackSession }, - createHlsPlayer = createHlsPlayer, + hlsPlayerFactory = { createHlsPlayer }, ) private fun stubJoinCall(result: io.getstream.result.Result) {