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