JingleRtpConnection.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import android.util.Log;
  4
  5import com.google.common.base.Strings;
  6import com.google.common.collect.ImmutableList;
  7import com.google.common.collect.ImmutableMap;
  8import com.google.common.primitives.Ints;
  9
 10import org.webrtc.IceCandidate;
 11import org.webrtc.PeerConnection;
 12
 13import java.util.ArrayDeque;
 14import java.util.Arrays;
 15import java.util.Collection;
 16import java.util.Collections;
 17import java.util.List;
 18import java.util.Map;
 19
 20import eu.siacs.conversations.Config;
 21import eu.siacs.conversations.xml.Element;
 22import eu.siacs.conversations.xml.Namespace;
 23import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
 24import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
 25import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
 26import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
 27import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 28import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 29import rocks.xmpp.addr.Jid;
 30
 31public class JingleRtpConnection extends AbstractJingleConnection implements WebRTCWrapper.EventCallback {
 32
 33    private static final Map<State, Collection<State>> VALID_TRANSITIONS;
 34
 35    static {
 36        final ImmutableMap.Builder<State, Collection<State>> transitionBuilder = new ImmutableMap.Builder<>();
 37        transitionBuilder.put(State.NULL, ImmutableList.of(
 38                State.PROPOSED,
 39                State.SESSION_INITIALIZED,
 40                State.TERMINATED_APPLICATION_FAILURE
 41        ));
 42        transitionBuilder.put(State.PROPOSED, ImmutableList.of(
 43                State.ACCEPTED,
 44                State.PROCEED,
 45                State.REJECTED,
 46                State.RETRACTED,
 47                State.TERMINATED_APPLICATION_FAILURE
 48        ));
 49        transitionBuilder.put(State.PROCEED, ImmutableList.of(
 50                State.SESSION_INITIALIZED_PRE_APPROVED,
 51                State.TERMINATED_SUCCESS,
 52                State.TERMINATED_APPLICATION_FAILURE
 53        ));
 54        transitionBuilder.put(State.SESSION_INITIALIZED, ImmutableList.of(
 55                State.SESSION_ACCEPTED,
 56                State.TERMINATED_CANCEL_OR_TIMEOUT,
 57                State.TERMINATED_DECLINED_OR_BUSY,
 58                State.TERMINATED_APPLICATION_FAILURE
 59        ));
 60        transitionBuilder.put(State.SESSION_INITIALIZED_PRE_APPROVED, ImmutableList.of(
 61                State.SESSION_ACCEPTED,
 62                State.TERMINATED_CANCEL_OR_TIMEOUT,
 63                State.TERMINATED_DECLINED_OR_BUSY,
 64                State.TERMINATED_APPLICATION_FAILURE
 65        ));
 66        transitionBuilder.put(State.SESSION_ACCEPTED, ImmutableList.of(
 67                State.TERMINATED_SUCCESS,
 68                State.TERMINATED_CONNECTIVITY_ERROR,
 69                State.TERMINATED_APPLICATION_FAILURE
 70        ));
 71        VALID_TRANSITIONS = transitionBuilder.build();
 72    }
 73
 74    private final WebRTCWrapper webRTCWrapper = new WebRTCWrapper(this);
 75    private final ArrayDeque<IceCandidate> pendingIceCandidates = new ArrayDeque<>();
 76    private State state = State.NULL;
 77    private RtpContentMap initiatorRtpContentMap;
 78    private RtpContentMap responderRtpContentMap;
 79
 80
 81    public JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
 82        super(jingleConnectionManager, id, initiator);
 83    }
 84
 85    private static State reasonToState(Reason reason) {
 86        switch (reason) {
 87            case SUCCESS:
 88                return State.TERMINATED_SUCCESS;
 89            case DECLINE:
 90            case BUSY:
 91                return State.TERMINATED_DECLINED_OR_BUSY;
 92            case CANCEL:
 93            case TIMEOUT:
 94                return State.TERMINATED_CANCEL_OR_TIMEOUT;
 95            case FAILED_APPLICATION:
 96                return State.TERMINATED_APPLICATION_FAILURE;
 97            default:
 98                return State.TERMINATED_CONNECTIVITY_ERROR;
 99        }
100    }
101
102    @Override
103    void deliverPacket(final JinglePacket jinglePacket) {
104        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": packet delivered to JingleRtpConnection");
105        switch (jinglePacket.getAction()) {
106            case SESSION_INITIATE:
107                receiveSessionInitiate(jinglePacket);
108                break;
109            case TRANSPORT_INFO:
110                receiveTransportInfo(jinglePacket);
111                break;
112            case SESSION_ACCEPT:
113                receiveSessionAccept(jinglePacket);
114                break;
115            case SESSION_TERMINATE:
116                receiveSessionTerminate(jinglePacket);
117                break;
118            default:
119                respondOk(jinglePacket);
120                Log.d(Config.LOGTAG, String.format("%s: received unhandled jingle action %s", id.account.getJid().asBareJid(), jinglePacket.getAction()));
121                break;
122        }
123    }
124
125    private void receiveSessionTerminate(final JinglePacket jinglePacket) {
126        respondOk(jinglePacket);
127        final Reason reason = jinglePacket.getReason();
128        final State previous = this.state;
129        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session terminate reason=" + reason + " while in state " + previous);
130        webRTCWrapper.close();
131        transitionOrThrow(reasonToState(reason));
132        if (previous == State.PROPOSED || previous == State.SESSION_INITIALIZED) {
133            xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
134        }
135        jingleConnectionManager.finishConnection(this);
136    }
137
138    private void receiveTransportInfo(final JinglePacket jinglePacket) {
139        if (isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
140            respondOk(jinglePacket);
141            final RtpContentMap contentMap;
142            try {
143                contentMap = RtpContentMap.of(jinglePacket);
144            } catch (IllegalArgumentException | NullPointerException e) {
145                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents; ignoring", e);
146                return;
147            }
148            final RtpContentMap rtpContentMap = isInitiator() ? this.responderRtpContentMap : this.initiatorRtpContentMap;
149            final Group originalGroup = rtpContentMap != null ? rtpContentMap.group : null;
150            final List<String> identificationTags = originalGroup == null ? Collections.emptyList() : originalGroup.getIdentificationTags();
151            if (identificationTags.size() == 0) {
152                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no identification tags found in initial offer. we won't be able to calculate mLineIndices");
153            }
154            for (final Map.Entry<String, RtpContentMap.DescriptionTransport> content : contentMap.contents.entrySet()) {
155                final String ufrag = content.getValue().transport.getAttribute("ufrag");
156                for (final IceUdpTransportInfo.Candidate candidate : content.getValue().transport.getCandidates()) {
157                    final String sdp = candidate.toSdpAttribute(ufrag);
158                    final String sdpMid = content.getKey();
159                    final int mLineIndex = identificationTags.indexOf(sdpMid);
160                    final IceCandidate iceCandidate = new IceCandidate(sdpMid, mLineIndex, sdp);
161                    if (isInState(State.SESSION_ACCEPTED)) {
162                        Log.d(Config.LOGTAG, "received candidate: " + iceCandidate);
163                        this.webRTCWrapper.addIceCandidate(iceCandidate);
164                    } else {
165                        this.pendingIceCandidates.offer(iceCandidate);
166                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": put ICE candidate on backlog");
167                    }
168                }
169            }
170        } else {
171            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received transport info while in state=" + this.state);
172            respondWithOutOfOrder(jinglePacket);
173        }
174    }
175
176    private void receiveSessionInitiate(final JinglePacket jinglePacket) {
177        if (isInitiator()) {
178            Log.d(Config.LOGTAG, String.format("%s: received session-initiate even though we were initiating", id.account.getJid().asBareJid()));
179            respondWithOutOfOrder(jinglePacket);
180            return;
181        }
182        final RtpContentMap contentMap;
183        try {
184            contentMap = RtpContentMap.of(jinglePacket);
185            contentMap.requireContentDescriptions();
186        } catch (IllegalArgumentException | IllegalStateException | NullPointerException e) {
187            respondOk(jinglePacket);
188            sendSessionTerminate(Reason.FAILED_APPLICATION);
189            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents", e);
190            return;
191        }
192        Log.d(Config.LOGTAG, "processing session-init with " + contentMap.contents.size() + " contents");
193        final State target;
194        if (this.state == State.PROCEED) {
195            target = State.SESSION_INITIALIZED_PRE_APPROVED;
196        } else {
197            target = State.SESSION_INITIALIZED;
198        }
199        if (transition(target)) {
200            respondOk(jinglePacket);
201            this.initiatorRtpContentMap = contentMap;
202            if (target == State.SESSION_INITIALIZED_PRE_APPROVED) {
203                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": automatically accepting session-initiate");
204                sendSessionAccept();
205            } else {
206                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received not pre-approved session-initiate. start ringing");
207                startRinging();
208            }
209        } else {
210            Log.d(Config.LOGTAG, String.format("%s: received session-initiate while in state %s", id.account.getJid().asBareJid(), state));
211            respondWithOutOfOrder(jinglePacket);
212        }
213    }
214
215    private void receiveSessionAccept(final JinglePacket jinglePacket) {
216        if (!isInitiator()) {
217            Log.d(Config.LOGTAG, String.format("%s: received session-accept even though we were responding", id.account.getJid().asBareJid()));
218            respondWithOutOfOrder(jinglePacket);
219            return;
220        }
221        final RtpContentMap contentMap;
222        try {
223            contentMap = RtpContentMap.of(jinglePacket);
224            contentMap.requireContentDescriptions();
225        } catch (IllegalArgumentException | IllegalStateException | NullPointerException e) {
226            respondOk(jinglePacket);
227            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents in session-accept", e);
228            webRTCWrapper.close();
229            sendSessionTerminate(Reason.FAILED_APPLICATION);
230            return;
231        }
232        Log.d(Config.LOGTAG, "processing session-accept with " + contentMap.contents.size() + " contents");
233        if (transition(State.SESSION_ACCEPTED)) {
234            respondOk(jinglePacket);
235            receiveSessionAccept(contentMap);
236        } else {
237            Log.d(Config.LOGTAG, String.format("%s: received session-accept while in state %s", id.account.getJid().asBareJid(), state));
238            respondOk(jinglePacket);
239        }
240    }
241
242    private void receiveSessionAccept(final RtpContentMap contentMap) {
243        this.responderRtpContentMap = contentMap;
244        final SessionDescription sessionDescription;
245        try {
246            sessionDescription = SessionDescription.of(contentMap);
247        } catch (IllegalArgumentException e) {
248            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-accept to SDP", e);
249            webRTCWrapper.close();
250            sendSessionTerminate(Reason.FAILED_APPLICATION);
251            return;
252        }
253        org.webrtc.SessionDescription answer = new org.webrtc.SessionDescription(
254                org.webrtc.SessionDescription.Type.ANSWER,
255                sessionDescription.toString()
256        );
257        try {
258            this.webRTCWrapper.setRemoteDescription(answer).get();
259        } catch (Exception e) {
260            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to set remote description after receiving session-accept", e);
261            webRTCWrapper.close();
262            sendSessionTerminate(Reason.FAILED_APPLICATION);
263        }
264    }
265
266    private void sendSessionAccept() {
267        final RtpContentMap rtpContentMap = this.initiatorRtpContentMap;
268        if (rtpContentMap == null) {
269            throw new IllegalStateException("initiator RTP Content Map has not been set");
270        }
271        final SessionDescription offer;
272        try {
273            offer = SessionDescription.of(rtpContentMap);
274        } catch (final IllegalArgumentException e) {
275            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-initiate to SDP", e);
276            webRTCWrapper.close();
277            sendSessionTerminate(Reason.FAILED_APPLICATION);
278            ;
279            return;
280        }
281        sendSessionAccept(offer);
282    }
283
284    private void sendSessionAccept(SessionDescription offer) {
285        discoverIceServers(iceServers -> {
286            try {
287                setupWebRTC(iceServers);
288            } catch (WebRTCWrapper.InitializationException e) {
289                sendSessionTerminate(Reason.FAILED_APPLICATION);
290                return;
291            }
292            final org.webrtc.SessionDescription sdp = new org.webrtc.SessionDescription(
293                    org.webrtc.SessionDescription.Type.OFFER,
294                    offer.toString()
295            );
296            try {
297                this.webRTCWrapper.setRemoteDescription(sdp).get();
298                addIceCandidatesFromBlackLog();
299                org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createAnswer().get();
300                final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
301                final RtpContentMap respondingRtpContentMap = RtpContentMap.of(sessionDescription);
302                sendSessionAccept(respondingRtpContentMap);
303                this.webRTCWrapper.setLocalDescription(webRTCSessionDescription);
304            } catch (Exception e) {
305                Log.d(Config.LOGTAG, "unable to send session accept", e);
306
307            }
308        });
309    }
310
311    private void addIceCandidatesFromBlackLog() {
312        while (!this.pendingIceCandidates.isEmpty()) {
313            final IceCandidate iceCandidate = this.pendingIceCandidates.poll();
314            this.webRTCWrapper.addIceCandidate(iceCandidate);
315            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": added ICE candidate from back log " + iceCandidate);
316        }
317    }
318
319    private void sendSessionAccept(final RtpContentMap rtpContentMap) {
320        this.responderRtpContentMap = rtpContentMap;
321        this.transitionOrThrow(State.SESSION_ACCEPTED);
322        final JinglePacket sessionAccept = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_ACCEPT, id.sessionId);
323        Log.d(Config.LOGTAG, sessionAccept.toString());
324        send(sessionAccept);
325    }
326
327    void deliveryMessage(final Jid from, final Element message) {
328        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": delivered message to JingleRtpConnection " + message);
329        switch (message.getName()) {
330            case "propose":
331                receivePropose(from, message);
332                break;
333            case "proceed":
334                receiveProceed(from, message);
335                break;
336            case "retract":
337                receiveRetract(from, message);
338                break;
339            case "reject":
340                receiveReject(from, message);
341                break;
342            case "accept":
343                receiveAccept(from, message);
344                break;
345            default:
346                break;
347        }
348    }
349
350    private void receiveAccept(Jid from, Element message) {
351        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
352        if (originatedFromMyself) {
353            if (transition(State.ACCEPTED)) {
354                this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
355                this.jingleConnectionManager.finishConnection(this);
356            } else {
357                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to transition to accept because already in state=" + this.state);
358            }
359        } else {
360            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring 'accept' from " + from);
361        }
362    }
363
364    private void receiveReject(Jid from, Element message) {
365        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
366        //reject from another one of my clients
367        if (originatedFromMyself) {
368            if (transition(State.REJECTED)) {
369                this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
370                this.jingleConnectionManager.finishConnection(this);
371            } else {
372                Log.d(Config.LOGTAG, "not able to transition into REJECTED because already in " + this.state);
373            }
374        } else {
375            Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from " + from + " for session with " + id.with);
376        }
377    }
378
379    private void receivePropose(final Jid from, final Element propose) {
380        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
381        //TODO we can use initiator logic here
382        if (originatedFromMyself) {
383            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": saw proposal from mysql. ignoring");
384        } else if (transition(State.PROPOSED)) {
385            startRinging();
386        } else {
387            Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring session proposal because already in " + state);
388        }
389    }
390
391    private void startRinging() {
392        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received call from " + id.with + ". start ringing");
393        xmppConnectionService.getNotificationService().showIncomingCallNotification(id);
394    }
395
396    private void receiveProceed(final Jid from, final Element proceed) {
397        if (from.equals(id.with)) {
398            if (isInitiator()) {
399                if (transition(State.PROCEED)) {
400                    this.sendSessionInitiate(State.SESSION_INITIALIZED_PRE_APPROVED);
401                } else {
402                    Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because already in %s", id.account.getJid().asBareJid(), this.state));
403                }
404            } else {
405                Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because we were not initializing", id.account.getJid().asBareJid()));
406            }
407        } else if (from.asBareJid().equals(id.account.getJid().asBareJid())) {
408            if (transition(State.ACCEPTED)) {
409                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": moved session with " + id.with + " into state accepted after received carbon copied procced");
410                this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
411                this.jingleConnectionManager.finishConnection(this);
412            }
413        } else {
414            Log.d(Config.LOGTAG, String.format("%s: ignoring proceed from %s. was expected from %s", id.account.getJid().asBareJid(), from, id.with));
415        }
416    }
417
418    private void receiveRetract(final Jid from, final Element retract) {
419        if (from.equals(id.with)) {
420            if (transition(State.RETRACTED)) {
421                xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
422                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": session with " + id.with + " has been retracted");
423                //TODO create missed call notification/message
424                jingleConnectionManager.finishConnection(this);
425            } else {
426                Log.d(Config.LOGTAG, "ignoring retract because already in " + this.state);
427            }
428        } else {
429            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received retract from " + from + ". expected retract from" + id.with + ". ignoring");
430        }
431    }
432
433    private void sendSessionInitiate(final State targetState) {
434        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": prepare session-initiate");
435        discoverIceServers(iceServers -> {
436            try {
437                setupWebRTC(iceServers);
438            } catch (WebRTCWrapper.InitializationException e) {
439                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize webrtc");
440                transitionOrThrow(State.TERMINATED_APPLICATION_FAILURE);
441                return;
442            }
443            try {
444                org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createOffer().get();
445                final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
446                Log.d(Config.LOGTAG, "description: " + webRTCSessionDescription.description);
447                final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription);
448                sendSessionInitiate(rtpContentMap, targetState);
449                this.webRTCWrapper.setLocalDescription(webRTCSessionDescription).get();
450            } catch (final Exception e) {
451                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to sendSessionInitiate", e);
452                webRTCWrapper.close();
453                if (isInState(targetState)) {
454                    sendSessionTerminate(Reason.FAILED_APPLICATION);
455                } else {
456                    transitionOrThrow(State.TERMINATED_APPLICATION_FAILURE);
457                }
458            }
459        });
460    }
461
462    private void sendSessionInitiate(RtpContentMap rtpContentMap, final State targetState) {
463        this.initiatorRtpContentMap = rtpContentMap;
464        this.transitionOrThrow(targetState);
465        final JinglePacket sessionInitiate = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_INITIATE, id.sessionId);
466        Log.d(Config.LOGTAG, sessionInitiate.toString());
467        send(sessionInitiate);
468    }
469
470    private void sendSessionTerminate(final Reason reason) {
471        final State target = reasonToState(reason);
472        transitionOrThrow(target);
473        final JinglePacket jinglePacket = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
474        jinglePacket.setReason(reason);
475        send(jinglePacket);
476        Log.d(Config.LOGTAG, jinglePacket.toString());
477        jingleConnectionManager.finishConnection(this);
478    }
479
480    private void sendTransportInfo(final String contentName, IceUdpTransportInfo.Candidate candidate) {
481        final RtpContentMap transportInfo;
482        try {
483            final RtpContentMap rtpContentMap = isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
484            transportInfo = rtpContentMap.transportInfo(contentName, candidate);
485        } catch (Exception e) {
486            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to prepare transport-info from candidate for content=" + contentName);
487            return;
488        }
489        final JinglePacket jinglePacket = transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
490        Log.d(Config.LOGTAG, jinglePacket.toString());
491        send(jinglePacket);
492    }
493
494    private void send(final JinglePacket jinglePacket) {
495        jinglePacket.setTo(id.with);
496        xmppConnectionService.sendIqPacket(id.account, jinglePacket, (account, response) -> {
497            if (response.getType() == IqPacket.TYPE.ERROR) {
498                final String errorCondition = response.getErrorCondition();
499                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ-error from " + response.getFrom() + " in RTP session. " + errorCondition);
500                this.webRTCWrapper.close();
501                final State target;
502                if (Arrays.asList(
503                        "service-unavailable",
504                        "recipient-unavailable",
505                        "remote-server-not-found",
506                        "remote-server-timeout"
507                ).contains(errorCondition)) {
508                    target = State.TERMINATED_CONNECTIVITY_ERROR;
509                } else {
510                    target = State.TERMINATED_APPLICATION_FAILURE;
511                }
512                if (transition(target)) {
513                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": terminated session with " + id.with);
514                } else {
515                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": not transitioning because already at state=" + this.state);
516                }
517
518            } else if (response.getType() == IqPacket.TYPE.TIMEOUT) {
519                this.webRTCWrapper.close();
520                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ timeout in RTP session with " + id.with + ". terminating with connectivity error");
521                transition(State.TERMINATED_CONNECTIVITY_ERROR);
522                this.jingleConnectionManager.finishConnection(this);
523            }
524        });
525    }
526
527    private void respondWithOutOfOrder(final JinglePacket jinglePacket) {
528        jingleConnectionManager.respondWithJingleError(id.account, jinglePacket, "out-of-order", "unexpected-request", "wait");
529    }
530
531    private void respondOk(final JinglePacket jinglePacket) {
532        xmppConnectionService.sendIqPacket(id.account, jinglePacket.generateResponse(IqPacket.TYPE.RESULT), null);
533    }
534
535    public RtpEndUserState getEndUserState() {
536        switch (this.state) {
537            case PROPOSED:
538            case SESSION_INITIALIZED:
539                if (isInitiator()) {
540                    return RtpEndUserState.RINGING;
541                } else {
542                    return RtpEndUserState.INCOMING_CALL;
543                }
544            case PROCEED:
545                if (isInitiator()) {
546                    return RtpEndUserState.RINGING;
547                } else {
548                    return RtpEndUserState.ACCEPTING_CALL;
549                }
550            case SESSION_INITIALIZED_PRE_APPROVED:
551                if (isInitiator()) {
552                    return RtpEndUserState.RINGING;
553                } else {
554                    return RtpEndUserState.CONNECTING;
555                }
556            case SESSION_ACCEPTED:
557                final PeerConnection.PeerConnectionState state = webRTCWrapper.getState();
558                if (state == PeerConnection.PeerConnectionState.CONNECTED) {
559                    return RtpEndUserState.CONNECTED;
560                } else if (state == PeerConnection.PeerConnectionState.NEW || state == PeerConnection.PeerConnectionState.CONNECTING) {
561                    return RtpEndUserState.CONNECTING;
562                } else if (state == PeerConnection.PeerConnectionState.CLOSED) {
563                    return RtpEndUserState.ENDING_CALL;
564                } else if (state == PeerConnection.PeerConnectionState.FAILED) {
565                    return RtpEndUserState.CONNECTIVITY_ERROR;
566                } else {
567                    return RtpEndUserState.ENDING_CALL;
568                }
569            case REJECTED:
570            case TERMINATED_DECLINED_OR_BUSY:
571                if (isInitiator()) {
572                    return RtpEndUserState.DECLINED_OR_BUSY;
573                } else {
574                    return RtpEndUserState.ENDED;
575                }
576            case TERMINATED_SUCCESS:
577            case ACCEPTED:
578            case RETRACTED:
579            case TERMINATED_CANCEL_OR_TIMEOUT:
580                return RtpEndUserState.ENDED;
581            case TERMINATED_CONNECTIVITY_ERROR:
582                return RtpEndUserState.CONNECTIVITY_ERROR;
583            case TERMINATED_APPLICATION_FAILURE:
584                return RtpEndUserState.APPLICATION_ERROR;
585        }
586        throw new IllegalStateException(String.format("%s has no equivalent EndUserState", this.state));
587    }
588
589
590    public void acceptCall() {
591        switch (this.state) {
592            case PROPOSED:
593                acceptCallFromProposed();
594                break;
595            case SESSION_INITIALIZED:
596                acceptCallFromSessionInitialized();
597                break;
598            default:
599                throw new IllegalStateException("Can not accept call from " + this.state);
600        }
601    }
602
603    public void rejectCall() {
604        switch (this.state) {
605            case PROPOSED:
606                rejectCallFromProposed();
607                break;
608            case SESSION_INITIALIZED:
609                rejectCallFromSessionInitiate();
610                break;
611            default:
612                throw new IllegalStateException("Can not reject call from " + this.state);
613        }
614    }
615
616    public void endCall() {
617        if (isInState(State.PROCEED)) {
618            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ending call while in state PROCEED just means ending the connection");
619            webRTCWrapper.close();
620            jingleConnectionManager.finishConnection(this);
621            transitionOrThrow(State.TERMINATED_SUCCESS); //arguably this wasn't success; but not a real failure either
622            return;
623        }
624        if (isInitiator() && isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED)) {
625            webRTCWrapper.close();
626            sendSessionTerminate(Reason.CANCEL);
627            return;
628        }
629        if (isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
630            webRTCWrapper.close();
631            sendSessionTerminate(Reason.SUCCESS);
632            return;
633        }
634        throw new IllegalStateException("called 'endCall' while in state " + this.state);
635    }
636
637    private void setupWebRTC(final List<PeerConnection.IceServer> iceServers) throws WebRTCWrapper.InitializationException {
638        this.webRTCWrapper.setup(this.xmppConnectionService);
639        this.webRTCWrapper.initializePeerConnection(iceServers);
640    }
641
642    private void acceptCallFromProposed() {
643        transitionOrThrow(State.PROCEED);
644        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
645        this.sendJingleMessage("accept", id.account.getJid().asBareJid());
646        this.sendJingleMessage("proceed");
647    }
648
649    private void rejectCallFromProposed() {
650        transitionOrThrow(State.REJECTED);
651        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
652        this.sendJingleMessage("reject");
653        jingleConnectionManager.finishConnection(this);
654    }
655
656    private void rejectCallFromSessionInitiate() {
657        webRTCWrapper.close();
658        sendSessionTerminate(Reason.DECLINE);
659        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
660    }
661
662    private void sendJingleMessage(final String action) {
663        sendJingleMessage(action, id.with);
664    }
665
666    private void sendJingleMessage(final String action, final Jid to) {
667        final MessagePacket messagePacket = new MessagePacket();
668        messagePacket.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
669        messagePacket.setTo(to);
670        messagePacket.addChild(action, Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
671        Log.d(Config.LOGTAG, messagePacket.toString());
672        xmppConnectionService.sendMessagePacket(id.account, messagePacket);
673    }
674
675    private void acceptCallFromSessionInitialized() {
676        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
677        sendSessionAccept();
678    }
679
680    private synchronized boolean isInState(State... state) {
681        return Arrays.asList(state).contains(this.state);
682    }
683
684    private synchronized boolean transition(final State target) {
685        final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
686        if (validTransitions != null && validTransitions.contains(target)) {
687            this.state = target;
688            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
689            updateEndUserState();
690            return true;
691        } else {
692            return false;
693        }
694    }
695
696    public void transitionOrThrow(final State target) {
697        if (!transition(target)) {
698            throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
699        }
700    }
701
702    @Override
703    public void onIceCandidate(final IceCandidate iceCandidate) {
704        final IceUdpTransportInfo.Candidate candidate = IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp);
705        Log.d(Config.LOGTAG, "sending candidate: " + iceCandidate.toString());
706        sendTransportInfo(iceCandidate.sdpMid, candidate);
707    }
708
709    @Override
710    public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
711        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnectionState changed to " + newState);
712        updateEndUserState();
713        if (newState == PeerConnection.PeerConnectionState.FAILED) { //TODO guard this in isState(initiated,initiated_approved,accepted) otherwise it might fire too late
714            sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
715        }
716    }
717
718    private void updateEndUserState() {
719        xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, getEndUserState());
720    }
721
722    private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
723        if (id.account.getXmppConnection().getFeatures().extendedServiceDiscovery()) {
724            final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
725            request.setTo(Jid.of(id.account.getJid().getDomain()));
726            request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
727            xmppConnectionService.sendIqPacket(id.account, request, (account, response) -> {
728                ImmutableList.Builder<PeerConnection.IceServer> listBuilder = new ImmutableList.Builder<>();
729                if (response.getType() == IqPacket.TYPE.RESULT) {
730                    final Element services = response.findChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
731                    final List<Element> children = services == null ? Collections.emptyList() : services.getChildren();
732                    for (final Element child : children) {
733                        if ("service".equals(child.getName())) {
734                            final String type = child.getAttribute("type");
735                            final String host = child.getAttribute("host");
736                            final String sport = child.getAttribute("port");
737                            final Integer port = sport == null ? null : Ints.tryParse(sport);
738                            final String transport = child.getAttribute("transport");
739                            final String username = child.getAttribute("username");
740                            final String password = child.getAttribute("password");
741                            if (Strings.isNullOrEmpty(host) || port == null) {
742                                continue;
743                            }
744                            if (port < 0 || port > 65535) {
745                                continue;
746                            }
747                            if (Arrays.asList("stun", "turn").contains(type) || Arrays.asList("udp", "tcp").contains(transport)) {
748                                //TODO wrap ipv6 addresses
749                                PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer.builder(String.format("%s:%s:%s?transport=%s", type, host, port, transport));
750                                if (username != null && password != null) {
751                                    iceServerBuilder.setUsername(username);
752                                    iceServerBuilder.setPassword(password);
753                                } else if (Arrays.asList("turn", "turns").contains(type)) {
754                                    //The WebRTC spec requires throwing an InvalidAccessError when username (from libwebrtc source coder)
755                                    //https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
756                                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping " + type + "/" + transport + " without username and password");
757                                    continue;
758                                }
759                                final PeerConnection.IceServer iceServer = iceServerBuilder.createIceServer();
760                                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": discovered ICE Server: " + iceServer);
761                                listBuilder.add(iceServer);
762                            }
763                        }
764                    }
765                }
766                List<PeerConnection.IceServer> iceServers = listBuilder.build();
767                if (iceServers.size() == 0) {
768                    Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no ICE server found " + response);
769                }
770                onIceServersDiscovered.onIceServersDiscovered(iceServers);
771            });
772        } else {
773            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": has no external service discovery");
774            onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
775        }
776    }
777
778    private interface OnIceServersDiscovered {
779        void onIceServersDiscovered(List<PeerConnection.IceServer> iceServers);
780    }
781}