From b13594009793c1e8f84646622ada7557ec92f8d3 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 22 Jul 2024 16:36:36 +0300 Subject: [PATCH] Fixes #3041 - ElementCall unable to access media on ongoing CallKit session. --- .../Screens/CallScreen/View/CallScreen.swift | 1 + .../ElementCall/ElementCallService.swift | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ElementX/Sources/Screens/CallScreen/View/CallScreen.swift b/ElementX/Sources/Screens/CallScreen/View/CallScreen.swift index 4e679d27f9..d0a8099998 100644 --- a/ElementX/Sources/Screens/CallScreen/View/CallScreen.swift +++ b/ElementX/Sources/Screens/CallScreen/View/CallScreen.swift @@ -88,6 +88,7 @@ private struct WebView: UIViewRepresentable { webView = WKWebView(frame: .zero, configuration: configuration) webView.uiDelegate = self webView.navigationDelegate = self + webView.isInspectable = true // https://stackoverflow.com/a/77963877/730924 webView.allowsLinkPreview = true diff --git a/ElementX/Sources/Services/ElementCall/ElementCallService.swift b/ElementX/Sources/Services/ElementCall/ElementCallService.swift index be1e305a79..35f5db7419 100644 --- a/ElementX/Sources/Services/ElementCall/ElementCallService.swift +++ b/ElementX/Sources/Services/ElementCall/ElementCallService.swift @@ -187,6 +187,20 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) { if let incomingCallID { + // Fixes broken videos on EC web when a CallKit session is established. + // + // Reporting an ongoing call through `reportNewIncomingCall` + `CXAnswerCallAction` + // or `reportOutgoingCall:connectedAt:` will give exclusive access for media to the + // ongoing process, which is different than the WKWebKit is running on, making EC + // unable to aquire media streams. + // Reporting the call as ended imediately after answering it works around that + // as EC gets access to media again and EX builds the right UI in `setupCallSession` + // + // https://github.com/element-hq/element-x-ios/issues/3041 + // https://forums.developer.apple.com/forums/thread/685268 + // https://stackoverflow.com/questions/71483732/webrtc-running-from-wkwebview-avaudiosession-development-roadblock + provider.reportCall(with: incomingCallID.callKitID, endedAt: nil, reason: .answeredElsewhere) + actionsSubject.send(.startCall(roomID: incomingCallID.roomID)) endUnansweredCallTask?.cancel() } else { @@ -210,6 +224,12 @@ class ElementCallService: NSObject, ElementCallServiceProtocol, PKPushRegistryDe } func provider(_ provider: CXProvider, perform action: CXEndCallAction) { + #if targetEnvironment(simulator) + // This gets called for no reason on simulators, where CallKit + // isn't even supported. Ignore + return + #endif + if let ongoingCallID { actionsSubject.send(.endCall(roomID: ongoingCallID.roomID)) }