Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / call / SignalGroupCallManager.kt
SignalGroupCallManager.kt232 lines · 8.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal.call |
| 2 | |
| 3 | import androidx.core.util.size |
| 4 | import android.content.Context |
| 5 | import com.vayunmathur.library.log.Log |
| 6 | import kotlinx.coroutines.CoroutineScope |
| 7 | import kotlinx.coroutines.Dispatchers |
| 8 | import kotlinx.coroutines.SupervisorJob |
| 9 | import kotlinx.coroutines.launch |
| 10 | import org.signal.ringrtc.CallManager |
| 11 | import org.signal.ringrtc.CallSummary |
| 12 | import org.signal.ringrtc.GroupCall |
| 13 | import org.webrtc.EglBase |
| 14 | import java.util.UUID |
| 15 | |
| 16 | /** |
| 17 | * A Signal group call, via RingRTC's SFU client. |
| 18 | * |
| 19 | * Unlike a 1:1 call there is no offer/answer over the chat channel: everyone connects to Signal's calling |
| 20 | * server, and the group's identity is proved with a short-lived membership token rather than by naming the |
| 21 | * participants. The SFU never learns who is in the group — RingRTC matches SFU-reported participants back to |
| 22 | * people using each member's ACI paired with its ciphertext. |
| 23 | * |
| 24 | * RingRTC asks for the proof and the member list rather than being given them up front |
| 25 | * ([GroupCall.Observer.requestMembershipProof] / [requestGroupMembers]), because both expire. |
| 26 | */ |
| 27 | class SignalGroupCallManager( |
| 28 | private val appContext: Context, |
| 29 | private val callManager: CallManager, |
| 30 | private val eglBase: EglBase, |
| 31 | private val signaling: Signaling, |
| 32 | ) : GroupCall.Observer { |
| 33 | |
| 34 | /** What the group call needs from the app, and what it reports back. */ |
| 35 | interface Signaling { |
| 36 | /** A fresh membership proof for [groupId], or null if one cannot be obtained. */ |
| 37 | suspend fun membershipProof(groupId: ByteArray): ByteArray? |
| 38 | |
| 39 | /** The group's members, as ACI/ciphertext pairs. */ |
| 40 | suspend fun groupMembers(groupId: ByteArray): List<Pair<UUID, ByteArray>> |
| 41 | |
| 42 | fun onGroupCallStateChanged(groupId: ByteArray, joined: Boolean, participants: Int) |
| 43 | |
| 44 | fun onGroupCallEnded(groupId: ByteArray, reason: CallManager.CallEndReason?) |
| 45 | } |
| 46 | |
| 47 | private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO) |
| 48 | |
| 49 | private var groupCall: GroupCall? = null |
| 50 | private var groupId: ByteArray? = null |
| 51 | |
| 52 | val localVideoSink = SwappableVideoSink() |
| 53 | |
| 54 | /** True while a group call object exists, whether or not we have joined the media session. */ |
| 55 | val isActive: Boolean get() = groupCall != null |
| 56 | |
| 57 | /** |
| 58 | * Create and connect a group call. [join] happens separately so the UI can show who is already on the call |
| 59 | * before committing to being seen and heard. |
| 60 | */ |
| 61 | fun connect(groupIdentifier: ByteArray): Boolean { |
| 62 | if (groupCall != null) { |
| 63 | Log.status(TAG, "a group call is already active") |
| 64 | return false |
| 65 | } |
| 66 | return try { |
| 67 | val call = callManager.createGroupCall( |
| 68 | groupIdentifier, |
| 69 | SFU_URL, |
| 70 | // No extra HKDF info: the group's own key material is the whole input. |
| 71 | ByteArray(0), |
| 72 | AUDIO_LEVEL_INTERVAL_MS, |
| 73 | null, |
| 74 | org.signal.ringrtc.AudioConfig(), |
| 75 | null, |
| 76 | this, |
| 77 | ) ?: return false |
| 78 | groupCall = call |
| 79 | groupId = groupIdentifier |
| 80 | call.connect() |
| 81 | Log.status(TAG, "connecting to the group call for ${groupIdentifier.toHex()}") |
| 82 | true |
| 83 | } catch (expected: Throwable) { |
| 84 | Log.status(TAG, "could not create the group call", expected) |
| 85 | groupCall = null |
| 86 | groupId = null |
| 87 | false |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** Join the media session, so we can be heard. */ |
| 92 | fun join() { |
| 93 | val call = groupCall ?: return |
| 94 | try { |
| 95 | call.setOutgoingVideoSource(localVideoSink, NoCameraControl) |
| 96 | call.join() |
| 97 | } catch (expected: Throwable) { |
| 98 | Log.status(TAG, "could not join the group call", expected) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | fun leave() { |
| 103 | val call = groupCall ?: return |
| 104 | try { |
| 105 | call.leave() |
| 106 | call.disconnect() |
| 107 | } catch (expected: Throwable) { |
| 108 | Log.status(TAG, "could not leave the group call", expected) |
| 109 | } finally { |
| 110 | groupCall = null |
| 111 | groupId = null |
| 112 | localVideoSink.attach(null) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fun setAudioMuted(muted: Boolean) { |
| 117 | try { |
| 118 | groupCall?.setOutgoingAudioMuted(muted) |
| 119 | } catch (expected: Throwable) { |
| 120 | Log.status(TAG, "could not change the group call mute state", expected) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fun setVideoMuted(muted: Boolean, isScreenShare: Boolean = false) { |
| 125 | try { |
| 126 | groupCall?.setOutgoingVideoMuted(muted, isScreenShare) |
| 127 | } catch (expected: Throwable) { |
| 128 | Log.status(TAG, "could not change the group call video state", expected) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // -- GroupCall.Observer. Both request callbacks must be answered or the call never establishes. -- |
| 133 | |
| 134 | override fun requestMembershipProof(groupCall: GroupCall) { |
| 135 | val id = groupId ?: return |
| 136 | scope.launch { |
| 137 | val proof = signaling.membershipProof(id) |
| 138 | if (proof == null) { |
| 139 | Log.status(TAG, "no membership proof; the SFU will refuse this call") |
| 140 | return@launch |
| 141 | } |
| 142 | try { |
| 143 | groupCall.setMembershipProof(proof) |
| 144 | } catch (expected: Throwable) { |
| 145 | Log.status(TAG, "could not set the membership proof", expected) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | override fun requestGroupMembers(groupCall: GroupCall) { |
| 151 | val id = groupId ?: return |
| 152 | scope.launch { |
| 153 | val members = signaling.groupMembers(id) |
| 154 | if (members.isEmpty()) { |
| 155 | Log.status(TAG, "no group members; participants cannot be identified") |
| 156 | return@launch |
| 157 | } |
| 158 | try { |
| 159 | groupCall.setGroupMembers( |
| 160 | members.map { (uuid, ciphertext) -> GroupCall.GroupMemberInfo(uuid, ciphertext) }, |
| 161 | ) |
| 162 | } catch (expected: Throwable) { |
| 163 | Log.status(TAG, "could not set the group members", expected) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | override fun onLocalDeviceStateChanged(groupCall: GroupCall) { |
| 169 | val id = groupId ?: return |
| 170 | val state = try { groupCall.localDeviceState } catch (_: Throwable) { null } |
| 171 | val joined = state?.joinState == GroupCall.JoinState.JOINED |
| 172 | val participants = try { groupCall.remoteDeviceStates?.size ?: 0 } catch (_: Throwable) { 0 } |
| 173 | Log.status(TAG, "group call state: joinState=${state?.joinState} participants=$participants") |
| 174 | signaling.onGroupCallStateChanged(id, joined, participants) |
| 175 | } |
| 176 | |
| 177 | override fun onRemoteDeviceStatesChanged(groupCall: GroupCall) { |
| 178 | val id = groupId ?: return |
| 179 | val participants = try { groupCall.remoteDeviceStates?.size ?: 0 } catch (_: Throwable) { 0 } |
| 180 | val joined = try { |
| 181 | groupCall.localDeviceState?.joinState == GroupCall.JoinState.JOINED |
| 182 | } catch (_: Throwable) { |
| 183 | false |
| 184 | } |
| 185 | signaling.onGroupCallStateChanged(id, joined, participants) |
| 186 | } |
| 187 | |
| 188 | /** Who is already on the call, before joining. */ |
| 189 | override fun onPeekChanged(groupCall: GroupCall) { |
| 190 | val peek = try { groupCall.peekInfo } catch (_: Throwable) { null } |
| 191 | Log.status(TAG, "group call peek: devices=${peek?.deviceCount} creator=${peek?.creator}") |
| 192 | } |
| 193 | |
| 194 | override fun onEnded( |
| 195 | groupCall: GroupCall, |
| 196 | reason: CallManager.CallEndReason, |
| 197 | summary: CallSummary, |
| 198 | ) { |
| 199 | val id = groupId |
| 200 | Log.status(TAG, "group call ended: $reason") |
| 201 | this.groupCall = null |
| 202 | this.groupId = null |
| 203 | localVideoSink.attach(null) |
| 204 | if (id != null) signaling.onGroupCallEnded(id, reason) |
| 205 | } |
| 206 | |
| 207 | // Media-detail callbacks the UI does not surface yet. Explicit rather than absent, so it is clear they were |
| 208 | // considered and are simply not wired. |
| 209 | override fun onAudioLevels(groupCall: GroupCall) = Unit |
| 210 | |
| 211 | override fun onSpeakingNotification(groupCall: GroupCall, event: GroupCall.SpeechEvent?) = Unit |
| 212 | |
| 213 | override fun onLowBandwidthForVideo(groupCall: GroupCall, recovered: Boolean) = Unit |
| 214 | |
| 215 | override fun onReactions(groupCall: GroupCall, reactions: List<GroupCall.Reaction>?) = Unit |
| 216 | |
| 217 | override fun onRaisedHands(groupCall: GroupCall, raisedHands: List<Long>?) = Unit |
| 218 | |
| 219 | override fun onRemoteMuteRequest(groupCall: GroupCall, sourceDemuxId: Long) = Unit |
| 220 | |
| 221 | override fun onObservedRemoteMute(groupCall: GroupCall, sourceDemuxId: Long, targetDemuxId: Long) = Unit |
| 222 | |
| 223 | private fun ByteArray.toHex(): String = |
| 224 | take(HEX_PREFIX_BYTES).joinToString("") { "%02x".format(it) } |
| 225 | |
| 226 | private companion object { |
| 227 | const val TAG = "SignalGroupCall" |
| 228 | const val AUDIO_LEVEL_INTERVAL_MS = 200 |
| 229 | const val SFU_URL = "https://sfu.voip.signal.org" |
| 230 | const val HEX_PREFIX_BYTES = 8 |
| 231 | } |
| 232 | } |