Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "pc/sdp_offer_answer.h" |
| 12 | |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 14 | #include <iterator> |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 15 | #include <map> |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 16 | #include <queue> |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 17 | #include <type_traits> |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 18 | #include <utility> |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 19 | |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 21 | #include "absl/memory/memory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 22 | #include "absl/strings/string_view.h" |
| 23 | #include "api/array_view.h" |
| 24 | #include "api/crypto/crypto_options.h" |
| 25 | #include "api/data_channel_interface.h" |
| 26 | #include "api/dtls_transport_interface.h" |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 27 | #include "api/media_stream_proxy.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 28 | #include "api/rtp_parameters.h" |
| 29 | #include "api/rtp_receiver_interface.h" |
| 30 | #include "api/rtp_sender_interface.h" |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 31 | #include "api/uma_metrics.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 32 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 33 | #include "media/base/codec.h" |
| 34 | #include "media/base/media_engine.h" |
| 35 | #include "media/base/rid_description.h" |
| 36 | #include "p2p/base/p2p_constants.h" |
| 37 | #include "p2p/base/p2p_transport_channel.h" |
| 38 | #include "p2p/base/port.h" |
| 39 | #include "p2p/base/transport_description.h" |
| 40 | #include "p2p/base/transport_description_factory.h" |
| 41 | #include "p2p/base/transport_info.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 42 | #include "pc/connection_context.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 43 | #include "pc/data_channel_utils.h" |
| 44 | #include "pc/media_protocol_names.h" |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 45 | #include "pc/media_stream.h" |
| 46 | #include "pc/peer_connection.h" |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 47 | #include "pc/peer_connection_message_handler.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 48 | #include "pc/rtp_data_channel.h" |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 49 | #include "pc/rtp_media_utils.h" |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 50 | #include "pc/rtp_sender.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 51 | #include "pc/rtp_transport_internal.h" |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 52 | #include "pc/sctp_transport.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 53 | #include "pc/simulcast_description.h" |
Harald Alvestrand | 3eaee6b | 2020-10-19 06:35:55 | [diff] [blame] | 54 | #include "pc/stats_collector.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 55 | #include "pc/usage_pattern.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 56 | #include "pc/webrtc_session_description_factory.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 57 | #include "rtc_base/bind.h" |
| 58 | #include "rtc_base/helpers.h" |
| 59 | #include "rtc_base/location.h" |
| 60 | #include "rtc_base/logging.h" |
| 61 | #include "rtc_base/ref_counted_object.h" |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 62 | #include "rtc_base/rtc_certificate.h" |
Harald Alvestrand | 1f7eab6 | 2020-10-18 16:51:47 | [diff] [blame] | 63 | #include "rtc_base/socket_address.h" |
| 64 | #include "rtc_base/ssl_stream_adapter.h" |
| 65 | #include "rtc_base/string_encode.h" |
| 66 | #include "rtc_base/strings/string_builder.h" |
| 67 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 68 | #include "rtc_base/trace_event.h" |
| 69 | #include "system_wrappers/include/metrics.h" |
| 70 | |
| 71 | using cricket::ContentInfo; |
| 72 | using cricket::ContentInfos; |
| 73 | using cricket::MediaContentDescription; |
| 74 | using cricket::MediaProtocolType; |
| 75 | using cricket::RidDescription; |
| 76 | using cricket::RidDirection; |
| 77 | using cricket::SessionDescription; |
| 78 | using cricket::SimulcastDescription; |
| 79 | using cricket::SimulcastLayer; |
| 80 | using cricket::SimulcastLayerList; |
| 81 | using cricket::StreamParams; |
| 82 | using cricket::TransportInfo; |
| 83 | |
| 84 | using cricket::LOCAL_PORT_TYPE; |
| 85 | using cricket::PRFLX_PORT_TYPE; |
| 86 | using cricket::RELAY_PORT_TYPE; |
| 87 | using cricket::STUN_PORT_TYPE; |
| 88 | |
| 89 | namespace webrtc { |
| 90 | |
| 91 | namespace { |
| 92 | |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 93 | typedef webrtc::PeerConnectionInterface::RTCOfferAnswerOptions |
| 94 | RTCOfferAnswerOptions; |
| 95 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 96 | // Error messages |
| 97 | const char kInvalidSdp[] = "Invalid session description."; |
| 98 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 99 | const char kBundleWithoutRtcpMux[] = |
| 100 | "rtcp-mux must be enabled when BUNDLE " |
| 101 | "is enabled."; |
| 102 | const char kMlineMismatchInAnswer[] = |
| 103 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 104 | "answer."; |
| 105 | const char kMlineMismatchInSubsequentOffer[] = |
| 106 | "The order of m-lines in subsequent offer doesn't match order from " |
| 107 | "previous offer/answer."; |
| 108 | const char kSdpWithoutIceUfragPwd[] = |
| 109 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 110 | const char kSdpWithoutDtlsFingerprint[] = |
| 111 | "Called with SDP without DTLS fingerprint."; |
| 112 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 113 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 114 | const char kSessionError[] = "Session error code: "; |
| 115 | const char kSessionErrorDesc[] = "Session error description: "; |
| 116 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 117 | // UMA metric names. |
| 118 | const char kSimulcastVersionApplyLocalDescription[] = |
| 119 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 120 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 121 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 122 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 123 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 124 | // The length of RTCP CNAMEs. |
| 125 | static const int kRtcpCnameLength = 16; |
| 126 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 127 | const char kDefaultStreamId[] = "default"; |
| 128 | // NOTE: Duplicated in peer_connection.cc: |
| 129 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 130 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
| 131 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 132 | void NoteAddIceCandidateResult(int result) { |
| 133 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 134 | kAddIceCandidateMax); |
| 135 | } |
| 136 | |
| 137 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 138 | cricket::MediaType media_type) { |
| 139 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 140 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 141 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 142 | static constexpr struct { |
| 143 | KeyExchangeProtocolType protocol_type; |
| 144 | cricket::MediaType media_type; |
| 145 | KeyExchangeProtocolMedia protocol_media; |
| 146 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 147 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 148 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 149 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 150 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 151 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 152 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 153 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 154 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 155 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 156 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 157 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 158 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 159 | }; |
| 160 | |
| 161 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 162 | kEnumCounterKeyProtocolMax); |
| 163 | |
| 164 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 165 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 166 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 167 | i.protocol_media, |
| 168 | kEnumCounterKeyProtocolMediaTypeMax); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 174 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 175 | const SessionDescriptionInterface* new_desc, |
| 176 | const std::string& content_name) { |
| 177 | if (!old_desc) { |
| 178 | return false; |
| 179 | } |
| 180 | const SessionDescription* new_sd = new_desc->description(); |
| 181 | const SessionDescription* old_sd = old_desc->description(); |
| 182 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 183 | if (!cinfo || cinfo->rejected) { |
| 184 | return false; |
| 185 | } |
| 186 | // If the content isn't rejected, check if ufrag and password has changed. |
| 187 | const cricket::TransportDescription* new_transport_desc = |
| 188 | new_sd->GetTransportDescriptionByName(content_name); |
| 189 | const cricket::TransportDescription* old_transport_desc = |
| 190 | old_sd->GetTransportDescriptionByName(content_name); |
| 191 | if (!new_transport_desc || !old_transport_desc) { |
| 192 | // No transport description exists. This is not an ICE restart. |
| 193 | return false; |
| 194 | } |
| 195 | if (cricket::IceCredentialsChanged( |
| 196 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 197 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
| 198 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 199 | << "."; |
| 200 | return true; |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 206 | // from an RTCError. |
| 207 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 208 | SdpType type, |
| 209 | const RTCError& error) { |
| 210 | rtc::StringBuilder oss; |
| 211 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 212 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 213 | return oss.Release(); |
| 214 | } |
| 215 | |
| 216 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 217 | std::string output = "streams=["; |
| 218 | const char* separator = ""; |
| 219 | for (const auto& stream_id : stream_ids) { |
| 220 | output.append(separator).append(stream_id); |
| 221 | separator = ", "; |
| 222 | } |
| 223 | output.append("]"); |
| 224 | return output; |
| 225 | } |
| 226 | |
| 227 | void ReportSimulcastApiVersion(const char* name, |
| 228 | const SessionDescription& session) { |
| 229 | bool has_legacy = false; |
| 230 | bool has_spec_compliant = false; |
| 231 | for (const ContentInfo& content : session.contents()) { |
| 232 | if (!content.media_description()) { |
| 233 | continue; |
| 234 | } |
| 235 | has_spec_compliant |= content.media_description()->HasSimulcast(); |
| 236 | for (const StreamParams& sp : content.media_description()->streams()) { |
| 237 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (has_legacy) { |
| 242 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 243 | kSimulcastApiVersionMax); |
| 244 | } |
| 245 | if (has_spec_compliant) { |
| 246 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 247 | kSimulcastApiVersionMax); |
| 248 | } |
| 249 | if (!has_legacy && !has_spec_compliant) { |
| 250 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 251 | kSimulcastApiVersionMax); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | const ContentInfo* FindTransceiverMSection( |
| 256 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 257 | const SessionDescriptionInterface* session_description) { |
| 258 | return transceiver->mid() |
| 259 | ? session_description->description()->GetContentByName( |
| 260 | *transceiver->mid()) |
| 261 | : nullptr; |
| 262 | } |
| 263 | |
| 264 | // If the direction is "recvonly" or "inactive", treat the description |
| 265 | // as containing no streams. |
| 266 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 267 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 268 | const cricket::MediaContentDescription* desc) { |
| 269 | return RtpTransceiverDirectionHasSend(desc->direction()) |
| 270 | ? desc->streams() |
| 271 | : std::vector<cricket::StreamParams>(); |
| 272 | } |
| 273 | |
| 274 | // Logic to decide if an m= section can be recycled. This means that the new |
| 275 | // m= section is not rejected, but the old local or remote m= section is |
| 276 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 277 | // of the old remote and old local descriptions in no particular order. |
| 278 | // We need to check both the old local and remote because either |
| 279 | // could be the most current from the latest negotation. |
| 280 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 281 | const ContentInfo& content, |
| 282 | const ContentInfo* old_content_one, |
| 283 | const ContentInfo* old_content_two) { |
| 284 | return type == SdpType::kOffer && !content.rejected && |
| 285 | ((old_content_one && old_content_one->rejected) || |
| 286 | (old_content_two && old_content_two->rejected)); |
| 287 | } |
| 288 | |
| 289 | // Verify that the order of media sections in |new_desc| matches |
| 290 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 291 | // less than |current_desc|. In the case of checking an answer's |
| 292 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 293 | // local or remote. In the case of checking an offer's |new_desc| we |
| 294 | // check against the local and remote descriptions stored from the last |
| 295 | // negotiation, because either of these could be the most up to date for |
| 296 | // possible rejected m sections. These are the |current_desc| and |
| 297 | // |secondary_current_desc|. |
| 298 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 299 | const SessionDescription* secondary_current_desc, |
| 300 | const SessionDescription& new_desc, |
| 301 | const SdpType type) { |
| 302 | if (current_desc.contents().size() > new_desc.contents().size()) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 307 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 308 | if (secondary_current_desc && |
| 309 | i < secondary_current_desc->contents().size()) { |
| 310 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 311 | } |
| 312 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 313 | ¤t_desc.contents()[i], |
| 314 | secondary_content_info)) { |
| 315 | // For new offer descriptions, if the media section can be recycled, it's |
| 316 | // valid for the MID and media type to change. |
| 317 | continue; |
| 318 | } |
| 319 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
| 320 | return false; |
| 321 | } |
| 322 | const MediaContentDescription* new_desc_mdesc = |
| 323 | new_desc.contents()[i].media_description(); |
| 324 | const MediaContentDescription* current_desc_mdesc = |
| 325 | current_desc.contents()[i].media_description(); |
| 326 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
| 327 | return false; |
| 328 | } |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 334 | const SessionDescription& desc2) { |
| 335 | return desc1.contents().size() == desc2.contents().size(); |
| 336 | } |
| 337 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 338 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 339 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 340 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 341 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 342 | // by Channel's |srtp_required| check. |
| 343 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
| 344 | const cricket::ContentGroup* bundle = |
| 345 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 346 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 347 | if (content_info.rejected) { |
| 348 | continue; |
| 349 | } |
| 350 | // Note what media is used with each crypto protocol, for all sections. |
| 351 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 352 | : webrtc::kEnumCounterKeyProtocolSdes, |
| 353 | content_info.media_description()->type()); |
| 354 | const std::string& mid = content_info.name; |
| 355 | if (bundle && bundle->HasContentName(mid) && |
| 356 | mid != *(bundle->FirstContentName())) { |
| 357 | // This isn't the first media section in the BUNDLE group, so it's not |
| 358 | // required to have crypto attributes, since only the crypto attributes |
| 359 | // from the first section actually get used. |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | // If the content isn't rejected or bundled into another m= section, crypto |
| 364 | // must be present. |
| 365 | const MediaContentDescription* media = content_info.media_description(); |
| 366 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
| 367 | if (!media || !tinfo) { |
| 368 | // Something is not right. |
| 369 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
| 370 | } |
| 371 | if (dtls_enabled) { |
| 372 | if (!tinfo->description.identity_fingerprint) { |
| 373 | RTC_LOG(LS_WARNING) |
| 374 | << "Session description must have DTLS fingerprint if " |
| 375 | "DTLS enabled."; |
| 376 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 377 | kSdpWithoutDtlsFingerprint); |
| 378 | } |
| 379 | } else { |
| 380 | if (media->cryptos().empty()) { |
| 381 | RTC_LOG(LS_WARNING) |
| 382 | << "Session description must have SDES when DTLS disabled."; |
| 383 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | return RTCError::OK(); |
| 388 | } |
| 389 | |
| 390 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 391 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 392 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 393 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 394 | const cricket::ContentGroup* bundle = |
| 395 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 396 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 397 | if (content_info.rejected) { |
| 398 | continue; |
| 399 | } |
| 400 | const std::string& mid = content_info.name; |
| 401 | if (bundle && bundle->HasContentName(mid) && |
| 402 | mid != *(bundle->FirstContentName())) { |
| 403 | // This isn't the first media section in the BUNDLE group, so it's not |
| 404 | // required to have ufrag/password, since only the ufrag/password from |
| 405 | // the first section actually get used. |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | // If the content isn't rejected or bundled into another m= section, |
| 410 | // ice-ufrag and ice-pwd must be present. |
| 411 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
| 412 | if (!tinfo) { |
| 413 | // Something is not right. |
| 414 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
| 415 | return false; |
| 416 | } |
| 417 | if (tinfo->description.ice_ufrag.empty() || |
| 418 | tinfo->description.ice_pwd.empty()) { |
| 419 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
| 420 | return false; |
| 421 | } |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 427 | std::set<std::string> mids; |
| 428 | for (const cricket::ContentInfo& content : description.contents()) { |
| 429 | if (content.name.empty()) { |
| 430 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 431 | "A media section is missing a MID attribute."); |
| 432 | } |
| 433 | if (!mids.insert(content.name).second) { |
| 434 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 435 | "Duplicate a=mid value '" + content.name + "'."); |
| 436 | } |
| 437 | } |
| 438 | return RTCError::OK(); |
| 439 | } |
| 440 | |
| 441 | bool IsValidOfferToReceiveMedia(int value) { |
| 442 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 443 | return (value >= Options::kUndefined) && |
| 444 | (value <= Options::kMaxOfferToReceiveMedia); |
| 445 | } |
| 446 | |
| 447 | bool ValidateOfferAnswerOptions( |
| 448 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 449 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 450 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
| 451 | } |
| 452 | |
| 453 | // Map internal signaling state name to spec name: |
| 454 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum |
| 455 | std::string GetSignalingStateString( |
| 456 | PeerConnectionInterface::SignalingState state) { |
| 457 | switch (state) { |
| 458 | case PeerConnectionInterface::kStable: |
| 459 | return "stable"; |
| 460 | case PeerConnectionInterface::kHaveLocalOffer: |
| 461 | return "have-local-offer"; |
| 462 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 463 | return "have-local-pranswer"; |
| 464 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 465 | return "have-remote-offer"; |
| 466 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 467 | return "have-remote-pranswer"; |
| 468 | case PeerConnectionInterface::kClosed: |
| 469 | return "closed"; |
| 470 | } |
| 471 | RTC_NOTREACHED(); |
| 472 | return ""; |
| 473 | } |
| 474 | |
| 475 | // This method will extract any send encodings that were sent by the remote |
| 476 | // connection. This is currently only relevant for Simulcast scenario (where |
| 477 | // the number of layers may be communicated by the server). |
| 478 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 479 | const MediaContentDescription& desc) { |
| 480 | if (!desc.HasSimulcast()) { |
| 481 | return {}; |
| 482 | } |
| 483 | std::vector<RtpEncodingParameters> result; |
| 484 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 485 | |
| 486 | // This is a remote description, the parameters we are after should appear |
| 487 | // as receive streams. |
| 488 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 489 | RTC_DCHECK(!alternatives.empty()); |
| 490 | // There is currently no way to specify or choose from alternatives. |
| 491 | // We will always use the first alternative, which is the most preferred. |
| 492 | const SimulcastLayer& layer = alternatives[0]; |
| 493 | RtpEncodingParameters parameters; |
| 494 | parameters.rid = layer.rid; |
| 495 | parameters.active = !layer.is_paused; |
| 496 | result.push_back(parameters); |
| 497 | } |
| 498 | |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 503 | const std::vector<SimulcastLayer>& layers, |
| 504 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
| 505 | RTC_DCHECK(sender); |
| 506 | RtpParameters parameters = sender->GetParametersInternal(); |
| 507 | std::vector<std::string> disabled_layers; |
| 508 | |
| 509 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 510 | // So we will iterate over the send encodings rather than the layers. |
| 511 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 512 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 513 | [&encoding](const SimulcastLayer& layer) { |
| 514 | return layer.rid == encoding.rid; |
| 515 | }); |
| 516 | // A layer that cannot be found may have been removed by the remote party. |
| 517 | if (iter == layers.end()) { |
| 518 | disabled_layers.push_back(encoding.rid); |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | encoding.active = !iter->is_paused; |
| 523 | } |
| 524 | |
| 525 | RTCError result = sender->SetParametersInternal(parameters); |
| 526 | if (result.ok()) { |
| 527 | result = sender->DisableEncodingLayers(disabled_layers); |
| 528 | } |
| 529 | |
| 530 | return result; |
| 531 | } |
| 532 | |
| 533 | static bool SimulcastIsRejected( |
| 534 | const ContentInfo* local_content, |
| 535 | const MediaContentDescription& answer_media_desc) { |
| 536 | bool simulcast_offered = local_content && |
| 537 | local_content->media_description() && |
| 538 | local_content->media_description()->HasSimulcast(); |
| 539 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 540 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 541 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 542 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 543 | } |
| 544 | |
| 545 | static RTCError DisableSimulcastInSender( |
| 546 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
| 547 | RTC_DCHECK(sender); |
| 548 | RtpParameters parameters = sender->GetParametersInternal(); |
| 549 | if (parameters.encodings.size() <= 1) { |
| 550 | return RTCError::OK(); |
| 551 | } |
| 552 | |
| 553 | std::vector<std::string> disabled_layers; |
| 554 | std::transform( |
| 555 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 556 | std::back_inserter(disabled_layers), |
| 557 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 558 | return sender->DisableEncodingLayers(disabled_layers); |
| 559 | } |
| 560 | |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 561 | // The SDP parser used to populate these values by default for the 'content |
| 562 | // name' if an a=mid line was absent. |
| 563 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 564 | switch (media_type) { |
| 565 | case cricket::MEDIA_TYPE_AUDIO: |
| 566 | return cricket::CN_AUDIO; |
| 567 | case cricket::MEDIA_TYPE_VIDEO: |
| 568 | return cricket::CN_VIDEO; |
| 569 | case cricket::MEDIA_TYPE_DATA: |
| 570 | return cricket::CN_DATA; |
Philipp Hancke | 4e8c115 | 2020-10-13 10:43:15 | [diff] [blame] | 571 | case cricket::MEDIA_TYPE_UNSUPPORTED: |
| 572 | return "not supported"; |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 573 | } |
| 574 | RTC_NOTREACHED(); |
| 575 | return ""; |
| 576 | } |
| 577 | |
| 578 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 579 | void AddPlanBRtpSenderOptions( |
| 580 | const std::vector<rtc::scoped_refptr< |
| 581 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
| 582 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 583 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 584 | int num_sim_layers) { |
| 585 | for (const auto& sender : senders) { |
| 586 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 587 | if (audio_media_description_options) { |
| 588 | audio_media_description_options->AddAudioSender( |
| 589 | sender->id(), sender->internal()->stream_ids()); |
| 590 | } |
| 591 | } else { |
| 592 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 593 | if (video_media_description_options) { |
| 594 | video_media_description_options->AddVideoSender( |
| 595 | sender->id(), sender->internal()->stream_ids(), {}, |
| 596 | SimulcastLayerList(), num_sim_layers); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | static cricket::MediaDescriptionOptions |
| 603 | GetMediaDescriptionOptionsForTransceiver( |
| 604 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 605 | transceiver, |
| 606 | const std::string& mid, |
| 607 | bool is_create_offer) { |
| 608 | // NOTE: a stopping transceiver should be treated as a stopped one in |
| 609 | // createOffer as specified in |
| 610 | // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer. |
| 611 | bool stopped = |
| 612 | is_create_offer ? transceiver->stopping() : transceiver->stopped(); |
| 613 | cricket::MediaDescriptionOptions media_description_options( |
| 614 | transceiver->media_type(), mid, transceiver->direction(), stopped); |
| 615 | media_description_options.codec_preferences = |
| 616 | transceiver->codec_preferences(); |
| 617 | media_description_options.header_extensions = |
| 618 | transceiver->HeaderExtensionsToOffer(); |
| 619 | // This behavior is specified in JSEP. The gist is that: |
| 620 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 621 | // sendrecv. |
| 622 | // 2. If the MSID is included, then it must be included in any subsequent |
| 623 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 624 | if (stopped || (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 625 | !transceiver->internal()->has_ever_been_used_to_send())) { |
| 626 | return media_description_options; |
| 627 | } |
| 628 | |
| 629 | cricket::SenderOptions sender_options; |
| 630 | sender_options.track_id = transceiver->sender()->id(); |
| 631 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 632 | |
| 633 | // The following sets up RIDs and Simulcast. |
| 634 | // RIDs are included if Simulcast is requested or if any RID was specified. |
| 635 | RtpParameters send_parameters = |
| 636 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
| 637 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 638 | send_parameters.encodings.end(), |
| 639 | [](const RtpEncodingParameters& encoding) { |
| 640 | return !encoding.rid.empty(); |
| 641 | }); |
| 642 | |
| 643 | std::vector<RidDescription> send_rids; |
| 644 | SimulcastLayerList send_layers; |
| 645 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 646 | if (encoding.rid.empty()) { |
| 647 | continue; |
| 648 | } |
| 649 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 650 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 651 | } |
| 652 | |
| 653 | if (has_rids) { |
| 654 | sender_options.rids = send_rids; |
| 655 | } |
| 656 | |
| 657 | sender_options.simulcast_layers = send_layers; |
| 658 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 659 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 660 | // simulcast, or simulcast is acheived by munging the SDP. |
| 661 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 662 | media_description_options.sender_options.push_back(sender_options); |
| 663 | |
| 664 | return media_description_options; |
| 665 | } |
| 666 | |
| 667 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 668 | static const ContentInfo* GetContentByIndex( |
| 669 | const SessionDescriptionInterface* sdesc, |
| 670 | size_t i) { |
| 671 | if (!sdesc) { |
| 672 | return nullptr; |
| 673 | } |
| 674 | const ContentInfos& contents = sdesc->description()->contents(); |
| 675 | return (i < contents.size() ? &contents[i] : nullptr); |
| 676 | } |
| 677 | |
| 678 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 679 | // m= sectionss (in other words, nothing that involves a map/array). |
| 680 | void ExtractSharedMediaSessionOptions( |
| 681 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 682 | cricket::MediaSessionOptions* session_options) { |
| 683 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 684 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 685 | session_options->raw_packetization_for_video = |
| 686 | rtc_options.raw_packetization_for_video; |
| 687 | } |
| 688 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 689 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 690 | std::string GenerateRtcpCname() { |
| 691 | std::string cname; |
| 692 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
| 693 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
| 694 | RTC_NOTREACHED(); |
| 695 | } |
| 696 | return cname; |
| 697 | } |
| 698 | |
| 699 | // Add options to |session_options| from |rtp_data_channels|. |
| 700 | void AddRtpDataChannelOptions( |
| 701 | const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>& |
| 702 | rtp_data_channels, |
| 703 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 704 | if (!data_media_description_options) { |
| 705 | return; |
| 706 | } |
| 707 | // Check for data channels. |
| 708 | for (const auto& kv : rtp_data_channels) { |
| 709 | const RtpDataChannel* channel = kv.second; |
| 710 | if (channel->state() == RtpDataChannel::kConnecting || |
| 711 | channel->state() == RtpDataChannel::kOpen) { |
| 712 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 713 | // the data channel's label. |
| 714 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 715 | channel->label()); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 720 | // Check if we can send |new_stream| on a PeerConnection. |
| 721 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 722 | webrtc::MediaStreamInterface* new_stream) { |
| 723 | if (!new_stream || !current_streams) { |
| 724 | return false; |
| 725 | } |
| 726 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 727 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
| 728 | << " is already added."; |
| 729 | return false; |
| 730 | } |
| 731 | return true; |
| 732 | } |
| 733 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 734 | } // namespace |
| 735 | |
| 736 | // Used by parameterless SetLocalDescription() to create an offer or answer. |
| 737 | // Upon completion of creating the session description, SetLocalDescription() is |
| 738 | // invoked with the result. |
| 739 | class SdpOfferAnswerHandler::ImplicitCreateSessionDescriptionObserver |
| 740 | : public CreateSessionDescriptionObserver { |
| 741 | public: |
| 742 | ImplicitCreateSessionDescriptionObserver( |
| 743 | rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler, |
| 744 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> |
| 745 | set_local_description_observer) |
| 746 | : sdp_handler_(std::move(sdp_handler)), |
| 747 | set_local_description_observer_( |
| 748 | std::move(set_local_description_observer)) {} |
| 749 | ~ImplicitCreateSessionDescriptionObserver() override { |
| 750 | RTC_DCHECK(was_called_); |
| 751 | } |
| 752 | |
| 753 | void SetOperationCompleteCallback( |
| 754 | std::function<void()> operation_complete_callback) { |
| 755 | operation_complete_callback_ = std::move(operation_complete_callback); |
| 756 | } |
| 757 | |
| 758 | bool was_called() const { return was_called_; } |
| 759 | |
| 760 | void OnSuccess(SessionDescriptionInterface* desc_ptr) override { |
| 761 | RTC_DCHECK(!was_called_); |
| 762 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 763 | was_called_ = true; |
| 764 | |
| 765 | // Abort early if |pc_| is no longer valid. |
| 766 | if (!sdp_handler_) { |
| 767 | operation_complete_callback_(); |
| 768 | return; |
| 769 | } |
| 770 | // DoSetLocalDescription() is a synchronous operation that invokes |
| 771 | // |set_local_description_observer_| with the result. |
| 772 | sdp_handler_->DoSetLocalDescription( |
| 773 | std::move(desc), std::move(set_local_description_observer_)); |
| 774 | operation_complete_callback_(); |
| 775 | } |
| 776 | |
| 777 | void OnFailure(RTCError error) override { |
| 778 | RTC_DCHECK(!was_called_); |
| 779 | was_called_ = true; |
| 780 | set_local_description_observer_->OnSetLocalDescriptionComplete(RTCError( |
| 781 | error.type(), std::string("SetLocalDescription failed to create " |
| 782 | "session description - ") + |
| 783 | error.message())); |
| 784 | operation_complete_callback_(); |
| 785 | } |
| 786 | |
| 787 | private: |
| 788 | bool was_called_ = false; |
| 789 | rtc::WeakPtr<SdpOfferAnswerHandler> sdp_handler_; |
| 790 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> |
| 791 | set_local_description_observer_; |
| 792 | std::function<void()> operation_complete_callback_; |
| 793 | }; |
| 794 | |
| 795 | // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation |
| 796 | // complete callback. When the observer is invoked, the wrapped observer is |
| 797 | // invoked followed by invoking the completion callback. |
| 798 | class CreateSessionDescriptionObserverOperationWrapper |
| 799 | : public CreateSessionDescriptionObserver { |
| 800 | public: |
| 801 | CreateSessionDescriptionObserverOperationWrapper( |
| 802 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer, |
| 803 | std::function<void()> operation_complete_callback) |
| 804 | : observer_(std::move(observer)), |
| 805 | operation_complete_callback_(std::move(operation_complete_callback)) { |
| 806 | RTC_DCHECK(observer_); |
| 807 | } |
| 808 | ~CreateSessionDescriptionObserverOperationWrapper() override { |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 809 | #if RTC_DCHECK_IS_ON |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 810 | RTC_DCHECK(was_called_); |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 811 | #endif |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | void OnSuccess(SessionDescriptionInterface* desc) override { |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 815 | #if RTC_DCHECK_IS_ON |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 816 | RTC_DCHECK(!was_called_); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 817 | was_called_ = true; |
| 818 | #endif // RTC_DCHECK_IS_ON |
| 819 | // Completing the operation before invoking the observer allows the observer |
| 820 | // to execute SetLocalDescription() without delay. |
| 821 | operation_complete_callback_(); |
| 822 | observer_->OnSuccess(desc); |
| 823 | } |
| 824 | |
| 825 | void OnFailure(RTCError error) override { |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 826 | #if RTC_DCHECK_IS_ON |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 827 | RTC_DCHECK(!was_called_); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 828 | was_called_ = true; |
| 829 | #endif // RTC_DCHECK_IS_ON |
| 830 | operation_complete_callback_(); |
| 831 | observer_->OnFailure(std::move(error)); |
| 832 | } |
| 833 | |
| 834 | private: |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 835 | #if RTC_DCHECK_IS_ON |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 836 | bool was_called_ = false; |
| 837 | #endif // RTC_DCHECK_IS_ON |
| 838 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_; |
| 839 | std::function<void()> operation_complete_callback_; |
| 840 | }; |
| 841 | |
| 842 | // Wrapper for SetSessionDescriptionObserver that invokes the success or failure |
| 843 | // callback in a posted message handled by the peer connection. This introduces |
| 844 | // a delay that prevents recursive API calls by the observer, but this also |
| 845 | // means that the PeerConnection can be modified before the observer sees the |
| 846 | // result of the operation. This is ill-advised for synchronizing states. |
| 847 | // |
| 848 | // Implements both the SetLocalDescriptionObserverInterface and the |
| 849 | // SetRemoteDescriptionObserverInterface. |
| 850 | class SdpOfferAnswerHandler::SetSessionDescriptionObserverAdapter |
| 851 | : public SetLocalDescriptionObserverInterface, |
| 852 | public SetRemoteDescriptionObserverInterface { |
| 853 | public: |
| 854 | SetSessionDescriptionObserverAdapter( |
| 855 | rtc::WeakPtr<SdpOfferAnswerHandler> handler, |
| 856 | rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer) |
| 857 | : handler_(std::move(handler)), |
| 858 | inner_observer_(std::move(inner_observer)) {} |
| 859 | |
| 860 | // SetLocalDescriptionObserverInterface implementation. |
| 861 | void OnSetLocalDescriptionComplete(RTCError error) override { |
| 862 | OnSetDescriptionComplete(std::move(error)); |
| 863 | } |
| 864 | // SetRemoteDescriptionObserverInterface implementation. |
| 865 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 866 | OnSetDescriptionComplete(std::move(error)); |
| 867 | } |
| 868 | |
| 869 | private: |
| 870 | void OnSetDescriptionComplete(RTCError error) { |
| 871 | if (!handler_) |
| 872 | return; |
| 873 | if (error.ok()) { |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 874 | handler_->pc_->message_handler()->PostSetSessionDescriptionSuccess( |
| 875 | inner_observer_); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 876 | } else { |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 877 | handler_->pc_->message_handler()->PostSetSessionDescriptionFailure( |
| 878 | inner_observer_, std::move(error)); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | rtc::WeakPtr<SdpOfferAnswerHandler> handler_; |
| 883 | rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer_; |
| 884 | }; |
| 885 | |
| 886 | class SdpOfferAnswerHandler::LocalIceCredentialsToReplace { |
| 887 | public: |
| 888 | // Sets the ICE credentials that need restarting to the ICE credentials of |
| 889 | // the current and pending descriptions. |
| 890 | void SetIceCredentialsFromLocalDescriptions( |
| 891 | const SessionDescriptionInterface* current_local_description, |
| 892 | const SessionDescriptionInterface* pending_local_description) { |
| 893 | ice_credentials_.clear(); |
| 894 | if (current_local_description) { |
| 895 | AppendIceCredentialsFromSessionDescription(*current_local_description); |
| 896 | } |
| 897 | if (pending_local_description) { |
| 898 | AppendIceCredentialsFromSessionDescription(*pending_local_description); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | void ClearIceCredentials() { ice_credentials_.clear(); } |
| 903 | |
| 904 | // Returns true if we have ICE credentials that need restarting. |
| 905 | bool HasIceCredentials() const { return !ice_credentials_.empty(); } |
| 906 | |
| 907 | // Returns true if |local_description| shares no ICE credentials with the |
| 908 | // ICE credentials that need restarting. |
| 909 | bool SatisfiesIceRestart( |
| 910 | const SessionDescriptionInterface& local_description) const { |
| 911 | for (const auto& transport_info : |
| 912 | local_description.description()->transport_infos()) { |
| 913 | if (ice_credentials_.find(std::make_pair( |
| 914 | transport_info.description.ice_ufrag, |
| 915 | transport_info.description.ice_pwd)) != ice_credentials_.end()) { |
| 916 | return false; |
| 917 | } |
| 918 | } |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | private: |
| 923 | void AppendIceCredentialsFromSessionDescription( |
| 924 | const SessionDescriptionInterface& desc) { |
| 925 | for (const auto& transport_info : desc.description()->transport_infos()) { |
| 926 | ice_credentials_.insert( |
| 927 | std::make_pair(transport_info.description.ice_ufrag, |
| 928 | transport_info.description.ice_pwd)); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | std::set<std::pair<std::string, std::string>> ice_credentials_; |
| 933 | }; |
| 934 | |
| 935 | SdpOfferAnswerHandler::SdpOfferAnswerHandler(PeerConnection* pc) |
| 936 | : pc_(pc), |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 937 | local_streams_(StreamCollection::Create()), |
| 938 | remote_streams_(StreamCollection::Create()), |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 939 | operations_chain_(rtc::OperationsChain::Create()), |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 940 | rtcp_cname_(GenerateRtcpCname()), |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 941 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()), |
| 942 | weak_ptr_factory_(this) { |
| 943 | operations_chain_->SetOnChainEmptyCallback( |
| 944 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr()]() { |
| 945 | if (!this_weak_ptr) |
| 946 | return; |
| 947 | this_weak_ptr->OnOperationsChainEmpty(); |
| 948 | }); |
| 949 | } |
| 950 | |
| 951 | SdpOfferAnswerHandler::~SdpOfferAnswerHandler() {} |
| 952 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 953 | // Static |
| 954 | std::unique_ptr<SdpOfferAnswerHandler> SdpOfferAnswerHandler::Create( |
| 955 | PeerConnection* pc, |
| 956 | const PeerConnectionInterface::RTCConfiguration& configuration, |
| 957 | PeerConnectionDependencies& dependencies) { |
| 958 | auto handler = absl::WrapUnique(new SdpOfferAnswerHandler(pc)); |
| 959 | handler->Initialize(configuration, dependencies); |
| 960 | return handler; |
| 961 | } |
| 962 | |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 963 | void SdpOfferAnswerHandler::Initialize( |
| 964 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 965 | PeerConnectionDependencies& dependencies) { |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 966 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 967 | video_options_.screencast_min_bitrate_kbps = |
| 968 | configuration.screencast_min_bitrate; |
| 969 | audio_options_.combined_audio_video_bwe = |
| 970 | configuration.combined_audio_video_bwe; |
| 971 | |
| 972 | audio_options_.audio_jitter_buffer_max_packets = |
| 973 | configuration.audio_jitter_buffer_max_packets; |
| 974 | |
| 975 | audio_options_.audio_jitter_buffer_fast_accelerate = |
| 976 | configuration.audio_jitter_buffer_fast_accelerate; |
| 977 | |
| 978 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 979 | configuration.audio_jitter_buffer_min_delay_ms; |
| 980 | |
| 981 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 982 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 983 | |
| 984 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 985 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 986 | if (!configuration.certificates.empty()) { |
| 987 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 988 | // just picking the first one. The decision should be made based on the DTLS |
| 989 | // handshake. The DTLS negotiations need to know about all certificates. |
| 990 | certificate = configuration.certificates[0]; |
| 991 | } |
| 992 | |
| 993 | webrtc_session_desc_factory_ = |
| 994 | std::make_unique<WebRtcSessionDescriptionFactory>( |
| 995 | signaling_thread(), channel_manager(), this, pc_->session_id(), |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 996 | pc_->dtls_enabled(), std::move(dependencies.cert_generator), |
Harald Alvestrand | a094787 | 2020-11-09 14:15:00 | [diff] [blame] | 997 | certificate, &ssrc_generator_, |
| 998 | [this](const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 999 | transport_controller()->SetLocalCertificate(certificate); |
| 1000 | }); |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 1001 | |
Harald Alvestrand | 4da4a87 | 2020-11-04 10:34:21 | [diff] [blame] | 1002 | if (pc_->options()->disable_encryption) { |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 1003 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1004 | } |
| 1005 | |
| 1006 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 1007 | pc_->GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
| 1008 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
| 1009 | |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1010 | if (dependencies.video_bitrate_allocator_factory) { |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 1011 | video_bitrate_allocator_factory_ = |
Harald Alvestrand | 9cd199d | 2020-10-27 07:10:43 | [diff] [blame] | 1012 | std::move(dependencies.video_bitrate_allocator_factory); |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 1013 | } else { |
| 1014 | video_bitrate_allocator_factory_ = |
| 1015 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1019 | // ================================================================== |
| 1020 | // Access to pc_ variables |
| 1021 | cricket::ChannelManager* SdpOfferAnswerHandler::channel_manager() const { |
| 1022 | return pc_->channel_manager(); |
| 1023 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1024 | TransceiverList* SdpOfferAnswerHandler::transceivers() { |
| 1025 | if (!pc_->rtp_manager()) { |
| 1026 | return nullptr; |
| 1027 | } |
| 1028 | return pc_->rtp_manager()->transceivers(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1029 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1030 | const TransceiverList* SdpOfferAnswerHandler::transceivers() const { |
| 1031 | if (!pc_->rtp_manager()) { |
| 1032 | return nullptr; |
| 1033 | } |
| 1034 | return pc_->rtp_manager()->transceivers(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1035 | } |
| 1036 | JsepTransportController* SdpOfferAnswerHandler::transport_controller() { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 1037 | return pc_->transport_controller(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1038 | } |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 1039 | const JsepTransportController* SdpOfferAnswerHandler::transport_controller() |
| 1040 | const { |
| 1041 | return pc_->transport_controller(); |
| 1042 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1043 | DataChannelController* SdpOfferAnswerHandler::data_channel_controller() { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 1044 | return pc_->data_channel_controller(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1045 | } |
| 1046 | const DataChannelController* SdpOfferAnswerHandler::data_channel_controller() |
| 1047 | const { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 1048 | return pc_->data_channel_controller(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1049 | } |
| 1050 | cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 1051 | return pc_->port_allocator(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1052 | } |
| 1053 | const cricket::PortAllocator* SdpOfferAnswerHandler::port_allocator() const { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 1054 | return pc_->port_allocator(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1055 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1056 | RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() { |
| 1057 | return pc_->rtp_manager(); |
| 1058 | } |
| 1059 | const RtpTransmissionManager* SdpOfferAnswerHandler::rtp_manager() const { |
| 1060 | return pc_->rtp_manager(); |
| 1061 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1062 | |
| 1063 | // =================================================================== |
| 1064 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1065 | void SdpOfferAnswerHandler::PrepareForShutdown() { |
| 1066 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1067 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1068 | } |
| 1069 | |
| 1070 | void SdpOfferAnswerHandler::Close() { |
| 1071 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
| 1072 | } |
| 1073 | |
| 1074 | void SdpOfferAnswerHandler::RestartIce() { |
| 1075 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1076 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 1077 | current_local_description(), pending_local_description()); |
| 1078 | UpdateNegotiationNeeded(); |
| 1079 | } |
| 1080 | |
| 1081 | rtc::Thread* SdpOfferAnswerHandler::signaling_thread() const { |
| 1082 | return pc_->signaling_thread(); |
| 1083 | } |
| 1084 | |
| 1085 | void SdpOfferAnswerHandler::CreateOffer( |
| 1086 | CreateSessionDescriptionObserver* observer, |
| 1087 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 1088 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1089 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1090 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1091 | // lambda will execute immediately. |
| 1092 | operations_chain_->ChainOperation( |
| 1093 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 1094 | observer_refptr = |
| 1095 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 1096 | options](std::function<void()> operations_chain_callback) { |
| 1097 | // Abort early if |this_weak_ptr| is no longer valid. |
| 1098 | if (!this_weak_ptr) { |
| 1099 | observer_refptr->OnFailure( |
| 1100 | RTCError(RTCErrorType::INTERNAL_ERROR, |
| 1101 | "CreateOffer failed because the session was shut down")); |
| 1102 | operations_chain_callback(); |
| 1103 | return; |
| 1104 | } |
| 1105 | // The operation completes asynchronously when the wrapper is invoked. |
| 1106 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 1107 | observer_wrapper(new rtc::RefCountedObject< |
| 1108 | CreateSessionDescriptionObserverOperationWrapper>( |
| 1109 | std::move(observer_refptr), |
| 1110 | std::move(operations_chain_callback))); |
| 1111 | this_weak_ptr->DoCreateOffer(options, observer_wrapper); |
| 1112 | }); |
| 1113 | } |
| 1114 | |
| 1115 | void SdpOfferAnswerHandler::SetLocalDescription( |
| 1116 | SetSessionDescriptionObserver* observer, |
| 1117 | SessionDescriptionInterface* desc_ptr) { |
| 1118 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1119 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1120 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1121 | // lambda will execute immediately. |
| 1122 | operations_chain_->ChainOperation( |
| 1123 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 1124 | observer_refptr = |
| 1125 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 1126 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 1127 | std::function<void()> operations_chain_callback) mutable { |
| 1128 | // Abort early if |this_weak_ptr| is no longer valid. |
| 1129 | if (!this_weak_ptr) { |
| 1130 | // For consistency with SetSessionDescriptionObserverAdapter whose |
| 1131 | // posted messages doesn't get processed when the PC is destroyed, we |
| 1132 | // do not inform |observer_refptr| that the operation failed. |
| 1133 | operations_chain_callback(); |
| 1134 | return; |
| 1135 | } |
| 1136 | // SetSessionDescriptionObserverAdapter takes care of making sure the |
| 1137 | // |observer_refptr| is invoked in a posted message. |
| 1138 | this_weak_ptr->DoSetLocalDescription( |
| 1139 | std::move(desc), |
| 1140 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface>( |
| 1141 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 1142 | this_weak_ptr, observer_refptr))); |
| 1143 | // For backwards-compatability reasons, we declare the operation as |
| 1144 | // completed here (rather than in a post), so that the operation chain |
| 1145 | // is not blocked by this operation when the observer is invoked. This |
| 1146 | // allows the observer to trigger subsequent offer/answer operations |
| 1147 | // synchronously if the operation chain is now empty. |
| 1148 | operations_chain_callback(); |
| 1149 | }); |
| 1150 | } |
| 1151 | |
| 1152 | void SdpOfferAnswerHandler::SetLocalDescription( |
| 1153 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1154 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1155 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1156 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1157 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1158 | // lambda will execute immediately. |
| 1159 | operations_chain_->ChainOperation( |
| 1160 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 1161 | desc = std::move(desc)]( |
| 1162 | std::function<void()> operations_chain_callback) mutable { |
| 1163 | // Abort early if |this_weak_ptr| is no longer valid. |
| 1164 | if (!this_weak_ptr) { |
| 1165 | observer->OnSetLocalDescriptionComplete(RTCError( |
| 1166 | RTCErrorType::INTERNAL_ERROR, |
| 1167 | "SetLocalDescription failed because the session was shut down")); |
| 1168 | operations_chain_callback(); |
| 1169 | return; |
| 1170 | } |
| 1171 | this_weak_ptr->DoSetLocalDescription(std::move(desc), observer); |
| 1172 | // DoSetLocalDescription() is implemented as a synchronous operation. |
| 1173 | // The |observer| will already have been informed that it completed, and |
| 1174 | // we can mark this operation as complete without any loose ends. |
| 1175 | operations_chain_callback(); |
| 1176 | }); |
| 1177 | } |
| 1178 | |
| 1179 | void SdpOfferAnswerHandler::SetLocalDescription( |
| 1180 | SetSessionDescriptionObserver* observer) { |
| 1181 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1182 | SetLocalDescription( |
| 1183 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 1184 | weak_ptr_factory_.GetWeakPtr(), observer)); |
| 1185 | } |
| 1186 | |
| 1187 | void SdpOfferAnswerHandler::SetLocalDescription( |
| 1188 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1189 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1190 | // The |create_sdp_observer| handles performing DoSetLocalDescription() with |
| 1191 | // the resulting description as well as completing the operation. |
| 1192 | rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver> |
| 1193 | create_sdp_observer( |
| 1194 | new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>( |
| 1195 | weak_ptr_factory_.GetWeakPtr(), observer)); |
| 1196 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1197 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1198 | // lambda will execute immediately. |
| 1199 | operations_chain_->ChainOperation( |
| 1200 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 1201 | create_sdp_observer](std::function<void()> operations_chain_callback) { |
| 1202 | // The |create_sdp_observer| is responsible for completing the |
| 1203 | // operation. |
| 1204 | create_sdp_observer->SetOperationCompleteCallback( |
| 1205 | std::move(operations_chain_callback)); |
| 1206 | // Abort early if |this_weak_ptr| is no longer valid. This triggers the |
| 1207 | // same code path as if DoCreateOffer() or DoCreateAnswer() failed. |
| 1208 | if (!this_weak_ptr) { |
| 1209 | create_sdp_observer->OnFailure(RTCError( |
| 1210 | RTCErrorType::INTERNAL_ERROR, |
| 1211 | "SetLocalDescription failed because the session was shut down")); |
| 1212 | return; |
| 1213 | } |
| 1214 | switch (this_weak_ptr->signaling_state()) { |
| 1215 | case PeerConnectionInterface::kStable: |
| 1216 | case PeerConnectionInterface::kHaveLocalOffer: |
| 1217 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 1218 | // TODO(hbos): If [LastCreatedOffer] exists and still represents the |
| 1219 | // current state of the system, use that instead of creating another |
| 1220 | // offer. |
| 1221 | this_weak_ptr->DoCreateOffer( |
| 1222 | PeerConnectionInterface::RTCOfferAnswerOptions(), |
| 1223 | create_sdp_observer); |
| 1224 | break; |
| 1225 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 1226 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 1227 | // TODO(hbos): If [LastCreatedAnswer] exists and still represents |
| 1228 | // the current state of the system, use that instead of creating |
| 1229 | // another answer. |
| 1230 | this_weak_ptr->DoCreateAnswer( |
| 1231 | PeerConnectionInterface::RTCOfferAnswerOptions(), |
| 1232 | create_sdp_observer); |
| 1233 | break; |
| 1234 | case PeerConnectionInterface::kClosed: |
| 1235 | create_sdp_observer->OnFailure(RTCError( |
| 1236 | RTCErrorType::INVALID_STATE, |
| 1237 | "SetLocalDescription called when PeerConnection is closed.")); |
| 1238 | break; |
| 1239 | } |
| 1240 | }); |
| 1241 | } |
| 1242 | |
| 1243 | RTCError SdpOfferAnswerHandler::ApplyLocalDescription( |
| 1244 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1245 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1246 | RTC_DCHECK(desc); |
| 1247 | |
| 1248 | // Update stats here so that we have the most recent stats for tracks and |
| 1249 | // streams that might be removed by updating the session description. |
| 1250 | pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); |
| 1251 | |
| 1252 | // Take a reference to the old local description since it's used below to |
| 1253 | // compare against the new local description. When setting the new local |
| 1254 | // description, grab ownership of the replaced session description in case it |
| 1255 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1256 | // of the method. |
| 1257 | const SessionDescriptionInterface* old_local_description = |
| 1258 | local_description(); |
| 1259 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
| 1260 | SdpType type = desc->GetType(); |
| 1261 | if (type == SdpType::kAnswer) { |
| 1262 | replaced_local_description = pending_local_description_ |
| 1263 | ? std::move(pending_local_description_) |
| 1264 | : std::move(current_local_description_); |
| 1265 | current_local_description_ = std::move(desc); |
| 1266 | pending_local_description_ = nullptr; |
| 1267 | current_remote_description_ = std::move(pending_remote_description_); |
| 1268 | } else { |
| 1269 | replaced_local_description = std::move(pending_local_description_); |
| 1270 | pending_local_description_ = std::move(desc); |
| 1271 | } |
| 1272 | // The session description to apply now must be accessed by |
| 1273 | // |local_description()|. |
| 1274 | RTC_DCHECK(local_description()); |
| 1275 | |
| 1276 | // Report statistics about any use of simulcast. |
| 1277 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 1278 | *local_description()->description()); |
| 1279 | |
| 1280 | if (!is_caller_) { |
| 1281 | if (remote_description()) { |
| 1282 | // Remote description was applied first, so this PC is the callee. |
| 1283 | is_caller_ = false; |
| 1284 | } else { |
| 1285 | // Local description is applied first, so this PC is the caller. |
| 1286 | is_caller_ = true; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1290 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1291 | if (!error.ok()) { |
| 1292 | return error; |
| 1293 | } |
| 1294 | |
| 1295 | if (IsUnifiedPlan()) { |
| 1296 | RTCError error = UpdateTransceiversAndDataChannels( |
| 1297 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 1298 | remote_description()); |
| 1299 | if (!error.ok()) { |
| 1300 | return error; |
| 1301 | } |
| 1302 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 1303 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1304 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1305 | if (transceiver->stopped()) { |
| 1306 | continue; |
| 1307 | } |
| 1308 | |
| 1309 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 1310 | // Note that code paths that don't set MID won't be able to use |
| 1311 | // information about DTLS transports. |
| 1312 | if (transceiver->mid()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1313 | auto dtls_transport = transport_controller()->LookupDtlsTransportByMid( |
| 1314 | *transceiver->mid()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1315 | transceiver->internal()->sender_internal()->set_transport( |
| 1316 | dtls_transport); |
| 1317 | transceiver->internal()->receiver_internal()->set_transport( |
| 1318 | dtls_transport); |
| 1319 | } |
| 1320 | |
| 1321 | const ContentInfo* content = |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 1322 | FindMediaSectionForTransceiver(transceiver, local_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1323 | if (!content) { |
| 1324 | continue; |
| 1325 | } |
| 1326 | const MediaContentDescription* media_desc = content->media_description(); |
| 1327 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 1328 | // the following steps: |
| 1329 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1330 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 1331 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 1332 | // "recvonly", process the removal of a remote track for the media |
| 1333 | // description, given transceiver, removeList, and muteTracks. |
| 1334 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 1335 | (transceiver->internal()->fired_direction() && |
| 1336 | RtpTransceiverDirectionHasRecv( |
| 1337 | *transceiver->internal()->fired_direction()))) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1338 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 1339 | &removed_streams); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1340 | } |
| 1341 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 1342 | // [[FiredDirection]] slots to direction. |
| 1343 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 1344 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
| 1345 | } |
| 1346 | } |
| 1347 | auto observer = pc_->Observer(); |
| 1348 | for (const auto& transceiver : remove_list) { |
| 1349 | observer->OnRemoveTrack(transceiver->receiver()); |
| 1350 | } |
| 1351 | for (const auto& stream : removed_streams) { |
| 1352 | observer->OnRemoveStream(stream); |
| 1353 | } |
| 1354 | } else { |
| 1355 | // Media channels will be created only when offer is set. These may use new |
| 1356 | // transports just created by PushdownTransportDescription. |
| 1357 | if (type == SdpType::kOffer) { |
| 1358 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 1359 | // description is applied. Restore back to old description. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1360 | RTCError error = CreateChannels(*local_description()->description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1361 | if (!error.ok()) { |
| 1362 | return error; |
| 1363 | } |
| 1364 | } |
| 1365 | // Remove unused channels if MediaContentDescription is rejected. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1366 | RemoveUnusedChannels(local_description()->description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 1370 | local_description()->description()); |
| 1371 | if (!error.ok()) { |
| 1372 | return error; |
| 1373 | } |
| 1374 | |
| 1375 | if (remote_description()) { |
| 1376 | // Now that we have a local description, we can push down remote candidates. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1377 | UseCandidatesInSessionDescription(remote_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | pending_ice_restarts_.clear(); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1381 | if (session_error() != SessionError::kNone) { |
| 1382 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | // If setting the description decided our SSL role, allocate any necessary |
| 1386 | // SCTP sids. |
| 1387 | rtc::SSLRole role; |
| 1388 | if (IsSctpLike(pc_->data_channel_type()) && pc_->GetSctpSslRole(&role)) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1389 | data_channel_controller()->AllocateSctpSids(role); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | if (IsUnifiedPlan()) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1393 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1394 | if (transceiver->stopped()) { |
| 1395 | continue; |
| 1396 | } |
| 1397 | const ContentInfo* content = |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 1398 | FindMediaSectionForTransceiver(transceiver, local_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1399 | if (!content) { |
| 1400 | continue; |
| 1401 | } |
| 1402 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 1403 | if (content->rejected || !channel || channel->local_streams().empty()) { |
| 1404 | // 0 is a special value meaning "this sender has no associated send |
| 1405 | // stream". Need to call this so the sender won't attempt to configure |
| 1406 | // a no longer existing stream and run into DCHECKs in the lower |
| 1407 | // layers. |
| 1408 | transceiver->internal()->sender_internal()->SetSsrc(0); |
| 1409 | } else { |
| 1410 | // Get the StreamParams from the channel which could generate SSRCs. |
| 1411 | const std::vector<StreamParams>& streams = channel->local_streams(); |
| 1412 | transceiver->internal()->sender_internal()->set_stream_ids( |
| 1413 | streams[0].stream_ids()); |
| 1414 | transceiver->internal()->sender_internal()->SetSsrc( |
| 1415 | streams[0].first_ssrc()); |
| 1416 | } |
| 1417 | } |
| 1418 | } else { |
| 1419 | // Plan B semantics. |
| 1420 | |
| 1421 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1422 | // local session description. |
| 1423 | const cricket::ContentInfo* audio_content = |
| 1424 | GetFirstAudioContent(local_description()->description()); |
| 1425 | if (audio_content) { |
| 1426 | if (audio_content->rejected) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1427 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1428 | } else { |
| 1429 | const cricket::AudioContentDescription* audio_desc = |
| 1430 | audio_content->media_description()->as_audio(); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1431 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | const cricket::ContentInfo* video_content = |
| 1436 | GetFirstVideoContent(local_description()->description()); |
| 1437 | if (video_content) { |
| 1438 | if (video_content->rejected) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1439 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1440 | } else { |
| 1441 | const cricket::VideoContentDescription* video_desc = |
| 1442 | video_content->media_description()->as_video(); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1443 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | const cricket::ContentInfo* data_content = |
| 1449 | GetFirstDataContent(local_description()->description()); |
| 1450 | if (data_content) { |
| 1451 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 1452 | data_content->media_description()->as_rtp_data(); |
| 1453 | // rtp_data_desc will be null if this is an SCTP description. |
| 1454 | if (rtp_data_desc) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1455 | data_channel_controller()->UpdateLocalRtpDataChannels( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1456 | rtp_data_desc->streams()); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | if (type == SdpType::kAnswer && |
| 1461 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 1462 | *current_local_description_)) { |
| 1463 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 1464 | } |
| 1465 | |
| 1466 | return RTCError::OK(); |
| 1467 | } |
| 1468 | |
| 1469 | void SdpOfferAnswerHandler::SetRemoteDescription( |
| 1470 | SetSessionDescriptionObserver* observer, |
| 1471 | SessionDescriptionInterface* desc_ptr) { |
| 1472 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1473 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1474 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1475 | // lambda will execute immediately. |
| 1476 | operations_chain_->ChainOperation( |
| 1477 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 1478 | observer_refptr = |
| 1479 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 1480 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 1481 | std::function<void()> operations_chain_callback) mutable { |
| 1482 | // Abort early if |this_weak_ptr| is no longer valid. |
| 1483 | if (!this_weak_ptr) { |
| 1484 | // For consistency with SetSessionDescriptionObserverAdapter whose |
| 1485 | // posted messages doesn't get processed when the PC is destroyed, we |
| 1486 | // do not inform |observer_refptr| that the operation failed. |
| 1487 | operations_chain_callback(); |
| 1488 | return; |
| 1489 | } |
| 1490 | // SetSessionDescriptionObserverAdapter takes care of making sure the |
| 1491 | // |observer_refptr| is invoked in a posted message. |
| 1492 | this_weak_ptr->DoSetRemoteDescription( |
| 1493 | std::move(desc), |
| 1494 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 1495 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 1496 | this_weak_ptr, observer_refptr))); |
| 1497 | // For backwards-compatability reasons, we declare the operation as |
| 1498 | // completed here (rather than in a post), so that the operation chain |
| 1499 | // is not blocked by this operation when the observer is invoked. This |
| 1500 | // allows the observer to trigger subsequent offer/answer operations |
| 1501 | // synchronously if the operation chain is now empty. |
| 1502 | operations_chain_callback(); |
| 1503 | }); |
| 1504 | } |
| 1505 | |
| 1506 | void SdpOfferAnswerHandler::SetRemoteDescription( |
| 1507 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1508 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
| 1509 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1510 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 1511 | // this operation will be queued to be invoked, otherwise the contents of the |
| 1512 | // lambda will execute immediately. |
| 1513 | operations_chain_->ChainOperation( |
| 1514 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 1515 | desc = std::move(desc)]( |
| 1516 | std::function<void()> operations_chain_callback) mutable { |
| 1517 | // Abort early if |this_weak_ptr| is no longer valid. |
| 1518 | if (!this_weak_ptr) { |
| 1519 | observer->OnSetRemoteDescriptionComplete(RTCError( |
| 1520 | RTCErrorType::INTERNAL_ERROR, |
| 1521 | "SetRemoteDescription failed because the session was shut down")); |
| 1522 | operations_chain_callback(); |
| 1523 | return; |
| 1524 | } |
| 1525 | this_weak_ptr->DoSetRemoteDescription(std::move(desc), |
| 1526 | std::move(observer)); |
| 1527 | // DoSetRemoteDescription() is implemented as a synchronous operation. |
| 1528 | // The |observer| will already have been informed that it completed, and |
| 1529 | // we can mark this operation as complete without any loose ends. |
| 1530 | operations_chain_callback(); |
| 1531 | }); |
| 1532 | } |
| 1533 | |
| 1534 | RTCError SdpOfferAnswerHandler::ApplyRemoteDescription( |
| 1535 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1536 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1537 | RTC_DCHECK(desc); |
| 1538 | |
| 1539 | // Update stats here so that we have the most recent stats for tracks and |
| 1540 | // streams that might be removed by updating the session description. |
| 1541 | pc_->stats()->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard); |
| 1542 | |
| 1543 | // Take a reference to the old remote description since it's used below to |
| 1544 | // compare against the new remote description. When setting the new remote |
| 1545 | // description, grab ownership of the replaced session description in case it |
| 1546 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 1547 | // of the method. |
| 1548 | const SessionDescriptionInterface* old_remote_description = |
| 1549 | remote_description(); |
| 1550 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
| 1551 | SdpType type = desc->GetType(); |
| 1552 | if (type == SdpType::kAnswer) { |
| 1553 | replaced_remote_description = pending_remote_description_ |
| 1554 | ? std::move(pending_remote_description_) |
| 1555 | : std::move(current_remote_description_); |
| 1556 | current_remote_description_ = std::move(desc); |
| 1557 | pending_remote_description_ = nullptr; |
| 1558 | current_local_description_ = std::move(pending_local_description_); |
| 1559 | } else { |
| 1560 | replaced_remote_description = std::move(pending_remote_description_); |
| 1561 | pending_remote_description_ = std::move(desc); |
| 1562 | } |
| 1563 | // The session description to apply now must be accessed by |
| 1564 | // |remote_description()|. |
| 1565 | RTC_DCHECK(remote_description()); |
| 1566 | |
| 1567 | // Report statistics about any use of simulcast. |
| 1568 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 1569 | *remote_description()->description()); |
| 1570 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1571 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1572 | if (!error.ok()) { |
| 1573 | return error; |
| 1574 | } |
| 1575 | // Transport and Media channels will be created only when offer is set. |
| 1576 | if (IsUnifiedPlan()) { |
| 1577 | RTCError error = UpdateTransceiversAndDataChannels( |
| 1578 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 1579 | old_remote_description); |
| 1580 | if (!error.ok()) { |
| 1581 | return error; |
| 1582 | } |
| 1583 | } else { |
| 1584 | // Media channels will be created only when offer is set. These may use new |
| 1585 | // transports just created by PushdownTransportDescription. |
| 1586 | if (type == SdpType::kOffer) { |
| 1587 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
| 1588 | // description is applied. Restore back to old description. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1589 | RTCError error = CreateChannels(*remote_description()->description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1590 | if (!error.ok()) { |
| 1591 | return error; |
| 1592 | } |
| 1593 | } |
| 1594 | // Remove unused channels if MediaContentDescription is rejected. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1595 | RemoveUnusedChannels(remote_description()->description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | // NOTE: Candidates allocation will be initiated only when |
| 1599 | // SetLocalDescription is called. |
| 1600 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 1601 | remote_description()->description()); |
| 1602 | if (!error.ok()) { |
| 1603 | return error; |
| 1604 | } |
| 1605 | |
| 1606 | if (local_description() && |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1607 | !UseCandidatesInSessionDescription(remote_description())) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1608 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 1609 | } |
| 1610 | |
| 1611 | if (old_remote_description) { |
| 1612 | for (const cricket::ContentInfo& content : |
| 1613 | old_remote_description->description()->contents()) { |
| 1614 | // Check if this new SessionDescription contains new ICE ufrag and |
| 1615 | // password that indicates the remote peer requests an ICE restart. |
| 1616 | // TODO(deadbeef): When we start storing both the current and pending |
| 1617 | // remote description, this should reset pending_ice_restarts and compare |
| 1618 | // against the current description. |
| 1619 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 1620 | content.name)) { |
| 1621 | if (type == SdpType::kOffer) { |
| 1622 | pending_ice_restarts_.insert(content.name); |
| 1623 | } |
| 1624 | } else { |
| 1625 | // We retain all received candidates only if ICE is not restarted. |
| 1626 | // When ICE is restarted, all previous candidates belong to an old |
| 1627 | // generation and should not be kept. |
| 1628 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 1629 | // description should only contain candidates from the last set remote |
| 1630 | // description plus any candidates added since then. We should remove |
| 1631 | // this once we're sure it won't break anything. |
| 1632 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 1633 | old_remote_description, content.name, mutable_remote_description()); |
| 1634 | } |
| 1635 | } |
| 1636 | } |
| 1637 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1638 | if (session_error() != SessionError::kNone) { |
| 1639 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | // Set the the ICE connection state to connecting since the connection may |
| 1643 | // become writable with peer reflexive candidates before any remote candidate |
| 1644 | // is signaled. |
| 1645 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 1646 | // is to have a new signal the indicates a change in checking state from the |
| 1647 | // transport and expose a new checking() member from transport that can be |
| 1648 | // read to determine the current checking state. The existing SignalConnecting |
| 1649 | // actually means "gathering candidates", so cannot be be used here. |
| 1650 | if (remote_description()->GetType() != SdpType::kOffer && |
| 1651 | remote_description()->number_of_mediasections() > 0u && |
| 1652 | pc_->ice_connection_state() == |
| 1653 | PeerConnectionInterface::kIceConnectionNew) { |
| 1654 | pc_->SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 1655 | } |
| 1656 | |
| 1657 | // If setting the description decided our SSL role, allocate any necessary |
| 1658 | // SCTP sids. |
| 1659 | rtc::SSLRole role; |
| 1660 | if (IsSctpLike(pc_->data_channel_type()) && pc_->GetSctpSslRole(&role)) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1661 | data_channel_controller()->AllocateSctpSids(role); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | if (IsUnifiedPlan()) { |
| 1665 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1666 | now_receiving_transceivers; |
| 1667 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 1668 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
| 1669 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1670 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 1671 | const ContentInfo* content = |
| 1672 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1673 | if (!content) { |
| 1674 | continue; |
| 1675 | } |
| 1676 | const MediaContentDescription* media_desc = content->media_description(); |
| 1677 | RtpTransceiverDirection local_direction = |
| 1678 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 1679 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 1680 | // RTCSessionDescription: Set the associated remote streams given |
| 1681 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 1682 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 1683 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 1684 | std::vector<std::string> stream_ids; |
| 1685 | if (!media_desc->streams().empty()) { |
| 1686 | // The remote description has signaled the stream IDs. |
| 1687 | stream_ids = media_desc->streams()[0].stream_ids(); |
| 1688 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1689 | transceivers() |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 1690 | ->StableState(transceiver) |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 1691 | ->SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1692 | |
| 1693 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 1694 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 1695 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 1696 | stream_ids, &added_streams, |
| 1697 | &removed_streams); |
| 1698 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 1699 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 1700 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 1701 | // process the addition of a remote track for the media description. |
| 1702 | if (!transceiver->fired_direction() || |
| 1703 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 1704 | RTC_LOG(LS_INFO) |
| 1705 | << "Processing the addition of a remote track for MID=" |
| 1706 | << content->name << "."; |
| 1707 | now_receiving_transceivers.push_back(transceiver); |
| 1708 | } |
| 1709 | } |
| 1710 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
| 1711 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 1712 | // removal of a remote track for the media description, given transceiver, |
| 1713 | // removeList, and muteTracks. |
| 1714 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
| 1715 | (transceiver->fired_direction() && |
| 1716 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1717 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 1718 | &removed_streams); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1719 | } |
| 1720 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
| 1721 | transceiver->internal()->set_fired_direction(local_direction); |
| 1722 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
| 1723 | // the following steps: |
| 1724 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1725 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
| 1726 | // direction. |
| 1727 | transceiver->internal()->set_current_direction(local_direction); |
| 1728 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 1729 | if (transceiver->mid()) { |
| 1730 | auto dtls_transport = |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1731 | transport_controller()->LookupDtlsTransportByMid( |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1732 | *transceiver->mid()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1733 | transceiver->internal()->sender_internal()->set_transport( |
| 1734 | dtls_transport); |
| 1735 | transceiver->internal()->receiver_internal()->set_transport( |
| 1736 | dtls_transport); |
| 1737 | } |
| 1738 | } |
| 1739 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
| 1740 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
| 1741 | if (content->rejected && !transceiver->stopped()) { |
| 1742 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 1743 | << " since the media section was rejected."; |
| 1744 | transceiver->internal()->StopTransceiverProcedure(); |
| 1745 | } |
| 1746 | if (!content->rejected && |
| 1747 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 1748 | if (!media_desc->streams().empty() && |
| 1749 | media_desc->streams()[0].has_ssrcs()) { |
| 1750 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 1751 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 1752 | } else { |
| 1753 | transceiver->internal() |
| 1754 | ->receiver_internal() |
| 1755 | ->SetupUnsignaledMediaChannel(); |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | // Once all processing has finished, fire off callbacks. |
| 1760 | auto observer = pc_->Observer(); |
| 1761 | for (const auto& transceiver : now_receiving_transceivers) { |
| 1762 | pc_->stats()->AddTrack(transceiver->receiver()->track()); |
| 1763 | observer->OnTrack(transceiver); |
| 1764 | observer->OnAddTrack(transceiver->receiver(), |
| 1765 | transceiver->receiver()->streams()); |
| 1766 | } |
| 1767 | for (const auto& stream : added_streams) { |
| 1768 | observer->OnAddStream(stream); |
| 1769 | } |
| 1770 | for (const auto& transceiver : remove_list) { |
| 1771 | observer->OnRemoveTrack(transceiver->receiver()); |
| 1772 | } |
| 1773 | for (const auto& stream : removed_streams) { |
| 1774 | observer->OnRemoveStream(stream); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | const cricket::ContentInfo* audio_content = |
| 1779 | GetFirstAudioContent(remote_description()->description()); |
| 1780 | const cricket::ContentInfo* video_content = |
| 1781 | GetFirstVideoContent(remote_description()->description()); |
| 1782 | const cricket::AudioContentDescription* audio_desc = |
| 1783 | GetFirstAudioContentDescription(remote_description()->description()); |
| 1784 | const cricket::VideoContentDescription* video_desc = |
| 1785 | GetFirstVideoContentDescription(remote_description()->description()); |
| 1786 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 1787 | GetFirstRtpDataContentDescription(remote_description()->description()); |
| 1788 | |
| 1789 | // Check if the descriptions include streams, just in case the peer supports |
| 1790 | // MSID, but doesn't indicate so with "a=msid-semantic". |
| 1791 | if (remote_description()->description()->msid_supported() || |
| 1792 | (audio_desc && !audio_desc->streams().empty()) || |
| 1793 | (video_desc && !video_desc->streams().empty())) { |
| 1794 | remote_peer_supports_msid_ = true; |
| 1795 | } |
| 1796 | |
| 1797 | // We wait to signal new streams until we finish processing the description, |
| 1798 | // since only at that point will new streams have all their tracks. |
| 1799 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 1800 | |
| 1801 | if (!IsUnifiedPlan()) { |
| 1802 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 1803 | // rejected media section, there is some cleanup logic that expects the |
| 1804 | // voice/ video channel to still be set. But in this method the voice/video |
| 1805 | // channel would have been destroyed by the SetRemoteDescription caller |
| 1806 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 1807 | // calls should be moved to right before the DestroyChannel calls to fix |
| 1808 | // this. |
| 1809 | |
| 1810 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 1811 | // and MediaStreams. |
| 1812 | if (audio_content) { |
| 1813 | if (audio_content->rejected) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1814 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1815 | } else { |
| 1816 | bool default_audio_track_needed = |
| 1817 | !remote_peer_supports_msid_ && |
| 1818 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1819 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 1820 | default_audio_track_needed, audio_desc->type(), |
| 1821 | new_streams); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 1826 | // and MediaStreams. |
| 1827 | if (video_content) { |
| 1828 | if (video_content->rejected) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1829 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1830 | } else { |
| 1831 | bool default_video_track_needed = |
| 1832 | !remote_peer_supports_msid_ && |
| 1833 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1834 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 1835 | default_video_track_needed, video_desc->type(), |
| 1836 | new_streams); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | // If this is an RTP data transport, update the DataChannels with the |
| 1841 | // information from the remote peer. |
| 1842 | if (rtp_data_desc) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1843 | data_channel_controller()->UpdateRemoteRtpDataChannels( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1844 | GetActiveStreams(rtp_data_desc)); |
| 1845 | } |
| 1846 | |
| 1847 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 1848 | auto observer = pc_->Observer(); |
| 1849 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 1850 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 1851 | pc_->stats()->AddStream(new_stream); |
| 1852 | observer->OnAddStream( |
| 1853 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 1854 | } |
| 1855 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1856 | UpdateEndedRemoteMediaStreams(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | if (type == SdpType::kAnswer && |
| 1860 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 1861 | *current_local_description_)) { |
| 1862 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 1863 | } |
| 1864 | |
| 1865 | return RTCError::OK(); |
| 1866 | } |
| 1867 | |
| 1868 | void SdpOfferAnswerHandler::DoSetLocalDescription( |
| 1869 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1870 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 1871 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1872 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetLocalDescription"); |
| 1873 | |
| 1874 | if (!observer) { |
| 1875 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
| 1876 | return; |
| 1877 | } |
| 1878 | |
| 1879 | if (!desc) { |
| 1880 | observer->OnSetLocalDescriptionComplete( |
| 1881 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
| 1882 | return; |
| 1883 | } |
| 1884 | |
| 1885 | // If a session error has occurred the PeerConnection is in a possibly |
| 1886 | // inconsistent state so fail right away. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1887 | if (session_error() != SessionError::kNone) { |
| 1888 | std::string error_message = GetSessionErrorMsg(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1889 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
| 1890 | observer->OnSetLocalDescriptionComplete( |
| 1891 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 1892 | return; |
| 1893 | } |
| 1894 | |
| 1895 | // For SLD we support only explicit rollback. |
| 1896 | if (desc->GetType() == SdpType::kRollback) { |
| 1897 | if (IsUnifiedPlan()) { |
| 1898 | observer->OnSetLocalDescriptionComplete(Rollback(desc->GetType())); |
| 1899 | } else { |
| 1900 | observer->OnSetLocalDescriptionComplete( |
| 1901 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1902 | "Rollback not supported in Plan B")); |
| 1903 | } |
| 1904 | return; |
| 1905 | } |
| 1906 | |
| 1907 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1908 | if (!error.ok()) { |
| 1909 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1910 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1911 | RTC_LOG(LS_ERROR) << error_message; |
| 1912 | observer->OnSetLocalDescriptionComplete( |
| 1913 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1918 | // which may destroy it before returning. |
| 1919 | const SdpType type = desc->GetType(); |
| 1920 | |
| 1921 | error = ApplyLocalDescription(std::move(desc)); |
| 1922 | // |desc| may be destroyed at this point. |
| 1923 | |
| 1924 | if (!error.ok()) { |
| 1925 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1926 | // inconsistent state, so act conservatively here and set the session error |
| 1927 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1928 | SetSessionError(SessionError::kContent, error.message()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1929 | std::string error_message = |
| 1930 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1931 | RTC_LOG(LS_ERROR) << error_message; |
| 1932 | observer->OnSetLocalDescriptionComplete( |
| 1933 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 1934 | return; |
| 1935 | } |
| 1936 | RTC_DCHECK(local_description()); |
| 1937 | |
| 1938 | if (local_description()->GetType() == SdpType::kAnswer) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1939 | RemoveStoppedTransceivers(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1940 | |
| 1941 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1942 | // MaybeStartGathering... |
| 1943 | pc_->network_thread()->Invoke<void>( |
| 1944 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1945 | port_allocator())); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1946 | // Make UMA notes about what was agreed to. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1947 | ReportNegotiatedSdpSemantics(*local_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | observer->OnSetLocalDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 1951 | pc_->NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1952 | |
| 1953 | // Check if negotiation is needed. We must do this after informing the |
| 1954 | // observer that SetLocalDescription() has completed to ensure negotiation is |
| 1955 | // not needed prior to the promise resolving. |
| 1956 | if (IsUnifiedPlan()) { |
| 1957 | bool was_negotiation_needed = is_negotiation_needed_; |
| 1958 | UpdateNegotiationNeeded(); |
| 1959 | if (signaling_state() == PeerConnectionInterface::kStable && |
| 1960 | was_negotiation_needed && is_negotiation_needed_) { |
| 1961 | // Legacy version. |
| 1962 | pc_->Observer()->OnRenegotiationNeeded(); |
| 1963 | // Spec-compliant version; the event may get invalidated before firing. |
| 1964 | GenerateNegotiationNeededEvent(); |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | // MaybeStartGathering needs to be called after informing the observer so that |
| 1969 | // we don't signal any candidates before signaling that SetLocalDescription |
| 1970 | // completed. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 1971 | transport_controller()->MaybeStartGathering(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | void SdpOfferAnswerHandler::DoCreateOffer( |
| 1975 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 1976 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 1977 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1978 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateOffer"); |
| 1979 | |
| 1980 | if (!observer) { |
| 1981 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
| 1982 | return; |
| 1983 | } |
| 1984 | |
| 1985 | if (pc_->IsClosed()) { |
| 1986 | std::string error = "CreateOffer called when PeerConnection is closed."; |
| 1987 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 1988 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1989 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
| 1990 | return; |
| 1991 | } |
| 1992 | |
| 1993 | // If a session error has occurred the PeerConnection is in a possibly |
| 1994 | // inconsistent state so fail right away. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 1995 | if (session_error() != SessionError::kNone) { |
| 1996 | std::string error_message = GetSessionErrorMsg(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1997 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 1998 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 1999 | observer, |
| 2000 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2001 | return; |
| 2002 | } |
| 2003 | |
| 2004 | if (!ValidateOfferAnswerOptions(options)) { |
| 2005 | std::string error = "CreateOffer called with invalid options."; |
| 2006 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2007 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2008 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
| 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2013 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2014 | if (IsUnifiedPlan()) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2015 | RTCError error = HandleLegacyOfferOptions(options); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2016 | if (!error.ok()) { |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2017 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
| 2018 | observer, std::move(error)); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | cricket::MediaSessionOptions session_options; |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2024 | GetOptionsForOffer(options, &session_options); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2025 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
| 2026 | } |
| 2027 | |
| 2028 | void SdpOfferAnswerHandler::CreateAnswer( |
| 2029 | CreateSessionDescriptionObserver* observer, |
| 2030 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 2031 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2032 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2033 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2034 | // lambda will execute immediately. |
| 2035 | operations_chain_->ChainOperation( |
| 2036 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2037 | observer_refptr = |
| 2038 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2039 | options](std::function<void()> operations_chain_callback) { |
| 2040 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2041 | if (!this_weak_ptr) { |
| 2042 | observer_refptr->OnFailure(RTCError( |
| 2043 | RTCErrorType::INTERNAL_ERROR, |
| 2044 | "CreateAnswer failed because the session was shut down")); |
| 2045 | operations_chain_callback(); |
| 2046 | return; |
| 2047 | } |
| 2048 | // The operation completes asynchronously when the wrapper is invoked. |
| 2049 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2050 | observer_wrapper(new rtc::RefCountedObject< |
| 2051 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2052 | std::move(observer_refptr), |
| 2053 | std::move(operations_chain_callback))); |
| 2054 | this_weak_ptr->DoCreateAnswer(options, observer_wrapper); |
| 2055 | }); |
| 2056 | } |
| 2057 | |
| 2058 | void SdpOfferAnswerHandler::DoCreateAnswer( |
| 2059 | const PeerConnectionInterface::RTCOfferAnswerOptions& options, |
| 2060 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2061 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2062 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoCreateAnswer"); |
| 2063 | if (!observer) { |
| 2064 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
| 2065 | return; |
| 2066 | } |
| 2067 | |
| 2068 | // If a session error has occurred the PeerConnection is in a possibly |
| 2069 | // inconsistent state so fail right away. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2070 | if (session_error() != SessionError::kNone) { |
| 2071 | std::string error_message = GetSessionErrorMsg(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2072 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2073 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2074 | observer, |
| 2075 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2076 | return; |
| 2077 | } |
| 2078 | |
| 2079 | if (!(signaling_state_ == PeerConnectionInterface::kHaveRemoteOffer || |
| 2080 | signaling_state_ == PeerConnectionInterface::kHaveLocalPrAnswer)) { |
| 2081 | std::string error = |
| 2082 | "PeerConnection cannot create an answer in a state other than " |
| 2083 | "have-remote-offer or have-local-pranswer."; |
| 2084 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 1090e44 | 2020-10-05 07:01:09 | [diff] [blame] | 2085 | pc_->message_handler()->PostCreateSessionDescriptionFailure( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2086 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
| 2087 | return; |
| 2088 | } |
| 2089 | |
| 2090 | // The remote description should be set if we're in the right state. |
| 2091 | RTC_DCHECK(remote_description()); |
| 2092 | |
| 2093 | if (IsUnifiedPlan()) { |
| 2094 | if (options.offer_to_receive_audio != |
| 2095 | PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) { |
| 2096 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2097 | "supported with Unified Plan semantics. Use the " |
| 2098 | "RtpTransceiver API instead."; |
| 2099 | } |
| 2100 | if (options.offer_to_receive_video != |
| 2101 | PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) { |
| 2102 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2103 | "supported with Unified Plan semantics. Use the " |
| 2104 | "RtpTransceiver API instead."; |
| 2105 | } |
| 2106 | } |
| 2107 | |
| 2108 | cricket::MediaSessionOptions session_options; |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2109 | GetOptionsForAnswer(options, &session_options); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2110 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
| 2111 | } |
| 2112 | |
| 2113 | void SdpOfferAnswerHandler::DoSetRemoteDescription( |
| 2114 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2115 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
| 2116 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2117 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::DoSetRemoteDescription"); |
| 2118 | |
| 2119 | if (!observer) { |
| 2120 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
| 2121 | return; |
| 2122 | } |
| 2123 | |
| 2124 | if (!desc) { |
| 2125 | observer->OnSetRemoteDescriptionComplete(RTCError( |
| 2126 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
| 2127 | return; |
| 2128 | } |
| 2129 | |
| 2130 | // If a session error has occurred the PeerConnection is in a possibly |
| 2131 | // inconsistent state so fail right away. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2132 | if (session_error() != SessionError::kNone) { |
| 2133 | std::string error_message = GetSessionErrorMsg(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2134 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2135 | observer->OnSetRemoteDescriptionComplete( |
| 2136 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2137 | return; |
| 2138 | } |
| 2139 | if (IsUnifiedPlan()) { |
| 2140 | if (pc_->configuration()->enable_implicit_rollback) { |
| 2141 | if (desc->GetType() == SdpType::kOffer && |
| 2142 | signaling_state() == PeerConnectionInterface::kHaveLocalOffer) { |
| 2143 | Rollback(desc->GetType()); |
| 2144 | } |
| 2145 | } |
| 2146 | // Explicit rollback. |
| 2147 | if (desc->GetType() == SdpType::kRollback) { |
| 2148 | observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType())); |
| 2149 | return; |
| 2150 | } |
| 2151 | } else if (desc->GetType() == SdpType::kRollback) { |
| 2152 | observer->OnSetRemoteDescriptionComplete( |
| 2153 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 2154 | "Rollback not supported in Plan B")); |
| 2155 | return; |
| 2156 | } |
Philipp Hancke | b8ca2a1 | 2020-10-07 10:47:10 | [diff] [blame] | 2157 | if (desc->GetType() == SdpType::kOffer || |
| 2158 | desc->GetType() == SdpType::kAnswer) { |
| 2159 | // Report to UMA the format of the received offer or answer. |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2160 | pc_->ReportSdpFormatReceived(*desc); |
| 2161 | } |
| 2162 | |
| 2163 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 2164 | // points. |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 2165 | FillInMissingRemoteMids(desc->description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2166 | |
| 2167 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
| 2168 | if (!error.ok()) { |
| 2169 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2170 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2171 | RTC_LOG(LS_ERROR) << error_message; |
| 2172 | observer->OnSetRemoteDescriptionComplete( |
| 2173 | RTCError(error.type(), std::move(error_message))); |
| 2174 | return; |
| 2175 | } |
| 2176 | |
| 2177 | // Grab the description type before moving ownership to |
| 2178 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2179 | const SdpType type = desc->GetType(); |
| 2180 | |
| 2181 | error = ApplyRemoteDescription(std::move(desc)); |
| 2182 | // |desc| may be destroyed at this point. |
| 2183 | |
| 2184 | if (!error.ok()) { |
| 2185 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2186 | // inconsistent state, so act conservatively here and set the session error |
| 2187 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2188 | SetSessionError(SessionError::kContent, error.message()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2189 | std::string error_message = |
| 2190 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2191 | RTC_LOG(LS_ERROR) << error_message; |
| 2192 | observer->OnSetRemoteDescriptionComplete( |
| 2193 | RTCError(error.type(), std::move(error_message))); |
| 2194 | return; |
| 2195 | } |
| 2196 | RTC_DCHECK(remote_description()); |
| 2197 | |
| 2198 | if (type == SdpType::kAnswer) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2199 | RemoveStoppedTransceivers(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2200 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2201 | // MaybeStartGathering... |
| 2202 | pc_->network_thread()->Invoke<void>( |
| 2203 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2204 | port_allocator())); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2205 | // Make UMA notes about what was agreed to. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2206 | ReportNegotiatedSdpSemantics(*remote_description()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2210 | pc_->NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2211 | |
| 2212 | // Check if negotiation is needed. We must do this after informing the |
| 2213 | // observer that SetRemoteDescription() has completed to ensure negotiation is |
| 2214 | // not needed prior to the promise resolving. |
| 2215 | if (IsUnifiedPlan()) { |
| 2216 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2217 | UpdateNegotiationNeeded(); |
| 2218 | if (signaling_state() == PeerConnectionInterface::kStable && |
| 2219 | was_negotiation_needed && is_negotiation_needed_) { |
| 2220 | // Legacy version. |
| 2221 | pc_->Observer()->OnRenegotiationNeeded(); |
| 2222 | // Spec-compliant version; the event may get invalidated before firing. |
| 2223 | GenerateNegotiationNeededEvent(); |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | |
| 2228 | void SdpOfferAnswerHandler::SetAssociatedRemoteStreams( |
| 2229 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 2230 | const std::vector<std::string>& stream_ids, |
| 2231 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 2232 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 2233 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2234 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 2235 | for (const std::string& stream_id : stream_ids) { |
| 2236 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2237 | remote_streams_->find(stream_id); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2238 | if (!stream) { |
| 2239 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2240 | MediaStream::Create(stream_id)); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2241 | remote_streams_->AddStream(stream); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2242 | added_streams->push_back(stream); |
| 2243 | } |
| 2244 | media_streams.push_back(stream); |
| 2245 | } |
| 2246 | // Special case: "a=msid" missing, use random stream ID. |
| 2247 | if (media_streams.empty() && |
| 2248 | !(remote_description()->description()->msid_signaling() & |
| 2249 | cricket::kMsidSignalingMediaSection)) { |
| 2250 | if (!missing_msid_default_stream_) { |
| 2251 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 2252 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 2253 | added_streams->push_back(missing_msid_default_stream_); |
| 2254 | } |
| 2255 | media_streams.push_back(missing_msid_default_stream_); |
| 2256 | } |
| 2257 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 2258 | receiver->streams(); |
| 2259 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 2260 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 2261 | // update the stream-track relationships in a later step. We do this earlier, |
| 2262 | // changing the order of things, but the end-result is the same. |
| 2263 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 2264 | // instead. https://crbug.com/webrtc/9480 |
| 2265 | receiver->SetStreams(media_streams); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2266 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | bool SdpOfferAnswerHandler::AddIceCandidate( |
| 2270 | const IceCandidateInterface* ice_candidate) { |
| 2271 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2272 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::AddIceCandidate"); |
| 2273 | if (pc_->IsClosed()) { |
| 2274 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
| 2275 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
| 2276 | return false; |
| 2277 | } |
| 2278 | |
| 2279 | if (!remote_description()) { |
| 2280 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
| 2281 | "without any remote session description."; |
| 2282 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
| 2283 | return false; |
| 2284 | } |
| 2285 | |
| 2286 | if (!ice_candidate) { |
| 2287 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
| 2288 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
| 2289 | return false; |
| 2290 | } |
| 2291 | |
| 2292 | bool valid = false; |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2293 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2294 | if (!valid) { |
| 2295 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
| 2296 | return false; |
| 2297 | } |
| 2298 | |
| 2299 | // Add this candidate to the remote session description. |
| 2300 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
| 2301 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
| 2302 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
| 2303 | return false; |
| 2304 | } |
| 2305 | |
| 2306 | if (ready) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2307 | bool result = UseCandidate(ice_candidate); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2308 | if (result) { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 2309 | pc_->NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2310 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 2311 | } else { |
| 2312 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 2313 | } |
| 2314 | return result; |
| 2315 | } else { |
| 2316 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
| 2317 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
| 2318 | return true; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | void SdpOfferAnswerHandler::AddIceCandidate( |
| 2323 | std::unique_ptr<IceCandidateInterface> candidate, |
| 2324 | std::function<void(RTCError)> callback) { |
| 2325 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2326 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2327 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2328 | // lambda will execute immediately. |
| 2329 | operations_chain_->ChainOperation( |
| 2330 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2331 | candidate = std::move(candidate), callback = std::move(callback)]( |
| 2332 | std::function<void()> operations_chain_callback) { |
| 2333 | if (!this_weak_ptr) { |
| 2334 | operations_chain_callback(); |
| 2335 | callback(RTCError( |
| 2336 | RTCErrorType::INVALID_STATE, |
| 2337 | "AddIceCandidate failed because the session was shut down")); |
| 2338 | return; |
| 2339 | } |
| 2340 | if (!this_weak_ptr->AddIceCandidate(candidate.get())) { |
| 2341 | operations_chain_callback(); |
| 2342 | // Fail with an error type and message consistent with Chromium. |
| 2343 | // TODO(hbos): Fail with error types according to spec. |
| 2344 | callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 2345 | "Error processing ICE candidate")); |
| 2346 | return; |
| 2347 | } |
| 2348 | operations_chain_callback(); |
| 2349 | callback(RTCError::OK()); |
| 2350 | }); |
| 2351 | } |
| 2352 | |
| 2353 | bool SdpOfferAnswerHandler::RemoveIceCandidates( |
| 2354 | const std::vector<cricket::Candidate>& candidates) { |
| 2355 | TRACE_EVENT0("webrtc", "SdpOfferAnswerHandler::RemoveIceCandidates"); |
| 2356 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2357 | if (pc_->IsClosed()) { |
| 2358 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 2359 | return false; |
| 2360 | } |
| 2361 | |
| 2362 | if (!remote_description()) { |
| 2363 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 2364 | "without any remote session description."; |
| 2365 | return false; |
| 2366 | } |
| 2367 | |
| 2368 | if (candidates.empty()) { |
| 2369 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
| 2370 | return false; |
| 2371 | } |
| 2372 | |
| 2373 | size_t number_removed = |
| 2374 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2375 | if (number_removed != candidates.size()) { |
| 2376 | RTC_LOG(LS_ERROR) |
| 2377 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
| 2378 | << candidates.size() << " but only " << number_removed |
| 2379 | << " are removed."; |
| 2380 | } |
| 2381 | |
| 2382 | // Remove the candidates from the transport controller. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2383 | RTCError error = transport_controller()->RemoveRemoteCandidates(candidates); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2384 | if (!error.ok()) { |
| 2385 | RTC_LOG(LS_ERROR) |
| 2386 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 2387 | << error.message(); |
| 2388 | } |
| 2389 | return true; |
| 2390 | } |
| 2391 | |
| 2392 | void SdpOfferAnswerHandler::AddLocalIceCandidate( |
| 2393 | const JsepIceCandidate* candidate) { |
| 2394 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2395 | if (local_description()) { |
| 2396 | mutable_local_description()->AddCandidate(candidate); |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | void SdpOfferAnswerHandler::RemoveLocalIceCandidates( |
| 2401 | const std::vector<cricket::Candidate>& candidates) { |
| 2402 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2403 | if (local_description()) { |
| 2404 | mutable_local_description()->RemoveCandidates(candidates); |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | const SessionDescriptionInterface* SdpOfferAnswerHandler::local_description() |
| 2409 | const { |
| 2410 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2411 | return pending_local_description_ ? pending_local_description_.get() |
| 2412 | : current_local_description_.get(); |
| 2413 | } |
| 2414 | |
| 2415 | const SessionDescriptionInterface* SdpOfferAnswerHandler::remote_description() |
| 2416 | const { |
| 2417 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2418 | return pending_remote_description_ ? pending_remote_description_.get() |
| 2419 | : current_remote_description_.get(); |
| 2420 | } |
| 2421 | |
| 2422 | const SessionDescriptionInterface* |
| 2423 | SdpOfferAnswerHandler::current_local_description() const { |
| 2424 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2425 | return current_local_description_.get(); |
| 2426 | } |
| 2427 | |
| 2428 | const SessionDescriptionInterface* |
| 2429 | SdpOfferAnswerHandler::current_remote_description() const { |
| 2430 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2431 | return current_remote_description_.get(); |
| 2432 | } |
| 2433 | |
| 2434 | const SessionDescriptionInterface* |
| 2435 | SdpOfferAnswerHandler::pending_local_description() const { |
| 2436 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2437 | return pending_local_description_.get(); |
| 2438 | } |
| 2439 | |
| 2440 | const SessionDescriptionInterface* |
| 2441 | SdpOfferAnswerHandler::pending_remote_description() const { |
| 2442 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2443 | return pending_remote_description_.get(); |
| 2444 | } |
| 2445 | |
| 2446 | PeerConnectionInterface::SignalingState SdpOfferAnswerHandler::signaling_state() |
| 2447 | const { |
| 2448 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2449 | return signaling_state_; |
| 2450 | } |
| 2451 | |
| 2452 | void SdpOfferAnswerHandler::ChangeSignalingState( |
| 2453 | PeerConnectionInterface::SignalingState signaling_state) { |
| 2454 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2455 | if (signaling_state_ == signaling_state) { |
| 2456 | return; |
| 2457 | } |
| 2458 | RTC_LOG(LS_INFO) << "Session: " << pc_->session_id() << " Old state: " |
| 2459 | << GetSignalingStateString(signaling_state_) |
| 2460 | << " New state: " |
| 2461 | << GetSignalingStateString(signaling_state); |
| 2462 | signaling_state_ = signaling_state; |
| 2463 | pc_->Observer()->OnSignalingChange(signaling_state_); |
| 2464 | } |
| 2465 | |
| 2466 | RTCError SdpOfferAnswerHandler::UpdateSessionState( |
| 2467 | SdpType type, |
| 2468 | cricket::ContentSource source, |
| 2469 | const cricket::SessionDescription* description) { |
| 2470 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2471 | |
| 2472 | // If there's already a pending error then no state transition should happen. |
| 2473 | // But all call-sites should be verifying this before calling us! |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2474 | RTC_DCHECK(session_error() == SessionError::kNone); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2475 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2476 | // Update the signaling state according to the specified state machine (see |
| 2477 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
| 2478 | if (type == SdpType::kOffer) { |
| 2479 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 2480 | ? PeerConnectionInterface::kHaveLocalOffer |
| 2481 | : PeerConnectionInterface::kHaveRemoteOffer); |
| 2482 | } else if (type == SdpType::kPrAnswer) { |
| 2483 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 2484 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 2485 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 2486 | } else { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2487 | RTC_DCHECK(type == SdpType::kAnswer); |
| 2488 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2489 | transceivers()->DiscardStableStates(); |
Harald Alvestrand | 75b9ab6 | 2020-09-30 22:17:18 | [diff] [blame] | 2490 | have_pending_rtp_data_channel_ = false; |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | // Update internal objects according to the session description's media |
| 2494 | // descriptions. |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2495 | RTCError error = PushdownMediaDescription(type, source); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2496 | if (!error.ok()) { |
| 2497 | return error; |
| 2498 | } |
| 2499 | |
| 2500 | return RTCError::OK(); |
| 2501 | } |
| 2502 | |
| 2503 | bool SdpOfferAnswerHandler::ShouldFireNegotiationNeededEvent( |
| 2504 | uint32_t event_id) { |
| 2505 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2506 | // Plan B? Always fire to conform with useless legacy behavior. |
| 2507 | if (!IsUnifiedPlan()) { |
| 2508 | return true; |
| 2509 | } |
| 2510 | // The event ID has been invalidated. Either negotiation is no longer needed |
| 2511 | // or a newer negotiation needed event has been generated. |
| 2512 | if (event_id != negotiation_needed_event_id_) { |
| 2513 | return false; |
| 2514 | } |
| 2515 | // The chain is no longer empty, update negotiation needed when it becomes |
| 2516 | // empty. This should generate a newer negotiation needed event, making this |
| 2517 | // one obsolete. |
| 2518 | if (!operations_chain_->IsEmpty()) { |
| 2519 | // Since we just suppressed an event that would have been fired, if |
| 2520 | // negotiation is still needed by the time the chain becomes empty again, we |
| 2521 | // must make sure to generate another event if negotiation is needed then. |
| 2522 | // This happens when |is_negotiation_needed_| goes from false to true, so we |
| 2523 | // set it to false until UpdateNegotiationNeeded() is called. |
| 2524 | is_negotiation_needed_ = false; |
| 2525 | update_negotiation_needed_on_empty_chain_ = true; |
| 2526 | return false; |
| 2527 | } |
| 2528 | // We must not fire if the signaling state is no longer "stable". If |
| 2529 | // negotiation is still needed when we return to "stable", a new negotiation |
| 2530 | // needed event will be generated, so this one can safely be suppressed. |
| 2531 | if (signaling_state_ != PeerConnectionInterface::kStable) { |
| 2532 | return false; |
| 2533 | } |
| 2534 | // All checks have passed - please fire "negotiationneeded" now! |
| 2535 | return true; |
| 2536 | } |
| 2537 | |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2538 | rtc::scoped_refptr<StreamCollectionInterface> |
| 2539 | SdpOfferAnswerHandler::local_streams() { |
| 2540 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2541 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 2542 | "Plan SdpSemantics. Please use GetSenders " |
| 2543 | "instead."; |
| 2544 | return local_streams_; |
| 2545 | } |
| 2546 | |
| 2547 | rtc::scoped_refptr<StreamCollectionInterface> |
| 2548 | SdpOfferAnswerHandler::remote_streams() { |
| 2549 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2550 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 2551 | "Plan SdpSemantics. Please use GetReceivers " |
| 2552 | "instead."; |
| 2553 | return remote_streams_; |
| 2554 | } |
| 2555 | |
| 2556 | bool SdpOfferAnswerHandler::AddStream(MediaStreamInterface* local_stream) { |
| 2557 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2558 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 2559 | "SdpSemantics. Please use AddTrack instead."; |
| 2560 | if (pc_->IsClosed()) { |
| 2561 | return false; |
| 2562 | } |
| 2563 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
| 2564 | return false; |
| 2565 | } |
| 2566 | |
| 2567 | local_streams_->AddStream(local_stream); |
| 2568 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 2569 | observer->SignalAudioTrackAdded.connect( |
| 2570 | this, &SdpOfferAnswerHandler::OnAudioTrackAdded); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2571 | observer->SignalAudioTrackRemoved.connect( |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 2572 | this, &SdpOfferAnswerHandler::OnAudioTrackRemoved); |
| 2573 | observer->SignalVideoTrackAdded.connect( |
| 2574 | this, &SdpOfferAnswerHandler::OnVideoTrackAdded); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2575 | observer->SignalVideoTrackRemoved.connect( |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 2576 | this, &SdpOfferAnswerHandler::OnVideoTrackRemoved); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2577 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
| 2578 | |
| 2579 | for (const auto& track : local_stream->GetAudioTracks()) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2580 | rtp_manager()->AddAudioTrack(track.get(), local_stream); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2581 | } |
| 2582 | for (const auto& track : local_stream->GetVideoTracks()) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2583 | rtp_manager()->AddVideoTrack(track.get(), local_stream); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | pc_->stats()->AddStream(local_stream); |
| 2587 | UpdateNegotiationNeeded(); |
| 2588 | return true; |
| 2589 | } |
| 2590 | |
| 2591 | void SdpOfferAnswerHandler::RemoveStream(MediaStreamInterface* local_stream) { |
| 2592 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2593 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 2594 | "Plan SdpSemantics. Please use RemoveTrack " |
| 2595 | "instead."; |
| 2596 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
| 2597 | if (!pc_->IsClosed()) { |
| 2598 | for (const auto& track : local_stream->GetAudioTracks()) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2599 | rtp_manager()->RemoveAudioTrack(track.get(), local_stream); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2600 | } |
| 2601 | for (const auto& track : local_stream->GetVideoTracks()) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2602 | rtp_manager()->RemoveVideoTrack(track.get(), local_stream); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 2603 | } |
| 2604 | } |
| 2605 | local_streams_->RemoveStream(local_stream); |
| 2606 | stream_observers_.erase( |
| 2607 | std::remove_if( |
| 2608 | stream_observers_.begin(), stream_observers_.end(), |
| 2609 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
| 2610 | return observer->stream()->id().compare(local_stream->id()) == 0; |
| 2611 | }), |
| 2612 | stream_observers_.end()); |
| 2613 | |
| 2614 | if (pc_->IsClosed()) { |
| 2615 | return; |
| 2616 | } |
| 2617 | UpdateNegotiationNeeded(); |
| 2618 | } |
| 2619 | |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 2620 | void SdpOfferAnswerHandler::OnAudioTrackAdded(AudioTrackInterface* track, |
| 2621 | MediaStreamInterface* stream) { |
| 2622 | if (pc_->IsClosed()) { |
| 2623 | return; |
| 2624 | } |
| 2625 | rtp_manager()->AddAudioTrack(track, stream); |
| 2626 | UpdateNegotiationNeeded(); |
| 2627 | } |
| 2628 | |
| 2629 | void SdpOfferAnswerHandler::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 2630 | MediaStreamInterface* stream) { |
| 2631 | if (pc_->IsClosed()) { |
| 2632 | return; |
| 2633 | } |
| 2634 | rtp_manager()->RemoveAudioTrack(track, stream); |
| 2635 | UpdateNegotiationNeeded(); |
| 2636 | } |
| 2637 | |
| 2638 | void SdpOfferAnswerHandler::OnVideoTrackAdded(VideoTrackInterface* track, |
| 2639 | MediaStreamInterface* stream) { |
| 2640 | if (pc_->IsClosed()) { |
| 2641 | return; |
| 2642 | } |
| 2643 | rtp_manager()->AddVideoTrack(track, stream); |
| 2644 | UpdateNegotiationNeeded(); |
| 2645 | } |
| 2646 | |
| 2647 | void SdpOfferAnswerHandler::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 2648 | MediaStreamInterface* stream) { |
| 2649 | if (pc_->IsClosed()) { |
| 2650 | return; |
| 2651 | } |
| 2652 | rtp_manager()->RemoveVideoTrack(track, stream); |
| 2653 | UpdateNegotiationNeeded(); |
| 2654 | } |
| 2655 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2656 | RTCError SdpOfferAnswerHandler::Rollback(SdpType desc_type) { |
| 2657 | auto state = signaling_state(); |
| 2658 | if (state != PeerConnectionInterface::kHaveLocalOffer && |
| 2659 | state != PeerConnectionInterface::kHaveRemoteOffer) { |
| 2660 | return RTCError(RTCErrorType::INVALID_STATE, |
| 2661 | "Called in wrong signalingState: " + |
| 2662 | GetSignalingStateString(signaling_state())); |
| 2663 | } |
| 2664 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2665 | RTC_DCHECK(IsUnifiedPlan()); |
| 2666 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams; |
| 2667 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams; |
| 2668 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers; |
| 2669 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2670 | for (auto&& transceivers_stable_state_pair : transceivers()->StableStates()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2671 | auto transceiver = transceivers_stable_state_pair.first; |
| 2672 | auto state = transceivers_stable_state_pair.second; |
| 2673 | |
| 2674 | if (state.remote_stream_ids()) { |
| 2675 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
| 2676 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
| 2677 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 2678 | state.remote_stream_ids().value(), |
| 2679 | &added_streams, &removed_streams); |
| 2680 | all_added_streams.insert(all_added_streams.end(), added_streams.begin(), |
| 2681 | added_streams.end()); |
| 2682 | all_removed_streams.insert(all_removed_streams.end(), |
| 2683 | removed_streams.begin(), |
| 2684 | removed_streams.end()); |
| 2685 | if (!state.has_m_section() && !state.newly_created()) { |
| 2686 | continue; |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | RTC_DCHECK(transceiver->internal()->mid().has_value()); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2691 | DestroyTransceiverChannel(transceiver); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2692 | |
| 2693 | if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer && |
| 2694 | transceiver->receiver()) { |
| 2695 | removed_receivers.push_back(transceiver->receiver()); |
| 2696 | } |
| 2697 | if (state.newly_created()) { |
| 2698 | if (transceiver->internal()->reused_for_addtrack()) { |
| 2699 | transceiver->internal()->set_created_by_addtrack(true); |
| 2700 | } else { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2701 | transceivers()->Remove(transceiver); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2702 | } |
| 2703 | } |
| 2704 | transceiver->internal()->sender_internal()->set_transport(nullptr); |
| 2705 | transceiver->internal()->receiver_internal()->set_transport(nullptr); |
| 2706 | transceiver->internal()->set_mid(state.mid()); |
| 2707 | transceiver->internal()->set_mline_index(state.mline_index()); |
| 2708 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2709 | transport_controller()->RollbackTransports(); |
Harald Alvestrand | 75b9ab6 | 2020-09-30 22:17:18 | [diff] [blame] | 2710 | if (have_pending_rtp_data_channel_) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2711 | DestroyDataChannelTransport(); |
Harald Alvestrand | 75b9ab6 | 2020-09-30 22:17:18 | [diff] [blame] | 2712 | have_pending_rtp_data_channel_ = false; |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2713 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2714 | transceivers()->DiscardStableStates(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2715 | pending_local_description_.reset(); |
| 2716 | pending_remote_description_.reset(); |
| 2717 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 2718 | |
| 2719 | // Once all processing has finished, fire off callbacks. |
| 2720 | for (const auto& receiver : removed_receivers) { |
| 2721 | pc_->Observer()->OnRemoveTrack(receiver); |
| 2722 | } |
| 2723 | for (const auto& stream : all_added_streams) { |
| 2724 | pc_->Observer()->OnAddStream(stream); |
| 2725 | } |
| 2726 | for (const auto& stream : all_removed_streams) { |
| 2727 | pc_->Observer()->OnRemoveStream(stream); |
| 2728 | } |
| 2729 | |
| 2730 | // The assumption is that in case of implicit rollback UpdateNegotiationNeeded |
| 2731 | // gets called in SetRemoteDescription. |
| 2732 | if (desc_type == SdpType::kRollback) { |
| 2733 | UpdateNegotiationNeeded(); |
| 2734 | if (is_negotiation_needed_) { |
| 2735 | // Legacy version. |
| 2736 | pc_->Observer()->OnRenegotiationNeeded(); |
| 2737 | // Spec-compliant version; the event may get invalidated before firing. |
| 2738 | GenerateNegotiationNeededEvent(); |
| 2739 | } |
| 2740 | } |
| 2741 | return RTCError::OK(); |
| 2742 | } |
| 2743 | |
| 2744 | bool SdpOfferAnswerHandler::IsUnifiedPlan() const { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2745 | return pc_->IsUnifiedPlan(); |
| 2746 | } |
| 2747 | |
| 2748 | void SdpOfferAnswerHandler::OnOperationsChainEmpty() { |
| 2749 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2750 | if (pc_->IsClosed() || !update_negotiation_needed_on_empty_chain_) |
| 2751 | return; |
| 2752 | update_negotiation_needed_on_empty_chain_ = false; |
| 2753 | // Firing when chain is empty is only supported in Unified Plan to avoid Plan |
| 2754 | // B regressions. (In Plan B, onnegotiationneeded is already broken anyway, so |
| 2755 | // firing it even more might just be confusing.) |
| 2756 | if (IsUnifiedPlan()) { |
| 2757 | UpdateNegotiationNeeded(); |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | absl::optional<bool> SdpOfferAnswerHandler::is_caller() { |
| 2762 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2763 | return is_caller_; |
| 2764 | } |
| 2765 | |
| 2766 | bool SdpOfferAnswerHandler::HasNewIceCredentials() { |
| 2767 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2768 | return local_ice_credentials_to_replace_->HasIceCredentials(); |
| 2769 | } |
| 2770 | |
| 2771 | bool SdpOfferAnswerHandler::IceRestartPending( |
| 2772 | const std::string& content_name) const { |
| 2773 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2774 | return pending_ice_restarts_.find(content_name) != |
| 2775 | pending_ice_restarts_.end(); |
| 2776 | } |
| 2777 | |
Harald Alvestrand | f01bd6c | 2020-10-23 13:30:46 | [diff] [blame] | 2778 | bool SdpOfferAnswerHandler::NeedsIceRestart( |
| 2779 | const std::string& content_name) const { |
| 2780 | return transport_controller()->NeedsIceRestart(content_name); |
| 2781 | } |
| 2782 | |
| 2783 | absl::optional<rtc::SSLRole> SdpOfferAnswerHandler::GetDtlsRole( |
| 2784 | const std::string& mid) const { |
| 2785 | return transport_controller()->GetDtlsRole(mid); |
| 2786 | } |
| 2787 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2788 | void SdpOfferAnswerHandler::UpdateNegotiationNeeded() { |
| 2789 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2790 | if (!IsUnifiedPlan()) { |
| 2791 | pc_->Observer()->OnRenegotiationNeeded(); |
| 2792 | GenerateNegotiationNeededEvent(); |
| 2793 | return; |
| 2794 | } |
| 2795 | |
| 2796 | // In the spec, a task is queued here to run the following steps - this is |
| 2797 | // meant to ensure we do not fire onnegotiationneeded prematurely if multiple |
| 2798 | // changes are being made at once. In order to support Chromium's |
| 2799 | // implementation where the JavaScript representation of the PeerConnection |
| 2800 | // lives on a separate thread though, the queuing of a task is instead |
| 2801 | // performed by the PeerConnectionObserver posting from the signaling thread |
| 2802 | // to the JavaScript main thread that negotiation is needed. And because the |
| 2803 | // Operations Chain lives on the WebRTC signaling thread, |
| 2804 | // ShouldFireNegotiationNeededEvent() must be called before firing the event |
| 2805 | // to ensure the Operations Chain is still empty and the event has not been |
| 2806 | // invalidated. |
| 2807 | |
| 2808 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 2809 | if (pc_->IsClosed()) |
| 2810 | return; |
| 2811 | |
| 2812 | // If connection's signaling state is not "stable", abort these steps. |
| 2813 | if (signaling_state() != PeerConnectionInterface::kStable) |
| 2814 | return; |
| 2815 | |
| 2816 | // NOTE |
| 2817 | // The negotiation-needed flag will be updated once the state transitions to |
| 2818 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 2819 | |
| 2820 | // If the result of checking if negotiation is needed is false, clear the |
| 2821 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 2822 | // to false, and abort these steps. |
| 2823 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 2824 | if (!is_negotiation_needed) { |
| 2825 | is_negotiation_needed_ = false; |
| 2826 | // Invalidate any negotiation needed event that may previosuly have been |
| 2827 | // generated. |
| 2828 | ++negotiation_needed_event_id_; |
| 2829 | return; |
| 2830 | } |
| 2831 | |
| 2832 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 2833 | // steps. |
| 2834 | if (is_negotiation_needed_) |
| 2835 | return; |
| 2836 | |
| 2837 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 2838 | is_negotiation_needed_ = true; |
| 2839 | |
| 2840 | // Queue a task that runs the following steps: |
| 2841 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 2842 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 2843 | // Fire an event named negotiationneeded at connection. |
| 2844 | pc_->Observer()->OnRenegotiationNeeded(); |
| 2845 | // Fire the spec-compliant version; when ShouldFireNegotiationNeededEvent() is |
| 2846 | // used in the task queued by the observer, this event will only fire when the |
| 2847 | // chain is empty. |
| 2848 | GenerateNegotiationNeededEvent(); |
| 2849 | } |
| 2850 | |
| 2851 | bool SdpOfferAnswerHandler::CheckIfNegotiationIsNeeded() { |
| 2852 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2853 | // 1. If any implementation-specific negotiation is required, as described at |
| 2854 | // the start of this section, return true. |
| 2855 | |
| 2856 | // 2. If connection.[[LocalIceCredentialsToReplace]] is not empty, return |
| 2857 | // true. |
| 2858 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 2859 | return true; |
| 2860 | } |
| 2861 | |
| 2862 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
| 2863 | const SessionDescriptionInterface* description = current_local_description(); |
| 2864 | if (!description) |
| 2865 | return true; |
| 2866 | |
| 2867 | // 4. If connection has created any RTCDataChannels, and no m= section in |
| 2868 | // description has been negotiated yet for data, return true. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 2869 | if (data_channel_controller()->HasSctpDataChannels()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2870 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 2871 | return true; |
| 2872 | } |
| 2873 | |
| 2874 | // 5. For each transceiver in connection's set of transceivers, perform the |
| 2875 | // following checks: |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 2876 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 2877 | const ContentInfo* current_local_msection = |
| 2878 | FindTransceiverMSection(transceiver.get(), description); |
| 2879 | |
| 2880 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 2881 | transceiver.get(), current_remote_description()); |
| 2882 | |
| 2883 | // 5.4 If transceiver is stopped and is associated with an m= section, |
| 2884 | // but the associated m= section is not yet rejected in |
| 2885 | // connection.[[CurrentLocalDescription]] or |
| 2886 | // connection.[[CurrentRemoteDescription]], return true. |
| 2887 | if (transceiver->stopped()) { |
| 2888 | RTC_DCHECK(transceiver->stopping()); |
| 2889 | if (current_local_msection && !current_local_msection->rejected && |
| 2890 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 2891 | !current_remote_msection)) { |
| 2892 | return true; |
| 2893 | } |
| 2894 | continue; |
| 2895 | } |
| 2896 | |
| 2897 | // 5.1 If transceiver.[[Stopping]] is true and transceiver.[[Stopped]] is |
| 2898 | // false, return true. |
| 2899 | if (transceiver->stopping() && !transceiver->stopped()) |
| 2900 | return true; |
| 2901 | |
| 2902 | // 5.2 If transceiver isn't stopped and isn't yet associated with an m= |
| 2903 | // section in description, return true. |
| 2904 | if (!current_local_msection) |
| 2905 | return true; |
| 2906 | |
| 2907 | const MediaContentDescription* current_local_media_description = |
| 2908 | current_local_msection->media_description(); |
| 2909 | // 5.3 If transceiver isn't stopped and is associated with an m= section |
| 2910 | // in description then perform the following checks: |
| 2911 | |
| 2912 | // 5.3.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
| 2913 | // associated m= section in description either doesn't contain a single |
| 2914 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 2915 | // m= section, or the MSID values themselves, differ from what is in |
| 2916 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 2917 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 2918 | if (current_local_media_description->streams().size() == 0) |
| 2919 | return true; |
| 2920 | |
| 2921 | std::vector<std::string> msection_msids; |
| 2922 | for (const auto& stream : current_local_media_description->streams()) { |
| 2923 | for (const std::string& msid : stream.stream_ids()) |
| 2924 | msection_msids.push_back(msid); |
| 2925 | } |
| 2926 | |
| 2927 | std::vector<std::string> transceiver_msids = |
| 2928 | transceiver->sender()->stream_ids(); |
| 2929 | if (msection_msids.size() != transceiver_msids.size()) |
| 2930 | return true; |
| 2931 | |
| 2932 | absl::c_sort(transceiver_msids); |
| 2933 | absl::c_sort(msection_msids); |
| 2934 | if (transceiver_msids != msection_msids) |
| 2935 | return true; |
| 2936 | } |
| 2937 | |
| 2938 | // 5.3.2 If description is of type "offer", and the direction of the |
| 2939 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 2940 | // nor connection.[[CurrentRemoteDescription]] matches |
| 2941 | // transceiver.[[Direction]], return true. |
| 2942 | if (description->GetType() == SdpType::kOffer) { |
| 2943 | if (!current_remote_description()) |
| 2944 | return true; |
| 2945 | |
| 2946 | if (!current_remote_msection) |
| 2947 | return true; |
| 2948 | |
| 2949 | RtpTransceiverDirection current_local_direction = |
| 2950 | current_local_media_description->direction(); |
| 2951 | RtpTransceiverDirection current_remote_direction = |
| 2952 | current_remote_msection->media_description()->direction(); |
| 2953 | if (transceiver->direction() != current_local_direction && |
| 2954 | transceiver->direction() != |
| 2955 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 2956 | return true; |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | // 5.3.3 If description is of type "answer", and the direction of the |
| 2961 | // associated m= section in the description does not match |
| 2962 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 2963 | // described in [JSEP] (section 5.3.1.)), return true. |
| 2964 | if (description->GetType() == SdpType::kAnswer) { |
| 2965 | if (!remote_description()) |
| 2966 | return true; |
| 2967 | |
| 2968 | const ContentInfo* offered_remote_msection = |
| 2969 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 2970 | |
| 2971 | RtpTransceiverDirection offered_direction = |
| 2972 | offered_remote_msection |
| 2973 | ? offered_remote_msection->media_description()->direction() |
| 2974 | : RtpTransceiverDirection::kInactive; |
| 2975 | |
| 2976 | if (current_local_media_description->direction() != |
| 2977 | (RtpTransceiverDirectionIntersection( |
| 2978 | transceiver->direction(), |
| 2979 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 2980 | return true; |
| 2981 | } |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | // If all the preceding checks were performed and true was not returned, |
| 2986 | // nothing remains to be negotiated; return false. |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
| 2990 | void SdpOfferAnswerHandler::GenerateNegotiationNeededEvent() { |
| 2991 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2992 | ++negotiation_needed_event_id_; |
| 2993 | pc_->Observer()->OnNegotiationNeededEvent(negotiation_needed_event_id_); |
| 2994 | } |
| 2995 | |
| 2996 | RTCError SdpOfferAnswerHandler::ValidateSessionDescription( |
| 2997 | const SessionDescriptionInterface* sdesc, |
| 2998 | cricket::ContentSource source) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 2999 | if (session_error() != SessionError::kNone) { |
| 3000 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | if (!sdesc || !sdesc->description()) { |
| 3004 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
| 3005 | } |
| 3006 | |
| 3007 | SdpType type = sdesc->GetType(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3008 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 3009 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3010 | LOG_AND_RETURN_ERROR( |
| 3011 | RTCErrorType::INVALID_STATE, |
| 3012 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
| 3013 | } |
| 3014 | |
| 3015 | RTCError error = ValidateMids(*sdesc->description()); |
| 3016 | if (!error.ok()) { |
| 3017 | return error; |
| 3018 | } |
| 3019 | |
| 3020 | // Verify crypto settings. |
| 3021 | std::string crypto_error; |
| 3022 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 3023 | pc_->dtls_enabled()) { |
| 3024 | RTCError crypto_error = |
| 3025 | VerifyCrypto(sdesc->description(), pc_->dtls_enabled()); |
| 3026 | if (!crypto_error.ok()) { |
| 3027 | return crypto_error; |
| 3028 | } |
| 3029 | } |
| 3030 | |
| 3031 | // Verify ice-ufrag and ice-pwd. |
| 3032 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
| 3033 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3034 | kSdpWithoutIceUfragPwd); |
| 3035 | } |
| 3036 | |
| 3037 | if (!pc_->ValidateBundleSettings(sdesc->description())) { |
| 3038 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3039 | kBundleWithoutRtcpMux); |
| 3040 | } |
| 3041 | |
| 3042 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 3043 | // m-lines that do not rtcp-mux enabled. |
| 3044 | |
| 3045 | // Verify m-lines in Answer when compared against Offer. |
| 3046 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 3047 | // With an answer we want to compare the new answer session description with |
| 3048 | // the offer's session description from the current negotiation. |
| 3049 | const cricket::SessionDescription* offer_desc = |
| 3050 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 3051 | : local_description()->description(); |
| 3052 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 3053 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 3054 | type)) { |
| 3055 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3056 | kMlineMismatchInAnswer); |
| 3057 | } |
| 3058 | } else { |
| 3059 | // The re-offers should respect the order of m= sections in current |
| 3060 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
| 3061 | // With a re-offer, either the current local or current remote descriptions |
| 3062 | // could be the most up to date, so we would like to check against both of |
| 3063 | // them if they exist. It could be the case that one of them has a 0 port |
| 3064 | // for a media section, but the other does not. This is important to check |
| 3065 | // against in the case that we are recycling an m= section. |
| 3066 | const cricket::SessionDescription* current_desc = nullptr; |
| 3067 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 3068 | if (local_description()) { |
| 3069 | current_desc = local_description()->description(); |
| 3070 | if (remote_description()) { |
| 3071 | secondary_current_desc = remote_description()->description(); |
| 3072 | } |
| 3073 | } else if (remote_description()) { |
| 3074 | current_desc = remote_description()->description(); |
| 3075 | } |
| 3076 | if (current_desc && |
| 3077 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 3078 | *sdesc->description(), type)) { |
| 3079 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3080 | kMlineMismatchInSubsequentOffer); |
| 3081 | } |
| 3082 | } |
| 3083 | |
| 3084 | if (IsUnifiedPlan()) { |
| 3085 | // Ensure that each audio and video media section has at most one |
| 3086 | // "StreamParams". This will return an error if receiving a session |
| 3087 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 3088 | // same type. With Unified Plan, there can only be at most one track per |
| 3089 | // media section. |
| 3090 | for (const ContentInfo& content : sdesc->description()->contents()) { |
| 3091 | const MediaContentDescription& desc = *content.media_description(); |
| 3092 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 3093 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 3094 | desc.streams().size() > 1u) { |
| 3095 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3096 | "Media section has more than one track specified " |
| 3097 | "with a=ssrc lines which is not supported with " |
| 3098 | "Unified Plan."); |
| 3099 | } |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | return RTCError::OK(); |
| 3104 | } |
| 3105 | |
| 3106 | RTCError SdpOfferAnswerHandler::UpdateTransceiversAndDataChannels( |
| 3107 | cricket::ContentSource source, |
| 3108 | const SessionDescriptionInterface& new_session, |
| 3109 | const SessionDescriptionInterface* old_local_description, |
| 3110 | const SessionDescriptionInterface* old_remote_description) { |
| 3111 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3112 | RTC_DCHECK(IsUnifiedPlan()); |
| 3113 | |
| 3114 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3115 | if (new_session.GetType() == SdpType::kOffer) { |
| 3116 | auto bundle_group_or_error = |
| 3117 | GetEarlyBundleGroup(*new_session.description()); |
| 3118 | if (!bundle_group_or_error.ok()) { |
| 3119 | return bundle_group_or_error.MoveError(); |
| 3120 | } |
| 3121 | bundle_group = bundle_group_or_error.MoveValue(); |
| 3122 | } |
| 3123 | |
| 3124 | const ContentInfos& new_contents = new_session.description()->contents(); |
| 3125 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3126 | const cricket::ContentInfo& new_content = new_contents[i]; |
| 3127 | cricket::MediaType media_type = new_content.media_description()->type(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3128 | mid_generator_.AddKnownId(new_content.name); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3129 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3130 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3131 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3132 | if (old_local_description && |
| 3133 | i < old_local_description->description()->contents().size()) { |
| 3134 | old_local_content = |
| 3135 | &old_local_description->description()->contents()[i]; |
| 3136 | } |
| 3137 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3138 | if (old_remote_description && |
| 3139 | i < old_remote_description->description()->contents().size()) { |
| 3140 | old_remote_content = |
| 3141 | &old_remote_description->description()->contents()[i]; |
| 3142 | } |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3143 | auto transceiver_or_error = |
| 3144 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3145 | old_local_content, old_remote_content); |
| 3146 | if (!transceiver_or_error.ok()) { |
Harald Alvestrand | 09bd9ba | 2020-10-09 08:13:30 | [diff] [blame] | 3147 | // In the case where a transceiver is rejected locally, we don't |
| 3148 | // expect to find a transceiver, but might find it in the case |
| 3149 | // where state is still "stopping", not "stopped". |
| 3150 | if (new_content.rejected) { |
| 3151 | continue; |
| 3152 | } |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3153 | return transceiver_or_error.MoveError(); |
| 3154 | } |
| 3155 | auto transceiver = transceiver_or_error.MoveValue(); |
| 3156 | RTCError error = |
| 3157 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3158 | if (!error.ok()) { |
| 3159 | return error; |
| 3160 | } |
| 3161 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
| 3162 | if (pc_->GetDataMid() && new_content.name != *(pc_->GetDataMid())) { |
| 3163 | // Ignore all but the first data section. |
| 3164 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3165 | << new_content.name; |
| 3166 | continue; |
| 3167 | } |
| 3168 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3169 | if (!error.ok()) { |
| 3170 | return error; |
| 3171 | } |
Philipp Hancke | 4e8c115 | 2020-10-13 10:43:15 | [diff] [blame] | 3172 | } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) { |
| 3173 | RTC_LOG(LS_INFO) << "Ignoring unsupported media type"; |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3174 | } else { |
| 3175 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3176 | "Unknown section type."); |
| 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | return RTCError::OK(); |
| 3181 | } |
| 3182 | |
| 3183 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3184 | SdpOfferAnswerHandler::AssociateTransceiver( |
| 3185 | cricket::ContentSource source, |
| 3186 | SdpType type, |
| 3187 | size_t mline_index, |
| 3188 | const ContentInfo& content, |
| 3189 | const ContentInfo* old_local_content, |
| 3190 | const ContentInfo* old_remote_content) { |
| 3191 | RTC_DCHECK(IsUnifiedPlan()); |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 3192 | #if RTC_DCHECK_IS_ON |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3193 | // If this is an offer then the m= section might be recycled. If the m= |
| 3194 | // section is being recycled (defined as: rejected in the current local or |
| 3195 | // remote description and not rejected in new description), the transceiver |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3196 | // should have been removed by RemoveStoppedtransceivers()-> |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3197 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3198 | old_remote_content)) { |
| 3199 | const std::string& old_mid = |
| 3200 | (old_local_content && old_local_content->rejected) |
| 3201 | ? old_local_content->name |
| 3202 | : old_remote_content->name; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3203 | auto old_transceiver = transceivers()->FindByMid(old_mid); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3204 | // The transceiver should be disassociated in RemoveStoppedTransceivers() |
| 3205 | RTC_DCHECK(!old_transceiver); |
| 3206 | } |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 3207 | #endif |
| 3208 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3209 | const MediaContentDescription* media_desc = content.media_description(); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3210 | auto transceiver = transceivers()->FindByMid(content.name); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3211 | if (source == cricket::CS_LOCAL) { |
| 3212 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3213 | // mapping between transceivers and m= section indices established when |
| 3214 | // creating the offer. |
| 3215 | if (!transceiver) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3216 | transceiver = transceivers()->FindByMLineIndex(mline_index); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3217 | } |
| 3218 | if (!transceiver) { |
Harald Alvestrand | 09bd9ba | 2020-10-09 08:13:30 | [diff] [blame] | 3219 | // This may happen normally when media sections are rejected. |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3220 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Harald Alvestrand | 09bd9ba | 2020-10-09 08:13:30 | [diff] [blame] | 3221 | "Transceiver not found based on m-line index"); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3222 | } |
| 3223 | } else { |
| 3224 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3225 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3226 | // of the same type... |
| 3227 | // When simulcast is requested, a transceiver cannot be associated because |
| 3228 | // AddTrack cannot be called to initialize it. |
| 3229 | if (!transceiver && |
| 3230 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3231 | !media_desc->HasSimulcast()) { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3232 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3233 | } |
| 3234 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3235 | // recvonly direction. |
| 3236 | if (!transceiver) { |
| 3237 | RTC_LOG(LS_INFO) << "Adding " |
| 3238 | << cricket::MediaTypeToString(media_desc->type()) |
| 3239 | << " transceiver for MID=" << content.name |
| 3240 | << " at i=" << mline_index |
| 3241 | << " in response to the remote description."; |
| 3242 | std::string sender_id = rtc::CreateRandomUuid(); |
| 3243 | std::vector<RtpEncodingParameters> send_encodings = |
| 3244 | GetSendEncodingsFromRemoteDescription(*media_desc); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3245 | auto sender = rtp_manager()->CreateSender(media_desc->type(), sender_id, |
| 3246 | nullptr, {}, send_encodings); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3247 | std::string receiver_id; |
| 3248 | if (!media_desc->streams().empty()) { |
| 3249 | receiver_id = media_desc->streams()[0].id; |
| 3250 | } else { |
| 3251 | receiver_id = rtc::CreateRandomUuid(); |
| 3252 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3253 | auto receiver = |
| 3254 | rtp_manager()->CreateReceiver(media_desc->type(), receiver_id); |
| 3255 | transceiver = rtp_manager()->CreateAndAddTransceiver(sender, receiver); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3256 | transceiver->internal()->set_direction( |
| 3257 | RtpTransceiverDirection::kRecvOnly); |
| 3258 | if (type == SdpType::kOffer) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3259 | transceivers()->StableState(transceiver)->set_newly_created(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3260 | } |
| 3261 | } |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 3262 | |
| 3263 | RTC_DCHECK(transceiver); |
| 3264 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3265 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3266 | // This can happen when simulcast is not supported on the remote party. |
| 3267 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
| 3268 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
| 3269 | RTCError error = |
| 3270 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
| 3271 | if (!error.ok()) { |
| 3272 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3273 | return std::move(error); |
| 3274 | } |
| 3275 | } |
| 3276 | } |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 3277 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3278 | if (transceiver->media_type() != media_desc->type()) { |
| 3279 | LOG_AND_RETURN_ERROR( |
| 3280 | RTCErrorType::INVALID_PARAMETER, |
| 3281 | "Transceiver type does not match media description type."); |
| 3282 | } |
Tomas Gunnarsson | 3699236 | 2020-10-05 19:41:36 | [diff] [blame] | 3283 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3284 | if (media_desc->HasSimulcast()) { |
| 3285 | std::vector<SimulcastLayer> layers = |
| 3286 | source == cricket::CS_LOCAL |
| 3287 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3288 | : media_desc->simulcast_description() |
| 3289 | .receive_layers() |
| 3290 | .GetAllLayers(); |
| 3291 | RTCError error = UpdateSimulcastLayerStatusInSender( |
| 3292 | layers, transceiver->internal()->sender_internal()); |
| 3293 | if (!error.ok()) { |
| 3294 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3295 | return std::move(error); |
| 3296 | } |
| 3297 | } |
| 3298 | if (type == SdpType::kOffer) { |
| 3299 | bool state_changes = transceiver->internal()->mid() != content.name || |
| 3300 | transceiver->internal()->mline_index() != mline_index; |
| 3301 | if (state_changes) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3302 | transceivers() |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3303 | ->StableState(transceiver) |
Harald Alvestrand | 38b768c | 2020-09-29 11:54:05 | [diff] [blame] | 3304 | ->SetMSectionIfUnset(transceiver->internal()->mid(), |
| 3305 | transceiver->internal()->mline_index()); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3306 | } |
| 3307 | } |
| 3308 | // Associate the found or created RtpTransceiver with the m= section by |
| 3309 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3310 | // section, and establish a mapping between the transceiver and the index of |
| 3311 | // the m= section. |
| 3312 | transceiver->internal()->set_mid(content.name); |
| 3313 | transceiver->internal()->set_mline_index(mline_index); |
| 3314 | return std::move(transceiver); |
| 3315 | } |
| 3316 | |
| 3317 | RTCErrorOr<const cricket::ContentGroup*> |
| 3318 | SdpOfferAnswerHandler::GetEarlyBundleGroup( |
| 3319 | const SessionDescription& desc) const { |
| 3320 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3321 | if (pc_->configuration()->bundle_policy == |
| 3322 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
| 3323 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 3324 | if (!bundle_group) { |
| 3325 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3326 | "max-bundle configured but session description " |
| 3327 | "has no BUNDLE group"); |
| 3328 | } |
| 3329 | } |
| 3330 | return bundle_group; |
| 3331 | } |
| 3332 | |
| 3333 | RTCError SdpOfferAnswerHandler::UpdateTransceiverChannel( |
| 3334 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3335 | transceiver, |
| 3336 | const cricket::ContentInfo& content, |
| 3337 | const cricket::ContentGroup* bundle_group) { |
| 3338 | RTC_DCHECK(IsUnifiedPlan()); |
| 3339 | RTC_DCHECK(transceiver); |
| 3340 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 3341 | if (content.rejected) { |
| 3342 | if (channel) { |
| 3343 | transceiver->internal()->SetChannel(nullptr); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3344 | DestroyChannelInterface(channel); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3345 | } |
| 3346 | } else { |
| 3347 | if (!channel) { |
| 3348 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3349 | channel = CreateVoiceChannel(content.name); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3350 | } else { |
| 3351 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3352 | channel = CreateVideoChannel(content.name); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3353 | } |
| 3354 | if (!channel) { |
| 3355 | LOG_AND_RETURN_ERROR( |
| 3356 | RTCErrorType::INTERNAL_ERROR, |
| 3357 | "Failed to create channel for mid=" + content.name); |
| 3358 | } |
| 3359 | transceiver->internal()->SetChannel(channel); |
| 3360 | } |
| 3361 | } |
| 3362 | return RTCError::OK(); |
| 3363 | } |
| 3364 | |
| 3365 | RTCError SdpOfferAnswerHandler::UpdateDataChannel( |
| 3366 | cricket::ContentSource source, |
| 3367 | const cricket::ContentInfo& content, |
| 3368 | const cricket::ContentGroup* bundle_group) { |
| 3369 | if (pc_->data_channel_type() == cricket::DCT_NONE) { |
| 3370 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3371 | // will take care of rejecting it. |
| 3372 | return RTCError::OK(); |
| 3373 | } |
| 3374 | if (content.rejected) { |
| 3375 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3376 | DestroyDataChannelTransport(); |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3377 | } else { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3378 | if (!data_channel_controller()->rtp_data_channel() && |
| 3379 | !data_channel_controller()->data_channel_transport()) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3380 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3381 | if (!CreateDataChannel(content.name)) { |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3382 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3383 | "Failed to create data channel."); |
| 3384 | } |
| 3385 | } |
| 3386 | if (source == cricket::CS_REMOTE) { |
| 3387 | const MediaContentDescription* data_desc = content.media_description(); |
| 3388 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3389 | data_channel_controller()->UpdateRemoteRtpDataChannels( |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 3390 | GetActiveStreams(data_desc)); |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | return RTCError::OK(); |
| 3395 | } |
| 3396 | |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3397 | bool SdpOfferAnswerHandler::ExpectSetLocalDescription(SdpType type) { |
| 3398 | PeerConnectionInterface::SignalingState state = signaling_state(); |
| 3399 | if (type == SdpType::kOffer) { |
| 3400 | return (state == PeerConnectionInterface::kStable) || |
| 3401 | (state == PeerConnectionInterface::kHaveLocalOffer); |
| 3402 | } else { |
| 3403 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
| 3404 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 3405 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | bool SdpOfferAnswerHandler::ExpectSetRemoteDescription(SdpType type) { |
| 3410 | PeerConnectionInterface::SignalingState state = signaling_state(); |
| 3411 | if (type == SdpType::kOffer) { |
| 3412 | return (state == PeerConnectionInterface::kStable) || |
| 3413 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
| 3414 | } else { |
| 3415 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
| 3416 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 3417 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | void SdpOfferAnswerHandler::FillInMissingRemoteMids( |
| 3422 | cricket::SessionDescription* new_remote_description) { |
| 3423 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3424 | RTC_DCHECK(new_remote_description); |
| 3425 | const cricket::ContentInfos no_infos; |
| 3426 | const cricket::ContentInfos& local_contents = |
| 3427 | (local_description() ? local_description()->description()->contents() |
| 3428 | : no_infos); |
| 3429 | const cricket::ContentInfos& remote_contents = |
| 3430 | (remote_description() ? remote_description()->description()->contents() |
| 3431 | : no_infos); |
| 3432 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 3433 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
| 3434 | if (!content.name.empty()) { |
| 3435 | continue; |
| 3436 | } |
| 3437 | std::string new_mid; |
| 3438 | absl::string_view source_explanation; |
| 3439 | if (IsUnifiedPlan()) { |
| 3440 | if (i < local_contents.size()) { |
| 3441 | new_mid = local_contents[i].name; |
| 3442 | source_explanation = "from the matching local media section"; |
| 3443 | } else if (i < remote_contents.size()) { |
| 3444 | new_mid = remote_contents[i].name; |
| 3445 | source_explanation = "from the matching previous remote media section"; |
| 3446 | } else { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3447 | new_mid = mid_generator_.GenerateString(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3448 | source_explanation = "generated just now"; |
| 3449 | } |
| 3450 | } else { |
| 3451 | new_mid = std::string( |
| 3452 | GetDefaultMidForPlanB(content.media_description()->type())); |
| 3453 | source_explanation = "to match pre-existing behavior"; |
| 3454 | } |
| 3455 | RTC_DCHECK(!new_mid.empty()); |
| 3456 | content.name = new_mid; |
| 3457 | new_remote_description->transport_infos()[i].content_name = new_mid; |
| 3458 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 3459 | << " is missing an a=mid line. Filling in the value '" |
| 3460 | << new_mid << "' " << source_explanation << "."; |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3465 | SdpOfferAnswerHandler::FindAvailableTransceiverToReceive( |
| 3466 | cricket::MediaType media_type) const { |
| 3467 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3468 | RTC_DCHECK(IsUnifiedPlan()); |
| 3469 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3470 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3471 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3472 | // associated with any m= section and are not stopped, find the first such |
| 3473 | // RtpTransceiver. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3474 | for (auto transceiver : transceivers()->List()) { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3475 | if (transceiver->media_type() == media_type && |
| 3476 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3477 | !transceiver->stopped()) { |
| 3478 | return transceiver; |
| 3479 | } |
| 3480 | } |
| 3481 | return nullptr; |
| 3482 | } |
| 3483 | |
| 3484 | const cricket::ContentInfo* |
| 3485 | SdpOfferAnswerHandler::FindMediaSectionForTransceiver( |
| 3486 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3487 | transceiver, |
| 3488 | const SessionDescriptionInterface* sdesc) const { |
| 3489 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3490 | RTC_DCHECK(transceiver); |
| 3491 | RTC_DCHECK(sdesc); |
| 3492 | if (IsUnifiedPlan()) { |
| 3493 | if (!transceiver->internal()->mid()) { |
| 3494 | // This transceiver is not associated with a media section yet. |
| 3495 | return nullptr; |
| 3496 | } |
| 3497 | return sdesc->description()->GetContentByName( |
| 3498 | *transceiver->internal()->mid()); |
| 3499 | } else { |
| 3500 | // Plan B only allows at most one audio and one video section, so use the |
| 3501 | // first media section of that type. |
| 3502 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
| 3503 | transceiver->media_type()); |
| 3504 | } |
| 3505 | } |
| 3506 | |
| 3507 | void SdpOfferAnswerHandler::GetOptionsForOffer( |
| 3508 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3509 | cricket::MediaSessionOptions* session_options) { |
| 3510 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3511 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3512 | |
| 3513 | if (IsUnifiedPlan()) { |
| 3514 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3515 | } else { |
| 3516 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3517 | } |
| 3518 | |
| 3519 | // Intentionally unset the data channel type for RTP data channel with the |
| 3520 | // second condition. Otherwise the RTP data channels would be successfully |
| 3521 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3522 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3523 | // people won't try to use them. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3524 | if (data_channel_controller()->HasRtpDataChannels() || |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3525 | pc_->data_channel_type() != cricket::DCT_RTP) { |
| 3526 | session_options->data_channel_type = pc_->data_channel_type(); |
| 3527 | } |
| 3528 | |
| 3529 | // Apply ICE restart flag and renomination flag. |
| 3530 | bool ice_restart = offer_answer_options.ice_restart || HasNewIceCredentials(); |
| 3531 | for (auto& options : session_options->media_description_options) { |
| 3532 | options.transport_options.ice_restart = ice_restart; |
| 3533 | options.transport_options.enable_ice_renomination = |
| 3534 | pc_->configuration()->enable_ice_renomination; |
| 3535 | } |
| 3536 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3537 | session_options->rtcp_cname = rtcp_cname_; |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3538 | session_options->crypto_options = pc_->GetCryptoOptions(); |
| 3539 | session_options->pooled_ice_credentials = |
| 3540 | pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 3541 | RTC_FROM_HERE, |
| 3542 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3543 | port_allocator())); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3544 | session_options->offer_extmap_allow_mixed = |
| 3545 | pc_->configuration()->offer_extmap_allow_mixed; |
| 3546 | |
| 3547 | // Allow fallback for using obsolete SCTP syntax. |
| 3548 | // Note that the default in |session_options| is true, while |
| 3549 | // the default in |options| is false. |
| 3550 | session_options->use_obsolete_sctp_sdp = |
| 3551 | offer_answer_options.use_obsolete_sctp_sdp; |
| 3552 | } |
| 3553 | |
| 3554 | void SdpOfferAnswerHandler::GetOptionsForPlanBOffer( |
| 3555 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3556 | cricket::MediaSessionOptions* session_options) { |
| 3557 | // Figure out transceiver directional preferences. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3558 | bool send_audio = |
| 3559 | !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty(); |
| 3560 | bool send_video = |
| 3561 | !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3562 | |
| 3563 | // By default, generate sendrecv/recvonly m= sections. |
| 3564 | bool recv_audio = true; |
| 3565 | bool recv_video = true; |
| 3566 | |
| 3567 | // By default, only offer a new m= section if we have media to send with it. |
| 3568 | bool offer_new_audio_description = send_audio; |
| 3569 | bool offer_new_video_description = send_video; |
| 3570 | bool offer_new_data_description = |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3571 | data_channel_controller()->HasDataChannels(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3572 | |
| 3573 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 3574 | if (offer_answer_options.offer_to_receive_audio != |
| 3575 | PeerConnectionInterface::RTCOfferAnswerOptions::kUndefined) { |
| 3576 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
| 3577 | offer_new_audio_description = |
| 3578 | offer_new_audio_description || |
| 3579 | (offer_answer_options.offer_to_receive_audio > 0); |
| 3580 | } |
| 3581 | if (offer_answer_options.offer_to_receive_video != |
| 3582 | RTCOfferAnswerOptions::kUndefined) { |
| 3583 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
| 3584 | offer_new_video_description = |
| 3585 | offer_new_video_description || |
| 3586 | (offer_answer_options.offer_to_receive_video > 0); |
| 3587 | } |
| 3588 | |
| 3589 | absl::optional<size_t> audio_index; |
| 3590 | absl::optional<size_t> video_index; |
| 3591 | absl::optional<size_t> data_index; |
| 3592 | // If a current description exists, generate m= sections in the same order, |
| 3593 | // using the first audio/video/data section that appears and rejecting |
| 3594 | // extraneous ones. |
| 3595 | if (local_description()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3596 | GenerateMediaDescriptionOptions( |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3597 | local_description(), |
| 3598 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3599 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3600 | &audio_index, &video_index, &data_index, session_options); |
| 3601 | } |
| 3602 | |
| 3603 | // Add audio/video/data m= sections to the end if needed. |
| 3604 | if (!audio_index && offer_new_audio_description) { |
| 3605 | cricket::MediaDescriptionOptions options( |
| 3606 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
| 3607 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false); |
| 3608 | options.header_extensions = |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3609 | channel_manager()->GetSupportedAudioRtpHeaderExtensions(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3610 | session_options->media_description_options.push_back(options); |
| 3611 | audio_index = session_options->media_description_options.size() - 1; |
| 3612 | } |
| 3613 | if (!video_index && offer_new_video_description) { |
| 3614 | cricket::MediaDescriptionOptions options( |
| 3615 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
| 3616 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false); |
| 3617 | options.header_extensions = |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3618 | channel_manager()->GetSupportedVideoRtpHeaderExtensions(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3619 | session_options->media_description_options.push_back(options); |
| 3620 | video_index = session_options->media_description_options.size() - 1; |
| 3621 | } |
| 3622 | if (!data_index && offer_new_data_description) { |
| 3623 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3624 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3625 | data_index = session_options->media_description_options.size() - 1; |
| 3626 | } |
| 3627 | |
| 3628 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3629 | !audio_index ? nullptr |
| 3630 | : &session_options->media_description_options[*audio_index]; |
| 3631 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3632 | !video_index ? nullptr |
| 3633 | : &session_options->media_description_options[*video_index]; |
| 3634 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3635 | AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(), |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3636 | audio_media_description_options, |
| 3637 | video_media_description_options, |
| 3638 | offer_answer_options.num_simulcast_layers); |
| 3639 | } |
| 3640 | |
| 3641 | void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanOffer( |
| 3642 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3643 | cricket::MediaSessionOptions* session_options) { |
| 3644 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3645 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3646 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3647 | const ContentInfos no_infos; |
| 3648 | const ContentInfos& local_contents = |
| 3649 | (local_description() ? local_description()->description()->contents() |
| 3650 | : no_infos); |
| 3651 | const ContentInfos& remote_contents = |
| 3652 | (remote_description() ? remote_description()->description()->contents() |
| 3653 | : no_infos); |
| 3654 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3655 | // slots first. |
| 3656 | std::queue<size_t> recycleable_mline_indices; |
| 3657 | // First, go through each media section that exists in either the local or |
| 3658 | // remote description and generate a media section in this offer for the |
| 3659 | // associated transceiver. If a media section can be recycled, generate a |
| 3660 | // default, rejected media section here that can be later overwritten. |
| 3661 | for (size_t i = 0; |
| 3662 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3663 | // Either |local_content| or |remote_content| is non-null. |
| 3664 | const ContentInfo* local_content = |
| 3665 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3666 | const ContentInfo* current_local_content = |
| 3667 | GetContentByIndex(current_local_description(), i); |
| 3668 | const ContentInfo* remote_content = |
| 3669 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3670 | const ContentInfo* current_remote_content = |
| 3671 | GetContentByIndex(current_remote_description(), i); |
| 3672 | bool had_been_rejected = |
| 3673 | (current_local_content && current_local_content->rejected) || |
| 3674 | (current_remote_content && current_remote_content->rejected); |
| 3675 | const std::string& mid = |
| 3676 | (local_content ? local_content->name : remote_content->name); |
| 3677 | cricket::MediaType media_type = |
| 3678 | (local_content ? local_content->media_description()->type() |
| 3679 | : remote_content->media_description()->type()); |
| 3680 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3681 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3682 | // A media section is considered eligible for recycling if it is marked as |
| 3683 | // rejected in either the current local or current remote description. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3684 | auto transceiver = transceivers()->FindByMid(mid); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3685 | if (!transceiver) { |
| 3686 | // No associated transceiver. The media section has been stopped. |
| 3687 | recycleable_mline_indices.push(i); |
| 3688 | session_options->media_description_options.push_back( |
| 3689 | cricket::MediaDescriptionOptions(media_type, mid, |
| 3690 | RtpTransceiverDirection::kInactive, |
| 3691 | /*stopped=*/true)); |
| 3692 | } else { |
| 3693 | // NOTE: a stopping transceiver should be treated as a stopped one in |
| 3694 | // createOffer as specified in |
| 3695 | // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer. |
| 3696 | if (had_been_rejected && transceiver->stopping()) { |
| 3697 | session_options->media_description_options.push_back( |
| 3698 | cricket::MediaDescriptionOptions( |
| 3699 | transceiver->media_type(), mid, |
| 3700 | RtpTransceiverDirection::kInactive, |
| 3701 | /*stopped=*/true)); |
| 3702 | recycleable_mline_indices.push(i); |
| 3703 | } else { |
| 3704 | session_options->media_description_options.push_back( |
| 3705 | GetMediaDescriptionOptionsForTransceiver( |
| 3706 | transceiver, mid, |
| 3707 | /*is_create_offer=*/true)); |
| 3708 | // CreateOffer shouldn't really cause any state changes in |
| 3709 | // PeerConnection, but we need a way to match new transceivers to new |
| 3710 | // media sections in SetLocalDescription and JSEP specifies this is |
| 3711 | // done by recording the index of the media section generated for the |
| 3712 | // transceiver in the offer. |
| 3713 | transceiver->internal()->set_mline_index(i); |
| 3714 | } |
| 3715 | } |
Philipp Hancke | 4e8c115 | 2020-10-13 10:43:15 | [diff] [blame] | 3716 | } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) { |
| 3717 | RTC_DCHECK(local_content->rejected); |
| 3718 | session_options->media_description_options.push_back( |
| 3719 | cricket::MediaDescriptionOptions(media_type, mid, |
| 3720 | RtpTransceiverDirection::kInactive, |
| 3721 | /*stopped=*/true)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3722 | } else { |
| 3723 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
| 3724 | if (had_been_rejected) { |
| 3725 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3726 | GetMediaDescriptionOptionsForRejectedData(mid)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3727 | } else { |
| 3728 | RTC_CHECK(pc_->GetDataMid()); |
| 3729 | if (mid == *(pc_->GetDataMid())) { |
| 3730 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3731 | GetMediaDescriptionOptionsForActiveData(mid)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3732 | } else { |
| 3733 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3734 | GetMediaDescriptionOptionsForRejectedData(mid)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3741 | // and not associated). Reuse media sections marked as recyclable first, |
| 3742 | // otherwise append to the end of the offer. New media sections should be |
| 3743 | // added in the order they were added to the PeerConnection. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3744 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3745 | if (transceiver->mid() || transceiver->stopping()) { |
| 3746 | continue; |
| 3747 | } |
| 3748 | size_t mline_index; |
| 3749 | if (!recycleable_mline_indices.empty()) { |
| 3750 | mline_index = recycleable_mline_indices.front(); |
| 3751 | recycleable_mline_indices.pop(); |
| 3752 | session_options->media_description_options[mline_index] = |
| 3753 | GetMediaDescriptionOptionsForTransceiver( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3754 | transceiver, mid_generator_.GenerateString(), |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3755 | /*is_create_offer=*/true); |
| 3756 | } else { |
| 3757 | mline_index = session_options->media_description_options.size(); |
| 3758 | session_options->media_description_options.push_back( |
| 3759 | GetMediaDescriptionOptionsForTransceiver( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3760 | transceiver, mid_generator_.GenerateString(), |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3761 | /*is_create_offer=*/true)); |
| 3762 | } |
| 3763 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3764 | transceiver->internal()->set_mline_index(mline_index); |
| 3765 | } |
| 3766 | // Lastly, add a m-section if we have local data channels and an m section |
| 3767 | // does not already exist. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3768 | if (!pc_->GetDataMid() && data_channel_controller()->HasDataChannels()) { |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3769 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3770 | GetMediaDescriptionOptionsForActiveData( |
| 3771 | mid_generator_.GenerateString())); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | void SdpOfferAnswerHandler::GetOptionsForAnswer( |
| 3776 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3777 | cricket::MediaSessionOptions* session_options) { |
| 3778 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3779 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3780 | |
| 3781 | if (IsUnifiedPlan()) { |
| 3782 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3783 | } else { |
| 3784 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3785 | } |
| 3786 | |
| 3787 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3788 | // the RTP data channels would be successfully negotiated by default and the |
| 3789 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3790 | // We want to leave RTP data channels broken, so people won't try to use them. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3791 | if (data_channel_controller()->HasRtpDataChannels() || |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3792 | pc_->data_channel_type() != cricket::DCT_RTP) { |
| 3793 | session_options->data_channel_type = pc_->data_channel_type(); |
| 3794 | } |
| 3795 | |
| 3796 | // Apply ICE renomination flag. |
| 3797 | for (auto& options : session_options->media_description_options) { |
| 3798 | options.transport_options.enable_ice_renomination = |
| 3799 | pc_->configuration()->enable_ice_renomination; |
| 3800 | } |
| 3801 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3802 | session_options->rtcp_cname = rtcp_cname_; |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3803 | session_options->crypto_options = pc_->GetCryptoOptions(); |
| 3804 | session_options->pooled_ice_credentials = |
| 3805 | pc_->network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 3806 | RTC_FROM_HERE, |
| 3807 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3808 | port_allocator())); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | void SdpOfferAnswerHandler::GetOptionsForPlanBAnswer( |
| 3812 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3813 | cricket::MediaSessionOptions* session_options) { |
| 3814 | // Figure out transceiver directional preferences. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3815 | bool send_audio = |
| 3816 | !rtp_manager()->GetAudioTransceiver()->internal()->senders().empty(); |
| 3817 | bool send_video = |
| 3818 | !rtp_manager()->GetVideoTransceiver()->internal()->senders().empty(); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3819 | |
| 3820 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3821 | // restricted by the direction in the offer. |
| 3822 | bool recv_audio = true; |
| 3823 | bool recv_video = true; |
| 3824 | |
| 3825 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
| 3826 | if (offer_answer_options.offer_to_receive_audio != |
| 3827 | RTCOfferAnswerOptions::kUndefined) { |
| 3828 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
| 3829 | } |
| 3830 | if (offer_answer_options.offer_to_receive_video != |
| 3831 | RTCOfferAnswerOptions::kUndefined) { |
| 3832 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
| 3833 | } |
| 3834 | |
| 3835 | absl::optional<size_t> audio_index; |
| 3836 | absl::optional<size_t> video_index; |
| 3837 | absl::optional<size_t> data_index; |
| 3838 | |
| 3839 | // Generate m= sections that match those in the offer. |
| 3840 | // Note that mediasession.cc will handle intersection our preferred |
| 3841 | // direction with the offered direction. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3842 | GenerateMediaDescriptionOptions( |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3843 | remote_description(), |
| 3844 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3845 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3846 | &video_index, &data_index, session_options); |
| 3847 | |
| 3848 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3849 | !audio_index ? nullptr |
| 3850 | : &session_options->media_description_options[*audio_index]; |
| 3851 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3852 | !video_index ? nullptr |
| 3853 | : &session_options->media_description_options[*video_index]; |
| 3854 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3855 | AddPlanBRtpSenderOptions(rtp_manager()->GetSendersInternal(), |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3856 | audio_media_description_options, |
| 3857 | video_media_description_options, |
| 3858 | offer_answer_options.num_simulcast_layers); |
| 3859 | } |
| 3860 | |
| 3861 | void SdpOfferAnswerHandler::GetOptionsForUnifiedPlanAnswer( |
| 3862 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3863 | cricket::MediaSessionOptions* session_options) { |
| 3864 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3865 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3866 | RTC_DCHECK(remote_description()); |
| 3867 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3868 | for (const ContentInfo& content : |
| 3869 | remote_description()->description()->contents()) { |
| 3870 | cricket::MediaType media_type = content.media_description()->type(); |
| 3871 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3872 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 3873 | auto transceiver = transceivers()->FindByMid(content.name); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3874 | if (transceiver) { |
| 3875 | session_options->media_description_options.push_back( |
| 3876 | GetMediaDescriptionOptionsForTransceiver( |
| 3877 | transceiver, content.name, |
| 3878 | /*is_create_offer=*/false)); |
| 3879 | } else { |
| 3880 | // This should only happen with rejected transceivers. |
| 3881 | RTC_DCHECK(content.rejected); |
| 3882 | session_options->media_description_options.push_back( |
| 3883 | cricket::MediaDescriptionOptions(media_type, content.name, |
| 3884 | RtpTransceiverDirection::kInactive, |
| 3885 | /*stopped=*/true)); |
| 3886 | } |
Philipp Hancke | 4e8c115 | 2020-10-13 10:43:15 | [diff] [blame] | 3887 | } else if (media_type == cricket::MEDIA_TYPE_UNSUPPORTED) { |
| 3888 | RTC_DCHECK(content.rejected); |
| 3889 | session_options->media_description_options.push_back( |
| 3890 | cricket::MediaDescriptionOptions(media_type, content.name, |
| 3891 | RtpTransceiverDirection::kInactive, |
| 3892 | /*stopped=*/true)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3893 | } else { |
| 3894 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
| 3895 | // Reject all data sections if data channels are disabled. |
| 3896 | // Reject a data section if it has already been rejected. |
| 3897 | // Reject all data sections except for the first one. |
| 3898 | if (pc_->data_channel_type() == cricket::DCT_NONE || content.rejected || |
| 3899 | content.name != *(pc_->GetDataMid())) { |
| 3900 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3901 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3902 | } else { |
| 3903 | session_options->media_description_options.push_back( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 3904 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Harald Alvestrand | c06e374 | 2020-10-01 10:23:33 | [diff] [blame] | 3905 | } |
| 3906 | } |
| 3907 | } |
| 3908 | } |
| 3909 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 3910 | const char* SdpOfferAnswerHandler::SessionErrorToString( |
| 3911 | SessionError error) const { |
| 3912 | switch (error) { |
| 3913 | case SessionError::kNone: |
| 3914 | return "ERROR_NONE"; |
| 3915 | case SessionError::kContent: |
| 3916 | return "ERROR_CONTENT"; |
| 3917 | case SessionError::kTransport: |
| 3918 | return "ERROR_TRANSPORT"; |
| 3919 | } |
| 3920 | RTC_NOTREACHED(); |
| 3921 | return ""; |
| 3922 | } |
| 3923 | |
| 3924 | std::string SdpOfferAnswerHandler::GetSessionErrorMsg() { |
| 3925 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3926 | rtc::StringBuilder desc; |
| 3927 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 3928 | desc << kSessionErrorDesc << session_error_desc() << "."; |
| 3929 | return desc.Release(); |
| 3930 | } |
| 3931 | |
| 3932 | void SdpOfferAnswerHandler::SetSessionError(SessionError error, |
| 3933 | const std::string& error_desc) { |
| 3934 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3935 | if (error != session_error_) { |
| 3936 | session_error_ = error; |
| 3937 | session_error_desc_ = error_desc; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | RTCError SdpOfferAnswerHandler::HandleLegacyOfferOptions( |
| 3942 | const PeerConnectionInterface::RTCOfferAnswerOptions& options) { |
| 3943 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3944 | RTC_DCHECK(IsUnifiedPlan()); |
| 3945 | |
| 3946 | if (options.offer_to_receive_audio == 0) { |
| 3947 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 3948 | cricket::MEDIA_TYPE_AUDIO); |
| 3949 | } else if (options.offer_to_receive_audio == 1) { |
| 3950 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 3951 | } else if (options.offer_to_receive_audio > 1) { |
| 3952 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 3953 | "offer_to_receive_audio > 1 is not supported."); |
| 3954 | } |
| 3955 | |
| 3956 | if (options.offer_to_receive_video == 0) { |
| 3957 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 3958 | cricket::MEDIA_TYPE_VIDEO); |
| 3959 | } else if (options.offer_to_receive_video == 1) { |
| 3960 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 3961 | } else if (options.offer_to_receive_video > 1) { |
| 3962 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 3963 | "offer_to_receive_video > 1 is not supported."); |
| 3964 | } |
| 3965 | |
| 3966 | return RTCError::OK(); |
| 3967 | } |
| 3968 | |
| 3969 | void SdpOfferAnswerHandler::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 3970 | cricket::MediaType media_type) { |
| 3971 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 3972 | RtpTransceiverDirection new_direction = |
| 3973 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 3974 | if (new_direction != transceiver->direction()) { |
| 3975 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 3976 | << " transceiver (MID=" |
| 3977 | << transceiver->mid().value_or("<not set>") << ") from " |
| 3978 | << RtpTransceiverDirectionToString( |
| 3979 | transceiver->direction()) |
| 3980 | << " to " |
| 3981 | << RtpTransceiverDirectionToString(new_direction) |
| 3982 | << " since CreateOffer specified offer_to_receive=0"; |
| 3983 | transceiver->internal()->set_direction(new_direction); |
| 3984 | } |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | void SdpOfferAnswerHandler::AddUpToOneReceivingTransceiverOfType( |
| 3989 | cricket::MediaType media_type) { |
| 3990 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3991 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 3992 | RTC_LOG(LS_INFO) |
| 3993 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 3994 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
| 3995 | RtpTransceiverInit init; |
| 3996 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 3997 | pc_->AddTransceiver(media_type, nullptr, init, |
| 3998 | /*update_negotiation_needed=*/false); |
| 3999 | } |
| 4000 | } |
| 4001 | |
| 4002 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 4003 | SdpOfferAnswerHandler::GetReceivingTransceiversOfType( |
| 4004 | cricket::MediaType media_type) { |
| 4005 | std::vector< |
| 4006 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 4007 | receiving_transceivers; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4008 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4009 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
| 4010 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 4011 | receiving_transceivers.push_back(transceiver); |
| 4012 | } |
| 4013 | } |
| 4014 | return receiving_transceivers; |
| 4015 | } |
| 4016 | |
| 4017 | void SdpOfferAnswerHandler::ProcessRemovalOfRemoteTrack( |
| 4018 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4019 | transceiver, |
| 4020 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 4021 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 4022 | RTC_DCHECK(transceiver->mid()); |
| 4023 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 4024 | << *transceiver->mid(); |
| 4025 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 4026 | transceiver->internal()->receiver_internal()->streams(); |
| 4027 | // This will remove the remote track from the streams. |
| 4028 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 4029 | remove_list->push_back(transceiver); |
| 4030 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 4031 | } |
| 4032 | |
| 4033 | void SdpOfferAnswerHandler::RemoveRemoteStreamsIfEmpty( |
| 4034 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 4035 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 4036 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4037 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 4038 | // streams, see if the stream was removed by checking if this was the last |
| 4039 | // receiver with that stream ID. |
| 4040 | for (const auto& remote_stream : remote_streams) { |
| 4041 | if (remote_stream->GetAudioTracks().empty() && |
| 4042 | remote_stream->GetVideoTracks().empty()) { |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4043 | remote_streams_->RemoveStream(remote_stream); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4044 | removed_streams->push_back(remote_stream); |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | |
| 4049 | void SdpOfferAnswerHandler::RemoveSenders(cricket::MediaType media_type) { |
| 4050 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4051 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 4052 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
| 4053 | media_type, nullptr); |
| 4054 | } |
| 4055 | |
| 4056 | void SdpOfferAnswerHandler::UpdateLocalSenders( |
| 4057 | const std::vector<cricket::StreamParams>& streams, |
| 4058 | cricket::MediaType media_type) { |
| 4059 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4060 | std::vector<RtpSenderInfo>* current_senders = |
| 4061 | rtp_manager()->GetLocalSenderInfos(media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4062 | |
| 4063 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
| 4064 | // don't match the new StreamParam. |
| 4065 | for (auto sender_it = current_senders->begin(); |
| 4066 | sender_it != current_senders->end(); |
| 4067 | /* incremented manually */) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4068 | const RtpSenderInfo& info = *sender_it; |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4069 | const cricket::StreamParams* params = |
| 4070 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4071 | if (!params || params->id != info.sender_id || |
| 4072 | params->first_stream_id() != info.stream_id) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4073 | rtp_manager()->OnLocalSenderRemoved(info, media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4074 | sender_it = current_senders->erase(sender_it); |
| 4075 | } else { |
| 4076 | ++sender_it; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | // Find new and active senders. |
| 4081 | for (const cricket::StreamParams& params : streams) { |
| 4082 | // The sync_label is the MediaStream label and the |stream.id| is the |
| 4083 | // sender id. |
| 4084 | const std::string& stream_id = params.first_stream_id(); |
| 4085 | const std::string& sender_id = params.id; |
| 4086 | uint32_t ssrc = params.first_ssrc(); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4087 | const RtpSenderInfo* sender_info = |
| 4088 | rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4089 | if (!sender_info) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4090 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
| 4091 | rtp_manager()->OnLocalSenderAdded(current_senders->back(), media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | void SdpOfferAnswerHandler::UpdateRemoteSendersList( |
| 4097 | const cricket::StreamParamsVec& streams, |
| 4098 | bool default_sender_needed, |
| 4099 | cricket::MediaType media_type, |
| 4100 | StreamCollection* new_streams) { |
| 4101 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4102 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4103 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4104 | std::vector<RtpSenderInfo>* current_senders = |
| 4105 | rtp_manager()->GetRemoteSenderInfos(media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4106 | |
| 4107 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
| 4108 | // the new StreamParam. |
| 4109 | for (auto sender_it = current_senders->begin(); |
| 4110 | sender_it != current_senders->end(); |
| 4111 | /* incremented manually */) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4112 | const RtpSenderInfo& info = *sender_it; |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4113 | const cricket::StreamParams* params = |
| 4114 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 4115 | std::string params_stream_id; |
| 4116 | if (params) { |
| 4117 | params_stream_id = |
| 4118 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 4119 | : kDefaultStreamId); |
| 4120 | } |
| 4121 | bool sender_exists = params && params->id == info.sender_id && |
| 4122 | params_stream_id == info.stream_id; |
| 4123 | // If this is a default track, and we still need it, don't remove it. |
| 4124 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
| 4125 | sender_exists) { |
| 4126 | ++sender_it; |
| 4127 | } else { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4128 | rtp_manager()->OnRemoteSenderRemoved( |
| 4129 | info, remote_streams_->find(info.stream_id), media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4130 | sender_it = current_senders->erase(sender_it); |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | // Find new and active senders. |
| 4135 | for (const cricket::StreamParams& params : streams) { |
| 4136 | if (!params.has_ssrcs()) { |
| 4137 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 4138 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 4139 | // create a default. |
| 4140 | default_sender_needed = true; |
| 4141 | break; |
| 4142 | } |
| 4143 | |
| 4144 | // |params.id| is the sender id and the stream id uses the first of |
| 4145 | // |params.stream_ids|. The remote description could come from a Unified |
| 4146 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 4147 | // not supported in Plan B, we just take the first here and create the |
| 4148 | // default stream ID if none is specified. |
| 4149 | const std::string& stream_id = |
| 4150 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 4151 | : kDefaultStreamId); |
| 4152 | const std::string& sender_id = params.id; |
| 4153 | uint32_t ssrc = params.first_ssrc(); |
| 4154 | |
| 4155 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4156 | remote_streams_->find(stream_id); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4157 | if (!stream) { |
| 4158 | // This is a new MediaStream. Create a new remote MediaStream. |
| 4159 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 4160 | MediaStream::Create(stream_id)); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4161 | remote_streams_->AddStream(stream); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4162 | new_streams->AddStream(stream); |
| 4163 | } |
| 4164 | |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4165 | const RtpSenderInfo* sender_info = |
| 4166 | rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4167 | if (!sender_info) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4168 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
| 4169 | rtp_manager()->OnRemoteSenderAdded(current_senders->back(), stream, |
| 4170 | media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | // Add default sender if necessary. |
| 4175 | if (default_sender_needed) { |
| 4176 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4177 | remote_streams_->find(kDefaultStreamId); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4178 | if (!default_stream) { |
| 4179 | // Create the new default MediaStream. |
| 4180 | default_stream = MediaStreamProxy::Create( |
| 4181 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4182 | remote_streams_->AddStream(default_stream); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4183 | new_streams->AddStream(default_stream); |
| 4184 | } |
| 4185 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4186 | ? kDefaultAudioSenderId |
| 4187 | : kDefaultVideoSenderId; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4188 | const RtpSenderInfo* default_sender_info = rtp_manager()->FindSenderInfo( |
| 4189 | *current_senders, kDefaultStreamId, default_sender_id); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4190 | if (!default_sender_info) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4191 | current_senders->push_back( |
| 4192 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
| 4193 | rtp_manager()->OnRemoteSenderAdded(current_senders->back(), |
| 4194 | default_stream, media_type); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4199 | RTCError SdpOfferAnswerHandler::PushdownMediaDescription( |
| 4200 | SdpType type, |
| 4201 | cricket::ContentSource source) { |
| 4202 | const SessionDescriptionInterface* sdesc = |
| 4203 | (source == cricket::CS_LOCAL ? local_description() |
| 4204 | : remote_description()); |
| 4205 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4206 | RTC_DCHECK(sdesc); |
| 4207 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4208 | // Gather lists of updates to be made on cricket channels on the signaling |
| 4209 | // thread, before performing them all at once on the worker thread. Necessary |
| 4210 | // due to threading restrictions. |
| 4211 | auto payload_type_demuxing_updates = GetPayloadTypeDemuxingUpdates(source); |
| 4212 | std::vector<ContentUpdate> content_updates; |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4213 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4214 | // Collect updates for each audio/video transceiver. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4215 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4216 | const ContentInfo* content_info = |
| 4217 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4218 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 4219 | if (!channel || !content_info || content_info->rejected) { |
| 4220 | continue; |
| 4221 | } |
| 4222 | const MediaContentDescription* content_desc = |
| 4223 | content_info->media_description(); |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4224 | if (content_desc) { |
| 4225 | content_updates.emplace_back(channel, content_desc); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4229 | // If using the RtpDataChannel, add it to the list of updates. |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4230 | if (data_channel_controller()->rtp_data_channel()) { |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4231 | const ContentInfo* data_content = |
| 4232 | cricket::GetFirstDataContent(sdesc->description()); |
| 4233 | if (data_content && !data_content->rejected) { |
| 4234 | const MediaContentDescription* data_desc = |
| 4235 | data_content->media_description(); |
| 4236 | if (data_desc) { |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4237 | content_updates.push_back( |
| 4238 | {data_channel_controller()->rtp_data_channel(), data_desc}); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | } |
| 4242 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4243 | RTCError error = pc_->worker_thread()->Invoke<RTCError>( |
| 4244 | RTC_FROM_HERE, |
| 4245 | rtc::Bind(&SdpOfferAnswerHandler::ApplyChannelUpdates, this, type, source, |
| 4246 | std::move(payload_type_demuxing_updates), |
| 4247 | std::move(content_updates))); |
| 4248 | if (!error.ok()) { |
| 4249 | return error; |
| 4250 | } |
| 4251 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4252 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4253 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4254 | if (pc_->sctp_mid() && local_description() && remote_description()) { |
| 4255 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4256 | transport_controller()->GetSctpTransport(*(pc_->sctp_mid())); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4257 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 4258 | local_description()->description()); |
| 4259 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 4260 | remote_description()->description()); |
| 4261 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
| 4262 | int max_message_size; |
| 4263 | // A remote max message size of zero means "any size supported". |
| 4264 | // We configure the connection with our own max message size. |
| 4265 | if (remote_sctp_description->max_message_size() == 0) { |
| 4266 | max_message_size = local_sctp_description->max_message_size(); |
| 4267 | } else { |
| 4268 | max_message_size = |
| 4269 | std::min(local_sctp_description->max_message_size(), |
| 4270 | remote_sctp_description->max_message_size()); |
| 4271 | } |
| 4272 | sctp_transport->Start(local_sctp_description->port(), |
| 4273 | remote_sctp_description->port(), max_message_size); |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | return RTCError::OK(); |
| 4278 | } |
| 4279 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4280 | RTCError SdpOfferAnswerHandler::ApplyChannelUpdates( |
| 4281 | SdpType type, |
| 4282 | cricket::ContentSource source, |
| 4283 | std::vector<PayloadTypeDemuxingUpdate> payload_type_demuxing_updates, |
| 4284 | std::vector<ContentUpdate> content_updates) { |
| 4285 | RTC_DCHECK_RUN_ON(pc_->worker_thread()); |
| 4286 | // If this is answer-ish we're ready to let media flow. |
| 4287 | bool enable_sending = type == SdpType::kPrAnswer || type == SdpType::kAnswer; |
| 4288 | std::set<cricket::ChannelInterface*> modified_channels; |
| 4289 | for (const auto& update : payload_type_demuxing_updates) { |
| 4290 | modified_channels.insert(update.channel); |
| 4291 | update.channel->SetPayloadTypeDemuxingEnabled(update.enabled); |
| 4292 | } |
| 4293 | for (const auto& update : content_updates) { |
| 4294 | modified_channels.insert(update.channel); |
| 4295 | std::string error; |
| 4296 | bool success = (source == cricket::CS_LOCAL) |
| 4297 | ? update.channel->SetLocalContent( |
| 4298 | update.content_description, type, &error) |
| 4299 | : update.channel->SetRemoteContent( |
| 4300 | update.content_description, type, &error); |
| 4301 | if (!success) { |
| 4302 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
| 4303 | } |
| 4304 | if (enable_sending && !update.channel->enabled()) { |
| 4305 | update.channel->Enable(true); |
| 4306 | } |
| 4307 | } |
| 4308 | // The above calls may have modified properties of the channel (header |
| 4309 | // extension mappings, demuxer criteria) which still need to be applied to the |
| 4310 | // RtpTransport. |
| 4311 | return pc_->network_thread()->Invoke<RTCError>( |
| 4312 | RTC_FROM_HERE, [modified_channels] { |
| 4313 | for (auto channel : modified_channels) { |
| 4314 | std::string error; |
| 4315 | if (!channel->UpdateRtpTransport(&error)) { |
| 4316 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
| 4317 | } |
| 4318 | } |
| 4319 | return RTCError::OK(); |
| 4320 | }); |
| 4321 | } |
| 4322 | |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4323 | RTCError SdpOfferAnswerHandler::PushdownTransportDescription( |
| 4324 | cricket::ContentSource source, |
| 4325 | SdpType type) { |
| 4326 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4327 | |
| 4328 | if (source == cricket::CS_LOCAL) { |
| 4329 | const SessionDescriptionInterface* sdesc = local_description(); |
| 4330 | RTC_DCHECK(sdesc); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4331 | return transport_controller()->SetLocalDescription(type, |
| 4332 | sdesc->description()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4333 | } else { |
| 4334 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 4335 | RTC_DCHECK(sdesc); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4336 | return transport_controller()->SetRemoteDescription(type, |
| 4337 | sdesc->description()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | void SdpOfferAnswerHandler::RemoveStoppedTransceivers() { |
| 4342 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4343 | // 3.2.10.1: For each transceiver in the connection's set of transceivers |
| 4344 | // run the following steps: |
| 4345 | if (!IsUnifiedPlan()) |
| 4346 | return; |
| 4347 | // Traverse a copy of the transceiver list. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4348 | auto transceiver_list = transceivers()->List(); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4349 | for (auto transceiver : transceiver_list) { |
| 4350 | // 3.2.10.1.1: If transceiver is stopped, associated with an m= section |
| 4351 | // and the associated m= section is rejected in |
| 4352 | // connection.[[CurrentLocalDescription]] or |
| 4353 | // connection.[[CurrentRemoteDescription]], remove the |
| 4354 | // transceiver from the connection's set of transceivers. |
| 4355 | if (!transceiver->stopped()) { |
| 4356 | continue; |
| 4357 | } |
| 4358 | const ContentInfo* local_content = |
| 4359 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 4360 | const ContentInfo* remote_content = |
| 4361 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 4362 | if ((local_content && local_content->rejected) || |
| 4363 | (remote_content && remote_content->rejected)) { |
| 4364 | RTC_LOG(LS_INFO) << "Dissociating transceiver" |
| 4365 | << " since the media section is being recycled."; |
| 4366 | transceiver->internal()->set_mid(absl::nullopt); |
| 4367 | transceiver->internal()->set_mline_index(absl::nullopt); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4368 | transceivers()->Remove(transceiver); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4369 | continue; |
| 4370 | } |
| 4371 | if (!local_content && !remote_content) { |
| 4372 | // TODO(bugs.webrtc.org/11973): Consider if this should be removed already |
| 4373 | // See https://github.com/w3c/webrtc-pc/issues/2576 |
| 4374 | RTC_LOG(LS_INFO) |
| 4375 | << "Dropping stopped transceiver that was never associated"; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4376 | transceivers()->Remove(transceiver); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4377 | continue; |
| 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
| 4382 | void SdpOfferAnswerHandler::RemoveUnusedChannels( |
| 4383 | const SessionDescription* desc) { |
| 4384 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4385 | // Destroy video channel first since it may have a pointer to the |
| 4386 | // voice channel. |
| 4387 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
| 4388 | if (!video_info || video_info->rejected) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4389 | DestroyTransceiverChannel(rtp_manager()->GetVideoTransceiver()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 4393 | if (!audio_info || audio_info->rejected) { |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4394 | DestroyTransceiverChannel(rtp_manager()->GetAudioTransceiver()); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 4398 | if (!data_info || data_info->rejected) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4399 | DestroyDataChannelTransport(); |
Harald Alvestrand | a474fbf | 2020-10-01 16:47:23 | [diff] [blame] | 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | void SdpOfferAnswerHandler::ReportNegotiatedSdpSemantics( |
| 4404 | const SessionDescriptionInterface& answer) { |
| 4405 | SdpSemanticNegotiated semantics_negotiated; |
| 4406 | switch (answer.description()->msid_signaling()) { |
| 4407 | case 0: |
| 4408 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
| 4409 | break; |
| 4410 | case cricket::kMsidSignalingMediaSection: |
| 4411 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
| 4412 | break; |
| 4413 | case cricket::kMsidSignalingSsrcAttribute: |
| 4414 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
| 4415 | break; |
| 4416 | case cricket::kMsidSignalingMediaSection | |
| 4417 | cricket::kMsidSignalingSsrcAttribute: |
| 4418 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
| 4419 | break; |
| 4420 | default: |
| 4421 | RTC_NOTREACHED(); |
| 4422 | } |
| 4423 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 4424 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
| 4425 | } |
| 4426 | |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4427 | void SdpOfferAnswerHandler::UpdateEndedRemoteMediaStreams() { |
| 4428 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4429 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4430 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 4431 | MediaStreamInterface* stream = remote_streams_->at(i); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4432 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 4433 | streams_to_remove.push_back(stream); |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | for (auto& stream : streams_to_remove) { |
Harald Alvestrand | 6f04b653 | 2020-10-09 11:42:17 | [diff] [blame] | 4438 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4439 | pc_->Observer()->OnRemoveStream(std::move(stream)); |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | bool SdpOfferAnswerHandler::UseCandidatesInSessionDescription( |
| 4444 | const SessionDescriptionInterface* remote_desc) { |
| 4445 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4446 | if (!remote_desc) { |
| 4447 | return true; |
| 4448 | } |
| 4449 | bool ret = true; |
| 4450 | |
| 4451 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 4452 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 4453 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 4454 | const IceCandidateInterface* candidate = candidates->at(n); |
| 4455 | bool valid = false; |
| 4456 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 4457 | if (valid) { |
| 4458 | RTC_LOG(LS_INFO) |
| 4459 | << "UseCandidatesInSessionDescription: Not ready to use " |
| 4460 | "candidate."; |
| 4461 | } |
| 4462 | continue; |
| 4463 | } |
| 4464 | ret = UseCandidate(candidate); |
| 4465 | if (!ret) { |
| 4466 | break; |
| 4467 | } |
| 4468 | } |
| 4469 | } |
| 4470 | return ret; |
| 4471 | } |
| 4472 | |
| 4473 | bool SdpOfferAnswerHandler::UseCandidate( |
| 4474 | const IceCandidateInterface* candidate) { |
| 4475 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4476 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 4477 | FindContentInfo(remote_description(), candidate); |
| 4478 | if (!result.ok()) { |
| 4479 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 4480 | << result.error().message(); |
| 4481 | return false; |
| 4482 | } |
| 4483 | std::vector<cricket::Candidate> candidates; |
| 4484 | candidates.push_back(candidate->candidate()); |
| 4485 | // Invoking BaseSession method to handle remote candidates. |
| 4486 | RTCError error = transport_controller()->AddRemoteCandidates( |
| 4487 | result.value()->name, candidates); |
| 4488 | if (error.ok()) { |
| 4489 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
| 4490 | // Candidates successfully submitted for checking. |
| 4491 | if (pc_->ice_connection_state() == |
| 4492 | PeerConnectionInterface::kIceConnectionNew || |
| 4493 | pc_->ice_connection_state() == |
| 4494 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 4495 | // If state is New, then the session has just gotten its first remote ICE |
| 4496 | // candidates, so go to Checking. |
| 4497 | // If state is Disconnected, the session is re-using old candidates or |
| 4498 | // receiving additional ones, so go to Checking. |
| 4499 | // If state is Connected, stay Connected. |
| 4500 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 4501 | // newly added transport, then the state actually _should_ move to |
| 4502 | // checking. Add a way to distinguish that case. |
| 4503 | pc_->SetIceConnectionState( |
| 4504 | PeerConnectionInterface::kIceConnectionChecking); |
| 4505 | } |
| 4506 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 4507 | } else { |
| 4508 | RTC_LOG(LS_WARNING) << error.message(); |
| 4509 | } |
| 4510 | return true; |
| 4511 | } |
| 4512 | |
| 4513 | // We need to check the local/remote description for the Transport instead of |
| 4514 | // the session, because a new Transport added during renegotiation may have |
| 4515 | // them unset while the session has them set from the previous negotiation. |
| 4516 | // Not doing so may trigger the auto generation of transport description and |
| 4517 | // mess up DTLS identity information, ICE credential, etc. |
| 4518 | bool SdpOfferAnswerHandler::ReadyToUseRemoteCandidate( |
| 4519 | const IceCandidateInterface* candidate, |
| 4520 | const SessionDescriptionInterface* remote_desc, |
| 4521 | bool* valid) { |
| 4522 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4523 | *valid = true; |
| 4524 | |
| 4525 | const SessionDescriptionInterface* current_remote_desc = |
| 4526 | remote_desc ? remote_desc : remote_description(); |
| 4527 | |
| 4528 | if (!current_remote_desc) { |
| 4529 | return false; |
| 4530 | } |
| 4531 | |
| 4532 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 4533 | FindContentInfo(current_remote_desc, candidate); |
| 4534 | if (!result.ok()) { |
| 4535 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 4536 | << result.error().message(); |
| 4537 | |
| 4538 | *valid = false; |
| 4539 | return false; |
| 4540 | } |
| 4541 | |
| 4542 | std::string transport_name = GetTransportName(result.value()->name); |
| 4543 | return !transport_name.empty(); |
| 4544 | } |
| 4545 | |
| 4546 | void SdpOfferAnswerHandler::ReportRemoteIceCandidateAdded( |
| 4547 | const cricket::Candidate& candidate) { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 4548 | pc_->NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4549 | if (candidate.address().IsPrivateIP()) { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 4550 | pc_->NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4551 | } |
| 4552 | if (candidate.address().IsUnresolvedIP()) { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 4553 | pc_->NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4554 | } |
| 4555 | if (candidate.address().family() == AF_INET6) { |
Harald Alvestrand | 44d0dff | 2020-10-09 05:43:53 | [diff] [blame] | 4556 | pc_->NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4557 | } |
| 4558 | } |
| 4559 | |
| 4560 | RTCErrorOr<const cricket::ContentInfo*> SdpOfferAnswerHandler::FindContentInfo( |
| 4561 | const SessionDescriptionInterface* description, |
| 4562 | const IceCandidateInterface* candidate) { |
| 4563 | if (candidate->sdp_mline_index() >= 0) { |
| 4564 | size_t mediacontent_index = |
| 4565 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 4566 | size_t content_size = description->description()->contents().size(); |
| 4567 | if (mediacontent_index < content_size) { |
| 4568 | return &description->description()->contents()[mediacontent_index]; |
| 4569 | } else { |
| 4570 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 4571 | "Media line index (" + |
| 4572 | rtc::ToString(candidate->sdp_mline_index()) + |
| 4573 | ") out of range (number of mlines: " + |
| 4574 | rtc::ToString(content_size) + ")."); |
| 4575 | } |
| 4576 | } else if (!candidate->sdp_mid().empty()) { |
| 4577 | auto& contents = description->description()->contents(); |
| 4578 | auto it = absl::c_find_if( |
| 4579 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 4580 | return content_info.mid() == candidate->sdp_mid(); |
| 4581 | }); |
| 4582 | if (it == contents.end()) { |
| 4583 | return RTCError( |
| 4584 | RTCErrorType::INVALID_PARAMETER, |
| 4585 | "Mid " + candidate->sdp_mid() + |
| 4586 | " specified but no media section with that mid found."); |
| 4587 | } else { |
| 4588 | return &*it; |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 4593 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 4594 | } |
| 4595 | |
| 4596 | RTCError SdpOfferAnswerHandler::CreateChannels(const SessionDescription& desc) { |
| 4597 | // Creating the media channels. Transports should already have been created |
| 4598 | // at this point. |
| 4599 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4600 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
| 4601 | if (voice && !voice->rejected && |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4602 | !rtp_manager()->GetAudioTransceiver()->internal()->channel()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4603 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
| 4604 | if (!voice_channel) { |
| 4605 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4606 | "Failed to create voice channel."); |
| 4607 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4608 | rtp_manager()->GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
| 4612 | if (video && !video->rejected && |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4613 | !rtp_manager()->GetVideoTransceiver()->internal()->channel()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4614 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
| 4615 | if (!video_channel) { |
| 4616 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4617 | "Failed to create video channel."); |
| 4618 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4619 | rtp_manager()->GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
| 4623 | if (pc_->data_channel_type() != cricket::DCT_NONE && data && |
| 4624 | !data->rejected && !data_channel_controller()->rtp_data_channel() && |
| 4625 | !data_channel_controller()->data_channel_transport()) { |
| 4626 | if (!CreateDataChannel(data->name)) { |
| 4627 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4628 | "Failed to create data channel."); |
| 4629 | } |
| 4630 | } |
| 4631 | |
| 4632 | return RTCError::OK(); |
| 4633 | } |
| 4634 | |
| 4635 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
| 4636 | cricket::VoiceChannel* SdpOfferAnswerHandler::CreateVoiceChannel( |
| 4637 | const std::string& mid) { |
| 4638 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4639 | RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid); |
| 4640 | |
| 4641 | // TODO(bugs.webrtc.org/11992): CreateVoiceChannel internally switches to the |
| 4642 | // worker thread. We shouldn't be using the |call_ptr_| hack here but simply |
| 4643 | // be on the worker thread and use |call_| (update upstream code). |
| 4644 | cricket::VoiceChannel* voice_channel; |
| 4645 | { |
| 4646 | RTC_DCHECK_RUN_ON(pc_->signaling_thread()); |
| 4647 | voice_channel = channel_manager()->CreateVoiceChannel( |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 4648 | pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4649 | signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(), |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 4650 | &ssrc_generator_, audio_options()); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4651 | } |
| 4652 | if (!voice_channel) { |
| 4653 | return nullptr; |
| 4654 | } |
| 4655 | voice_channel->SignalSentPacket().connect(pc_, |
| 4656 | &PeerConnection::OnSentPacket_w); |
| 4657 | voice_channel->SetRtpTransport(rtp_transport); |
| 4658 | |
| 4659 | return voice_channel; |
| 4660 | } |
| 4661 | |
| 4662 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
| 4663 | cricket::VideoChannel* SdpOfferAnswerHandler::CreateVideoChannel( |
| 4664 | const std::string& mid) { |
| 4665 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4666 | RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid); |
| 4667 | |
| 4668 | // TODO(bugs.webrtc.org/11992): CreateVideoChannel internally switches to the |
| 4669 | // worker thread. We shouldn't be using the |call_ptr_| hack here but simply |
| 4670 | // be on the worker thread and use |call_| (update upstream code). |
| 4671 | cricket::VideoChannel* video_channel; |
| 4672 | { |
| 4673 | RTC_DCHECK_RUN_ON(pc_->signaling_thread()); |
| 4674 | video_channel = channel_manager()->CreateVideoChannel( |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 4675 | pc_->call_ptr(), pc_->configuration()->media_config, rtp_transport, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4676 | signaling_thread(), mid, pc_->SrtpRequired(), pc_->GetCryptoOptions(), |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 4677 | &ssrc_generator_, video_options(), |
| 4678 | video_bitrate_allocator_factory_.get()); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4679 | } |
| 4680 | if (!video_channel) { |
| 4681 | return nullptr; |
| 4682 | } |
| 4683 | video_channel->SignalSentPacket().connect(pc_, |
| 4684 | &PeerConnection::OnSentPacket_w); |
| 4685 | video_channel->SetRtpTransport(rtp_transport); |
| 4686 | |
| 4687 | return video_channel; |
| 4688 | } |
| 4689 | |
| 4690 | bool SdpOfferAnswerHandler::CreateDataChannel(const std::string& mid) { |
| 4691 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4692 | switch (pc_->data_channel_type()) { |
| 4693 | case cricket::DCT_SCTP: |
| 4694 | if (pc_->network_thread()->Invoke<bool>( |
| 4695 | RTC_FROM_HERE, |
| 4696 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, pc_, |
| 4697 | mid))) { |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 4698 | pc_->SetSctpDataMid(mid); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4699 | } else { |
| 4700 | return false; |
| 4701 | } |
| 4702 | return true; |
| 4703 | case cricket::DCT_RTP: |
| 4704 | default: |
| 4705 | RtpTransportInternal* rtp_transport = pc_->GetRtpTransport(mid); |
| 4706 | // TODO(bugs.webrtc.org/9987): set_rtp_data_channel() should be called on |
| 4707 | // the network thread like set_data_channel_transport is. |
| 4708 | { |
| 4709 | RTC_DCHECK_RUN_ON(pc_->signaling_thread()); |
| 4710 | data_channel_controller()->set_rtp_data_channel( |
| 4711 | channel_manager()->CreateRtpDataChannel( |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 4712 | pc_->configuration()->media_config, rtp_transport, |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4713 | signaling_thread(), mid, pc_->SrtpRequired(), |
Harald Alvestrand | 763f5a9 | 2020-10-22 10:39:40 | [diff] [blame] | 4714 | pc_->GetCryptoOptions(), &ssrc_generator_)); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4715 | } |
| 4716 | if (!data_channel_controller()->rtp_data_channel()) { |
| 4717 | return false; |
| 4718 | } |
| 4719 | data_channel_controller()->rtp_data_channel()->SignalSentPacket().connect( |
| 4720 | pc_, &PeerConnection::OnSentPacket_w); |
| 4721 | data_channel_controller()->rtp_data_channel()->SetRtpTransport( |
| 4722 | rtp_transport); |
| 4723 | SetHavePendingRtpDataChannel(); |
| 4724 | return true; |
| 4725 | } |
| 4726 | return false; |
| 4727 | } |
| 4728 | |
| 4729 | void SdpOfferAnswerHandler::DestroyTransceiverChannel( |
| 4730 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4731 | transceiver) { |
| 4732 | RTC_DCHECK(transceiver); |
| 4733 | |
| 4734 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 4735 | if (channel) { |
| 4736 | transceiver->internal()->SetChannel(nullptr); |
| 4737 | DestroyChannelInterface(channel); |
| 4738 | } |
| 4739 | } |
| 4740 | |
| 4741 | void SdpOfferAnswerHandler::DestroyDataChannelTransport() { |
| 4742 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4743 | if (data_channel_controller()->rtp_data_channel()) { |
| 4744 | data_channel_controller()->OnTransportChannelClosed(); |
| 4745 | DestroyChannelInterface(data_channel_controller()->rtp_data_channel()); |
| 4746 | data_channel_controller()->set_rtp_data_channel(nullptr); |
| 4747 | } |
| 4748 | |
| 4749 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 4750 | // grab a reference to this PeerConnection. If this is called from the |
| 4751 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 4752 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 4753 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 4754 | |
| 4755 | if (pc_->sctp_mid()) { |
| 4756 | RTC_DCHECK_RUN_ON(pc_->signaling_thread()); |
| 4757 | data_channel_controller()->OnTransportChannelClosed(); |
| 4758 | pc_->network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 4759 | RTC_DCHECK_RUN_ON(pc_->network_thread()); |
| 4760 | pc_->TeardownDataChannelTransport_n(); |
| 4761 | }); |
Harald Alvestrand | 653429c | 2020-10-19 16:05:20 | [diff] [blame] | 4762 | pc_->ResetSctpDataMid(); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4763 | } |
| 4764 | } |
| 4765 | |
| 4766 | void SdpOfferAnswerHandler::DestroyChannelInterface( |
| 4767 | cricket::ChannelInterface* channel) { |
| 4768 | // TODO(bugs.webrtc.org/11992): All the below methods should be called on the |
| 4769 | // worker thread. (they switch internally anyway). Change |
| 4770 | // DestroyChannelInterface to either be called on the worker thread, or do |
| 4771 | // this asynchronously on the worker. |
| 4772 | RTC_DCHECK(channel); |
| 4773 | switch (channel->media_type()) { |
| 4774 | case cricket::MEDIA_TYPE_AUDIO: |
| 4775 | channel_manager()->DestroyVoiceChannel( |
| 4776 | static_cast<cricket::VoiceChannel*>(channel)); |
| 4777 | break; |
| 4778 | case cricket::MEDIA_TYPE_VIDEO: |
| 4779 | channel_manager()->DestroyVideoChannel( |
| 4780 | static_cast<cricket::VideoChannel*>(channel)); |
| 4781 | break; |
| 4782 | case cricket::MEDIA_TYPE_DATA: |
| 4783 | channel_manager()->DestroyRtpDataChannel( |
| 4784 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 4785 | break; |
| 4786 | default: |
| 4787 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 4788 | break; |
| 4789 | } |
| 4790 | } |
| 4791 | |
| 4792 | void SdpOfferAnswerHandler::DestroyAllChannels() { |
| 4793 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4794 | if (!transceivers()) { |
| 4795 | return; |
| 4796 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4797 | // Destroy video channels first since they may have a pointer to a voice |
| 4798 | // channel. |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4799 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4800 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
| 4801 | DestroyTransceiverChannel(transceiver); |
| 4802 | } |
| 4803 | } |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4804 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4805 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4806 | DestroyTransceiverChannel(transceiver); |
| 4807 | } |
| 4808 | } |
| 4809 | DestroyDataChannelTransport(); |
| 4810 | } |
| 4811 | |
| 4812 | void SdpOfferAnswerHandler::GenerateMediaDescriptionOptions( |
| 4813 | const SessionDescriptionInterface* session_desc, |
| 4814 | RtpTransceiverDirection audio_direction, |
| 4815 | RtpTransceiverDirection video_direction, |
| 4816 | absl::optional<size_t>* audio_index, |
| 4817 | absl::optional<size_t>* video_index, |
| 4818 | absl::optional<size_t>* data_index, |
| 4819 | cricket::MediaSessionOptions* session_options) { |
| 4820 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4821 | for (const cricket::ContentInfo& content : |
| 4822 | session_desc->description()->contents()) { |
| 4823 | if (IsAudioContent(&content)) { |
| 4824 | // If we already have an audio m= section, reject this extra one. |
| 4825 | if (*audio_index) { |
| 4826 | session_options->media_description_options.push_back( |
| 4827 | cricket::MediaDescriptionOptions( |
| 4828 | cricket::MEDIA_TYPE_AUDIO, content.name, |
| 4829 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
| 4830 | } else { |
| 4831 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
| 4832 | session_options->media_description_options.push_back( |
| 4833 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 4834 | content.name, audio_direction, |
| 4835 | stopped)); |
| 4836 | *audio_index = session_options->media_description_options.size() - 1; |
| 4837 | } |
| 4838 | session_options->media_description_options.back().header_extensions = |
| 4839 | channel_manager()->GetSupportedAudioRtpHeaderExtensions(); |
| 4840 | } else if (IsVideoContent(&content)) { |
| 4841 | // If we already have an video m= section, reject this extra one. |
| 4842 | if (*video_index) { |
| 4843 | session_options->media_description_options.push_back( |
| 4844 | cricket::MediaDescriptionOptions( |
| 4845 | cricket::MEDIA_TYPE_VIDEO, content.name, |
| 4846 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
| 4847 | } else { |
| 4848 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
| 4849 | session_options->media_description_options.push_back( |
| 4850 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 4851 | content.name, video_direction, |
| 4852 | stopped)); |
| 4853 | *video_index = session_options->media_description_options.size() - 1; |
| 4854 | } |
| 4855 | session_options->media_description_options.back().header_extensions = |
| 4856 | channel_manager()->GetSupportedVideoRtpHeaderExtensions(); |
Philipp Hancke | 4e8c115 | 2020-10-13 10:43:15 | [diff] [blame] | 4857 | } else if (IsUnsupportedContent(&content)) { |
| 4858 | session_options->media_description_options.push_back( |
| 4859 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_UNSUPPORTED, |
| 4860 | content.name, |
| 4861 | RtpTransceiverDirection::kInactive, |
| 4862 | /*stopped=*/true)); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4863 | } else { |
| 4864 | RTC_DCHECK(IsDataContent(&content)); |
| 4865 | // If we already have an data m= section, reject this extra one. |
| 4866 | if (*data_index) { |
| 4867 | session_options->media_description_options.push_back( |
| 4868 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 4869 | } else { |
| 4870 | session_options->media_description_options.push_back( |
| 4871 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 4872 | *data_index = session_options->media_description_options.size() - 1; |
| 4873 | } |
| 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | |
| 4878 | cricket::MediaDescriptionOptions |
| 4879 | SdpOfferAnswerHandler::GetMediaDescriptionOptionsForActiveData( |
| 4880 | const std::string& mid) const { |
| 4881 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4882 | // Direction for data sections is meaningless, but legacy endpoints might |
| 4883 | // expect sendrecv. |
| 4884 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4885 | RtpTransceiverDirection::kSendRecv, |
| 4886 | /*stopped=*/false); |
| 4887 | AddRtpDataChannelOptions(*(data_channel_controller()->rtp_data_channels()), |
| 4888 | &options); |
| 4889 | return options; |
| 4890 | } |
| 4891 | |
| 4892 | cricket::MediaDescriptionOptions |
| 4893 | SdpOfferAnswerHandler::GetMediaDescriptionOptionsForRejectedData( |
| 4894 | const std::string& mid) const { |
| 4895 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4896 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 4897 | RtpTransceiverDirection::kInactive, |
| 4898 | /*stopped=*/true); |
| 4899 | AddRtpDataChannelOptions(*(data_channel_controller()->rtp_data_channels()), |
| 4900 | &options); |
| 4901 | return options; |
| 4902 | } |
| 4903 | |
| 4904 | const std::string SdpOfferAnswerHandler::GetTransportName( |
| 4905 | const std::string& content_name) { |
| 4906 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4907 | cricket::ChannelInterface* channel = pc_->GetChannel(content_name); |
| 4908 | if (channel) { |
| 4909 | return channel->transport_name(); |
| 4910 | } |
| 4911 | if (data_channel_controller()->data_channel_transport()) { |
| 4912 | RTC_DCHECK(pc_->sctp_mid()); |
| 4913 | if (content_name == *(pc_->sctp_mid())) { |
| 4914 | return *(pc_->sctp_transport_name()); |
| 4915 | } |
| 4916 | } |
| 4917 | // Return an empty string if failed to retrieve the transport name. |
| 4918 | return ""; |
| 4919 | } |
| 4920 | |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4921 | std::vector<SdpOfferAnswerHandler::PayloadTypeDemuxingUpdate> |
| 4922 | SdpOfferAnswerHandler::GetPayloadTypeDemuxingUpdates( |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4923 | cricket::ContentSource source) { |
| 4924 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4925 | // We may need to delete any created default streams and disable creation of |
| 4926 | // new ones on the basis of payload type. This is needed to avoid SSRC |
| 4927 | // collisions in Call's RtpDemuxer, in the case that a transceiver has |
| 4928 | // created a default stream, and then some other channel gets the SSRC |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4929 | // signaled in the corresponding Unified Plan "m=" section. Specifically, we |
| 4930 | // need to disable payload type based demuxing when two bundled "m=" sections |
| 4931 | // are using the same payload type(s). For more context |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4932 | // see https://bugs.chromium.org/p/webrtc/issues/detail?id=11477 |
| 4933 | const SessionDescriptionInterface* sdesc = |
| 4934 | (source == cricket::CS_LOCAL ? local_description() |
| 4935 | : remote_description()); |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4936 | const cricket::ContentGroup* bundle_group = |
| 4937 | sdesc->description()->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 4938 | std::set<int> audio_payload_types; |
| 4939 | std::set<int> video_payload_types; |
| 4940 | bool pt_demuxing_enabled_audio = true; |
| 4941 | bool pt_demuxing_enabled_video = true; |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4942 | for (auto& content_info : sdesc->description()->contents()) { |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4943 | // If this m= section isn't bundled, it's safe to demux by payload type |
| 4944 | // since other m= sections using the same payload type will also be using |
| 4945 | // different transports. |
| 4946 | if (!bundle_group || !bundle_group->HasContentName(content_info.name)) { |
| 4947 | continue; |
| 4948 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4949 | if (content_info.rejected || |
| 4950 | (source == cricket::ContentSource::CS_LOCAL && |
| 4951 | !RtpTransceiverDirectionHasRecv( |
| 4952 | content_info.media_description()->direction())) || |
| 4953 | (source == cricket::ContentSource::CS_REMOTE && |
| 4954 | !RtpTransceiverDirectionHasSend( |
| 4955 | content_info.media_description()->direction()))) { |
| 4956 | // Ignore transceivers that are not receiving. |
| 4957 | continue; |
| 4958 | } |
| 4959 | switch (content_info.media_description()->type()) { |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4960 | case cricket::MediaType::MEDIA_TYPE_AUDIO: { |
| 4961 | const cricket::AudioContentDescription* audio_desc = |
| 4962 | content_info.media_description()->as_audio(); |
| 4963 | for (const cricket::AudioCodec& audio : audio_desc->codecs()) { |
| 4964 | if (audio_payload_types.count(audio.id)) { |
| 4965 | // Two m= sections are using the same payload type, thus demuxing |
| 4966 | // by payload type is not possible. |
| 4967 | pt_demuxing_enabled_audio = false; |
| 4968 | } |
| 4969 | audio_payload_types.insert(audio.id); |
| 4970 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4971 | break; |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4972 | } |
| 4973 | case cricket::MediaType::MEDIA_TYPE_VIDEO: { |
| 4974 | const cricket::VideoContentDescription* video_desc = |
| 4975 | content_info.media_description()->as_video(); |
| 4976 | for (const cricket::VideoCodec& video : video_desc->codecs()) { |
| 4977 | if (video_payload_types.count(video.id)) { |
| 4978 | // Two m= sections are using the same payload type, thus demuxing |
| 4979 | // by payload type is not possible. |
| 4980 | pt_demuxing_enabled_video = false; |
| 4981 | } |
| 4982 | video_payload_types.insert(video.id); |
| 4983 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4984 | break; |
Taylor Brandstetter | d3ef499 | 2020-10-16 01:22:57 | [diff] [blame] | 4985 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4986 | default: |
| 4987 | // Ignore data channels. |
| 4988 | continue; |
| 4989 | } |
| 4990 | } |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4991 | |
| 4992 | // Gather all updates ahead of time so that all channels can be updated in a |
| 4993 | // single Invoke; necessary due to thread guards. |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 4994 | std::vector<PayloadTypeDemuxingUpdate> channel_updates; |
Harald Alvestrand | e15fb15 | 2020-10-19 13:28:05 | [diff] [blame] | 4995 | for (const auto& transceiver : transceivers()->List()) { |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 4996 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 4997 | const ContentInfo* content = |
| 4998 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4999 | if (!channel || !content) { |
| 5000 | continue; |
| 5001 | } |
| 5002 | RtpTransceiverDirection local_direction = |
| 5003 | content->media_description()->direction(); |
| 5004 | if (source == cricket::CS_REMOTE) { |
| 5005 | local_direction = RtpTransceiverDirectionReversed(local_direction); |
| 5006 | } |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 5007 | cricket::MediaType media_type = channel->media_type(); |
| 5008 | bool in_bundle_group = |
| 5009 | (bundle_group && bundle_group->HasContentName(channel->content_name())); |
| 5010 | bool payload_type_demuxing_enabled = false; |
| 5011 | if (media_type == cricket::MediaType::MEDIA_TYPE_AUDIO) { |
| 5012 | payload_type_demuxing_enabled = |
| 5013 | (!in_bundle_group || pt_demuxing_enabled_audio) && |
| 5014 | RtpTransceiverDirectionHasRecv(local_direction); |
| 5015 | } else if (media_type == cricket::MediaType::MEDIA_TYPE_VIDEO) { |
| 5016 | payload_type_demuxing_enabled = |
| 5017 | (!in_bundle_group || pt_demuxing_enabled_video) && |
| 5018 | RtpTransceiverDirectionHasRecv(local_direction); |
| 5019 | } |
| 5020 | channel_updates.emplace_back(channel, payload_type_demuxing_enabled); |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 5021 | } |
Taylor Brandstetter | c1ad1ff | 2020-12-10 23:31:14 | [diff] [blame] | 5022 | return channel_updates; |
Harald Alvestrand | bc9ca25 | 2020-10-05 13:08:41 | [diff] [blame] | 5023 | } |
| 5024 | |
Harald Alvestrand | cdcfab0 | 2020-09-28 13:02:07 | [diff] [blame] | 5025 | } // namespace webrtc |