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