From 52082a9defdb9b836a7261d6ece74217709764c9 Mon Sep 17 00:00:00 2001
From: Benoit Marty <benoit@matrix.org>
Date: Fri, 6 Oct 2023 10:41:15 +0200
Subject: [PATCH] Ensure the incoming call will not ring forever, in case the
 call is not ended by another way (#8178) Add a safe 2 minutes timer.

---
 changelog.d/8178.bugfix                                |  1 +
 .../app/features/call/webrtc/WebRtcCallManager.kt      | 10 ++++++++++
 2 files changed, 11 insertions(+)
 create mode 100644 changelog.d/8178.bugfix

diff --git a/changelog.d/8178.bugfix b/changelog.d/8178.bugfix
new file mode 100644
index 0000000000..e7f073f4fc
--- /dev/null
+++ b/changelog.d/8178.bugfix
@@ -0,0 +1 @@
+Ensure the incoming call will not ring forever, in case the call is not ended by another way.
diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
index 074779c6cc..1eb4134a87 100644
--- a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
+++ b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCallManager.kt
@@ -34,6 +34,8 @@ import im.vector.app.features.call.vectorCallService
 import im.vector.app.features.session.coroutineScope
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.asCoroutineDispatcher
+import kotlinx.coroutines.delay
+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.logger.LoggerTag
@@ -386,6 +388,14 @@ class WebRtcCallManager @Inject constructor(
                 // Maybe increase sync freq? but how to set back to default values?
             }
         }
+
+        // ensure the incoming call will not ring forever
+        sessionScope?.launch {
+            delay(2 * 60 * 1000 /* 2 minutes */)
+            if (mxCall.state is CallState.LocalRinging) {
+                onCallEnded(mxCall.callId, EndCallReason.INVITE_TIMEOUT, rejected = false)
+            }
+        }
     }
 
     override fun onCallAnswerReceived(callAnswerContent: CallAnswerContent) {