2020-04-02 09:30:16 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2021-05-06 16:40:35 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
import com.google.common.base.Joiner;
|
2020-04-14 17:06:39 +00:00
|
|
|
import com.google.common.base.Optional;
|
2020-04-15 08:49:38 +00:00
|
|
|
import com.google.common.base.Preconditions;
|
2021-11-11 15:52:18 +00:00
|
|
|
import com.google.common.base.Stopwatch;
|
2020-04-09 11:27:13 +00:00
|
|
|
import com.google.common.base.Strings;
|
2020-04-29 06:51:38 +00:00
|
|
|
import com.google.common.base.Throwables;
|
2020-04-15 08:49:38 +00:00
|
|
|
import com.google.common.collect.Collections2;
|
2020-04-02 19:12:38 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
import com.google.common.collect.ImmutableMap;
|
2023-10-10 16:42:23 +00:00
|
|
|
import com.google.common.collect.ImmutableMultimap;
|
2022-11-28 07:59:23 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2023-10-10 16:42:23 +00:00
|
|
|
import com.google.common.collect.Iterables;
|
2023-10-03 10:55:44 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2020-04-15 08:49:38 +00:00
|
|
|
import com.google.common.collect.Sets;
|
2020-04-09 11:27:13 +00:00
|
|
|
import com.google.common.primitives.Ints;
|
2021-05-06 16:40:35 +00:00
|
|
|
import com.google.common.util.concurrent.FutureCallback;
|
|
|
|
import com.google.common.util.concurrent.Futures;
|
2020-05-02 15:15:46 +00:00
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
2021-05-06 16:40:35 +00:00
|
|
|
import com.google.common.util.concurrent.MoreExecutors;
|
2020-04-03 08:46:42 +00:00
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
import eu.siacs.conversations.BuildConfig;
|
2020-04-02 09:30:16 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2021-03-02 20:13:49 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
|
|
|
|
import eu.siacs.conversations.crypto.axolotl.CryptoFailedException;
|
2021-03-03 08:41:05 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
|
2020-04-17 08:56:27 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2022-12-12 09:15:10 +00:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2020-04-12 15:12:59 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.Conversational;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2022-12-12 09:15:10 +00:00
|
|
|
import eu.siacs.conversations.entities.Presence;
|
2020-04-12 15:12:59 +00:00
|
|
|
import eu.siacs.conversations.entities.RtpSessionStatus;
|
2022-12-12 09:15:10 +00:00
|
|
|
import eu.siacs.conversations.entities.ServiceDiscoveryResult;
|
2020-04-13 10:02:34 +00:00
|
|
|
import eu.siacs.conversations.services.AppRTCAudioManager;
|
2020-05-08 15:52:41 +00:00
|
|
|
import eu.siacs.conversations.utils.IP;
|
2020-04-02 14:29:33 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2020-04-02 19:12:38 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2020-06-22 16:07:27 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2022-11-28 07:59:23 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
|
2020-04-06 08:26:29 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Group;
|
2020-04-03 08:46:42 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.IceUdpTransportInfo;
|
2020-04-02 09:30:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
2021-03-02 20:13:49 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Proceed;
|
2020-04-15 08:49:38 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Propose;
|
2020-04-07 19:26:51 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
|
2020-04-15 08:49:38 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.RtpDescription;
|
2020-04-08 15:52:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
2020-04-02 19:12:38 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2020-04-02 09:30:16 +00:00
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
import org.webrtc.EglBase;
|
|
|
|
import org.webrtc.IceCandidate;
|
|
|
|
import org.webrtc.PeerConnection;
|
|
|
|
import org.webrtc.VideoTrack;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Queue;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
public class JingleRtpConnection extends AbstractJingleConnection
|
|
|
|
implements WebRTCWrapper.EventCallback {
|
2020-04-02 09:30:16 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
public static final List<State> STATES_SHOWING_ONGOING_CALL =
|
|
|
|
Arrays.asList(
|
|
|
|
State.PROCEED, State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED);
|
2020-04-29 08:36:54 +00:00
|
|
|
private static final long BUSY_TIME_OUT = 30;
|
2022-02-25 15:24:13 +00:00
|
|
|
private static final List<State> TERMINATED =
|
|
|
|
Arrays.asList(
|
|
|
|
State.ACCEPTED,
|
|
|
|
State.REJECTED,
|
|
|
|
State.REJECTED_RACED,
|
|
|
|
State.RETRACTED,
|
|
|
|
State.RETRACTED_RACED,
|
|
|
|
State.TERMINATED_SUCCESS,
|
|
|
|
State.TERMINATED_DECLINED_OR_BUSY,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR,
|
|
|
|
State.TERMINATED_CANCEL_OR_TIMEOUT,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR);
|
2020-04-12 07:59:32 +00:00
|
|
|
|
2020-04-02 19:12:38 +00:00
|
|
|
private static final Map<State, Collection<State>> VALID_TRANSITIONS;
|
|
|
|
|
|
|
|
static {
|
2022-02-25 15:24:13 +00:00
|
|
|
final ImmutableMap.Builder<State, Collection<State>> transitionBuilder =
|
|
|
|
new ImmutableMap.Builder<>();
|
|
|
|
transitionBuilder.put(
|
|
|
|
State.NULL,
|
|
|
|
ImmutableList.of(
|
|
|
|
State.PROPOSED,
|
|
|
|
State.SESSION_INITIALIZED,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR));
|
|
|
|
transitionBuilder.put(
|
2020-04-09 13:22:03 +00:00
|
|
|
State.PROPOSED,
|
2022-02-25 15:24:13 +00:00
|
|
|
ImmutableList.of(
|
|
|
|
State.ACCEPTED,
|
|
|
|
State.PROCEED,
|
|
|
|
State.REJECTED,
|
|
|
|
State.RETRACTED,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR // only used when the xmpp connection
|
|
|
|
// rebinds
|
|
|
|
));
|
|
|
|
transitionBuilder.put(
|
2020-04-09 13:22:03 +00:00
|
|
|
State.PROCEED,
|
2022-02-25 15:24:13 +00:00
|
|
|
ImmutableList.of(
|
|
|
|
State.REJECTED_RACED,
|
|
|
|
State.RETRACTED_RACED,
|
|
|
|
State.SESSION_INITIALIZED_PRE_APPROVED,
|
|
|
|
State.TERMINATED_SUCCESS,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR // at this state used for error
|
|
|
|
// bounces of the proceed message
|
|
|
|
));
|
|
|
|
transitionBuilder.put(
|
|
|
|
State.SESSION_INITIALIZED,
|
|
|
|
ImmutableList.of(
|
|
|
|
State.SESSION_ACCEPTED,
|
|
|
|
State.TERMINATED_SUCCESS,
|
|
|
|
State.TERMINATED_DECLINED_OR_BUSY,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR, // at this state used for IQ errors
|
|
|
|
// and IQ timeouts
|
|
|
|
State.TERMINATED_CANCEL_OR_TIMEOUT,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR));
|
|
|
|
transitionBuilder.put(
|
2020-04-09 13:22:03 +00:00
|
|
|
State.SESSION_INITIALIZED_PRE_APPROVED,
|
2022-02-25 15:24:13 +00:00
|
|
|
ImmutableList.of(
|
|
|
|
State.SESSION_ACCEPTED,
|
|
|
|
State.TERMINATED_SUCCESS,
|
|
|
|
State.TERMINATED_DECLINED_OR_BUSY,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR, // at this state used for IQ errors
|
|
|
|
// and IQ timeouts
|
|
|
|
State.TERMINATED_CANCEL_OR_TIMEOUT,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR));
|
|
|
|
transitionBuilder.put(
|
2020-04-09 13:22:03 +00:00
|
|
|
State.SESSION_ACCEPTED,
|
2022-02-25 15:24:13 +00:00
|
|
|
ImmutableList.of(
|
|
|
|
State.TERMINATED_SUCCESS,
|
|
|
|
State.TERMINATED_DECLINED_OR_BUSY,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR,
|
|
|
|
State.TERMINATED_CANCEL_OR_TIMEOUT,
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_SECURITY_ERROR));
|
2020-04-02 19:12:38 +00:00
|
|
|
VALID_TRANSITIONS = transitionBuilder.build();
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:01:17 +00:00
|
|
|
private final WebRTCWrapper webRTCWrapper = new WebRTCWrapper(this);
|
2022-02-25 15:24:13 +00:00
|
|
|
private final Queue<Map.Entry<String, RtpContentMap.DescriptionTransport>>
|
|
|
|
pendingIceCandidates = new LinkedList<>();
|
2021-03-02 20:13:49 +00:00
|
|
|
private final OmemoVerification omemoVerification = new OmemoVerification();
|
2020-04-12 15:12:59 +00:00
|
|
|
private final Message message;
|
2020-04-06 11:01:17 +00:00
|
|
|
private State state = State.NULL;
|
2020-04-15 08:49:38 +00:00
|
|
|
private Set<Media> proposedMedia;
|
2020-04-06 11:01:17 +00:00
|
|
|
private RtpContentMap initiatorRtpContentMap;
|
|
|
|
private RtpContentMap responderRtpContentMap;
|
2022-11-28 07:59:23 +00:00
|
|
|
private RtpContentMap incomingContentAdd;
|
|
|
|
private RtpContentMap outgoingContentAdd;
|
2021-11-16 10:21:11 +00:00
|
|
|
private IceUdpTransportInfo.Setup peerDtlsSetup;
|
2021-11-11 15:52:18 +00:00
|
|
|
private final Stopwatch sessionDuration = Stopwatch.createUnstarted();
|
|
|
|
private final Queue<PeerConnection.PeerConnectionState> stateHistory = new LinkedList<>();
|
2020-04-22 19:59:20 +00:00
|
|
|
private ScheduledFuture<?> ringingTimeoutFuture;
|
2020-04-06 08:26:29 +00:00
|
|
|
|
2020-04-10 05:45:23 +00:00
|
|
|
JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id, Jid initiator) {
|
2020-04-03 06:16:55 +00:00
|
|
|
super(jingleConnectionManager, id, initiator);
|
2022-02-25 15:24:13 +00:00
|
|
|
final Conversation conversation =
|
|
|
|
jingleConnectionManager
|
|
|
|
.getXmppConnectionService()
|
|
|
|
.findOrCreateConversation(id.account, id.with.asBareJid(), false, false);
|
|
|
|
this.message =
|
|
|
|
new Message(
|
|
|
|
conversation,
|
|
|
|
isInitiator() ? Message.STATUS_SEND : Message.STATUS_RECEIVED,
|
|
|
|
Message.TYPE_RTP_SESSION,
|
|
|
|
id.sessionId);
|
2020-04-02 09:30:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:52:47 +00:00
|
|
|
private static State reasonToState(Reason reason) {
|
2023-11-13 11:54:55 +00:00
|
|
|
return switch (reason) {
|
|
|
|
case SUCCESS -> State.TERMINATED_SUCCESS;
|
|
|
|
case DECLINE, BUSY -> State.TERMINATED_DECLINED_OR_BUSY;
|
|
|
|
case CANCEL, TIMEOUT -> State.TERMINATED_CANCEL_OR_TIMEOUT;
|
|
|
|
case SECURITY_ERROR -> State.TERMINATED_SECURITY_ERROR;
|
|
|
|
case FAILED_APPLICATION, UNSUPPORTED_TRANSPORTS, UNSUPPORTED_APPLICATIONS -> State
|
|
|
|
.TERMINATED_APPLICATION_FAILURE;
|
|
|
|
default -> State.TERMINATED_CONNECTIVITY_ERROR;
|
|
|
|
};
|
2020-04-08 15:52:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 09:30:16 +00:00
|
|
|
@Override
|
2020-04-14 07:53:01 +00:00
|
|
|
synchronized void deliverPacket(final JinglePacket jinglePacket) {
|
2020-04-03 08:46:42 +00:00
|
|
|
switch (jinglePacket.getAction()) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case SESSION_INITIATE -> receiveSessionInitiate(jinglePacket);
|
|
|
|
case TRANSPORT_INFO -> receiveTransportInfo(jinglePacket);
|
|
|
|
case SESSION_ACCEPT -> receiveSessionAccept(jinglePacket);
|
|
|
|
case SESSION_TERMINATE -> receiveSessionTerminate(jinglePacket);
|
|
|
|
case CONTENT_ADD -> receiveContentAdd(jinglePacket);
|
|
|
|
case CONTENT_ACCEPT -> receiveContentAccept(jinglePacket);
|
|
|
|
case CONTENT_REJECT -> receiveContentReject(jinglePacket);
|
|
|
|
case CONTENT_REMOVE -> receiveContentRemove(jinglePacket);
|
|
|
|
case CONTENT_MODIFY -> receiveContentModify(jinglePacket);
|
|
|
|
default -> {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: received unhandled jingle action %s",
|
|
|
|
id.account.getJid().asBareJid(), jinglePacket.getAction()));
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:30:12 +00:00
|
|
|
@Override
|
2020-04-14 07:53:01 +00:00
|
|
|
synchronized void notifyRebound() {
|
2020-04-24 07:41:54 +00:00
|
|
|
if (isTerminated()) {
|
2020-04-13 16:30:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
webRTCWrapper.close();
|
2020-04-15 10:07:19 +00:00
|
|
|
if (!isInitiator() && isInState(State.PROPOSED, State.SESSION_INITIALIZED)) {
|
2020-04-13 16:30:12 +00:00
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
if (isInState(
|
|
|
|
State.SESSION_INITIALIZED,
|
|
|
|
State.SESSION_INITIALIZED_PRE_APPROVED,
|
|
|
|
State.SESSION_ACCEPTED)) {
|
|
|
|
// we might have already changed resources (full jid) at this point; so this might not
|
|
|
|
// even reach the other party
|
2020-04-13 16:30:12 +00:00
|
|
|
sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
|
|
|
|
} else {
|
|
|
|
transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
|
2020-04-18 16:22:10 +00:00
|
|
|
finish();
|
2020-04-13 16:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:51 +00:00
|
|
|
private void receiveSessionTerminate(final JinglePacket jinglePacket) {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2020-04-12 07:59:32 +00:00
|
|
|
final JinglePacket.ReasonWrapper wrapper = jinglePacket.getReason();
|
2020-04-08 13:27:17 +00:00
|
|
|
final State previous = this.state;
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received session terminate reason="
|
|
|
|
+ wrapper.reason
|
|
|
|
+ "("
|
|
|
|
+ Strings.nullToEmpty(wrapper.text)
|
|
|
|
+ ") while in state "
|
|
|
|
+ previous);
|
2020-04-12 07:59:32 +00:00
|
|
|
if (TERMINATED.contains(previous)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ignoring session terminate because already in "
|
|
|
|
+ previous);
|
2020-04-12 07:59:32 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-08 13:27:17 +00:00
|
|
|
webRTCWrapper.close();
|
2020-04-12 15:12:59 +00:00
|
|
|
final State target = reasonToState(wrapper.reason);
|
|
|
|
transitionOrThrow(target);
|
|
|
|
writeLogMessage(target);
|
2020-04-08 13:27:17 +00:00
|
|
|
if (previous == State.PROPOSED || previous == State.SESSION_INITIALIZED) {
|
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
|
|
|
}
|
2020-04-18 16:22:10 +00:00
|
|
|
finish();
|
2020-04-08 13:27:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 08:26:29 +00:00
|
|
|
private void receiveTransportInfo(final JinglePacket jinglePacket) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// Due to the asynchronicity of processing session-init we might move from NULL|PROCEED to
|
|
|
|
// INITIALIZED only after transport-info has been received
|
|
|
|
if (isInState(
|
|
|
|
State.NULL,
|
|
|
|
State.PROCEED,
|
|
|
|
State.SESSION_INITIALIZED,
|
|
|
|
State.SESSION_INITIALIZED_PRE_APPROVED,
|
|
|
|
State.SESSION_ACCEPTED)) {
|
2020-04-06 08:26:29 +00:00
|
|
|
final RtpContentMap contentMap;
|
|
|
|
try {
|
|
|
|
contentMap = RtpContentMap.of(jinglePacket);
|
2021-11-14 17:22:18 +00:00
|
|
|
} catch (final IllegalArgumentException | NullPointerException e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": improperly formatted contents; ignoring",
|
|
|
|
e);
|
2021-11-14 17:22:18 +00:00
|
|
|
respondOk(jinglePacket);
|
2020-04-06 08:26:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-15 12:03:04 +00:00
|
|
|
receiveTransportInfo(jinglePacket, contentMap);
|
2020-04-29 08:36:54 +00:00
|
|
|
} else {
|
|
|
|
if (isTerminated()) {
|
|
|
|
respondOk(jinglePacket);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ignoring out-of-order transport info; we where already terminated");
|
2020-04-29 08:36:54 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received transport info while in state="
|
|
|
|
+ this.state);
|
2020-04-29 08:36:54 +00:00
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void receiveTransportInfo(
|
|
|
|
final JinglePacket jinglePacket, final RtpContentMap contentMap) {
|
|
|
|
final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> candidates =
|
|
|
|
contentMap.contents.entrySet();
|
2023-10-11 09:38:09 +00:00
|
|
|
final RtpContentMap remote = getRemoteContentMap();
|
2023-11-13 11:54:55 +00:00
|
|
|
final Set<String> remoteContentIds =
|
|
|
|
remote == null ? Collections.emptySet() : remote.contents.keySet();
|
2023-10-11 09:38:09 +00:00
|
|
|
if (Collections.disjoint(remoteContentIds, contentMap.contents.keySet())) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"received transport-info for unknown contents "
|
|
|
|
+ contentMap.contents.keySet()
|
|
|
|
+ " (known: "
|
|
|
|
+ remoteContentIds
|
|
|
|
+ ")");
|
2021-11-15 12:03:04 +00:00
|
|
|
respondOk(jinglePacket);
|
2023-10-11 09:38:09 +00:00
|
|
|
pendingIceCandidates.addAll(candidates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.state != State.SESSION_ACCEPTED) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(Config.LOGTAG, "received transport-info prematurely. adding to backlog");
|
2021-11-15 12:03:04 +00:00
|
|
|
respondOk(jinglePacket);
|
|
|
|
pendingIceCandidates.addAll(candidates);
|
2023-10-11 09:38:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// zero candidates + modified credentials are an ICE restart offer
|
|
|
|
if (checkForIceRestart(jinglePacket, contentMap)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
try {
|
|
|
|
processCandidates(candidates);
|
|
|
|
} catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
|
|
|
|
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");
|
2021-11-15 12:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
private void receiveContentAdd(final JinglePacket jinglePacket) {
|
|
|
|
final RtpContentMap modification;
|
|
|
|
try {
|
|
|
|
modification = RtpContentMap.of(jinglePacket);
|
|
|
|
modification.requireContentDescriptions();
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid() + ": improperly formatted contents",
|
|
|
|
Throwables.getRootCause(e));
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isInState(State.SESSION_ACCEPTED)) {
|
2023-10-09 11:17:14 +00:00
|
|
|
final boolean hasFullTransportInfo = modification.hasFullTransportInfo();
|
|
|
|
final ListenableFuture<RtpContentMap> future =
|
|
|
|
receiveRtpContentMap(
|
2023-11-13 11:54:55 +00:00
|
|
|
modification,
|
|
|
|
this.omemoVerification.hasFingerprint() && hasFullTransportInfo);
|
|
|
|
Futures.addCallback(
|
|
|
|
future,
|
|
|
|
new FutureCallback<>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap rtpContentMap) {
|
|
|
|
receiveContentAdd(jinglePacket, rtpContentMap);
|
|
|
|
}
|
2023-10-09 11:17:14 +00:00
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Throwable throwable) {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
final Throwable rootCause = Throwables.getRootCause(throwable);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": improperly formatted contents in content-add",
|
|
|
|
throwable);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.ofThrowable(rootCause), rootCause.getMessage());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2022-11-28 07:59:23 +00:00
|
|
|
} else {
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentAdd(
|
|
|
|
final JinglePacket jinglePacket, final RtpContentMap modification) {
|
|
|
|
final RtpContentMap remote = getRemoteContentMap();
|
|
|
|
if (!Collections.disjoint(modification.getNames(), remote.getNames())) {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
this.webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.FAILED_APPLICATION,
|
|
|
|
String.format(
|
|
|
|
"contents with names %s already exists",
|
|
|
|
Joiner.on(", ").join(modification.getNames())));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final ContentAddition contentAddition =
|
|
|
|
ContentAddition.of(ContentAddition.Direction.INCOMING, modification);
|
|
|
|
|
|
|
|
final RtpContentMap outgoing = this.outgoingContentAdd;
|
|
|
|
final Set<ContentAddition.Summary> outgoingContentAddSummary =
|
|
|
|
outgoing == null ? Collections.emptySet() : ContentAddition.summary(outgoing);
|
|
|
|
|
|
|
|
if (outgoingContentAddSummary.equals(contentAddition.summary)) {
|
|
|
|
if (isInitiator()) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": respond with tie break to matching content-add offer");
|
|
|
|
respondWithTieBreak(jinglePacket);
|
|
|
|
} else {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": automatically accept matching content-add offer");
|
|
|
|
acceptContentAdd(contentAddition.summary, modification);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// once we can display multiple video tracks we can be more loose with this condition
|
|
|
|
// theoretically it should also be fine to automatically accept audio only contents
|
|
|
|
if (Media.audioOnly(remote.getMedia()) && Media.videoOnly(contentAddition.media())) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid() + ": received " + contentAddition);
|
|
|
|
this.incomingContentAdd = modification;
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
updateEndUserState();
|
|
|
|
} else {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
// TODO do we want to add a reason?
|
|
|
|
rejectContentAdd(modification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentAccept(final JinglePacket jinglePacket) {
|
|
|
|
final RtpContentMap receivedContentAccept;
|
|
|
|
try {
|
|
|
|
receivedContentAccept = RtpContentMap.of(jinglePacket);
|
|
|
|
receivedContentAccept.requireContentDescriptions();
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid() + ": improperly formatted contents",
|
|
|
|
Throwables.getRootCause(e));
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final RtpContentMap outgoingContentAdd = this.outgoingContentAdd;
|
|
|
|
if (outgoingContentAdd == null) {
|
|
|
|
Log.d(Config.LOGTAG, "received content-accept when we had no outgoing content add");
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Set<ContentAddition.Summary> ourSummary = ContentAddition.summary(outgoingContentAdd);
|
|
|
|
if (ourSummary.equals(ContentAddition.summary(receivedContentAccept))) {
|
|
|
|
this.outgoingContentAdd = null;
|
|
|
|
respondOk(jinglePacket);
|
2023-10-09 11:17:14 +00:00
|
|
|
final boolean hasFullTransportInfo = receivedContentAccept.hasFullTransportInfo();
|
|
|
|
final ListenableFuture<RtpContentMap> future =
|
|
|
|
receiveRtpContentMap(
|
2023-11-13 11:54:55 +00:00
|
|
|
receivedContentAccept,
|
|
|
|
this.omemoVerification.hasFingerprint() && hasFullTransportInfo);
|
|
|
|
Futures.addCallback(
|
|
|
|
future,
|
|
|
|
new FutureCallback<>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap result) {
|
|
|
|
receiveContentAccept(result);
|
|
|
|
}
|
2023-10-09 11:17:14 +00:00
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Throwable throwable) {
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.ofThrowable(throwable), throwable.getMessage());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2022-11-28 07:59:23 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, "received content-accept did not match our outgoing content-add");
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentAccept(final RtpContentMap receivedContentAccept) {
|
|
|
|
final IceUdpTransportInfo.Setup peerDtlsSetup = getPeerDtlsSetup();
|
|
|
|
final RtpContentMap modifiedContentMap =
|
|
|
|
getRemoteContentMap().addContent(receivedContentAccept, peerDtlsSetup);
|
|
|
|
|
|
|
|
setRemoteContentMap(modifiedContentMap);
|
|
|
|
|
|
|
|
final SessionDescription answer = SessionDescription.of(modifiedContentMap, !isInitiator());
|
|
|
|
|
|
|
|
final org.webrtc.SessionDescription sdp =
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.ANSWER, answer.toString());
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.webRTCWrapper.setRemoteDescription(sdp).get();
|
|
|
|
} catch (final Exception e) {
|
|
|
|
final Throwable cause = Throwables.getRootCause(e);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": unable to set remote description after receiving content-accept",
|
|
|
|
cause);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, cause.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": remote has accepted content-add "
|
|
|
|
+ ContentAddition.summary(receivedContentAccept));
|
2023-10-13 06:29:23 +00:00
|
|
|
processCandidates(receivedContentAccept.contents.entrySet());
|
|
|
|
updateEndUserState();
|
2022-11-28 07:59:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 10:55:44 +00:00
|
|
|
private void receiveContentModify(final JinglePacket jinglePacket) {
|
2023-10-15 06:51:06 +00:00
|
|
|
if (this.state != State.SESSION_ACCEPTED) {
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
return;
|
|
|
|
}
|
2023-10-03 10:55:44 +00:00
|
|
|
final Map<String, Content.Senders> modification =
|
|
|
|
Maps.transformEntries(
|
|
|
|
jinglePacket.getJingleContents(), (key, value) -> value.getSenders());
|
2023-10-10 16:42:23 +00:00
|
|
|
final boolean isInitiator = isInitiator();
|
2023-10-05 14:23:43 +00:00
|
|
|
final RtpContentMap currentOutgoing = this.outgoingContentAdd;
|
2023-10-10 16:42:23 +00:00
|
|
|
final RtpContentMap remoteContentMap = this.getRemoteContentMap();
|
2023-11-13 11:54:55 +00:00
|
|
|
final Set<String> currentOutgoingMediaIds =
|
|
|
|
currentOutgoing == null
|
|
|
|
? Collections.emptySet()
|
|
|
|
: currentOutgoing.contents.keySet();
|
2023-10-03 10:55:44 +00:00
|
|
|
Log.d(Config.LOGTAG, "receiveContentModification(" + modification + ")");
|
2023-10-05 14:23:43 +00:00
|
|
|
if (currentOutgoing != null && currentOutgoingMediaIds.containsAll(modification.keySet())) {
|
2023-10-10 16:42:23 +00:00
|
|
|
respondOk(jinglePacket);
|
2023-10-05 14:23:43 +00:00
|
|
|
final RtpContentMap modifiedContentMap;
|
|
|
|
try {
|
2023-11-13 11:54:55 +00:00
|
|
|
modifiedContentMap =
|
|
|
|
currentOutgoing.modifiedSendersChecked(isInitiator, modification);
|
2023-10-05 14:23:43 +00:00
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.outgoingContentAdd = modifiedContentMap;
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": processed content-modification for pending content-add");
|
|
|
|
} else if (remoteContentMap != null
|
|
|
|
&& remoteContentMap.contents.keySet().containsAll(modification.keySet())) {
|
2023-10-10 16:42:23 +00:00
|
|
|
respondOk(jinglePacket);
|
|
|
|
final RtpContentMap modifiedRemoteContentMap;
|
|
|
|
try {
|
2023-11-13 11:54:55 +00:00
|
|
|
modifiedRemoteContentMap =
|
|
|
|
remoteContentMap.modifiedSendersChecked(isInitiator, modification);
|
2023-10-10 16:42:23 +00:00
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final SessionDescription offer;
|
|
|
|
try {
|
|
|
|
offer = SessionDescription.of(modifiedRemoteContentMap, !isInitiator());
|
|
|
|
} catch (final IllegalArgumentException | NullPointerException e) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": unable convert offer from content-modify to SDP",
|
|
|
|
e);
|
2023-10-10 16:42:23 +00:00
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": auto accepting content-modification");
|
2023-10-10 16:42:23 +00:00
|
|
|
this.autoAcceptContentModify(modifiedRemoteContentMap, offer);
|
2023-10-05 14:23:43 +00:00
|
|
|
} else {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(Config.LOGTAG, "received unsupported content modification " + modification);
|
2023-10-10 16:42:23 +00:00
|
|
|
respondWithItemNotFound(jinglePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
private void autoAcceptContentModify(
|
|
|
|
final RtpContentMap modifiedRemoteContentMap, final SessionDescription offer) {
|
2023-10-10 16:42:23 +00:00
|
|
|
this.setRemoteContentMap(modifiedRemoteContentMap);
|
|
|
|
final org.webrtc.SessionDescription sdp =
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.OFFER, offer.toString());
|
|
|
|
try {
|
|
|
|
this.webRTCWrapper.setRemoteDescription(sdp).get();
|
|
|
|
// auto accept is only done when we already have tracks
|
|
|
|
final SessionDescription answer = setLocalSessionDescription();
|
|
|
|
final RtpContentMap rtpContentMap = RtpContentMap.of(answer, isInitiator());
|
|
|
|
modifyLocalContentMap(rtpContentMap);
|
2023-11-13 11:54:55 +00:00
|
|
|
// we do not need to send an answer but do we have to resend the candidates currently in
|
|
|
|
// SDP?
|
|
|
|
// resendCandidatesFromSdp(answer);
|
2023-10-10 16:42:23 +00:00
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to accept content add", Throwables.getRootCause(e));
|
2023-10-05 14:23:43 +00:00
|
|
|
webRTCWrapper.close();
|
2023-10-10 16:42:23 +00:00
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION);
|
2023-10-05 14:23:43 +00:00
|
|
|
}
|
2023-10-03 10:55:44 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
private static ImmutableMultimap<String, IceUdpTransportInfo.Candidate> parseCandidates(
|
|
|
|
final SessionDescription answer) {
|
|
|
|
final ImmutableMultimap.Builder<String, IceUdpTransportInfo.Candidate> candidateBuilder =
|
|
|
|
new ImmutableMultimap.Builder<>();
|
|
|
|
for (final SessionDescription.Media media : answer.media) {
|
2023-10-10 16:42:23 +00:00
|
|
|
final String mid = Iterables.getFirst(media.attributes.get("mid"), null);
|
|
|
|
if (Strings.isNullOrEmpty(mid)) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
for (final String sdpCandidate : media.attributes.get("candidate")) {
|
|
|
|
final IceUdpTransportInfo.Candidate candidate =
|
|
|
|
IceUdpTransportInfo.Candidate.fromSdpAttributeValue(sdpCandidate, null);
|
2023-10-10 16:42:23 +00:00
|
|
|
if (candidate != null) {
|
2023-11-13 11:54:55 +00:00
|
|
|
candidateBuilder.put(mid, candidate);
|
2023-10-10 16:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-13 11:36:20 +00:00
|
|
|
return candidateBuilder.build();
|
2023-10-10 16:42:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
private void receiveContentReject(final JinglePacket jinglePacket) {
|
|
|
|
final RtpContentMap receivedContentReject;
|
|
|
|
try {
|
|
|
|
receivedContentReject = RtpContentMap.of(jinglePacket);
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid() + ": improperly formatted contents",
|
|
|
|
Throwables.getRootCause(e));
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
this.webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final RtpContentMap outgoingContentAdd = this.outgoingContentAdd;
|
|
|
|
if (outgoingContentAdd == null) {
|
|
|
|
Log.d(Config.LOGTAG, "received content-reject when we had no outgoing content add");
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Set<ContentAddition.Summary> ourSummary = ContentAddition.summary(outgoingContentAdd);
|
|
|
|
if (ourSummary.equals(ContentAddition.summary(receivedContentReject))) {
|
|
|
|
this.outgoingContentAdd = null;
|
|
|
|
respondOk(jinglePacket);
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(Config.LOGTAG, jinglePacket.toString());
|
2022-11-28 07:59:23 +00:00
|
|
|
receiveContentReject(ourSummary);
|
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, "received content-reject did not match our outgoing content-add");
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentReject(final Set<ContentAddition.Summary> summary) {
|
|
|
|
try {
|
|
|
|
this.webRTCWrapper.removeTrack(Media.VIDEO);
|
|
|
|
final RtpContentMap localContentMap = customRollback();
|
|
|
|
modifyLocalContentMap(localContentMap);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
final Throwable cause = Throwables.getRootCause(e);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": unable to rollback local description after receiving content-reject",
|
|
|
|
cause);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, cause.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": remote has rejected our content-add "
|
|
|
|
+ summary);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentRemove(final JinglePacket jinglePacket) {
|
|
|
|
final RtpContentMap receivedContentRemove;
|
|
|
|
try {
|
|
|
|
receivedContentRemove = RtpContentMap.of(jinglePacket);
|
|
|
|
receivedContentRemove.requireContentDescriptions();
|
|
|
|
} catch (final RuntimeException e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid() + ": improperly formatted contents",
|
|
|
|
Throwables.getRootCause(e));
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
this.webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
receiveContentRemove(receivedContentRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveContentRemove(final RtpContentMap receivedContentRemove) {
|
|
|
|
final RtpContentMap incomingContentAdd = this.incomingContentAdd;
|
|
|
|
final Set<ContentAddition.Summary> contentAddSummary =
|
|
|
|
incomingContentAdd == null
|
|
|
|
? Collections.emptySet()
|
|
|
|
: ContentAddition.summary(incomingContentAdd);
|
|
|
|
final Set<ContentAddition.Summary> removeSummary =
|
|
|
|
ContentAddition.summary(receivedContentRemove);
|
|
|
|
if (contentAddSummary.equals(removeSummary)) {
|
|
|
|
this.incomingContentAdd = null;
|
|
|
|
updateEndUserState();
|
|
|
|
} else {
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.FAILED_APPLICATION,
|
|
|
|
String.format(
|
|
|
|
"%s only supports %s as a means to retract a not yet accepted %s",
|
|
|
|
BuildConfig.APP_NAME,
|
|
|
|
JinglePacket.Action.CONTENT_REMOVE,
|
2023-10-05 14:23:43 +00:00
|
|
|
JinglePacket.Action.CONTENT_ADD));
|
2022-11-28 07:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void retractContentAdd() {
|
|
|
|
final RtpContentMap outgoingContentAdd = this.outgoingContentAdd;
|
|
|
|
if (outgoingContentAdd == null) {
|
|
|
|
throw new IllegalStateException("Not outgoing content add");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
webRTCWrapper.removeTrack(Media.VIDEO);
|
|
|
|
final RtpContentMap localContentMap = customRollback();
|
|
|
|
modifyLocalContentMap(localContentMap);
|
|
|
|
} catch (final Exception e) {
|
|
|
|
final Throwable cause = Throwables.getRootCause(e);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": unable to rollback local description after trying to retract content-add",
|
|
|
|
cause);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, cause.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.outgoingContentAdd = null;
|
|
|
|
final JinglePacket retract =
|
|
|
|
outgoingContentAdd
|
|
|
|
.toStub()
|
|
|
|
.toJinglePacket(JinglePacket.Action.CONTENT_REMOVE, id.sessionId);
|
|
|
|
this.send(retract);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid()
|
|
|
|
+ ": retract content-add "
|
|
|
|
+ ContentAddition.summary(outgoingContentAdd));
|
|
|
|
}
|
|
|
|
|
|
|
|
private RtpContentMap customRollback() throws ExecutionException, InterruptedException {
|
|
|
|
final SessionDescription sdp = setLocalSessionDescription();
|
|
|
|
final RtpContentMap localRtpContentMap = RtpContentMap.of(sdp, isInitiator());
|
|
|
|
final SessionDescription answer = generateFakeResponse(localRtpContentMap);
|
|
|
|
this.webRTCWrapper
|
|
|
|
.setRemoteDescription(
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.ANSWER, answer.toString()))
|
|
|
|
.get();
|
|
|
|
return localRtpContentMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private SessionDescription generateFakeResponse(final RtpContentMap localContentMap) {
|
|
|
|
final RtpContentMap currentRemote = getRemoteContentMap();
|
|
|
|
final RtpContentMap.Diff diff = currentRemote.diff(localContentMap);
|
|
|
|
if (diff.isEmpty()) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"Unexpected rollback condition. No difference between local and remote");
|
|
|
|
}
|
|
|
|
final RtpContentMap patch = localContentMap.toContentModification(diff.added);
|
|
|
|
if (ImmutableSet.of(Content.Senders.NONE).equals(patch.getSenders())) {
|
|
|
|
final RtpContentMap nextRemote =
|
|
|
|
currentRemote.addContent(
|
|
|
|
patch.modifiedSenders(Content.Senders.NONE), getPeerDtlsSetup());
|
|
|
|
return SessionDescription.of(nextRemote, !isInitiator());
|
|
|
|
}
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"Unexpected rollback condition. Senders were not uniformly none");
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
public synchronized void acceptContentAdd(
|
|
|
|
@NonNull final Set<ContentAddition.Summary> contentAddition) {
|
2022-11-28 07:59:23 +00:00
|
|
|
final RtpContentMap incomingContentAdd = this.incomingContentAdd;
|
|
|
|
if (incomingContentAdd == null) {
|
|
|
|
throw new IllegalStateException("No incoming content add");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contentAddition.equals(ContentAddition.summary(incomingContentAdd))) {
|
|
|
|
this.incomingContentAdd = null;
|
2023-10-11 09:38:09 +00:00
|
|
|
final Set<Content.Senders> senders = incomingContentAdd.getSenders();
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(Config.LOGTAG, "senders of incoming content-add: " + senders);
|
2023-10-11 09:38:09 +00:00
|
|
|
if (senders.equals(Content.Senders.receiveOnly(isInitiator()))) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"content addition is receive only. we want to upgrade to 'both'");
|
|
|
|
final RtpContentMap modifiedSenders =
|
|
|
|
incomingContentAdd.modifiedSenders(Content.Senders.BOTH);
|
|
|
|
final JinglePacket proposedContentModification =
|
|
|
|
modifiedSenders
|
|
|
|
.toStub()
|
|
|
|
.toJinglePacket(JinglePacket.Action.CONTENT_MODIFY, id.sessionId);
|
2023-10-11 09:38:09 +00:00
|
|
|
proposedContentModification.setTo(id.with);
|
2023-11-13 11:54:55 +00:00
|
|
|
xmppConnectionService.sendIqPacket(
|
|
|
|
id.account,
|
|
|
|
proposedContentModification,
|
|
|
|
(account, response) -> {
|
|
|
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": remote has accepted our upgrade to senders=both");
|
|
|
|
acceptContentAdd(
|
|
|
|
ContentAddition.summary(modifiedSenders), modifiedSenders);
|
|
|
|
} else {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": remote has rejected our upgrade to senders=both");
|
|
|
|
acceptContentAdd(contentAddition, incomingContentAdd);
|
|
|
|
}
|
|
|
|
});
|
2023-11-13 12:46:15 +00:00
|
|
|
} else {
|
|
|
|
acceptContentAdd(contentAddition, incomingContentAdd);
|
2023-10-11 09:38:09 +00:00
|
|
|
}
|
2022-11-28 07:59:23 +00:00
|
|
|
} else {
|
2023-11-13 11:54:55 +00:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Accepted content add does not match pending content-add");
|
2022-11-28 07:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
private void acceptContentAdd(
|
|
|
|
@NonNull final Set<ContentAddition.Summary> contentAddition,
|
|
|
|
final RtpContentMap incomingContentAdd) {
|
2022-11-28 07:59:23 +00:00
|
|
|
final IceUdpTransportInfo.Setup setup = getPeerDtlsSetup();
|
2023-11-13 11:54:55 +00:00
|
|
|
final RtpContentMap modifiedContentMap =
|
|
|
|
getRemoteContentMap().addContent(incomingContentAdd, setup);
|
2022-11-28 07:59:23 +00:00
|
|
|
this.setRemoteContentMap(modifiedContentMap);
|
|
|
|
|
|
|
|
final SessionDescription offer;
|
|
|
|
try {
|
|
|
|
offer = SessionDescription.of(modifiedContentMap, !isInitiator());
|
|
|
|
} catch (final IllegalArgumentException | NullPointerException e) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": unable convert offer from content-add to SDP",
|
|
|
|
e);
|
2022-11-28 07:59:23 +00:00
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.incomingContentAdd = null;
|
|
|
|
acceptContentAdd(contentAddition, offer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void acceptContentAdd(
|
|
|
|
final Set<ContentAddition.Summary> contentAddition, final SessionDescription offer) {
|
|
|
|
final org.webrtc.SessionDescription sdp =
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.OFFER, offer.toString());
|
|
|
|
try {
|
|
|
|
this.webRTCWrapper.setRemoteDescription(sdp).get();
|
|
|
|
|
|
|
|
// TODO add tracks for 'media' where contentAddition.senders matches
|
|
|
|
|
|
|
|
// TODO if senders.sending(isInitiator())
|
|
|
|
|
|
|
|
this.webRTCWrapper.addTrack(Media.VIDEO);
|
|
|
|
|
|
|
|
// TODO add additional transceivers for recv only cases
|
|
|
|
|
|
|
|
final SessionDescription answer = setLocalSessionDescription();
|
|
|
|
final RtpContentMap rtpContentMap = RtpContentMap.of(answer, isInitiator());
|
|
|
|
|
|
|
|
final RtpContentMap contentAcceptMap =
|
|
|
|
rtpContentMap.toContentModification(
|
|
|
|
Collections2.transform(contentAddition, ca -> ca.name));
|
2023-10-09 11:17:14 +00:00
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": sending content-accept "
|
|
|
|
+ ContentAddition.summary(contentAcceptMap));
|
2023-10-11 09:38:09 +00:00
|
|
|
|
|
|
|
addIceCandidatesFromBlackLog();
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
modifyLocalContentMap(rtpContentMap);
|
2023-11-13 11:54:55 +00:00
|
|
|
final ListenableFuture<RtpContentMap> future =
|
|
|
|
prepareOutgoingContentMap(contentAcceptMap);
|
2023-10-09 11:17:14 +00:00
|
|
|
Futures.addCallback(
|
|
|
|
future,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2023-10-09 11:17:14 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap rtpContentMap) {
|
|
|
|
sendContentAccept(rtpContentMap);
|
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Throwable throwable) {
|
|
|
|
failureToPerformAction(JinglePacket.Action.CONTENT_ACCEPT, throwable);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2022-11-28 07:59:23 +00:00
|
|
|
} catch (final Exception e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to accept content add", Throwables.getRootCause(e));
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendContentAccept(final RtpContentMap contentAcceptMap) {
|
2023-11-13 11:54:55 +00:00
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
contentAcceptMap.toJinglePacket(JinglePacket.Action.CONTENT_ACCEPT, id.sessionId);
|
2022-11-28 07:59:23 +00:00
|
|
|
send(jinglePacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void rejectContentAdd() {
|
|
|
|
final RtpContentMap incomingContentAdd = this.incomingContentAdd;
|
|
|
|
if (incomingContentAdd == null) {
|
|
|
|
throw new IllegalStateException("No incoming content add");
|
|
|
|
}
|
|
|
|
this.incomingContentAdd = null;
|
|
|
|
updateEndUserState();
|
|
|
|
rejectContentAdd(incomingContentAdd);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void rejectContentAdd(final RtpContentMap contentMap) {
|
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
contentMap
|
|
|
|
.toStub()
|
|
|
|
.toJinglePacket(JinglePacket.Action.CONTENT_REJECT, id.sessionId);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": rejecting content "
|
|
|
|
+ ContentAddition.summary(contentMap));
|
|
|
|
send(jinglePacket);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private boolean checkForIceRestart(
|
|
|
|
final JinglePacket jinglePacket, final RtpContentMap rtpContentMap) {
|
2021-11-14 17:22:18 +00:00
|
|
|
final RtpContentMap existing = getRemoteContentMap();
|
2022-02-25 16:26:36 +00:00
|
|
|
final Set<IceUdpTransportInfo.Credentials> existingCredentials;
|
2021-11-16 16:08:34 +00:00
|
|
|
final IceUdpTransportInfo.Credentials newCredentials;
|
|
|
|
try {
|
|
|
|
existingCredentials = existing.getCredentials();
|
2022-02-25 16:26:36 +00:00
|
|
|
newCredentials = rtpContentMap.getDistinctCredentials();
|
2021-11-16 16:08:34 +00:00
|
|
|
} catch (final IllegalStateException e) {
|
|
|
|
Log.d(Config.LOGTAG, "unable to gather credentials for comparison", e);
|
2021-11-14 17:22:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-25 16:26:36 +00:00
|
|
|
if (existingCredentials.contains(newCredentials)) {
|
2021-11-14 17:22:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
// TODO an alternative approach is to check if we already got an iq result to our
|
|
|
|
// ICE-restart
|
2021-11-17 09:49:16 +00:00
|
|
|
// and if that's the case we are seeing an answer.
|
|
|
|
// This might be more spec compliant but also more error prone potentially
|
2023-11-13 11:54:55 +00:00
|
|
|
final boolean isSignalStateStable =
|
|
|
|
this.webRTCWrapper.getSignalingState() == PeerConnection.SignalingState.STABLE;
|
2023-10-05 14:23:43 +00:00
|
|
|
// TODO a stable signal state can be another indicator that we have an offer to restart ICE
|
2021-11-14 17:22:18 +00:00
|
|
|
final boolean isOffer = rtpContentMap.emptyCandidates();
|
2021-11-15 20:49:31 +00:00
|
|
|
final RtpContentMap restartContentMap;
|
2021-11-14 17:22:18 +00:00
|
|
|
try {
|
2021-11-15 20:49:31 +00:00
|
|
|
if (isOffer) {
|
2021-11-16 16:08:34 +00:00
|
|
|
Log.d(Config.LOGTAG, "received offer to restart ICE " + newCredentials);
|
2022-02-25 15:24:13 +00:00
|
|
|
restartContentMap =
|
|
|
|
existing.modifiedCredentials(
|
|
|
|
newCredentials, IceUdpTransportInfo.Setup.ACTPASS);
|
2021-11-15 20:49:31 +00:00
|
|
|
} else {
|
|
|
|
final IceUdpTransportInfo.Setup setup = getPeerDtlsSetup();
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"received confirmation of ICE restart"
|
|
|
|
+ newCredentials
|
|
|
|
+ " peer_setup="
|
|
|
|
+ setup);
|
2021-11-15 20:49:31 +00:00
|
|
|
// DTLS setup attribute needs to be rewritten to reflect current peer state
|
|
|
|
// https://groups.google.com/g/discuss-webrtc/c/DfpIMwvUfeM
|
|
|
|
restartContentMap = existing.modifiedCredentials(newCredentials, setup);
|
|
|
|
}
|
2021-11-15 12:03:04 +00:00
|
|
|
if (applyIceRestart(jinglePacket, restartContentMap, isOffer)) {
|
|
|
|
return isOffer;
|
2021-11-14 17:22:18 +00:00
|
|
|
} else {
|
2021-11-16 16:08:34 +00:00
|
|
|
Log.d(Config.LOGTAG, "ignoring ICE restart. sending tie-break");
|
2021-11-15 12:03:04 +00:00
|
|
|
respondWithTieBreak(jinglePacket);
|
2021-11-14 17:22:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-15 16:18:43 +00:00
|
|
|
} catch (final Exception exception) {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
final Throwable rootCause = Throwables.getRootCause(exception);
|
2021-11-16 14:17:12 +00:00
|
|
|
if (rootCause instanceof WebRTCWrapper.PeerConnectionNotInitialized) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// If this happens a termination is already in progress
|
2021-11-17 09:49:16 +00:00
|
|
|
Log.d(Config.LOGTAG, "ignoring PeerConnectionNotInitialized on ICE restart");
|
2021-11-16 14:17:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-15 16:18:43 +00:00
|
|
|
Log.d(Config.LOGTAG, "failure to apply ICE restart", rootCause);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.ofThrowable(rootCause), rootCause.getMessage());
|
2021-11-14 17:22:18 +00:00
|
|
|
return true;
|
2020-06-22 16:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:49:31 +00:00
|
|
|
private IceUdpTransportInfo.Setup getPeerDtlsSetup() {
|
2021-11-16 10:21:11 +00:00
|
|
|
final IceUdpTransportInfo.Setup peerSetup = this.peerDtlsSetup;
|
|
|
|
if (peerSetup == null || peerSetup == IceUdpTransportInfo.Setup.ACTPASS) {
|
|
|
|
throw new IllegalStateException("Invalid peer setup");
|
2021-11-15 20:49:31 +00:00
|
|
|
}
|
2021-11-16 10:21:11 +00:00
|
|
|
return peerSetup;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void storePeerDtlsSetup(final IceUdpTransportInfo.Setup setup) {
|
|
|
|
if (setup == null || setup == IceUdpTransportInfo.Setup.ACTPASS) {
|
|
|
|
throw new IllegalArgumentException("Trying to store invalid peer dtls setup");
|
|
|
|
}
|
|
|
|
this.peerDtlsSetup = setup;
|
2021-11-15 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private boolean applyIceRestart(
|
|
|
|
final JinglePacket jinglePacket,
|
|
|
|
final RtpContentMap restartContentMap,
|
|
|
|
final boolean isOffer)
|
|
|
|
throws ExecutionException, InterruptedException {
|
2023-11-13 11:54:55 +00:00
|
|
|
final SessionDescription sessionDescription =
|
|
|
|
SessionDescription.of(restartContentMap, !isInitiator());
|
2022-02-25 15:24:13 +00:00
|
|
|
final org.webrtc.SessionDescription.Type type =
|
|
|
|
isOffer
|
|
|
|
? org.webrtc.SessionDescription.Type.OFFER
|
|
|
|
: org.webrtc.SessionDescription.Type.ANSWER;
|
|
|
|
org.webrtc.SessionDescription sdp =
|
|
|
|
new org.webrtc.SessionDescription(type, sessionDescription.toString());
|
2021-11-14 17:22:18 +00:00
|
|
|
if (isOffer && webRTCWrapper.getSignalingState() != PeerConnection.SignalingState.STABLE) {
|
|
|
|
if (isInitiator()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// We ignore the offer and respond with tie-break. This will clause the responder
|
|
|
|
// not to apply the content map
|
2021-11-14 17:22:18 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
webRTCWrapper.setRemoteDescription(sdp).get();
|
2021-11-16 10:21:11 +00:00
|
|
|
setRemoteContentMap(restartContentMap);
|
2021-11-14 17:22:18 +00:00
|
|
|
if (isOffer) {
|
|
|
|
final SessionDescription localSessionDescription = setLocalSessionDescription();
|
2022-11-20 16:00:40 +00:00
|
|
|
setLocalContentMap(RtpContentMap.of(localSessionDescription, isInitiator()));
|
2022-02-25 15:24:13 +00:00
|
|
|
// We need to respond OK before sending any candidates
|
2021-11-15 12:03:04 +00:00
|
|
|
respondOk(jinglePacket);
|
2021-11-14 17:22:18 +00:00
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
2021-11-16 10:21:11 +00:00
|
|
|
} else {
|
|
|
|
storePeerDtlsSetup(restartContentMap.getDtlsSetup());
|
2021-11-14 17:22:18 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void processCandidates(
|
|
|
|
final Set<Map.Entry<String, RtpContentMap.DescriptionTransport>> contents) {
|
2020-04-29 08:36:54 +00:00
|
|
|
for (final Map.Entry<String, RtpContentMap.DescriptionTransport> content : contents) {
|
2021-11-14 17:22:18 +00:00
|
|
|
processCandidate(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void processCandidate(
|
|
|
|
final Map.Entry<String, RtpContentMap.DescriptionTransport> content) {
|
2021-11-14 17:22:18 +00:00
|
|
|
final RtpContentMap rtpContentMap = getRemoteContentMap();
|
|
|
|
final List<String> indices = toIdentificationTags(rtpContentMap);
|
2022-02-25 15:24:13 +00:00
|
|
|
final String sdpMid = content.getKey(); // aka content name
|
2021-11-14 17:22:18 +00:00
|
|
|
final IceUdpTransportInfo transport = content.getValue().transport;
|
|
|
|
final IceUdpTransportInfo.Credentials credentials = transport.getCredentials();
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
// TODO check that credentials remained the same
|
2021-11-14 17:22:18 +00:00
|
|
|
|
|
|
|
for (final IceUdpTransportInfo.Candidate candidate : transport.getCandidates()) {
|
|
|
|
final String sdp;
|
|
|
|
try {
|
|
|
|
sdp = candidate.toSdpAttribute(credentials.ufrag);
|
|
|
|
} catch (final IllegalArgumentException e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ignoring invalid ICE candidate "
|
|
|
|
+ e.getMessage());
|
2021-11-14 17:22:18 +00:00
|
|
|
continue;
|
2020-04-06 08:26:29 +00:00
|
|
|
}
|
2021-11-14 17:22:18 +00:00
|
|
|
final int mLineIndex = indices.indexOf(sdpMid);
|
|
|
|
if (mLineIndex < 0) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"mLineIndex not found for " + sdpMid + ". available indices " + indices);
|
2021-11-14 17:22:18 +00:00
|
|
|
}
|
|
|
|
final IceCandidate iceCandidate = new IceCandidate(sdpMid, mLineIndex, sdp);
|
|
|
|
Log.d(Config.LOGTAG, "received candidate: " + iceCandidate);
|
|
|
|
this.webRTCWrapper.addIceCandidate(iceCandidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private RtpContentMap getRemoteContentMap() {
|
|
|
|
return isInitiator() ? this.responderRtpContentMap : this.initiatorRtpContentMap;
|
|
|
|
}
|
|
|
|
|
2022-11-21 08:10:01 +00:00
|
|
|
private RtpContentMap getLocalContentMap() {
|
|
|
|
return isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:22:18 +00:00
|
|
|
private List<String> toIdentificationTags(final RtpContentMap rtpContentMap) {
|
|
|
|
final Group originalGroup = rtpContentMap.group;
|
2022-02-25 15:24:13 +00:00
|
|
|
final List<String> identificationTags =
|
|
|
|
originalGroup == null
|
|
|
|
? rtpContentMap.getNames()
|
|
|
|
: originalGroup.getIdentificationTags();
|
2021-11-14 17:22:18 +00:00
|
|
|
if (identificationTags.size() == 0) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": no identification tags found in initial offer. we won't be able to calculate mLineIndices");
|
2020-04-06 08:26:29 +00:00
|
|
|
}
|
2021-11-14 17:22:18 +00:00
|
|
|
return identificationTags;
|
2020-04-06 08:26:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private ListenableFuture<RtpContentMap> receiveRtpContentMap(
|
|
|
|
final JinglePacket jinglePacket, final boolean expectVerification) {
|
2021-10-05 13:43:05 +00:00
|
|
|
try {
|
2023-10-09 11:17:14 +00:00
|
|
|
return receiveRtpContentMap(RtpContentMap.of(jinglePacket), expectVerification);
|
2021-10-05 13:43:05 +00:00
|
|
|
} catch (final Exception e) {
|
|
|
|
return Futures.immediateFailedFuture(e);
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private ListenableFuture<RtpContentMap> receiveRtpContentMap(
|
|
|
|
final RtpContentMap receivedContentMap, final boolean expectVerification) {
|
2023-10-09 11:17:14 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"receiveRtpContentMap("
|
|
|
|
+ receivedContentMap.getClass().getSimpleName()
|
|
|
|
+ ",expectVerification="
|
|
|
|
+ expectVerification
|
|
|
|
+ ")");
|
2021-03-02 20:13:49 +00:00
|
|
|
if (receivedContentMap instanceof OmemoVerifiedRtpContentMap) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final ListenableFuture<AxolotlService.OmemoVerifiedPayload<RtpContentMap>> future =
|
|
|
|
id.account
|
|
|
|
.getAxolotlService()
|
|
|
|
.decrypt((OmemoVerifiedRtpContentMap) receivedContentMap, id.with);
|
|
|
|
return Futures.transform(
|
|
|
|
future,
|
|
|
|
omemoVerifiedPayload -> {
|
|
|
|
// TODO test if an exception here triggers a correct abort
|
|
|
|
omemoVerification.setOrEnsureEqual(omemoVerifiedPayload);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received verifiable DTLS fingerprint via "
|
|
|
|
+ omemoVerification);
|
|
|
|
return omemoVerifiedPayload.getPayload();
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-05-07 20:55:20 +00:00
|
|
|
} else if (Config.REQUIRE_RTP_VERIFICATION || expectVerification) {
|
|
|
|
return Futures.immediateFailedFuture(
|
2022-02-25 15:24:13 +00:00
|
|
|
new SecurityException("DTLS fingerprint was unexpectedly not verifiable"));
|
2021-03-02 20:13:49 +00:00
|
|
|
} else {
|
2021-05-06 16:40:35 +00:00
|
|
|
return Futures.immediateFuture(receivedContentMap);
|
2021-03-02 20:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 08:46:42 +00:00
|
|
|
private void receiveSessionInitiate(final JinglePacket jinglePacket) {
|
|
|
|
if (isInitiator()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: received session-initiate even though we were initiating",
|
|
|
|
id.account.getJid().asBareJid()));
|
2021-04-08 08:23:37 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: got a reason to terminate with out-of-order. but already in state %s",
|
|
|
|
id.account.getJid().asBareJid(), getState()));
|
2021-04-08 08:23:37 +00:00
|
|
|
respondWithOutOfOrder(jinglePacket);
|
|
|
|
} else {
|
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
|
|
|
}
|
2020-04-03 08:46:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-06 16:40:35 +00:00
|
|
|
final ListenableFuture<RtpContentMap> future = receiveRtpContentMap(jinglePacket, false);
|
2022-02-25 15:24:13 +00:00
|
|
|
Futures.addCallback(
|
|
|
|
future,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
|
|
|
|
receiveSessionInitiate(jinglePacket, rtpContentMap);
|
|
|
|
}
|
2021-05-06 16:40:35 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Throwable throwable) {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-05-06 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void receiveSessionInitiate(
|
|
|
|
final JinglePacket jinglePacket, final RtpContentMap contentMap) {
|
2020-04-03 08:46:42 +00:00
|
|
|
try {
|
2020-04-06 08:26:29 +00:00
|
|
|
contentMap.requireContentDescriptions();
|
2021-11-17 09:49:16 +00:00
|
|
|
contentMap.requireDTLSFingerprint(true);
|
2020-04-22 14:35:08 +00:00
|
|
|
} catch (final RuntimeException e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": improperly formatted contents",
|
|
|
|
Throwables.getRootCause(e));
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2020-04-16 06:20:13 +00:00
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
2020-04-03 08:46:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"processing session-init with " + contentMap.contents.size() + " contents");
|
2020-04-08 13:27:17 +00:00
|
|
|
final State target;
|
|
|
|
if (this.state == State.PROCEED) {
|
2020-04-15 10:07:19 +00:00
|
|
|
Preconditions.checkState(
|
|
|
|
proposedMedia != null && proposedMedia.size() > 0,
|
2022-02-25 15:24:13 +00:00
|
|
|
"proposed media must be set when processing pre-approved session-initiate");
|
2020-04-15 10:07:19 +00:00
|
|
|
if (!this.proposedMedia.equals(contentMap.getMedia())) {
|
2022-02-25 15:24:13 +00:00
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.SECURITY_ERROR,
|
|
|
|
String.format(
|
|
|
|
"Your session proposal (Jingle Message Initiation) included media %s but your session-initiate was %s",
|
|
|
|
this.proposedMedia, contentMap.getMedia()));
|
2020-04-15 10:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-08 13:27:17 +00:00
|
|
|
target = State.SESSION_INITIALIZED_PRE_APPROVED;
|
|
|
|
} else {
|
|
|
|
target = State.SESSION_INITIALIZED;
|
|
|
|
}
|
2020-04-15 20:40:37 +00:00
|
|
|
if (transition(target, () -> this.initiatorRtpContentMap = contentMap)) {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2021-11-14 17:22:18 +00:00
|
|
|
pendingIceCandidates.addAll(contentMap.contents.entrySet());
|
2020-04-08 13:27:17 +00:00
|
|
|
if (target == State.SESSION_INITIALIZED_PRE_APPROVED) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": automatically accepting session-initiate");
|
2020-04-03 08:46:42 +00:00
|
|
|
sendSessionAccept();
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received not pre-approved session-initiate. start ringing");
|
2020-04-09 07:04:59 +00:00
|
|
|
startRinging();
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: received session-initiate while in state %s",
|
|
|
|
id.account.getJid().asBareJid(), state));
|
2020-04-10 05:07:22 +00:00
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
2020-04-02 09:30:16 +00:00
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
|
2020-04-06 13:45:06 +00:00
|
|
|
private void receiveSessionAccept(final JinglePacket jinglePacket) {
|
|
|
|
if (!isInitiator()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: received session-accept even though we were responding",
|
|
|
|
id.account.getJid().asBareJid()));
|
2020-04-10 05:07:22 +00:00
|
|
|
terminateWithOutOfOrder(jinglePacket);
|
2020-04-06 13:45:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final ListenableFuture<RtpContentMap> future =
|
|
|
|
receiveRtpContentMap(jinglePacket, this.omemoVerification.hasFingerprint());
|
|
|
|
Futures.addCallback(
|
|
|
|
future,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(@Nullable RtpContentMap rtpContentMap) {
|
|
|
|
receiveSessionAccept(jinglePacket, rtpContentMap);
|
|
|
|
}
|
2021-05-06 16:40:35 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Throwable throwable) {
|
|
|
|
respondOk(jinglePacket);
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": improperly formatted contents in session-accept",
|
|
|
|
throwable);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.ofThrowable(throwable), throwable.getMessage());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-05-06 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void receiveSessionAccept(
|
|
|
|
final JinglePacket jinglePacket, final RtpContentMap contentMap) {
|
2020-04-06 13:45:06 +00:00
|
|
|
try {
|
|
|
|
contentMap.requireContentDescriptions();
|
2020-04-10 05:45:23 +00:00
|
|
|
contentMap.requireDTLSFingerprint();
|
2020-04-22 14:35:08 +00:00
|
|
|
} catch (final RuntimeException e) {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": improperly formatted contents in session-accept",
|
|
|
|
e);
|
2020-04-09 13:22:03 +00:00
|
|
|
webRTCWrapper.close();
|
2020-04-16 06:20:13 +00:00
|
|
|
sendSessionTerminate(Reason.of(e), e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Set<Media> initiatorMedia = this.initiatorRtpContentMap.getMedia();
|
|
|
|
if (!initiatorMedia.equals(contentMap.getMedia())) {
|
2022-02-25 15:24:13 +00:00
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.SECURITY_ERROR,
|
|
|
|
String.format(
|
|
|
|
"Your session-included included media %s but our session-initiate was %s",
|
|
|
|
this.proposedMedia, contentMap.getMedia()));
|
2020-04-06 13:45:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"processing session-accept with " + contentMap.contents.size() + " contents");
|
2020-04-06 13:45:06 +00:00
|
|
|
if (transition(State.SESSION_ACCEPTED)) {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2020-04-06 13:45:06 +00:00
|
|
|
receiveSessionAccept(contentMap);
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: received session-accept while in state %s",
|
|
|
|
id.account.getJid().asBareJid(), state));
|
2020-04-09 13:22:03 +00:00
|
|
|
respondOk(jinglePacket);
|
2020-04-06 13:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveSessionAccept(final RtpContentMap contentMap) {
|
|
|
|
this.responderRtpContentMap = contentMap;
|
2021-11-16 10:21:11 +00:00
|
|
|
this.storePeerDtlsSetup(contentMap.getDtlsSetup());
|
2020-04-09 13:22:03 +00:00
|
|
|
final SessionDescription sessionDescription;
|
|
|
|
try {
|
2022-11-20 16:00:40 +00:00
|
|
|
sessionDescription = SessionDescription.of(contentMap, false);
|
2020-04-10 05:53:29 +00:00
|
|
|
} catch (final IllegalArgumentException | NullPointerException e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable convert offer from session-accept to SDP",
|
|
|
|
e);
|
2020-04-09 13:22:03 +00:00
|
|
|
webRTCWrapper.close();
|
2020-04-09 18:35:44 +00:00
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
2020-04-09 13:22:03 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final org.webrtc.SessionDescription answer =
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.ANSWER, sessionDescription.toString());
|
2020-04-06 13:45:06 +00:00
|
|
|
try {
|
|
|
|
this.webRTCWrapper.setRemoteDescription(answer).get();
|
2020-04-29 06:51:38 +00:00
|
|
|
} catch (final Exception e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable to set remote description after receiving session-accept",
|
|
|
|
Throwables.getRootCause(e));
|
2020-04-09 13:22:03 +00:00
|
|
|
webRTCWrapper.close();
|
2022-02-25 15:24:13 +00:00
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.FAILED_APPLICATION, Throwables.getRootCause(e).getMessage());
|
2020-08-22 06:11:21 +00:00
|
|
|
return;
|
2020-04-06 13:45:06 +00:00
|
|
|
}
|
2021-11-14 17:22:18 +00:00
|
|
|
processCandidates(contentMap.contents.entrySet());
|
2020-04-06 13:45:06 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 11:01:17 +00:00
|
|
|
private void sendSessionAccept() {
|
|
|
|
final RtpContentMap rtpContentMap = this.initiatorRtpContentMap;
|
|
|
|
if (rtpContentMap == null) {
|
2020-04-06 13:45:06 +00:00
|
|
|
throw new IllegalStateException("initiator RTP Content Map has not been set");
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
2020-04-09 05:38:12 +00:00
|
|
|
final SessionDescription offer;
|
|
|
|
try {
|
2022-11-20 16:00:40 +00:00
|
|
|
offer = SessionDescription.of(rtpContentMap, true);
|
2020-04-10 05:53:29 +00:00
|
|
|
} catch (final IllegalArgumentException | NullPointerException e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable convert offer from session-initiate to SDP",
|
|
|
|
e);
|
2020-04-09 13:22:03 +00:00
|
|
|
webRTCWrapper.close();
|
2020-04-09 18:35:44 +00:00
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
2020-04-09 05:38:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-15 10:07:19 +00:00
|
|
|
sendSessionAccept(rtpContentMap.getMedia(), offer);
|
2020-04-09 05:38:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 10:07:19 +00:00
|
|
|
private void sendSessionAccept(final Set<Media> media, final SessionDescription offer) {
|
|
|
|
discoverIceServers(iceServers -> sendSessionAccept(media, offer, iceServers));
|
2020-04-14 07:06:07 +00:00
|
|
|
}
|
2020-04-05 14:12:44 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private synchronized void sendSessionAccept(
|
|
|
|
final Set<Media> media,
|
|
|
|
final SessionDescription offer,
|
|
|
|
final List<PeerConnection.IceServer> iceServers) {
|
2020-04-24 07:41:54 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ICE servers got discovered when session was already terminated. nothing to do.");
|
2020-04-14 07:53:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-13 11:36:20 +00:00
|
|
|
final boolean includeCandidates = remoteHasSdpOfferAnswer();
|
2020-04-14 07:06:07 +00:00
|
|
|
try {
|
2023-11-13 11:36:20 +00:00
|
|
|
setupWebRTC(media, iceServers, !includeCandidates);
|
2020-05-01 11:56:24 +00:00
|
|
|
} catch (final WebRTCWrapper.InitializationException e) {
|
|
|
|
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
|
|
|
|
webRTCWrapper.close();
|
2022-08-01 08:14:49 +00:00
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, e.getMessage());
|
2020-04-14 07:06:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final org.webrtc.SessionDescription sdp =
|
|
|
|
new org.webrtc.SessionDescription(
|
|
|
|
org.webrtc.SessionDescription.Type.OFFER, offer.toString());
|
2020-04-14 07:06:07 +00:00
|
|
|
try {
|
|
|
|
this.webRTCWrapper.setRemoteDescription(sdp).get();
|
|
|
|
addIceCandidatesFromBlackLog();
|
2022-02-25 15:24:13 +00:00
|
|
|
org.webrtc.SessionDescription webRTCSessionDescription =
|
2023-11-13 11:36:20 +00:00
|
|
|
this.webRTCWrapper.setLocalDescription(includeCandidates).get();
|
|
|
|
prepareSessionAccept(webRTCSessionDescription, includeCandidates);
|
2020-04-29 06:51:38 +00:00
|
|
|
} catch (final Exception e) {
|
2021-05-08 06:45:31 +00:00
|
|
|
failureToAcceptSession(e);
|
2020-04-14 07:06:07 +00:00
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
}
|
2020-04-05 14:12:44 +00:00
|
|
|
|
2021-05-08 06:45:31 +00:00
|
|
|
private void failureToAcceptSession(final Throwable throwable) {
|
2021-05-08 09:50:18 +00:00
|
|
|
if (isTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-15 16:18:43 +00:00
|
|
|
final Throwable rootCause = Throwables.getRootCause(throwable);
|
|
|
|
Log.d(Config.LOGTAG, "unable to send session accept", rootCause);
|
2021-05-08 06:45:31 +00:00
|
|
|
webRTCWrapper.close();
|
2021-11-15 16:18:43 +00:00
|
|
|
sendSessionTerminate(Reason.ofThrowable(rootCause), rootCause.getMessage());
|
2021-05-08 06:45:31 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
private void failureToPerformAction(
|
|
|
|
final JinglePacket.Action action, final Throwable throwable) {
|
2023-10-09 11:17:14 +00:00
|
|
|
if (isTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Throwable rootCause = Throwables.getRootCause(throwable);
|
|
|
|
Log.d(Config.LOGTAG, "unable to send " + action, rootCause);
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.ofThrowable(rootCause), rootCause.getMessage());
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:29:07 +00:00
|
|
|
private void addIceCandidatesFromBlackLog() {
|
2021-11-14 17:22:18 +00:00
|
|
|
Map.Entry<String, RtpContentMap.DescriptionTransport> foo;
|
|
|
|
while ((foo = this.pendingIceCandidates.poll()) != null) {
|
|
|
|
processCandidate(foo);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": added candidate from back log");
|
2020-04-09 07:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void prepareSessionAccept(
|
2023-11-13 11:54:55 +00:00
|
|
|
final org.webrtc.SessionDescription webRTCSessionDescription,
|
|
|
|
final boolean includeCandidates) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final SessionDescription sessionDescription =
|
|
|
|
SessionDescription.parse(webRTCSessionDescription.description);
|
2022-11-20 16:00:40 +00:00
|
|
|
final RtpContentMap respondingRtpContentMap = RtpContentMap.of(sessionDescription, false);
|
2023-11-13 11:36:20 +00:00
|
|
|
final ImmutableMultimap<String, IceUdpTransportInfo.Candidate> candidates;
|
|
|
|
if (includeCandidates) {
|
|
|
|
candidates = parseCandidates(sessionDescription);
|
|
|
|
} else {
|
|
|
|
candidates = ImmutableMultimap.of();
|
|
|
|
}
|
2021-05-08 06:45:31 +00:00
|
|
|
this.responderRtpContentMap = respondingRtpContentMap;
|
2021-11-16 10:21:11 +00:00
|
|
|
storePeerDtlsSetup(respondingRtpContentMap.getDtlsSetup().flip());
|
2022-02-25 15:24:13 +00:00
|
|
|
final ListenableFuture<RtpContentMap> outgoingContentMapFuture =
|
|
|
|
prepareOutgoingContentMap(respondingRtpContentMap);
|
|
|
|
Futures.addCallback(
|
|
|
|
outgoingContentMapFuture,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2021-05-08 06:45:31 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap outgoingContentMap) {
|
2023-11-13 11:36:20 +00:00
|
|
|
if (includeCandidates) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"including "
|
|
|
|
+ candidates.size()
|
|
|
|
+ " candidates in session accept");
|
|
|
|
sendSessionAccept(outgoingContentMap.withCandidates(candidates));
|
|
|
|
} else {
|
|
|
|
sendSessionAccept(outgoingContentMap);
|
|
|
|
}
|
2023-11-14 07:57:22 +00:00
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
2021-05-08 06:45:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Throwable throwable) {
|
|
|
|
failureToAcceptSession(throwable);
|
|
|
|
}
|
|
|
|
},
|
2022-02-25 15:24:13 +00:00
|
|
|
MoreExecutors.directExecutor());
|
2021-05-08 06:45:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:40:16 +00:00
|
|
|
private void sendSessionAccept(final RtpContentMap rtpContentMap) {
|
2021-05-08 09:50:18 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": preparing session accept was too slow. already terminated. nothing to do.");
|
2021-05-08 09:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-08 06:45:31 +00:00
|
|
|
transitionOrThrow(State.SESSION_ACCEPTED);
|
2022-02-25 15:24:13 +00:00
|
|
|
final JinglePacket sessionAccept =
|
|
|
|
rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_ACCEPT, id.sessionId);
|
2021-05-08 06:45:31 +00:00
|
|
|
send(sessionAccept);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private ListenableFuture<RtpContentMap> prepareOutgoingContentMap(
|
|
|
|
final RtpContentMap rtpContentMap) {
|
2021-03-02 20:13:49 +00:00
|
|
|
if (this.omemoVerification.hasDeviceId()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>>
|
|
|
|
verifiedPayloadFuture =
|
|
|
|
id.account
|
|
|
|
.getAxolotlService()
|
|
|
|
.encrypt(
|
|
|
|
rtpContentMap,
|
|
|
|
id.with,
|
|
|
|
omemoVerification.getDeviceId());
|
|
|
|
return Futures.transform(
|
|
|
|
verifiedPayloadFuture,
|
|
|
|
verifiedPayload -> {
|
|
|
|
omemoVerification.setOrEnsureEqual(verifiedPayload);
|
|
|
|
return verifiedPayload.getPayload();
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-03-02 20:13:49 +00:00
|
|
|
} else {
|
2021-05-08 06:45:31 +00:00
|
|
|
return Futures.immediateFuture(rtpContentMap);
|
2021-03-02 20:13:49 +00:00
|
|
|
}
|
2020-04-03 13:25:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
synchronized void deliveryMessage(
|
|
|
|
final Jid from,
|
|
|
|
final Element message,
|
|
|
|
final String serverMessageId,
|
|
|
|
final long timestamp) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": delivered message to JingleRtpConnection "
|
|
|
|
+ message);
|
2020-04-02 19:12:38 +00:00
|
|
|
switch (message.getName()) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case "propose" -> receivePropose(
|
|
|
|
from, Propose.upgrade(message), serverMessageId, timestamp);
|
|
|
|
case "proceed" -> receiveProceed(
|
|
|
|
from, Proceed.upgrade(message), serverMessageId, timestamp);
|
|
|
|
case "retract" -> receiveRetract(from, serverMessageId, timestamp);
|
|
|
|
case "reject" -> receiveReject(from, serverMessageId, timestamp);
|
|
|
|
case "accept" -> receiveAccept(from, serverMessageId, timestamp);
|
2020-04-02 19:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 08:14:49 +00:00
|
|
|
void deliverFailedProceed(final String message) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
2023-11-13 11:54:55 +00:00
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": receive message error for proceed message ("
|
|
|
|
+ Strings.nullToEmpty(message)
|
|
|
|
+ ")");
|
2020-04-10 05:45:23 +00:00
|
|
|
if (transition(State.TERMINATED_CONNECTIVITY_ERROR)) {
|
|
|
|
webRTCWrapper.close();
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": transitioned into connectivity error");
|
2020-04-18 16:22:10 +00:00
|
|
|
this.finish();
|
2020-04-10 05:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:07:31 +00:00
|
|
|
private void receiveAccept(final Jid from, final String serverMsgId, final long timestamp) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final boolean originatedFromMyself =
|
|
|
|
from.asBareJid().equals(id.account.getJid().asBareJid());
|
2020-04-08 10:17:46 +00:00
|
|
|
if (originatedFromMyself) {
|
|
|
|
if (transition(State.ACCEPTED)) {
|
2022-11-22 09:13:07 +00:00
|
|
|
acceptedOnOtherDevice(serverMsgId, timestamp);
|
2020-04-08 10:17:46 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable to transition to accept because already in state="
|
|
|
|
+ this.state);
|
2023-09-30 13:56:00 +00:00
|
|
|
Log.d(Config.LOGTAG, id.account.getJid() + ": received accept from " + from);
|
2020-04-08 10:17:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": ignoring 'accept' from " + from);
|
2020-04-08 10:17:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 09:13:07 +00:00
|
|
|
private void acceptedOnOtherDevice(final String serverMsgId, final long timestamp) {
|
|
|
|
if (serverMsgId != null) {
|
|
|
|
this.message.setServerMsgId(serverMsgId);
|
|
|
|
}
|
|
|
|
this.message.setTime(timestamp);
|
|
|
|
this.message.setCarbon(true); // indicate that call was accepted on other device
|
|
|
|
this.writeLogMessageSuccess(0);
|
2023-11-13 11:54:55 +00:00
|
|
|
this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
2022-11-22 09:13:07 +00:00
|
|
|
this.finish();
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:56:57 +00:00
|
|
|
private void receiveReject(final Jid from, final String serverMsgId, final long timestamp) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final boolean originatedFromMyself =
|
|
|
|
from.asBareJid().equals(id.account.getJid().asBareJid());
|
|
|
|
// reject from another one of my clients
|
2020-04-08 09:29:01 +00:00
|
|
|
if (originatedFromMyself) {
|
2021-02-11 15:56:57 +00:00
|
|
|
receiveRejectFromMyself(serverMsgId, timestamp);
|
|
|
|
} else if (isInitiator()) {
|
|
|
|
if (from.equals(id.with)) {
|
|
|
|
receiveRejectFromResponder();
|
2020-04-08 09:29:01 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": ignoring reject from "
|
|
|
|
+ from
|
|
|
|
+ " for session with "
|
|
|
|
+ id.with);
|
2020-04-08 09:29:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": ignoring reject from "
|
|
|
|
+ from
|
|
|
|
+ " for session with "
|
|
|
|
+ id.with);
|
2020-04-08 09:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:56:57 +00:00
|
|
|
private void receiveRejectFromMyself(String serverMsgId, long timestamp) {
|
|
|
|
if (transition(State.REJECTED)) {
|
|
|
|
this.xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
|
|
|
this.finish();
|
|
|
|
if (serverMsgId != null) {
|
|
|
|
this.message.setServerMsgId(serverMsgId);
|
|
|
|
}
|
|
|
|
this.message.setTime(timestamp);
|
2022-02-25 15:24:13 +00:00
|
|
|
this.message.setCarbon(true); // indicate that call was rejected on other device
|
2021-02-11 15:56:57 +00:00
|
|
|
writeLogMessageMissed();
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"not able to transition into REJECTED because already in " + this.state);
|
2021-02-11 15:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void receiveRejectFromResponder() {
|
|
|
|
if (isInState(State.PROCEED)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": received reject while still in proceed. callee reconsidered");
|
2021-02-11 15:56:57 +00:00
|
|
|
closeTransitionLogFinish(State.REJECTED_RACED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": received reject while in SESSION_INITIATED_PRE_APPROVED. callee reconsidered before receiving session-init");
|
2021-02-11 15:56:57 +00:00
|
|
|
closeTransitionLogFinish(State.TERMINATED_DECLINED_OR_BUSY);
|
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": ignoring reject from responder because already in state "
|
|
|
|
+ this.state);
|
2021-02-11 15:56:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void receivePropose(
|
|
|
|
final Jid from, final Propose propose, final String serverMsgId, final long timestamp) {
|
|
|
|
final boolean originatedFromMyself =
|
|
|
|
from.asBareJid().equals(id.account.getJid().asBareJid());
|
2020-04-03 08:46:42 +00:00
|
|
|
if (originatedFromMyself) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": saw proposal from myself. ignoring");
|
|
|
|
} else if (transition(
|
|
|
|
State.PROPOSED,
|
|
|
|
() -> {
|
|
|
|
final Collection<RtpDescription> descriptions =
|
|
|
|
Collections2.transform(
|
|
|
|
Collections2.filter(
|
|
|
|
propose.getDescriptions(),
|
|
|
|
d -> d instanceof RtpDescription),
|
|
|
|
input -> (RtpDescription) input);
|
|
|
|
final Collection<Media> media =
|
|
|
|
Collections2.transform(descriptions, RtpDescription::getMedia);
|
|
|
|
Preconditions.checkState(
|
|
|
|
!media.contains(Media.UNKNOWN),
|
|
|
|
"RTP descriptions contain unknown media");
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received session proposal from "
|
|
|
|
+ from
|
|
|
|
+ " for "
|
|
|
|
+ media);
|
|
|
|
this.proposedMedia = Sets.newHashSet(media);
|
|
|
|
})) {
|
2020-04-12 16:07:31 +00:00
|
|
|
if (serverMsgId != null) {
|
|
|
|
this.message.setServerMsgId(serverMsgId);
|
|
|
|
}
|
|
|
|
this.message.setTime(timestamp);
|
2020-04-07 09:36:28 +00:00
|
|
|
startRinging();
|
2023-10-01 06:05:36 +00:00
|
|
|
if (xmppConnectionService.confirmMessages() && id.getContact().showInContactList()) {
|
|
|
|
sendJingleMessage("ringing");
|
|
|
|
}
|
2020-04-03 08:46:42 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid()
|
|
|
|
+ ": ignoring session proposal because already in "
|
|
|
|
+ state);
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:36:28 +00:00
|
|
|
private void startRinging() {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received call from "
|
|
|
|
+ id.with
|
|
|
|
+ ". start ringing");
|
|
|
|
ringingTimeoutFuture =
|
|
|
|
jingleConnectionManager.schedule(
|
|
|
|
this::ringingTimeout, BUSY_TIME_OUT, TimeUnit.SECONDS);
|
2021-02-18 19:55:29 +00:00
|
|
|
xmppConnectionService.getNotificationService().startRinging(id, getMedia());
|
2020-04-07 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 19:59:20 +00:00
|
|
|
private synchronized void ringingTimeout() {
|
|
|
|
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": timeout reached for ringing");
|
|
|
|
switch (this.state) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case PROPOSED -> {
|
2020-05-03 16:07:00 +00:00
|
|
|
message.markUnread();
|
2020-04-22 19:59:20 +00:00
|
|
|
rejectCallFromProposed();
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case SESSION_INITIALIZED -> {
|
2020-05-03 16:07:00 +00:00
|
|
|
message.markUnread();
|
2020-04-22 19:59:20 +00:00
|
|
|
rejectCallFromSessionInitiate();
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
2020-04-22 19:59:20 +00:00
|
|
|
}
|
2022-08-29 10:41:35 +00:00
|
|
|
xmppConnectionService.getNotificationService().pushMissedCallNow(message);
|
2020-04-22 19:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelRingingTimeout() {
|
|
|
|
final ScheduledFuture<?> future = this.ringingTimeoutFuture;
|
|
|
|
if (future != null && !future.isCancelled()) {
|
|
|
|
future.cancel(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void receiveProceed(
|
|
|
|
final Jid from, final Proceed proceed, final String serverMsgId, final long timestamp) {
|
|
|
|
final Set<Media> media =
|
|
|
|
Preconditions.checkNotNull(
|
|
|
|
this.proposedMedia, "Proposed media has to be set before handling proceed");
|
2020-04-15 10:07:19 +00:00
|
|
|
Preconditions.checkState(media.size() > 0, "Proposed media should not be empty");
|
2020-04-03 08:46:42 +00:00
|
|
|
if (from.equals(id.with)) {
|
|
|
|
if (isInitiator()) {
|
2020-04-04 09:31:53 +00:00
|
|
|
if (transition(State.PROCEED)) {
|
2020-04-12 16:07:31 +00:00
|
|
|
if (serverMsgId != null) {
|
|
|
|
this.message.setServerMsgId(serverMsgId);
|
|
|
|
}
|
|
|
|
this.message.setTime(timestamp);
|
2021-03-03 11:55:27 +00:00
|
|
|
final Integer remoteDeviceId = proceed.getDeviceId();
|
|
|
|
if (isOmemoEnabled()) {
|
|
|
|
this.omemoVerification.setDeviceId(remoteDeviceId);
|
|
|
|
} else {
|
|
|
|
if (remoteDeviceId != null) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": remote party signaled support for OMEMO verification but we have OMEMO disabled");
|
2021-03-03 11:55:27 +00:00
|
|
|
}
|
|
|
|
this.omemoVerification.setDeviceId(null);
|
|
|
|
}
|
2020-04-15 10:07:19 +00:00
|
|
|
this.sendSessionInitiate(media, State.SESSION_INITIALIZED_PRE_APPROVED);
|
2020-04-03 08:46:42 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: ignoring proceed because already in %s",
|
|
|
|
id.account.getJid().asBareJid(), this.state));
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: ignoring proceed because we were not initializing",
|
|
|
|
id.account.getJid().asBareJid()));
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
2020-04-08 09:29:01 +00:00
|
|
|
} else if (from.asBareJid().equals(id.account.getJid().asBareJid())) {
|
|
|
|
if (transition(State.ACCEPTED)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": moved session with "
|
|
|
|
+ id.with
|
2022-11-22 09:13:07 +00:00
|
|
|
+ " into state accepted after received carbon copied proceed");
|
|
|
|
acceptedOnOtherDevice(serverMsgId, timestamp);
|
2020-04-08 09:29:01 +00:00
|
|
|
}
|
2020-04-03 08:46:42 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
String.format(
|
|
|
|
"%s: ignoring proceed from %s. was expected from %s",
|
|
|
|
id.account.getJid().asBareJid(), from, id.with));
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 16:07:31 +00:00
|
|
|
private void receiveRetract(final Jid from, final String serverMsgId, final long timestamp) {
|
2020-04-08 07:42:06 +00:00
|
|
|
if (from.equals(id.with)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final State target =
|
|
|
|
this.state == State.PROCEED ? State.RETRACTED_RACED : State.RETRACTED;
|
2020-05-09 19:35:21 +00:00
|
|
|
if (transition(target)) {
|
2020-04-08 07:42:06 +00:00
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
2022-08-29 10:41:35 +00:00
|
|
|
xmppConnectionService.getNotificationService().pushMissedCallNow(message);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": session with "
|
|
|
|
+ id.with
|
|
|
|
+ " has been retracted (serverMsgId="
|
|
|
|
+ serverMsgId
|
|
|
|
+ ")");
|
2020-04-12 16:07:31 +00:00
|
|
|
if (serverMsgId != null) {
|
|
|
|
this.message.setServerMsgId(serverMsgId);
|
|
|
|
}
|
|
|
|
this.message.setTime(timestamp);
|
2020-05-09 19:35:21 +00:00
|
|
|
if (target == State.RETRACTED) {
|
|
|
|
this.message.markUnread();
|
|
|
|
}
|
2020-04-12 15:12:59 +00:00
|
|
|
writeLogMessageMissed();
|
2020-04-18 16:22:10 +00:00
|
|
|
finish();
|
2020-04-08 07:42:06 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, "ignoring retract because already in " + this.state);
|
|
|
|
}
|
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
// TODO parse retract from self
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received retract from "
|
|
|
|
+ from
|
|
|
|
+ ". expected retract from"
|
|
|
|
+ id.with
|
|
|
|
+ ". ignoring");
|
2020-04-08 07:42:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-30 12:56:12 +00:00
|
|
|
public void sendSessionInitiate() {
|
|
|
|
sendSessionInitiate(this.proposedMedia, State.SESSION_INITIALIZED);
|
|
|
|
}
|
|
|
|
|
2020-04-15 10:07:19 +00:00
|
|
|
private void sendSessionInitiate(final Set<Media> media, final State targetState) {
|
2020-04-05 08:20:34 +00:00
|
|
|
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": prepare session-initiate");
|
2020-04-15 10:07:19 +00:00
|
|
|
discoverIceServers(iceServers -> sendSessionInitiate(media, targetState, iceServers));
|
2020-04-14 07:06:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private synchronized void sendSessionInitiate(
|
|
|
|
final Set<Media> media,
|
|
|
|
final State targetState,
|
|
|
|
final List<PeerConnection.IceServer> iceServers) {
|
2020-04-24 07:41:54 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ICE servers got discovered when session was already terminated. nothing to do.");
|
2020-04-14 07:53:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-11-13 11:36:20 +00:00
|
|
|
final boolean includeCandidates = remoteHasSdpOfferAnswer();
|
2020-04-14 07:06:07 +00:00
|
|
|
try {
|
2023-11-13 11:36:20 +00:00
|
|
|
setupWebRTC(media, iceServers, !includeCandidates);
|
2020-05-08 16:36:52 +00:00
|
|
|
} catch (final WebRTCWrapper.InitializationException e) {
|
2020-05-01 11:56:24 +00:00
|
|
|
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": unable to initialize WebRTC");
|
|
|
|
webRTCWrapper.close();
|
2021-05-06 16:40:35 +00:00
|
|
|
sendRetract(Reason.ofThrowable(e));
|
2020-04-14 07:06:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2022-02-25 15:24:13 +00:00
|
|
|
org.webrtc.SessionDescription webRTCSessionDescription =
|
2023-11-13 11:36:20 +00:00
|
|
|
this.webRTCWrapper.setLocalDescription(includeCandidates).get();
|
|
|
|
prepareSessionInitiate(webRTCSessionDescription, includeCandidates, targetState);
|
2020-04-14 07:06:07 +00:00
|
|
|
} catch (final Exception e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// TODO sending the error text is worthwhile as well. Especially for FailureToSet
|
|
|
|
// exceptions
|
2021-05-08 06:45:31 +00:00
|
|
|
failureToInitiateSession(e, targetState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void failureToInitiateSession(final Throwable throwable, final State targetState) {
|
2021-05-08 09:50:18 +00:00
|
|
|
if (isTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": unable to sendSessionInitiate",
|
|
|
|
Throwables.getRootCause(throwable));
|
2021-05-08 06:45:31 +00:00
|
|
|
webRTCWrapper.close();
|
|
|
|
final Reason reason = Reason.ofThrowable(throwable);
|
|
|
|
if (isInState(targetState)) {
|
2022-08-01 08:14:49 +00:00
|
|
|
sendSessionTerminate(reason, throwable.getMessage());
|
2021-05-08 06:45:31 +00:00
|
|
|
} else {
|
|
|
|
sendRetract(reason);
|
2020-04-14 07:06:07 +00:00
|
|
|
}
|
2020-04-05 08:20:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 17:04:01 +00:00
|
|
|
private void sendRetract(final Reason reason) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// TODO embed reason into retract
|
2021-05-04 17:04:01 +00:00
|
|
|
sendJingleMessage("retract", id.with.asBareJid());
|
|
|
|
transitionOrThrow(reasonToState(reason));
|
|
|
|
this.finish();
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void prepareSessionInitiate(
|
2023-11-13 11:54:55 +00:00
|
|
|
final org.webrtc.SessionDescription webRTCSessionDescription,
|
|
|
|
final boolean includeCandidates,
|
|
|
|
final State targetState) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final SessionDescription sessionDescription =
|
|
|
|
SessionDescription.parse(webRTCSessionDescription.description);
|
2022-11-20 16:00:40 +00:00
|
|
|
final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription, true);
|
2023-11-13 11:36:20 +00:00
|
|
|
final ImmutableMultimap<String, IceUdpTransportInfo.Candidate> candidates;
|
|
|
|
if (includeCandidates) {
|
|
|
|
candidates = parseCandidates(sessionDescription);
|
|
|
|
} else {
|
|
|
|
candidates = ImmutableMultimap.of();
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
this.initiatorRtpContentMap = rtpContentMap;
|
2022-02-25 15:24:13 +00:00
|
|
|
final ListenableFuture<RtpContentMap> outgoingContentMapFuture =
|
|
|
|
encryptSessionInitiate(rtpContentMap);
|
|
|
|
Futures.addCallback(
|
|
|
|
outgoingContentMapFuture,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap outgoingContentMap) {
|
2023-11-13 11:36:20 +00:00
|
|
|
if (includeCandidates) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"including "
|
|
|
|
+ candidates.size()
|
|
|
|
+ " candidates in session initiate");
|
2023-11-13 11:54:55 +00:00
|
|
|
sendSessionInitiate(
|
|
|
|
outgoingContentMap.withCandidates(candidates), targetState);
|
2023-11-13 11:36:20 +00:00
|
|
|
} else {
|
|
|
|
sendSessionInitiate(outgoingContentMap, targetState);
|
|
|
|
}
|
2023-11-14 07:57:22 +00:00
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
2022-02-25 15:24:13 +00:00
|
|
|
}
|
2021-05-08 06:45:31 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull final Throwable throwable) {
|
|
|
|
failureToInitiateSession(throwable, targetState);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-05-08 06:45:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:40:16 +00:00
|
|
|
private void sendSessionInitiate(final RtpContentMap rtpContentMap, final State targetState) {
|
2021-05-08 09:50:18 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": preparing session was too slow. already terminated. nothing to do.");
|
2021-05-08 09:50:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-05-04 17:04:01 +00:00
|
|
|
this.transitionOrThrow(targetState);
|
2022-02-25 15:24:13 +00:00
|
|
|
final JinglePacket sessionInitiate =
|
|
|
|
rtpContentMap.toJinglePacket(JinglePacket.Action.SESSION_INITIATE, id.sessionId);
|
2020-04-05 11:58:05 +00:00
|
|
|
send(sessionInitiate);
|
2020-04-03 08:46:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private ListenableFuture<RtpContentMap> encryptSessionInitiate(
|
|
|
|
final RtpContentMap rtpContentMap) {
|
2021-03-02 20:13:49 +00:00
|
|
|
if (this.omemoVerification.hasDeviceId()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final ListenableFuture<AxolotlService.OmemoVerifiedPayload<OmemoVerifiedRtpContentMap>>
|
|
|
|
verifiedPayloadFuture =
|
|
|
|
id.account
|
|
|
|
.getAxolotlService()
|
|
|
|
.encrypt(
|
|
|
|
rtpContentMap,
|
|
|
|
id.with,
|
|
|
|
omemoVerification.getDeviceId());
|
|
|
|
final ListenableFuture<RtpContentMap> future =
|
|
|
|
Futures.transform(
|
|
|
|
verifiedPayloadFuture,
|
|
|
|
verifiedPayload -> {
|
|
|
|
omemoVerification.setSessionFingerprint(
|
|
|
|
verifiedPayload.getFingerprint());
|
|
|
|
return verifiedPayload.getPayload();
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
2021-05-08 06:45:31 +00:00
|
|
|
if (Config.REQUIRE_RTP_VERIFICATION) {
|
|
|
|
return future;
|
2021-03-02 20:13:49 +00:00
|
|
|
}
|
2021-05-08 06:45:31 +00:00
|
|
|
return Futures.catching(
|
|
|
|
future,
|
|
|
|
CryptoFailedException.class,
|
|
|
|
e -> {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable to use OMEMO DTLS verification on outgoing session initiate. falling back",
|
|
|
|
e);
|
2021-05-08 06:45:31 +00:00
|
|
|
return rtpContentMap;
|
|
|
|
},
|
2022-02-25 15:24:13 +00:00
|
|
|
MoreExecutors.directExecutor());
|
2021-03-02 20:13:49 +00:00
|
|
|
} else {
|
2021-05-08 06:45:31 +00:00
|
|
|
return Futures.immediateFuture(rtpContentMap);
|
2021-03-02 20:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:27:17 +00:00
|
|
|
private void sendSessionTerminate(final Reason reason) {
|
2020-04-09 18:35:44 +00:00
|
|
|
sendSessionTerminate(reason, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendSessionTerminate(final Reason reason, final String text) {
|
2020-04-26 08:38:19 +00:00
|
|
|
final State previous = this.state;
|
2020-04-08 13:27:17 +00:00
|
|
|
final State target = reasonToState(reason);
|
|
|
|
transitionOrThrow(target);
|
2020-04-26 08:38:19 +00:00
|
|
|
if (previous != State.NULL) {
|
|
|
|
writeLogMessage(target);
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
|
2020-04-09 18:35:44 +00:00
|
|
|
jinglePacket.setReason(reason, text);
|
2020-04-08 13:27:17 +00:00
|
|
|
send(jinglePacket);
|
2020-04-18 16:22:10 +00:00
|
|
|
finish();
|
2020-04-08 13:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private void sendTransportInfo(
|
|
|
|
final String contentName, IceUdpTransportInfo.Candidate candidate) {
|
2020-04-05 11:58:05 +00:00
|
|
|
final RtpContentMap transportInfo;
|
|
|
|
try {
|
2022-02-25 15:24:13 +00:00
|
|
|
final RtpContentMap rtpContentMap =
|
|
|
|
isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
|
2020-04-06 13:45:06 +00:00
|
|
|
transportInfo = rtpContentMap.transportInfo(contentName, candidate);
|
2020-04-26 08:38:19 +00:00
|
|
|
} catch (final Exception e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": unable to prepare transport-info from candidate for content="
|
|
|
|
+ contentName);
|
2020-04-05 11:58:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
|
2020-04-05 11:58:05 +00:00
|
|
|
send(jinglePacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void send(final JinglePacket jinglePacket) {
|
|
|
|
jinglePacket.setTo(id.with);
|
2020-04-17 08:56:27 +00:00
|
|
|
xmppConnectionService.sendIqPacket(id.account, jinglePacket, this::handleIqResponse);
|
|
|
|
}
|
2020-04-09 13:22:03 +00:00
|
|
|
|
2020-04-17 08:56:27 +00:00
|
|
|
private synchronized void handleIqResponse(final Account account, final IqPacket response) {
|
|
|
|
if (response.getType() == IqPacket.TYPE.ERROR) {
|
2021-11-17 09:49:16 +00:00
|
|
|
handleIqErrorResponse(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (response.getType() == IqPacket.TYPE.TIMEOUT) {
|
|
|
|
handleIqTimeoutResponse(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIqErrorResponse(final IqPacket response) {
|
|
|
|
Preconditions.checkArgument(response.getType() == IqPacket.TYPE.ERROR);
|
|
|
|
final String errorCondition = response.getErrorCondition();
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received IQ-error from "
|
|
|
|
+ response.getFrom()
|
|
|
|
+ " in RTP session. "
|
|
|
|
+ errorCondition);
|
2021-11-17 09:49:16 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.i(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ignoring error because session was already terminated");
|
2021-11-17 09:49:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.webRTCWrapper.close();
|
|
|
|
final State target;
|
|
|
|
if (Arrays.asList(
|
2022-02-25 15:24:13 +00:00
|
|
|
"service-unavailable",
|
|
|
|
"recipient-unavailable",
|
|
|
|
"remote-server-not-found",
|
|
|
|
"remote-server-timeout")
|
|
|
|
.contains(errorCondition)) {
|
2021-11-17 09:49:16 +00:00
|
|
|
target = State.TERMINATED_CONNECTIVITY_ERROR;
|
|
|
|
} else {
|
|
|
|
target = State.TERMINATED_APPLICATION_FAILURE;
|
|
|
|
}
|
|
|
|
transitionOrThrow(target);
|
|
|
|
this.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIqTimeoutResponse(final IqPacket response) {
|
2022-02-12 09:19:54 +00:00
|
|
|
Preconditions.checkArgument(response.getType() == IqPacket.TYPE.TIMEOUT);
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received IQ timeout in RTP session with "
|
|
|
|
+ id.with
|
|
|
|
+ ". terminating with connectivity error");
|
2021-11-17 09:49:16 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.i(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": ignoring error because session was already terminated");
|
2021-11-17 09:49:16 +00:00
|
|
|
return;
|
2020-04-17 08:56:27 +00:00
|
|
|
}
|
2021-11-17 09:49:16 +00:00
|
|
|
this.webRTCWrapper.close();
|
|
|
|
transitionOrThrow(State.TERMINATED_CONNECTIVITY_ERROR);
|
|
|
|
this.finish();
|
2020-04-09 13:22:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 05:07:22 +00:00
|
|
|
private void terminateWithOutOfOrder(final JinglePacket jinglePacket) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": terminating session with out-of-order");
|
2020-04-18 16:22:10 +00:00
|
|
|
this.webRTCWrapper.close();
|
2020-04-10 05:07:22 +00:00
|
|
|
transitionOrThrow(State.TERMINATED_APPLICATION_FAILURE);
|
|
|
|
respondWithOutOfOrder(jinglePacket);
|
2020-04-18 16:22:10 +00:00
|
|
|
this.finish();
|
2020-04-10 05:07:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:03:04 +00:00
|
|
|
private void respondWithTieBreak(final JinglePacket jinglePacket) {
|
|
|
|
respondWithJingleError(jinglePacket, "tie-break", "conflict", "cancel");
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:22:03 +00:00
|
|
|
private void respondWithOutOfOrder(final JinglePacket jinglePacket) {
|
2021-11-15 12:03:04 +00:00
|
|
|
respondWithJingleError(jinglePacket, "out-of-order", "unexpected-request", "wait");
|
|
|
|
}
|
|
|
|
|
2023-10-10 16:42:23 +00:00
|
|
|
private void respondWithItemNotFound(final JinglePacket jinglePacket) {
|
|
|
|
respondWithJingleError(jinglePacket, null, "item-not-found", "cancel");
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
void respondWithJingleError(
|
|
|
|
final IqPacket original,
|
|
|
|
String jingleCondition,
|
|
|
|
String condition,
|
|
|
|
String conditionType) {
|
|
|
|
jingleConnectionManager.respondWithJingleError(
|
|
|
|
id.account, original, jingleCondition, condition, conditionType);
|
2020-04-09 13:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void respondOk(final JinglePacket jinglePacket) {
|
2022-02-25 15:24:13 +00:00
|
|
|
xmppConnectionService.sendIqPacket(
|
|
|
|
id.account, jinglePacket.generateResponse(IqPacket.TYPE.RESULT), null);
|
2020-05-28 07:22:58 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
public RtpEndUserState getEndUserState() {
|
|
|
|
switch (this.state) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case NULL, PROPOSED, SESSION_INITIALIZED -> {
|
2020-04-07 11:15:24 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
return RtpEndUserState.RINGING;
|
|
|
|
} else {
|
|
|
|
return RtpEndUserState.INCOMING_CALL;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case PROCEED -> {
|
2020-04-07 11:15:24 +00:00
|
|
|
if (isInitiator()) {
|
2020-04-09 07:04:59 +00:00
|
|
|
return RtpEndUserState.RINGING;
|
2020-04-07 11:15:24 +00:00
|
|
|
} else {
|
|
|
|
return RtpEndUserState.ACCEPTING_CALL;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case SESSION_INITIALIZED_PRE_APPROVED -> {
|
2020-04-09 07:04:59 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
return RtpEndUserState.RINGING;
|
|
|
|
} else {
|
|
|
|
return RtpEndUserState.CONNECTING;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case SESSION_ACCEPTED -> {
|
2022-11-28 07:59:23 +00:00
|
|
|
final ContentAddition ca = getPendingContentAddition();
|
|
|
|
if (ca != null && ca.direction == ContentAddition.Direction.INCOMING) {
|
|
|
|
return RtpEndUserState.INCOMING_CONTENT_ADD;
|
|
|
|
}
|
2021-11-11 13:40:15 +00:00
|
|
|
return getPeerConnectionStateAsEndUserState();
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case REJECTED, REJECTED_RACED, TERMINATED_DECLINED_OR_BUSY -> {
|
2020-04-07 19:26:51 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
return RtpEndUserState.DECLINED_OR_BUSY;
|
|
|
|
} else {
|
|
|
|
return RtpEndUserState.ENDED;
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case TERMINATED_SUCCESS, ACCEPTED, RETRACTED, TERMINATED_CANCEL_OR_TIMEOUT -> {
|
2020-04-07 19:26:51 +00:00
|
|
|
return RtpEndUserState.ENDED;
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case RETRACTED_RACED -> {
|
2021-02-11 15:56:57 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
return RtpEndUserState.ENDED;
|
|
|
|
} else {
|
|
|
|
return RtpEndUserState.RETRACTED;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case TERMINATED_CONNECTIVITY_ERROR -> {
|
2022-02-25 15:24:13 +00:00
|
|
|
return zeroDuration()
|
|
|
|
? RtpEndUserState.CONNECTIVITY_ERROR
|
|
|
|
: RtpEndUserState.CONNECTIVITY_LOST_ERROR;
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case TERMINATED_APPLICATION_FAILURE -> {
|
2020-04-09 13:22:03 +00:00
|
|
|
return RtpEndUserState.APPLICATION_ERROR;
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case TERMINATED_SECURITY_ERROR -> {
|
2021-05-04 08:10:30 +00:00
|
|
|
return RtpEndUserState.SECURITY_ERROR;
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
throw new IllegalStateException(
|
|
|
|
String.format("%s has no equivalent EndUserState", this.state));
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 13:40:15 +00:00
|
|
|
private RtpEndUserState getPeerConnectionStateAsEndUserState() {
|
|
|
|
final PeerConnection.PeerConnectionState state;
|
|
|
|
try {
|
|
|
|
state = webRTCWrapper.getState();
|
|
|
|
} catch (final WebRTCWrapper.PeerConnectionNotInitialized e) {
|
2022-02-25 15:24:13 +00:00
|
|
|
// We usually close the WebRTCWrapper *before* transitioning so we might still
|
|
|
|
// be in SESSION_ACCEPTED even though the peerConnection has been torn down
|
2021-11-11 13:40:15 +00:00
|
|
|
return RtpEndUserState.ENDING_CALL;
|
|
|
|
}
|
2023-11-13 11:54:55 +00:00
|
|
|
return switch (state) {
|
|
|
|
case CONNECTED -> RtpEndUserState.CONNECTED;
|
|
|
|
case NEW, CONNECTING -> RtpEndUserState.CONNECTING;
|
|
|
|
case CLOSED -> RtpEndUserState.ENDING_CALL;
|
|
|
|
default -> zeroDuration()
|
|
|
|
? RtpEndUserState.CONNECTIVITY_ERROR
|
|
|
|
: RtpEndUserState.RECONNECTING;
|
|
|
|
};
|
2021-11-11 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
public ContentAddition getPendingContentAddition() {
|
|
|
|
final RtpContentMap in = this.incomingContentAdd;
|
|
|
|
final RtpContentMap out = this.outgoingContentAdd;
|
|
|
|
if (out != null) {
|
|
|
|
return ContentAddition.of(ContentAddition.Direction.OUTGOING, out);
|
|
|
|
} else if (in != null) {
|
|
|
|
return ContentAddition.of(ContentAddition.Direction.INCOMING, in);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:28:04 +00:00
|
|
|
public Set<Media> getMedia() {
|
2020-04-29 06:51:38 +00:00
|
|
|
final State current = getState();
|
|
|
|
if (current == State.NULL) {
|
2020-05-30 12:56:12 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
return Preconditions.checkNotNull(
|
2022-02-25 15:24:13 +00:00
|
|
|
this.proposedMedia, "RTP connection has not been initialized properly");
|
2020-05-30 12:56:12 +00:00
|
|
|
}
|
2020-04-15 16:28:04 +00:00
|
|
|
throw new IllegalStateException("RTP connection has not been initialized yet");
|
|
|
|
}
|
2020-04-29 06:51:38 +00:00
|
|
|
if (Arrays.asList(State.PROPOSED, State.PROCEED).contains(current)) {
|
2020-05-30 12:56:12 +00:00
|
|
|
return Preconditions.checkNotNull(
|
2022-02-25 15:24:13 +00:00
|
|
|
this.proposedMedia, "RTP connection has not been initialized properly");
|
2020-04-16 17:49:34 +00:00
|
|
|
}
|
2022-11-28 07:59:23 +00:00
|
|
|
final RtpContentMap localContentMap = getLocalContentMap();
|
2020-04-16 17:49:34 +00:00
|
|
|
final RtpContentMap initiatorContentMap = initiatorRtpContentMap;
|
2022-11-28 07:59:23 +00:00
|
|
|
if (localContentMap != null) {
|
|
|
|
return localContentMap.getMedia();
|
|
|
|
} else if (initiatorContentMap != null) {
|
2020-04-16 17:49:34 +00:00
|
|
|
return initiatorContentMap.getMedia();
|
2020-04-29 13:54:02 +00:00
|
|
|
} else if (isTerminated()) {
|
2023-11-13 11:54:55 +00:00
|
|
|
return Collections.emptySet(); // we might fail before we ever got a chance to set media
|
2020-04-16 17:49:34 +00:00
|
|
|
} else {
|
2023-11-13 11:54:55 +00:00
|
|
|
return Preconditions.checkNotNull(
|
|
|
|
this.proposedMedia, "RTP connection has not been initialized properly");
|
2020-04-15 16:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 08:41:05 +00:00
|
|
|
public boolean isVerified() {
|
|
|
|
final String fingerprint = this.omemoVerification.getFingerprint();
|
|
|
|
if (fingerprint == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final FingerprintStatus status =
|
|
|
|
id.account.getAxolotlService().getFingerprintTrust(fingerprint);
|
2021-05-06 16:40:35 +00:00
|
|
|
return status != null && status.isVerified();
|
2021-03-03 08:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
public boolean addMedia(final Media media) {
|
|
|
|
final Set<Media> currentMedia = getMedia();
|
|
|
|
if (currentMedia.contains(media)) {
|
|
|
|
throw new IllegalStateException(String.format("%s has already been proposed", media));
|
|
|
|
}
|
|
|
|
// TODO add state protection - can only add while ACCEPTED or so
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(Config.LOGTAG, "adding media: " + media);
|
2022-11-28 07:59:23 +00:00
|
|
|
return webRTCWrapper.addTrack(media);
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:53:01 +00:00
|
|
|
public synchronized void acceptCall() {
|
2020-04-02 19:12:38 +00:00
|
|
|
switch (this.state) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case PROPOSED -> {
|
2020-04-22 19:59:20 +00:00
|
|
|
cancelRingingTimeout();
|
2020-04-07 11:15:24 +00:00
|
|
|
acceptCallFromProposed();
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case SESSION_INITIALIZED -> {
|
2020-04-22 19:59:20 +00:00
|
|
|
cancelRingingTimeout();
|
2020-04-07 11:15:24 +00:00
|
|
|
acceptCallFromSessionInitialized();
|
2023-11-13 11:54:55 +00:00
|
|
|
}
|
|
|
|
case ACCEPTED -> Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": the call has already been accepted with another client. UI was just lagging behind");
|
|
|
|
case PROCEED, SESSION_ACCEPTED -> Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": the call has already been accepted. user probably double tapped the UI");
|
|
|
|
default -> throw new IllegalStateException("Can not accept call from " + this.state);
|
2020-04-02 19:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 10:47:54 +00:00
|
|
|
public void notifyPhoneCall() {
|
|
|
|
Log.d(Config.LOGTAG, "a phone call has just been started. killing jingle rtp connections");
|
|
|
|
if (Arrays.asList(State.PROPOSED, State.SESSION_INITIALIZED).contains(this.state)) {
|
|
|
|
rejectCall();
|
|
|
|
} else {
|
|
|
|
endCall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 07:53:01 +00:00
|
|
|
public synchronized void rejectCall() {
|
2020-06-18 18:32:58 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received rejectCall() when session has already been terminated. nothing to do");
|
2020-06-18 18:32:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-07 19:26:51 +00:00
|
|
|
switch (this.state) {
|
2023-11-13 11:54:55 +00:00
|
|
|
case PROPOSED -> rejectCallFromProposed();
|
|
|
|
case SESSION_INITIALIZED -> rejectCallFromSessionInitiate();
|
|
|
|
default -> throw new IllegalStateException("Can not reject call from " + this.state);
|
2020-04-07 19:26:51 +00:00
|
|
|
}
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 07:53:01 +00:00
|
|
|
public synchronized void endCall() {
|
2020-04-24 07:41:54 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": received endCall() when session has already been terminated. nothing to do");
|
2020-04-14 09:56:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-10 08:35:00 +00:00
|
|
|
if (isInState(State.PROPOSED) && !isInitiator()) {
|
|
|
|
rejectCallFromProposed();
|
|
|
|
return;
|
|
|
|
}
|
2020-04-09 07:04:59 +00:00
|
|
|
if (isInState(State.PROCEED)) {
|
2021-02-11 15:56:57 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
retractFromProceed();
|
|
|
|
} else {
|
|
|
|
rejectCallFromProceed();
|
|
|
|
}
|
2020-04-09 07:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
if (isInitiator()
|
|
|
|
&& isInState(State.SESSION_INITIALIZED, State.SESSION_INITIALIZED_PRE_APPROVED)) {
|
2020-04-18 16:22:10 +00:00
|
|
|
this.webRTCWrapper.close();
|
2020-04-08 13:27:17 +00:00
|
|
|
sendSessionTerminate(Reason.CANCEL);
|
2020-04-09 07:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-10 08:35:00 +00:00
|
|
|
if (isInState(State.SESSION_INITIALIZED)) {
|
|
|
|
rejectCallFromSessionInitiate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isInState(State.SESSION_INITIALIZED_PRE_APPROVED, State.SESSION_ACCEPTED)) {
|
2020-04-18 16:22:10 +00:00
|
|
|
this.webRTCWrapper.close();
|
2020-04-08 13:27:17 +00:00
|
|
|
sendSessionTerminate(Reason.SUCCESS);
|
2020-04-09 07:04:59 +00:00
|
|
|
return;
|
2020-04-07 12:22:12 +00:00
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
if (isInState(
|
|
|
|
State.TERMINATED_APPLICATION_FAILURE,
|
|
|
|
State.TERMINATED_CONNECTIVITY_ERROR,
|
|
|
|
State.TERMINATED_DECLINED_OR_BUSY)) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"ignoring request to end call because already in state " + this.state);
|
2020-04-10 11:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"called 'endCall' while in state " + this.state + ". isInitiator=" + isInitiator());
|
2020-04-07 12:22:12 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 15:56:57 +00:00
|
|
|
private void retractFromProceed() {
|
|
|
|
Log.d(Config.LOGTAG, "retract from proceed");
|
|
|
|
this.sendJingleMessage("retract");
|
|
|
|
closeTransitionLogFinish(State.RETRACTED_RACED);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void closeTransitionLogFinish(final State state) {
|
|
|
|
this.webRTCWrapper.close();
|
|
|
|
transitionOrThrow(state);
|
|
|
|
writeLogMessage(state);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:54:55 +00:00
|
|
|
private void setupWebRTC(
|
|
|
|
final Set<Media> media,
|
|
|
|
final List<PeerConnection.IceServer> iceServers,
|
|
|
|
final boolean trickle)
|
|
|
|
throws WebRTCWrapper.InitializationException {
|
2020-04-18 19:27:50 +00:00
|
|
|
this.jingleConnectionManager.ensureConnectionIsRegistered(this);
|
2023-11-13 11:54:55 +00:00
|
|
|
this.webRTCWrapper.setup(
|
|
|
|
this.xmppConnectionService, AppRTCAudioManager.SpeakerPhonePreference.of(media));
|
2023-11-13 11:36:20 +00:00
|
|
|
this.webRTCWrapper.initializePeerConnection(media, iceServers, trickle);
|
2020-04-04 13:30:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
private void acceptCallFromProposed() {
|
2020-04-02 19:12:38 +00:00
|
|
|
transitionOrThrow(State.PROCEED);
|
2020-04-07 16:50:39 +00:00
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
2020-04-08 10:17:46 +00:00
|
|
|
this.sendJingleMessage("accept", id.account.getJid().asBareJid());
|
2020-04-07 19:26:51 +00:00
|
|
|
this.sendJingleMessage("proceed");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void rejectCallFromProposed() {
|
|
|
|
transitionOrThrow(State.REJECTED);
|
2020-04-12 16:07:31 +00:00
|
|
|
writeLogMessageMissed();
|
2020-04-07 19:26:51 +00:00
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
|
|
|
this.sendJingleMessage("reject");
|
2020-04-18 16:22:10 +00:00
|
|
|
finish();
|
2020-04-07 19:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 15:56:57 +00:00
|
|
|
private void rejectCallFromProceed() {
|
|
|
|
this.sendJingleMessage("reject");
|
|
|
|
closeTransitionLogFinish(State.REJECTED_RACED);
|
|
|
|
}
|
|
|
|
|
2020-04-09 07:04:59 +00:00
|
|
|
private void rejectCallFromSessionInitiate() {
|
|
|
|
webRTCWrapper.close();
|
|
|
|
sendSessionTerminate(Reason.DECLINE);
|
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:51 +00:00
|
|
|
private void sendJingleMessage(final String action) {
|
2020-04-08 10:17:46 +00:00
|
|
|
sendJingleMessage(action, id.with);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendJingleMessage(final String action, final Jid to) {
|
2020-04-02 19:12:38 +00:00
|
|
|
final MessagePacket messagePacket = new MessagePacket();
|
2022-02-25 15:24:13 +00:00
|
|
|
messagePacket.setType(MessagePacket.TYPE_CHAT); // we want to carbon copy those
|
2021-03-02 20:13:49 +00:00
|
|
|
messagePacket.setTo(to);
|
2022-02-25 15:24:13 +00:00
|
|
|
final Element intent =
|
|
|
|
messagePacket
|
|
|
|
.addChild(action, Namespace.JINGLE_MESSAGE)
|
|
|
|
.setAttribute("id", id.sessionId);
|
2020-04-10 05:45:23 +00:00
|
|
|
if ("proceed".equals(action)) {
|
|
|
|
messagePacket.setId(JINGLE_MESSAGE_PROCEED_ID_PREFIX + id.sessionId);
|
2021-03-03 11:55:27 +00:00
|
|
|
if (isOmemoEnabled()) {
|
|
|
|
final int deviceId = id.account.getAxolotlService().getOwnDeviceId();
|
2022-02-25 15:24:13 +00:00
|
|
|
final Element device =
|
|
|
|
intent.addChild("device", Namespace.OMEMO_DTLS_SRTP_VERIFICATION);
|
2021-03-03 11:55:27 +00:00
|
|
|
device.setAttribute("id", deviceId);
|
|
|
|
}
|
2020-04-10 05:45:23 +00:00
|
|
|
}
|
2020-04-13 07:00:25 +00:00
|
|
|
messagePacket.addChild("store", "urn:xmpp:hints");
|
2020-04-02 19:12:38 +00:00
|
|
|
xmppConnectionService.sendMessagePacket(id.account, messagePacket);
|
2020-04-02 14:29:33 +00:00
|
|
|
}
|
2020-04-02 19:12:38 +00:00
|
|
|
|
2021-03-03 11:55:27 +00:00
|
|
|
private boolean isOmemoEnabled() {
|
|
|
|
final Conversational conversational = message.getConversation();
|
|
|
|
if (conversational instanceof Conversation) {
|
2022-02-25 15:24:13 +00:00
|
|
|
return ((Conversation) conversational).getNextEncryption()
|
|
|
|
== Message.ENCRYPTION_AXOLOTL;
|
2021-03-03 11:55:27 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
private void acceptCallFromSessionInitialized() {
|
2020-04-07 16:50:39 +00:00
|
|
|
xmppConnectionService.getNotificationService().cancelIncomingCallNotification();
|
2020-04-09 07:04:59 +00:00
|
|
|
sendSessionAccept();
|
2020-04-02 19:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 08:26:29 +00:00
|
|
|
private synchronized boolean isInState(State... state) {
|
|
|
|
return Arrays.asList(state).contains(this.state);
|
|
|
|
}
|
|
|
|
|
2020-04-15 20:40:37 +00:00
|
|
|
private boolean transition(final State target) {
|
|
|
|
return transition(target, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized boolean transition(final State target, final Runnable runnable) {
|
2020-04-02 19:12:38 +00:00
|
|
|
final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
|
|
|
|
if (validTransitions != null && validTransitions.contains(target)) {
|
|
|
|
this.state = target;
|
2020-04-15 20:40:37 +00:00
|
|
|
if (runnable != null) {
|
|
|
|
runnable.run();
|
|
|
|
}
|
2020-04-02 19:12:38 +00:00
|
|
|
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
|
2020-04-07 11:15:24 +00:00
|
|
|
updateEndUserState();
|
2020-04-10 13:19:56 +00:00
|
|
|
updateOngoingCallNotification();
|
2020-04-02 19:12:38 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-24 07:16:59 +00:00
|
|
|
void transitionOrThrow(final State target) {
|
2020-04-02 19:12:38 +00:00
|
|
|
if (!transition(target)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
throw new IllegalStateException(
|
|
|
|
String.format("Unable to transition from %s to %s", this.state, target));
|
2020-04-02 19:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-06 11:01:17 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIceCandidate(final IceCandidate iceCandidate) {
|
2022-02-25 15:24:13 +00:00
|
|
|
final RtpContentMap rtpContentMap =
|
|
|
|
isInitiator() ? this.initiatorRtpContentMap : this.responderRtpContentMap;
|
2022-02-25 16:26:36 +00:00
|
|
|
final IceUdpTransportInfo.Credentials credentials;
|
|
|
|
try {
|
|
|
|
credentials = rtpContentMap.getCredentials(iceCandidate.sdpMid);
|
|
|
|
} catch (final IllegalArgumentException e) {
|
|
|
|
Log.d(Config.LOGTAG, "ignoring (not sending) candidate: " + iceCandidate, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String uFrag = credentials.ufrag;
|
2022-02-25 15:24:13 +00:00
|
|
|
final IceUdpTransportInfo.Candidate candidate =
|
2022-02-25 16:26:36 +00:00
|
|
|
IceUdpTransportInfo.Candidate.fromSdpAttribute(iceCandidate.sdp, uFrag);
|
2021-11-14 17:22:18 +00:00
|
|
|
if (candidate == null) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(Config.LOGTAG, "ignoring (not sending) candidate: " + iceCandidate);
|
2021-11-14 17:22:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(Config.LOGTAG, "sending candidate: " + iceCandidate);
|
2020-04-06 11:01:17 +00:00
|
|
|
sendTransportInfo(iceCandidate.sdpMid, candidate);
|
|
|
|
}
|
2020-04-07 11:15:24 +00:00
|
|
|
|
|
|
|
@Override
|
2021-11-11 16:05:32 +00:00
|
|
|
public void onConnectionChange(final PeerConnection.PeerConnectionState newState) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": PeerConnectionState changed to " + newState);
|
2021-11-11 15:52:18 +00:00
|
|
|
this.stateHistory.add(newState);
|
|
|
|
if (newState == PeerConnection.PeerConnectionState.CONNECTED) {
|
|
|
|
this.sessionDuration.start();
|
2021-11-19 11:26:11 +00:00
|
|
|
updateOngoingCallNotification();
|
2021-11-11 15:52:18 +00:00
|
|
|
} else if (this.sessionDuration.isRunning()) {
|
|
|
|
this.sessionDuration.stop();
|
2021-11-19 11:26:11 +00:00
|
|
|
updateOngoingCallNotification();
|
2020-05-09 09:14:39 +00:00
|
|
|
}
|
2021-11-11 13:40:15 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
final boolean neverConnected =
|
|
|
|
!this.stateHistory.contains(PeerConnection.PeerConnectionState.CONNECTED);
|
2021-11-11 15:52:18 +00:00
|
|
|
|
2021-11-16 14:17:12 +00:00
|
|
|
if (newState == PeerConnection.PeerConnectionState.FAILED) {
|
|
|
|
if (neverConnected) {
|
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": not sending session-terminate after connectivity error because session is already in state "
|
|
|
|
+ this.state);
|
2021-11-16 14:17:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
webRTCWrapper.execute(this::closeWebRTCSessionAfterFailedConnection);
|
2020-04-12 07:59:32 +00:00
|
|
|
return;
|
2021-11-16 14:17:12 +00:00
|
|
|
} else {
|
2022-11-28 07:59:23 +00:00
|
|
|
this.restartIce();
|
2020-04-12 07:59:32 +00:00
|
|
|
}
|
2020-04-08 13:27:17 +00:00
|
|
|
}
|
2021-11-11 13:40:15 +00:00
|
|
|
updateEndUserState();
|
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
private void restartIce() {
|
|
|
|
this.stateHistory.clear();
|
2023-10-02 09:03:08 +00:00
|
|
|
this.webRTCWrapper.restartIceAsync();
|
2022-11-28 07:59:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 13:40:15 +00:00
|
|
|
@Override
|
|
|
|
public void onRenegotiationNeeded() {
|
2022-11-21 08:10:01 +00:00
|
|
|
this.webRTCWrapper.execute(this::renegotiate);
|
2021-11-11 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 08:10:01 +00:00
|
|
|
private void renegotiate() {
|
2021-11-14 17:22:18 +00:00
|
|
|
final SessionDescription sessionDescription;
|
2021-11-11 20:02:15 +00:00
|
|
|
try {
|
2021-11-14 17:22:18 +00:00
|
|
|
sessionDescription = setLocalSessionDescription();
|
2021-11-11 20:02:15 +00:00
|
|
|
} catch (final Exception e) {
|
2021-11-17 09:49:16 +00:00
|
|
|
final Throwable cause = Throwables.getRootCause(e);
|
2022-11-21 08:10:01 +00:00
|
|
|
webRTCWrapper.close();
|
2023-10-31 12:16:17 +00:00
|
|
|
if (isTerminated()) {
|
2023-11-13 11:54:55 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"failed to renegotiate. session was already terminated",
|
|
|
|
cause);
|
2023-10-31 12:16:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, "failed to renegotiate. sending session-terminate", cause);
|
2021-11-17 09:49:16 +00:00
|
|
|
sendSessionTerminate(Reason.FAILED_APPLICATION, cause.getMessage());
|
2021-11-14 17:22:18 +00:00
|
|
|
return;
|
2021-11-11 20:02:15 +00:00
|
|
|
}
|
2022-11-20 16:00:40 +00:00
|
|
|
final RtpContentMap rtpContentMap = RtpContentMap.of(sessionDescription, isInitiator());
|
2022-11-21 08:10:01 +00:00
|
|
|
final RtpContentMap currentContentMap = getLocalContentMap();
|
|
|
|
final boolean iceRestart = currentContentMap.iceRestart(rtpContentMap);
|
|
|
|
final RtpContentMap.Diff diff = currentContentMap.diff(rtpContentMap);
|
|
|
|
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": renegotiate. iceRestart="
|
|
|
|
+ iceRestart
|
|
|
|
+ " content id diff="
|
|
|
|
+ diff);
|
|
|
|
|
|
|
|
if (diff.hasModifications() && iceRestart) {
|
|
|
|
webRTCWrapper.close();
|
2022-11-28 07:59:23 +00:00
|
|
|
sendSessionTerminate(
|
|
|
|
Reason.FAILED_APPLICATION,
|
|
|
|
"WebRTC unexpectedly tried to modify content and transport at once");
|
2022-11-21 08:10:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iceRestart) {
|
|
|
|
initiateIceRestart(rtpContentMap);
|
|
|
|
return;
|
2022-11-28 07:59:23 +00:00
|
|
|
} else if (diff.isEmpty()) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"renegotiation. nothing to do. SignalingState="
|
|
|
|
+ this.webRTCWrapper.getSignalingState());
|
2022-11-21 08:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (diff.added.size() > 0) {
|
2022-11-28 07:59:23 +00:00
|
|
|
modifyLocalContentMap(rtpContentMap);
|
|
|
|
sendContentAdd(rtpContentMap, diff.added);
|
2022-11-21 08:10:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initiateIceRestart(final RtpContentMap rtpContentMap) {
|
2021-11-14 17:22:18 +00:00
|
|
|
final RtpContentMap transportInfo = rtpContentMap.transportInfo();
|
2022-02-25 15:24:13 +00:00
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
transportInfo.toJinglePacket(JinglePacket.Action.TRANSPORT_INFO, id.sessionId);
|
2021-11-14 17:22:18 +00:00
|
|
|
Log.d(Config.LOGTAG, "initiating ice restart: " + jinglePacket);
|
|
|
|
jinglePacket.setTo(id.with);
|
2022-02-25 15:24:13 +00:00
|
|
|
xmppConnectionService.sendIqPacket(
|
|
|
|
id.account,
|
|
|
|
jinglePacket,
|
|
|
|
(account, response) -> {
|
|
|
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
Log.d(Config.LOGTAG, "received success to our ice restart");
|
|
|
|
setLocalContentMap(rtpContentMap);
|
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (response.getType() == IqPacket.TYPE.ERROR) {
|
2022-11-28 07:59:23 +00:00
|
|
|
if (isTieBreak(response)) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(Config.LOGTAG, "received tie-break as result of ice restart");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleIqErrorResponse(response);
|
|
|
|
}
|
|
|
|
if (response.getType() == IqPacket.TYPE.TIMEOUT) {
|
|
|
|
handleIqTimeoutResponse(response);
|
|
|
|
}
|
|
|
|
});
|
2021-11-11 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
private boolean isTieBreak(final IqPacket response) {
|
|
|
|
final Element error = response.findChild("error");
|
|
|
|
return error != null && error.hasChild("tie-break", Namespace.JINGLE_ERRORS);
|
|
|
|
}
|
2022-11-21 08:10:01 +00:00
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
private void sendContentAdd(final RtpContentMap rtpContentMap, final Collection<String> added) {
|
|
|
|
final RtpContentMap contentAdd = rtpContentMap.toContentModification(added);
|
|
|
|
this.outgoingContentAdd = contentAdd;
|
2023-10-09 11:17:14 +00:00
|
|
|
final ListenableFuture<RtpContentMap> outgoingContentMapFuture =
|
|
|
|
prepareOutgoingContentMap(contentAdd);
|
|
|
|
Futures.addCallback(
|
|
|
|
outgoingContentMapFuture,
|
2023-11-13 11:54:55 +00:00
|
|
|
new FutureCallback<>() {
|
2023-10-09 11:17:14 +00:00
|
|
|
@Override
|
|
|
|
public void onSuccess(final RtpContentMap outgoingContentMap) {
|
|
|
|
sendContentAdd(outgoingContentMap);
|
|
|
|
webRTCWrapper.setIsReadyToReceiveIceCandidates(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Throwable throwable) {
|
|
|
|
failureToPerformAction(JinglePacket.Action.CONTENT_ADD, throwable);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
MoreExecutors.directExecutor());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendContentAdd(final RtpContentMap contentAdd) {
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
final JinglePacket jinglePacket =
|
|
|
|
contentAdd.toJinglePacket(JinglePacket.Action.CONTENT_ADD, id.sessionId);
|
|
|
|
jinglePacket.setTo(id.with);
|
|
|
|
xmppConnectionService.sendIqPacket(
|
|
|
|
id.account,
|
|
|
|
jinglePacket,
|
|
|
|
(connection, response) -> {
|
|
|
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.getAccount().getJid().asBareJid()
|
|
|
|
+ ": received ACK to our content-add");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (response.getType() == IqPacket.TYPE.ERROR) {
|
|
|
|
if (isTieBreak(response)) {
|
|
|
|
this.outgoingContentAdd = null;
|
|
|
|
Log.d(Config.LOGTAG, "received tie-break as result of our content-add");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handleIqErrorResponse(response);
|
|
|
|
}
|
|
|
|
if (response.getType() == IqPacket.TYPE.TIMEOUT) {
|
|
|
|
handleIqTimeoutResponse(response);
|
|
|
|
}
|
|
|
|
});
|
2022-11-21 08:10:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 17:22:18 +00:00
|
|
|
private void setLocalContentMap(final RtpContentMap rtpContentMap) {
|
2021-11-11 20:02:15 +00:00
|
|
|
if (isInitiator()) {
|
|
|
|
this.initiatorRtpContentMap = rtpContentMap;
|
|
|
|
} else {
|
|
|
|
this.responderRtpContentMap = rtpContentMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 10:21:11 +00:00
|
|
|
private void setRemoteContentMap(final RtpContentMap rtpContentMap) {
|
|
|
|
if (isInitiator()) {
|
|
|
|
this.responderRtpContentMap = rtpContentMap;
|
|
|
|
} else {
|
|
|
|
this.initiatorRtpContentMap = rtpContentMap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-28 07:59:23 +00:00
|
|
|
// this method is to be used for content map modifications that modify media
|
|
|
|
private void modifyLocalContentMap(final RtpContentMap rtpContentMap) {
|
|
|
|
final RtpContentMap activeContents = rtpContentMap.activeContents();
|
|
|
|
setLocalContentMap(activeContents);
|
|
|
|
this.webRTCWrapper.switchSpeakerPhonePreference(
|
|
|
|
AppRTCAudioManager.SpeakerPhonePreference.of(activeContents.getMedia()));
|
|
|
|
updateEndUserState();
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
private SessionDescription setLocalSessionDescription()
|
|
|
|
throws ExecutionException, InterruptedException {
|
|
|
|
final org.webrtc.SessionDescription sessionDescription =
|
2023-11-13 11:36:20 +00:00
|
|
|
this.webRTCWrapper.setLocalDescription(false).get();
|
2021-11-11 20:02:15 +00:00
|
|
|
return SessionDescription.parse(sessionDescription.description);
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 18:57:15 +00:00
|
|
|
private void closeWebRTCSessionAfterFailedConnection() {
|
|
|
|
this.webRTCWrapper.close();
|
2020-04-21 20:59:54 +00:00
|
|
|
synchronized (this) {
|
2020-04-24 07:41:54 +00:00
|
|
|
if (isTerminated()) {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": no need to send session-terminate after failed connection. Other party already did");
|
2020-04-21 20:59:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendSessionTerminate(Reason.CONNECTIVITY_ERROR);
|
|
|
|
}
|
2020-04-18 18:57:15 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 15:52:18 +00:00
|
|
|
public boolean zeroDuration() {
|
|
|
|
return this.sessionDuration.elapsed(TimeUnit.NANOSECONDS) <= 0;
|
2020-05-09 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
2021-11-11 15:52:18 +00:00
|
|
|
public long getCallDuration() {
|
|
|
|
return this.sessionDuration.elapsed(TimeUnit.MILLISECONDS);
|
2020-05-09 09:14:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 10:53:23 +00:00
|
|
|
public AppRTCAudioManager getAudioManager() {
|
|
|
|
return webRTCWrapper.getAudioManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isMicrophoneEnabled() {
|
|
|
|
return webRTCWrapper.isMicrophoneEnabled();
|
|
|
|
}
|
|
|
|
|
2020-07-09 17:14:28 +00:00
|
|
|
public boolean setMicrophoneEnabled(final boolean enabled) {
|
|
|
|
return webRTCWrapper.setMicrophoneEnabled(enabled);
|
2020-04-13 16:30:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 17:16:47 +00:00
|
|
|
public boolean isVideoEnabled() {
|
|
|
|
return webRTCWrapper.isVideoEnabled();
|
|
|
|
}
|
|
|
|
|
2020-05-10 12:09:16 +00:00
|
|
|
public void setVideoEnabled(final boolean enabled) {
|
|
|
|
webRTCWrapper.setVideoEnabled(enabled);
|
|
|
|
}
|
2020-05-02 15:15:46 +00:00
|
|
|
|
|
|
|
public boolean isCameraSwitchable() {
|
|
|
|
return webRTCWrapper.isCameraSwitchable();
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:08:11 +00:00
|
|
|
public boolean isFrontCamera() {
|
|
|
|
return webRTCWrapper.isFrontCamera();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ListenableFuture<Boolean> switchCamera() {
|
2020-05-02 15:15:46 +00:00
|
|
|
return webRTCWrapper.switchCamera();
|
|
|
|
}
|
|
|
|
|
2020-04-13 10:02:34 +00:00
|
|
|
@Override
|
2022-02-25 15:24:13 +00:00
|
|
|
public void onAudioDeviceChanged(
|
|
|
|
AppRTCAudioManager.AudioDevice selectedAudioDevice,
|
|
|
|
Set<AppRTCAudioManager.AudioDevice> availableAudioDevices) {
|
|
|
|
xmppConnectionService.notifyJingleRtpConnectionUpdate(
|
|
|
|
selectedAudioDevice, availableAudioDevices);
|
2020-04-13 10:02:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
private void updateEndUserState() {
|
2020-04-27 15:51:38 +00:00
|
|
|
final RtpEndUserState endUserState = getEndUserState();
|
2020-04-29 06:51:38 +00:00
|
|
|
jingleConnectionManager.toneManager.transition(isInitiator(), endUserState, getMedia());
|
2022-02-25 15:24:13 +00:00
|
|
|
xmppConnectionService.notifyJingleRtpConnectionUpdate(
|
|
|
|
id.account, id.with, id.sessionId, endUserState);
|
2020-04-07 11:15:24 +00:00
|
|
|
}
|
2020-04-08 15:52:47 +00:00
|
|
|
|
2020-04-10 13:19:56 +00:00
|
|
|
private void updateOngoingCallNotification() {
|
2021-11-19 11:26:11 +00:00
|
|
|
final State state = this.state;
|
|
|
|
if (STATES_SHOWING_ONGOING_CALL.contains(state)) {
|
|
|
|
final boolean reconnecting;
|
|
|
|
if (state == State.SESSION_ACCEPTED) {
|
2022-02-25 15:24:13 +00:00
|
|
|
reconnecting =
|
|
|
|
getPeerConnectionStateAsEndUserState() == RtpEndUserState.RECONNECTING;
|
2021-11-19 11:26:11 +00:00
|
|
|
} else {
|
|
|
|
reconnecting = false;
|
|
|
|
}
|
|
|
|
xmppConnectionService.setOngoingCall(id, getMedia(), reconnecting);
|
2020-04-10 13:19:56 +00:00
|
|
|
} else {
|
2020-04-15 20:40:37 +00:00
|
|
|
xmppConnectionService.removeOngoingCall();
|
2020-04-10 13:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:52:47 +00:00
|
|
|
private void discoverIceServers(final OnIceServersDiscovered onIceServersDiscovered) {
|
2020-04-21 09:40:05 +00:00
|
|
|
if (id.account.getXmppConnection().getFeatures().externalServiceDiscovery()) {
|
2020-04-08 15:52:47 +00:00
|
|
|
final IqPacket request = new IqPacket(IqPacket.TYPE.GET);
|
2020-05-17 08:24:46 +00:00
|
|
|
request.setTo(id.account.getDomain());
|
2020-04-08 15:52:47 +00:00
|
|
|
request.addChild("services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
|
2022-02-25 15:24:13 +00:00
|
|
|
xmppConnectionService.sendIqPacket(
|
|
|
|
id.account,
|
|
|
|
request,
|
|
|
|
(account, response) -> {
|
|
|
|
ImmutableList.Builder<PeerConnection.IceServer> listBuilder =
|
|
|
|
new ImmutableList.Builder<>();
|
|
|
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
|
|
|
final Element services =
|
|
|
|
response.findChild(
|
|
|
|
"services", Namespace.EXTERNAL_SERVICE_DISCOVERY);
|
|
|
|
final List<Element> children =
|
|
|
|
services == null
|
|
|
|
? Collections.emptyList()
|
|
|
|
: services.getChildren();
|
|
|
|
for (final Element child : children) {
|
|
|
|
if ("service".equals(child.getName())) {
|
|
|
|
final String type = child.getAttribute("type");
|
|
|
|
final String host = child.getAttribute("host");
|
|
|
|
final String sport = child.getAttribute("port");
|
|
|
|
final Integer port =
|
|
|
|
sport == null ? null : Ints.tryParse(sport);
|
|
|
|
final String transport = child.getAttribute("transport");
|
|
|
|
final String username = child.getAttribute("username");
|
|
|
|
final String password = child.getAttribute("password");
|
|
|
|
if (Strings.isNullOrEmpty(host) || port == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (port < 0 || port > 65535) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-29 14:19:01 +00:00
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
if (Arrays.asList("stun", "stuns", "turn", "turns")
|
|
|
|
.contains(type)
|
|
|
|
&& Arrays.asList("udp", "tcp").contains(transport)) {
|
|
|
|
if (Arrays.asList("stuns", "turns").contains(type)
|
|
|
|
&& "udp".equals(transport)) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": skipping invalid combination of udp/tls in external services");
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-29 14:19:01 +00:00
|
|
|
|
|
|
|
// STUN URLs do not support a query section since M110
|
|
|
|
final String uri;
|
2023-11-13 11:54:55 +00:00
|
|
|
if (Arrays.asList("stun", "stuns").contains(type)) {
|
|
|
|
uri =
|
|
|
|
String.format(
|
|
|
|
"%s:%s:%s",
|
|
|
|
type, IP.wrapIPv6(host), port);
|
2023-09-29 14:19:01 +00:00
|
|
|
} else {
|
2023-11-13 11:54:55 +00:00
|
|
|
uri =
|
|
|
|
String.format(
|
|
|
|
"%s:%s:%s?transport=%s",
|
|
|
|
type,
|
|
|
|
IP.wrapIPv6(host),
|
|
|
|
port,
|
|
|
|
transport);
|
2023-09-29 14:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 15:24:13 +00:00
|
|
|
final PeerConnection.IceServer.Builder iceServerBuilder =
|
2023-09-29 14:19:01 +00:00
|
|
|
PeerConnection.IceServer.builder(uri);
|
2022-02-25 15:24:13 +00:00
|
|
|
iceServerBuilder.setTlsCertPolicy(
|
|
|
|
PeerConnection.TlsCertPolicy
|
|
|
|
.TLS_CERT_POLICY_INSECURE_NO_CHECK);
|
|
|
|
if (username != null && password != null) {
|
|
|
|
iceServerBuilder.setUsername(username);
|
|
|
|
iceServerBuilder.setPassword(password);
|
|
|
|
} else if (Arrays.asList("turn", "turns").contains(type)) {
|
|
|
|
// The WebRTC spec requires throwing an
|
|
|
|
// InvalidAccessError when username (from libwebrtc
|
|
|
|
// source coder)
|
|
|
|
// https://chromium.googlesource.com/external/webrtc/+/master/pc/ice_server_parsing.cc
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": skipping "
|
|
|
|
+ type
|
|
|
|
+ "/"
|
|
|
|
+ transport
|
|
|
|
+ " without username and password");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final PeerConnection.IceServer iceServer =
|
|
|
|
iceServerBuilder.createIceServer();
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": discovered ICE Server: "
|
|
|
|
+ iceServer);
|
|
|
|
listBuilder.add(iceServer);
|
|
|
|
}
|
2020-04-17 12:16:39 +00:00
|
|
|
}
|
2020-04-08 15:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 15:24:13 +00:00
|
|
|
final List<PeerConnection.IceServer> iceServers = listBuilder.build();
|
|
|
|
if (iceServers.size() == 0) {
|
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid()
|
|
|
|
+ ": no ICE server found "
|
|
|
|
+ response);
|
|
|
|
}
|
|
|
|
onIceServersDiscovered.onIceServersDiscovered(iceServers);
|
|
|
|
});
|
2020-04-08 15:52:47 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
Log.w(
|
|
|
|
Config.LOGTAG,
|
|
|
|
id.account.getJid().asBareJid() + ": has no external service discovery");
|
2020-04-08 15:52:47 +00:00
|
|
|
onIceServersDiscovered.onIceServersDiscovered(Collections.emptyList());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 16:22:10 +00:00
|
|
|
private void finish() {
|
2020-05-08 16:36:52 +00:00
|
|
|
if (isTerminated()) {
|
|
|
|
this.cancelRingingTimeout();
|
|
|
|
this.webRTCWrapper.verifyClosed();
|
2020-06-11 19:17:13 +00:00
|
|
|
this.jingleConnectionManager.setTerminalSessionState(id, getEndUserState(), getMedia());
|
2020-06-07 12:59:55 +00:00
|
|
|
this.jingleConnectionManager.finishConnectionOrThrow(this);
|
2020-05-08 16:36:52 +00:00
|
|
|
} else {
|
2022-02-25 15:24:13 +00:00
|
|
|
throw new IllegalStateException(
|
|
|
|
String.format("Unable to call finish from %s", this.state));
|
2020-05-08 16:36:52 +00:00
|
|
|
}
|
2020-04-18 16:22:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 15:12:59 +00:00
|
|
|
private void writeLogMessage(final State state) {
|
2021-11-11 15:52:18 +00:00
|
|
|
final long duration = getCallDuration();
|
2022-02-25 15:24:13 +00:00
|
|
|
if (state == State.TERMINATED_SUCCESS
|
|
|
|
|| (state == State.TERMINATED_CONNECTIVITY_ERROR && duration > 0)) {
|
2020-04-12 15:12:59 +00:00
|
|
|
writeLogMessageSuccess(duration);
|
|
|
|
} else {
|
|
|
|
writeLogMessageMissed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeLogMessageSuccess(final long duration) {
|
|
|
|
this.message.setBody(new RtpSessionStatus(true, duration).toString());
|
|
|
|
this.writeMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeLogMessageMissed() {
|
2020-04-13 16:30:12 +00:00
|
|
|
this.message.setBody(new RtpSessionStatus(false, 0).toString());
|
2020-04-12 15:12:59 +00:00
|
|
|
this.writeMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeMessage() {
|
|
|
|
final Conversational conversational = message.getConversation();
|
|
|
|
if (conversational instanceof Conversation) {
|
|
|
|
((Conversation) conversational).add(this.message);
|
2020-06-12 07:08:09 +00:00
|
|
|
xmppConnectionService.createMessageAsync(message);
|
2020-04-12 15:12:59 +00:00
|
|
|
xmppConnectionService.updateConversationUi();
|
|
|
|
} else {
|
|
|
|
throw new IllegalStateException("Somehow the conversation in a message was a stub");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:19:56 +00:00
|
|
|
public State getState() {
|
|
|
|
return this.state;
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:22:27 +00:00
|
|
|
boolean isTerminated() {
|
2020-04-24 07:41:54 +00:00
|
|
|
return TERMINATED.contains(this.state);
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:51:38 +00:00
|
|
|
public Optional<VideoTrack> getLocalVideoTrack() {
|
2020-04-14 17:06:39 +00:00
|
|
|
return webRTCWrapper.getLocalVideoTrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Optional<VideoTrack> getRemoteVideoTrack() {
|
|
|
|
return webRTCWrapper.getRemoteVideoTrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
public EglBase.Context getEglBaseContext() {
|
|
|
|
return webRTCWrapper.getEglBaseContext();
|
|
|
|
}
|
|
|
|
|
2020-04-24 07:16:59 +00:00
|
|
|
void setProposedMedia(final Set<Media> media) {
|
2020-04-15 10:07:19 +00:00
|
|
|
this.proposedMedia = media;
|
2020-04-15 08:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 07:50:51 +00:00
|
|
|
public void fireStateUpdate() {
|
|
|
|
final RtpEndUserState endUserState = getEndUserState();
|
2022-02-25 15:24:13 +00:00
|
|
|
xmppConnectionService.notifyJingleRtpConnectionUpdate(
|
|
|
|
id.account, id.with, id.sessionId, endUserState);
|
2020-08-01 07:50:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 09:15:10 +00:00
|
|
|
public boolean isSwitchToVideoAvailable() {
|
|
|
|
final boolean prerequisite =
|
|
|
|
Media.audioOnly(getMedia())
|
|
|
|
&& Arrays.asList(RtpEndUserState.CONNECTED, RtpEndUserState.RECONNECTING)
|
|
|
|
.contains(getEndUserState());
|
|
|
|
return prerequisite && remoteHasVideoFeature();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean remoteHasVideoFeature() {
|
2023-11-13 11:36:20 +00:00
|
|
|
return remoteHasFeature(Namespace.JINGLE_FEATURE_VIDEO);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean remoteHasSdpOfferAnswer() {
|
|
|
|
return remoteHasFeature(Namespace.SDP_OFFER_ANSWER);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean remoteHasFeature(final String feature) {
|
2022-12-12 09:15:10 +00:00
|
|
|
final Contact contact = id.getContact();
|
|
|
|
final Presence presence =
|
|
|
|
contact.getPresences().get(Strings.nullToEmpty(id.with.getResource()));
|
|
|
|
final ServiceDiscoveryResult serviceDiscoveryResult =
|
|
|
|
presence == null ? null : presence.getServiceDiscoveryResult();
|
|
|
|
final List<String> features =
|
|
|
|
serviceDiscoveryResult == null ? null : serviceDiscoveryResult.getFeatures();
|
2023-11-13 11:36:20 +00:00
|
|
|
return features != null && features.contains(feature);
|
2022-12-12 09:15:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:52:47 +00:00
|
|
|
private interface OnIceServersDiscovered {
|
|
|
|
void onIceServersDiscovered(List<PeerConnection.IceServer> iceServers);
|
|
|
|
}
|
2020-04-02 09:30:16 +00:00
|
|
|
}
|