From baa4b95e18e8cbe28570cf64ac7491d8e6a16eaf Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 14 May 2021 14:35:08 +0200 Subject: [PATCH] Remove usage of GlobalScope --- .../app/features/call/webrtc/WebRtcCall.kt | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt index fcd6bf0a77..a3a1a29c4b 100644 --- a/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt +++ b/vector/src/main/java/im/vector/app/features/call/webrtc/WebRtcCall.kt @@ -33,11 +33,12 @@ import im.vector.app.features.call.utils.awaitCreateOffer import im.vector.app.features.call.utils.awaitSetLocalDescription import im.vector.app.features.call.utils.awaitSetRemoteDescription import im.vector.app.features.call.utils.mapToCallCandidate +import im.vector.app.features.session.coroutineScope import io.reactivex.disposables.Disposable import io.reactivex.subjects.PublishSubject import io.reactivex.subjects.ReplaySubject +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -103,6 +104,9 @@ class WebRtcCall(val mxCall: MxCall, private val listeners = CopyOnWriteArrayList() + private val sessionScope: CoroutineScope? + get() = sessionProvider.get()?.coroutineScope + fun addListener(listener: Listener) { listeners.add(listener) } @@ -191,7 +195,7 @@ class WebRtcCall(val mxCall: MxCall, fun onIceCandidate(iceCandidate: IceCandidate) = iceCandidateSource.onNext(iceCandidate) fun onRenegotiationNeeded(restartIce: Boolean) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { if (mxCall.state != CallState.CreateOffer && mxCall.opponentVersion == 0) { Timber.v("Opponent does not support renegotiation: ignoring onRenegotiationNeeded event") return@launch @@ -262,7 +266,7 @@ class WebRtcCall(val mxCall: MxCall, localSurfaceRenderers.addIfNeeded(localViewRenderer) remoteSurfaceRenderers.addIfNeeded(remoteViewRenderer) - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { when (mode) { VectorCallActivity.INCOMING_ACCEPT -> { internalAcceptIncomingCall() @@ -283,7 +287,7 @@ class WebRtcCall(val mxCall: MxCall, } fun acceptIncomingCall() { - GlobalScope.launch { + sessionScope?.launch { Timber.v("## VOIP acceptIncomingCall from state ${mxCall.state}") if (mxCall.state == CallState.LocalRinging) { internalAcceptIncomingCall() @@ -564,7 +568,7 @@ class WebRtcCall(val mxCall: MxCall, } fun updateRemoteOnHold(onHold: Boolean) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { if (remoteOnHold == onHold) return@launch val direction: RtpTransceiver.RtpTransceiverDirection if (onHold) { @@ -688,7 +692,7 @@ class WebRtcCall(val mxCall: MxCall, } fun onAddStream(stream: MediaStream) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { // reportError("Weird-looking stream: " + stream); if (stream.audioTracks.size > 1 || stream.videoTracks.size > 1) { Timber.e("## VOIP StreamObserver weird looking stream: $stream") @@ -712,7 +716,7 @@ class WebRtcCall(val mxCall: MxCall, } fun onRemoveStream() { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { remoteSurfaceRenderers .mapNotNull { it.get() } .forEach { remoteVideoTrack?.removeSink(it) } @@ -734,7 +738,7 @@ class WebRtcCall(val mxCall: MxCall, } val wasRinging = mxCall.state is CallState.LocalRinging mxCall.state = CallState.Terminated - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { release() } onCallEnded(callId) @@ -750,7 +754,7 @@ class WebRtcCall(val mxCall: MxCall, // Call listener fun onCallIceCandidateReceived(iceCandidatesContent: CallCandidatesContent) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { iceCandidatesContent.candidates.forEach { if (it.sdpMid.isNullOrEmpty() || it.candidate.isNullOrEmpty()) { return@forEach @@ -763,7 +767,7 @@ class WebRtcCall(val mxCall: MxCall, } fun onCallAnswerReceived(callAnswerContent: CallAnswerContent) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { Timber.v("## VOIP onCallAnswerReceived ${callAnswerContent.callId}") val sdp = SessionDescription(SessionDescription.Type.ANSWER, callAnswerContent.answer.sdp) try { @@ -779,7 +783,7 @@ class WebRtcCall(val mxCall: MxCall, } fun onCallNegotiateReceived(callNegotiateContent: CallNegotiateContent) { - GlobalScope.launch(dispatcher) { + sessionScope?.launch(dispatcher) { val description = callNegotiateContent.description val type = description?.type val sdpText = description?.sdp