forked from androidx/media
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionDescriptionParser.java
247 lines (216 loc) · 9.46 KB
/
SessionDescriptionParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.exoplayer.rtsp;
import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.exoplayer.rtsp.SessionDescription.SUPPORTED_SDP_VERSION;
import static com.google.common.base.Strings.nullToEmpty;
import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.media3.common.ParserException;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** Parses a String based SDP message into {@link SessionDescription}. */
@UnstableApi
/* package */ final class SessionDescriptionParser {
// SDP line always starts with an one letter tag, followed by an equal sign. The information
// under the given tag follows an optional space.
private static final Pattern SDP_LINE_PATTERN = Pattern.compile("([a-z])=\\s?(.+)");
// SDP line with a one letter tag, an equal sign, and an empty value.
private static final Pattern SDP_LINE_WITH_EMPTY_VALUE_PATTERN = Pattern.compile("^([a-z])=$");
// Matches an attribute line (with a= sdp tag removed. Example: range:npt=0-50.0).
// Attribute can also be a flag, i.e. without a value, like recvonly. Reference RFC4566 Section 9
// Page 43, under "token-char".
private static final Pattern ATTRIBUTE_PATTERN =
Pattern.compile(
"([\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x30-\\x39\\x41-\\x5a\\x5e-\\x7e]+)(?::(.*))?");
// SDP media description line: <mediaType> <port> <transmissionProtocol> <rtpPayloadType>
// For instance: audio 0 RTP/AVP 97
private static final Pattern MEDIA_DESCRIPTION_PATTERN =
Pattern.compile("(\\S+)\\s(\\S+)\\s(\\S+)\\s(\\S+)");
private static final String VERSION_TYPE = "v";
private static final String ORIGIN_TYPE = "o";
private static final String SESSION_TYPE = "s";
private static final String INFORMATION_TYPE = "i";
private static final String URI_TYPE = "u";
private static final String EMAIL_TYPE = "e";
private static final String PHONE_NUMBER_TYPE = "p";
private static final String CONNECTION_TYPE = "c";
private static final String BANDWIDTH_TYPE = "b";
private static final String TIMING_TYPE = "t";
private static final String KEY_TYPE = "k";
private static final String ATTRIBUTE_TYPE = "a";
private static final String MEDIA_TYPE = "m";
private static final String REPEAT_TYPE = "r";
private static final String ZONE_TYPE = "z";
/**
* Parses a String based SDP message into {@link SessionDescription}.
*
* @throws ParserException On SDP message line that cannot be parsed, or when one or more of the
* mandatory SDP fields {@link SessionDescription#timing}, {@link SessionDescription#origin}
* and {@link SessionDescription#sessionName} are not set.
*/
public static SessionDescription parse(String sdpString) throws ParserException {
SessionDescription.Builder sessionDescriptionBuilder = new SessionDescription.Builder();
@Nullable MediaDescription.Builder mediaDescriptionBuilder = null;
// Lines are separated by an CRLF.
for (String line : RtspMessageUtil.splitRtspMessageBody(sdpString)) {
if ("".equals(line)) {
continue;
}
Matcher matcher = SDP_LINE_PATTERN.matcher(line);
if (!matcher.matches()) {
Matcher sdpTagMatcher = SDP_LINE_WITH_EMPTY_VALUE_PATTERN.matcher(line);
if (sdpTagMatcher.matches() && Objects.equals(sdpTagMatcher.group(1), INFORMATION_TYPE)) {
// Allow and skip empty Session Information (tag 'i') attributes
continue;
}
throw ParserException.createForMalformedManifest(
"Malformed SDP line: " + line, /* cause= */ null);
}
String sdpType = checkNotNull(matcher.group(1));
String sdpValue = checkNotNull(matcher.group(2));
switch (sdpType) {
case VERSION_TYPE:
if (!SUPPORTED_SDP_VERSION.equals(sdpValue)) {
throw ParserException.createForMalformedManifest(
String.format("SDP version %s is not supported.", sdpValue), /* cause= */ null);
}
break;
case ORIGIN_TYPE:
sessionDescriptionBuilder.setOrigin(sdpValue);
break;
case SESSION_TYPE:
sessionDescriptionBuilder.setSessionName(sdpValue);
break;
case INFORMATION_TYPE:
if (mediaDescriptionBuilder == null) {
sessionDescriptionBuilder.setSessionInfo(sdpValue);
} else {
mediaDescriptionBuilder.setMediaTitle(sdpValue);
}
break;
case URI_TYPE:
sessionDescriptionBuilder.setUri(Uri.parse(sdpValue));
break;
case EMAIL_TYPE:
sessionDescriptionBuilder.setEmailAddress(sdpValue);
break;
case PHONE_NUMBER_TYPE:
sessionDescriptionBuilder.setPhoneNumber(sdpValue);
break;
case CONNECTION_TYPE:
if (mediaDescriptionBuilder == null) {
sessionDescriptionBuilder.setConnection(sdpValue);
} else {
mediaDescriptionBuilder.setConnection(sdpValue);
}
break;
case BANDWIDTH_TYPE:
String[] bandwidthComponents = Util.split(sdpValue, ":\\s?");
checkArgument(bandwidthComponents.length == 2);
int bitrateKbps = Integer.parseInt(bandwidthComponents[1]);
// Converting kilobits per second to bits per second.
if (mediaDescriptionBuilder == null) {
sessionDescriptionBuilder.setBitrate(bitrateKbps * 1000);
} else {
mediaDescriptionBuilder.setBitrate(bitrateKbps * 1000);
}
break;
case TIMING_TYPE:
sessionDescriptionBuilder.setTiming(sdpValue);
break;
case KEY_TYPE:
if (mediaDescriptionBuilder == null) {
sessionDescriptionBuilder.setKey(sdpValue);
} else {
mediaDescriptionBuilder.setKey(sdpValue);
}
break;
case ATTRIBUTE_TYPE:
matcher = ATTRIBUTE_PATTERN.matcher(sdpValue);
if (!matcher.matches()) {
throw ParserException.createForMalformedManifest(
"Malformed Attribute line: " + line, /* cause= */ null);
}
String attributeName = checkNotNull(matcher.group(1));
// The second catching group is optional and thus could be null.
String attributeValue = nullToEmpty(matcher.group(2));
if (mediaDescriptionBuilder == null) {
sessionDescriptionBuilder.addAttribute(attributeName, attributeValue);
} else {
mediaDescriptionBuilder.addAttribute(attributeName, attributeValue);
}
break;
case MEDIA_TYPE:
if (mediaDescriptionBuilder != null) {
addMediaDescriptionToSession(sessionDescriptionBuilder, mediaDescriptionBuilder);
}
mediaDescriptionBuilder = parseMediaDescriptionLine(sdpValue);
break;
case REPEAT_TYPE:
case ZONE_TYPE:
default:
// Not handled.
}
}
if (mediaDescriptionBuilder != null) {
addMediaDescriptionToSession(sessionDescriptionBuilder, mediaDescriptionBuilder);
}
try {
return sessionDescriptionBuilder.build();
} catch (IllegalArgumentException | IllegalStateException e) {
throw ParserException.createForMalformedManifest(/* message= */ null, e);
}
}
private static void addMediaDescriptionToSession(
SessionDescription.Builder sessionDescriptionBuilder,
MediaDescription.Builder mediaDescriptionBuilder)
throws ParserException {
try {
sessionDescriptionBuilder.addMediaDescription(mediaDescriptionBuilder.build());
} catch (IllegalArgumentException | IllegalStateException e) {
throw ParserException.createForMalformedManifest(/* message= */ null, e);
}
}
private static MediaDescription.Builder parseMediaDescriptionLine(String line)
throws ParserException {
Matcher matcher = MEDIA_DESCRIPTION_PATTERN.matcher(line);
if (!matcher.matches()) {
throw ParserException.createForMalformedManifest(
"Malformed SDP media description line: " + line, /* cause= */ null);
}
String mediaType = checkNotNull(matcher.group(1));
String portString = checkNotNull(matcher.group(2));
String transportProtocol = checkNotNull(matcher.group(3));
String payloadTypeString = checkNotNull(matcher.group(4));
try {
return new MediaDescription.Builder(
mediaType,
Integer.parseInt(portString),
transportProtocol,
Integer.parseInt(payloadTypeString));
} catch (NumberFormatException e) {
throw ParserException.createForMalformedManifest(
"Malformed SDP media description line: " + line, e);
}
}
/** Prevents initialization. */
private SessionDescriptionParser() {}
}