JingleConnectionManager.java

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