JingleConnectionManager.java

   1package eu.siacs.conversations.xmpp.jingle;
   2
   3import android.util.Base64;
   4import android.util.Log;
   5
   6import com.google.common.base.Objects;
   7import com.google.common.base.Optional;
   8import com.google.common.base.Preconditions;
   9import com.google.common.cache.Cache;
  10import com.google.common.cache.CacheBuilder;
  11import com.google.common.collect.Collections2;
  12import com.google.common.collect.ComparisonChain;
  13import com.google.common.collect.ImmutableSet;
  14
  15import eu.siacs.conversations.Config;
  16import eu.siacs.conversations.entities.Account;
  17import eu.siacs.conversations.entities.Contact;
  18import eu.siacs.conversations.entities.Conversation;
  19import eu.siacs.conversations.entities.Conversational;
  20import eu.siacs.conversations.entities.Message;
  21import eu.siacs.conversations.entities.RtpSessionStatus;
  22import eu.siacs.conversations.entities.Transferable;
  23import eu.siacs.conversations.services.AbstractConnectionManager;
  24import eu.siacs.conversations.services.XmppConnectionService;
  25import eu.siacs.conversations.xml.Element;
  26import eu.siacs.conversations.xml.Namespace;
  27import eu.siacs.conversations.xmpp.Jid;
  28import eu.siacs.conversations.xmpp.XmppConnection;
  29import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
  30import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
  31import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
  32import eu.siacs.conversations.xmpp.jingle.stanzas.IbbTransportInfo;
  33import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
  34import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
  35import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
  36import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
  37import eu.siacs.conversations.xmpp.jingle.transports.InbandBytestreamsTransport;
  38import eu.siacs.conversations.xmpp.jingle.transports.Transport;
  39import eu.siacs.conversations.xmpp.stanzas.IqPacket;
  40import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
  41
  42import java.lang.ref.WeakReference;
  43import java.security.SecureRandom;
  44import java.util.Collection;
  45import java.util.HashMap;
  46import java.util.List;
  47import java.util.Map;
  48import java.util.Set;
  49import java.util.concurrent.ConcurrentHashMap;
  50import java.util.concurrent.Executors;
  51import java.util.concurrent.ScheduledExecutorService;
  52import java.util.concurrent.ScheduledFuture;
  53import java.util.concurrent.TimeUnit;
  54
  55public class JingleConnectionManager extends AbstractConnectionManager {
  56    static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE =
  57            Executors.newSingleThreadScheduledExecutor();
  58    final ToneManager toneManager;
  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        this.toneManager = new ToneManager(service);
  70    }
  71
  72    static String nextRandomId() {
  73        final byte[] id = new byte[16];
  74        new SecureRandom().nextBytes(id);
  75        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING | Base64.URL_SAFE);
  76    }
  77
  78    public void deliverPacket(final Account account, final JinglePacket packet) {
  79        final String sessionId = packet.getSessionId();
  80        if (sessionId == null) {
  81            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
  82            return;
  83        }
  84        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
  85        final AbstractJingleConnection existingJingleConnection = connections.get(id);
  86        if (existingJingleConnection != null) {
  87            existingJingleConnection.deliverPacket(packet);
  88        } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
  89            final Jid from = packet.getFrom();
  90            final Content content = packet.getJingleContent();
  91            final String descriptionNamespace =
  92                    content == null ? null : content.getDescriptionNamespace();
  93            final AbstractJingleConnection connection;
  94            if (Namespace.JINGLE_APPS_FILE_TRANSFER.equals(descriptionNamespace)) {
  95                connection = new JingleFileTransferConnection(this, id, from);
  96            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace)
  97                    && isUsingClearNet(account)) {
  98                final boolean sessionEnded =
  99                        this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 100                final boolean stranger =
 101                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 102                final boolean busy = isBusy() != null;
 103                if (busy || sessionEnded || stranger) {
 104                    Log.d(
 105                            Config.LOGTAG,
 106                            id.account.getJid().asBareJid()
 107                                    + ": rejected session with "
 108                                    + id.with
 109                                    + " because busy. sessionEnded="
 110                                    + sessionEnded
 111                                    + ", stranger="
 112                                    + stranger);
 113                    mXmppConnectionService.sendIqPacket(
 114                            account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
 115                    final JinglePacket sessionTermination =
 116                            new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
 117                    sessionTermination.setTo(id.with);
 118                    sessionTermination.setReason(Reason.BUSY, null);
 119                    mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
 120                    if (busy || stranger) {
 121                        writeLogMissedIncoming(
 122                                account,
 123                                id.with,
 124                                id.sessionId,
 125                                null,
 126                                System.currentTimeMillis(),
 127                                stranger);
 128                    }
 129                    return;
 130                }
 131                connection = new JingleRtpConnection(this, id, from);
 132            } else {
 133                respondWithJingleError(
 134                        account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 135                return;
 136            }
 137            connections.put(id, connection);
 138            mXmppConnectionService.updateConversationUi();
 139            connection.deliverPacket(packet);
 140        } else {
 141            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 142            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 143        }
 144    }
 145
 146    private boolean isUsingClearNet(final Account account) {
 147        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
 148    }
 149
 150    public String isBusy() {
 151        if (mXmppConnectionService.isPhoneInCall()) {
 152            return "isPhoneInCall";
 153        }
 154        for (AbstractJingleConnection connection : this.connections.values()) {
 155            if (connection instanceof JingleRtpConnection) {
 156                if (((JingleRtpConnection) connection).isTerminated()) {
 157                    continue;
 158                }
 159                return "connection !isTerminated";
 160            }
 161        }
 162        synchronized (this.rtpSessionProposals) {
 163            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)) return "discovered";
 164            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)) return "searching";
 165            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)) return "searching_acknolwedged";
 166            return null;
 167        }
 168    }
 169
 170    public boolean hasJingleRtpConnection(final Account account) {
 171        for (AbstractJingleConnection connection : this.connections.values()) {
 172            if (connection instanceof JingleRtpConnection rtpConnection) {
 173                if (rtpConnection.isTerminated()) {
 174                    continue;
 175                }
 176                if (rtpConnection.id.account == account) {
 177                    return true;
 178                }
 179            }
 180        }
 181        return false;
 182    }
 183
 184    public void notifyPhoneCallStarted() {
 185        for (AbstractJingleConnection connection : connections.values()) {
 186            if (connection instanceof JingleRtpConnection rtpConnection) {
 187                if (rtpConnection.isTerminated()) {
 188                    continue;
 189                }
 190                rtpConnection.notifyPhoneCall();
 191            }
 192        }
 193    }
 194
 195    private Optional<RtpSessionProposal> findMatchingSessionProposal(
 196            final Account account, final Jid with, final Set<Media> media) {
 197        synchronized (this.rtpSessionProposals) {
 198            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 199                    this.rtpSessionProposals.entrySet()) {
 200                final RtpSessionProposal proposal = entry.getKey();
 201                final DeviceDiscoveryState state = entry.getValue();
 202                final boolean openProposal =
 203                        state == DeviceDiscoveryState.DISCOVERED
 204                                || state == DeviceDiscoveryState.SEARCHING
 205                                || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
 206                if (openProposal
 207                        && proposal.account == account
 208                        && proposal.with.equals(with.asBareJid())
 209                        && proposal.media.equals(media)) {
 210                    return Optional.of(proposal);
 211                }
 212            }
 213        }
 214        return Optional.absent();
 215    }
 216
 217    private String hasMatchingRtpSession(final Account account, final Jid with, final Set<Media> media) {
 218        for (AbstractJingleConnection connection : this.connections.values()) {
 219            if (connection instanceof JingleRtpConnection rtpConnection) {
 220                if (rtpConnection.isTerminated()) {
 221                    continue;
 222                }
 223                if (rtpConnection.getId().account == account
 224                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
 225                        && rtpConnection.getMedia().equals(media)) {
 226                    return rtpConnection.getId().sessionId;
 227                }
 228            }
 229        }
 230        return null;
 231    }
 232
 233    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
 234        final boolean notifyForStrangers =
 235                mXmppConnectionService.getNotificationService().notificationsFromStrangers();
 236        if (notifyForStrangers) {
 237            return false;
 238        }
 239        final Contact contact = account.getRoster().getContact(with);
 240        return !contact.showInContactList();
 241    }
 242
 243    ScheduledFuture<?> schedule(
 244            final Runnable runnable, final long delay, final TimeUnit timeUnit) {
 245        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
 246    }
 247
 248    void respondWithJingleError(
 249            final Account account,
 250            final IqPacket original,
 251            String jingleCondition,
 252            String condition,
 253            String conditionType) {
 254        final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
 255        final Element error = response.addChild("error");
 256        error.setAttribute("type", conditionType);
 257        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
 258        if (jingleCondition != null) {
 259            error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
 260        }
 261        account.getXmppConnection().sendIqPacket(response, null);
 262    }
 263
 264    public void deliverMessage(
 265            final Account account,
 266            final Jid to,
 267            final Jid from,
 268            final Element message,
 269            String remoteMsgId,
 270            String serverMsgId,
 271            long timestamp) {
 272        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
 273        final String sessionId = message.getAttribute("id");
 274        if (sessionId == null) {
 275            return;
 276        }
 277        if ("accept".equals(message.getName()) || "reject".equals(message.getName())) {
 278            for (AbstractJingleConnection connection : connections.values()) {
 279                if (connection instanceof JingleRtpConnection rtpConnection) {
 280                    final AbstractJingleConnection.Id id = connection.getId();
 281                    if (id.account == account && id.sessionId.equals(sessionId)) {
 282                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 283                        return;
 284                    }
 285                }
 286            }
 287            if ("accept".equals(message.getName())) return;
 288        }
 289        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
 290        // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
 291        final boolean addressedDirectly = to != null && to.equals(account.getJid());
 292        final AbstractJingleConnection.Id id;
 293        if (fromSelf) {
 294            if (to != null && to.isFullJid()) {
 295                id = AbstractJingleConnection.Id.of(account, to, sessionId);
 296            } else {
 297                return;
 298            }
 299        } else {
 300            id = AbstractJingleConnection.Id.of(account, from, sessionId);
 301        }
 302        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 303        if (existingJingleConnection != null) {
 304            if (existingJingleConnection instanceof JingleRtpConnection) {
 305                ((JingleRtpConnection) existingJingleConnection)
 306                        .deliveryMessage(from, message, serverMsgId, timestamp);
 307            } else {
 308                Log.d(
 309                        Config.LOGTAG,
 310                        account.getJid().asBareJid()
 311                                + ": "
 312                                + existingJingleConnection.getClass().getName()
 313                                + " does not support jingle messages");
 314            }
 315            return;
 316        }
 317
 318        if (fromSelf) {
 319            if ("proceed".equals(message.getName())) {
 320                final Conversation c =
 321                        mXmppConnectionService.findOrCreateConversation(
 322                                account, id.with, false, false);
 323                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
 324                if (previousBusy != null) {
 325                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
 326                    if (serverMsgId != null) {
 327                        previousBusy.setServerMsgId(serverMsgId);
 328                    }
 329                    previousBusy.setTime(timestamp);
 330                    mXmppConnectionService.updateMessage(previousBusy, true);
 331                    Log.d(
 332                            Config.LOGTAG,
 333                            id.account.getJid().asBareJid()
 334                                    + ": updated previous busy because call got picked up by another device");
 335                    mXmppConnectionService.getNotificationService().clearMissedCall(previousBusy);
 336                    return;
 337                }
 338            }
 339            // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
 340            // be sent to own bare jid as well)
 341            Log.d(
 342                    Config.LOGTAG,
 343                    account.getJid().asBareJid() + ": ignore jingle message from self");
 344            return;
 345        }
 346
 347        if ("propose".equals(message.getName())) {
 348            final Propose propose = Propose.upgrade(message);
 349            final List<GenericDescription> descriptions = propose.getDescriptions();
 350            final Collection<RtpDescription> rtpDescriptions =
 351                    Collections2.transform(
 352                            Collections2.filter(descriptions, d -> d instanceof RtpDescription),
 353                            input -> (RtpDescription) input);
 354            if (rtpDescriptions.size() > 0
 355                    && rtpDescriptions.size() == descriptions.size()
 356                    && isUsingClearNet(account)) {
 357                final Collection<Media> media =
 358                        Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
 359                if (media.contains(Media.UNKNOWN)) {
 360                    Log.d(
 361                            Config.LOGTAG,
 362                            account.getJid().asBareJid()
 363                                    + ": encountered unknown media in session proposal. "
 364                                    + propose);
 365                    return;
 366                }
 367                final Optional<RtpSessionProposal> matchingSessionProposal =
 368                        findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
 369                if (matchingSessionProposal.isPresent()) {
 370                    final String ourSessionId = matchingSessionProposal.get().sessionId;
 371                    final String theirSessionId = id.sessionId;
 372                    if (ComparisonChain.start()
 373                                    .compare(ourSessionId, theirSessionId)
 374                                    .compare(
 375                                            account.getJid().toEscapedString(),
 376                                            id.with.toEscapedString())
 377                                    .result()
 378                            > 0) {
 379                        Log.d(
 380                                Config.LOGTAG,
 381                                account.getJid().asBareJid()
 382                                        + ": our session lost tie break. automatically accepting their session. winning Session="
 383                                        + theirSessionId);
 384                        // TODO a retract for this reason should probably include some indication of
 385                        // tie break
 386                        retractSessionProposal(matchingSessionProposal.get());
 387                        final JingleRtpConnection rtpConnection =
 388                                new JingleRtpConnection(this, id, from);
 389                        this.connections.put(id, rtpConnection);
 390                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 391                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 392                        // TODO actually do the automatic accept?!
 393                    } else {
 394                        Log.d(
 395                                Config.LOGTAG,
 396                                account.getJid().asBareJid()
 397                                        + ": our session won tie break. waiting for other party to accept. winningSession="
 398                                        + ourSessionId);
 399                        // TODO reject their session with <tie-break/>?
 400                    }
 401                    return;
 402                }
 403                final boolean stranger =
 404                        isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 405                if (isBusy() != null || stranger) {
 406                    writeLogMissedIncoming(
 407                            account,
 408                            id.with.asBareJid(),
 409                            id.sessionId,
 410                            serverMsgId,
 411                            timestamp,
 412                            stranger);
 413                    if (stranger) {
 414                        Log.d(
 415                                Config.LOGTAG,
 416                                id.account.getJid().asBareJid()
 417                                        + ": ignoring call proposal from stranger "
 418                                        + id.with);
 419                        return;
 420                    }
 421                    final int activeDevices = account.activeDevicesWithRtpCapability();
 422                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
 423                    if (activeDevices == 0) {
 424                        final MessagePacket reject =
 425                                mXmppConnectionService
 426                                        .getMessageGenerator()
 427                                        .sessionReject(from, sessionId);
 428                        mXmppConnectionService.sendMessagePacket(account, reject);
 429                    } else {
 430                        Log.d(
 431                                Config.LOGTAG,
 432                                id.account.getJid().asBareJid()
 433                                        + ": ignoring proposal because busy on this device but there are other devices");
 434                    }
 435                } else {
 436                    final JingleRtpConnection rtpConnection =
 437                            new JingleRtpConnection(this, id, from);
 438                    this.connections.put(id, rtpConnection);
 439                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
 440                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 441                }
 442            } else {
 443                Log.d(
 444                        Config.LOGTAG,
 445                        account.getJid().asBareJid()
 446                                + ": unable to react to proposed session with "
 447                                + rtpDescriptions.size()
 448                                + " rtp descriptions of "
 449                                + descriptions.size()
 450                                + " total descriptions");
 451            }
 452        } else if (addressedDirectly && "proceed".equals(message.getName())) {
 453            synchronized (rtpSessionProposals) {
 454                final RtpSessionProposal proposal =
 455                        getRtpSessionProposal(account, from.asBareJid(), sessionId);
 456                if (proposal != null) {
 457                    rtpSessionProposals.remove(proposal);
 458                    final JingleRtpConnection rtpConnection =
 459                            new JingleRtpConnection(this, id, account.getJid());
 460                    rtpConnection.setProposedMedia(proposal.media);
 461                    this.connections.put(id, rtpConnection);
 462                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
 463                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
 464                } else {
 465                    Log.d(
 466                            Config.LOGTAG,
 467                            account.getJid().asBareJid()
 468                                    + ": no rtp session ("
 469                                    + sessionId
 470                                    + ") proposal found for "
 471                                    + from
 472                                    + " to deliver proceed");
 473                    if (remoteMsgId == null) {
 474                        return;
 475                    }
 476                    final MessagePacket errorMessage = new MessagePacket();
 477                    errorMessage.setTo(from);
 478                    errorMessage.setId(remoteMsgId);
 479                    errorMessage.setType(MessagePacket.TYPE_ERROR);
 480                    final Element error = errorMessage.addChild("error");
 481                    error.setAttribute("code", "404");
 482                    error.setAttribute("type", "cancel");
 483                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
 484                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
 485                }
 486            }
 487        } else if (addressedDirectly && "reject".equals(message.getName())) {
 488            final RtpSessionProposal proposal =
 489                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 490            synchronized (rtpSessionProposals) {
 491                if (proposal != null && rtpSessionProposals.remove(proposal) != null) {
 492                    writeLogMissedOutgoing(
 493                            account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
 494                    toneManager.transition(RtpEndUserState.DECLINED_OR_BUSY, proposal.media);
 495                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 496                            account,
 497                            proposal.with,
 498                            proposal.sessionId,
 499                            RtpEndUserState.DECLINED_OR_BUSY);
 500                } else {
 501                    Log.d(
 502                            Config.LOGTAG,
 503                            account.getJid().asBareJid()
 504                                    + ": no rtp session proposal found for "
 505                                    + from
 506                                    + " to deliver reject");
 507                }
 508            }
 509        } else if (addressedDirectly && "ringing".equals(message.getName())) {
 510            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
 511            updateProposedSessionDiscovered(
 512                    account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
 513        } else {
 514            Log.d(
 515                    Config.LOGTAG,
 516                    account.getJid()
 517                            + ": retrieved out of order jingle message from "
 518                            + from
 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 finishConnection(final AbstractJingleConnection connection) {
 631        this.connections.remove(connection.getId());
 632    }
 633
 634    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
 635        final AbstractJingleConnection.Id id = connection.getId();
 636        if (this.connections.remove(id) == null) {
 637            throw new IllegalStateException(
 638                    String.format("Unable to finish connection with id=%s", id));
 639        }
 640        // update chat UI to remove 'ongoing call' icon
 641        mXmppConnectionService.updateConversationUi();
 642    }
 643
 644    public boolean fireJingleRtpConnectionStateUpdates() {
 645        boolean firedUpdates = false;
 646        for (final AbstractJingleConnection connection : this.connections.values()) {
 647            if (connection instanceof JingleRtpConnection jingleRtpConnection) {
 648                if (jingleRtpConnection.isTerminated()) {
 649                    continue;
 650                }
 651                jingleRtpConnection.fireStateUpdate();
 652                firedUpdates = true;
 653            }
 654        }
 655        return firedUpdates;
 656    }
 657
 658    public void retractSessionProposal(final Account account, final Jid with) {
 659        synchronized (this.rtpSessionProposals) {
 660            RtpSessionProposal matchingProposal = null;
 661            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
 662                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 663                    matchingProposal = proposal;
 664                    break;
 665                }
 666            }
 667            if (matchingProposal != null) {
 668                retractSessionProposal(matchingProposal);
 669            }
 670        }
 671    }
 672
 673    private void retractSessionProposal(RtpSessionProposal rtpSessionProposal) {
 674        final Account account = rtpSessionProposal.account;
 675        toneManager.transition(RtpEndUserState.ENDED, rtpSessionProposal.media);
 676        Log.d(
 677                Config.LOGTAG,
 678                account.getJid().asBareJid()
 679                        + ": retracting rtp session proposal with "
 680                        + rtpSessionProposal.with);
 681        this.rtpSessionProposals.remove(rtpSessionProposal);
 682        final MessagePacket messagePacket =
 683                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 684        writeLogMissedOutgoing(
 685                account,
 686                rtpSessionProposal.with,
 687                rtpSessionProposal.sessionId,
 688                null,
 689                System.currentTimeMillis());
 690        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 691    }
 692
 693    public String initializeRtpSession(
 694            final Account account, final Jid with, final Set<Media> media) {
 695        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 696        final JingleRtpConnection rtpConnection =
 697                new JingleRtpConnection(this, id, account.getJid());
 698        rtpConnection.setProposedMedia(media);
 699        this.connections.put(id, rtpConnection);
 700        rtpConnection.sendSessionInitiate();
 701        return id.sessionId;
 702    }
 703
 704    public String proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
 705        synchronized (this.rtpSessionProposals) {
 706            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 707                    this.rtpSessionProposals.entrySet()) {
 708                RtpSessionProposal proposal = entry.getKey();
 709                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 710                    final DeviceDiscoveryState preexistingState = entry.getValue();
 711                    if (preexistingState != null
 712                            && preexistingState != DeviceDiscoveryState.FAILED) {
 713                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
 714                        toneManager.transition(endUserState, media);
 715                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 716                                account,
 717                                with,
 718                                proposal.sessionId,
 719                                endUserState
 720                        );
 721                        return proposal.sessionId;
 722                    }
 723                }
 724            }
 725            String busyCode = isBusy();
 726            if (busyCode != null) {
 727                String sessionId = hasMatchingRtpSession(account, with, media);
 728                if (sessionId != null) {
 729                    Log.d(Config.LOGTAG, "ignoring request to propose jingle session because the other party already created one for us: " + sessionId);
 730                    return sessionId;
 731                }
 732                throw new IllegalStateException("There is already a running RTP session: " + busyCode);
 733            }
 734            final RtpSessionProposal proposal =
 735                    RtpSessionProposal.of(account, with.asBareJid(), media);
 736            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 737            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 738                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 739            final MessagePacket messagePacket =
 740                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 741            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 742            return proposal.sessionId;
 743        }
 744    }
 745
 746    public boolean hasMatchingProposal(final Account account, final Jid with) {
 747        synchronized (this.rtpSessionProposals) {
 748            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 749                    this.rtpSessionProposals.entrySet()) {
 750                final RtpSessionProposal proposal = entry.getKey();
 751                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 752                    return true;
 753                }
 754            }
 755        }
 756        return false;
 757    }
 758
 759    public void deliverIbbPacket(final Account account, final IqPacket packet) {
 760        final String sid;
 761        final Element payload;
 762        final InbandBytestreamsTransport.PacketType packetType;
 763        if (packet.hasChild("open", Namespace.IBB)) {
 764            packetType = InbandBytestreamsTransport.PacketType.OPEN;
 765            payload = packet.findChild("open", Namespace.IBB);
 766            sid = payload.getAttribute("sid");
 767        } else if (packet.hasChild("data", Namespace.IBB)) {
 768            packetType = InbandBytestreamsTransport.PacketType.DATA;
 769            payload = packet.findChild("data", Namespace.IBB);
 770            sid = payload.getAttribute("sid");
 771        } else if (packet.hasChild("close", Namespace.IBB)) {
 772            packetType = InbandBytestreamsTransport.PacketType.CLOSE;
 773            payload = packet.findChild("close", Namespace.IBB);
 774            sid = payload.getAttribute("sid");
 775        } else {
 776            packetType = null;
 777            payload = null;
 778            sid = null;
 779        }
 780        if (sid == null) {
 781            Log.d(Config.LOGTAG, account.getJid().asBareJid()+": unable to deliver ibb packet. missing sid");
 782            account.getXmppConnection()
 783                    .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 784            return;
 785        }
 786        for (final AbstractJingleConnection connection : this.connections.values()) {
 787            if (connection instanceof JingleFileTransferConnection fileTransfer) {
 788                final Transport transport = fileTransfer.getTransport();
 789                if (transport instanceof InbandBytestreamsTransport inBandTransport) {
 790                    if (sid.equals(inBandTransport.getStreamId())) {
 791                        if (inBandTransport.deliverPacket(packetType, packet.getFrom(), payload)) {
 792                            account.getXmppConnection()
 793                                    .sendIqPacket(
 794                                            packet.generateResponse(IqPacket.TYPE.RESULT), null);
 795                        } else {
 796                            account.getXmppConnection()
 797                                    .sendIqPacket(
 798                                            packet.generateResponse(IqPacket.TYPE.ERROR), null);
 799                        }
 800                        return;
 801                    }
 802                }
 803            }
 804        }
 805        Log.d(Config.LOGTAG, account.getJid().asBareJid()+": unable to deliver ibb packet with sid="+sid);
 806        account.getXmppConnection()
 807                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 808    }
 809
 810    public void notifyRebound(final Account account) {
 811        for (final AbstractJingleConnection connection : this.connections.values()) {
 812            connection.notifyRebound();
 813        }
 814        final XmppConnection xmppConnection = account.getXmppConnection();
 815        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 816            resendSessionProposals(account);
 817        }
 818    }
 819
 820    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 821            Account account, Jid with, String sessionId) {
 822        final AbstractJingleConnection.Id id =
 823                AbstractJingleConnection.Id.of(account, with, sessionId);
 824        final AbstractJingleConnection connection = connections.get(id);
 825        if (connection instanceof JingleRtpConnection) {
 826            return new WeakReference<>((JingleRtpConnection) connection);
 827        }
 828        return null;
 829    }
 830
 831    private void resendSessionProposals(final Account account) {
 832        synchronized (this.rtpSessionProposals) {
 833            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 834                    this.rtpSessionProposals.entrySet()) {
 835                final RtpSessionProposal proposal = entry.getKey();
 836                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 837                        && proposal.account == account) {
 838                    Log.d(
 839                            Config.LOGTAG,
 840                            account.getJid().asBareJid()
 841                                    + ": resending session proposal to "
 842                                    + proposal.with);
 843                    final MessagePacket messagePacket =
 844                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 845                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 846                }
 847            }
 848        }
 849    }
 850
 851    public void updateProposedSessionDiscovered(
 852            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 853        synchronized (this.rtpSessionProposals) {
 854            final RtpSessionProposal sessionProposal =
 855                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 856            final DeviceDiscoveryState currentState =
 857                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 858            if (currentState == null) {
 859                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 860                return;
 861            }
 862            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 863                Log.d(
 864                        Config.LOGTAG,
 865                        "session proposal already at discovered. not going to fall back");
 866                return;
 867            }
 868            this.rtpSessionProposals.put(sessionProposal, target);
 869            final RtpEndUserState endUserState = target.toEndUserState();
 870            toneManager.transition(endUserState, sessionProposal.media);
 871            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 872                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 873            Log.d(
 874                    Config.LOGTAG,
 875                    account.getJid().asBareJid()
 876                            + ": flagging session "
 877                            + sessionId
 878                            + " as "
 879                            + target);
 880        }
 881    }
 882
 883    public void rejectRtpSession(final String sessionId) {
 884        for (final AbstractJingleConnection connection : this.connections.values()) {
 885            if (connection.getId().sessionId.equals(sessionId)) {
 886                if (connection instanceof JingleRtpConnection) {
 887                    try {
 888                        ((JingleRtpConnection) connection).rejectCall();
 889                        return;
 890                    } catch (final IllegalStateException e) {
 891                        Log.w(
 892                                Config.LOGTAG,
 893                                "race condition on rejecting call from notification",
 894                                e);
 895                    }
 896                }
 897            }
 898        }
 899    }
 900
 901    public void endRtpSession(final String sessionId) {
 902        for (final AbstractJingleConnection connection : this.connections.values()) {
 903            if (connection.getId().sessionId.equals(sessionId)) {
 904                if (connection instanceof JingleRtpConnection) {
 905                    ((JingleRtpConnection) connection).endCall();
 906                }
 907            }
 908        }
 909    }
 910
 911    public void failProceed(
 912            Account account, final Jid with, final String sessionId, final String message) {
 913        final AbstractJingleConnection.Id id =
 914                AbstractJingleConnection.Id.of(account, with, sessionId);
 915        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 916        if (existingJingleConnection instanceof JingleRtpConnection) {
 917            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 918        }
 919    }
 920
 921    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 922        if (connections.containsValue(connection)) {
 923            return;
 924        }
 925        final IllegalStateException e =
 926                new IllegalStateException(
 927                        "JingleConnection has not been registered with connection manager");
 928        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 929        throw e;
 930    }
 931
 932    void setTerminalSessionState(
 933            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
 934        this.terminatedSessions.put(
 935                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
 936    }
 937
 938    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
 939        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
 940    }
 941
 942    private static class PersistableSessionId {
 943        private final Jid with;
 944        private final String sessionId;
 945
 946        private PersistableSessionId(Jid with, String sessionId) {
 947            this.with = with;
 948            this.sessionId = sessionId;
 949        }
 950
 951        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
 952            return new PersistableSessionId(id.with, id.sessionId);
 953        }
 954
 955        @Override
 956        public boolean equals(Object o) {
 957            if (this == o) return true;
 958            if (o == null || getClass() != o.getClass()) return false;
 959            PersistableSessionId that = (PersistableSessionId) o;
 960            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
 961        }
 962
 963        @Override
 964        public int hashCode() {
 965            return Objects.hashCode(with, sessionId);
 966        }
 967    }
 968
 969    public static class TerminatedRtpSession {
 970        public final RtpEndUserState state;
 971        public final Set<Media> media;
 972
 973        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
 974            this.state = state;
 975            this.media = media;
 976        }
 977    }
 978
 979    public enum DeviceDiscoveryState {
 980        SEARCHING,
 981        SEARCHING_ACKNOWLEDGED,
 982        DISCOVERED,
 983        FAILED;
 984
 985        public RtpEndUserState toEndUserState() {
 986            return switch (this) {
 987                case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
 988                case DISCOVERED -> RtpEndUserState.RINGING;
 989                default -> RtpEndUserState.CONNECTIVITY_ERROR;
 990            };
 991        }
 992    }
 993
 994    public static class RtpSessionProposal implements OngoingRtpSession {
 995        public final Jid with;
 996        public final String sessionId;
 997        public final Set<Media> media;
 998        private final Account account;
 999
1000        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
1001            this.account = account;
1002            this.with = with;
1003            this.sessionId = sessionId;
1004            this.media = media;
1005        }
1006
1007        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
1008            return new RtpSessionProposal(account, with, nextRandomId(), media);
1009        }
1010
1011        @Override
1012        public boolean equals(Object o) {
1013            if (this == o) return true;
1014            if (o == null || getClass() != o.getClass()) return false;
1015            RtpSessionProposal proposal = (RtpSessionProposal) o;
1016            return Objects.equal(account.getJid(), proposal.account.getJid())
1017                    && Objects.equal(with, proposal.with)
1018                    && Objects.equal(sessionId, proposal.sessionId);
1019        }
1020
1021        @Override
1022        public int hashCode() {
1023            return Objects.hashCode(account.getJid(), with, sessionId);
1024        }
1025
1026        @Override
1027        public Account getAccount() {
1028            return account;
1029        }
1030
1031        @Override
1032        public Jid getWith() {
1033            return with;
1034        }
1035
1036        @Override
1037        public String getSessionId() {
1038            return sessionId;
1039        }
1040    }
1041}