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