Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / call / InAppCall.kt
InAppCall.kt363 lines · 13.1 KiBRaw
| 1 | package com.vayunmathur.communicate.data.call |
| 2 | |
| 3 | import android.Manifest |
| 4 | import com.vayunmathur.communicate.data.CommunicateLine |
| 5 | import com.vayunmathur.library.log.Log |
| 6 | import kotlinx.coroutines.flow.MutableStateFlow |
| 7 | import kotlinx.coroutines.flow.StateFlow |
| 8 | import kotlinx.coroutines.flow.asStateFlow |
| 9 | |
| 10 | /** |
| 11 | * What the line behind the active call can actually do. |
| 12 | * |
| 13 | * The call screen is shared across lines, so it renders from this rather than branching on the line: |
| 14 | * Google Voice has a DTMF keypad but no video, WhatsApp and Signal have video but no keypad. A control the |
| 15 | * line cannot honour is simply not shown, instead of being present and doing nothing. |
| 16 | */ |
| 17 | data class CallCapabilities( |
| 18 | val mute: Boolean = true, |
| 19 | val speaker: Boolean = true, |
| 20 | val video: Boolean = false, |
| 21 | val dtmf: Boolean = false, |
| 22 | val screenShare: Boolean = false, |
| 23 | val hold: Boolean = false, |
| 24 | ) { |
| 25 | /** Prebuilt capability sets for audio-only and audio+video calls. */ |
| 26 | companion object { |
| 27 | val AudioOnly = CallCapabilities() |
| 28 | val AudioAndVideo = CallCapabilities(video = true, screenShare = true) |
| 29 | val AudioAndKeypad = CallCapabilities(dtmf = true) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Whether this line captures call media in-app (WebRTC/SIP) and so dead-ends without a runtime |
| 35 | * permission. SIM calls are captured by Telecom/the carrier stack, which needs no RECORD_AUDIO |
| 36 | * from this app, and RCS has no call path at all — neither is permission-gated at call start. |
| 37 | */ |
| 38 | fun CommunicateLine.requiresInAppCallPermissions(): Boolean = when (this) { |
| 39 | CommunicateLine.GoogleVoice, |
| 40 | CommunicateLine.WhatsApp, |
| 41 | CommunicateLine.Signal, |
| 42 | -> true |
| 43 | CommunicateLine.Sim, |
| 44 | CommunicateLine.Rcs, |
| 45 | -> false |
| 46 | } |
| 47 | |
| 48 | /** Runtime permissions a call on an in-app media line needs before any media is captured. */ |
| 49 | fun inAppCallPermissions(video: Boolean): List<String> = if (video) { |
| 50 | listOf(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO) |
| 51 | } else { |
| 52 | listOf(Manifest.permission.RECORD_AUDIO) |
| 53 | } |
| 54 | |
| 55 | /** Phases an in-app VoIP call moves through, shared by every line that isn't handled by the SIM. */ |
| 56 | enum class InAppCallPhase { Idle, Outgoing, Incoming, Connecting, Active, Ended } |
| 57 | |
| 58 | /** True while a call is live (ringing, connecting, or connected). */ |
| 59 | fun InAppCallPhase.isLiveCall(): Boolean = this == InAppCallPhase.Incoming || |
| 60 | this == InAppCallPhase.Outgoing || |
| 61 | this == InAppCallPhase.Connecting || |
| 62 | this == InAppCallPhase.Active |
| 63 | |
| 64 | /** |
| 65 | * The single active in-app call, in the shape the UI and the Telecom bridge need. |
| 66 | * |
| 67 | * Deliberately richer than what the underlying stacks expose: RingRTC reports only |
| 68 | * ringing/connecting/connected and drops the call id on most callbacks, so mute, speaker, the peer's |
| 69 | * name and the connect time have to be tracked here or the UI has nothing to show. |
| 70 | */ |
| 71 | data class InAppCallState( |
| 72 | val phase: InAppCallPhase = InAppCallPhase.Idle, |
| 73 | val line: CommunicateLine? = null, |
| 74 | /** Line-specific id (ACI, JID) used to address call actions. */ |
| 75 | val peerId: String = "", |
| 76 | val peerName: String = "", |
| 77 | val isVideo: Boolean = false, |
| 78 | val capabilities: CallCapabilities = CallCapabilities.AudioOnly, |
| 79 | /** Whether the peer is currently sending video, which is independent of whether we are. */ |
| 80 | val remoteVideoEnabled: Boolean = false, |
| 81 | /** Whether our own camera is on. */ |
| 82 | val localVideoEnabled: Boolean = false, |
| 83 | /** Whether we are sending the screen rather than the camera. */ |
| 84 | val screenSharing: Boolean = false, |
| 85 | /** Whether the peer is sharing their screen. */ |
| 86 | val remoteScreenSharing: Boolean = false, |
| 87 | val muted: Boolean = false, |
| 88 | val speaker: Boolean = false, |
| 89 | /** Whether the carrier call is held (GSM hold / Telecom STATE_HOLDING). Only the SIM line sets this. */ |
| 90 | val held: Boolean = false, |
| 91 | /** When the call became [InAppCallPhase.Active], for the duration readout. 0 until then. */ |
| 92 | val connectedAtMs: Long = 0L, |
| 93 | val endReason: String? = null, |
| 94 | ) { |
| 95 | val isRinging: Boolean get() = phase == InAppCallPhase.Incoming |
| 96 | val isOngoing: Boolean get() = phase == InAppCallPhase.Active || phase == InAppCallPhase.Connecting |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * What the call UI and the system call surface can do to a call, implemented per line. |
| 101 | * |
| 102 | * Kept minimal on purpose: anything a line cannot do is a no-op rather than an error, so the shared |
| 103 | * screen does not need to know which line it is driving. |
| 104 | */ |
| 105 | interface InAppCallController { |
| 106 | fun answer() |
| 107 | fun reject() |
| 108 | fun hangup() |
| 109 | fun setMuted(muted: Boolean) |
| 110 | fun setSpeaker(on: Boolean) |
| 111 | } |
| 112 | |
| 113 | /** Implemented by the Telecom `Connection` so a line's manager can drive the system call surface. */ |
| 114 | interface InAppCallConnectionBridge { |
| 115 | fun onCallActive() |
| 116 | |
| 117 | fun onCallEnded() |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tone dialling, for lines that carry a call to the PSTN. Separate from [InAppCallController] because only |
| 122 | * Google Voice can act on it. |
| 123 | */ |
| 124 | interface InAppCallDtmfController { |
| 125 | fun sendDtmf(digit: String) |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Call hold, separate from [InAppCallController] because only the carrier line can act on it. |
| 130 | */ |
| 131 | interface InAppCallHoldController { |
| 132 | fun setHeld(held: Boolean) |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Video capability, separate from [InAppCallController] because not every line has it and the shared screen |
| 137 | * should only offer the control when something can act on it. |
| 138 | */ |
| 139 | interface InAppCallVideoController { |
| 140 | fun setVideoEnabled(enabled: Boolean) |
| 141 | |
| 142 | fun flipCamera() |
| 143 | |
| 144 | /** EGL context the renderers must share with the decoder, or null if video is unavailable. */ |
| 145 | fun eglContext(): org.webrtc.EglBase.Context? |
| 146 | |
| 147 | /** |
| 148 | * Attached separately rather than as a pair: the two renderers are created and disposed independently by |
| 149 | * the UI, and a combined setter meant whichever recomposed last cleared the other's sink — which showed up |
| 150 | * as remote video freezing when the self-view appeared. |
| 151 | */ |
| 152 | fun attachLocalRenderer(sink: org.webrtc.VideoSink?) |
| 153 | |
| 154 | fun attachRemoteRenderer(sink: org.webrtc.VideoSink?) |
| 155 | |
| 156 | /** Share the screen instead of the camera. */ |
| 157 | fun setScreenShareEnabled(enabled: Boolean, permission: android.content.Intent?) |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * The one place that knows whether a call is up, regardless of line. |
| 162 | * |
| 163 | * A single registry rather than one per line because only one in-app call can be active at a time, and |
| 164 | * because the call screen, the notification and the Telecom connection all need the same view of it. |
| 165 | * Google Voice deliberately stays outside this: it already has its own Telecom integration. |
| 166 | */ |
| 167 | object InAppCallRegistry { |
| 168 | private const val TAG = "InAppCallRegistry" |
| 169 | |
| 170 | private val _state = MutableStateFlow(InAppCallState()) |
| 171 | val state: StateFlow<InAppCallState> = _state.asStateFlow() |
| 172 | |
| 173 | @Volatile |
| 174 | private var controller: InAppCallController? = null |
| 175 | |
| 176 | /** Set only by lines that can carry video, so the UI can offer the control. */ |
| 177 | @Volatile |
| 178 | var videoController: InAppCallVideoController? = null |
| 179 | |
| 180 | /** Set only by lines that can send tones. */ |
| 181 | @Volatile |
| 182 | var dtmfController: InAppCallDtmfController? = null |
| 183 | |
| 184 | /** Set only by lines that can hold (the carrier line). */ |
| 185 | @Volatile |
| 186 | var holdController: InAppCallHoldController? = null |
| 187 | |
| 188 | /** Set by the Telecom ConnectionService so state changes can be reflected into the system UI. */ |
| 189 | @Volatile |
| 190 | var connection: InAppCallConnectionBridge? = null |
| 191 | |
| 192 | fun bind( |
| 193 | line: CommunicateLine, |
| 194 | controller: InAppCallController, |
| 195 | capabilities: CallCapabilities = CallCapabilities.AudioOnly, |
| 196 | ) { |
| 197 | this.controller = controller |
| 198 | _state.value = _state.value.copy(line = line, capabilities = capabilities) |
| 199 | } |
| 200 | |
| 201 | /** Called by a line when its call starts, so the UI can appear before any media is up. */ |
| 202 | fun onCallStarting( |
| 203 | line: CommunicateLine, |
| 204 | peerId: String, |
| 205 | peerName: String, |
| 206 | isVideo: Boolean, |
| 207 | incoming: Boolean, |
| 208 | capabilities: CallCapabilities = _state.value.capabilities, |
| 209 | ) { |
| 210 | Log.status(TAG, "call starting: line=$line incoming=$incoming video=$isVideo peer=$peerId") |
| 211 | _state.value = InAppCallState( |
| 212 | phase = if (incoming) InAppCallPhase.Incoming else InAppCallPhase.Outgoing, |
| 213 | line = line, |
| 214 | peerId = peerId, |
| 215 | peerName = peerName.ifBlank { peerId }, |
| 216 | isVideo = isVideo, |
| 217 | // A call placed as video starts with the camera and speaker on; RingRTC is told the same via |
| 218 | // proceed()'s enableCamera, so the UI must not claim otherwise. |
| 219 | localVideoEnabled = isVideo && !incoming, |
| 220 | speaker = isVideo && !incoming, |
| 221 | capabilities = capabilities, |
| 222 | ) |
| 223 | } |
| 224 | |
| 225 | fun onPhase(phase: InAppCallPhase) { |
| 226 | val current = _state.value |
| 227 | if (current.phase == InAppCallPhase.Idle && phase != InAppCallPhase.Incoming) { |
| 228 | Log.status(TAG, "ignoring phase $phase while idle") |
| 229 | return |
| 230 | } |
| 231 | if (current.phase != phase) Log.status(TAG, "phase ${current.phase} -> $phase") |
| 232 | _state.value = current.copy( |
| 233 | phase = phase, |
| 234 | // Stamped once, on the transition into Active, so the duration does not restart. |
| 235 | connectedAtMs = if (phase == InAppCallPhase.Active && current.connectedAtMs == 0L) { |
| 236 | System.currentTimeMillis() |
| 237 | } else { |
| 238 | current.connectedAtMs |
| 239 | }, |
| 240 | ) |
| 241 | if (phase == InAppCallPhase.Active) connection?.onCallActive() |
| 242 | } |
| 243 | |
| 244 | fun onEnded(reason: String?) { |
| 245 | val current = _state.value |
| 246 | if (current.phase == InAppCallPhase.Idle) return |
| 247 | _state.value = current.copy(phase = InAppCallPhase.Ended, endReason = reason) |
| 248 | connection?.onCallEnded() |
| 249 | } |
| 250 | |
| 251 | fun onMuted(muted: Boolean) { |
| 252 | _state.value = _state.value.copy(muted = muted) |
| 253 | } |
| 254 | |
| 255 | fun onSpeaker(on: Boolean) { |
| 256 | _state.value = _state.value.copy(speaker = on) |
| 257 | } |
| 258 | |
| 259 | fun onHeld(held: Boolean) { |
| 260 | if (_state.value.phase == InAppCallPhase.Idle) return |
| 261 | _state.value = _state.value.copy(held = held) |
| 262 | } |
| 263 | |
| 264 | fun onRemoteVideo(enabled: Boolean) { |
| 265 | if (_state.value.phase == InAppCallPhase.Idle) return |
| 266 | Log.status(TAG, "remote video enabled=$enabled") |
| 267 | _state.value = _state.value.copy(remoteVideoEnabled = enabled) |
| 268 | } |
| 269 | |
| 270 | fun onRemoteScreenShare(enabled: Boolean) { |
| 271 | if (_state.value.phase == InAppCallPhase.Idle) return |
| 272 | Log.status(TAG, "remote screen share enabled=$enabled") |
| 273 | _state.value = _state.value.copy(remoteScreenSharing = enabled) |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Start or stop sharing the screen. [permission] is the result of the MediaProjection consent dialog and |
| 278 | * is required to start; stopping needs nothing. |
| 279 | */ |
| 280 | fun setScreenShare(enabled: Boolean, permission: android.content.Intent?) { |
| 281 | if (enabled && permission == null) return |
| 282 | videoController?.setScreenShareEnabled(enabled, permission) |
| 283 | _state.value = _state.value.copy( |
| 284 | screenSharing = enabled, |
| 285 | // Sharing the screen is sending video, so the peer-facing state has to agree. |
| 286 | localVideoEnabled = enabled || _state.value.localVideoEnabled, |
| 287 | ) |
| 288 | } |
| 289 | |
| 290 | fun onLocalVideo(enabled: Boolean) { |
| 291 | if (_state.value.phase == InAppCallPhase.Idle) return |
| 292 | _state.value = _state.value.copy(localVideoEnabled = enabled, isVideo = _state.value.isVideo || enabled) |
| 293 | } |
| 294 | |
| 295 | /** Turn our own camera on or off mid-call. */ |
| 296 | fun toggleVideo() { |
| 297 | val next = !_state.value.localVideoEnabled |
| 298 | videoController?.setVideoEnabled(next) |
| 299 | onLocalVideo(next) |
| 300 | // Sending video implies hands-free: nobody holds a phone to their ear to be seen. The speaker |
| 301 | // control is hidden while video is on, so the route is forced here rather than left stale. |
| 302 | if (next && !_state.value.speaker) { |
| 303 | controller?.setSpeaker(true) |
| 304 | onSpeaker(true) |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | fun flipCamera() { |
| 309 | videoController?.flipCamera() |
| 310 | } |
| 311 | |
| 312 | fun sendDtmf(digit: String) { |
| 313 | dtmfController?.sendDtmf(digit) |
| 314 | } |
| 315 | |
| 316 | /** Called by the UI once a terminal state has been shown, returning to Idle. */ |
| 317 | fun clearEnded() { |
| 318 | if (_state.value.phase == InAppCallPhase.Ended) { |
| 319 | _state.value = InAppCallState() |
| 320 | controller = null |
| 321 | videoController = null |
| 322 | dtmfController = null |
| 323 | holdController = null |
| 324 | connection = null |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // -- Actions, forwarded to whichever line owns the call -- |
| 329 | |
| 330 | /** [source] is logged so it is clear what ended a call; several paths can. */ |
| 331 | fun answer(source: String = "ui") { |
| 332 | Log.status(TAG, "answer requested by $source") |
| 333 | controller?.answer() |
| 334 | } |
| 335 | |
| 336 | fun reject(source: String = "ui") { |
| 337 | Log.status(TAG, "reject requested by $source") |
| 338 | controller?.reject() |
| 339 | } |
| 340 | |
| 341 | fun hangup(source: String = "ui") { |
| 342 | Log.status(TAG, "hangup requested by $source") |
| 343 | controller?.hangup() |
| 344 | } |
| 345 | |
| 346 | fun toggleMuted() { |
| 347 | val next = !_state.value.muted |
| 348 | controller?.setMuted(next) |
| 349 | onMuted(next) |
| 350 | } |
| 351 | |
| 352 | fun toggleSpeaker() { |
| 353 | val next = !_state.value.speaker |
| 354 | controller?.setSpeaker(next) |
| 355 | onSpeaker(next) |
| 356 | } |
| 357 | |
| 358 | fun toggleHeld() { |
| 359 | val next = !_state.value.held |
| 360 | holdController?.setHeld(next) |
| 361 | onHeld(next) |
| 362 | } |
| 363 | } |