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