|
@@ -30,7 +30,6 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
|
|
|
import com.nextcloud.talk.events.MediaStreamEvent;
|
|
|
import com.nextcloud.talk.events.PeerConnectionEvent;
|
|
|
import com.nextcloud.talk.events.SessionDescriptionSendEvent;
|
|
|
-import com.nextcloud.talk.events.WebSocketCommunicationEvent;
|
|
|
import com.nextcloud.talk.models.json.signaling.DataChannelMessage;
|
|
|
import com.nextcloud.talk.models.json.signaling.NCIceCandidate;
|
|
|
|
|
@@ -51,14 +50,13 @@ import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
|
|
|
-import androidx.annotation.Nullable;
|
|
|
-
|
|
|
public class MagicPeerConnectionWrapper {
|
|
|
private static String TAG = "MagicPeerConnectionWrapper";
|
|
|
- private List<IceCandidate> iceCandidates = new ArrayList<>();
|
|
|
+ List<IceCandidate> iceCandidates = new ArrayList<>();
|
|
|
private PeerConnection peerConnection;
|
|
|
private String sessionId;
|
|
|
private String nick;
|
|
|
+ private String userId;
|
|
|
private MediaConstraints mediaConstraints;
|
|
|
private DataChannel magicDataChannel;
|
|
|
private MagicSdpObserver magicSdpObserver;
|
|
@@ -75,7 +73,7 @@ public class MagicPeerConnectionWrapper {
|
|
|
public MagicPeerConnectionWrapper(PeerConnectionFactory peerConnectionFactory,
|
|
|
List<PeerConnection.IceServer> iceServerList,
|
|
|
MediaConstraints mediaConstraints,
|
|
|
- String sessionId, String localSession, @Nullable MediaStream mediaStream,
|
|
|
+ String sessionId, String localSession, MediaStream mediaStream,
|
|
|
boolean isMCUPublisher, boolean hasMCU) {
|
|
|
|
|
|
this.localMediaStream = mediaStream;
|
|
@@ -85,28 +83,21 @@ public class MagicPeerConnectionWrapper {
|
|
|
|
|
|
magicSdpObserver = new MagicSdpObserver();
|
|
|
hasInitiated = sessionId.compareTo(localSession) < 0;
|
|
|
- this.isMCUPublisher = isMCUPublisher;
|
|
|
|
|
|
peerConnection = peerConnectionFactory.createPeerConnection(iceServerList, mediaConstraints,
|
|
|
new MagicPeerConnectionObserver());
|
|
|
|
|
|
+ this.isMCUPublisher = isMCUPublisher;
|
|
|
+
|
|
|
if (peerConnection != null) {
|
|
|
- if (localSession != null && localMediaStream != null) {
|
|
|
- peerConnection.addStream(localMediaStream);
|
|
|
- }
|
|
|
+ peerConnection.addStream(localMediaStream);
|
|
|
|
|
|
- if (hasMCU || hasInitiated) {
|
|
|
+ if (hasInitiated) {
|
|
|
DataChannel.Init init = new DataChannel.Init();
|
|
|
init.negotiated = false;
|
|
|
magicDataChannel = peerConnection.createDataChannel("status", init);
|
|
|
magicDataChannel.registerObserver(new MagicDataChannelObserver());
|
|
|
- if (isMCUPublisher) {
|
|
|
- peerConnection.createOffer(magicSdpObserver, mediaConstraints);
|
|
|
- } else if (hasMCU) {
|
|
|
- HashMap<String, String> hashMap = new HashMap<>();
|
|
|
- hashMap.put("sessionId", sessionId);
|
|
|
- EventBus.getDefault().post(new WebSocketCommunicationEvent("peerReadyForRequestingOffer", hashMap));
|
|
|
- }
|
|
|
+ peerConnection.createOffer(magicSdpObserver, mediaConstraints);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -118,10 +109,7 @@ public class MagicPeerConnectionWrapper {
|
|
|
}
|
|
|
|
|
|
if (peerConnection != null) {
|
|
|
- if (localMediaStream != null) {
|
|
|
- peerConnection.removeStream(localMediaStream);
|
|
|
- }
|
|
|
-
|
|
|
+ peerConnection.removeStream(localMediaStream);
|
|
|
peerConnection.close();
|
|
|
peerConnection = null;
|
|
|
}
|
|
@@ -187,18 +175,16 @@ public class MagicPeerConnectionWrapper {
|
|
|
}
|
|
|
|
|
|
private void sendInitialMediaStatus() {
|
|
|
- if (localMediaStream != null) {
|
|
|
- if (localMediaStream.videoTracks.size() == 1 && localMediaStream.videoTracks.get(0).enabled()) {
|
|
|
- sendChannelData(new DataChannelMessage("videoOn"));
|
|
|
- } else {
|
|
|
- sendChannelData(new DataChannelMessage("videoOff"));
|
|
|
- }
|
|
|
+ if (localMediaStream.videoTracks.size() == 1 && localMediaStream.videoTracks.get(0).enabled()) {
|
|
|
+ sendChannelData(new DataChannelMessage("videoOn"));
|
|
|
+ } else {
|
|
|
+ sendChannelData(new DataChannelMessage("videoOff"));
|
|
|
+ }
|
|
|
|
|
|
- if (localMediaStream.audioTracks.size() == 1 && localMediaStream.audioTracks.get(0).enabled()) {
|
|
|
- sendChannelData(new DataChannelMessage("audioOn"));
|
|
|
- } else {
|
|
|
- sendChannelData(new DataChannelMessage("audioOff"));
|
|
|
- }
|
|
|
+ if (localMediaStream.audioTracks.size() == 1 && localMediaStream.audioTracks.get(0).enabled()) {
|
|
|
+ sendChannelData(new DataChannelMessage("audioOn"));
|
|
|
+ } else {
|
|
|
+ sendChannelData(new DataChannelMessage("audioOff"));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -233,8 +219,8 @@ public class MagicPeerConnectionWrapper {
|
|
|
try {
|
|
|
DataChannelMessage dataChannelMessage = LoganSquare.parse(strData, DataChannelMessage.class);
|
|
|
|
|
|
- String internalNick;
|
|
|
if ("nickChanged".equals(dataChannelMessage.getType())) {
|
|
|
+ String internalNick;
|
|
|
if (dataChannelMessage.getPayload() instanceof String) {
|
|
|
internalNick = (String) dataChannelMessage.getPayload();
|
|
|
if (!internalNick.equals(nick)) {
|
|
@@ -247,7 +233,6 @@ public class MagicPeerConnectionWrapper {
|
|
|
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
|
|
|
.NICK_CHANGE, payloadHashMap.get("userid"), payloadHashMap.get("name"), null));
|
|
|
}
|
|
|
-
|
|
|
} else if ("audioOn".equals(dataChannelMessage.getType())) {
|
|
|
remoteAudioOn = true;
|
|
|
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
|
|
@@ -283,15 +268,11 @@ public class MagicPeerConnectionWrapper {
|
|
|
if (iceConnectionState.equals(PeerConnection.IceConnectionState.CONNECTED)) {
|
|
|
/*EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
|
|
|
.PEER_CONNECTED, sessionId, null, null));*/
|
|
|
-
|
|
|
- if (!isMCUPublisher) {
|
|
|
- EventBus.getDefault().post(new MediaStreamEvent(remoteMediaStream, sessionId));
|
|
|
- }
|
|
|
+ EventBus.getDefault().post(new MediaStreamEvent(remoteMediaStream, sessionId));
|
|
|
|
|
|
if (hasInitiated) {
|
|
|
sendInitialMediaStatus();
|
|
|
}
|
|
|
-
|
|
|
} else if (iceConnectionState.equals(PeerConnection.IceConnectionState.CLOSED)) {
|
|
|
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
|
|
|
.PEER_CLOSED, sessionId, null, null));
|
|
@@ -330,9 +311,7 @@ public class MagicPeerConnectionWrapper {
|
|
|
|
|
|
@Override
|
|
|
public void onRemoveStream(MediaStream mediaStream) {
|
|
|
- if (!isMCUPublisher) {
|
|
|
- EventBus.getDefault().post(new MediaStreamEvent(null, sessionId));
|
|
|
- }
|
|
|
+ EventBus.getDefault().post(new MediaStreamEvent(null, sessionId));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -368,18 +347,16 @@ public class MagicPeerConnectionWrapper {
|
|
|
|
|
|
@Override
|
|
|
public void onCreateSuccess(SessionDescription sessionDescription) {
|
|
|
- SessionDescription sessionDescriptionWithPreferredCodec;
|
|
|
String sessionDescriptionStringWithPreferredCodec = MagicWebRTCUtils.preferCodec
|
|
|
(sessionDescription.description,
|
|
|
"VP8", false);
|
|
|
- sessionDescriptionWithPreferredCodec = new SessionDescription(
|
|
|
+
|
|
|
+ SessionDescription sessionDescriptionWithPreferredCodec = new SessionDescription(
|
|
|
sessionDescription.type,
|
|
|
sessionDescriptionStringWithPreferredCodec);
|
|
|
|
|
|
-
|
|
|
EventBus.getDefault().post(new SessionDescriptionSendEvent(sessionDescriptionWithPreferredCodec, sessionId,
|
|
|
sessionDescription.type.canonicalForm().toLowerCase(), null));
|
|
|
-
|
|
|
if (peerConnection != null) {
|
|
|
peerConnection.setLocalDescription(magicSdpObserver, sessionDescriptionWithPreferredCodec);
|
|
|
}
|
|
@@ -398,4 +375,8 @@ public class MagicPeerConnectionWrapper {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-}
|
|
|
+
|
|
|
+ public boolean isMCUPublisher() {
|
|
|
+ return isMCUPublisher;
|
|
|
+ }
|
|
|
+}
|