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 if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED)) return true;
189 if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING)) return true;
190 if (this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING_ACKNOWLEDGED)) return true;
191 return false;
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 String hasMatchingRtpSession(final Account account, final Jid with, final Set<Media> media) {
232 for (AbstractJingleConnection connection : this.connections.values()) {
233 if (connection instanceof JingleRtpConnection rtpConnection) {
234 if (rtpConnection.isTerminated()) {
235 continue;
236 }
237 if (rtpConnection.getId().account == account
238 && rtpConnection.getId().with.asBareJid().equals(with.asBareJid())
239 && rtpConnection.getMedia().equals(media)) {
240 return rtpConnection.getId().sessionId;
241 }
242 }
243 }
244 return null;
245 }
246
247 private boolean isWithStrangerAndStrangerNotificationsAreOff(final Account account, Jid with) {
248 final boolean notifyForStrangers =
249 mXmppConnectionService.getNotificationService().notificationsFromStrangers();
250 if (notifyForStrangers) {
251 return false;
252 }
253 final Contact contact = account.getRoster().getContact(with);
254 return !contact.showInContactList();
255 }
256
257 ScheduledFuture<?> schedule(
258 final Runnable runnable, final long delay, final TimeUnit timeUnit) {
259 return SCHEDULED_EXECUTOR_SERVICE.schedule(runnable, delay, timeUnit);
260 }
261
262 void respondWithJingleError(
263 final Account account,
264 final IqPacket original,
265 final String jingleCondition,
266 final String condition,
267 final String conditionType) {
268 final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
269 final Element error = response.addChild("error");
270 error.setAttribute("type", conditionType);
271 error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
272 if (jingleCondition != null) {
273 error.addChild(jingleCondition, Namespace.JINGLE_ERRORS);
274 }
275 account.getXmppConnection().sendIqPacket(response, null);
276 }
277
278 public void deliverMessage(
279 final Account account,
280 final Jid to,
281 final Jid from,
282 final Element message,
283 String remoteMsgId,
284 String serverMsgId,
285 long timestamp) {
286 Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
287 final String sessionId = message.getAttribute("id");
288 if (sessionId == null) {
289 return;
290 }
291 if ("accept".equals(message.getName()) || "reject".equals(message.getName())) {
292 for (AbstractJingleConnection connection : connections.values()) {
293 if (connection instanceof JingleRtpConnection rtpConnection) {
294 final AbstractJingleConnection.Id id = connection.getId();
295 if (id.account == account && id.sessionId.equals(sessionId)) {
296 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
297 return;
298 }
299 }
300 }
301 if ("accept".equals(message.getName())) return;
302 }
303 final boolean fromSelf = from.asBareJid().equals(account.getJid().asBareJid());
304 // XEP version 0.6.0 sends proceed, reject, ringing to bare jid
305 final boolean addressedDirectly = to != null && to.equals(account.getJid());
306 final AbstractJingleConnection.Id id;
307 if (fromSelf) {
308 if (to != null && to.isFullJid()) {
309 id = AbstractJingleConnection.Id.of(account, to, sessionId);
310 } else {
311 return;
312 }
313 } else {
314 id = AbstractJingleConnection.Id.of(account, from, sessionId);
315 }
316 final AbstractJingleConnection existingJingleConnection = connections.get(id);
317 if (existingJingleConnection != null) {
318 if (existingJingleConnection instanceof JingleRtpConnection) {
319 ((JingleRtpConnection) existingJingleConnection)
320 .deliveryMessage(from, message, serverMsgId, timestamp);
321 } else {
322 Log.d(
323 Config.LOGTAG,
324 account.getJid().asBareJid()
325 + ": "
326 + existingJingleConnection.getClass().getName()
327 + " does not support jingle messages");
328 }
329 return;
330 }
331
332 if (fromSelf) {
333 if ("proceed".equals(message.getName())) {
334 final Conversation c =
335 mXmppConnectionService.findOrCreateConversation(
336 account, id.with, false, false);
337 final Message previousBusy = c.findRtpSession(sessionId, Message.STATUS_RECEIVED);
338 if (previousBusy != null) {
339 previousBusy.setBody(new RtpSessionStatus(true, 0).toString());
340 if (serverMsgId != null) {
341 previousBusy.setServerMsgId(serverMsgId);
342 }
343 previousBusy.setTime(timestamp);
344 mXmppConnectionService.updateMessage(previousBusy, true);
345 Log.d(
346 Config.LOGTAG,
347 id.account.getJid().asBareJid()
348 + ": updated previous busy because call got picked up by another device");
349 mXmppConnectionService.getNotificationService().clearMissedCall(previousBusy);
350 return;
351 }
352 }
353 // TODO handle reject for cases where we don’t have carbon copies (normally reject is to
354 // be sent to own bare jid as well)
355 Log.d(
356 Config.LOGTAG,
357 account.getJid().asBareJid() + ": ignore jingle message from self");
358 return;
359 }
360
361 if ("propose".equals(message.getName())) {
362 final Propose propose = Propose.upgrade(message);
363 final List<GenericDescription> descriptions = propose.getDescriptions();
364 final Collection<RtpDescription> rtpDescriptions =
365 Collections2.transform(
366 Collections2.filter(descriptions, d -> d instanceof RtpDescription),
367 input -> (RtpDescription) input);
368 if (rtpDescriptions.size() > 0
369 && rtpDescriptions.size() == descriptions.size()
370 && isUsingClearNet(account)) {
371 final Collection<Media> media =
372 Collections2.transform(rtpDescriptions, RtpDescription::getMedia);
373 if (media.contains(Media.UNKNOWN)) {
374 Log.d(
375 Config.LOGTAG,
376 account.getJid().asBareJid()
377 + ": encountered unknown media in session proposal. "
378 + propose);
379 return;
380 }
381 final Optional<RtpSessionProposal> matchingSessionProposal =
382 findMatchingSessionProposal(account, id.with, ImmutableSet.copyOf(media));
383 if (matchingSessionProposal.isPresent()) {
384 final String ourSessionId = matchingSessionProposal.get().sessionId;
385 final String theirSessionId = id.sessionId;
386 if (ComparisonChain.start()
387 .compare(ourSessionId, theirSessionId)
388 .compare(
389 account.getJid().toEscapedString(),
390 id.with.toEscapedString())
391 .result()
392 > 0) {
393 Log.d(
394 Config.LOGTAG,
395 account.getJid().asBareJid()
396 + ": our session lost tie break. automatically accepting their session. winning Session="
397 + theirSessionId);
398 // TODO a retract for this reason should probably include some indication of
399 // tie break
400 retractSessionProposal(matchingSessionProposal.get());
401 final JingleRtpConnection rtpConnection =
402 new JingleRtpConnection(this, id, from);
403 this.connections.put(id, rtpConnection);
404 rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
405 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
406 addNewIncomingCall(rtpConnection);
407 // TODO actually do the automatic accept?!
408 } else {
409 Log.d(
410 Config.LOGTAG,
411 account.getJid().asBareJid()
412 + ": our session won tie break. waiting for other party to accept. winningSession="
413 + ourSessionId);
414 // TODO reject their session with <tie-break/>?
415 }
416 return;
417 }
418 final boolean stranger =
419 isWithStrangerAndStrangerNotificationsAreOff(account, id.with);
420 if (isBusy() || stranger) {
421 writeLogMissedIncoming(
422 account,
423 id.with.asBareJid(),
424 id.sessionId,
425 serverMsgId,
426 timestamp,
427 stranger);
428 if (stranger) {
429 Log.d(
430 Config.LOGTAG,
431 id.account.getJid().asBareJid()
432 + ": ignoring call proposal from stranger "
433 + id.with);
434 return;
435 }
436 final int activeDevices = account.activeDevicesWithRtpCapability();
437 Log.d(Config.LOGTAG, "active devices with rtp capability: " + activeDevices);
438 if (activeDevices == 0) {
439 final MessagePacket reject =
440 mXmppConnectionService
441 .getMessageGenerator()
442 .sessionReject(from, sessionId);
443 mXmppConnectionService.sendMessagePacket(account, reject);
444 } else {
445 Log.d(
446 Config.LOGTAG,
447 id.account.getJid().asBareJid()
448 + ": ignoring proposal because busy on this device but there are other devices");
449 }
450 } else {
451 final JingleRtpConnection rtpConnection =
452 new JingleRtpConnection(this, id, from);
453 this.connections.put(id, rtpConnection);
454 rtpConnection.setProposedMedia(ImmutableSet.copyOf(media));
455 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
456 addNewIncomingCall(rtpConnection);
457 }
458 } else {
459 Log.d(
460 Config.LOGTAG,
461 account.getJid().asBareJid()
462 + ": unable to react to proposed session with "
463 + rtpDescriptions.size()
464 + " rtp descriptions of "
465 + descriptions.size()
466 + " total descriptions");
467 }
468 } else if (addressedDirectly && "proceed".equals(message.getName())) {
469 synchronized (rtpSessionProposals) {
470 final RtpSessionProposal proposal =
471 getRtpSessionProposal(account, from.asBareJid(), sessionId);
472 if (proposal != null) {
473 rtpSessionProposals.remove(proposal);
474 final JingleRtpConnection rtpConnection =
475 new JingleRtpConnection(
476 this, id, account.getJid(), proposal.callIntegration);
477 rtpConnection.setProposedMedia(proposal.media);
478 this.connections.put(id, rtpConnection);
479 rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
480 rtpConnection.deliveryMessage(from, message, serverMsgId, timestamp);
481 } else {
482 Log.d(
483 Config.LOGTAG,
484 account.getJid().asBareJid()
485 + ": no rtp session ("
486 + sessionId
487 + ") proposal found for "
488 + from
489 + " to deliver proceed");
490 if (remoteMsgId == null) {
491 return;
492 }
493 final MessagePacket errorMessage = new MessagePacket();
494 errorMessage.setTo(from);
495 errorMessage.setId(remoteMsgId);
496 errorMessage.setType(MessagePacket.TYPE_ERROR);
497 final Element error = errorMessage.addChild("error");
498 error.setAttribute("code", "404");
499 error.setAttribute("type", "cancel");
500 error.addChild("item-not-found", "urn:ietf:params:xml:ns:xmpp-stanzas");
501 mXmppConnectionService.sendMessagePacket(account, errorMessage);
502 }
503 }
504 } else if (addressedDirectly && "reject".equals(message.getName())) {
505 final RtpSessionProposal proposal =
506 getRtpSessionProposal(account, from.asBareJid(), sessionId);
507 synchronized (rtpSessionProposals) {
508 if (proposal != null) {
509 setTerminalSessionState(proposal, RtpEndUserState.DECLINED_OR_BUSY);
510 rtpSessionProposals.remove(proposal);
511 proposal.callIntegration.busy();
512 writeLogMissedOutgoing(
513 account, proposal.with, proposal.sessionId, serverMsgId, timestamp);
514 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
515 account,
516 proposal.with,
517 proposal.sessionId,
518 RtpEndUserState.DECLINED_OR_BUSY);
519 } else {
520 Log.d(
521 Config.LOGTAG,
522 account.getJid().asBareJid()
523 + ": no rtp session proposal found for "
524 + from
525 + " to deliver reject");
526 }
527 }
528 } else if (addressedDirectly && "ringing".equals(message.getName())) {
529 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + from + " started ringing");
530 updateProposedSessionDiscovered(
531 account, from, sessionId, DeviceDiscoveryState.DISCOVERED);
532 } else {
533 Log.d(
534 Config.LOGTAG,
535 account.getJid()
536 + ": received out of order jingle message from="
537 + from
538 + ", message="
539 + message
540 + ", addressedDirectly="
541 + addressedDirectly);
542 }
543 }
544
545 private RtpSessionProposal getRtpSessionProposal(
546 final Account account, Jid from, String sessionId) {
547 for (RtpSessionProposal rtpSessionProposal : rtpSessionProposals.keySet()) {
548 if (rtpSessionProposal.sessionId.equals(sessionId)
549 && rtpSessionProposal.with.equals(from)
550 && rtpSessionProposal.account.getJid().equals(account.getJid())) {
551 return rtpSessionProposal;
552 }
553 }
554 return null;
555 }
556
557 private void writeLogMissedOutgoing(
558 final Account account,
559 Jid with,
560 final String sessionId,
561 String serverMsgId,
562 long timestamp) {
563 final Conversation conversation =
564 mXmppConnectionService.findOrCreateConversation(
565 account, with.asBareJid(), false, false);
566 final Message message =
567 new Message(conversation, Message.STATUS_SEND, Message.TYPE_RTP_SESSION, sessionId);
568 message.setBody(new RtpSessionStatus(false, 0).toString());
569 message.setServerMsgId(serverMsgId);
570 message.setTime(timestamp);
571 writeMessage(message);
572 }
573
574 private void writeLogMissedIncoming(
575 final Account account,
576 final Jid with,
577 final String sessionId,
578 final String serverMsgId,
579 final long timestamp,
580 final boolean stranger) {
581 final Conversation conversation =
582 mXmppConnectionService.findOrCreateConversation(
583 account, with.asBareJid(), false, false);
584 final Message message =
585 new Message(
586 conversation, Message.STATUS_RECEIVED, Message.TYPE_RTP_SESSION, sessionId);
587 message.setBody(new RtpSessionStatus(false, 0).toString());
588 message.setServerMsgId(serverMsgId);
589 message.setTime(timestamp);
590 message.setCounterpart(with);
591 writeMessage(message);
592 if (stranger) {
593 return;
594 }
595 mXmppConnectionService.getNotificationService().pushMissedCallNow(message);
596 }
597
598 private void writeMessage(final Message message) {
599 final Conversational conversational = message.getConversation();
600 if (conversational instanceof Conversation) {
601 ((Conversation) conversational).add(message);
602 mXmppConnectionService.databaseBackend.createMessage(message);
603 mXmppConnectionService.updateConversationUi();
604 } else {
605 throw new IllegalStateException("Somehow the conversation in a message was a stub");
606 }
607 }
608
609 public void startJingleFileTransfer(final Message message) {
610 Preconditions.checkArgument(
611 message.isFileOrImage(), "Message is not of type file or image");
612 final Transferable old = message.getTransferable();
613 if (old != null) {
614 old.cancel();
615 }
616 final JingleFileTransferConnection connection =
617 new JingleFileTransferConnection(this, message);
618 this.connections.put(connection.getId(), connection);
619 connection.sendSessionInitialize();
620 }
621
622 public Optional<OngoingRtpSession> getOngoingRtpConnection(final Contact contact) {
623 for (final Map.Entry<AbstractJingleConnection.Id, AbstractJingleConnection> entry :
624 this.connections.entrySet()) {
625 if (entry.getValue() instanceof JingleRtpConnection jingleRtpConnection) {
626 final AbstractJingleConnection.Id id = entry.getKey();
627 if (id.account == contact.getAccount()
628 && id.with.asBareJid().equals(contact.getJid().asBareJid())) {
629 return Optional.of(jingleRtpConnection);
630 }
631 }
632 }
633 synchronized (this.rtpSessionProposals) {
634 for (final Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry :
635 this.rtpSessionProposals.entrySet()) {
636 final RtpSessionProposal proposal = entry.getKey();
637 if (proposal.account == contact.getAccount()
638 && contact.getJid().asBareJid().equals(proposal.with)) {
639 final DeviceDiscoveryState preexistingState = entry.getValue();
640 if (preexistingState != null
641 && preexistingState != DeviceDiscoveryState.FAILED) {
642 return Optional.of(proposal);
643 }
644 }
645 }
646 }
647 return Optional.absent();
648 }
649
650 public JingleRtpConnection getOngoingRtpConnection() {
651 for(final AbstractJingleConnection jingleConnection : this.connections.values()) {
652 if (jingleConnection instanceof JingleRtpConnection jingleRtpConnection) {
653 if (jingleRtpConnection.isTerminated()) {
654 continue;
655 }
656 return jingleRtpConnection;
657 }
658 }
659 return null;
660 }
661
662 void finishConnectionOrThrow(final AbstractJingleConnection connection) {
663 final AbstractJingleConnection.Id id = connection.getId();
664 if (this.connections.remove(id) == null) {
665 throw new IllegalStateException(
666 String.format("Unable to finish connection with id=%s", id));
667 }
668 // update chat UI to remove 'ongoing call' icon
669 mXmppConnectionService.updateConversationUi();
670 }
671
672 public boolean fireJingleRtpConnectionStateUpdates() {
673 for (final AbstractJingleConnection connection : this.connections.values()) {
674 if (connection instanceof JingleRtpConnection jingleRtpConnection) {
675 if (jingleRtpConnection.isTerminated()) {
676 continue;
677 }
678 jingleRtpConnection.fireStateUpdate();
679 return true;
680 }
681 }
682 return false;
683 }
684
685 public void retractSessionProposal(final Account account, final Jid with) {
686 synchronized (this.rtpSessionProposals) {
687 RtpSessionProposal matchingProposal = null;
688 for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
689 if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
690 matchingProposal = proposal;
691 break;
692 }
693 }
694 if (matchingProposal != null) {
695 retractSessionProposal(matchingProposal, false);
696 }
697 }
698 }
699
700 private void retractSessionProposal(final RtpSessionProposal rtpSessionProposal) {
701 retractSessionProposal(rtpSessionProposal, true);
702 }
703
704 private void retractSessionProposal(
705 final RtpSessionProposal rtpSessionProposal, final boolean refresh) {
706 final Account account = rtpSessionProposal.account;
707 Log.d(
708 Config.LOGTAG,
709 account.getJid().asBareJid()
710 + ": retracting rtp session proposal with "
711 + rtpSessionProposal.with);
712 this.rtpSessionProposals.remove(rtpSessionProposal);
713 rtpSessionProposal.callIntegration.retracted();
714 if (refresh) {
715 mXmppConnectionService.notifyJingleRtpConnectionUpdate(
716 account,
717 rtpSessionProposal.with,
718 rtpSessionProposal.sessionId,
719 RtpEndUserState.RETRACTED);
720 }
721 final MessagePacket messagePacket =
722 mXmppConnectionService.getMessageGenerator().sessionRetract(rtpSessionProposal);
723 writeLogMissedOutgoing(
724 account,
725 rtpSessionProposal.with,
726 rtpSessionProposal.sessionId,
727 null,
728 System.currentTimeMillis());
729 mXmppConnectionService.sendMessagePacket(account, messagePacket);
730 }
731
732 public JingleRtpConnection initializeRtpSession(
733 final Account account, final Jid with, final Set<Media> media) {
734 final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with);
735 final JingleRtpConnection rtpConnection =
736 new JingleRtpConnection(this, id, account.getJid());
737 rtpConnection.setProposedMedia(media);
738 rtpConnection.getCallIntegration().startAudioRouting();
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) != null) {
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("There is already a running RTP session");
770 }
771 final CallIntegration callIntegration =
772 new CallIntegration(mXmppConnectionService.getApplicationContext());
773 callIntegration.setVideoState(
774 Media.audioOnly(media)
775 ? VideoProfile.STATE_AUDIO_ONLY
776 : VideoProfile.STATE_BIDIRECTIONAL);
777 callIntegration.setInitialAudioDevice(CallIntegration.initialAudioDevice(media));
778 callIntegration.startAudioRouting();
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 @Override
1229 public boolean applyDtmfTone(final String dtmf) {
1230 return false;
1231 }
1232 }
1233}