package com.cottondb.calling import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonObject // What the call engine performs its effects ON: one port per system (iOS: CottonCalls/CallPorts.swift). // // The live ports are Telecom (TelecomCalls, a self-managed ConnectionService), OkHttp's socket (RoomSocket), // WebRTC's peer and camera (WebRtcPeer), the audio route (CallAudio), the ringback (Ringback) and a main-looper // clock. The JVM tests hand fakes of every one, so each effect is checked to reach its port without a phone, a // network or a camera. Every port is called on the main thread. /** The system's call: Telecom on Android (a self-managed PhoneAccount), so a call rings and holds like a phone call. */ interface SystemCallPort { /** An outgoing call starts (`TelecomManager.placeCall`). */ fun startOutgoing(uuid: String, handle: String, name: String?, video: Boolean) fun outgoingConnecting(uuid: String) /** The call is up (`Connection.setActive`), either way. */ fun connected(uuid: String) /** A ring here (`TelecomManager.addNewIncomingCall`); a ring the push already reported is updated instead. */ fun reportIncoming(uuid: String, title: String, video: Boolean) fun update(uuid: String, handle: String, name: String?, video: Boolean) /** The call is over: [cause] is Telecom's `DisconnectCause` code. */ fun ended(uuid: String, cause: Int) } /** The room's socket. */ interface CallSocketPort { /** Open [url] with `Authorization: Bearer `. Each text frame arrives at [received]; a close nobody asked for at [closed]. */ fun open(url: String, credential: String, received: (JsonElement) -> Unit, closed: () -> Unit) /** Send one frame; frames handed before the socket opens go once it does. */ fun send(frame: JsonElement) fun close() } /** What the peer hands back to the core. */ interface CallPeerEvents { /** A description or candidate made here: `calls.signal { out }`. */ fun signal(kind: CallSignalKind, sdp: String?, candidate: String?, mid: String?, mline: Int?) /** The ICE connection's state: `calls.media`. */ fun media(state: CallMediaState) /** A group call's SFU op made here (`push` with its tracks, `renegotiate`, `close` with mids): `calls.signal { out: { sfu } }`. */ fun sfu(op: String, sdp: String?, tracks: List?, mids: List?) {} /** This phone's own voice started or stopped (a group call): `calls.speaking`. */ fun speaking(on: Boolean) {} } /** A track on the SFU: its transceiver's mid, whose it is (pulled) and its name and kind. */ data class SfuTrack(val mid: String?, val member: String?, val name: String, val kind: String? = null) /** The peer connection, its tracks and the camera: WebRTC. */ interface CallPeerPort { /** Make the peer over these servers, its microphone track (and camera track for a video call) added. The offerer makes its offer at once. */ fun create(offerer: Boolean, iceServers: List, video: Boolean, events: CallPeerEvents) /** Set the remote description; an offer or a restart is answered. */ fun applyRemote(kind: CallSignalKind, sdp: String) fun addCandidate(candidate: String, mid: String?, mline: Int?) /** A restart offer, handed back as `restart`. */ fun restartIce() fun close() fun setSending(audio: Boolean, video: Boolean) /** The front camera: 720p at 30 fps for a one-to-one call. */ fun startCamera() fun stopCamera() /** The phone's other camera (front ⇄ back), while the camera is on. */ fun flipCamera() {} // A group call: one connection to the SFU, this phone's tracks pushed, each person's pulled and kept as theirs. /** Make the one connection to the SFU (any peer-to-peer one closed first). */ fun switchToSfu(iceServers: List, video: Boolean, events: CallPeerEvents) {} /** Add the microphone (and camera) as tracks of their own, offer them, and hand back `push` with each one's mid. */ fun pushTracks(audio: Boolean, video: Boolean) {} /** The SFU's answer to a push, or its offer of others' tracks ([tracks] say whose each mid is): an offer is answered (`renegotiate`). */ fun applySfu(op: String, sdp: String, tracks: List) {} /** [member] left: their tracks and tile go, and `close` is handed back for their mids. */ fun closeTracks(member: String) {} } /** The audio route, held for a call. */ interface CallAudioPort { /** Configure for a call (voice or video): communication mode, the earpiece or, for video, the loudspeaker. */ fun configure(video: Boolean) fun stop() /** The loudspeaker, a route of this phone's alone. */ fun setSpeaker(on: Boolean) /** Where the sound can go now (the earpiece, the loudspeaker, a Bluetooth device, headphones), as `chat.callScreen`'s `routes`. */ fun routes(): List = emptyList() /** The route in use, by id; null when the phone does not say. */ fun current(): String? = null /** The sound sent to the route [id]. */ fun choose(id: String) {} /** [changed] runs whenever a route comes or goes (a headset connected): the screen asks again. */ fun watch(changed: () -> Unit) {} } /** One audio route: `kind` `phone`, `speaker`, `wired` or `bluetooth`; `name` a Bluetooth device's own. */ data class AudioRoute(val id: String, val kind: String, val name: String? = null) /** The ringback heard while the other phone rings. */ interface CallTonePort { fun play(tone: String) fun stop() } /** When to hand `calls.tick` back. */ interface CallClockPort { fun after(milliseconds: Long, fire: () -> Unit) fun cancelAll() } /** The core's `calls.*` methods, as the engine asks them; a refusal throws [com.cottondb.shell.core.CoreException]. */ fun interface CallsCore { suspend fun ask(method: String, request: JsonObject): JsonObject } /** The calls' developer log (logcat, tag `cotton-calls`, in the live engine): never a person's words. */ class CallLog(private val sink: (String) -> Unit = {}) { fun i(text: String) = sink(text) }