diff --git a/CHANGES.md b/CHANGES.md index d29c3d3ecf..34a8930291 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -26,6 +26,7 @@ Bugfix 🐛: - Highlighted Event when opening a permalink from another room (#1033) - A Kick appears has "someone has made no change" (#1959) - Avoid NetworkOnMainThreadException when setting a user avatar + - Renew turnserver credentials when ttl runs out Translations 🗣: - Add PlayStore description resources in the Triple-T format, to let Weblate handle them diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/call/DefaultCallSignalingService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/call/DefaultCallSignalingService.kt index 0c1a129733..6a17f0e925 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/call/DefaultCallSignalingService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/call/DefaultCallSignalingService.kt @@ -58,18 +58,32 @@ internal class DefaultCallSignalingService @Inject constructor( private val activeCalls = mutableListOf() - private var cachedTurnServerResponse: TurnServerResponse? = null + private val cachedTurnServerResponse = object { + + private val MIN_TTL = 60 + + private val now = { System.currentTimeMillis() / 1000 } + + private var expiresAt: Long = 0 + + var data: TurnServerResponse? = null + get() = if (expiresAt > now()) field else null + set(value) { + expiresAt = now() + (value?.ttl ?: 0) - MIN_TTL + field = value + } + } override fun getTurnServer(callback: MatrixCallback): Cancelable { - if (cachedTurnServerResponse != null) { - cachedTurnServerResponse?.let { callback.onSuccess(it) } + if (cachedTurnServerResponse.data != null) { + cachedTurnServerResponse.data?.let { callback.onSuccess(it) } return NoOpCancellable } return turnServerTask .configureWith(GetTurnServerTask.Params) { this.callback = object : MatrixCallback { override fun onSuccess(data: TurnServerResponse) { - cachedTurnServerResponse = data + cachedTurnServerResponse.data = data callback.onSuccess(data) }