Skip to content

Conversation

@rahul-lohra
Copy link
Contributor

@rahul-lohra rahul-lohra commented Jan 27, 2026

Goal

  1. Allow a Call instance to be safely re-joined after call.leave()
  2. Split the Call class into clear, single-purpose responsibilities

Expected Behavior

call.leave()  // Returns immediately, cleanup starts in background
call.leave() // should await for previous cleanup. And If cleanup is already done then skip it
call.join()   // Waits for cleanup, then joins ✅

call.leave()  // Returns immediately, cleanup starts in background  
call.join()   // Waits for cleanup, then joins ✅

call.leave()  // Returns immediately, cleanup completes
// No more joins, Call is cleaned up ✅

Implementation

Safe Re-join After call.leave()

  1. Cleanup triggered via call.leave() is serialized:
    • If a cleanup is already in progress, subsequent leave() calls wait for it.
    • If cleanup is already completed, repeated leave() calls are no-ops.
  • call.join() always waits for cleanup to complete before proceeding.
  • Call-scoped coroutines are re-initialized only during call.join(), and only after cleanup has finished.

This guarantees deterministic behavior across:
• leave → leave → join
• leave → join
• repeated leave calls

Call Refactor

The Call class has been decomposed into the following focused components:

  1. CallApiDelegate
  2. CallEventManager
  3. CallLifecycleManager
  4. CallSessionManager
  5. CallConnectivityMonitor
  6. CallIceConnectionMonitor
  7. CallJoinCoordinator
  8. CallNetworkSubscriptionController
  9. CallReInitializer
  10. CallRenderer
  11. CallMediaManager
  12. CallRenderer
  13. CallStatsReporter

Each class now has clear ownership and a single reason to change.

Responsibility breakdown

1. CallApiDelegate

  • Client-side API delegation
  • Call-related REST operations

Methods

    fun get(): Result<GetCallResponse>
    suspend fun create(..):Result<GetOrCreateCallResponse>
    suspend fun update(..):Result<UpdateCallResponse>
    suspend fun goLive(..):Result<GoLiveResponse>
    suspend fun stopLive(..):Result<StopLiveResponse>
    fun startScreenSharing()
    fun stopScreenSharing()
    suspend fun startHLS(..):Result<Any>
    suspend fun stopHLS(..):Result<Any>
    suspend fun accept(): Result<AcceptCallResponse>
    fun collectUserFeedback(..):Unit
    suspend fun takeScreenshot(..): Bitmap?
    suspend fun muteUser(): Result<MuteUsersResponse>
    suspend fun muteUsers(): Result<MuteUsersResponse>
    suspend fun muteAllUsers(): Result<MuteUsersResponse>
    suspend fun queryMembers(..): Result<QueriedMembers>
    suspend fun joinRequest(..):Result<JoinCallResponse>

2. CallEventManager

  • Handles call-level video events.

Methods

    fun fireEvent(event: VideoEvent)
    fun handleEvent(event: VideoEvent)

3. CallCleanupManager

  • Manages call teardown and cleanup coordination.
  • Manages mutex, locks, coroutines, scopes

Methods

    fun leave(..)
    fun cleanup()

4. CallSessionManager
- Owns RTC session creation and lifecycle mechanics.
Methods

    suspend fun _join(..):Result<RtcSession>
    suspend fun rejoin(..):Result<RtcSession>
    suspend fun fastReconnect(..)
    internal fun monitorSession(..)
    WIP
    ...

5. CallConnectivityMonitor
- Observes network connectivity changes and emits connectivity signals.

6. CallIceConnectionMonitor
- Monitors ICE connection state and manages ICE-level recovery.
Methods

    fun start()
    fun stop()

7. CallJoinCoordinator
- Owns join / rejoin orchestration and retry semantics.
Methods

    fun join()
    fun rejoin()

9. CallNetworkSubscriptionController
- Manages subscription and unsubscription to network state providers.

10. CallReInitializer
- Re-initializes call-scoped coroutines after cleanup.
Methods

    fun reinitializeCoroutines()
    fun reinitialiseCoroutinesIfNeeded()
    fun waitFromCleanup()
    fun cleanupJobReference()
    fun cleanupLockVars()

11. CallRenderer
- Manages video renderer.
Methods

    fun initRenderer()

12. CallMediaManager
- Manages camera, microphone, speaker, and media configuration.
Methods

    fun setIncomingAudioEnabled()
    fun updateMediaManagerFromSettings()
    fun ensureFactoryMatchesAudioProfile()
    fun recreateFactoryAndAudioTracks()
    fun recreatePeerConnectionFactory()

13. CallStatsReporter
- Handles call statistics collection and reporting.
Methods

    fun collectStats()
    fun startCallStatsReporting()

🎨 UI Changes

None

Before After
img img

Testing

To be updated, will provide patch which will comment out destroying calls logic

Provide the patch summary here
Provide the patch code here

@rahul-lohra rahul-lohra self-assigned this Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants