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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
|
/*
* Copyright 2025 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/sdp_munging_detector.h"
#include <algorithm>
#include <cstddef>
#include <string>
#include "absl/algorithm/container.h"
#include "api/jsep.h"
#include "api/media_types.h"
#include "api/uma_metrics.h"
#include "media/base/codec.h"
#include "media/base/media_constants.h"
#include "media/base/stream_params.h"
#include "p2p/base/transport_description.h"
#include "p2p/base/transport_info.h"
#include "pc/session_description.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
namespace webrtc {
namespace {
SdpMungingType DetermineTransportModification(
const TransportInfos& last_created_transport_infos,
const TransportInfos& transport_infos_to_set) {
if (last_created_transport_infos.size() != transport_infos_to_set.size()) {
RTC_LOG(LS_WARNING) << "SDP munging: Number of transport-infos does not "
"match last created description.";
// Number of transports should always match number of contents so this
// should never happen.
return SdpMungingType::kNumberOfContents;
}
for (size_t i = 0; i < last_created_transport_infos.size(); i++) {
if (last_created_transport_infos[i].description.ice_ufrag !=
transport_infos_to_set[i].description.ice_ufrag) {
RTC_LOG(LS_WARNING)
<< "SDP munging: ice-ufrag does not match last created description.";
return SdpMungingType::kIceUfrag;
}
if (last_created_transport_infos[i].description.ice_pwd !=
transport_infos_to_set[i].description.ice_pwd) {
RTC_LOG(LS_WARNING)
<< "SDP munging: ice-pwd does not match last created description.";
return SdpMungingType::kIcePwd;
}
if (last_created_transport_infos[i].description.ice_mode !=
transport_infos_to_set[i].description.ice_mode) {
RTC_LOG(LS_WARNING)
<< "SDP munging: ice mode does not match last created description.";
return SdpMungingType::kIceMode;
}
if (last_created_transport_infos[i].description.connection_role !=
transport_infos_to_set[i].description.connection_role) {
RTC_LOG(LS_WARNING)
<< "SDP munging: DTLS role does not match last created description.";
return SdpMungingType::kDtlsSetup;
}
if (last_created_transport_infos[i].description.transport_options !=
transport_infos_to_set[i].description.transport_options) {
RTC_LOG(LS_WARNING) << "SDP munging: ice_options does not match last "
"created description.";
bool created_renomination =
absl::c_find(
last_created_transport_infos[i].description.transport_options,
ICE_OPTION_RENOMINATION) !=
last_created_transport_infos[i].description.transport_options.end();
bool set_renomination =
absl::c_find(transport_infos_to_set[i].description.transport_options,
ICE_OPTION_RENOMINATION) !=
transport_infos_to_set[i].description.transport_options.end();
if (!created_renomination && set_renomination) {
return SdpMungingType::kIceOptionsRenomination;
}
return SdpMungingType::kIceOptions;
}
}
return SdpMungingType::kNoModification;
}
SdpMungingType DetermineAudioSdpMungingType(
const MediaContentDescription* last_created_media_description,
const MediaContentDescription* media_description_to_set) {
RTC_DCHECK(last_created_media_description);
RTC_DCHECK(media_description_to_set);
// Removing codecs should be done via setCodecPreferences or negotiation, not
// munging.
if (last_created_media_description->codecs().size() >
media_description_to_set->codecs().size()) {
RTC_LOG(LS_WARNING) << "SDP munging: audio codecs removed.";
return SdpMungingType::kAudioCodecsRemoved;
}
// Adding audio codecs is measured after the more specific multiopus and L16
// checks.
// Opus stereo modification required to enabled stereo playout for opus.
bool created_opus_stereo =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamStereo, &value) &&
value == kParamValueTrue;
}) != last_created_media_description->codecs().end();
bool set_opus_stereo =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamStereo, &value) &&
value == kParamValueTrue;
}) != media_description_to_set->codecs().end();
if (!created_opus_stereo && set_opus_stereo) {
RTC_LOG(LS_WARNING) << "SDP munging: Opus stereo enabled.";
return SdpMungingType::kAudioCodecsFmtpOpusStereo;
}
// Nonstandard 5.1/7.1 opus variant.
bool created_multiopus =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
return codec.name == "multiopus";
}) != last_created_media_description->codecs().end();
bool set_multiopus =
absl::c_find_if(media_description_to_set->codecs(),
[](const Codec codec) {
return codec.name == "multiopus";
}) != media_description_to_set->codecs().end();
if (!created_multiopus && set_multiopus) {
RTC_LOG(LS_WARNING) << "SDP munging: multiopus enabled.";
return SdpMungingType::kAudioCodecsAddedMultiOpus;
}
// L16.
bool created_l16 =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
return codec.name == kL16CodecName;
}) != last_created_media_description->codecs().end();
bool set_l16 = absl::c_find_if(media_description_to_set->codecs(),
[](const Codec codec) {
return codec.name == kL16CodecName;
}) != media_description_to_set->codecs().end();
if (!created_l16 && set_l16) {
RTC_LOG(LS_WARNING) << "SDP munging: L16 enabled.";
return SdpMungingType::kAudioCodecsAddedL16;
}
if (last_created_media_description->codecs().size() <
media_description_to_set->codecs().size()) {
RTC_LOG(LS_WARNING) << "SDP munging: audio codecs added.";
return SdpMungingType::kAudioCodecsAdded;
}
// Audio NACK is not offered by default.
bool created_nack =
absl::c_find_if(
last_created_media_description->codecs(), [](const Codec codec) {
return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack));
}) != last_created_media_description->codecs().end();
bool set_nack =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack));
}) != media_description_to_set->codecs().end();
if (!created_nack && set_nack) {
RTC_LOG(LS_WARNING) << "SDP munging: audio nack enabled.";
return SdpMungingType::kAudioCodecsRtcpFbAudioNack;
}
// RRTR is not offered by default.
bool created_rrtr =
absl::c_find_if(
last_created_media_description->codecs(), [](const Codec codec) {
return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRrtr));
}) != last_created_media_description->codecs().end();
bool set_rrtr =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamRrtr));
}) != media_description_to_set->codecs().end();
if (!created_rrtr && set_rrtr) {
RTC_LOG(LS_WARNING) << "SDP munging: audio rrtr enabled.";
return SdpMungingType::kAudioCodecsRtcpFbRrtr;
}
// Opus FEC is on by default. Should not be munged, can be controlled by
// the other side.
bool created_opus_fec =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamUseInbandFec,
&value) &&
value == kParamValueTrue;
}) != last_created_media_description->codecs().end();
bool set_opus_fec =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamUseInbandFec, &value) &&
value == kParamValueTrue;
}) != media_description_to_set->codecs().end();
if (created_opus_fec && !set_opus_fec) {
RTC_LOG(LS_WARNING) << "SDP munging: Opus FEC disabled.";
return SdpMungingType::kAudioCodecsFmtpOpusFec;
}
// Opus DTX is off by default. Should not be munged, can be controlled by
// the other side.
bool created_opus_dtx =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamUseDtx, &value) &&
value == kParamValueTrue;
}) != last_created_media_description->codecs().end();
bool set_opus_dtx =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamUseDtx, &value) &&
value == kParamValueTrue;
}) != media_description_to_set->codecs().end();
if (!created_opus_dtx && set_opus_dtx) {
RTC_LOG(LS_WARNING) << "SDP munging: Opus DTX enabled.";
return SdpMungingType::kAudioCodecsFmtpOpusDtx;
}
// Opus CBR is off by default. Should not be munged, can be controlled by
// the other side.
bool created_opus_cbr =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamCbr, &value) &&
value == kParamValueTrue;
}) != last_created_media_description->codecs().end();
bool set_opus_cbr =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
std::string value;
return codec.name == kOpusCodecName &&
codec.GetParam(kCodecParamCbr, &value) &&
value == kParamValueTrue;
}) != media_description_to_set->codecs().end();
if (!created_opus_cbr && set_opus_cbr) {
RTC_LOG(LS_WARNING) << "SDP munging: Opus CBR enabled.";
return SdpMungingType::kAudioCodecsFmtpOpusCbr;
}
return SdpMungingType::kNoModification;
}
SdpMungingType DetermineVideoSdpMungingType(
const MediaContentDescription* last_created_media_description,
const MediaContentDescription* media_description_to_set) {
RTC_DCHECK(last_created_media_description);
RTC_DCHECK(media_description_to_set);
// Removing codecs should be done via setCodecPreferences or negotiation, not
// munging.
if (last_created_media_description->codecs().size() >
media_description_to_set->codecs().size()) {
RTC_LOG(LS_WARNING) << "SDP munging: video codecs removed.";
return SdpMungingType::kVideoCodecsRemoved;
}
if (last_created_media_description->codecs().size() <
media_description_to_set->codecs().size()) {
// Nonstandard a=packetization:raw
bool created_raw_packetization =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
return codec.packetization.has_value();
}) != last_created_media_description->codecs().end();
bool set_raw_packetization =
absl::c_find_if(media_description_to_set->codecs(),
[](const Codec codec) {
return codec.packetization.has_value();
}) != media_description_to_set->codecs().end();
if (!created_raw_packetization && set_raw_packetization) {
RTC_LOG(LS_WARNING)
<< "SDP munging: video codecs with raw packetization added.";
return SdpMungingType::kVideoCodecsAddedWithRawPacketization;
}
RTC_LOG(LS_WARNING) << "SDP munging: video codecs added.";
return SdpMungingType::kVideoCodecsAdded;
}
// Simulcast munging.
if (last_created_media_description->streams().size() == 1 &&
media_description_to_set->streams().size() == 1) {
bool created_sim =
absl::c_find_if(
last_created_media_description->streams()[0].ssrc_groups,
[](const SsrcGroup group) {
return group.semantics == kSimSsrcGroupSemantics;
}) !=
last_created_media_description->streams()[0].ssrc_groups.end();
bool set_sim =
absl::c_find_if(media_description_to_set->streams()[0].ssrc_groups,
[](const SsrcGroup group) {
return group.semantics == kSimSsrcGroupSemantics;
}) !=
media_description_to_set->streams()[0].ssrc_groups.end();
if (!created_sim && set_sim) {
RTC_LOG(LS_WARNING) << "SDP munging: legacy simulcast group created.";
return SdpMungingType::kVideoCodecsLegacySimulcast;
}
}
// sps-pps-idr-in-keyframe.
bool created_sps_pps_idr_in_keyframe =
absl::c_find_if(last_created_media_description->codecs(),
[](const Codec codec) {
std::string value;
return codec.name == kH264CodecName &&
codec.GetParam(kH264FmtpSpsPpsIdrInKeyframe,
&value) &&
value == kParamValueTrue;
}) != last_created_media_description->codecs().end();
bool set_sps_pps_idr_in_keyframe =
absl::c_find_if(
media_description_to_set->codecs(), [](const Codec codec) {
std::string value;
return codec.name == kH264CodecName &&
codec.GetParam(kH264FmtpSpsPpsIdrInKeyframe, &value) &&
value == kParamValueTrue;
}) != media_description_to_set->codecs().end();
if (!created_sps_pps_idr_in_keyframe && set_sps_pps_idr_in_keyframe) {
RTC_LOG(LS_WARNING) << "SDP munging: sps-pps-idr-in-keyframe enabled.";
return SdpMungingType::kVideoCodecsFmtpH264SpsPpsIdrInKeyframe;
}
return SdpMungingType::kNoModification;
}
} // namespace
// Determine if the SDP was modified between createOffer and
// setLocalDescription.
SdpMungingType DetermineSdpMungingType(
const SessionDescriptionInterface* sdesc,
const SessionDescriptionInterface* last_created_desc) {
if (!sdesc || !sdesc->description()) {
RTC_LOG(LS_WARNING) << "SDP munging: Failed to parse session description.";
return SdpMungingType::kUnknownModification;
}
if (!last_created_desc || !last_created_desc->description()) {
RTC_LOG(LS_WARNING) << "SDP munging: SetLocalDescription called without "
"CreateOffer or CreateAnswer.";
if (sdesc->GetType() == SdpType::kOffer) {
return SdpMungingType::kWithoutCreateOffer;
} else { // answer or pranswer.
return SdpMungingType::kWithoutCreateAnswer;
}
}
// TODO: crbug.com/40567530 - we currently allow answer->pranswer
// so can not check sdesc->GetType() == last_created_desc->GetType().
SdpMungingType type;
// TODO: crbug.com/40567530 - change Chromium so that pointer comparison works
// at least for implicit local description.
if (sdesc->description() == last_created_desc->description()) {
return SdpMungingType::kNoModification;
}
// Validate contents.
const auto& last_created_contents =
last_created_desc->description()->contents();
const auto& contents_to_set = sdesc->description()->contents();
if (last_created_contents.size() != contents_to_set.size()) {
RTC_LOG(LS_WARNING) << "SDP munging: Number of m= sections does not match "
"last created description.";
return SdpMungingType::kNumberOfContents;
}
for (size_t content_index = 0; content_index < last_created_contents.size();
content_index++) {
// TODO: crbug.com/40567530 - more checks are needed here.
if (last_created_contents[content_index].mid() !=
contents_to_set[content_index].mid()) {
RTC_LOG(LS_WARNING) << "SDP munging: mid does not match "
"last created description.";
return SdpMungingType::kMid;
}
auto* last_created_media_description =
last_created_contents[content_index].media_description();
auto* media_description_to_set =
contents_to_set[content_index].media_description();
if (!(last_created_media_description && media_description_to_set)) {
continue;
}
// Validate video and audio contents.
MediaType media_type = last_created_media_description->type();
if (media_type == MediaType::VIDEO) {
type = DetermineVideoSdpMungingType(last_created_media_description,
media_description_to_set);
if (type != SdpMungingType::kNoModification) {
return type;
}
} else if (media_type == MediaType::AUDIO) {
type = DetermineAudioSdpMungingType(last_created_media_description,
media_description_to_set);
if (type != SdpMungingType::kNoModification) {
return type;
}
}
// Validate codecs. We should have bailed out earlier if codecs were added
// or removed.
auto last_created_codecs = last_created_media_description->codecs();
auto codecs_to_set = media_description_to_set->codecs();
if (last_created_codecs.size() == codecs_to_set.size()) {
for (size_t i = 0; i < last_created_codecs.size(); i++) {
if (last_created_codecs[i] == codecs_to_set[i]) {
continue;
}
// Codec position swapped.
for (size_t j = i + 1; j < last_created_codecs.size(); j++) {
if (last_created_codecs[i] == codecs_to_set[j]) {
return media_type == MediaType::AUDIO
? SdpMungingType::kAudioCodecsReordered
: SdpMungingType::kVideoCodecsReordered;
}
}
// Same codec but id changed.
if (last_created_codecs[i].name == codecs_to_set[i].name &&
last_created_codecs[i].id != codecs_to_set[i].id) {
return SdpMungingType::kPayloadTypes;
}
if (last_created_codecs[i].params != codecs_to_set[i].params) {
return media_type == MediaType::AUDIO
? SdpMungingType::kAudioCodecsFmtp
: SdpMungingType::kVideoCodecsFmtp;
}
if (last_created_codecs[i].feedback_params !=
codecs_to_set[i].feedback_params) {
return media_type == MediaType::AUDIO
? SdpMungingType::kAudioCodecsRtcpFb
: SdpMungingType::kVideoCodecsRtcpFb;
}
// Nonstandard a=packetization:raw added by munging
if (media_type == MediaType::VIDEO &&
last_created_codecs[i].packetization !=
codecs_to_set[i].packetization) {
return SdpMungingType::kVideoCodecsModifiedWithRawPacketization;
}
// At this point clockrate or channels changed. This should already be
// rejected later in the process so ignore for munging.
}
}
if (last_created_media_description->direction() !=
media_description_to_set->direction()) {
RTC_LOG(LS_WARNING) << "SDP munging: transceiver direction modified.";
return SdpMungingType::kDirection;
}
// Validate media streams.
if (last_created_media_description->streams().size() !=
media_description_to_set->streams().size()) {
RTC_LOG(LS_WARNING) << "SDP munging: streams size does not match last "
"created description.";
return SdpMungingType::kSsrcs;
}
for (size_t i = 0; i < last_created_media_description->streams().size();
i++) {
if (last_created_media_description->streams()[i].ssrcs !=
media_description_to_set->streams()[i].ssrcs) {
RTC_LOG(LS_WARNING)
<< "SDP munging: SSRCs do not match last created description.";
return SdpMungingType::kSsrcs;
}
}
// Validate RTP header extensions.
auto last_created_extensions =
last_created_media_description->rtp_header_extensions();
auto extensions_to_set = media_description_to_set->rtp_header_extensions();
if (last_created_extensions.size() < extensions_to_set.size()) {
RTC_LOG(LS_WARNING) << "SDP munging: RTP header extension added.";
return SdpMungingType::kRtpHeaderExtensionAdded;
}
if (last_created_extensions.size() > extensions_to_set.size()) {
RTC_LOG(LS_WARNING) << "SDP munging: RTP header extension removed.";
return SdpMungingType::kRtpHeaderExtensionRemoved;
}
for (size_t i = 0; i < last_created_extensions.size(); i++) {
if (!(last_created_extensions[i].id == extensions_to_set[i].id)) {
RTC_LOG(LS_WARNING) << "SDP munging: header extension modified.";
return SdpMungingType::kRtpHeaderExtensionModified;
}
}
}
// Validate transport descriptions.
type = DetermineTransportModification(
last_created_desc->description()->transport_infos(),
sdesc->description()->transport_infos());
if (type != SdpMungingType::kNoModification) {
return type;
}
// TODO: crbug.com/40567530 - this serializes the descriptions back to a SDP
// string which is very complex and we not should be be forced to rely on
// string equality.
std::string serialized_description;
std::string serialized_last_description;
if (sdesc->ToString(&serialized_description) &&
last_created_desc->ToString(&serialized_last_description) &&
serialized_description == serialized_last_description) {
return SdpMungingType::kNoModification;
}
return SdpMungingType::kUnknownModification;
}
// Similar to DetermineSdpMungingType, but only checks whether the ICE ufrag or
// pwd of the SDP has been modified between createOffer and setLocalDescription.
bool HasUfragSdpMunging(const SessionDescriptionInterface* sdesc,
const SessionDescriptionInterface* last_created_desc) {
if (!sdesc || !sdesc->description()) {
RTC_LOG(LS_WARNING) << "SDP munging: Failed to parse session description.";
return false;
}
if (!last_created_desc || !last_created_desc->description()) {
RTC_LOG(LS_WARNING) << "SDP munging: SetLocalDescription called without "
"CreateOffer or CreateAnswer.";
return false;
}
TransportInfos last_created_transport_infos =
last_created_desc->description()->transport_infos();
TransportInfos transport_infos_to_set =
sdesc->description()->transport_infos();
for (size_t i = 0; i < std::min(last_created_transport_infos.size(),
transport_infos_to_set.size());
i++) {
if (last_created_transport_infos[i].description.ice_ufrag !=
transport_infos_to_set[i].description.ice_ufrag) {
return true;
}
if (last_created_transport_infos[i].description.ice_pwd !=
transport_infos_to_set[i].description.ice_pwd) {
return true;
}
}
return false;
}
} // namespace webrtc
|