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