1package eu.siacs.conversations.xmpp.jingle;
2
3import android.os.SystemClock;
4import android.util.Log;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8
9import com.google.common.base.Optional;
10import com.google.common.base.Preconditions;
11import com.google.common.base.Strings;
12import com.google.common.base.Throwables;
13import com.google.common.collect.Collections2;
14import com.google.common.collect.ImmutableList;
15import com.google.common.collect.ImmutableMap;
16import com.google.common.collect.Sets;
17import com.google.common.primitives.Ints;
18import com.google.common.util.concurrent.FutureCallback;
19import com.google.common.util.concurrent.Futures;
20import com.google.common.util.concurrent.ListenableFuture;
21import com.google.common.util.concurrent.MoreExecutors;
22
23import org.webrtc.EglBase;
24import org.webrtc.IceCandidate;
25import org.webrtc.PeerConnection;
26import org.webrtc.VideoTrack;
27
28import java.util.ArrayDeque;
29import java.util.Arrays;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.List;
33import java.util.Map;
34import java.util.Set;
35import java.util.concurrent.ScheduledFuture;
36import java.util.concurrent.TimeUnit;
37
38import eu.siacs.conversations.Config;
39import eu.siacs.conversations.crypto.axolotl.AxolotlService;
40import eu.siacs.conversations.crypto.axolotl.CryptoFailedException;
41import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
42import eu.siacs.conversations.entities.Account;
43import eu.siacs.conversations.entities.Conversation;
44import eu.siacs.conversations.entities.Conversational;
45import eu.siacs.conversations.entities.Message;
46import eu.siacs.conversations.entities.RtpSessionStatus;
47import eu.siacs.conversations.services.AppRTCAudioManager;
48import eu.siacs.conversations.utils.IP;
49import eu.siacs.conversations.xml.Element;
50import eu.siacs.conversations.xml.Namespace;
51import eu.siacs.conversations.xmpp.Jid;
52import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
53import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
54import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
55import eu.siacs.conversations.xmpp.jingle.stanzas.Proceed;
56import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
57import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
58import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
59import eu.siacs.conversations.xmpp.stanzas.IqPacket;
60import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
61
62public class JingleRtpConnection extends AbstractJingleConnection implements WebRTCWrapper.EventCallback {
63
64 public static final List<State> STATES_SHOWING_ONGOING_CALL = Arrays.asList(
65 State.PROCEED,
66 State.SESSION_INITIALIZED_PRE_APPROVED,
67 State.SESSION_ACCEPTED
68 );
69 private static final long BUSY_TIME_OUT = 30;
70 private static final List<State> TERMINATED = Arrays.asList(
71 State.ACCEPTED,
72 State.REJECTED,
73 State.REJECTED_RACED,
74 State.RETRACTED,
75 State.RETRACTED_RACED,
76 State.TERMINATED_SUCCESS,
77 State.TERMINATED_DECLINED_OR_BUSY,
78 State.TERMINATED_CONNECTIVITY_ERROR,
79 State.TERMINATED_CANCEL_OR_TIMEOUT,
80 State.TERMINATED_APPLICATION_FAILURE,
81 State.TERMINATED_SECURITY_ERROR
82 );
83
84 private static final Map<State, Collection<State>> VALID_TRANSITIONS;
85
86 static {
87 final ImmutableMap.Builder<State, Collection<State>> transitionBuilder = new ImmutableMap.Builder<>();
88 transitionBuilder.put(State.NULL, ImmutableList.of(
89 State.PROPOSED,
90 State.SESSION_INITIALIZED,
91 State.TERMINATED_APPLICATION_FAILURE,
92 State.TERMINATED_SECURITY_ERROR
93 ));
94 transitionBuilder.put(State.PROPOSED, ImmutableList.of(
95 State.ACCEPTED,
96 State.PROCEED,
97 State.REJECTED,
98 State.RETRACTED,
99 State.TERMINATED_APPLICATION_FAILURE,
100 State.TERMINATED_SECURITY_ERROR,
101 State.TERMINATED_CONNECTIVITY_ERROR //only used when the xmpp connection rebinds
102 ));
103 transitionBuilder.put(State.PROCEED, ImmutableList.of(
104 State.REJECTED_RACED,
105 State.RETRACTED_RACED,
106 State.SESSION_INITIALIZED_PRE_APPROVED,
107 State.TERMINATED_SUCCESS,
108 State.TERMINATED_APPLICATION_FAILURE,
109 State.TERMINATED_SECURITY_ERROR,
110 State.TERMINATED_CONNECTIVITY_ERROR //at this state used for error bounces of the proceed message
111 ));
112 transitionBuilder.put(State.SESSION_INITIALIZED, ImmutableList.of(
113 State.SESSION_ACCEPTED,
114 State.TERMINATED_SUCCESS,
115 State.TERMINATED_DECLINED_OR_BUSY,
116 State.TERMINATED_CONNECTIVITY_ERROR, //at this state used for IQ errors and IQ timeouts
117 State.TERMINATED_CANCEL_OR_TIMEOUT,
118 State.TERMINATED_APPLICATION_FAILURE,
119 State.TERMINATED_SECURITY_ERROR
120 ));
121 transitionBuilder.put(State.SESSION_INITIALIZED_PRE_APPROVED, ImmutableList.of(
122 State.SESSION_ACCEPTED,
123 State.TERMINATED_SUCCESS,
124 State.TERMINATED_DECLINED_OR_BUSY,
125 State.TERMINATED_CONNECTIVITY_ERROR, //at this state used for IQ errors and IQ timeouts
126 State.TERMINATED_CANCEL_OR_TIMEOUT,
127 State.TERMINATED_APPLICATION_FAILURE,
128 State.TERMINATED_SECURITY_ERROR
129 ));
130 transitionBuilder.put(State.SESSION_ACCEPTED, ImmutableList.of(
131 State.TERMINATED_SUCCESS,
132 State.TERMINATED_DECLINED_OR_BUSY,
133 State.TERMINATED_CONNECTIVITY_ERROR,
134 State.TERMINATED_CANCEL_OR_TIMEOUT,
135 State.TERMINATED_APPLICATION_FAILURE,
136 State.TERMINATED_SECURITY_ERROR
137 ));
138 VALID_TRANSITIONS = transitionBuilder.build();
139 }
140
141 private final WebRTCWrapper webRTCWrapper = new WebRTCWrapper(this);
142 private final ArrayDeque<Set<Map.Entry<String, RtpContentMap.DescriptionTransport>>> pendingIceCandidates = new ArrayDeque<>();
143 private final OmemoVerification omemoVerification = new OmemoVerification();
144 private final Message message;
145 private State state = State.NULL;
146 private StateTransitionException stateTransitionException;
147 private Set<Media> proposedMedia;
148 private RtpContentMap initiatorRtpContentMap;
149 private RtpContentMap responderRtpContentMap;
150 private long rtpConnectionStarted = 0; //time of 'connected'
151 private long rtpConnectionEnded = 0;
152 private ScheduledFuture<?> ringingTimeoutFuture;
153
154 JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
155 super(jingleConnectionManager, id, initiator);
156 final Conversation conversation = jingleConnectionManager.getXmppConnectionService().findOrCreateConversation(
157 id.account,
158 id.with.asBareJid(),
159 false,
160 false
161 );
162 this.message = new Message(
163 conversation,
164 isInitiator() ? Message.STATUS_SEND : Message.STATUS_RECEIVED,
165 Message.TYPE_RTP_SESSION,
166 id.sessionId
167 );
168 }
169
170 private static State reasonToState(Reason reason) {
171 switch (reason) {
172 case SUCCESS:
173 return State.TERMINATED_SUCCESS;
174 case DECLINE:
175 case BUSY:
176 return State.TERMINATED_DECLINED_OR_BUSY;
177 case CANCEL:
178 case TIMEOUT:
179 return State.TERMINATED_CANCEL_OR_TIMEOUT;
180 case SECURITY_ERROR:
181 return State.TERMINATED_SECURITY_ERROR;
182 case FAILED_APPLICATION:
183 case UNSUPPORTED_TRANSPORTS:
184 case UNSUPPORTED_APPLICATIONS:
185 return State.TERMINATED_APPLICATION_FAILURE;
186 default:
187 return State.TERMINATED_CONNECTIVITY_ERROR;
188 }
189 }
190
191 @Override
192 synchronized void deliverPacket(final JinglePacket jinglePacket) {
193 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": packet delivered to JingleRtpConnection");
194 switch (jinglePacket.getAction()) {
195 case SESSION_INITIATE:
196 receiveSessionInitiate(jinglePacket);
197 break;
198 case TRANSPORT_INFO:
199 receiveTransportInfo(jinglePacket);
200 break;
201 case SESSION_ACCEPT:
202 receiveSessionAccept(jinglePacket);
203 break;
204 case SESSION_TERMINATE:
205 receiveSessionTerminate(jinglePacket);
206 break;
207 default:
208 respondOk(jinglePacket);
209 Log.d(Config.LOGTAG, String.format("%s: received unhandled jingle action %s", id.account.getJid().asBareJid(), jinglePacket.getAction()));
210 break;
211 }
212 }
213
214 @Override
215 synchronized void notifyRebound() {
216 if (isTerminated()) {
217 return;
218 }
219 webRTCWrapper.close();
220 if (!isInitiator() && isInState(State.PROPOSED, State.SESSION_INITIALIZED)) {
221 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
222 }
223 if (isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
224 //we might have already changed resources (full jid) at this point; so this might not even reach the other party
225 sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
226 } else {
227 transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
228 finish();
229 }
230 }
231
232 private void receiveSessionTerminate(final JinglePacket jinglePacket) {
233 respondOk(jinglePacket);
234 final JinglePacket.ReasonWrapper wrapper = jinglePacket.getReason();
235 final State previous = this.state;
236 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session terminate reason=" + wrapper.reason + "(" + Strings.nullToEmpty(wrapper.text) + ") while in state " + previous);
237 if (TERMINATED.contains(previous)) {
238 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring session terminate because already in " + previous);
239 return;
240 }
241 webRTCWrapper.close();
242 final State target = reasonToState(wrapper.reason);
243 transitionOrThrow(target);
244 writeLogMessage(target);
245 if (previous == State.PROPOSED || previous == State.SESSION_INITIALIZED) {
246 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
247 }
248 finish();
249 }
250
251 private void receiveTransportInfo(final JinglePacket jinglePacket) {
252 //Due to the asynchronicity of processing session-init we might move from NULL|PROCEED to INITIALIZED only after transport-info has been received
253 if (isInState(State.NULL, State.PROCEED, State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
254 respondOk(jinglePacket);
255 final RtpContentMap contentMap;
256 try {
257 contentMap = RtpContentMap.of(jinglePacket);
258 } catch (IllegalArgumentException | NullPointerException e) {
259 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents; ignoring", e);
260 return;
261 }
262 final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> candidates = contentMap.contents.entrySet();
263 if (this.state == State.SESSION_ACCEPTED) {
264 try {
265 processCandidates(candidates);
266 } catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
267 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnection was not initialized when processing transport info. this usually indicates a race condition that can be ignored");
268 }
269 } else {
270 pendingIceCandidates.push(candidates);
271 }
272 } else {
273 if (isTerminated()) {
274 respondOk(jinglePacket);
275 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring out-of-order transport info; we where already terminated");
276 } else {
277 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received transport info while in state=" + this.state);
278 terminateWithOutOfOrder(jinglePacket);
279 }
280 }
281 }
282
283 private void processCandidates(final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> contents) {
284 final RtpContentMap rtpContentMap = isInitiator() ? this.responderRtpContentMap : this.initiatorRtpContentMap;
285 final Group originalGroup = rtpContentMap.group;
286 final List<String> identificationTags = originalGroup == null ? rtpContentMap.getNames() : originalGroup.getIdentificationTags();
287 if (identificationTags.size() == 0) {
288 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no identification tags found in initial offer. we won't be able to calculate mLineIndices");
289 }
290 processCandidates(identificationTags, contents);
291 }
292
293 private void processCandidates(final List<String> indices, final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> contents) {
294 for (final Map.Entry<String, RtpContentMap.DescriptionTransport> content : contents) {
295 final String ufrag = content.getValue().transport.getAttribute("ufrag");
296 for (final IceUdpTransportInfo.Candidate candidate : content.getValue().transport.getCandidates()) {
297 final String sdp;
298 try {
299 sdp = candidate.toSdpAttribute(ufrag);
300 } catch (IllegalArgumentException e) {
301 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring invalid ICE candidate " + e.getMessage());
302 continue;
303 }
304 final String sdpMid = content.getKey();
305 final int mLineIndex = indices.indexOf(sdpMid);
306 if (mLineIndex < 0) {
307 Log.w(Config.LOGTAG, "mLineIndex not found for " + sdpMid + ". available indices " + indices);
308 }
309 final IceCandidate iceCandidate = new IceCandidate(sdpMid, mLineIndex, sdp);
310 Log.d(Config.LOGTAG, "received candidate: " + iceCandidate);
311 this.webRTCWrapper.addIceCandidate(iceCandidate);
312 }
313 }
314 }
315
316 private ListenableFuture<RtpContentMap> receiveRtpContentMap(final JinglePacket jinglePacket, final boolean expectVerification) {
317 final RtpContentMap receivedContentMap;
318 try {
319 receivedContentMap = RtpContentMap.of(jinglePacket);
320 } catch (final Exception e) {
321 return Futures.immediateFailedFuture(e);
322 }
323 if (receivedContentMap instanceof OmemoVerifiedRtpContentMap) {
324 final ListenableFuture<AxolotlService.OmemoVerifiedPayload<RtpContentMap>> future = id.account.getAxolotlService().decrypt((OmemoVerifiedRtpContentMap) receivedContentMap, id.with);
325 return Futures.transform(future, omemoVerifiedPayload -> {
326 //TODO test if an exception here triggers a correct abort
327 omemoVerification.setOrEnsureEqual(omemoVerifiedPayload);
328 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received verifiable DTLS fingerprint via " + omemoVerification);
329 return omemoVerifiedPayload.getPayload();
330 }, MoreExecutors.directExecutor());
331 } else if (Config.REQUIRE_RTP_VERIFICATION || expectVerification) {
332 return Futures.immediateFailedFuture(
333 new SecurityException("DTLS fingerprint was unexpectedly not verifiable")
334 );
335 } else {
336 return Futures.immediateFuture(receivedContentMap);
337 }
338 }
339
340 private void receiveSessionInitiate(final JinglePacket jinglePacket) {
341 if (isInitiator()) {
342 Log.d(Config.LOGTAG, String.format("%s: received session-initiate even though we were initiating", id.account.getJid().asBareJid()));
343 if (isTerminated()) {
344 Log.d(Config.LOGTAG, String.format(
345 "%s: got a reason to terminate with out-of-order. but already in state %s",
346 id.account.getJid().asBareJid(),
347 getState()
348 ));
349 respondWithOutOfOrder(jinglePacket);
350 } else {
351 terminateWithOutOfOrder(jinglePacket);
352 }
353 return;
354 }
355 final ListenableFuture<RtpContentMap> future = receiveRtpContentMap(jinglePacket, false);
356 Futures.addCallback(future, new FutureCallback<RtpContentMap>() {
357 @Override
358 public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
359 receiveSessionInitiate(jinglePacket, rtpContentMap);
360 }
361
362 @Override
363 public void onFailure(@NonNull final Throwable throwable) {
364 respondOk(jinglePacket);
365 sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
366 }
367 }, MoreExecutors.directExecutor());
368 }
369
370 private void receiveSessionInitiate(final JinglePacket jinglePacket, final RtpContentMap contentMap) {
371 try {
372 contentMap.requireContentDescriptions();
373 contentMap.requireDTLSFingerprint();
374 } catch (final RuntimeException e) {
375 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents", Throwables.getRootCause(e));
376 respondOk(jinglePacket);
377 sendSessionTerminate(Reason.of(e), e.getMessage());
378 return;
379 }
380 Log.d(Config.LOGTAG, "processing session-init with " + contentMap.contents.size() + " contents");
381 final State target;
382 if (this.state == State.PROCEED) {
383 Preconditions.checkState(
384 proposedMedia != null && proposedMedia.size() > 0,
385 "proposed media must be set when processing pre-approved session-initiate"
386 );
387 if (!this.proposedMedia.equals(contentMap.getMedia())) {
388 sendSessionTerminate(Reason.SECURITY_ERROR, String.format(
389 "Your session proposal (Jingle Message Initiation) included media %s but your session-initiate was %s",
390 this.proposedMedia,
391 contentMap.getMedia()
392 ));
393 return;
394 }
395 target = State.SESSION_INITIALIZED_PRE_APPROVED;
396 } else {
397 target = State.SESSION_INITIALIZED;
398 }
399 if (transition(target, () -> this.initiatorRtpContentMap = contentMap)) {
400 respondOk(jinglePacket);
401
402 final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> candidates = contentMap.contents.entrySet();
403 if (candidates.size() > 0) {
404 pendingIceCandidates.push(candidates);
405 }
406 if (target == State.SESSION_INITIALIZED_PRE_APPROVED) {
407 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": automatically accepting session-initiate");
408 sendSessionAccept();
409 } else {
410 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received not pre-approved session-initiate. start ringing");
411 startRinging();
412 }
413 } else {
414 Log.d(Config.LOGTAG, String.format("%s: received session-initiate while in state %s", id.account.getJid().asBareJid(), state));
415 terminateWithOutOfOrder(jinglePacket);
416 }
417 }
418
419 private void receiveSessionAccept(final JinglePacket jinglePacket) {
420 if (!isInitiator()) {
421 Log.d(Config.LOGTAG, String.format("%s: received session-accept even though we were responding", id.account.getJid().asBareJid()));
422 terminateWithOutOfOrder(jinglePacket);
423 return;
424 }
425 final ListenableFuture<RtpContentMap> future = receiveRtpContentMap(jinglePacket, this.omemoVerification.hasFingerprint());
426 Futures.addCallback(future, new FutureCallback<RtpContentMap>() {
427 @Override
428 public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
429 receiveSessionAccept(jinglePacket, rtpContentMap);
430 }
431
432 @Override
433 public void onFailure(@NonNull final Throwable throwable) {
434 respondOk(jinglePacket);
435 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents in session-accept", throwable);
436 webRTCWrapper.close();
437 sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
438 }
439 }, MoreExecutors.directExecutor());
440 }
441
442 private void receiveSessionAccept(final JinglePacket jinglePacket, final RtpContentMap contentMap) {
443 try {
444 contentMap.requireContentDescriptions();
445 contentMap.requireDTLSFingerprint();
446 } catch (final RuntimeException e) {
447 respondOk(jinglePacket);
448 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": improperly formatted contents in session-accept", e);
449 webRTCWrapper.close();
450 sendSessionTerminate(Reason.of(e), e.getMessage());
451 return;
452 }
453 final Set<Media> initiatorMedia = this.initiatorRtpContentMap.getMedia();
454 if (!initiatorMedia.equals(contentMap.getMedia())) {
455 sendSessionTerminate(Reason.SECURITY_ERROR, String.format(
456 "Your session-included included media %s but our session-initiate was %s",
457 this.proposedMedia,
458 contentMap.getMedia()
459 ));
460 return;
461 }
462 Log.d(Config.LOGTAG, "processing session-accept with " + contentMap.contents.size() + " contents");
463 if (transition(State.SESSION_ACCEPTED)) {
464 respondOk(jinglePacket);
465 receiveSessionAccept(contentMap);
466 } else {
467 Log.d(Config.LOGTAG, String.format("%s: received session-accept while in state %s", id.account.getJid().asBareJid(), state));
468 respondOk(jinglePacket);
469 }
470 }
471
472 private void receiveSessionAccept(final RtpContentMap contentMap) {
473 this.responderRtpContentMap = contentMap;
474 final SessionDescription sessionDescription;
475 try {
476 sessionDescription = SessionDescription.of(contentMap);
477 } catch (final IllegalArgumentException | NullPointerException e) {
478 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-accept to SDP", e);
479 webRTCWrapper.close();
480 sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
481 return;
482 }
483 final org.webrtc.SessionDescription answer = new org.webrtc.SessionDescription(
484 org.webrtc.SessionDescription.Type.ANSWER,
485 sessionDescription.toString()
486 );
487 try {
488 this.webRTCWrapper.setRemoteDescription(answer).get();
489 } catch (final Exception e) {
490 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to set remote description after receiving session-accept", Throwables.getRootCause(e));
491 webRTCWrapper.close();
492 sendSessionTerminate(Reason.FAILED_APPLICATION);
493 return;
494 }
495 final List<String> identificationTags = contentMap.group == null ? contentMap.getNames() : contentMap.group.getIdentificationTags();
496 processCandidates(identificationTags, contentMap.contents.entrySet());
497 }
498
499 private void sendSessionAccept() {
500 final RtpContentMap rtpContentMap = this.initiatorRtpContentMap;
501 if (rtpContentMap == null) {
502 throw new IllegalStateException("initiator RTP Content Map has not been set");
503 }
504 final SessionDescription offer;
505 try {
506 offer = SessionDescription.of(rtpContentMap);
507 } catch (final IllegalArgumentException | NullPointerException e) {
508 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable convert offer from session-initiate to SDP", e);
509 webRTCWrapper.close();
510 sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
511 return;
512 }
513 sendSessionAccept(rtpContentMap.getMedia(), offer);
514 }
515
516 private void sendSessionAccept(final Set<Media> media, final SessionDescription offer) {
517 discoverIceServers(iceServers -> sendSessionAccept(media, offer, iceServers));
518 }
519
520 private synchronized void sendSessionAccept(final Set<Media> media, final SessionDescription offer, final List<PeerConnection.IceServer> iceServers) {
521 if (isTerminated()) {
522 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": ICE servers got discovered when session was already terminated. nothing to do.");
523 return;
524 }
525 try {
526 setupWebRTC(media, iceServers);
527 } catch (final WebRTCWrapper.InitializationException e) {
528 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
529 webRTCWrapper.close();
530 sendSessionTerminate(Reason.FAILED_APPLICATION);
531 return;
532 }
533 final org.webrtc.SessionDescription sdp = new org.webrtc.SessionDescription(
534 org.webrtc.SessionDescription.Type.OFFER,
535 offer.toString()
536 );
537 try {
538 this.webRTCWrapper.setRemoteDescription(sdp).get();
539 addIceCandidatesFromBlackLog();
540 org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.setLocalDescription().get();
541 prepareSessionAccept(webRTCSessionDescription);
542 } catch (final Exception e) {
543 //TODO sending the error text is worthwhile as well. Especially for FailureToSet exceptions
544 failureToAcceptSession(e);
545 }
546 }
547
548 private void failureToAcceptSession(final Throwable throwable) {
549 if (isTerminated()) {
550 return;
551 }
552 Log.d(Config.LOGTAG, "unable to send session accept", Throwables.getRootCause(throwable));
553 webRTCWrapper.close();
554 sendSessionTerminate(Reason.ofThrowable(throwable));
555 }
556
557 private void addIceCandidatesFromBlackLog() {
558 while (!this.pendingIceCandidates.isEmpty()) {
559 processCandidates(this.pendingIceCandidates.poll());
560 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": added candidates from back log");
561 }
562 }
563
564 private void prepareSessionAccept(final org.webrtc.SessionDescription webRTCSessionDescription) {
565 final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
566 final RtpContentMap respondingRtpContentMap = RtpContentMap.of(sessionDescription);
567 this.responderRtpContentMap = respondingRtpContentMap;
568 final ListenableFuture<RtpContentMap> outgoingContentMapFuture = prepareOutgoingContentMap(respondingRtpContentMap);
569 Futures.addCallback(outgoingContentMapFuture,
570 new FutureCallback<RtpContentMap>() {
571 @Override
572 public void onSuccess(final RtpContentMap outgoingContentMap) {
573 sendSessionAccept(outgoingContentMap);
574 }
575
576 @Override
577 public void onFailure(@NonNull Throwable throwable) {
578 failureToAcceptSession(throwable);
579 }
580 },
581 MoreExecutors.directExecutor()
582 );
583 }
584
585 private void sendSessionAccept(final RtpContentMap rtpContentMap) {
586 if (isTerminated()) {
587 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": preparing session accept was too slow. already terminated. nothing to do.");
588 return;
589 }
590 transitionOrThrow(State.SESSION_ACCEPTED);
591 final JinglePacket sessionAccept = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_ACCEPT, id.sessionId);
592 send(sessionAccept);
593 }
594
595 private ListenableFuture<RtpContentMap> prepareOutgoingContentMap(final RtpContentMap rtpContentMap) {
596 if (this.omemoVerification.hasDeviceId()) {
597 ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>> verifiedPayloadFuture = id.account.getAxolotlService()
598 .encrypt(rtpContentMap, id.with, omemoVerification.getDeviceId());
599 return Futures.transform(verifiedPayloadFuture, verifiedPayload -> {
600 omemoVerification.setOrEnsureEqual(verifiedPayload);
601 return verifiedPayload.getPayload();
602 }, MoreExecutors.directExecutor());
603 } else {
604 return Futures.immediateFuture(rtpContentMap);
605 }
606 }
607
608 synchronized void deliveryMessage(final Jid from, final Element message, final String serverMessageId, final long timestamp) {
609 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": delivered message to JingleRtpConnection " + message);
610 switch (message.getName()) {
611 case "propose":
612 receivePropose(from, Propose.upgrade(message), serverMessageId, timestamp);
613 break;
614 case "proceed":
615 receiveProceed(from, Proceed.upgrade(message), serverMessageId, timestamp);
616 break;
617 case "retract":
618 receiveRetract(from, serverMessageId, timestamp);
619 break;
620 case "reject":
621 receiveReject(from, serverMessageId, timestamp);
622 break;
623 case "accept":
624 receiveAccept(from, serverMessageId, timestamp);
625 break;
626 default:
627 break;
628 }
629 }
630
631 void deliverFailedProceed() {
632 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": receive message error for proceed message");
633 if (transition(State.TERMINATED_CONNECTIVITY_ERROR)) {
634 webRTCWrapper.close();
635 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into connectivity error");
636 this.finish();
637 }
638 }
639
640 private void receiveAccept(final Jid from, final String serverMsgId, final long timestamp) {
641 final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
642 if (originatedFromMyself) {
643 if (transition(State.ACCEPTED)) {
644 if (serverMsgId != null) {
645 this.message.setServerMsgId(serverMsgId);
646 }
647 this.message.setTime(timestamp);
648 this.message.setCarbon(true); //indicate that call was accepted on other device
649 this.writeLogMessageSuccess(0);
650 this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
651 this.finish();
652 } else {
653 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to transition to accept because already in state=" + this.state);
654 }
655 } else {
656 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring 'accept' from " + from);
657 }
658 }
659
660 private void receiveReject(final Jid from, final String serverMsgId, final long timestamp) {
661 final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
662 //reject from another one of my clients
663 if (originatedFromMyself) {
664 receiveRejectFromMyself(serverMsgId, timestamp);
665 } else if (isInitiator()) {
666 if (from.equals(id.with)) {
667 receiveRejectFromResponder();
668 } else {
669 Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from " + from + " for session with " + id.with);
670 }
671 } else {
672 Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from " + from + " for session with " + id.with);
673 }
674 }
675
676 private void receiveRejectFromMyself(String serverMsgId, long timestamp) {
677 if (transition(State.REJECTED)) {
678 this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
679 this.finish();
680 if (serverMsgId != null) {
681 this.message.setServerMsgId(serverMsgId);
682 }
683 this.message.setTime(timestamp);
684 this.message.setCarbon(true); //indicate that call was rejected on other device
685 writeLogMessageMissed();
686 } else {
687 Log.d(Config.LOGTAG, "not able to transition into REJECTED because already in " + this.state);
688 }
689 }
690
691 private void receiveRejectFromResponder() {
692 if (isInState(State.PROCEED)) {
693 Log.d(Config.LOGTAG, id.account.getJid() + ": received reject while still in proceed. callee reconsidered");
694 closeTransitionLogFinish(State.REJECTED_RACED);
695 return;
696 }
697 if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED)) {
698 Log.d(Config.LOGTAG, id.account.getJid() + ": received reject while in SESSION_INITIATED_PRE_APPROVED. callee reconsidered before receiving session-init");
699 closeTransitionLogFinish(State.TERMINATED_DECLINED_OR_BUSY);
700 return;
701 }
702 Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring reject from responder because already in state " + this.state);
703 }
704
705 private void receivePropose(final Jid from, final Propose propose, final String serverMsgId, final long timestamp) {
706 final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
707 if (originatedFromMyself) {
708 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": saw proposal from myself. ignoring");
709 } else if (transition(State.PROPOSED, () -> {
710 final Collection<RtpDescription> descriptions = Collections2.transform(
711 Collections2.filter(propose.getDescriptions(), d -> d instanceof RtpDescription),
712 input -> (RtpDescription) input
713 );
714 final Collection<Media> media = Collections2.transform(descriptions, RtpDescription::getMedia);
715 Preconditions.checkState(!media.contains(Media.UNKNOWN), "RTP descriptions contain unknown media");
716 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received session proposal from " + from + " for " + media);
717 this.proposedMedia = Sets.newHashSet(media);
718 })) {
719 if (serverMsgId != null) {
720 this.message.setServerMsgId(serverMsgId);
721 }
722 this.message.setTime(timestamp);
723 startRinging();
724 } else {
725 Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring session proposal because already in " + state);
726 }
727 }
728
729 private void startRinging() {
730 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received call from " + id.with + ". start ringing");
731 ringingTimeoutFuture = jingleConnectionManager.schedule(this::ringingTimeout, BUSY_TIME_OUT, TimeUnit.SECONDS);
732 xmppConnectionService.getNotificationService().startRinging(id, getMedia());
733 }
734
735 private synchronized void ringingTimeout() {
736 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": timeout reached for ringing");
737 switch (this.state) {
738 case PROPOSED:
739 message.markUnread();
740 rejectCallFromProposed();
741 break;
742 case SESSION_INITIALIZED:
743 message.markUnread();
744 rejectCallFromSessionInitiate();
745 break;
746 }
747 }
748
749 private void cancelRingingTimeout() {
750 final ScheduledFuture<?> future = this.ringingTimeoutFuture;
751 if (future != null && !future.isCancelled()) {
752 future.cancel(false);
753 }
754 }
755
756 private void receiveProceed(final Jid from, final Proceed proceed, final String serverMsgId, final long timestamp) {
757 final Set<Media> media = Preconditions.checkNotNull(this.proposedMedia, "Proposed media has to be set before handling proceed");
758 Preconditions.checkState(media.size() > 0, "Proposed media should not be empty");
759 if (from.equals(id.with)) {
760 if (isInitiator()) {
761 if (transition(State.PROCEED)) {
762 if (serverMsgId != null) {
763 this.message.setServerMsgId(serverMsgId);
764 }
765 this.message.setTime(timestamp);
766 final Integer remoteDeviceId = proceed.getDeviceId();
767 if (isOmemoEnabled()) {
768 this.omemoVerification.setDeviceId(remoteDeviceId);
769 } else {
770 if (remoteDeviceId != null) {
771 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": remote party signaled support for OMEMO verification but we have OMEMO disabled");
772 }
773 this.omemoVerification.setDeviceId(null);
774 }
775 this.sendSessionInitiate(media, State.SESSION_INITIALIZED_PRE_APPROVED);
776 } else {
777 Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because already in %s", id.account.getJid().asBareJid(), this.state));
778 }
779 } else {
780 Log.d(Config.LOGTAG, String.format("%s: ignoring proceed because we were not initializing", id.account.getJid().asBareJid()));
781 }
782 } else if (from.asBareJid().equals(id.account.getJid().asBareJid())) {
783 if (transition(State.ACCEPTED)) {
784 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": moved session with " + id.with + " into state accepted after received carbon copied procced");
785 this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
786 this.finish();
787 }
788 } else {
789 Log.d(Config.LOGTAG, String.format("%s: ignoring proceed from %s. was expected from %s", id.account.getJid().asBareJid(), from, id.with));
790 }
791 }
792
793 private void receiveRetract(final Jid from, final String serverMsgId, final long timestamp) {
794 if (from.equals(id.with)) {
795 final State target = this.state == State.PROCEED ? State.RETRACTED_RACED : State.RETRACTED;
796 if (transition(target)) {
797 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
798 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": session with " + id.with + " has been retracted (serverMsgId=" + serverMsgId + ")");
799 if (serverMsgId != null) {
800 this.message.setServerMsgId(serverMsgId);
801 }
802 this.message.setTime(timestamp);
803 if (target == State.RETRACTED) {
804 this.message.markUnread();
805 }
806 writeLogMessageMissed();
807 finish();
808 } else {
809 Log.d(Config.LOGTAG, "ignoring retract because already in " + this.state);
810 }
811 } else {
812 //TODO parse retract from self
813 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received retract from " + from + ". expected retract from" + id.with + ". ignoring");
814 }
815 }
816
817 public void sendSessionInitiate() {
818 sendSessionInitiate(this.proposedMedia, State.SESSION_INITIALIZED);
819 }
820
821 private void sendSessionInitiate(final Set<Media> media, final State targetState) {
822 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": prepare session-initiate");
823 discoverIceServers(iceServers -> sendSessionInitiate(media, targetState, iceServers));
824 }
825
826 private synchronized void sendSessionInitiate(final Set<Media> media, final State targetState, final List<PeerConnection.IceServer> iceServers) {
827 if (isTerminated()) {
828 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": ICE servers got discovered when session was already terminated. nothing to do.");
829 return;
830 }
831 try {
832 setupWebRTC(media, iceServers);
833 } catch (final WebRTCWrapper.InitializationException e) {
834 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
835 webRTCWrapper.close();
836 sendRetract(Reason.ofThrowable(e));
837 return;
838 }
839 try {
840 org.webrtc.SessionDescription webRTCSessionDescription = this.webRTCWrapper.setLocalDescription().get();
841 prepareSessionInitiate(webRTCSessionDescription, targetState);
842 } catch (final Exception e) {
843 //TODO sending the error text is worthwhile as well. Especially for FailureToSet exceptions
844 failureToInitiateSession(e, targetState);
845 }
846 }
847
848 private void failureToInitiateSession(final Throwable throwable, final State targetState) {
849 if (isTerminated()) {
850 return;
851 }
852 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to sendSessionInitiate", Throwables.getRootCause(throwable));
853 webRTCWrapper.close();
854 final Reason reason = Reason.ofThrowable(throwable);
855 if (isInState(targetState)) {
856 sendSessionTerminate(reason);
857 } else {
858 sendRetract(reason);
859 }
860 }
861
862 private void sendRetract(final Reason reason) {
863 //TODO embed reason into retract
864 sendJingleMessage("retract", id.with.asBareJid());
865 transitionOrThrow(reasonToState(reason));
866 this.finish();
867 }
868
869 private void prepareSessionInitiate(final org.webrtc.SessionDescription webRTCSessionDescription, final State targetState) {
870 final SessionDescription sessionDescription = SessionDescription.parse(webRTCSessionDescription.description);
871 final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription);
872 this.initiatorRtpContentMap = rtpContentMap;
873 final ListenableFuture<RtpContentMap> outgoingContentMapFuture = encryptSessionInitiate(rtpContentMap);
874 Futures.addCallback(outgoingContentMapFuture, new FutureCallback<RtpContentMap>() {
875 @Override
876 public void onSuccess(final RtpContentMap outgoingContentMap) {
877 sendSessionInitiate(outgoingContentMap, targetState);
878 }
879
880 @Override
881 public void onFailure(@NonNull final Throwable throwable) {
882 failureToInitiateSession(throwable, targetState);
883 }
884 }, MoreExecutors.directExecutor());
885 }
886
887 private void sendSessionInitiate(final RtpContentMap rtpContentMap, final State targetState) {
888 if (isTerminated()) {
889 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": preparing session was too slow. already terminated. nothing to do.");
890 return;
891 }
892 this.transitionOrThrow(targetState);
893 final JinglePacket sessionInitiate = rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_INITIATE, id.sessionId);
894 send(sessionInitiate);
895 }
896
897 private ListenableFuture<RtpContentMap> encryptSessionInitiate(final RtpContentMap rtpContentMap) {
898 if (this.omemoVerification.hasDeviceId()) {
899 final ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>> verifiedPayloadFuture = id.account.getAxolotlService()
900 .encrypt(rtpContentMap, id.with, omemoVerification.getDeviceId());
901 final ListenableFuture<RtpContentMap> future = Futures.transform(verifiedPayloadFuture, verifiedPayload -> {
902 omemoVerification.setSessionFingerprint(verifiedPayload.getFingerprint());
903 return verifiedPayload.getPayload();
904 }, MoreExecutors.directExecutor());
905 if (Config.REQUIRE_RTP_VERIFICATION) {
906 return future;
907 }
908 return Futures.catching(
909 future,
910 CryptoFailedException.class,
911 e -> {
912 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to use OMEMO DTLS verification on outgoing session initiate. falling back", e);
913 return rtpContentMap;
914 },
915 MoreExecutors.directExecutor()
916 );
917 } else {
918 return Futures.immediateFuture(rtpContentMap);
919 }
920 }
921
922 private void sendSessionTerminate(final Reason reason) {
923 sendSessionTerminate(reason, null);
924 }
925
926 private void sendSessionTerminate(final Reason reason, final String text) {
927 final State previous = this.state;
928 final State target = reasonToState(reason);
929 transitionOrThrow(target);
930 if (previous != State.NULL) {
931 writeLogMessage(target);
932 }
933 final JinglePacket jinglePacket = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
934 jinglePacket.setReason(reason, text);
935 Log.d(Config.LOGTAG, jinglePacket.toString());
936 send(jinglePacket);
937 finish();
938 }
939
940 private void sendTransportInfo(final String contentName, IceUdpTransportInfo.Candidate candidate) {
941 final RtpContentMap transportInfo;
942 try {
943 final RtpContentMap rtpContentMap = isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
944 transportInfo = rtpContentMap.transportInfo(contentName, candidate);
945 } catch (final Exception e) {
946 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to prepare transport-info from candidate for content=" + contentName);
947 return;
948 }
949 final JinglePacket jinglePacket = transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
950 send(jinglePacket);
951 }
952
953 private void send(final JinglePacket jinglePacket) {
954 jinglePacket.setTo(id.with);
955 xmppConnectionService.sendIqPacket(id.account, jinglePacket, this::handleIqResponse);
956 }
957
958 private synchronized void handleIqResponse(final Account account, final IqPacket response) {
959 if (response.getType() == IqPacket.TYPE.ERROR) {
960 final String errorCondition = response.getErrorCondition();
961 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ-error from " + response.getFrom() + " in RTP session. " + errorCondition);
962 if (isTerminated()) {
963 Log.i(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring error because session was already terminated");
964 return;
965 }
966 this.webRTCWrapper.close();
967 final State target;
968 if (Arrays.asList(
969 "service-unavailable",
970 "recipient-unavailable",
971 "remote-server-not-found",
972 "remote-server-timeout"
973 ).contains(errorCondition)) {
974 target = State.TERMINATED_CONNECTIVITY_ERROR;
975 } else {
976 target = State.TERMINATED_APPLICATION_FAILURE;
977 }
978 transitionOrThrow(target);
979 this.finish();
980 } else if (response.getType() == IqPacket.TYPE.TIMEOUT) {
981 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": received IQ timeout in RTP session with " + id.with + ". terminating with connectivity error");
982 if (isTerminated()) {
983 Log.i(Config.LOGTAG, id.account.getJid().asBareJid() + ": ignoring error because session was already terminated");
984 return;
985 }
986 this.webRTCWrapper.close();
987 transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
988 this.finish();
989 }
990 }
991
992 private void terminateWithOutOfOrder(final JinglePacket jinglePacket) {
993 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": terminating session with out-of-order");
994 this.webRTCWrapper.close();
995 transitionOrThrow(State.TERMINATED_APPLICATION_FAILURE);
996 respondWithOutOfOrder(jinglePacket);
997 this.finish();
998 }
999
1000 private void respondWithOutOfOrder(final JinglePacket jinglePacket) {
1001 jingleConnectionManager.respondWithJingleError(id.account, jinglePacket, "out-of-order", "unexpected-request", "wait");
1002 }
1003
1004 private void respondOk(final JinglePacket jinglePacket) {
1005 xmppConnectionService.sendIqPacket(id.account, jinglePacket.generateResponse(IqPacket.TYPE.RESULT), null);
1006 }
1007
1008 public void throwStateTransitionException() {
1009 final StateTransitionException exception = this.stateTransitionException;
1010 if (exception != null) {
1011 throw new IllegalStateException(String.format("Transition to %s did not call finish", exception.state), exception);
1012 }
1013 }
1014
1015 public RtpEndUserState getEndUserState() {
1016 switch (this.state) {
1017 case NULL:
1018 case PROPOSED:
1019 case SESSION_INITIALIZED:
1020 if (isInitiator()) {
1021 return RtpEndUserState.RINGING;
1022 } else {
1023 return RtpEndUserState.INCOMING_CALL;
1024 }
1025 case PROCEED:
1026 if (isInitiator()) {
1027 return RtpEndUserState.RINGING;
1028 } else {
1029 return RtpEndUserState.ACCEPTING_CALL;
1030 }
1031 case SESSION_INITIALIZED_PRE_APPROVED:
1032 if (isInitiator()) {
1033 return RtpEndUserState.RINGING;
1034 } else {
1035 return RtpEndUserState.CONNECTING;
1036 }
1037 case SESSION_ACCEPTED:
1038 //TODO refactor this out into separate method (that uses switch for better readability)
1039 final PeerConnection.PeerConnectionState state;
1040 try {
1041 state = webRTCWrapper.getState();
1042 } catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
1043 //We usually close the WebRTCWrapper *before* transitioning so we might still
1044 //be in SESSION_ACCEPTED even though the peerConnection has been torn down
1045 return RtpEndUserState.ENDING_CALL;
1046 }
1047 if (state == PeerConnection.PeerConnectionState.CONNECTED) {
1048 return RtpEndUserState.CONNECTED;
1049 } else if (state == PeerConnection.PeerConnectionState.NEW || state == PeerConnection.PeerConnectionState.CONNECTING) {
1050 return RtpEndUserState.CONNECTING;
1051 } else if (state == PeerConnection.PeerConnectionState.CLOSED) {
1052 return RtpEndUserState.ENDING_CALL;
1053 } else {
1054 return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1055 }
1056 case REJECTED:
1057 case REJECTED_RACED:
1058 case TERMINATED_DECLINED_OR_BUSY:
1059 if (isInitiator()) {
1060 return RtpEndUserState.DECLINED_OR_BUSY;
1061 } else {
1062 return RtpEndUserState.ENDED;
1063 }
1064 case TERMINATED_SUCCESS:
1065 case ACCEPTED:
1066 case RETRACTED:
1067 case TERMINATED_CANCEL_OR_TIMEOUT:
1068 return RtpEndUserState.ENDED;
1069 case RETRACTED_RACED:
1070 if (isInitiator()) {
1071 return RtpEndUserState.ENDED;
1072 } else {
1073 return RtpEndUserState.RETRACTED;
1074 }
1075 case TERMINATED_CONNECTIVITY_ERROR:
1076 return rtpConnectionStarted == 0 ? RtpEndUserState.CONNECTIVITY_ERROR : RtpEndUserState.CONNECTIVITY_LOST_ERROR;
1077 case TERMINATED_APPLICATION_FAILURE:
1078 return RtpEndUserState.APPLICATION_ERROR;
1079 case TERMINATED_SECURITY_ERROR:
1080 return RtpEndUserState.SECURITY_ERROR;
1081 }
1082 throw new IllegalStateException(String.format("%s has no equivalent EndUserState", this.state));
1083 }
1084
1085 public Set<Media> getMedia() {
1086 final State current = getState();
1087 if (current == State.NULL) {
1088 if (isInitiator()) {
1089 return Preconditions.checkNotNull(
1090 this.proposedMedia,
1091 "RTP connection has not been initialized properly"
1092 );
1093 }
1094 throw new IllegalStateException("RTP connection has not been initialized yet");
1095 }
1096 if (Arrays.asList(State.PROPOSED, State.PROCEED).contains(current)) {
1097 return Preconditions.checkNotNull(
1098 this.proposedMedia,
1099 "RTP connection has not been initialized properly"
1100 );
1101 }
1102 final RtpContentMap initiatorContentMap = initiatorRtpContentMap;
1103 if (initiatorContentMap != null) {
1104 return initiatorContentMap.getMedia();
1105 } else if (isTerminated()) {
1106 return Collections.emptySet(); //we might fail before we ever got a chance to set media
1107 } else {
1108 return Preconditions.checkNotNull(this.proposedMedia, "RTP connection has not been initialized properly");
1109 }
1110 }
1111
1112
1113 public boolean isVerified() {
1114 final String fingerprint = this.omemoVerification.getFingerprint();
1115 if (fingerprint == null) {
1116 return false;
1117 }
1118 final FingerprintStatus status = id.account.getAxolotlService().getFingerprintTrust(fingerprint);
1119 return status != null && status.isVerified();
1120 }
1121
1122 public synchronized void acceptCall() {
1123 switch (this.state) {
1124 case PROPOSED:
1125 cancelRingingTimeout();
1126 acceptCallFromProposed();
1127 break;
1128 case SESSION_INITIALIZED:
1129 cancelRingingTimeout();
1130 acceptCallFromSessionInitialized();
1131 break;
1132 case ACCEPTED:
1133 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted with another client. UI was just lagging behind");
1134 break;
1135 case PROCEED:
1136 case SESSION_ACCEPTED:
1137 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": the call has already been accepted. user probably double tapped the UI");
1138 break;
1139 default:
1140 throw new IllegalStateException("Can not accept call from " + this.state);
1141 }
1142 }
1143
1144
1145 public void notifyPhoneCall() {
1146 Log.d(Config.LOGTAG, "a phone call has just been started. killing jingle rtp connections");
1147 if (Arrays.asList(State.PROPOSED, State.SESSION_INITIALIZED).contains(this.state)) {
1148 rejectCall();
1149 } else {
1150 endCall();
1151 }
1152 }
1153
1154 public synchronized void rejectCall() {
1155 if (isTerminated()) {
1156 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received rejectCall() when session has already been terminated. nothing to do");
1157 return;
1158 }
1159 switch (this.state) {
1160 case PROPOSED:
1161 rejectCallFromProposed();
1162 break;
1163 case SESSION_INITIALIZED:
1164 rejectCallFromSessionInitiate();
1165 break;
1166 default:
1167 throw new IllegalStateException("Can not reject call from " + this.state);
1168 }
1169 }
1170
1171 public synchronized void endCall() {
1172 if (isTerminated()) {
1173 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": received endCall() when session has already been terminated. nothing to do");
1174 return;
1175 }
1176 if (isInState(State.PROPOSED) && !isInitiator()) {
1177 rejectCallFromProposed();
1178 return;
1179 }
1180 if (isInState(State.PROCEED)) {
1181 if (isInitiator()) {
1182 retractFromProceed();
1183 } else {
1184 rejectCallFromProceed();
1185 }
1186 return;
1187 }
1188 if (isInitiator() && isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED)) {
1189 this.webRTCWrapper.close();
1190 sendSessionTerminate(Reason.CANCEL);
1191 return;
1192 }
1193 if (isInState(State.SESSION_INITIALIZED)) {
1194 rejectCallFromSessionInitiate();
1195 return;
1196 }
1197 if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
1198 this.webRTCWrapper.close();
1199 sendSessionTerminate(Reason.SUCCESS);
1200 return;
1201 }
1202 if (isInState(State.TERMINATED_APPLICATION_FAILURE, State.TERMINATED_CONNECTIVITY_ERROR, State.TERMINATED_DECLINED_OR_BUSY)) {
1203 Log.d(Config.LOGTAG, "ignoring request to end call because already in state " + this.state);
1204 return;
1205 }
1206 throw new IllegalStateException("called 'endCall' while in state " + this.state + ". isInitiator=" + isInitiator());
1207 }
1208
1209 private void retractFromProceed() {
1210 Log.d(Config.LOGTAG, "retract from proceed");
1211 this.sendJingleMessage("retract");
1212 closeTransitionLogFinish(State.RETRACTED_RACED);
1213 }
1214
1215 private void closeTransitionLogFinish(final State state) {
1216 this.webRTCWrapper.close();
1217 transitionOrThrow(state);
1218 writeLogMessage(state);
1219 finish();
1220 }
1221
1222 private void setupWebRTC(final Set<Media> media, final List<PeerConnection.IceServer> iceServers) throws WebRTCWrapper.InitializationException {
1223 this.jingleConnectionManager.ensureConnectionIsRegistered(this);
1224 final AppRTCAudioManager.SpeakerPhonePreference speakerPhonePreference;
1225 if (media.contains(Media.VIDEO)) {
1226 speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.SPEAKER;
1227 } else {
1228 speakerPhonePreference = AppRTCAudioManager.SpeakerPhonePreference.EARPIECE;
1229 }
1230 this.webRTCWrapper.setup(this.xmppConnectionService, speakerPhonePreference);
1231 this.webRTCWrapper.initializePeerConnection(media, iceServers);
1232 }
1233
1234 private void acceptCallFromProposed() {
1235 transitionOrThrow(State.PROCEED);
1236 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1237 this.sendJingleMessage("accept", id.account.getJid().asBareJid());
1238 this.sendJingleMessage("proceed");
1239 }
1240
1241 private void rejectCallFromProposed() {
1242 transitionOrThrow(State.REJECTED);
1243 writeLogMessageMissed();
1244 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1245 this.sendJingleMessage("reject");
1246 finish();
1247 }
1248
1249 private void rejectCallFromProceed() {
1250 this.sendJingleMessage("reject");
1251 closeTransitionLogFinish(State.REJECTED_RACED);
1252 }
1253
1254 private void rejectCallFromSessionInitiate() {
1255 webRTCWrapper.close();
1256 sendSessionTerminate(Reason.DECLINE);
1257 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1258 }
1259
1260 private void sendJingleMessage(final String action) {
1261 sendJingleMessage(action, id.with);
1262 }
1263
1264 private void sendJingleMessage(final String action, final Jid to) {
1265 final MessagePacket messagePacket = new MessagePacket();
1266 messagePacket.setType(MessagePacket.TYPE_CHAT); //we want to carbon copy those
1267 messagePacket.setTo(to);
1268 final Element intent = messagePacket.addChild(action, Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
1269 if ("proceed".equals(action)) {
1270 messagePacket.setId(JINGLE_MESSAGE_PROCEED_ID_PREFIX + id.sessionId);
1271 if (isOmemoEnabled()) {
1272 final int deviceId = id.account.getAxolotlService().getOwnDeviceId();
1273 final Element device = intent.addChild("device", Namespace.OMEMO_DTLS_SRTP_VERIFICATION);
1274 device.setAttribute("id", deviceId);
1275 }
1276 }
1277 messagePacket.addChild("store", "urn:xmpp:hints");
1278 xmppConnectionService.sendMessagePacket(id.account, messagePacket);
1279 }
1280
1281 private boolean isOmemoEnabled() {
1282 final Conversational conversational = message.getConversation();
1283 if (conversational instanceof Conversation) {
1284 return ((Conversation) conversational).getNextEncryption() == Message.ENCRYPTION_AXOLOTL;
1285 }
1286 return false;
1287 }
1288
1289 private void acceptCallFromSessionInitialized() {
1290 xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
1291 sendSessionAccept();
1292 }
1293
1294 private synchronized boolean isInState(State... state) {
1295 return Arrays.asList(state).contains(this.state);
1296 }
1297
1298 private boolean transition(final State target) {
1299 return transition(target, null);
1300 }
1301
1302 private synchronized boolean transition(final State target, final Runnable runnable) {
1303 final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
1304 if (validTransitions != null && validTransitions.contains(target)) {
1305 this.state = target;
1306 this.stateTransitionException = new StateTransitionException(target);
1307 if (runnable != null) {
1308 runnable.run();
1309 }
1310 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
1311 updateEndUserState();
1312 updateOngoingCallNotification();
1313 return true;
1314 } else {
1315 return false;
1316 }
1317 }
1318
1319 void transitionOrThrow(final State target) {
1320 if (!transition(target)) {
1321 throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
1322 }
1323 }
1324
1325 @Override
1326 public void onIceCandidate(final IceCandidate iceCandidate) {
1327 final IceUdpTransportInfo.Candidate candidate = IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp);
1328 Log.d(Config.LOGTAG, "sending candidate: " + iceCandidate.toString());
1329 sendTransportInfo(iceCandidate.sdpMid, candidate);
1330 }
1331
1332 @Override
1333 public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
1334 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": PeerConnectionState changed to " + newState);
1335 if (newState == PeerConnection.PeerConnectionState.CONNECTED && this.rtpConnectionStarted == 0) {
1336 this.rtpConnectionStarted = SystemClock.elapsedRealtime();
1337 }
1338 if (newState == PeerConnection.PeerConnectionState.CLOSED && this.rtpConnectionEnded == 0) {
1339 this.rtpConnectionEnded = SystemClock.elapsedRealtime();
1340 }
1341 //TODO 'failed' means we need to restart ICE
1342 //
1343 //TODO 'disconnected' can probably be ignored as "This is a less stringent test than failed
1344 // and may trigger intermittently and resolve just as spontaneously on less reliable networks,
1345 // or during temporary disconnections. When the problem resolves, the connection may return
1346 // to the connected state."
1347 // Obviously the UI needs to reflect this new state with a 'reconnecting' display or something
1348 if (Arrays.asList(PeerConnection.PeerConnectionState.FAILED, PeerConnection.PeerConnectionState.DISCONNECTED).contains(newState)) {
1349 if (isTerminated()) {
1350 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": not sending session-terminate after connectivity error because session is already in state " + this.state);
1351 return;
1352 }
1353 new Thread(this::closeWebRTCSessionAfterFailedConnection).start();
1354 } else {
1355 updateEndUserState();
1356 }
1357 }
1358
1359 private void closeWebRTCSessionAfterFailedConnection() {
1360 this.webRTCWrapper.close();
1361 synchronized (this) {
1362 if (isTerminated()) {
1363 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": no need to send session-terminate after failed connection. Other party already did");
1364 return;
1365 }
1366 sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
1367 }
1368 }
1369
1370 public long getRtpConnectionStarted() {
1371 return this.rtpConnectionStarted;
1372 }
1373
1374 public long getRtpConnectionEnded() {
1375 return this.rtpConnectionEnded;
1376 }
1377
1378 public AppRTCAudioManager getAudioManager() {
1379 return webRTCWrapper.getAudioManager();
1380 }
1381
1382 public boolean isMicrophoneEnabled() {
1383 return webRTCWrapper.isMicrophoneEnabled();
1384 }
1385
1386 public boolean setMicrophoneEnabled(final boolean enabled) {
1387 return webRTCWrapper.setMicrophoneEnabled(enabled);
1388 }
1389
1390 public boolean isVideoEnabled() {
1391 return webRTCWrapper.isVideoEnabled();
1392 }
1393
1394 public void setVideoEnabled(final boolean enabled) {
1395 webRTCWrapper.setVideoEnabled(enabled);
1396 }
1397
1398 public boolean isCameraSwitchable() {
1399 return webRTCWrapper.isCameraSwitchable();
1400 }
1401
1402 public boolean isFrontCamera() {
1403 return webRTCWrapper.isFrontCamera();
1404 }
1405
1406 public ListenableFuture<Boolean> switchCamera() {
1407 return webRTCWrapper.switchCamera();
1408 }
1409
1410 @Override
1411 public void onAudioDeviceChanged(AppRTCAudioManager.AudioDevice selectedAudioDevice, Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
1412 xmppConnectionService.notifyJingleRtpConnectionUpdate(selectedAudioDevice, availableAudioDevices);
1413 }
1414
1415 private void updateEndUserState() {
1416 final RtpEndUserState endUserState = getEndUserState();
1417 jingleConnectionManager.toneManager.transition(isInitiator(), endUserState, getMedia());
1418 xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1419 }
1420
1421 private void updateOngoingCallNotification() {
1422 if (STATES_SHOWING_ONGOING_CALL.contains(this.state)) {
1423 xmppConnectionService.setOngoingCall(id, getMedia());
1424 } else {
1425 xmppConnectionService.removeOngoingCall();
1426 }
1427 }
1428
1429 private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
1430 if (id.account.getXmppConnection().getFeatures().externalServiceDiscovery()) {
1431 final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
1432 request.setTo(id.account.getDomain());
1433 request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1434 xmppConnectionService.sendIqPacket(id.account, request, (account, response) -> {
1435 ImmutableList.Builder<PeerConnection.IceServer> listBuilder = new ImmutableList.Builder<>();
1436 if (response.getType() == IqPacket.TYPE.RESULT) {
1437 final Element services = response.findChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
1438 final List<Element> children = services == null ? Collections.emptyList() : services.getChildren();
1439 for (final Element child : children) {
1440 if ("service".equals(child.getName())) {
1441 final String type = child.getAttribute("type");
1442 final String host = child.getAttribute("host");
1443 final String sport = child.getAttribute("port");
1444 final Integer port = sport == null ? null : Ints.tryParse(sport);
1445 final String transport = child.getAttribute("transport");
1446 final String username = child.getAttribute("username");
1447 final String password = child.getAttribute("password");
1448 if (Strings.isNullOrEmpty(host) || port == null) {
1449 continue;
1450 }
1451 if (port < 0 || port > 65535) {
1452 continue;
1453 }
1454 if (Arrays.asList("stun", "stuns", "turn", "turns").contains(type) && Arrays.asList("udp", "tcp").contains(transport)) {
1455 if (Arrays.asList("stuns", "turns").contains(type) && "udp".equals(transport)) {
1456 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping invalid combination of udp/tls in external services");
1457 continue;
1458 }
1459 final PeerConnection.IceServer.Builder iceServerBuilder = PeerConnection.IceServer
1460 .builder(String.format("%s:%s:%s?transport=%s", type, IP.wrapIPv6(host), port, transport));
1461 iceServerBuilder.setTlsCertPolicy(PeerConnection.TlsCertPolicy.TLS_CERT_POLICY_INSECURE_NO_CHECK);
1462 if (username != null && password != null) {
1463 iceServerBuilder.setUsername(username);
1464 iceServerBuilder.setPassword(password);
1465 } else if (Arrays.asList("turn", "turns").contains(type)) {
1466 //The WebRTC spec requires throwing an InvalidAccessError when username (from libwebrtc source coder)
1467 //https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
1468 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": skipping " + type + "/" + transport + " without username and password");
1469 continue;
1470 }
1471 final PeerConnection.IceServer iceServer = iceServerBuilder.createIceServer();
1472 Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": discovered ICE Server: " + iceServer);
1473 listBuilder.add(iceServer);
1474 }
1475 }
1476 }
1477 }
1478 final List<PeerConnection.IceServer> iceServers = listBuilder.build();
1479 if (iceServers.size() == 0) {
1480 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": no ICE server found " + response);
1481 }
1482 onIceServersDiscovered.onIceServersDiscovered(iceServers);
1483 });
1484 } else {
1485 Log.w(Config.LOGTAG, id.account.getJid().asBareJid() + ": has no external service discovery");
1486 onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
1487 }
1488 }
1489
1490 private void finish() {
1491 if (isTerminated()) {
1492 this.cancelRingingTimeout();
1493 this.webRTCWrapper.verifyClosed();
1494 this.jingleConnectionManager.setTerminalSessionState(id, getEndUserState(), getMedia());
1495 this.jingleConnectionManager.finishConnectionOrThrow(this);
1496 } else {
1497 throw new IllegalStateException(String.format("Unable to call finish from %s", this.state));
1498 }
1499 }
1500
1501 private void writeLogMessage(final State state) {
1502 final long started = this.rtpConnectionStarted;
1503 long duration = started <= 0 ? 0 : SystemClock.elapsedRealtime() - started;
1504 if (state == State.TERMINATED_SUCCESS || (state == State.TERMINATED_CONNECTIVITY_ERROR && duration > 0)) {
1505 writeLogMessageSuccess(duration);
1506 } else {
1507 writeLogMessageMissed();
1508 }
1509 }
1510
1511 private void writeLogMessageSuccess(final long duration) {
1512 this.message.setBody(new RtpSessionStatus(true, duration).toString());
1513 this.writeMessage();
1514 }
1515
1516 private void writeLogMessageMissed() {
1517 this.message.setBody(new RtpSessionStatus(false, 0).toString());
1518 this.writeMessage();
1519 }
1520
1521 private void writeMessage() {
1522 final Conversational conversational = message.getConversation();
1523 if (conversational instanceof Conversation) {
1524 ((Conversation) conversational).add(this.message);
1525 xmppConnectionService.createMessageAsync(message);
1526 xmppConnectionService.updateConversationUi();
1527 } else {
1528 throw new IllegalStateException("Somehow the conversation in a message was a stub");
1529 }
1530 }
1531
1532 public State getState() {
1533 return this.state;
1534 }
1535
1536 boolean isTerminated() {
1537 return TERMINATED.contains(this.state);
1538 }
1539
1540 public Optional<VideoTrack> getLocalVideoTrack() {
1541 return webRTCWrapper.getLocalVideoTrack();
1542 }
1543
1544 public Optional<VideoTrack> getRemoteVideoTrack() {
1545 return webRTCWrapper.getRemoteVideoTrack();
1546 }
1547
1548
1549 public EglBase.Context getEglBaseContext() {
1550 return webRTCWrapper.getEglBaseContext();
1551 }
1552
1553 void setProposedMedia(final Set<Media> media) {
1554 this.proposedMedia = media;
1555 }
1556
1557 public void fireStateUpdate() {
1558 final RtpEndUserState endUserState = getEndUserState();
1559 xmppConnectionService.notifyJingleRtpConnectionUpdate(id.account, id.with, id.sessionId, endUserState);
1560 }
1561
1562 private interface OnIceServersDiscovered {
1563 void onIceServersDiscovered(List<PeerConnection.IceServer> iceServers);
1564 }
1565
1566 private static class StateTransitionException extends Exception {
1567 private final State state;
1568
1569 private StateTransitionException(final State state) {
1570 this.state = state;
1571 }
1572 }
1573}