JingleRtpConnection.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.os.SystemClock;
   4import android.util.Log;
   5
   6import androidx.annotation.NonNull;
   7import androidx.annotation.Nullable;
   8
   9import com.google.common.base.Optional;
  10import com.google.common.base.Preconditions;
  11import com.google.common.base.Strings;
  12import com.google.common.base.Throwables;
  13import com.google.common.collect.Collections2;
  14import com.google.common.collect.ImmutableList;
  15import com.google.common.collect.ImmutableMap;
  16import com.google.common.collect.Sets;
  17import com.google.common.primitives.Ints;
  18import com.google.common.util.concurrent.FutureCallback;
  19import com.google.common.util.concurrent.Futures;
  20import com.google.common.util.concurrent.ListenableFuture;
  21import com.google.common.util.concurrent.MoreExecutors;
  22
  23import org.webrtc.EglBase;
  24import org.webrtc.IceCandidate;
  25import org.webrtc.PeerConnection;
  26import org.webrtc.VideoTrack;
  27
  28import java.util.ArrayDeque;
  29import java.util.Arrays;
  30import java.util.Collection;
  31import java.util.Collections;
  32import java.util.List;
  33import java.util.Map;
  34import java.util.Set;
  35import java.util.concurrent.ScheduledFuture;
  36import java.util.concurrent.TimeUnit;
  37
  38import eu.siacs.conversations.Config;
  39import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  40import eu.siacs.conversations.crypto.axolotl.CryptoFailedException;
  41import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  42import eu.siacs.conversations.entities.Account;
  43import eu.siacs.conversations.entities.Conversation;
  44import eu.siacs.conversations.entities.Conversational;
  45import eu.siacs.conversations.entities.Message;
  46import eu.siacs.conversations.entities.RtpSessionStatus;
  47import eu.siacs.conversations.services.AppRTCAudioManager;
  48import eu.siacs.conversations.utils.IP;
  49import eu.siacs.conversations.xml.Element;
  50import eu.siacs.conversations.xml.Namespace;
  51import eu.siacs.conversations.xmpp.Jid;
  52import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
  53import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
  54import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  55import eu.siacs.conversations.xmpp.jingle.stanzas.Proceed;
  56import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  57import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  58import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  59import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  60import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  61
  62public class JingleRtpConnection extends AbstractJingleConnection implements WebRTCWrapper.EventCallback {
  63
  64    public static final List<State> STATES_SHOWING_ONGOING_CALL = Arrays.asList(
  65            State.PROCEED,
  66            State.SESSION_INITIALIZED_PRE_APPROVED,
  67            State.SESSION_ACCEPTED
  68    );
  69    private static final long BUSY_TIME_OUT = 30;
  70    private static final List<State> TERMINATED = Arrays.asList(
  71            State.ACCEPTED,
  72            State.REJECTED,
  73            State.REJECTED_RACED,
  74            State.RETRACTED,
  75            State.RETRACTED_RACED,
  76            State.TERMINATED_SUCCESS,
  77            State.TERMINATED_DECLINED_OR_BUSY,
  78            State.TERMINATED_CONNECTIVITY_ERROR,
  79            State.TERMINATED_CANCEL_OR_TIMEOUT,
  80            State.TERMINATED_APPLICATION_FAILURE,
  81            State.TERMINATED_SECURITY_ERROR
  82    );
  83
  84    private static final Map<State, Collection<State>> VALID_TRANSITIONS;
  85
  86    static {
  87        final ImmutableMap.Builder<State, Collection<State>> transitionBuilder = new ImmutableMap.Builder<>();
  88        transitionBuilder.put(State.NULL, ImmutableList.of(
  89                State.PROPOSED,
  90                State.SESSION_INITIALIZED,
  91                State.TERMINATED_APPLICATION_FAILURE,
  92                State.TERMINATED_SECURITY_ERROR
  93        ));
  94        transitionBuilder.put(State.PROPOSED, ImmutableList.of(
  95                State.ACCEPTED,
  96                State.PROCEED,
  97                State.REJECTED,
  98                State.RETRACTED,
  99                State.TERMINATED_APPLICATION_FAILURE,
 100                State.TERMINATED_SECURITY_ERROR,
 101                State.TERMINATED_CONNECTIVITY_ERROR //only used when the xmpp connection rebinds
 102        ));
 103        transitionBuilder.put(State.PROCEED, ImmutableList.of(
 104                State.REJECTED_RACED,
 105                State.RETRACTED_RACED,
 106                State.SESSION_INITIALIZED_PRE_APPROVED,
 107                State.TERMINATED_SUCCESS,
 108                State.TERMINATED_APPLICATION_FAILURE,
 109                State.TERMINATED_SECURITY_ERROR,
 110                State.TERMINATED_CONNECTIVITY_ERROR //at this state used for error bounces of the proceed message
 111        ));
 112        transitionBuilder.put(State.SESSION_INITIALIZED, ImmutableList.of(
 113                State.SESSION_ACCEPTED,
 114                State.TERMINATED_SUCCESS,
 115                State.TERMINATED_DECLINED_OR_BUSY,
 116                State.TERMINATED_CONNECTIVITY_ERROR,  //at this state used for IQ errors and IQ timeouts
 117                State.TERMINATED_CANCEL_OR_TIMEOUT,
 118                State.TERMINATED_APPLICATION_FAILURE,
 119                State.TERMINATED_SECURITY_ERROR
 120        ));
 121        transitionBuilder.put(State.SESSION_INITIALIZED_PRE_APPROVED, ImmutableList.of(
 122                State.SESSION_ACCEPTED,
 123                State.TERMINATED_SUCCESS,
 124                State.TERMINATED_DECLINED_OR_BUSY,
 125                State.TERMINATED_CONNECTIVITY_ERROR,  //at this state used for IQ errors and IQ timeouts
 126                State.TERMINATED_CANCEL_OR_TIMEOUT,
 127                State.TERMINATED_APPLICATION_FAILURE,
 128                State.TERMINATED_SECURITY_ERROR
 129        ));
 130        transitionBuilder.put(State.SESSION_ACCEPTED, ImmutableList.of(
 131                State.TERMINATED_SUCCESS,
 132                State.TERMINATED_DECLINED_OR_BUSY,
 133                State.TERMINATED_CONNECTIVITY_ERROR,
 134                State.TERMINATED_CANCEL_OR_TIMEOUT,
 135                State.TERMINATED_APPLICATION_FAILURE,
 136                State.TERMINATED_SECURITY_ERROR
 137        ));
 138        VALID_TRANSITIONS = transitionBuilder.build();
 139    }
 140
 141    private final WebRTCWrapper webRTCWrapper = new WebRTCWrapper(this);
 142    private final ArrayDeque<Set<Map.Entry<String, RtpContentMap.DescriptionTransport>>> pendingIceCandidates = new ArrayDeque<>();
 143    private final OmemoVerification omemoVerification = new OmemoVerification();
 144    private final Message message;
 145    private State state = State.NULL;
 146    private StateTransitionException stateTransitionException;
 147    private Set<Media> proposedMedia;
 148    private RtpContentMap initiatorRtpContentMap;
 149    private RtpContentMap responderRtpContentMap;
 150    private long rtpConnectionStarted = 0; //time of 'connected'
 151    private long rtpConnectionEnded = 0;
 152    private ScheduledFuture<?> ringingTimeoutFuture;
 153
 154    JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
 155        super(jingleConnectionManager, id, initiator);
 156        final Conversation conversation = jingleConnectionManager.getXmppConnectionService().findOrCreateConversation(
 157                id.account,
 158                id.with.asBareJid(),
 159                false,
 160                false
 161        );
 162        this.message = new Message(
 163                conversation,
 164                isInitiator() ? Message.STATUS_SEND : Message.STATUS_RECEIVED,
 165                Message.TYPE_RTP_SESSION,
 166                id.sessionId
 167        );
 168    }
 169
 170    private static State reasonToState(Reason reason) {
 171        switch (reason) {
 172            case SUCCESS:
 173                return State.TERMINATED_SUCCESS;
 174            case DECLINE:
 175            case BUSY:
 176                return State.TERMINATED_DECLINED_OR_BUSY;
 177            case CANCEL:
 178            case TIMEOUT:
 179                return State.TERMINATED_CANCEL_OR_TIMEOUT;
 180            case SECURITY_ERROR:
 181                return State.TERMINATED_SECURITY_ERROR;
 182            case FAILED_APPLICATION:
 183            case UNSUPPORTED_TRANSPORTS:
 184            case UNSUPPORTED_APPLICATIONS:
 185                return State.TERMINATED_APPLICATION_FAILURE;
 186            default:
 187                return State.TERMINATED_CONNECTIVITY_ERROR;
 188        }
 189    }
 190
 191    @Override
 192    synchronized void deliverPacket(final JinglePacket jinglePacket) {
 193        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": packet delivered to JingleRtpConnection");
 194        switch (jinglePacket.getAction()) {
 195            case SESSION_INITIATE:
 196                receiveSessionInitiate(jinglePacket);
 197                break;
 198            case TRANSPORT_INFO:
 199                receiveTransportInfo(jinglePacket);
 200                break;
 201            case SESSION_ACCEPT:
 202                receiveSessionAccept(jinglePacket);
 203                break;
 204            case SESSION_TERMINATE:
 205                receiveSessionTerminate(jinglePacket);
 206                break;
 207            default:
 208                respondOk(jinglePacket);
 209                Log.d(Config.LOGTAG, String.format("%s: received unhandled jingle action %s", id.account.getJid().asBareJid(), jinglePacket.getAction()));
 210                break;
 211        }
 212    }
 213
 214    @Override
 215    synchronized void notifyRebound() {
 216        if (isTerminated()) {
 217            return;
 218        }
 219        webRTCWrapper.close();
 220        if (!isInitiator() && isInState(State.PROPOSED, State.SESSION_INITIALIZED)) {
 221            xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 222        }
 223        if (isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
 224            //we might have already changed resources (full jid) at this point; so this might not even reach the other party
 225            sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
 226        } else {
 227            transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
 228            finish();
 229        }
 230    }
 231
 232    private void receiveSessionTerminate(final JinglePacket jinglePacket) {
 233        respondOk(jinglePacket);
 234        final JinglePacket.ReasonWrapper wrapper = jinglePacket.getReason();
 235        final State previous = this.state;
 236        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session terminate reason=" + wrapper.reason + "(" + Strings.nullToEmpty(wrapper.text) + ") while in state " + previous);
 237        if (TERMINATED.contains(previous)) {
 238            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring session terminate because already in " + previous);
 239            return;
 240        }
 241        webRTCWrapper.close();
 242        final State target = reasonToState(wrapper.reason);
 243        transitionOrThrow(target);
 244        writeLogMessage(target);
 245        if (previous == State.PROPOSED || previous == State.SESSION_INITIALIZED) {
 246            xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 247        }
 248        finish();
 249    }
 250
 251    private void receiveTransportInfo(final JinglePacket jinglePacket) {
 252        //Due to the asynchronicity of processing session-init we might move from NULL|PROCEED to INITIALIZED only after transport-info has been received
 253        if (isInState(State.NULL, State.PROCEED, State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
 254            respondOk(jinglePacket);
 255            final RtpContentMap contentMap;
 256            try {
 257                contentMap = RtpContentMap.of(jinglePacket);
 258            } catch (IllegalArgumentException | NullPointerException e) {
 259                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents; ignoring", e);
 260                return;
 261            }
 262            final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> candidates = contentMap.contents.entrySet();
 263            if (this.state == State.SESSION_ACCEPTED) {
 264                try {
 265                    processCandidates(candidates);
 266                } catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
 267                    Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnection was not initialized when processing transport info. this usually indicates a race condition that can be ignored");
 268                }
 269            } else {
 270                pendingIceCandidates.push(candidates);
 271            }
 272        } else {
 273            if (isTerminated()) {
 274                respondOk(jinglePacket);
 275                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring out-of-order transport info; we where already terminated");
 276            } else {
 277                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received transport info while in state=" + this.state);
 278                terminateWithOutOfOrder(jinglePacket);
 279            }
 280        }
 281    }
 282
 283    private void processCandidates(final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> contents) {
 284        final RtpContentMap rtpContentMap = isInitiator() ? this.responderRtpContentMap : this.initiatorRtpContentMap;
 285        final Group originalGroup = rtpContentMap.group;
 286        final List<String> identificationTags = originalGroup == null ? rtpContentMap.getNames() : originalGroup.getIdentificationTags();
 287        if (identificationTags.size() == 0) {
 288            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no identification tags found in initial offer. we won't be able to calculate mLineIndices");
 289        }
 290        processCandidates(identificationTags, contents);
 291    }
 292
 293    private void processCandidates(final List<String> indices, final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> contents) {
 294        for (final Map.Entry<String, RtpContentMap.DescriptionTransport> content : contents) {
 295            final String ufrag = content.getValue().transport.getAttribute("ufrag");
 296            for (final IceUdpTransportInfo.Candidate candidate : content.getValue().transport.getCandidates()) {
 297                final String sdp;
 298                try {
 299                    sdp = candidate.toSdpAttribute(ufrag);
 300                } catch (IllegalArgumentException e) {
 301                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring invalid ICE candidate " + e.getMessage());
 302                    continue;
 303                }
 304                final String sdpMid = content.getKey();
 305                final int mLineIndex = indices.indexOf(sdpMid);
 306                if (mLineIndex < 0) {
 307                    Log.w(Config.LOGTAG, "mLineIndex not found for " + sdpMid + ". available indices " + indices);
 308                }
 309                final IceCandidate iceCandidate = new IceCandidate(sdpMid, mLineIndex, sdp);
 310                Log.d(Config.LOGTAG, "received candidate: " + iceCandidate);
 311                this.webRTCWrapper.addIceCandidate(iceCandidate);
 312            }
 313        }
 314    }
 315
 316    private ListenableFuture<RtpContentMap> receiveRtpContentMap(final JinglePacket jinglePacket, final boolean expectVerification) {
 317        final RtpContentMap receivedContentMap = RtpContentMap.of(jinglePacket);
 318        if (receivedContentMap instanceof OmemoVerifiedRtpContentMap) {
 319            final ListenableFuture<AxolotlService.OmemoVerifiedPayload<RtpContentMap>> future = id.account.getAxolotlService().decrypt((OmemoVerifiedRtpContentMap) receivedContentMap, id.with);
 320            return Futures.transform(future, omemoVerifiedPayload -> {
 321                //TODO test if an exception here triggers a correct abort
 322                omemoVerification.setOrEnsureEqual(omemoVerifiedPayload);
 323                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received verifiable DTLS fingerprint via " + omemoVerification);
 324                return omemoVerifiedPayload.getPayload();
 325            }, MoreExecutors.directExecutor());
 326        } else if (Config.REQUIRE_RTP_VERIFICATION || expectVerification) {
 327            return Futures.immediateFailedFuture(
 328                    new SecurityException("DTLS fingerprint was unexpectedly not verifiable")
 329            );
 330        } else {
 331            return Futures.immediateFuture(receivedContentMap);
 332        }
 333    }
 334
 335    private void receiveSessionInitiate(final JinglePacket jinglePacket) {
 336        if (isInitiator()) {
 337            Log.d(Config.LOGTAG, String.format("%s: received session-initiate even though we were initiating", id.account.getJid().asBareJid()));
 338            if (isTerminated()) {
 339                Log.d(Config.LOGTAG, String.format(
 340                        "%s: got a reason to terminate with out-of-order. but already in state %s",
 341                        id.account.getJid().asBareJid(),
 342                        getState()
 343                ));
 344                respondWithOutOfOrder(jinglePacket);
 345            } else {
 346                terminateWithOutOfOrder(jinglePacket);
 347            }
 348            return;
 349        }
 350        final ListenableFuture<RtpContentMap> future = receiveRtpContentMap(jinglePacket, false);
 351        Futures.addCallback(future, new FutureCallback<RtpContentMap>() {
 352            @Override
 353            public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
 354                receiveSessionInitiate(jinglePacket, rtpContentMap);
 355            }
 356
 357            @Override
 358            public void onFailure(@NonNull final Throwable throwable) {
 359                respondOk(jinglePacket);
 360                sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
 361            }
 362        }, MoreExecutors.directExecutor());
 363    }
 364
 365    private void receiveSessionInitiate(final JinglePacket jinglePacket, final RtpContentMap contentMap) {
 366        try {
 367            contentMap.requireContentDescriptions();
 368            contentMap.requireDTLSFingerprint();
 369        } catch (final RuntimeException e) {
 370            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents", Throwables.getRootCause(e));
 371            respondOk(jinglePacket);
 372            sendSessionTerminate(Reason.of(e), e.getMessage());
 373            return;
 374        }
 375        Log.d(Config.LOGTAG, "processing session-init with " + contentMap.contents.size() + " contents");
 376        final State target;
 377        if (this.state == State.PROCEED) {
 378            Preconditions.checkState(
 379                    proposedMedia != null && proposedMedia.size() > 0,
 380                    "proposed media must be set when processing pre-approved session-initiate"
 381            );
 382            if (!this.proposedMedia.equals(contentMap.getMedia())) {
 383                sendSessionTerminate(Reason.SECURITY_ERROR, String.format(
 384                        "Your session proposal (Jingle Message Initiation) included media %s but your session-initiate was %s",
 385                        this.proposedMedia,
 386                        contentMap.getMedia()
 387                ));
 388                return;
 389            }
 390            target = State.SESSION_INITIALIZED_PRE_APPROVED;
 391        } else {
 392            target = State.SESSION_INITIALIZED;
 393        }
 394        if (transition(target, () -> this.initiatorRtpContentMap = contentMap)) {
 395            respondOk(jinglePacket);
 396
 397            final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> candidates = contentMap.contents.entrySet();
 398            if (candidates.size() > 0) {
 399                pendingIceCandidates.push(candidates);
 400            }
 401            if (target == State.SESSION_INITIALIZED_PRE_APPROVED) {
 402                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": automatically accepting session-initiate");
 403                sendSessionAccept();
 404            } else {
 405                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received not pre-approved session-initiate. start ringing");
 406                startRinging();
 407            }
 408        } else {
 409            Log.d(Config.LOGTAG, String.format("%s: received session-initiate while in state %s", id.account.getJid().asBareJid(), state));
 410            terminateWithOutOfOrder(jinglePacket);
 411        }
 412    }
 413
 414    private void receiveSessionAccept(final JinglePacket jinglePacket) {
 415        if (!isInitiator()) {
 416            Log.d(Config.LOGTAG, String.format("%s: received session-accept even though we were responding", id.account.getJid().asBareJid()));
 417            terminateWithOutOfOrder(jinglePacket);
 418            return;
 419        }
 420        final ListenableFuture<RtpContentMap> future = receiveRtpContentMap(jinglePacket, this.omemoVerification.hasFingerprint());
 421        Futures.addCallback(future, new FutureCallback<RtpContentMap>() {
 422            @Override
 423            public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
 424                receiveSessionAccept(jinglePacket, rtpContentMap);
 425            }
 426
 427            @Override
 428            public void onFailure(@NonNull final Throwable throwable) {
 429                respondOk(jinglePacket);
 430                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents in session-accept", throwable);
 431                webRTCWrapper.close();
 432                sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
 433            }
 434        }, MoreExecutors.directExecutor());
 435    }
 436
 437    private void receiveSessionAccept(final JinglePacket jinglePacket, final RtpContentMap contentMap) {
 438        try {
 439            contentMap.requireContentDescriptions();
 440            contentMap.requireDTLSFingerprint();
 441        } catch (final RuntimeException e) {
 442            respondOk(jinglePacket);
 443            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents in session-accept", e);
 444            webRTCWrapper.close();
 445            sendSessionTerminate(Reason.of(e), e.getMessage());
 446            return;
 447        }
 448        final Set<Media> initiatorMedia = this.initiatorRtpContentMap.getMedia();
 449        if (!initiatorMedia.equals(contentMap.getMedia())) {
 450            sendSessionTerminate(Reason.SECURITY_ERROR, String.format(
 451                    "Your session-included included media %s but our session-initiate was %s",
 452                    this.proposedMedia,
 453                    contentMap.getMedia()
 454            ));
 455            return;
 456        }
 457        Log.d(Config.LOGTAG, "processing session-accept with " + contentMap.contents.size() + " contents");
 458        if (transition(State.SESSION_ACCEPTED)) {
 459            respondOk(jinglePacket);
 460            receiveSessionAccept(contentMap);
 461        } else {
 462            Log.d(Config.LOGTAG, String.format("%s: received session-accept while in state %s", id.account.getJid().asBareJid(), state));
 463            respondOk(jinglePacket);
 464        }
 465    }
 466
 467    private void receiveSessionAccept(final RtpContentMap contentMap) {
 468        this.responderRtpContentMap = contentMap;
 469        final SessionDescription sessionDescription;
 470        try {
 471            sessionDescription = SessionDescription.of(contentMap);
 472        } catch (final IllegalArgumentException | NullPointerException e) {
 473            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-accept to SDP", e);
 474            webRTCWrapper.close();
 475            sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
 476            return;
 477        }
 478        final org.webrtc.SessionDescription answer = new org.webrtc.SessionDescription(
 479                org.webrtc.SessionDescription.Type.ANSWER,
 480                sessionDescription.toString()
 481        );
 482        try {
 483            this.webRTCWrapper.setRemoteDescription(answer).get();
 484        } catch (final Exception e) {
 485            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to set remote description after receiving session-accept", Throwables.getRootCause(e));
 486            webRTCWrapper.close();
 487            sendSessionTerminate(Reason.FAILED_APPLICATION);
 488            return;
 489        }
 490        final List<String> identificationTags = contentMap.group == null ? contentMap.getNames() : contentMap.group.getIdentificationTags();
 491        processCandidates(identificationTags, contentMap.contents.entrySet());
 492    }
 493
 494    private void sendSessionAccept() {
 495        final RtpContentMap rtpContentMap = this.initiatorRtpContentMap;
 496        if (rtpContentMap == null) {
 497            throw new IllegalStateException("initiator RTP Content Map has not been set");
 498        }
 499        final SessionDescription offer;
 500        try {
 501            offer = SessionDescription.of(rtpContentMap);
 502        } catch (final IllegalArgumentException | NullPointerException e) {
 503            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-initiate to SDP", e);
 504            webRTCWrapper.close();
 505            sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
 506            return;
 507        }
 508        sendSessionAccept(rtpContentMap.getMedia(), offer);
 509    }
 510
 511    private void sendSessionAccept(final Set<Media> media, final SessionDescription offer) {
 512        discoverIceServers(iceServers -> sendSessionAccept(media, offer, iceServers));
 513    }
 514
 515    private synchronized void sendSessionAccept(final Set<Media> media, final SessionDescription offer, final List<PeerConnection.IceServer> iceServers) {
 516        if (isTerminated()) {
 517            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": ICE servers got discovered when session was already terminated. nothing to do.");
 518            return;
 519        }
 520        try {
 521            setupWebRTC(media, iceServers);
 522        } catch (final WebRTCWrapper.InitializationException e) {
 523            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
 524            webRTCWrapper.close();
 525            sendSessionTerminate(Reason.FAILED_APPLICATION);
 526            return;
 527        }
 528        final org.webrtc.SessionDescription sdp = new org.webrtc.SessionDescription(
 529                org.webrtc.SessionDescription.Type.OFFER,
 530                offer.toString()
 531        );
 532        try {
 533            this.webRTCWrapper.setRemoteDescription(sdp).get();
 534            addIceCandidatesFromBlackLog();
 535            org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createAnswer().get();
 536            prepareSessionAccept(webRTCSessionDescription);
 537        } catch (final Exception e) {
 538            failureToAcceptSession(e);
 539        }
 540    }
 541
 542    private void failureToAcceptSession(final Throwable throwable) {
 543        if (isTerminated()) {
 544            return;
 545        }
 546        Log.d(Config.LOGTAG, "unable to send session accept", Throwables.getRootCause(throwable));
 547        webRTCWrapper.close();
 548        sendSessionTerminate(Reason.ofThrowable(throwable));
 549    }
 550
 551    private void addIceCandidatesFromBlackLog() {
 552        while (!this.pendingIceCandidates.isEmpty()) {
 553            processCandidates(this.pendingIceCandidates.poll());
 554            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": added candidates from back log");
 555        }
 556    }
 557
 558    private void prepareSessionAccept(final org.webrtc.SessionDescription webRTCSessionDescription) {
 559        final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
 560        final RtpContentMap respondingRtpContentMap = RtpContentMap.of(sessionDescription);
 561        this.responderRtpContentMap = respondingRtpContentMap;
 562        final ListenableFuture<RtpContentMap> outgoingContentMapFuture = prepareOutgoingContentMap(respondingRtpContentMap);
 563        Futures.addCallback(outgoingContentMapFuture,
 564                new FutureCallback<RtpContentMap>() {
 565                    @Override
 566                    public void onSuccess(final RtpContentMap outgoingContentMap) {
 567                        sendSessionAccept(outgoingContentMap, webRTCSessionDescription);
 568                    }
 569
 570                    @Override
 571                    public void onFailure(@NonNull Throwable throwable) {
 572                        failureToAcceptSession(throwable);
 573                    }
 574                },
 575                MoreExecutors.directExecutor()
 576        );
 577    }
 578
 579    private void sendSessionAccept(final RtpContentMap rtpContentMap, final org.webrtc.SessionDescription webRTCSessionDescription) {
 580        if (isTerminated()) {
 581            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": preparing session accept was too slow. already terminated. nothing to do.");
 582            return;
 583        }
 584        transitionOrThrow(State.SESSION_ACCEPTED);
 585        final JinglePacket sessionAccept = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_ACCEPT, id.sessionId);
 586        send(sessionAccept);
 587        try {
 588            webRTCWrapper.setLocalDescription(webRTCSessionDescription).get();
 589        } catch (Exception e) {
 590            failureToAcceptSession(e);
 591        }
 592    }
 593
 594    private ListenableFuture<RtpContentMap> prepareOutgoingContentMap(final RtpContentMap rtpContentMap) {
 595        if (this.omemoVerification.hasDeviceId()) {
 596            ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>> verifiedPayloadFuture = id.account.getAxolotlService()
 597                    .encrypt(rtpContentMap, id.with, omemoVerification.getDeviceId());
 598            return Futures.transform(verifiedPayloadFuture, verifiedPayload -> {
 599                omemoVerification.setOrEnsureEqual(verifiedPayload);
 600                return verifiedPayload.getPayload();
 601            }, MoreExecutors.directExecutor());
 602        } else {
 603            return Futures.immediateFuture(rtpContentMap);
 604        }
 605    }
 606
 607    synchronized void deliveryMessage(final Jid from, final Element message, final String serverMessageId, final long timestamp) {
 608        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": delivered message to JingleRtpConnection " + message);
 609        switch (message.getName()) {
 610            case "propose":
 611                receivePropose(from, Propose.upgrade(message), serverMessageId, timestamp);
 612                break;
 613            case "proceed":
 614                receiveProceed(from, Proceed.upgrade(message), serverMessageId, timestamp);
 615                break;
 616            case "retract":
 617                receiveRetract(from, serverMessageId, timestamp);
 618                break;
 619            case "reject":
 620                receiveReject(from, serverMessageId, timestamp);
 621                break;
 622            case "accept":
 623                receiveAccept(from, serverMessageId, timestamp);
 624                break;
 625            default:
 626                break;
 627        }
 628    }
 629
 630    void deliverFailedProceed() {
 631        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": receive message error for proceed message");
 632        if (transition(State.TERMINATED_CONNECTIVITY_ERROR)) {
 633            webRTCWrapper.close();
 634            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into connectivity error");
 635            this.finish();
 636        }
 637    }
 638
 639    private void receiveAccept(final Jid from, final String serverMsgId, final long timestamp) {
 640        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
 641        if (originatedFromMyself) {
 642            if (transition(State.ACCEPTED)) {
 643                if (serverMsgId != null) {
 644                    this.message.setServerMsgId(serverMsgId);
 645                }
 646                this.message.setTime(timestamp);
 647                this.message.setCarbon(true); //indicate that call was accepted on other device
 648                this.writeLogMessageSuccess(0);
 649                this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 650                this.finish();
 651            } else {
 652                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to transition to accept because already in state=" + this.state);
 653            }
 654        } else {
 655            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring 'accept' from " + from);
 656        }
 657    }
 658
 659    private void receiveReject(final Jid from, final String serverMsgId, final long timestamp) {
 660        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
 661        //reject from another one of my clients
 662        if (originatedFromMyself) {
 663            receiveRejectFromMyself(serverMsgId, timestamp);
 664        } else if (isInitiator()) {
 665            if (from.equals(id.with)) {
 666                receiveRejectFromResponder();
 667            } else {
 668                Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from " + from + " for session with " + id.with);
 669            }
 670        } else {
 671            Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from " + from + " for session with " + id.with);
 672        }
 673    }
 674
 675    private void receiveRejectFromMyself(String serverMsgId, long timestamp) {
 676        if (transition(State.REJECTED)) {
 677            this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 678            this.finish();
 679            if (serverMsgId != null) {
 680                this.message.setServerMsgId(serverMsgId);
 681            }
 682            this.message.setTime(timestamp);
 683            this.message.setCarbon(true); //indicate that call was rejected on other device
 684            writeLogMessageMissed();
 685        } else {
 686            Log.d(Config.LOGTAG, "not able to transition into REJECTED because already in " + this.state);
 687        }
 688    }
 689
 690    private void receiveRejectFromResponder() {
 691        if (isInState(State.PROCEED)) {
 692            Log.d(Config.LOGTAG, id.account.getJid() + ": received reject while still in proceed. callee reconsidered");
 693            closeTransitionLogFinish(State.REJECTED_RACED);
 694            return;
 695        }
 696        if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED)) {
 697            Log.d(Config.LOGTAG, id.account.getJid() + ": received reject while in SESSION_INITIATED_PRE_APPROVED. callee reconsidered before receiving session-init");
 698            closeTransitionLogFinish(State.TERMINATED_DECLINED_OR_BUSY);
 699            return;
 700        }
 701        Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from responder because already in state " + this.state);
 702    }
 703
 704    private void receivePropose(final Jid from, final Propose propose, final String serverMsgId, final long timestamp) {
 705        final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
 706        if (originatedFromMyself) {
 707            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": saw proposal from myself. ignoring");
 708        } else if (transition(State.PROPOSED, () -> {
 709            final Collection<RtpDescription> descriptions = Collections2.transform(
 710                    Collections2.filter(propose.getDescriptions(), d -> d instanceof RtpDescription),
 711                    input -> (RtpDescription) input
 712            );
 713            final Collection<Media> media = Collections2.transform(descriptions, RtpDescription::getMedia);
 714            Preconditions.checkState(!media.contains(Media.UNKNOWN), "RTP descriptions contain unknown media");
 715            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session proposal from " + from + " for " + media);
 716            this.proposedMedia = Sets.newHashSet(media);
 717        })) {
 718            if (serverMsgId != null) {
 719                this.message.setServerMsgId(serverMsgId);
 720            }
 721            this.message.setTime(timestamp);
 722            startRinging();
 723        } else {
 724            Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring session proposal because already in " + state);
 725        }
 726    }
 727
 728    private void startRinging() {
 729        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received call from " + id.with + ". start ringing");
 730        ringingTimeoutFuture = jingleConnectionManager.schedule(this::ringingTimeout, BUSY_TIME_OUT, TimeUnit.SECONDS);
 731        xmppConnectionService.getNotificationService().startRinging(id, getMedia());
 732    }
 733
 734    private synchronized void ringingTimeout() {
 735        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": timeout reached for ringing");
 736        switch (this.state) {
 737            case PROPOSED:
 738                message.markUnread();
 739                rejectCallFromProposed();
 740                break;
 741            case SESSION_INITIALIZED:
 742                message.markUnread();
 743                rejectCallFromSessionInitiate();
 744                break;
 745        }
 746    }
 747
 748    private void cancelRingingTimeout() {
 749        final ScheduledFuture<?> future = this.ringingTimeoutFuture;
 750        if (future != null && !future.isCancelled()) {
 751            future.cancel(false);
 752        }
 753    }
 754
 755    private void receiveProceed(final Jid from, final Proceed proceed, final String serverMsgId, final long timestamp) {
 756        final Set<Media> media = Preconditions.checkNotNull(this.proposedMedia, "Proposed media has to be set before handling proceed");
 757        Preconditions.checkState(media.size() > 0, "Proposed media should not be empty");
 758        if (from.equals(id.with)) {
 759            if (isInitiator()) {
 760                if (transition(State.PROCEED)) {
 761                    if (serverMsgId != null) {
 762                        this.message.setServerMsgId(serverMsgId);
 763                    }
 764                    this.message.setTime(timestamp);
 765                    final Integer remoteDeviceId = proceed.getDeviceId();
 766                    if (isOmemoEnabled()) {
 767                        this.omemoVerification.setDeviceId(remoteDeviceId);
 768                    } else {
 769                        if (remoteDeviceId != null) {
 770                            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": remote party signaled support for OMEMO verification but we have OMEMO disabled");
 771                        }
 772                        this.omemoVerification.setDeviceId(null);
 773                    }
 774                    this.sendSessionInitiate(media, State.SESSION_INITIALIZED_PRE_APPROVED);
 775                } else {
 776                    Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because already in %s", id.account.getJid().asBareJid(), this.state));
 777                }
 778            } else {
 779                Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because we were not initializing", id.account.getJid().asBareJid()));
 780            }
 781        } else if (from.asBareJid().equals(id.account.getJid().asBareJid())) {
 782            if (transition(State.ACCEPTED)) {
 783                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": moved session with " + id.with + " into state accepted after received carbon copied procced");
 784                this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 785                this.finish();
 786            }
 787        } else {
 788            Log.d(Config.LOGTAG, String.format("%s: ignoring proceed from %s. was expected from %s", id.account.getJid().asBareJid(), from, id.with));
 789        }
 790    }
 791
 792    private void receiveRetract(final Jid from, final String serverMsgId, final long timestamp) {
 793        if (from.equals(id.with)) {
 794            final State target = this.state == State.PROCEED ? State.RETRACTED_RACED : State.RETRACTED;
 795            if (transition(target)) {
 796                xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
 797                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": session with " + id.with + " has been retracted (serverMsgId=" + serverMsgId + ")");
 798                if (serverMsgId != null) {
 799                    this.message.setServerMsgId(serverMsgId);
 800                }
 801                this.message.setTime(timestamp);
 802                if (target == State.RETRACTED) {
 803                    this.message.markUnread();
 804                }
 805                writeLogMessageMissed();
 806                finish();
 807            } else {
 808                Log.d(Config.LOGTAG, "ignoring retract because already in " + this.state);
 809            }
 810        } else {
 811            //TODO parse retract from self
 812            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received retract from " + from + ". expected retract from" + id.with + ". ignoring");
 813        }
 814    }
 815
 816    public void sendSessionInitiate() {
 817        sendSessionInitiate(this.proposedMedia, State.SESSION_INITIALIZED);
 818    }
 819
 820    private void sendSessionInitiate(final Set<Media> media, final State targetState) {
 821        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": prepare session-initiate");
 822        discoverIceServers(iceServers -> sendSessionInitiate(media, targetState, iceServers));
 823    }
 824
 825    private synchronized void sendSessionInitiate(final Set<Media> media, final State targetState, final List<PeerConnection.IceServer> iceServers) {
 826        if (isTerminated()) {
 827            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": ICE servers got discovered when session was already terminated. nothing to do.");
 828            return;
 829        }
 830        try {
 831            setupWebRTC(media, iceServers);
 832        } catch (final WebRTCWrapper.InitializationException e) {
 833            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
 834            webRTCWrapper.close();
 835            sendRetract(Reason.ofThrowable(e));
 836            return;
 837        }
 838        try {
 839            org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.createOffer().get();
 840            prepareSessionInitiate(webRTCSessionDescription, targetState);
 841        } catch (final Exception e) {
 842            failureToInitiateSession(e, targetState);
 843        }
 844    }
 845
 846    private void failureToInitiateSession(final Throwable throwable, final State targetState) {
 847        if (isTerminated()) {
 848            return;
 849        }
 850        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to sendSessionInitiate", Throwables.getRootCause(throwable));
 851        webRTCWrapper.close();
 852        final Reason reason = Reason.ofThrowable(throwable);
 853        if (isInState(targetState)) {
 854            sendSessionTerminate(reason);
 855        } else {
 856            sendRetract(reason);
 857        }
 858    }
 859
 860    private void sendRetract(final Reason reason) {
 861        //TODO embed reason into retract
 862        sendJingleMessage("retract", id.with.asBareJid());
 863        transitionOrThrow(reasonToState(reason));
 864        this.finish();
 865    }
 866
 867    private void prepareSessionInitiate(final org.webrtc.SessionDescription webRTCSessionDescription, final State targetState) {
 868        final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
 869        final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription);
 870        this.initiatorRtpContentMap = rtpContentMap;
 871        final ListenableFuture<RtpContentMap> outgoingContentMapFuture = encryptSessionInitiate(rtpContentMap);
 872        Futures.addCallback(outgoingContentMapFuture, new FutureCallback<RtpContentMap>() {
 873            @Override
 874            public void onSuccess(final RtpContentMap outgoingContentMap) {
 875                sendSessionInitiate(outgoingContentMap, webRTCSessionDescription, targetState);
 876            }
 877
 878            @Override
 879            public void onFailure(@NonNull final Throwable throwable) {
 880                failureToInitiateSession(throwable, targetState);
 881            }
 882        }, MoreExecutors.directExecutor());
 883    }
 884
 885    private void sendSessionInitiate(final RtpContentMap rtpContentMap, final org.webrtc.SessionDescription webRTCSessionDescription, final State targetState) {
 886        if (isTerminated()) {
 887            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": preparing session was too slow. already terminated. nothing to do.");
 888            return;
 889        }
 890        this.transitionOrThrow(targetState);
 891        final JinglePacket sessionInitiate = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_INITIATE, id.sessionId);
 892        send(sessionInitiate);
 893        try {
 894            this.webRTCWrapper.setLocalDescription(webRTCSessionDescription).get();
 895        } catch (Exception e) {
 896            failureToInitiateSession(e, targetState);
 897        }
 898    }
 899
 900    private ListenableFuture<RtpContentMap> encryptSessionInitiate(final RtpContentMap rtpContentMap) {
 901        if (this.omemoVerification.hasDeviceId()) {
 902            final ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>> verifiedPayloadFuture = id.account.getAxolotlService()
 903                    .encrypt(rtpContentMap, id.with, omemoVerification.getDeviceId());
 904            final ListenableFuture<RtpContentMap> future = Futures.transform(verifiedPayloadFuture, verifiedPayload -> {
 905                omemoVerification.setSessionFingerprint(verifiedPayload.getFingerprint());
 906                return verifiedPayload.getPayload();
 907            }, MoreExecutors.directExecutor());
 908            if (Config.REQUIRE_RTP_VERIFICATION) {
 909                return future;
 910            }
 911            return Futures.catching(
 912                    future,
 913                    CryptoFailedException.class,
 914                    e -> {
 915                        Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to use OMEMO DTLS verification on outgoing session initiate. falling back", e);
 916                        return rtpContentMap;
 917                    },
 918                    MoreExecutors.directExecutor()
 919            );
 920        } else {
 921            return Futures.immediateFuture(rtpContentMap);
 922        }
 923    }
 924
 925    private void sendSessionTerminate(final Reason reason) {
 926        sendSessionTerminate(reason, null);
 927    }
 928
 929    private void sendSessionTerminate(final Reason reason, final String text) {
 930        final State previous = this.state;
 931        final State target = reasonToState(reason);
 932        transitionOrThrow(target);
 933        if (previous != State.NULL) {
 934            writeLogMessage(target);
 935        }
 936        final JinglePacket jinglePacket = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
 937        jinglePacket.setReason(reason, text);
 938        Log.d(Config.LOGTAG, jinglePacket.toString());
 939        send(jinglePacket);
 940        finish();
 941    }
 942
 943    private void sendTransportInfo(final String contentName, IceUdpTransportInfo.Candidate candidate) {
 944        final RtpContentMap transportInfo;
 945        try {
 946            final RtpContentMap rtpContentMap = isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
 947            transportInfo = rtpContentMap.transportInfo(contentName, candidate);
 948        } catch (final Exception e) {
 949            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to prepare transport-info from candidate for content=" + contentName);
 950            return;
 951        }
 952        final JinglePacket jinglePacket = transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
 953        send(jinglePacket);
 954    }
 955
 956    private void send(final JinglePacket jinglePacket) {
 957        jinglePacket.setTo(id.with);
 958        xmppConnectionService.sendIqPacket(id.account, jinglePacket, this::handleIqResponse);
 959    }
 960
 961    private synchronized void handleIqResponse(final Account account, final IqPacket response) {
 962        if (response.getType() == IqPacket.TYPE.ERROR) {
 963            final String errorCondition = response.getErrorCondition();
 964            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ-error from " + response.getFrom() + " in RTP session. " + errorCondition);
 965            if (isTerminated()) {
 966                Log.i(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring error because session was already terminated");
 967                return;
 968            }
 969            this.webRTCWrapper.close();
 970            final State target;
 971            if (Arrays.asList(
 972                    "service-unavailable",
 973                    "recipient-unavailable",
 974                    "remote-server-not-found",
 975                    "remote-server-timeout"
 976            ).contains(errorCondition)) {
 977                target = State.TERMINATED_CONNECTIVITY_ERROR;
 978            } else {
 979                target = State.TERMINATED_APPLICATION_FAILURE;
 980            }
 981            transitionOrThrow(target);
 982            this.finish();
 983        } else if (response.getType() == IqPacket.TYPE.TIMEOUT) {
 984            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ timeout in RTP session with " + id.with + ". terminating with connectivity error");
 985            if (isTerminated()) {
 986                Log.i(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring error because session was already terminated");
 987                return;
 988            }
 989            this.webRTCWrapper.close();
 990            transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
 991            this.finish();
 992        }
 993    }
 994
 995    private void terminateWithOutOfOrder(final JinglePacket jinglePacket) {
 996        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": terminating session with out-of-order");
 997        this.webRTCWrapper.close();
 998        transitionOrThrow(State.TERMINATED_APPLICATION_FAILURE);
 999        respondWithOutOfOrder(jinglePacket);
1000        this.finish();
1001    }
1002
1003    private void respondWithOutOfOrder(final JinglePacket jinglePacket) {
1004        jingleConnectionManager.respondWithJingleError(id.account, jinglePacket, "out-of-order", "unexpected-request", "wait");
1005    }
1006
1007    private void respondOk(final JinglePacket jinglePacket) {
1008        xmppConnectionService.sendIqPacket(id.account, jinglePacket.generateResponse(IqPacket.TYPE.RESULT), null);
1009    }
1010
1011    public void throwStateTransitionException() {
1012        final StateTransitionException exception = this.stateTransitionException;
1013        if (exception != null) {
1014            throw new IllegalStateException(String.format("Transition to %s did not call finish", exception.state), exception);
1015        }
1016    }
1017
1018    public RtpEndUserState getEndUserState() {
1019        switch (this.state) {
1020            case NULL:
1021            case PROPOSED:
1022            case SESSION_INITIALIZED:
1023                if (isInitiator()) {
1024                    return RtpEndUserState.RINGING;
1025                } else {
1026                    return RtpEndUserState.INCOMING_CALL;
1027                }
1028            case PROCEED:
1029                if (isInitiator()) {
1030                    return RtpEndUserState.RINGING;
1031                } else {
1032                    return RtpEndUserState.ACCEPTING_CALL;
1033                }
1034            case SESSION_INITIALIZED_PRE_APPROVED:
1035                if (isInitiator()) {
1036                    return RtpEndUserState.RINGING;
1037                } else {
1038                    return RtpEndUserState.CONNECTING;
1039                }
1040            case SESSION_ACCEPTED:
1041                //TODO refactor this out into separate method (that uses switch for better readability)
1042                final PeerConnection.PeerConnectionState state;
1043                try {
1044                    state = webRTCWrapper.getState();
1045                } catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
1046                    //We usually close the WebRTCWrapper *before* transitioning so we might still
1047                    //be in SESSION_ACCEPTED even though the peerConnection has been torn down
1048                    return RtpEndUserState.ENDING_CALL;
1049                }
1050                if (state == PeerConnection.PeerConnectionState.CONNECTED) {
1051                    return RtpEndUserState.CONNECTED;
1052                } else if (state == PeerConnection.PeerConnectionState.NEW || state == PeerConnection.PeerConnectionState.CONNECTING) {
1053                    return RtpEndUserState.CONNECTING;
1054                } else if (state == PeerConnection.PeerConnectionState.CLOSED) {
1055                    return RtpEndUserState.ENDING_CALL;
1056                } else {
1057                    return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1058                }
1059            case REJECTED:
1060            case REJECTED_RACED:
1061            case TERMINATED_DECLINED_OR_BUSY:
1062                if (isInitiator()) {
1063                    return RtpEndUserState.DECLINED_OR_BUSY;
1064                } else {
1065                    return RtpEndUserState.ENDED;
1066                }
1067            case TERMINATED_SUCCESS:
1068            case ACCEPTED:
1069            case RETRACTED:
1070            case TERMINATED_CANCEL_OR_TIMEOUT:
1071                return RtpEndUserState.ENDED;
1072            case RETRACTED_RACED:
1073                if (isInitiator()) {
1074                    return RtpEndUserState.ENDED;
1075                } else {
1076                    return RtpEndUserState.RETRACTED;
1077                }
1078            case TERMINATED_CONNECTIVITY_ERROR:
1079                return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1080            case TERMINATED_APPLICATION_FAILURE:
1081                return RtpEndUserState.APPLICATION_ERROR;
1082            case TERMINATED_SECURITY_ERROR:
1083                return RtpEndUserState.SECURITY_ERROR;
1084        }
1085        throw new IllegalStateException(String.format("%s has no equivalent EndUserState", this.state));
1086    }
1087
1088    public Set<Media> getMedia() {
1089        final State current = getState();
1090        if (current == State.NULL) {
1091            if (isInitiator()) {
1092                return Preconditions.checkNotNull(
1093                        this.proposedMedia,
1094                        "RTP connection has not been initialized properly"
1095                );
1096            }
1097            throw new IllegalStateException("RTP connection has not been initialized yet");
1098        }
1099        if (Arrays.asList(State.PROPOSED, State.PROCEED).contains(current)) {
1100            return Preconditions.checkNotNull(
1101                    this.proposedMedia,
1102                    "RTP connection has not been initialized properly"
1103            );
1104        }
1105        final RtpContentMap initiatorContentMap = initiatorRtpContentMap;
1106        if (initiatorContentMap != null) {
1107            return initiatorContentMap.getMedia();
1108        } else if (isTerminated()) {
1109            return Collections.emptySet(); //we might fail before we ever got a chance to set media
1110        } else {
1111            return Preconditions.checkNotNull(this.proposedMedia, "RTP connection has not been initialized properly");
1112        }
1113    }
1114
1115
1116    public boolean isVerified() {
1117        final String fingerprint = this.omemoVerification.getFingerprint();
1118        if (fingerprint == null) {
1119            return false;
1120        }
1121        final FingerprintStatus status = id.account.getAxolotlService().getFingerprintTrust(fingerprint);
1122        return status != null && status.isVerified();
1123    }
1124
1125    public synchronized void acceptCall() {
1126        switch (this.state) {
1127            case PROPOSED:
1128                cancelRingingTimeout();
1129                acceptCallFromProposed();
1130                break;
1131            case SESSION_INITIALIZED:
1132                cancelRingingTimeout();
1133                acceptCallFromSessionInitialized();
1134                break;
1135            case ACCEPTED:
1136                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted  with another client. UI was just lagging behind");
1137                break;
1138            case PROCEED:
1139            case SESSION_ACCEPTED:
1140                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted. user probably double tapped the UI");
1141                break;
1142            default:
1143                throw new IllegalStateException("Can not accept call from " + this.state);
1144        }
1145    }
1146
1147
1148    public void notifyPhoneCall() {
1149        Log.d(Config.LOGTAG, "a phone call has just been started. killing jingle rtp connections");
1150        if (Arrays.asList(State.PROPOSED, State.SESSION_INITIALIZED).contains(this.state)) {
1151            rejectCall();
1152        } else {
1153            endCall();
1154        }
1155    }
1156
1157    public synchronized void rejectCall() {
1158        if (isTerminated()) {
1159            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received rejectCall() when session has already been terminated. nothing to do");
1160            return;
1161        }
1162        switch (this.state) {
1163            case PROPOSED:
1164                rejectCallFromProposed();
1165                break;
1166            case SESSION_INITIALIZED:
1167                rejectCallFromSessionInitiate();
1168                break;
1169            default:
1170                throw new IllegalStateException("Can not reject call from " + this.state);
1171        }
1172    }
1173
1174    public synchronized void endCall() {
1175        if (isTerminated()) {
1176            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received endCall() when session has already been terminated. nothing to do");
1177            return;
1178        }
1179        if (isInState(State.PROPOSED) && !isInitiator()) {
1180            rejectCallFromProposed();
1181            return;
1182        }
1183        if (isInState(State.PROCEED)) {
1184            if (isInitiator()) {
1185                retractFromProceed();
1186            } else {
1187                rejectCallFromProceed();
1188            }
1189            return;
1190        }
1191        if (isInitiator() && isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED)) {
1192            this.webRTCWrapper.close();
1193            sendSessionTerminate(Reason.CANCEL);
1194            return;
1195        }
1196        if (isInState(State.SESSION_INITIALIZED)) {
1197            rejectCallFromSessionInitiate();
1198            return;
1199        }
1200        if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
1201            this.webRTCWrapper.close();
1202            sendSessionTerminate(Reason.SUCCESS);
1203            return;
1204        }
1205        if (isInState(State.TERMINATED_APPLICATION_FAILURE, State.TERMINATED_CONNECTIVITY_ERROR, State.TERMINATED_DECLINED_OR_BUSY)) {
1206            Log.d(Config.LOGTAG, "ignoring request to end call because already in state " + this.state);
1207            return;
1208        }
1209        throw new IllegalStateException("called 'endCall' while in state " + this.state + ". isInitiator=" + isInitiator());
1210    }
1211
1212    private void retractFromProceed() {
1213        Log.d(Config.LOGTAG, "retract from proceed");
1214        this.sendJingleMessage("retract");
1215        closeTransitionLogFinish(State.RETRACTED_RACED);
1216    }
1217
1218    private void closeTransitionLogFinish(final State state) {
1219        this.webRTCWrapper.close();
1220        transitionOrThrow(state);
1221        writeLogMessage(state);
1222        finish();
1223    }
1224
1225    private void setupWebRTC(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws WebRTCWrapper.InitializationException {
1226        this.jingleConnectionManager.ensureConnectionIsRegistered(this);
1227        final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference;
1228        if (media.contains(Media.VIDEO)) {
1229            speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.SPEAKER;
1230        } else {
1231            speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.EARPIECE;
1232        }
1233        this.webRTCWrapper.setup(this.xmppConnectionService, speakerPhonePreference);
1234        this.webRTCWrapper.initializePeerConnection(media, iceServers);
1235    }
1236
1237    private void acceptCallFromProposed() {
1238        transitionOrThrow(State.PROCEED);
1239        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1240        this.sendJingleMessage("accept", id.account.getJid().asBareJid());
1241        this.sendJingleMessage("proceed");
1242    }
1243
1244    private void rejectCallFromProposed() {
1245        transitionOrThrow(State.REJECTED);
1246        writeLogMessageMissed();
1247        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1248        this.sendJingleMessage("reject");
1249        finish();
1250    }
1251
1252    private void rejectCallFromProceed() {
1253        this.sendJingleMessage("reject");
1254        closeTransitionLogFinish(State.REJECTED_RACED);
1255    }
1256
1257    private void rejectCallFromSessionInitiate() {
1258        webRTCWrapper.close();
1259        sendSessionTerminate(Reason.DECLINE);
1260        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1261    }
1262
1263    private void sendJingleMessage(final String action) {
1264        sendJingleMessage(action, id.with);
1265    }
1266
1267    private void sendJingleMessage(final String action, final Jid to) {
1268        final MessagePacket messagePacket = new MessagePacket();
1269        messagePacket.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
1270        messagePacket.setTo(to);
1271        final Element intent = messagePacket.addChild(action, Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
1272        if ("proceed".equals(action)) {
1273            messagePacket.setId(JINGLE_MESSAGE_PROCEED_ID_PREFIX + id.sessionId);
1274            if (isOmemoEnabled()) {
1275                final int deviceId = id.account.getAxolotlService().getOwnDeviceId();
1276                final Element device = intent.addChild("device", Namespace.OMEMO_DTLS_SRTP_VERIFICATION);
1277                device.setAttribute("id", deviceId);
1278            }
1279        }
1280        messagePacket.addChild("store", "urn:xmpp:hints");
1281        xmppConnectionService.sendMessagePacket(id.account, messagePacket);
1282    }
1283
1284    private boolean isOmemoEnabled() {
1285        final Conversational conversational = message.getConversation();
1286        if (conversational instanceof Conversation) {
1287            return ((Conversation) conversational).getNextEncryption() == Message.ENCRYPTION_AXOLOTL;
1288        }
1289        return false;
1290    }
1291
1292    private void acceptCallFromSessionInitialized() {
1293        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1294        sendSessionAccept();
1295    }
1296
1297    private synchronized boolean isInState(State... state) {
1298        return Arrays.asList(state).contains(this.state);
1299    }
1300
1301    private boolean transition(final State target) {
1302        return transition(target, null);
1303    }
1304
1305    private synchronized boolean transition(final State target, final Runnable runnable) {
1306        final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
1307        if (validTransitions != null && validTransitions.contains(target)) {
1308            this.state = target;
1309            this.stateTransitionException = new StateTransitionException(target);
1310            if (runnable != null) {
1311                runnable.run();
1312            }
1313            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
1314            updateEndUserState();
1315            updateOngoingCallNotification();
1316            return true;
1317        } else {
1318            return false;
1319        }
1320    }
1321
1322    void transitionOrThrow(final State target) {
1323        if (!transition(target)) {
1324            throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
1325        }
1326    }
1327
1328    @Override
1329    public void onIceCandidate(final IceCandidate iceCandidate) {
1330        final IceUdpTransportInfo.Candidate candidate = IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp);
1331        Log.d(Config.LOGTAG, "sending candidate: " + iceCandidate.toString());
1332        sendTransportInfo(iceCandidate.sdpMid, candidate);
1333    }
1334
1335    @Override
1336    public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
1337        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnectionState changed to " + newState);
1338        if (newState == PeerConnection.PeerConnectionState.CONNECTED && this.rtpConnectionStarted == 0) {
1339            this.rtpConnectionStarted = SystemClock.elapsedRealtime();
1340        }
1341        if (newState == PeerConnection.PeerConnectionState.CLOSED && this.rtpConnectionEnded == 0) {
1342            this.rtpConnectionEnded = SystemClock.elapsedRealtime();
1343        }
1344        //TODO 'failed' means we need to restart ICE
1345        //
1346        //TODO 'disconnected' can probably be ignored as "This is a less stringent test than failed
1347        // and may trigger intermittently and resolve just as spontaneously on less reliable networks,
1348        // or during temporary disconnections. When the problem resolves, the connection may return
1349        // to the connected state."
1350        // Obviously the UI needs to reflect this new state with a 'reconnecting' display or something
1351        if (Arrays.asList(PeerConnection.PeerConnectionState.FAILED, PeerConnection.PeerConnectionState.DISCONNECTED).contains(newState)) {
1352            if (isTerminated()) {
1353                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": not sending session-terminate after connectivity error because session is already in state " + this.state);
1354                return;
1355            }
1356            new Thread(this::closeWebRTCSessionAfterFailedConnection).start();
1357        } else {
1358            updateEndUserState();
1359        }
1360    }
1361
1362    private void closeWebRTCSessionAfterFailedConnection() {
1363        this.webRTCWrapper.close();
1364        synchronized (this) {
1365            if (isTerminated()) {
1366                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": no need to send session-terminate after failed connection. Other party already did");
1367                return;
1368            }
1369            sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
1370        }
1371    }
1372
1373    public long getRtpConnectionStarted() {
1374        return this.rtpConnectionStarted;
1375    }
1376
1377    public long getRtpConnectionEnded() {
1378        return this.rtpConnectionEnded;
1379    }
1380
1381    public AppRTCAudioManager getAudioManager() {
1382        return webRTCWrapper.getAudioManager();
1383    }
1384
1385    public boolean isMicrophoneEnabled() {
1386        return webRTCWrapper.isMicrophoneEnabled();
1387    }
1388
1389    public boolean setMicrophoneEnabled(final boolean enabled) {
1390        return webRTCWrapper.setMicrophoneEnabled(enabled);
1391    }
1392
1393    public boolean isVideoEnabled() {
1394        return webRTCWrapper.isVideoEnabled();
1395    }
1396
1397    public void setVideoEnabled(final boolean enabled) {
1398        webRTCWrapper.setVideoEnabled(enabled);
1399    }
1400
1401    public boolean isCameraSwitchable() {
1402        return webRTCWrapper.isCameraSwitchable();
1403    }
1404
1405    public boolean isFrontCamera() {
1406        return webRTCWrapper.isFrontCamera();
1407    }
1408
1409    public ListenableFuture<Boolean> switchCamera() {
1410        return webRTCWrapper.switchCamera();
1411    }
1412
1413    @Override
1414    public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
1415        xmppConnectionService.notifyJingleRtpConnectionUpdate(selectedAudioDevice, availableAudioDevices);
1416    }
1417
1418    private void updateEndUserState() {
1419        final RtpEndUserState endUserState = getEndUserState();
1420        jingleConnectionManager.toneManager.transition(isInitiator(), endUserState, getMedia());
1421        xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1422    }
1423
1424    private void updateOngoingCallNotification() {
1425        if (STATES_SHOWING_ONGOING_CALL.contains(this.state)) {
1426            xmppConnectionService.setOngoingCall(id, getMedia());
1427        } else {
1428            xmppConnectionService.removeOngoingCall();
1429        }
1430    }
1431
1432    private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
1433        if (id.account.getXmppConnection().getFeatures().externalServiceDiscovery()) {
1434            final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1435            request.setTo(id.account.getDomain());
1436            request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1437            xmppConnectionService.sendIqPacket(id.account, request, (account, response) -> {
1438                ImmutableList.Builder<PeerConnection.IceServer> listBuilder = new ImmutableList.Builder<>();
1439                if (response.getType() == IqPacket.TYPE.RESULT) {
1440                    final Element services = response.findChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1441                    final List<Element> children = services == null ? Collections.emptyList() : services.getChildren();
1442                    for (final Element child : children) {
1443                        if ("service".equals(child.getName())) {
1444                            final String type = child.getAttribute("type");
1445                            final String host = child.getAttribute("host");
1446                            final String sport = child.getAttribute("port");
1447                            final Integer port = sport == null ? null : Ints.tryParse(sport);
1448                            final String transport = child.getAttribute("transport");
1449                            final String username = child.getAttribute("username");
1450                            final String password = child.getAttribute("password");
1451                            if (Strings.isNullOrEmpty(host) || port == null) {
1452                                continue;
1453                            }
1454                            if (port < 0 || port > 65535) {
1455                                continue;
1456                            }
1457                            if (Arrays.asList("stun", "stuns", "turn", "turns").contains(type) && Arrays.asList("udp", "tcp").contains(transport)) {
1458                                if (Arrays.asList("stuns", "turns").contains(type) && "udp".equals(transport)) {
1459                                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping invalid combination of udp/tls in external services");
1460                                    continue;
1461                                }
1462                                final PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer
1463                                        .builder(String.format("%s:%s:%s?transport=%s", type, IP.wrapIPv6(host), port, transport));
1464                                iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK);
1465                                if (username != null && password != null) {
1466                                    iceServerBuilder.setUsername(username);
1467                                    iceServerBuilder.setPassword(password);
1468                                } else if (Arrays.asList("turn", "turns").contains(type)) {
1469                                    //The WebRTC spec requires throwing an InvalidAccessError when username (from libwebrtc source coder)
1470                                    //https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
1471                                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping " + type + "/" + transport + " without username and password");
1472                                    continue;
1473                                }
1474                                final PeerConnection.IceServer iceServer = iceServerBuilder.createIceServer();
1475                                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": discovered ICE Server: " + iceServer);
1476                                listBuilder.add(iceServer);
1477                            }
1478                        }
1479                    }
1480                }
1481                final List<PeerConnection.IceServer> iceServers = listBuilder.build();
1482                if (iceServers.size() == 0) {
1483                    Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no ICE server found " + response);
1484                }
1485                onIceServersDiscovered.onIceServersDiscovered(iceServers);
1486            });
1487        } else {
1488            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": has no external service discovery");
1489            onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
1490        }
1491    }
1492
1493    private void finish() {
1494        if (isTerminated()) {
1495            this.cancelRingingTimeout();
1496            this.webRTCWrapper.verifyClosed();
1497            this.jingleConnectionManager.setTerminalSessionState(id, getEndUserState(), getMedia());
1498            this.jingleConnectionManager.finishConnectionOrThrow(this);
1499        } else {
1500            throw new IllegalStateException(String.format("Unable to call finish from %s", this.state));
1501        }
1502    }
1503
1504    private void writeLogMessage(final State state) {
1505        final long started = this.rtpConnectionStarted;
1506        long duration = started <= 0 ? 0 : SystemClock.elapsedRealtime() - started;
1507        if (state == State.TERMINATED_SUCCESS || (state == State.TERMINATED_CONNECTIVITY_ERROR && duration > 0)) {
1508            writeLogMessageSuccess(duration);
1509        } else {
1510            writeLogMessageMissed();
1511        }
1512    }
1513
1514    private void writeLogMessageSuccess(final long duration) {
1515        this.message.setBody(new RtpSessionStatus(true, duration).toString());
1516        this.writeMessage();
1517    }
1518
1519    private void writeLogMessageMissed() {
1520        this.message.setBody(new RtpSessionStatus(false, 0).toString());
1521        this.writeMessage();
1522    }
1523
1524    private void writeMessage() {
1525        final Conversational conversational = message.getConversation();
1526        if (conversational instanceof Conversation) {
1527            ((Conversation) conversational).add(this.message);
1528            xmppConnectionService.createMessageAsync(message);
1529            xmppConnectionService.updateConversationUi();
1530        } else {
1531            throw new IllegalStateException("Somehow the conversation in a message was a stub");
1532        }
1533    }
1534
1535    public State getState() {
1536        return this.state;
1537    }
1538
1539    boolean isTerminated() {
1540        return TERMINATED.contains(this.state);
1541    }
1542
1543    public Optional<VideoTrack> getLocalVideoTrack() {
1544        return webRTCWrapper.getLocalVideoTrack();
1545    }
1546
1547    public Optional<VideoTrack> getRemoteVideoTrack() {
1548        return webRTCWrapper.getRemoteVideoTrack();
1549    }
1550
1551
1552    public EglBase.Context getEglBaseContext() {
1553        return webRTCWrapper.getEglBaseContext();
1554    }
1555
1556    void setProposedMedia(final Set<Media> media) {
1557        this.proposedMedia = media;
1558    }
1559
1560    public void fireStateUpdate() {
1561        final RtpEndUserState endUserState = getEndUserState();
1562        xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1563    }
1564
1565    private interface OnIceServersDiscovered {
1566        void onIceServersDiscovered(List<PeerConnection.IceServer> iceServers);
1567    }
1568
1569    private static class StateTransitionException extends Exception {
1570        private final State state;
1571
1572        private StateTransitionException(final State state) {
1573            this.state = state;
1574        }
1575    }
1576}