JingleConnectionManager.java

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