2020-04-04 08:45:42 +00:00
|
|
|
package eu.siacs.conversations.xmpp.jingle;
|
|
|
|
|
|
|
|
import android.util.Log;
|
2020-04-04 14:51:51 +00:00
|
|
|
import android.util.Pair;
|
2020-04-04 08:45:42 +00:00
|
|
|
|
2020-04-04 14:51:51 +00:00
|
|
|
import com.google.common.collect.ArrayListMultimap;
|
2020-04-04 08:45:42 +00:00
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
|
|
|
|
|
|
|
public class SessionDescription {
|
|
|
|
|
2020-04-04 13:30:13 +00:00
|
|
|
public final int version;
|
|
|
|
public final String name;
|
|
|
|
public final String connectionData;
|
2020-04-04 14:51:51 +00:00
|
|
|
public final ArrayListMultimap<String, String> attributes;
|
2020-04-04 13:30:13 +00:00
|
|
|
public final List<Media> media;
|
2020-04-04 08:45:42 +00:00
|
|
|
|
|
|
|
|
2020-04-04 14:51:51 +00:00
|
|
|
public SessionDescription(int version, String name, String connectionData, ArrayListMultimap<String, String> attributes, List<Media> media) {
|
2020-04-04 08:45:42 +00:00
|
|
|
this.version = version;
|
|
|
|
this.name = name;
|
|
|
|
this.connectionData = connectionData;
|
|
|
|
this.attributes = attributes;
|
|
|
|
this.media = media;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SessionDescription parse(final Map<String, JingleRtpConnection.DescriptionTransport> contents) {
|
|
|
|
final SessionDescriptionBuilder sessionDescriptionBuilder = new SessionDescriptionBuilder();
|
|
|
|
return sessionDescriptionBuilder.createSessionDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SessionDescription parse(final String input) {
|
|
|
|
final SessionDescriptionBuilder sessionDescriptionBuilder = new SessionDescriptionBuilder();
|
|
|
|
MediaBuilder currentMediaBuilder = null;
|
2020-04-04 14:51:51 +00:00
|
|
|
ArrayListMultimap<String, String> attributeMap = ArrayListMultimap.create();
|
2020-04-04 08:45:42 +00:00
|
|
|
ImmutableList.Builder<Media> mediaBuilder = new ImmutableList.Builder<>();
|
2020-04-04 13:30:13 +00:00
|
|
|
for (final String line : input.split("\n")) {
|
2020-04-04 14:51:51 +00:00
|
|
|
final String[] pair = line.trim().split("=", 2);
|
2020-04-04 08:45:42 +00:00
|
|
|
if (pair.length < 2 || pair[0].length() != 1) {
|
2020-04-04 13:30:13 +00:00
|
|
|
Log.d(Config.LOGTAG, "skipping sdp parsing on line " + line);
|
2020-04-04 08:45:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final char key = pair[0].charAt(0);
|
|
|
|
final String value = pair[1];
|
|
|
|
switch (key) {
|
|
|
|
case 'v':
|
|
|
|
sessionDescriptionBuilder.setVersion(ignorantIntParser(value));
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (currentMediaBuilder != null) {
|
|
|
|
currentMediaBuilder.setConnectionData(value);
|
|
|
|
} else {
|
|
|
|
sessionDescriptionBuilder.setConnectionData(value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sessionDescriptionBuilder.setName(value);
|
|
|
|
break;
|
|
|
|
case 'a':
|
2020-04-04 14:51:51 +00:00
|
|
|
final Pair<String, String> attribute = parseAttribute(value);
|
|
|
|
attributeMap.put(attribute.first, attribute.second);
|
2020-04-04 08:45:42 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
if (currentMediaBuilder == null) {
|
2020-04-04 14:51:51 +00:00
|
|
|
sessionDescriptionBuilder.setAttributes(attributeMap);
|
2020-04-04 13:30:13 +00:00
|
|
|
;
|
2020-04-04 08:45:42 +00:00
|
|
|
} else {
|
2020-04-04 14:51:51 +00:00
|
|
|
currentMediaBuilder.setAttributes(attributeMap);
|
2020-04-04 08:45:42 +00:00
|
|
|
mediaBuilder.add(currentMediaBuilder.createMedia());
|
|
|
|
}
|
2020-04-04 14:51:51 +00:00
|
|
|
attributeMap = ArrayListMultimap.create();
|
2020-04-04 08:45:42 +00:00
|
|
|
currentMediaBuilder = new MediaBuilder();
|
|
|
|
final String[] parts = value.split(" ");
|
|
|
|
if (parts.length >= 3) {
|
|
|
|
currentMediaBuilder.setMedia(parts[0]);
|
|
|
|
currentMediaBuilder.setPort(ignorantIntParser(parts[1]));
|
|
|
|
currentMediaBuilder.setProtocol(parts[2]);
|
|
|
|
ImmutableList.Builder<Integer> formats = new ImmutableList.Builder<>();
|
2020-04-04 13:30:13 +00:00
|
|
|
for (int i = 3; i < parts.length; ++i) {
|
2020-04-04 08:45:42 +00:00
|
|
|
formats.add(ignorantIntParser(parts[i]));
|
|
|
|
}
|
|
|
|
currentMediaBuilder.setFormats(formats.build());
|
|
|
|
} else {
|
2020-04-04 13:30:13 +00:00
|
|
|
Log.d(Config.LOGTAG, "skipping media line " + line);
|
2020-04-04 08:45:42 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (currentMediaBuilder != null) {
|
2020-04-04 14:51:51 +00:00
|
|
|
currentMediaBuilder.setAttributes(attributeMap);
|
2020-04-04 08:45:42 +00:00
|
|
|
mediaBuilder.add(currentMediaBuilder.createMedia());
|
|
|
|
}
|
|
|
|
sessionDescriptionBuilder.setMedia(mediaBuilder.build());
|
|
|
|
return sessionDescriptionBuilder.createSessionDescription();
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:51:51 +00:00
|
|
|
public static int ignorantIntParser(final String input) {
|
2020-04-04 08:45:42 +00:00
|
|
|
try {
|
|
|
|
return Integer.parseInt(input);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:51:51 +00:00
|
|
|
public static Pair<String, String> parseAttribute(final String input) {
|
|
|
|
final String[] pair = input.split(":", 2);
|
|
|
|
if (pair.length == 2) {
|
|
|
|
return new Pair<>(pair[0], pair[1]);
|
|
|
|
} else {
|
|
|
|
return new Pair<>(pair[0], "");
|
2020-04-04 08:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class Media {
|
2020-04-04 13:30:13 +00:00
|
|
|
public final String media;
|
|
|
|
public final int port;
|
|
|
|
public final String protocol;
|
|
|
|
public final List<Integer> formats;
|
|
|
|
public final String connectionData;
|
2020-04-04 14:51:51 +00:00
|
|
|
public final ArrayListMultimap<String, String> attributes;
|
2020-04-04 08:45:42 +00:00
|
|
|
|
2020-04-04 14:51:51 +00:00
|
|
|
public Media(String media, int port, String protocol, List<Integer> formats, String connectionData, ArrayListMultimap<String, String> attributes) {
|
2020-04-04 08:45:42 +00:00
|
|
|
this.media = media;
|
|
|
|
this.port = port;
|
|
|
|
this.protocol = protocol;
|
|
|
|
this.formats = formats;
|
|
|
|
this.connectionData = connectionData;
|
|
|
|
this.attributes = attributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|