JingleConnectionManager.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import android.util.Base64;
  4import android.util.Log;
  5
  6import com.google.common.base.Objects;
  7import com.google.common.base.Optional;
  8import com.google.common.base.Preconditions;
  9import com.google.common.cache.Cache;
 10import com.google.common.cache.CacheBuilder;
 11import com.google.common.collect.Collections2;
 12import com.google.common.collect.ComparisonChain;
 13import com.google.common.collect.ImmutableSet;
 14
 15import java.lang.ref.WeakReference;
 16import java.security.SecureRandom;
 17import java.util.Collection;
 18import java.util.Collections;
 19import java.util.HashMap;
 20import java.util.List;
 21import java.util.Map;
 22import java.util.Set;
 23import java.util.concurrent.ConcurrentHashMap;
 24import java.util.concurrent.Executors;
 25import java.util.concurrent.ScheduledExecutorService;
 26import java.util.concurrent.ScheduledFuture;
 27import java.util.concurrent.TimeUnit;
 28
 29import eu.siacs.conversations.Config;
 30import eu.siacs.conversations.entities.Account;
 31import eu.siacs.conversations.entities.Contact;
 32import eu.siacs.conversations.entities.Conversation;
 33import eu.siacs.conversations.entities.Conversational;
 34import eu.siacs.conversations.entities.Message;
 35import eu.siacs.conversations.entities.RtpSessionStatus;
 36import eu.siacs.conversations.entities.Transferable;
 37import eu.siacs.conversations.services.AbstractConnectionManager;
 38import eu.siacs.conversations.services.XmppConnectionService;
 39import eu.siacs.conversations.xml.Element;
 40import eu.siacs.conversations.xml.Namespace;
 41import eu.siacs.conversations.xmpp.Jid;
 42import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 43import eu.siacs.conversations.xmpp.XmppConnection;
 44import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
 45import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
 46import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
 47import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
 48import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
 49import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
 50import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
 51import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 52import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 53
 54public class JingleConnectionManager extends AbstractConnectionManager {
 55    static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors.newSingleThreadScheduledExecutor();
 56    final ToneManager toneManager;
 57    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals = new HashMap<>();
 58    private final ConcurrentHashMap<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
 59
 60    private final Cache<PersistableSessionId, TerminatedRtpSession> terminatedSessions = CacheBuilder.newBuilder()
 61            .expireAfterWrite(24, TimeUnit.HOURS)
 62            .build();
 63
 64    private final HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
 65
 66    public JingleConnectionManager(XmppConnectionService service) {
 67        super(service);
 68        this.toneManager = new ToneManager(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);
 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 = content == null ? null : content.getDescriptionNamespace();
 91            final AbstractJingleConnection connection;
 92            if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
 93                connection = new JingleFileTransferConnection(this, id, from);
 94            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace) && isUsingClearNet(account)) {
 95                final boolean sessionEnded = this.terminatedSessions.asMap().containsKey(PersistableSessionId.of(id));
 96                final boolean stranger = isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
 97                if (isBusy() != null || sessionEnded || stranger) {
 98                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": rejected session with " + id.with + " because busy. sessionEnded=" + sessionEnded + ", stranger=" + stranger);
 99                    mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
100                    final JinglePacket sessionTermination = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
101                    sessionTermination.setTo(id.with);
102                    sessionTermination.setReason(Reason.BUSY, null);
103                    mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
104                    return;
105                }
106                connection = new JingleRtpConnection(this, id, from);
107            } else {
108                respondWithJingleError(account, packet, "unsupported-info", "feature-not-implemented", "cancel");
109                return;
110            }
111            connections.put(id, connection);
112            mXmppConnectionService.updateConversationUi();
113            connection.deliverPacket(packet);
114        } else {
115            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
116            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
117        }
118    }
119
120    private boolean isUsingClearNet(final Account account) {
121        return !account.isOnion() && !mXmppConnectionService.useTorToConnect();
122    }
123
124    public String isBusy() {
125        if (mXmppConnectionService.isPhoneInCall()) {
126            return "isPhoneInCall";
127        }
128        for (AbstractJingleConnection connection : this.connections.values()) {
129            if (connection instanceof JingleRtpConnection) {
130                if (((JingleRtpConnection) connection).isTerminated()) {
131                    continue;
132                }
133                return "connection !isTerminated";
134            }
135        }
136        synchronized (this.rtpSessionProposals) {
137            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)) return "discovered";
138            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)) return "searching";
139            if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)) return "searching_acknolwedged";
140            return null;
141        }
142    }
143
144    public void notifyPhoneCallStarted() {
145        for (AbstractJingleConnection connection : connections.values()) {
146            if (connection instanceof JingleRtpConnection) {
147                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
148                if (rtpConnection.isTerminated()) {
149                    continue;
150                }
151                rtpConnection.notifyPhoneCall();
152            }
153        }
154    }
155
156    private Optional<RtpSessionProposal> findMatchingSessionProposal(final Account account, final Jid with, final Set<Media> media) {
157        synchronized (this.rtpSessionProposals) {
158            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
159                final RtpSessionProposal proposal = entry.getKey();
160                final DeviceDiscoveryState state = entry.getValue();
161                final boolean openProposal = state == DeviceDiscoveryState.DISCOVERED
162                        || state == DeviceDiscoveryState.SEARCHING
163                        || state == DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED;
164                if (openProposal
165                        && proposal.account == account
166                        && proposal.with.equals(with.asBareJid())
167                        && proposal.media.equals(media)) {
168                    return Optional.of(proposal);
169                }
170            }
171        }
172        return Optional.absent();
173    }
174
175    private String hasMatchingRtpSession(final Account account, final Jid with, final Set<Media> media) {
176        for (AbstractJingleConnection connection : this.connections.values()) {
177            if (connection instanceof JingleRtpConnection) {
178                final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
179                if (rtpConnection.isTerminated()) {
180                    continue;
181                }
182                if (rtpConnection.getId().account == account
183                        && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
184                        && rtpConnection.getMedia().equals(media)) {
185                    return rtpConnection.getId().sessionId;
186                }
187            }
188        }
189        return null;
190    }
191
192    private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
193        final boolean notifyForStrangers = mXmppConnectionService.getNotificationService().notificationsFromStrangers();
194        if (notifyForStrangers) {
195            return false;
196        }
197        final Contact contact = account.getRoster().getContact(with);
198        return !contact.showInContactList();
199    }
200
201    ScheduledFuture<?> schedule(final Runnable runnable, final long delay, final TimeUnit timeUnit) {
202        return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
203    }
204
205    void respondWithJingleError(final Account account, final IqPacket original, String jingleCondition, String condition, String conditionType) {
206        final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
207        final Element error = response.addChild("error");
208        error.setAttribute("type", conditionType);
209        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
210        error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
211        account.getXmppConnection().sendIqPacket(response, null);
212    }
213
214    public void deliverMessage(final Account account, final Jid to, final Jid from, final Element message, String remoteMsgId, String serverMsgId, long timestamp) {
215        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
216        final String sessionId = message.getAttribute("id");
217        if (sessionId == null) {
218            return;
219        }
220        if ("accept".equals(message.getName())) {
221            for (AbstractJingleConnection connection : connections.values()) {
222                if (connection instanceof JingleRtpConnection) {
223                    final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
224                    final AbstractJingleConnection.Id id = connection.getId();
225                    if (id.account == account && id.sessionId.equals(sessionId)) {
226                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
227                        return;
228                    }
229                }
230            }
231            return;
232        }
233        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
234        final boolean addressedDirectly = to != null && to.equals(account.getJid());
235        final AbstractJingleConnection.Id id;
236        if (fromSelf) {
237            if (to != null && to.isFullJid()) {
238                id = AbstractJingleConnection.Id.of(account, to, sessionId);
239            } else {
240                return;
241            }
242        } else {
243            id = AbstractJingleConnection.Id.of(account, from, sessionId);
244        }
245        final AbstractJingleConnection existingJingleConnection = connections.get(id);
246        if (existingJingleConnection != null) {
247            if (existingJingleConnection instanceof JingleRtpConnection) {
248                ((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message, serverMsgId, timestamp);
249            } else {
250                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
251            }
252            return;
253        }
254
255        if (fromSelf) {
256            if ("proceed".equals(message.getName())) {
257                final Conversation c = mXmppConnectionService.findOrCreateConversation(account, id.with, false, false);
258                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
259                if (previousBusy != null) {
260                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
261                    if (serverMsgId != null) {
262                        previousBusy.setServerMsgId(serverMsgId);
263                    }
264                    previousBusy.setTime(timestamp);
265                    mXmppConnectionService.updateMessage(previousBusy, true);
266                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": updated previous busy because call got picked up by another device");
267                    return;
268                }
269            }
270            //TODO handle reject for cases where we don’t have carbon copies (normally reject is to be sent to own bare jid as well)
271            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignore jingle message from self");
272            return;
273        }
274
275        if ("propose".equals(message.getName())) {
276            final Propose propose = Propose.upgrade(message);
277            final List<GenericDescription> descriptions = propose.getDescriptions();
278            final Collection<RtpDescription> rtpDescriptions = Collections2.transform(
279                    Collections2.filter(descriptions, d -> d instanceof RtpDescription),
280                    input -> (RtpDescription) input
281            );
282            if (rtpDescriptions.size() > 0 && rtpDescriptions.size() == descriptions.size() && isUsingClearNet(account)) {
283                final Collection<Media> media = Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
284                if (media.contains(Media.UNKNOWN)) {
285                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered unknown media in session proposal. " + propose);
286                    return;
287                }
288                final Optional<RtpSessionProposal> matchingSessionProposal = findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
289                if (matchingSessionProposal.isPresent()) {
290                    final String ourSessionId = matchingSessionProposal.get().sessionId;
291                    final String theirSessionId = id.sessionId;
292                    if (ComparisonChain.start()
293                            .compare(ourSessionId, theirSessionId)
294                            .compare(account.getJid().toEscapedString(), id.with.toEscapedString())
295                            .result() > 0) {
296                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": our session lost tie break. automatically accepting their session. winning Session=" + theirSessionId);
297                        //TODO a retract for this reason should probably include some indication of tie break
298                        retractSessionProposal(matchingSessionProposal.get());
299                        final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
300                        this.connections.put(id, rtpConnection);
301                        rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
302                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
303                    } else {
304                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": our session won tie break. waiting for other party to accept. winningSession=" + ourSessionId);
305                    }
306                    return;
307                }
308                final boolean stranger = isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
309                if (isBusy() != null || stranger) {
310                    writeLogMissedIncoming(account, id.with.asBareJid(), id.sessionId, serverMsgId, timestamp);
311                    if (stranger) {
312                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring call proposal from stranger " + id.with);
313                        return;
314                    }
315                    final int activeDevices = account.activeDevicesWithRtpCapability();
316                    Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
317                    if (activeDevices == 0) {
318                        final MessagePacket reject = mXmppConnectionService.getMessageGenerator().sessionReject(from, sessionId);
319                        mXmppConnectionService.sendMessagePacket(account, reject);
320                    } else {
321                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring proposal because busy on this device but there are other devices");
322                    }
323                } else {
324                    final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
325                    this.connections.put(id, rtpConnection);
326                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
327                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
328                }
329            } else {
330                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed session with " + rtpDescriptions.size() + " rtp descriptions of " + descriptions.size() + " total descriptions");
331            }
332        } else if (addressedDirectly && "proceed".equals(message.getName())) {
333            synchronized (rtpSessionProposals) {
334                final RtpSessionProposal proposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
335                if (proposal != null) {
336                    rtpSessionProposals.remove(proposal);
337                    final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
338                    rtpConnection.setProposedMedia(proposal.media);
339                    this.connections.put(id, rtpConnection);
340                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
341                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
342                } else {
343                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver proceed");
344                    if (remoteMsgId == null) {
345                        return;
346                    }
347                    final MessagePacket errorMessage = new MessagePacket();
348                    errorMessage.setTo(from);
349                    errorMessage.setId(remoteMsgId);
350                    errorMessage.setType(MessagePacket.TYPE_ERROR);
351                    final Element error = errorMessage.addChild("error");
352                    error.setAttribute("code", "404");
353                    error.setAttribute("type", "cancel");
354                    error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
355                    mXmppConnectionService.sendMessagePacket(account, errorMessage);
356                }
357            }
358        } else if (addressedDirectly && "reject".equals(message.getName())) {
359            final RtpSessionProposal proposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
360            synchronized (rtpSessionProposals) {
361                if (proposal != null && rtpSessionProposals.remove(proposal) != null) {
362                    writeLogMissedOutgoing(account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
363                    toneManager.transition(RtpEndUserState.DECLINED_OR_BUSY, proposal.media);
364                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, proposal.with, proposal.sessionId, RtpEndUserState.DECLINED_OR_BUSY);
365                } else {
366                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver reject");
367                }
368            }
369        } else {
370            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message" + message);
371        }
372
373    }
374
375    private RtpSessionProposal getRtpSessionProposal(final Account account, Jid from, String sessionId) {
376        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
377            if (rtpSessionProposal.sessionId.equals(sessionId) && rtpSessionProposal.with.equals(from) && rtpSessionProposal.account.getJid().equals(account.getJid())) {
378                return rtpSessionProposal;
379            }
380        }
381        return null;
382    }
383
384    private void writeLogMissedOutgoing(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
385        final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
386                account,
387                with.asBareJid(),
388                false,
389                false
390        );
391        final Message message = new Message(
392                conversation,
393                Message.STATUS_SEND,
394                Message.TYPE_RTP_SESSION,
395                sessionId
396        );
397        message.setBody(new RtpSessionStatus(false, 0).toString());
398        message.setServerMsgId(serverMsgId);
399        message.setTime(timestamp);
400        writeMessage(message);
401    }
402
403    private void writeLogMissedIncoming(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
404        final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
405                account,
406                with.asBareJid(),
407                false,
408                false
409        );
410        final Message message = new Message(
411                conversation,
412                Message.STATUS_RECEIVED,
413                Message.TYPE_RTP_SESSION,
414                sessionId
415        );
416        message.setBody(new RtpSessionStatus(false, 0).toString());
417        message.setServerMsgId(serverMsgId);
418        message.setTime(timestamp);
419        writeMessage(message);
420    }
421
422    private void writeMessage(final Message message) {
423        final Conversational conversational = message.getConversation();
424        if (conversational instanceof Conversation) {
425            ((Conversation) conversational).add(message);
426            mXmppConnectionService.databaseBackend.createMessage(message);
427            mXmppConnectionService.updateConversationUi();
428        } else {
429            throw new IllegalStateException("Somehow the conversation in a message was a stub");
430        }
431    }
432
433    public void startJingleFileTransfer(final Message message) {
434        Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
435        final Transferable old = message.getTransferable();
436        if (old != null) {
437            old.cancel();
438        }
439        final Account account = message.getConversation().getAccount();
440        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
441        final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
442        mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
443        this.connections.put(id, connection);
444        connection.init(message);
445    }
446
447    public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
448        for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry : this.connections.entrySet()) {
449            if (entry.getValue() instanceof JingleRtpConnection) {
450                final AbstractJingleConnection.Id id = entry.getKey();
451                if (id.account == contact.getAccount() && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
452                    return Optional.of(id);
453                }
454            }
455        }
456        synchronized (this.rtpSessionProposals) {
457            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
458                RtpSessionProposal proposal = entry.getKey();
459                if (proposal.account == contact.getAccount() && contact.getJid().asBareJid().equals(proposal.with)) {
460                    final DeviceDiscoveryState preexistingState = entry.getValue();
461                    if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
462                        return Optional.of(proposal);
463                    }
464                }
465            }
466        }
467        return Optional.absent();
468    }
469
470    void finishConnection(final AbstractJingleConnection connection) {
471        this.connections.remove(connection.getId());
472    }
473
474    void finishConnectionOrThrow(final AbstractJingleConnection connection) {
475        final AbstractJingleConnection.Id id = connection.getId();
476        if (this.connections.remove(id) == null) {
477            throw new IllegalStateException(String.format("Unable to finish connection with id=%s", id.toString()));
478        }
479    }
480
481    public boolean fireJingleRtpConnectionStateUpdates() {
482        boolean firedUpdates = false;
483        for (final AbstractJingleConnection connection : this.connections.values()) {
484            if (connection instanceof JingleRtpConnection) {
485                final JingleRtpConnection jingleRtpConnection = (JingleRtpConnection) connection;
486                if (jingleRtpConnection.isTerminated()) {
487                    continue;
488                }
489                jingleRtpConnection.fireStateUpdate();
490                firedUpdates = true;
491            }
492        }
493        return firedUpdates;
494    }
495
496    void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
497        if (Config.DISABLE_PROXY_LOOKUP) {
498            listener.onPrimaryCandidateFound(false, null);
499            return;
500        }
501        if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
502            final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
503            if (proxy != null) {
504                IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
505                iq.setTo(proxy);
506                iq.query(Namespace.BYTE_STREAMS);
507                account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
508
509                    @Override
510                    public void onIqPacketReceived(Account account, IqPacket packet) {
511                        final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
512                        final String host = streamhost == null ? null : streamhost.getAttribute("host");
513                        final String port = streamhost == null ? null : streamhost.getAttribute("port");
514                        if (host != null && port != null) {
515                            try {
516                                JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
517                                candidate.setHost(host);
518                                candidate.setPort(Integer.parseInt(port));
519                                candidate.setType(JingleCandidate.TYPE_PROXY);
520                                candidate.setJid(proxy);
521                                candidate.setPriority(655360 + (initiator ? 30 : 0));
522                                primaryCandidates.put(account.getJid().asBareJid(), candidate);
523                                listener.onPrimaryCandidateFound(true, candidate);
524                            } catch (final NumberFormatException e) {
525                                listener.onPrimaryCandidateFound(false, null);
526                            }
527                        } else {
528                            listener.onPrimaryCandidateFound(false, null);
529                        }
530                    }
531                });
532            } else {
533                listener.onPrimaryCandidateFound(false, null);
534            }
535
536        } else {
537            listener.onPrimaryCandidateFound(true,
538                    this.primaryCandidates.get(account.getJid().asBareJid()));
539        }
540    }
541
542    public void retractSessionProposal(final Account account, final Jid with) {
543        synchronized (this.rtpSessionProposals) {
544            RtpSessionProposal matchingProposal = null;
545            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
546                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
547                    matchingProposal = proposal;
548                    break;
549                }
550            }
551            if (matchingProposal != null) {
552                retractSessionProposal(matchingProposal);
553            }
554        }
555    }
556
557    private void retractSessionProposal(RtpSessionProposal rtpSessionProposal) {
558        final Account account = rtpSessionProposal.account;
559        toneManager.transition(RtpEndUserState.ENDED, rtpSessionProposal.media);
560        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retracting rtp session proposal with " + rtpSessionProposal.with);
561        this.rtpSessionProposals.remove(rtpSessionProposal);
562        final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
563        writeLogMissedOutgoing(account, rtpSessionProposal.with, rtpSessionProposal.sessionId, null, System.currentTimeMillis());
564        mXmppConnectionService.sendMessagePacket(account, messagePacket);
565    }
566
567    public String initializeRtpSession(final Account account, final Jid with, final Set<Media> media) {
568        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
569        final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
570        rtpConnection.setProposedMedia(media);
571        this.connections.put(id, rtpConnection);
572        rtpConnection.sendSessionInitiate();
573        return id.sessionId;
574    }
575
576    public String proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
577        synchronized (this.rtpSessionProposals) {
578            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
579                RtpSessionProposal proposal = entry.getKey();
580                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
581                    final DeviceDiscoveryState preexistingState = entry.getValue();
582                    if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
583                        final RtpEndUserState endUserState = preexistingState.toEndUserState();
584                        toneManager.transition(endUserState, media);
585                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
586                                account,
587                                with,
588                                proposal.sessionId,
589                                endUserState
590                        );
591                        return proposal.sessionId;
592                    }
593                }
594            }
595            String busyCode = isBusy();
596            if (busyCode != null) {
597                String sessionId = hasMatchingRtpSession(account, with, media);
598                if (sessionId != null) {
599                    Log.d(Config.LOGTAG, "ignoring request to propose jingle session because the other party already created one for us: " + sessionId);
600                    return sessionId;
601                }
602                throw new IllegalStateException("There is already a running RTP session: " + busyCode);
603            }
604            final RtpSessionProposal proposal = RtpSessionProposal.of(account, with.asBareJid(), media);
605            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
606            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
607                    account,
608                    proposal.with,
609                    proposal.sessionId,
610                    RtpEndUserState.FINDING_DEVICE
611            );
612            final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
613            mXmppConnectionService.sendMessagePacket(account, messagePacket);
614            return proposal.sessionId;
615        }
616    }
617
618    public boolean hasMatchingProposal(final Account account, final Jid with) {
619        synchronized (this.rtpSessionProposals) {
620            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
621                final RtpSessionProposal proposal = entry.getKey();
622                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
623                    return true;
624                }
625            }
626        }
627        return false;
628    }
629
630    public void deliverIbbPacket(Account account, IqPacket packet) {
631        final String sid;
632        final Element payload;
633        if (packet.hasChild("open", Namespace.IBB)) {
634            payload = packet.findChild("open", Namespace.IBB);
635            sid = payload.getAttribute("sid");
636        } else if (packet.hasChild("data", Namespace.IBB)) {
637            payload = packet.findChild("data", Namespace.IBB);
638            sid = payload.getAttribute("sid");
639        } else if (packet.hasChild("close", Namespace.IBB)) {
640            payload = packet.findChild("close", Namespace.IBB);
641            sid = payload.getAttribute("sid");
642        } else {
643            payload = null;
644            sid = null;
645        }
646        if (sid != null) {
647            for (final AbstractJingleConnection connection : this.connections.values()) {
648                if (connection instanceof JingleFileTransferConnection) {
649                    final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
650                    final JingleTransport transport = fileTransfer.getTransport();
651                    if (transport instanceof JingleInBandTransport) {
652                        final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
653                        if (inBandTransport.matches(account, sid)) {
654                            inBandTransport.deliverPayload(packet, payload);
655                        }
656                        return;
657                    }
658                }
659            }
660        }
661        Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
662        account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
663    }
664
665    public void notifyRebound(final Account account) {
666        for (final AbstractJingleConnection connection : this.connections.values()) {
667            connection.notifyRebound();
668        }
669        final XmppConnection xmppConnection = account.getXmppConnection();
670        if (xmppConnection != null && xmppConnection.getFeatures().sm()) {
671            resendSessionProposals(account);
672        }
673    }
674
675    public WeakReference<JingleRtpConnection> findJingleRtpConnection(Account account, Jid with, String sessionId) {
676        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
677        final AbstractJingleConnection connection = connections.get(id);
678        if (connection instanceof JingleRtpConnection) {
679            return new WeakReference<>((JingleRtpConnection) connection);
680        }
681        return null;
682    }
683
684    private void resendSessionProposals(final Account account) {
685        synchronized (this.rtpSessionProposals) {
686            for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
687                final RtpSessionProposal proposal = entry.getKey();
688                if (entry.getValue() == DeviceDiscoveryState.SEARCHING && proposal.account == account) {
689                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": resending session proposal to " + proposal.with);
690                    final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
691                    mXmppConnectionService.sendMessagePacket(account, messagePacket);
692                }
693            }
694        }
695    }
696
697    public void updateProposedSessionDiscovered(Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
698        synchronized (this.rtpSessionProposals) {
699            final RtpSessionProposal sessionProposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
700            final DeviceDiscoveryState currentState = sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
701            if (currentState == null) {
702                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
703                return;
704            }
705            if (currentState == DeviceDiscoveryState.DISCOVERED) {
706                Log.d(Config.LOGTAG, "session proposal already at discovered. not going to fall back");
707                return;
708            }
709            this.rtpSessionProposals.put(sessionProposal, target);
710            final RtpEndUserState endUserState = target.toEndUserState();
711            toneManager.transition(endUserState, sessionProposal.media);
712            mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, sessionProposal.with, sessionProposal.sessionId, endUserState);
713            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": flagging session " + sessionId + " as " + target);
714        }
715    }
716
717    public void rejectRtpSession(final String sessionId) {
718        for (final AbstractJingleConnection connection : this.connections.values()) {
719            if (connection.getId().sessionId.equals(sessionId)) {
720                if (connection instanceof JingleRtpConnection) {
721                    ((JingleRtpConnection) connection).rejectCall();
722                }
723            }
724        }
725    }
726
727    public void endRtpSession(final String sessionId) {
728        for (final AbstractJingleConnection connection : this.connections.values()) {
729            if (connection.getId().sessionId.equals(sessionId)) {
730                if (connection instanceof JingleRtpConnection) {
731                    ((JingleRtpConnection) connection).endCall();
732                }
733            }
734        }
735    }
736
737    public void failProceed(Account account, final Jid with, String sessionId) {
738        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
739        final AbstractJingleConnection existingJingleConnection = connections.get(id);
740        if (existingJingleConnection instanceof JingleRtpConnection) {
741            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed();
742        }
743    }
744
745    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
746        if (connections.containsValue(connection)) {
747            return;
748        }
749        final IllegalStateException e = new IllegalStateException("JingleConnection has not been registered with connection manager");
750        Log.e(Config.LOGTAG, "ensureConnectionIsRegistered() failed. Going to throw", e);
751        throw e;
752    }
753
754    void setTerminalSessionState(AbstractJingleConnection.Id id, final RtpEndUserState state, final Set<Media> media) {
755        this.terminatedSessions.put(PersistableSessionId.of(id), new TerminatedRtpSession(state, media));
756    }
757
758    public TerminatedRtpSession getTerminalSessionState(final Jid with, final String sessionId) {
759        return this.terminatedSessions.getIfPresent(new PersistableSessionId(with, sessionId));
760    }
761
762    private static class PersistableSessionId {
763        private final Jid with;
764        private final String sessionId;
765
766        private PersistableSessionId(Jid with, String sessionId) {
767            this.with = with;
768            this.sessionId = sessionId;
769        }
770
771        public static PersistableSessionId of(AbstractJingleConnection.Id id) {
772            return new PersistableSessionId(id.with, id.sessionId);
773        }
774
775        @Override
776        public boolean equals(Object o) {
777            if (this == o) return true;
778            if (o == null || getClass() != o.getClass()) return false;
779            PersistableSessionId that = (PersistableSessionId) o;
780            return Objects.equal(with, that.with) &&
781                    Objects.equal(sessionId, that.sessionId);
782        }
783
784        @Override
785        public int hashCode() {
786            return Objects.hashCode(with, sessionId);
787        }
788    }
789
790    public static class TerminatedRtpSession {
791        public final RtpEndUserState state;
792        public final Set<Media> media;
793
794        TerminatedRtpSession(RtpEndUserState state, Set<Media> media) {
795            this.state = state;
796            this.media = media;
797        }
798    }
799
800    public enum DeviceDiscoveryState {
801        SEARCHING, SEARCHING_ACKNOWLEDGED, DISCOVERED, FAILED;
802
803        public RtpEndUserState toEndUserState() {
804            switch (this) {
805                case SEARCHING:
806                case SEARCHING_ACKNOWLEDGED:
807                    return RtpEndUserState.FINDING_DEVICE;
808                case DISCOVERED:
809                    return RtpEndUserState.RINGING;
810                default:
811                    return RtpEndUserState.CONNECTIVITY_ERROR;
812            }
813        }
814    }
815
816    public static class RtpSessionProposal implements OngoingRtpSession {
817        public final Jid with;
818        public final String sessionId;
819        public final Set<Media> media;
820        private final Account account;
821
822        private RtpSessionProposal(Account account, Jid with, String sessionId) {
823            this(account, with, sessionId, Collections.emptySet());
824        }
825
826        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
827            this.account = account;
828            this.with = with;
829            this.sessionId = sessionId;
830            this.media = media;
831        }
832
833        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
834            return new RtpSessionProposal(account, with, nextRandomId(), media);
835        }
836
837        @Override
838        public boolean equals(Object o) {
839            if (this == o) return true;
840            if (o == null || getClass() != o.getClass()) return false;
841            RtpSessionProposal proposal = (RtpSessionProposal) o;
842            return Objects.equal(account.getJid(), proposal.account.getJid()) &&
843                    Objects.equal(with, proposal.with) &&
844                    Objects.equal(sessionId, proposal.sessionId);
845        }
846
847        @Override
848        public int hashCode() {
849            return Objects.hashCode(account.getJid(), with, sessionId);
850        }
851
852        @Override
853        public Account getAccount() {
854            return account;
855        }
856
857        @Override
858        public Jid getWith() {
859            return with;
860        }
861
862        @Override
863        public String getSessionId() {
864            return sessionId;
865        }
866    }
867}