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