From 67e259f79257fe5cd7679f872f7546737d2d5fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Tue, 29 Nov 2022 20:49:29 +0100 Subject: [PATCH] Simplify ending the peer connections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The peer connections will be of either "video" or "screen" type, so they can be simply removed based on the session id and an explicit type. Signed-off-by: Daniel Calviño Sánchez --- .../talk/activities/CallActivity.java | 67 +++++++------------ 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java index ee07afe0b..49fcb6d6a 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java @@ -1726,7 +1726,8 @@ public class CallActivity extends CallBaseActivity { peerConnectionIdsToEnd.add(wrapper.getSessionId()); } for (String sessionId : peerConnectionIdsToEnd) { - endPeerConnection(sessionId, false); + endPeerConnection(sessionId, "video"); + endPeerConnection(sessionId, "screen"); } List callParticipantIdsToEnd = new ArrayList(peerConnectionWrapperList.size()); @@ -1835,7 +1836,8 @@ public class CallActivity extends CallBaseActivity { for (Participant participant : participantsInCall) { String sessionId = participant.getSessionId(); Log.d(TAG, " session that will be removed is: " + sessionId); - endPeerConnection(sessionId, false); + endPeerConnection(sessionId, "video"); + endPeerConnection(sessionId, "screen"); removeCallParticipant(sessionId); } @@ -1904,7 +1906,8 @@ public class CallActivity extends CallBaseActivity { for (Participant participant : left) { String sessionId = participant.getSessionId(); Log.d(TAG, " oldSession that will be removed is: " + sessionId); - endPeerConnection(sessionId, false); + endPeerConnection(sessionId, "video"); + endPeerConnection(sessionId, "screen"); removeCallParticipant(sessionId); } } @@ -1918,11 +1921,6 @@ public class CallActivity extends CallBaseActivity { (!isVoiceOnlyCall && (participant.getInCall() & Participant.InCallFlags.WITH_VIDEO) > 0); } - private void deletePeerConnection(PeerConnectionWrapper peerConnectionWrapper) { - peerConnectionWrapper.removePeerConnection(); - peerConnectionWrapperList.remove(peerConnectionWrapper); - } - private PeerConnectionWrapper getPeerConnectionWrapperForSessionIdAndType(String sessionId, String type) { for (PeerConnectionWrapper wrapper : peerConnectionWrapperList) { if (wrapper.getSessionId().equals(sessionId) @@ -2057,42 +2055,27 @@ public class CallActivity extends CallBaseActivity { return callParticipant; } - private List getPeerConnectionWrapperListForSessionId(String sessionId) { - List internalList = new ArrayList<>(); - for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrapperList) { - if (peerConnectionWrapper.getSessionId().equals(sessionId)) { - internalList.add(peerConnectionWrapper); + private void endPeerConnection(String sessionId, String type) { + PeerConnectionWrapper peerConnectionWrapper = getPeerConnectionWrapperForSessionIdAndType(sessionId, type); + if (peerConnectionWrapper == null) { + return; + } + + if (webSocketClient != null && webSocketClient.getSessionId() != null && webSocketClient.getSessionId().equals(sessionId)) { + peerConnectionWrapper.removeObserver(selfPeerConnectionObserver); + } + + CallParticipant callParticipant = callParticipants.get(sessionId); + if (callParticipant != null) { + if ("screen".equals(type)) { + callParticipant.setScreenPeerConnectionWrapper(null); + } else { + callParticipant.setPeerConnectionWrapper(null); } } - return internalList; - } - - private void endPeerConnection(String sessionId, boolean justScreen) { - List peerConnectionWrappers; - if (!(peerConnectionWrappers = getPeerConnectionWrapperListForSessionId(sessionId)).isEmpty()) { - for (PeerConnectionWrapper peerConnectionWrapper : peerConnectionWrappers) { - if (peerConnectionWrapper.getSessionId().equals(sessionId)) { - if (webSocketClient != null && webSocketClient.getSessionId() != null && webSocketClient.getSessionId().equals(sessionId)) { - peerConnectionWrapper.removeObserver(selfPeerConnectionObserver); - } - - String videoStreamType = peerConnectionWrapper.getVideoStreamType(); - if (VIDEO_STREAM_TYPE_SCREEN.equals(videoStreamType) || !justScreen) { - CallParticipant callParticipant = callParticipants.get(sessionId); - if (callParticipant != null) { - if ("screen".equals(videoStreamType)) { - callParticipant.setScreenPeerConnectionWrapper(null); - } else { - callParticipant.setPeerConnectionWrapper(null); - } - } - - deletePeerConnection(peerConnectionWrapper); - } - } - } - } + peerConnectionWrapper.removePeerConnection(); + peerConnectionWrapperList.remove(peerConnectionWrapper); } private void removeCallParticipant(String sessionId) { @@ -2599,7 +2582,7 @@ public class CallActivity extends CallBaseActivity { @Override public void onUnshareScreen() { - endPeerConnection(sessionId, true); + endPeerConnection(sessionId, "screen"); } }