Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-11-21 10:27:43 +01:00
parent 53580378d1
commit bec37f82ae
4 changed files with 44 additions and 115 deletions

View file

@ -49,10 +49,8 @@ import com.nextcloud.talk.api.models.json.signaling.Signaling;
import com.nextcloud.talk.api.models.json.signaling.SignalingOverall;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.events.MediaStreamEvent;
import com.nextcloud.talk.events.PeerReadyEvent;
import com.nextcloud.talk.events.SessionDescriptionSendEvent;
import com.nextcloud.talk.persistence.entities.UserEntity;
import com.nextcloud.talk.webrtc.MagicSdpObserver;
import com.nextcloud.talk.webrtc.PeerConnectionWrapper;
import org.apache.commons.lang3.StringEscapeUtils;
@ -134,6 +132,8 @@ public class CallActivity extends AppCompatActivity {
private UserEntity userEntity;
private String callSession;
private MediaStream localMediaStream;
private String credentials;
private List<PeerConnectionWrapper> peerConnectionWrapperList = new ArrayList<>();
@ -258,6 +258,10 @@ public class CallActivity extends AppCompatActivity {
audioSource = peerConnectionFactory.createAudioSource(audioConstraints);
localAudioTrack = peerConnectionFactory.createAudioTrack("NCa0", audioSource);
localMediaStream = peerConnectionFactory.createLocalMediaStream("NCMS");
localMediaStream.addTrack(localAudioTrack);
localMediaStream.addTrack(localVideoTrack);
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, r.getDisplayMetrics());
videoCapturerAndroid.startCapture(px, px, 30);
@ -270,8 +274,10 @@ public class CallActivity extends AppCompatActivity {
//we already have video and audio tracks. Now create peerconnections
iceServers = new ArrayList<>();
//iceServers.add(new PeerConnection.IceServer("stun:stun.l.google.com:19302"));
iceServers.add(new PeerConnection.IceServer("turn:172.104.225.9:3478"));
iceServers.add(new PeerConnection.IceServer("stun:stun.l.google.com:19302"));
//iceServers.add(new PeerConnection.IceServer("turn:mario:mario@172.104.225.9:3478"));
//iceServers.add(PeerConnection.IceServer.builder("http://172.104.225.9:3478").setUsername("mario").setPassword
// ("mario").createIceServer());
//create sdpConstraints
sdpConstraints = new MediaConstraints();
@ -308,8 +314,6 @@ public class CallActivity extends AppCompatActivity {
localPeer = alwaysGetPeerConnectionWrapperForSessionId(callSession, true).
getPeerConnection();
//creating local mediastream
// start pinging the call
ncApi.pingCall(ApiHelper.getCredentials(userEntity.getUsername(), userEntity.getToken()),
ApiHelper.getUrlForCallPing(userEntity.getBaseUrl(), roomToken))
@ -434,25 +438,9 @@ public class CallActivity extends AppCompatActivity {
case "offer":
case "answer":
peerConnectionWrapper.setNick(ncSignalingMessage.getPayload().getNick());
peerConnectionWrapper.getPeerConnection().setRemoteDescription(new MagicSdpObserver() {
@Override
public void onSetSuccess() {
super.onSetSuccess();
peerConnectionWrapper.getPeerConnection().createAnswer(new MagicSdpObserver() {
@Override
public void onCreateSuccess(SessionDescription sessionDescription) {
super.onCreateSuccess(sessionDescription);
peerConnectionWrapper.getPeerConnection().setLocalDescription(new MagicSdpObserver(),
sessionDescription);
}
}, sdpConstraints);
}
}, new SessionDescription(SessionDescription.Type.fromCanonicalForm(type),
peerConnectionWrapper.getPeerConnection().setRemoteDescription(peerConnectionWrapper
.getMagicSdpObserver(), new SessionDescription(SessionDescription.Type.fromCanonicalForm(type),
ncSignalingMessage.getPayload().getSdp()));
break;
case "candidate":
NCIceCandidate ncIceCandidate = ncSignalingMessage.getPayload().getIceCandidate();
@ -461,17 +449,7 @@ public class CallActivity extends AppCompatActivity {
peerConnectionWrapper.addCandidate(iceCandidate);
break;
case "endOfCandidates":
peerConnectionWrapper.drainIceCandidates();
if (peerConnectionWrapper.getPeerConnection().getLocalDescription() != null) {
EventBus.getDefault().post(new SessionDescriptionSendEvent
(peerConnectionWrapper.getPeerConnection().getLocalDescription(), callSession,
peerConnectionWrapper.getPeerConnection().getLocalDescription().type.canonicalForm(),
null));
} else {
peerConnectionWrapper.getPeerConnection().createAnswer(peerConnectionWrapper
.getMagicSdpObserver(), sdpConstraints);
}
peerConnectionWrapper.sendLocalCandidates();
//peerConnectionWrapper.drainIceCandidates();
break;
default:
break;
@ -550,6 +528,7 @@ public class CallActivity extends AppCompatActivity {
} else {
peerConnectionWrapper = new PeerConnectionWrapper(peerConnectionFactory,
iceServers, sdpConstraints, sessionId, isLocalPeer, callSession);
peerConnectionWrapper.getPeerConnection().addStream(localMediaStream);
peerConnectionWrapperList.add(peerConnectionWrapper);
return peerConnectionWrapper;
}
@ -665,16 +644,6 @@ public class CallActivity extends AppCompatActivity {
eventBus.unregister(this);
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(PeerReadyEvent peerReadyEvent) {
MediaStream stream = peerConnectionFactory.createLocalMediaStream("NCMS");
stream.addTrack(localAudioTrack);
stream.addTrack(localVideoTrack);
if (peerReadyEvent.getPeerConnection() != null) {
peerReadyEvent.getPeerConnection().addStream(stream);
}
}
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(MediaStreamEvent mediaStreamEvent) {
gotRemoteStream(mediaStreamEvent.getMediaStream());
@ -689,9 +658,9 @@ public class CallActivity extends AppCompatActivity {
ncMessageWrapper.setSessionId(callSession);
// Create signaling message and payload
NCSignalingMessage ncSignalingMessage = new NCSignalingMessage();
//ncSignalingMessage.setFrom(callSession);
ncSignalingMessage.setTo(sessionDescriptionSend.getPeerId());
ncSignalingMessage.setRoomType("video");
ncSignalingMessage.setType(sessionDescriptionSend.getType());
NCMessagePayload ncMessagePayload = new NCMessagePayload();
ncMessagePayload.setType(sessionDescriptionSend.getType());
if (!"candidate".equals(sessionDescriptionSend.getType())) {
@ -701,6 +670,7 @@ public class CallActivity extends AppCompatActivity {
ncMessagePayload.setIceCandidate(sessionDescriptionSend.getNcIceCandidate());
}
// Set all we need
ncSignalingMessage.setPayload(ncMessagePayload);
ncMessageWrapper.setSignalingMessage(ncSignalingMessage);
@ -722,8 +692,6 @@ public class CallActivity extends AppCompatActivity {
String stringToSend = stringBuilder.toString();
strings.add(stringToSend);
List<NCMessageWrapper> wrappers = new ArrayList<>();
wrappers.add(ncMessageWrapper);
ncApi.sendSignalingMessages(credentials, ApiHelper.getUrlForSignaling(userEntity.getBaseUrl()),
strings.toString())
.subscribeOn(Schedulers.newThread())

View file

@ -1,35 +0,0 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.events;
import org.webrtc.PeerConnection;
import lombok.Data;
@Data
public class PeerReadyEvent {
private final PeerConnection peerConnection;
public PeerReadyEvent(PeerConnection peerConnection) {
this.peerConnection = peerConnection;
}
}

View file

@ -66,6 +66,11 @@ public class MagicFirebaseMessagingService extends FirebaseMessagingService {
byte[] decryptedSubject = cipher.doFinal(base64DecodedSubject);
DecryptedPushMessage decryptedPushMessage = LoganSquare.parse(new String(decryptedSubject),
DecryptedPushMessage.class);
if (decryptedPushMessage.getApp().equals("spreed") || decryptedPushMessage.getApp().equals
("talk")) {
// process message
}
}
} catch (NoSuchAlgorithmException e1) {
Log.d(TAG, "No proper algorithm to decrypt the message");

View file

@ -25,7 +25,6 @@ import android.util.Log;
import com.nextcloud.talk.api.models.json.signaling.DataChannelMessage;
import com.nextcloud.talk.api.models.json.signaling.NCIceCandidate;
import com.nextcloud.talk.events.MediaStreamEvent;
import com.nextcloud.talk.events.PeerReadyEvent;
import com.nextcloud.talk.events.SessionDescriptionSendEvent;
import org.greenrobot.eventbus.EventBus;
@ -80,7 +79,6 @@ public class PeerConnectionWrapper {
@Override
public void onAddStream(MediaStream mediaStream) {
Log.d("MARIO", "MEDIA");
EventBus.getDefault().post(new MediaStreamEvent(mediaStream));
}
@ -103,18 +101,19 @@ public class PeerConnectionWrapper {
ncIceCandidate.setSdpMid(iceCandidate.sdpMid);
ncIceCandidate.setSdpMLineIndex(iceCandidate.sdpMLineIndex);
ncIceCandidate.setCandidate(iceCandidate.sdp);
localCandidates.add(ncIceCandidate);
if (peerConnection.getRemoteDescription() == null) {
localCandidates.add(ncIceCandidate);
} else {
EventBus.getDefault().post(new SessionDescriptionSendEvent(null, sessionId,
"candidate", ncIceCandidate));
}
}
};
peerConnection = peerConnectionFactory.createPeerConnection(iceServerList, mediaConstraints,
magicPeerConnectionObserver);
peerConnection = peerConnectionFactory.createPeerConnection(iceServerList, mediaConstraints,
magicPeerConnectionObserver);
if (isLocalPeer) {
EventBus.getDefault().post(new PeerReadyEvent(peerConnection));
}
this.sessionId = sessionId;
this.local = isLocalPeer;
this.mediaConstraints = mediaConstraints;
@ -140,30 +139,22 @@ public class PeerConnectionWrapper {
@Override
public void onSetSuccess() {
if (isInitiator) {
// For offering peer connection we first create offer and set
// local SDP, then after receiving answer set remote SDP.
if (peerConnection.getRemoteDescription() == null) {
// We've just set our local SDP so time to send it.
EventBus.getDefault().post(new SessionDescriptionSendEvent(peerConnection.getLocalDescription(), sessionId,
peerConnection.getLocalDescription().type.canonicalForm(), null));
}
if (peerConnection.getRemoteDescription() == null) {
EventBus.getDefault().post(new SessionDescriptionSendEvent(peerConnection.getLocalDescription(), sessionId,
peerConnection.getLocalDescription().type.canonicalForm(), null));
} else if (peerConnection.getLocalDescription() == null && peerConnection.getRemoteDescription().type
.canonicalForm().equals
("offer")) {
peerConnection.createAnswer(magicSdpObserver, mediaConstraints);
} else if ((peerConnection.getLocalDescription() != null && peerConnection.getRemoteDescription().type
.canonicalForm().equals
("offer"))) {
EventBus.getDefault().post(new SessionDescriptionSendEvent(peerConnection.getLocalDescription(), sessionId,
peerConnection.getLocalDescription().type.canonicalForm(), null));
} else {
// For anwering peer connection we set remote SDP and then
// create answer and set local SDP.
if (peerConnection.getLocalDescription() != null) {
EventBus.getDefault().post(new SessionDescriptionSendEvent(peerConnection.getLocalDescription
(),
sessionId,
peerConnection.getLocalDescription().type.canonicalForm(), null));
// We've just set our local SDP so time to send it, drain
// remote and send local ICE candidates.
} else {
// We've just set remote SDP - do nothing for now -
// answer will be created soon.
}
drainIceCandidates();
sendLocalCandidates();
}
}