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                final PeerConnection.PeerConnectionState state;
1042                try {
1043                    state = webRTCWrapper.getState();
1044                } catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
1045                    //We usually close the WebRTCWrapper *before* transitioning so we might still
1046                    //be in SESSION_ACCEPTED even though the peerConnection has been torn down
1047                    return RtpEndUserState.ENDING_CALL;
1048                }
1049                if (state == PeerConnection.PeerConnectionState.CONNECTED) {
1050                    return RtpEndUserState.CONNECTED;
1051                } else if (state == PeerConnection.PeerConnectionState.NEW || state == PeerConnection.PeerConnectionState.CONNECTING) {
1052                    return RtpEndUserState.CONNECTING;
1053                } else if (state == PeerConnection.PeerConnectionState.CLOSED) {
1054                    return RtpEndUserState.ENDING_CALL;
1055                } else {
1056                    return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1057                }
1058            case REJECTED:
1059            case REJECTED_RACED:
1060            case TERMINATED_DECLINED_OR_BUSY:
1061                if (isInitiator()) {
1062                    return RtpEndUserState.DECLINED_OR_BUSY;
1063                } else {
1064                    return RtpEndUserState.ENDED;
1065                }
1066            case TERMINATED_SUCCESS:
1067            case ACCEPTED:
1068            case RETRACTED:
1069            case TERMINATED_CANCEL_OR_TIMEOUT:
1070                return RtpEndUserState.ENDED;
1071            case RETRACTED_RACED:
1072                if (isInitiator()) {
1073                    return RtpEndUserState.ENDED;
1074                } else {
1075                    return RtpEndUserState.RETRACTED;
1076                }
1077            case TERMINATED_CONNECTIVITY_ERROR:
1078                return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1079            case TERMINATED_APPLICATION_FAILURE:
1080                return RtpEndUserState.APPLICATION_ERROR;
1081            case TERMINATED_SECURITY_ERROR:
1082                return RtpEndUserState.SECURITY_ERROR;
1083        }
1084        throw new IllegalStateException(String.format("%s has no equivalent EndUserState", this.state));
1085    }
1086
1087    public Set<Media> getMedia() {
1088        final State current = getState();
1089        if (current == State.NULL) {
1090            if (isInitiator()) {
1091                return Preconditions.checkNotNull(
1092                        this.proposedMedia,
1093                        "RTP connection has not been initialized properly"
1094                );
1095            }
1096            throw new IllegalStateException("RTP connection has not been initialized yet");
1097        }
1098        if (Arrays.asList(State.PROPOSED, State.PROCEED).contains(current)) {
1099            return Preconditions.checkNotNull(
1100                    this.proposedMedia,
1101                    "RTP connection has not been initialized properly"
1102            );
1103        }
1104        final RtpContentMap initiatorContentMap = initiatorRtpContentMap;
1105        if (initiatorContentMap != null) {
1106            return initiatorContentMap.getMedia();
1107        } else if (isTerminated()) {
1108            return Collections.emptySet(); //we might fail before we ever got a chance to set media
1109        } else {
1110            return Preconditions.checkNotNull(this.proposedMedia, "RTP connection has not been initialized properly");
1111        }
1112    }
1113
1114
1115    public boolean isVerified() {
1116        final String fingerprint = this.omemoVerification.getFingerprint();
1117        if (fingerprint == null) {
1118            return false;
1119        }
1120        final FingerprintStatus status = id.account.getAxolotlService().getFingerprintTrust(fingerprint);
1121        return status != null && status.isVerified();
1122    }
1123
1124    public synchronized void acceptCall() {
1125        switch (this.state) {
1126            case PROPOSED:
1127                cancelRingingTimeout();
1128                acceptCallFromProposed();
1129                break;
1130            case SESSION_INITIALIZED:
1131                cancelRingingTimeout();
1132                acceptCallFromSessionInitialized();
1133                break;
1134            case ACCEPTED:
1135                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted  with another client. UI was just lagging behind");
1136                break;
1137            case PROCEED:
1138            case SESSION_ACCEPTED:
1139                Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted. user probably double tapped the UI");
1140                break;
1141            default:
1142                throw new IllegalStateException("Can not accept call from " + this.state);
1143        }
1144    }
1145
1146
1147    public void notifyPhoneCall() {
1148        Log.d(Config.LOGTAG, "a phone call has just been started. killing jingle rtp connections");
1149        if (Arrays.asList(State.PROPOSED, State.SESSION_INITIALIZED).contains(this.state)) {
1150            rejectCall();
1151        } else {
1152            endCall();
1153        }
1154    }
1155
1156    public synchronized void rejectCall() {
1157        if (isTerminated()) {
1158            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received rejectCall() when session has already been terminated. nothing to do");
1159            return;
1160        }
1161        switch (this.state) {
1162            case PROPOSED:
1163                rejectCallFromProposed();
1164                break;
1165            case SESSION_INITIALIZED:
1166                rejectCallFromSessionInitiate();
1167                break;
1168            default:
1169                throw new IllegalStateException("Can not reject call from " + this.state);
1170        }
1171    }
1172
1173    public synchronized void endCall() {
1174        if (isTerminated()) {
1175            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received endCall() when session has already been terminated. nothing to do");
1176            return;
1177        }
1178        if (isInState(State.PROPOSED) && !isInitiator()) {
1179            rejectCallFromProposed();
1180            return;
1181        }
1182        if (isInState(State.PROCEED)) {
1183            if (isInitiator()) {
1184                retractFromProceed();
1185            } else {
1186                rejectCallFromProceed();
1187            }
1188            return;
1189        }
1190        if (isInitiator() && isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED)) {
1191            this.webRTCWrapper.close();
1192            sendSessionTerminate(Reason.CANCEL);
1193            return;
1194        }
1195        if (isInState(State.SESSION_INITIALIZED)) {
1196            rejectCallFromSessionInitiate();
1197            return;
1198        }
1199        if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
1200            this.webRTCWrapper.close();
1201            sendSessionTerminate(Reason.SUCCESS);
1202            return;
1203        }
1204        if (isInState(State.TERMINATED_APPLICATION_FAILURE, State.TERMINATED_CONNECTIVITY_ERROR, State.TERMINATED_DECLINED_OR_BUSY)) {
1205            Log.d(Config.LOGTAG, "ignoring request to end call because already in state " + this.state);
1206            return;
1207        }
1208        throw new IllegalStateException("called 'endCall' while in state " + this.state + ". isInitiator=" + isInitiator());
1209    }
1210
1211    private void retractFromProceed() {
1212        Log.d(Config.LOGTAG, "retract from proceed");
1213        this.sendJingleMessage("retract");
1214        closeTransitionLogFinish(State.RETRACTED_RACED);
1215    }
1216
1217    private void closeTransitionLogFinish(final State state) {
1218        this.webRTCWrapper.close();
1219        transitionOrThrow(state);
1220        writeLogMessage(state);
1221        finish();
1222    }
1223
1224    private void setupWebRTC(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws WebRTCWrapper.InitializationException {
1225        this.jingleConnectionManager.ensureConnectionIsRegistered(this);
1226        final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference;
1227        if (media.contains(Media.VIDEO)) {
1228            speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.SPEAKER;
1229        } else {
1230            speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.EARPIECE;
1231        }
1232        this.webRTCWrapper.setup(this.xmppConnectionService, speakerPhonePreference);
1233        this.webRTCWrapper.initializePeerConnection(media, iceServers);
1234    }
1235
1236    private void acceptCallFromProposed() {
1237        transitionOrThrow(State.PROCEED);
1238        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1239        this.sendJingleMessage("accept", id.account.getJid().asBareJid());
1240        this.sendJingleMessage("proceed");
1241    }
1242
1243    private void rejectCallFromProposed() {
1244        transitionOrThrow(State.REJECTED);
1245        writeLogMessageMissed();
1246        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1247        this.sendJingleMessage("reject");
1248        finish();
1249    }
1250
1251    private void rejectCallFromProceed() {
1252        this.sendJingleMessage("reject");
1253        closeTransitionLogFinish(State.REJECTED_RACED);
1254    }
1255
1256    private void rejectCallFromSessionInitiate() {
1257        webRTCWrapper.close();
1258        sendSessionTerminate(Reason.DECLINE);
1259        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1260    }
1261
1262    private void sendJingleMessage(final String action) {
1263        sendJingleMessage(action, id.with);
1264    }
1265
1266    private void sendJingleMessage(final String action, final Jid to) {
1267        final MessagePacket messagePacket = new MessagePacket();
1268        messagePacket.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
1269        messagePacket.setTo(to);
1270        final Element intent = messagePacket.addChild(action, Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
1271        if ("proceed".equals(action)) {
1272            messagePacket.setId(JINGLE_MESSAGE_PROCEED_ID_PREFIX + id.sessionId);
1273            if (isOmemoEnabled()) {
1274                final int deviceId = id.account.getAxolotlService().getOwnDeviceId();
1275                final Element device = intent.addChild("device", Namespace.OMEMO_DTLS_SRTP_VERIFICATION);
1276                device.setAttribute("id", deviceId);
1277            }
1278        }
1279        messagePacket.addChild("store", "urn:xmpp:hints");
1280        xmppConnectionService.sendMessagePacket(id.account, messagePacket);
1281    }
1282
1283    private boolean isOmemoEnabled() {
1284        final Conversational conversational = message.getConversation();
1285        if (conversational instanceof Conversation) {
1286            return ((Conversation) conversational).getNextEncryption() == Message.ENCRYPTION_AXOLOTL;
1287        }
1288        return false;
1289    }
1290
1291    private void acceptCallFromSessionInitialized() {
1292        xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1293        sendSessionAccept();
1294    }
1295
1296    private synchronized boolean isInState(State... state) {
1297        return Arrays.asList(state).contains(this.state);
1298    }
1299
1300    private boolean transition(final State target) {
1301        return transition(target, null);
1302    }
1303
1304    private synchronized boolean transition(final State target, final Runnable runnable) {
1305        final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
1306        if (validTransitions != null && validTransitions.contains(target)) {
1307            this.state = target;
1308            this.stateTransitionException = new StateTransitionException(target);
1309            if (runnable != null) {
1310                runnable.run();
1311            }
1312            Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
1313            updateEndUserState();
1314            updateOngoingCallNotification();
1315            return true;
1316        } else {
1317            return false;
1318        }
1319    }
1320
1321    void transitionOrThrow(final State target) {
1322        if (!transition(target)) {
1323            throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
1324        }
1325    }
1326
1327    @Override
1328    public void onIceCandidate(final IceCandidate iceCandidate) {
1329        final IceUdpTransportInfo.Candidate candidate = IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp);
1330        Log.d(Config.LOGTAG, "sending candidate: " + iceCandidate.toString());
1331        sendTransportInfo(iceCandidate.sdpMid, candidate);
1332    }
1333
1334    @Override
1335    public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
1336        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnectionState changed to " + newState);
1337        if (newState == PeerConnection.PeerConnectionState.CONNECTED && this.rtpConnectionStarted == 0) {
1338            this.rtpConnectionStarted = SystemClock.elapsedRealtime();
1339        }
1340        if (newState == PeerConnection.PeerConnectionState.CLOSED && this.rtpConnectionEnded == 0) {
1341            this.rtpConnectionEnded = SystemClock.elapsedRealtime();
1342        }
1343        //TODO 'DISCONNECTED' might be an opportunity to renew the offer and send a transport-replace
1344        //TODO exact syntax is yet to be determined but transport-replace sounds like the most reasonable
1345        //as there is no content-replace
1346        if (Arrays.asList(PeerConnection.PeerConnectionState.FAILED, PeerConnection.PeerConnectionState.DISCONNECTED).contains(newState)) {
1347            if (isTerminated()) {
1348                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": not sending session-terminate after connectivity error because session is already in state " + this.state);
1349                return;
1350            }
1351            new Thread(this::closeWebRTCSessionAfterFailedConnection).start();
1352        } else {
1353            updateEndUserState();
1354        }
1355    }
1356
1357    private void closeWebRTCSessionAfterFailedConnection() {
1358        this.webRTCWrapper.close();
1359        synchronized (this) {
1360            if (isTerminated()) {
1361                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": no need to send session-terminate after failed connection. Other party already did");
1362                return;
1363            }
1364            sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
1365        }
1366    }
1367
1368    public long getRtpConnectionStarted() {
1369        return this.rtpConnectionStarted;
1370    }
1371
1372    public long getRtpConnectionEnded() {
1373        return this.rtpConnectionEnded;
1374    }
1375
1376    public AppRTCAudioManager getAudioManager() {
1377        return webRTCWrapper.getAudioManager();
1378    }
1379
1380    public boolean isMicrophoneEnabled() {
1381        return webRTCWrapper.isMicrophoneEnabled();
1382    }
1383
1384    public boolean setMicrophoneEnabled(final boolean enabled) {
1385        return webRTCWrapper.setMicrophoneEnabled(enabled);
1386    }
1387
1388    public boolean isVideoEnabled() {
1389        return webRTCWrapper.isVideoEnabled();
1390    }
1391
1392    public void setVideoEnabled(final boolean enabled) {
1393        webRTCWrapper.setVideoEnabled(enabled);
1394    }
1395
1396    public boolean isCameraSwitchable() {
1397        return webRTCWrapper.isCameraSwitchable();
1398    }
1399
1400    public boolean isFrontCamera() {
1401        return webRTCWrapper.isFrontCamera();
1402    }
1403
1404    public ListenableFuture<Boolean> switchCamera() {
1405        return webRTCWrapper.switchCamera();
1406    }
1407
1408    @Override
1409    public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
1410        xmppConnectionService.notifyJingleRtpConnectionUpdate(selectedAudioDevice, availableAudioDevices);
1411    }
1412
1413    private void updateEndUserState() {
1414        final RtpEndUserState endUserState = getEndUserState();
1415        jingleConnectionManager.toneManager.transition(isInitiator(), endUserState, getMedia());
1416        xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1417    }
1418
1419    private void updateOngoingCallNotification() {
1420        if (STATES_SHOWING_ONGOING_CALL.contains(this.state)) {
1421            xmppConnectionService.setOngoingCall(id, getMedia());
1422        } else {
1423            xmppConnectionService.removeOngoingCall();
1424        }
1425    }
1426
1427    private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
1428        if (id.account.getXmppConnection().getFeatures().externalServiceDiscovery()) {
1429            final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1430            request.setTo(id.account.getDomain());
1431            request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1432            xmppConnectionService.sendIqPacket(id.account, request, (account, response) -> {
1433                ImmutableList.Builder<PeerConnection.IceServer> listBuilder = new ImmutableList.Builder<>();
1434                if (response.getType() == IqPacket.TYPE.RESULT) {
1435                    final Element services = response.findChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1436                    final List<Element> children = services == null ? Collections.emptyList() : services.getChildren();
1437                    for (final Element child : children) {
1438                        if ("service".equals(child.getName())) {
1439                            final String type = child.getAttribute("type");
1440                            final String host = child.getAttribute("host");
1441                            final String sport = child.getAttribute("port");
1442                            final Integer port = sport == null ? null : Ints.tryParse(sport);
1443                            final String transport = child.getAttribute("transport");
1444                            final String username = child.getAttribute("username");
1445                            final String password = child.getAttribute("password");
1446                            if (Strings.isNullOrEmpty(host) || port == null) {
1447                                continue;
1448                            }
1449                            if (port < 0 || port > 65535) {
1450                                continue;
1451                            }
1452                            if (Arrays.asList("stun", "stuns", "turn", "turns").contains(type) && Arrays.asList("udp", "tcp").contains(transport)) {
1453                                if (Arrays.asList("stuns", "turns").contains(type) && "udp".equals(transport)) {
1454                                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping invalid combination of udp/tls in external services");
1455                                    continue;
1456                                }
1457                                final PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer
1458                                        .builder(String.format("%s:%s:%s?transport=%s", type, IP.wrapIPv6(host), port, transport));
1459                                iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK);
1460                                if (username != null && password != null) {
1461                                    iceServerBuilder.setUsername(username);
1462                                    iceServerBuilder.setPassword(password);
1463                                } else if (Arrays.asList("turn", "turns").contains(type)) {
1464                                    //The WebRTC spec requires throwing an InvalidAccessError when username (from libwebrtc source coder)
1465                                    //https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
1466                                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping " + type + "/" + transport + " without username and password");
1467                                    continue;
1468                                }
1469                                final PeerConnection.IceServer iceServer = iceServerBuilder.createIceServer();
1470                                Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": discovered ICE Server: " + iceServer);
1471                                listBuilder.add(iceServer);
1472                            }
1473                        }
1474                    }
1475                }
1476                final List<PeerConnection.IceServer> iceServers = listBuilder.build();
1477                if (iceServers.size() == 0) {
1478                    Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no ICE server found " + response);
1479                }
1480                onIceServersDiscovered.onIceServersDiscovered(iceServers);
1481            });
1482        } else {
1483            Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": has no external service discovery");
1484            onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
1485        }
1486    }
1487
1488    private void finish() {
1489        if (isTerminated()) {
1490            this.cancelRingingTimeout();
1491            this.webRTCWrapper.verifyClosed();
1492            this.jingleConnectionManager.setTerminalSessionState(id, getEndUserState(), getMedia());
1493            this.jingleConnectionManager.finishConnectionOrThrow(this);
1494        } else {
1495            throw new IllegalStateException(String.format("Unable to call finish from %s", this.state));
1496        }
1497    }
1498
1499    private void writeLogMessage(final State state) {
1500        final long started = this.rtpConnectionStarted;
1501        long duration = started <= 0 ? 0 : SystemClock.elapsedRealtime() - started;
1502        if (state == State.TERMINATED_SUCCESS || (state == State.TERMINATED_CONNECTIVITY_ERROR && duration > 0)) {
1503            writeLogMessageSuccess(duration);
1504        } else {
1505            writeLogMessageMissed();
1506        }
1507    }
1508
1509    private void writeLogMessageSuccess(final long duration) {
1510        this.message.setBody(new RtpSessionStatus(true, duration).toString());
1511        this.writeMessage();
1512    }
1513
1514    private void writeLogMessageMissed() {
1515        this.message.setBody(new RtpSessionStatus(false, 0).toString());
1516        this.writeMessage();
1517    }
1518
1519    private void writeMessage() {
1520        final Conversational conversational = message.getConversation();
1521        if (conversational instanceof Conversation) {
1522            ((Conversation) conversational).add(this.message);
1523            xmppConnectionService.createMessageAsync(message);
1524            xmppConnectionService.updateConversationUi();
1525        } else {
1526            throw new IllegalStateException("Somehow the conversation in a message was a stub");
1527        }
1528    }
1529
1530    public State getState() {
1531        return this.state;
1532    }
1533
1534    boolean isTerminated() {
1535        return TERMINATED.contains(this.state);
1536    }
1537
1538    public Optional<VideoTrack> getLocalVideoTrack() {
1539        return webRTCWrapper.getLocalVideoTrack();
1540    }
1541
1542    public Optional<VideoTrack> getRemoteVideoTrack() {
1543        return webRTCWrapper.getRemoteVideoTrack();
1544    }
1545
1546
1547    public EglBase.Context getEglBaseContext() {
1548        return webRTCWrapper.getEglBaseContext();
1549    }
1550
1551    void setProposedMedia(final Set<Media> media) {
1552        this.proposedMedia = media;
1553    }
1554
1555    public void fireStateUpdate() {
1556        final RtpEndUserState endUserState = getEndUserState();
1557        xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1558    }
1559
1560    private interface OnIceServersDiscovered {
1561        void onIceServersDiscovered(List<PeerConnection.IceServer> iceServers);
1562    }
1563
1564    private static class StateTransitionException extends Exception {
1565        private final State state;
1566
1567        private StateTransitionException(final State state) {
1568            this.state = state;
1569        }
1570    }
1571}