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 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(jingleRtpConnection);
 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 Optional<RtpSessionProposal> matchingProposal(final Account account, final Jid with) {
 769        synchronized (this.rtpSessionProposals) {
 770            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 771                    this.rtpSessionProposals.entrySet()) {
 772                final RtpSessionProposal proposal = entry.getKey();
 773                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 774                    return Optional.of(proposal);
 775                }
 776            }
 777        }
 778        return Optional.absent();
 779    }
 780
 781    public boolean hasMatchingProposal(final Account account, final Jid with) {
 782        synchronized (this.rtpSessionProposals) {
 783            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 784                    this.rtpSessionProposals.entrySet()) {
 785                final var state = entry.getValue();
 786                final RtpSessionProposal proposal = entry.getKey();
 787                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
 788                    // CallIntegrationConnectionService starts RtpSessionActivity with ACTION_VIEW
 789                    // and an EXTRA_LAST_REPORTED_STATE of DISCOVERING devices. however due to
 790                    // possible race conditions the state might have already moved on so we are
 791                    // going
 792                    // to update the UI
 793                    final RtpEndUserState endUserState = state.toEndUserState();
 794                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 795                            account, proposal.with, proposal.sessionId, endUserState);
 796                    return true;
 797                }
 798            }
 799        }
 800        return false;
 801    }
 802
 803    public void deliverIbbPacket(final Account account, final IqPacket packet) {
 804        final String sid;
 805        final Element payload;
 806        final InbandBytestreamsTransport.PacketType packetType;
 807        if (packet.hasChild("open", Namespace.IBB)) {
 808            packetType = InbandBytestreamsTransport.PacketType.OPEN;
 809            payload = packet.findChild("open", Namespace.IBB);
 810            sid = payload.getAttribute("sid");
 811        } else if (packet.hasChild("data", Namespace.IBB)) {
 812            packetType = InbandBytestreamsTransport.PacketType.DATA;
 813            payload = packet.findChild("data", Namespace.IBB);
 814            sid = payload.getAttribute("sid");
 815        } else if (packet.hasChild("close", Namespace.IBB)) {
 816            packetType = InbandBytestreamsTransport.PacketType.CLOSE;
 817            payload = packet.findChild("close", Namespace.IBB);
 818            sid = payload.getAttribute("sid");
 819        } else {
 820            packetType = null;
 821            payload = null;
 822            sid = null;
 823        }
 824        if (sid == null) {
 825            Log.d(
 826                    Config.LOGTAG,
 827                    account.getJid().asBareJid() + ": unable to deliver ibb packet. missing sid");
 828            account.getXmppConnection()
 829                    .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 830            return;
 831        }
 832        for (final AbstractJingleConnection connection : this.connections.values()) {
 833            if (connection instanceof JingleFileTransferConnection fileTransfer) {
 834                final Transport transport = fileTransfer.getTransport();
 835                if (transport instanceof InbandBytestreamsTransport inBandTransport) {
 836                    if (sid.equals(inBandTransport.getStreamId())) {
 837                        if (inBandTransport.deliverPacket(packetType, packet.getFrom(), payload)) {
 838                            account.getXmppConnection()
 839                                    .sendIqPacket(
 840                                            packet.generateResponse(IqPacket.TYPE.RESULT), null);
 841                        } else {
 842                            account.getXmppConnection()
 843                                    .sendIqPacket(
 844                                            packet.generateResponse(IqPacket.TYPE.ERROR), null);
 845                        }
 846                        return;
 847                    }
 848                }
 849            }
 850        }
 851        Log.d(
 852                Config.LOGTAG,
 853                account.getJid().asBareJid() + ": unable to deliver ibb packet with sid=" + sid);
 854        account.getXmppConnection()
 855                .sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
 856    }
 857
 858    public void notifyRebound(final Account account) {
 859        for (final AbstractJingleConnection connection : this.connections.values()) {
 860            connection.notifyRebound();
 861        }
 862        final XmppConnection xmppConnection = account.getXmppConnection();
 863        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
 864            resendSessionProposals(account);
 865        }
 866    }
 867
 868    public WeakReference<JingleRtpConnection> findJingleRtpConnection(
 869            Account account, Jid with, String sessionId) {
 870        final AbstractJingleConnection.Id id =
 871                AbstractJingleConnection.Id.of(account, with, sessionId);
 872        final AbstractJingleConnection connection = connections.get(id);
 873        if (connection instanceof JingleRtpConnection) {
 874            return new WeakReference<>((JingleRtpConnection) connection);
 875        }
 876        return null;
 877    }
 878
 879    public JingleRtpConnection findJingleRtpConnection(final Account account, final Jid with) {
 880        for (final AbstractJingleConnection connection : this.connections.values()) {
 881            if (connection instanceof JingleRtpConnection rtpConnection) {
 882                if (rtpConnection.isTerminated()) {
 883                    continue;
 884                }
 885                final var id = rtpConnection.getId();
 886                if (id.account == account && account.getJid().equals(with)) {
 887                    return rtpConnection;
 888                }
 889            }
 890        }
 891        return null;
 892    }
 893
 894    private void resendSessionProposals(final Account account) {
 895        synchronized (this.rtpSessionProposals) {
 896            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
 897                    this.rtpSessionProposals.entrySet()) {
 898                final RtpSessionProposal proposal = entry.getKey();
 899                if (entry.getValue() == DeviceDiscoveryState.SEARCHING
 900                        && proposal.account == account) {
 901                    Log.d(
 902                            Config.LOGTAG,
 903                            account.getJid().asBareJid()
 904                                    + ": resending session proposal to "
 905                                    + proposal.with);
 906                    final MessagePacket messagePacket =
 907                            mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
 908                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
 909                }
 910            }
 911        }
 912    }
 913
 914    public void updateProposedSessionDiscovered(
 915            Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
 916        synchronized (this.rtpSessionProposals) {
 917            final RtpSessionProposal sessionProposal =
 918                    getRtpSessionProposal(account, from.asBareJid(), sessionId);
 919            final DeviceDiscoveryState currentState =
 920                    sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
 921            if (currentState == null) {
 922                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
 923                return;
 924            }
 925            if (currentState == DeviceDiscoveryState.DISCOVERED) {
 926                Log.d(
 927                        Config.LOGTAG,
 928                        "session proposal already at discovered. not going to fall back");
 929                return;
 930            }
 931            this.rtpSessionProposals.put(sessionProposal, target);
 932            final RtpEndUserState endUserState = target.toEndUserState();
 933            if (endUserState == RtpEndUserState.RINGING) {
 934                sessionProposal.callIntegration.setDialing();
 935            }
 936            // toneManager.transition(endUserState, sessionProposal.media);
 937            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
 938                    account, sessionProposal.with, sessionProposal.sessionId, endUserState);
 939            Log.d(
 940                    Config.LOGTAG,
 941                    account.getJid().asBareJid()
 942                            + ": flagging session "
 943                            + sessionId
 944                            + " as "
 945                            + target);
 946        }
 947    }
 948
 949    public void rejectRtpSession(final String sessionId) {
 950        for (final AbstractJingleConnection connection : this.connections.values()) {
 951            if (connection.getId().sessionId.equals(sessionId)) {
 952                if (connection instanceof JingleRtpConnection) {
 953                    try {
 954                        ((JingleRtpConnection) connection).rejectCall();
 955                        return;
 956                    } catch (final IllegalStateException e) {
 957                        Log.w(
 958                                Config.LOGTAG,
 959                                "race condition on rejecting call from notification",
 960                                e);
 961                    }
 962                }
 963            }
 964        }
 965    }
 966
 967    public void endRtpSession(final String sessionId) {
 968        for (final AbstractJingleConnection connection : this.connections.values()) {
 969            if (connection.getId().sessionId.equals(sessionId)) {
 970                if (connection instanceof JingleRtpConnection) {
 971                    ((JingleRtpConnection) connection).endCall();
 972                }
 973            }
 974        }
 975    }
 976
 977    public void failProceed(
 978            Account account, final Jid with, final String sessionId, final String message) {
 979        final AbstractJingleConnection.Id id =
 980                AbstractJingleConnection.Id.of(account, with, sessionId);
 981        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 982        if (existingJingleConnection instanceof JingleRtpConnection) {
 983            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed(message);
 984        }
 985    }
 986
 987    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
 988        if (connections.containsValue(connection)) {
 989            return;
 990        }
 991        final IllegalStateException e =
 992                new IllegalStateException(
 993                        "JingleConnection has not been registered with connection manager");
 994        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
 995        throw e;
 996    }
 997
 998    void setTerminalSessionState(
 999            AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
1000        this.terminatedSessions.put(
1001                PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
1002    }
1003
1004    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
1005        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
1006    }
1007
1008    private static class PersistableSessionId {
1009        private final Jid with;
1010        private final String sessionId;
1011
1012        private PersistableSessionId(Jid with, String sessionId) {
1013            this.with = with;
1014            this.sessionId = sessionId;
1015        }
1016
1017        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
1018            return new PersistableSessionId(id.with, id.sessionId);
1019        }
1020
1021        @Override
1022        public boolean equals(Object o) {
1023            if (this == o) return true;
1024            if (o == null || getClass() != o.getClass()) return false;
1025            PersistableSessionId that = (PersistableSessionId) o;
1026            return Objects.equal(with, that.with) && Objects.equal(sessionId, that.sessionId);
1027        }
1028
1029        @Override
1030        public int hashCode() {
1031            return Objects.hashCode(with, sessionId);
1032        }
1033    }
1034
1035    public static class TerminatedRtpSession {
1036        public final RtpEndUserState state;
1037        public final Set<Media> media;
1038
1039        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
1040            this.state = state;
1041            this.media = media;
1042        }
1043    }
1044
1045    public enum DeviceDiscoveryState {
1046        SEARCHING,
1047        SEARCHING_ACKNOWLEDGED,
1048        DISCOVERED,
1049        FAILED;
1050
1051        public RtpEndUserState toEndUserState() {
1052            return switch (this) {
1053                case SEARCHING, SEARCHING_ACKNOWLEDGED -> RtpEndUserState.FINDING_DEVICE;
1054                case DISCOVERED -> RtpEndUserState.RINGING;
1055                default -> RtpEndUserState.CONNECTIVITY_ERROR;
1056            };
1057        }
1058    }
1059
1060    public static class RtpSessionProposal implements OngoingRtpSession {
1061        public final Jid with;
1062        public final String sessionId;
1063        public final Set<Media> media;
1064        private final Account account;
1065        private final CallIntegration callIntegration;
1066
1067        private RtpSessionProposal(
1068                Account account,
1069                Jid with,
1070                String sessionId,
1071                Set<Media> media,
1072                final CallIntegration callIntegration) {
1073            this.account = account;
1074            this.with = with;
1075            this.sessionId = sessionId;
1076            this.media = media;
1077            this.callIntegration = callIntegration;
1078        }
1079
1080        public static RtpSessionProposal of(
1081                Account account,
1082                Jid with,
1083                Set<Media> media,
1084                final CallIntegration callIntegration) {
1085            return new RtpSessionProposal(account, with, nextRandomId(), media, callIntegration);
1086        }
1087
1088        @Override
1089        public boolean equals(Object o) {
1090            if (this == o) return true;
1091            if (o == null || getClass() != o.getClass()) return false;
1092            RtpSessionProposal proposal = (RtpSessionProposal) o;
1093            return Objects.equal(account.getJid(), proposal.account.getJid())
1094                    && Objects.equal(with, proposal.with)
1095                    && Objects.equal(sessionId, proposal.sessionId);
1096        }
1097
1098        @Override
1099        public int hashCode() {
1100            return Objects.hashCode(account.getJid(), with, sessionId);
1101        }
1102
1103        @Override
1104        public Account getAccount() {
1105            return account;
1106        }
1107
1108        @Override
1109        public Jid getWith() {
1110            return with;
1111        }
1112
1113        @Override
1114        public String getSessionId() {
1115            return sessionId;
1116        }
1117
1118        @Override
1119        public CallIntegration getCallIntegration() {
1120            return this.callIntegration;
1121        }
1122
1123        @Override
1124        public Set<Media> getMedia() {
1125            return this.media;
1126        }
1127    }
1128
1129    public class ProposalStateCallback implements CallIntegration.Callback {
1130
1131        private final RtpSessionProposal proposal;
1132
1133        public ProposalStateCallback(final RtpSessionProposal proposal) {
1134            this.proposal = proposal;
1135        }
1136
1137        @Override
1138        public void onCallIntegrationShowIncomingCallUi() {}
1139
1140        @Override
1141        public void onCallIntegrationDisconnect() {
1142            Log.d(Config.LOGTAG, "a phone call has just been started. retracting proposal");
1143            retractSessionProposal(this.proposal);
1144        }
1145
1146        @Override
1147        public void onAudioDeviceChanged(
1148                final CallIntegration.AudioDevice selectedAudioDevice,
1149                final Set<CallIntegration.AudioDevice> availableAudioDevices) {
1150            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
1151                    selectedAudioDevice, availableAudioDevices);
1152        }
1153
1154        @Override
1155        public void onCallIntegrationReject() {}
1156
1157        @Override
1158        public void onCallIntegrationAnswer() {}
1159
1160        @Override
1161        public void onCallIntegrationSilence() {
1162
1163        }
1164    }
1165}