2020-04-03 14:44:29 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
2021-01-21 15:47:30 +00:00
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
2020-05-30 12:56:12 +00:00
|
|
|
import java.util.ArrayList;
|
2020-04-03 14:44:29 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2020-05-30 12:56:12 +00:00
|
|
|
import java.util.Map;
|
2020-04-03 14:44:29 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Presence;
|
|
|
|
import eu.siacs.conversations.entities.Presences;
|
|
|
|
import eu.siacs.conversations.entities.ServiceDiscoveryResult;
|
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
|
|
|
|
|
|
|
public class RtpCapability {
|
|
|
|
|
2021-01-23 08:25:34 +00:00
|
|
|
private static final List<String> BASIC_RTP_REQUIREMENTS = Arrays.asList(
|
2020-04-03 14:44:29 +00:00
|
|
|
Namespace.JINGLE,
|
|
|
|
Namespace.JINGLE_TRANSPORT_ICE_UDP,
|
|
|
|
Namespace.JINGLE_APPS_RTP,
|
|
|
|
Namespace.JINGLE_APPS_DTLS
|
|
|
|
);
|
2021-01-21 15:47:30 +00:00
|
|
|
private static final List<String> VIDEO_REQUIREMENTS = Arrays.asList(
|
2020-04-03 14:44:29 +00:00
|
|
|
Namespace.JINGLE_FEATURE_AUDIO,
|
|
|
|
Namespace.JINGLE_FEATURE_VIDEO
|
|
|
|
);
|
|
|
|
|
|
|
|
public static Capability check(final Presence presence) {
|
|
|
|
final ServiceDiscoveryResult disco = presence.getServiceDiscoveryResult();
|
|
|
|
final List<String> features = disco == null ? Collections.emptyList() : disco.getFeatures();
|
|
|
|
if (features.containsAll(BASIC_RTP_REQUIREMENTS)) {
|
|
|
|
if (features.containsAll(VIDEO_REQUIREMENTS)) {
|
|
|
|
return Capability.VIDEO;
|
|
|
|
}
|
|
|
|
if (features.contains(Namespace.JINGLE_FEATURE_AUDIO)) {
|
|
|
|
return Capability.AUDIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Capability.NONE;
|
|
|
|
}
|
|
|
|
|
2020-05-30 12:56:12 +00:00
|
|
|
public static String[] filterPresences(final Contact contact, Capability required) {
|
|
|
|
final Presences presences = contact.getPresences();
|
|
|
|
final ArrayList<String> resources = new ArrayList<>();
|
2021-01-21 15:47:30 +00:00
|
|
|
for (final Map.Entry<String, Presence> presence : presences.getPresencesMap().entrySet()) {
|
2020-05-30 12:56:12 +00:00
|
|
|
final Capability capability = check(presence.getValue());
|
|
|
|
if (capability == Capability.NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (required == Capability.AUDIO || capability == required) {
|
|
|
|
resources.add(presence.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resources.toArray(new String[0]);
|
|
|
|
}
|
|
|
|
|
2020-04-03 14:44:29 +00:00
|
|
|
public static Capability check(final Contact contact) {
|
2021-01-21 15:47:30 +00:00
|
|
|
return check(contact, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Capability check(final Contact contact, final boolean allowFallback) {
|
2020-04-03 14:44:29 +00:00
|
|
|
final Presences presences = contact.getPresences();
|
2021-01-21 15:47:30 +00:00
|
|
|
if (presences.size() == 0 && allowFallback) {
|
|
|
|
return contact.getRtpCapability();
|
|
|
|
}
|
2020-04-03 14:44:29 +00:00
|
|
|
Capability result = Capability.NONE;
|
2021-01-21 15:47:30 +00:00
|
|
|
for (final Presence presence : presences.getPresences()) {
|
2020-04-03 14:44:29 +00:00
|
|
|
Capability capability = check(presence);
|
|
|
|
if (capability == Capability.VIDEO) {
|
|
|
|
result = capability;
|
|
|
|
} else if (capability == Capability.AUDIO && result == Capability.NONE) {
|
|
|
|
result = capability;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Capability {
|
2021-01-21 15:47:30 +00:00
|
|
|
NONE, AUDIO, VIDEO;
|
|
|
|
|
|
|
|
public static Capability of(String value) {
|
|
|
|
if (Strings.isNullOrEmpty(value)) {
|
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return valueOf(value);
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
return NONE;
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 14:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|