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