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);
 663            }
 664        }
 665    }
 666
 667    private void retractSessionProposal(final RtpSessionProposal rtpSessionProposal) {
 668        final Account account = rtpSessionProposal.account;
 669        Log.d(
 670                Config.LOGTAG,
 671                account.getJid().asBareJid()
 672                        + ": retracting rtp session proposal with "
 673                        + rtpSessionProposal.with);
 674        this.rtpSessionProposals.remove(rtpSessionProposal);
 675        rtpSessionProposal.callIntegration.retracted();
 676        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 677                account,
 678                rtpSessionProposal.with,
 679                rtpSessionProposal.sessionId,
 680                RtpEndUserState.RETRACTED);
 681        final MessagePacket messagePacket =
 682                mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
 683        writeLogMissedOutgoing(
 684                account,
 685                rtpSessionProposal.with,
 686                rtpSessionProposal.sessionId,
 687                null,
 688                System.currentTimeMillis());
 689        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 690    }
 691
 692    public JingleRtpConnection initializeRtpSession(
 693            final Account account, final Jid with, final Set<Media> media) {
 694        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
 695        final JingleRtpConnection rtpConnection =
 696                new JingleRtpConnection(this, id, account.getJid());
 697        rtpConnection.setProposedMedia(media);
 698        this.connections.put(id, rtpConnection);
 699        rtpConnection.sendSessionInitiate();
 700        return rtpConnection;
 701    }
 702
 703    public RtpSessionProposal proposeJingleRtpSession(
 704            final Account account, final Jid with, final Set<Media> media) {
 705        synchronized (this.rtpSessionProposals) {
 706            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 707                    this.rtpSessionProposals.entrySet()) {
 708                final 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                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 715                                account, with, proposal.sessionId, endUserState);
 716                        return proposal;
 717                    }
 718                }
 719            }
 720            if (isBusy()) {
 721                if (hasMatchingRtpSession(account, with, media)) {
 722                    Log.d(
 723                            Config.LOGTAG,
 724                            "ignoring request to propose jingle session because the other party already created one for us");
 725                    // TODO return something that we can parse the connection of of
 726                    return null;
 727                }
 728                throw new IllegalStateException(
 729                        "There is already a running RTP session. This should have been caught by the UI");
 730            }
 731            final CallIntegration callIntegration =
 732                    new CallIntegration(mXmppConnectionService.getApplicationContext());
 733            callIntegration.setVideoState(
 734                    Media.audioOnly(media)
 735                            ? VideoProfile.STATE_AUDIO_ONLY
 736                            : VideoProfile.STATE_BIDIRECTIONAL);
 737            callIntegration.setInitialAudioDevice(CallIntegration.initialAudioDevice(media));
 738            final RtpSessionProposal proposal =
 739                    RtpSessionProposal.of(account, with.asBareJid(), media, callIntegration);
 740            callIntegration.setCallback(new ProposalStateCallback(proposal));
 741            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
 742            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 743                    account, proposal.with, proposal.sessionId, RtpEndUserState.FINDING_DEVICE);
 744            final MessagePacket messagePacket =
 745                    mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 746            mXmppConnectionService.sendMessagePacket(account, messagePacket);
 747            return proposal;
 748        }
 749    }
 750
 751    public void sendJingleMessageFinish(
 752            final Contact contact, final String sessionId, final Reason reason) {
 753        final var account = contact.getAccount();
 754        final MessagePacket messagePacket =
 755                mXmppConnectionService
 756                        .getMessageGenerator()
 757                        .sessionFinish(contact.getJid(), sessionId, reason);
 758        mXmppConnectionService.sendMessagePacket(account, messagePacket);
 759    }
 760
 761    public boolean hasMatchingProposal(final Account account, final Jid with) {
 762        synchronized (this.rtpSessionProposals) {
 763            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 764                    this.rtpSessionProposals.entrySet()) {
 765                final var state = entry.getValue();
 766                final RtpSessionProposal proposal = entry.getKey();
 767                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 768                    // CallIntegrationConnectionService starts RtpSessionActivity with ACTION_VIEW
 769                    // and an EXTRA_LAST_REPORTED_STATE of DISCOVERING devices. however due to
 770                    // possible race conditions the state might have already moved on so we are
 771                    // going
 772                    // to update the UI
 773                    final RtpEndUserState endUserState = state.toEndUserState();
 774                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 775                            account, proposal.with, proposal.sessionId, endUserState);
 776                    return true;
 777                }
 778            }
 779        }
 780        return false;
 781    }
 782
 783    public void deliverIbbPacket(final Account account, final IqPacket packet) {
 784        final String sid;
 785        final Element payload;
 786        final InbandBytestreamsTransport.PacketType packetType;
 787        if (packet.hasChild("open", Namespace.IBB)) {
 788            packetType = InbandBytestreamsTransport.PacketType.OPEN;
 789            payload = packet.findChild("open", Namespace.IBB);
 790            sid = payload.getAttribute("sid");
 791        } else if (packet.hasChild("data", Namespace.IBB)) {
 792            packetType = InbandBytestreamsTransport.PacketType.DATA;
 793            payload = packet.findChild("data", Namespace.IBB);
 794            sid = payload.getAttribute("sid");
 795        } else if (packet.hasChild("close", Namespace.IBB)) {
 796            packetType = InbandBytestreamsTransport.PacketType.CLOSE;
 797            payload = packet.findChild("close", Namespace.IBB);
 798            sid = payload.getAttribute("sid");
 799        } else {
 800            packetType = null;
 801            payload = null;
 802            sid = null;
 803        }
 804        if (sid == null) {
 805            Log.d(
 806                    Config.LOGTAG,
 807                    account.getJid().asBareJid() + ": unable to deliver ibb packet. missing sid");
 808            account.getXmppConnection()
 809                    .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 810            return;
 811        }
 812        for (final AbstractJingleConnection connection : this.connections.values()) {
 813            if (connection instanceof JingleFileTransferConnection fileTransfer) {
 814                final Transport transport = fileTransfer.getTransport();
 815                if (transport instanceof InbandBytestreamsTransport inBandTransport) {
 816                    if (sid.equals(inBandTransport.getStreamId())) {
 817                        if (inBandTransport.deliverPacket(packetType, packet.getFrom(), payload)) {
 818                            account.getXmppConnection()
 819                                    .sendIqPacket(
 820                                            packet.generateResponse(IqPacket.TYPE.RESULT), null);
 821                        } else {
 822                            account.getXmppConnection()
 823                                    .sendIqPacket(
 824                                            packet.generateResponse(IqPacket.TYPE.ERROR), null);
 825                        }
 826                        return;
 827                    }
 828                }
 829            }
 830        }
 831        Log.d(
 832                Config.LOGTAG,
 833                account.getJid().asBareJid() + ": unable to deliver ibb packet with sid=" + sid);
 834        account.getXmppConnection()
 835                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 836    }
 837
 838    public void notifyRebound(final Account account) {
 839        for (final AbstractJingleConnection connection : this.connections.values()) {
 840            connection.notifyRebound();
 841        }
 842        final XmppConnection xmppConnection = account.getXmppConnection();
 843        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 844            resendSessionProposals(account);
 845        }
 846    }
 847
 848    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 849            Account account, Jid with, String sessionId) {
 850        final AbstractJingleConnection.Id id =
 851                AbstractJingleConnection.Id.of(account, with, sessionId);
 852        final AbstractJingleConnection connection = connections.get(id);
 853        if (connection instanceof JingleRtpConnection) {
 854            return new WeakReference<>((JingleRtpConnection) connection);
 855        }
 856        return null;
 857    }
 858
 859    public JingleRtpConnection findJingleRtpConnection(final Account account, final Jid with) {
 860        for (final AbstractJingleConnection connection : this.connections.values()) {
 861            if (connection instanceof JingleRtpConnection rtpConnection) {
 862                if (rtpConnection.isTerminated()) {
 863                    continue;
 864                }
 865                final var id = rtpConnection.getId();
 866                if (id.account == account && account.getJid().equals(with)) {
 867                    return rtpConnection;
 868                }
 869            }
 870        }
 871        return null;
 872    }
 873
 874    private void resendSessionProposals(final Account account) {
 875        synchronized (this.rtpSessionProposals) {
 876            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 877                    this.rtpSessionProposals.entrySet()) {
 878                final RtpSessionProposal proposal = entry.getKey();
 879                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 880                        && proposal.account == account) {
 881                    Log.d(
 882                            Config.LOGTAG,
 883                            account.getJid().asBareJid()
 884                                    + ": resending session proposal to "
 885                                    + proposal.with);
 886                    final MessagePacket messagePacket =
 887                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 888                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 889                }
 890            }
 891        }
 892    }
 893
 894    public void updateProposedSessionDiscovered(
 895            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 896        synchronized (this.rtpSessionProposals) {
 897            final RtpSessionProposal sessionProposal =
 898                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 899            final DeviceDiscoveryState currentState =
 900                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 901            if (currentState == null) {
 902                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 903                return;
 904            }
 905            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 906                Log.d(
 907                        Config.LOGTAG,
 908                        "session proposal already at discovered. not going to fall back");
 909                return;
 910            }
 911            this.rtpSessionProposals.put(sessionProposal, target);
 912            final RtpEndUserState endUserState = target.toEndUserState();
 913            if (endUserState == RtpEndUserState.RINGING) {
 914                sessionProposal.callIntegration.setDialing();
 915            }
 916            // toneManager.transition(endUserState, sessionProposal.media);
 917            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 918                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 919            Log.d(
 920                    Config.LOGTAG,
 921                    account.getJid().asBareJid()
 922                            + ": flagging session "
 923                            + sessionId
 924                            + " as "
 925                            + target);
 926        }
 927    }
 928
 929    public void rejectRtpSession(final String sessionId) {
 930        for (final AbstractJingleConnection connection : this.connections.values()) {
 931            if (connection.getId().sessionId.equals(sessionId)) {
 932                if (connection instanceof JingleRtpConnection) {
 933                    try {
 934                        ((JingleRtpConnection) connection).rejectCall();
 935                        return;
 936                    } catch (final IllegalStateException e) {
 937                        Log.w(
 938                                Config.LOGTAG,
 939                                "race condition on rejecting call from notification",
 940                                e);
 941                    }
 942                }
 943            }
 944        }
 945    }
 946
 947    public void endRtpSession(final String sessionId) {
 948        for (final AbstractJingleConnection connection : this.connections.values()) {
 949            if (connection.getId().sessionId.equals(sessionId)) {
 950                if (connection instanceof JingleRtpConnection) {
 951                    ((JingleRtpConnection) connection).endCall();
 952                }
 953            }
 954        }
 955    }
 956
 957    public void failProceed(
 958            Account account, final Jid with, final String sessionId, final String message) {
 959        final AbstractJingleConnection.Id id =
 960                AbstractJingleConnection.Id.of(account, with, sessionId);
 961        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 962        if (existingJingleConnection instanceof JingleRtpConnection) {
 963            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 964        }
 965    }
 966
 967    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 968        if (connections.containsValue(connection)) {
 969            return;
 970        }
 971        final IllegalStateException e =
 972                new IllegalStateException(
 973                        "JingleConnection has not been registered with connection manager");
 974        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 975        throw e;
 976    }
 977
 978    void setTerminalSessionState(
 979            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
 980        this.terminatedSessions.put(
 981                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
 982    }
 983
 984    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
 985        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
 986    }
 987
 988    private static class PersistableSessionId {
 989        private final Jid with;
 990        private final String sessionId;
 991
 992        private PersistableSessionId(Jid with, String sessionId) {
 993            this.with = with;
 994            this.sessionId = sessionId;
 995        }
 996
 997        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
 998            return new PersistableSessionId(id.with, id.sessionId);
 999        }
1000
1001        @Override
1002        public boolean equals(Object o) {
1003            if (this == o) return true;
1004            if (o == null || getClass() != o.getClass()) return false;
1005            PersistableSessionId that = (PersistableSessionId) o;
1006            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
1007        }
1008
1009        @Override
1010        public int hashCode() {
1011            return Objects.hashCode(with, sessionId);
1012        }
1013    }
1014
1015    public static class TerminatedRtpSession {
1016        public final RtpEndUserState state;
1017        public final Set<Media> media;
1018
1019        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
1020            this.state = state;
1021            this.media = media;
1022        }
1023    }
1024
1025    public enum DeviceDiscoveryState {
1026        SEARCHING,
1027        SEARCHING_ACKNOWLEDGED,
1028        DISCOVERED,
1029        FAILED;
1030
1031        public RtpEndUserState toEndUserState() {
1032            return switch (this) {
1033                case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
1034                case DISCOVERED -> RtpEndUserState.RINGING;
1035                default -> RtpEndUserState.CONNECTIVITY_ERROR;
1036            };
1037        }
1038    }
1039
1040    public static class RtpSessionProposal implements OngoingRtpSession {
1041        public final Jid with;
1042        public final String sessionId;
1043        public final Set<Media> media;
1044        private final Account account;
1045        private final CallIntegration callIntegration;
1046
1047        private RtpSessionProposal(
1048                Account account,
1049                Jid with,
1050                String sessionId,
1051                Set<Media> media,
1052                final CallIntegration callIntegration) {
1053            this.account = account;
1054            this.with = with;
1055            this.sessionId = sessionId;
1056            this.media = media;
1057            this.callIntegration = callIntegration;
1058        }
1059
1060        public static RtpSessionProposal of(
1061                Account account,
1062                Jid with,
1063                Set<Media> media,
1064                final CallIntegration callIntegration) {
1065            return new RtpSessionProposal(account, with, nextRandomId(), media, callIntegration);
1066        }
1067
1068        @Override
1069        public boolean equals(Object o) {
1070            if (this == o) return true;
1071            if (o == null || getClass() != o.getClass()) return false;
1072            RtpSessionProposal proposal = (RtpSessionProposal) o;
1073            return Objects.equal(account.getJid(), proposal.account.getJid())
1074                    && Objects.equal(with, proposal.with)
1075                    && Objects.equal(sessionId, proposal.sessionId);
1076        }
1077
1078        @Override
1079        public int hashCode() {
1080            return Objects.hashCode(account.getJid(), with, sessionId);
1081        }
1082
1083        @Override
1084        public Account getAccount() {
1085            return account;
1086        }
1087
1088        @Override
1089        public Jid getWith() {
1090            return with;
1091        }
1092
1093        @Override
1094        public String getSessionId() {
1095            return sessionId;
1096        }
1097
1098        public CallIntegration getCallIntegration() {
1099            return this.callIntegration;
1100        }
1101    }
1102
1103    public class ProposalStateCallback implements CallIntegration.Callback {
1104
1105        private final RtpSessionProposal proposal;
1106
1107        public ProposalStateCallback(final RtpSessionProposal proposal) {
1108            this.proposal = proposal;
1109        }
1110
1111        @Override
1112        public void onCallIntegrationShowIncomingCallUi() {}
1113
1114        @Override
1115        public void onCallIntegrationDisconnect() {
1116            Log.d(Config.LOGTAG, "a phone call has just been started. retracting proposal");
1117            retractSessionProposal(this.proposal);
1118        }
1119
1120        @Override
1121        public void onAudioDeviceChanged(
1122                CallIntegration.AudioDevice selectedAudioDevice,
1123                Set<CallIntegration.AudioDevice> availableAudioDevices) {}
1124
1125        @Override
1126        public void onCallIntegrationReject() {}
1127
1128        @Override
1129        public void onCallIntegrationAnswer() {}
1130    }
1131}