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