conversations-classic/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleRtpConnection.java

106 lines
3.9 KiB
Java
Raw Normal View History

2020-04-02 09:30:16 +00:00
package eu.siacs.conversations.xmpp.jingle;
import android.util.Log;
2020-04-02 19:12:38 +00:00
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.Map;
2020-04-02 09:30:16 +00:00
import eu.siacs.conversations.Config;
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-04-02 09:30:16 +00:00
import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
2020-04-02 19:12:38 +00:00
import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
2020-04-02 14:29:33 +00:00
import rocks.xmpp.addr.Jid;
2020-04-02 09:30:16 +00:00
public class JingleRtpConnection extends AbstractJingleConnection {
2020-04-02 19:12:38 +00:00
private static final Map<State, Collection<State>> VALID_TRANSITIONS;
static {
final ImmutableMap.Builder<State, Collection<State>> transitionBuilder = new ImmutableMap.Builder<>();
transitionBuilder.put(State.NULL, ImmutableList.of(State.PROPOSED, State.SESSION_INITIALIZED));
transitionBuilder.put(State.PROPOSED, ImmutableList.of(State.ACCEPTED, State.PROCEED));
VALID_TRANSITIONS = transitionBuilder.build();
}
private State state = State.NULL;
2020-04-02 09:30:16 +00:00
public JingleRtpConnection(JingleConnectionManager jingleConnectionManager, Id id) {
super(jingleConnectionManager, id);
}
@Override
void deliverPacket(final JinglePacket jinglePacket) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": packet delivered to JingleRtpConnection");
2020-04-02 19:12:38 +00:00
Log.d(Config.LOGTAG, jinglePacket.toString());
2020-04-02 09:30:16 +00:00
}
2020-04-02 14:29:33 +00:00
2020-04-02 19:12:38 +00:00
void deliveryMessage(final Jid to, final Jid from, final Element message) {
2020-04-02 14:29:33 +00:00
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": delivered message to JingleRtpConnection " + message);
2020-04-02 19:12:38 +00:00
final boolean originatedFromMyself = from.asBareJid().equals(id.account.getJid().asBareJid());
switch (message.getName()) {
case "propose":
if (originatedFromMyself) {
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": saw proposal from mysql. ignoring");
} else if (transition(State.PROPOSED)) {
//TODO start ringing or something
pickUpCall();
} else {
Log.d(Config.LOGTAG, id.account.getJid() + ": ignoring session proposal because already in " + state);
}
break;
default:
break;
}
}
public void pickUpCall() {
switch (this.state) {
case PROPOSED:
pickupCallFromProposed();
break;
case SESSION_INITIALIZED:
pickupCallFromSessionInitialized();
break;
default:
throw new IllegalStateException("Can not pick up call from " + this.state);
}
}
private void pickupCallFromProposed() {
transitionOrThrow(State.PROCEED);
final MessagePacket messagePacket = new MessagePacket();
messagePacket.setTo(id.with);
//Note that Movim needs 'accept'
messagePacket.addChild("proceed", Namespace.JINGLE_MESSAGE).setAttribute("id", id.sessionId);
Log.d(Config.LOGTAG, messagePacket.toString());
xmppConnectionService.sendMessagePacket(id.account, messagePacket);
2020-04-02 14:29:33 +00:00
}
2020-04-02 19:12:38 +00:00
private void pickupCallFromSessionInitialized() {
}
private synchronized boolean transition(final State target) {
final Collection<State> validTransitions = VALID_TRANSITIONS.get(this.state);
if (validTransitions != null && validTransitions.contains(target)) {
this.state = target;
Log.d(Config.LOGTAG, id.account.getJid().asBareJid() + ": transitioned into " + target);
return true;
} else {
return false;
}
}
private void transitionOrThrow(final State target) {
if (!transition(target)) {
throw new IllegalStateException(String.format("Unable to transition from %s to %s", this.state, target));
}
}
2020-04-02 09:30:16 +00:00
}