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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/*
* Copyright 2018 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "pc/simulcast_sdp_serializer.h"
#include <algorithm>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "absl/algorithm/container.h"
#include "absl/strings/string_view.h"
#include "api/rtc_error.h"
#include "api/rtp_parameters.h"
#include "media/base/codec.h"
#include "media/base/codec_comparators.h"
#include "media/base/rid_description.h"
#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
#include "pc/session_description.h"
#include "pc/simulcast_description.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/string_encode.h"
#include "rtc_base/string_to_number.h"
#include "rtc_base/strings/string_builder.h"
using ::webrtc::RidDescription;
using ::webrtc::RidDirection;
using ::webrtc::SimulcastDescription;
using ::webrtc::SimulcastLayer;
using ::webrtc::SimulcastLayerList;
namespace webrtc {
namespace {
// delimiters
const char kDelimiterComma[] = ",";
const char kDelimiterCommaChar = ',';
const char kDelimiterEqual[] = "=";
const char kDelimiterEqualChar = '=';
const char kDelimiterSemicolon[] = ";";
const char kDelimiterSemicolonChar = ';';
const char kDelimiterSpace[] = " ";
const char kDelimiterSpaceChar = ' ';
// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
// https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
const char kSimulcastPausedStream[] = "~";
const char kSimulcastPausedStreamChar = '~';
const char kSendDirection[] = "send";
const char kReceiveDirection[] = "recv";
const char kPayloadType[] = "pt";
RTCError ParseError(absl::string_view message) {
return RTCError(RTCErrorType::SYNTAX_ERROR, message);
}
// These methods serialize simulcast according to the specification:
// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
StringBuilder& operator<<(StringBuilder& builder,
const SimulcastLayer& simulcast_layer) {
if (simulcast_layer.is_paused) {
builder << kSimulcastPausedStream;
}
builder << simulcast_layer.rid;
return builder;
}
StringBuilder& operator<<(
StringBuilder& builder,
const std::vector<SimulcastLayer>& layer_alternatives) {
bool first = true;
for (const SimulcastLayer& rid : layer_alternatives) {
if (!first) {
builder << kDelimiterComma;
}
builder << rid;
first = false;
}
return builder;
}
StringBuilder& operator<<(StringBuilder& builder,
const SimulcastLayerList& simulcast_layers) {
bool first = true;
for (const auto& alternatives : simulcast_layers) {
if (!first) {
builder << kDelimiterSemicolon;
}
builder << alternatives;
first = false;
}
return builder;
}
// This method deserializes simulcast according to the specification:
// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
// sc-str-list = sc-alt-list *( ";" sc-alt-list )
// sc-alt-list = sc-id *( "," sc-id )
// sc-id-paused = "~"
// sc-id = [sc-id-paused] rid-id
// rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
RTCErrorOr<SimulcastLayerList> ParseSimulcastLayerList(const std::string& str) {
std::vector<absl::string_view> tokens = split(str, kDelimiterSemicolonChar);
if (tokens.empty()) {
return ParseError("Layer list cannot be empty.");
}
SimulcastLayerList result;
for (const absl::string_view& token : tokens) {
if (token.empty()) {
return ParseError("Simulcast alternative layer list is empty.");
}
std::vector<absl::string_view> rid_tokens =
split(token, kDelimiterCommaChar);
if (rid_tokens.empty()) {
return ParseError("Simulcast alternative layer list is malformed.");
}
std::vector<SimulcastLayer> layers;
for (const absl::string_view& rid_token : rid_tokens) {
if (rid_token.empty() || rid_token == kSimulcastPausedStream) {
return ParseError("Rid must not be empty.");
}
bool paused = rid_token[0] == kSimulcastPausedStreamChar;
absl::string_view rid = paused ? rid_token.substr(1) : rid_token;
layers.push_back(SimulcastLayer(rid, paused));
}
result.AddLayerWithAlternatives(layers);
}
return std::move(result);
}
RTCError ParseRidPayloadList(const std::string& payload_list,
RidDescription* rid_description,
std::vector<int>* rid_payload_types) {
RTC_DCHECK(rid_description);
RTC_DCHECK(rid_payload_types);
// Check that the description doesn't have any payload types or restrictions.
// If the pt= field is specified, it must be first and must not repeat.
if (!rid_payload_types->empty()) {
return ParseError("Multiple pt= found in RID Description.");
}
if (!rid_description->restrictions.empty()) {
return ParseError("Payload list must appear first in the restrictions.");
}
// If the pt= field is specified, it must have a value.
if (payload_list.empty()) {
return ParseError("Payload list must have at least one value.");
}
// Tokenize the ',' delimited list
std::vector<std::string> string_payloads;
tokenize(payload_list, kDelimiterCommaChar, &string_payloads);
if (string_payloads.empty()) {
return ParseError("Payload list must have at least one value.");
}
for (const std::string& payload_type : string_payloads) {
std::optional<int> value = StringToNumber<int>(payload_type);
if (!value.has_value()) {
return ParseError("Invalid payload type: " + payload_type);
}
// Check if the value already appears in the payload list.
if (absl::c_linear_search(*rid_payload_types, value.value())) {
return ParseError("Duplicate payload type in list: " + payload_type);
}
rid_payload_types->push_back(value.value());
}
return RTCError::OK();
}
} // namespace
std::string SimulcastSdpSerializer::SerializeSimulcastDescription(
const SimulcastDescription& simulcast) const {
StringBuilder sb;
std::string delimiter;
if (!simulcast.send_layers().empty()) {
sb << kSendDirection << kDelimiterSpace << simulcast.send_layers();
delimiter = kDelimiterSpace;
}
if (!simulcast.receive_layers().empty()) {
sb << delimiter << kReceiveDirection << kDelimiterSpace
<< simulcast.receive_layers();
}
return sb.Release();
}
// https://tools.ietf.org/html/draft-ietf-mmusic-sdp-simulcast-13#section-5.1
// a:simulcast:<send> <streams> <recv> <streams>
// Formal Grammar
// sc-value = ( sc-send [SP sc-recv] ) / ( sc-recv [SP sc-send] )
// sc-send = %s"send" SP sc-str-list
// sc-recv = %s"recv" SP sc-str-list
// sc-str-list = sc-alt-list *( ";" sc-alt-list )
// sc-alt-list = sc-id *( "," sc-id )
// sc-id-paused = "~"
// sc-id = [sc-id-paused] rid-id
// rid-id = 1*(alpha-numeric / "-" / "_") ; see: I-D.ietf-mmusic-rid
RTCErrorOr<SimulcastDescription>
SimulcastSdpSerializer::DeserializeSimulcastDescription(
absl::string_view string) const {
std::vector<std::string> tokens;
tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
if (tokens.size() != 2 && tokens.size() != 4) {
return ParseError("Must have one or two <direction, streams> pairs.");
}
bool bidirectional = tokens.size() == 4; // indicates both send and recv
// Tokens 0, 2 (if exists) should be send / recv
if ((tokens[0] != kSendDirection && tokens[0] != kReceiveDirection) ||
(bidirectional && tokens[2] != kSendDirection &&
tokens[2] != kReceiveDirection) ||
(bidirectional && tokens[0] == tokens[2])) {
return ParseError("Valid values: send / recv.");
}
// Tokens 1, 3 (if exists) should be alternative layer lists
RTCErrorOr<SimulcastLayerList> list1, list2;
list1 = ParseSimulcastLayerList(tokens[1]);
if (!list1.ok()) {
return list1.MoveError();
}
if (bidirectional) {
list2 = ParseSimulcastLayerList(tokens[3]);
if (!list2.ok()) {
return list2.MoveError();
}
}
// Set the layers so that list1 is for send and list2 is for recv
if (tokens[0] != kSendDirection) {
std::swap(list1, list2);
}
// Set the layers according to which pair is send and which is recv
// At this point if the simulcast is unidirectional then
// either `list1` or `list2` will be in 'error' state indicating that
// the value should not be used.
SimulcastDescription simulcast;
if (list1.ok()) {
simulcast.send_layers() = list1.MoveValue();
}
if (list2.ok()) {
simulcast.receive_layers() = list2.MoveValue();
}
return std::move(simulcast);
}
std::string SimulcastSdpSerializer::SerializeRidDescription(
const MediaContentDescription& media_desc,
const RidDescription& rid_description) const {
RTC_DCHECK(!rid_description.rid.empty());
RTC_DCHECK(rid_description.direction == RidDirection::kSend ||
rid_description.direction == RidDirection::kReceive);
StringBuilder builder;
builder << rid_description.rid << kDelimiterSpace
<< (rid_description.direction == RidDirection::kSend
? kSendDirection
: kReceiveDirection);
// Convert `rid_descriptions.codecs` into a list of payload types based on
// looking up codecs from the media description, as opposed to trusting the
// `rid_descriptions.codecs[i].id` directly as these are typically wrong.
std::vector<int> payload_types;
for (const Codec& codec : rid_description.codecs) {
RtpCodec rtp_codec = codec.ToCodecParameters();
const auto it = std::find_if(
media_desc.codecs().begin(), media_desc.codecs().end(),
[&rtp_codec](const Codec& m_section_codec) {
return IsSameRtpCodecIgnoringLevel(m_section_codec, rtp_codec);
});
// The desired codec from setParameters() may not have been negotiated, e.g.
// if excluded with setCodecPreferences().
if (it == media_desc.codecs().end()) {
break;
}
if (it->id == Codec::kIdNotSet) {
RTC_DCHECK_NOTREACHED();
break;
}
payload_types.push_back(it->id);
}
const auto& restrictions = rid_description.restrictions;
// First property is separated by ' ', the next ones by ';'.
const char* propertyDelimiter = kDelimiterSpace;
// Serialize any codecs in the description.
if (!payload_types.empty()) {
builder << propertyDelimiter << kPayloadType << kDelimiterEqual;
propertyDelimiter = kDelimiterSemicolon;
const char* formatDelimiter = "";
for (int payload_type : payload_types) {
builder << formatDelimiter << payload_type;
formatDelimiter = kDelimiterComma;
}
}
// Serialize any restrictions in the description.
for (const auto& pair : restrictions) {
// Serialize key=val pairs. =val part is ommitted if val is empty.
builder << propertyDelimiter << pair.first;
if (!pair.second.empty()) {
builder << kDelimiterEqual << pair.second;
}
propertyDelimiter = kDelimiterSemicolon;
}
return builder.Release();
}
// https://tools.ietf.org/html/draft-ietf-mmusic-rid-15#section-10
// Formal Grammar
// rid-syntax = %s"a=rid:" rid-id SP rid-dir
// [ rid-pt-param-list / rid-param-list ]
// rid-id = 1*(alpha-numeric / "-" / "_")
// rid-dir = %s"send" / %s"recv"
// rid-pt-param-list = SP rid-fmt-list *( ";" rid-param )
// rid-param-list = SP rid-param *( ";" rid-param )
// rid-fmt-list = %s"pt=" fmt *( "," fmt )
// rid-param = 1*(alpha-numeric / "-") [ "=" param-val ]
// param-val = *( %x20-58 / %x60-7E )
// ; Any printable character except semicolon
RTCErrorOr<RidDescription> SimulcastSdpSerializer::DeserializeRidDescription(
const MediaContentDescription& media_desc,
absl::string_view string) const {
std::vector<std::string> tokens;
tokenize(std::string(string), kDelimiterSpaceChar, &tokens);
if (tokens.size() < 2) {
return ParseError("RID Description must contain <RID> <direction>.");
}
if (tokens.size() > 3) {
return ParseError("Invalid RID Description format. Too many arguments.");
}
if (!IsLegalRsidName(tokens[0])) {
return ParseError("Invalid RID value: " + tokens[0] + ".");
}
if (tokens[1] != kSendDirection && tokens[1] != kReceiveDirection) {
return ParseError("Invalid RID direction. Supported values: send / recv.");
}
RidDirection direction = tokens[1] == kSendDirection ? RidDirection::kSend
: RidDirection::kReceive;
RidDescription rid_description(tokens[0], direction);
std::vector<int> rid_payload_types;
// If there is a third argument it is a payload list and/or restriction list.
if (tokens.size() == 3) {
std::vector<std::string> restrictions;
tokenize(tokens[2], kDelimiterSemicolonChar, &restrictions);
// Check for malformed restriction list, such as ';' or ';;;' etc.
if (restrictions.empty()) {
return ParseError("Invalid RID restriction list: " + tokens[2]);
}
// Parse the restrictions. The payload indicator (pt) can only appear first.
for (const std::string& restriction : restrictions) {
std::vector<std::string> parts;
tokenize(restriction, kDelimiterEqualChar, &parts);
if (parts.empty() || parts.size() > 2) {
return ParseError("Invalid format for restriction: " + restriction);
}
// `parts` contains at least one value and it does not contain a space.
// Note: `parts` and other values might still contain tab, newline,
// unprintable characters, etc. which will not generate errors here but
// will (most-likely) be ignored by components down stream.
if (parts[0] == kPayloadType) {
RTCError error =
ParseRidPayloadList(parts.size() > 1 ? parts[1] : std::string(),
&rid_description, &rid_payload_types);
if (!error.ok()) {
return std::move(error);
}
continue;
}
// Parse `parts` as a key=value pair which allows unspecified values.
if (rid_description.restrictions.find(parts[0]) !=
rid_description.restrictions.end()) {
return ParseError("Duplicate restriction specified: " + parts[0]);
}
rid_description.restrictions[parts[0]] =
parts.size() > 1 ? parts[1] : std::string();
}
}
// Look up any referenced codecs from the media section and add them to
// `rid_description.codecs`.
for (const int& payload_type : rid_payload_types) {
const auto it =
std::find_if(media_desc.codecs().begin(), media_desc.codecs().end(),
[&payload_type](const Codec& m_section_codec) {
return m_section_codec.id == payload_type;
});
if (it == media_desc.codecs().end()) {
// This RID has a payload type that doesn't map to any known codec. While
// this is an error on the part of the entity that generated the SDP, this
// information falls into the "FYI" category and does not really change
// anything, so it's safe to ignore it.
RTC_LOG(LS_WARNING) << "A RID contains an unknown payload type.";
continue;
}
rid_description.codecs.push_back(*it);
}
return std::move(rid_description);
}
} // namespace webrtc
|