Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / call / WhatsAppCallSignaling.kt
WhatsAppCallSignaling.kt228 lines · 9.1 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp.call |
| 2 | |
| 3 | import android.util.Base64 |
| 4 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol |
| 5 | import com.vayunmathur.communicate.data.whatsapp.WhatsAppProtocol.Node |
| 6 | |
| 7 | /** |
| 8 | * WhatsApp call **signaling** (Phase D 3a). Builds/parses the `<call>` XMPP stanzas that ride the |
| 9 | * same Noise socket as messaging (offer / preaccept / accept / reject / terminate), with per-device |
| 10 | * E2E `<enc>` children carrying the 32-byte call key (see [WhatsAppCallCrypto]). |
| 11 | * |
| 12 | * Because WhatsApp's wire carries **no SDP** (its media is a proprietary native RTP engine), we |
| 13 | * transport standards-based WebRTC SDP + ICE candidates inside our **own** `<webrtc>` child of the |
| 14 | * `<call>` action. This makes calling interoperate **client-to-client (our app ↔ our app)** but NOT |
| 15 | * with official WhatsApp calling servers — an accepted, documented limitation of the dev scaffolding. |
| 16 | * |
| 17 | * Reference: whatsapp-documentation.md "Voice Calls And Video Calls" (SignalingXmppCallback |
| 18 | * `sendCallStanza`, OutgoingSignalingHandler per-destination `enc` children) + signaling.md. |
| 19 | */ |
| 20 | object WhatsAppCallSignaling { |
| 21 | |
| 22 | /** Our WebRTC transport extension tag (not part of the official WA wire). */ |
| 23 | private const val WEBRTC_TAG = "webrtc" |
| 24 | |
| 25 | /** A parsed inbound call stanza. */ |
| 26 | data class InboundCall( |
| 27 | val callId: String, |
| 28 | val from: String, |
| 29 | val creator: String, |
| 30 | val isVideo: Boolean, |
| 31 | val kind: Kind, |
| 32 | /** The `<enc>` carrying the call key, targeted to our device (null on accept/reject/etc.). */ |
| 33 | val enc: Enc? = null, |
| 34 | /** WebRTC offer/answer SDP carried in our `<webrtc>` extension, if present. */ |
| 35 | val sdp: String? = null, |
| 36 | /** Trickle/gathered ICE candidate lines from our `<webrtc>` extension. */ |
| 37 | val candidates: List<String> = emptyList(), |
| 38 | val stanzaId: String = "", |
| 39 | ) { |
| 40 | /** The kind of a WhatsApp call signalling stanza. */ |
| 41 | enum class Kind { OFFER, PREACCEPT, ACCEPT, REJECT, TERMINATE, RELAY, UNKNOWN } |
| 42 | } |
| 43 | |
| 44 | /** A single Signal `<enc>` (type = pkmsg|msg, ciphertext). */ |
| 45 | data class Enc(val type: String, val ciphertext: ByteArray) |
| 46 | |
| 47 | // ------------------------------------------------------------------ builders |
| 48 | |
| 49 | /** |
| 50 | * Build an outbound call offer. |
| 51 | * @param encs per-device encrypted call-key children (dev jid, enc type, ciphertext). |
| 52 | * @param sdp / [candidates] our WebRTC offer carried in the `<webrtc>` extension. |
| 53 | */ |
| 54 | fun buildOffer( |
| 55 | to: String, |
| 56 | callId: String, |
| 57 | callCreator: String, |
| 58 | video: Boolean, |
| 59 | encs: List<WhatsAppProtocol.ParticipantEnc>, |
| 60 | stanzaId: String, |
| 61 | sdp: String? = null, |
| 62 | candidates: List<String> = emptyList(), |
| 63 | ): Node { |
| 64 | val destinations = encs.map { enc -> |
| 65 | Node( |
| 66 | tag = "to", |
| 67 | attrs = mapOf("jid" to enc.deviceJid), |
| 68 | content = listOf( |
| 69 | Node(tag = "enc", attrs = mapOf("v" to "2", "type" to enc.encType), data = enc.ciphertext), |
| 70 | ), |
| 71 | ) |
| 72 | } |
| 73 | val offer = Node( |
| 74 | tag = "offer", |
| 75 | attrs = mapOf("call-id" to callId, "call-creator" to callCreator), |
| 76 | content = buildList { |
| 77 | add(mediaMarker(video)) |
| 78 | add(Node(tag = "net", attrs = mapOf("medium" to "3"))) |
| 79 | add(Node(tag = "encopt", attrs = mapOf("keygen" to "2"))) |
| 80 | webrtcChild(sdp, candidates)?.let { add(it) } |
| 81 | addAll(destinations) |
| 82 | }, |
| 83 | ) |
| 84 | return call(to, stanzaId, offer) |
| 85 | } |
| 86 | |
| 87 | /** Pre-accept (early, before the user picks up) — lets media/ICE start during ringing. */ |
| 88 | fun buildPreAccept( |
| 89 | to: String, |
| 90 | callId: String, |
| 91 | callCreator: String, |
| 92 | video: Boolean, |
| 93 | stanzaId: String, |
| 94 | sdp: String? = null, |
| 95 | candidates: List<String> = emptyList(), |
| 96 | ): Node { |
| 97 | val preaccept = Node( |
| 98 | tag = "preaccept", |
| 99 | attrs = mapOf("call-id" to callId, "call-creator" to callCreator), |
| 100 | content = buildList { |
| 101 | add(mediaMarker(video)) |
| 102 | webrtcChild(sdp, candidates)?.let { add(it) } |
| 103 | }, |
| 104 | ) |
| 105 | return call(to, stanzaId, preaccept) |
| 106 | } |
| 107 | |
| 108 | /** Accept an inbound call, carrying our WebRTC answer. */ |
| 109 | fun buildAccept( |
| 110 | to: String, |
| 111 | callId: String, |
| 112 | callCreator: String, |
| 113 | stanzaId: String, |
| 114 | video: Boolean = false, |
| 115 | sdp: String? = null, |
| 116 | candidates: List<String> = emptyList(), |
| 117 | ): Node { |
| 118 | val accept = Node( |
| 119 | tag = "accept", |
| 120 | attrs = mapOf("call-id" to callId, "call-creator" to callCreator), |
| 121 | content = buildList { |
| 122 | add(mediaMarker(video)) |
| 123 | webrtcChild(sdp, candidates)?.let { add(it) } |
| 124 | }, |
| 125 | ) |
| 126 | return call(to, stanzaId, accept) |
| 127 | } |
| 128 | |
| 129 | fun buildReject(to: String, callId: String, callCreator: String, stanzaId: String): Node = |
| 130 | call(to, stanzaId, Node(tag = "reject", attrs = mapOf("call-id" to callId, "call-creator" to callCreator))) |
| 131 | |
| 132 | fun buildTerminate(to: String, callId: String, callCreator: String, reason: String, stanzaId: String): Node = |
| 133 | call( |
| 134 | to, stanzaId, |
| 135 | Node( |
| 136 | tag = "terminate", |
| 137 | attrs = mapOf("call-id" to callId, "call-creator" to callCreator, "reason" to reason)), |
| 138 | ) |
| 139 | |
| 140 | /** Trickle a batch of ICE candidates mid-call inside a `<transport>`-like `<webrtc>` node. */ |
| 141 | fun buildIceUpdate( |
| 142 | to: String, |
| 143 | callId: String, |
| 144 | callCreator: String, |
| 145 | stanzaId: String, |
| 146 | candidates: List<String>): Node = |
| 147 | call( |
| 148 | to, stanzaId, |
| 149 | Node( |
| 150 | tag = "relay", |
| 151 | attrs = mapOf("call-id" to callId, "call-creator" to callCreator), |
| 152 | content = listOfNotNull(webrtcChild(null, candidates)), |
| 153 | ), |
| 154 | ) |
| 155 | |
| 156 | // ------------------------------------------------------------------ parse |
| 157 | |
| 158 | /** Parse an inbound `<call>` stanza into a typed [InboundCall]. */ |
| 159 | fun parse(node: Node): InboundCall? { |
| 160 | if (node.tag != "call") return null |
| 161 | val from = node.attrs["from"] ?: node.attrs["participant"] ?: return null |
| 162 | val child = node.getChildren().firstOrNull() ?: return null |
| 163 | val kind = kindFor(child.tag) |
| 164 | val callId = child.attrs["call-id"] ?: node.attrs["id"] ?: "" |
| 165 | val creator = child.attrs["call-creator"] ?: from |
| 166 | val isVideo = child.getChildren().any { it.tag == "video" } |
| 167 | val encNode = child.getChildByTag("enc") |
| 168 | val enc = encNode?.data?.let { Enc(encNode.attrs["type"] ?: "msg", it) } |
| 169 | val webrtc = child.getChildByTag(WEBRTC_TAG) |
| 170 | return InboundCall( |
| 171 | callId = callId, |
| 172 | from = from, |
| 173 | creator = creator, |
| 174 | isVideo = isVideo, |
| 175 | kind = kind, |
| 176 | enc = enc, |
| 177 | sdp = webrtc?.let { extractSdp(it) }, |
| 178 | candidates = webrtc?.let { extractCandidates(it) } ?: emptyList(), |
| 179 | stanzaId = node.attrs["id"] ?: "", |
| 180 | ) |
| 181 | } |
| 182 | |
| 183 | /** Map a call child tag to its kind. */ |
| 184 | private fun kindFor(tag: String): InboundCall.Kind = when (tag) { |
| 185 | "offer" -> InboundCall.Kind.OFFER |
| 186 | "preaccept" -> InboundCall.Kind.PREACCEPT |
| 187 | "accept" -> InboundCall.Kind.ACCEPT |
| 188 | "reject" -> InboundCall.Kind.REJECT |
| 189 | "terminate" -> InboundCall.Kind.TERMINATE |
| 190 | "relay" -> InboundCall.Kind.RELAY |
| 191 | else -> InboundCall.Kind.UNKNOWN |
| 192 | } |
| 193 | |
| 194 | // ------------------------------------------------------------------ webrtc extension helpers |
| 195 | |
| 196 | /** |
| 197 | * Build our `<webrtc>` transport extension: base64 SDP as the `<sdp>` node data plus one |
| 198 | * `<ice>` node per candidate. Returns null when there's nothing to carry. |
| 199 | */ |
| 200 | fun webrtcChild(sdp: String?, candidates: List<String>): Node? { |
| 201 | if (sdp == null && candidates.isEmpty()) return null |
| 202 | val children = buildList { |
| 203 | if (sdp != null) { |
| 204 | add(Node(tag = "sdp", data = Base64.encode(sdp.toByteArray(Charsets.UTF_8), Base64.NO_WRAP))) |
| 205 | } |
| 206 | candidates.forEach { c -> |
| 207 | add(Node(tag = "ice", data = Base64.encode(c.toByteArray(Charsets.UTF_8), Base64.NO_WRAP))) |
| 208 | } |
| 209 | } |
| 210 | return Node(tag = WEBRTC_TAG, content = children) |
| 211 | } |
| 212 | |
| 213 | fun extractSdp(webrtc: Node): String? = |
| 214 | webrtc.getChildByTag("sdp")?.data?.let { String(Base64.decode(it, Base64.NO_WRAP), Charsets.UTF_8) } |
| 215 | |
| 216 | fun extractCandidates(webrtc: Node): List<String> = |
| 217 | webrtc.getChildren().filter { it.tag == "ice" }.mapNotNull { ice -> |
| 218 | ice.data?.let { String(Base64.decode(it, Base64.NO_WRAP), Charsets.UTF_8) } |
| 219 | } |
| 220 | |
| 221 | // ------------------------------------------------------------------ small builders |
| 222 | |
| 223 | private fun mediaMarker(video: Boolean): Node = |
| 224 | if (video) Node(tag = "video") else Node(tag = "audio", attrs = mapOf("enc" to "opus", "rate" to "16000")) |
| 225 | |
| 226 | private fun call(to: String, stanzaId: String, action: Node): Node = |
| 227 | Node(tag = "call", attrs = mapOf("to" to to, "id" to stanzaId), content = listOf(action)) |
| 228 | } |