| 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/media_session.h" |
| 12 |
| 13 | #include <stddef.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <map> |
| 17 | #include <string> |
| 18 | #include <unordered_map> |
| 19 | #include <utility> |
| 20 |
| 21 | #include "absl/algorithm/container.h" |
| 22 | #include "absl/strings/match.h" |
| 23 | #include "absl/strings/string_view.h" |
| 24 | #include "absl/types/optional.h" |
| 25 | #include "media/base/codec.h" |
| 26 | #include "media/base/media_constants.h" |
| 27 | #include "media/base/media_engine.h" |
| 28 | #include "media/base/sdp_video_format_utils.h" |
| 29 | #include "media/sctp/sctp_transport_internal.h" |
| 30 | #include "p2p/base/p2p_constants.h" |
| 31 | #include "pc/media_protocol_names.h" |
| 32 | #include "pc/rtp_media_utils.h" |
| 33 | #include "pc/used_ids.h" |
| 34 | #include "rtc_base/checks.h" |
| 35 | #include "rtc_base/helpers.h" |
| 36 | #include "rtc_base/logging.h" |
| 37 | #include "rtc_base/ssl_stream_adapter.h" |
| 38 | #include "rtc_base/string_encode.h" |
| 39 | #include "rtc_base/third_party/base64/base64.h" |
| 40 | #include "rtc_base/unique_id_generator.h" |
| 41 |
| 42 | namespace { |
| 43 |
| 44 | using rtc::UniqueRandomIdGenerator; |
| 45 | using webrtc::RTCError; |
| 46 | using webrtc::RTCErrorType; |
| 47 | using webrtc::RtpTransceiverDirection; |
| 48 |
| 49 | webrtc::RtpExtension RtpExtensionFromCapability( |
| 50 | const webrtc::RtpHeaderExtensionCapability& capability) { |
| 51 | return webrtc::RtpExtension(capability.uri, |
| 52 | capability.preferred_id.value_or(1)); |
| 53 | } |
| 54 |
| 55 | cricket::RtpHeaderExtensions RtpHeaderExtensionsFromCapabilities( |
| 56 | const std::vector<webrtc::RtpHeaderExtensionCapability>& capabilities) { |
| 57 | cricket::RtpHeaderExtensions exts; |
| 58 | for (const auto& capability : capabilities) { |
| 59 | exts.push_back(RtpExtensionFromCapability(capability)); |
| 60 | } |
| 61 | return exts; |
| 62 | } |
| 63 |
| 64 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 65 | UnstoppedRtpHeaderExtensionCapabilities( |
| 66 | std::vector<webrtc::RtpHeaderExtensionCapability> capabilities) { |
| 67 | capabilities.erase( |
| 68 | std::remove_if( |
| 69 | capabilities.begin(), capabilities.end(), |
| 70 | [](const webrtc::RtpHeaderExtensionCapability& capability) { |
| 71 | return capability.direction == RtpTransceiverDirection::kStopped; |
| 72 | }), |
| 73 | capabilities.end()); |
| 74 | return capabilities; |
| 75 | } |
| 76 |
| 77 | bool IsCapabilityPresent(const webrtc::RtpHeaderExtensionCapability& capability, |
| 78 | const cricket::RtpHeaderExtensions& extensions) { |
| 79 | return std::find_if(extensions.begin(), extensions.end(), |
| 80 | [&capability](const webrtc::RtpExtension& extension) { |
| 81 | return capability.uri == extension.uri; |
| 82 | }) != extensions.end(); |
| 83 | } |
| 84 |
| 85 | cricket::RtpHeaderExtensions UnstoppedOrPresentRtpHeaderExtensions( |
| 86 | const std::vector<webrtc::RtpHeaderExtensionCapability>& capabilities, |
| 87 | const cricket::RtpHeaderExtensions& unencrypted, |
| 88 | const cricket::RtpHeaderExtensions& encrypted) { |
| 89 | cricket::RtpHeaderExtensions extensions; |
| 90 | for (const auto& capability : capabilities) { |
| 91 | if (capability.direction != RtpTransceiverDirection::kStopped || |
| 92 | IsCapabilityPresent(capability, unencrypted) || |
| 93 | IsCapabilityPresent(capability, encrypted)) { |
| 94 | extensions.push_back(RtpExtensionFromCapability(capability)); |
| 95 | } |
| 96 | } |
| 97 | return extensions; |
| 98 | } |
| 99 |
| 100 | } // namespace |
| 101 |
| 102 | namespace cricket { |
| 103 |
| 104 | namespace { |
| 105 |
| 106 | bool IsRtxCodec(const webrtc::RtpCodecCapability& capability) { |
| 107 | return absl::EqualsIgnoreCase(capability.name, kRtxCodecName); |
| 108 | } |
| 109 |
| 110 | bool ContainsRtxCodec(const std::vector<Codec>& codecs) { |
| 111 | return absl::c_find_if(codecs, [](const Codec& c) { |
| 112 | return c.GetResiliencyType() == Codec::ResiliencyType::kRtx; |
| 113 | }) != codecs.end(); |
| 114 | } |
| 115 |
| 116 | bool IsRedCodec(const webrtc::RtpCodecCapability& capability) { |
| 117 | return absl::EqualsIgnoreCase(capability.name, kRedCodecName); |
| 118 | } |
| 119 |
| 120 | bool ContainsFlexfecCodec(const std::vector<Codec>& codecs) { |
| 121 | return absl::c_find_if(codecs, [](const Codec& c) { |
| 122 | return c.GetResiliencyType() == Codec::ResiliencyType::kFlexfec; |
| 123 | }) != codecs.end(); |
| 124 | } |
| 125 |
| 126 | bool IsComfortNoiseCodec(const Codec& codec) { |
| 127 | return absl::EqualsIgnoreCase(codec.name, kComfortNoiseCodecName); |
| 128 | } |
| 129 |
| 130 | void StripCNCodecs(AudioCodecs* audio_codecs) { |
| 131 | audio_codecs->erase(std::remove_if(audio_codecs->begin(), audio_codecs->end(), |
| 132 | [](const AudioCodec& codec) { |
| 133 | return IsComfortNoiseCodec(codec); |
| 134 | }), |
| 135 | audio_codecs->end()); |
| 136 | } |
| 137 |
| 138 | RtpTransceiverDirection NegotiateRtpTransceiverDirection( |
| 139 | RtpTransceiverDirection offer, |
| 140 | RtpTransceiverDirection wants) { |
| 141 | bool offer_send = webrtc::RtpTransceiverDirectionHasSend(offer); |
| 142 | bool offer_recv = webrtc::RtpTransceiverDirectionHasRecv(offer); |
| 143 | bool wants_send = webrtc::RtpTransceiverDirectionHasSend(wants); |
| 144 | bool wants_recv = webrtc::RtpTransceiverDirectionHasRecv(wants); |
| 145 | return webrtc::RtpTransceiverDirectionFromSendRecv(offer_recv && wants_send, |
| 146 | offer_send && wants_recv); |
| 147 | } |
| 148 |
| 149 | bool IsMediaContentOfType(const ContentInfo* content, MediaType media_type) { |
| 150 | if (!content || !content->media_description()) { |
| 151 | return false; |
| 152 | } |
| 153 | return content->media_description()->type() == media_type; |
| 154 | } |
| 155 |
| 156 | // Finds all StreamParams of all media types and attach them to stream_params. |
| 157 | StreamParamsVec GetCurrentStreamParams( |
| 158 | const std::vector<const ContentInfo*>& active_local_contents) { |
| 159 | StreamParamsVec stream_params; |
| 160 | for (const ContentInfo* content : active_local_contents) { |
| 161 | for (const StreamParams& params : content->media_description()->streams()) { |
| 162 | stream_params.push_back(params); |
| 163 | } |
| 164 | } |
| 165 | return stream_params; |
| 166 | } |
| 167 |
| 168 | StreamParams CreateStreamParamsForNewSenderWithSsrcs( |
| 169 | const SenderOptions& sender, |
| 170 | const std::string& rtcp_cname, |
| 171 | bool include_rtx_streams, |
| 172 | bool include_flexfec_stream, |
| 173 | UniqueRandomIdGenerator* ssrc_generator, |
| 174 | const webrtc::FieldTrialsView& field_trials) { |
| 175 | StreamParams result; |
| 176 | result.id = sender.track_id; |
| 177 |
| 178 | // TODO(brandtr): Update when we support multistream protection. |
| 179 | if (include_flexfec_stream && sender.num_sim_layers > 1) { |
| 180 | include_flexfec_stream = false; |
| 181 | RTC_LOG(LS_WARNING) |
| 182 | << "Our FlexFEC implementation only supports protecting " |
| 183 | "a single media streams. This session has multiple " |
| 184 | "media streams however, so no FlexFEC SSRC will be generated."; |
| 185 | } |
| 186 | if (include_flexfec_stream && !field_trials.IsEnabled("WebRTC-FlexFEC-03")) { |
| 187 | include_flexfec_stream = false; |
| 188 | RTC_LOG(LS_WARNING) |
| 189 | << "WebRTC-FlexFEC trial is not enabled, not sending FlexFEC"; |
| 190 | } |
| 191 |
| 192 | result.GenerateSsrcs(sender.num_sim_layers, include_rtx_streams, |
| 193 | include_flexfec_stream, ssrc_generator); |
| 194 |
| 195 | result.cname = rtcp_cname; |
| 196 | result.set_stream_ids(sender.stream_ids); |
| 197 |
| 198 | return result; |
| 199 | } |
| 200 |
| 201 | bool ValidateSimulcastLayers(const std::vector<RidDescription>& rids, |
| 202 | const SimulcastLayerList& simulcast_layers) { |
| 203 | return absl::c_all_of( |
| 204 | simulcast_layers.GetAllLayers(), [&rids](const SimulcastLayer& layer) { |
| 205 | return absl::c_any_of(rids, [&layer](const RidDescription& rid) { |
| 206 | return rid.rid == layer.rid; |
| 207 | }); |
| 208 | }); |
| 209 | } |
| 210 |
| 211 | StreamParams CreateStreamParamsForNewSenderWithRids( |
| 212 | const SenderOptions& sender, |
| 213 | const std::string& rtcp_cname) { |
| 214 | RTC_DCHECK(!sender.rids.empty()); |
| 215 | RTC_DCHECK_EQ(sender.num_sim_layers, 0) |
| 216 | << "RIDs are the compliant way to indicate simulcast."; |
| 217 | RTC_DCHECK(ValidateSimulcastLayers(sender.rids, sender.simulcast_layers)); |
| 218 | StreamParams result; |
| 219 | result.id = sender.track_id; |
| 220 | result.cname = rtcp_cname; |
| 221 | result.set_stream_ids(sender.stream_ids); |
| 222 |
| 223 | // More than one rid should be signaled. |
| 224 | if (sender.rids.size() > 1) { |
| 225 | result.set_rids(sender.rids); |
| 226 | } |
| 227 |
| 228 | return result; |
| 229 | } |
| 230 |
| 231 | // Adds SimulcastDescription if indicated by the media description options. |
| 232 | // MediaContentDescription should already be set up with the send rids. |
| 233 | void AddSimulcastToMediaDescription( |
| 234 | const MediaDescriptionOptions& media_description_options, |
| 235 | MediaContentDescription* description) { |
| 236 | RTC_DCHECK(description); |
| 237 |
| 238 | // Check if we are using RIDs in this scenario. |
| 239 | if (absl::c_all_of(description->streams(), [](const StreamParams& params) { |
| 240 | return !params.has_rids(); |
| 241 | })) { |
| 242 | return; |
| 243 | } |
| 244 |
| 245 | RTC_DCHECK_EQ(1, description->streams().size()) |
| 246 | << "RIDs are only supported in Unified Plan semantics."; |
| 247 | RTC_DCHECK_EQ(1, media_description_options.sender_options.size()); |
| 248 | RTC_DCHECK(description->type() == MediaType::MEDIA_TYPE_AUDIO || |
| 249 | description->type() == MediaType::MEDIA_TYPE_VIDEO); |
| 250 |
| 251 | // One RID or less indicates that simulcast is not needed. |
| 252 | if (description->streams()[0].rids().size() <= 1) { |
| 253 | return; |
| 254 | } |
| 255 |
| 256 | // Only negotiate the send layers. |
| 257 | SimulcastDescription simulcast; |
| 258 | simulcast.send_layers() = |
| 259 | media_description_options.sender_options[0].simulcast_layers; |
| 260 | description->set_simulcast_description(simulcast); |
| 261 | } |
| 262 |
| 263 | // Adds a StreamParams for each SenderOptions in `sender_options` to |
| 264 | // content_description. |
| 265 | // `current_params` - All currently known StreamParams of any media type. |
| 266 | bool AddStreamParams(const std::vector<SenderOptions>& sender_options, |
| 267 | const std::string& rtcp_cname, |
| 268 | UniqueRandomIdGenerator* ssrc_generator, |
| 269 | StreamParamsVec* current_streams, |
| 270 | MediaContentDescription* content_description, |
| 271 | const webrtc::FieldTrialsView& field_trials) { |
| 272 | // SCTP streams are not negotiated using SDP/ContentDescriptions. |
| 273 | if (IsSctpProtocol(content_description->protocol())) { |
| 274 | return true; |
| 275 | } |
| 276 |
| 277 | const bool include_rtx_streams = |
| 278 | ContainsRtxCodec(content_description->codecs()); |
| 279 |
| 280 | const bool include_flexfec_stream = |
| 281 | ContainsFlexfecCodec(content_description->codecs()); |
| 282 |
| 283 | for (const SenderOptions& sender : sender_options) { |
| 284 | StreamParams* param = GetStreamByIds(*current_streams, sender.track_id); |
| 285 | if (!param) { |
| 286 | // This is a new sender. |
| 287 | StreamParams stream_param = |
| 288 | sender.rids.empty() |
| 289 | ? |
| 290 | // Signal SSRCs and legacy simulcast (if requested). |
| 291 | CreateStreamParamsForNewSenderWithSsrcs( |
| 292 | sender, rtcp_cname, include_rtx_streams, |
| 293 | include_flexfec_stream, ssrc_generator, field_trials) |
| 294 | : |
| 295 | // Signal RIDs and spec-compliant simulcast (if requested). |
| 296 | CreateStreamParamsForNewSenderWithRids(sender, rtcp_cname); |
| 297 |
| 298 | content_description->AddStream(stream_param); |
| 299 |
| 300 | // Store the new StreamParams in current_streams. |
| 301 | // This is necessary so that we can use the CNAME for other media types. |
| 302 | current_streams->push_back(stream_param); |
| 303 | } else { |
| 304 | // Use existing generated SSRCs/groups, but update the sync_label if |
| 305 | // necessary. This may be needed if a MediaStreamTrack was moved from one |
| 306 | // MediaStream to another. |
| 307 | param->set_stream_ids(sender.stream_ids); |
| 308 | content_description->AddStream(*param); |
| 309 | } |
| 310 | } |
| 311 | return true; |
| 312 | } |
| 313 |
| 314 | // Updates the transport infos of the `sdesc` according to the given |
| 315 | // `bundle_group`. The transport infos of the content names within the |
| 316 | // `bundle_group` should be updated to use the ufrag, pwd and DTLS role of the |
| 317 | // first content within the `bundle_group`. |
| 318 | bool UpdateTransportInfoForBundle(const ContentGroup& bundle_group, |
| 319 | SessionDescription* sdesc) { |
| 320 | // The bundle should not be empty. |
| 321 | if (!sdesc || !bundle_group.FirstContentName()) { |
| 322 | return false; |
| 323 | } |
| 324 |
| 325 | // We should definitely have a transport for the first content. |
| 326 | const std::string& selected_content_name = *bundle_group.FirstContentName(); |
| 327 | const TransportInfo* selected_transport_info = |
| 328 | sdesc->GetTransportInfoByName(selected_content_name); |
| 329 | if (!selected_transport_info) { |
| 330 | return false; |
| 331 | } |
| 332 |
| 333 | // Set the other contents to use the same ICE credentials. |
| 334 | const std::string& selected_ufrag = |
| 335 | selected_transport_info->description.ice_ufrag; |
| 336 | const std::string& selected_pwd = |
| 337 | selected_transport_info->description.ice_pwd; |
| 338 | ConnectionRole selected_connection_role = |
| 339 | selected_transport_info->description.connection_role; |
| 340 | for (TransportInfo& transport_info : sdesc->transport_infos()) { |
| 341 | if (bundle_group.HasContentName(transport_info.content_name) && |
| 342 | transport_info.content_name != selected_content_name) { |
| 343 | transport_info.description.ice_ufrag = selected_ufrag; |
| 344 | transport_info.description.ice_pwd = selected_pwd; |
| 345 | transport_info.description.connection_role = selected_connection_role; |
| 346 | } |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 |
| 351 | std::vector<const ContentInfo*> GetActiveContents( |
| 352 | const SessionDescription& description, |
| 353 | const MediaSessionOptions& session_options) { |
| 354 | std::vector<const ContentInfo*> active_contents; |
| 355 | for (size_t i = 0; i < description.contents().size(); ++i) { |
| 356 | RTC_DCHECK_LT(i, session_options.media_description_options.size()); |
| 357 | const ContentInfo& content = description.contents()[i]; |
| 358 | const MediaDescriptionOptions& media_options = |
| 359 | session_options.media_description_options[i]; |
| 360 | if (!content.rejected && !media_options.stopped && |
| 361 | content.name == media_options.mid) { |
| 362 | active_contents.push_back(&content); |
| 363 | } |
| 364 | } |
| 365 | return active_contents; |
| 366 | } |
| 367 |
| 368 | // Create a media content to be offered for the given `sender_options`, |
| 369 | // according to the given options.rtcp_mux, session_options.is_muc, codecs, |
| 370 | // secure_transport, crypto, and current_streams. If we don't currently have |
| 371 | // crypto (in current_cryptos) and it is enabled (in secure_policy), crypto is |
| 372 | // created (according to crypto_suites). The created content is added to the |
| 373 | // offer. |
| 374 | RTCError CreateContentOffer( |
| 375 | const MediaDescriptionOptions& media_description_options, |
| 376 | const MediaSessionOptions& session_options, |
| 377 | const RtpHeaderExtensions& rtp_extensions, |
| 378 | UniqueRandomIdGenerator* ssrc_generator, |
| 379 | StreamParamsVec* current_streams, |
| 380 | MediaContentDescription* offer) { |
| 381 | offer->set_rtcp_mux(session_options.rtcp_mux_enabled); |
| 382 | if (offer->type() == cricket::MEDIA_TYPE_VIDEO) { |
| 383 | offer->set_rtcp_reduced_size(true); |
| 384 | } |
| 385 |
| 386 | // Build the vector of header extensions with directions for this |
| 387 | // media_description's options. |
| 388 | RtpHeaderExtensions extensions; |
| 389 | for (auto extension_with_id : rtp_extensions) { |
| 390 | for (const auto& extension : media_description_options.header_extensions) { |
| 391 | if (extension_with_id.uri == extension.uri) { |
| 392 | // TODO(crbug.com/1051821): Configure the extension direction from |
| 393 | // the information in the media_description_options extension |
| 394 | // capability. |
| 395 | if (extension.direction != RtpTransceiverDirection::kStopped) { |
| 396 | extensions.push_back(extension_with_id); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | offer->set_rtp_header_extensions(extensions); |
| 402 |
| 403 | AddSimulcastToMediaDescription(media_description_options, offer); |
| 404 |
| 405 | return RTCError::OK(); |
| 406 | } |
| 407 |
| 408 | RTCError CreateMediaContentOffer( |
| 409 | const MediaDescriptionOptions& media_description_options, |
| 410 | const MediaSessionOptions& session_options, |
| 411 | const std::vector<Codec>& codecs, |
| 412 | const RtpHeaderExtensions& rtp_extensions, |
| 413 | UniqueRandomIdGenerator* ssrc_generator, |
| 414 | StreamParamsVec* current_streams, |
| 415 | MediaContentDescription* offer, |
| 416 | const webrtc::FieldTrialsView& field_trials) { |
| 417 | offer->AddCodecs(codecs); |
| 418 | if (!AddStreamParams(media_description_options.sender_options, |
| 419 | session_options.rtcp_cname, ssrc_generator, |
| 420 | current_streams, offer, field_trials)) { |
| 421 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 422 | "Failed to add stream parameters"); |
| 423 | } |
| 424 |
| 425 | return CreateContentOffer(media_description_options, session_options, |
| 426 | rtp_extensions, ssrc_generator, current_streams, |
| 427 | offer); |
| 428 | } |
| 429 |
| 430 | bool ReferencedCodecsMatch(const std::vector<Codec>& codecs1, |
| 431 | const int codec1_id, |
| 432 | const std::vector<Codec>& codecs2, |
| 433 | const int codec2_id) { |
| 434 | const Codec* codec1 = FindCodecById(codecs1, codec1_id); |
| 435 | const Codec* codec2 = FindCodecById(codecs2, codec2_id); |
| 436 | return codec1 != nullptr && codec2 != nullptr && codec1->Matches(*codec2); |
| 437 | } |
| 438 |
| 439 | void NegotiatePacketization(const Codec& local_codec, |
| 440 | const Codec& remote_codec, |
| 441 | Codec* negotiated_codec) { |
| 442 | negotiated_codec->packetization = |
| 443 | (local_codec.packetization == remote_codec.packetization) |
| 444 | ? local_codec.packetization |
| 445 | : absl::nullopt; |
| 446 | } |
| 447 |
| 448 | #ifdef RTC_ENABLE_H265 |
| 449 | void NegotiateTxMode(const Codec& local_codec, |
| 450 | const Codec& remote_codec, |
| 451 | Codec* negotiated_codec) { |
| 452 | negotiated_codec->tx_mode = (local_codec.tx_mode == remote_codec.tx_mode) |
| 453 | ? local_codec.tx_mode |
| 454 | : absl::nullopt; |
| 455 | } |
| 456 | #endif |
| 457 |
| 458 | // Finds a codec in `codecs2` that matches `codec_to_match`, which is |
| 459 | // a member of `codecs1`. If `codec_to_match` is an RED or RTX codec, both |
| 460 | // the codecs themselves and their associated codecs must match. |
| 461 | absl::optional<Codec> FindMatchingCodec(const std::vector<Codec>& codecs1, |
| 462 | const std::vector<Codec>& codecs2, |
| 463 | const Codec& codec_to_match) { |
| 464 | // `codec_to_match` should be a member of `codecs1`, in order to look up |
| 465 | // RED/RTX codecs' associated codecs correctly. If not, that's a programming |
| 466 | // error. |
| 467 | RTC_DCHECK(absl::c_any_of(codecs1, [&codec_to_match](const Codec& codec) { |
| 468 | return &codec == &codec_to_match; |
| 469 | })); |
| 470 | for (const Codec& potential_match : codecs2) { |
| 471 | if (potential_match.Matches(codec_to_match)) { |
| 472 | if (codec_to_match.GetResiliencyType() == Codec::ResiliencyType::kRtx) { |
| 473 | int apt_value_1 = 0; |
| 474 | int apt_value_2 = 0; |
| 475 | if (!codec_to_match.GetParam(kCodecParamAssociatedPayloadType, |
| 476 | &apt_value_1) || |
| 477 | !potential_match.GetParam(kCodecParamAssociatedPayloadType, |
| 478 | &apt_value_2)) { |
| 479 | RTC_LOG(LS_WARNING) << "RTX missing associated payload type."; |
| 480 | continue; |
| 481 | } |
| 482 | if (!ReferencedCodecsMatch(codecs1, apt_value_1, codecs2, |
| 483 | apt_value_2)) { |
| 484 | continue; |
| 485 | } |
| 486 | } else if (codec_to_match.GetResiliencyType() == |
| 487 | Codec::ResiliencyType::kRed) { |
| 488 | auto red_parameters_1 = |
| 489 | codec_to_match.params.find(kCodecParamNotInNameValueFormat); |
| 490 | auto red_parameters_2 = |
| 491 | potential_match.params.find(kCodecParamNotInNameValueFormat); |
| 492 | bool has_parameters_1 = red_parameters_1 != codec_to_match.params.end(); |
| 493 | bool has_parameters_2 = |
| 494 | red_parameters_2 != potential_match.params.end(); |
| 495 | if (has_parameters_1 && has_parameters_2) { |
| 496 | // Mixed reference codecs (i.e. 111/112) are not supported. |
| 497 | // Different levels of redundancy between offer and answer are |
| 498 | // since RED is considered to be declarative. |
| 499 | std::vector<absl::string_view> redundant_payloads_1 = |
| 500 | rtc::split(red_parameters_1->second, '/'); |
| 501 | std::vector<absl::string_view> redundant_payloads_2 = |
| 502 | rtc::split(red_parameters_2->second, '/'); |
| 503 | if (redundant_payloads_1.size() > 0 && |
| 504 | redundant_payloads_2.size() > 0) { |
| 505 | bool consistent = true; |
| 506 | for (size_t i = 1; i < redundant_payloads_1.size(); i++) { |
| 507 | if (redundant_payloads_1[i] != redundant_payloads_1[0]) { |
| 508 | consistent = false; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | for (size_t i = 1; i < redundant_payloads_2.size(); i++) { |
| 513 | if (redundant_payloads_2[i] != redundant_payloads_2[0]) { |
| 514 | consistent = false; |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | if (!consistent) { |
| 519 | continue; |
| 520 | } |
| 521 |
| 522 | int red_value_1; |
| 523 | int red_value_2; |
| 524 | if (rtc::FromString(redundant_payloads_1[0], &red_value_1) && |
| 525 | rtc::FromString(redundant_payloads_2[0], &red_value_2)) { |
| 526 | if (!ReferencedCodecsMatch(codecs1, red_value_1, codecs2, |
| 527 | red_value_2)) { |
| 528 | continue; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } else if (has_parameters_1 != has_parameters_2) { |
| 533 | continue; |
| 534 | } |
| 535 | } |
| 536 | return potential_match; |
| 537 | } |
| 538 | } |
| 539 | return absl::nullopt; |
| 540 | } |
| 541 |
| 542 | void NegotiateCodecs(const std::vector<Codec>& local_codecs, |
| 543 | const std::vector<Codec>& offered_codecs, |
| 544 | std::vector<Codec>* negotiated_codecs, |
| 545 | bool keep_offer_order) { |
| 546 | for (const Codec& ours : local_codecs) { |
| 547 | absl::optional<Codec> theirs = |
| 548 | FindMatchingCodec(local_codecs, offered_codecs, ours); |
| 549 | // Note that we intentionally only find one matching codec for each of our |
| 550 | // local codecs, in case the remote offer contains duplicate codecs. |
| 551 | if (theirs) { |
| 552 | Codec negotiated = ours; |
| 553 | NegotiatePacketization(ours, *theirs, &negotiated); |
| 554 | negotiated.IntersectFeedbackParams(*theirs); |
| 555 | if (negotiated.GetResiliencyType() == Codec::ResiliencyType::kRtx) { |
| 556 | const auto apt_it = |
| 557 | theirs->params.find(kCodecParamAssociatedPayloadType); |
| 558 | // FindMatchingCodec shouldn't return something with no apt value. |
| 559 | RTC_DCHECK(apt_it != theirs->params.end()); |
| 560 | negotiated.SetParam(kCodecParamAssociatedPayloadType, apt_it->second); |
| 561 |
| 562 | // We support parsing the declarative rtx-time parameter. |
| 563 | const auto rtx_time_it = theirs->params.find(kCodecParamRtxTime); |
| 564 | if (rtx_time_it != theirs->params.end()) { |
| 565 | negotiated.SetParam(kCodecParamRtxTime, rtx_time_it->second); |
| 566 | } |
| 567 | } else if (negotiated.GetResiliencyType() == |
| 568 | Codec::ResiliencyType::kRed) { |
| 569 | const auto red_it = |
| 570 | theirs->params.find(kCodecParamNotInNameValueFormat); |
| 571 | if (red_it != theirs->params.end()) { |
| 572 | negotiated.SetParam(kCodecParamNotInNameValueFormat, red_it->second); |
| 573 | } |
| 574 | } |
| 575 | if (absl::EqualsIgnoreCase(ours.name, kH264CodecName)) { |
| 576 | webrtc::H264GenerateProfileLevelIdForAnswer(ours.params, theirs->params, |
| 577 | &negotiated.params); |
| 578 | } |
| 579 | #ifdef RTC_ENABLE_H265 |
| 580 | if (absl::EqualsIgnoreCase(ours.name, kH265CodecName)) { |
| 581 | webrtc::H265GenerateProfileTierLevelForAnswer( |
| 582 | ours.params, theirs->params, &negotiated.params); |
| 583 | NegotiateTxMode(ours, *theirs, &negotiated); |
| 584 | } |
| 585 | #endif |
| 586 | negotiated.id = theirs->id; |
| 587 | negotiated.name = theirs->name; |
| 588 | negotiated_codecs->push_back(std::move(negotiated)); |
| 589 | } |
| 590 | } |
| 591 | if (keep_offer_order) { |
| 592 | // RFC3264: Although the answerer MAY list the formats in their desired |
| 593 | // order of preference, it is RECOMMENDED that unless there is a |
| 594 | // specific reason, the answerer list formats in the same relative order |
| 595 | // they were present in the offer. |
| 596 | // This can be skipped when the transceiver has any codec preferences. |
| 597 | std::unordered_map<int, int> payload_type_preferences; |
| 598 | int preference = static_cast<int>(offered_codecs.size() + 1); |
| 599 | for (const Codec& codec : offered_codecs) { |
| 600 | payload_type_preferences[codec.id] = preference--; |
| 601 | } |
| 602 | absl::c_sort(*negotiated_codecs, [&payload_type_preferences]( |
| 603 | const Codec& a, const Codec& b) { |
| 604 | return payload_type_preferences[a.id] > payload_type_preferences[b.id]; |
| 605 | }); |
| 606 | } |
| 607 | } |
| 608 |
| 609 | // Find the codec in `codec_list` that `rtx_codec` is associated with. |
| 610 | const Codec* GetAssociatedCodecForRtx(const std::vector<Codec>& codec_list, |
| 611 | const Codec& rtx_codec) { |
| 612 | std::string associated_pt_str; |
| 613 | if (!rtx_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 614 | &associated_pt_str)) { |
| 615 | RTC_LOG(LS_WARNING) << "RTX codec " << rtx_codec.id |
| 616 | << " is missing an associated payload type."; |
| 617 | return nullptr; |
| 618 | } |
| 619 |
| 620 | int associated_pt; |
| 621 | if (!rtc::FromString(associated_pt_str, &associated_pt)) { |
| 622 | RTC_LOG(LS_WARNING) << "Couldn't convert payload type " << associated_pt_str |
| 623 | << " of RTX codec " << rtx_codec.id |
| 624 | << " to an integer."; |
| 625 | return nullptr; |
| 626 | } |
| 627 |
| 628 | // Find the associated codec for the RTX codec. |
| 629 | const Codec* associated_codec = FindCodecById(codec_list, associated_pt); |
| 630 | if (!associated_codec) { |
| 631 | RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type " |
| 632 | << associated_pt << " for RTX codec " << rtx_codec.id |
| 633 | << "."; |
| 634 | } |
| 635 | return associated_codec; |
| 636 | } |
| 637 |
| 638 | // Find the codec in `codec_list` that `red_codec` is associated with. |
| 639 | const Codec* GetAssociatedCodecForRed(const std::vector<Codec>& codec_list, |
| 640 | const Codec& red_codec) { |
| 641 | std::string fmtp; |
| 642 | if (!red_codec.GetParam(kCodecParamNotInNameValueFormat, &fmtp)) { |
| 643 | // Don't log for video/RED where this is normal. |
| 644 | if (red_codec.type == Codec::Type::kAudio) { |
| 645 | RTC_LOG(LS_WARNING) << "RED codec " << red_codec.id |
| 646 | << " is missing an associated payload type."; |
| 647 | } |
| 648 | return nullptr; |
| 649 | } |
| 650 |
| 651 | std::vector<absl::string_view> redundant_payloads = rtc::split(fmtp, '/'); |
| 652 | if (redundant_payloads.size() < 2) { |
| 653 | return nullptr; |
| 654 | } |
| 655 |
| 656 | absl::string_view associated_pt_str = redundant_payloads[0]; |
| 657 | int associated_pt; |
| 658 | if (!rtc::FromString(associated_pt_str, &associated_pt)) { |
| 659 | RTC_LOG(LS_WARNING) << "Couldn't convert first payload type " |
| 660 | << associated_pt_str << " of RED codec " << red_codec.id |
| 661 | << " to an integer."; |
| 662 | return nullptr; |
| 663 | } |
| 664 |
| 665 | // Find the associated codec for the RED codec. |
| 666 | const Codec* associated_codec = FindCodecById(codec_list, associated_pt); |
| 667 | if (!associated_codec) { |
| 668 | RTC_LOG(LS_WARNING) << "Couldn't find associated codec with payload type " |
| 669 | << associated_pt << " for RED codec " << red_codec.id |
| 670 | << "."; |
| 671 | } |
| 672 | return associated_codec; |
| 673 | } |
| 674 |
| 675 | // Adds all codecs from `reference_codecs` to `offered_codecs` that don't |
| 676 | // already exist in `offered_codecs` and ensure the payload types don't |
| 677 | // collide. |
| 678 | void MergeCodecs(const std::vector<Codec>& reference_codecs, |
| 679 | std::vector<Codec>* offered_codecs, |
| 680 | UsedPayloadTypes* used_pltypes) { |
| 681 | // Add all new codecs that are not RTX/RED codecs. |
| 682 | // The two-pass splitting of the loops means preferring payload types |
| 683 | // of actual codecs with respect to collisions. |
| 684 | for (const Codec& reference_codec : reference_codecs) { |
| 685 | if (reference_codec.GetResiliencyType() != Codec::ResiliencyType::kRtx && |
| 686 | reference_codec.GetResiliencyType() != Codec::ResiliencyType::kRed && |
| 687 | !FindMatchingCodec(reference_codecs, *offered_codecs, |
| 688 | reference_codec)) { |
| 689 | Codec codec = reference_codec; |
| 690 | used_pltypes->FindAndSetIdUsed(&codec); |
| 691 | offered_codecs->push_back(codec); |
| 692 | } |
| 693 | } |
| 694 |
| 695 | // Add all new RTX or RED codecs. |
| 696 | for (const Codec& reference_codec : reference_codecs) { |
| 697 | if (reference_codec.GetResiliencyType() == Codec::ResiliencyType::kRtx && |
| 698 | !FindMatchingCodec(reference_codecs, *offered_codecs, |
| 699 | reference_codec)) { |
| 700 | Codec rtx_codec = reference_codec; |
| 701 | const Codec* associated_codec = |
| 702 | GetAssociatedCodecForRtx(reference_codecs, rtx_codec); |
| 703 | if (!associated_codec) { |
| 704 | continue; |
| 705 | } |
| 706 | // Find a codec in the offered list that matches the reference codec. |
| 707 | // Its payload type may be different than the reference codec. |
| 708 | absl::optional<Codec> matching_codec = FindMatchingCodec( |
| 709 | reference_codecs, *offered_codecs, *associated_codec); |
| 710 | if (!matching_codec) { |
| 711 | RTC_LOG(LS_WARNING) |
| 712 | << "Couldn't find matching " << associated_codec->name << " codec."; |
| 713 | continue; |
| 714 | } |
| 715 |
| 716 | rtx_codec.params[kCodecParamAssociatedPayloadType] = |
| 717 | rtc::ToString(matching_codec->id); |
| 718 | used_pltypes->FindAndSetIdUsed(&rtx_codec); |
| 719 | offered_codecs->push_back(rtx_codec); |
| 720 | } else if (reference_codec.GetResiliencyType() == |
| 721 | Codec::ResiliencyType::kRed && |
| 722 | !FindMatchingCodec(reference_codecs, *offered_codecs, |
| 723 | reference_codec)) { |
| 724 | Codec red_codec = reference_codec; |
| 725 | const Codec* associated_codec = |
| 726 | GetAssociatedCodecForRed(reference_codecs, red_codec); |
| 727 | if (associated_codec) { |
| 728 | absl::optional<Codec> matching_codec = FindMatchingCodec( |
| 729 | reference_codecs, *offered_codecs, *associated_codec); |
| 730 | if (!matching_codec) { |
| 731 | RTC_LOG(LS_WARNING) << "Couldn't find matching " |
| 732 | << associated_codec->name << " codec."; |
| 733 | continue; |
| 734 | } |
| 735 |
| 736 | red_codec.params[kCodecParamNotInNameValueFormat] = |
| 737 | rtc::ToString(matching_codec->id) + "/" + |
| 738 | rtc::ToString(matching_codec->id); |
| 739 | } |
| 740 | used_pltypes->FindAndSetIdUsed(&red_codec); |
| 741 | offered_codecs->push_back(red_codec); |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 |
| 746 | // `codecs` is a full list of codecs with correct payload type mappings, which |
| 747 | // don't conflict with mappings of the other media type; `supported_codecs` is |
| 748 | // a list filtered for the media section`s direction but with default payload |
| 749 | // types. |
| 750 | std::vector<Codec> MatchCodecPreference( |
| 751 | const std::vector<webrtc::RtpCodecCapability>& codec_preferences, |
| 752 | const std::vector<Codec>& codecs, |
| 753 | const std::vector<Codec>& supported_codecs) { |
| 754 | std::vector<Codec> filtered_codecs; |
| 755 | bool want_rtx = false; |
| 756 | bool want_red = false; |
| 757 |
| 758 | for (const auto& codec_preference : codec_preferences) { |
| 759 | if (IsRtxCodec(codec_preference)) { |
| 760 | want_rtx = true; |
| 761 | } else if (IsRedCodec(codec_preference)) { |
| 762 | want_red = true; |
| 763 | } |
| 764 | } |
| 765 | bool red_was_added = false; |
| 766 | for (const auto& codec_preference : codec_preferences) { |
| 767 | auto found_codec = absl::c_find_if( |
| 768 | supported_codecs, [&codec_preference](const Codec& codec) { |
| 769 | webrtc::RtpCodecParameters codec_parameters = |
| 770 | codec.ToCodecParameters(); |
| 771 | return codec_parameters.name == codec_preference.name && |
| 772 | codec_parameters.kind == codec_preference.kind && |
| 773 | codec_parameters.num_channels == |
| 774 | codec_preference.num_channels && |
| 775 | codec_parameters.clock_rate == codec_preference.clock_rate && |
| 776 | codec_parameters.parameters == codec_preference.parameters; |
| 777 | }); |
| 778 |
| 779 | if (found_codec != supported_codecs.end()) { |
| 780 | absl::optional<Codec> found_codec_with_correct_pt = |
| 781 | FindMatchingCodec(supported_codecs, codecs, *found_codec); |
| 782 | if (found_codec_with_correct_pt) { |
| 783 | // RED may already have been added if its primary codec is before RED |
| 784 | // in the codec list. |
| 785 | bool is_red_codec = found_codec_with_correct_pt->GetResiliencyType() == |
| 786 | Codec::ResiliencyType::kRed; |
| 787 | if (!is_red_codec || !red_was_added) { |
| 788 | filtered_codecs.push_back(*found_codec_with_correct_pt); |
| 789 | red_was_added = is_red_codec ? true : red_was_added; |
| 790 | } |
| 791 | std::string id = rtc::ToString(found_codec_with_correct_pt->id); |
| 792 | // Search for the matching rtx or red codec. |
| 793 | if (want_red || want_rtx) { |
| 794 | for (const auto& codec : codecs) { |
| 795 | if (codec.GetResiliencyType() == Codec::ResiliencyType::kRtx) { |
| 796 | const auto apt = |
| 797 | codec.params.find(cricket::kCodecParamAssociatedPayloadType); |
| 798 | if (apt != codec.params.end() && apt->second == id) { |
| 799 | filtered_codecs.push_back(codec); |
| 800 | break; |
| 801 | } |
| 802 | } else if (codec.GetResiliencyType() == |
| 803 | Codec::ResiliencyType::kRed) { |
| 804 | // For RED, do not insert the codec again if it was already |
| 805 | // inserted. audio/red for opus gets enabled by having RED before |
| 806 | // the primary codec. |
| 807 | const auto fmtp = |
| 808 | codec.params.find(cricket::kCodecParamNotInNameValueFormat); |
| 809 | if (fmtp != codec.params.end()) { |
| 810 | std::vector<absl::string_view> redundant_payloads = |
| 811 | rtc::split(fmtp->second, '/'); |
| 812 | if (!redundant_payloads.empty() && |
| 813 | redundant_payloads[0] == id) { |
| 814 | if (!red_was_added) { |
| 815 | filtered_codecs.push_back(codec); |
| 816 | red_was_added = true; |
| 817 | } |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 |
| 828 | return filtered_codecs; |
| 829 | } |
| 830 |
| 831 | // Compute the union of `codecs1` and `codecs2`. |
| 832 | std::vector<Codec> ComputeCodecsUnion(const std::vector<Codec>& codecs1, |
| 833 | const std::vector<Codec>& codecs2) { |
| 834 | std::vector<Codec> all_codecs; |
| 835 | UsedPayloadTypes used_payload_types; |
| 836 | for (const Codec& codec : codecs1) { |
| 837 | Codec codec_mutable = codec; |
| 838 | used_payload_types.FindAndSetIdUsed(&codec_mutable); |
| 839 | all_codecs.push_back(codec_mutable); |
| 840 | } |
| 841 |
| 842 | // Use MergeCodecs to merge the second half of our list as it already checks |
| 843 | // and fixes problems with duplicate payload types. |
| 844 | MergeCodecs(codecs2, &all_codecs, &used_payload_types); |
| 845 |
| 846 | return all_codecs; |
| 847 | } |
| 848 |
| 849 | // Adds all extensions from `reference_extensions` to `offered_extensions` that |
| 850 | // don't already exist in `offered_extensions` and ensure the IDs don't |
| 851 | // collide. If an extension is added, it's also added to `regular_extensions` or |
| 852 | // `encrypted_extensions`, and if the extension is in `regular_extensions` or |
| 853 | // `encrypted_extensions`, its ID is marked as used in `used_ids`. |
| 854 | // `offered_extensions` is for either audio or video while `regular_extensions` |
| 855 | // and `encrypted_extensions` are used for both audio and video. There could be |
| 856 | // overlap between audio extensions and video extensions. |
| 857 | void MergeRtpHdrExts(const RtpHeaderExtensions& reference_extensions, |
| 858 | RtpHeaderExtensions* offered_extensions, |
| 859 | RtpHeaderExtensions* regular_extensions, |
| 860 | RtpHeaderExtensions* encrypted_extensions, |
| 861 | UsedRtpHeaderExtensionIds* used_ids) { |
| 862 | for (auto reference_extension : reference_extensions) { |
| 863 | if (!webrtc::RtpExtension::FindHeaderExtensionByUriAndEncryption( |
| 864 | *offered_extensions, reference_extension.uri, |
| 865 | reference_extension.encrypt)) { |
| 866 | if (reference_extension.encrypt) { |
| 867 | const webrtc::RtpExtension* existing = |
| 868 | webrtc::RtpExtension::FindHeaderExtensionByUriAndEncryption( |
| 869 | *encrypted_extensions, reference_extension.uri, |
| 870 | reference_extension.encrypt); |
| 871 | if (existing) { |
| 872 | offered_extensions->push_back(*existing); |
| 873 | } else { |
| 874 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 875 | encrypted_extensions->push_back(reference_extension); |
| 876 | offered_extensions->push_back(reference_extension); |
| 877 | } |
| 878 | } else { |
| 879 | const webrtc::RtpExtension* existing = |
| 880 | webrtc::RtpExtension::FindHeaderExtensionByUriAndEncryption( |
| 881 | *regular_extensions, reference_extension.uri, |
| 882 | reference_extension.encrypt); |
| 883 | if (existing) { |
| 884 | offered_extensions->push_back(*existing); |
| 885 | } else { |
| 886 | used_ids->FindAndSetIdUsed(&reference_extension); |
| 887 | regular_extensions->push_back(reference_extension); |
| 888 | offered_extensions->push_back(reference_extension); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 |
| 895 | void AddEncryptedVersionsOfHdrExts(RtpHeaderExtensions* offered_extensions, |
| 896 | RtpHeaderExtensions* encrypted_extensions, |
| 897 | UsedRtpHeaderExtensionIds* used_ids) { |
| 898 | RtpHeaderExtensions encrypted_extensions_to_add; |
| 899 | for (const auto& extension : *offered_extensions) { |
| 900 | // Skip existing encrypted offered extension |
| 901 | if (extension.encrypt) { |
| 902 | continue; |
| 903 | } |
| 904 |
| 905 | // Skip if we cannot encrypt the extension |
| 906 | if (!webrtc::RtpExtension::IsEncryptionSupported(extension.uri)) { |
| 907 | continue; |
| 908 | } |
| 909 |
| 910 | // Skip if an encrypted extension with that URI already exists in the |
| 911 | // offered extensions. |
| 912 | const bool have_encrypted_extension = |
| 913 | webrtc::RtpExtension::FindHeaderExtensionByUriAndEncryption( |
| 914 | *offered_extensions, extension.uri, true); |
| 915 | if (have_encrypted_extension) { |
| 916 | continue; |
| 917 | } |
| 918 |
| 919 | // Determine if a shared encrypted extension with that URI already exists. |
| 920 | const webrtc::RtpExtension* shared_encrypted_extension = |
| 921 | webrtc::RtpExtension::FindHeaderExtensionByUriAndEncryption( |
| 922 | *encrypted_extensions, extension.uri, true); |
| 923 | if (shared_encrypted_extension) { |
| 924 | // Re-use the shared encrypted extension |
| 925 | encrypted_extensions_to_add.push_back(*shared_encrypted_extension); |
| 926 | continue; |
| 927 | } |
| 928 |
| 929 | // None exists. Create a new shared encrypted extension from the |
| 930 | // non-encrypted one. |
| 931 | webrtc::RtpExtension new_encrypted_extension(extension); |
| 932 | new_encrypted_extension.encrypt = true; |
| 933 | used_ids->FindAndSetIdUsed(&new_encrypted_extension); |
| 934 | encrypted_extensions->push_back(new_encrypted_extension); |
| 935 | encrypted_extensions_to_add.push_back(new_encrypted_extension); |
| 936 | } |
| 937 |
| 938 | // Append the additional encrypted extensions to be offered |
| 939 | offered_extensions->insert(offered_extensions->end(), |
| 940 | encrypted_extensions_to_add.begin(), |
| 941 | encrypted_extensions_to_add.end()); |
| 942 | } |
| 943 |
| 944 | // Mostly identical to RtpExtension::FindHeaderExtensionByUri but discards any |
| 945 | // encrypted extensions that this implementation cannot encrypt. |
| 946 | const webrtc::RtpExtension* FindHeaderExtensionByUriDiscardUnsupported( |
| 947 | const std::vector<webrtc::RtpExtension>& extensions, |
| 948 | absl::string_view uri, |
| 949 | webrtc::RtpExtension::Filter filter) { |
| 950 | // Note: While it's technically possible to decrypt extensions that we don't |
| 951 | // encrypt, the symmetric API of libsrtp does not allow us to supply |
| 952 | // different IDs for encryption/decryption of header extensions depending on |
| 953 | // whether the packet is inbound or outbound. Thereby, we are limited to |
| 954 | // what we can send in encrypted form. |
| 955 | if (!webrtc::RtpExtension::IsEncryptionSupported(uri)) { |
| 956 | // If there's no encryption support and we only want encrypted extensions, |
| 957 | // there's no point in continuing the search here. |
| 958 | if (filter == webrtc::RtpExtension::kRequireEncryptedExtension) { |
| 959 | return nullptr; |
| 960 | } |
| 961 |
| 962 | // Instruct to only return non-encrypted extensions |
| 963 | filter = webrtc::RtpExtension::Filter::kDiscardEncryptedExtension; |
| 964 | } |
| 965 |
| 966 | return webrtc::RtpExtension::FindHeaderExtensionByUri(extensions, uri, |
| 967 | filter); |
| 968 | } |
| 969 |
| 970 | void NegotiateRtpHeaderExtensions(const RtpHeaderExtensions& local_extensions, |
| 971 | const RtpHeaderExtensions& offered_extensions, |
| 972 | webrtc::RtpExtension::Filter filter, |
| 973 | RtpHeaderExtensions* negotiated_extensions) { |
| 974 | bool frame_descriptor_in_local = false; |
| 975 | bool dependency_descriptor_in_local = false; |
| 976 | bool abs_capture_time_in_local = false; |
| 977 |
| 978 | for (const webrtc::RtpExtension& ours : local_extensions) { |
| 979 | if (ours.uri == webrtc::RtpExtension::kGenericFrameDescriptorUri00) |
| 980 | frame_descriptor_in_local = true; |
| 981 | else if (ours.uri == webrtc::RtpExtension::kDependencyDescriptorUri) |
| 982 | dependency_descriptor_in_local = true; |
| 983 | else if (ours.uri == webrtc::RtpExtension::kAbsoluteCaptureTimeUri) |
| 984 | abs_capture_time_in_local = true; |
| 985 |
| 986 | const webrtc::RtpExtension* theirs = |
| 987 | FindHeaderExtensionByUriDiscardUnsupported(offered_extensions, ours.uri, |
| 988 | filter); |
| 989 | if (theirs) { |
| 990 | // We respond with their RTP header extension id. |
| 991 | negotiated_extensions->push_back(*theirs); |
| 992 | } |
| 993 | } |
| 994 |
| 995 | // Frame descriptors support. If the extension is not present locally, but is |
| 996 | // in the offer, we add it to the list. |
| 997 | if (!dependency_descriptor_in_local) { |
| 998 | const webrtc::RtpExtension* theirs = |
| 999 | FindHeaderExtensionByUriDiscardUnsupported( |
| 1000 | offered_extensions, webrtc::RtpExtension::kDependencyDescriptorUri, |
| 1001 | filter); |
| 1002 | if (theirs) { |
| 1003 | negotiated_extensions->push_back(*theirs); |
| 1004 | } |
| 1005 | } |
| 1006 | if (!frame_descriptor_in_local) { |
| 1007 | const webrtc::RtpExtension* theirs = |
| 1008 | FindHeaderExtensionByUriDiscardUnsupported( |
| 1009 | offered_extensions, |
| 1010 | webrtc::RtpExtension::kGenericFrameDescriptorUri00, filter); |
| 1011 | if (theirs) { |
| 1012 | negotiated_extensions->push_back(*theirs); |
| 1013 | } |
| 1014 | } |
| 1015 |
| 1016 | // Absolute capture time support. If the extension is not present locally, but |
| 1017 | // is in the offer, we add it to the list. |
| 1018 | if (!abs_capture_time_in_local) { |
| 1019 | const webrtc::RtpExtension* theirs = |
| 1020 | FindHeaderExtensionByUriDiscardUnsupported( |
| 1021 | offered_extensions, webrtc::RtpExtension::kAbsoluteCaptureTimeUri, |
| 1022 | filter); |
| 1023 | if (theirs) { |
| 1024 | negotiated_extensions->push_back(*theirs); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 |
| 1029 | bool SetCodecsInAnswer(const MediaContentDescription* offer, |
| 1030 | const std::vector<Codec>& local_codecs, |
| 1031 | const MediaDescriptionOptions& media_description_options, |
| 1032 | const MediaSessionOptions& session_options, |
| 1033 | UniqueRandomIdGenerator* ssrc_generator, |
| 1034 | StreamParamsVec* current_streams, |
| 1035 | MediaContentDescription* answer, |
| 1036 | const webrtc::FieldTrialsView& field_trials) { |
| 1037 | RTC_DCHECK(offer->type() == MEDIA_TYPE_AUDIO || |
| 1038 | offer->type() == MEDIA_TYPE_VIDEO); |
| 1039 | std::vector<Codec> negotiated_codecs; |
| 1040 | NegotiateCodecs(local_codecs, offer->codecs(), &negotiated_codecs, |
| 1041 | media_description_options.codec_preferences.empty()); |
| 1042 | answer->AddCodecs(negotiated_codecs); |
| 1043 | answer->set_protocol(offer->protocol()); |
| 1044 | if (!AddStreamParams(media_description_options.sender_options, |
| 1045 | session_options.rtcp_cname, ssrc_generator, |
| 1046 | current_streams, answer, field_trials)) { |
| 1047 | return false; // Something went seriously wrong. |
| 1048 | } |
| 1049 | return true; |
| 1050 | } |
| 1051 |
| 1052 | // Create a media content to be answered for the given `sender_options` |
| 1053 | // according to the given session_options.rtcp_mux, session_options.streams, |
| 1054 | // codecs, crypto, and current_streams. If we don't currently have crypto (in |
| 1055 | // current_cryptos) and it is enabled (in secure_policy), crypto is created |
| 1056 | // (according to crypto_suites). The codecs, rtcp_mux, and crypto are all |
| 1057 | // negotiated with the offer. If the negotiation fails, this method returns |
| 1058 | // false. The created content is added to the offer. |
| 1059 | bool CreateMediaContentAnswer( |
| 1060 | const MediaContentDescription* offer, |
| 1061 | const MediaDescriptionOptions& media_description_options, |
| 1062 | const MediaSessionOptions& session_options, |
| 1063 | const RtpHeaderExtensions& local_rtp_extensions, |
| 1064 | UniqueRandomIdGenerator* ssrc_generator, |
| 1065 | bool enable_encrypted_rtp_header_extensions, |
| 1066 | StreamParamsVec* current_streams, |
| 1067 | bool bundle_enabled, |
| 1068 | MediaContentDescription* answer) { |
| 1069 | answer->set_extmap_allow_mixed_enum(offer->extmap_allow_mixed_enum()); |
| 1070 | const webrtc::RtpExtension::Filter extensions_filter = |
| 1071 | enable_encrypted_rtp_header_extensions |
| 1072 | ? webrtc::RtpExtension::Filter::kPreferEncryptedExtension |
| 1073 | : webrtc::RtpExtension::Filter::kDiscardEncryptedExtension; |
| 1074 |
| 1075 | // Filter local extensions by capabilities and direction. |
| 1076 | RtpHeaderExtensions local_rtp_extensions_to_reply_with; |
| 1077 | for (auto extension_with_id : local_rtp_extensions) { |
| 1078 | for (const auto& extension : media_description_options.header_extensions) { |
| 1079 | if (extension_with_id.uri == extension.uri) { |
| 1080 | // TODO(crbug.com/1051821): Configure the extension direction from |
| 1081 | // the information in the media_description_options extension |
| 1082 | // capability. For now, do not include stopped extensions. |
| 1083 | // See also crbug.com/webrtc/7477 about the general lack of direction. |
| 1084 | if (extension.direction != RtpTransceiverDirection::kStopped) { |
| 1085 | local_rtp_extensions_to_reply_with.push_back(extension_with_id); |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | RtpHeaderExtensions negotiated_rtp_extensions; |
| 1091 | NegotiateRtpHeaderExtensions(local_rtp_extensions_to_reply_with, |
| 1092 | offer->rtp_header_extensions(), |
| 1093 | extensions_filter, &negotiated_rtp_extensions); |
| 1094 | answer->set_rtp_header_extensions(negotiated_rtp_extensions); |
| 1095 |
| 1096 | answer->set_rtcp_mux(session_options.rtcp_mux_enabled && offer->rtcp_mux()); |
| 1097 | if (answer->type() == cricket::MEDIA_TYPE_VIDEO) { |
| 1098 | answer->set_rtcp_reduced_size(offer->rtcp_reduced_size()); |
| 1099 | } |
| 1100 |
| 1101 | answer->set_remote_estimate(offer->remote_estimate()); |
| 1102 |
| 1103 | AddSimulcastToMediaDescription(media_description_options, answer); |
| 1104 |
| 1105 | answer->set_direction(NegotiateRtpTransceiverDirection( |
| 1106 | offer->direction(), media_description_options.direction)); |
| 1107 |
| 1108 | return true; |
| 1109 | } |
| 1110 |
| 1111 | bool IsMediaProtocolSupported(MediaType type, |
| 1112 | const std::string& protocol, |
| 1113 | bool secure_transport) { |
| 1114 | // Since not all applications serialize and deserialize the media protocol, |
| 1115 | // we will have to accept `protocol` to be empty. |
| 1116 | if (protocol.empty()) { |
| 1117 | return true; |
| 1118 | } |
| 1119 |
| 1120 | if (type == MEDIA_TYPE_DATA) { |
| 1121 | // Check for SCTP |
| 1122 | if (secure_transport) { |
| 1123 | // Most likely scenarios first. |
| 1124 | return IsDtlsSctp(protocol); |
| 1125 | } else { |
| 1126 | return IsPlainSctp(protocol); |
| 1127 | } |
| 1128 | } |
| 1129 |
| 1130 | // Allow for non-DTLS RTP protocol even when using DTLS because that's what |
| 1131 | // JSEP specifies. |
| 1132 | if (secure_transport) { |
| 1133 | // Most likely scenarios first. |
| 1134 | return IsDtlsRtp(protocol) || IsPlainRtp(protocol); |
| 1135 | } else { |
| 1136 | return IsPlainRtp(protocol); |
| 1137 | } |
| 1138 | } |
| 1139 |
| 1140 | void SetMediaProtocol(bool secure_transport, MediaContentDescription* desc) { |
| 1141 | if (secure_transport) |
| 1142 | desc->set_protocol(kMediaProtocolDtlsSavpf); |
| 1143 | else |
| 1144 | desc->set_protocol(kMediaProtocolAvpf); |
| 1145 | } |
| 1146 |
| 1147 | // Gets the TransportInfo of the given `content_name` from the |
| 1148 | // `current_description`. If doesn't exist, returns a new one. |
| 1149 | const TransportDescription* GetTransportDescription( |
| 1150 | const std::string& content_name, |
| 1151 | const SessionDescription* current_description) { |
| 1152 | const TransportDescription* desc = NULL; |
| 1153 | if (current_description) { |
| 1154 | const TransportInfo* info = |
| 1155 | current_description->GetTransportInfoByName(content_name); |
| 1156 | if (info) { |
| 1157 | desc = &info->description; |
| 1158 | } |
| 1159 | } |
| 1160 | return desc; |
| 1161 | } |
| 1162 |
| 1163 | webrtc::RTCErrorOr<AudioCodecs> GetNegotiatedCodecsForOffer( |
| 1164 | const MediaDescriptionOptions& media_description_options, |
| 1165 | const MediaSessionOptions& session_options, |
| 1166 | const ContentInfo* current_content, |
| 1167 | const std::vector<Codec>& codecs, |
| 1168 | const std::vector<Codec>& supported_codecs) { |
| 1169 | std::vector<Codec> filtered_codecs; |
| 1170 | if (!media_description_options.codec_preferences.empty()) { |
| 1171 | // Add the codecs from the current transceiver's codec preferences. |
| 1172 | // They override any existing codecs from previous negotiations. |
| 1173 | filtered_codecs = MatchCodecPreference( |
| 1174 | media_description_options.codec_preferences, codecs, supported_codecs); |
| 1175 | } else { |
| 1176 | // Add the codecs from current content if it exists and is not rejected nor |
| 1177 | // recycled. |
| 1178 | if (current_content && !current_content->rejected && |
| 1179 | current_content->name == media_description_options.mid) { |
| 1180 | if (!IsMediaContentOfType(current_content, |
| 1181 | media_description_options.type)) { |
| 1182 | // Can happen if the remote side re-uses a MID while recycling. |
| 1183 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 1184 | "Media type for content with mid='" + |
| 1185 | current_content->name + |
| 1186 | "' does not match previous type."); |
| 1187 | } |
| 1188 | const MediaContentDescription* mcd = current_content->media_description(); |
| 1189 | for (const Codec& codec : mcd->codecs()) { |
| 1190 | if (FindMatchingCodec(mcd->codecs(), codecs, codec)) { |
| 1191 | filtered_codecs.push_back(codec); |
| 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | // Add other supported codecs. |
| 1196 | for (const Codec& codec : supported_codecs) { |
| 1197 | absl::optional<Codec> found_codec = |
| 1198 | FindMatchingCodec(supported_codecs, codecs, codec); |
| 1199 | if (found_codec && |
| 1200 | !FindMatchingCodec(supported_codecs, filtered_codecs, codec)) { |
| 1201 | // Use the `found_codec` from `codecs` because it has the |
| 1202 | // correctly mapped payload type. |
| 1203 | // This is only done for video since we do not yet have rtx for audio. |
| 1204 | if (media_description_options.type == MEDIA_TYPE_VIDEO && |
| 1205 | found_codec->GetResiliencyType() == Codec::ResiliencyType::kRtx) { |
| 1206 | // For RTX we might need to adjust the apt parameter if we got a |
| 1207 | // remote offer without RTX for a codec for which we support RTX. |
| 1208 | auto referenced_codec = |
| 1209 | GetAssociatedCodecForRtx(supported_codecs, codec); |
| 1210 | RTC_DCHECK(referenced_codec); |
| 1211 |
| 1212 | // Find the codec we should be referencing and point to it. |
| 1213 | absl::optional<Codec> changed_referenced_codec = FindMatchingCodec( |
| 1214 | supported_codecs, filtered_codecs, *referenced_codec); |
| 1215 | if (changed_referenced_codec) { |
| 1216 | found_codec->SetParam(kCodecParamAssociatedPayloadType, |
| 1217 | changed_referenced_codec->id); |
| 1218 | } |
| 1219 | } |
| 1220 | filtered_codecs.push_back(*found_codec); |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 |
| 1225 | if (media_description_options.type == MEDIA_TYPE_AUDIO && |
| 1226 | !session_options.vad_enabled) { |
| 1227 | // If application doesn't want CN codecs in offer. |
| 1228 | StripCNCodecs(&filtered_codecs); |
| 1229 | } else if (media_description_options.type == MEDIA_TYPE_VIDEO && |
| 1230 | session_options.raw_packetization_for_video) { |
| 1231 | for (Codec& codec : filtered_codecs) { |
| 1232 | if (codec.IsMediaCodec()) { |
| 1233 | codec.packetization = kPacketizationParamRaw; |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | return filtered_codecs; |
| 1238 | } |
| 1239 |
| 1240 | webrtc::RTCErrorOr<AudioCodecs> GetNegotiatedCodecsForAnswer( |
| 1241 | const MediaDescriptionOptions& media_description_options, |
| 1242 | const MediaSessionOptions& session_options, |
| 1243 | const ContentInfo* current_content, |
| 1244 | const std::vector<Codec>& codecs, |
| 1245 | const std::vector<Codec>& supported_codecs) { |
| 1246 | std::vector<Codec> filtered_codecs; |
| 1247 |
| 1248 | if (!media_description_options.codec_preferences.empty()) { |
| 1249 | filtered_codecs = MatchCodecPreference( |
| 1250 | media_description_options.codec_preferences, codecs, supported_codecs); |
| 1251 | } else { |
| 1252 | // Add the codecs from current content if it exists and is not rejected nor |
| 1253 | // recycled. |
| 1254 | if (current_content && !current_content->rejected && |
| 1255 | current_content->name == media_description_options.mid) { |
| 1256 | if (!IsMediaContentOfType(current_content, |
| 1257 | media_description_options.type)) { |
| 1258 | // Can happen if the remote side re-uses a MID while recycling. |
| 1259 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 1260 | "Media type for content with mid='" + |
| 1261 | current_content->name + |
| 1262 | "' does not match previous type."); |
| 1263 | } |
| 1264 | const MediaContentDescription* mcd = current_content->media_description(); |
| 1265 | for (const Codec& codec : mcd->codecs()) { |
| 1266 | if (FindMatchingCodec(mcd->codecs(), codecs, codec)) { |
| 1267 | filtered_codecs.push_back(codec); |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | // Add other supported video codecs. |
| 1272 | std::vector<Codec> other_codecs; |
| 1273 | for (const Codec& codec : supported_codecs) { |
| 1274 | if (FindMatchingCodec(supported_codecs, codecs, codec) && |
| 1275 | !FindMatchingCodec(supported_codecs, filtered_codecs, codec)) { |
| 1276 | // We should use the local codec with local parameters and the codec id |
| 1277 | // would be correctly mapped in `NegotiateCodecs`. |
| 1278 | other_codecs.push_back(codec); |
| 1279 | } |
| 1280 | } |
| 1281 |
| 1282 | // Use ComputeCodecsUnion to avoid having duplicate payload IDs. |
| 1283 | // This is a no-op for audio until RTX is added. |
| 1284 | filtered_codecs = ComputeCodecsUnion(filtered_codecs, other_codecs); |
| 1285 | } |
| 1286 |
| 1287 | if (media_description_options.type == MEDIA_TYPE_AUDIO && |
| 1288 | !session_options.vad_enabled) { |
| 1289 | // If application doesn't want CN codecs in offer. |
| 1290 | StripCNCodecs(&filtered_codecs); |
| 1291 | } else if (media_description_options.type == MEDIA_TYPE_VIDEO && |
| 1292 | session_options.raw_packetization_for_video) { |
| 1293 | for (Codec& codec : filtered_codecs) { |
| 1294 | if (codec.IsMediaCodec()) { |
| 1295 | codec.packetization = kPacketizationParamRaw; |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | return filtered_codecs; |
| 1300 | } |
| 1301 |
| 1302 | } // namespace |
| 1303 |
| 1304 | void MediaDescriptionOptions::AddAudioSender( |
| 1305 | const std::string& track_id, |
| 1306 | const std::vector<std::string>& stream_ids) { |
| 1307 | RTC_DCHECK(type == MEDIA_TYPE_AUDIO); |
| 1308 | AddSenderInternal(track_id, stream_ids, {}, SimulcastLayerList(), 1); |
| 1309 | } |
| 1310 |
| 1311 | void MediaDescriptionOptions::AddVideoSender( |
| 1312 | const std::string& track_id, |
| 1313 | const std::vector<std::string>& stream_ids, |
| 1314 | const std::vector<RidDescription>& rids, |
| 1315 | const SimulcastLayerList& simulcast_layers, |
| 1316 | int num_sim_layers) { |
| 1317 | RTC_DCHECK(type == MEDIA_TYPE_VIDEO); |
| 1318 | RTC_DCHECK(rids.empty() || num_sim_layers == 0) |
| 1319 | << "RIDs are the compliant way to indicate simulcast."; |
| 1320 | RTC_DCHECK(ValidateSimulcastLayers(rids, simulcast_layers)); |
| 1321 | AddSenderInternal(track_id, stream_ids, rids, simulcast_layers, |
| 1322 | num_sim_layers); |
| 1323 | } |
| 1324 |
| 1325 | void MediaDescriptionOptions::AddSenderInternal( |
| 1326 | const std::string& track_id, |
| 1327 | const std::vector<std::string>& stream_ids, |
| 1328 | const std::vector<RidDescription>& rids, |
| 1329 | const SimulcastLayerList& simulcast_layers, |
| 1330 | int num_sim_layers) { |
| 1331 | // TODO(steveanton): Support any number of stream ids. |
| 1332 | RTC_CHECK(stream_ids.size() == 1U); |
| 1333 | SenderOptions options; |
| 1334 | options.track_id = track_id; |
| 1335 | options.stream_ids = stream_ids; |
| 1336 | options.simulcast_layers = simulcast_layers; |
| 1337 | options.rids = rids; |
| 1338 | options.num_sim_layers = num_sim_layers; |
| 1339 | sender_options.push_back(options); |
| 1340 | } |
| 1341 |
| 1342 | bool MediaSessionOptions::HasMediaDescription(MediaType type) const { |
| 1343 | return absl::c_any_of( |
| 1344 | media_description_options, |
| 1345 | [type](const MediaDescriptionOptions& t) { return t.type == type; }); |
| 1346 | } |
| 1347 |
| 1348 | MediaSessionDescriptionFactory::MediaSessionDescriptionFactory( |
| 1349 | cricket::MediaEngineInterface* media_engine, |
| 1350 | bool rtx_enabled, |
| 1351 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
| 1352 | const TransportDescriptionFactory* transport_desc_factory) |
| 1353 | : ssrc_generator_(ssrc_generator), |
| 1354 | transport_desc_factory_(transport_desc_factory) { |
| 1355 | RTC_CHECK(transport_desc_factory_); |
| 1356 | if (media_engine) { |
| 1357 | audio_send_codecs_ = media_engine->voice().send_codecs(); |
| 1358 | audio_recv_codecs_ = media_engine->voice().recv_codecs(); |
| 1359 | video_send_codecs_ = media_engine->video().send_codecs(rtx_enabled); |
| 1360 | video_recv_codecs_ = media_engine->video().recv_codecs(rtx_enabled); |
| 1361 | } |
| 1362 | ComputeAudioCodecsIntersectionAndUnion(); |
| 1363 | ComputeVideoCodecsIntersectionAndUnion(); |
| 1364 | } |
| 1365 |
| 1366 | const AudioCodecs& MediaSessionDescriptionFactory::audio_sendrecv_codecs() |
| 1367 | const { |
| 1368 | return audio_sendrecv_codecs_; |
| 1369 | } |
| 1370 |
| 1371 | const AudioCodecs& MediaSessionDescriptionFactory::audio_send_codecs() const { |
| 1372 | return audio_send_codecs_; |
| 1373 | } |
| 1374 |
| 1375 | const AudioCodecs& MediaSessionDescriptionFactory::audio_recv_codecs() const { |
| 1376 | return audio_recv_codecs_; |
| 1377 | } |
| 1378 |
| 1379 | void MediaSessionDescriptionFactory::set_audio_codecs( |
| 1380 | const AudioCodecs& send_codecs, |
| 1381 | const AudioCodecs& recv_codecs) { |
| 1382 | audio_send_codecs_ = send_codecs; |
| 1383 | audio_recv_codecs_ = recv_codecs; |
| 1384 | ComputeAudioCodecsIntersectionAndUnion(); |
| 1385 | } |
| 1386 |
| 1387 | const VideoCodecs& MediaSessionDescriptionFactory::video_sendrecv_codecs() |
| 1388 | const { |
| 1389 | return video_sendrecv_codecs_; |
| 1390 | } |
| 1391 |
| 1392 | const VideoCodecs& MediaSessionDescriptionFactory::video_send_codecs() const { |
| 1393 | return video_send_codecs_; |
| 1394 | } |
| 1395 |
| 1396 | const VideoCodecs& MediaSessionDescriptionFactory::video_recv_codecs() const { |
| 1397 | return video_recv_codecs_; |
| 1398 | } |
| 1399 |
| 1400 | void MediaSessionDescriptionFactory::set_video_codecs( |
| 1401 | const VideoCodecs& send_codecs, |
| 1402 | const VideoCodecs& recv_codecs) { |
| 1403 | video_send_codecs_ = send_codecs; |
| 1404 | video_recv_codecs_ = recv_codecs; |
| 1405 | ComputeVideoCodecsIntersectionAndUnion(); |
| 1406 | } |
| 1407 |
| 1408 | RtpHeaderExtensions |
| 1409 | MediaSessionDescriptionFactory::filtered_rtp_header_extensions( |
| 1410 | RtpHeaderExtensions extensions) const { |
| 1411 | if (!is_unified_plan_) { |
| 1412 | // Remove extensions only supported with unified-plan. |
| 1413 | extensions.erase( |
| 1414 | std::remove_if( |
| 1415 | extensions.begin(), extensions.end(), |
| 1416 | [](const webrtc::RtpExtension& extension) { |
| 1417 | return extension.uri == webrtc::RtpExtension::kMidUri || |
| 1418 | extension.uri == webrtc::RtpExtension::kRidUri || |
| 1419 | extension.uri == webrtc::RtpExtension::kRepairedRidUri; |
| 1420 | }), |
| 1421 | extensions.end()); |
| 1422 | } |
| 1423 | return extensions; |
| 1424 | } |
| 1425 |
| 1426 | webrtc::RTCErrorOr<std::unique_ptr<SessionDescription>> |
| 1427 | MediaSessionDescriptionFactory::CreateOfferOrError( |
| 1428 | const MediaSessionOptions& session_options, |
| 1429 | const SessionDescription* current_description) const { |
| 1430 | // Must have options for each existing section. |
| 1431 | if (current_description) { |
| 1432 | RTC_DCHECK_LE(current_description->contents().size(), |
| 1433 | session_options.media_description_options.size()); |
| 1434 | } |
| 1435 |
| 1436 | IceCredentialsIterator ice_credentials( |
| 1437 | session_options.pooled_ice_credentials); |
| 1438 |
| 1439 | std::vector<const ContentInfo*> current_active_contents; |
| 1440 | if (current_description) { |
| 1441 | current_active_contents = |
| 1442 | GetActiveContents(*current_description, session_options); |
| 1443 | } |
| 1444 |
| 1445 | StreamParamsVec current_streams = |
| 1446 | GetCurrentStreamParams(current_active_contents); |
| 1447 |
| 1448 | AudioCodecs offer_audio_codecs; |
| 1449 | VideoCodecs offer_video_codecs; |
| 1450 | GetCodecsForOffer(current_active_contents, &offer_audio_codecs, |
| 1451 | &offer_video_codecs); |
| 1452 | AudioVideoRtpHeaderExtensions extensions_with_ids = |
| 1453 | GetOfferedRtpHeaderExtensionsWithIds( |
| 1454 | current_active_contents, session_options.offer_extmap_allow_mixed, |
| 1455 | session_options.media_description_options); |
| 1456 |
| 1457 | auto offer = std::make_unique<SessionDescription>(); |
| 1458 |
| 1459 | // Iterate through the media description options, matching with existing media |
| 1460 | // descriptions in `current_description`. |
| 1461 | size_t msection_index = 0; |
| 1462 | for (const MediaDescriptionOptions& media_description_options : |
| 1463 | session_options.media_description_options) { |
| 1464 | const ContentInfo* current_content = nullptr; |
| 1465 | if (current_description && |
| 1466 | msection_index < current_description->contents().size()) { |
| 1467 | current_content = ¤t_description->contents()[msection_index]; |
| 1468 | // Media type must match unless this media section is being recycled. |
| 1469 | } |
| 1470 | RTCError error; |
| 1471 | switch (media_description_options.type) { |
| 1472 | case MEDIA_TYPE_AUDIO: |
| 1473 | case MEDIA_TYPE_VIDEO: |
| 1474 | error = AddRtpContentForOffer( |
| 1475 | media_description_options, session_options, current_content, |
| 1476 | current_description, |
| 1477 | media_description_options.type == MEDIA_TYPE_AUDIO |
| 1478 | ? extensions_with_ids.audio |
| 1479 | : extensions_with_ids.video, |
| 1480 | media_description_options.type == MEDIA_TYPE_AUDIO |
| 1481 | ? offer_audio_codecs |
| 1482 | : offer_video_codecs, |
| 1483 | ¤t_streams, offer.get(), &ice_credentials); |
| 1484 | break; |
| 1485 | case MEDIA_TYPE_DATA: |
| 1486 | error = AddDataContentForOffer(media_description_options, |
| 1487 | session_options, current_content, |
| 1488 | current_description, ¤t_streams, |
| 1489 | offer.get(), &ice_credentials); |
| 1490 | break; |
| 1491 | case MEDIA_TYPE_UNSUPPORTED: |
| 1492 | error = AddUnsupportedContentForOffer( |
| 1493 | media_description_options, session_options, current_content, |
| 1494 | current_description, offer.get(), &ice_credentials); |
| 1495 | break; |
| 1496 | default: |
| 1497 | RTC_DCHECK_NOTREACHED(); |
| 1498 | } |
| 1499 | if (!error.ok()) { |
| 1500 | return error; |
| 1501 | } |
| 1502 | ++msection_index; |
| 1503 | } |
| 1504 |
| 1505 | // Bundle the contents together, if we've been asked to do so, and update any |
| 1506 | // parameters that need to be tweaked for BUNDLE. |
| 1507 | if (session_options.bundle_enabled) { |
| 1508 | ContentGroup offer_bundle(GROUP_TYPE_BUNDLE); |
| 1509 | for (const ContentInfo& content : offer->contents()) { |
| 1510 | if (content.rejected) { |
| 1511 | continue; |
| 1512 | } |
| 1513 | // TODO(deadbeef): There are conditions that make bundling two media |
| 1514 | // descriptions together illegal. For example, they use the same payload |
| 1515 | // type to represent different codecs, or same IDs for different header |
| 1516 | // extensions. We need to detect this and not try to bundle those media |
| 1517 | // descriptions together. |
| 1518 | offer_bundle.AddContentName(content.name); |
| 1519 | } |
| 1520 | if (!offer_bundle.content_names().empty()) { |
| 1521 | offer->AddGroup(offer_bundle); |
| 1522 | if (!UpdateTransportInfoForBundle(offer_bundle, offer.get())) { |
| 1523 | LOG_AND_RETURN_ERROR( |
| 1524 | RTCErrorType::INTERNAL_ERROR, |
| 1525 | "CreateOffer failed to UpdateTransportInfoForBundle"); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 |
| 1530 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1531 | // older endpoints (in particular, older Plan B endpoints). |
| 1532 | if (is_unified_plan_) { |
| 1533 | // Be conservative and signal using both a=msid and a=ssrc lines. Unified |
| 1534 | // Plan answerers will look at a=msid and Plan B answerers will look at the |
| 1535 | // a=ssrc MSID line. |
| 1536 | offer->set_msid_signaling(cricket::kMsidSignalingSemantic | |
| 1537 | cricket::kMsidSignalingMediaSection | |
| 1538 | cricket::kMsidSignalingSsrcAttribute); |
| 1539 | } else { |
| 1540 | // Plan B always signals MSID using a=ssrc lines. |
| 1541 | offer->set_msid_signaling(cricket::kMsidSignalingSemantic | |
| 1542 | cricket::kMsidSignalingSsrcAttribute); |
| 1543 | } |
| 1544 |
| 1545 | offer->set_extmap_allow_mixed(session_options.offer_extmap_allow_mixed); |
| 1546 |
| 1547 | return offer; |
| 1548 | } |
| 1549 |
| 1550 | webrtc::RTCErrorOr<std::unique_ptr<SessionDescription>> |
| 1551 | MediaSessionDescriptionFactory::CreateAnswerOrError( |
| 1552 | const SessionDescription* offer, |
| 1553 | const MediaSessionOptions& session_options, |
| 1554 | const SessionDescription* current_description) const { |
| 1555 | if (!offer) { |
| 1556 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, "Called without offer."); |
| 1557 | } |
| 1558 |
| 1559 | // Must have options for exactly as many sections as in the offer. |
| 1560 | RTC_DCHECK_EQ(offer->contents().size(), |
| 1561 | session_options.media_description_options.size()); |
| 1562 |
| 1563 | IceCredentialsIterator ice_credentials( |
| 1564 | session_options.pooled_ice_credentials); |
| 1565 |
| 1566 | std::vector<const ContentInfo*> current_active_contents; |
| 1567 | if (current_description) { |
| 1568 | current_active_contents = |
| 1569 | GetActiveContents(*current_description, session_options); |
| 1570 | } |
| 1571 |
| 1572 | StreamParamsVec current_streams = |
| 1573 | GetCurrentStreamParams(current_active_contents); |
| 1574 |
| 1575 | // Get list of all possible codecs that respects existing payload type |
| 1576 | // mappings and uses a single payload type space. |
| 1577 | // |
| 1578 | // Note that these lists may be further filtered for each m= section; this |
| 1579 | // step is done just to establish the payload type mappings shared by all |
| 1580 | // sections. |
| 1581 | AudioCodecs answer_audio_codecs; |
| 1582 | VideoCodecs answer_video_codecs; |
| 1583 | GetCodecsForAnswer(current_active_contents, *offer, &answer_audio_codecs, |
| 1584 | &answer_video_codecs); |
| 1585 |
| 1586 | auto answer = std::make_unique<SessionDescription>(); |
| 1587 |
| 1588 | // If the offer supports BUNDLE, and we want to use it too, create a BUNDLE |
| 1589 | // group in the answer with the appropriate content names. |
| 1590 | std::vector<const ContentGroup*> offer_bundles = |
| 1591 | offer->GetGroupsByName(GROUP_TYPE_BUNDLE); |
| 1592 | // There are as many answer BUNDLE groups as offer BUNDLE groups (even if |
| 1593 | // rejected, we respond with an empty group). `offer_bundles`, |
| 1594 | // `answer_bundles` and `bundle_transports` share the same size and indices. |
| 1595 | std::vector<ContentGroup> answer_bundles; |
| 1596 | std::vector<std::unique_ptr<TransportInfo>> bundle_transports; |
| 1597 | answer_bundles.reserve(offer_bundles.size()); |
| 1598 | bundle_transports.reserve(offer_bundles.size()); |
| 1599 | for (size_t i = 0; i < offer_bundles.size(); ++i) { |
| 1600 | answer_bundles.emplace_back(GROUP_TYPE_BUNDLE); |
| 1601 | bundle_transports.emplace_back(nullptr); |
| 1602 | } |
| 1603 |
| 1604 | answer->set_extmap_allow_mixed(offer->extmap_allow_mixed()); |
| 1605 |
| 1606 | // Iterate through the media description options, matching with existing |
| 1607 | // media descriptions in `current_description`. |
| 1608 | size_t msection_index = 0; |
| 1609 | for (const MediaDescriptionOptions& media_description_options : |
| 1610 | session_options.media_description_options) { |
| 1611 | const ContentInfo* offer_content = &offer->contents()[msection_index]; |
| 1612 | // Media types and MIDs must match between the remote offer and the |
| 1613 | // MediaDescriptionOptions. |
| 1614 | RTC_DCHECK( |
| 1615 | IsMediaContentOfType(offer_content, media_description_options.type)); |
| 1616 | RTC_DCHECK(media_description_options.mid == offer_content->name); |
| 1617 | // Get the index of the BUNDLE group that this MID belongs to, if any. |
| 1618 | absl::optional<size_t> bundle_index; |
| 1619 | for (size_t i = 0; i < offer_bundles.size(); ++i) { |
| 1620 | if (offer_bundles[i]->HasContentName(media_description_options.mid)) { |
| 1621 | bundle_index = i; |
| 1622 | break; |
| 1623 | } |
| 1624 | } |
| 1625 | TransportInfo* bundle_transport = |
| 1626 | bundle_index.has_value() ? bundle_transports[bundle_index.value()].get() |
| 1627 | : nullptr; |
| 1628 |
| 1629 | const ContentInfo* current_content = nullptr; |
| 1630 | if (current_description && |
| 1631 | msection_index < current_description->contents().size()) { |
| 1632 | current_content = ¤t_description->contents()[msection_index]; |
| 1633 | } |
| 1634 | RtpHeaderExtensions header_extensions = RtpHeaderExtensionsFromCapabilities( |
| 1635 | UnstoppedRtpHeaderExtensionCapabilities( |
| 1636 | media_description_options.header_extensions)); |
| 1637 | RTCError error; |
| 1638 | switch (media_description_options.type) { |
| 1639 | case MEDIA_TYPE_AUDIO: |
| 1640 | case MEDIA_TYPE_VIDEO: |
| 1641 | error = AddRtpContentForAnswer( |
| 1642 | media_description_options, session_options, offer_content, offer, |
| 1643 | current_content, current_description, bundle_transport, |
| 1644 | media_description_options.type == MEDIA_TYPE_AUDIO |
| 1645 | ? answer_audio_codecs |
| 1646 | : answer_video_codecs, |
| 1647 | header_extensions, ¤t_streams, answer.get(), |
| 1648 | &ice_credentials); |
| 1649 | break; |
| 1650 | case MEDIA_TYPE_DATA: |
| 1651 | error = AddDataContentForAnswer( |
| 1652 | media_description_options, session_options, offer_content, offer, |
| 1653 | current_content, current_description, bundle_transport, |
| 1654 | ¤t_streams, answer.get(), &ice_credentials); |
| 1655 | break; |
| 1656 | case MEDIA_TYPE_UNSUPPORTED: |
| 1657 | error = AddUnsupportedContentForAnswer( |
| 1658 | media_description_options, session_options, offer_content, offer, |
| 1659 | current_content, current_description, bundle_transport, |
| 1660 | answer.get(), &ice_credentials); |
| 1661 | break; |
| 1662 | default: |
| 1663 | RTC_DCHECK_NOTREACHED(); |
| 1664 | } |
| 1665 | if (!error.ok()) { |
| 1666 | return error; |
| 1667 | } |
| 1668 | ++msection_index; |
| 1669 | // See if we can add the newly generated m= section to the BUNDLE group in |
| 1670 | // the answer. |
| 1671 | ContentInfo& added = answer->contents().back(); |
| 1672 | if (!added.rejected && session_options.bundle_enabled && |
| 1673 | bundle_index.has_value()) { |
| 1674 | // The `bundle_index` is for `media_description_options.mid`. |
| 1675 | RTC_DCHECK_EQ(media_description_options.mid, added.name); |
| 1676 | answer_bundles[bundle_index.value()].AddContentName(added.name); |
| 1677 | bundle_transports[bundle_index.value()].reset( |
| 1678 | new TransportInfo(*answer->GetTransportInfoByName(added.name))); |
| 1679 | } |
| 1680 | } |
| 1681 |
| 1682 | // If BUNDLE group(s) were offered, put the same number of BUNDLE groups in |
| 1683 | // the answer even if they're empty. RFC5888 says: |
| 1684 | // |
| 1685 | // A SIP entity that receives an offer that contains an "a=group" line |
| 1686 | // with semantics that are understood MUST return an answer that |
| 1687 | // contains an "a=group" line with the same semantics. |
| 1688 | if (!offer_bundles.empty()) { |
| 1689 | for (const ContentGroup& answer_bundle : answer_bundles) { |
| 1690 | answer->AddGroup(answer_bundle); |
| 1691 |
| 1692 | if (answer_bundle.FirstContentName()) { |
| 1693 | // Share the same ICE credentials and crypto params across all contents, |
| 1694 | // as BUNDLE requires. |
| 1695 | if (!UpdateTransportInfoForBundle(answer_bundle, answer.get())) { |
| 1696 | LOG_AND_RETURN_ERROR( |
| 1697 | RTCErrorType::INTERNAL_ERROR, |
| 1698 | "CreateAnswer failed to UpdateTransportInfoForBundle."); |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 |
| 1704 | // The following determines how to signal MSIDs to ensure compatibility with |
| 1705 | // older endpoints (in particular, older Plan B endpoints). |
| 1706 | if (is_unified_plan_) { |
| 1707 | // Unified Plan needs to look at what the offer included to find the most |
| 1708 | // compatible answer. |
| 1709 | int msid_signaling = offer->msid_signaling(); |
| 1710 | if (msid_signaling == |
| 1711 | (cricket::kMsidSignalingSemantic | cricket::kMsidSignalingMediaSection | |
| 1712 | cricket::kMsidSignalingSsrcAttribute)) { |
| 1713 | // If both a=msid and a=ssrc MSID signaling methods were used, we're |
| 1714 | // probably talking to a Unified Plan endpoint so respond with just |
| 1715 | // a=msid. |
| 1716 | answer->set_msid_signaling(cricket::kMsidSignalingSemantic | |
| 1717 | cricket::kMsidSignalingMediaSection); |
| 1718 | } else if (msid_signaling == cricket::kMsidSignalingSemantic) { |
| 1719 | // We end up here in one of three cases: |
| 1720 | // 1. An empty offer. We'll reply with an empty answer so it doesn't |
| 1721 | // matter what we pick here. |
| 1722 | // 2. A data channel only offer. We won't add any MSIDs to the answer so |
| 1723 | // it also doesn't matter what we pick here. |
| 1724 | // 3. Media that's either sendonly or inactive from the remote endpoint. |
| 1725 | // We don't have any information to say whether the endpoint is Plan B |
| 1726 | // or Unified Plan, so be conservative and send both. |
| 1727 | answer->set_msid_signaling(cricket::kMsidSignalingSemantic | |
| 1728 | cricket::kMsidSignalingMediaSection | |
| 1729 | cricket::kMsidSignalingSsrcAttribute); |
| 1730 | } else { |
| 1731 | // Otherwise, it's clear which method the offerer is using so repeat that |
| 1732 | // back to them. This includes the case where the msid-semantic line is |
| 1733 | // not included. |
| 1734 | answer->set_msid_signaling(msid_signaling); |
| 1735 | } |
| 1736 | } else { |
| 1737 | // Plan B always signals MSID using a=ssrc lines. |
| 1738 | answer->set_msid_signaling(cricket::kMsidSignalingSemantic | |
| 1739 | cricket::kMsidSignalingSsrcAttribute); |
| 1740 | } |
| 1741 |
| 1742 | return answer; |
| 1743 | } |
| 1744 |
| 1745 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForOffer( |
| 1746 | const RtpTransceiverDirection& direction) const { |
| 1747 | switch (direction) { |
| 1748 | // If stream is inactive - generate list as if sendrecv. |
| 1749 | case RtpTransceiverDirection::kSendRecv: |
| 1750 | case RtpTransceiverDirection::kStopped: |
| 1751 | case RtpTransceiverDirection::kInactive: |
| 1752 | return audio_sendrecv_codecs_; |
| 1753 | case RtpTransceiverDirection::kSendOnly: |
| 1754 | return audio_send_codecs_; |
| 1755 | case RtpTransceiverDirection::kRecvOnly: |
| 1756 | return audio_recv_codecs_; |
| 1757 | } |
| 1758 | RTC_CHECK_NOTREACHED(); |
| 1759 | } |
| 1760 |
| 1761 | const AudioCodecs& MediaSessionDescriptionFactory::GetAudioCodecsForAnswer( |
| 1762 | const RtpTransceiverDirection& offer, |
| 1763 | const RtpTransceiverDirection& answer) const { |
| 1764 | switch (answer) { |
| 1765 | // For inactive and sendrecv answers, generate lists as if we were to accept |
| 1766 | // the offer's direction. See RFC 3264 Section 6.1. |
| 1767 | case RtpTransceiverDirection::kSendRecv: |
| 1768 | case RtpTransceiverDirection::kStopped: |
| 1769 | case RtpTransceiverDirection::kInactive: |
| 1770 | return GetAudioCodecsForOffer( |
| 1771 | webrtc::RtpTransceiverDirectionReversed(offer)); |
| 1772 | case RtpTransceiverDirection::kSendOnly: |
| 1773 | return audio_send_codecs_; |
| 1774 | case RtpTransceiverDirection::kRecvOnly: |
| 1775 | return audio_recv_codecs_; |
| 1776 | } |
| 1777 | RTC_CHECK_NOTREACHED(); |
| 1778 | } |
| 1779 |
| 1780 | const VideoCodecs& MediaSessionDescriptionFactory::GetVideoCodecsForOffer( |
| 1781 | const RtpTransceiverDirection& direction) const { |
| 1782 | switch (direction) { |
| 1783 | // If stream is inactive - generate list as if sendrecv. |
| 1784 | case RtpTransceiverDirection::kSendRecv: |
| 1785 | case RtpTransceiverDirection::kStopped: |
| 1786 | case RtpTransceiverDirection::kInactive: |
| 1787 | return video_sendrecv_codecs_; |
| 1788 | case RtpTransceiverDirection::kSendOnly: |
| 1789 | return video_send_codecs_; |
| 1790 | case RtpTransceiverDirection::kRecvOnly: |
| 1791 | return video_recv_codecs_; |
| 1792 | } |
| 1793 | RTC_CHECK_NOTREACHED(); |
| 1794 | } |
| 1795 |
| 1796 | const VideoCodecs& MediaSessionDescriptionFactory::GetVideoCodecsForAnswer( |
| 1797 | const RtpTransceiverDirection& offer, |
| 1798 | const RtpTransceiverDirection& answer) const { |
| 1799 | switch (answer) { |
| 1800 | // For inactive and sendrecv answers, generate lists as if we were to accept |
| 1801 | // the offer's direction. See RFC 3264 Section 6.1. |
| 1802 | case RtpTransceiverDirection::kSendRecv: |
| 1803 | case RtpTransceiverDirection::kStopped: |
| 1804 | case RtpTransceiverDirection::kInactive: |
| 1805 | return GetVideoCodecsForOffer( |
| 1806 | webrtc::RtpTransceiverDirectionReversed(offer)); |
| 1807 | case RtpTransceiverDirection::kSendOnly: |
| 1808 | return video_send_codecs_; |
| 1809 | case RtpTransceiverDirection::kRecvOnly: |
| 1810 | return video_recv_codecs_; |
| 1811 | } |
| 1812 | RTC_CHECK_NOTREACHED(); |
| 1813 | } |
| 1814 |
| 1815 | void MergeCodecsFromDescription( |
| 1816 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1817 | AudioCodecs* audio_codecs, |
| 1818 | VideoCodecs* video_codecs, |
| 1819 | UsedPayloadTypes* used_pltypes) { |
| 1820 | for (const ContentInfo* content : current_active_contents) { |
| 1821 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
| 1822 | MergeCodecs(content->media_description()->codecs(), audio_codecs, |
| 1823 | used_pltypes); |
| 1824 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
| 1825 | MergeCodecs(content->media_description()->codecs(), video_codecs, |
| 1826 | used_pltypes); |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 |
| 1831 | // Getting codecs for an offer involves these steps: |
| 1832 | // |
| 1833 | // 1. Construct payload type -> codec mappings for current description. |
| 1834 | // 2. Add any reference codecs that weren't already present |
| 1835 | // 3. For each individual media description (m= section), filter codecs based |
| 1836 | // on the directional attribute (happens in another method). |
| 1837 | void MediaSessionDescriptionFactory::GetCodecsForOffer( |
| 1838 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1839 | AudioCodecs* audio_codecs, |
| 1840 | VideoCodecs* video_codecs) const { |
| 1841 | // First - get all codecs from the current description if the media type |
| 1842 | // is used. Add them to `used_pltypes` so the payload type is not reused if a |
| 1843 | // new media type is added. |
| 1844 | UsedPayloadTypes used_pltypes; |
| 1845 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
| 1846 | video_codecs, &used_pltypes); |
| 1847 |
| 1848 | // Add our codecs that are not in the current description. |
| 1849 | MergeCodecs(all_audio_codecs_, audio_codecs, &used_pltypes); |
| 1850 | MergeCodecs(all_video_codecs_, video_codecs, &used_pltypes); |
| 1851 | } |
| 1852 |
| 1853 | // Getting codecs for an answer involves these steps: |
| 1854 | // |
| 1855 | // 1. Construct payload type -> codec mappings for current description. |
| 1856 | // 2. Add any codecs from the offer that weren't already present. |
| 1857 | // 3. Add any remaining codecs that weren't already present. |
| 1858 | // 4. For each individual media description (m= section), filter codecs based |
| 1859 | // on the directional attribute (happens in another method). |
| 1860 | void MediaSessionDescriptionFactory::GetCodecsForAnswer( |
| 1861 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1862 | const SessionDescription& remote_offer, |
| 1863 | AudioCodecs* audio_codecs, |
| 1864 | VideoCodecs* video_codecs) const { |
| 1865 | // First - get all codecs from the current description if the media type |
| 1866 | // is used. Add them to `used_pltypes` so the payload type is not reused if a |
| 1867 | // new media type is added. |
| 1868 | UsedPayloadTypes used_pltypes; |
| 1869 | MergeCodecsFromDescription(current_active_contents, audio_codecs, |
| 1870 | video_codecs, &used_pltypes); |
| 1871 |
| 1872 | // Second - filter out codecs that we don't support at all and should ignore. |
| 1873 | AudioCodecs filtered_offered_audio_codecs; |
| 1874 | VideoCodecs filtered_offered_video_codecs; |
| 1875 | for (const ContentInfo& content : remote_offer.contents()) { |
| 1876 | if (IsMediaContentOfType(&content, MEDIA_TYPE_AUDIO)) { |
| 1877 | std::vector<Codec> offered_codecs = content.media_description()->codecs(); |
| 1878 | for (const Codec& offered_audio_codec : offered_codecs) { |
| 1879 | if (!FindMatchingCodec(offered_codecs, filtered_offered_audio_codecs, |
| 1880 | offered_audio_codec) && |
| 1881 | FindMatchingCodec(offered_codecs, all_audio_codecs_, |
| 1882 | offered_audio_codec)) { |
| 1883 | filtered_offered_audio_codecs.push_back(offered_audio_codec); |
| 1884 | } |
| 1885 | } |
| 1886 | } else if (IsMediaContentOfType(&content, MEDIA_TYPE_VIDEO)) { |
| 1887 | std::vector<Codec> offered_codecs = content.media_description()->codecs(); |
| 1888 | for (const Codec& offered_video_codec : offered_codecs) { |
| 1889 | if (!FindMatchingCodec(offered_codecs, filtered_offered_video_codecs, |
| 1890 | offered_video_codec) && |
| 1891 | FindMatchingCodec(offered_codecs, all_video_codecs_, |
| 1892 | offered_video_codec)) { |
| 1893 | filtered_offered_video_codecs.push_back(offered_video_codec); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 |
| 1899 | // Add codecs that are not in the current description but were in |
| 1900 | // `remote_offer`. |
| 1901 | MergeCodecs(filtered_offered_audio_codecs, audio_codecs, &used_pltypes); |
| 1902 | MergeCodecs(filtered_offered_video_codecs, video_codecs, &used_pltypes); |
| 1903 | } |
| 1904 |
| 1905 | MediaSessionDescriptionFactory::AudioVideoRtpHeaderExtensions |
| 1906 | MediaSessionDescriptionFactory::GetOfferedRtpHeaderExtensionsWithIds( |
| 1907 | const std::vector<const ContentInfo*>& current_active_contents, |
| 1908 | bool extmap_allow_mixed, |
| 1909 | const std::vector<MediaDescriptionOptions>& media_description_options) |
| 1910 | const { |
| 1911 | // All header extensions allocated from the same range to avoid potential |
| 1912 | // issues when using BUNDLE. |
| 1913 |
| 1914 | // Strictly speaking the SDP attribute extmap_allow_mixed signals that the |
| 1915 | // receiver supports an RTP stream where one- and two-byte RTP header |
| 1916 | // extensions are mixed. For backwards compatibility reasons it's used in |
| 1917 | // WebRTC to signal that two-byte RTP header extensions are supported. |
| 1918 | UsedRtpHeaderExtensionIds used_ids( |
| 1919 | extmap_allow_mixed ? UsedRtpHeaderExtensionIds::IdDomain::kTwoByteAllowed |
| 1920 | : UsedRtpHeaderExtensionIds::IdDomain::kOneByteOnly); |
| 1921 | RtpHeaderExtensions all_regular_extensions; |
| 1922 | RtpHeaderExtensions all_encrypted_extensions; |
| 1923 |
| 1924 | AudioVideoRtpHeaderExtensions offered_extensions; |
| 1925 | // First - get all extensions from the current description if the media type |
| 1926 | // is used. |
| 1927 | // Add them to `used_ids` so the local ids are not reused if a new media |
| 1928 | // type is added. |
| 1929 | for (const ContentInfo* content : current_active_contents) { |
| 1930 | if (IsMediaContentOfType(content, MEDIA_TYPE_AUDIO)) { |
| 1931 | MergeRtpHdrExts(content->media_description()->rtp_header_extensions(), |
| 1932 | &offered_extensions.audio, &all_regular_extensions, |
| 1933 | &all_encrypted_extensions, &used_ids); |
| 1934 | } else if (IsMediaContentOfType(content, MEDIA_TYPE_VIDEO)) { |
| 1935 | MergeRtpHdrExts(content->media_description()->rtp_header_extensions(), |
| 1936 | &offered_extensions.video, &all_regular_extensions, |
| 1937 | &all_encrypted_extensions, &used_ids); |
| 1938 | } |
| 1939 | } |
| 1940 |
| 1941 | // Add all encountered header extensions in the media description options that |
| 1942 | // are not in the current description. |
| 1943 |
| 1944 | for (const auto& entry : media_description_options) { |
| 1945 | RtpHeaderExtensions filtered_extensions = |
| 1946 | filtered_rtp_header_extensions(UnstoppedOrPresentRtpHeaderExtensions( |
| 1947 | entry.header_extensions, all_regular_extensions, |
| 1948 | all_encrypted_extensions)); |
| 1949 | if (entry.type == MEDIA_TYPE_AUDIO) |
| 1950 | MergeRtpHdrExts(filtered_extensions, &offered_extensions.audio, |
| 1951 | &all_regular_extensions, &all_encrypted_extensions, |
| 1952 | &used_ids); |
| 1953 | else if (entry.type == MEDIA_TYPE_VIDEO) |
| 1954 | MergeRtpHdrExts(filtered_extensions, &offered_extensions.video, |
| 1955 | &all_regular_extensions, &all_encrypted_extensions, |
| 1956 | &used_ids); |
| 1957 | } |
| 1958 | // TODO(jbauch): Support adding encrypted header extensions to existing |
| 1959 | // sessions. |
| 1960 | if (enable_encrypted_rtp_header_extensions_ && |
| 1961 | current_active_contents.empty()) { |
| 1962 | AddEncryptedVersionsOfHdrExts(&offered_extensions.audio, |
| 1963 | &all_encrypted_extensions, &used_ids); |
| 1964 | AddEncryptedVersionsOfHdrExts(&offered_extensions.video, |
| 1965 | &all_encrypted_extensions, &used_ids); |
| 1966 | } |
| 1967 | return offered_extensions; |
| 1968 | } |
| 1969 |
| 1970 | RTCError MediaSessionDescriptionFactory::AddTransportOffer( |
| 1971 | const std::string& content_name, |
| 1972 | const TransportOptions& transport_options, |
| 1973 | const SessionDescription* current_desc, |
| 1974 | SessionDescription* offer_desc, |
| 1975 | IceCredentialsIterator* ice_credentials) const { |
| 1976 | const TransportDescription* current_tdesc = |
| 1977 | GetTransportDescription(content_name, current_desc); |
| 1978 | std::unique_ptr<TransportDescription> new_tdesc( |
| 1979 | transport_desc_factory_->CreateOffer(transport_options, current_tdesc, |
| 1980 | ice_credentials)); |
| 1981 | if (!new_tdesc) { |
| 1982 | RTC_LOG(LS_ERROR) << "Failed to AddTransportOffer, content name=" |
| 1983 | << content_name; |
| 1984 | } |
| 1985 | offer_desc->AddTransportInfo(TransportInfo(content_name, *new_tdesc)); |
| 1986 | return RTCError::OK(); |
| 1987 | } |
| 1988 |
| 1989 | std::unique_ptr<TransportDescription> |
| 1990 | MediaSessionDescriptionFactory::CreateTransportAnswer( |
| 1991 | const std::string& content_name, |
| 1992 | const SessionDescription* offer_desc, |
| 1993 | const TransportOptions& transport_options, |
| 1994 | const SessionDescription* current_desc, |
| 1995 | bool require_transport_attributes, |
| 1996 | IceCredentialsIterator* ice_credentials) const { |
| 1997 | const TransportDescription* offer_tdesc = |
| 1998 | GetTransportDescription(content_name, offer_desc); |
| 1999 | const TransportDescription* current_tdesc = |
| 2000 | GetTransportDescription(content_name, current_desc); |
| 2001 | return transport_desc_factory_->CreateAnswer(offer_tdesc, transport_options, |
| 2002 | require_transport_attributes, |
| 2003 | current_tdesc, ice_credentials); |
| 2004 | } |
| 2005 |
| 2006 | RTCError MediaSessionDescriptionFactory::AddTransportAnswer( |
| 2007 | const std::string& content_name, |
| 2008 | const TransportDescription& transport_desc, |
| 2009 | SessionDescription* answer_desc) const { |
| 2010 | answer_desc->AddTransportInfo(TransportInfo(content_name, transport_desc)); |
| 2011 | return RTCError::OK(); |
| 2012 | } |
| 2013 |
| 2014 | // `codecs` = set of all possible codecs that can be used, with correct |
| 2015 | // payload type mappings |
| 2016 | // |
| 2017 | // `supported_codecs` = set of codecs that are supported for the direction |
| 2018 | // of this m= section |
| 2019 | // `current_content` = current description, may be null. |
| 2020 | // current_content->codecs() = set of previously negotiated codecs for this m= |
| 2021 | // section |
| 2022 | // |
| 2023 | // The payload types should come from codecs, but the order should come |
| 2024 | // from current_content->codecs() and then supported_codecs, to ensure that |
| 2025 | // re-offers don't change existing codec priority, and that new codecs are added |
| 2026 | // with the right priority. |
| 2027 | RTCError MediaSessionDescriptionFactory::AddRtpContentForOffer( |
| 2028 | const MediaDescriptionOptions& media_description_options, |
| 2029 | const MediaSessionOptions& session_options, |
| 2030 | const ContentInfo* current_content, |
| 2031 | const SessionDescription* current_description, |
| 2032 | const RtpHeaderExtensions& header_extensions, |
| 2033 | const std::vector<Codec>& codecs, |
| 2034 | StreamParamsVec* current_streams, |
| 2035 | SessionDescription* session_description, |
| 2036 | IceCredentialsIterator* ice_credentials) const { |
| 2037 | RTC_DCHECK(media_description_options.type == MEDIA_TYPE_AUDIO || |
| 2038 | media_description_options.type == MEDIA_TYPE_VIDEO); |
| 2039 |
| 2040 | const std::vector<Codec>& supported_codecs = |
| 2041 | media_description_options.type == MEDIA_TYPE_AUDIO |
| 2042 | ? GetAudioCodecsForOffer(media_description_options.direction) |
| 2043 | : GetVideoCodecsForOffer(media_description_options.direction); |
| 2044 | webrtc::RTCErrorOr<std::vector<Codec>> error_or_filtered_codecs = |
| 2045 | GetNegotiatedCodecsForOffer(media_description_options, session_options, |
| 2046 | current_content, codecs, supported_codecs); |
| 2047 | if (!error_or_filtered_codecs.ok()) { |
| 2048 | return error_or_filtered_codecs.MoveError(); |
| 2049 | } |
| 2050 |
| 2051 | std::unique_ptr<MediaContentDescription> content_description; |
| 2052 | if (media_description_options.type == MEDIA_TYPE_AUDIO) { |
| 2053 | content_description = std::make_unique<AudioContentDescription>(); |
| 2054 | } else { |
| 2055 | content_description = std::make_unique<VideoContentDescription>(); |
| 2056 | } |
| 2057 |
| 2058 | auto error = CreateMediaContentOffer( |
| 2059 | media_description_options, session_options, |
| 2060 | error_or_filtered_codecs.MoveValue(), header_extensions, ssrc_generator(), |
| 2061 | current_streams, content_description.get(), |
| 2062 | transport_desc_factory_->trials()); |
| 2063 | if (!error.ok()) { |
| 2064 | return error; |
| 2065 | } |
| 2066 |
| 2067 | // Insecure transport should only occur in testing. |
| 2068 | bool secure_transport = !(transport_desc_factory_->insecure()); |
| 2069 | SetMediaProtocol(secure_transport, content_description.get()); |
| 2070 |
| 2071 | content_description->set_direction(media_description_options.direction); |
| 2072 |
| 2073 | session_description->AddContent( |
| 2074 | media_description_options.mid, MediaProtocolType::kRtp, |
| 2075 | media_description_options.stopped, std::move(content_description)); |
| 2076 | return AddTransportOffer(media_description_options.mid, |
| 2077 | media_description_options.transport_options, |
| 2078 | current_description, session_description, |
| 2079 | ice_credentials); |
| 2080 | } |
| 2081 |
| 2082 | RTCError MediaSessionDescriptionFactory::AddDataContentForOffer( |
| 2083 | const MediaDescriptionOptions& media_description_options, |
| 2084 | const MediaSessionOptions& session_options, |
| 2085 | const ContentInfo* current_content, |
| 2086 | const SessionDescription* current_description, |
| 2087 | StreamParamsVec* current_streams, |
| 2088 | SessionDescription* desc, |
| 2089 | IceCredentialsIterator* ice_credentials) const { |
| 2090 | auto data = std::make_unique<SctpDataContentDescription>(); |
| 2091 |
| 2092 | bool secure_transport = true; |
| 2093 |
| 2094 | std::vector<std::string> crypto_suites; |
| 2095 | // Unlike SetMediaProtocol below, we need to set the protocol |
| 2096 | // before we call CreateMediaContentOffer. Otherwise, |
| 2097 | // CreateMediaContentOffer won't know this is SCTP and will |
| 2098 | // generate SSRCs rather than SIDs. |
| 2099 | data->set_protocol(secure_transport ? kMediaProtocolUdpDtlsSctp |
| 2100 | : kMediaProtocolSctp); |
| 2101 | data->set_use_sctpmap(session_options.use_obsolete_sctp_sdp); |
| 2102 | data->set_max_message_size(kSctpSendBufferSize); |
| 2103 |
| 2104 | auto error = CreateContentOffer(media_description_options, session_options, |
| 2105 | RtpHeaderExtensions(), ssrc_generator(), |
| 2106 | current_streams, data.get()); |
| 2107 | if (!error.ok()) { |
| 2108 | return error; |
| 2109 | } |
| 2110 |
| 2111 | desc->AddContent(media_description_options.mid, MediaProtocolType::kSctp, |
| 2112 | media_description_options.stopped, std::move(data)); |
| 2113 | return AddTransportOffer(media_description_options.mid, |
| 2114 | media_description_options.transport_options, |
| 2115 | current_description, desc, ice_credentials); |
| 2116 | } |
| 2117 |
| 2118 | RTCError MediaSessionDescriptionFactory::AddUnsupportedContentForOffer( |
| 2119 | const MediaDescriptionOptions& media_description_options, |
| 2120 | const MediaSessionOptions& session_options, |
| 2121 | const ContentInfo* current_content, |
| 2122 | const SessionDescription* current_description, |
| 2123 | SessionDescription* desc, |
| 2124 | IceCredentialsIterator* ice_credentials) const { |
| 2125 | RTC_CHECK(IsMediaContentOfType(current_content, MEDIA_TYPE_UNSUPPORTED)); |
| 2126 |
| 2127 | const UnsupportedContentDescription* current_unsupported_description = |
| 2128 | current_content->media_description()->as_unsupported(); |
| 2129 | auto unsupported = std::make_unique<UnsupportedContentDescription>( |
| 2130 | current_unsupported_description->media_type()); |
| 2131 | unsupported->set_protocol(current_content->media_description()->protocol()); |
| 2132 | desc->AddContent(media_description_options.mid, MediaProtocolType::kOther, |
| 2133 | /*rejected=*/true, std::move(unsupported)); |
| 2134 |
| 2135 | return AddTransportOffer(media_description_options.mid, |
| 2136 | media_description_options.transport_options, |
| 2137 | current_description, desc, ice_credentials); |
| 2138 | } |
| 2139 |
| 2140 | // `codecs` = set of all possible codecs that can be used, with correct |
| 2141 | // payload type mappings |
| 2142 | // |
| 2143 | // `supported_codecs` = set of codecs that are supported for the direction |
| 2144 | // of this m= section |
| 2145 | // |
| 2146 | // mcd->codecs() = set of previously negotiated codecs for this m= section |
| 2147 | // |
| 2148 | // The payload types should come from codecs, but the order should come |
| 2149 | // from mcd->codecs() and then supported_codecs, to ensure that re-offers don't |
| 2150 | // change existing codec priority, and that new codecs are added with the right |
| 2151 | // priority. |
| 2152 | RTCError MediaSessionDescriptionFactory::AddRtpContentForAnswer( |
| 2153 | const MediaDescriptionOptions& media_description_options, |
| 2154 | const MediaSessionOptions& session_options, |
| 2155 | const ContentInfo* offer_content, |
| 2156 | const SessionDescription* offer_description, |
| 2157 | const ContentInfo* current_content, |
| 2158 | const SessionDescription* current_description, |
| 2159 | const TransportInfo* bundle_transport, |
| 2160 | const std::vector<Codec>& codecs, |
| 2161 | const RtpHeaderExtensions& header_extensions, |
| 2162 | StreamParamsVec* current_streams, |
| 2163 | SessionDescription* answer, |
| 2164 | IceCredentialsIterator* ice_credentials) const { |
| 2165 | RTC_DCHECK(media_description_options.type == MEDIA_TYPE_AUDIO || |
| 2166 | media_description_options.type == MEDIA_TYPE_VIDEO); |
| 2167 | RTC_CHECK( |
| 2168 | IsMediaContentOfType(offer_content, media_description_options.type)); |
| 2169 | const RtpMediaContentDescription* offer_content_description; |
| 2170 | if (media_description_options.type == MEDIA_TYPE_AUDIO) { |
| 2171 | offer_content_description = offer_content->media_description()->as_audio(); |
| 2172 | } else { |
| 2173 | offer_content_description = offer_content->media_description()->as_video(); |
| 2174 | } |
| 2175 | // If this section is part of a bundle, bundle_transport is non-null. |
| 2176 | // Then require_transport_attributes is false - we can handle sections |
| 2177 | // without the DTLS parameters. For rejected m-lines it does not matter. |
| 2178 | // Otherwise, transport attributes MUST be present. |
| 2179 | std::unique_ptr<TransportDescription> transport = CreateTransportAnswer( |
| 2180 | media_description_options.mid, offer_description, |
| 2181 | media_description_options.transport_options, current_description, |
| 2182 | !offer_content->rejected && bundle_transport == nullptr, ice_credentials); |
| 2183 | if (!transport) { |
| 2184 | LOG_AND_RETURN_ERROR( |
| 2185 | RTCErrorType::INTERNAL_ERROR, |
| 2186 | "Failed to create transport answer, transport is missing"); |
| 2187 | } |
| 2188 |
| 2189 | // Pick codecs based on the requested communications direction in the offer |
| 2190 | // and the selected direction in the answer. |
| 2191 | // Note these will be filtered one final time in CreateMediaContentAnswer. |
| 2192 | auto wants_rtd = media_description_options.direction; |
| 2193 | auto offer_rtd = offer_content_description->direction(); |
| 2194 | auto answer_rtd = NegotiateRtpTransceiverDirection(offer_rtd, wants_rtd); |
| 2195 |
| 2196 | const std::vector<Codec>& supported_codecs = |
| 2197 | media_description_options.type == MEDIA_TYPE_AUDIO |
| 2198 | ? GetAudioCodecsForAnswer(offer_rtd, answer_rtd) |
| 2199 | : GetVideoCodecsForAnswer(offer_rtd, answer_rtd); |
| 2200 | webrtc::RTCErrorOr<std::vector<Codec>> error_or_filtered_codecs = |
| 2201 | GetNegotiatedCodecsForAnswer(media_description_options, session_options, |
| 2202 | current_content, codecs, supported_codecs); |
| 2203 | if (!error_or_filtered_codecs.ok()) { |
| 2204 | return error_or_filtered_codecs.MoveError(); |
| 2205 | } |
| 2206 | auto filtered_codecs = error_or_filtered_codecs.MoveValue(); |
| 2207 |
| 2208 | // Determine if we have media codecs in common. |
| 2209 | bool has_common_media_codecs = |
| 2210 | std::find_if(filtered_codecs.begin(), filtered_codecs.end(), |
| 2211 | [](const Codec& c) { |
| 2212 | return c.IsMediaCodec() && !IsComfortNoiseCodec(c); |
| 2213 | }) != filtered_codecs.end(); |
| 2214 |
| 2215 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2216 | session_options.bundle_enabled; |
| 2217 | std::unique_ptr<MediaContentDescription> answer_content; |
| 2218 | if (media_description_options.type == MEDIA_TYPE_AUDIO) { |
| 2219 | answer_content = std::make_unique<AudioContentDescription>(); |
| 2220 | } else { |
| 2221 | answer_content = std::make_unique<VideoContentDescription>(); |
| 2222 | } |
| 2223 | if (!SetCodecsInAnswer( |
| 2224 | offer_content_description, filtered_codecs, media_description_options, |
| 2225 | session_options, ssrc_generator(), current_streams, |
| 2226 | answer_content.get(), transport_desc_factory_->trials())) { |
| 2227 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2228 | "Failed to set codecs in answer"); |
| 2229 | } |
| 2230 | if (!CreateMediaContentAnswer( |
| 2231 | offer_content_description, media_description_options, session_options, |
| 2232 | filtered_rtp_header_extensions(header_extensions), ssrc_generator(), |
| 2233 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2234 | bundle_enabled, answer_content.get())) { |
| 2235 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2236 | "Failed to create answer"); |
| 2237 | } |
| 2238 |
| 2239 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2240 | : transport->secure(); |
| 2241 | bool rejected = media_description_options.stopped || |
| 2242 | offer_content->rejected || !has_common_media_codecs || |
| 2243 | !IsMediaProtocolSupported(MEDIA_TYPE_AUDIO, |
| 2244 | answer_content->protocol(), secure); |
| 2245 | if (rejected) { |
| 2246 | RTC_LOG(LS_INFO) << "m= section '" << media_description_options.mid |
| 2247 | << "' being rejected in answer."; |
| 2248 | } |
| 2249 |
| 2250 | auto error = AddTransportAnswer(media_description_options.mid, |
| 2251 | *(transport.get()), answer); |
| 2252 | if (!error.ok()) { |
| 2253 | return error; |
| 2254 | } |
| 2255 |
| 2256 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2257 | rejected, std::move(answer_content)); |
| 2258 | return RTCError::OK(); |
| 2259 | } |
| 2260 |
| 2261 | RTCError MediaSessionDescriptionFactory::AddDataContentForAnswer( |
| 2262 | const MediaDescriptionOptions& media_description_options, |
| 2263 | const MediaSessionOptions& session_options, |
| 2264 | const ContentInfo* offer_content, |
| 2265 | const SessionDescription* offer_description, |
| 2266 | const ContentInfo* current_content, |
| 2267 | const SessionDescription* current_description, |
| 2268 | const TransportInfo* bundle_transport, |
| 2269 | StreamParamsVec* current_streams, |
| 2270 | SessionDescription* answer, |
| 2271 | IceCredentialsIterator* ice_credentials) const { |
| 2272 | std::unique_ptr<TransportDescription> data_transport = CreateTransportAnswer( |
| 2273 | media_description_options.mid, offer_description, |
| 2274 | media_description_options.transport_options, current_description, |
| 2275 | !offer_content->rejected && bundle_transport == nullptr, ice_credentials); |
| 2276 | if (!data_transport) { |
| 2277 | LOG_AND_RETURN_ERROR( |
| 2278 | RTCErrorType::INTERNAL_ERROR, |
| 2279 | "Failed to create transport answer, data transport is missing"); |
| 2280 | } |
| 2281 |
| 2282 | bool bundle_enabled = offer_description->HasGroup(GROUP_TYPE_BUNDLE) && |
| 2283 | session_options.bundle_enabled; |
| 2284 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_DATA)); |
| 2285 | std::unique_ptr<MediaContentDescription> data_answer; |
| 2286 | if (offer_content->media_description()->as_sctp()) { |
| 2287 | // SCTP data content |
| 2288 | data_answer = std::make_unique<SctpDataContentDescription>(); |
| 2289 | const SctpDataContentDescription* offer_data_description = |
| 2290 | offer_content->media_description()->as_sctp(); |
| 2291 | // Respond with the offerer's proto, whatever it is. |
| 2292 | data_answer->as_sctp()->set_protocol(offer_data_description->protocol()); |
| 2293 | // Respond with our max message size or the remote max messsage size, |
| 2294 | // whichever is smaller. |
| 2295 | // 0 is treated specially - it means "I can accept any size". Since |
| 2296 | // we do not implement infinite size messages, reply with |
| 2297 | // kSctpSendBufferSize. |
| 2298 | if (offer_data_description->max_message_size() == 0) { |
| 2299 | data_answer->as_sctp()->set_max_message_size(kSctpSendBufferSize); |
| 2300 | } else { |
| 2301 | data_answer->as_sctp()->set_max_message_size(std::min( |
| 2302 | offer_data_description->max_message_size(), kSctpSendBufferSize)); |
| 2303 | } |
| 2304 | if (!CreateMediaContentAnswer( |
| 2305 | offer_data_description, media_description_options, session_options, |
| 2306 | RtpHeaderExtensions(), ssrc_generator(), |
| 2307 | enable_encrypted_rtp_header_extensions_, current_streams, |
| 2308 | bundle_enabled, data_answer.get())) { |
| 2309 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2310 | "Failed to create answer"); |
| 2311 | } |
| 2312 | // Respond with sctpmap if the offer uses sctpmap. |
| 2313 | bool offer_uses_sctpmap = offer_data_description->use_sctpmap(); |
| 2314 | data_answer->as_sctp()->set_use_sctpmap(offer_uses_sctpmap); |
| 2315 | } else { |
| 2316 | RTC_DCHECK_NOTREACHED() << "Non-SCTP data content found"; |
| 2317 | } |
| 2318 |
| 2319 | bool secure = bundle_transport ? bundle_transport->description.secure() |
| 2320 | : data_transport->secure(); |
| 2321 |
| 2322 | bool rejected = media_description_options.stopped || |
| 2323 | offer_content->rejected || |
| 2324 | !IsMediaProtocolSupported(MEDIA_TYPE_DATA, |
| 2325 | data_answer->protocol(), secure); |
| 2326 | auto error = AddTransportAnswer(media_description_options.mid, |
| 2327 | *(data_transport.get()), answer); |
| 2328 | if (!error.ok()) { |
| 2329 | return error; |
| 2330 | } |
| 2331 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2332 | rejected, std::move(data_answer)); |
| 2333 | return RTCError::OK(); |
| 2334 | } |
| 2335 |
| 2336 | RTCError MediaSessionDescriptionFactory::AddUnsupportedContentForAnswer( |
| 2337 | const MediaDescriptionOptions& media_description_options, |
| 2338 | const MediaSessionOptions& session_options, |
| 2339 | const ContentInfo* offer_content, |
| 2340 | const SessionDescription* offer_description, |
| 2341 | const ContentInfo* current_content, |
| 2342 | const SessionDescription* current_description, |
| 2343 | const TransportInfo* bundle_transport, |
| 2344 | SessionDescription* answer, |
| 2345 | IceCredentialsIterator* ice_credentials) const { |
| 2346 | std::unique_ptr<TransportDescription> unsupported_transport = |
| 2347 | CreateTransportAnswer( |
| 2348 | media_description_options.mid, offer_description, |
| 2349 | media_description_options.transport_options, current_description, |
| 2350 | !offer_content->rejected && bundle_transport == nullptr, |
| 2351 | ice_credentials); |
| 2352 | if (!unsupported_transport) { |
| 2353 | LOG_AND_RETURN_ERROR( |
| 2354 | RTCErrorType::INTERNAL_ERROR, |
| 2355 | "Failed to create transport answer, unsupported transport is missing"); |
| 2356 | } |
| 2357 | RTC_CHECK(IsMediaContentOfType(offer_content, MEDIA_TYPE_UNSUPPORTED)); |
| 2358 |
| 2359 | const UnsupportedContentDescription* offer_unsupported_description = |
| 2360 | offer_content->media_description()->as_unsupported(); |
| 2361 | std::unique_ptr<MediaContentDescription> unsupported_answer = |
| 2362 | std::make_unique<UnsupportedContentDescription>( |
| 2363 | offer_unsupported_description->media_type()); |
| 2364 | unsupported_answer->set_protocol(offer_unsupported_description->protocol()); |
| 2365 |
| 2366 | auto error = AddTransportAnswer(media_description_options.mid, |
| 2367 | *(unsupported_transport.get()), answer); |
| 2368 | if (!error.ok()) { |
| 2369 | return error; |
| 2370 | } |
| 2371 |
| 2372 | answer->AddContent(media_description_options.mid, offer_content->type, |
| 2373 | /*rejected=*/true, std::move(unsupported_answer)); |
| 2374 | return RTCError::OK(); |
| 2375 | } |
| 2376 |
| 2377 | void MediaSessionDescriptionFactory::ComputeAudioCodecsIntersectionAndUnion() { |
| 2378 | audio_sendrecv_codecs_.clear(); |
| 2379 | all_audio_codecs_.clear(); |
| 2380 | // Compute the audio codecs union. |
| 2381 | for (const Codec& send : audio_send_codecs_) { |
| 2382 | all_audio_codecs_.push_back(send); |
| 2383 | if (!FindMatchingCodec(audio_send_codecs_, audio_recv_codecs_, send)) { |
| 2384 | // It doesn't make sense to have an RTX codec we support sending but not |
| 2385 | // receiving. |
| 2386 | RTC_DCHECK(send.GetResiliencyType() != Codec::ResiliencyType::kRtx); |
| 2387 | } |
| 2388 | } |
| 2389 | for (const Codec& recv : audio_recv_codecs_) { |
| 2390 | if (!FindMatchingCodec(audio_recv_codecs_, audio_send_codecs_, recv)) { |
| 2391 | all_audio_codecs_.push_back(recv); |
| 2392 | } |
| 2393 | } |
| 2394 | // Use NegotiateCodecs to merge our codec lists, since the operation is |
| 2395 | // essentially the same. Put send_codecs as the offered_codecs, which is the |
| 2396 | // order we'd like to follow. The reasoning is that encoding is usually more |
| 2397 | // expensive than decoding, and prioritizing a codec in the send list probably |
| 2398 | // means it's a codec we can handle efficiently. |
| 2399 | NegotiateCodecs(audio_recv_codecs_, audio_send_codecs_, |
| 2400 | &audio_sendrecv_codecs_, true); |
| 2401 | } |
| 2402 |
| 2403 | void MediaSessionDescriptionFactory::ComputeVideoCodecsIntersectionAndUnion() { |
| 2404 | video_sendrecv_codecs_.clear(); |
| 2405 |
| 2406 | // Use ComputeCodecsUnion to avoid having duplicate payload IDs |
| 2407 | all_video_codecs_ = |
| 2408 | ComputeCodecsUnion(video_recv_codecs_, video_send_codecs_); |
| 2409 |
| 2410 | // Use NegotiateCodecs to merge our codec lists, since the operation is |
| 2411 | // essentially the same. Put send_codecs as the offered_codecs, which is the |
| 2412 | // order we'd like to follow. The reasoning is that encoding is usually more |
| 2413 | // expensive than decoding, and prioritizing a codec in the send list probably |
| 2414 | // means it's a codec we can handle efficiently. |
| 2415 | NegotiateCodecs(video_recv_codecs_, video_send_codecs_, |
| 2416 | &video_sendrecv_codecs_, true); |
| 2417 | } |
| 2418 |
| 2419 | bool IsMediaContent(const ContentInfo* content) { |
| 2420 | return (content && (content->type == MediaProtocolType::kRtp || |
| 2421 | content->type == MediaProtocolType::kSctp)); |
| 2422 | } |
| 2423 |
| 2424 | bool IsAudioContent(const ContentInfo* content) { |
| 2425 | return IsMediaContentOfType(content, MEDIA_TYPE_AUDIO); |
| 2426 | } |
| 2427 |
| 2428 | bool IsVideoContent(const ContentInfo* content) { |
| 2429 | return IsMediaContentOfType(content, MEDIA_TYPE_VIDEO); |
| 2430 | } |
| 2431 |
| 2432 | bool IsDataContent(const ContentInfo* content) { |
| 2433 | return IsMediaContentOfType(content, MEDIA_TYPE_DATA); |
| 2434 | } |
| 2435 |
| 2436 | bool IsUnsupportedContent(const ContentInfo* content) { |
| 2437 | return IsMediaContentOfType(content, MEDIA_TYPE_UNSUPPORTED); |
| 2438 | } |
| 2439 |
| 2440 | const ContentInfo* GetFirstMediaContent(const ContentInfos& contents, |
| 2441 | MediaType media_type) { |
| 2442 | for (const ContentInfo& content : contents) { |
| 2443 | if (IsMediaContentOfType(&content, media_type)) { |
| 2444 | return &content; |
| 2445 | } |
| 2446 | } |
| 2447 | return nullptr; |
| 2448 | } |
| 2449 |
| 2450 | const ContentInfo* GetFirstAudioContent(const ContentInfos& contents) { |
| 2451 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2452 | } |
| 2453 |
| 2454 | const ContentInfo* GetFirstVideoContent(const ContentInfos& contents) { |
| 2455 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2456 | } |
| 2457 |
| 2458 | const ContentInfo* GetFirstDataContent(const ContentInfos& contents) { |
| 2459 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2460 | } |
| 2461 |
| 2462 | const ContentInfo* GetFirstMediaContent(const SessionDescription* sdesc, |
| 2463 | MediaType media_type) { |
| 2464 | if (sdesc == nullptr) { |
| 2465 | return nullptr; |
| 2466 | } |
| 2467 |
| 2468 | return GetFirstMediaContent(sdesc->contents(), media_type); |
| 2469 | } |
| 2470 |
| 2471 | const ContentInfo* GetFirstAudioContent(const SessionDescription* sdesc) { |
| 2472 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2473 | } |
| 2474 |
| 2475 | const ContentInfo* GetFirstVideoContent(const SessionDescription* sdesc) { |
| 2476 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2477 | } |
| 2478 |
| 2479 | const ContentInfo* GetFirstDataContent(const SessionDescription* sdesc) { |
| 2480 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2481 | } |
| 2482 |
| 2483 | const MediaContentDescription* GetFirstMediaContentDescription( |
| 2484 | const SessionDescription* sdesc, |
| 2485 | MediaType media_type) { |
| 2486 | const ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
| 2487 | return (content ? content->media_description() : nullptr); |
| 2488 | } |
| 2489 |
| 2490 | const AudioContentDescription* GetFirstAudioContentDescription( |
| 2491 | const SessionDescription* sdesc) { |
| 2492 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO); |
| 2493 | return desc ? desc->as_audio() : nullptr; |
| 2494 | } |
| 2495 |
| 2496 | const VideoContentDescription* GetFirstVideoContentDescription( |
| 2497 | const SessionDescription* sdesc) { |
| 2498 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO); |
| 2499 | return desc ? desc->as_video() : nullptr; |
| 2500 | } |
| 2501 |
| 2502 | const SctpDataContentDescription* GetFirstSctpDataContentDescription( |
| 2503 | const SessionDescription* sdesc) { |
| 2504 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA); |
| 2505 | return desc ? desc->as_sctp() : nullptr; |
| 2506 | } |
| 2507 |
| 2508 | // |
| 2509 | // Non-const versions of the above functions. |
| 2510 | // |
| 2511 |
| 2512 | ContentInfo* GetFirstMediaContent(ContentInfos* contents, |
| 2513 | MediaType media_type) { |
| 2514 | for (ContentInfo& content : *contents) { |
| 2515 | if (IsMediaContentOfType(&content, media_type)) { |
| 2516 | return &content; |
| 2517 | } |
| 2518 | } |
| 2519 | return nullptr; |
| 2520 | } |
| 2521 |
| 2522 | ContentInfo* GetFirstAudioContent(ContentInfos* contents) { |
| 2523 | return GetFirstMediaContent(contents, MEDIA_TYPE_AUDIO); |
| 2524 | } |
| 2525 |
| 2526 | ContentInfo* GetFirstVideoContent(ContentInfos* contents) { |
| 2527 | return GetFirstMediaContent(contents, MEDIA_TYPE_VIDEO); |
| 2528 | } |
| 2529 |
| 2530 | ContentInfo* GetFirstDataContent(ContentInfos* contents) { |
| 2531 | return GetFirstMediaContent(contents, MEDIA_TYPE_DATA); |
| 2532 | } |
| 2533 |
| 2534 | ContentInfo* GetFirstMediaContent(SessionDescription* sdesc, |
| 2535 | MediaType media_type) { |
| 2536 | if (sdesc == nullptr) { |
| 2537 | return nullptr; |
| 2538 | } |
| 2539 |
| 2540 | return GetFirstMediaContent(&sdesc->contents(), media_type); |
| 2541 | } |
| 2542 |
| 2543 | ContentInfo* GetFirstAudioContent(SessionDescription* sdesc) { |
| 2544 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_AUDIO); |
| 2545 | } |
| 2546 |
| 2547 | ContentInfo* GetFirstVideoContent(SessionDescription* sdesc) { |
| 2548 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_VIDEO); |
| 2549 | } |
| 2550 |
| 2551 | ContentInfo* GetFirstDataContent(SessionDescription* sdesc) { |
| 2552 | return GetFirstMediaContent(sdesc, MEDIA_TYPE_DATA); |
| 2553 | } |
| 2554 |
| 2555 | MediaContentDescription* GetFirstMediaContentDescription( |
| 2556 | SessionDescription* sdesc, |
| 2557 | MediaType media_type) { |
| 2558 | ContentInfo* content = GetFirstMediaContent(sdesc, media_type); |
| 2559 | return (content ? content->media_description() : nullptr); |
| 2560 | } |
| 2561 |
| 2562 | AudioContentDescription* GetFirstAudioContentDescription( |
| 2563 | SessionDescription* sdesc) { |
| 2564 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_AUDIO); |
| 2565 | return desc ? desc->as_audio() : nullptr; |
| 2566 | } |
| 2567 |
| 2568 | VideoContentDescription* GetFirstVideoContentDescription( |
| 2569 | SessionDescription* sdesc) { |
| 2570 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_VIDEO); |
| 2571 | return desc ? desc->as_video() : nullptr; |
| 2572 | } |
| 2573 |
| 2574 | SctpDataContentDescription* GetFirstSctpDataContentDescription( |
| 2575 | SessionDescription* sdesc) { |
| 2576 | auto desc = GetFirstMediaContentDescription(sdesc, MEDIA_TYPE_DATA); |
| 2577 | return desc ? desc->as_sctp() : nullptr; |
| 2578 | } |
| 2579 |
| 2580 | } // namespace cricket |
| 2581 |