2014-04-08 21:15:55 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2020-04-10 13:19:56 +00:00
|
|
|
import android.util.Base64;
|
2015-07-20 12:26:29 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
2020-04-04 09:31:53 +00:00
|
|
|
import com.google.common.base.Objects;
|
2020-04-01 08:45:03 +00:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
2020-04-10 13:19:56 +00:00
|
|
|
import java.security.SecureRandom;
|
2014-04-19 00:19:26 +00:00
|
|
|
import java.util.HashMap;
|
2020-04-01 08:45:03 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2015-07-20 12:26:29 +00:00
|
|
|
|
2014-08-31 14:28:21 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Message;
|
2015-07-20 12:26:29 +00:00
|
|
|
import eu.siacs.conversations.entities.Transferable;
|
2014-10-14 10:02:48 +00:00
|
|
|
import eu.siacs.conversations.services.AbstractConnectionManager;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2020-04-01 08:45:03 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
2020-04-01 16:35:36 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
|
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.FileTransferDescription;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
|
2020-04-10 14:02:01 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
|
2014-04-08 21:15:55 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
2020-04-04 09:31:53 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
2014-04-08 21:15:55 +00:00
|
|
|
|
2014-10-13 23:06:45 +00:00
|
|
|
public class JingleConnectionManager extends AbstractConnectionManager {
|
2020-04-07 09:36:28 +00:00
|
|
|
private final HashMap<RtpSessionProposal, DeviceDiscoveryState> rtpSessionProposals = new HashMap<>();
|
2020-04-04 09:31:53 +00:00
|
|
|
private final Map<AbstractJingleConnection.Id, AbstractJingleConnection> connections = new ConcurrentHashMap<>();
|
2019-12-05 13:03:54 +00:00
|
|
|
|
|
|
|
private HashMap<Jid, JingleCandidate> primaryCandidates = new HashMap<>();
|
|
|
|
|
|
|
|
public JingleConnectionManager(XmppConnectionService service) {
|
|
|
|
super(service);
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:02:01 +00:00
|
|
|
static String nextRandomId() {
|
|
|
|
final byte[] id = new byte[16];
|
|
|
|
new SecureRandom().nextBytes(id);
|
|
|
|
return Base64.encodeToString(id, Base64.NO_WRAP | Base64.NO_PADDING);
|
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
public void deliverPacket(final Account account, final JinglePacket packet) {
|
2020-04-10 17:22:29 +00:00
|
|
|
final String sessionId = packet.getSessionId();
|
|
|
|
if (sessionId == null) {
|
|
|
|
respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-01 08:45:03 +00:00
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, packet);
|
2020-04-02 14:29:33 +00:00
|
|
|
final AbstractJingleConnection existingJingleConnection = connections.get(id);
|
|
|
|
if (existingJingleConnection != null) {
|
|
|
|
existingJingleConnection.deliverPacket(packet);
|
|
|
|
} else if (packet.getAction() == JinglePacket.Action.SESSION_INITIATE) {
|
2020-04-03 06:16:55 +00:00
|
|
|
final Jid from = packet.getFrom();
|
2020-04-01 16:35:36 +00:00
|
|
|
final Content content = packet.getJingleContent();
|
|
|
|
final String descriptionNamespace = content == null ? null : content.getDescriptionNamespace();
|
|
|
|
final AbstractJingleConnection connection;
|
|
|
|
if (FileTransferDescription.NAMESPACES.contains(descriptionNamespace)) {
|
2020-04-03 06:16:55 +00:00
|
|
|
connection = new JingleFileTransferConnection(this, id, from);
|
2020-04-10 14:28:15 +00:00
|
|
|
} else if (Namespace.JINGLE_APPS_RTP.equals(descriptionNamespace) && !usesTor(account)) {
|
2020-04-10 14:02:01 +00:00
|
|
|
if (isBusy()) {
|
|
|
|
mXmppConnectionService.sendIqPacket(account, packet.generateResponse(IqPacket.TYPE.RESULT), null);
|
|
|
|
final JinglePacket sessionTermination = new JinglePacket(JinglePacket.Action.SESSION_TERMINATE, id.sessionId);
|
|
|
|
sessionTermination.setTo(id.with);
|
|
|
|
sessionTermination.setReason(Reason.BUSY, null);
|
|
|
|
mXmppConnectionService.sendIqPacket(account, sessionTermination, null);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-03 06:16:55 +00:00
|
|
|
connection = new JingleRtpConnection(this, id, from);
|
2020-04-01 16:35:36 +00:00
|
|
|
} else {
|
2020-04-09 13:22:03 +00:00
|
|
|
respondWithJingleError(account, packet, "unsupported-info", "feature-not-implemented", "cancel");
|
2020-04-01 16:35:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-01 08:45:03 +00:00
|
|
|
connections.put(id, connection);
|
2020-04-01 16:35:36 +00:00
|
|
|
connection.deliverPacket(packet);
|
2019-12-05 13:03:54 +00:00
|
|
|
} else {
|
2020-04-02 14:29:33 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to route jingle packet: " + packet);
|
2020-04-09 13:22:03 +00:00
|
|
|
respondWithJingleError(account, packet, "unknown-session", "item-not-found", "cancel");
|
2020-04-02 14:29:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:28:15 +00:00
|
|
|
private boolean usesTor(final Account account) {
|
|
|
|
return account.isOnion() || mXmppConnectionService.useTorToConnect();
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:02:01 +00:00
|
|
|
private boolean isBusy() {
|
|
|
|
for (AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
if (connection instanceof JingleRtpConnection) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
synchronized (this.rtpSessionProposals) {
|
|
|
|
return this.rtpSessionProposals.containsValue(DeviceDiscoveryState.DISCOVERED) || this.rtpSessionProposals.containsValue(DeviceDiscoveryState.SEARCHING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 13:22:03 +00:00
|
|
|
public void respondWithJingleError(final Account account, final IqPacket original, String jingleCondition, String condition, String conditionType) {
|
|
|
|
final IqPacket response = original.generateResponse(IqPacket.TYPE.ERROR);
|
|
|
|
final Element error = response.addChild("error");
|
|
|
|
error.setAttribute("type", conditionType);
|
|
|
|
error.addChild(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
|
|
|
|
error.addChild(jingleCondition, "urn:xmpp:jingle:errors:1");
|
|
|
|
account.getXmppConnection().sendIqPacket(response, null);
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:29:33 +00:00
|
|
|
public void deliverMessage(final Account account, final Jid to, final Jid from, final Element message) {
|
|
|
|
Preconditions.checkArgument(Namespace.JINGLE_MESSAGE.equals(message.getNamespace()));
|
|
|
|
final String sessionId = message.getAttribute("id");
|
|
|
|
if (sessionId == null) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-08 10:17:46 +00:00
|
|
|
if ("accept".equals(message.getName())) {
|
|
|
|
for (AbstractJingleConnection connection : connections.values()) {
|
|
|
|
if (connection instanceof JingleRtpConnection) {
|
|
|
|
final JingleRtpConnection rtpConnection = (JingleRtpConnection) connection;
|
|
|
|
final AbstractJingleConnection.Id id = connection.getId();
|
|
|
|
if (id.account == account && id.sessionId.equals(sessionId)) {
|
|
|
|
rtpConnection.deliveryMessage(from, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-04-10 19:18:43 +00:00
|
|
|
final boolean addressedToSelf = from.asBareJid().equals(account.getJid().asBareJid());
|
|
|
|
final AbstractJingleConnection.Id id;
|
|
|
|
if (addressedToSelf) {
|
|
|
|
if (to.isFullJid()) {
|
|
|
|
id = AbstractJingleConnection.Id.of(account, to, sessionId);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
} else {
|
2020-04-10 19:18:43 +00:00
|
|
|
id = AbstractJingleConnection.Id.of(account, from, sessionId);
|
2020-04-02 14:29:33 +00:00
|
|
|
}
|
|
|
|
final AbstractJingleConnection existingJingleConnection = connections.get(id);
|
|
|
|
if (existingJingleConnection != null) {
|
|
|
|
if (existingJingleConnection instanceof JingleRtpConnection) {
|
2020-04-03 08:46:42 +00:00
|
|
|
((JingleRtpConnection) existingJingleConnection).deliveryMessage(from, message);
|
2020-04-02 14:29:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + existingJingleConnection.getClass().getName() + " does not support jingle messages");
|
|
|
|
}
|
2020-04-10 14:02:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-10 19:18:43 +00:00
|
|
|
|
|
|
|
if (addressedToSelf) {
|
2020-04-10 14:02:01 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignore jingle message from self");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("propose".equals(message.getName())) {
|
2020-04-02 14:29:33 +00:00
|
|
|
final Element description = message.findChild("description");
|
|
|
|
final String namespace = description == null ? null : description.getNamespace();
|
2020-04-10 14:28:15 +00:00
|
|
|
if (Namespace.JINGLE_APPS_RTP.equals(namespace) && !usesTor(account)) {
|
2020-04-10 14:02:01 +00:00
|
|
|
if (isBusy()) {
|
|
|
|
final MessagePacket reject = mXmppConnectionService.getMessageGenerator().sessionReject(from, sessionId);
|
|
|
|
mXmppConnectionService.sendMessagePacket(account, reject);
|
|
|
|
} else {
|
2020-04-10 19:18:43 +00:00
|
|
|
final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, from);
|
2020-04-10 14:02:01 +00:00
|
|
|
this.connections.put(id, rtpConnection);
|
|
|
|
rtpConnection.deliveryMessage(from, message);
|
|
|
|
}
|
2020-04-01 08:45:03 +00:00
|
|
|
} else {
|
2020-04-02 14:29:33 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to react to proposed " + namespace + " session");
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
2020-04-04 09:31:53 +00:00
|
|
|
} else if ("proceed".equals(message.getName())) {
|
2020-04-10 14:02:01 +00:00
|
|
|
|
2020-04-10 19:18:43 +00:00
|
|
|
final RtpSessionProposal proposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
|
2020-04-04 09:31:53 +00:00
|
|
|
synchronized (rtpSessionProposals) {
|
2020-04-07 09:36:28 +00:00
|
|
|
if (rtpSessionProposals.remove(proposal) != null) {
|
2020-04-04 09:31:53 +00:00
|
|
|
final JingleRtpConnection rtpConnection = new JingleRtpConnection(this, id, account.getJid());
|
|
|
|
this.connections.put(id, rtpConnection);
|
|
|
|
rtpConnection.transitionOrThrow(AbstractJingleConnection.State.PROPOSED);
|
|
|
|
rtpConnection.deliveryMessage(from, message);
|
|
|
|
} else {
|
2020-04-10 19:18:43 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver proceed");
|
2020-04-08 08:46:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ("reject".equals(message.getName())) {
|
2020-04-10 19:18:43 +00:00
|
|
|
final RtpSessionProposal proposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
|
2020-04-08 08:46:26 +00:00
|
|
|
synchronized (rtpSessionProposals) {
|
|
|
|
if (rtpSessionProposals.remove(proposal) != null) {
|
|
|
|
mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, proposal.with, proposal.sessionId, RtpEndUserState.DECLINED_OR_BUSY);
|
|
|
|
} else {
|
2020-04-10 19:18:43 +00:00
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": no rtp session proposal found for " + from + " to deliver reject");
|
2020-04-04 09:31:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved out of order jingle message");
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
2020-04-02 14:29:33 +00:00
|
|
|
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
public void startJingleFileTransfer(final Message message) {
|
|
|
|
Preconditions.checkArgument(message.isFileOrImage(), "Message is not of type file or image");
|
|
|
|
final Transferable old = message.getTransferable();
|
2019-12-05 13:03:54 +00:00
|
|
|
if (old != null) {
|
|
|
|
old.cancel();
|
|
|
|
}
|
2020-04-03 06:16:55 +00:00
|
|
|
final Account account = message.getConversation().getAccount();
|
2020-04-01 08:45:03 +00:00
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(message);
|
2020-04-03 06:16:55 +00:00
|
|
|
final JingleFileTransferConnection connection = new JingleFileTransferConnection(this, id, account.getJid());
|
2019-12-05 13:03:54 +00:00
|
|
|
mXmppConnectionService.markMessage(message, Message.STATUS_WAITING);
|
2020-04-01 08:45:03 +00:00
|
|
|
this.connections.put(id, connection);
|
2020-04-03 06:16:55 +00:00
|
|
|
connection.init(message);
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
void finishConnection(final AbstractJingleConnection connection) {
|
|
|
|
this.connections.remove(connection.getId());
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 08:45:03 +00:00
|
|
|
void getPrimaryCandidate(final Account account, final boolean initiator, final OnPrimaryCandidateFound listener) {
|
2019-12-05 13:03:54 +00:00
|
|
|
if (Config.DISABLE_PROXY_LOOKUP) {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.primaryCandidates.containsKey(account.getJid().asBareJid())) {
|
|
|
|
final Jid proxy = account.getXmppConnection().findDiscoItemByFeature(Namespace.BYTE_STREAMS);
|
|
|
|
if (proxy != null) {
|
|
|
|
IqPacket iq = new IqPacket(IqPacket.TYPE.GET);
|
|
|
|
iq.setTo(proxy);
|
|
|
|
iq.query(Namespace.BYTE_STREAMS);
|
|
|
|
account.getXmppConnection().sendIqPacket(iq, new OnIqPacketReceived() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onIqPacketReceived(Account account, IqPacket packet) {
|
2020-04-01 08:45:03 +00:00
|
|
|
final Element streamhost = packet.query().findChild("streamhost", Namespace.BYTE_STREAMS);
|
2019-12-05 13:03:54 +00:00
|
|
|
final String host = streamhost == null ? null : streamhost.getAttribute("host");
|
|
|
|
final String port = streamhost == null ? null : streamhost.getAttribute("port");
|
|
|
|
if (host != null && port != null) {
|
|
|
|
try {
|
|
|
|
JingleCandidate candidate = new JingleCandidate(nextRandomId(), true);
|
|
|
|
candidate.setHost(host);
|
|
|
|
candidate.setPort(Integer.parseInt(port));
|
|
|
|
candidate.setType(JingleCandidate.TYPE_PROXY);
|
|
|
|
candidate.setJid(proxy);
|
|
|
|
candidate.setPriority(655360 + (initiator ? 30 : 0));
|
|
|
|
primaryCandidates.put(account.getJid().asBareJid(), candidate);
|
|
|
|
listener.onPrimaryCandidateFound(true, candidate);
|
|
|
|
} catch (final NumberFormatException e) {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(false, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
listener.onPrimaryCandidateFound(true,
|
|
|
|
this.primaryCandidates.get(account.getJid().asBareJid()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 07:42:06 +00:00
|
|
|
public void retractSessionProposal(final Account account, final Jid with) {
|
2020-04-04 09:31:53 +00:00
|
|
|
synchronized (this.rtpSessionProposals) {
|
2020-04-08 07:42:06 +00:00
|
|
|
RtpSessionProposal matchingProposal = null;
|
|
|
|
for (RtpSessionProposal proposal : this.rtpSessionProposals.keySet()) {
|
|
|
|
if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
|
|
|
|
matchingProposal = proposal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (matchingProposal != null) {
|
|
|
|
this.rtpSessionProposals.remove(matchingProposal);
|
|
|
|
final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionRetract(matchingProposal);
|
|
|
|
Log.d(Config.LOGTAG, messagePacket.toString());
|
|
|
|
mXmppConnectionService.sendMessagePacket(account, messagePacket);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void proposeJingleRtpSession(final Account account, final Jid with) {
|
|
|
|
synchronized (this.rtpSessionProposals) {
|
|
|
|
for (Map.Entry<RtpSessionProposal, DeviceDiscoveryState> entry : this.rtpSessionProposals.entrySet()) {
|
|
|
|
RtpSessionProposal proposal = entry.getKey();
|
|
|
|
if (proposal.account == account && with.asBareJid().equals(proposal.with)) {
|
|
|
|
final DeviceDiscoveryState preexistingState = entry.getValue();
|
|
|
|
if (preexistingState != null && preexistingState != DeviceDiscoveryState.FAILED) {
|
|
|
|
mXmppConnectionService.notifyJingleRtpConnectionUpdate(
|
|
|
|
account,
|
|
|
|
with,
|
|
|
|
proposal.sessionId,
|
|
|
|
preexistingState.toEndUserState()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final RtpSessionProposal proposal = RtpSessionProposal.of(account, with.asBareJid());
|
2020-04-07 09:36:28 +00:00
|
|
|
this.rtpSessionProposals.put(proposal, DeviceDiscoveryState.SEARCHING);
|
2020-04-08 07:42:06 +00:00
|
|
|
mXmppConnectionService.notifyJingleRtpConnectionUpdate(
|
|
|
|
account,
|
|
|
|
proposal.with,
|
|
|
|
proposal.sessionId,
|
|
|
|
RtpEndUserState.FINDING_DEVICE
|
|
|
|
);
|
2020-04-04 09:31:53 +00:00
|
|
|
final MessagePacket messagePacket = mXmppConnectionService.getMessageGenerator().sessionProposal(proposal);
|
2020-04-08 07:42:06 +00:00
|
|
|
Log.d(Config.LOGTAG, messagePacket.toString());
|
2020-04-04 09:31:53 +00:00
|
|
|
mXmppConnectionService.sendMessagePacket(account, messagePacket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 13:03:54 +00:00
|
|
|
public void deliverIbbPacket(Account account, IqPacket packet) {
|
2020-04-01 13:17:38 +00:00
|
|
|
final String sid;
|
|
|
|
final Element payload;
|
2020-04-01 08:45:03 +00:00
|
|
|
if (packet.hasChild("open", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("open", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 08:45:03 +00:00
|
|
|
} else if (packet.hasChild("data", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("data", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 08:45:03 +00:00
|
|
|
} else if (packet.hasChild("close", Namespace.IBB)) {
|
|
|
|
payload = packet.findChild("close", Namespace.IBB);
|
2019-12-05 13:03:54 +00:00
|
|
|
sid = payload.getAttribute("sid");
|
2020-04-01 13:17:38 +00:00
|
|
|
} else {
|
|
|
|
payload = null;
|
|
|
|
sid = null;
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
if (sid != null) {
|
2020-04-01 08:45:03 +00:00
|
|
|
for (final AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
if (connection instanceof JingleFileTransferConnection) {
|
|
|
|
final JingleFileTransferConnection fileTransfer = (JingleFileTransferConnection) connection;
|
|
|
|
final JingleTransport transport = fileTransfer.getTransport();
|
2019-12-05 13:03:54 +00:00
|
|
|
if (transport instanceof JingleInBandTransport) {
|
2020-04-01 13:17:38 +00:00
|
|
|
final JingleInBandTransport inBandTransport = (JingleInBandTransport) transport;
|
|
|
|
if (inBandTransport.matches(account, sid)) {
|
|
|
|
inBandTransport.deliverPayload(packet, payload);
|
|
|
|
}
|
2019-12-05 13:03:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.d(Config.LOGTAG, "unable to deliver ibb packet: " + packet.toString());
|
|
|
|
account.getXmppConnection().sendIqPacket(packet.generateResponse(IqPacket.TYPE.ERROR), null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelInTransmission() {
|
2020-04-01 08:45:03 +00:00
|
|
|
for (AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
/*if (connection.getJingleStatus() == JingleFileTransferConnection.JINGLE_STATUS_TRANSMITTING) {
|
2019-12-05 13:03:54 +00:00
|
|
|
connection.abort("connectivity-error");
|
2020-04-01 08:45:03 +00:00
|
|
|
}*/
|
2019-12-05 13:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-04 09:31:53 +00:00
|
|
|
|
2020-04-07 11:15:24 +00:00
|
|
|
public WeakReference<JingleRtpConnection> findJingleRtpConnection(Account account, Jid with, String sessionId) {
|
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, Jid.ofEscaped(with), sessionId);
|
|
|
|
final AbstractJingleConnection connection = connections.get(id);
|
|
|
|
if (connection instanceof JingleRtpConnection) {
|
|
|
|
return new WeakReference<>((JingleRtpConnection) connection);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-07 09:36:28 +00:00
|
|
|
public void updateProposedSessionDiscovered(Account account, Jid from, String sessionId, final DeviceDiscoveryState target) {
|
2020-04-08 07:42:06 +00:00
|
|
|
final RtpSessionProposal sessionProposal = new RtpSessionProposal(account, from.asBareJid(), sessionId);
|
2020-04-07 09:36:28 +00:00
|
|
|
synchronized (this.rtpSessionProposals) {
|
|
|
|
final DeviceDiscoveryState currentState = rtpSessionProposals.get(sessionProposal);
|
|
|
|
if (currentState == null) {
|
2020-04-08 07:42:06 +00:00
|
|
|
Log.d(Config.LOGTAG, "unable to find session proposal for session id " + sessionId);
|
2020-04-07 09:36:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (currentState == DeviceDiscoveryState.DISCOVERED) {
|
2020-04-08 07:42:06 +00:00
|
|
|
Log.d(Config.LOGTAG, "session proposal already at discovered. not going to fall back");
|
2020-04-07 09:36:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.rtpSessionProposals.put(sessionProposal, target);
|
2020-04-08 07:42:06 +00:00
|
|
|
mXmppConnectionService.notifyJingleRtpConnectionUpdate(account, sessionProposal.with, sessionProposal.sessionId, target.toEndUserState());
|
|
|
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": flagging session " + sessionId + " as " + target);
|
2020-04-07 09:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 19:26:51 +00:00
|
|
|
public void rejectRtpSession(final String sessionId) {
|
2020-04-08 07:42:06 +00:00
|
|
|
for (final AbstractJingleConnection connection : this.connections.values()) {
|
2020-04-07 19:26:51 +00:00
|
|
|
if (connection.getId().sessionId.equals(sessionId)) {
|
|
|
|
if (connection instanceof JingleRtpConnection) {
|
|
|
|
((JingleRtpConnection) connection).rejectCall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 13:19:56 +00:00
|
|
|
public void endRtpSession(final String sessionId) {
|
|
|
|
for (final AbstractJingleConnection connection : this.connections.values()) {
|
|
|
|
if (connection.getId().sessionId.equals(sessionId)) {
|
|
|
|
if (connection instanceof JingleRtpConnection) {
|
|
|
|
((JingleRtpConnection) connection).endCall();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 05:45:23 +00:00
|
|
|
public void failProceed(Account account, final Jid with, String sessionId) {
|
|
|
|
final AbstractJingleConnection.Id id = AbstractJingleConnection.Id.of(account, with, sessionId);
|
|
|
|
final AbstractJingleConnection existingJingleConnection = connections.get(id);
|
|
|
|
if (existingJingleConnection instanceof JingleRtpConnection) {
|
|
|
|
((JingleRtpConnection) existingJingleConnection).deliverFailedProceed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 14:02:01 +00:00
|
|
|
public enum DeviceDiscoveryState {
|
|
|
|
SEARCHING, DISCOVERED, FAILED;
|
|
|
|
|
|
|
|
public RtpEndUserState toEndUserState() {
|
|
|
|
switch (this) {
|
|
|
|
case SEARCHING:
|
|
|
|
return RtpEndUserState.FINDING_DEVICE;
|
|
|
|
case DISCOVERED:
|
|
|
|
return RtpEndUserState.RINGING;
|
|
|
|
default:
|
|
|
|
return RtpEndUserState.CONNECTIVITY_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 09:31:53 +00:00
|
|
|
public static class RtpSessionProposal {
|
|
|
|
public final Jid with;
|
|
|
|
public final String sessionId;
|
2020-04-10 14:02:01 +00:00
|
|
|
private final Account account;
|
2020-04-04 09:31:53 +00:00
|
|
|
|
|
|
|
private RtpSessionProposal(Account account, Jid with, String sessionId) {
|
|
|
|
this.account = account;
|
|
|
|
this.with = with;
|
|
|
|
this.sessionId = sessionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RtpSessionProposal of(Account account, Jid with) {
|
2020-04-10 13:19:56 +00:00
|
|
|
return new RtpSessionProposal(account, with, nextRandomId());
|
2020-04-04 09:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
RtpSessionProposal proposal = (RtpSessionProposal) o;
|
|
|
|
return Objects.equal(account.getJid(), proposal.account.getJid()) &&
|
|
|
|
Objects.equal(with, proposal.with) &&
|
|
|
|
Objects.equal(sessionId, proposal.sessionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return Objects.hashCode(account.getJid(), with, sessionId);
|
|
|
|
}
|
|
|
|
}
|
2014-04-08 21:15:55 +00:00
|
|
|
}
|