From 4dffd29ceba62166390226165223137af3565d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 17 Oct 2022 10:45:38 +0200 Subject: [PATCH] Move handling of WebRTC messages to PeerConnectionWrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- .../talk/activities/CallActivity.java | 33 +++++--------- .../talk/webrtc/PeerConnectionWrapper.java | 43 +++++++++++++++++++ 2 files changed, 53 insertions(+), 23 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 5f04f4920..33528f640 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java @@ -116,14 +116,12 @@ import org.webrtc.CameraVideoCapturer; import org.webrtc.DefaultVideoDecoderFactory; import org.webrtc.DefaultVideoEncoderFactory; import org.webrtc.EglBase; -import org.webrtc.IceCandidate; import org.webrtc.Logging; import org.webrtc.MediaConstraints; import org.webrtc.MediaStream; import org.webrtc.PeerConnection; import org.webrtc.PeerConnectionFactory; import org.webrtc.RendererCommon; -import org.webrtc.SessionDescription; import org.webrtc.SurfaceTextureHelper; import org.webrtc.VideoCapturer; import org.webrtc.VideoSource; @@ -1688,35 +1686,24 @@ public class CallActivity extends CallBaseActivity { return; } + String sdp = ncSignalingMessage.getPayload().getSdp(); + String nick = ncSignalingMessage.getPayload().getNick(); + switch (type) { case "offer": + peerConnectionWrapper.getWebRtcMessageListener().onOffer(sdp, nick); + break; case "answer": - peerConnectionWrapper.setNick(ncSignalingMessage.getPayload().getNick()); - SessionDescription sessionDescriptionWithPreferredCodec; - - String sessionDescriptionStringWithPreferredCodec = MagicWebRTCUtils.preferCodec - (ncSignalingMessage.getPayload().getSdp(), - "H264", false); - - sessionDescriptionWithPreferredCodec = new SessionDescription( - SessionDescription.Type.fromCanonicalForm(type), - sessionDescriptionStringWithPreferredCodec); - - if (peerConnectionWrapper.getPeerConnection() != null) { - peerConnectionWrapper.getPeerConnection().setRemoteDescription( - peerConnectionWrapper.getMagicSdpObserver(), - sessionDescriptionWithPreferredCodec); - } + peerConnectionWrapper.getWebRtcMessageListener().onAnswer(sdp, nick); break; case "candidate": NCIceCandidate ncIceCandidate = ncSignalingMessage.getPayload().getIceCandidate(); - IceCandidate iceCandidate = new IceCandidate(ncIceCandidate.getSdpMid(), - ncIceCandidate.getSdpMLineIndex(), - ncIceCandidate.getCandidate()); - peerConnectionWrapper.addCandidate(iceCandidate); + peerConnectionWrapper.getWebRtcMessageListener().onCandidate(ncIceCandidate.getSdpMid(), + ncIceCandidate.getSdpMLineIndex(), + ncIceCandidate.getCandidate()); break; case "endOfCandidates": - peerConnectionWrapper.drainIceCandidates(); + peerConnectionWrapper.getWebRtcMessageListener().onEndOfCandidates(); break; default: break; diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/PeerConnectionWrapper.java b/app/src/main/java/com/nextcloud/talk/webrtc/PeerConnectionWrapper.java index f0597f5a7..ad0a9744a 100644 --- a/app/src/main/java/com/nextcloud/talk/webrtc/PeerConnectionWrapper.java +++ b/app/src/main/java/com/nextcloud/talk/webrtc/PeerConnectionWrapper.java @@ -74,6 +74,8 @@ public class PeerConnectionWrapper { private static final String TAG = PeerConnectionWrapper.class.getCanonicalName(); + private final WebRtcMessageListener webRtcMessageListener = new WebRtcMessageListener(); + private List iceCandidates = new ArrayList<>(); private PeerConnection peerConnection; private String sessionId; @@ -267,6 +269,47 @@ public class PeerConnectionWrapper { return false; } + public WebRtcMessageListener getWebRtcMessageListener() { + return webRtcMessageListener; + } + + public class WebRtcMessageListener { + + public void onOffer(String sdp, String nick) { + onOfferOrAnswer("offer", sdp, nick); + } + + public void onAnswer(String sdp, String nick) { + onOfferOrAnswer("answer", sdp, nick); + } + + private void onOfferOrAnswer(String type, String sdp, String nick) { + setNick(nick); + + SessionDescription sessionDescriptionWithPreferredCodec; + + boolean isAudio = false; + String sessionDescriptionStringWithPreferredCodec = MagicWebRTCUtils.preferCodec(sdp, "H264", isAudio); + + sessionDescriptionWithPreferredCodec = new SessionDescription( + SessionDescription.Type.fromCanonicalForm(type), + sessionDescriptionStringWithPreferredCodec); + + if (getPeerConnection() != null) { + getPeerConnection().setRemoteDescription(magicSdpObserver, sessionDescriptionWithPreferredCodec); + } + } + + public void onCandidate(String sdpMid, int sdpMLineIndex, String sdp) { + IceCandidate iceCandidate = new IceCandidate(sdpMid, sdpMLineIndex, sdp); + addCandidate(iceCandidate); + } + + public void onEndOfCandidates() { + drainIceCandidates(); + } + } + private class MagicDataChannelObserver implements DataChannel.Observer { @Override