JingleConnectionManager.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.telecom.TelecomManager;
   4import android.telecom.VideoProfile;
   5import android.util.Base64;
   6import android.util.Log;
   7import androidx.annotation.Nullable;
   8import com.google.common.base.Objects;
   9import com.google.common.base.Optional;
  10import com.google.common.base.Preconditions;
  11import com.google.common.cache.Cache;
  12import com.google.common.cache.CacheBuilder;
  13import com.google.common.collect.Collections2;
  14import com.google.common.collect.ComparisonChain;
  15import com.google.common.collect.ImmutableSet;
  16import eu.siacs.conversations.Config;
  17import eu.siacs.conversations.entities.Account;
  18import eu.siacs.conversations.entities.Contact;
  19import eu.siacs.conversations.entities.Conversation;
  20import eu.siacs.conversations.entities.Conversational;
  21import eu.siacs.conversations.entities.Message;
  22import eu.siacs.conversations.entities.RtpSessionStatus;
  23import eu.siacs.conversations.entities.Transferable;
  24import eu.siacs.conversations.services.AbstractConnectionManager;
  25import eu.siacs.conversations.services.CallIntegration;
  26import eu.siacs.conversations.services.CallIntegrationConnectionService;
  27import eu.siacs.conversations.services.XmppConnectionService;
  28import eu.siacs.conversations.xml.Element;
  29import eu.siacs.conversations.xml.Namespace;
  30import eu.siacs.conversations.xmpp.Jid;
  31import eu.siacs.conversations.xmpp.XmppConnection;
  32import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  33import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
  34import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  35import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  36import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  37import eu.siacs.conversations.xmpp.jingle.transports.InbandBytestreamsTransport;
  38import eu.siacs.conversations.xmpp.jingle.transports.Transport;
  39import im.conversations.android.xmpp.model.jingle.Jingle;
  40import im.conversations.android.xmpp.model.stanza.Iq;
  41import java.lang.ref.WeakReference;
  42import java.security.SecureRandom;
  43import java.util.Arrays;
  44import java.util.Collection;
  45import java.util.HashMap;
  46import java.util.List;
  47import java.util.Map;
  48import java.util.Set;
  49import java.util.concurrent.ConcurrentHashMap;
  50import java.util.concurrent.Executors;
  51import java.util.concurrent.ScheduledExecutorService;
  52import java.util.concurrent.ScheduledFuture;
  53import java.util.concurrent.TimeUnit;
  54
  55public class JingleConnectionManager extends AbstractConnectionManager {
  56    public static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
  57            Executors.newSingleThreadScheduledExecutor();
  58    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals =
  59            new HashMap<>();
  60    private final ConcurrentHashMap<AbstractJingleConnection.Id, AbstractJingleConnection>
  61            connections = new ConcurrentHashMap<>();
  62
  63    private final Cache<PersistableSessionId, TerminatedRtpSession> terminatedSessions =
  64            CacheBuilder.newBuilder().expireAfterWrite(24, TimeUnit.HOURS).build();
  65
  66    public JingleConnectionManager(XmppConnectionService service) {
  67        super(service);
  68    }
  69
  70    static String nextRandomId() {
  71        final byte[] id = new byte[16];
  72        new SecureRandom().nextBytes(id);
  73        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
  74    }
  75
  76    public void deliverPacket(final Account account, final Iq packet) {
  77        final var jingle = packet.getExtension(Jingle.class);
  78        Preconditions.checkNotNull(
  79                jingle, "Passed iq packet w/o jingle extension to Connection Manager");
  80        final String sessionId = jingle.getSessionId();
  81        final Jingle.Action action = jingle.getAction();
  82        if (sessionId == null) {
  83            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
  84            return;
  85        }
  86        if (action == null) {
  87            respondWithJingleError(account, packet, null, "bad-request", "cancel");
  88            return;
  89        }
  90        final AbstractJingleConnection.Id id =
  91                AbstractJingleConnection.Id.of(account, packet, jingle);
  92        final AbstractJingleConnection existingJingleConnection = connections.get(id);
  93        if (existingJingleConnection != null) {
  94            existingJingleConnection.deliverPacket(packet);
  95        } else if (action == Jingle.Action.SESSION_INITIATE) {
  96            final Jid from = packet.getFrom();
  97            final Content content = jingle.getJingleContent();
  98            final String descriptionNamespace =
  99                    content == null ? null : content.getDescriptionNamespace();
 100            final AbstractJingleConnection connection;
 101            if (Namespace.JINGLE_APPS_FILE_TRANSFER.equals(descriptionNamespace)) {
 102                connection = new JingleFileTransferConnection(this, id, from);
 103            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)
 104                    && isUsingClearNet(account)) {
 105                final boolean sessionEnded =
 106                        this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 107                final boolean stranger =
 108                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 109                final boolean busy = isBusy();
 110                if (busy || sessionEnded || stranger) {
 111                    Log.d(
 112                            Config.LOGTAG,
 113                            id.account.getJid().asBareJid()
 114                                    + ": rejected session with "
 115                                    + id.with
 116                                    + " because busy. sessionEnded="
 117                                    + sessionEnded
 118                                    + ", stranger="
 119                                    + stranger);
 120                    sendSessionTerminate(account, packet, id);
 121                    if (busy || stranger) {
 122                        writeLogMissedIncoming(
 123                                account,
 124                                id.with,
 125                                id.sessionId,
 126                                null,
 127                                System.currentTimeMillis(),
 128                                stranger);
 129                    }
 130                    return;
 131                }
 132                connection = new JingleRtpConnection(this, id, from);
 133            } else {
 134                respondWithJingleError(
 135                        account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 136                return;
 137            }
 138            connections.put(id, connection);
 139            mXmppConnectionService.updateConversationUi();
 140            connection.deliverPacket(packet);
 141            if (connection instanceof JingleRtpConnection rtpConnection) {
 142                addNewIncomingCall(rtpConnection);
 143            }
 144        } else {
 145            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 146            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 147        }
 148    }
 149
 150    private void addNewIncomingCall(final JingleRtpConnection rtpConnection) {
 151        if (rtpConnection.isTerminated()) {
 152            Log.d(
 153                    Config.LOGTAG,
 154                    "skip call integration because something must have gone during initiate");
 155            return;
 156        }
 157        if (CallIntegrationConnectionService.addNewIncomingCall(
 158                mXmppConnectionService, rtpConnection.getId())) {
 159            return;
 160        }
 161        rtpConnection.integrationFailure();
 162    }
 163
 164    private void sendSessionTerminate(
 165            final Account account, final Iq request, final AbstractJingleConnection.Id id) {
 166        mXmppConnectionService.sendIqPacket(
 167                account, request.generateResponse(Iq.Type.RESULT), null);
 168        final var iq = new Iq(Iq.Type.SET);
 169        iq.setTo(id.with);
 170        final var sessionTermination =
 171                iq.addExtension(new Jingle(Jingle.Action.SESSION_TERMINATE, id.sessionId));
 172        sessionTermination.setReason(Reason.BUSY, null);
 173        mXmppConnectionService.sendIqPacket(account, iq, null);
 174    }
 175
 176    private boolean isUsingClearNet(final Account account) {
 177        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
 178    }
 179
 180    public boolean isBusy() {
 181        for (final AbstractJingleConnection connection : this.connections.values()) {
 182            if (connection instanceof JingleRtpConnection rtpConnection) {
 183                if (connection.isTerminated() && rtpConnection.getCallIntegration().isDestroyed()) {
 184                    continue;
 185                }
 186                return true;
 187            }
 188        }
 189        synchronized (this.rtpSessionProposals) {
 190            return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)
 191                    || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)
 192                    || this.rtpSessionProposals.containsValue(
 193                            DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED);
 194        }
 195    }
 196
 197    public boolean hasJingleRtpConnection(final Account account) {
 198        for (AbstractJingleConnection connection : this.connections.values()) {
 199            if (connection instanceof JingleRtpConnection rtpConnection) {
 200                if (rtpConnection.isTerminated()) {
 201                    continue;
 202                }
 203                if (rtpConnection.id.account == account) {
 204                    return true;
 205                }
 206            }
 207        }
 208        return false;
 209    }
 210
 211    private Optional<RtpSessionProposal> findMatchingSessionProposal(
 212            final Account account, final Jid with, final Set<Media> media) {
 213        synchronized (this.rtpSessionProposals) {
 214            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 215                    this.rtpSessionProposals.entrySet()) {
 216                final RtpSessionProposal proposal = entry.getKey();
 217                final DeviceDiscoveryState state = entry.getValue();
 218                final boolean openProposal =
 219                        state == DeviceDiscoveryState.DISCOVERED
 220                                || state == DeviceDiscoveryState.SEARCHING
 221                                || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
 222                if (openProposal
 223                        && proposal.account == account
 224                        && proposal.with.equals(with.asBareJid())
 225                        && proposal.media.equals(media)) {
 226                    return Optional.of(proposal);
 227                }
 228            }
 229        }
 230        return Optional.absent();
 231    }
 232
 233    private boolean hasMatchingRtpSession(
 234            final Account account, final Jid with, final Set<Media> media) {
 235        for (AbstractJingleConnection connection : this.connections.values()) {
 236            if (connection instanceof JingleRtpConnection rtpConnection) {
 237                if (rtpConnection.isTerminated()) {
 238                    continue;
 239                }
 240                if (rtpConnection.getId().account == account
 241                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
 242                        && rtpConnection.getMedia().equals(media)) {
 243                    return true;
 244                }
 245            }
 246        }
 247        return false;
 248    }
 249
 250    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
 251        final boolean notifyForStrangers =
 252                mXmppConnectionService.getNotificationService().notificationsFromStrangers();
 253        if (notifyForStrangers) {
 254            return false;
 255        }
 256        final Contact contact = account.getRoster().getContact(with);
 257        return !contact.showInContactList();
 258    }
 259
 260    ScheduledFuture<?> schedule(
 261            final Runnable runnable, final long delay, final TimeUnit timeUnit) {
 262        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
 263    }
 264
 265    void respondWithJingleError(
 266            final Account account,
 267            final Iq original,
 268            final String jingleCondition,
 269            final String condition,
 270            final String conditionType) {
 271        final Iq response = original.generateResponse(Iq.Type.ERROR);
 272        final Element error = response.addChild("error");
 273        error.setAttribute("type", conditionType);
 274        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
 275        if (jingleCondition != null) {
 276            error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
 277        }
 278        account.getXmppConnection().sendIqPacket(response, null);
 279    }
 280
 281    public void deliverMessage(
 282            final Account account,
 283            final Jid to,
 284            final Jid from,
 285            final Element message,
 286            String remoteMsgId,
 287            String serverMsgId,
 288            long timestamp) {
 289        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
 290        final String sessionId = message.getAttribute("id");
 291        if (sessionId == null) {
 292            return;
 293        }
 294        if ("accept".equals(message.getName())) {
 295            for (AbstractJingleConnection connection : connections.values()) {
 296                if (connection instanceof JingleRtpConnection rtpConnection) {
 297                    final AbstractJingleConnection.Id id = connection.getId();
 298                    if (id.account == account && id.sessionId.equals(sessionId)) {
 299                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 300                        return;
 301                    }
 302                }
 303            }
 304            return;
 305        }
 306        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
 307        // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
 308        final boolean addressedDirectly = to != null && to.equals(account.getJid());
 309        final AbstractJingleConnection.Id id;
 310        if (fromSelf) {
 311            if (to != null && to.isFullJid()) {
 312                id = AbstractJingleConnection.Id.of(account, to, sessionId);
 313            } else {
 314                return;
 315            }
 316        } else {
 317            id = AbstractJingleConnection.Id.of(account, from, sessionId);
 318        }
 319        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 320        if (existingJingleConnection != null) {
 321            if (existingJingleConnection instanceof JingleRtpConnection) {
 322                ((JingleRtpConnection) existingJingleConnection)
 323                        .deliveryMessage(from, message, serverMsgId, timestamp);
 324            } else {
 325                Log.d(
 326                        Config.LOGTAG,
 327                        account.getJid().asBareJid()
 328                                + ": "
 329                                + existingJingleConnection.getClass().getName()
 330                                + " does not support jingle messages");
 331            }
 332            return;
 333        }
 334
 335        if (fromSelf) {
 336            if ("proceed".equals(message.getName())) {
 337                final Conversation c =
 338                        mXmppConnectionService.findOrCreateConversation(
 339                                account, id.with, false, false);
 340                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
 341                if (previousBusy != null) {
 342                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
 343                    if (serverMsgId != null) {
 344                        previousBusy.setServerMsgId(serverMsgId);
 345                    }
 346                    previousBusy.setTime(timestamp);
 347                    mXmppConnectionService.updateMessage(previousBusy, true);
 348                    Log.d(
 349                            Config.LOGTAG,
 350                            id.account.getJid().asBareJid()
 351                                    + ": updated previous busy because call got picked up by"
 352                                    + " another device");
 353                    mXmppConnectionService.getNotificationService().clearMissedCall(previousBusy);
 354                    return;
 355                }
 356            }
 357            // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
 358            // be sent to own bare jid as well)
 359            Log.d(
 360                    Config.LOGTAG,
 361                    account.getJid().asBareJid() + ": ignore jingle message from self");
 362            return;
 363        }
 364
 365        if ("propose".equals(message.getName())) {
 366            final Propose propose = Propose.upgrade(message);
 367            final List<GenericDescription> descriptions = propose.getDescriptions();
 368            final Collection<RtpDescription> rtpDescriptions =
 369                    Collections2.transform(
 370                            Collections2.filter(descriptions, d -> d instanceof RtpDescription),
 371                            input -> (RtpDescription) input);
 372            if (rtpDescriptions.size() > 0
 373                    && rtpDescriptions.size() == descriptions.size()
 374                    && isUsingClearNet(account)) {
 375                final Collection<Media> media =
 376                        Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
 377                if (media.contains(Media.UNKNOWN)) {
 378                    Log.d(
 379                            Config.LOGTAG,
 380                            account.getJid().asBareJid()
 381                                    + ": encountered unknown media in session proposal. "
 382                                    + propose);
 383                    return;
 384                }
 385                final Optional<RtpSessionProposal> matchingSessionProposal =
 386                        findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
 387                if (matchingSessionProposal.isPresent()) {
 388                    final String ourSessionId = matchingSessionProposal.get().sessionId;
 389                    final String theirSessionId = id.sessionId;
 390                    if (ComparisonChain.start()
 391                                    .compare(ourSessionId, theirSessionId)
 392                                    .compare(account.getJid().toString(), id.with.toString())
 393                                    .result()
 394                            > 0) {
 395                        Log.d(
 396                                Config.LOGTAG,
 397                                account.getJid().asBareJid()
 398                                        + ": our session lost tie break. automatically accepting"
 399                                        + " their session. winning Session="
 400                                        + theirSessionId);
 401                        // TODO a retract for this reason should probably include some indication of
 402                        // tie break
 403                        retractSessionProposal(matchingSessionProposal.get());
 404                        final JingleRtpConnection rtpConnection =
 405                                new JingleRtpConnection(this, id, from);
 406                        this.connections.put(id, rtpConnection);
 407                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 408                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 409                        addNewIncomingCall(rtpConnection);
 410                        // TODO actually do the automatic accept?!
 411                    } else {
 412                        Log.d(
 413                                Config.LOGTAG,
 414                                account.getJid().asBareJid()
 415                                        + ": our session won tie break. waiting for other party to"
 416                                        + " accept. winningSession="
 417                                        + ourSessionId);
 418                        // TODO reject their session with <tie-break/>?
 419                    }
 420                    return;
 421                }
 422                final boolean stranger =
 423                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 424                if (isBusy() || stranger) {
 425                    writeLogMissedIncoming(
 426                            account,
 427                            id.with.asBareJid(),
 428                            id.sessionId,
 429                            serverMsgId,
 430                            timestamp,
 431                            stranger);
 432                    if (stranger) {
 433                        Log.d(
 434                                Config.LOGTAG,
 435                                id.account.getJid().asBareJid()
 436                                        + ": ignoring call proposal from stranger "
 437                                        + id.with);
 438                        return;
 439                    }
 440                    final int activeDevices = account.activeDevicesWithRtpCapability();
 441                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
 442                    if (activeDevices == 0) {
 443                        final var reject =
 444                                mXmppConnectionService
 445                                        .getMessageGenerator()
 446                                        .sessionReject(from, sessionId);
 447                        mXmppConnectionService.sendMessagePacket(account, reject);
 448                    } else {
 449                        Log.d(
 450                                Config.LOGTAG,
 451                                id.account.getJid().asBareJid()
 452                                        + ": ignoring proposal because busy on this device but"
 453                                        + " there are other devices");
 454                    }
 455                } else {
 456                    final JingleRtpConnection rtpConnection =
 457                            new JingleRtpConnection(this, id, from);
 458                    this.connections.put(id, rtpConnection);
 459                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 460                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 461                    addNewIncomingCall(rtpConnection);
 462                }
 463            } else {
 464                Log.d(
 465                        Config.LOGTAG,
 466                        account.getJid().asBareJid()
 467                                + ": unable to react to proposed session with "
 468                                + rtpDescriptions.size()
 469                                + " rtp descriptions of "
 470                                + descriptions.size()
 471                                + " total descriptions");
 472            }
 473        } else if (addressedDirectly && "proceed".equals(message.getName())) {
 474            synchronized (rtpSessionProposals) {
 475                final RtpSessionProposal proposal =
 476                        getRtpSessionProposal(account, from.asBareJid(), sessionId);
 477                if (proposal != null) {
 478                    rtpSessionProposals.remove(proposal);
 479                    final JingleRtpConnection rtpConnection =
 480                            new JingleRtpConnection(
 481                                    this, id, account.getJid(), proposal.callIntegration);
 482                    rtpConnection.setProposedMedia(proposal.media);
 483                    this.connections.put(id, rtpConnection);
 484                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
 485                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 486                } else {
 487                    Log.d(
 488                            Config.LOGTAG,
 489                            account.getJid().asBareJid()
 490                                    + ": no rtp session ("
 491                                    + sessionId
 492                                    + ") proposal found for "
 493                                    + from
 494                                    + " to deliver proceed");
 495                    if (remoteMsgId == null) {
 496                        return;
 497                    }
 498                    final var errorMessage =
 499                            new im.conversations.android.xmpp.model.stanza.Message();
 500                    errorMessage.setTo(from);
 501                    errorMessage.setId(remoteMsgId);
 502                    errorMessage.setType(
 503                            im.conversations.android.xmpp.model.stanza.Message.Type.ERROR);
 504                    final Element error = errorMessage.addChild("error");
 505                    error.setAttribute("code", "404");
 506                    error.setAttribute("type", "cancel");
 507                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 508                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 509                }
 510            }
 511        } else if (addressedDirectly && "reject".equals(message.getName())) {
 512            final RtpSessionProposal proposal =
 513                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 514            synchronized (rtpSessionProposals) {
 515                if (proposal != null) {
 516                    setTerminalSessionState(proposal, RtpEndUserState.DECLINED_OR_BUSY);
 517                    rtpSessionProposals.remove(proposal);
 518                    proposal.callIntegration.busy();
 519                    writeLogMissedOutgoing(
 520                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 521                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 522                            account,
 523                            proposal.with,
 524                            proposal.sessionId,
 525                            RtpEndUserState.DECLINED_OR_BUSY);
 526                } else {
 527                    Log.d(
 528                            Config.LOGTAG,
 529                            account.getJid().asBareJid()
 530                                    + ": no rtp session proposal found for "
 531                                    + from
 532                                    + " to deliver reject");
 533                }
 534            }
 535        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 536            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 537            updateProposedSessionDiscovered(
 538                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 539        } else {
 540            Log.d(
 541                    Config.LOGTAG,
 542                    account.getJid()
 543                            + ": received out of order jingle message from="
 544                            + from
 545                            + ", message="
 546                            + message
 547                            + ", addressedDirectly="
 548                            + addressedDirectly);
 549        }
 550    }
 551
 552    private RtpSessionProposal getRtpSessionProposal(
 553            final Account account, Jid from, String sessionId) {
 554        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
 555            if (rtpSessionProposal.sessionId.equals(sessionId)
 556                    && rtpSessionProposal.with.equals(from)
 557                    && rtpSessionProposal.account.getJid().equals(account.getJid())) {
 558                return rtpSessionProposal;
 559            }
 560        }
 561        return null;
 562    }
 563
 564    private void writeLogMissedOutgoing(
 565            final Account account,
 566            Jid with,
 567            final String sessionId,
 568            String serverMsgId,
 569            long timestamp) {
 570        final Conversation conversation =
 571                mXmppConnectionService.findOrCreateConversation(
 572                        account, with.asBareJid(), false, false);
 573        final Message message =
 574                new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
 575        message.setBody(new RtpSessionStatus(false, 0).toString());
 576        message.setServerMsgId(serverMsgId);
 577        message.setTime(timestamp);
 578        writeMessage(message);
 579    }
 580
 581    private void writeLogMissedIncoming(
 582            final Account account,
 583            final Jid with,
 584            final String sessionId,
 585            final String serverMsgId,
 586            final long timestamp,
 587            final boolean stranger) {
 588        final Conversation conversation =
 589                mXmppConnectionService.findOrCreateConversation(
 590                        account, with.asBareJid(), false, false);
 591        final Message message =
 592                new Message(
 593                        conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
 594        message.setBody(new RtpSessionStatus(false, 0).toString());
 595        message.setServerMsgId(serverMsgId);
 596        message.setTime(timestamp);
 597        message.setCounterpart(with);
 598        writeMessage(message);
 599        if (stranger) {
 600            return;
 601        }
 602        mXmppConnectionService.getNotificationService().pushMissedCallNow(message);
 603    }
 604
 605    private void writeMessage(final Message message) {
 606        final Conversational conversational = message.getConversation();
 607        if (conversational instanceof Conversation) {
 608            ((Conversation) conversational).add(message);
 609            mXmppConnectionService.databaseBackend.createMessage(message);
 610            mXmppConnectionService.updateConversationUi();
 611        } else {
 612            throw new IllegalStateException("Somehow the conversation in a message was a stub");
 613        }
 614    }
 615
 616    public void startJingleFileTransfer(final Message message) {
 617        Preconditions.checkArgument(
 618                message.isFileOrImage(), "Message is not of type file or image");
 619        final Transferable old = message.getTransferable();
 620        if (old != null) {
 621            old.cancel();
 622        }
 623        final JingleFileTransferConnection connection =
 624                new JingleFileTransferConnection(this, message);
 625        this.connections.put(connection.getId(), connection);
 626        connection.sendSessionInitialize();
 627    }
 628
 629    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
 630        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
 631                this.connections.entrySet()) {
 632            if (entry.getValue() instanceof JingleRtpConnection jingleRtpConnection) {
 633                final AbstractJingleConnection.Id id = entry.getKey();
 634                if (id.account == contact.getAccount()
 635                        && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
 636                    return Optional.of(jingleRtpConnection);
 637                }
 638            }
 639        }
 640        synchronized (this.rtpSessionProposals) {
 641            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 642                    this.rtpSessionProposals.entrySet()) {
 643                final RtpSessionProposal proposal = entry.getKey();
 644                if (proposal.account == contact.getAccount()
 645                        && contact.getJid().asBareJid().equals(proposal.with)) {
 646                    final DeviceDiscoveryState preexistingState = entry.getValue();
 647                    if (preexistingState != null
 648                            && preexistingState != DeviceDiscoveryState.FAILED) {
 649                        return Optional.of(proposal);
 650                    }
 651                }
 652            }
 653        }
 654        return Optional.absent();
 655    }
 656
 657    public JingleRtpConnection getOngoingRtpConnection() {
 658        for (final AbstractJingleConnection jingleConnection : this.connections.values()) {
 659            if (jingleConnection instanceof JingleRtpConnection jingleRtpConnection) {
 660                if (jingleRtpConnection.isTerminated()) {
 661                    continue;
 662                }
 663                return jingleRtpConnection;
 664            }
 665        }
 666        return null;
 667    }
 668
 669    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 670        final AbstractJingleConnection.Id id = connection.getId();
 671        if (this.connections.remove(id) == null) {
 672            throw new IllegalStateException(
 673                    String.format("Unable to finish connection with id=%s", id));
 674        }
 675        // update chat UI to remove 'ongoing call' icon
 676        mXmppConnectionService.updateConversationUi();
 677    }
 678
 679    public boolean fireJingleRtpConnectionStateUpdates() {
 680        for (final AbstractJingleConnection connection : this.connections.values()) {
 681            if (connection instanceof JingleRtpConnection jingleRtpConnection) {
 682                if (jingleRtpConnection.isTerminated()) {
 683                    continue;
 684                }
 685                jingleRtpConnection.fireStateUpdate();
 686                return true;
 687            }
 688        }
 689        return false;
 690    }
 691
 692    public void retractSessionProposal(final Account account, final Jid with) {
 693        synchronized (this.rtpSessionProposals) {
 694            RtpSessionProposal matchingProposal = null;
 695            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 696                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 697                    matchingProposal = proposal;
 698                    break;
 699                }
 700            }
 701            if (matchingProposal != null) {
 702                retractSessionProposal(matchingProposal, false);
 703            }
 704        }
 705    }
 706
 707    private void retractSessionProposal(final RtpSessionProposal rtpSessionProposal) {
 708        retractSessionProposal(rtpSessionProposal, true);
 709    }
 710
 711    private void retractSessionProposal(
 712            final RtpSessionProposal rtpSessionProposal, final boolean refresh) {
 713        final Account account = rtpSessionProposal.account;
 714        Log.d(
 715                Config.LOGTAG,
 716                account.getJid().asBareJid()
 717                        + ": retracting rtp session proposal with "
 718                        + rtpSessionProposal.with);
 719        this.rtpSessionProposals.remove(rtpSessionProposal);
 720        rtpSessionProposal.callIntegration.retracted();
 721        if (refresh) {
 722            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 723                    account,
 724                    rtpSessionProposal.with,
 725                    rtpSessionProposal.sessionId,
 726                    RtpEndUserState.RETRACTED);
 727        }
 728        final var messagePacket =
 729                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 730        writeLogMissedOutgoing(
 731                account,
 732                rtpSessionProposal.with,
 733                rtpSessionProposal.sessionId,
 734                null,
 735                System.currentTimeMillis());
 736        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 737    }
 738
 739    public JingleRtpConnection initializeRtpSession(
 740            final Account account, final Jid with, final Set<Media> media) {
 741        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 742        final JingleRtpConnection rtpConnection =
 743                new JingleRtpConnection(this, id, account.getJid());
 744        rtpConnection.setProposedMedia(media);
 745        rtpConnection.getCallIntegration().startAudioRouting();
 746        this.connections.put(id, rtpConnection);
 747        rtpConnection.sendSessionInitiate();
 748        return rtpConnection;
 749    }
 750
 751    public @Nullable RtpSessionProposal proposeJingleRtpSession(
 752            final Account account, final Jid with, final Set<Media> media) {
 753        synchronized (this.rtpSessionProposals) {
 754            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 755                    this.rtpSessionProposals.entrySet()) {
 756                final RtpSessionProposal proposal = entry.getKey();
 757                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 758                    final DeviceDiscoveryState preexistingState = entry.getValue();
 759                    if (preexistingState != null
 760                            && preexistingState != DeviceDiscoveryState.FAILED) {
 761                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 762                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 763                                account, with, proposal.sessionId, endUserState);
 764                        return proposal;
 765                    }
 766                }
 767            }
 768            if (isBusy()) {
 769                if (hasMatchingRtpSession(account, with, media)) {
 770                    Log.d(
 771                            Config.LOGTAG,
 772                            "ignoring request to propose jingle session because the other party"
 773                                    + " already created one for us");
 774                    // TODO return something that we can parse the connection of of
 775                    return null;
 776                }
 777                throw new IllegalStateException(
 778                        "There is already a running RTP session. This should have been caught by"
 779                                + " the UI");
 780            }
 781            final CallIntegration callIntegration =
 782                    new CallIntegration(mXmppConnectionService.getApplicationContext());
 783            callIntegration.setVideoState(
 784                    Media.audioOnly(media)
 785                            ? VideoProfile.STATE_AUDIO_ONLY
 786                            : VideoProfile.STATE_BIDIRECTIONAL);
 787            callIntegration.setAddress(
 788                    CallIntegration.address(with.asBareJid()), TelecomManager.PRESENTATION_ALLOWED);
 789            final var contact = account.getRoster().getContact(with);
 790            callIntegration.setCallerDisplayName(
 791                    contact.getDisplayName(), TelecomManager.PRESENTATION_ALLOWED);
 792            callIntegration.setInitialized();
 793            callIntegration.setInitialAudioDevice(CallIntegration.initialAudioDevice(media));
 794            callIntegration.startAudioRouting();
 795            final RtpSessionProposal proposal =
 796                    RtpSessionProposal.of(account, with.asBareJid(), media, callIntegration);
 797            callIntegration.setCallback(new ProposalStateCallback(proposal));
 798            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 799            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 800                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 801            final var messagePacket =
 802                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 803            // in privacy preserving environments 'propose' is only ACKed when we have presence
 804            // subscription (to not leak presence). Therefor a timeout is only appropriate for
 805            // contacts where we can expect the 'ringing' response
 806            final boolean triggerTimeout =
 807                    Config.JINGLE_MESSAGE_INIT_STRICT_DEVICE_TIMEOUT
 808                            || contact.mutualPresenceSubscription();
 809            SCHEDULED_EXECUTOR_SERVICE.schedule(
 810                    () -> {
 811                        final var currentProposalState = rtpSessionProposals.get(proposal);
 812                        Log.d(
 813                                Config.LOGTAG,
 814                                "proposal state after timeout " + currentProposalState);
 815                        if (triggerTimeout
 816                                && Arrays.asList(
 817                                                DeviceDiscoveryState.SEARCHING,
 818                                                DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)
 819                                        .contains(currentProposalState)) {
 820                            deviceDiscoveryTimeout(account, proposal);
 821                        }
 822                    },
 823                    Config.DEVICE_DISCOVERY_TIMEOUT,
 824                    TimeUnit.MILLISECONDS);
 825            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 826            return proposal;
 827        }
 828    }
 829
 830    private void deviceDiscoveryTimeout(final Account account, final RtpSessionProposal proposal) {
 831        // 'endUserState' is what we display in the UI. There is an argument to use 'BUSY' here
 832        // instead
 833        // we may or may not want to match this with the tone we are playing (see
 834        // callIntegration.error() or callIntegration.busy())
 835        final var endUserState = RtpEndUserState.CONNECTIVITY_ERROR;
 836        Log.d(Config.LOGTAG, "call proposal still in device discovery state after timeout");
 837        setTerminalSessionState(proposal, endUserState);
 838
 839        rtpSessionProposals.remove(proposal);
 840        // error and busy would probably be both appropriate tones to play
 841        // playing the error tone is probably more in line with what happens on a technical level
 842        // and would be a similar UX to what happens when you call a user that doesn't exist
 843        // playing the busy tone might be more in line with what some telephony networks play
 844        proposal.callIntegration.error();
 845        writeLogMissedOutgoing(
 846                account, proposal.with, proposal.sessionId, null, System.currentTimeMillis());
 847        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 848                account, proposal.with, proposal.sessionId, endUserState);
 849
 850        final var retraction =
 851                mXmppConnectionService.getMessageGenerator().sessionRetract(proposal);
 852        mXmppConnectionService.sendMessagePacket(account, retraction);
 853    }
 854
 855    public void sendJingleMessageFinish(
 856            final Contact contact, final String sessionId, final Reason reason) {
 857        final var account = contact.getAccount();
 858        final var messagePacket =
 859                mXmppConnectionService
 860                        .getMessageGenerator()
 861                        .sessionFinish(contact.getJid(), sessionId, reason);
 862        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 863    }
 864
 865    public Optional<RtpSessionProposal> matchingProposal(final Account account, final Jid with) {
 866        synchronized (this.rtpSessionProposals) {
 867            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 868                    this.rtpSessionProposals.entrySet()) {
 869                final RtpSessionProposal proposal = entry.getKey();
 870                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 871                    return Optional.of(proposal);
 872                }
 873            }
 874        }
 875        return Optional.absent();
 876    }
 877
 878    public boolean hasMatchingProposal(final Account account, final Jid with) {
 879        synchronized (this.rtpSessionProposals) {
 880            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 881                    this.rtpSessionProposals.entrySet()) {
 882                final var state = entry.getValue();
 883                final RtpSessionProposal proposal = entry.getKey();
 884                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 885                    // CallIntegrationConnectionService starts RtpSessionActivity with ACTION_VIEW
 886                    // and an EXTRA_LAST_REPORTED_STATE of DISCOVERING devices. however due to
 887                    // possible race conditions the state might have already moved on so we are
 888                    // going
 889                    // to update the UI
 890                    final RtpEndUserState endUserState = state.toEndUserState();
 891                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 892                            account, proposal.with, proposal.sessionId, endUserState);
 893                    return true;
 894                }
 895            }
 896        }
 897        return false;
 898    }
 899
 900    public void deliverIbbPacket(final Account account, final Iq packet) {
 901        // TODO use extensions
 902        final String sid;
 903        final Element payload;
 904        final InbandBytestreamsTransport.PacketType packetType;
 905        if (packet.hasChild("open", Namespace.IBB)) {
 906            packetType = InbandBytestreamsTransport.PacketType.OPEN;
 907            payload = packet.findChild("open", Namespace.IBB);
 908            sid = payload.getAttribute("sid");
 909        } else if (packet.hasChild("data", Namespace.IBB)) {
 910            packetType = InbandBytestreamsTransport.PacketType.DATA;
 911            payload = packet.findChild("data", Namespace.IBB);
 912            sid = payload.getAttribute("sid");
 913        } else if (packet.hasChild("close", Namespace.IBB)) {
 914            packetType = InbandBytestreamsTransport.PacketType.CLOSE;
 915            payload = packet.findChild("close", Namespace.IBB);
 916            sid = payload.getAttribute("sid");
 917        } else {
 918            packetType = null;
 919            payload = null;
 920            sid = null;
 921        }
 922        if (sid == null) {
 923            Log.d(
 924                    Config.LOGTAG,
 925                    account.getJid().asBareJid() + ": unable to deliver ibb packet. missing sid");
 926            account.getXmppConnection().sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 927            return;
 928        }
 929        for (final AbstractJingleConnection connection : this.connections.values()) {
 930            if (connection instanceof JingleFileTransferConnection fileTransfer) {
 931                final Transport transport = fileTransfer.getTransport();
 932                if (transport instanceof InbandBytestreamsTransport inBandTransport) {
 933                    if (sid.equals(inBandTransport.getStreamId())) {
 934                        if (inBandTransport.deliverPacket(packetType, packet.getFrom(), payload)) {
 935                            account.getXmppConnection()
 936                                    .sendIqPacket(packet.generateResponse(Iq.Type.RESULT), null);
 937                        } else {
 938                            account.getXmppConnection()
 939                                    .sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 940                        }
 941                        return;
 942                    }
 943                }
 944            }
 945        }
 946        Log.d(
 947                Config.LOGTAG,
 948                account.getJid().asBareJid() + ": unable to deliver ibb packet with sid=" + sid);
 949        account.getXmppConnection().sendIqPacket(packet.generateResponse(Iq.Type.ERROR), null);
 950    }
 951
 952    public void notifyRebound(final Account account) {
 953        for (final AbstractJingleConnection connection : this.connections.values()) {
 954            connection.notifyRebound();
 955        }
 956        final XmppConnection xmppConnection = account.getXmppConnection();
 957        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 958            resendSessionProposals(account);
 959        }
 960    }
 961
 962    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 963            Account account, Jid with, String sessionId) {
 964        final AbstractJingleConnection.Id id =
 965                AbstractJingleConnection.Id.of(account, with, sessionId);
 966        final AbstractJingleConnection connection = connections.get(id);
 967        if (connection instanceof JingleRtpConnection) {
 968            return new WeakReference<>((JingleRtpConnection) connection);
 969        }
 970        return null;
 971    }
 972
 973    public JingleRtpConnection findJingleRtpConnection(final Account account, final Jid with) {
 974        for (final AbstractJingleConnection connection : this.connections.values()) {
 975            if (connection instanceof JingleRtpConnection rtpConnection) {
 976                if (rtpConnection.isTerminated()) {
 977                    continue;
 978                }
 979                final var id = rtpConnection.getId();
 980                if (id.account == account && account.getJid().equals(with)) {
 981                    return rtpConnection;
 982                }
 983            }
 984        }
 985        return null;
 986    }
 987
 988    private void resendSessionProposals(final Account account) {
 989        synchronized (this.rtpSessionProposals) {
 990            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 991                    this.rtpSessionProposals.entrySet()) {
 992                final RtpSessionProposal proposal = entry.getKey();
 993                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 994                        && proposal.account == account) {
 995                    Log.d(
 996                            Config.LOGTAG,
 997                            account.getJid().asBareJid()
 998                                    + ": resending session proposal to "
 999                                    + proposal.with);
1000                    final var messagePacket =
1001                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
1002                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
1003                }
1004            }
1005        }
1006    }
1007
1008    public void updateProposedSessionDiscovered(
1009            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
1010        synchronized (this.rtpSessionProposals) {
1011            final RtpSessionProposal sessionProposal =
1012                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
1013            final DeviceDiscoveryState currentState =
1014                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
1015            if (currentState == null) {
1016                Log.d(
1017                        Config.LOGTAG,
1018                        "unable to find session proposal for session id "
1019                                + sessionId
1020                                + " target="
1021                                + target);
1022                return;
1023            }
1024            if (currentState == DeviceDiscoveryState.DISCOVERED) {
1025                Log.d(
1026                        Config.LOGTAG,
1027                        "session proposal already at discovered. not going to fall back");
1028                return;
1029            }
1030
1031            Log.d(
1032                    Config.LOGTAG,
1033                    account.getJid().asBareJid()
1034                            + ": flagging session "
1035                            + sessionId
1036                            + " as "
1037                            + target);
1038
1039            final RtpEndUserState endUserState = target.toEndUserState();
1040
1041            if (target == DeviceDiscoveryState.FAILED) {
1042                Log.d(Config.LOGTAG, "removing session proposal after failure");
1043                setTerminalSessionState(sessionProposal, endUserState);
1044                this.rtpSessionProposals.remove(sessionProposal);
1045                sessionProposal.getCallIntegration().error();
1046                mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1047                        account, sessionProposal.with, sessionProposal.sessionId, endUserState);
1048                return;
1049            }
1050
1051            this.rtpSessionProposals.put(sessionProposal, target);
1052
1053            if (endUserState == RtpEndUserState.RINGING) {
1054                sessionProposal.callIntegration.setDialing();
1055            }
1056
1057            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1058                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
1059        }
1060    }
1061
1062    public void rejectRtpSession(final String sessionId) {
1063        for (final AbstractJingleConnection connection : this.connections.values()) {
1064            if (connection.getId().sessionId.equals(sessionId)) {
1065                if (connection instanceof JingleRtpConnection) {
1066                    try {
1067                        ((JingleRtpConnection) connection).rejectCall();
1068                        return;
1069                    } catch (final IllegalStateException e) {
1070                        Log.w(
1071                                Config.LOGTAG,
1072                                "race condition on rejecting call from notification",
1073                                e);
1074                    }
1075                }
1076            }
1077        }
1078    }
1079
1080    public void endRtpSession(final String sessionId) {
1081        for (final AbstractJingleConnection connection : this.connections.values()) {
1082            if (connection.getId().sessionId.equals(sessionId)) {
1083                if (connection instanceof JingleRtpConnection) {
1084                    ((JingleRtpConnection) connection).endCall();
1085                }
1086            }
1087        }
1088    }
1089
1090    public void failProceed(
1091            Account account, final Jid with, final String sessionId, final String message) {
1092        final AbstractJingleConnection.Id id =
1093                AbstractJingleConnection.Id.of(account, with, sessionId);
1094        final AbstractJingleConnection existingJingleConnection = connections.get(id);
1095        if (existingJingleConnection instanceof JingleRtpConnection) {
1096            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
1097        }
1098    }
1099
1100    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
1101        if (connections.containsValue(connection)) {
1102            return;
1103        }
1104        final IllegalStateException e =
1105                new IllegalStateException(
1106                        "JingleConnection has not been registered with connection manager");
1107        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
1108        throw e;
1109    }
1110
1111    void setTerminalSessionState(
1112            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
1113        this.terminatedSessions.put(
1114                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
1115    }
1116
1117    void setTerminalSessionState(final RtpSessionProposal proposal, final RtpEndUserState state) {
1118        this.terminatedSessions.put(
1119                PersistableSessionId.of(proposal), new TerminatedRtpSession(state, proposal.media));
1120    }
1121
1122    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
1123        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
1124    }
1125
1126    private static class PersistableSessionId {
1127        private final Jid with;
1128        private final String sessionId;
1129
1130        private PersistableSessionId(Jid with, String sessionId) {
1131            this.with = with;
1132            this.sessionId = sessionId;
1133        }
1134
1135        public static PersistableSessionId of(final AbstractJingleConnection.Id id) {
1136            return new PersistableSessionId(id.with, id.sessionId);
1137        }
1138
1139        public static PersistableSessionId of(final RtpSessionProposal proposal) {
1140            return new PersistableSessionId(proposal.with, proposal.sessionId);
1141        }
1142
1143        @Override
1144        public boolean equals(Object o) {
1145            if (this == o) return true;
1146            if (o == null || getClass() != o.getClass()) return false;
1147            PersistableSessionId that = (PersistableSessionId) o;
1148            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
1149        }
1150
1151        @Override
1152        public int hashCode() {
1153            return Objects.hashCode(with, sessionId);
1154        }
1155    }
1156
1157    public static class TerminatedRtpSession {
1158        public final RtpEndUserState state;
1159        public final Set<Media> media;
1160
1161        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
1162            this.state = state;
1163            this.media = media;
1164        }
1165    }
1166
1167    public enum DeviceDiscoveryState {
1168        SEARCHING,
1169        SEARCHING_ACKNOWLEDGED,
1170        DISCOVERED,
1171        FAILED;
1172
1173        public RtpEndUserState toEndUserState() {
1174            return switch (this) {
1175                case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
1176                case DISCOVERED -> RtpEndUserState.RINGING;
1177                default -> RtpEndUserState.CONNECTIVITY_ERROR;
1178            };
1179        }
1180    }
1181
1182    public static class RtpSessionProposal implements OngoingRtpSession {
1183        public final Jid with;
1184        public final String sessionId;
1185        public final Set<Media> media;
1186        private final Account account;
1187        private final CallIntegration callIntegration;
1188
1189        private RtpSessionProposal(
1190                Account account,
1191                Jid with,
1192                String sessionId,
1193                Set<Media> media,
1194                final CallIntegration callIntegration) {
1195            this.account = account;
1196            this.with = with;
1197            this.sessionId = sessionId;
1198            this.media = media;
1199            this.callIntegration = callIntegration;
1200        }
1201
1202        public static RtpSessionProposal of(
1203                Account account,
1204                Jid with,
1205                Set<Media> media,
1206                final CallIntegration callIntegration) {
1207            return new RtpSessionProposal(account, with, nextRandomId(), media, callIntegration);
1208        }
1209
1210        @Override
1211        public boolean equals(Object o) {
1212            if (this == o) return true;
1213            if (o == null || getClass() != o.getClass()) return false;
1214            RtpSessionProposal proposal = (RtpSessionProposal) o;
1215            return Objects.equal(account.getJid(), proposal.account.getJid())
1216                    && Objects.equal(with, proposal.with)
1217                    && Objects.equal(sessionId, proposal.sessionId);
1218        }
1219
1220        @Override
1221        public int hashCode() {
1222            return Objects.hashCode(account.getJid(), with, sessionId);
1223        }
1224
1225        @Override
1226        public Account getAccount() {
1227            return account;
1228        }
1229
1230        @Override
1231        public Jid getWith() {
1232            return with;
1233        }
1234
1235        @Override
1236        public String getSessionId() {
1237            return sessionId;
1238        }
1239
1240        @Override
1241        public CallIntegration getCallIntegration() {
1242            return this.callIntegration;
1243        }
1244
1245        @Override
1246        public Set<Media> getMedia() {
1247            return this.media;
1248        }
1249    }
1250
1251    public class ProposalStateCallback implements CallIntegration.Callback {
1252
1253        private final RtpSessionProposal proposal;
1254
1255        public ProposalStateCallback(final RtpSessionProposal proposal) {
1256            this.proposal = proposal;
1257        }
1258
1259        @Override
1260        public void onCallIntegrationShowIncomingCallUi() {}
1261
1262        @Override
1263        public void onCallIntegrationDisconnect() {
1264            Log.d(Config.LOGTAG, "a phone call has just been started. retracting proposal");
1265            retractSessionProposal(this.proposal);
1266        }
1267
1268        @Override
1269        public void onAudioDeviceChanged(
1270                final CallIntegration.AudioDevice selectedAudioDevice,
1271                final Set<CallIntegration.AudioDevice> availableAudioDevices) {
1272            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1273                    selectedAudioDevice, availableAudioDevices);
1274        }
1275
1276        @Override
1277        public void onCallIntegrationReject() {}
1278
1279        @Override
1280        public void onCallIntegrationAnswer() {}
1281
1282        @Override
1283        public void onCallIntegrationSilence() {}
1284
1285        @Override
1286        public void onCallIntegrationMicrophoneEnabled(boolean enabled) {}
1287    }
1288}