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