JingleConnectionManager.java

  1package eu.siacs.conversations.xmpp.jingle;
  2
  3import android.os.SystemClock;
  4import android.util.Base64;
  5import android.util.Log;
  6
  7import com.google.common.base.Function;
  8import com.google.common.base.Objects;
  9import com.google.common.base.Preconditions;
 10import com.google.common.collect.Collections2;
 11import com.google.common.collect.ImmutableSet;
 12
 13import org.checkerframework.checker.nullness.compatqual.NullableDecl;
 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;
 24
 25import eu.siacs.conversations.Config;
 26import eu.siacs.conversations.entities.Account;
 27import eu.siacs.conversations.entities.Conversation;
 28import eu.siacs.conversations.entities.Conversational;
 29import eu.siacs.conversations.entities.Message;
 30import eu.siacs.conversations.entities.RtpSessionStatus;
 31import eu.siacs.conversations.entities.Transferable;
 32import eu.siacs.conversations.services.AbstractConnectionManager;
 33import eu.siacs.conversations.services.XmppConnectionService;
 34import eu.siacs.conversations.xml.Element;
 35import eu.siacs.conversations.xml.Namespace;
 36import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 37import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
 38import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
 39import eu.siacs.conversations.xmpp.jingle.stanzas.GenericDescription;
 40import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
 41import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
 42import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
 43import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
 44import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 45import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 46import rocks.xmpp.addr.Jid;
 47
 48public class JingleConnectionManager extends AbstractConnectionManager {
 49    private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals = new HashMap<>();
 50    private final Map<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
 51
 52    private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
 53
 54    public JingleConnectionManager(XmppConnectionService service) {
 55        super(service);
 56    }
 57
 58    static String nextRandomId() {
 59        final byte[] id = new byte[16];
 60        new SecureRandom().nextBytes(id);
 61        return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING);
 62    }
 63
 64    public void deliverPacket(final Account account, final JinglePacket packet) {
 65        final String sessionId = packet.getSessionId();
 66        if (sessionId == null) {
 67            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
 68            return;
 69        }
 70        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
 71        final AbstractJingleConnection existingJingleConnection = connections.get(id);
 72        if (existingJingleConnection != null) {
 73            existingJingleConnection.deliverPacket(packet);
 74        } else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
 75            final Jid from = packet.getFrom();
 76            final Content content = packet.getJingleContent();
 77            final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace();
 78            final AbstractJingleConnection connection;
 79            if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
 80                connection = new JingleFileTransferConnection(this, id, from);
 81            } else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace) && !usesTor(account)) {
 82                if (isBusy()) {
 83                    mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
 84                    final JinglePacket sessionTermination = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
 85                    sessionTermination.setTo(id.with);
 86                    sessionTermination.setReason(Reason.BUSY, null);
 87                    mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
 88                    return;
 89                }
 90                connection = new JingleRtpConnection(this, id, from);
 91            } else {
 92                respondWithJingleError(account, packet, "unsupported-info", "feature-not-implemented", "cancel");
 93                return;
 94            }
 95            connections.put(id, connection);
 96            connection.deliverPacket(packet);
 97        } else {
 98            Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
 99            respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
100        }
101    }
102
103    private boolean usesTor(final Account account) {
104        return account.isOnion() || mXmppConnectionService.useTorToConnect();
105    }
106
107    public boolean isBusy() {
108        for (AbstractJingleConnection connection : this.connections.values()) {
109            if (connection instanceof JingleRtpConnection) {
110                return true;
111            }
112        }
113        synchronized (this.rtpSessionProposals) {
114            return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED) || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING);
115        }
116    }
117
118    public void respondWithJingleError(final Account account, final IqPacket original, String jingleCondition, String condition, String conditionType) {
119        final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
120        final Element error = response.addChild("error");
121        error.setAttribute("type", conditionType);
122        error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
123        error.addChild(jingleCondition, "urn:xmpp:jingle:errors:1");
124        account.getXmppConnection().sendIqPacket(response, null);
125    }
126
127    public void deliverMessage(final Account account, final Jid to, final Jid from, final Element message, String serverMsgId, long timestamp) {
128        Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
129        final String sessionId = message.getAttribute("id");
130        if (sessionId == null) {
131            return;
132        }
133        if ("accept".equals(message.getName())) {
134            for (AbstractJingleConnection connection : connections.values()) {
135                if (connection instanceof JingleRtpConnection) {
136                    final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
137                    final AbstractJingleConnection.Id id = connection.getId();
138                    if (id.account == account && id.sessionId.equals(sessionId)) {
139                        rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
140                        return;
141                    }
142                }
143            }
144            return;
145        }
146        final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
147        final AbstractJingleConnection.Id id;
148        if (fromSelf) {
149            if (to.isFullJid()) {
150                id = AbstractJingleConnection.Id.of(account, to, sessionId);
151            } else {
152                return;
153            }
154        } else {
155            id = AbstractJingleConnection.Id.of(account, from, sessionId);
156        }
157        final AbstractJingleConnection existingJingleConnection = connections.get(id);
158        if (existingJingleConnection != null) {
159            if (existingJingleConnection instanceof JingleRtpConnection) {
160                ((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message, serverMsgId, timestamp);
161            } else {
162                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
163            }
164            return;
165        }
166
167        if (fromSelf) {
168            if ("proceed".equals(message.getName())) {
169                final Conversation c = mXmppConnectionService.findOrCreateConversation(account, id.with, false, false);
170                final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
171                if (previousBusy != null) {
172                    previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
173                    if (serverMsgId != null) {
174                        previousBusy.setServerMsgId(serverMsgId);
175                    }
176                    previousBusy.setTime(timestamp);
177                    mXmppConnectionService.updateMessage(previousBusy, true);
178                    Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": updated previous busy because call got picked up by another device");
179                    return;
180                }
181            }
182            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignore jingle message from self");
183            return;
184        }
185
186        if ("propose".equals(message.getName())) {
187            final Propose propose = Propose.upgrade(message);
188            final List<GenericDescription> descriptions = propose.getDescriptions();
189            final Collection<RtpDescription> rtpDescriptions = Collections2.transform(
190                    Collections2.filter(descriptions, d -> d instanceof RtpDescription),
191                    input -> (RtpDescription) input
192            );
193            if (rtpDescriptions.size() > 0 && rtpDescriptions.size() == descriptions.size() && !usesTor(account)) {
194                final Collection<Media> media = Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
195                if (media.contains(Media.UNKNOWN)) {
196                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": encountered unknown media in session proposal. " + propose);
197                    return;
198                }
199                if (isBusy()) {
200                    writeLogMissedIncoming(account, id.with.asBareJid(), id.sessionId, serverMsgId, timestamp);
201                    final int activeDevices = account.countPresences();
202                    Log.d(Config.LOGTAG, "active devices: " + activeDevices);
203                    if (activeDevices == 0) {
204                        final MessagePacket reject = mXmppConnectionService.getMessageGenerator().sessionReject(from, sessionId);
205                        mXmppConnectionService.sendMessagePacket(account, reject);
206                    } else {
207                        Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring proposal because busy on this device but there are other devices");
208                    }
209                } else {
210                    final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
211                    this.connections.put(id, rtpConnection);
212                    rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
213                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
214                }
215            } else {
216                Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed session with " + rtpDescriptions.size() + " rtp descriptions of " + descriptions.size() + " total descriptions");
217            }
218        } else if ("proceed".equals(message.getName())) {
219            synchronized (rtpSessionProposals) {
220                final RtpSessionProposal proposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
221                if (proposal != null) {
222                    rtpSessionProposals.remove(proposal);
223                    final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
224                    rtpConnection.setProposedMedia(proposal.media);
225                    this.connections.put(id, rtpConnection);
226                    rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
227                    rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
228                } else {
229                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver proceed");
230                }
231            }
232        } else if ("reject".equals(message.getName())) {
233            final RtpSessionProposal proposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
234            synchronized (rtpSessionProposals) {
235                if (rtpSessionProposals.remove(proposal) != null) {
236                    writeLogMissedOutgoing(account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
237                    mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, proposal.with, proposal.sessionId, RtpEndUserState.DECLINED_OR_BUSY);
238                } else {
239                    Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver reject");
240                }
241            }
242        } else {
243            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message");
244        }
245
246    }
247
248    private RtpSessionProposal getRtpSessionProposal(final Account account, Jid from, String sessionId) {
249        for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
250            if (rtpSessionProposal.sessionId.equals(sessionId) && rtpSessionProposal.with.equals(from) && rtpSessionProposal.account.getJid().equals(account.getJid())) {
251                return rtpSessionProposal;
252            }
253        }
254        return null;
255    }
256
257    private void writeLogMissedOutgoing(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
258        final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
259                account,
260                with.asBareJid(),
261                false,
262                false
263        );
264        final Message message = new Message(
265                conversation,
266                Message.STATUS_SEND,
267                Message.TYPE_RTP_SESSION,
268                sessionId
269        );
270        message.setBody(new RtpSessionStatus(false, 0).toString());
271        message.setServerMsgId(serverMsgId);
272        message.setTime(timestamp);
273        writeMessage(message);
274    }
275
276    private void writeLogMissedIncoming(final Account account, Jid with, final String sessionId, String serverMsgId, long timestamp) {
277        final Conversation conversation = mXmppConnectionService.findOrCreateConversation(
278                account,
279                with.asBareJid(),
280                false,
281                false
282        );
283        final Message message = new Message(
284                conversation,
285                Message.STATUS_RECEIVED,
286                Message.TYPE_RTP_SESSION,
287                sessionId
288        );
289        message.setBody(new RtpSessionStatus(false, 0).toString());
290        message.setServerMsgId(serverMsgId);
291        message.setTime(timestamp);
292        writeMessage(message);
293    }
294
295    private void writeMessage(final Message message) {
296        final Conversational conversational = message.getConversation();
297        if (conversational instanceof Conversation) {
298            ((Conversation) conversational).add(message);
299            mXmppConnectionService.databaseBackend.createMessage(message);
300            mXmppConnectionService.updateConversationUi();
301        } else {
302            throw new IllegalStateException("Somehow the conversation in a message was a stub");
303        }
304    }
305
306    public void startJingleFileTransfer(final Message message) {
307        Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
308        final Transferable old = message.getTransferable();
309        if (old != null) {
310            old.cancel();
311        }
312        final Account account = message.getConversation().getAccount();
313        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
314        final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
315        mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
316        this.connections.put(id, connection);
317        connection.init(message);
318    }
319
320    void finishConnection(final AbstractJingleConnection connection) {
321        this.connections.remove(connection.getId());
322    }
323
324    void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
325        if (Config.DISABLE_PROXY_LOOKUP) {
326            listener.onPrimaryCandidateFound(false, null);
327            return;
328        }
329        if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
330            final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
331            if (proxy != null) {
332                IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
333                iq.setTo(proxy);
334                iq.query(Namespace.BYTE_STREAMS);
335                account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
336
337                    @Override
338                    public void onIqPacketReceived(Account account, IqPacket packet) {
339                        final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
340                        final String host = streamhost == null ? null : streamhost.getAttribute("host");
341                        final String port = streamhost == null ? null : streamhost.getAttribute("port");
342                        if (host != null && port != null) {
343                            try {
344                                JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
345                                candidate.setHost(host);
346                                candidate.setPort(Integer.parseInt(port));
347                                candidate.setType(JingleCandidate.TYPE_PROXY);
348                                candidate.setJid(proxy);
349                                candidate.setPriority(655360 + (initiator ? 30 : 0));
350                                primaryCandidates.put(account.getJid().asBareJid(), candidate);
351                                listener.onPrimaryCandidateFound(true, candidate);
352                            } catch (final NumberFormatException e) {
353                                listener.onPrimaryCandidateFound(false, null);
354                            }
355                        } else {
356                            listener.onPrimaryCandidateFound(false, null);
357                        }
358                    }
359                });
360            } else {
361                listener.onPrimaryCandidateFound(false, null);
362            }
363
364        } else {
365            listener.onPrimaryCandidateFound(true,
366                    this.primaryCandidates.get(account.getJid().asBareJid()));
367        }
368    }
369
370    public void retractSessionProposal(final Account account, final Jid with) {
371        synchronized (this.rtpSessionProposals) {
372            RtpSessionProposal matchingProposal = null;
373            for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
374                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
375                    matchingProposal = proposal;
376                    break;
377                }
378            }
379            if (matchingProposal != null) {
380                this.rtpSessionProposals.remove(matchingProposal);
381                final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionRetract(matchingProposal);
382                writeLogMissedOutgoing(account, matchingProposal.with, matchingProposal.sessionId, null, System.currentTimeMillis());
383                mXmppConnectionService.sendMessagePacket(account, messagePacket);
384
385            }
386        }
387    }
388
389    public void proposeJingleRtpSession(final Account account, final Jid with, final Set<Media> media) {
390        synchronized (this.rtpSessionProposals) {
391            for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
392                RtpSessionProposal proposal = entry.getKey();
393                if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
394                    final DeviceDiscoveryState preexistingState = entry.getValue();
395                    if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
396                        mXmppConnectionService.notifyJingleRtpConnectionUpdate(
397                                account,
398                                with,
399                                proposal.sessionId,
400                                preexistingState.toEndUserState()
401                        );
402                        return;
403                    }
404                }
405            }
406            if (isBusy()) {
407                throw new IllegalStateException("There is already a running RTP session. This should have been caught by the UI");
408            }
409            final RtpSessionProposal proposal = RtpSessionProposal.of(account, with.asBareJid(), media);
410            this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
411            mXmppConnectionService.notifyJingleRtpConnectionUpdate(
412                    account,
413                    proposal.with,
414                    proposal.sessionId,
415                    RtpEndUserState.FINDING_DEVICE
416            );
417            final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
418            Log.d(Config.LOGTAG, messagePacket.toString());
419            mXmppConnectionService.sendMessagePacket(account, messagePacket);
420        }
421    }
422
423    public void deliverIbbPacket(Account account, IqPacket packet) {
424        final String sid;
425        final Element payload;
426        if (packet.hasChild("open", Namespace.IBB)) {
427            payload = packet.findChild("open", Namespace.IBB);
428            sid = payload.getAttribute("sid");
429        } else if (packet.hasChild("data", Namespace.IBB)) {
430            payload = packet.findChild("data", Namespace.IBB);
431            sid = payload.getAttribute("sid");
432        } else if (packet.hasChild("close", Namespace.IBB)) {
433            payload = packet.findChild("close", Namespace.IBB);
434            sid = payload.getAttribute("sid");
435        } else {
436            payload = null;
437            sid = null;
438        }
439        if (sid != null) {
440            for (final AbstractJingleConnection connection : this.connections.values()) {
441                if (connection instanceof JingleFileTransferConnection) {
442                    final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
443                    final JingleTransport transport = fileTransfer.getTransport();
444                    if (transport instanceof JingleInBandTransport) {
445                        final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
446                        if (inBandTransport.matches(account, sid)) {
447                            inBandTransport.deliverPayload(packet, payload);
448                        }
449                        return;
450                    }
451                }
452            }
453        }
454        Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
455        account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
456    }
457
458    public void notifyRebound() {
459        for (final AbstractJingleConnection connection : this.connections.values()) {
460            connection.notifyRebound();
461        }
462    }
463
464    public WeakReference<JingleRtpConnection> findJingleRtpConnection(Account account, Jid with, String sessionId) {
465        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, Jid.ofEscaped(with), sessionId);
466        final AbstractJingleConnection connection = connections.get(id);
467        if (connection instanceof JingleRtpConnection) {
468            return new WeakReference<>((JingleRtpConnection) connection);
469        }
470        return null;
471    }
472
473    public void updateProposedSessionDiscovered(Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
474        synchronized (this.rtpSessionProposals) {
475            final RtpSessionProposal sessionProposal = getRtpSessionProposal(account, from.asBareJid(), sessionId);
476            final DeviceDiscoveryState currentState = sessionProposal == null ? null : rtpSessionProposals.get(sessionProposal);
477            if (currentState == null) {
478                Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
479                return;
480            }
481            if (currentState == DeviceDiscoveryState.DISCOVERED) {
482                Log.d(Config.LOGTAG, "session proposal already at discovered. not going to fall back");
483                return;
484            }
485            this.rtpSessionProposals.put(sessionProposal, target);
486            mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, sessionProposal.with, sessionProposal.sessionId, target.toEndUserState());
487            Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": flagging session " + sessionId + " as " + target);
488        }
489    }
490
491    public void rejectRtpSession(final String sessionId) {
492        for (final AbstractJingleConnection connection : this.connections.values()) {
493            if (connection.getId().sessionId.equals(sessionId)) {
494                if (connection instanceof JingleRtpConnection) {
495                    ((JingleRtpConnection) connection).rejectCall();
496                }
497            }
498        }
499    }
500
501    public void endRtpSession(final String sessionId) {
502        for (final AbstractJingleConnection connection : this.connections.values()) {
503            if (connection.getId().sessionId.equals(sessionId)) {
504                if (connection instanceof JingleRtpConnection) {
505                    ((JingleRtpConnection) connection).endCall();
506                }
507            }
508        }
509    }
510
511    public void failProceed(Account account, final Jid with, String sessionId) {
512        final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
513        final AbstractJingleConnection existingJingleConnection = connections.get(id);
514        if (existingJingleConnection instanceof JingleRtpConnection) {
515            ((JingleRtpConnection) existingJingleConnection).deliverFailedProceed();
516        }
517    }
518
519    void ensureConnectionIsRegistered(final AbstractJingleConnection connection) {
520        if (connections.containsValue(connection)) {
521            return;
522        }
523        throw new IllegalStateException("JingleConnection has not been registered with connection manager");
524    }
525
526    public enum DeviceDiscoveryState {
527        SEARCHING, DISCOVERED, FAILED;
528
529        public RtpEndUserState toEndUserState() {
530            switch (this) {
531                case SEARCHING:
532                    return RtpEndUserState.FINDING_DEVICE;
533                case DISCOVERED:
534                    return RtpEndUserState.RINGING;
535                default:
536                    return RtpEndUserState.CONNECTIVITY_ERROR;
537            }
538        }
539    }
540
541    public static class RtpSessionProposal {
542        public final Jid with;
543        public final String sessionId;
544        public final Set<Media> media;
545        private final Account account;
546
547        private RtpSessionProposal(Account account, Jid with, String sessionId) {
548            this(account, with, sessionId, Collections.emptySet());
549        }
550
551        private RtpSessionProposal(Account account, Jid with, String sessionId, Set<Media> media) {
552            this.account = account;
553            this.with = with;
554            this.sessionId = sessionId;
555            this.media = media;
556        }
557
558        public static RtpSessionProposal of(Account account, Jid with, Set<Media> media) {
559            return new RtpSessionProposal(account, with, nextRandomId(), media);
560        }
561
562        @Override
563        public boolean equals(Object o) {
564            if (this == o) return true;
565            if (o == null || getClass() != o.getClass()) return false;
566            RtpSessionProposal proposal = (RtpSessionProposal) o;
567            return Objects.equal(account.getJid(), proposal.account.getJid()) &&
568                    Objects.equal(with, proposal.with) &&
569                    Objects.equal(sessionId, proposal.sessionId);
570        }
571
572        @Override
573        public int hashCode() {
574            return Objects.hashCode(account.getJid(), with, sessionId);
575        }
576    }
577}