refactor: clean up PeerSessionManager and CallController integration
- Split PeerSession.kt out of PeerSessionManager.kt (types, interface, manager are now in separate files) - Remove webRtcSessions duplication in CallController — PeerSessionManager is now the single source of truth for session tracking; WebRtcCallSession is retrieved via the adapter cast when WebRTC-specific APIs are needed - Initialize PeerSessionManager eagerly with localPubKey (passed to CallController constructor) instead of lazy suspend init — fixes early ICE candidates being silently dropped before first suspend call - Extract FakePeerSession into its own file for reuse across test files - Remove assertion-only glare tiebreaker tests from NipACStateMachineTest (now properly tested with real logic in PeerSessionManagerTest) https://claude.ai/code/session_01AfRYTRCvtKqqDxeKQujUrx
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.call
|
||||
|
||||
/**
|
||||
* Represents a single ICE candidate received from a peer.
|
||||
* Platform-neutral — does not depend on org.webrtc.
|
||||
*/
|
||||
data class IceCandidateData(
|
||||
val sdp: String,
|
||||
val sdpMid: String,
|
||||
val sdpMLineIndex: Int,
|
||||
)
|
||||
|
||||
/**
|
||||
* Represents the signaling state of a peer connection.
|
||||
* Maps 1:1 with WebRTC's PeerConnection.SignalingState.
|
||||
*/
|
||||
enum class SignalingState {
|
||||
STABLE,
|
||||
HAVE_LOCAL_OFFER,
|
||||
HAVE_REMOTE_OFFER,
|
||||
HAVE_LOCAL_PRANSWER,
|
||||
HAVE_REMOTE_PRANSWER,
|
||||
CLOSED,
|
||||
}
|
||||
|
||||
enum class SdpType {
|
||||
OFFER,
|
||||
ANSWER,
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstraction over a single peer connection's signaling operations.
|
||||
* Implemented by the platform-specific WebRTC wrapper (e.g. WebRtcPeerSessionAdapter).
|
||||
*/
|
||||
interface PeerSession {
|
||||
fun getSignalingState(): SignalingState?
|
||||
|
||||
fun setRemoteDescription(
|
||||
type: SdpType,
|
||||
sdp: String,
|
||||
)
|
||||
|
||||
fun addIceCandidate(candidate: IceCandidateData)
|
||||
|
||||
fun createOffer(onSdpCreated: (String) -> Unit)
|
||||
|
||||
fun createAnswer(onSdpCreated: (String) -> Unit)
|
||||
|
||||
fun rollback(onDone: () -> Unit)
|
||||
|
||||
fun dispose()
|
||||
}
|
||||
+19
-67
@@ -22,57 +22,6 @@ package com.vitorpamplona.amethyst.commons.call
|
||||
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
|
||||
/**
|
||||
* Represents a single ICE candidate received from a peer.
|
||||
* Platform-neutral — does not depend on org.webrtc.
|
||||
*/
|
||||
data class IceCandidateData(
|
||||
val sdp: String,
|
||||
val sdpMid: String,
|
||||
val sdpMLineIndex: Int,
|
||||
)
|
||||
|
||||
/**
|
||||
* Represents the signaling state of a peer connection.
|
||||
* Maps 1:1 with WebRTC's PeerConnection.SignalingState.
|
||||
*/
|
||||
enum class SignalingState {
|
||||
STABLE,
|
||||
HAVE_LOCAL_OFFER,
|
||||
HAVE_REMOTE_OFFER,
|
||||
HAVE_LOCAL_PRANSWER,
|
||||
HAVE_REMOTE_PRANSWER,
|
||||
CLOSED,
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstraction over a single peer connection's signaling operations.
|
||||
* Implemented by the platform-specific WebRTC wrapper.
|
||||
*/
|
||||
interface PeerSession {
|
||||
fun getSignalingState(): SignalingState?
|
||||
|
||||
fun setRemoteDescription(
|
||||
type: SdpType,
|
||||
sdp: String,
|
||||
)
|
||||
|
||||
fun addIceCandidate(candidate: IceCandidateData)
|
||||
|
||||
fun createOffer(onSdpCreated: (String) -> Unit)
|
||||
|
||||
fun createAnswer(onSdpCreated: (String) -> Unit)
|
||||
|
||||
fun rollback(onDone: () -> Unit)
|
||||
|
||||
fun dispose()
|
||||
}
|
||||
|
||||
enum class SdpType {
|
||||
OFFER,
|
||||
ANSWER,
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages per-peer session state and ICE candidate buffering.
|
||||
*
|
||||
@@ -85,7 +34,7 @@ enum class SdpType {
|
||||
* It is platform-independent and testable without real WebRTC.
|
||||
*/
|
||||
class PeerSessionManager(
|
||||
private val localPubKey: HexKey,
|
||||
val localPubKey: HexKey,
|
||||
) {
|
||||
data class SessionEntry(
|
||||
val session: PeerSession,
|
||||
@@ -126,9 +75,9 @@ class PeerSessionManager(
|
||||
|
||||
/**
|
||||
* Routes an incoming ICE candidate to the correct destination:
|
||||
* 1. If session exists AND remote description is set → add directly
|
||||
* 2. If session exists but remote description NOT set → buffer per-session
|
||||
* 3. If no session exists → buffer globally (keyed by sender)
|
||||
* 1. If session exists AND remote description is set -> add directly
|
||||
* 2. If session exists but remote description NOT set -> buffer per-session
|
||||
* 3. If no session exists -> buffer globally (keyed by sender)
|
||||
*
|
||||
* Returns the action taken for testability.
|
||||
*/
|
||||
@@ -137,15 +86,21 @@ class PeerSessionManager(
|
||||
candidate: IceCandidateData,
|
||||
): IceRouteAction {
|
||||
val entry = sessions[senderPubKey]
|
||||
if (entry != null && entry.remoteDescriptionSet) {
|
||||
entry.session.addIceCandidate(candidate)
|
||||
return IceRouteAction.ADDED_DIRECTLY
|
||||
} else if (entry != null) {
|
||||
entry.pendingIceCandidates.add(candidate)
|
||||
return IceRouteAction.BUFFERED_PER_SESSION
|
||||
} else {
|
||||
globalPendingIce.getOrPut(senderPubKey) { mutableListOf() }.add(candidate)
|
||||
return IceRouteAction.BUFFERED_GLOBALLY
|
||||
return when {
|
||||
entry != null && entry.remoteDescriptionSet -> {
|
||||
entry.session.addIceCandidate(candidate)
|
||||
IceRouteAction.ADDED_DIRECTLY
|
||||
}
|
||||
|
||||
entry != null -> {
|
||||
entry.pendingIceCandidates.add(candidate)
|
||||
IceRouteAction.BUFFERED_PER_SESSION
|
||||
}
|
||||
|
||||
else -> {
|
||||
globalPendingIce.getOrPut(senderPubKey) { mutableListOf() }.add(candidate)
|
||||
IceRouteAction.BUFFERED_GLOBALLY
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,17 +139,14 @@ class PeerSessionManager(
|
||||
|
||||
val signalingState = entry.session.getSignalingState()
|
||||
if (signalingState != SignalingState.HAVE_LOCAL_OFFER) {
|
||||
// No glare — accept the remote offer directly
|
||||
onAcceptRemote(entry)
|
||||
return GlareResolution.NO_GLARE
|
||||
}
|
||||
|
||||
// Glare detected: both sides sent offers simultaneously
|
||||
return if (localPubKey > peerPubKey) {
|
||||
// We win — our offer takes priority, ignore remote
|
||||
GlareResolution.LOCAL_WINS
|
||||
} else {
|
||||
// We lose — rollback our local offer, accept remote
|
||||
entry.session.rollback {
|
||||
onAcceptRemote(entry)
|
||||
}
|
||||
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.amethyst.commons.call
|
||||
|
||||
/**
|
||||
* Fake PeerSession that records all operations for test assertions.
|
||||
* Simulates WebRTC PeerConnection signaling state transitions
|
||||
* without native libraries.
|
||||
*/
|
||||
class FakePeerSession(
|
||||
private var signalingState: SignalingState = SignalingState.STABLE,
|
||||
) : PeerSession {
|
||||
val addedCandidates = mutableListOf<IceCandidateData>()
|
||||
var lastRemoteDescription: Pair<SdpType, String>? = null
|
||||
var rolledBack = false
|
||||
var disposed = false
|
||||
var lastCreatedOffer: String? = null
|
||||
var lastCreatedAnswer: String? = null
|
||||
|
||||
override fun getSignalingState(): SignalingState = signalingState
|
||||
|
||||
override fun setRemoteDescription(
|
||||
type: SdpType,
|
||||
sdp: String,
|
||||
) {
|
||||
lastRemoteDescription = type to sdp
|
||||
signalingState =
|
||||
when (type) {
|
||||
SdpType.ANSWER -> SignalingState.STABLE
|
||||
SdpType.OFFER -> SignalingState.HAVE_REMOTE_OFFER
|
||||
}
|
||||
}
|
||||
|
||||
override fun addIceCandidate(candidate: IceCandidateData) {
|
||||
addedCandidates.add(candidate)
|
||||
}
|
||||
|
||||
override fun createOffer(onSdpCreated: (String) -> Unit) {
|
||||
signalingState = SignalingState.HAVE_LOCAL_OFFER
|
||||
val sdp = "fake-offer-sdp"
|
||||
lastCreatedOffer = sdp
|
||||
onSdpCreated(sdp)
|
||||
}
|
||||
|
||||
override fun createAnswer(onSdpCreated: (String) -> Unit) {
|
||||
val sdp = "fake-answer-sdp"
|
||||
lastCreatedAnswer = sdp
|
||||
signalingState = SignalingState.STABLE
|
||||
onSdpCreated(sdp)
|
||||
}
|
||||
|
||||
override fun rollback(onDone: () -> Unit) {
|
||||
rolledBack = true
|
||||
signalingState = SignalingState.STABLE
|
||||
onDone()
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
disposed = true
|
||||
signalingState = SignalingState.CLOSED
|
||||
}
|
||||
}
|
||||
-58
@@ -487,61 +487,3 @@ class PeerSessionManagerTest {
|
||||
assertFalse(bobAccepted, "bob should ignore alice's offer")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fake PeerSession that records all operations for test assertions.
|
||||
* No real WebRTC involved.
|
||||
*/
|
||||
class FakePeerSession(
|
||||
private var signalingState: SignalingState = SignalingState.STABLE,
|
||||
) : PeerSession {
|
||||
val addedCandidates = mutableListOf<IceCandidateData>()
|
||||
var lastRemoteDescription: Pair<SdpType, String>? = null
|
||||
var rolledBack = false
|
||||
var disposed = false
|
||||
var lastCreatedOffer: String? = null
|
||||
var lastCreatedAnswer: String? = null
|
||||
|
||||
override fun getSignalingState(): SignalingState = signalingState
|
||||
|
||||
override fun setRemoteDescription(
|
||||
type: SdpType,
|
||||
sdp: String,
|
||||
) {
|
||||
lastRemoteDescription = type to sdp
|
||||
if (type == SdpType.ANSWER) {
|
||||
signalingState = SignalingState.STABLE
|
||||
} else if (type == SdpType.OFFER) {
|
||||
signalingState = SignalingState.HAVE_REMOTE_OFFER
|
||||
}
|
||||
}
|
||||
|
||||
override fun addIceCandidate(candidate: IceCandidateData) {
|
||||
addedCandidates.add(candidate)
|
||||
}
|
||||
|
||||
override fun createOffer(onSdpCreated: (String) -> Unit) {
|
||||
signalingState = SignalingState.HAVE_LOCAL_OFFER
|
||||
val sdp = "fake-offer-sdp"
|
||||
lastCreatedOffer = sdp
|
||||
onSdpCreated(sdp)
|
||||
}
|
||||
|
||||
override fun createAnswer(onSdpCreated: (String) -> Unit) {
|
||||
val sdp = "fake-answer-sdp"
|
||||
lastCreatedAnswer = sdp
|
||||
signalingState = SignalingState.STABLE
|
||||
onSdpCreated(sdp)
|
||||
}
|
||||
|
||||
override fun rollback(onDone: () -> Unit) {
|
||||
rolledBack = true
|
||||
signalingState = SignalingState.STABLE
|
||||
onDone()
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
disposed = true
|
||||
signalingState = SignalingState.CLOSED
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user