blob: cdaddd7199054465babcb8a9a5e7cb86eac569fc [file] [log] [blame]
Amit Hilbucha2012042018-12-03 11:35:05 -08001/*
2 * Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/sdp_serializer.h"
Amit Hilbucha2012042018-12-03 11:35:05 -080012
Amit Hilbuchc57d5732018-12-11 15:30:11 -080013#include <algorithm>
Amit Hilbucha2012042018-12-03 11:35:05 -080014#include <string>
15#include <utility>
16#include <vector>
17
18#include "api/jsep.h"
Amit Hilbuchc57d5732018-12-11 15:30:11 -080019#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/string_encode.h"
Amit Hilbuchc57d5732018-12-11 15:30:11 -080021#include "rtc_base/string_to_number.h"
Amit Hilbucha2012042018-12-03 11:35:05 -080022#include "rtc_base/strings/string_builder.h"
23
Amit Hilbuchc57d5732018-12-11 15:30:11 -080024using cricket::RidDescription;
25using cricket::RidDirection;
Amit Hilbucha2012042018-12-03 11:35:05 -080026using cricket::SimulcastDescription;
27using cricket::SimulcastLayer;
28using cricket::SimulcastLayerList;
29
30namespace webrtc {
31
32namespace {
33
34// delimiters
35const char kDelimiterComma[] = ",";
36const char kDelimiterCommaChar = ',';
Amit Hilbuchc57d5732018-12-11 15:30:11 -080037const char kDelimiterEqual[] = "=";
38const char kDelimiterEqualChar = '=';
Amit Hilbucha2012042018-12-03 11:35:05 -080039const char kDelimiterSemicolon[] = ";";
40const char kDelimiterSemicolonChar = ';';
41const char kDelimiterSpace[] = " ";
42const char kDelimiterSpaceChar = ' ';
43
44// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
Amit Hilbuchc57d5732018-12-11 15:30:11 -080045// https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
Amit Hilbucha2012042018-12-03 11:35:05 -080046const char kSimulcastPausedStream[] = "~";
47const char kSimulcastPausedStreamChar = '~';
Amit Hilbuchc57d5732018-12-11 15:30:11 -080048const char kSendDirection[] = "send";
49const char kReceiveDirection[] = "recv";
50const char kPayloadType[] = "pt";
Amit Hilbucha2012042018-12-03 11:35:05 -080051
52RTCError ParseError(const std::string& message) {
53 return RTCError(RTCErrorType::SYNTAX_ERROR, message);
54}
55
56// These methods serialize simulcast according to the specification:
57// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
58rtc::StringBuilder& operator<<(rtc::StringBuilder& builder,
59 const SimulcastLayer& simulcast_layer) {
60 if (simulcast_layer.is_paused) {
61 builder << kSimulcastPausedStream;
62 }
63 builder << simulcast_layer.rid;
64 return builder;
65}
66
67rtc::StringBuilder& operator<<(
68 rtc::StringBuilder& builder,
69 const std::vector<SimulcastLayer>& layer_alternatives) {
70 bool first = true;
71 for (const SimulcastLayer& rid : layer_alternatives) {
72 if (!first) {
73 builder << kDelimiterComma;
74 }
75 builder << rid;
76 first = false;
77 }
78 return builder;
79}
80
81rtc::StringBuilder& operator<<(rtc::StringBuilder& builder,
82 const SimulcastLayerList& simulcast_layers) {
83 bool first = true;
84 for (auto alternatives : simulcast_layers) {
85 if (!first) {
86 builder << kDelimiterSemicolon;
87 }
88 builder << alternatives;
89 first = false;
90 }
91 return builder;
92}
Amit Hilbuchc57d5732018-12-11 15:30:11 -080093// This method deserializes simulcast according to the specification:
Amit Hilbucha2012042018-12-03 11:35:05 -080094// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
95// sc-str-list = sc-alt-list *( ";" sc-alt-list )
96// sc-alt-list = sc-id *( "," sc-id )
97// sc-id-paused = "~"
98// sc-id = [sc-id-paused] rid-id
99// rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
100RTCErrorOr<SimulcastLayerList> ParseSimulcastLayerList(const std::string& str) {
101 std::vector<std::string> tokens;
102 rtc::tokenize_with_empty_tokens(str, kDelimiterSemicolonChar, &tokens);
103 if (tokens.empty()) {
104 return ParseError("Layer list cannot be empty.");
105 }
106
107 SimulcastLayerList result;
108 for (const std::string& token : tokens) {
109 if (token.empty()) {
110 return ParseError("Simulcast alternative layer list is empty.");
111 }
112
113 std::vector<std::string> rid_tokens;
114 rtc::tokenize_with_empty_tokens(token, kDelimiterCommaChar, &rid_tokens);
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800115
Amit Hilbucha2012042018-12-03 11:35:05 -0800116 if (rid_tokens.empty()) {
117 return ParseError("Simulcast alternative layer list is malformed.");
118 }
119
120 std::vector<SimulcastLayer> layers;
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800121 for (const std::string& rid_token : rid_tokens) {
Amit Hilbucha2012042018-12-03 11:35:05 -0800122 if (rid_token.empty() || rid_token == kSimulcastPausedStream) {
123 return ParseError("Rid must not be empty.");
124 }
125
126 bool paused = rid_token[0] == kSimulcastPausedStreamChar;
127 std::string rid = paused ? rid_token.substr(1) : rid_token;
Amit Hilbucha2012042018-12-03 11:35:05 -0800128 layers.push_back(SimulcastLayer(rid, paused));
129 }
130
131 result.AddLayerWithAlternatives(layers);
132 }
133
134 return std::move(result);
135}
136
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800137webrtc::RTCError ParseRidPayloadList(const std::string& payload_list,
138 RidDescription* rid_description) {
139 RTC_DCHECK(rid_description);
140 std::vector<int>& payload_types = rid_description->payload_types;
141 // Check that the description doesn't have any payload types or restrictions.
142 // If the pt= field is specified, it must be first and must not repeat.
143 if (!payload_types.empty()) {
144 return ParseError("Multiple pt= found in RID Description.");
145 }
146 if (!rid_description->restrictions.empty()) {
147 return ParseError("Payload list must appear first in the restrictions.");
148 }
149
150 // If the pt= field is specified, it must have a value.
151 if (payload_list.empty()) {
152 return ParseError("Payload list must have at least one value.");
153 }
154
155 // Tokenize the ',' delimited list
156 std::vector<std::string> string_payloads;
157 rtc::tokenize(payload_list, kDelimiterCommaChar, &string_payloads);
158 if (string_payloads.empty()) {
159 return ParseError("Payload list must have at least one value.");
160 }
161
162 for (const std::string& payload_type : string_payloads) {
163 absl::optional<int> value = rtc::StringToNumber<int>(payload_type);
164 if (!value.has_value()) {
165 return ParseError("Invalid payload type: " + payload_type);
166 }
167
168 // Check if the value already appears in the payload list.
169 if (std::find(payload_types.begin(), payload_types.end(), value.value()) !=
170 payload_types.end()) {
171 return ParseError("Duplicate payload type in list: " + payload_type);
172 }
173 payload_types.push_back(value.value());
174 }
175
176 return RTCError::OK();
177}
178
Amit Hilbucha2012042018-12-03 11:35:05 -0800179} // namespace
180
181std::string SdpSerializer::SerializeSimulcastDescription(
182 const cricket::SimulcastDescription& simulcast) const {
183 rtc::StringBuilder sb;
184 std::string delimiter;
185
186 if (!simulcast.send_layers().empty()) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800187 sb << kSendDirection << kDelimiterSpace << simulcast.send_layers();
Amit Hilbucha2012042018-12-03 11:35:05 -0800188 delimiter = kDelimiterSpace;
189 }
190
191 if (!simulcast.receive_layers().empty()) {
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800192 sb << delimiter << kReceiveDirection << kDelimiterSpace
Amit Hilbucha2012042018-12-03 11:35:05 -0800193 << simulcast.receive_layers();
194 }
195
196 return sb.str();
197}
198
199// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
200// a:simulcast:<send> <streams> <recv> <streams>
201// Formal Grammar
202// sc-value = ( sc-send [SP sc-recv] ) / ( sc-recv [SP sc-send] )
203// sc-send = %s"send" SP sc-str-list
204// sc-recv = %s"recv" SP sc-str-list
205// sc-str-list = sc-alt-list *( ";" sc-alt-list )
206// sc-alt-list = sc-id *( "," sc-id )
207// sc-id-paused = "~"
208// sc-id = [sc-id-paused] rid-id
209// rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
210RTCErrorOr<SimulcastDescription> SdpSerializer::DeserializeSimulcastDescription(
211 absl::string_view string) const {
212 std::vector<std::string> tokens;
213 rtc::tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
214
215 if (tokens.size() != 2 && tokens.size() != 4) {
216 return ParseError("Must have one or two <direction, streams> pairs.");
217 }
218
219 bool bidirectional = tokens.size() == 4; // indicates both send and recv
220
221 // Tokens 0, 2 (if exists) should be send / recv
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800222 if ((tokens[0] != kSendDirection && tokens[0] != kReceiveDirection) ||
223 (bidirectional && tokens[2] != kSendDirection &&
224 tokens[2] != kReceiveDirection) ||
Amit Hilbucha2012042018-12-03 11:35:05 -0800225 (bidirectional && tokens[0] == tokens[2])) {
226 return ParseError("Valid values: send / recv.");
227 }
228
229 // Tokens 1, 3 (if exists) should be alternative layer lists
230 RTCErrorOr<SimulcastLayerList> list1, list2;
231 list1 = ParseSimulcastLayerList(tokens[1]);
232 if (!list1.ok()) {
233 return list1.MoveError();
234 }
235
236 if (bidirectional) {
237 list2 = ParseSimulcastLayerList(tokens[3]);
238 if (!list2.ok()) {
239 return list2.MoveError();
240 }
241 }
242
243 // Set the layers so that list1 is for send and list2 is for recv
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800244 if (tokens[0] != kSendDirection) {
Amit Hilbucha2012042018-12-03 11:35:05 -0800245 std::swap(list1, list2);
246 }
247
248 // Set the layers according to which pair is send and which is recv
249 // At this point if the simulcast is unidirectional then
250 // either |list1| or |list2| will be in 'error' state indicating that
251 // the value should not be used.
252 SimulcastDescription simulcast;
253 if (list1.ok()) {
254 simulcast.send_layers() = list1.MoveValue();
255 }
256
257 if (list2.ok()) {
258 simulcast.receive_layers() = list2.MoveValue();
259 }
260
261 return std::move(simulcast);
262}
263
Amit Hilbuchc57d5732018-12-11 15:30:11 -0800264std::string SdpSerializer::SerializeRidDescription(
265 const RidDescription& rid_description) const {
266 RTC_DCHECK(!rid_description.rid.empty());
267 RTC_DCHECK(rid_description.direction == RidDirection::kSend ||
268 rid_description.direction == RidDirection::kReceive);
269
270 rtc::StringBuilder builder;
271 builder << rid_description.rid << kDelimiterSpace
272 << (rid_description.direction == RidDirection::kSend
273 ? kSendDirection
274 : kReceiveDirection);
275
276 const auto& payload_types = rid_description.payload_types;
277 const auto& restrictions = rid_description.restrictions;
278
279 // First property is separated by ' ', the next ones by ';'.
280 const char* propertyDelimiter = kDelimiterSpace;
281
282 // Serialize any codecs in the description.
283 if (!payload_types.empty()) {
284 builder << propertyDelimiter << kPayloadType << kDelimiterEqual;
285 propertyDelimiter = kDelimiterSemicolon;
286 const char* formatDelimiter = "";
287 for (int payload_type : payload_types) {
288 builder << formatDelimiter << payload_type;
289 formatDelimiter = kDelimiterComma;
290 }
291 }
292
293 // Serialize any restrictions in the description.
294 for (const auto& pair : restrictions) {
295 // Serialize key=val pairs. =val part is ommitted if val is empty.
296 builder << propertyDelimiter << pair.first;
297 if (!pair.second.empty()) {
298 builder << kDelimiterEqual << pair.second;
299 }
300
301 propertyDelimiter = kDelimiterSemicolon;
302 }
303
304 return builder.str();
305}
306
307// https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
308// Formal Grammar
309// rid-syntax = %s"a=rid:" rid-id SP rid-dir
310// [ rid-pt-param-list / rid-param-list ]
311// rid-id = 1*(alpha-numeric / "-" / "_")
312// rid-dir = %s"send" / %s"recv"
313// rid-pt-param-list = SP rid-fmt-list *( ";" rid-param )
314// rid-param-list = SP rid-param *( ";" rid-param )
315// rid-fmt-list = %s"pt=" fmt *( "," fmt )
316// rid-param = 1*(alpha-numeric / "-") [ "=" param-val ]
317// param-val = *( %x20-58 / %x60-7E )
318// ; Any printable character except semicolon
319RTCErrorOr<RidDescription> SdpSerializer::DeserializeRidDescription(
320 absl::string_view string) const {
321 std::vector<std::string> tokens;
322 rtc::tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
323
324 if (tokens.size() < 2) {
325 return ParseError("RID Description must contain <RID> <direction>.");
326 }
327
328 if (tokens.size() > 3) {
329 return ParseError("Invalid RID Description format. Too many arguments.");
330 }
331
332 if (tokens[1] != kSendDirection && tokens[1] != kReceiveDirection) {
333 return ParseError("Invalid RID direction. Supported values: send / recv.");
334 }
335
336 RidDirection direction = tokens[1] == kSendDirection ? RidDirection::kSend
337 : RidDirection::kReceive;
338
339 RidDescription rid_description(tokens[0], direction);
340
341 // If there is a third argument it is a payload list and/or restriction list.
342 if (tokens.size() == 3) {
343 std::vector<std::string> restrictions;
344 rtc::tokenize(tokens[2], kDelimiterSemicolonChar, &restrictions);
345
346 // Check for malformed restriction list, such as ';' or ';;;' etc.
347 if (restrictions.empty()) {
348 return ParseError("Invalid RID restriction list: " + tokens[2]);
349 }
350
351 // Parse the restrictions. The payload indicator (pt) can only appear first.
352 for (const std::string& restriction : restrictions) {
353 std::vector<std::string> parts;
354 rtc::tokenize(restriction, kDelimiterEqualChar, &parts);
355 if (parts.empty() || parts.size() > 2) {
356 return ParseError("Invalid format for restriction: " + restriction);
357 }
358
359 // |parts| contains at least one value and it does not contain a space.
360 // Note: |parts| and other values might still contain tab, newline,
361 // unprintable characters, etc. which will not generate errors here but
362 // will (most-likely) be ignored by components down stream.
363 if (parts[0] == kPayloadType) {
364 RTCError error = ParseRidPayloadList(
365 parts.size() > 1 ? parts[1] : std::string(), &rid_description);
366 if (!error.ok()) {
367 return std::move(error);
368 }
369
370 continue;
371 }
372
373 // Parse |parts| as a key=value pair which allows unspecified values.
374 if (rid_description.restrictions.find(parts[0]) !=
375 rid_description.restrictions.end()) {
376 return ParseError("Duplicate restriction specified: " + parts[0]);
377 }
378
379 rid_description.restrictions[parts[0]] =
380 parts.size() > 1 ? parts[1] : std::string();
381 }
382 }
383
384 return std::move(rid_description);
385}
386
Amit Hilbucha2012042018-12-03 11:35:05 -0800387} // namespace webrtc