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