@poolse/native 0.0.0-stage → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +206 -0
- package/README.md +116 -2
- package/android/build.gradle +58 -0
- package/android/consumer-rules.pro +11 -0
- package/android/src/main/AndroidManifest.xml +46 -0
- package/android/src/main/java/dev/poolse/callcore/CallCoreRuntime.kt +540 -0
- package/android/src/main/java/dev/poolse/callcore/CallMachine.kt +492 -0
- package/android/src/main/java/dev/poolse/callcore/Ports.kt +132 -0
- package/android/src/main/java/dev/poolse/callcore/android/AndroidRingSurface.kt +383 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallActionReceiver.kt +45 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallCore.kt +578 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallForegroundService.kt +294 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallHttpClient.kt +207 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallMessagingService.kt +161 -0
- package/android/src/main/java/dev/poolse/callcore/android/CallVideo.kt +36 -0
- package/android/src/main/java/dev/poolse/callcore/android/HandlerTimerPort.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/IncomingCallActivity.kt +314 -0
- package/android/src/main/java/dev/poolse/callcore/android/MessageNotifications.kt +353 -0
- package/android/src/main/java/dev/poolse/callcore/android/MissedCallNotification.kt +103 -0
- package/android/src/main/java/dev/poolse/callcore/android/RingBundle.kt +42 -0
- package/android/src/main/java/dev/poolse/callcore/android/SnapshotJson.kt +99 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallLockscreen.kt +42 -0
- package/android/src/main/java/expo/modules/poolsecalls/CallPictureInPicture.kt +92 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallVideoView.kt +64 -0
- package/android/src/main/java/expo/modules/poolsecalls/PoolseCallsModule.kt +205 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitMediaPort.kt +594 -0
- package/android/src/main/livekit/java/dev/poolse/callcore/livekit/LiveKitVideoSurface.kt +107 -0
- package/android/src/test/java/dev/poolse/callcore/android/CallHttpClientTest.kt +61 -0
- package/app.plugin.js +5 -0
- package/dist/index.cjs +197 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +313 -0
- package/dist/index.d.ts +313 -0
- package/dist/index.js +161 -0
- package/dist/index.js.map +1 -0
- package/expo-module.config.json +11 -0
- package/ios/Core/CallCoreRuntime.swift +543 -0
- package/ios/Core/CallHttpClient.swift +213 -0
- package/ios/Core/CallMachine.swift +394 -0
- package/ios/Core/PORTS_READY.md +93 -0
- package/ios/Core/Ports.swift +238 -0
- package/ios/Core/SnapshotEncoding.swift +122 -0
- package/ios/Module/PoolseCallVideoView.swift +70 -0
- package/ios/Module/PoolseCallsAppDelegateSubscriber.swift +16 -0
- package/ios/Module/PoolseCallsModule.swift +150 -0
- package/ios/Platform/CallCore.swift +623 -0
- package/ios/Platform/CallKitController.swift +326 -0
- package/ios/Platform/CallPictureInPicture.swift +145 -0
- package/ios/Platform/LiveKitMediaPort.swift +798 -0
- package/ios/Platform/LiveKitVideoSurface.swift +68 -0
- package/ios/Platform/MessageNotifications.swift +163 -0
- package/ios/Platform/Ringback.swift +61 -0
- package/ios/Platform/VoipPush.swift +174 -0
- package/ios/PoolseCalls.podspec +68 -0
- package/package.json +52 -3
- package/plugin/index.js +42 -0
- package/plugin/package.json +1 -0
- package/plugin/withCallDelegate.js +35 -0
- package/plugin/withLockscreenCalls.js +84 -0
- package/plugin/withNativeCallMedia.js +19 -0
- package/plugin/withPictureInPicture.js +28 -0
- package/plugin/withRingback.js +86 -0
- package/src/expo-modules-core.d.ts +16 -0
- package/src/index.ts +564 -0
- package/src/react-shims.d.ts +22 -0
|
@@ -0,0 +1,492 @@
|
|
|
1
|
+
package dev.poolse.callcore
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The call state machine — contract cv 1, CALL_CONTRACT.md §7.
|
|
5
|
+
*
|
|
6
|
+
* Pure: no I/O, no platform, no time. Events in, (state, effects) out.
|
|
7
|
+
* The adapters execute effects; the machine only decides. Its behavior
|
|
8
|
+
* is pinned by contracts/call-core/vectors.json, which the conformance
|
|
9
|
+
* test runs verbatim — change the vectors first, then this file.
|
|
10
|
+
*
|
|
11
|
+
* Lifecycle authority is the SERVER's (LiveKit-counted). This machine
|
|
12
|
+
* never counts participants to decide whether a call should end; it
|
|
13
|
+
* reacts to `RING_ENDED` and `CALL_FINISHED` and reports what the user
|
|
14
|
+
* did.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
enum class Phase { IDLE, RINGING_IN, RINGING_OUT, CONNECTING, ACTIVE, RECONNECTING, ENDED }
|
|
18
|
+
|
|
19
|
+
enum class Direction { INCOMING, OUTGOING, JOINED }
|
|
20
|
+
|
|
21
|
+
enum class EndReason {
|
|
22
|
+
LEFT, FINISHED, DECLINED_ALL, BUSY_ALL, NO_ANSWER, CANCELLED, CONNECTION_LOST, FAILED;
|
|
23
|
+
|
|
24
|
+
val wire: String get() = name.lowercase()
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A second call ringing while this device is on one (CALL_CONTRACT.md §7,
|
|
29
|
+
* call waiting): shown beside the current call, never instead of it.
|
|
30
|
+
*/
|
|
31
|
+
data class WaitingCall(
|
|
32
|
+
val callId: String,
|
|
33
|
+
val conversationId: String?,
|
|
34
|
+
val media: String,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
/** Everything the machine knows. Immutable; transitions return a new one. */
|
|
38
|
+
data class MachineState(
|
|
39
|
+
val phase: Phase = Phase.IDLE,
|
|
40
|
+
val callId: String? = null,
|
|
41
|
+
val conversationId: String? = null,
|
|
42
|
+
val direction: Direction? = null,
|
|
43
|
+
val media: String = "audio",
|
|
44
|
+
val reason: EndReason? = null,
|
|
45
|
+
/** HTTP attempts made for the current connecting request (retry budget = 2). */
|
|
46
|
+
val httpAttempts: Int = 0,
|
|
47
|
+
/** A second call ringing beside this one, if any. */
|
|
48
|
+
val waiting: WaitingCall? = null,
|
|
49
|
+
) {
|
|
50
|
+
companion object { val IDLE = MachineState() }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Inputs, named exactly as in the contract and vectors. */
|
|
54
|
+
sealed interface Event {
|
|
55
|
+
/** A ring from push or socket — the machine treats them identically. */
|
|
56
|
+
data class Ring(
|
|
57
|
+
val callId: String,
|
|
58
|
+
val conversationId: String? = null,
|
|
59
|
+
val media: String = "audio",
|
|
60
|
+
/** Deadline already passed, or registry already finished. Never surface. */
|
|
61
|
+
val stale: Boolean = false,
|
|
62
|
+
) : Event
|
|
63
|
+
|
|
64
|
+
data class RingEnded(val callId: String, val reason: String) : Event
|
|
65
|
+
/**
|
|
66
|
+
* The server finished the call. [reason] is the push's own reason
|
|
67
|
+
* (`finished`, `declined_all`, `no_answer`, `cancelled`); it only
|
|
68
|
+
* colours the caller's ended screen when the call never got answered.
|
|
69
|
+
*/
|
|
70
|
+
data class CallFinished(val callId: String, val reason: String? = null) : Event
|
|
71
|
+
|
|
72
|
+
data object UserAnswer : Event
|
|
73
|
+
data object UserDecline : Event
|
|
74
|
+
data class UserPlace(val callId: String, val conversationId: String? = null, val media: String = "audio") : Event
|
|
75
|
+
data class UserJoin(val callId: String, val conversationId: String? = null) : Event
|
|
76
|
+
data object UserHangup : Event
|
|
77
|
+
|
|
78
|
+
/** Server accepted answer/join/place. `created=false` on place means an existing live call (it became a join). */
|
|
79
|
+
data class HttpOk(val created: Boolean = true) : Event
|
|
80
|
+
data object HttpGone : Event
|
|
81
|
+
data object HttpTimeout : Event
|
|
82
|
+
/**
|
|
83
|
+
* Terminal refusal that is NOT "the call is over": 401/403 (grant
|
|
84
|
+
* spent, wrong device), 503, or an unparseable 2xx. Ends as `failed`,
|
|
85
|
+
* never as `finished`, so the UI does not claim the other side hung up.
|
|
86
|
+
*/
|
|
87
|
+
data class HttpFailed(val code: String = "failed") : Event
|
|
88
|
+
|
|
89
|
+
data object MediaConnected : Event
|
|
90
|
+
data object MediaFailed : Event
|
|
91
|
+
data object MediaLost : Event
|
|
92
|
+
/** The SFU is recovering the session on its own (ICE restart, resume). Show it; do not rejoin. */
|
|
93
|
+
data object MediaReconnecting : Event
|
|
94
|
+
data object MediaRecovered : Event
|
|
95
|
+
data object RemoteJoined : Event
|
|
96
|
+
|
|
97
|
+
/** Caller side: every ring resolved without anyone joining. */
|
|
98
|
+
data class AllRingsResolved(val outcome: String) : Event
|
|
99
|
+
|
|
100
|
+
data object RingDeadline : Event
|
|
101
|
+
data object ReconnectBudgetElapsed : Event
|
|
102
|
+
/** CONNECTING watchdog: neither the server nor the SFU got us in. */
|
|
103
|
+
data object ConnectTimeout : Event
|
|
104
|
+
data object LingerElapsed : Event
|
|
105
|
+
|
|
106
|
+
/** Call waiting: leave the current call and answer the waiting one. */
|
|
107
|
+
data object UserAnswerWaiting : Event
|
|
108
|
+
/** Call waiting: turn the waiting call away — its caller hears busy. */
|
|
109
|
+
data object UserDeclineWaiting : Event
|
|
110
|
+
/** The waiting call's ring deadline passed. */
|
|
111
|
+
data object WaitingDeadline : Event
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Abstract effects — each adapter maps them to its platform. */
|
|
115
|
+
sealed interface Effect {
|
|
116
|
+
val wire: String
|
|
117
|
+
|
|
118
|
+
data object ShowRing : Effect { override val wire = "SHOW_RING" }
|
|
119
|
+
data object DismissRing : Effect { override val wire = "DISMISS_RING" }
|
|
120
|
+
data object RingtoneStart : Effect { override val wire = "RINGTONE_START" }
|
|
121
|
+
data object RingtoneStop : Effect { override val wire = "RINGTONE_STOP" }
|
|
122
|
+
data object RingbackStart : Effect { override val wire = "RINGBACK_START" }
|
|
123
|
+
data object RingbackStop : Effect { override val wire = "RINGBACK_STOP" }
|
|
124
|
+
data object HttpAnswer : Effect { override val wire = "HTTP_ANSWER" }
|
|
125
|
+
data object HttpJoin : Effect { override val wire = "HTTP_JOIN" }
|
|
126
|
+
data object HttpPlace : Effect { override val wire = "HTTP_PLACE" }
|
|
127
|
+
data object HttpDecline : Effect { override val wire = "HTTP_DECLINE" }
|
|
128
|
+
data class HttpDeclineBusy(val callId: String) : Effect { override val wire = "HTTP_DECLINE_BUSY:$callId" }
|
|
129
|
+
data object HttpHangup : Effect { override val wire = "HTTP_HANGUP" }
|
|
130
|
+
data object HttpReconnect : Effect { override val wire = "HTTP_RECONNECT" }
|
|
131
|
+
data object MediaJoin : Effect { override val wire = "MEDIA_JOIN" }
|
|
132
|
+
data object MediaLeave : Effect { override val wire = "MEDIA_LEAVE" }
|
|
133
|
+
data object CallSessionStart : Effect { override val wire = "CALL_SESSION_START" }
|
|
134
|
+
data object CallSessionStop : Effect { override val wire = "CALL_SESSION_STOP" }
|
|
135
|
+
data object ShowWaiting : Effect { override val wire = "SHOW_WAITING" }
|
|
136
|
+
data object DismissWaiting : Effect { override val wire = "DISMISS_WAITING" }
|
|
137
|
+
/**
|
|
138
|
+
* The waiting call becomes the current one: retire the old call's
|
|
139
|
+
* grant/bundle/generation and take the waiting ring's.
|
|
140
|
+
*/
|
|
141
|
+
data object PromoteWaiting : Effect { override val wire = "PROMOTE_WAITING" }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
data class Transition(val state: MachineState, val effects: List<Effect>)
|
|
145
|
+
|
|
146
|
+
object CallMachine {
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Phases that can hold a call waiting beside them: this device is on
|
|
150
|
+
* (or placing, or joining) a call. A second ring while one is ringING
|
|
151
|
+
* in is still busy — two rings at once is not a choice anyone can make.
|
|
152
|
+
*/
|
|
153
|
+
private fun canHoldWaiting(s: MachineState) = when (s.phase) {
|
|
154
|
+
Phase.RINGING_OUT, Phase.CONNECTING, Phase.ACTIVE, Phase.RECONNECTING -> true
|
|
155
|
+
else -> false
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The ring surface effects the end-and-answer composite skips: the
|
|
160
|
+
* waiting call goes straight from waiting to connecting, never ringing.
|
|
161
|
+
*/
|
|
162
|
+
private val ringSurfaceEffects =
|
|
163
|
+
setOf<Effect>(Effect.ShowRing, Effect.RingtoneStart, Effect.RingtoneStop, Effect.DismissRing)
|
|
164
|
+
|
|
165
|
+
fun transition(s: MachineState, e: Event): Transition {
|
|
166
|
+
val w = s.waiting
|
|
167
|
+
if (w == null) {
|
|
168
|
+
return when {
|
|
169
|
+
e is Event.Ring && !e.stale && canHoldWaiting(s) && e.callId != s.callId -> Transition(
|
|
170
|
+
s.copy(waiting = WaitingCall(e.callId, e.conversationId, e.media)),
|
|
171
|
+
listOf(Effect.ShowWaiting),
|
|
172
|
+
)
|
|
173
|
+
e == Event.UserAnswerWaiting || e == Event.UserDeclineWaiting || e == Event.WaitingDeadline -> noop(s)
|
|
174
|
+
else -> single(s, e)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
val cleared = s.copy(waiting = null)
|
|
179
|
+
return when {
|
|
180
|
+
e is Event.Ring -> when (e.callId) {
|
|
181
|
+
w.callId -> noop(s)
|
|
182
|
+
s.callId -> carry(single(cleared, e), w)
|
|
183
|
+
else -> if (e.stale) noop(s) else Transition(s, listOf(Effect.HttpDeclineBusy(e.callId)))
|
|
184
|
+
}
|
|
185
|
+
e is Event.RingEnded && e.callId == w.callId -> Transition(cleared, listOf(Effect.DismissWaiting))
|
|
186
|
+
e is Event.CallFinished && e.callId == w.callId -> Transition(cleared, listOf(Effect.DismissWaiting))
|
|
187
|
+
e == Event.WaitingDeadline -> Transition(cleared, listOf(Effect.DismissWaiting))
|
|
188
|
+
e == Event.UserDeclineWaiting ->
|
|
189
|
+
Transition(cleared, listOf(Effect.HttpDeclineBusy(w.callId), Effect.DismissWaiting))
|
|
190
|
+
e == Event.UserAnswerWaiting -> {
|
|
191
|
+
// Leaving rings the waiting call (below); answering it at once
|
|
192
|
+
// makes that ring a no-op the surface never sees.
|
|
193
|
+
val left = transition(s, Event.UserHangup)
|
|
194
|
+
if (left.state.phase != Phase.RINGING_IN || left.state.callId != w.callId) {
|
|
195
|
+
noop(s)
|
|
196
|
+
} else {
|
|
197
|
+
val answered = single(left.state, Event.UserAnswer)
|
|
198
|
+
Transition(answered.state, (left.effects + answered.effects).filterNot { it in ringSurfaceEffects })
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else -> {
|
|
202
|
+
val t = single(cleared, e)
|
|
203
|
+
if (t.state.phase != Phase.ENDED && t.state.phase != Phase.IDLE) {
|
|
204
|
+
carry(t, w)
|
|
205
|
+
} else {
|
|
206
|
+
// The current call is over while another rings beside it: that
|
|
207
|
+
// ring is now simply THE ring (no ended screen in between).
|
|
208
|
+
Transition(
|
|
209
|
+
MachineState(
|
|
210
|
+
phase = Phase.RINGING_IN, callId = w.callId, conversationId = w.conversationId,
|
|
211
|
+
direction = Direction.INCOMING, media = w.media,
|
|
212
|
+
),
|
|
213
|
+
t.effects + listOf(Effect.PromoteWaiting, Effect.ShowRing, Effect.RingtoneStart),
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private fun carry(t: Transition, w: WaitingCall) = Transition(t.state.copy(waiting = w), t.effects)
|
|
221
|
+
|
|
222
|
+
/** The one-call machine: everything that is not about a waiting call. */
|
|
223
|
+
private fun single(s: MachineState, e: Event): Transition = when (e) {
|
|
224
|
+
|
|
225
|
+
is Event.Ring -> ring(s, e)
|
|
226
|
+
|
|
227
|
+
is Event.RingEnded ->
|
|
228
|
+
if (s.phase == Phase.RINGING_IN && e.callId == s.callId) {
|
|
229
|
+
Transition(MachineState.IDLE, listOf(Effect.RingtoneStop, Effect.DismissRing))
|
|
230
|
+
} else noop(s)
|
|
231
|
+
|
|
232
|
+
is Event.CallFinished ->
|
|
233
|
+
if (e.callId == s.callId) when (s.phase) {
|
|
234
|
+
// The finish push can overtake (or replace) ring_ended: a ring
|
|
235
|
+
// for a finished call is over either way.
|
|
236
|
+
Phase.RINGING_IN -> Transition(MachineState.IDLE, listOf(Effect.RingtoneStop, Effect.DismissRing))
|
|
237
|
+
// Caller still ringing out: nobody ever joined, so the push's
|
|
238
|
+
// reason (no_answer / declined_all / cancelled) is the truth the
|
|
239
|
+
// ended screen should show. The server already finished the call
|
|
240
|
+
// — no hangup to send.
|
|
241
|
+
Phase.RINGING_OUT -> Transition(
|
|
242
|
+
s.copy(phase = Phase.ENDED, reason = ringOutcome(e.reason)),
|
|
243
|
+
listOf(Effect.RingbackStop, Effect.MediaLeave, Effect.CallSessionStop),
|
|
244
|
+
)
|
|
245
|
+
Phase.ACTIVE, Phase.RECONNECTING -> Transition(
|
|
246
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FINISHED),
|
|
247
|
+
listOf(Effect.MediaLeave, Effect.CallSessionStop),
|
|
248
|
+
)
|
|
249
|
+
// CONNECTING may already have joined media (HTTP_OK came back):
|
|
250
|
+
// leave it, or the room outlives the call.
|
|
251
|
+
Phase.CONNECTING -> Transition(
|
|
252
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FINISHED),
|
|
253
|
+
listOf(Effect.MediaLeave, Effect.CallSessionStop),
|
|
254
|
+
)
|
|
255
|
+
// Lingering "ended": a later finish with a real outcome (the FCM
|
|
256
|
+
// push after a reason-less socket event, or vice versa) corrects
|
|
257
|
+
// an unanswered outgoing call's generic "finished" to what
|
|
258
|
+
// actually happened. Display only — nothing left to tear down.
|
|
259
|
+
Phase.ENDED -> {
|
|
260
|
+
val outcome = ringOutcome(e.reason)
|
|
261
|
+
if (s.direction == Direction.OUTGOING && s.reason == EndReason.FINISHED &&
|
|
262
|
+
outcome != EndReason.FINISHED
|
|
263
|
+
) Transition(s.copy(reason = outcome), emptyList()) else noop(s)
|
|
264
|
+
}
|
|
265
|
+
else -> noop(s)
|
|
266
|
+
} else noop(s)
|
|
267
|
+
|
|
268
|
+
Event.UserAnswer ->
|
|
269
|
+
if (s.phase == Phase.RINGING_IN) Transition(
|
|
270
|
+
s.copy(phase = Phase.CONNECTING, direction = Direction.INCOMING, httpAttempts = 1),
|
|
271
|
+
listOf(Effect.RingtoneStop, Effect.DismissRing, Effect.HttpAnswer, Effect.CallSessionStart),
|
|
272
|
+
) else noop(s)
|
|
273
|
+
|
|
274
|
+
Event.UserDecline ->
|
|
275
|
+
if (s.phase == Phase.RINGING_IN) Transition(
|
|
276
|
+
MachineState.IDLE,
|
|
277
|
+
listOf(Effect.RingtoneStop, Effect.DismissRing, Effect.HttpDecline),
|
|
278
|
+
) else noop(s)
|
|
279
|
+
|
|
280
|
+
is Event.UserPlace ->
|
|
281
|
+
if (s.phase == Phase.IDLE || s.phase == Phase.ENDED) Transition(
|
|
282
|
+
MachineState(
|
|
283
|
+
phase = Phase.CONNECTING, callId = e.callId, conversationId = e.conversationId,
|
|
284
|
+
direction = Direction.OUTGOING, media = e.media, httpAttempts = 1,
|
|
285
|
+
),
|
|
286
|
+
listOf(Effect.HttpPlace, Effect.CallSessionStart),
|
|
287
|
+
) else noop(s)
|
|
288
|
+
|
|
289
|
+
is Event.UserJoin ->
|
|
290
|
+
if (s.phase == Phase.IDLE || s.phase == Phase.ENDED) Transition(
|
|
291
|
+
MachineState(
|
|
292
|
+
phase = Phase.CONNECTING, callId = e.callId, conversationId = e.conversationId,
|
|
293
|
+
direction = Direction.JOINED, httpAttempts = 1,
|
|
294
|
+
),
|
|
295
|
+
listOf(Effect.HttpJoin, Effect.CallSessionStart),
|
|
296
|
+
) else noop(s)
|
|
297
|
+
|
|
298
|
+
Event.UserHangup -> when (s.phase) {
|
|
299
|
+
Phase.RINGING_OUT -> Transition(
|
|
300
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.CANCELLED),
|
|
301
|
+
listOf(Effect.RingbackStop, Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
302
|
+
)
|
|
303
|
+
Phase.ACTIVE, Phase.RECONNECTING, Phase.CONNECTING -> Transition(
|
|
304
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.LEFT),
|
|
305
|
+
listOf(Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
306
|
+
)
|
|
307
|
+
else -> noop(s)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
is Event.HttpOk -> when {
|
|
311
|
+
s.phase != Phase.CONNECTING -> noop(s)
|
|
312
|
+
s.direction == Direction.OUTGOING && e.created -> Transition(
|
|
313
|
+
s.copy(phase = Phase.RINGING_OUT),
|
|
314
|
+
listOf(Effect.MediaJoin, Effect.RingbackStart),
|
|
315
|
+
)
|
|
316
|
+
s.direction == Direction.OUTGOING && !e.created -> Transition(
|
|
317
|
+
// Somebody's call was already live: our place collapsed into a join.
|
|
318
|
+
s.copy(direction = Direction.JOINED),
|
|
319
|
+
listOf(Effect.MediaJoin),
|
|
320
|
+
)
|
|
321
|
+
else -> Transition(s, listOf(Effect.MediaJoin))
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
Event.HttpGone -> when (s.phase) {
|
|
325
|
+
Phase.CONNECTING -> Transition(
|
|
326
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FINISHED),
|
|
327
|
+
listOf(Effect.CallSessionStop),
|
|
328
|
+
)
|
|
329
|
+
Phase.RECONNECTING -> Transition(
|
|
330
|
+
// Finished — or this device was superseded by the same user
|
|
331
|
+
// joining elsewhere. Either way: we are no longer in it.
|
|
332
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.LEFT),
|
|
333
|
+
listOf(Effect.MediaLeave, Effect.CallSessionStop),
|
|
334
|
+
)
|
|
335
|
+
else -> noop(s)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
Event.HttpTimeout -> when {
|
|
339
|
+
s.phase != Phase.CONNECTING -> noop(s)
|
|
340
|
+
s.httpAttempts < 2 -> Transition(
|
|
341
|
+
s.copy(httpAttempts = s.httpAttempts + 1),
|
|
342
|
+
listOf(
|
|
343
|
+
when (s.direction) {
|
|
344
|
+
Direction.OUTGOING -> Effect.HttpPlace
|
|
345
|
+
Direction.JOINED -> Effect.HttpJoin
|
|
346
|
+
else -> Effect.HttpAnswer
|
|
347
|
+
},
|
|
348
|
+
),
|
|
349
|
+
)
|
|
350
|
+
// Out of retries. A timeout only ever answers the CONNECTING
|
|
351
|
+
// request, so there is no bundle and media can never join:
|
|
352
|
+
// waiting on it was the "stuck in connecting forever" bug.
|
|
353
|
+
else -> Transition(
|
|
354
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
355
|
+
listOf(Effect.HttpHangup, Effect.CallSessionStop),
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
is Event.HttpFailed -> when (s.phase) {
|
|
360
|
+
// HTTP_HANGUP still runs: with no bundle it resolves the ring
|
|
361
|
+
// grant, so an incoming caller is not left ringing a dead answer.
|
|
362
|
+
Phase.CONNECTING -> Transition(
|
|
363
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
364
|
+
listOf(Effect.HttpHangup, Effect.CallSessionStop),
|
|
365
|
+
)
|
|
366
|
+
// Reconnect refused for auth: the control token is dead, so a
|
|
367
|
+
// hangup with it would be refused too — just leave.
|
|
368
|
+
Phase.RECONNECTING -> Transition(
|
|
369
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
370
|
+
listOf(Effect.MediaLeave, Effect.CallSessionStop),
|
|
371
|
+
)
|
|
372
|
+
else -> noop(s)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
Event.ConnectTimeout ->
|
|
376
|
+
if (s.phase == Phase.CONNECTING) Transition(
|
|
377
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
378
|
+
listOf(Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
379
|
+
) else noop(s)
|
|
380
|
+
|
|
381
|
+
Event.MediaConnected -> when (s.phase) {
|
|
382
|
+
Phase.CONNECTING -> Transition(s.copy(phase = Phase.ACTIVE), emptyList())
|
|
383
|
+
// A rejoin after /reconnect lands as a fresh connect.
|
|
384
|
+
Phase.RECONNECTING -> Transition(s.copy(phase = Phase.ACTIVE), emptyList())
|
|
385
|
+
else -> noop(s) // ringing_out keeps ringing until a remote joins
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
Event.MediaFailed -> when (s.phase) {
|
|
389
|
+
Phase.CONNECTING -> Transition(
|
|
390
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
391
|
+
listOf(Effect.HttpHangup, Effect.CallSessionStop),
|
|
392
|
+
)
|
|
393
|
+
// The caller's own media never came up: the callees would answer
|
|
394
|
+
// into an empty room. Give up and tell the server.
|
|
395
|
+
Phase.RINGING_OUT -> Transition(
|
|
396
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.FAILED),
|
|
397
|
+
listOf(Effect.RingbackStop, Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
398
|
+
)
|
|
399
|
+
else -> noop(s) // reconnecting: the budget timer is the backstop
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Lost = the SFU gave up (unexpected disconnect). Ask the server for
|
|
403
|
+
// a fresh token and rejoin — also from RECONNECTING, where the SFU's
|
|
404
|
+
// own recovery just failed.
|
|
405
|
+
Event.MediaLost -> when (s.phase) {
|
|
406
|
+
Phase.ACTIVE -> Transition(s.copy(phase = Phase.RECONNECTING), listOf(Effect.HttpReconnect))
|
|
407
|
+
Phase.RECONNECTING -> Transition(s, listOf(Effect.HttpReconnect))
|
|
408
|
+
else -> noop(s)
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// The SFU is resuming the same session itself: only reflect it.
|
|
412
|
+
// Tearing the room down here fought LiveKit's own recovery.
|
|
413
|
+
Event.MediaReconnecting ->
|
|
414
|
+
if (s.phase == Phase.ACTIVE) Transition(s.copy(phase = Phase.RECONNECTING), emptyList())
|
|
415
|
+
else noop(s)
|
|
416
|
+
|
|
417
|
+
Event.MediaRecovered ->
|
|
418
|
+
if (s.phase == Phase.RECONNECTING) Transition(s.copy(phase = Phase.ACTIVE), emptyList())
|
|
419
|
+
else noop(s)
|
|
420
|
+
|
|
421
|
+
Event.RemoteJoined ->
|
|
422
|
+
if (s.phase == Phase.RINGING_OUT) Transition(
|
|
423
|
+
s.copy(phase = Phase.ACTIVE),
|
|
424
|
+
listOf(Effect.RingbackStop),
|
|
425
|
+
) else noop(s)
|
|
426
|
+
|
|
427
|
+
is Event.AllRingsResolved ->
|
|
428
|
+
if (s.phase == Phase.RINGING_OUT) Transition(
|
|
429
|
+
s.copy(
|
|
430
|
+
phase = Phase.ENDED,
|
|
431
|
+
reason = when (e.outcome) {
|
|
432
|
+
"busy_all" -> EndReason.BUSY_ALL
|
|
433
|
+
"no_answer" -> EndReason.NO_ANSWER
|
|
434
|
+
"cancelled" -> EndReason.CANCELLED
|
|
435
|
+
else -> EndReason.DECLINED_ALL
|
|
436
|
+
},
|
|
437
|
+
),
|
|
438
|
+
listOf(Effect.RingbackStop, Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
439
|
+
) else noop(s)
|
|
440
|
+
|
|
441
|
+
Event.RingDeadline ->
|
|
442
|
+
if (s.phase == Phase.RINGING_IN) Transition(
|
|
443
|
+
MachineState.IDLE,
|
|
444
|
+
listOf(Effect.RingtoneStop, Effect.DismissRing),
|
|
445
|
+
) else noop(s)
|
|
446
|
+
|
|
447
|
+
Event.ReconnectBudgetElapsed ->
|
|
448
|
+
if (s.phase == Phase.RECONNECTING) Transition(
|
|
449
|
+
s.copy(phase = Phase.ENDED, reason = EndReason.CONNECTION_LOST),
|
|
450
|
+
listOf(Effect.MediaLeave, Effect.HttpHangup, Effect.CallSessionStop),
|
|
451
|
+
) else noop(s)
|
|
452
|
+
|
|
453
|
+
Event.LingerElapsed ->
|
|
454
|
+
if (s.phase == Phase.ENDED) Transition(MachineState.IDLE, emptyList()) else noop(s)
|
|
455
|
+
|
|
456
|
+
// Handled by `transition`, which owns the waiting slot.
|
|
457
|
+
Event.UserAnswerWaiting, Event.UserDeclineWaiting, Event.WaitingDeadline -> noop(s)
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
private fun ring(s: MachineState, e: Event.Ring): Transition = when {
|
|
461
|
+
e.stale -> noop(s)
|
|
462
|
+
|
|
463
|
+
// Already showing this very ring (push + socket race): one surface.
|
|
464
|
+
s.phase == Phase.RINGING_IN && e.callId == s.callId -> noop(s)
|
|
465
|
+
|
|
466
|
+
// A fresh ring lands on idle — or over a lingering ended screen,
|
|
467
|
+
// which the new call simply replaces.
|
|
468
|
+
s.phase == Phase.IDLE || s.phase == Phase.ENDED -> Transition(
|
|
469
|
+
MachineState(
|
|
470
|
+
phase = Phase.RINGING_IN, callId = e.callId,
|
|
471
|
+
conversationId = e.conversationId, direction = Direction.INCOMING, media = e.media,
|
|
472
|
+
),
|
|
473
|
+
listOf(Effect.ShowRing, Effect.RingtoneStart),
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
// Busy ringing another call (a call in progress holds it waiting
|
|
477
|
+
// instead — see `transition`): auto-decline the newcomer, whose
|
|
478
|
+
// banner still offers Join later. (Contract §7, scenario s9.)
|
|
479
|
+
else -> Transition(s, listOf(Effect.HttpDeclineBusy(e.callId)))
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** A finish push's reason, read as the caller's unanswered outcome. */
|
|
483
|
+
private fun ringOutcome(reason: String?): EndReason = when (reason) {
|
|
484
|
+
"no_answer", "timeout" -> EndReason.NO_ANSWER
|
|
485
|
+
"declined_all", "declined" -> EndReason.DECLINED_ALL
|
|
486
|
+
"busy_all" -> EndReason.BUSY_ALL
|
|
487
|
+
"cancelled" -> EndReason.CANCELLED
|
|
488
|
+
else -> EndReason.FINISHED
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
private fun noop(s: MachineState) = Transition(s, emptyList())
|
|
492
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
package dev.poolse.callcore
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The seams between the pure machine and the platform. Each adapter
|
|
5
|
+
* lives in the bridge/Android layer; the core knows only these shapes.
|
|
6
|
+
* Every port call corresponds 1:1 to an Effect — the runtime below is
|
|
7
|
+
* the only place effects are executed.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** CALL_CONTRACT.md §4 — the three doors. Tokens are opaque strings. */
|
|
11
|
+
interface HttpPort {
|
|
12
|
+
/** POST /calls/:id/answer with the ring grant. */
|
|
13
|
+
fun answer(callId: String, grant: String, cb: (HttpResult) -> Unit)
|
|
14
|
+
/** POST /calls/:id/join with a session token. */
|
|
15
|
+
fun join(callId: String, sessionToken: String, cb: (HttpResult) -> Unit)
|
|
16
|
+
/** POST /conversations/:id/calls with a session token. */
|
|
17
|
+
fun place(conversationId: String, media: String, sessionToken: String, cb: (HttpResult) -> Unit)
|
|
18
|
+
/** POST /calls/:id/decline — grant or session; fire-and-forget with retry. */
|
|
19
|
+
fun decline(callId: String, auth: String, busy: Boolean)
|
|
20
|
+
/** POST /calls/:id/hangup with the call-control token; fire-and-forget with retry. */
|
|
21
|
+
fun hangup(callId: String, controlToken: String)
|
|
22
|
+
/**
|
|
23
|
+
* POST /calls/:id/reconnect with the call-control token. The server
|
|
24
|
+
* answers with the FULL bundle (same shape as answer/join); [current]
|
|
25
|
+
* is passed so an older server's `{livekit:{url,token}}` refresh can
|
|
26
|
+
* still be applied onto it.
|
|
27
|
+
*/
|
|
28
|
+
fun reconnect(current: CallSessionBundle, cb: (HttpResult) -> Unit)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
sealed interface HttpResult {
|
|
32
|
+
/** 2xx: the CallSessionBundle (or reconnect's token refresh). */
|
|
33
|
+
data class Ok(val bundle: CallSessionBundle) : HttpResult
|
|
34
|
+
/** 410 — the call is finished (or this device was superseded). */
|
|
35
|
+
data object Gone : HttpResult
|
|
36
|
+
/** Timeout / transient network failure. */
|
|
37
|
+
data object Timeout : HttpResult
|
|
38
|
+
/** 401/403 — grant spent, expired, or wrong device. Terminal. */
|
|
39
|
+
data class AuthFailed(val code: String) : HttpResult
|
|
40
|
+
/**
|
|
41
|
+
* Terminal but not auth: 503 (no SFU capacity), another 4xx, or a 2xx
|
|
42
|
+
* whose body would not parse. Retrying would only repeat it.
|
|
43
|
+
*/
|
|
44
|
+
data class Failed(val code: String) : HttpResult
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
data class CallSessionBundle(
|
|
48
|
+
val callId: String,
|
|
49
|
+
val conversationId: String,
|
|
50
|
+
val conversationName: String?,
|
|
51
|
+
val media: String,
|
|
52
|
+
val created: Boolean,
|
|
53
|
+
val livekitUrl: String,
|
|
54
|
+
val livekitToken: String,
|
|
55
|
+
val controlToken: String,
|
|
56
|
+
/** `room` (rings nobody, CALL_CONTRACT.md §5.6) or `call`. */
|
|
57
|
+
val kind: String = "call",
|
|
58
|
+
/** How many people the server says are being rung right now. */
|
|
59
|
+
val ringing: Int = 0,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
/** LiveKit (or any SFU) — media only, no lifecycle opinions. */
|
|
63
|
+
interface MediaPort {
|
|
64
|
+
fun join(bundle: CallSessionBundle)
|
|
65
|
+
fun leave()
|
|
66
|
+
fun setMuted(muted: Boolean)
|
|
67
|
+
fun setCamera(on: Boolean)
|
|
68
|
+
fun switchCamera()
|
|
69
|
+
/**
|
|
70
|
+
* Route call audio to the loudspeaker (`true`) or back to the private
|
|
71
|
+
* route — earpiece, or a connected headset/Bluetooth device (`false`).
|
|
72
|
+
*/
|
|
73
|
+
fun setSpeaker(on: Boolean)
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Play through one specific output — an id from the snapshot's
|
|
77
|
+
* `audioOutputs`. Engines without device selection fall back to the
|
|
78
|
+
* speaker toggle.
|
|
79
|
+
*/
|
|
80
|
+
fun setAudioOutput(id: String) = setSpeaker(id == "speaker")
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The ring UX: CallStyle notification + full-screen activity + tones. */
|
|
84
|
+
interface RingSurfacePort {
|
|
85
|
+
fun show(ring: RingContext)
|
|
86
|
+
fun dismiss(callId: String)
|
|
87
|
+
fun ringtoneStart()
|
|
88
|
+
fun ringtoneStop()
|
|
89
|
+
fun ringbackStart()
|
|
90
|
+
fun ringbackStop()
|
|
91
|
+
/**
|
|
92
|
+
* A call waiting beside the current one: its own quiet surface (a
|
|
93
|
+
* heads-up with a waiting tone; iOS: CallKit's call-waiting UI).
|
|
94
|
+
*/
|
|
95
|
+
fun showWaiting(ring: RingContext) {}
|
|
96
|
+
/** The waiting call is over (declined, cancelled, timed out). */
|
|
97
|
+
fun dismissWaiting(callId: String) {}
|
|
98
|
+
/**
|
|
99
|
+
* The waiting call becomes THE ring (SHOW_RING follows): drop its
|
|
100
|
+
* waiting surface without resolving it.
|
|
101
|
+
*/
|
|
102
|
+
fun promoteWaiting(callId: String) {}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
data class RingContext(
|
|
106
|
+
val callId: String,
|
|
107
|
+
val conversationId: String,
|
|
108
|
+
val conversationName: String?,
|
|
109
|
+
val callerName: String?,
|
|
110
|
+
val media: String,
|
|
111
|
+
val grant: String,
|
|
112
|
+
val ringDeadlineEpochMs: Long,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
/** Android: the phoneCall foreground service. iOS: the CallKit session. */
|
|
116
|
+
interface CallSessionPort {
|
|
117
|
+
fun start(callId: String, media: String)
|
|
118
|
+
fun stop()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Snapshots out — the bridge turns these into JS events. */
|
|
122
|
+
fun interface SnapshotSink {
|
|
123
|
+
fun emit(state: MachineState)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Time, injectable: real adapters use a Handler/looper; tests a fake. */
|
|
127
|
+
interface TimerPort {
|
|
128
|
+
fun nowMs(): Long
|
|
129
|
+
/** Scheduling an existing key replaces it. */
|
|
130
|
+
fun schedule(key: String, delayMs: Long, fire: () -> Unit)
|
|
131
|
+
fun cancel(key: String)
|
|
132
|
+
}
|