Move handling of WebRTC messages to PeerConnectionWrapper

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
This commit is contained in:
Daniel Calviño Sánchez 2022-10-17 10:45:38 +02:00
parent f3e04b8e18
commit 4dffd29ceb
2 changed files with 53 additions and 23 deletions

View file

@ -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;

View file

@ -74,6 +74,8 @@ public class PeerConnectionWrapper {
private static final String TAG = PeerConnectionWrapper.class.getCanonicalName();
private final WebRtcMessageListener webRtcMessageListener = new WebRtcMessageListener();
private List<IceCandidate> 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