conversations-classic/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java

308 lines
14 KiB
Java
Raw Normal View History

2020-04-02 09:30:16 +00:00
package eu.siacs.conversations.xmpp.jingle;
import android.util.Log;
2020-04-02 19:12:38 +00:00
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
2020-04-03 08:46:42 +00:00
import org.webrtc.IceCandidate;
2020-04-02 19:12:38 +00:00
2020-04-06 08:26:29 +00:00
import java.util.ArrayDeque;
import java.util.Arrays;
2020-04-02 19:12:38 +00:00
import java.util.Collection;
import java.util.Collections;
2020-04-06 08:26:29 +00:00
import java.util.List;
2020-04-02 19:12:38 +00:00
import java.util.Map;
2020-04-02 09:30:16 +00:00
import eu.siacs.conversations.Config;
2020-04-02 14:29:33 +00:00
import eu.siacs.conversations.xml.Element;
2020-04-02 19:12:38 +00:00
import eu.siacs.conversations.xml.Namespace;
2020-04-06 08:26:29 +00:00
import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
2020-04-03 08:46:42 +00:00
import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
2020-04-02 09:30:16 +00:00
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
2020-04-02 19:12:38 +00:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
2020-04-02 14:29:33 +00:00
import rocks.xmpp.addr.Jid;
2020-04-02 09:30:16 +00:00
2020-04-06 11:01:17 +00:00
public class JingleRtpConnection extends AbstractJingleConnection implements WebRTCWrapper.EventCallback {
2020-04-02 09:30:16 +00:00
2020-04-02 19:12:38 +00:00
private static final Map<State, Collection<State>> VALID_TRANSITIONS;
static {
final ImmutableMap.Builder<State, Collection<State>> transitionBuilder = new ImmutableMap.Builder<>();
transitionBuilder.put(State.NULL, ImmutableList.of(State.PROPOSED, State.SESSION_INITIALIZED));
transitionBuilder.put(State.PROPOSED, ImmutableList.of(State.ACCEPTED, State.PROCEED));
2020-04-03 08:46:42 +00:00
transitionBuilder.put(State.PROCEED, ImmutableList.of(State.SESSION_INITIALIZED));
2020-04-06 11:01:17 +00:00
transitionBuilder.put(State.SESSION_INITIALIZED, ImmutableList.of(State.SESSION_ACCEPTED));
2020-04-02 19:12:38 +00:00
VALID_TRANSITIONS = transitionBuilder.build();
}
2020-04-06 11:01:17 +00:00
private final WebRTCWrapper webRTCWrapper = new WebRTCWrapper(this);
2020-04-06 08:26:29 +00:00
private final ArrayDeque<IceCandidate> pendingIceCandidates = new ArrayDeque<>();
2020-04-06 11:01:17 +00:00
private State state = State.NULL;
private RtpContentMap initiatorRtpContentMap;
private RtpContentMap responderRtpContentMap;
2020-04-06 08:26:29 +00:00
2020-04-02 09:30:16 +00:00
public JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
super(jingleConnectionManager, id, initiator);
2020-04-02 09:30:16 +00:00
}
@Override
void deliverPacket(final JinglePacket jinglePacket) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": packet delivered to JingleRtpConnection");
2020-04-03 08:46:42 +00:00
switch (jinglePacket.getAction()) {
case SESSION_INITIATE:
receiveSessionInitiate(jinglePacket);
break;
2020-04-06 08:26:29 +00:00
case TRANSPORT_INFO:
receiveTransportInfo(jinglePacket);
break;
2020-04-03 08:46:42 +00:00
default:
Log.d(Config.LOGTAG, String.format("%s: received unhandled jingle action %s", id.account.getJid().asBareJid(), jinglePacket.getAction()));
break;
}
}
2020-04-06 08:26:29 +00:00
private void receiveTransportInfo(final JinglePacket jinglePacket) {
if (isInState(State.SESSION_INITIALIZED, State.SESSION_ACCEPTED)) {
final RtpContentMap contentMap;
try {
contentMap = RtpContentMap.of(jinglePacket);
} catch (IllegalArgumentException | NullPointerException e) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents", e);
return;
}
2020-04-06 11:01:17 +00:00
//TODO pick proper rtpContentMap
final Group originalGroup = this.initiatorRtpContentMap != null ? this.initiatorRtpContentMap.group : null;
2020-04-06 08:26:29 +00:00
final List<String> identificationTags = originalGroup == null ? Collections.emptyList() : originalGroup.getIdentificationTags();
if (identificationTags.size() == 0) {
2020-04-06 11:01:17 +00:00
Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no identification tags found in initial offer. we won't be able to calculate mLineIndices");
2020-04-06 08:26:29 +00:00
}
2020-04-06 11:01:17 +00:00
for (final Map.Entry<String, RtpContentMap.DescriptionTransport> content : contentMap.contents.entrySet()) {
2020-04-06 08:26:29 +00:00
final String ufrag = content.getValue().transport.getAttribute("ufrag");
2020-04-06 11:01:17 +00:00
for (final IceUdpTransportInfo.Candidate candidate : content.getValue().transport.getCandidates()) {
2020-04-06 08:26:29 +00:00
final String sdp = candidate.toSdpAttribute(ufrag);
final String sdpMid = content.getKey();
final int mLineIndex = identificationTags.indexOf(sdpMid);
final IceCandidate iceCandidate = new IceCandidate(sdpMid, mLineIndex, sdp);
2020-04-06 11:01:17 +00:00
Log.d(Config.LOGTAG, "received candidate: " + iceCandidate);
2020-04-06 08:26:29 +00:00
if (isInState(State.SESSION_ACCEPTED)) {
2020-04-06 11:01:17 +00:00
this.webRTCWrapper.addIceCandidate(iceCandidate);
2020-04-06 08:26:29 +00:00
} else {
this.pendingIceCandidates.push(iceCandidate);
}
}
}
} else {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received transport info while in state=" + this.state);
}
}
2020-04-03 08:46:42 +00:00
private void receiveSessionInitiate(final JinglePacket jinglePacket) {
if (isInitiator()) {
Log.d(Config.LOGTAG, String.format("%s: received session-initiate even though we were initiating", id.account.getJid().asBareJid()));
//TODO respond with out-of-order
return;
}
final RtpContentMap contentMap;
2020-04-03 08:46:42 +00:00
try {
contentMap = RtpContentMap.of(jinglePacket);
2020-04-06 08:26:29 +00:00
contentMap.requireContentDescriptions();
} catch (IllegalArgumentException | IllegalStateException | NullPointerException e) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents", e);
2020-04-03 08:46:42 +00:00
return;
}
Log.d(Config.LOGTAG, "processing session-init with " + contentMap.contents.size() + " contents");
2020-04-03 08:46:42 +00:00
final State oldState = this.state;
if (transition(State.SESSION_INITIALIZED)) {
2020-04-06 11:01:17 +00:00
this.initiatorRtpContentMap = contentMap;
2020-04-03 08:46:42 +00:00
if (oldState == State.PROCEED) {
2020-04-06 11:01:17 +00:00
Log.d(Config.LOGTAG, "automatically accepting");
2020-04-03 08:46:42 +00:00
sendSessionAccept();
} else {
2020-04-06 11:01:17 +00:00
Log.d(Config.LOGTAG, "start ringing");
2020-04-03 08:46:42 +00:00
//TODO start ringing
}
} else {
Log.d(Config.LOGTAG, String.format("%s: received session-initiate while in state %s", id.account.getJid().asBareJid(), state));
}
2020-04-02 09:30:16 +00:00
}
2020-04-02 14:29:33 +00:00
2020-04-06 11:01:17 +00:00
private void sendSessionAccept() {
final RtpContentMap rtpContentMap = this.initiatorRtpContentMap;
if (rtpContentMap == null) {
throw new IllegalStateException("intital RTP Content Map has not been set");
}
setupWebRTC();
2020-04-06 11:01:17 +00:00
final org.webrtc.SessionDescription offer = new org.webrtc.SessionDescription(
org.webrtc.SessionDescription.Type.OFFER,
SessionDescription.of(rtpContentMap).toString()
);
try {
this.webRTCWrapper.setRemoteDescription(offer).get();
org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createAnswer().get();
this.webRTCWrapper.setLocalDescription(webRTCSessionDescription);
final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
final RtpContentMap respondingRtpContentMap = RtpContentMap.of(sessionDescription);
sendSessionAccept(respondingRtpContentMap);
} catch (Exception e) {
Log.d(Config.LOGTAG, "unable to send session accept", e);
2020-04-06 11:01:17 +00:00
}
}
2020-04-06 11:01:17 +00:00
private void sendSessionAccept(final RtpContentMap rtpContentMap) {
this.responderRtpContentMap = rtpContentMap;
this.transitionOrThrow(State.SESSION_ACCEPTED);
final JinglePacket sessionAccept = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_ACCEPT, id.sessionId);
Log.d(Config.LOGTAG, sessionAccept.toString());
send(sessionAccept);
}
2020-04-03 08:46:42 +00:00
void deliveryMessage(final Jid from, final Element message) {
2020-04-02 14:29:33 +00:00
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": delivered message to JingleRtpConnection " + message);
2020-04-02 19:12:38 +00:00
switch (message.getName()) {
case "propose":
2020-04-03 08:46:42 +00:00
receivePropose(from, message);
2020-04-02 19:12:38 +00:00
break;
2020-04-03 08:46:42 +00:00
case "proceed":
receiveProceed(from, message);
2020-04-02 19:12:38 +00:00
default:
break;
}
}
2020-04-03 08:46:42 +00:00
private void receivePropose(final Jid from, final Element propose) {
final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
2020-04-06 11:01:17 +00:00
//TODO we can use initiator logic here
2020-04-03 08:46:42 +00:00
if (originatedFromMyself) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": saw proposal from mysql. ignoring");
} else if (transition(State.PROPOSED)) {
//TODO start ringing or something
pickUpCall();
} else {
Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring session proposal because already in " + state);
}
}
private void receiveProceed(final Jid from, final Element proceed) {
if (from.equals(id.with)) {
if (isInitiator()) {
if (transition(State.PROCEED)) {
2020-04-03 08:46:42 +00:00
this.sendSessionInitiate();
} else {
Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because already in %s", id.account.getJid().asBareJid(), this.state));
}
} else {
Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because we were not initializing", id.account.getJid().asBareJid()));
}
} else {
Log.d(Config.LOGTAG, String.format("%s: ignoring proceed from %s. was expected from %s", id.account.getJid().asBareJid(), from, id.with));
}
}
private void sendSessionInitiate() {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": prepare session-initiate");
setupWebRTC();
2020-04-06 11:01:17 +00:00
try {
org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createOffer().get();
final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
Log.d(Config.LOGTAG, "description: " + webRTCSessionDescription.description);
final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription);
sendSessionInitiate(rtpContentMap);
this.webRTCWrapper.setLocalDescription(webRTCSessionDescription).get();
} catch (Exception e) {
Log.d(Config.LOGTAG, "unable to sendSessionInitiate", e);
}
}
private void sendSessionInitiate(RtpContentMap rtpContentMap) {
2020-04-06 11:01:17 +00:00
this.initiatorRtpContentMap = rtpContentMap;
this.transitionOrThrow(State.SESSION_INITIALIZED);
2020-04-05 11:58:05 +00:00
final JinglePacket sessionInitiate = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_INITIATE, id.sessionId);
Log.d(Config.LOGTAG, sessionInitiate.toString());
send(sessionInitiate);
2020-04-03 08:46:42 +00:00
}
2020-04-05 11:58:05 +00:00
private void sendTransportInfo(final String contentName, IceUdpTransportInfo.Candidate candidate) {
final RtpContentMap transportInfo;
try {
2020-04-06 11:01:17 +00:00
//TODO when responding use responderRtpContentMap
transportInfo = this.initiatorRtpContentMap.transportInfo(contentName, candidate);
2020-04-05 11:58:05 +00:00
} catch (Exception e) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to prepare transport-info from candidate for content=" + contentName);
return;
}
final JinglePacket jinglePacket = transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
Log.d(Config.LOGTAG, jinglePacket.toString());
send(jinglePacket);
}
private void send(final JinglePacket jinglePacket) {
jinglePacket.setTo(id.with);
//TODO track errors
xmppConnectionService.sendIqPacket(id.account, jinglePacket, null);
}
2020-04-02 19:12:38 +00:00
public void pickUpCall() {
switch (this.state) {
case PROPOSED:
pickupCallFromProposed();
break;
case SESSION_INITIALIZED:
pickupCallFromSessionInitialized();
break;
default:
throw new IllegalStateException("Can not pick up call from " + this.state);
}
}
private void setupWebRTC() {
2020-04-06 11:01:17 +00:00
this.webRTCWrapper.setup(this.xmppConnectionService);
this.webRTCWrapper.initializePeerConnection();
}
2020-04-02 19:12:38 +00:00
private void pickupCallFromProposed() {
transitionOrThrow(State.PROCEED);
final MessagePacket messagePacket = new MessagePacket();
messagePacket.setTo(id.with);
2020-04-03 08:46:42 +00:00
//Note that Movim needs 'accept', correct is 'proceed' https://github.com/movim/movim/issues/916
messagePacket.addChild("proceed", Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
2020-04-02 19:12:38 +00:00
Log.d(Config.LOGTAG, messagePacket.toString());
xmppConnectionService.sendMessagePacket(id.account, messagePacket);
2020-04-02 14:29:33 +00:00
}
2020-04-02 19:12:38 +00:00
private void pickupCallFromSessionInitialized() {
}
2020-04-06 08:26:29 +00:00
private synchronized boolean isInState(State... state) {
return Arrays.asList(state).contains(this.state);
}
2020-04-02 19:12:38 +00:00
private synchronized boolean transition(final State target) {
final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
if (validTransitions != null && validTransitions.contains(target)) {
this.state = target;
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
return true;
} else {
return false;
}
}
public void transitionOrThrow(final State target) {
2020-04-02 19:12:38 +00:00
if (!transition(target)) {
throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
}
}
2020-04-06 11:01:17 +00:00
@Override
public void onIceCandidate(final IceCandidate iceCandidate) {
final IceUdpTransportInfo.Candidate candidate = IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp);
Log.d(Config.LOGTAG, "onIceCandidate: " + iceCandidate.sdp + " mLineIndex=" + iceCandidate.sdpMLineIndex);
sendTransportInfo(iceCandidate.sdpMid, candidate);
}
2020-04-02 09:30:16 +00:00
}