VoIP: check for support of PSTN protocol

This commit is contained in:
ganfra 2021-01-07 12:20:50 +01:00
parent d09a6714af
commit 201a346d41
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.call.webrtc
import kotlinx.coroutines.delay
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.model.thirdparty.ThirdPartyProtocol
import org.matrix.android.sdk.internal.util.awaitCallback
private const val PSTN_VECTOR_KEY = "im.vector.protocol.pstn"
private const val PSTN_MATRIX_KEY = "m.protocol.pstn"
suspend fun Session.supportPSTN(maxTries: Int): Boolean {
val thirdPartyProtocols: Map<String, ThirdPartyProtocol> = try {
awaitCallback {
getThirdPartyProtocol(it)
}
} catch (failure: Throwable) {
if (maxTries == 1) {
return false
} else {
// Wait for 10s before trying again
delay(10_000L)
return supportPSTN(maxTries - 1)
}
} ?: return false
return thirdPartyProtocols.containsKey(PSTN_VECTOR_KEY) || thirdPartyProtocols.containsKey(PSTN_MATRIX_KEY)
}

View File

@ -28,7 +28,10 @@ import im.vector.app.features.call.CallAudioManager
import im.vector.app.features.call.VectorCallActivity
import im.vector.app.features.call.utils.EglUtils
import im.vector.app.push.fcm.FcmHelper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.call.CallListener
@ -67,6 +70,19 @@ class WebRtcCallManager @Inject constructor(
fun onAudioDevicesChange() {}
}
interface PSTNSupportListener {
fun onPSTNSupportUpdated()
}
private val pstnSupportListeners = emptyList<PSTNSupportListener>().toMutableList()
fun addPstnSupportListener(listener: PSTNSupportListener) {
pstnSupportListeners.add(listener)
}
fun removePstnSupportListener(listener: PSTNSupportListener) {
pstnSupportListeners.remove(listener)
}
private val currentCallsListeners = emptyList<CurrentCallListener>().toMutableList()
fun addCurrentCallListener(listener: CurrentCallListener) {
currentCallsListeners.add(listener)
@ -85,11 +101,22 @@ class WebRtcCallManager @Inject constructor(
private var peerConnectionFactory: PeerConnectionFactory? = null
private val executor = Executors.newSingleThreadExecutor()
private val dispatcher = executor.asCoroutineDispatcher()
var supportsPSTNProtocol: Boolean = false
private set
private val rootEglBase by lazy { EglUtils.rootEglBase }
private var isInBackground: Boolean = true
init {
GlobalScope.launch {
supportsPSTNProtocol = currentSession?.supportPSTN(3).orFalse()
if (supportsPSTNProtocol) {
pstnSupportListeners.forEach { it.onPSTNSupportUpdated() }
}
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun entersForeground() {
isInBackground = false