henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 15:54:43 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #include "pc/peer_connection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 13 | #include <algorithm> |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 14 | #include <limits> |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 15 | #include <memory> |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 16 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 17 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 11:04:15 | [diff] [blame] | 18 | #include <utility> |
| 19 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 20 | |
Steve Anton | 64b626b | 2019-01-29 01:25:26 | [diff] [blame] | 21 | #include "absl/algorithm/container.h" |
Fredrik Solenberg | 41f3a43 | 2018-12-17 20:02:22 | [diff] [blame] | 22 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "api/jsep_ice_candidate.h" |
| 24 | #include "api/jsep_session_description.h" |
| 25 | #include "api/media_stream_proxy.h" |
| 26 | #include "api/media_stream_track_proxy.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 27 | #include "api/rtc_error.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 10:24:53 | [diff] [blame] | 28 | #include "api/rtc_event_log/rtc_event_log.h" |
Niels Möller | d8b9ed7 | 2019-05-08 11:53:51 | [diff] [blame] | 29 | #include "api/rtc_event_log_output_file.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 30 | #include "api/rtp_parameters.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 31 | #include "api/uma_metrics.h" |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 32 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 33 | #include "call/call.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 34 | #include "logging/rtc_event_log/ice_logger.h" |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 35 | #include "media/base/rid_description.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 36 | #include "media/sctp/sctp_transport.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 37 | #include "pc/audio_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 38 | #include "pc/audio_track.h" |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 39 | #include "pc/channel.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 40 | #include "pc/channel_manager.h" |
| 41 | #include "pc/dtmf_sender.h" |
| 42 | #include "pc/media_stream.h" |
| 43 | #include "pc/media_stream_observer.h" |
| 44 | #include "pc/remote_audio_source.h" |
| 45 | #include "pc/rtp_media_utils.h" |
| 46 | #include "pc/rtp_receiver.h" |
| 47 | #include "pc/rtp_sender.h" |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 48 | #include "pc/sctp_transport.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 49 | #include "pc/sctp_utils.h" |
| 50 | #include "pc/sdp_utils.h" |
| 51 | #include "pc/stream_collection.h" |
Ruslan Burakov | 501bfba | 2019-02-11 09:29:19 | [diff] [blame] | 52 | #include "pc/video_rtp_receiver.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 53 | #include "pc/video_track.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 54 | #include "rtc_base/bind.h" |
| 55 | #include "rtc_base/checks.h" |
| 56 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 57 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 58 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 59 | #include "rtc_base/trace_event.h" |
| 60 | #include "system_wrappers/include/clock.h" |
| 61 | #include "system_wrappers/include/field_trial.h" |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 62 | #include "system_wrappers/include/metrics.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 63 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 64 | using cricket::ContentInfo; |
| 65 | using cricket::ContentInfos; |
| 66 | using cricket::MediaContentDescription; |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 67 | using cricket::MediaProtocolType; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 68 | using cricket::RidDescription; |
| 69 | using cricket::RidDirection; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 70 | using cricket::SessionDescription; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 71 | using cricket::SimulcastDescription; |
| 72 | using cricket::SimulcastLayer; |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 73 | using cricket::SimulcastLayerList; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 74 | using cricket::StreamParams; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 75 | using cricket::TransportInfo; |
| 76 | |
| 77 | using cricket::LOCAL_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 78 | using cricket::PRFLX_PORT_TYPE; |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 79 | using cricket::RELAY_PORT_TYPE; |
| 80 | using cricket::STUN_PORT_TYPE; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 81 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 82 | namespace webrtc { |
| 83 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 84 | // Error messages |
| 85 | const char kBundleWithoutRtcpMux[] = |
| 86 | "rtcp-mux must be enabled when BUNDLE " |
| 87 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 88 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 89 | const char kInvalidSdp[] = "Invalid session description."; |
| 90 | const char kMlineMismatchInAnswer[] = |
| 91 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 92 | "answer."; |
| 93 | const char kMlineMismatchInSubsequentOffer[] = |
| 94 | "The order of m-lines in subsequent offer doesn't match order from " |
| 95 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 96 | const char kSdpWithoutDtlsFingerprint[] = |
| 97 | "Called with SDP without DTLS fingerprint."; |
| 98 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 99 | const char kSdpWithoutIceUfragPwd[] = |
| 100 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 101 | const char kSessionError[] = "Session error code: "; |
| 102 | const char kSessionErrorDesc[] = "Session error description: "; |
| 103 | const char kDtlsSrtpSetupFailureRtp[] = |
| 104 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 105 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 106 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 107 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 108 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 109 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 110 | // UMA metric names. |
| 111 | const char kSimulcastVersionApplyLocalDescription[] = |
| 112 | "WebRTC.PeerConnection.Simulcast.ApplyLocalDescription"; |
| 113 | const char kSimulcastVersionApplyRemoteDescription[] = |
| 114 | "WebRTC.PeerConnection.Simulcast.ApplyRemoteDescription"; |
| 115 | const char kSimulcastNumberOfEncodings[] = |
| 116 | "WebRTC.PeerConnection.Simulcast.NumberOfSendEncodings"; |
| 117 | const char kSimulcastDisabled[] = "WebRTC.PeerConnection.Simulcast.Disabled"; |
| 118 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 119 | static const char kDefaultStreamId[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 120 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 121 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 122 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 123 | // The length of RTCP CNAMEs. |
| 124 | static const int kRtcpCnameLength = 16; |
| 125 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 126 | enum { |
| 127 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
| 128 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
| 129 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
| 130 | MSG_GETSTATS, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 131 | MSG_REPORT_USAGE_PATTERN, |
| 132 | }; |
| 133 | |
Harald Alvestrand | 1979384 | 2018-06-25 10:03:50 | [diff] [blame] | 134 | static const int REPORT_USAGE_PATTERN_DELAY_MS = 60000; |
| 135 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 136 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
| 137 | explicit SetSessionDescriptionMsg( |
| 138 | webrtc::SetSessionDescriptionObserver* observer) |
| 139 | : observer(observer) {} |
| 140 | |
| 141 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
| 142 | RTCError error; |
| 143 | }; |
| 144 | |
| 145 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 146 | explicit CreateSessionDescriptionMsg( |
| 147 | webrtc::CreateSessionDescriptionObserver* observer) |
| 148 | : observer(observer) {} |
| 149 | |
| 150 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 151 | RTCError error; |
| 152 | }; |
| 153 | |
| 154 | struct GetStatsMsg : public rtc::MessageData { |
| 155 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 156 | webrtc::MediaStreamTrackInterface* track) |
| 157 | : observer(observer), track(track) {} |
| 158 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
| 159 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
| 160 | }; |
| 161 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 162 | // Check if we can send |new_stream| on a PeerConnection. |
| 163 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 164 | webrtc::MediaStreamInterface* new_stream) { |
| 165 | if (!new_stream || !current_streams) { |
| 166 | return false; |
| 167 | } |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 168 | if (current_streams->find(new_stream->id()) != nullptr) { |
| 169 | RTC_LOG(LS_ERROR) << "MediaStream with ID " << new_stream->id() |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 170 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 176 | // If the direction is "recvonly" or "inactive", treat the description |
| 177 | // as containing no streams. |
| 178 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 179 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 180 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 22:57:10 | [diff] [blame] | 181 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 19:49:08 | [diff] [blame] | 182 | ? desc->streams() |
| 183 | : std::vector<cricket::StreamParams>(); |
| 184 | } |
| 185 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 186 | bool IsValidOfferToReceiveMedia(int value) { |
| 187 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 188 | return (value >= Options::kUndefined) && |
| 189 | (value <= Options::kMaxOfferToReceiveMedia); |
| 190 | } |
| 191 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 192 | // Add options to |[audio/video]_media_description_options| from |senders|. |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 193 | void AddPlanBRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 194 | const std::vector<rtc::scoped_refptr< |
| 195 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 196 | cricket::MediaDescriptionOptions* audio_media_description_options, |
Jonas Oreland | fc1acd2 | 2018-08-24 08:58:37 | [diff] [blame] | 197 | cricket::MediaDescriptionOptions* video_media_description_options, |
| 198 | int num_sim_layers) { |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 199 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 200 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 201 | if (audio_media_description_options) { |
| 202 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 22:45:38 | [diff] [blame] | 203 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 204 | } |
| 205 | } else { |
| 206 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 207 | if (video_media_description_options) { |
| 208 | video_media_description_options->AddVideoSender( |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 209 | sender->id(), sender->internal()->stream_ids(), {}, |
| 210 | SimulcastLayerList(), num_sim_layers); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 211 | } |
| 212 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 213 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 214 | } |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 215 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 216 | // Add options to |session_options| from |rtp_data_channels|. |
| 217 | void AddRtpDataChannelOptions( |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 218 | const std::map<std::string, rtc::scoped_refptr<RtpDataChannel>>& |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 219 | rtp_data_channels, |
| 220 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 221 | if (!data_media_description_options) { |
| 222 | return; |
| 223 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 224 | // Check for data channels. |
| 225 | for (const auto& kv : rtp_data_channels) { |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 226 | const RtpDataChannel* channel = kv.second; |
| 227 | if (channel->state() == RtpDataChannel::kConnecting || |
| 228 | channel->state() == RtpDataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 229 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 230 | // the data channel's label. |
| 231 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 232 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 237 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 238 | PeerConnectionInterface::IceTransportsType type) { |
| 239 | switch (type) { |
| 240 | case PeerConnectionInterface::kNone: |
| 241 | return cricket::CF_NONE; |
| 242 | case PeerConnectionInterface::kRelay: |
| 243 | return cricket::CF_RELAY; |
| 244 | case PeerConnectionInterface::kNoHost: |
| 245 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 246 | case PeerConnectionInterface::kAll: |
| 247 | return cricket::CF_ALL; |
| 248 | default: |
nisse | c80e741 | 2017-01-11 13:56:46 | [diff] [blame] | 249 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 250 | } |
| 251 | return cricket::CF_NONE; |
| 252 | } |
| 253 | |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 254 | // Map internal signaling state name to spec name: |
| 255 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 256 | std::string GetSignalingStateString( |
| 257 | PeerConnectionInterface::SignalingState state) { |
| 258 | switch (state) { |
| 259 | case PeerConnectionInterface::kStable: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 260 | return "stable"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 261 | case PeerConnectionInterface::kHaveLocalOffer: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 262 | return "have-local-offer"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 263 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 264 | return "have-local-pranswer"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 265 | case PeerConnectionInterface::kHaveRemoteOffer: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 266 | return "have-remote-offer"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 267 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 268 | return "have-remote-pranswer"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 269 | case PeerConnectionInterface::kClosed: |
Philipp Hancke | c2cfd18 | 2020-06-30 11:56:40 | [diff] [blame] | 270 | return "closed"; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 271 | } |
| 272 | RTC_NOTREACHED(); |
| 273 | return ""; |
| 274 | } |
deadbeef | 0a6c4ca | 2015-10-06 18:38:28 | [diff] [blame] | 275 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 276 | IceCandidatePairType GetIceCandidatePairCounter( |
| 277 | const cricket::Candidate& local, |
| 278 | const cricket::Candidate& remote) { |
| 279 | const auto& l = local.type(); |
| 280 | const auto& r = remote.type(); |
| 281 | const auto& host = LOCAL_PORT_TYPE; |
| 282 | const auto& srflx = STUN_PORT_TYPE; |
| 283 | const auto& relay = RELAY_PORT_TYPE; |
| 284 | const auto& prflx = PRFLX_PORT_TYPE; |
| 285 | if (l == host && r == host) { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 286 | bool local_hostname = |
| 287 | !local.address().hostname().empty() && local.address().IsUnresolvedIP(); |
| 288 | bool remote_hostname = !remote.address().hostname().empty() && |
| 289 | remote.address().IsUnresolvedIP(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 290 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 291 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 292 | if (local_hostname) { |
| 293 | if (remote_hostname) { |
| 294 | return kIceCandidatePairHostNameHostName; |
| 295 | } else if (remote_private) { |
| 296 | return kIceCandidatePairHostNameHostPrivate; |
| 297 | } else { |
| 298 | return kIceCandidatePairHostNameHostPublic; |
| 299 | } |
| 300 | } else if (local_private) { |
| 301 | if (remote_hostname) { |
| 302 | return kIceCandidatePairHostPrivateHostName; |
| 303 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 304 | return kIceCandidatePairHostPrivateHostPrivate; |
| 305 | } else { |
| 306 | return kIceCandidatePairHostPrivateHostPublic; |
| 307 | } |
| 308 | } else { |
Jeroen de Borst | 833979f | 2018-12-13 16:25:54 | [diff] [blame] | 309 | if (remote_hostname) { |
| 310 | return kIceCandidatePairHostPublicHostName; |
| 311 | } else if (remote_private) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 312 | return kIceCandidatePairHostPublicHostPrivate; |
| 313 | } else { |
| 314 | return kIceCandidatePairHostPublicHostPublic; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | if (l == host && r == srflx) |
| 319 | return kIceCandidatePairHostSrflx; |
| 320 | if (l == host && r == relay) |
| 321 | return kIceCandidatePairHostRelay; |
| 322 | if (l == host && r == prflx) |
| 323 | return kIceCandidatePairHostPrflx; |
| 324 | if (l == srflx && r == host) |
| 325 | return kIceCandidatePairSrflxHost; |
| 326 | if (l == srflx && r == srflx) |
| 327 | return kIceCandidatePairSrflxSrflx; |
| 328 | if (l == srflx && r == relay) |
| 329 | return kIceCandidatePairSrflxRelay; |
| 330 | if (l == srflx && r == prflx) |
| 331 | return kIceCandidatePairSrflxPrflx; |
| 332 | if (l == relay && r == host) |
| 333 | return kIceCandidatePairRelayHost; |
| 334 | if (l == relay && r == srflx) |
| 335 | return kIceCandidatePairRelaySrflx; |
| 336 | if (l == relay && r == relay) |
| 337 | return kIceCandidatePairRelayRelay; |
| 338 | if (l == relay && r == prflx) |
| 339 | return kIceCandidatePairRelayPrflx; |
| 340 | if (l == prflx && r == host) |
| 341 | return kIceCandidatePairPrflxHost; |
| 342 | if (l == prflx && r == srflx) |
| 343 | return kIceCandidatePairPrflxSrflx; |
| 344 | if (l == prflx && r == relay) |
| 345 | return kIceCandidatePairPrflxRelay; |
| 346 | return kIceCandidatePairMax; |
| 347 | } |
| 348 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 349 | // Logic to decide if an m= section can be recycled. This means that the new |
| 350 | // m= section is not rejected, but the old local or remote m= section is |
| 351 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 352 | // of the old remote and old local descriptions in no particular order. |
| 353 | // We need to check both the old local and remote because either |
| 354 | // could be the most current from the latest negotation. |
| 355 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 356 | const ContentInfo& content, |
| 357 | const ContentInfo* old_content_one, |
| 358 | const ContentInfo* old_content_two) { |
| 359 | return type == SdpType::kOffer && !content.rejected && |
| 360 | ((old_content_one && old_content_one->rejected) || |
| 361 | (old_content_two && old_content_two->rejected)); |
| 362 | } |
| 363 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 364 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 365 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 366 | // less than |current_desc|. In the case of checking an answer's |
| 367 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 368 | // local or remote. In the case of checking an offer's |new_desc| we |
| 369 | // check against the local and remote descriptions stored from the last |
| 370 | // negotiation, because either of these could be the most up to date for |
| 371 | // possible rejected m sections. These are the |current_desc| and |
| 372 | // |secondary_current_desc|. |
| 373 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 374 | const SessionDescription* secondary_current_desc, |
| 375 | const SessionDescription& new_desc, |
| 376 | const SdpType type) { |
| 377 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 378 | return false; |
| 379 | } |
| 380 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 381 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 382 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 383 | if (secondary_current_desc && |
| 384 | i < secondary_current_desc->contents().size()) { |
| 385 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 386 | } |
| 387 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 388 | ¤t_desc.contents()[i], |
| 389 | secondary_content_info)) { |
| 390 | // For new offer descriptions, if the media section can be recycled, it's |
| 391 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 392 | continue; |
| 393 | } |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 394 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 398 | new_desc.contents()[i].media_description(); |
| 399 | const MediaContentDescription* current_desc_mdesc = |
| 400 | current_desc.contents()[i].media_description(); |
| 401 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 402 | return false; |
| 403 | } |
| 404 | } |
| 405 | return true; |
| 406 | } |
| 407 | |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 408 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 409 | const SessionDescription& desc2) { |
| 410 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 411 | } |
| 412 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 413 | void NoteKeyProtocolAndMedia(KeyExchangeProtocolType protocol_type, |
| 414 | cricket::MediaType media_type) { |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 415 | // Array of structs needed to map {KeyExchangeProtocolType, |
| 416 | // cricket::MediaType} to KeyExchangeProtocolMedia without using std::map in |
| 417 | // order to avoid -Wglobal-constructors and -Wexit-time-destructors. |
| 418 | static constexpr struct { |
| 419 | KeyExchangeProtocolType protocol_type; |
| 420 | cricket::MediaType media_type; |
| 421 | KeyExchangeProtocolMedia protocol_media; |
| 422 | } kEnumCounterKeyProtocolMediaMap[] = { |
| 423 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_AUDIO, |
| 424 | kEnumCounterKeyProtocolMediaTypeDtlsAudio}, |
| 425 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_VIDEO, |
| 426 | kEnumCounterKeyProtocolMediaTypeDtlsVideo}, |
| 427 | {kEnumCounterKeyProtocolDtls, cricket::MEDIA_TYPE_DATA, |
| 428 | kEnumCounterKeyProtocolMediaTypeDtlsData}, |
| 429 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_AUDIO, |
| 430 | kEnumCounterKeyProtocolMediaTypeSdesAudio}, |
| 431 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_VIDEO, |
| 432 | kEnumCounterKeyProtocolMediaTypeSdesVideo}, |
| 433 | {kEnumCounterKeyProtocolSdes, cricket::MEDIA_TYPE_DATA, |
| 434 | kEnumCounterKeyProtocolMediaTypeSdesData}, |
| 435 | }; |
| 436 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 437 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocol", protocol_type, |
| 438 | kEnumCounterKeyProtocolMax); |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 439 | |
Mirko Bonadei | ab49982 | 2018-09-11 08:43:20 | [diff] [blame] | 440 | for (const auto& i : kEnumCounterKeyProtocolMediaMap) { |
| 441 | if (i.protocol_type == protocol_type && i.media_type == media_type) { |
| 442 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.KeyProtocolByMedia", |
| 443 | i.protocol_media, |
| 444 | kEnumCounterKeyProtocolMediaTypeMax); |
| 445 | } |
Harald Alvestrand | f9d0f1d | 2018-03-02 13:15:26 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 449 | void NoteAddIceCandidateResult(int result) { |
| 450 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.AddIceCandidate", result, |
| 451 | kAddIceCandidateMax); |
| 452 | } |
| 453 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 454 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 455 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 456 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 457 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 458 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 459 | // by Channel's |srtp_required| check. |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 460 | RTCError VerifyCrypto(const SessionDescription* desc, bool dtls_enabled) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 461 | const cricket::ContentGroup* bundle = |
| 462 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 463 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 464 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 465 | continue; |
| 466 | } |
Harald Alvestrand | 2e18061 | 2018-03-07 09:56:14 | [diff] [blame] | 467 | // Note what media is used with each crypto protocol, for all sections. |
| 468 | NoteKeyProtocolAndMedia(dtls_enabled ? webrtc::kEnumCounterKeyProtocolDtls |
| 469 | : webrtc::kEnumCounterKeyProtocolSdes, |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 470 | content_info.media_description()->type()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 471 | const std::string& mid = content_info.name; |
| 472 | if (bundle && bundle->HasContentName(mid) && |
| 473 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 474 | // This isn't the first media section in the BUNDLE group, so it's not |
| 475 | // required to have crypto attributes, since only the crypto attributes |
| 476 | // from the first section actually get used. |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // If the content isn't rejected or bundled into another m= section, crypto |
| 481 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 482 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 483 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 484 | if (!media || !tinfo) { |
| 485 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 486 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 487 | } |
| 488 | if (dtls_enabled) { |
| 489 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 490 | RTC_LOG(LS_WARNING) |
| 491 | << "Session description must have DTLS fingerprint if " |
| 492 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 493 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 494 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 495 | } |
| 496 | } else { |
| 497 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 498 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 499 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 500 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 504 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 508 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 509 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 510 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 511 | const cricket::ContentGroup* bundle = |
| 512 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 513 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 514 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 515 | continue; |
| 516 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 517 | const std::string& mid = content_info.name; |
| 518 | if (bundle && bundle->HasContentName(mid) && |
| 519 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 520 | // This isn't the first media section in the BUNDLE group, so it's not |
| 521 | // required to have ufrag/password, since only the ufrag/password from |
| 522 | // the first section actually get used. |
| 523 | continue; |
| 524 | } |
| 525 | |
| 526 | // If the content isn't rejected or bundled into another m= section, |
| 527 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 528 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 529 | if (!tinfo) { |
| 530 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 531 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | if (tinfo->description.ice_ufrag.empty() || |
| 535 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 536 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 537 | return false; |
| 538 | } |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 543 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 544 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 545 | const SessionDescriptionInterface* new_desc, |
| 546 | const std::string& content_name) { |
| 547 | if (!old_desc) { |
| 548 | return false; |
| 549 | } |
| 550 | const SessionDescription* new_sd = new_desc->description(); |
| 551 | const SessionDescription* old_sd = old_desc->description(); |
| 552 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 553 | if (!cinfo || cinfo->rejected) { |
| 554 | return false; |
| 555 | } |
| 556 | // If the content isn't rejected, check if ufrag and password has changed. |
| 557 | const cricket::TransportDescription* new_transport_desc = |
| 558 | new_sd->GetTransportDescriptionByName(content_name); |
| 559 | const cricket::TransportDescription* old_transport_desc = |
| 560 | old_sd->GetTransportDescriptionByName(content_name); |
| 561 | if (!new_transport_desc || !old_transport_desc) { |
| 562 | // No transport description exists. This is not an ICE restart. |
| 563 | return false; |
| 564 | } |
| 565 | if (cricket::IceCredentialsChanged( |
| 566 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 567 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 568 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 569 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | return false; |
| 573 | } |
| 574 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 575 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 576 | // from an RTCError. |
| 577 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 578 | SdpType type, |
| 579 | const RTCError& error) { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 580 | rtc::StringBuilder oss; |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 581 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 582 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 583 | return oss.Release(); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 584 | } |
| 585 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 586 | std::string GetStreamIdsString(rtc::ArrayView<const std::string> stream_ids) { |
| 587 | std::string output = "streams=["; |
| 588 | const char* separator = ""; |
| 589 | for (const auto& stream_id : stream_ids) { |
| 590 | output.append(separator).append(stream_id); |
| 591 | separator = ", "; |
| 592 | } |
| 593 | output.append("]"); |
| 594 | return output; |
| 595 | } |
| 596 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 597 | absl::optional<int> RTCConfigurationToIceConfigOptionalInt( |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 598 | int rtc_configuration_parameter) { |
| 599 | if (rtc_configuration_parameter == |
| 600 | webrtc::PeerConnectionInterface::RTCConfiguration::kUndefined) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 601 | return absl::nullopt; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 602 | } |
| 603 | return rtc_configuration_parameter; |
| 604 | } |
| 605 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 606 | void ReportSimulcastApiVersion(const char* name, |
| 607 | const SessionDescription& session) { |
| 608 | bool has_legacy = false; |
| 609 | bool has_spec_compliant = false; |
| 610 | for (const ContentInfo& content : session.contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 611 | if (!content.media_description()) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 612 | continue; |
| 613 | } |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 614 | has_spec_compliant |= content.media_description()->HasSimulcast(); |
| 615 | for (const StreamParams& sp : content.media_description()->streams()) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 616 | has_legacy |= sp.has_ssrc_group(cricket::kSimSsrcGroupSemantics); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (has_legacy) { |
| 621 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionLegacy, |
| 622 | kSimulcastApiVersionMax); |
| 623 | } |
| 624 | if (has_spec_compliant) { |
| 625 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionSpecCompliant, |
| 626 | kSimulcastApiVersionMax); |
| 627 | } |
| 628 | if (!has_legacy && !has_spec_compliant) { |
| 629 | RTC_HISTOGRAM_ENUMERATION(name, kSimulcastApiVersionNone, |
| 630 | kSimulcastApiVersionMax); |
| 631 | } |
| 632 | } |
| 633 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 634 | const ContentInfo* FindTransceiverMSection( |
| 635 | RtpTransceiverProxyWithInternal<RtpTransceiver>* transceiver, |
| 636 | const SessionDescriptionInterface* session_description) { |
| 637 | return transceiver->mid() |
| 638 | ? session_description->description()->GetContentByName( |
| 639 | *transceiver->mid()) |
| 640 | : nullptr; |
| 641 | } |
| 642 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 643 | // Wraps a CreateSessionDescriptionObserver and an OperationsChain operation |
| 644 | // complete callback. When the observer is invoked, the wrapped observer is |
| 645 | // invoked followed by invoking the completion callback. |
| 646 | class CreateSessionDescriptionObserverOperationWrapper |
| 647 | : public CreateSessionDescriptionObserver { |
| 648 | public: |
| 649 | CreateSessionDescriptionObserverOperationWrapper( |
| 650 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer, |
| 651 | std::function<void()> operation_complete_callback) |
| 652 | : observer_(std::move(observer)), |
| 653 | operation_complete_callback_(std::move(operation_complete_callback)) { |
| 654 | RTC_DCHECK(observer_); |
| 655 | } |
| 656 | ~CreateSessionDescriptionObserverOperationWrapper() override { |
| 657 | RTC_DCHECK(was_called_); |
| 658 | } |
| 659 | |
| 660 | void OnSuccess(SessionDescriptionInterface* desc) override { |
| 661 | RTC_DCHECK(!was_called_); |
| 662 | #ifdef RTC_DCHECK_IS_ON |
| 663 | was_called_ = true; |
| 664 | #endif // RTC_DCHECK_IS_ON |
| 665 | // Completing the operation before invoking the observer allows the observer |
| 666 | // to execute SetLocalDescription() without delay. |
| 667 | operation_complete_callback_(); |
| 668 | observer_->OnSuccess(desc); |
| 669 | } |
| 670 | |
| 671 | void OnFailure(RTCError error) override { |
| 672 | RTC_DCHECK(!was_called_); |
| 673 | #ifdef RTC_DCHECK_IS_ON |
| 674 | was_called_ = true; |
| 675 | #endif // RTC_DCHECK_IS_ON |
| 676 | operation_complete_callback_(); |
| 677 | observer_->OnFailure(std::move(error)); |
| 678 | } |
| 679 | |
| 680 | private: |
| 681 | #ifdef RTC_DCHECK_IS_ON |
| 682 | bool was_called_ = false; |
| 683 | #endif // RTC_DCHECK_IS_ON |
| 684 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer_; |
| 685 | std::function<void()> operation_complete_callback_; |
| 686 | }; |
| 687 | |
Jonas Oreland | e309651 | 2020-05-27 07:01:05 | [diff] [blame] | 688 | // Check if the changes of IceTransportsType motives an ice restart. |
| 689 | bool NeedIceRestart(bool surface_ice_candidates_on_ice_transport_type_changed, |
| 690 | PeerConnectionInterface::IceTransportsType current, |
| 691 | PeerConnectionInterface::IceTransportsType modified) { |
| 692 | if (current == modified) { |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | if (!surface_ice_candidates_on_ice_transport_type_changed) { |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | auto current_filter = ConvertIceTransportTypeToCandidateFilter(current); |
| 701 | auto modified_filter = ConvertIceTransportTypeToCandidateFilter(modified); |
| 702 | |
| 703 | // If surface_ice_candidates_on_ice_transport_type_changed is true and we |
| 704 | // extend the filter, then no ice restart is needed. |
| 705 | return (current_filter & modified_filter) != current_filter; |
| 706 | } |
| 707 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 708 | } // namespace |
| 709 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 710 | // Used by parameterless SetLocalDescription() to create an offer or answer. |
| 711 | // Upon completion of creating the session description, SetLocalDescription() is |
| 712 | // invoked with the result. |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 713 | class PeerConnection::ImplicitCreateSessionDescriptionObserver |
| 714 | : public CreateSessionDescriptionObserver { |
| 715 | public: |
| 716 | ImplicitCreateSessionDescriptionObserver( |
| 717 | rtc::WeakPtr<PeerConnection> pc, |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 718 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 719 | set_local_description_observer) |
| 720 | : pc_(std::move(pc)), |
| 721 | set_local_description_observer_( |
| 722 | std::move(set_local_description_observer)) {} |
| 723 | ~ImplicitCreateSessionDescriptionObserver() override { |
| 724 | RTC_DCHECK(was_called_); |
| 725 | } |
| 726 | |
| 727 | void SetOperationCompleteCallback( |
| 728 | std::function<void()> operation_complete_callback) { |
| 729 | operation_complete_callback_ = std::move(operation_complete_callback); |
| 730 | } |
| 731 | |
| 732 | bool was_called() const { return was_called_; } |
| 733 | |
| 734 | void OnSuccess(SessionDescriptionInterface* desc_ptr) override { |
| 735 | RTC_DCHECK(!was_called_); |
| 736 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 737 | was_called_ = true; |
| 738 | |
| 739 | // Abort early if |pc_| is no longer valid. |
| 740 | if (!pc_) { |
| 741 | operation_complete_callback_(); |
| 742 | return; |
| 743 | } |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 744 | // DoSetLocalDescription() is a synchronous operation that invokes |
| 745 | // |set_local_description_observer_| with the result. |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 746 | pc_->DoSetLocalDescription(std::move(desc), |
| 747 | std::move(set_local_description_observer_)); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 748 | operation_complete_callback_(); |
| 749 | } |
| 750 | |
| 751 | void OnFailure(RTCError error) override { |
| 752 | RTC_DCHECK(!was_called_); |
| 753 | was_called_ = true; |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 754 | set_local_description_observer_->OnSetLocalDescriptionComplete(RTCError( |
| 755 | error.type(), std::string("SetLocalDescription failed to create " |
| 756 | "session description - ") + |
| 757 | error.message())); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 758 | operation_complete_callback_(); |
| 759 | } |
| 760 | |
| 761 | private: |
| 762 | bool was_called_ = false; |
| 763 | rtc::WeakPtr<PeerConnection> pc_; |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 764 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 765 | set_local_description_observer_; |
| 766 | std::function<void()> operation_complete_callback_; |
| 767 | }; |
| 768 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 769 | class PeerConnection::LocalIceCredentialsToReplace { |
| 770 | public: |
| 771 | // Sets the ICE credentials that need restarting to the ICE credentials of |
| 772 | // the current and pending descriptions. |
| 773 | void SetIceCredentialsFromLocalDescriptions( |
| 774 | const SessionDescriptionInterface* current_local_description, |
| 775 | const SessionDescriptionInterface* pending_local_description) { |
| 776 | ice_credentials_.clear(); |
| 777 | if (current_local_description) { |
| 778 | AppendIceCredentialsFromSessionDescription(*current_local_description); |
| 779 | } |
| 780 | if (pending_local_description) { |
| 781 | AppendIceCredentialsFromSessionDescription(*pending_local_description); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | void ClearIceCredentials() { ice_credentials_.clear(); } |
| 786 | |
| 787 | // Returns true if we have ICE credentials that need restarting. |
| 788 | bool HasIceCredentials() const { return !ice_credentials_.empty(); } |
| 789 | |
| 790 | // Returns true if |local_description| shares no ICE credentials with the |
| 791 | // ICE credentials that need restarting. |
| 792 | bool SatisfiesIceRestart( |
| 793 | const SessionDescriptionInterface& local_description) const { |
| 794 | for (const auto& transport_info : |
| 795 | local_description.description()->transport_infos()) { |
| 796 | if (ice_credentials_.find(std::make_pair( |
| 797 | transport_info.description.ice_ufrag, |
| 798 | transport_info.description.ice_pwd)) != ice_credentials_.end()) { |
| 799 | return false; |
| 800 | } |
| 801 | } |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | private: |
| 806 | void AppendIceCredentialsFromSessionDescription( |
| 807 | const SessionDescriptionInterface& desc) { |
| 808 | for (const auto& transport_info : desc.description()->transport_infos()) { |
| 809 | ice_credentials_.insert( |
| 810 | std::make_pair(transport_info.description.ice_ufrag, |
| 811 | transport_info.description.ice_pwd)); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | std::set<std::pair<std::string, std::string>> ice_credentials_; |
| 816 | }; |
| 817 | |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 818 | // Wrapper for SetSessionDescriptionObserver that invokes the success or failure |
| 819 | // callback in a posted message handled by the peer connection. This introduces |
| 820 | // a delay that prevents recursive API calls by the observer, but this also |
| 821 | // means that the PeerConnection can be modified before the observer sees the |
| 822 | // result of the operation. This is ill-advised for synchronizing states. |
| 823 | // |
| 824 | // Implements both the SetLocalDescriptionObserverInterface and the |
| 825 | // SetRemoteDescriptionObserverInterface. |
| 826 | class PeerConnection::SetSessionDescriptionObserverAdapter |
| 827 | : public SetLocalDescriptionObserverInterface, |
| 828 | public SetRemoteDescriptionObserverInterface { |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 829 | public: |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 830 | SetSessionDescriptionObserverAdapter( |
| 831 | rtc::WeakPtr<PeerConnection> pc, |
| 832 | rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer) |
| 833 | : pc_(std::move(pc)), inner_observer_(std::move(inner_observer)) {} |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 834 | |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 835 | // SetLocalDescriptionObserverInterface implementation. |
| 836 | void OnSetLocalDescriptionComplete(RTCError error) override { |
| 837 | OnSetDescriptionComplete(std::move(error)); |
| 838 | } |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 839 | // SetRemoteDescriptionObserverInterface implementation. |
| 840 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 841 | OnSetDescriptionComplete(std::move(error)); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | private: |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 845 | void OnSetDescriptionComplete(RTCError error) { |
| 846 | if (!pc_) |
| 847 | return; |
| 848 | if (error.ok()) { |
| 849 | pc_->PostSetSessionDescriptionSuccess(inner_observer_); |
| 850 | } else { |
| 851 | pc_->PostSetSessionDescriptionFailure(inner_observer_, std::move(error)); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | rtc::WeakPtr<PeerConnection> pc_; |
| 856 | rtc::scoped_refptr<SetSessionDescriptionObserver> inner_observer_; |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 857 | }; |
| 858 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 859 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 860 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 861 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 862 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 863 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 | [diff] [blame] | 864 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 865 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 866 | BundlePolicy bundle_policy; |
| 867 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 868 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 869 | int ice_candidate_pool_size; |
| 870 | bool disable_ipv6; |
| 871 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 872 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 873 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 874 | bool enable_rtp_data_channel; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 875 | absl::optional<int> screencast_min_bitrate; |
| 876 | absl::optional<bool> combined_audio_video_bwe; |
| 877 | absl::optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 878 | TcpCandidatePolicy tcp_candidate_policy; |
| 879 | CandidateNetworkPolicy candidate_network_policy; |
| 880 | int audio_jitter_buffer_max_packets; |
| 881 | bool audio_jitter_buffer_fast_accelerate; |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 882 | int audio_jitter_buffer_min_delay_ms; |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 883 | bool audio_jitter_buffer_enable_rtx_handling; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 884 | int ice_connection_receiving_timeout; |
| 885 | int ice_backup_candidate_pair_ping_interval; |
| 886 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 887 | bool prioritize_most_likely_ice_candidate_pairs; |
| 888 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 889 | bool prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 890 | PortPrunePolicy turn_port_prune_policy; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 891 | bool presume_writable_when_fully_relayed; |
| 892 | bool enable_ice_renomination; |
| 893 | bool redetermine_role_on_ice_restart; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 894 | bool surface_ice_candidates_on_ice_transport_type_changed; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 895 | absl::optional<int> ice_check_interval_strong_connectivity; |
| 896 | absl::optional<int> ice_check_interval_weak_connectivity; |
| 897 | absl::optional<int> ice_check_min_interval; |
| 898 | absl::optional<int> ice_unwritable_timeout; |
| 899 | absl::optional<int> ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 900 | absl::optional<int> ice_inactive_timeout; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 901 | absl::optional<int> stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 902 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 903 | SdpSemantics sdp_semantics; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 904 | absl::optional<rtc::AdapterType> network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 905 | bool active_reset_srtp_params; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 906 | absl::optional<CryptoOptions> crypto_options; |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 907 | bool offer_extmap_allow_mixed; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 908 | std::string turn_logging_id; |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 909 | bool enable_implicit_rollback; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 910 | absl::optional<bool> allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 911 | }; |
| 912 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 913 | "Did you add something to RTCConfiguration and forget to " |
| 914 | "update operator==?"); |
| 915 | return type == o.type && servers == o.servers && |
| 916 | bundle_policy == o.bundle_policy && |
| 917 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 918 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 919 | candidate_network_policy == o.candidate_network_policy && |
| 920 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 921 | audio_jitter_buffer_fast_accelerate == |
| 922 | o.audio_jitter_buffer_fast_accelerate && |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 923 | audio_jitter_buffer_min_delay_ms == |
| 924 | o.audio_jitter_buffer_min_delay_ms && |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 925 | audio_jitter_buffer_enable_rtx_handling == |
| 926 | o.audio_jitter_buffer_enable_rtx_handling && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 927 | ice_connection_receiving_timeout == |
| 928 | o.ice_connection_receiving_timeout && |
| 929 | ice_backup_candidate_pair_ping_interval == |
| 930 | o.ice_backup_candidate_pair_ping_interval && |
| 931 | continual_gathering_policy == o.continual_gathering_policy && |
| 932 | certificates == o.certificates && |
| 933 | prioritize_most_likely_ice_candidate_pairs == |
| 934 | o.prioritize_most_likely_ice_candidate_pairs && |
| 935 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 936 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 937 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 938 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 939 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 940 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 941 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 942 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 943 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 944 | prune_turn_ports == o.prune_turn_ports && |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 945 | turn_port_prune_policy == o.turn_port_prune_policy && |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 946 | presume_writable_when_fully_relayed == |
| 947 | o.presume_writable_when_fully_relayed && |
| 948 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 19:50:14 | [diff] [blame] | 949 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 950 | surface_ice_candidates_on_ice_transport_type_changed == |
| 951 | o.surface_ice_candidates_on_ice_transport_type_changed && |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 952 | ice_check_interval_strong_connectivity == |
| 953 | o.ice_check_interval_strong_connectivity && |
| 954 | ice_check_interval_weak_connectivity == |
| 955 | o.ice_check_interval_weak_connectivity && |
Steve Anton | 300bf8e | 2017-07-14 17:13:10 | [diff] [blame] | 956 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 957 | ice_unwritable_timeout == o.ice_unwritable_timeout && |
| 958 | ice_unwritable_min_checks == o.ice_unwritable_min_checks && |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 959 | ice_inactive_timeout == o.ice_inactive_timeout && |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 960 | stun_candidate_keepalive_interval == |
| 961 | o.stun_candidate_keepalive_interval && |
Steve Anton | 79e7960 | 2017-11-20 18:25:56 | [diff] [blame] | 962 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 963 | sdp_semantics == o.sdp_semantics && |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 964 | network_preference == o.network_preference && |
Piotr (Peter) Slatala | e0c2e97 | 2018-10-08 16:43:21 | [diff] [blame] | 965 | active_reset_srtp_params == o.active_reset_srtp_params && |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 966 | crypto_options == o.crypto_options && |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 967 | offer_extmap_allow_mixed == o.offer_extmap_allow_mixed && |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 968 | turn_logging_id == o.turn_logging_id && |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 969 | enable_implicit_rollback == o.enable_implicit_rollback && |
Eldar Rello | 9276e2c | 2020-06-10 14:53:39 | [diff] [blame] | 970 | allow_codec_switching == o.allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 974 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 975 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 19:44:26 | [diff] [blame] | 976 | } |
| 977 | |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 978 | void PeerConnection::TransceiverStableState::set_newly_created() { |
| 979 | RTC_DCHECK(!has_m_section_); |
| 980 | newly_created_ = true; |
| 981 | } |
| 982 | |
| 983 | void PeerConnection::TransceiverStableState::SetMSectionIfUnset( |
| 984 | absl::optional<std::string> mid, |
| 985 | absl::optional<size_t> mline_index) { |
| 986 | if (!has_m_section_) { |
| 987 | mid_ = mid; |
| 988 | mline_index_ = mline_index; |
| 989 | has_m_section_ = true; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | void PeerConnection::TransceiverStableState::SetRemoteStreamIdsIfUnset( |
| 994 | const std::vector<std::string>& ids) { |
| 995 | if (!remote_stream_ids_.has_value()) { |
| 996 | remote_stream_ids_ = ids; |
| 997 | } |
| 998 | } |
| 999 | |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 1000 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 1001 | std::string GenerateRtcpCname() { |
| 1002 | std::string cname; |
| 1003 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1004 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 1005 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 1006 | } |
| 1007 | return cname; |
| 1008 | } |
| 1009 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1010 | bool ValidateOfferAnswerOptions( |
| 1011 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 1012 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 1013 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 13:50:32 | [diff] [blame] | 1014 | } |
| 1015 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1016 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 1017 | // m= sections (in other words, nothing that involves a map/array). |
| 1018 | void ExtractSharedMediaSessionOptions( |
| 1019 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 1020 | cricket::MediaSessionOptions* session_options) { |
| 1021 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 1022 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
Mirta Dvornicic | 479a3c0 | 2019-06-04 13:38:50 | [diff] [blame] | 1023 | session_options->raw_packetization_for_video = |
| 1024 | rtc_options.raw_packetization_for_video; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 1025 | } |
zhihuang | a77e6bb | 2017-08-15 01:17:48 | [diff] [blame] | 1026 | |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1027 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 1028 | std::unique_ptr<RtcEventLog> event_log, |
| 1029 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1030 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1031 | event_log_(std::move(event_log)), |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1032 | event_log_ptr_(event_log_.get()), |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1033 | operations_chain_(rtc::OperationsChain::Create()), |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 1034 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1035 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 19:52:32 | [diff] [blame] | 1036 | remote_streams_(StreamCollection::Create()), |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1037 | call_(std::move(call)), |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 1038 | call_ptr_(call_.get()), |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1039 | local_ice_credentials_to_replace_(new LocalIceCredentialsToReplace()), |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 1040 | data_channel_controller_(this), |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 1041 | weak_ptr_factory_(this) { |
| 1042 | operations_chain_->SetOnChainEmptyCallback( |
| 1043 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr()]() { |
| 1044 | if (!this_weak_ptr) |
| 1045 | return; |
| 1046 | this_weak_ptr->OnOperationsChainEmpty(); |
| 1047 | }); |
| 1048 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1049 | |
| 1050 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1051 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1052 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1053 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1054 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 1055 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1056 | // Need to stop transceivers before destroying the stats collector because |
| 1057 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 1058 | // stopping. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1059 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 1060 | transceiver->StopInternal(); |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1061 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1062 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1063 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 13:06:57 | [diff] [blame] | 1064 | if (stats_collector_) { |
| 1065 | stats_collector_->WaitForPendingRequest(); |
| 1066 | stats_collector_ = nullptr; |
| 1067 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1068 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1069 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 1070 | // the last stats request can still read from the channels. |
| 1071 | DestroyAllChannels(); |
| 1072 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1073 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1074 | |
| 1075 | webrtc_session_desc_factory_.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1076 | transport_controller_.reset(); |
| 1077 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 1078 | // port_allocator_ lives on the network thread and should be destroyed there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1079 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 1080 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1081 | port_allocator_.reset(); |
| 1082 | }); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1083 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 1084 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1085 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1086 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 1087 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 1088 | event_log_.reset(); |
| 1089 | }); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 1090 | |
| 1091 | // Process all pending notifications in the message queue. If we don't do |
| 1092 | // this, requests will linger and not know they succeeded or failed. |
| 1093 | rtc::MessageList list; |
| 1094 | signaling_thread()->Clear(this, rtc::MQID_ANY, &list); |
| 1095 | for (auto& msg : list) { |
| 1096 | if (msg.message_id == MSG_CREATE_SESSIONDESCRIPTION_FAILED) { |
| 1097 | // Processing CreateOffer() and CreateAnswer() messages ensures their |
| 1098 | // observers are invoked even if the PeerConnection is destroyed early. |
| 1099 | OnMessage(&msg); |
| 1100 | } else { |
| 1101 | // TODO(hbos): Consider processing all pending messages. This would mean |
| 1102 | // that SetLocalDescription() and SetRemoteDescription() observers are |
| 1103 | // informed of successes and failures; this is currently NOT the case. |
| 1104 | delete msg.pdata; |
| 1105 | } |
| 1106 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1107 | } |
| 1108 | |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 1109 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1110 | // Destroy video channels first since they may have a pointer to a voice |
| 1111 | // channel. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1112 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1113 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1114 | DestroyTransceiverChannel(transceiver); |
| 1115 | } |
| 1116 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1117 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1118 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1119 | DestroyTransceiverChannel(transceiver); |
| 1120 | } |
| 1121 | } |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 1122 | DestroyDataChannelTransport(); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 1123 | } |
| 1124 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1125 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 1126 | const PeerConnectionInterface::RTCConfiguration& configuration, |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1127 | PeerConnectionDependencies dependencies) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1128 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1129 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1130 | |
| 1131 | RTCError config_error = ValidateConfiguration(configuration); |
| 1132 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1133 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1134 | return false; |
| 1135 | } |
| 1136 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1137 | if (!dependencies.allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1138 | RTC_LOG(LS_ERROR) |
| 1139 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 1140 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1141 | return false; |
| 1142 | } |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 1143 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1144 | if (!dependencies.observer) { |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 1145 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1146 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 1147 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 1148 | return false; |
| 1149 | } |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 1150 | |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1151 | observer_ = dependencies.observer; |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 1152 | async_resolver_factory_ = std::move(dependencies.async_resolver_factory); |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1153 | port_allocator_ = std::move(dependencies.allocator); |
Jorge E. Moreira | 00b46f7 | 2020-03-28 01:01:19 | [diff] [blame] | 1154 | packet_socket_factory_ = std::move(dependencies.packet_socket_factory); |
Qingsi Wang | 25ec888 | 2019-11-15 20:33:05 | [diff] [blame] | 1155 | ice_transport_factory_ = std::move(dependencies.ice_transport_factory); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 1156 | tls_cert_verifier_ = std::move(dependencies.tls_cert_verifier); |
deadbeef | 653b8e0 | 2015-11-11 20:55:10 | [diff] [blame] | 1157 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1158 | cricket::ServerAddresses stun_servers; |
| 1159 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 1160 | |
| 1161 | RTCErrorType parse_error = |
| 1162 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 1163 | if (parse_error != RTCErrorType::NONE) { |
| 1164 | return false; |
| 1165 | } |
| 1166 | |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 1167 | // Add the turn logging id to all turn servers |
| 1168 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 1169 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 1170 | } |
| 1171 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 1172 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 1173 | // there. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1174 | const auto pa_result = |
| 1175 | network_thread()->Invoke<InitializePortAllocatorResult>( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1176 | RTC_FROM_HERE, |
| 1177 | rtc::Bind(&PeerConnection::InitializePortAllocator_n, this, |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1178 | stun_servers, turn_servers, configuration)); |
| 1179 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 1180 | // If initialization was successful, note if STUN or TURN servers |
| 1181 | // were supplied. |
| 1182 | if (!stun_servers.empty()) { |
| 1183 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 1184 | } |
| 1185 | if (!turn_servers.empty()) { |
| 1186 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 1187 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1188 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1189 | // Send information about IPv4/IPv6 status. |
| 1190 | PeerConnectionAddressFamilyCounter address_family; |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 1191 | if (pa_result.enable_ipv6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 1192 | address_family = kPeerConnection_IPv6; |
| 1193 | } else { |
| 1194 | address_family = kPeerConnection_IPv4; |
| 1195 | } |
| 1196 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", address_family, |
| 1197 | kPeerConnectionAddressFamilyCounter_Max); |
| 1198 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1199 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 1200 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1201 | // RFC 3264: The numeric value of the session id and version in the |
| 1202 | // o line MUST be representable with a "64 bit signed integer". |
| 1203 | // Due to this constraint session id |session_id_| is max limited to |
| 1204 | // LLONG_MAX. |
| 1205 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1206 | JsepTransportController::Config config; |
| 1207 | config.redetermine_role_on_ice_restart = |
| 1208 | configuration.redetermine_role_on_ice_restart; |
| 1209 | config.ssl_max_version = factory_->options().ssl_max_version; |
| 1210 | config.disable_encryption = options.disable_encryption; |
| 1211 | config.bundle_policy = configuration.bundle_policy; |
| 1212 | config.rtcp_mux_policy = configuration.rtcp_mux_policy; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1213 | // TODO(bugs.webrtc.org/9891) - Remove options.crypto_options then remove this |
| 1214 | // stub. |
| 1215 | config.crypto_options = configuration.crypto_options.has_value() |
| 1216 | ? *configuration.crypto_options |
| 1217 | : options.crypto_options; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 1218 | config.transport_observer = this; |
Sebastian Jansson | 1b83a9e | 2019-09-18 16:22:12 | [diff] [blame] | 1219 | // It's safe to pass |this| and using |rtcp_invoker_| and the |call_| pointer |
| 1220 | // since the JsepTransportController instance is owned by this PeerConnection |
| 1221 | // instance and is destroyed before both |rtcp_invoker_| and the |call_| |
| 1222 | // pointer. |
| 1223 | config.rtcp_handler = [this](const rtc::CopyOnWriteBuffer& packet, |
| 1224 | int64_t packet_time_us) { |
| 1225 | RTC_DCHECK_RUN_ON(network_thread()); |
| 1226 | rtcp_invoker_.AsyncInvoke<void>( |
| 1227 | RTC_FROM_HERE, worker_thread(), [this, packet, packet_time_us] { |
| 1228 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 1229 | // |call_| is reset on the worker thread in the PeerConnection |
| 1230 | // destructor, so we check that it's still valid before propagating |
| 1231 | // the packet. |
| 1232 | if (call_) { |
| 1233 | call_->Receiver()->DeliverPacket(MediaType::ANY, packet, |
| 1234 | packet_time_us); |
| 1235 | } |
| 1236 | }); |
| 1237 | }; |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 1238 | config.event_log = event_log_ptr_; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1239 | #if defined(ENABLE_EXTERNAL_AUTH) |
| 1240 | config.enable_external_auth = true; |
| 1241 | #endif |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 1242 | config.active_reset_srtp_params = configuration.active_reset_srtp_params; |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 1243 | |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1244 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 1245 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 1246 | if (!configuration.certificates.empty()) { |
| 1247 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 1248 | // just picking the first one. The decision should be made based on the DTLS |
| 1249 | // handshake. The DTLS negotiations need to know about all certificates. |
| 1250 | certificate = configuration.certificates[0]; |
| 1251 | } |
| 1252 | |
| 1253 | if (options.disable_encryption) { |
| 1254 | dtls_enabled_ = false; |
| 1255 | } else { |
| 1256 | // Enable DTLS by default if we have an identity store or a certificate. |
| 1257 | dtls_enabled_ = (dependencies.cert_generator || certificate); |
| 1258 | // |configuration| can override the default |dtls_enabled_| value. |
| 1259 | if (configuration.enable_dtls_srtp) { |
| 1260 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 1261 | } |
| 1262 | } |
| 1263 | |
Niels Möller | 2a70703 | 2020-06-16 14:39:13 | [diff] [blame] | 1264 | if (configuration.enable_rtp_data_channel) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1265 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is |
| 1266 | // set. It takes precendence over the disable_sctp_data_channels |
| 1267 | // PeerConnectionFactoryInterface::Options. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 1268 | data_channel_controller_.set_data_channel_type(cricket::DCT_RTP); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1269 | } else { |
| 1270 | // DTLS has to be enabled to use SCTP. |
| 1271 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 1272 | data_channel_controller_.set_data_channel_type(cricket::DCT_SCTP); |
Per Kjellander | 2bca008 | 2020-08-28 07:15:15 | [diff] [blame] | 1273 | config.sctp_factory = factory_->sctp_transport_factory(); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Qingsi Wang | 25ec888 | 2019-11-15 20:33:05 | [diff] [blame] | 1277 | config.ice_transport_factory = ice_transport_factory_.get(); |
| 1278 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1279 | transport_controller_.reset(new JsepTransportController( |
Zach Stein | e20867f | 2018-08-02 20:20:15 | [diff] [blame] | 1280 | signaling_thread(), network_thread(), port_allocator_.get(), |
| 1281 | async_resolver_factory_.get(), config)); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1282 | transport_controller_->SignalIceConnectionState.connect( |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 1283 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 1284 | transport_controller_->SignalStandardizedIceConnectionState.connect( |
| 1285 | this, &PeerConnection::SetStandardizedIceConnectionState); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 1286 | transport_controller_->SignalConnectionState.connect( |
| 1287 | this, &PeerConnection::SetConnectionState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1288 | transport_controller_->SignalIceGatheringState.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1289 | this, &PeerConnection::OnTransportControllerGatheringState); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1290 | transport_controller_->SignalIceCandidatesGathered.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1291 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 1292 | transport_controller_->SignalIceCandidateError.connect( |
| 1293 | this, &PeerConnection::OnTransportControllerCandidateError); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 1294 | transport_controller_->SignalIceCandidatesRemoved.connect( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1295 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 1296 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 1297 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 1298 | transport_controller_->SignalIceCandidatePairChanged.connect( |
| 1299 | this, &PeerConnection::OnTransportControllerCandidateChanged); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1300 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1301 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 1302 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1303 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 1304 | configuration_ = configuration; |
| 1305 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 1306 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1307 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1308 | video_options_.screencast_min_bitrate_kbps = |
| 1309 | configuration.screencast_min_bitrate; |
| 1310 | audio_options_.combined_audio_video_bwe = |
| 1311 | configuration.combined_audio_video_bwe; |
| 1312 | |
| 1313 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1314 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1315 | |
| 1316 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 1317 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1318 | |
Jakob Ivarsson | 10403ae | 2018-11-27 14:45:20 | [diff] [blame] | 1319 | audio_options_.audio_jitter_buffer_min_delay_ms = |
| 1320 | configuration.audio_jitter_buffer_min_delay_ms; |
| 1321 | |
Jakob Ivarsson | 53eae87 | 2019-01-10 14:58:36 | [diff] [blame] | 1322 | audio_options_.audio_jitter_buffer_enable_rtx_handling = |
| 1323 | configuration.audio_jitter_buffer_enable_rtx_handling; |
| 1324 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1325 | // Whether the certificate generator/certificate is null or not determines |
| 1326 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 1327 | // the right instructions by clearing the variables if needed. |
| 1328 | if (!dtls_enabled_) { |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1329 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1330 | certificate = nullptr; |
| 1331 | } else if (certificate) { |
| 1332 | // Favor generated certificate over the certificate generator. |
Benjamin Wright | cab58888 | 2018-05-02 22:12:47 | [diff] [blame] | 1333 | dependencies.cert_generator.reset(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 1337 | signaling_thread(), channel_manager(), this, session_id(), |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 1338 | std::move(dependencies.cert_generator), certificate, &ssrc_generator_)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 1339 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 1340 | this, &PeerConnection::OnCertificateReady); |
| 1341 | |
| 1342 | if (options.disable_encryption) { |
| 1343 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 1344 | } |
| 1345 | |
| 1346 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 1347 | GetCryptoOptions().srtp.enable_encrypted_rtp_header_extensions); |
Steve Anton | 8f66ddb | 2018-12-11 00:08:05 | [diff] [blame] | 1348 | webrtc_session_desc_factory_->set_is_unified_plan(IsUnifiedPlan()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1349 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1350 | // Add default audio/video transceivers for Plan B SDP. |
| 1351 | if (!IsUnifiedPlan()) { |
| 1352 | transceivers_.push_back( |
| 1353 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1354 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 1355 | transceivers_.push_back( |
| 1356 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1357 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 1358 | } |
Harald Alvestrand | 183e09d | 2018-06-28 10:04:41 | [diff] [blame] | 1359 | int delay_ms = |
| 1360 | return_histogram_very_quickly_ ? 0 : REPORT_USAGE_PATTERN_DELAY_MS; |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 1361 | signaling_thread()->PostDelayed(RTC_FROM_HERE, delay_ms, this, |
| 1362 | MSG_REPORT_USAGE_PATTERN, nullptr); |
Jonas Oreland | a3aa9bd | 2019-04-17 05:38:40 | [diff] [blame] | 1363 | |
| 1364 | if (dependencies.video_bitrate_allocator_factory) { |
| 1365 | video_bitrate_allocator_factory_ = |
| 1366 | std::move(dependencies.video_bitrate_allocator_factory); |
| 1367 | } else { |
| 1368 | video_bitrate_allocator_factory_ = |
| 1369 | CreateBuiltinVideoBitrateAllocatorFactory(); |
| 1370 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1374 | RTCError PeerConnection::ValidateConfiguration( |
| 1375 | const RTCConfiguration& config) const { |
Steve Anton | f417238 | 2020-01-27 23:45:02 | [diff] [blame] | 1376 | return cricket::P2PTransportChannel::ValidateIceConfig( |
| 1377 | ParseIceConfig(config)); |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 1378 | } |
| 1379 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1380 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::local_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1381 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1382 | RTC_CHECK(!IsUnifiedPlan()) << "local_streams is not available with Unified " |
| 1383 | "Plan SdpSemantics. Please use GetSenders " |
| 1384 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1385 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1386 | } |
| 1387 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 1388 | rtc::scoped_refptr<StreamCollectionInterface> PeerConnection::remote_streams() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1389 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1390 | RTC_CHECK(!IsUnifiedPlan()) << "remote_streams is not available with Unified " |
| 1391 | "Plan SdpSemantics. Please use GetReceivers " |
| 1392 | "instead."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1393 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1394 | } |
| 1395 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 | [diff] [blame] | 1396 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1397 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1398 | RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan " |
| 1399 | "SdpSemantics. Please use AddTrack instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1400 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1401 | if (IsClosed()) { |
| 1402 | return false; |
| 1403 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1404 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1407 | |
| 1408 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1409 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1410 | observer->SignalAudioTrackAdded.connect(this, |
| 1411 | &PeerConnection::OnAudioTrackAdded); |
| 1412 | observer->SignalAudioTrackRemoved.connect( |
| 1413 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1414 | observer->SignalVideoTrackAdded.connect(this, |
| 1415 | &PeerConnection::OnVideoTrackAdded); |
| 1416 | observer->SignalVideoTrackRemoved.connect( |
| 1417 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1418 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1419 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1420 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1421 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1422 | } |
| 1423 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1424 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1425 | } |
| 1426 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 1427 | stats_->AddStream(local_stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1428 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1429 | return true; |
| 1430 | } |
| 1431 | |
| 1432 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1433 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1434 | RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified " |
| 1435 | "Plan SdpSemantics. Please use RemoveTrack " |
| 1436 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1437 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 1438 | if (!IsClosed()) { |
| 1439 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1440 | RemoveAudioTrack(track.get(), local_stream); |
| 1441 | } |
| 1442 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1443 | RemoveVideoTrack(track.get(), local_stream); |
| 1444 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1445 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1446 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1447 | stream_observers_.erase( |
| 1448 | std::remove_if( |
| 1449 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 1450 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
Seth Hampson | 13b8bad | 2018-03-13 23:05:28 | [diff] [blame] | 1451 | return observer->stream()->id().compare(local_stream->id()) == 0; |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 1452 | }), |
| 1453 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 1454 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1455 | if (IsClosed()) { |
| 1456 | return; |
| 1457 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1458 | UpdateNegotiationNeeded(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 1459 | } |
| 1460 | |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1461 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1462 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1463 | const std::vector<std::string>& stream_ids) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1464 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 2d6c76a | 2018-01-06 01:10:52 | [diff] [blame] | 1465 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1466 | if (!track) { |
| 1467 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1468 | } |
| 1469 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1470 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1471 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1472 | "Track has invalid kind: " + track->kind()); |
| 1473 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1474 | if (IsClosed()) { |
| 1475 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1476 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1477 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1478 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1479 | LOG_AND_RETURN_ERROR( |
| 1480 | RTCErrorType::INVALID_PARAMETER, |
| 1481 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1482 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1483 | auto sender_or_error = |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1484 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, stream_ids) |
| 1485 | : AddTrackPlanB(track, stream_ids)); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1486 | if (sender_or_error.ok()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1487 | UpdateNegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 23:48:17 | [diff] [blame] | 1488 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1489 | } |
| 1490 | return sender_or_error; |
| 1491 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1492 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1493 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1494 | PeerConnection::AddTrackPlanB( |
| 1495 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1496 | const std::vector<std::string>& stream_ids) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1497 | if (stream_ids.size() > 1u) { |
| 1498 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_OPERATION, |
| 1499 | "AddTrack with more than one stream is not " |
| 1500 | "supported with Plan B semantics."); |
| 1501 | } |
| 1502 | std::vector<std::string> adjusted_stream_ids = stream_ids; |
| 1503 | if (adjusted_stream_ids.empty()) { |
| 1504 | adjusted_stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1505 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1506 | cricket::MediaType media_type = |
| 1507 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1508 | ? cricket::MEDIA_TYPE_AUDIO |
| 1509 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1510 | auto new_sender = |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1511 | CreateSender(media_type, track->id(), track, adjusted_stream_ids, {}); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1512 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1513 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1514 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1515 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1516 | FindSenderInfo(local_audio_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1517 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1518 | if (sender_info) { |
| 1519 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1520 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1521 | } else { |
| 1522 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1523 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1524 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1525 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 1526 | FindSenderInfo(local_video_sender_infos_, |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1527 | new_sender->internal()->stream_ids()[0], track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1528 | if (sender_info) { |
| 1529 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1530 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1531 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1532 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1533 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1534 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1535 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1536 | PeerConnection::AddTrackUnifiedPlan( |
| 1537 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1538 | const std::vector<std::string>& stream_ids) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1539 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1540 | if (transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1541 | RTC_LOG(LS_INFO) << "Reusing an existing " |
| 1542 | << cricket::MediaTypeToString(transceiver->media_type()) |
| 1543 | << " transceiver for AddTrack."; |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 1544 | if (transceiver->stopping()) { |
| 1545 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1546 | "The existing transceiver is stopping."); |
| 1547 | } |
| 1548 | |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1549 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1550 | transceiver->internal()->set_direction( |
| 1551 | RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1552 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1553 | transceiver->internal()->set_direction( |
| 1554 | RtpTransceiverDirection::kSendOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1555 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1556 | transceiver->sender()->SetTrack(track); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1557 | transceiver->internal()->sender_internal()->set_stream_ids(stream_ids); |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 1558 | transceiver->internal()->set_reused_for_addtrack(true); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1559 | } else { |
| 1560 | cricket::MediaType media_type = |
| 1561 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1562 | ? cricket::MEDIA_TYPE_AUDIO |
| 1563 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1564 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1565 | << " transceiver in response to a call to AddTrack."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1566 | std::string sender_id = track->id(); |
| 1567 | // Avoid creating a sender with an existing ID by generating a random ID. |
| 1568 | // This can happen if this is the second time AddTrack has created a sender |
| 1569 | // for this track. |
| 1570 | if (FindSenderById(sender_id)) { |
| 1571 | sender_id = rtc::CreateRandomUuid(); |
| 1572 | } |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1573 | auto sender = CreateSender(media_type, sender_id, track, stream_ids, {}); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1574 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1575 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1576 | transceiver->internal()->set_created_by_addtrack(true); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1577 | transceiver->internal()->set_direction(RtpTransceiverDirection::kSendRecv); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1578 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1579 | return transceiver->sender(); |
| 1580 | } |
| 1581 | |
| 1582 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1583 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1584 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1585 | RTC_DCHECK(track); |
| 1586 | for (auto transceiver : transceivers_) { |
| 1587 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 1588 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1589 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 1590 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1591 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1592 | return transceiver; |
| 1593 | } |
| 1594 | } |
| 1595 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1599 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1600 | return RemoveTrackNew(sender).ok(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1601 | } |
| 1602 | |
Steve Anton | 24db573 | 2018-07-23 17:27:33 | [diff] [blame] | 1603 | RTCError PeerConnection::RemoveTrackNew( |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1604 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1605 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1606 | if (!sender) { |
| 1607 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1608 | } |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1609 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1610 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1611 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1612 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1613 | if (IsUnifiedPlan()) { |
| 1614 | auto transceiver = FindTransceiverBySender(sender); |
| 1615 | if (!transceiver || !sender->track()) { |
| 1616 | return RTCError::OK(); |
| 1617 | } |
| 1618 | sender->SetTrack(nullptr); |
| 1619 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1620 | transceiver->internal()->set_direction( |
| 1621 | RtpTransceiverDirection::kRecvOnly); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1622 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1623 | transceiver->internal()->set_direction( |
| 1624 | RtpTransceiverDirection::kInactive); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1625 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1626 | } else { |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1627 | bool removed; |
| 1628 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1629 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1630 | } else { |
| 1631 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1632 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1633 | } |
| 1634 | if (!removed) { |
| 1635 | LOG_AND_RETURN_ERROR( |
| 1636 | RTCErrorType::INVALID_PARAMETER, |
| 1637 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1638 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1639 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1640 | UpdateNegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1641 | return RTCError::OK(); |
| 1642 | } |
| 1643 | |
| 1644 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1645 | PeerConnection::FindTransceiverBySender( |
| 1646 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1647 | for (auto transceiver : transceivers_) { |
| 1648 | if (transceiver->sender() == sender) { |
| 1649 | return transceiver; |
| 1650 | } |
| 1651 | } |
| 1652 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1653 | } |
| 1654 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1655 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1656 | PeerConnection::AddTransceiver( |
| 1657 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1658 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1659 | } |
| 1660 | |
| 1661 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1662 | PeerConnection::AddTransceiver( |
| 1663 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1664 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1665 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1666 | RTC_CHECK(IsUnifiedPlan()) |
| 1667 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1668 | if (!track) { |
| 1669 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1670 | } |
| 1671 | cricket::MediaType media_type; |
| 1672 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1673 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1674 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1675 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1676 | } else { |
| 1677 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1678 | "Track kind is not audio or video"); |
| 1679 | } |
| 1680 | return AddTransceiver(media_type, track, init); |
| 1681 | } |
| 1682 | |
| 1683 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1684 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1685 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1686 | } |
| 1687 | |
| 1688 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1689 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1690 | const RtpTransceiverInit& init) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 1691 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1692 | RTC_CHECK(IsUnifiedPlan()) |
| 1693 | << "AddTransceiver is only available with Unified Plan SdpSemantics"; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1694 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1695 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1696 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1697 | "media type is not audio or video"); |
| 1698 | } |
| 1699 | return AddTransceiver(media_type, nullptr, init); |
| 1700 | } |
| 1701 | |
| 1702 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1703 | PeerConnection::AddTransceiver( |
| 1704 | cricket::MediaType media_type, |
| 1705 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1706 | const RtpTransceiverInit& init, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1707 | bool update_negotiation_needed) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1708 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1709 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1710 | if (track) { |
| 1711 | RTC_DCHECK_EQ(media_type, |
| 1712 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1713 | ? cricket::MEDIA_TYPE_AUDIO |
| 1714 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1715 | } |
| 1716 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 1717 | RTC_HISTOGRAM_COUNTS_LINEAR(kSimulcastNumberOfEncodings, |
| 1718 | init.send_encodings.size(), 0, 7, 8); |
| 1719 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1720 | size_t num_rids = absl::c_count_if(init.send_encodings, |
| 1721 | [](const RtpEncodingParameters& encoding) { |
| 1722 | return !encoding.rid.empty(); |
| 1723 | }); |
| 1724 | if (num_rids > 0 && num_rids != init.send_encodings.size()) { |
Amit Hilbuch | ce470aa | 2019-02-07 01:09:52 | [diff] [blame] | 1725 | LOG_AND_RETURN_ERROR( |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1726 | RTCErrorType::INVALID_PARAMETER, |
| 1727 | "RIDs must be provided for either all or none of the send encodings."); |
Emircan Uysaler | 7832343 | 2019-02-08 20:41:39 | [diff] [blame] | 1728 | } |
| 1729 | |
Amit Hilbuch | f477040 | 2019-04-08 21:11:57 | [diff] [blame] | 1730 | if (num_rids > 0 && absl::c_any_of(init.send_encodings, |
| 1731 | [](const RtpEncodingParameters& encoding) { |
| 1732 | return !IsLegalRsidName(encoding.rid); |
| 1733 | })) { |
| 1734 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1735 | "Invalid RID value provided."); |
| 1736 | } |
| 1737 | |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1738 | if (absl::c_any_of(init.send_encodings, |
| 1739 | [](const RtpEncodingParameters& encoding) { |
| 1740 | return encoding.ssrc.has_value(); |
| 1741 | })) { |
| 1742 | LOG_AND_RETURN_ERROR( |
| 1743 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1744 | "Attempted to set an unimplemented parameter of RtpParameters."); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | RtpParameters parameters; |
| 1748 | parameters.encodings = init.send_encodings; |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1749 | |
| 1750 | // Encodings are dropped from the tail if too many are provided. |
| 1751 | if (parameters.encodings.size() > kMaxSimulcastStreams) { |
| 1752 | parameters.encodings.erase( |
| 1753 | parameters.encodings.begin() + kMaxSimulcastStreams, |
| 1754 | parameters.encodings.end()); |
| 1755 | } |
| 1756 | |
| 1757 | // Single RID should be removed. |
| 1758 | if (parameters.encodings.size() == 1 && |
| 1759 | !parameters.encodings[0].rid.empty()) { |
| 1760 | RTC_LOG(LS_INFO) << "Removing RID: " << parameters.encodings[0].rid << "."; |
| 1761 | parameters.encodings[0].rid.clear(); |
| 1762 | } |
| 1763 | |
| 1764 | // If RIDs were not provided, they are generated for simulcast scenario. |
| 1765 | if (parameters.encodings.size() > 1 && num_rids == 0) { |
| 1766 | rtc::UniqueStringGenerator rid_generator; |
| 1767 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 1768 | encoding.rid = rid_generator(); |
| 1769 | } |
| 1770 | } |
| 1771 | |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1772 | if (UnimplementedRtpParameterHasValue(parameters)) { |
| 1773 | LOG_AND_RETURN_ERROR( |
| 1774 | RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1775 | "Attempted to set an unimplemented parameter of RtpParameters."); |
| 1776 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1777 | |
Florent Castelli | c1a0bcb | 2019-01-29 13:26:48 | [diff] [blame] | 1778 | auto result = cricket::CheckRtpParametersValues(parameters); |
| 1779 | if (!result.ok()) { |
| 1780 | LOG_AND_RETURN_ERROR(result.type(), result.message()); |
| 1781 | } |
| 1782 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 1783 | RTC_LOG(LS_INFO) << "Adding " << cricket::MediaTypeToString(media_type) |
| 1784 | << " transceiver in response to a call to AddTransceiver."; |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1785 | // Set the sender ID equal to the track ID if the track is specified unless |
| 1786 | // that sender ID is already in use. |
| 1787 | std::string sender_id = |
| 1788 | (track && !FindSenderById(track->id()) ? track->id() |
| 1789 | : rtc::CreateRandomUuid()); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1790 | auto sender = CreateSender(media_type, sender_id, track, init.stream_ids, |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 1791 | parameters.encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1792 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1793 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1794 | transceiver->internal()->set_direction(init.direction); |
| 1795 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1796 | if (update_negotiation_needed) { |
| 1797 | UpdateNegotiationNeeded(); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 1798 | } |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1799 | |
| 1800 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1801 | } |
| 1802 | |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1803 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1804 | PeerConnection::CreateSender( |
| 1805 | cricket::MediaType media_type, |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1806 | const std::string& id, |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1807 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1808 | const std::vector<std::string>& stream_ids, |
| 1809 | const std::vector<RtpEncodingParameters>& send_encodings) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 1810 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1811 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1812 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1813 | RTC_DCHECK(!track || |
| 1814 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1815 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1816 | signaling_thread(), |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1817 | AudioRtpSender::Create(worker_thread(), id, stats_.get(), this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1818 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1819 | } else { |
| 1820 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1821 | RTC_DCHECK(!track || |
| 1822 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1823 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1824 | signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this)); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1825 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1826 | } |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 1827 | bool set_track_succeeded = sender->SetTrack(track); |
| 1828 | RTC_DCHECK(set_track_succeeded); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1829 | sender->internal()->set_stream_ids(stream_ids); |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 1830 | sender->internal()->set_init_send_encodings(send_encodings); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1831 | return sender; |
| 1832 | } |
| 1833 | |
| 1834 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1835 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1836 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1837 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1838 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1839 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1840 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1841 | signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id, |
| 1842 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1843 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1844 | } else { |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1845 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1846 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 1847 | signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id, |
| 1848 | std::vector<std::string>({}))); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 1849 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1850 | } |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1851 | return receiver; |
| 1852 | } |
| 1853 | |
| 1854 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1855 | PeerConnection::CreateAndAddTransceiver( |
| 1856 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1857 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1858 | receiver) { |
Steve Anton | 0756373 | 2018-06-26 18:13:50 | [diff] [blame] | 1859 | // Ensure that the new sender does not have an ID that is already in use by |
| 1860 | // another sender. |
| 1861 | // Allow receiver IDs to conflict since those come from remote SDP (which |
| 1862 | // could be invalid, but should not cause a crash). |
| 1863 | RTC_DCHECK(!FindSenderById(sender->id())); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1864 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 1865 | signaling_thread(), |
Markus Handell | 0357b3e | 2020-03-16 12:40:51 | [diff] [blame] | 1866 | new RtpTransceiver( |
| 1867 | sender, receiver, channel_manager(), |
| 1868 | sender->media_type() == cricket::MEDIA_TYPE_AUDIO |
| 1869 | ? channel_manager()->GetSupportedAudioRtpHeaderExtensions() |
| 1870 | : channel_manager()->GetSupportedVideoRtpHeaderExtensions())); |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1871 | transceivers_.push_back(transceiver); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1872 | transceiver->internal()->SignalNegotiationNeeded.connect( |
| 1873 | this, &PeerConnection::OnNegotiationNeeded); |
Steve Anton | f9381f0 | 2017-12-14 18:23:57 | [diff] [blame] | 1874 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1875 | } |
| 1876 | |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1877 | void PeerConnection::OnNegotiationNeeded() { |
| 1878 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1879 | RTC_DCHECK(!IsClosed()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 1880 | UpdateNegotiationNeeded(); |
Steve Anton | 52d8677 | 2018-02-20 23:48:12 | [diff] [blame] | 1881 | } |
| 1882 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1883 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-19 00:58:44 | [diff] [blame] | 1884 | const std::string& kind, |
| 1885 | const std::string& stream_id) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1886 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1887 | RTC_CHECK(!IsUnifiedPlan()) << "CreateSender is not available with Unified " |
| 1888 | "Plan SdpSemantics. Please use AddTransceiver " |
| 1889 | "instead."; |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 1890 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 1891 | if (IsClosed()) { |
| 1892 | return nullptr; |
| 1893 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1894 | |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1895 | // Internally we need to have one stream with Plan B semantics, so we |
| 1896 | // generate a random stream ID if not specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1897 | std::vector<std::string> stream_ids; |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 1898 | if (stream_id.empty()) { |
| 1899 | stream_ids.push_back(rtc::CreateRandomUuid()); |
| 1900 | RTC_LOG(LS_INFO) |
| 1901 | << "No stream_id specified for sender. Generated stream ID: " |
| 1902 | << stream_ids[0]; |
| 1903 | } else { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 1904 | stream_ids.push_back(stream_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1905 | } |
| 1906 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1907 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1908 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1909 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1910 | auto audio_sender = AudioRtpSender::Create( |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1911 | worker_thread(), rtc::CreateRandomUuid(), stats_.get(), this); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1912 | audio_sender->SetMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1913 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1914 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1915 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1916 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Amit Hilbuch | ea7ef2a | 2019-02-19 23:20:21 | [diff] [blame] | 1917 | auto video_sender = |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 1918 | VideoRtpSender::Create(worker_thread(), rtc::CreateRandomUuid(), this); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 1919 | video_sender->SetMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1920 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 1921 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1922 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1923 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 1924 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1925 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1926 | } |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 1927 | new_sender->internal()->set_stream_ids(stream_ids); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1928 | |
deadbeef | e1f9d83 | 2016-01-14 23:35:42 | [diff] [blame] | 1929 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 1930 | } |
| 1931 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1932 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1933 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1934 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1935 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1936 | for (const auto& sender : GetSendersInternal()) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1937 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1938 | } |
| 1939 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1940 | } |
| 1941 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1942 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1943 | PeerConnection::GetSendersInternal() const { |
| 1944 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1945 | all_senders; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1946 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 1947 | if (IsUnifiedPlan() && transceiver->internal()->stopped()) |
| 1948 | continue; |
| 1949 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1950 | auto senders = transceiver->internal()->senders(); |
| 1951 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1952 | } |
| 1953 | return all_senders; |
| 1954 | } |
| 1955 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1956 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1957 | PeerConnection::GetReceivers() const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 1958 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1959 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1960 | for (const auto& receiver : GetReceiversInternal()) { |
| 1961 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 21:27:39 | [diff] [blame] | 1962 | } |
| 1963 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 1964 | } |
| 1965 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1966 | std::vector< |
| 1967 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1968 | PeerConnection::GetReceiversInternal() const { |
| 1969 | std::vector< |
| 1970 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1971 | all_receivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1972 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 1973 | if (IsUnifiedPlan() && transceiver->internal()->stopped()) |
| 1974 | continue; |
| 1975 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 1976 | auto receivers = transceiver->internal()->receivers(); |
| 1977 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1978 | receivers.end()); |
| 1979 | } |
| 1980 | return all_receivers; |
| 1981 | } |
| 1982 | |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1983 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1984 | PeerConnection::GetTransceivers() const { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 1985 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | fc85371 | 2018-03-01 21:48:58 | [diff] [blame] | 1986 | RTC_CHECK(IsUnifiedPlan()) |
| 1987 | << "GetTransceivers is only supported with Unified Plan SdpSemantics."; |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1988 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 1989 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 1990 | // Temporary fix: Do not show stopped transceivers. |
| 1991 | // The long term fix is to remove them from transceivers_, but this |
| 1992 | // turns out to cause issues with audio channel lifetimes. |
| 1993 | // TODO(https://crbug.com/webrtc/11840): Fix issue. |
| 1994 | if (!transceiver->stopped()) { |
| 1995 | all_transceivers.push_back(transceiver); |
| 1996 | } |
Steve Anton | 9158ef6 | 2017-11-27 21:01:52 | [diff] [blame] | 1997 | } |
| 1998 | return all_transceivers; |
| 1999 | } |
| 2000 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2001 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 | [diff] [blame] | 2002 | MediaStreamTrackInterface* track, |
| 2003 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2004 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2005 | RTC_DCHECK_RUN_ON(signaling_thread()); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2006 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2007 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2008 | return false; |
| 2009 | } |
| 2010 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2011 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 2012 | // The StatsCollector is used to tell if a track is valid because it may |
| 2013 | // remember tracks that the PeerConnection previously removed. |
| 2014 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2015 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 2016 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 18:38:15 | [diff] [blame] | 2017 | return false; |
| 2018 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 2019 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
| 2020 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2021 | return true; |
| 2022 | } |
| 2023 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2024 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2025 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2026 | RTC_DCHECK_RUN_ON(signaling_thread()); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2027 | RTC_DCHECK(stats_collector_); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2028 | RTC_DCHECK(callback); |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 2029 | stats_collector_->GetStatsReport(callback); |
| 2030 | } |
| 2031 | |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2032 | void PeerConnection::GetStats( |
| 2033 | rtc::scoped_refptr<RtpSenderInterface> selector, |
| 2034 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2035 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2036 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2037 | RTC_DCHECK(callback); |
| 2038 | RTC_DCHECK(stats_collector_); |
| 2039 | rtc::scoped_refptr<RtpSenderInternal> internal_sender; |
| 2040 | if (selector) { |
| 2041 | for (const auto& proxy_transceiver : transceivers_) { |
| 2042 | for (const auto& proxy_sender : |
| 2043 | proxy_transceiver->internal()->senders()) { |
| 2044 | if (proxy_sender == selector) { |
| 2045 | internal_sender = proxy_sender->internal(); |
| 2046 | break; |
| 2047 | } |
| 2048 | } |
| 2049 | if (internal_sender) |
| 2050 | break; |
| 2051 | } |
| 2052 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 2053 | // If there is no |internal_sender| then |selector| is either null or does not |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2054 | // belong to the PeerConnection (in Plan B, senders can be removed from the |
| 2055 | // PeerConnection). This means that "all the stats objects representing the |
| 2056 | // selector" is an empty set. Invoking GetStatsReport() with a null selector |
| 2057 | // produces an empty stats report. |
| 2058 | stats_collector_->GetStatsReport(internal_sender, callback); |
| 2059 | } |
| 2060 | |
| 2061 | void PeerConnection::GetStats( |
| 2062 | rtc::scoped_refptr<RtpReceiverInterface> selector, |
| 2063 | rtc::scoped_refptr<RTCStatsCollectorCallback> callback) { |
| 2064 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 2065 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2066 | RTC_DCHECK(callback); |
| 2067 | RTC_DCHECK(stats_collector_); |
| 2068 | rtc::scoped_refptr<RtpReceiverInternal> internal_receiver; |
| 2069 | if (selector) { |
| 2070 | for (const auto& proxy_transceiver : transceivers_) { |
| 2071 | for (const auto& proxy_receiver : |
| 2072 | proxy_transceiver->internal()->receivers()) { |
| 2073 | if (proxy_receiver == selector) { |
| 2074 | internal_receiver = proxy_receiver->internal(); |
| 2075 | break; |
| 2076 | } |
| 2077 | } |
| 2078 | if (internal_receiver) |
| 2079 | break; |
| 2080 | } |
| 2081 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 2082 | // If there is no |internal_receiver| then |selector| is either null or does |
Henrik Boström | 1df1bf8 | 2018-03-20 12:24:20 | [diff] [blame] | 2083 | // not belong to the PeerConnection (in Plan B, receivers can be removed from |
| 2084 | // the PeerConnection). This means that "all the stats objects representing |
| 2085 | // the selector" is an empty set. Invoking GetStatsReport() with a null |
| 2086 | // selector produces an empty stats report. |
| 2087 | stats_collector_->GetStatsReport(internal_receiver, callback); |
| 2088 | } |
| 2089 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2090 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2091 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2092 | return signaling_state_; |
| 2093 | } |
| 2094 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2095 | PeerConnectionInterface::IceConnectionState |
| 2096 | PeerConnection::ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2097 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2098 | return ice_connection_state_; |
| 2099 | } |
| 2100 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2101 | PeerConnectionInterface::IceConnectionState |
| 2102 | PeerConnection::standardized_ice_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2103 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 2104 | return standardized_ice_connection_state_; |
| 2105 | } |
| 2106 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 2107 | PeerConnectionInterface::PeerConnectionState |
| 2108 | PeerConnection::peer_connection_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2109 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 2110 | return connection_state_; |
| 2111 | } |
| 2112 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2113 | PeerConnectionInterface::IceGatheringState |
| 2114 | PeerConnection::ice_gathering_state() { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2115 | RTC_DCHECK_RUN_ON(signaling_thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2116 | return ice_gathering_state_; |
| 2117 | } |
| 2118 | |
Harald Alvestrand | 61f74d9 | 2020-03-02 10:20:00 | [diff] [blame] | 2119 | absl::optional<bool> PeerConnection::can_trickle_ice_candidates() { |
| 2120 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2121 | SessionDescriptionInterface* description = current_remote_description_.get(); |
| 2122 | if (!description) { |
| 2123 | description = pending_remote_description_.get(); |
| 2124 | } |
| 2125 | if (!description) { |
| 2126 | return absl::nullopt; |
| 2127 | } |
| 2128 | // TODO(bugs.webrtc.org/7443): Change to retrieve from session-level option. |
| 2129 | if (description->description()->transport_infos().size() < 1) { |
| 2130 | return absl::nullopt; |
| 2131 | } |
| 2132 | return description->description()->transport_infos()[0].description.HasOption( |
| 2133 | "trickle"); |
| 2134 | } |
| 2135 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 2136 | rtc::scoped_refptr<DataChannelInterface> PeerConnection::CreateDataChannel( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2137 | const std::string& label, |
| 2138 | const DataChannelInit* config) { |
Karl Wiberg | 106d92d | 2019-02-14 09:17:47 | [diff] [blame] | 2139 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 2140 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 18:14:50 | [diff] [blame] | 2141 | |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 2142 | bool first_datachannel = !data_channel_controller_.HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2143 | |
kwiberg | d1fe281 | 2016-04-27 13:47:29 | [diff] [blame] | 2144 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 | [diff] [blame] | 2145 | if (config) { |
| 2146 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 2147 | } |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2148 | rtc::scoped_refptr<DataChannelInterface> channel( |
| 2149 | data_channel_controller_.InternalCreateDataChannelWithProxy( |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2150 | label, internal_config.get())); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2151 | if (!channel.get()) { |
| 2152 | return nullptr; |
| 2153 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2154 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2155 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 2156 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 2157 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2158 | UpdateNegotiationNeeded(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 | [diff] [blame] | 2159 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 2160 | NoteUsageEvent(UsageEvent::DATA_ADDED); |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2161 | return channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2162 | } |
| 2163 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 2164 | void PeerConnection::RestartIce() { |
| 2165 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2166 | local_ice_credentials_to_replace_->SetIceCredentialsFromLocalDescriptions( |
| 2167 | current_local_description_.get(), pending_local_description_.get()); |
| 2168 | UpdateNegotiationNeeded(); |
| 2169 | } |
| 2170 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2171 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 2172 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2173 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2174 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2175 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2176 | // lambda will execute immediately. |
| 2177 | operations_chain_->ChainOperation( |
| 2178 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2179 | observer_refptr = |
| 2180 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2181 | options](std::function<void()> operations_chain_callback) { |
| 2182 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2183 | if (!this_weak_ptr) { |
| 2184 | observer_refptr->OnFailure( |
| 2185 | RTCError(RTCErrorType::INTERNAL_ERROR, |
| 2186 | "CreateOffer failed because the session was shut down")); |
| 2187 | operations_chain_callback(); |
| 2188 | return; |
| 2189 | } |
| 2190 | // The operation completes asynchronously when the wrapper is invoked. |
| 2191 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2192 | observer_wrapper(new rtc::RefCountedObject< |
| 2193 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2194 | std::move(observer_refptr), |
| 2195 | std::move(operations_chain_callback))); |
| 2196 | this_weak_ptr->DoCreateOffer(options, observer_wrapper); |
| 2197 | }); |
| 2198 | } |
| 2199 | |
| 2200 | void PeerConnection::DoCreateOffer( |
| 2201 | const RTCOfferAnswerOptions& options, |
| 2202 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2203 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2204 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2205 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2206 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2207 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 | [diff] [blame] | 2208 | return; |
| 2209 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2210 | |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2211 | if (IsClosed()) { |
| 2212 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2213 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2214 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2215 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2216 | return; |
| 2217 | } |
| 2218 | |
Steve Anton | 25ca0ac | 2019-06-25 17:40:48 | [diff] [blame] | 2219 | // If a session error has occurred the PeerConnection is in a possibly |
| 2220 | // inconsistent state so fail right away. |
| 2221 | if (session_error() != SessionError::kNone) { |
| 2222 | std::string error_message = GetSessionErrorMsg(); |
| 2223 | RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message; |
| 2224 | PostCreateSessionDescriptionFailure( |
| 2225 | observer, |
| 2226 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2227 | return; |
| 2228 | } |
| 2229 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2230 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2231 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2232 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2233 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2234 | observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error))); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2235 | return; |
| 2236 | } |
| 2237 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2238 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 2239 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 2240 | if (IsUnifiedPlan()) { |
| 2241 | RTCError error = HandleLegacyOfferOptions(options); |
| 2242 | if (!error.ok()) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2243 | PostCreateSessionDescriptionFailure(observer, std::move(error)); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2244 | return; |
| 2245 | } |
| 2246 | } |
| 2247 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2248 | cricket::MediaSessionOptions session_options; |
| 2249 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2250 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2251 | } |
| 2252 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2253 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 2254 | const RTCOfferAnswerOptions& options) { |
| 2255 | RTC_DCHECK(IsUnifiedPlan()); |
| 2256 | |
| 2257 | if (options.offer_to_receive_audio == 0) { |
| 2258 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2259 | cricket::MEDIA_TYPE_AUDIO); |
| 2260 | } else if (options.offer_to_receive_audio == 1) { |
| 2261 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 2262 | } else if (options.offer_to_receive_audio > 1) { |
| 2263 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2264 | "offer_to_receive_audio > 1 is not supported."); |
| 2265 | } |
| 2266 | |
| 2267 | if (options.offer_to_receive_video == 0) { |
| 2268 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2269 | cricket::MEDIA_TYPE_VIDEO); |
| 2270 | } else if (options.offer_to_receive_video == 1) { |
| 2271 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 2272 | } else if (options.offer_to_receive_video > 1) { |
| 2273 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 2274 | "offer_to_receive_video > 1 is not supported."); |
| 2275 | } |
| 2276 | |
| 2277 | return RTCError::OK(); |
| 2278 | } |
| 2279 | |
| 2280 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 2281 | cricket::MediaType media_type) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2282 | for (const auto& transceiver : GetReceivingTransceiversOfType(media_type)) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2283 | RtpTransceiverDirection new_direction = |
| 2284 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false); |
| 2285 | if (new_direction != transceiver->direction()) { |
| 2286 | RTC_LOG(LS_INFO) << "Changing " << cricket::MediaTypeToString(media_type) |
| 2287 | << " transceiver (MID=" |
| 2288 | << transceiver->mid().value_or("<not set>") << ") from " |
| 2289 | << RtpTransceiverDirectionToString( |
| 2290 | transceiver->direction()) |
| 2291 | << " to " |
| 2292 | << RtpTransceiverDirectionToString(new_direction) |
| 2293 | << " since CreateOffer specified offer_to_receive=0"; |
| 2294 | transceiver->internal()->set_direction(new_direction); |
| 2295 | } |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 2300 | cricket::MediaType media_type) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 2301 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2302 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 2303 | RTC_LOG(LS_INFO) |
| 2304 | << "Adding one recvonly " << cricket::MediaTypeToString(media_type) |
| 2305 | << " transceiver since CreateOffer specified offer_to_receive=1"; |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2306 | RtpTransceiverInit init; |
| 2307 | init.direction = RtpTransceiverDirection::kRecvOnly; |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2308 | AddTransceiver(media_type, nullptr, init, |
| 2309 | /*update_negotiation_needed=*/false); |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2314 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 2315 | std::vector< |
| 2316 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2317 | receiving_transceivers; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2318 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 2319 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2320 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 2321 | receiving_transceivers.push_back(transceiver); |
| 2322 | } |
| 2323 | } |
| 2324 | return receiving_transceivers; |
| 2325 | } |
| 2326 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2327 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 2328 | const RTCOfferAnswerOptions& options) { |
Karl Wiberg | 8d2e228 | 2019-02-17 12:00:07 | [diff] [blame] | 2329 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2330 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2331 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2332 | // lambda will execute immediately. |
| 2333 | operations_chain_->ChainOperation( |
| 2334 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2335 | observer_refptr = |
| 2336 | rtc::scoped_refptr<CreateSessionDescriptionObserver>(observer), |
| 2337 | options](std::function<void()> operations_chain_callback) { |
| 2338 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2339 | if (!this_weak_ptr) { |
| 2340 | observer_refptr->OnFailure(RTCError( |
| 2341 | RTCErrorType::INTERNAL_ERROR, |
| 2342 | "CreateAnswer failed because the session was shut down")); |
| 2343 | operations_chain_callback(); |
| 2344 | return; |
| 2345 | } |
| 2346 | // The operation completes asynchronously when the wrapper is invoked. |
| 2347 | rtc::scoped_refptr<CreateSessionDescriptionObserverOperationWrapper> |
| 2348 | observer_wrapper(new rtc::RefCountedObject< |
| 2349 | CreateSessionDescriptionObserverOperationWrapper>( |
| 2350 | std::move(observer_refptr), |
| 2351 | std::move(operations_chain_callback))); |
| 2352 | this_weak_ptr->DoCreateAnswer(options, observer_wrapper); |
| 2353 | }); |
| 2354 | } |
| 2355 | |
| 2356 | void PeerConnection::DoCreateAnswer( |
| 2357 | const RTCOfferAnswerOptions& options, |
| 2358 | rtc::scoped_refptr<CreateSessionDescriptionObserver> observer) { |
| 2359 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2360 | TRACE_EVENT0("webrtc", "PeerConnection::DoCreateAnswer"); |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2361 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2362 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2363 | return; |
| 2364 | } |
| 2365 | |
Steve Anton | 25ca0ac | 2019-06-25 17:40:48 | [diff] [blame] | 2366 | // If a session error has occurred the PeerConnection is in a possibly |
| 2367 | // inconsistent state so fail right away. |
| 2368 | if (session_error() != SessionError::kNone) { |
| 2369 | std::string error_message = GetSessionErrorMsg(); |
| 2370 | RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message; |
| 2371 | PostCreateSessionDescriptionFailure( |
| 2372 | observer, |
| 2373 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2374 | return; |
| 2375 | } |
| 2376 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2377 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 2378 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 2379 | std::string error = |
| 2380 | "PeerConnection cannot create an answer in a state other than " |
| 2381 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2382 | RTC_LOG(LS_ERROR) << error; |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2383 | PostCreateSessionDescriptionFailure( |
Harald Alvestrand | 3725d54 | 2018-04-13 13:02:10 | [diff] [blame] | 2384 | observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2385 | return; |
| 2386 | } |
| 2387 | |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 2388 | // The remote description should be set if we're in the right state. |
| 2389 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2390 | |
Steve Anton | 22da89f | 2018-01-25 21:58:07 | [diff] [blame] | 2391 | if (IsUnifiedPlan()) { |
| 2392 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 2393 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 2394 | "supported with Unified Plan semantics. Use the " |
| 2395 | "RtpTransceiver API instead."; |
| 2396 | } |
| 2397 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 2398 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 2399 | "supported with Unified Plan semantics. Use the " |
| 2400 | "RtpTransceiver API instead."; |
| 2401 | } |
| 2402 | } |
| 2403 | |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2404 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 2405 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 2406 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 2407 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | void PeerConnection::SetLocalDescription( |
| 2411 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2412 | SessionDescriptionInterface* desc_ptr) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2413 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2414 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2415 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2416 | // lambda will execute immediately. |
| 2417 | operations_chain_->ChainOperation( |
| 2418 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2419 | observer_refptr = |
| 2420 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2421 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2422 | std::function<void()> operations_chain_callback) mutable { |
| 2423 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2424 | if (!this_weak_ptr) { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2425 | // For consistency with SetSessionDescriptionObserverAdapter whose |
| 2426 | // posted messages doesn't get processed when the PC is destroyed, we |
| 2427 | // do not inform |observer_refptr| that the operation failed. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2428 | operations_chain_callback(); |
| 2429 | return; |
| 2430 | } |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2431 | // SetSessionDescriptionObserverAdapter takes care of making sure the |
| 2432 | // |observer_refptr| is invoked in a posted message. |
| 2433 | this_weak_ptr->DoSetLocalDescription( |
| 2434 | std::move(desc), |
| 2435 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface>( |
| 2436 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 2437 | this_weak_ptr, observer_refptr))); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2438 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2439 | // completed here (rather than in a post), so that the operation chain |
| 2440 | // is not blocked by this operation when the observer is invoked. This |
| 2441 | // allows the observer to trigger subsequent offer/answer operations |
| 2442 | // synchronously if the operation chain is now empty. |
| 2443 | operations_chain_callback(); |
| 2444 | }); |
| 2445 | } |
| 2446 | |
| 2447 | void PeerConnection::SetLocalDescription( |
| 2448 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2449 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 2450 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2451 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2452 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2453 | // lambda will execute immediately. |
| 2454 | operations_chain_->ChainOperation( |
| 2455 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 2456 | desc = std::move(desc)]( |
| 2457 | std::function<void()> operations_chain_callback) mutable { |
| 2458 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2459 | if (!this_weak_ptr) { |
| 2460 | observer->OnSetLocalDescriptionComplete(RTCError( |
| 2461 | RTCErrorType::INTERNAL_ERROR, |
| 2462 | "SetLocalDescription failed because the session was shut down")); |
| 2463 | operations_chain_callback(); |
| 2464 | return; |
| 2465 | } |
| 2466 | this_weak_ptr->DoSetLocalDescription(std::move(desc), observer); |
| 2467 | // DoSetLocalDescription() is implemented as a synchronous operation. |
| 2468 | // The |observer| will already have been informed that it completed, and |
| 2469 | // we can mark this operation as complete without any loose ends. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2470 | operations_chain_callback(); |
| 2471 | }); |
| 2472 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2473 | |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2474 | void PeerConnection::SetLocalDescription( |
| 2475 | SetSessionDescriptionObserver* observer) { |
| 2476 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2477 | SetLocalDescription( |
| 2478 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 2479 | weak_ptr_factory_.GetWeakPtr(), observer)); |
| 2480 | } |
| 2481 | |
| 2482 | void PeerConnection::SetLocalDescription( |
| 2483 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
| 2484 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2485 | // The |create_sdp_observer| handles performing DoSetLocalDescription() with |
| 2486 | // the resulting description as well as completing the operation. |
| 2487 | rtc::scoped_refptr<ImplicitCreateSessionDescriptionObserver> |
| 2488 | create_sdp_observer( |
| 2489 | new rtc::RefCountedObject<ImplicitCreateSessionDescriptionObserver>( |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2490 | weak_ptr_factory_.GetWeakPtr(), observer)); |
Henrik Boström | 4e19670 | 2019-10-30 09:35:50 | [diff] [blame] | 2491 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2492 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2493 | // lambda will execute immediately. |
| 2494 | operations_chain_->ChainOperation( |
| 2495 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2496 | create_sdp_observer](std::function<void()> operations_chain_callback) { |
| 2497 | // The |create_sdp_observer| is responsible for completing the |
| 2498 | // operation. |
| 2499 | create_sdp_observer->SetOperationCompleteCallback( |
| 2500 | std::move(operations_chain_callback)); |
| 2501 | // Abort early if |this_weak_ptr| is no longer valid. This triggers the |
| 2502 | // same code path as if DoCreateOffer() or DoCreateAnswer() failed. |
| 2503 | if (!this_weak_ptr) { |
| 2504 | create_sdp_observer->OnFailure(RTCError( |
| 2505 | RTCErrorType::INTERNAL_ERROR, |
| 2506 | "SetLocalDescription failed because the session was shut down")); |
| 2507 | return; |
| 2508 | } |
| 2509 | switch (this_weak_ptr->signaling_state()) { |
| 2510 | case PeerConnectionInterface::kStable: |
| 2511 | case PeerConnectionInterface::kHaveLocalOffer: |
| 2512 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 2513 | // TODO(hbos): If [LastCreatedOffer] exists and still represents the |
| 2514 | // current state of the system, use that instead of creating another |
| 2515 | // offer. |
| 2516 | this_weak_ptr->DoCreateOffer(RTCOfferAnswerOptions(), |
| 2517 | create_sdp_observer); |
| 2518 | break; |
| 2519 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 2520 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 2521 | // TODO(hbos): If [LastCreatedAnswer] exists and still represents |
| 2522 | // the current state of the system, use that instead of creating |
| 2523 | // another answer. |
| 2524 | this_weak_ptr->DoCreateAnswer(RTCOfferAnswerOptions(), |
| 2525 | create_sdp_observer); |
| 2526 | break; |
| 2527 | case PeerConnectionInterface::kClosed: |
| 2528 | create_sdp_observer->OnFailure(RTCError( |
| 2529 | RTCErrorType::INVALID_STATE, |
| 2530 | "SetLocalDescription called when PeerConnection is closed.")); |
| 2531 | break; |
| 2532 | } |
| 2533 | }); |
| 2534 | } |
| 2535 | |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2536 | void PeerConnection::DoSetLocalDescription( |
| 2537 | std::unique_ptr<SessionDescriptionInterface> desc, |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2538 | rtc::scoped_refptr<SetLocalDescriptionObserverInterface> observer) { |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2539 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2540 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetLocalDescription"); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2541 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 2542 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 2543 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2544 | return; |
| 2545 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2546 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2547 | if (!desc) { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2548 | observer->OnSetLocalDescriptionComplete( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2549 | RTCError(RTCErrorType::INTERNAL_ERROR, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2550 | return; |
| 2551 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2552 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2553 | // If a session error has occurred the PeerConnection is in a possibly |
| 2554 | // inconsistent state so fail right away. |
| 2555 | if (session_error() != SessionError::kNone) { |
| 2556 | std::string error_message = GetSessionErrorMsg(); |
| 2557 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2558 | observer->OnSetLocalDescriptionComplete( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2559 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2560 | return; |
| 2561 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2562 | |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2563 | // For SLD we support only explicit rollback. |
| 2564 | if (desc->GetType() == SdpType::kRollback) { |
| 2565 | if (IsUnifiedPlan()) { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2566 | observer->OnSetLocalDescriptionComplete(Rollback(desc->GetType())); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2567 | } else { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2568 | observer->OnSetLocalDescriptionComplete( |
| 2569 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 2570 | "Rollback not supported in Plan B")); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 2571 | } |
| 2572 | return; |
| 2573 | } |
| 2574 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2575 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 2576 | if (!error.ok()) { |
| 2577 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2578 | cricket::CS_LOCAL, desc->GetType(), error); |
| 2579 | RTC_LOG(LS_ERROR) << error_message; |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2580 | observer->OnSetLocalDescriptionComplete( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2581 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2582 | return; |
| 2583 | } |
| 2584 | |
| 2585 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 2586 | // which may destroy it before returning. |
| 2587 | const SdpType type = desc->GetType(); |
| 2588 | |
| 2589 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2590 | // |desc| may be destroyed at this point. |
| 2591 | |
| 2592 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 2593 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 2594 | // inconsistent state, so act conservatively here and set the session error |
| 2595 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2596 | SetSessionError(SessionError::kContent, error.message()); |
| 2597 | std::string error_message = |
| 2598 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 2599 | RTC_LOG(LS_ERROR) << error_message; |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2600 | observer->OnSetLocalDescriptionComplete( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 2601 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 2602 | return; |
| 2603 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2604 | RTC_DCHECK(local_description()); |
| 2605 | |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 2606 | if (local_description()->GetType() == SdpType::kAnswer) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 2607 | // 3.2.10.1: For each transceiver in the connection's set of transceivers |
| 2608 | // run the following steps: |
| 2609 | if (IsUnifiedPlan()) { |
| 2610 | for (auto it = transceivers_.begin(); it != transceivers_.end();) { |
| 2611 | const auto& transceiver = *it; |
| 2612 | // 3.2.10.1.1: If transceiver is stopped, associated with an m= section |
| 2613 | // and the associated m= section is rejected in |
| 2614 | // connection.[[CurrentLocalDescription]] or |
| 2615 | // connection.[[CurrentRemoteDescription]], remove the |
| 2616 | // transceiver from the connection's set of transceivers. |
| 2617 | if (transceiver->stopped()) { |
| 2618 | const ContentInfo* content = |
| 2619 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2620 | |
| 2621 | if (content && content->rejected) { |
| 2622 | RTC_LOG(LS_INFO) << "Dissociating transceiver" |
| 2623 | << " since the media section is being recycled."; |
| 2624 | (*it)->internal()->set_mid(absl::nullopt); |
| 2625 | (*it)->internal()->set_mline_index(absl::nullopt); |
| 2626 | it = transceivers_.erase(it); |
| 2627 | } else { |
| 2628 | ++it; |
| 2629 | } |
| 2630 | } else { |
| 2631 | ++it; |
| 2632 | } |
| 2633 | } |
| 2634 | } |
| 2635 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2636 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2637 | // MaybeStartGathering... |
| 2638 | network_thread()->Invoke<void>( |
| 2639 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2640 | port_allocator_.get())); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 2641 | // Make UMA notes about what was agreed to. |
| 2642 | ReportNegotiatedSdpSemantics(*local_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2643 | } |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2644 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2645 | observer->OnSetLocalDescriptionComplete(RTCError::OK()); |
| 2646 | NoteUsageEvent(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED); |
| 2647 | |
| 2648 | // Check if negotiation is needed. We must do this after informing the |
| 2649 | // observer that SetLocalDescription() has completed to ensure negotiation is |
| 2650 | // not needed prior to the promise resolving. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2651 | if (IsUnifiedPlan()) { |
| 2652 | bool was_negotiation_needed = is_negotiation_needed_; |
| 2653 | UpdateNegotiationNeeded(); |
| 2654 | if (signaling_state() == kStable && was_negotiation_needed && |
| 2655 | is_negotiation_needed_) { |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2656 | // Legacy version. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2657 | Observer()->OnRenegotiationNeeded(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 2658 | // Spec-compliant version; the event may get invalidated before firing. |
| 2659 | GenerateNegotiationNeededEvent(); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2663 | // MaybeStartGathering needs to be called after informing the observer so that |
| 2664 | // we don't signal any candidates before signaling that SetLocalDescription |
| 2665 | // completed. |
| 2666 | transport_controller_->MaybeStartGathering(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | RTCError PeerConnection::ApplyLocalDescription( |
| 2670 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2671 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2672 | RTC_DCHECK(desc); |
| 2673 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2674 | // Update stats here so that we have the most recent stats for tracks and |
| 2675 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 2676 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2677 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2678 | // Take a reference to the old local description since it's used below to |
| 2679 | // compare against the new local description. When setting the new local |
| 2680 | // description, grab ownership of the replaced session description in case it |
| 2681 | // is the same as |old_local_description|, to keep it alive for the duration |
| 2682 | // of the method. |
| 2683 | const SessionDescriptionInterface* old_local_description = |
| 2684 | local_description(); |
| 2685 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2686 | SdpType type = desc->GetType(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 2687 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2688 | replaced_local_description = pending_local_description_ |
| 2689 | ? std::move(pending_local_description_) |
| 2690 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2691 | current_local_description_ = std::move(desc); |
| 2692 | pending_local_description_ = nullptr; |
| 2693 | current_remote_description_ = std::move(pending_remote_description_); |
| 2694 | } else { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2695 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2696 | pending_local_description_ = std::move(desc); |
| 2697 | } |
| 2698 | // The session description to apply now must be accessed by |
| 2699 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2700 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2701 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 2702 | // Report statistics about any use of simulcast. |
| 2703 | ReportSimulcastApiVersion(kSimulcastVersionApplyLocalDescription, |
| 2704 | *local_description()->description()); |
| 2705 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 2706 | if (!is_caller_) { |
| 2707 | if (remote_description()) { |
| 2708 | // Remote description was applied first, so this PC is the callee. |
| 2709 | is_caller_ = false; |
| 2710 | } else { |
| 2711 | // Local description is applied first, so this PC is the caller. |
| 2712 | is_caller_ = true; |
| 2713 | } |
| 2714 | } |
| 2715 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2716 | RTCError error = PushdownTransportDescription(cricket::CS_LOCAL, type); |
| 2717 | if (!error.ok()) { |
| 2718 | return error; |
| 2719 | } |
| 2720 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2721 | if (IsUnifiedPlan()) { |
| 2722 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 2723 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 2724 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2725 | if (!error.ok()) { |
| 2726 | return error; |
| 2727 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2728 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
| 2729 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2730 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 2731 | if (transceiver->stopped()) { |
| 2732 | continue; |
| 2733 | } |
| 2734 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 2735 | // 2.2.7.1.1.(6-9): Set sender and receiver's transport slots. |
| 2736 | // Note that code paths that don't set MID won't be able to use |
| 2737 | // information about DTLS transports. |
| 2738 | if (transceiver->mid()) { |
| 2739 | auto dtls_transport = |
| 2740 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 2741 | transceiver->internal()->sender_internal()->set_transport( |
| 2742 | dtls_transport); |
| 2743 | transceiver->internal()->receiver_internal()->set_transport( |
| 2744 | dtls_transport); |
| 2745 | } |
| 2746 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2747 | const ContentInfo* content = |
| 2748 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2749 | if (!content) { |
| 2750 | continue; |
| 2751 | } |
| 2752 | const MediaContentDescription* media_desc = content->media_description(); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2753 | // 2.2.7.1.6: If description is of type "answer" or "pranswer", then run |
| 2754 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2755 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2756 | // 2.2.7.1.6.1: If direction is "sendonly" or "inactive", and |
| 2757 | // transceiver's [[FiredDirection]] slot is either "sendrecv" or |
| 2758 | // "recvonly", process the removal of a remote track for the media |
| 2759 | // description, given transceiver, removeList, and muteTracks. |
| 2760 | if (!RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 2761 | (transceiver->internal()->fired_direction() && |
| 2762 | RtpTransceiverDirectionHasRecv( |
| 2763 | *transceiver->internal()->fired_direction()))) { |
| 2764 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 2765 | &removed_streams); |
| 2766 | } |
| 2767 | // 2.2.7.1.6.2: Set transceiver's [[CurrentDirection]] and |
| 2768 | // [[FiredDirection]] slots to direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2769 | transceiver->internal()->set_current_direction(media_desc->direction()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2770 | transceiver->internal()->set_fired_direction(media_desc->direction()); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2771 | } |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2772 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2773 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2774 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2775 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 2776 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2777 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 2778 | observer->OnRemoveStream(stream); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2779 | } |
| 2780 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2781 | // Media channels will be created only when offer is set. These may use new |
| 2782 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2783 | if (type == SdpType::kOffer) { |
| 2784 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2785 | // description is applied. Restore back to old description. |
| 2786 | RTCError error = CreateChannels(*local_description()->description()); |
| 2787 | if (!error.ok()) { |
| 2788 | return error; |
| 2789 | } |
| 2790 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2791 | // Remove unused channels if MediaContentDescription is rejected. |
| 2792 | RemoveUnusedChannels(local_description()->description()); |
| 2793 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2794 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 2795 | error = UpdateSessionState(type, cricket::CS_LOCAL, |
| 2796 | local_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2797 | if (!error.ok()) { |
| 2798 | return error; |
| 2799 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2800 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2801 | if (remote_description()) { |
| 2802 | // Now that we have a local description, we can push down remote candidates. |
| 2803 | UseCandidatesInSessionDescription(remote_description()); |
| 2804 | } |
| 2805 | |
| 2806 | pending_ice_restarts_.clear(); |
| 2807 | if (session_error() != SessionError::kNone) { |
| 2808 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2809 | } |
| 2810 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2811 | // If setting the description decided our SSL role, allocate any necessary |
| 2812 | // SCTP sids. |
| 2813 | rtc::SSLRole role; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 2814 | if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 2815 | data_channel_controller_.AllocateSctpSids(role); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2816 | } |
| 2817 | |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2818 | if (IsUnifiedPlan()) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 2819 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 2820 | if (transceiver->stopped()) { |
| 2821 | continue; |
| 2822 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2823 | const ContentInfo* content = |
| 2824 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 2825 | if (!content) { |
| 2826 | continue; |
| 2827 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2828 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
| 2829 | if (content->rejected || !channel || channel->local_streams().empty()) { |
Steve Anton | 60b6c1d | 2018-06-13 18:32:27 | [diff] [blame] | 2830 | // 0 is a special value meaning "this sender has no associated send |
| 2831 | // stream". Need to call this so the sender won't attempt to configure |
| 2832 | // a no longer existing stream and run into DCHECKs in the lower |
| 2833 | // layers. |
| 2834 | transceiver->internal()->sender_internal()->SetSsrc(0); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2835 | } else { |
| 2836 | // Get the StreamParams from the channel which could generate SSRCs. |
| 2837 | const std::vector<StreamParams>& streams = channel->local_streams(); |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 2838 | transceiver->internal()->sender_internal()->set_stream_ids( |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 2839 | streams[0].stream_ids()); |
| 2840 | transceiver->internal()->sender_internal()->SetSsrc( |
| 2841 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | } else { |
| 2845 | // Plan B semantics. |
| 2846 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2847 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 2848 | // local session description. |
| 2849 | const cricket::ContentInfo* audio_content = |
| 2850 | GetFirstAudioContent(local_description()->description()); |
| 2851 | if (audio_content) { |
| 2852 | if (audio_content->rejected) { |
| 2853 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2854 | } else { |
| 2855 | const cricket::AudioContentDescription* audio_desc = |
| 2856 | audio_content->media_description()->as_audio(); |
| 2857 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 2858 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2859 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2860 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 2861 | const cricket::ContentInfo* video_content = |
| 2862 | GetFirstVideoContent(local_description()->description()); |
| 2863 | if (video_content) { |
| 2864 | if (video_content->rejected) { |
| 2865 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2866 | } else { |
| 2867 | const cricket::VideoContentDescription* video_desc = |
| 2868 | video_content->media_description()->as_video(); |
| 2869 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 2870 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 2871 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 2875 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2876 | if (data_content) { |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 2877 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 2878 | data_content->media_description()->as_rtp_data(); |
| 2879 | // rtp_data_desc will be null if this is an SCTP description. |
| 2880 | if (rtp_data_desc) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 2881 | data_channel_controller_.UpdateLocalRtpDataChannels( |
| 2882 | rtp_data_desc->streams()); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 2883 | } |
| 2884 | } |
| 2885 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 2886 | if (type == SdpType::kAnswer && |
| 2887 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 2888 | *current_local_description_)) { |
| 2889 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 2890 | } |
| 2891 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 2892 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2893 | } |
| 2894 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2895 | // The SDP parser used to populate these values by default for the 'content |
| 2896 | // name' if an a=mid line was absent. |
| 2897 | static absl::string_view GetDefaultMidForPlanB(cricket::MediaType media_type) { |
| 2898 | switch (media_type) { |
| 2899 | case cricket::MEDIA_TYPE_AUDIO: |
| 2900 | return cricket::CN_AUDIO; |
| 2901 | case cricket::MEDIA_TYPE_VIDEO: |
| 2902 | return cricket::CN_VIDEO; |
| 2903 | case cricket::MEDIA_TYPE_DATA: |
| 2904 | return cricket::CN_DATA; |
| 2905 | } |
| 2906 | RTC_NOTREACHED(); |
| 2907 | return ""; |
| 2908 | } |
| 2909 | |
| 2910 | void PeerConnection::FillInMissingRemoteMids( |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2911 | cricket::SessionDescription* new_remote_description) { |
| 2912 | RTC_DCHECK(new_remote_description); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2913 | const cricket::ContentInfos no_infos; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2914 | const cricket::ContentInfos& local_contents = |
| 2915 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2916 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2917 | const cricket::ContentInfos& remote_contents = |
| 2918 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 2919 | : no_infos); |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2920 | for (size_t i = 0; i < new_remote_description->contents().size(); ++i) { |
| 2921 | cricket::ContentInfo& content = new_remote_description->contents()[i]; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2922 | if (!content.name.empty()) { |
| 2923 | continue; |
| 2924 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2925 | std::string new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2926 | absl::string_view source_explanation; |
| 2927 | if (IsUnifiedPlan()) { |
| 2928 | if (i < local_contents.size()) { |
| 2929 | new_mid = local_contents[i].name; |
| 2930 | source_explanation = "from the matching local media section"; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2931 | } else if (i < remote_contents.size()) { |
| 2932 | new_mid = remote_contents[i].name; |
| 2933 | source_explanation = "from the matching previous remote media section"; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2934 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2935 | new_mid = mid_generator_(); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2936 | source_explanation = "generated just now"; |
| 2937 | } |
| 2938 | } else { |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2939 | new_mid = std::string( |
| 2940 | GetDefaultMidForPlanB(content.media_description()->type())); |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2941 | source_explanation = "to match pre-existing behavior"; |
| 2942 | } |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2943 | RTC_DCHECK(!new_mid.empty()); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 2944 | content.name = new_mid; |
Steve Anton | d7180cc | 2019-02-07 18:44:53 | [diff] [blame] | 2945 | new_remote_description->transport_infos()[i].content_name = new_mid; |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 2946 | RTC_LOG(LS_INFO) << "SetRemoteDescription: Remote media section at i=" << i |
| 2947 | << " is missing an a=mid line. Filling in the value '" |
| 2948 | << new_mid << "' " << source_explanation << "."; |
| 2949 | } |
| 2950 | } |
| 2951 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 2952 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 | [diff] [blame] | 2953 | SetSessionDescriptionObserver* observer, |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2954 | SessionDescriptionInterface* desc_ptr) { |
| 2955 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2956 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2957 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2958 | // lambda will execute immediately. |
| 2959 | operations_chain_->ChainOperation( |
| 2960 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 2961 | observer_refptr = |
| 2962 | rtc::scoped_refptr<SetSessionDescriptionObserver>(observer), |
| 2963 | desc = std::unique_ptr<SessionDescriptionInterface>(desc_ptr)]( |
| 2964 | std::function<void()> operations_chain_callback) mutable { |
| 2965 | // Abort early if |this_weak_ptr| is no longer valid. |
| 2966 | if (!this_weak_ptr) { |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2967 | // For consistency with SetSessionDescriptionObserverAdapter whose |
| 2968 | // posted messages doesn't get processed when the PC is destroyed, we |
| 2969 | // do not inform |observer_refptr| that the operation failed. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2970 | operations_chain_callback(); |
| 2971 | return; |
| 2972 | } |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2973 | // SetSessionDescriptionObserverAdapter takes care of making sure the |
| 2974 | // |observer_refptr| is invoked in a posted message. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2975 | this_weak_ptr->DoSetRemoteDescription( |
| 2976 | std::move(desc), |
| 2977 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2978 | new rtc::RefCountedObject<SetSessionDescriptionObserverAdapter>( |
| 2979 | this_weak_ptr, observer_refptr))); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2980 | // For backwards-compatability reasons, we declare the operation as |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 2981 | // completed here (rather than in a post), so that the operation chain |
| 2982 | // is not blocked by this operation when the observer is invoked. This |
| 2983 | // allows the observer to trigger subsequent offer/answer operations |
| 2984 | // synchronously if the operation chain is now empty. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2985 | operations_chain_callback(); |
| 2986 | }); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | void PeerConnection::SetRemoteDescription( |
| 2990 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2991 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 2992 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 2993 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 2994 | // this operation will be queued to be invoked, otherwise the contents of the |
| 2995 | // lambda will execute immediately. |
| 2996 | operations_chain_->ChainOperation( |
| 2997 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), observer, |
| 2998 | desc = std::move(desc)]( |
| 2999 | std::function<void()> operations_chain_callback) mutable { |
| 3000 | // Abort early if |this_weak_ptr| is no longer valid. |
| 3001 | if (!this_weak_ptr) { |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 3002 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 3003 | RTCErrorType::INTERNAL_ERROR, |
| 3004 | "SetRemoteDescription failed because the session was shut down")); |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 3005 | operations_chain_callback(); |
| 3006 | return; |
| 3007 | } |
| 3008 | this_weak_ptr->DoSetRemoteDescription(std::move(desc), |
| 3009 | std::move(observer)); |
Henrik Boström | 831ae4e | 2020-07-29 10:04:00 | [diff] [blame] | 3010 | // DoSetRemoteDescription() is implemented as a synchronous operation. |
| 3011 | // The |observer| will already have been informed that it completed, and |
| 3012 | // we can mark this operation as complete without any loose ends. |
Henrik Boström | a3728d3 | 2019-10-28 11:09:49 | [diff] [blame] | 3013 | operations_chain_callback(); |
| 3014 | }); |
| 3015 | } |
| 3016 | |
| 3017 | void PeerConnection::DoSetRemoteDescription( |
| 3018 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 3019 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
| 3020 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3021 | TRACE_EVENT0("webrtc", "PeerConnection::DoSetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3022 | |
nisse | 7ce109a | 2017-01-31 08:57:56 | [diff] [blame] | 3023 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 3024 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3025 | return; |
| 3026 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3027 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3028 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 3029 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3030 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3031 | return; |
| 3032 | } |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 3033 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3034 | // If a session error has occurred the PeerConnection is in a possibly |
| 3035 | // inconsistent state so fail right away. |
| 3036 | if (session_error() != SessionError::kNone) { |
| 3037 | std::string error_message = GetSessionErrorMsg(); |
| 3038 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 3039 | observer->OnSetRemoteDescriptionComplete( |
| 3040 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 3041 | return; |
| 3042 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3043 | if (IsUnifiedPlan()) { |
| 3044 | if (configuration_.enable_implicit_rollback) { |
| 3045 | if (desc->GetType() == SdpType::kOffer && |
| 3046 | signaling_state() == kHaveLocalOffer) { |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 3047 | Rollback(desc->GetType()); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | // Explicit rollback. |
| 3051 | if (desc->GetType() == SdpType::kRollback) { |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 3052 | observer->OnSetRemoteDescriptionComplete(Rollback(desc->GetType())); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3053 | return; |
| 3054 | } |
| 3055 | } else if (desc->GetType() == SdpType::kRollback) { |
| 3056 | observer->OnSetRemoteDescriptionComplete( |
| 3057 | RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 3058 | "Rollback not supported in Plan B")); |
| 3059 | return; |
| 3060 | } |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 3061 | if (desc->GetType() == SdpType::kOffer) { |
| 3062 | // Report to UMA the format of the received offer. |
| 3063 | ReportSdpFormatReceived(*desc); |
| 3064 | } |
| 3065 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 3066 | // Handle remote descriptions missing a=mid lines for interop with legacy end |
| 3067 | // points. |
| 3068 | FillInMissingRemoteMids(desc->description()); |
| 3069 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3070 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3071 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3072 | std::string error_message = GetSetDescriptionErrorMessage( |
| 3073 | cricket::CS_REMOTE, desc->GetType(), error); |
| 3074 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3075 | observer->OnSetRemoteDescriptionComplete( |
| 3076 | RTCError(error.type(), std::move(error_message))); |
| 3077 | return; |
| 3078 | } |
Steve Anton | 71439a6 | 2018-02-15 19:53:06 | [diff] [blame] | 3079 | |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 3080 | // Grab the description type before moving ownership to |
| 3081 | // ApplyRemoteDescription, which may destroy it before returning. |
| 3082 | const SdpType type = desc->GetType(); |
| 3083 | |
| 3084 | error = ApplyRemoteDescription(std::move(desc)); |
| 3085 | // |desc| may be destroyed at this point. |
| 3086 | |
| 3087 | if (!error.ok()) { |
| 3088 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 3089 | // inconsistent state, so act conservatively here and set the session error |
| 3090 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 3091 | SetSessionError(SessionError::kContent, error.message()); |
| 3092 | std::string error_message = |
| 3093 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 3094 | RTC_LOG(LS_ERROR) << error_message; |
| 3095 | observer->OnSetRemoteDescriptionComplete( |
| 3096 | RTCError(error.type(), std::move(error_message))); |
| 3097 | return; |
| 3098 | } |
| 3099 | RTC_DCHECK(remote_description()); |
| 3100 | |
| 3101 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3102 | // TODO(deadbeef): We already had to hop to the network thread for |
| 3103 | // MaybeStartGathering... |
| 3104 | network_thread()->Invoke<void>( |
| 3105 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3106 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 22:48:00 | [diff] [blame] | 3107 | // Make UMA notes about what was agreed to. |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 3108 | ReportNegotiatedSdpSemantics(*remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3109 | } |
| 3110 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 3111 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 3112 | NoteUsageEvent(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED); |
| 3113 | |
| 3114 | // Check if negotiation is needed. We must do this after informing the |
| 3115 | // observer that SetRemoteDescription() has completed to ensure negotiation is |
| 3116 | // not needed prior to the promise resolving. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 3117 | if (IsUnifiedPlan()) { |
| 3118 | bool was_negotiation_needed = is_negotiation_needed_; |
| 3119 | UpdateNegotiationNeeded(); |
| 3120 | if (signaling_state() == kStable && was_negotiation_needed && |
| 3121 | is_negotiation_needed_) { |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 3122 | // Legacy version. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 3123 | Observer()->OnRenegotiationNeeded(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 3124 | // Spec-compliant version; the event may get invalidated before firing. |
| 3125 | GenerateNegotiationNeededEvent(); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 3126 | } |
| 3127 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | RTCError PeerConnection::ApplyRemoteDescription( |
| 3131 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 3132 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 3133 | RTC_DCHECK(desc); |
| 3134 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3135 | // Update stats here so that we have the most recent stats for tracks and |
| 3136 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 3137 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3138 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3139 | // Take a reference to the old remote description since it's used below to |
| 3140 | // compare against the new remote description. When setting the new remote |
| 3141 | // description, grab ownership of the replaced session description in case it |
| 3142 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 3143 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3144 | const SessionDescriptionInterface* old_remote_description = |
| 3145 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3146 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 3147 | SdpType type = desc->GetType(); |
| 3148 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3149 | replaced_remote_description = pending_remote_description_ |
| 3150 | ? std::move(pending_remote_description_) |
| 3151 | : std::move(current_remote_description_); |
| 3152 | current_remote_description_ = std::move(desc); |
| 3153 | pending_remote_description_ = nullptr; |
| 3154 | current_local_description_ = std::move(pending_local_description_); |
| 3155 | } else { |
| 3156 | replaced_remote_description = std::move(pending_remote_description_); |
| 3157 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3158 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3159 | // The session description to apply now must be accessed by |
| 3160 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3161 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 3162 | |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 3163 | // Report statistics about any use of simulcast. |
| 3164 | ReportSimulcastApiVersion(kSimulcastVersionApplyRemoteDescription, |
| 3165 | *remote_description()->description()); |
| 3166 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3167 | RTCError error = PushdownTransportDescription(cricket::CS_REMOTE, type); |
| 3168 | if (!error.ok()) { |
| 3169 | return error; |
| 3170 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3171 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3172 | if (IsUnifiedPlan()) { |
| 3173 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3174 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 3175 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3176 | if (!error.ok()) { |
| 3177 | return error; |
| 3178 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3179 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3180 | // Media channels will be created only when offer is set. These may use new |
| 3181 | // transports just created by PushdownTransportDescription. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3182 | if (type == SdpType::kOffer) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3183 | // TODO(mallinath) - Handle CreateChannel failure, as new local |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3184 | // description is applied. Restore back to old description. |
| 3185 | RTCError error = CreateChannels(*remote_description()->description()); |
| 3186 | if (!error.ok()) { |
| 3187 | return error; |
| 3188 | } |
| 3189 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3190 | // Remove unused channels if MediaContentDescription is rejected. |
| 3191 | RemoveUnusedChannels(remote_description()->description()); |
| 3192 | } |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3193 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3194 | // NOTE: Candidates allocation will be initiated only when |
| 3195 | // SetLocalDescription is called. |
| 3196 | error = UpdateSessionState(type, cricket::CS_REMOTE, |
| 3197 | remote_description()->description()); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3198 | if (!error.ok()) { |
| 3199 | return error; |
| 3200 | } |
| 3201 | |
| 3202 | if (local_description() && |
| 3203 | !UseCandidatesInSessionDescription(remote_description())) { |
| 3204 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 3205 | } |
| 3206 | |
| 3207 | if (old_remote_description) { |
| 3208 | for (const cricket::ContentInfo& content : |
| 3209 | old_remote_description->description()->contents()) { |
| 3210 | // Check if this new SessionDescription contains new ICE ufrag and |
| 3211 | // password that indicates the remote peer requests an ICE restart. |
| 3212 | // TODO(deadbeef): When we start storing both the current and pending |
| 3213 | // remote description, this should reset pending_ice_restarts and compare |
| 3214 | // against the current description. |
| 3215 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 3216 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 3217 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3218 | pending_ice_restarts_.insert(content.name); |
| 3219 | } |
| 3220 | } else { |
| 3221 | // We retain all received candidates only if ICE is not restarted. |
| 3222 | // When ICE is restarted, all previous candidates belong to an old |
| 3223 | // generation and should not be kept. |
| 3224 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 3225 | // description should only contain candidates from the last set remote |
| 3226 | // description plus any candidates added since then. We should remove |
| 3227 | // this once we're sure it won't break anything. |
| 3228 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 3229 | old_remote_description, content.name, mutable_remote_description()); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | if (session_error() != SessionError::kNone) { |
| 3235 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 3236 | } |
| 3237 | |
| 3238 | // Set the the ICE connection state to connecting since the connection may |
| 3239 | // become writable with peer reflexive candidates before any remote candidate |
| 3240 | // is signaled. |
| 3241 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 3242 | // is to have a new signal the indicates a change in checking state from the |
| 3243 | // transport and expose a new checking() member from transport that can be |
| 3244 | // read to determine the current checking state. The existing SignalConnecting |
| 3245 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 18:27:41 | [diff] [blame] | 3246 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | f764cf4 | 2018-05-01 21:32:17 | [diff] [blame] | 3247 | remote_description()->number_of_mediasections() > 0u && |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3248 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 3249 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 3250 | } |
| 3251 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3252 | // If setting the description decided our SSL role, allocate any necessary |
| 3253 | // SCTP sids. |
| 3254 | rtc::SSLRole role; |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 3255 | if (IsSctpLike(data_channel_type()) && GetSctpSslRole(&role)) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 3256 | data_channel_controller_.AllocateSctpSids(role); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3257 | } |
| 3258 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3259 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-17 00:14:42 | [diff] [blame] | 3260 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3261 | now_receiving_transceivers; |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3262 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> remove_list; |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3263 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3264 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3265 | for (const auto& transceiver : transceivers_) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3266 | const ContentInfo* content = |
| 3267 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 3268 | if (!content) { |
| 3269 | continue; |
| 3270 | } |
| 3271 | const MediaContentDescription* media_desc = content->media_description(); |
| 3272 | RtpTransceiverDirection local_direction = |
| 3273 | RtpTransceiverDirectionReversed(media_desc->direction()); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3274 | // Roughly the same as steps 2.2.8.6 of section 4.4.1.6 "Set the |
| 3275 | // RTCSessionDescription: Set the associated remote streams given |
| 3276 | // transceiver.[[Receiver]], msids, addList, and removeList". |
| 3277 | // https://w3c.github.io/webrtc-pc/#set-the-rtcsessiondescription |
| 3278 | if (RtpTransceiverDirectionHasRecv(local_direction)) { |
| 3279 | std::vector<std::string> stream_ids; |
| 3280 | if (!media_desc->streams().empty()) { |
| 3281 | // The remote description has signaled the stream IDs. |
| 3282 | stream_ids = media_desc->streams()[0].stream_ids(); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 3283 | } |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 3284 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3285 | .SetRemoteStreamIdsIfUnset(transceiver->receiver()->stream_ids()); |
| 3286 | |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3287 | RTC_LOG(LS_INFO) << "Processing the MSIDs for MID=" << content->name |
| 3288 | << " (" << GetStreamIdsString(stream_ids) << ")."; |
| 3289 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 3290 | stream_ids, &added_streams, |
| 3291 | &removed_streams); |
| 3292 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 |
| 3293 | // "Set the RTCSessionDescription: If direction is sendrecv or recvonly, |
| 3294 | // and transceiver's current direction is neither sendrecv nor recvonly, |
| 3295 | // process the addition of a remote track for the media description. |
| 3296 | if (!transceiver->fired_direction() || |
| 3297 | !RtpTransceiverDirectionHasRecv(*transceiver->fired_direction())) { |
| 3298 | RTC_LOG(LS_INFO) |
| 3299 | << "Processing the addition of a remote track for MID=" |
| 3300 | << content->name << "."; |
| 3301 | now_receiving_transceivers.push_back(transceiver); |
Henrik Boström | 5b14778 | 2018-12-04 10:25:05 | [diff] [blame] | 3302 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3303 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3304 | // 2.2.8.1.9: If direction is "sendonly" or "inactive", and transceiver's |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3305 | // [[FiredDirection]] slot is either "sendrecv" or "recvonly", process the |
| 3306 | // removal of a remote track for the media description, given transceiver, |
| 3307 | // removeList, and muteTracks. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3308 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3309 | (transceiver->fired_direction() && |
| 3310 | RtpTransceiverDirectionHasRecv(*transceiver->fired_direction()))) { |
| 3311 | ProcessRemovalOfRemoteTrack(transceiver, &remove_list, |
| 3312 | &removed_streams); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3313 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3314 | // 2.2.8.1.10: Set transceiver's [[FiredDirection]] slot to direction. |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3315 | transceiver->internal()->set_fired_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3316 | // 2.2.8.1.11: If description is of type "answer" or "pranswer", then run |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3317 | // the following steps: |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3318 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3319 | // 2.2.8.1.11.1: Set transceiver's [[CurrentDirection]] slot to |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3320 | // direction. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3321 | transceiver->internal()->set_current_direction(local_direction); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3322 | // 2.2.8.1.11.[3-6]: Set the transport internal slots. |
| 3323 | if (transceiver->mid()) { |
| 3324 | auto dtls_transport = |
| 3325 | LookupDtlsTransportByMidInternal(*transceiver->mid()); |
| 3326 | transceiver->internal()->sender_internal()->set_transport( |
| 3327 | dtls_transport); |
| 3328 | transceiver->internal()->receiver_internal()->set_transport( |
| 3329 | dtls_transport); |
| 3330 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3331 | } |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 3332 | // 2.2.8.1.12: If the media description is rejected, and transceiver is |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3333 | // not already stopped, stop the RTCRtpTransceiver transceiver. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3334 | if (content->rejected && !transceiver->stopped()) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3335 | RTC_LOG(LS_INFO) << "Stopping transceiver for MID=" << content->name |
| 3336 | << " since the media section was rejected."; |
Harald Alvestrand | c75c428 | 2020-08-26 12:17:54 | [diff] [blame] | 3337 | transceiver->internal()->StopTransceiverProcedure(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3338 | } |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 3339 | if (!content->rejected && |
| 3340 | RtpTransceiverDirectionHasRecv(local_direction)) { |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 3341 | if (!media_desc->streams().empty() && |
| 3342 | media_desc->streams()[0].has_ssrcs()) { |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 3343 | uint32_t ssrc = media_desc->streams()[0].first_ssrc(); |
| 3344 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
| 3345 | } else { |
| 3346 | transceiver->internal() |
| 3347 | ->receiver_internal() |
| 3348 | ->SetupUnsignaledMediaChannel(); |
Seth Hampson | 2f0d702 | 2018-02-20 19:54:42 | [diff] [blame] | 3349 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 3350 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3351 | } |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3352 | // Once all processing has finished, fire off callbacks. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3353 | auto observer = Observer(); |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3354 | for (const auto& transceiver : now_receiving_transceivers) { |
Steve Anton | 6e22137 | 2018-02-20 20:59:16 | [diff] [blame] | 3355 | stats_->AddTrack(transceiver->receiver()->track()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3356 | observer->OnTrack(transceiver); |
| 3357 | observer->OnAddTrack(transceiver->receiver(), |
| 3358 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-11 01:15:20 | [diff] [blame] | 3359 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3360 | for (const auto& stream : added_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3361 | observer->OnAddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 22:28:13 | [diff] [blame] | 3362 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3363 | for (const auto& transceiver : remove_list) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3364 | observer->OnRemoveTrack(transceiver->receiver()); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3365 | } |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3366 | for (const auto& stream : removed_streams) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3367 | observer->OnRemoveStream(stream); |
Steve Anton | 3172c03 | 2018-05-03 22:30:18 | [diff] [blame] | 3368 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3369 | } |
| 3370 | |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3371 | const cricket::ContentInfo* audio_content = |
| 3372 | GetFirstAudioContent(remote_description()->description()); |
| 3373 | const cricket::ContentInfo* video_content = |
| 3374 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3375 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3376 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3377 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3378 | GetFirstVideoContentDescription(remote_description()->description()); |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 3379 | const cricket::RtpDataContentDescription* rtp_data_desc = |
| 3380 | GetFirstRtpDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3381 | |
| 3382 | // Check if the descriptions include streams, just in case the peer supports |
| 3383 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 18:55:44 | [diff] [blame] | 3384 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 3385 | (audio_desc && !audio_desc->streams().empty()) || |
| 3386 | (video_desc && !video_desc->streams().empty())) { |
| 3387 | remote_peer_supports_msid_ = true; |
| 3388 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3389 | |
| 3390 | // We wait to signal new streams until we finish processing the description, |
| 3391 | // since only at that point will new streams have all their tracks. |
| 3392 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 3393 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3394 | if (!IsUnifiedPlan()) { |
| 3395 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 3396 | // rejected media section, there is some cleanup logic that expects the |
| 3397 | // voice/ video channel to still be set. But in this method the voice/video |
| 3398 | // channel would have been destroyed by the SetRemoteDescription caller |
| 3399 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 3400 | // calls should be moved to right before the DestroyChannel calls to fix |
| 3401 | // this. |
Steve Anton | 8d3444d | 2017-10-20 22:30:51 | [diff] [blame] | 3402 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3403 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 3404 | // and MediaStreams. |
| 3405 | if (audio_content) { |
| 3406 | if (audio_content->rejected) { |
| 3407 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 3408 | } else { |
| 3409 | bool default_audio_track_needed = |
| 3410 | !remote_peer_supports_msid_ && |
| 3411 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 3412 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 3413 | default_audio_track_needed, audio_desc->type(), |
| 3414 | new_streams); |
| 3415 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 3416 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3417 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3418 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 3419 | // and MediaStreams. |
| 3420 | if (video_content) { |
| 3421 | if (video_content->rejected) { |
| 3422 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 3423 | } else { |
| 3424 | bool default_video_track_needed = |
| 3425 | !remote_peer_supports_msid_ && |
| 3426 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 3427 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 3428 | default_video_track_needed, video_desc->type(), |
| 3429 | new_streams); |
| 3430 | } |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 3431 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3432 | |
Harald Alvestrand | 5fc28b1 | 2019-05-13 11:36:16 | [diff] [blame] | 3433 | // If this is an RTP data transport, update the DataChannels with the |
| 3434 | // information from the remote peer. |
| 3435 | if (rtp_data_desc) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 3436 | data_channel_controller_.UpdateRemoteRtpDataChannels( |
| 3437 | GetActiveStreams(rtp_data_desc)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3438 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3439 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3440 | // Iterate new_streams and notify the observer about new MediaStreams. |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3441 | auto observer = Observer(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3442 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 3443 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 3444 | stats_->AddStream(new_stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 3445 | observer->OnAddStream( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3446 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 3447 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3448 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3449 | UpdateEndedRemoteMediaStreams(); |
| 3450 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 3451 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 3452 | if (type == SdpType::kAnswer && |
| 3453 | local_ice_credentials_to_replace_->SatisfiesIceRestart( |
| 3454 | *current_local_description_)) { |
| 3455 | local_ice_credentials_to_replace_->ClearIceCredentials(); |
| 3456 | } |
| 3457 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 3458 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 23:42:33 | [diff] [blame] | 3459 | } |
| 3460 | |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3461 | void PeerConnection::SetAssociatedRemoteStreams( |
| 3462 | rtc::scoped_refptr<RtpReceiverInternal> receiver, |
| 3463 | const std::vector<std::string>& stream_ids, |
| 3464 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* added_streams, |
| 3465 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3466 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams; |
| 3467 | for (const std::string& stream_id : stream_ids) { |
| 3468 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3469 | remote_streams_->find(stream_id); |
| 3470 | if (!stream) { |
| 3471 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3472 | MediaStream::Create(stream_id)); |
| 3473 | remote_streams_->AddStream(stream); |
| 3474 | added_streams->push_back(stream); |
| 3475 | } |
| 3476 | media_streams.push_back(stream); |
| 3477 | } |
| 3478 | // Special case: "a=msid" missing, use random stream ID. |
| 3479 | if (media_streams.empty() && |
| 3480 | !(remote_description()->description()->msid_signaling() & |
| 3481 | cricket::kMsidSignalingMediaSection)) { |
| 3482 | if (!missing_msid_default_stream_) { |
| 3483 | missing_msid_default_stream_ = MediaStreamProxy::Create( |
| 3484 | rtc::Thread::Current(), MediaStream::Create(rtc::CreateRandomUuid())); |
| 3485 | added_streams->push_back(missing_msid_default_stream_); |
| 3486 | } |
| 3487 | media_streams.push_back(missing_msid_default_stream_); |
| 3488 | } |
| 3489 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
| 3490 | receiver->streams(); |
| 3491 | // SetStreams() will add/remove the receiver's track to/from the streams. This |
| 3492 | // differs from the spec - the spec uses an "addList" and "removeList" to |
| 3493 | // update the stream-track relationships in a later step. We do this earlier, |
| 3494 | // changing the order of things, but the end-result is the same. |
| 3495 | // TODO(hbos): When we remove remote_streams(), use set_stream_ids() |
| 3496 | // instead. https://crbug.com/webrtc/9480 |
| 3497 | receiver->SetStreams(media_streams); |
| 3498 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3499 | } |
| 3500 | |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3501 | void PeerConnection::ProcessRemovalOfRemoteTrack( |
| 3502 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3503 | transceiver, |
| 3504 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>>* remove_list, |
| 3505 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3506 | RTC_DCHECK(transceiver->mid()); |
| 3507 | RTC_LOG(LS_INFO) << "Processing the removal of a track for MID=" |
| 3508 | << *transceiver->mid(); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3509 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> previous_streams = |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3510 | transceiver->internal()->receiver_internal()->streams(); |
| 3511 | // This will remove the remote track from the streams. |
| 3512 | transceiver->internal()->receiver_internal()->set_stream_ids({}); |
| 3513 | remove_list->push_back(transceiver); |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3514 | RemoveRemoteStreamsIfEmpty(previous_streams, removed_streams); |
| 3515 | } |
| 3516 | |
| 3517 | void PeerConnection::RemoveRemoteStreamsIfEmpty( |
| 3518 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& remote_streams, |
| 3519 | std::vector<rtc::scoped_refptr<MediaStreamInterface>>* removed_streams) { |
| 3520 | // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead of |
| 3521 | // streams, see if the stream was removed by checking if this was the last |
| 3522 | // receiver with that stream ID. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 3523 | for (const auto& remote_stream : remote_streams) { |
Henrik Boström | afa07dd | 2018-12-20 10:06:02 | [diff] [blame] | 3524 | if (remote_stream->GetAudioTracks().empty() && |
| 3525 | remote_stream->GetVideoTracks().empty()) { |
| 3526 | remote_streams_->RemoveStream(remote_stream); |
| 3527 | removed_streams->push_back(remote_stream); |
Steve Anton | 0f5400a | 2018-07-17 21:25:36 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3532 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 3533 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3534 | const SessionDescriptionInterface& new_session, |
| 3535 | const SessionDescriptionInterface* old_local_description, |
| 3536 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3537 | RTC_DCHECK(IsUnifiedPlan()); |
| 3538 | |
Steve Anton | 7464fca | 2018-01-19 19:10:37 | [diff] [blame] | 3539 | const cricket::ContentGroup* bundle_group = nullptr; |
| 3540 | if (new_session.GetType() == SdpType::kOffer) { |
| 3541 | auto bundle_group_or_error = |
| 3542 | GetEarlyBundleGroup(*new_session.description()); |
| 3543 | if (!bundle_group_or_error.ok()) { |
| 3544 | return bundle_group_or_error.MoveError(); |
| 3545 | } |
| 3546 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3547 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3548 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3549 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3550 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 3551 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3552 | cricket::MediaType media_type = new_content.media_description()->type(); |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 3553 | mid_generator_.AddKnownId(new_content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3554 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3555 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3556 | const cricket::ContentInfo* old_local_content = nullptr; |
| 3557 | if (old_local_description && |
| 3558 | i < old_local_description->description()->contents().size()) { |
| 3559 | old_local_content = |
| 3560 | &old_local_description->description()->contents()[i]; |
| 3561 | } |
| 3562 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 3563 | if (old_remote_description && |
| 3564 | i < old_remote_description->description()->contents().size()) { |
| 3565 | old_remote_content = |
| 3566 | &old_remote_description->description()->contents()[i]; |
| 3567 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3568 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3569 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 3570 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3571 | if (!transceiver_or_error.ok()) { |
| 3572 | return transceiver_or_error.MoveError(); |
| 3573 | } |
| 3574 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3575 | RTCError error = |
| 3576 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 3577 | if (!error.ok()) { |
| 3578 | return error; |
| 3579 | } |
| 3580 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3581 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 3582 | // Ignore all but the first data section. |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3583 | RTC_LOG(LS_INFO) << "Ignoring data media section with MID=" |
| 3584 | << new_content.name; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3585 | continue; |
| 3586 | } |
| 3587 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 3588 | if (!error.ok()) { |
| 3589 | return error; |
| 3590 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3591 | } else { |
| 3592 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3593 | "Unknown section type."); |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | return RTCError::OK(); |
| 3598 | } |
| 3599 | |
| 3600 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 3601 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3602 | transceiver, |
| 3603 | const cricket::ContentInfo& content, |
| 3604 | const cricket::ContentGroup* bundle_group) { |
| 3605 | RTC_DCHECK(IsUnifiedPlan()); |
| 3606 | RTC_DCHECK(transceiver); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3607 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3608 | if (content.rejected) { |
| 3609 | if (channel) { |
| 3610 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 3611 | DestroyChannelInterface(channel); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3612 | } |
| 3613 | } else { |
| 3614 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3615 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3616 | channel = CreateVoiceChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3617 | } else { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3618 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3619 | channel = CreateVideoChannel(content.name); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3620 | } |
| 3621 | if (!channel) { |
| 3622 | LOG_AND_RETURN_ERROR( |
| 3623 | RTCErrorType::INTERNAL_ERROR, |
| 3624 | "Failed to create channel for mid=" + content.name); |
| 3625 | } |
| 3626 | transceiver->internal()->SetChannel(channel); |
| 3627 | } |
| 3628 | } |
| 3629 | return RTCError::OK(); |
| 3630 | } |
| 3631 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3632 | RTCError PeerConnection::UpdateDataChannel( |
| 3633 | cricket::ContentSource source, |
| 3634 | const cricket::ContentInfo& content, |
| 3635 | const cricket::ContentGroup* bundle_group) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 3636 | if (data_channel_type() == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 3637 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 3638 | // will take care of rejecting it. |
| 3639 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3640 | } |
| 3641 | if (content.rejected) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 3642 | RTC_LOG(LS_INFO) << "Rejected data channel, mid=" << content.mid(); |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 3643 | DestroyDataChannelTransport(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3644 | } else { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 3645 | if (!data_channel_controller_.rtp_data_channel() && |
| 3646 | !data_channel_controller_.data_channel_transport()) { |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 3647 | RTC_LOG(LS_INFO) << "Creating data channel, mid=" << content.mid(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 3648 | if (!CreateDataChannel(content.name)) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3649 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 3650 | "Failed to create data channel."); |
| 3651 | } |
| 3652 | } |
| 3653 | if (source == cricket::CS_REMOTE) { |
| 3654 | const MediaContentDescription* data_desc = content.media_description(); |
| 3655 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 3656 | data_channel_controller_.UpdateRemoteRtpDataChannels( |
| 3657 | GetActiveStreams(data_desc)); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 3658 | } |
| 3659 | } |
| 3660 | } |
| 3661 | return RTCError::OK(); |
| 3662 | } |
| 3663 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3664 | // This method will extract any send encodings that were sent by the remote |
| 3665 | // connection. This is currently only relevant for Simulcast scenario (where |
| 3666 | // the number of layers may be communicated by the server). |
| 3667 | static std::vector<RtpEncodingParameters> GetSendEncodingsFromRemoteDescription( |
| 3668 | const MediaContentDescription& desc) { |
| 3669 | if (!desc.HasSimulcast()) { |
| 3670 | return {}; |
| 3671 | } |
| 3672 | std::vector<RtpEncodingParameters> result; |
| 3673 | const SimulcastDescription& simulcast = desc.simulcast_description(); |
| 3674 | |
| 3675 | // This is a remote description, the parameters we are after should appear |
| 3676 | // as receive streams. |
| 3677 | for (const auto& alternatives : simulcast.receive_layers()) { |
| 3678 | RTC_DCHECK(!alternatives.empty()); |
| 3679 | // There is currently no way to specify or choose from alternatives. |
| 3680 | // We will always use the first alternative, which is the most preferred. |
| 3681 | const SimulcastLayer& layer = alternatives[0]; |
| 3682 | RtpEncodingParameters parameters; |
| 3683 | parameters.rid = layer.rid; |
| 3684 | parameters.active = !layer.is_paused; |
| 3685 | result.push_back(parameters); |
| 3686 | } |
| 3687 | |
| 3688 | return result; |
| 3689 | } |
| 3690 | |
| 3691 | static RTCError UpdateSimulcastLayerStatusInSender( |
| 3692 | const std::vector<SimulcastLayer>& layers, |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3693 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3694 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3695 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3696 | std::vector<std::string> disabled_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3697 | |
| 3698 | // The simulcast envelope cannot be changed, only the status of the streams. |
| 3699 | // So we will iterate over the send encodings rather than the layers. |
| 3700 | for (RtpEncodingParameters& encoding : parameters.encodings) { |
| 3701 | auto iter = std::find_if(layers.begin(), layers.end(), |
| 3702 | [&encoding](const SimulcastLayer& layer) { |
| 3703 | return layer.rid == encoding.rid; |
| 3704 | }); |
| 3705 | // A layer that cannot be found may have been removed by the remote party. |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3706 | if (iter == layers.end()) { |
| 3707 | disabled_layers.push_back(encoding.rid); |
| 3708 | continue; |
| 3709 | } |
| 3710 | |
| 3711 | encoding.active = !iter->is_paused; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3712 | } |
| 3713 | |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3714 | RTCError result = sender->SetParametersInternal(parameters); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3715 | if (result.ok()) { |
| 3716 | result = sender->DisableEncodingLayers(disabled_layers); |
| 3717 | } |
| 3718 | |
| 3719 | return result; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3720 | } |
| 3721 | |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3722 | static bool SimulcastIsRejected( |
| 3723 | const ContentInfo* local_content, |
| 3724 | const MediaContentDescription& answer_media_desc) { |
| 3725 | bool simulcast_offered = local_content && |
| 3726 | local_content->media_description() && |
| 3727 | local_content->media_description()->HasSimulcast(); |
| 3728 | bool simulcast_answered = answer_media_desc.HasSimulcast(); |
| 3729 | bool rids_supported = RtpExtension::FindHeaderExtensionByUri( |
| 3730 | answer_media_desc.rtp_header_extensions(), RtpExtension::kRidUri); |
| 3731 | return simulcast_offered && (!simulcast_answered || !rids_supported); |
| 3732 | } |
| 3733 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3734 | static RTCError DisableSimulcastInSender( |
| 3735 | rtc::scoped_refptr<RtpSenderInternal> sender) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3736 | RTC_DCHECK(sender); |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 3737 | RtpParameters parameters = sender->GetParametersInternal(); |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3738 | if (parameters.encodings.size() <= 1) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3739 | return RTCError::OK(); |
| 3740 | } |
| 3741 | |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3742 | std::vector<std::string> disabled_layers; |
| 3743 | std::transform( |
| 3744 | parameters.encodings.begin() + 1, parameters.encodings.end(), |
| 3745 | std::back_inserter(disabled_layers), |
| 3746 | [](const RtpEncodingParameters& encoding) { return encoding.rid; }); |
| 3747 | return sender->DisableEncodingLayers(disabled_layers); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3748 | } |
| 3749 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3750 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 3751 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3752 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3753 | size_t mline_index, |
| 3754 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3755 | const ContentInfo* old_local_content, |
| 3756 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3757 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 3758 | // If this is an offer then the m= section might be recycled. If the m= |
| 3759 | // section is being recycled (defined as: rejected in the current local or |
| 3760 | // remote description and not rejected in new description), dissociate the |
| 3761 | // currently associated RtpTransceiver by setting its mid property to null, |
| 3762 | // and discard the mapping between the transceiver and its m= section index. |
| 3763 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 3764 | old_remote_content)) { |
| 3765 | // We want to dissociate the transceiver that has the rejected mid. |
| 3766 | const std::string& old_mid = |
| 3767 | (old_local_content && old_local_content->rejected) |
| 3768 | ? old_local_content->name |
| 3769 | : old_remote_content->name; |
| 3770 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3771 | if (old_transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3772 | RTC_LOG(LS_INFO) << "Dissociating transceiver for MID=" << old_mid |
| 3773 | << " since the media section is being recycled."; |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 3774 | old_transceiver->internal()->set_mid(absl::nullopt); |
| 3775 | old_transceiver->internal()->set_mline_index(absl::nullopt); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3776 | } |
| 3777 | } |
| 3778 | const MediaContentDescription* media_desc = content.media_description(); |
| 3779 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3780 | if (source == cricket::CS_LOCAL) { |
| 3781 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 3782 | // mapping between transceivers and m= section indices established when |
| 3783 | // creating the offer. |
| 3784 | if (!transceiver) { |
| 3785 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 3786 | } |
| 3787 | if (!transceiver) { |
| 3788 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 3789 | "Unknown transceiver"); |
| 3790 | } |
| 3791 | } else { |
| 3792 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 3793 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 3794 | // of the same type... |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3795 | // When simulcast is requested, a transceiver cannot be associated because |
| 3796 | // AddTrack cannot be called to initialize it. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3797 | if (!transceiver && |
Amit Hilbuch | aa58415 | 2019-02-07 01:09:52 | [diff] [blame] | 3798 | RtpTransceiverDirectionHasRecv(media_desc->direction()) && |
| 3799 | !media_desc->HasSimulcast()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3800 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 3801 | } |
| 3802 | // If no RtpTransceiver was found in the previous step, create one with a |
| 3803 | // recvonly direction. |
| 3804 | if (!transceiver) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 3805 | RTC_LOG(LS_INFO) << "Adding " |
| 3806 | << cricket::MediaTypeToString(media_desc->type()) |
| 3807 | << " transceiver for MID=" << content.name |
| 3808 | << " at i=" << mline_index |
| 3809 | << " in response to the remote description."; |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 3810 | std::string sender_id = rtc::CreateRandomUuid(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3811 | std::vector<RtpEncodingParameters> send_encodings = |
| 3812 | GetSendEncodingsFromRemoteDescription(*media_desc); |
| 3813 | auto sender = CreateSender(media_desc->type(), sender_id, nullptr, {}, |
| 3814 | send_encodings); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 3815 | std::string receiver_id; |
| 3816 | if (!media_desc->streams().empty()) { |
| 3817 | receiver_id = media_desc->streams()[0].id; |
| 3818 | } else { |
| 3819 | receiver_id = rtc::CreateRandomUuid(); |
| 3820 | } |
| 3821 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-11 00:26:06 | [diff] [blame] | 3822 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3823 | transceiver->internal()->set_direction( |
| 3824 | RtpTransceiverDirection::kRecvOnly); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3825 | if (type == SdpType::kOffer) { |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 3826 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3827 | .set_newly_created(); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3828 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3829 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3830 | // Check if the offer indicated simulcast but the answer rejected it. |
| 3831 | // This can happen when simulcast is not supported on the remote party. |
Amit Hilbuch | aabd036 | 2019-03-01 21:14:46 | [diff] [blame] | 3832 | if (SimulcastIsRejected(old_local_content, *media_desc)) { |
Amit Hilbuch | e2a284d | 2019-03-05 20:36:31 | [diff] [blame] | 3833 | RTC_HISTOGRAM_BOOLEAN(kSimulcastDisabled, true); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3834 | RTCError error = |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3835 | DisableSimulcastInSender(transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3836 | if (!error.ok()) { |
| 3837 | RTC_LOG(LS_ERROR) << "Failed to remove rejected simulcast."; |
| 3838 | return std::move(error); |
| 3839 | } |
| 3840 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3841 | } |
| 3842 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3843 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3844 | LOG_AND_RETURN_ERROR( |
| 3845 | RTCErrorType::INVALID_PARAMETER, |
| 3846 | "Transceiver type does not match media description type."); |
| 3847 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3848 | if (media_desc->HasSimulcast()) { |
| 3849 | std::vector<SimulcastLayer> layers = |
| 3850 | source == cricket::CS_LOCAL |
| 3851 | ? media_desc->simulcast_description().send_layers().GetAllLayers() |
| 3852 | : media_desc->simulcast_description() |
| 3853 | .receive_layers() |
| 3854 | .GetAllLayers(); |
| 3855 | RTCError error = UpdateSimulcastLayerStatusInSender( |
Amit Hilbuch | 2297d33 | 2019-02-19 20:49:22 | [diff] [blame] | 3856 | layers, transceiver->internal()->sender_internal()); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 3857 | if (!error.ok()) { |
| 3858 | RTC_LOG(LS_ERROR) << "Failed updating status for simulcast layers."; |
| 3859 | return std::move(error); |
| 3860 | } |
| 3861 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3862 | if (type == SdpType::kOffer) { |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 3863 | bool state_changes = transceiver->internal()->mid() != content.name || |
| 3864 | transceiver->internal()->mline_index() != mline_index; |
| 3865 | if (state_changes) { |
| 3866 | transceiver_stable_states_by_transceivers_[transceiver] |
| 3867 | .SetMSectionIfUnset(transceiver->internal()->mid(), |
| 3868 | transceiver->internal()->mline_index()); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 3869 | } |
| 3870 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3871 | // Associate the found or created RtpTransceiver with the m= section by |
| 3872 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 3873 | // section, and establish a mapping between the transceiver and the index of |
| 3874 | // the m= section. |
| 3875 | transceiver->internal()->set_mid(content.name); |
| 3876 | transceiver->internal()->set_mline_index(mline_index); |
| 3877 | return std::move(transceiver); |
| 3878 | } |
| 3879 | |
| 3880 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3881 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 3882 | RTC_DCHECK(IsUnifiedPlan()); |
| 3883 | for (auto transceiver : transceivers_) { |
| 3884 | if (transceiver->mid() == mid) { |
| 3885 | return transceiver; |
| 3886 | } |
| 3887 | } |
| 3888 | return nullptr; |
| 3889 | } |
| 3890 | |
| 3891 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3892 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 3893 | RTC_DCHECK(IsUnifiedPlan()); |
| 3894 | for (auto transceiver : transceivers_) { |
| 3895 | if (transceiver->internal()->mline_index() == mline_index) { |
| 3896 | return transceiver; |
| 3897 | } |
| 3898 | } |
| 3899 | return nullptr; |
| 3900 | } |
| 3901 | |
| 3902 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3903 | PeerConnection::FindAvailableTransceiverToReceive( |
| 3904 | cricket::MediaType media_type) const { |
| 3905 | RTC_DCHECK(IsUnifiedPlan()); |
| 3906 | // From JSEP section 5.10 (Applying a Remote Description): |
| 3907 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 3908 | // the same type that were added to the PeerConnection by addTrack and are not |
| 3909 | // associated with any m= section and are not stopped, find the first such |
| 3910 | // RtpTransceiver. |
| 3911 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3912 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 3913 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 3914 | !transceiver->stopped()) { |
| 3915 | return transceiver; |
| 3916 | } |
| 3917 | } |
| 3918 | return nullptr; |
| 3919 | } |
| 3920 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3921 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 3922 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3923 | transceiver, |
| 3924 | const SessionDescriptionInterface* sdesc) const { |
| 3925 | RTC_DCHECK(transceiver); |
| 3926 | RTC_DCHECK(sdesc); |
| 3927 | if (IsUnifiedPlan()) { |
| 3928 | if (!transceiver->internal()->mid()) { |
| 3929 | // This transceiver is not associated with a media section yet. |
| 3930 | return nullptr; |
| 3931 | } |
| 3932 | return sdesc->description()->GetContentByName( |
| 3933 | *transceiver->internal()->mid()); |
| 3934 | } else { |
| 3935 | // Plan B only allows at most one audio and one video section, so use the |
| 3936 | // first media section of that type. |
| 3937 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 3938 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 3939 | } |
| 3940 | } |
| 3941 | |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3942 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3943 | RTC_DCHECK_RUN_ON(signaling_thread()); |
deadbeef | 46c7389 | 2016-11-17 03:42:04 | [diff] [blame] | 3944 | return configuration_; |
| 3945 | } |
| 3946 | |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3947 | RTCError PeerConnection::SetConfiguration( |
| 3948 | const RTCConfiguration& configuration) { |
Karl Wiberg | 5966c50 | 2019-02-21 22:55:09 | [diff] [blame] | 3949 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 3950 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3951 | if (IsClosed()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3952 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 3953 | "SetConfiguration: PeerConnection is closed."); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 3954 | } |
| 3955 | |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 3956 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 3957 | // size is not allowed, and changing the set of ICE servers will not result |
| 3958 | // in new candidates being gathered. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 3959 | if (local_description() && configuration.ice_candidate_pool_size != |
| 3960 | configuration_.ice_candidate_pool_size) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3961 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3962 | "Can't change candidate pool size after calling " |
| 3963 | "SetLocalDescription."); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 3964 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 3965 | |
Piotr (Peter) Slatala | aa1e7c2 | 2018-10-16 17:04:45 | [diff] [blame] | 3966 | if (local_description() && |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3967 | configuration.crypto_options != configuration_.crypto_options) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 3968 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 3969 | "Can't change crypto_options after calling " |
| 3970 | "SetLocalDescription."); |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 3971 | } |
| 3972 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 3973 | // The simplest (and most future-compatible) way to tell if the config was |
| 3974 | // modified in an invalid way is to copy each property we do support |
| 3975 | // modifying, then use operator==. There are far more properties we don't |
| 3976 | // support modifying than those we do, and more could be added. |
| 3977 | RTCConfiguration modified_config = configuration_; |
| 3978 | modified_config.servers = configuration.servers; |
| 3979 | modified_config.type = configuration.type; |
| 3980 | modified_config.ice_candidate_pool_size = |
| 3981 | configuration.ice_candidate_pool_size; |
| 3982 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 3983 | modified_config.turn_port_prune_policy = configuration.turn_port_prune_policy; |
Qingsi Wang | bca1485 | 2019-06-26 21:56:02 | [diff] [blame] | 3984 | modified_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 3985 | configuration.surface_ice_candidates_on_ice_transport_type_changed; |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 3986 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 3987 | modified_config.ice_check_interval_strong_connectivity = |
| 3988 | configuration.ice_check_interval_strong_connectivity; |
| 3989 | modified_config.ice_check_interval_weak_connectivity = |
| 3990 | configuration.ice_check_interval_weak_connectivity; |
Qingsi Wang | 22e623a | 2018-03-13 17:53:57 | [diff] [blame] | 3991 | modified_config.ice_unwritable_timeout = configuration.ice_unwritable_timeout; |
| 3992 | modified_config.ice_unwritable_min_checks = |
| 3993 | configuration.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 3994 | modified_config.ice_inactive_timeout = configuration.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 3995 | modified_config.stun_candidate_keepalive_interval = |
| 3996 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 12:01:40 | [diff] [blame] | 3997 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 3998 | modified_config.network_preference = configuration.network_preference; |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 3999 | modified_config.active_reset_srtp_params = |
| 4000 | configuration.active_reset_srtp_params; |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 4001 | modified_config.turn_logging_id = configuration.turn_logging_id; |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4002 | modified_config.allow_codec_switching = configuration.allow_codec_switching; |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4003 | if (configuration != modified_config) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4004 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION, |
| 4005 | "Modifying the configuration in an unsupported way."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4006 | } |
| 4007 | |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 4008 | // Validate the modified configuration. |
| 4009 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 4010 | if (!validate_error.ok()) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4011 | return validate_error; |
Steve Anton | 038834f | 2017-07-14 22:59:59 | [diff] [blame] | 4012 | } |
| 4013 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4014 | // Note that this isn't possible through chromium, since it's an unsigned |
| 4015 | // short in WebIDL. |
| 4016 | if (configuration.ice_candidate_pool_size < 0 || |
Wez | 939eb80 | 2018-05-03 10:34:17 | [diff] [blame] | 4017 | configuration.ice_candidate_pool_size > static_cast<int>(UINT16_MAX)) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4018 | return RTCError(RTCErrorType::INVALID_RANGE); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | // Parse ICE servers before hopping to network thread. |
| 4022 | cricket::ServerAddresses stun_servers; |
| 4023 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 4024 | RTCErrorType parse_error = |
| 4025 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 4026 | if (parse_error != RTCErrorType::NONE) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4027 | return RTCError(parse_error); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4028 | } |
Jonas Oreland | 3c02842 | 2019-08-22 14:16:35 | [diff] [blame] | 4029 | // Add the turn logging id to all turn servers |
| 4030 | for (cricket::RelayServerConfig& turn_server : turn_servers) { |
| 4031 | turn_server.turn_logging_id = configuration.turn_logging_id; |
| 4032 | } |
| 4033 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4034 | // Note if STUN or TURN servers were supplied. |
| 4035 | if (!stun_servers.empty()) { |
| 4036 | NoteUsageEvent(UsageEvent::STUN_SERVER_ADDED); |
| 4037 | } |
| 4038 | if (!turn_servers.empty()) { |
| 4039 | NoteUsageEvent(UsageEvent::TURN_SERVER_ADDED); |
| 4040 | } |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4041 | |
| 4042 | // In theory this shouldn't fail. |
| 4043 | if (!network_thread()->Invoke<bool>( |
| 4044 | RTC_FROM_HERE, |
| 4045 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 4046 | stun_servers, turn_servers, modified_config.type, |
| 4047 | modified_config.ice_candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 4048 | modified_config.GetTurnPortPrunePolicy(), |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 4049 | modified_config.turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4050 | modified_config.stun_candidate_keepalive_interval, |
| 4051 | static_cast<bool>(local_description())))) { |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4052 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4053 | "Failed to apply configuration to PortAllocator."); |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4054 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 4055 | |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 4056 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 4057 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 4058 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4059 | if (modified_config.servers != configuration_.servers || |
Jonas Oreland | e309651 | 2020-05-27 07:01:05 | [diff] [blame] | 4060 | NeedIceRestart( |
| 4061 | configuration_.surface_ice_candidates_on_ice_transport_type_changed, |
| 4062 | configuration_.type, modified_config.type) || |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 4063 | modified_config.GetTurnPortPrunePolicy() != |
| 4064 | configuration_.GetTurnPortPrunePolicy()) { |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4065 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 21:15:33 | [diff] [blame] | 4066 | } |
skvlad | d1f5fda | 2017-02-04 00:54:05 | [diff] [blame] | 4067 | |
Qingsi Wang | 9c98f0c | 2018-02-15 23:10:59 | [diff] [blame] | 4068 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
Bjorn A Mellem | 5985a04 | 2019-06-28 21:19:38 | [diff] [blame] | 4069 | |
Zhi Huang | b57e169 | 2018-06-12 18:41:11 | [diff] [blame] | 4070 | if (configuration_.active_reset_srtp_params != |
| 4071 | modified_config.active_reset_srtp_params) { |
| 4072 | transport_controller_->SetActiveResetSrtpParams( |
| 4073 | modified_config.active_reset_srtp_params); |
| 4074 | } |
| 4075 | |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4076 | if (modified_config.allow_codec_switching.has_value()) { |
Taylor Brandstetter | 6b381c7 | 2020-08-24 20:28:47 | [diff] [blame] | 4077 | for (const auto& transceiver : transceivers_) { |
| 4078 | if (transceiver->media_type() != cricket::MEDIA_TYPE_VIDEO || |
| 4079 | !transceiver->internal()->channel()) { |
| 4080 | continue; |
| 4081 | } |
| 4082 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4083 | transceiver->internal()->channel()); |
| 4084 | video_channel->media_channel()->SetVideoCodecSwitchingEnabled( |
philipel | 01294f0 | 2019-11-14 12:03:25 | [diff] [blame] | 4085 | *modified_config.allow_codec_switching); |
| 4086 | } |
philipel | 16cec3b | 2019-10-25 10:23:02 | [diff] [blame] | 4087 | } |
| 4088 | |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 4089 | configuration_ = modified_config; |
Niels Möller | 2579f0c | 2019-08-19 07:58:17 | [diff] [blame] | 4090 | return RTCError::OK(); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 | [diff] [blame] | 4091 | } |
| 4092 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4093 | bool PeerConnection::AddIceCandidate( |
| 4094 | const IceCandidateInterface* ice_candidate) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4095 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 4096 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4097 | if (IsClosed()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4098 | RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4099 | NoteAddIceCandidateResult(kAddIceCandidateFailClosed); |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4100 | return false; |
| 4101 | } |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4102 | |
| 4103 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4104 | RTC_LOG(LS_ERROR) << "AddIceCandidate: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 4105 | "without any remote session description."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4106 | NoteAddIceCandidateResult(kAddIceCandidateFailNoRemoteDescription); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4107 | return false; |
| 4108 | } |
| 4109 | |
| 4110 | if (!ice_candidate) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4111 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate is null."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4112 | NoteAddIceCandidateResult(kAddIceCandidateFailNullCandidate); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4113 | return false; |
| 4114 | } |
| 4115 | |
| 4116 | bool valid = false; |
| 4117 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 4118 | if (!valid) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4119 | NoteAddIceCandidateResult(kAddIceCandidateFailNotValid); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4120 | return false; |
| 4121 | } |
| 4122 | |
| 4123 | // Add this candidate to the remote session description. |
| 4124 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4125 | RTC_LOG(LS_ERROR) << "AddIceCandidate: Candidate cannot be used."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4126 | NoteAddIceCandidateResult(kAddIceCandidateFailInAddition); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4127 | return false; |
| 4128 | } |
| 4129 | |
| 4130 | if (ready) { |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4131 | bool result = UseCandidate(ice_candidate); |
| 4132 | if (result) { |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4133 | NoteUsageEvent(UsageEvent::ADD_ICE_CANDIDATE_SUCCEEDED); |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4134 | NoteAddIceCandidateResult(kAddIceCandidateSuccess); |
| 4135 | } else { |
| 4136 | NoteAddIceCandidateResult(kAddIceCandidateFailNotUsable); |
| 4137 | } |
| 4138 | return result; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4139 | } else { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4140 | RTC_LOG(LS_INFO) << "AddIceCandidate: Not ready to use candidate."; |
Harald Alvestrand | 76829d7 | 2018-07-18 21:24:36 | [diff] [blame] | 4141 | NoteAddIceCandidateResult(kAddIceCandidateFailNotReady); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4142 | return true; |
| 4143 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4144 | } |
| 4145 | |
Henrik Boström | ee6f4f6 | 2019-11-06 11:36:12 | [diff] [blame] | 4146 | void PeerConnection::AddIceCandidate( |
| 4147 | std::unique_ptr<IceCandidateInterface> candidate, |
| 4148 | std::function<void(RTCError)> callback) { |
| 4149 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4150 | // Chain this operation. If asynchronous operations are pending on the chain, |
| 4151 | // this operation will be queued to be invoked, otherwise the contents of the |
| 4152 | // lambda will execute immediately. |
| 4153 | operations_chain_->ChainOperation( |
| 4154 | [this_weak_ptr = weak_ptr_factory_.GetWeakPtr(), |
| 4155 | candidate = std::move(candidate), callback = std::move(callback)]( |
| 4156 | std::function<void()> operations_chain_callback) { |
| 4157 | if (!this_weak_ptr) { |
| 4158 | operations_chain_callback(); |
| 4159 | callback(RTCError( |
| 4160 | RTCErrorType::INVALID_STATE, |
| 4161 | "AddIceCandidate failed because the session was shut down")); |
| 4162 | return; |
| 4163 | } |
| 4164 | if (!this_weak_ptr->AddIceCandidate(candidate.get())) { |
| 4165 | operations_chain_callback(); |
| 4166 | // Fail with an error type and message consistent with Chromium. |
| 4167 | // TODO(hbos): Fail with error types according to spec. |
| 4168 | callback(RTCError(RTCErrorType::UNSUPPORTED_OPERATION, |
| 4169 | "Error processing ICE candidate")); |
| 4170 | return; |
| 4171 | } |
| 4172 | operations_chain_callback(); |
| 4173 | callback(RTCError::OK()); |
| 4174 | }); |
| 4175 | } |
| 4176 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4177 | bool PeerConnection::RemoveIceCandidates( |
| 4178 | const std::vector<cricket::Candidate>& candidates) { |
| 4179 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4180 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4181 | if (IsClosed()) { |
| 4182 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: PeerConnection is closed."; |
| 4183 | return false; |
| 4184 | } |
| 4185 | |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4186 | if (!remote_description()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4187 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: ICE candidates can't be removed " |
| 4188 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4189 | return false; |
| 4190 | } |
| 4191 | |
| 4192 | if (candidates.empty()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4193 | RTC_LOG(LS_ERROR) << "RemoveIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4194 | return false; |
| 4195 | } |
| 4196 | |
| 4197 | size_t number_removed = |
| 4198 | mutable_remote_description()->RemoveCandidates(candidates); |
| 4199 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4200 | RTC_LOG(LS_ERROR) |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4201 | << "RemoveIceCandidates: Failed to remove candidates. Requested " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 4202 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4203 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | // Remove the candidates from the transport controller. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 4207 | RTCError error = transport_controller_->RemoveRemoteCandidates(candidates); |
| 4208 | if (!error.ok()) { |
Steve Anton | c79268f | 2018-04-24 16:54:10 | [diff] [blame] | 4209 | RTC_LOG(LS_ERROR) |
| 4210 | << "RemoveIceCandidates: Error when removing remote candidates: " |
| 4211 | << error.message(); |
Steve Anton | d25da37 | 2017-11-06 22:50:29 | [diff] [blame] | 4212 | } |
| 4213 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4214 | } |
| 4215 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4216 | RTCError PeerConnection::SetBitrate(const BitrateSettings& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4217 | if (!worker_thread()->IsCurrent()) { |
| 4218 | return worker_thread()->Invoke<RTCError>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4219 | RTC_FROM_HERE, [&]() { return SetBitrate(bitrate); }); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4220 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 4221 | RTC_DCHECK_RUN_ON(worker_thread()); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4222 | |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4223 | const bool has_min = bitrate.min_bitrate_bps.has_value(); |
| 4224 | const bool has_start = bitrate.start_bitrate_bps.has_value(); |
| 4225 | const bool has_max = bitrate.max_bitrate_bps.has_value(); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4226 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 4227 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4228 | "min_bitrate_bps <= 0"); |
| 4229 | } |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4230 | if (has_start) { |
| 4231 | if (has_min && *bitrate.start_bitrate_bps < *bitrate.min_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4232 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4233 | "start_bitrate_bps < min_bitrate_bps"); |
| 4234 | } else if (*bitrate.start_bitrate_bps < 0) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4235 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4236 | "curent_bitrate_bps < 0"); |
| 4237 | } |
| 4238 | } |
| 4239 | if (has_max) { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4240 | if (has_start && *bitrate.max_bitrate_bps < *bitrate.start_bitrate_bps) { |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4241 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
Niels Möller | 0c4f7be | 2018-05-07 12:01:37 | [diff] [blame] | 4242 | "max_bitrate_bps < start_bitrate_bps"); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4243 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 4244 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4245 | "max_bitrate_bps < min_bitrate_bps"); |
| 4246 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 4247 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4248 | "max_bitrate_bps < 0"); |
| 4249 | } |
| 4250 | } |
| 4251 | |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4252 | RTC_DCHECK(call_.get()); |
Piotr (Peter) Slatala | 7fbfaa4 | 2019-03-18 17:31:54 | [diff] [blame] | 4253 | call_->SetClientBitratePreferences(bitrate); |
zstein | 4b97980 | 2017-06-02 21:37:37 | [diff] [blame] | 4254 | |
| 4255 | return RTCError::OK(); |
| 4256 | } |
| 4257 | |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4258 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 4259 | if (!worker_thread()->IsCurrent()) { |
| 4260 | worker_thread()->Invoke<void>( |
| 4261 | RTC_FROM_HERE, |
| 4262 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 4263 | return; |
| 4264 | } |
| 4265 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 4266 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4267 | audio_state->SetPlayout(playout); |
| 4268 | } |
| 4269 | |
| 4270 | void PeerConnection::SetAudioRecording(bool recording) { |
| 4271 | if (!worker_thread()->IsCurrent()) { |
| 4272 | worker_thread()->Invoke<void>( |
| 4273 | RTC_FROM_HERE, |
| 4274 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 4275 | return; |
| 4276 | } |
| 4277 | auto audio_state = |
Sebastian Jansson | 6eb8a16 | 2018-11-16 10:29:55 | [diff] [blame] | 4278 | factory_->channel_manager()->media_engine()->voice().GetAudioState(); |
henrika | 5f6bf24 | 2017-11-01 10:06:56 | [diff] [blame] | 4279 | audio_state->SetRecording(recording); |
| 4280 | } |
| 4281 | |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4282 | std::unique_ptr<rtc::SSLCertificate> |
| 4283 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 4284 | std::unique_ptr<rtc::SSLCertChain> chain = GetRemoteAudioSSLCertChain(); |
| 4285 | if (!chain || !chain->GetSize()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4286 | return nullptr; |
| 4287 | } |
Steve Anton | f25303e | 2018-10-16 22:23:31 | [diff] [blame] | 4288 | return chain->Get(0).Clone(); |
Steve Anton | 8c0f7a7 | 2017-10-03 17:03:10 | [diff] [blame] | 4289 | } |
| 4290 | |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4291 | std::unique_ptr<rtc::SSLCertChain> |
| 4292 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 4293 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4294 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 4295 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4296 | return nullptr; |
| 4297 | } |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4298 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4299 | audio_transceiver->internal()->channel()->transport_name()); |
| 4300 | } |
| 4301 | |
| 4302 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4303 | PeerConnection::GetFirstAudioTransceiver() const { |
| 4304 | for (auto transceiver : transceivers_) { |
| 4305 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4306 | return transceiver; |
| 4307 | } |
| 4308 | } |
| 4309 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 4310 | } |
| 4311 | |
Henrik Boström | 4c1e7cc | 2020-06-11 10:26:53 | [diff] [blame] | 4312 | void PeerConnection::AddAdaptationResource( |
| 4313 | rtc::scoped_refptr<Resource> resource) { |
| 4314 | if (!worker_thread()->IsCurrent()) { |
| 4315 | return worker_thread()->Invoke<void>(RTC_FROM_HERE, [this, resource]() { |
| 4316 | return AddAdaptationResource(resource); |
| 4317 | }); |
| 4318 | } |
| 4319 | RTC_DCHECK_RUN_ON(worker_thread()); |
| 4320 | if (!call_) { |
| 4321 | // The PeerConnection has been closed. |
| 4322 | return; |
| 4323 | } |
| 4324 | call_->AddAdaptationResource(resource); |
| 4325 | } |
| 4326 | |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 4327 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 4328 | int64_t output_period_ms) { |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 4329 | return worker_thread()->Invoke<bool>( |
Danil Chapovalov | 116ffe7 | 2019-09-05 08:21:11 | [diff] [blame] | 4330 | RTC_FROM_HERE, |
| 4331 | [this, output = std::move(output), output_period_ms]() mutable { |
| 4332 | return StartRtcEventLog_w(std::move(output), output_period_ms); |
| 4333 | }); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4334 | } |
| 4335 | |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 4336 | bool PeerConnection::StartRtcEventLog( |
| 4337 | std::unique_ptr<RtcEventLogOutput> output) { |
Niels Möller | 695cf6a | 2019-05-13 10:27:23 | [diff] [blame] | 4338 | int64_t output_period_ms = webrtc::RtcEventLog::kImmediateOutput; |
| 4339 | if (field_trial::IsEnabled("WebRTC-RtcEventLogNewFormat")) { |
| 4340 | output_period_ms = 5000; |
| 4341 | } |
| 4342 | return StartRtcEventLog(std::move(output), output_period_ms); |
Niels Möller | f00ca1a | 2019-05-10 09:33:12 | [diff] [blame] | 4343 | } |
| 4344 | |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4345 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4346 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 4347 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 4348 | } |
| 4349 | |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 4350 | rtc::scoped_refptr<DtlsTransportInterface> |
| 4351 | PeerConnection::LookupDtlsTransportByMid(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4352 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 4353 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4354 | } |
| 4355 | |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 4356 | rtc::scoped_refptr<DtlsTransport> |
| 4357 | PeerConnection::LookupDtlsTransportByMidInternal(const std::string& mid) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4358 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 4359 | return transport_controller_->LookupDtlsTransportByMid(mid); |
| 4360 | } |
| 4361 | |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 4362 | rtc::scoped_refptr<SctpTransportInterface> PeerConnection::GetSctpTransport() |
| 4363 | const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 4364 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 4365 | if (!sctp_mid_s_) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 4366 | return nullptr; |
| 4367 | } |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 4368 | return transport_controller_->GetSctpTransport(*sctp_mid_s_); |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 4369 | } |
| 4370 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4371 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4372 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4373 | return pending_local_description_ ? pending_local_description_.get() |
| 4374 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4378 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4379 | return pending_remote_description_ ? pending_remote_description_.get() |
| 4380 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4381 | } |
| 4382 | |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4383 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 4384 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4385 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4386 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 4390 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4391 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4392 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4393 | } |
| 4394 | |
| 4395 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 4396 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4397 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4398 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 4402 | const { |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 4403 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4404 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-21 01:56:17 | [diff] [blame] | 4405 | } |
| 4406 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4407 | void PeerConnection::Close() { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4408 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Peter Boström | 1a9d615 | 2015-12-08 21:15:17 | [diff] [blame] | 4409 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4410 | // Update stats here so that we have the most recent stats for tracks and |
| 4411 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 | [diff] [blame] | 4412 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4413 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4414 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4415 | NoteUsageEvent(UsageEvent::CLOSE_CALLED); |
Steve Anton | 3fe1b15 | 2017-12-12 18:20:08 | [diff] [blame] | 4416 | |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 4417 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 4418 | transceiver->internal()->SetPeerConnectionClosed(); |
| 4419 | if (!transceiver->stopped()) |
| 4420 | transceiver->StopInternal(); |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 4421 | } |
Steve Anton | 25cfeb9 | 2018-04-26 18:44:00 | [diff] [blame] | 4422 | |
| 4423 | // Ensure that all asynchronous stats requests are completed before destroying |
| 4424 | // the transport controller below. |
| 4425 | if (stats_collector_) { |
| 4426 | stats_collector_->WaitForPendingRequest(); |
| 4427 | } |
| 4428 | |
| 4429 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 4430 | // the last stats request can still read from the channels. |
Steve Anton | 8af2186 | 2017-12-15 19:20:13 | [diff] [blame] | 4431 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4432 | |
Qingsi Wang | 93a8439 | 2018-01-31 01:13:09 | [diff] [blame] | 4433 | // The event log is used in the transport controller, which must be outlived |
| 4434 | // by the former. CreateOffer by the peer connection is implemented |
| 4435 | // asynchronously and if the peer connection is closed without resetting the |
| 4436 | // WebRTC session description factory, the session description factory would |
| 4437 | // call the transport controller. |
| 4438 | webrtc_session_desc_factory_.reset(); |
| 4439 | transport_controller_.reset(); |
| 4440 | |
deadbeef | 42a4263 | 2017-03-10 23:18:00 | [diff] [blame] | 4441 | network_thread()->Invoke<void>( |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 4442 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 4443 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 4444 | |
Steve Anton | 978b876 | 2017-09-29 19:15:02 | [diff] [blame] | 4445 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 4446 | RTC_DCHECK_RUN_ON(worker_thread()); |
eladalon | 248fd4f | 2017-09-06 12:18:15 | [diff] [blame] | 4447 | call_.reset(); |
| 4448 | // The event log must outlive call (and any other object that uses it). |
| 4449 | event_log_.reset(); |
| 4450 | }); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4451 | ReportUsagePattern(); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4452 | // The .h file says that observer can be discarded after close() returns. |
| 4453 | // Make sure this is true. |
| 4454 | observer_ = nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4455 | } |
| 4456 | |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4457 | void PeerConnection::OnMessage(rtc::Message* msg) { |
Karl Wiberg | 744310f | 2019-02-14 09:18:56 | [diff] [blame] | 4458 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4459 | switch (msg->message_id) { |
| 4460 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 4461 | SetSessionDescriptionMsg* param = |
| 4462 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4463 | param->observer->OnSuccess(); |
| 4464 | delete param; |
| 4465 | break; |
| 4466 | } |
| 4467 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 4468 | SetSessionDescriptionMsg* param = |
| 4469 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 4470 | param->observer->OnFailure(std::move(param->error)); |
| 4471 | delete param; |
| 4472 | break; |
| 4473 | } |
| 4474 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 4475 | CreateSessionDescriptionMsg* param = |
| 4476 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 4477 | param->observer->OnFailure(std::move(param->error)); |
| 4478 | delete param; |
| 4479 | break; |
| 4480 | } |
| 4481 | case MSG_GETSTATS: { |
| 4482 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
| 4483 | StatsReports reports; |
| 4484 | stats_->GetStats(param->track, &reports); |
| 4485 | param->observer->OnComplete(reports); |
| 4486 | delete param; |
| 4487 | break; |
| 4488 | } |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4489 | case MSG_REPORT_USAGE_PATTERN: { |
| 4490 | ReportUsagePattern(); |
| 4491 | break; |
| 4492 | } |
| 4493 | default: |
| 4494 | RTC_NOTREACHED() << "Not implemented"; |
| 4495 | break; |
| 4496 | } |
| 4497 | } |
| 4498 | |
Steve Anton | afb0bb7 | 2018-02-20 19:35:37 | [diff] [blame] | 4499 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 4500 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4501 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 4502 | GetAudioTransceiver()->internal()->channel()); |
| 4503 | if (voice_channel) { |
| 4504 | return voice_channel->media_channel(); |
| 4505 | } else { |
| 4506 | return nullptr; |
| 4507 | } |
| 4508 | } |
| 4509 | |
| 4510 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 4511 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4512 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 4513 | GetVideoTransceiver()->internal()->channel()); |
| 4514 | if (video_channel) { |
| 4515 | return video_channel->media_channel(); |
| 4516 | } else { |
| 4517 | return nullptr; |
| 4518 | } |
| 4519 | } |
| 4520 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4521 | void PeerConnection::CreateAudioReceiver( |
| 4522 | MediaStreamInterface* stream, |
| 4523 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 4524 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4525 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 4526 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4527 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4528 | auto* audio_receiver = new AudioRtpReceiver( |
| 4529 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4530 | audio_receiver->SetMediaChannel(voice_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 4531 | if (remote_sender_info.sender_id == kDefaultAudioSenderId) { |
| 4532 | audio_receiver->SetupUnsignaledMediaChannel(); |
| 4533 | } else { |
| 4534 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4535 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4536 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4537 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4538 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 4539 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4540 | NoteUsageEvent(UsageEvent::AUDIO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4541 | } |
| 4542 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4543 | void PeerConnection::CreateVideoReceiver( |
| 4544 | MediaStreamInterface* stream, |
| 4545 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 12:41:51 | [diff] [blame] | 4546 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 4547 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Henrik Boström | 199e27b | 2018-07-04 18:51:53 | [diff] [blame] | 4548 | // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use |
| 4549 | // the constructor taking stream IDs instead. |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4550 | auto* video_receiver = new VideoRtpReceiver( |
| 4551 | worker_thread(), remote_sender_info.sender_id, streams); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4552 | video_receiver->SetMediaChannel(video_media_channel()); |
Saurav Das | 7262fc2 | 2019-09-11 23:23:05 | [diff] [blame] | 4553 | if (remote_sender_info.sender_id == kDefaultVideoSenderId) { |
| 4554 | video_receiver->SetupUnsignaledMediaChannel(); |
| 4555 | } else { |
| 4556 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 4557 | } |
Steve Anton | d367921 | 2018-01-18 01:41:02 | [diff] [blame] | 4558 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 4559 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4560 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Mirko Bonadei | 05cf6be | 2019-01-31 20:38:12 | [diff] [blame] | 4561 | Observer()->OnAddTrack(receiver, streams); |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 4562 | NoteUsageEvent(UsageEvent::VIDEO_ADDED); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4563 | } |
| 4564 | |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4565 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 4566 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4567 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4568 | const RtpSenderInfo& remote_sender_info) { |
| 4569 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 4570 | if (!receiver) { |
| 4571 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 4572 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4573 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 4574 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4575 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 4576 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 4577 | } else { |
| 4578 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 4579 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 4580 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4581 | } |
| 4582 | |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4583 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 4584 | MediaStreamInterface* stream) { |
| 4585 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4586 | RTC_DCHECK(track); |
| 4587 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4588 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4589 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4590 | // We already have a sender for this track, so just change the stream_id |
| 4591 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 4592 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4593 | return; |
| 4594 | } |
| 4595 | |
| 4596 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4597 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_AUDIO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4598 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4599 | new_sender->internal()->SetMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4600 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4601 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 4602 | // which will connect the sender to the underlying transport. This can |
| 4603 | // occur if a local session description that contains the ID of the sender |
| 4604 | // is set before AddStream is called. It can also occur if the local |
| 4605 | // session description is not changed and RemoveStream is called, and |
| 4606 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4607 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4608 | FindSenderInfo(local_audio_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4609 | if (sender_info) { |
| 4610 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4611 | } |
| 4612 | } |
| 4613 | |
| 4614 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 4615 | // indefinitely, when we have unified plan SDP. |
| 4616 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 4617 | MediaStreamInterface* stream) { |
| 4618 | RTC_DCHECK(!IsClosed()); |
| 4619 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4620 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4621 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4622 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4623 | return; |
| 4624 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4625 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 4629 | MediaStreamInterface* stream) { |
| 4630 | RTC_DCHECK(!IsClosed()); |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4631 | RTC_DCHECK(track); |
| 4632 | RTC_DCHECK(stream); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4633 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4634 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4635 | // We already have a sender for this track, so just change the stream_id |
| 4636 | // so that it's correct in the next call to CreateOffer. |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 4637 | sender->internal()->set_stream_ids({stream->id()}); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4638 | return; |
| 4639 | } |
| 4640 | |
| 4641 | // Normal case; we've never seen this track before. |
Steve Anton | 111fdfd | 2018-06-25 20:03:36 | [diff] [blame] | 4642 | auto new_sender = CreateSender(cricket::MEDIA_TYPE_VIDEO, track->id(), track, |
Florent Castelli | 892acf0 | 2018-10-01 20:47:20 | [diff] [blame] | 4643 | {stream->id()}, {}); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 4644 | new_sender->internal()->SetMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4645 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 4646 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 4647 | FindSenderInfo(local_video_sender_infos_, stream->id(), track->id()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4648 | if (sender_info) { |
| 4649 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 4654 | MediaStreamInterface* stream) { |
| 4655 | RTC_DCHECK(!IsClosed()); |
| 4656 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4657 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4658 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 4659 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4660 | return; |
| 4661 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 4662 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4663 | } |
| 4664 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4665 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4666 | if (ice_connection_state_ == new_state) { |
| 4667 | return; |
| 4668 | } |
| 4669 | |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4670 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4671 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4672 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 18:50:27 | [diff] [blame] | 4673 | return; |
| 4674 | } |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4675 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4676 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 4677 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4678 | RTC_DCHECK(ice_connection_state_ != |
| 4679 | PeerConnectionInterface::kIceConnectionClosed); |
| 4680 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4681 | ice_connection_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4682 | Observer()->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4683 | } |
| 4684 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4685 | void PeerConnection::SetStandardizedIceConnectionState( |
| 4686 | PeerConnectionInterface::IceConnectionState new_state) { |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4687 | if (standardized_ice_connection_state_ == new_state) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4688 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | if (IsClosed()) { |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4692 | return; |
Qingsi Wang | 36e3147 | 2019-05-29 18:37:26 | [diff] [blame] | 4693 | } |
| 4694 | |
| 4695 | RTC_LOG(LS_INFO) << "Changing standardized IceConnectionState " |
| 4696 | << standardized_ice_connection_state_ << " => " << new_state; |
| 4697 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4698 | standardized_ice_connection_state_ = new_state; |
Jonas Olsson | 1204690 | 2018-12-06 10:25:14 | [diff] [blame] | 4699 | Observer()->OnStandardizedIceConnectionChange(new_state); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4700 | } |
| 4701 | |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4702 | void PeerConnection::SetConnectionState( |
| 4703 | PeerConnectionInterface::PeerConnectionState new_state) { |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4704 | if (connection_state_ == new_state) |
| 4705 | return; |
| 4706 | if (IsClosed()) |
| 4707 | return; |
| 4708 | connection_state_ = new_state; |
| 4709 | Observer()->OnConnectionChange(new_state); |
| 4710 | } |
| 4711 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4712 | void PeerConnection::OnIceGatheringChange( |
| 4713 | PeerConnectionInterface::IceGatheringState new_state) { |
| 4714 | if (IsClosed()) { |
| 4715 | return; |
| 4716 | } |
| 4717 | ice_gathering_state_ = new_state; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4718 | Observer()->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4719 | } |
| 4720 | |
jbauch | 81bf7b0 | 2017-03-25 15:31:12 | [diff] [blame] | 4721 | void PeerConnection::OnIceCandidate( |
| 4722 | std::unique_ptr<IceCandidateInterface> candidate) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4723 | if (IsClosed()) { |
| 4724 | return; |
| 4725 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4726 | ReportIceCandidateCollected(candidate->candidate()); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4727 | Observer()->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4728 | } |
| 4729 | |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 4730 | void PeerConnection::OnIceCandidateError(const std::string& address, |
| 4731 | int port, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 4732 | const std::string& url, |
| 4733 | int error_code, |
| 4734 | const std::string& error_text) { |
| 4735 | if (IsClosed()) { |
| 4736 | return; |
| 4737 | } |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 4738 | Observer()->OnIceCandidateError(address, port, url, error_code, error_text); |
| 4739 | // Leftover not to break wpt test during migration to the new API. |
| 4740 | Observer()->OnIceCandidateError(address + ":", url, error_code, error_text); |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 4741 | } |
| 4742 | |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4743 | void PeerConnection::OnIceCandidatesRemoved( |
| 4744 | const std::vector<cricket::Candidate>& candidates) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4745 | if (IsClosed()) { |
| 4746 | return; |
| 4747 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4748 | Observer()->OnIceCandidatesRemoved(candidates); |
Honghai Zhang | 7fb69db | 2016-03-14 18:59:18 | [diff] [blame] | 4749 | } |
| 4750 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 4751 | void PeerConnection::OnSelectedCandidatePairChanged( |
| 4752 | const cricket::CandidatePairChangeEvent& event) { |
| 4753 | if (IsClosed()) { |
| 4754 | return; |
| 4755 | } |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 4756 | |
Qingsi Wang | cc46b10c | 2019-09-12 18:19:01 | [diff] [blame] | 4757 | if (event.selected_candidate_pair.local_candidate().type() == |
| 4758 | LOCAL_PORT_TYPE && |
| 4759 | event.selected_candidate_pair.remote_candidate().type() == |
| 4760 | LOCAL_PORT_TYPE) { |
| 4761 | NoteUsageEvent(UsageEvent::DIRECT_CONNECTION_SELECTED); |
| 4762 | } |
| 4763 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 4764 | Observer()->OnIceSelectedCandidatePairChanged(event); |
| 4765 | } |
| 4766 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4767 | void PeerConnection::ChangeSignalingState( |
| 4768 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 4769 | if (signaling_state_ == signaling_state) { |
| 4770 | return; |
| 4771 | } |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 4772 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 4773 | << GetSignalingStateString(signaling_state_) |
| 4774 | << " New state: " |
| 4775 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4776 | signaling_state_ = signaling_state; |
| 4777 | if (signaling_state == kClosed) { |
| 4778 | ice_connection_state_ = kIceConnectionClosed; |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4779 | Observer()->OnIceConnectionChange(ice_connection_state_); |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 4780 | standardized_ice_connection_state_ = |
| 4781 | PeerConnectionInterface::IceConnectionState::kIceConnectionClosed; |
Jonas Olsson | 635474e | 2018-10-18 13:58:17 | [diff] [blame] | 4782 | connection_state_ = PeerConnectionInterface::PeerConnectionState::kClosed; |
| 4783 | Observer()->OnConnectionChange(connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4784 | } |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 4785 | Observer()->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 4786 | } |
| 4787 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4788 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 4789 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4790 | if (IsClosed()) { |
| 4791 | return; |
| 4792 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4793 | AddAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4794 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4795 | } |
| 4796 | |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4797 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 4798 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4799 | if (IsClosed()) { |
| 4800 | return; |
| 4801 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4802 | RemoveAudioTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4803 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 4807 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4808 | if (IsClosed()) { |
| 4809 | return; |
| 4810 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4811 | AddVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4812 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4813 | } |
| 4814 | |
| 4815 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 4816 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 18:07:25 | [diff] [blame] | 4817 | if (IsClosed()) { |
| 4818 | return; |
| 4819 | } |
korniltsev.anatoly | ec390b5 | 2017-07-25 00:00:25 | [diff] [blame] | 4820 | RemoveVideoTrack(track, stream); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 4821 | UpdateNegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-16 03:24:43 | [diff] [blame] | 4822 | } |
| 4823 | |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4824 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 4825 | SetSessionDescriptionObserver* observer) { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4826 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4827 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4828 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
Henrik Boström | 3163867 | 2017-11-23 16:48:32 | [diff] [blame] | 4829 | } |
| 4830 | |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4831 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 4832 | SetSessionDescriptionObserver* observer, |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4833 | RTCError&& error) { |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4834 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4835 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 4836 | msg->error = std::move(error); |
| 4837 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4838 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4839 | } |
| 4840 | |
| 4841 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 4842 | CreateSessionDescriptionObserver* observer, |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 4843 | RTCError error) { |
| 4844 | RTC_DCHECK(!error.ok()); |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 4845 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 4846 | msg->error = std::move(error); |
| 4847 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 4848 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4849 | } |
| 4850 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4851 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4852 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4853 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4854 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4855 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4856 | if (IsUnifiedPlan()) { |
| 4857 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 4858 | } else { |
| 4859 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 4860 | } |
| 4861 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4862 | // Intentionally unset the data channel type for RTP data channel with the |
| 4863 | // second condition. Otherwise the RTP data channels would be successfully |
| 4864 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 4865 | // when building with chromium. We want to leave RTP data channels broken, so |
| 4866 | // people won't try to use them. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 4867 | if (data_channel_controller_.HasRtpDataChannels() || |
| 4868 | data_channel_type() != cricket::DCT_RTP) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4869 | session_options->data_channel_type = data_channel_type(); |
| 4870 | } |
| 4871 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4872 | // Apply ICE restart flag and renomination flag. |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 4873 | bool ice_restart = offer_answer_options.ice_restart || |
| 4874 | local_ice_credentials_to_replace_->HasIceCredentials(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4875 | for (auto& options : session_options->media_description_options) { |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 4876 | options.transport_options.ice_restart = ice_restart; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4877 | options.transport_options.enable_ice_renomination = |
| 4878 | configuration_.enable_ice_renomination; |
| 4879 | } |
| 4880 | |
| 4881 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 4882 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 4883 | session_options->pooled_ice_credentials = |
| 4884 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 4885 | RTC_FROM_HERE, |
| 4886 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 4887 | port_allocator_.get())); |
Johannes Kron | 89f874e | 2018-11-12 09:25:48 | [diff] [blame] | 4888 | session_options->offer_extmap_allow_mixed = |
| 4889 | configuration_.offer_extmap_allow_mixed; |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 4890 | |
Harald Alvestrand | 4aa1192 | 2019-05-14 20:00:01 | [diff] [blame] | 4891 | // Allow fallback for using obsolete SCTP syntax. |
| 4892 | // Note that the default in |session_options| is true, while |
| 4893 | // the default in |options| is false. |
| 4894 | session_options->use_obsolete_sctp_sdp = |
| 4895 | offer_answer_options.use_obsolete_sctp_sdp; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | void PeerConnection::GetOptionsForPlanBOffer( |
| 4899 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 4900 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4901 | // Figure out transceiver directional preferences. |
Philipp Hancke | 0800010 | 2020-07-10 11:26:44 | [diff] [blame] | 4902 | bool send_audio = !GetAudioTransceiver()->internal()->senders().empty(); |
| 4903 | bool send_video = !GetVideoTransceiver()->internal()->senders().empty(); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4904 | |
| 4905 | // By default, generate sendrecv/recvonly m= sections. |
| 4906 | bool recv_audio = true; |
| 4907 | bool recv_video = true; |
| 4908 | |
| 4909 | // By default, only offer a new m= section if we have media to send with it. |
| 4910 | bool offer_new_audio_description = send_audio; |
| 4911 | bool offer_new_video_description = send_video; |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 4912 | bool offer_new_data_description = data_channel_controller_.HasDataChannels(); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4913 | |
| 4914 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4915 | if (offer_answer_options.offer_to_receive_audio != |
| 4916 | RTCOfferAnswerOptions::kUndefined) { |
| 4917 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4918 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4919 | offer_new_audio_description || |
| 4920 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4921 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4922 | if (offer_answer_options.offer_to_receive_video != |
| 4923 | RTCOfferAnswerOptions::kUndefined) { |
| 4924 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4925 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4926 | offer_new_video_description || |
| 4927 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4928 | } |
| 4929 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 4930 | absl::optional<size_t> audio_index; |
| 4931 | absl::optional<size_t> video_index; |
| 4932 | absl::optional<size_t> data_index; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4933 | // If a current description exists, generate m= sections in the same order, |
| 4934 | // using the first audio/video/data section that appears and rejecting |
| 4935 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4936 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4937 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 4938 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 4939 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 4940 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 4941 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 4942 | } |
| 4943 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4944 | // Add audio/video/data m= sections to the end if needed. |
| 4945 | if (!audio_index && offer_new_audio_description) { |
Markus Handell | 755c65d | 2020-06-23 23:06:10 | [diff] [blame] | 4946 | cricket::MediaDescriptionOptions options( |
| 4947 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
| 4948 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), false); |
| 4949 | options.header_extensions = |
| 4950 | channel_manager()->GetSupportedAudioRtpHeaderExtensions(); |
| 4951 | session_options->media_description_options.push_back(options); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4952 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 4953 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4954 | if (!video_index && offer_new_video_description) { |
Markus Handell | 755c65d | 2020-06-23 23:06:10 | [diff] [blame] | 4955 | cricket::MediaDescriptionOptions options( |
| 4956 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
| 4957 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), false); |
| 4958 | options.header_extensions = |
| 4959 | channel_manager()->GetSupportedVideoRtpHeaderExtensions(); |
| 4960 | session_options->media_description_options.push_back(options); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4961 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 20:14:45 | [diff] [blame] | 4962 | } |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4963 | if (!data_index && offer_new_data_description) { |
| 4964 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 4965 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 4966 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4967 | } |
| 4968 | |
| 4969 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 4970 | !audio_index ? nullptr |
| 4971 | : &session_options->media_description_options[*audio_index]; |
| 4972 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 4973 | !video_index ? nullptr |
| 4974 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 4975 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 4976 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 4977 | audio_media_description_options, |
| 4978 | video_media_description_options, |
| 4979 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4980 | } |
| 4981 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4982 | static cricket::MediaDescriptionOptions |
| 4983 | GetMediaDescriptionOptionsForTransceiver( |
| 4984 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4985 | transceiver, |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 4986 | const std::string& mid, |
| 4987 | bool is_create_offer) { |
| 4988 | // NOTE: a stopping transceiver should be treated as a stopped one in |
| 4989 | // createOffer as specified in |
| 4990 | // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer. |
| 4991 | bool stopped = |
| 4992 | is_create_offer ? transceiver->stopping() : transceiver->stopped(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 4993 | cricket::MediaDescriptionOptions media_description_options( |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 4994 | transceiver->media_type(), mid, transceiver->direction(), stopped); |
Florent Castelli | 2d9d82e | 2019-04-23 17:25:51 | [diff] [blame] | 4995 | media_description_options.codec_preferences = |
| 4996 | transceiver->codec_preferences(); |
Markus Handell | 755c65d | 2020-06-23 23:06:10 | [diff] [blame] | 4997 | media_description_options.header_extensions = |
| 4998 | transceiver->HeaderExtensionsToOffer(); |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 4999 | // This behavior is specified in JSEP. The gist is that: |
| 5000 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 5001 | // sendrecv. |
| 5002 | // 2. If the MSID is included, then it must be included in any subsequent |
| 5003 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5004 | if (stopped || (!RtpTransceiverDirectionHasSend(transceiver->direction()) && |
| 5005 | !transceiver->internal()->has_ever_been_used_to_send())) { |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5006 | return media_description_options; |
Steve Anton | 5f94aa2 | 2018-02-01 18:58:30 | [diff] [blame] | 5007 | } |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5008 | |
| 5009 | cricket::SenderOptions sender_options; |
| 5010 | sender_options.track_id = transceiver->sender()->id(); |
| 5011 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 5012 | |
| 5013 | // The following sets up RIDs and Simulcast. |
| 5014 | // RIDs are included if Simulcast is requested or if any RID was specified. |
Amit Hilbuch | 619b294 | 2019-02-26 23:55:19 | [diff] [blame] | 5015 | RtpParameters send_parameters = |
| 5016 | transceiver->internal()->sender_internal()->GetParametersInternal(); |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5017 | bool has_rids = std::any_of(send_parameters.encodings.begin(), |
| 5018 | send_parameters.encodings.end(), |
| 5019 | [](const RtpEncodingParameters& encoding) { |
| 5020 | return !encoding.rid.empty(); |
| 5021 | }); |
| 5022 | |
Amit Hilbuch | b7446ed | 2019-01-28 20:25:25 | [diff] [blame] | 5023 | std::vector<RidDescription> send_rids; |
| 5024 | SimulcastLayerList send_layers; |
| 5025 | for (const RtpEncodingParameters& encoding : send_parameters.encodings) { |
| 5026 | if (encoding.rid.empty()) { |
| 5027 | continue; |
| 5028 | } |
| 5029 | send_rids.push_back(RidDescription(encoding.rid, RidDirection::kSend)); |
| 5030 | send_layers.AddLayer(SimulcastLayer(encoding.rid, !encoding.active)); |
| 5031 | } |
| 5032 | |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5033 | if (has_rids) { |
| 5034 | sender_options.rids = send_rids; |
| 5035 | } |
| 5036 | |
| 5037 | sender_options.simulcast_layers = send_layers; |
Amit Hilbuch | bcd39d4 | 2019-01-26 01:13:56 | [diff] [blame] | 5038 | // When RIDs are configured, we must set num_sim_layers to 0 to. |
| 5039 | // Otherwise, num_sim_layers must be 1 because either there is no |
| 5040 | // simulcast, or simulcast is acheived by munging the SDP. |
| 5041 | sender_options.num_sim_layers = has_rids ? 0 : 1; |
| 5042 | media_description_options.sender_options.push_back(sender_options); |
| 5043 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5044 | return media_description_options; |
| 5045 | } |
| 5046 | |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5047 | // Returns the ContentInfo at mline index |i|, or null if none exists. |
| 5048 | static const ContentInfo* GetContentByIndex( |
| 5049 | const SessionDescriptionInterface* sdesc, |
| 5050 | size_t i) { |
| 5051 | if (!sdesc) { |
| 5052 | return nullptr; |
| 5053 | } |
| 5054 | const ContentInfos& contents = sdesc->description()->contents(); |
| 5055 | return (i < contents.size() ? &contents[i] : nullptr); |
| 5056 | } |
| 5057 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5058 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 5059 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5060 | cricket::MediaSessionOptions* session_options) { |
| 5061 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 5062 | // Offers) and 5.2.2 (Subsequent Offers). |
| 5063 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5064 | const ContentInfos no_infos; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5065 | const ContentInfos& local_contents = |
| 5066 | (local_description() ? local_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5067 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5068 | const ContentInfos& remote_contents = |
| 5069 | (remote_description() ? remote_description()->description()->contents() |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 5070 | : no_infos); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5071 | // The mline indices that can be recycled. New transceivers should reuse these |
| 5072 | // slots first. |
| 5073 | std::queue<size_t> recycleable_mline_indices; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5074 | // First, go through each media section that exists in either the local or |
| 5075 | // remote description and generate a media section in this offer for the |
| 5076 | // associated transceiver. If a media section can be recycled, generate a |
| 5077 | // default, rejected media section here that can be later overwritten. |
| 5078 | for (size_t i = 0; |
| 5079 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 5080 | // Either |local_content| or |remote_content| is non-null. |
| 5081 | const ContentInfo* local_content = |
| 5082 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5083 | const ContentInfo* current_local_content = |
| 5084 | GetContentByIndex(current_local_description(), i); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5085 | const ContentInfo* remote_content = |
| 5086 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5087 | const ContentInfo* current_remote_content = |
| 5088 | GetContentByIndex(current_remote_description(), i); |
| 5089 | bool had_been_rejected = |
| 5090 | (current_local_content && current_local_content->rejected) || |
| 5091 | (current_remote_content && current_remote_content->rejected); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5092 | const std::string& mid = |
| 5093 | (local_content ? local_content->name : remote_content->name); |
| 5094 | cricket::MediaType media_type = |
| 5095 | (local_content ? local_content->media_description()->type() |
| 5096 | : remote_content->media_description()->type()); |
| 5097 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5098 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5099 | // A media section is considered eligible for recycling if it is marked as |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5100 | // rejected in either the current local or current remote description. |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5101 | auto transceiver = GetAssociatedTransceiver(mid); |
| 5102 | if (!transceiver) { |
| 5103 | // No associated transceiver. The media section has been stopped. |
| 5104 | recycleable_mline_indices.push(i); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5105 | session_options->media_description_options.push_back( |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5106 | cricket::MediaDescriptionOptions(media_type, mid, |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5107 | RtpTransceiverDirection::kInactive, |
| 5108 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5109 | } else { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5110 | // NOTE: a stopping transceiver should be treated as a stopped one in |
| 5111 | // createOffer as specified in |
| 5112 | // https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer. |
| 5113 | if (had_been_rejected && transceiver->stopping()) { |
| 5114 | session_options->media_description_options.push_back( |
| 5115 | cricket::MediaDescriptionOptions( |
| 5116 | transceiver->media_type(), mid, |
| 5117 | RtpTransceiverDirection::kInactive, |
| 5118 | /*stopped=*/true)); |
| 5119 | recycleable_mline_indices.push(i); |
| 5120 | } else { |
| 5121 | session_options->media_description_options.push_back( |
| 5122 | GetMediaDescriptionOptionsForTransceiver( |
| 5123 | transceiver, mid, |
| 5124 | /*is_create_offer=*/true)); |
| 5125 | // CreateOffer shouldn't really cause any state changes in |
| 5126 | // PeerConnection, but we need a way to match new transceivers to new |
| 5127 | // media sections in SetLocalDescription and JSEP specifies this is |
| 5128 | // done by recording the index of the media section generated for the |
| 5129 | // transceiver in the offer. |
| 5130 | transceiver->internal()->set_mline_index(i); |
| 5131 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5132 | } |
| 5133 | } else { |
| 5134 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | c8ff160 | 2020-02-05 21:53:38 | [diff] [blame] | 5135 | if (had_been_rejected) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5136 | session_options->media_description_options.push_back( |
| 5137 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 5138 | } else { |
Steve Anton | c8ff160 | 2020-02-05 21:53:38 | [diff] [blame] | 5139 | RTC_CHECK(GetDataMid()); |
| 5140 | if (mid == *GetDataMid()) { |
| 5141 | session_options->media_description_options.push_back( |
| 5142 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 5143 | } else { |
| 5144 | session_options->media_description_options.push_back( |
| 5145 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 5146 | } |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5147 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5148 | } |
| 5149 | } |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5150 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5151 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 5152 | // and not associated). Reuse media sections marked as recyclable first, |
| 5153 | // otherwise append to the end of the offer. New media sections should be |
| 5154 | // added in the order they were added to the PeerConnection. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5155 | for (const auto& transceiver : transceivers_) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5156 | if (transceiver->mid() || transceiver->stopping()) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5157 | continue; |
| 5158 | } |
| 5159 | size_t mline_index; |
| 5160 | if (!recycleable_mline_indices.empty()) { |
| 5161 | mline_index = recycleable_mline_indices.front(); |
| 5162 | recycleable_mline_indices.pop(); |
| 5163 | session_options->media_description_options[mline_index] = |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5164 | GetMediaDescriptionOptionsForTransceiver( |
| 5165 | transceiver, mid_generator_(), /*is_create_offer=*/true); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5166 | } else { |
| 5167 | mline_index = session_options->media_description_options.size(); |
| 5168 | session_options->media_description_options.push_back( |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5169 | GetMediaDescriptionOptionsForTransceiver( |
| 5170 | transceiver, mid_generator_(), /*is_create_offer=*/true)); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5171 | } |
| 5172 | // See comment above for why CreateOffer changes the transceiver's state. |
| 5173 | transceiver->internal()->set_mline_index(mline_index); |
| 5174 | } |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5175 | // Lastly, add a m-section if we have local data channels and an m section |
| 5176 | // does not already exist. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5177 | if (!GetDataMid() && data_channel_controller_.HasDataChannels()) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5178 | session_options->media_description_options.push_back( |
Amit Hilbuch | ae3df54 | 2019-01-07 20:13:08 | [diff] [blame] | 5179 | GetMediaDescriptionOptionsForActiveData(mid_generator_())); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5180 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | void PeerConnection::GetOptionsForAnswer( |
| 5184 | const RTCOfferAnswerOptions& offer_answer_options, |
| 5185 | cricket::MediaSessionOptions* session_options) { |
| 5186 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 5187 | |
| 5188 | if (IsUnifiedPlan()) { |
| 5189 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 5190 | } else { |
| 5191 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 5192 | } |
| 5193 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5194 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 5195 | // the RTP data channels would be successfully negotiated by default and the |
| 5196 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 5197 | // We want to leave RTP data channels broken, so people won't try to use them. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5198 | if (data_channel_controller_.HasRtpDataChannels() || |
| 5199 | data_channel_type() != cricket::DCT_RTP) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5200 | session_options->data_channel_type = data_channel_type(); |
| 5201 | } |
| 5202 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5203 | // Apply ICE renomination flag. |
| 5204 | for (auto& options : session_options->media_description_options) { |
| 5205 | options.transport_options.enable_ice_renomination = |
| 5206 | configuration_.enable_ice_renomination; |
| 5207 | } |
zhihuang | 8f65cdf | 2016-05-07 01:40:30 | [diff] [blame] | 5208 | |
| 5209 | session_options->rtcp_cname = rtcp_cname_; |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 5210 | session_options->crypto_options = GetCryptoOptions(); |
Jonas Oreland | 1cd39fa | 2018-10-11 05:47:12 | [diff] [blame] | 5211 | session_options->pooled_ice_credentials = |
| 5212 | network_thread()->Invoke<std::vector<cricket::IceParameters>>( |
| 5213 | RTC_FROM_HERE, |
| 5214 | rtc::Bind(&cricket::PortAllocator::GetPooledIceCredentials, |
| 5215 | port_allocator_.get())); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5216 | } |
| 5217 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5218 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 5219 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 15:18:11 | [diff] [blame] | 5220 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5221 | // Figure out transceiver directional preferences. |
Philipp Hancke | 0800010 | 2020-07-10 11:26:44 | [diff] [blame] | 5222 | bool send_audio = !GetAudioTransceiver()->internal()->senders().empty(); |
| 5223 | bool send_video = !GetVideoTransceiver()->internal()->senders().empty(); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5224 | |
| 5225 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 5226 | // restricted by the direction in the offer. |
| 5227 | bool recv_audio = true; |
| 5228 | bool recv_video = true; |
| 5229 | |
| 5230 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5231 | if (offer_answer_options.offer_to_receive_audio != |
| 5232 | RTCOfferAnswerOptions::kUndefined) { |
| 5233 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-24 01:24:52 | [diff] [blame] | 5234 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5235 | if (offer_answer_options.offer_to_receive_video != |
| 5236 | RTCOfferAnswerOptions::kUndefined) { |
| 5237 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5238 | } |
| 5239 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5240 | absl::optional<size_t> audio_index; |
| 5241 | absl::optional<size_t> video_index; |
| 5242 | absl::optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 18:31:29 | [diff] [blame] | 5243 | |
| 5244 | // Generate m= sections that match those in the offer. |
| 5245 | // Note that mediasession.cc will handle intersection our preferred |
| 5246 | // direction with the offered direction. |
| 5247 | GenerateMediaDescriptionOptions( |
| 5248 | remote_description(), |
| 5249 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 5250 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 5251 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5252 | |
| 5253 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 5254 | !audio_index ? nullptr |
| 5255 | : &session_options->media_description_options[*audio_index]; |
| 5256 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 5257 | !video_index ? nullptr |
| 5258 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5259 | |
Amit Hilbuch | c63ddb2 | 2019-01-02 18:13:58 | [diff] [blame] | 5260 | AddPlanBRtpSenderOptions(GetSendersInternal(), |
| 5261 | audio_media_description_options, |
| 5262 | video_media_description_options, |
| 5263 | offer_answer_options.num_simulcast_layers); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5264 | } |
zhihuang | af38847 | 2016-11-02 23:49:48 | [diff] [blame] | 5265 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5266 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 5267 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 5268 | cricket::MediaSessionOptions* session_options) { |
| 5269 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 5270 | // Answers) and 5.3.2 (Subsequent Answers). |
| 5271 | RTC_DCHECK(remote_description()); |
| 5272 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 5273 | for (const ContentInfo& content : |
| 5274 | remote_description()->description()->contents()) { |
| 5275 | cricket::MediaType media_type = content.media_description()->type(); |
| 5276 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5277 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 5278 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 5279 | RTC_CHECK(transceiver); |
| 5280 | session_options->media_description_options.push_back( |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 5281 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name, |
| 5282 | /*is_create_offer=*/false)); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5283 | } else { |
| 5284 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 5285 | // Reject all data sections if data channels are disabled. |
| 5286 | // Reject a data section if it has already been rejected. |
| 5287 | // Reject all data sections except for the first one. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5288 | if (data_channel_type() == cricket::DCT_NONE || content.rejected || |
Steve Anton | dbf9d03 | 2018-01-19 23:23:40 | [diff] [blame] | 5289 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5290 | session_options->media_description_options.push_back( |
| 5291 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 5292 | } else { |
| 5293 | session_options->media_description_options.push_back( |
| 5294 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 5295 | } |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5296 | } |
| 5297 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5298 | } |
| 5299 | |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5300 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 5301 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 22:30:09 | [diff] [blame] | 5302 | RtpTransceiverDirection audio_direction, |
| 5303 | RtpTransceiverDirection video_direction, |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5304 | absl::optional<size_t>* audio_index, |
| 5305 | absl::optional<size_t>* video_index, |
| 5306 | absl::optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5307 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5308 | for (const cricket::ContentInfo& content : |
| 5309 | session_desc->description()->contents()) { |
| 5310 | if (IsAudioContent(&content)) { |
| 5311 | // If we already have an audio m= section, reject this extra one. |
| 5312 | if (*audio_index) { |
| 5313 | session_options->media_description_options.push_back( |
| 5314 | cricket::MediaDescriptionOptions( |
| 5315 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5316 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5317 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5318 | bool stopped = (audio_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5319 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5320 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_AUDIO, |
| 5321 | content.name, audio_direction, |
| 5322 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5323 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5324 | } |
Markus Handell | 755c65d | 2020-06-23 23:06:10 | [diff] [blame] | 5325 | session_options->media_description_options.back().header_extensions = |
| 5326 | channel_manager()->GetSupportedAudioRtpHeaderExtensions(); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5327 | } else if (IsVideoContent(&content)) { |
| 5328 | // If we already have an video m= section, reject this extra one. |
| 5329 | if (*video_index) { |
| 5330 | session_options->media_description_options.push_back( |
| 5331 | cricket::MediaDescriptionOptions( |
| 5332 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5333 | RtpTransceiverDirection::kInactive, /*stopped=*/true)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5334 | } else { |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5335 | bool stopped = (video_direction == RtpTransceiverDirection::kInactive); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5336 | session_options->media_description_options.push_back( |
Steve Anton | 5c72e71 | 2018-12-10 22:25:30 | [diff] [blame] | 5337 | cricket::MediaDescriptionOptions(cricket::MEDIA_TYPE_VIDEO, |
| 5338 | content.name, video_direction, |
| 5339 | stopped)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5340 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5341 | } |
Markus Handell | 755c65d | 2020-06-23 23:06:10 | [diff] [blame] | 5342 | session_options->media_description_options.back().header_extensions = |
| 5343 | channel_manager()->GetSupportedVideoRtpHeaderExtensions(); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5344 | } else { |
| 5345 | RTC_DCHECK(IsDataContent(&content)); |
| 5346 | // If we already have an data m= section, reject this extra one. |
| 5347 | if (*data_index) { |
| 5348 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5349 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5350 | } else { |
| 5351 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5352 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 09:53:30 | [diff] [blame] | 5353 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 21:10:50 | [diff] [blame] | 5354 | } |
| 5355 | } |
hta | a2a49d9 | 2016-03-04 10:51:39 | [diff] [blame] | 5356 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5357 | } |
| 5358 | |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5359 | cricket::MediaDescriptionOptions |
| 5360 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 5361 | const std::string& mid) const { |
| 5362 | // Direction for data sections is meaningless, but legacy endpoints might |
| 5363 | // expect sendrecv. |
| 5364 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5365 | RtpTransceiverDirection::kSendRecv, |
| 5366 | /*stopped=*/false); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5367 | AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(), |
| 5368 | &options); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5369 | return options; |
| 5370 | } |
| 5371 | |
| 5372 | cricket::MediaDescriptionOptions |
| 5373 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 5374 | const std::string& mid) const { |
| 5375 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 5376 | RtpTransceiverDirection::kInactive, |
| 5377 | /*stopped=*/true); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5378 | AddRtpDataChannelOptions(*data_channel_controller_.rtp_data_channels(), |
| 5379 | &options); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5380 | return options; |
| 5381 | } |
| 5382 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5383 | absl::optional<std::string> PeerConnection::GetDataMid() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5384 | switch (data_channel_type()) { |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5385 | case cricket::DCT_RTP: |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 5386 | if (!data_channel_controller_.rtp_data_channel()) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5387 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5388 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 5389 | return data_channel_controller_.rtp_data_channel()->content_name(); |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5390 | case cricket::DCT_SCTP: |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 5391 | return sctp_mid_s_; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5392 | default: |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 5393 | return absl::nullopt; |
Steve Anton | fa2260d | 2017-12-29 00:38:23 | [diff] [blame] | 5394 | } |
| 5395 | } |
| 5396 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5397 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 5398 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 5399 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5400 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 23:33:07 | [diff] [blame] | 5401 | } |
| 5402 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5403 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5404 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5405 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5406 | cricket::MediaType media_type, |
| 5407 | StreamCollection* new_streams) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5408 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5409 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5410 | std::vector<RtpSenderInfo>* current_senders = |
| 5411 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5412 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5413 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5414 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5415 | for (auto sender_it = current_senders->begin(); |
| 5416 | sender_it != current_senders->end(); |
| 5417 | /* incremented manually */) { |
| 5418 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5419 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5420 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5421 | std::string params_stream_id; |
| 5422 | if (params) { |
| 5423 | params_stream_id = |
| 5424 | (!params->first_stream_id().empty() ? params->first_stream_id() |
| 5425 | : kDefaultStreamId); |
| 5426 | } |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5427 | bool sender_exists = params && params->id == info.sender_id && |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5428 | params_stream_id == info.stream_id; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5429 | // If this is a default track, and we still need it, don't remove it. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5430 | if ((info.stream_id == kDefaultStreamId && default_sender_needed) || |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5431 | sender_exists) { |
| 5432 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5433 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5434 | OnRemoteSenderRemoved(info, media_type); |
| 5435 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5436 | } |
| 5437 | } |
| 5438 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5439 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5440 | for (const cricket::StreamParams& params : streams) { |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 5441 | if (!params.has_ssrcs()) { |
| 5442 | // The remote endpoint has streams, but didn't signal ssrcs. For an active |
| 5443 | // sender, this means it is coming from a Unified Plan endpoint,so we just |
| 5444 | // create a default. |
| 5445 | default_sender_needed = true; |
| 5446 | break; |
| 5447 | } |
| 5448 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5449 | // |params.id| is the sender id and the stream id uses the first of |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5450 | // |params.stream_ids|. The remote description could come from a Unified |
Seth Hampson | 5897a6e | 2018-04-03 18:16:33 | [diff] [blame] | 5451 | // Plan endpoint, with multiple or no stream_ids() signaled. Since this is |
| 5452 | // not supported in Plan B, we just take the first here and create the |
| 5453 | // default stream ID if none is specified. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5454 | const std::string& stream_id = |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5455 | (!params.first_stream_id().empty() ? params.first_stream_id() |
| 5456 | : kDefaultStreamId); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5457 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5458 | uint32_t ssrc = params.first_ssrc(); |
| 5459 | |
| 5460 | rtc::scoped_refptr<MediaStreamInterface> stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5461 | remote_streams_->find(stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5462 | if (!stream) { |
| 5463 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5464 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5465 | MediaStream::Create(stream_id)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5466 | remote_streams_->AddStream(stream); |
| 5467 | new_streams->AddStream(stream); |
| 5468 | } |
| 5469 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5470 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5471 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5472 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5473 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5474 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5475 | } |
| 5476 | } |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5477 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5478 | // Add default sender if necessary. |
| 5479 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5480 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5481 | remote_streams_->find(kDefaultStreamId); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5482 | if (!default_stream) { |
| 5483 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5484 | default_stream = MediaStreamProxy::Create( |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5485 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamId)); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5486 | remote_streams_->AddStream(default_stream); |
| 5487 | new_streams->AddStream(default_stream); |
| 5488 | } |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5489 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5490 | ? kDefaultAudioSenderId |
| 5491 | : kDefaultVideoSenderId; |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5492 | const RtpSenderInfo* default_sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5493 | FindSenderInfo(*current_senders, kDefaultStreamId, default_sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5494 | if (!default_sender_info) { |
| 5495 | current_senders->push_back( |
Ruslan Burakov | 7ea4605 | 2019-02-16 01:07:05 | [diff] [blame] | 5496 | RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5497 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-09 01:13:40 | [diff] [blame] | 5498 | } |
| 5499 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5500 | } |
| 5501 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5502 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 5503 | cricket::MediaType media_type) { |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 5504 | RTC_LOG(LS_INFO) << "Creating " << cricket::MediaTypeToString(media_type) |
| 5505 | << " receiver for track_id=" << sender_info.sender_id |
| 5506 | << " and stream_id=" << sender_info.stream_id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5507 | |
Steve Anton | 3d954a6 | 2018-04-02 18:27:23 | [diff] [blame] | 5508 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5509 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5510 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5511 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5512 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5513 | } else { |
nisse | eb4ca4e | 2017-01-12 10:24:27 | [diff] [blame] | 5514 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5515 | } |
| 5516 | } |
| 5517 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5518 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 5519 | cricket::MediaType media_type) { |
Seth Hampson | 83d676b | 2018-04-06 01:12:09 | [diff] [blame] | 5520 | RTC_LOG(LS_INFO) << "Removing " << cricket::MediaTypeToString(media_type) |
| 5521 | << " receiver for track_id=" << sender_info.sender_id |
| 5522 | << " and stream_id=" << sender_info.stream_id; |
| 5523 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5524 | MediaStreamInterface* stream = remote_streams_->find(sender_info.stream_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5525 | |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5526 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5527 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5528 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 5529 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5530 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5531 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5532 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5533 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5534 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5535 | } |
| 5536 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5537 | // Stopping or destroying a VideoRtpReceiver will end the |
| 5538 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5539 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5540 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5541 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5542 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 10:16:19 | [diff] [blame] | 5543 | // There's no guarantee the track is still available, e.g. the track may |
| 5544 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5545 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5546 | } |
| 5547 | } else { |
nisse | ede5da4 | 2017-01-12 13:15:36 | [diff] [blame] | 5548 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5549 | } |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5550 | if (receiver) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5551 | Observer()->OnRemoveTrack(receiver); |
Henrik Boström | 933d8b0 | 2017-10-10 17:05:16 | [diff] [blame] | 5552 | } |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5553 | } |
| 5554 | |
| 5555 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 5556 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 5557 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 5558 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 5559 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 5560 | streams_to_remove.push_back(stream); |
| 5561 | } |
| 5562 | } |
| 5563 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 20:02:21 | [diff] [blame] | 5564 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5565 | remote_streams_->RemoveStream(stream); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 5566 | Observer()->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5567 | } |
| 5568 | } |
| 5569 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5570 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5571 | const std::vector<cricket::StreamParams>& streams, |
| 5572 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5573 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5574 | |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5575 | // Find removed tracks. I.e., tracks where the track id, stream id or ssrc |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5576 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5577 | for (auto sender_it = current_senders->begin(); |
| 5578 | sender_it != current_senders->end(); |
| 5579 | /* incremented manually */) { |
| 5580 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5581 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5582 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 5583 | if (!params || params->id != info.sender_id || |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5584 | params->first_stream_id() != info.stream_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5585 | OnLocalSenderRemoved(info, media_type); |
| 5586 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5587 | } else { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5588 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5589 | } |
| 5590 | } |
| 5591 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5592 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5593 | for (const cricket::StreamParams& params : streams) { |
| 5594 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5595 | // sender id. |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5596 | const std::string& stream_id = params.first_stream_id(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5597 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5598 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5599 | const RtpSenderInfo* sender_info = |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5600 | FindSenderInfo(*current_senders, stream_id, sender_id); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5601 | if (!sender_info) { |
Seth Hampson | 845e878 | 2018-03-02 19:34:10 | [diff] [blame] | 5602 | current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc)); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5603 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | } |
| 5607 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5608 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 5609 | cricket::MediaType media_type) { |
Seth Hampson | 5b4f075 | 2018-04-02 23:31:36 | [diff] [blame] | 5610 | RTC_DCHECK(!IsUnifiedPlan()); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5611 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5612 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5613 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 5614 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5615 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5616 | return; |
| 5617 | } |
| 5618 | |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5619 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5620 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5621 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5622 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5623 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5624 | |
Henrik Andreassson | cc18917 | 2019-05-20 09:01:38 | [diff] [blame] | 5625 | sender->internal()->set_stream_ids({sender_info.stream_id}); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5626 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5627 | } |
| 5628 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5629 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 5630 | cricket::MediaType media_type) { |
| 5631 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5632 | if (!sender) { |
| 5633 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5634 | // SessionDescriptions has been renegotiated. |
| 5635 | return; |
| 5636 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5637 | |
| 5638 | // A sender has been removed from the SessionDescription but it's still |
| 5639 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 5640 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 5641 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5642 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5643 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5644 | return; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5645 | } |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5646 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5647 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5648 | } |
| 5649 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 5650 | void PeerConnection::OnSctpDataChannelClosed(DataChannelInterface* channel) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 5651 | // Since data_channel_controller doesn't do signals, this |
| 5652 | // signal is relayed here. |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 5653 | data_channel_controller_.OnSctpDataChannelClosed( |
| 5654 | static_cast<SctpDataChannel*>(channel)); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5655 | } |
| 5656 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5657 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5658 | PeerConnection::GetAudioTransceiver() const { |
| 5659 | // This method only works with Plan B SDP, where there is a single |
| 5660 | // audio/video transceiver. |
| 5661 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5662 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5663 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5664 | return transceiver; |
| 5665 | } |
| 5666 | } |
| 5667 | RTC_NOTREACHED(); |
| 5668 | return nullptr; |
| 5669 | } |
| 5670 | |
| 5671 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5672 | PeerConnection::GetVideoTransceiver() const { |
| 5673 | // This method only works with Plan B SDP, where there is a single |
| 5674 | // audio/video transceiver. |
| 5675 | RTC_DCHECK(!IsUnifiedPlan()); |
| 5676 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 5677 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5678 | return transceiver; |
| 5679 | } |
| 5680 | } |
| 5681 | RTC_NOTREACHED(); |
| 5682 | return nullptr; |
| 5683 | } |
| 5684 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5685 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5686 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5687 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5688 | for (auto sender : transceiver->internal()->senders()) { |
| 5689 | if (sender->track() == track) { |
| 5690 | return sender; |
| 5691 | } |
| 5692 | } |
| 5693 | } |
| 5694 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 19:26:01 | [diff] [blame] | 5695 | } |
| 5696 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5697 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 5698 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5699 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5700 | for (auto sender : transceiver->internal()->senders()) { |
| 5701 | if (sender->id() == sender_id) { |
| 5702 | return sender; |
| 5703 | } |
| 5704 | } |
| 5705 | } |
| 5706 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 5707 | } |
| 5708 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5709 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 5710 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5711 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5712 | for (auto receiver : transceiver->internal()->receivers()) { |
| 5713 | if (receiver->id() == receiver_id) { |
| 5714 | return receiver; |
| 5715 | } |
| 5716 | } |
| 5717 | } |
| 5718 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 23:53:55 | [diff] [blame] | 5719 | } |
| 5720 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5721 | std::vector<PeerConnection::RtpSenderInfo>* |
| 5722 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 5723 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5724 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 5725 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 5726 | ? &remote_audio_sender_infos_ |
| 5727 | : &remote_video_sender_infos_; |
| 5728 | } |
| 5729 | |
| 5730 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5731 | cricket::MediaType media_type) { |
| 5732 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 5733 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5734 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 5735 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5736 | } |
| 5737 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5738 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 5739 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5740 | const std::string& stream_id, |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5741 | const std::string sender_id) const { |
| 5742 | for (const RtpSenderInfo& sender_info : infos) { |
Emircan Uysaler | bc609eaa | 2018-03-27 21:57:18 | [diff] [blame] | 5743 | if (sender_info.stream_id == stream_id && |
| 5744 | sender_info.sender_id == sender_id) { |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 5745 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5746 | } |
| 5747 | } |
| 5748 | return nullptr; |
| 5749 | } |
| 5750 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 5751 | SctpDataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 5752 | return data_channel_controller_.FindDataChannelBySid(sid); |
deadbeef | ab9b2d1 | 2015-10-14 18:33:11 | [diff] [blame] | 5753 | } |
| 5754 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5755 | PeerConnection::InitializePortAllocatorResult |
| 5756 | PeerConnection::InitializePortAllocator_n( |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 5757 | const cricket::ServerAddresses& stun_servers, |
| 5758 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5759 | const RTCConfiguration& configuration) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5760 | RTC_DCHECK_RUN_ON(network_thread()); |
| 5761 | |
Taylor Brandstetter | f8e6577 | 2016-06-28 00:20:15 | [diff] [blame] | 5762 | port_allocator_->Initialize(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5763 | // To handle both internal and externally created port allocator, we will |
| 5764 | // enable BUNDLE here. |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5765 | int port_allocator_flags = port_allocator_->flags(); |
| 5766 | port_allocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
| 5767 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 5768 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5769 | // If the disable-IPv6 flag was specified, we'll not override it |
| 5770 | // by experiment. |
| 5771 | if (configuration.disable_ipv6) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5772 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Mirko Bonadei | 57cabed | 2020-04-01 10:03:11 | [diff] [blame] | 5773 | } else if (absl::StartsWith( |
| 5774 | webrtc::field_trial::FindFullName("WebRTC-IPv6Default"), |
| 5775 | "Disabled")) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5776 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5777 | } |
| 5778 | |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 5779 | if (configuration.disable_ipv6_on_wifi) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5780 | port_allocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5781 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 22:40:51 | [diff] [blame] | 5782 | } |
| 5783 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5784 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5785 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5786 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5787 | } |
| 5788 | |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 5789 | if (configuration.candidate_network_policy == |
| 5790 | kCandidateNetworkPolicyLowCost) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5791 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5792 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-06-01 01:29:12 | [diff] [blame] | 5793 | } |
| 5794 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 5795 | if (configuration.disable_link_local_networks) { |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5796 | port_allocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 5797 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 5798 | } |
| 5799 | |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5800 | port_allocator_->set_flags(port_allocator_flags); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5801 | // No step delay is used while allocating ports. |
| 5802 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 5803 | port_allocator_->SetCandidateFilter( |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5804 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3e | 2017-07-26 23:50:11 | [diff] [blame] | 5805 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5806 | |
Harald Alvestrand | b2a7478 | 2018-06-28 11:54:07 | [diff] [blame] | 5807 | auto turn_servers_copy = turn_servers; |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5808 | for (auto& turn_server : turn_servers_copy) { |
| 5809 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
Benjamin Wright | d6f86e8 | 2018-05-08 20:12:25 | [diff] [blame] | 5810 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5811 | // Call this last since it may create pooled allocator sessions using the |
| 5812 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5813 | port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5814 | stun_servers, std::move(turn_servers_copy), |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 5815 | configuration.ice_candidate_pool_size, |
| 5816 | configuration.GetTurnPortPrunePolicy(), configuration.turn_customizer, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5817 | configuration.stun_candidate_keepalive_interval); |
Karl Wiberg | fb3be39 | 2019-03-22 13:13:22 | [diff] [blame] | 5818 | |
| 5819 | InitializePortAllocatorResult res; |
| 5820 | res.enable_ipv6 = port_allocator_flags & cricket::PORTALLOCATOR_ENABLE_IPV6; |
| 5821 | return res; |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5822 | } |
| 5823 | |
deadbeef | 91dd567 | 2016-05-18 23:55:30 | [diff] [blame] | 5824 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 5825 | const cricket::ServerAddresses& stun_servers, |
| 5826 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 5827 | IceTransportsType type, |
| 5828 | int candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 5829 | PortPrunePolicy turn_port_prune_policy, |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 5830 | webrtc::TurnCustomizer* turn_customizer, |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5831 | absl::optional<int> stun_candidate_keepalive_interval, |
| 5832 | bool have_local_description) { |
Qingsi Wang | c129c35 | 2019-04-18 17:41:58 | [diff] [blame] | 5833 | port_allocator_->SetCandidateFilter( |
deadbeef | 293e926 | 2017-01-11 20:28:30 | [diff] [blame] | 5834 | ConvertIceTransportTypeToCandidateFilter(type)); |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 5835 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 5836 | // size is not allowed, and changing the set of ICE servers will not result |
| 5837 | // in new candidates being gathered. |
Karl Wiberg | 739506e | 2019-04-03 09:37:28 | [diff] [blame] | 5838 | if (have_local_description) { |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 5839 | port_allocator_->FreezeCandidatePool(); |
| 5840 | } |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5841 | // Add the custom tls turn servers if they exist. |
| 5842 | auto turn_servers_copy = turn_servers; |
| 5843 | for (auto& turn_server : turn_servers_copy) { |
| 5844 | turn_server.tls_cert_verifier = tls_cert_verifier_.get(); |
| 5845 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5846 | // Call this last since it may create pooled allocator sessions using the |
| 5847 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-13 02:49:32 | [diff] [blame] | 5848 | return port_allocator_->SetConfiguration( |
Benjamin Wright | 6f80f09 | 2018-08-14 00:06:26 | [diff] [blame] | 5849 | stun_servers, std::move(turn_servers_copy), candidate_pool_size, |
Honghai Zhang | f8998cf | 2019-10-14 18:27:50 | [diff] [blame] | 5850 | turn_port_prune_policy, turn_customizer, |
| 5851 | stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 15:15:11 | [diff] [blame] | 5852 | } |
| 5853 | |
Steve Anton | ba81867 | 2017-11-06 18:21:57 | [diff] [blame] | 5854 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 5855 | return factory_->channel_manager(); |
| 5856 | } |
| 5857 | |
Elad Alon | 99c3fe5 | 2017-10-13 14:29:40 | [diff] [blame] | 5858 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 5859 | std::unique_ptr<RtcEventLogOutput> output, |
| 5860 | int64_t output_period_ms) { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 5861 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 5862 | if (!event_log_) { |
| 5863 | return false; |
| 5864 | } |
Bjorn Terelius | de93943 | 2017-11-20 16:38:14 | [diff] [blame] | 5865 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | void PeerConnection::StopRtcEventLog_w() { |
Karl Wiberg | b03ab71 | 2019-02-14 10:59:57 | [diff] [blame] | 5869 | RTC_DCHECK_RUN_ON(worker_thread()); |
zhihuang | 7798501 | 2017-02-07 23:45:16 | [diff] [blame] | 5870 | if (event_log_) { |
| 5871 | event_log_->StopLogging(); |
| 5872 | } |
ivoc | 14d5dbe | 2016-07-04 14:06:55 | [diff] [blame] | 5873 | } |
nisse | eaabdf6 | 2017-05-05 09:23:02 | [diff] [blame] | 5874 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5875 | cricket::ChannelInterface* PeerConnection::GetChannel( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5876 | const std::string& content_name) { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5877 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5878 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 5879 | if (channel && channel->content_name() == content_name) { |
| 5880 | return channel; |
| 5881 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5882 | } |
| 5883 | if (rtp_data_channel() && |
| 5884 | rtp_data_channel()->content_name() == content_name) { |
| 5885 | return rtp_data_channel(); |
| 5886 | } |
| 5887 | return nullptr; |
| 5888 | } |
| 5889 | |
| 5890 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5891 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5892 | if (!local_description() || !remote_description()) { |
Harald Alvestrand | 977b265 | 2019-12-12 12:40:50 | [diff] [blame] | 5893 | RTC_LOG(LS_VERBOSE) |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5894 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5895 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5896 | return false; |
| 5897 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 5898 | if (!data_channel_controller_.data_channel_transport()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5899 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5900 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5901 | return false; |
| 5902 | } |
| 5903 | |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 5904 | absl::optional<rtc::SSLRole> dtls_role; |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 5905 | if (sctp_mid_s_) { |
| 5906 | dtls_role = transport_controller_->GetDtlsRole(*sctp_mid_s_); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 5907 | if (!dtls_role && is_caller_.has_value()) { |
| 5908 | dtls_role = *is_caller_ ? rtc::SSL_SERVER : rtc::SSL_CLIENT; |
| 5909 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5910 | *role = *dtls_role; |
| 5911 | return true; |
| 5912 | } |
| 5913 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 5917 | rtc::SSLRole* role) { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 5918 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5919 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 5920 | RTC_LOG(LS_INFO) |
| 5921 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 5922 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5923 | return false; |
| 5924 | } |
| 5925 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5926 | auto dtls_role = transport_controller_->GetDtlsRole(content_name); |
| 5927 | if (dtls_role) { |
| 5928 | *role = *dtls_role; |
| 5929 | return true; |
| 5930 | } |
| 5931 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5932 | } |
| 5933 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 5934 | void PeerConnection::SetSessionError(SessionError error, |
| 5935 | const std::string& error_desc) { |
| 5936 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 5937 | if (error != session_error_) { |
| 5938 | session_error_ = error; |
| 5939 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5940 | } |
| 5941 | } |
| 5942 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5943 | RTCError PeerConnection::UpdateSessionState( |
| 5944 | SdpType type, |
| 5945 | cricket::ContentSource source, |
| 5946 | const cricket::SessionDescription* description) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5947 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5948 | |
| 5949 | // If there's already a pending error then no state transition should happen. |
| 5950 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 5951 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5952 | |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5953 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5954 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5955 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5956 | } |
| 5957 | |
| 5958 | // Update the signaling state according to the specified state machine (see |
| 5959 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5960 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5961 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5962 | ? PeerConnectionInterface::kHaveLocalOffer |
| 5963 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5964 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5965 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 5966 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 5967 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5968 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5969 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5970 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 5971 | transceiver_stable_states_by_transceivers_.clear(); |
Eldar Rello | d9ebe01 | 2020-03-18 18:41:45 | [diff] [blame] | 5972 | have_pending_rtp_data_channel_ = false; |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | // Update internal objects according to the session description's media |
| 5976 | // descriptions. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 5977 | RTCError error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5978 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-17 01:08:42 | [diff] [blame] | 5979 | return error; |
Steve Anton | 6d6a2ae | 2017-12-05 01:19:47 | [diff] [blame] | 5980 | } |
| 5981 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5982 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5983 | } |
| 5984 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5985 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 5986 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 5987 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5988 | const SessionDescriptionInterface* sdesc = |
| 5989 | (source == cricket::CS_LOCAL ? local_description() |
| 5990 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5991 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5992 | |
| 5993 | // Push down the new SDP media section for each audio/video transceiver. |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 5994 | for (const auto& transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5995 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5996 | FindMediaSectionForTransceiver(transceiver, sdesc); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 5997 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 5998 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 5999 | continue; |
| 6000 | } |
| 6001 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6002 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6003 | if (!content_desc) { |
| 6004 | continue; |
| 6005 | } |
| 6006 | std::string error; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 6007 | bool success = (source == cricket::CS_LOCAL) |
| 6008 | ? channel->SetLocalContent(content_desc, type, &error) |
| 6009 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6010 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 6011 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6012 | } |
| 6013 | } |
| 6014 | |
| 6015 | // If using the RtpDataChannel, push down the new SDP section for it too. |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6016 | if (data_channel_controller_.rtp_data_channel()) { |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6017 | const ContentInfo* data_content = |
| 6018 | cricket::GetFirstDataContent(sdesc->description()); |
| 6019 | if (data_content && !data_content->rejected) { |
| 6020 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6021 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6022 | if (data_desc) { |
| 6023 | std::string error; |
| 6024 | bool success = |
| 6025 | (source == cricket::CS_LOCAL) |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6026 | ? data_channel_controller_.rtp_data_channel()->SetLocalContent( |
| 6027 | data_desc, type, &error) |
| 6028 | : data_channel_controller_.rtp_data_channel()->SetRemoteContent( |
| 6029 | data_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6030 | if (!success) { |
Yves Gerey | ae6e0b2 | 2019-01-22 20:48:57 | [diff] [blame] | 6031 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, error); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6032 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6033 | } |
| 6034 | } |
| 6035 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6036 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6037 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 6038 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6039 | if (sctp_mid_s_ && local_description() && remote_description()) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6040 | rtc::scoped_refptr<SctpTransport> sctp_transport = |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6041 | transport_controller_->GetSctpTransport(*sctp_mid_s_); |
Harald Alvestrand | fbb45bd | 2019-05-15 06:07:47 | [diff] [blame] | 6042 | auto local_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6043 | local_description()->description()); |
| 6044 | auto remote_sctp_description = cricket::GetFirstSctpDataContentDescription( |
| 6045 | remote_description()->description()); |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6046 | if (sctp_transport && local_sctp_description && remote_sctp_description) { |
Harald Alvestrand | 8d3d6cf | 2019-05-16 09:49:17 | [diff] [blame] | 6047 | int max_message_size; |
| 6048 | // A remote max message size of zero means "any size supported". |
| 6049 | // We configure the connection with our own max message size. |
| 6050 | if (remote_sctp_description->max_message_size() == 0) { |
| 6051 | max_message_size = local_sctp_description->max_message_size(); |
| 6052 | } else { |
| 6053 | max_message_size = |
| 6054 | std::min(local_sctp_description->max_message_size(), |
| 6055 | remote_sctp_description->max_message_size()); |
Harald Alvestrand | fbb45bd | 2019-05-15 06:07:47 | [diff] [blame] | 6056 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6057 | sctp_transport->Start(local_sctp_description->port(), |
| 6058 | remote_sctp_description->port(), max_message_size); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6059 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6060 | } |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6061 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6062 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6063 | } |
| 6064 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6065 | RTCError PeerConnection::PushdownTransportDescription( |
| 6066 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6067 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6068 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6069 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6070 | if (source == cricket::CS_LOCAL) { |
| 6071 | const SessionDescriptionInterface* sdesc = local_description(); |
| 6072 | RTC_DCHECK(sdesc); |
| 6073 | return transport_controller_->SetLocalDescription(type, |
| 6074 | sdesc->description()); |
| 6075 | } else { |
| 6076 | const SessionDescriptionInterface* sdesc = remote_description(); |
| 6077 | RTC_DCHECK(sdesc); |
| 6078 | return transport_controller_->SetRemoteDescription(type, |
| 6079 | sdesc->description()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6080 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6081 | } |
| 6082 | |
| 6083 | bool PeerConnection::GetTransportDescription( |
| 6084 | const SessionDescription* description, |
| 6085 | const std::string& content_name, |
| 6086 | cricket::TransportDescription* tdesc) { |
| 6087 | if (!description || !tdesc) { |
| 6088 | return false; |
| 6089 | } |
| 6090 | const TransportInfo* transport_info = |
| 6091 | description->GetTransportInfoByName(content_name); |
| 6092 | if (!transport_info) { |
| 6093 | return false; |
| 6094 | } |
| 6095 | *tdesc = transport_info->description; |
| 6096 | return true; |
| 6097 | } |
| 6098 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6099 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 6100 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 6101 | cricket::ContinualGatheringPolicy gathering_policy; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6102 | switch (config.continual_gathering_policy) { |
| 6103 | case PeerConnectionInterface::GATHER_ONCE: |
| 6104 | gathering_policy = cricket::GATHER_ONCE; |
| 6105 | break; |
| 6106 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 6107 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 6108 | break; |
| 6109 | default: |
| 6110 | RTC_NOTREACHED(); |
| 6111 | gathering_policy = cricket::GATHER_ONCE; |
| 6112 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 6113 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6114 | cricket::IceConfig ice_config; |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 6115 | ice_config.receiving_timeout = RTCConfigurationToIceConfigOptionalInt( |
| 6116 | config.ice_connection_receiving_timeout); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6117 | ice_config.prioritize_most_likely_candidate_pairs = |
| 6118 | config.prioritize_most_likely_ice_candidate_pairs; |
| 6119 | ice_config.backup_connection_ping_interval = |
Qingsi Wang | 866e08d | 2018-03-23 00:54:23 | [diff] [blame] | 6120 | RTCConfigurationToIceConfigOptionalInt( |
| 6121 | config.ice_backup_candidate_pair_ping_interval); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6122 | ice_config.continual_gathering_policy = gathering_policy; |
| 6123 | ice_config.presume_writable_when_fully_relayed = |
| 6124 | config.presume_writable_when_fully_relayed; |
Qingsi Wang | 1fe119f | 2019-05-31 23:55:33 | [diff] [blame] | 6125 | ice_config.surface_ice_candidates_on_ice_transport_type_changed = |
| 6126 | config.surface_ice_candidates_on_ice_transport_type_changed; |
Qingsi Wang | e6826d2 | 2018-03-08 22:55:14 | [diff] [blame] | 6127 | ice_config.ice_check_interval_strong_connectivity = |
| 6128 | config.ice_check_interval_strong_connectivity; |
| 6129 | ice_config.ice_check_interval_weak_connectivity = |
| 6130 | config.ice_check_interval_weak_connectivity; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6131 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Jiawei Ou | cc88737 | 2018-11-30 07:08:51 | [diff] [blame] | 6132 | ice_config.ice_unwritable_timeout = config.ice_unwritable_timeout; |
| 6133 | ice_config.ice_unwritable_min_checks = config.ice_unwritable_min_checks; |
Jiawei Ou | 9d4fd555 | 2018-12-07 07:30:17 | [diff] [blame] | 6134 | ice_config.ice_inactive_timeout = config.ice_inactive_timeout; |
Qingsi Wang | db53f8e | 2018-02-20 22:45:49 | [diff] [blame] | 6135 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 18:38:40 | [diff] [blame] | 6136 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6137 | return ice_config; |
| 6138 | } |
| 6139 | |
Taylor Brandstetter | 3a034e1 | 2020-07-09 22:32:34 | [diff] [blame] | 6140 | std::vector<DataChannelStats> PeerConnection::GetDataChannelStats() const { |
Tomas Gunnarsson | 2e94de5 | 2020-06-16 14:54:10 | [diff] [blame] | 6141 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6142 | return data_channel_controller_.GetDataChannelStats(); |
| 6143 | } |
| 6144 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6145 | absl::optional<std::string> PeerConnection::sctp_transport_name() const { |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6146 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6147 | if (sctp_mid_s_ && transport_controller_) { |
| 6148 | auto dtls_transport = transport_controller_->GetDtlsTransport(*sctp_mid_s_); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6149 | if (dtls_transport) { |
| 6150 | return dtls_transport->transport_name(); |
| 6151 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6152 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6153 | } |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6154 | return absl::optional<std::string>(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6155 | } |
| 6156 | |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 6157 | cricket::CandidateStatsList PeerConnection::GetPooledCandidateStats() const { |
| 6158 | cricket::CandidateStatsList candidate_states_list; |
Qingsi Wang | a2d6067 | 2018-04-11 23:57:45 | [diff] [blame] | 6159 | network_thread()->Invoke<void>( |
| 6160 | RTC_FROM_HERE, |
| 6161 | rtc::Bind(&cricket::PortAllocator::GetCandidateStatsFromPooledSessions, |
| 6162 | port_allocator_.get(), &candidate_states_list)); |
Qingsi Wang | 72a43a1 | 2018-02-21 00:03:18 | [diff] [blame] | 6163 | return candidate_states_list; |
| 6164 | } |
| 6165 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6166 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 6167 | const { |
Karl Wiberg | a58e169 | 2019-03-26 12:33:43 | [diff] [blame] | 6168 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6169 | std::map<std::string, std::string> transport_names_by_mid; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6170 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6171 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6172 | if (channel) { |
| 6173 | transport_names_by_mid[channel->content_name()] = |
| 6174 | channel->transport_name(); |
| 6175 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6176 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6177 | if (data_channel_controller_.rtp_data_channel()) { |
| 6178 | transport_names_by_mid[data_channel_controller_.rtp_data_channel() |
| 6179 | ->content_name()] = |
| 6180 | data_channel_controller_.rtp_data_channel()->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6181 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6182 | if (data_channel_controller_.data_channel_transport()) { |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 6183 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6184 | RTC_DCHECK(transport_name); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6185 | transport_names_by_mid[*sctp_mid_s_] = *transport_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6186 | } |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6187 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6188 | } |
| 6189 | |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6190 | std::map<std::string, cricket::TransportStats> |
| 6191 | PeerConnection::GetTransportStatsByNames( |
| 6192 | const std::set<std::string>& transport_names) { |
| 6193 | if (!network_thread()->IsCurrent()) { |
| 6194 | return network_thread() |
| 6195 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 6196 | RTC_FROM_HERE, |
| 6197 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6198 | } |
Karl Wiberg | 2cc368f | 2019-04-02 09:31:56 | [diff] [blame] | 6199 | RTC_DCHECK_RUN_ON(network_thread()); |
Steve Anton | 5dfde18 | 2018-02-06 18:34:40 | [diff] [blame] | 6200 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 6201 | for (const std::string& transport_name : transport_names) { |
| 6202 | cricket::TransportStats transport_stats; |
| 6203 | bool success = |
| 6204 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 6205 | if (success) { |
| 6206 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 6207 | } else { |
| 6208 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 6209 | << transport_name; |
| 6210 | } |
| 6211 | } |
| 6212 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6213 | } |
| 6214 | |
| 6215 | bool PeerConnection::GetLocalCertificate( |
| 6216 | const std::string& transport_name, |
| 6217 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6218 | if (!certificate) { |
| 6219 | return false; |
| 6220 | } |
| 6221 | *certificate = transport_controller_->GetLocalCertificate(transport_name); |
| 6222 | return *certificate != nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6223 | } |
| 6224 | |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6225 | std::unique_ptr<rtc::SSLCertChain> PeerConnection::GetRemoteSSLCertChain( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6226 | const std::string& transport_name) { |
Taylor Brandstetter | c392866 | 2018-02-23 21:04:51 | [diff] [blame] | 6227 | return transport_controller_->GetRemoteSSLCertChain(transport_name); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6228 | } |
| 6229 | |
| 6230 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 6231 | return data_channel_controller_.data_channel_type(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6232 | } |
| 6233 | |
| 6234 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
Karl Wiberg | f73f7d6 | 2019-04-08 13:36:53 | [diff] [blame] | 6235 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6236 | return pending_ice_restarts_.find(content_name) != |
| 6237 | pending_ice_restarts_.end(); |
| 6238 | } |
| 6239 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6240 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 6241 | return transport_controller_->NeedsIceRestart(content_name); |
| 6242 | } |
| 6243 | |
| 6244 | void PeerConnection::OnCertificateReady( |
| 6245 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 6246 | transport_controller_->SetLocalCertificate(certificate); |
| 6247 | } |
| 6248 | |
| 6249 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6250 | SetSessionError(SessionError::kTransport, |
| 6251 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6252 | } |
| 6253 | |
Alex Loiko | 9289eda | 2018-11-23 16:18:59 | [diff] [blame] | 6254 | void PeerConnection::OnTransportControllerConnectionState( |
| 6255 | cricket::IceConnectionState state) { |
| 6256 | switch (state) { |
| 6257 | case cricket::kIceConnectionConnecting: |
| 6258 | // If the current state is Connected or Completed, then there were |
| 6259 | // writable channels but now there are not, so the next state must |
| 6260 | // be Disconnected. |
| 6261 | // kIceConnectionConnecting is currently used as the default, |
| 6262 | // un-connected state by the TransportController, so its only use is |
| 6263 | // detecting disconnections. |
| 6264 | if (ice_connection_state_ == |
| 6265 | PeerConnectionInterface::kIceConnectionConnected || |
| 6266 | ice_connection_state_ == |
| 6267 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 6268 | SetIceConnectionState( |
| 6269 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 6270 | } |
| 6271 | break; |
| 6272 | case cricket::kIceConnectionFailed: |
| 6273 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 6274 | break; |
| 6275 | case cricket::kIceConnectionConnected: |
| 6276 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
| 6277 | "all transports are writable."; |
| 6278 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6279 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6280 | break; |
| 6281 | case cricket::kIceConnectionCompleted: |
| 6282 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
| 6283 | "all transports are complete."; |
| 6284 | if (ice_connection_state_ != |
| 6285 | PeerConnectionInterface::kIceConnectionConnected) { |
| 6286 | // If jumping directly from "checking" to "connected", |
| 6287 | // signal "connected" first. |
| 6288 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 6289 | } |
| 6290 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 6291 | NoteUsageEvent(UsageEvent::ICE_STATE_CONNECTED); |
| 6292 | ReportTransportStats(); |
| 6293 | break; |
| 6294 | default: |
| 6295 | RTC_NOTREACHED(); |
| 6296 | } |
| 6297 | } |
| 6298 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6299 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 6300 | const std::string& transport_name, |
| 6301 | const cricket::Candidates& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6302 | int sdp_mline_index; |
| 6303 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6304 | RTC_LOG(LS_ERROR) |
| 6305 | << "OnTransportControllerCandidatesGathered: content name " |
| 6306 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6307 | return; |
| 6308 | } |
| 6309 | |
| 6310 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 6311 | citer != candidates.end(); ++citer) { |
| 6312 | // Use transport_name as the candidate media id. |
| 6313 | std::unique_ptr<JsepIceCandidate> candidate( |
| 6314 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 6315 | if (local_description()) { |
| 6316 | mutable_local_description()->AddCandidate(candidate.get()); |
| 6317 | } |
| 6318 | OnIceCandidate(std::move(candidate)); |
| 6319 | } |
| 6320 | } |
| 6321 | |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 6322 | void PeerConnection::OnTransportControllerCandidateError( |
| 6323 | const cricket::IceCandidateErrorEvent& event) { |
Eldar Rello | 0095d37 | 2019-12-02 20:22:07 | [diff] [blame] | 6324 | OnIceCandidateError(event.address, event.port, event.url, event.error_code, |
Eldar Rello | da13ea2 | 2019-06-01 09:23:43 | [diff] [blame] | 6325 | event.error_text); |
| 6326 | } |
| 6327 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6328 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 6329 | const std::vector<cricket::Candidate>& candidates) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6330 | // Sanity check. |
| 6331 | for (const cricket::Candidate& candidate : candidates) { |
| 6332 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6333 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6334 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6335 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6336 | return; |
| 6337 | } |
| 6338 | } |
| 6339 | |
| 6340 | if (local_description()) { |
| 6341 | mutable_local_description()->RemoveCandidates(candidates); |
| 6342 | } |
| 6343 | OnIceCandidatesRemoved(candidates); |
| 6344 | } |
| 6345 | |
Alex Drake | 00c7ecf | 2019-08-06 17:54:47 | [diff] [blame] | 6346 | void PeerConnection::OnTransportControllerCandidateChanged( |
| 6347 | const cricket::CandidatePairChangeEvent& event) { |
| 6348 | OnSelectedCandidatePairChanged(event); |
| 6349 | } |
| 6350 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6351 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 6352 | rtc::SSLHandshakeError error) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6353 | RTC_HISTOGRAM_ENUMERATION( |
| 6354 | "WebRTC.PeerConnection.DtlsHandshakeError", static_cast<int>(error), |
| 6355 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6356 | } |
| 6357 | |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6358 | void PeerConnection::EnableSending() { |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 6359 | for (const auto& transceiver : transceivers_) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 6360 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | ed10bd9 | 2017-12-05 18:52:59 | [diff] [blame] | 6361 | if (channel && !channel->enabled()) { |
| 6362 | channel->Enable(true); |
| 6363 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6364 | } |
| 6365 | |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6366 | if (data_channel_controller_.rtp_data_channel() && |
| 6367 | !data_channel_controller_.rtp_data_channel()->enabled()) { |
| 6368 | data_channel_controller_.rtp_data_channel()->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6369 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6370 | } |
| 6371 | |
| 6372 | // Returns the media index for a local ice candidate given the content name. |
| 6373 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 6374 | const std::string& content_name, |
| 6375 | int* sdp_mline_index) { |
| 6376 | if (!local_description() || !sdp_mline_index) { |
| 6377 | return false; |
| 6378 | } |
| 6379 | |
| 6380 | bool content_found = false; |
| 6381 | const ContentInfos& contents = local_description()->description()->contents(); |
| 6382 | for (size_t index = 0; index < contents.size(); ++index) { |
| 6383 | if (contents[index].name == content_name) { |
| 6384 | *sdp_mline_index = static_cast<int>(index); |
| 6385 | content_found = true; |
| 6386 | break; |
| 6387 | } |
| 6388 | } |
| 6389 | return content_found; |
| 6390 | } |
| 6391 | |
| 6392 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 6393 | const SessionDescriptionInterface* remote_desc) { |
| 6394 | if (!remote_desc) { |
| 6395 | return true; |
| 6396 | } |
| 6397 | bool ret = true; |
| 6398 | |
| 6399 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 6400 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 6401 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 6402 | const IceCandidateInterface* candidate = candidates->at(n); |
| 6403 | bool valid = false; |
| 6404 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 6405 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 6406 | RTC_LOG(LS_INFO) |
| 6407 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 6408 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6409 | } |
| 6410 | continue; |
| 6411 | } |
| 6412 | ret = UseCandidate(candidate); |
| 6413 | if (!ret) { |
| 6414 | break; |
| 6415 | } |
| 6416 | } |
| 6417 | } |
| 6418 | return ret; |
| 6419 | } |
| 6420 | |
| 6421 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6422 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 6423 | FindContentInfo(remote_description(), candidate); |
| 6424 | if (!result.ok()) { |
| 6425 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate. " |
| 6426 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6427 | return false; |
| 6428 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6429 | std::vector<cricket::Candidate> candidates; |
| 6430 | candidates.push_back(candidate->candidate()); |
| 6431 | // Invoking BaseSession method to handle remote candidates. |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6432 | RTCError error = transport_controller_->AddRemoteCandidates( |
| 6433 | result.value()->name, candidates); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 | [diff] [blame] | 6434 | if (error.ok()) { |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 6435 | ReportRemoteIceCandidateAdded(candidate->candidate()); |
Henrik Boström | 5d8f8fa | 2018-04-13 15:22:50 | [diff] [blame] | 6436 | // Candidates successfully submitted for checking. |
| 6437 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 6438 | ice_connection_state_ == |
| 6439 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 6440 | // If state is New, then the session has just gotten its first remote ICE |
| 6441 | // candidates, so go to Checking. |
| 6442 | // If state is Disconnected, the session is re-using old candidates or |
| 6443 | // receiving additional ones, so go to Checking. |
| 6444 | // If state is Connected, stay Connected. |
| 6445 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 6446 | // newly added transport, then the state actually _should_ move to |
| 6447 | // checking. Add a way to distinguish that case. |
| 6448 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 6449 | } |
| 6450 | // TODO(bemasc): If state is Completed, go back to Connected. |
Jonas Olsson | 941a07c | 2018-09-13 08:07:07 | [diff] [blame] | 6451 | } else { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6452 | RTC_LOG(LS_WARNING) << error.message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6453 | } |
| 6454 | return true; |
| 6455 | } |
| 6456 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 6457 | RTCErrorOr<const cricket::ContentInfo*> PeerConnection::FindContentInfo( |
| 6458 | const SessionDescriptionInterface* description, |
| 6459 | const IceCandidateInterface* candidate) { |
| 6460 | if (candidate->sdp_mline_index() >= 0) { |
| 6461 | size_t mediacontent_index = |
| 6462 | static_cast<size_t>(candidate->sdp_mline_index()); |
| 6463 | size_t content_size = description->description()->contents().size(); |
| 6464 | if (mediacontent_index < content_size) { |
| 6465 | return &description->description()->contents()[mediacontent_index]; |
| 6466 | } else { |
| 6467 | return RTCError(RTCErrorType::INVALID_RANGE, |
| 6468 | "Media line index (" + |
| 6469 | rtc::ToString(candidate->sdp_mline_index()) + |
| 6470 | ") out of range (number of mlines: " + |
| 6471 | rtc::ToString(content_size) + ")."); |
| 6472 | } |
| 6473 | } else if (!candidate->sdp_mid().empty()) { |
| 6474 | auto& contents = description->description()->contents(); |
| 6475 | auto it = absl::c_find_if( |
| 6476 | contents, [candidate](const cricket::ContentInfo& content_info) { |
| 6477 | return content_info.mid() == candidate->sdp_mid(); |
| 6478 | }); |
| 6479 | if (it == contents.end()) { |
| 6480 | return RTCError( |
| 6481 | RTCErrorType::INVALID_PARAMETER, |
| 6482 | "Mid " + candidate->sdp_mid() + |
| 6483 | " specified but no media section with that mid found."); |
| 6484 | } else { |
| 6485 | return &*it; |
| 6486 | } |
| 6487 | } |
| 6488 | |
| 6489 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 6490 | "Neither sdp_mline_index nor sdp_mid specified."); |
| 6491 | } |
| 6492 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6493 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6494 | // Destroy video channel first since it may have a pointer to the |
| 6495 | // voice channel. |
| 6496 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6497 | if (!video_info || video_info->rejected) { |
| 6498 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6499 | } |
| 6500 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 6501 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 6502 | if (!audio_info || audio_info->rejected) { |
| 6503 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6504 | } |
| 6505 | |
| 6506 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 6507 | if (!data_info || data_info->rejected) { |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 6508 | DestroyDataChannelTransport(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6509 | } |
| 6510 | } |
| 6511 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6512 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 6513 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6514 | const cricket::ContentGroup* bundle_group = nullptr; |
| 6515 | if (configuration_.bundle_policy == |
| 6516 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6517 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6518 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6519 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6520 | "max-bundle configured but session description " |
| 6521 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6522 | } |
| 6523 | } |
Mirko Bonadei | 9f3a44f | 2019-01-30 12:47:42 | [diff] [blame] | 6524 | return bundle_group; |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6528 | // Creating the media channels. Transports should already have been created |
| 6529 | // at this point. |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6530 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6531 | if (voice && !voice->rejected && |
| 6532 | !GetAudioTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6533 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel(voice->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6534 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6535 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6536 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6537 | } |
| 6538 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 6539 | } |
| 6540 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6541 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6542 | if (video && !video->rejected && |
| 6543 | !GetVideoTransceiver()->internal()->channel()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6544 | cricket::VideoChannel* video_channel = CreateVideoChannel(video->name); |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6545 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6546 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6547 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6548 | } |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6549 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6550 | } |
| 6551 | |
Steve Anton | dcc3c02 | 2017-12-23 00:02:54 | [diff] [blame] | 6552 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 6553 | if (data_channel_type() != cricket::DCT_NONE && data && !data->rejected && |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6554 | !data_channel_controller_.rtp_data_channel() && |
| 6555 | !data_channel_controller_.data_channel_transport()) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6556 | if (!CreateDataChannel(data->name)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6557 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 6558 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6559 | } |
| 6560 | } |
| 6561 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6562 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6563 | } |
| 6564 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6565 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6566 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6567 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6568 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6569 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6570 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
Niels Möller | 2a70703 | 2020-06-16 14:39:13 | [diff] [blame] | 6571 | call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(), |
| 6572 | mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_, |
| 6573 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6574 | if (!voice_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6575 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6576 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6577 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6578 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6579 | voice_channel->SignalSentPacket.connect(this, |
| 6580 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6581 | voice_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6582 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6583 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6584 | } |
| 6585 | |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6586 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6587 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6588 | const std::string& mid) { |
Anton Sukhanov | 98a462c | 2018-10-17 20:15:42 | [diff] [blame] | 6589 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
| 6590 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6591 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
Niels Möller | 2a70703 | 2020-06-16 14:39:13 | [diff] [blame] | 6592 | call_ptr_, configuration_.media_config, rtp_transport, signaling_thread(), |
| 6593 | mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_, video_options_, |
Anton Sukhanov | 4f08faa | 2019-05-21 18:12:57 | [diff] [blame] | 6594 | video_bitrate_allocator_factory_.get()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6595 | if (!video_channel) { |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6596 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6597 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6598 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 6599 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6600 | video_channel->SignalSentPacket.connect(this, |
| 6601 | &PeerConnection::OnSentPacket_w); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6602 | video_channel->SetRtpTransport(rtp_transport); |
Steve Anton | 4171afb | 2017-11-20 18:20:22 | [diff] [blame] | 6603 | |
Steve Anton | eda6ccd | 2017-12-04 18:21:55 | [diff] [blame] | 6604 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6605 | } |
| 6606 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 6607 | bool PeerConnection::CreateDataChannel(const std::string& mid) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 6608 | switch (data_channel_type()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6609 | case cricket::DCT_SCTP: |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6610 | if (network_thread()->Invoke<bool>( |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6611 | RTC_FROM_HERE, |
| 6612 | rtc::Bind(&PeerConnection::SetupDataChannelTransport_n, this, |
| 6613 | mid))) { |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6614 | sctp_mid_s_ = mid; |
| 6615 | } else { |
Qingsi Wang | 437077d | 2019-09-10 17:52:26 | [diff] [blame] | 6616 | return false; |
| 6617 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6618 | return true; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6619 | case cricket::DCT_RTP: |
| 6620 | default: |
| 6621 | RtpTransportInternal* rtp_transport = GetRtpTransport(mid); |
Tomas Gunnarsson | 7d3cfbf | 2020-06-15 11:47:42 | [diff] [blame] | 6622 | // TODO(bugs.webrtc.org/9987): set_rtp_data_channel() should be called on |
| 6623 | // the network thread like set_data_channel_transport is. |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6624 | data_channel_controller_.set_rtp_data_channel( |
| 6625 | channel_manager()->CreateRtpDataChannel( |
| 6626 | configuration_.media_config, rtp_transport, signaling_thread(), |
| 6627 | mid, SrtpRequired(), GetCryptoOptions(), &ssrc_generator_)); |
| 6628 | if (!data_channel_controller_.rtp_data_channel()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6629 | return false; |
| 6630 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6631 | data_channel_controller_.rtp_data_channel() |
| 6632 | ->SignalDtlsSrtpSetupFailure.connect( |
| 6633 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 6634 | data_channel_controller_.rtp_data_channel()->SignalSentPacket.connect( |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6635 | this, &PeerConnection::OnSentPacket_w); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6636 | data_channel_controller_.rtp_data_channel()->SetRtpTransport( |
| 6637 | rtp_transport); |
Eldar Rello | d9ebe01 | 2020-03-18 18:41:45 | [diff] [blame] | 6638 | have_pending_rtp_data_channel_ = true; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6639 | return true; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6640 | } |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6641 | return false; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | Call::Stats PeerConnection::GetCallStats() { |
| 6645 | if (!worker_thread()->IsCurrent()) { |
| 6646 | return worker_thread()->Invoke<Call::Stats>( |
| 6647 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 6648 | } |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 6649 | RTC_DCHECK_RUN_ON(worker_thread()); |
Henrik Boström | e88c95e | 2020-07-08 09:18:50 | [diff] [blame] | 6650 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6651 | if (call_) { |
| 6652 | return call_->GetStats(); |
| 6653 | } else { |
| 6654 | return Call::Stats(); |
| 6655 | } |
| 6656 | } |
| 6657 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6658 | bool PeerConnection::SetupDataChannelTransport_n(const std::string& mid) { |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6659 | DataChannelTransportInterface* transport = |
| 6660 | transport_controller_->GetDataChannelTransport(mid); |
| 6661 | if (!transport) { |
Piotr (Peter) Slatala | b1ae10b | 2019-03-01 19:14:05 | [diff] [blame] | 6662 | RTC_LOG(LS_ERROR) |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6663 | << "Data channel transport is not available for data channels, mid=" |
| 6664 | << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6665 | return false; |
| 6666 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6667 | RTC_LOG(LS_INFO) << "Setting up data channel transport for mid=" << mid; |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6668 | |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6669 | data_channel_controller_.set_data_channel_transport(transport); |
| 6670 | data_channel_controller_.SetupDataChannelTransport_n(); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6671 | sctp_mid_n_ = mid; |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 6672 | |
| 6673 | // Note: setting the data sink and checking initial state must be done last, |
| 6674 | // after setting up the data channel. Setting the data sink may trigger |
| 6675 | // callbacks to PeerConnection which require the transport to be completely |
| 6676 | // set up (eg. OnReadyToSend()). |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6677 | transport->SetDataSink(&data_channel_controller_); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6678 | return true; |
| 6679 | } |
| 6680 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6681 | void PeerConnection::TeardownDataChannelTransport_n() { |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6682 | if (!sctp_mid_n_ && !data_channel_controller_.data_channel_transport()) { |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6683 | return; |
| 6684 | } |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6685 | RTC_LOG(LS_INFO) << "Tearing down data channel transport for mid=" |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6686 | << *sctp_mid_n_; |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6687 | |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 6688 | // |sctp_mid_| may still be active through an SCTP transport. If not, unset |
| 6689 | // it. |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 6690 | sctp_mid_n_.reset(); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 6691 | data_channel_controller_.TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 6692 | } |
| 6693 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6694 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 6695 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 6696 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 6697 | if (!bundle_enabled) |
| 6698 | return true; |
| 6699 | |
| 6700 | const cricket::ContentGroup* bundle_group = |
| 6701 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 6702 | RTC_DCHECK(bundle_group != NULL); |
| 6703 | |
| 6704 | const cricket::ContentInfos& contents = desc->contents(); |
| 6705 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 6706 | citer != contents.end(); ++citer) { |
| 6707 | const cricket::ContentInfo* content = (&*citer); |
| 6708 | RTC_DCHECK(content != NULL); |
| 6709 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-21 00:34:00 | [diff] [blame] | 6710 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6711 | if (!HasRtcpMuxEnabled(content)) |
| 6712 | return false; |
| 6713 | } |
| 6714 | } |
| 6715 | // RTCP-MUX is enabled in all the contents. |
| 6716 | return true; |
| 6717 | } |
| 6718 | |
| 6719 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 23:14:30 | [diff] [blame] | 6720 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6721 | } |
| 6722 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6723 | static RTCError ValidateMids(const cricket::SessionDescription& description) { |
| 6724 | std::set<std::string> mids; |
| 6725 | for (const cricket::ContentInfo& content : description.contents()) { |
Steve Anton | ceac015 | 2018-12-19 19:32:20 | [diff] [blame] | 6726 | if (content.name.empty()) { |
| 6727 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6728 | "A media section is missing a MID attribute."); |
| 6729 | } |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6730 | if (!mids.insert(content.name).second) { |
| 6731 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6732 | "Duplicate a=mid value '" + content.name + "'."); |
| 6733 | } |
| 6734 | } |
| 6735 | return RTCError::OK(); |
| 6736 | } |
| 6737 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6738 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6739 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6740 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6741 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6742 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6743 | } |
| 6744 | |
| 6745 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6746 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6747 | } |
| 6748 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6749 | SdpType type = sdesc->GetType(); |
| 6750 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 6751 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6752 | LOG_AND_RETURN_ERROR( |
Harald Alvestrand | 5081c0c | 2018-03-09 14:18:03 | [diff] [blame] | 6753 | RTCErrorType::INVALID_STATE, |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6754 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6755 | } |
| 6756 | |
Steve Anton | 06817cd | 2018-12-18 23:55:30 | [diff] [blame] | 6757 | RTCError error = ValidateMids(*sdesc->description()); |
| 6758 | if (!error.ok()) { |
| 6759 | return error; |
| 6760 | } |
| 6761 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6762 | // Verify crypto settings. |
| 6763 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6764 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 6765 | dtls_enabled_) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6766 | RTCError crypto_error = VerifyCrypto(sdesc->description(), dtls_enabled_); |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6767 | if (!crypto_error.ok()) { |
| 6768 | return crypto_error; |
| 6769 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | // Verify ice-ufrag and ice-pwd. |
| 6773 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6774 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6775 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6776 | } |
| 6777 | |
| 6778 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6779 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6780 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6781 | } |
| 6782 | |
| 6783 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 6784 | // m-lines that do not rtcp-mux enabled. |
| 6785 | |
| 6786 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6787 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6788 | // With an answer we want to compare the new answer session description with |
| 6789 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6790 | const cricket::SessionDescription* offer_desc = |
| 6791 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 6792 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6793 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 6794 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 6795 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6796 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6797 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6798 | } |
| 6799 | } else { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6800 | // The re-offers should respect the order of m= sections in current |
| 6801 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6802 | // With a re-offer, either the current local or current remote descriptions |
| 6803 | // could be the most up to date, so we would like to check against both of |
| 6804 | // them if they exist. It could be the case that one of them has a 0 port |
| 6805 | // for a media section, but the other does not. This is important to check |
| 6806 | // against in the case that we are recycling an m= section. |
| 6807 | const cricket::SessionDescription* current_desc = nullptr; |
| 6808 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 6809 | if (local_description()) { |
| 6810 | current_desc = local_description()->description(); |
| 6811 | if (remote_description()) { |
| 6812 | secondary_current_desc = remote_description()->description(); |
| 6813 | } |
| 6814 | } else if (remote_description()) { |
| 6815 | current_desc = remote_description()->description(); |
| 6816 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6817 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 23:33:48 | [diff] [blame] | 6818 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 6819 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6820 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6821 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6822 | } |
| 6823 | } |
| 6824 | |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 6825 | if (IsUnifiedPlan()) { |
| 6826 | // Ensure that each audio and video media section has at most one |
| 6827 | // "StreamParams". This will return an error if receiving a session |
| 6828 | // description from a "Plan B" endpoint which adds multiple tracks of the |
| 6829 | // same type. With Unified Plan, there can only be at most one track per |
| 6830 | // media section. |
| 6831 | for (const ContentInfo& content : sdesc->description()->contents()) { |
Harald Alvestrand | 1716d39 | 2019-06-03 18:35:45 | [diff] [blame] | 6832 | const MediaContentDescription& desc = *content.media_description(); |
Steve Anton | ba42e99 | 2018-04-09 21:10:01 | [diff] [blame] | 6833 | if ((desc.type() == cricket::MEDIA_TYPE_AUDIO || |
| 6834 | desc.type() == cricket::MEDIA_TYPE_VIDEO) && |
| 6835 | desc.streams().size() > 1u) { |
| 6836 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 6837 | "Media section has more than one track specified " |
| 6838 | "with a=ssrc lines which is not supported with " |
| 6839 | "Unified Plan."); |
| 6840 | } |
| 6841 | } |
| 6842 | } |
| 6843 | |
Steve Anton | 8a00691 | 2017-12-04 23:25:56 | [diff] [blame] | 6844 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6845 | } |
| 6846 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6847 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6848 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6849 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6850 | return (state == PeerConnectionInterface::kStable) || |
| 6851 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 6852 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6853 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6854 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 6855 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 6856 | } |
| 6857 | } |
| 6858 | |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6859 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6860 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6861 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6862 | return (state == PeerConnectionInterface::kStable) || |
| 6863 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-05 00:24:52 | [diff] [blame] | 6864 | } else { |
Steve Anton | 3828c06 | 2017-12-06 18:34:51 | [diff] [blame] | 6865 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6866 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 6867 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 6868 | } |
| 6869 | } |
| 6870 | |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6871 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 6872 | switch (error) { |
| 6873 | case SessionError::kNone: |
| 6874 | return "ERROR_NONE"; |
| 6875 | case SessionError::kContent: |
| 6876 | return "ERROR_CONTENT"; |
| 6877 | case SessionError::kTransport: |
| 6878 | return "ERROR_TRANSPORT"; |
| 6879 | } |
| 6880 | RTC_NOTREACHED(); |
| 6881 | return ""; |
| 6882 | } |
| 6883 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6884 | std::string PeerConnection::GetSessionErrorMsg() { |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 6885 | rtc::StringBuilder desc; |
Steve Anton | f847081 | 2017-12-04 18:46:21 | [diff] [blame] | 6886 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 6887 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Jonas Olsson | 84df1c7 | 2018-09-14 14:59:32 | [diff] [blame] | 6888 | return desc.Release(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 6889 | } |
| 6890 | |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6891 | void PeerConnection::ReportSdpFormatReceived( |
| 6892 | const SessionDescriptionInterface& remote_offer) { |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6893 | int num_audio_mlines = 0; |
| 6894 | int num_video_mlines = 0; |
| 6895 | int num_audio_tracks = 0; |
| 6896 | int num_video_tracks = 0; |
| 6897 | for (const ContentInfo& content : remote_offer.description()->contents()) { |
| 6898 | cricket::MediaType media_type = content.media_description()->type(); |
| 6899 | int num_tracks = std::max( |
| 6900 | 1, static_cast<int>(content.media_description()->streams().size())); |
| 6901 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 6902 | num_audio_mlines += 1; |
| 6903 | num_audio_tracks += num_tracks; |
| 6904 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 6905 | num_video_mlines += 1; |
| 6906 | num_video_tracks += num_tracks; |
| 6907 | } |
| 6908 | } |
| 6909 | SdpFormatReceived format = kSdpFormatReceivedNoTracks; |
| 6910 | if (num_audio_mlines > 1 || num_video_mlines > 1) { |
| 6911 | format = kSdpFormatReceivedComplexUnifiedPlan; |
| 6912 | } else if (num_audio_tracks > 1 || num_video_tracks > 1) { |
| 6913 | format = kSdpFormatReceivedComplexPlanB; |
| 6914 | } else if (num_audio_tracks > 0 || num_video_tracks > 0) { |
| 6915 | format = kSdpFormatReceivedSimple; |
| 6916 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6917 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpFormatReceived", format, |
| 6918 | kSdpFormatReceivedMax); |
Steve Anton | 8e20f17 | 2018-03-06 18:55:04 | [diff] [blame] | 6919 | } |
| 6920 | |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 6921 | void PeerConnection::ReportIceCandidateCollected( |
| 6922 | const cricket::Candidate& candidate) { |
| 6923 | NoteUsageEvent(UsageEvent::CANDIDATE_COLLECTED); |
| 6924 | if (candidate.address().IsPrivateIP()) { |
| 6925 | NoteUsageEvent(UsageEvent::PRIVATE_CANDIDATE_COLLECTED); |
| 6926 | } |
| 6927 | if (candidate.address().IsUnresolvedIP()) { |
| 6928 | NoteUsageEvent(UsageEvent::MDNS_CANDIDATE_COLLECTED); |
| 6929 | } |
| 6930 | if (candidate.address().family() == AF_INET6) { |
| 6931 | NoteUsageEvent(UsageEvent::IPV6_CANDIDATE_COLLECTED); |
| 6932 | } |
| 6933 | } |
| 6934 | |
| 6935 | void PeerConnection::ReportRemoteIceCandidateAdded( |
| 6936 | const cricket::Candidate& candidate) { |
| 6937 | NoteUsageEvent(UsageEvent::REMOTE_CANDIDATE_ADDED); |
| 6938 | if (candidate.address().IsPrivateIP()) { |
| 6939 | NoteUsageEvent(UsageEvent::REMOTE_PRIVATE_CANDIDATE_ADDED); |
| 6940 | } |
| 6941 | if (candidate.address().IsUnresolvedIP()) { |
| 6942 | NoteUsageEvent(UsageEvent::REMOTE_MDNS_CANDIDATE_ADDED); |
| 6943 | } |
| 6944 | if (candidate.address().family() == AF_INET6) { |
| 6945 | NoteUsageEvent(UsageEvent::REMOTE_IPV6_CANDIDATE_ADDED); |
| 6946 | } |
| 6947 | } |
| 6948 | |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 6949 | void PeerConnection::NoteUsageEvent(UsageEvent event) { |
| 6950 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 6951 | usage_event_accumulator_ |= static_cast<int>(event); |
| 6952 | } |
| 6953 | |
| 6954 | void PeerConnection::ReportUsagePattern() const { |
| 6955 | RTC_DLOG(LS_INFO) << "Usage signature is " << usage_event_accumulator_; |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6956 | RTC_HISTOGRAM_ENUMERATION_SPARSE("WebRTC.PeerConnection.UsagePattern", |
| 6957 | usage_event_accumulator_, |
| 6958 | static_cast<int>(UsageEvent::MAX_VALUE)); |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6959 | const int bad_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 6960 | static_cast<int>(UsageEvent::SET_LOCAL_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6961 | static_cast<int>(UsageEvent::CANDIDATE_COLLECTED); |
| 6962 | const int good_bits = |
Qingsi Wang | 1ba5dec | 2019-08-19 18:57:17 | [diff] [blame] | 6963 | static_cast<int>(UsageEvent::SET_REMOTE_DESCRIPTION_SUCCEEDED) | |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6964 | static_cast<int>(UsageEvent::REMOTE_CANDIDATE_ADDED) | |
| 6965 | static_cast<int>(UsageEvent::ICE_STATE_CONNECTED); |
| 6966 | if ((usage_event_accumulator_ & bad_bits) == bad_bits && |
| 6967 | (usage_event_accumulator_ & good_bits) == 0) { |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 6968 | // If called after close(), we can't report, because observer may have |
| 6969 | // been deallocated, and therefore pointer is null. Write to log instead. |
| 6970 | if (observer_) { |
| 6971 | Observer()->OnInterestingUsage(usage_event_accumulator_); |
| 6972 | } else { |
| 6973 | RTC_LOG(LS_INFO) << "Interesting usage signature " |
| 6974 | << usage_event_accumulator_ |
| 6975 | << " observed after observer shutdown"; |
| 6976 | } |
Harald Alvestrand | c0e9725 | 2018-07-26 08:39:55 | [diff] [blame] | 6977 | } |
Harald Alvestrand | 8ebba74 | 2018-05-31 12:00:34 | [diff] [blame] | 6978 | } |
| 6979 | |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6980 | void PeerConnection::ReportNegotiatedSdpSemantics( |
| 6981 | const SessionDescriptionInterface& answer) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6982 | SdpSemanticNegotiated semantics_negotiated; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6983 | switch (answer.description()->msid_signaling()) { |
| 6984 | case 0: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6985 | semantics_negotiated = kSdpSemanticNegotiatedNone; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6986 | break; |
| 6987 | case cricket::kMsidSignalingMediaSection: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6988 | semantics_negotiated = kSdpSemanticNegotiatedUnifiedPlan; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6989 | break; |
| 6990 | case cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6991 | semantics_negotiated = kSdpSemanticNegotiatedPlanB; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6992 | break; |
| 6993 | case cricket::kMsidSignalingMediaSection | |
| 6994 | cricket::kMsidSignalingSsrcAttribute: |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 6995 | semantics_negotiated = kSdpSemanticNegotiatedMixed; |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 6996 | break; |
| 6997 | default: |
| 6998 | RTC_NOTREACHED(); |
| 6999 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7000 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.SdpSemanticNegotiated", |
| 7001 | semantics_negotiated, kSdpSemanticNegotiatedMax); |
Steve Anton | 0ffaaa2 | 2018-02-23 18:31:30 | [diff] [blame] | 7002 | } |
| 7003 | |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7004 | // We need to check the local/remote description for the Transport instead of |
| 7005 | // the session, because a new Transport added during renegotiation may have |
| 7006 | // them unset while the session has them set from the previous negotiation. |
| 7007 | // Not doing so may trigger the auto generation of transport description and |
| 7008 | // mess up DTLS identity information, ICE credential, etc. |
| 7009 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 7010 | const IceCandidateInterface* candidate, |
| 7011 | const SessionDescriptionInterface* remote_desc, |
| 7012 | bool* valid) { |
| 7013 | *valid = true; |
| 7014 | |
| 7015 | const SessionDescriptionInterface* current_remote_desc = |
| 7016 | remote_desc ? remote_desc : remote_description(); |
| 7017 | |
| 7018 | if (!current_remote_desc) { |
| 7019 | return false; |
| 7020 | } |
| 7021 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 7022 | RTCErrorOr<const cricket::ContentInfo*> result = |
| 7023 | FindContentInfo(current_remote_desc, candidate); |
| 7024 | if (!result.ok()) { |
| 7025 | RTC_LOG(LS_ERROR) << "ReadyToUseRemoteCandidate: Invalid candidate. " |
| 7026 | << result.error().message(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7027 | |
| 7028 | *valid = false; |
| 7029 | return false; |
| 7030 | } |
| 7031 | |
Guido Urdaneta | 4163317 | 2019-05-23 18:12:29 | [diff] [blame] | 7032 | std::string transport_name = GetTransportName(result.value()->name); |
| 7033 | return !transport_name.empty(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | bool PeerConnection::SrtpRequired() const { |
Niels Möller | 2a70703 | 2020-06-16 14:39:13 | [diff] [blame] | 7037 | return (dtls_enabled_ || |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 7038 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7039 | } |
| 7040 | |
| 7041 | void PeerConnection::OnTransportControllerGatheringState( |
| 7042 | cricket::IceGatheringState state) { |
| 7043 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 7044 | if (state == cricket::kIceGatheringGathering) { |
| 7045 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 7046 | } else if (state == cricket::kIceGatheringComplete) { |
| 7047 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
Harald Alvestrand | bedb605 | 2020-08-20 12:50:10 | [diff] [blame] | 7048 | } else if (state == cricket::kIceGatheringNew) { |
| 7049 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringNew); |
| 7050 | } else { |
| 7051 | RTC_LOG(LS_ERROR) << "Unknown state received: " << state; |
| 7052 | RTC_NOTREACHED(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7057 | std::map<std::string, std::set<cricket::MediaType>> |
| 7058 | media_types_by_transport_name; |
Mirko Bonadei | 739baf0 | 2019-01-27 16:29:42 | [diff] [blame] | 7059 | for (const auto& transceiver : transceivers_) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7060 | if (transceiver->internal()->channel()) { |
| 7061 | const std::string& transport_name = |
| 7062 | transceiver->internal()->channel()->transport_name(); |
| 7063 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 19:43:08 | [diff] [blame] | 7064 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7065 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7066 | } |
| 7067 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7068 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 7069 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7070 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7071 | |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 7072 | absl::optional<std::string> transport_name = sctp_transport_name(); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7073 | if (transport_name) { |
| 7074 | media_types_by_transport_name[*transport_name].insert( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7075 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7076 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7077 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7078 | for (const auto& entry : media_types_by_transport_name) { |
| 7079 | const std::string& transport_name = entry.first; |
| 7080 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7081 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7082 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7083 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7084 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7085 | } |
| 7086 | } |
| 7087 | } |
| 7088 | // Walk through the ConnectionInfos to gather best connection usage |
| 7089 | // for IPv4 and IPv6. |
| 7090 | void PeerConnection::ReportBestConnectionState( |
| 7091 | const cricket::TransportStats& stats) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7092 | for (const cricket::TransportChannelStats& channel_stats : |
| 7093 | stats.channel_stats) { |
| 7094 | for (const cricket::ConnectionInfo& connection_info : |
Jonas Oreland | 149dc72 | 2019-08-28 06:10:27 | [diff] [blame] | 7095 | channel_stats.ice_transport_stats.connection_infos) { |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7096 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7097 | continue; |
| 7098 | } |
| 7099 | |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7100 | const cricket::Candidate& local = connection_info.local_candidate; |
| 7101 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7102 | |
| 7103 | // Increment the counter for IceCandidatePairType. |
| 7104 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 7105 | (local.type() == RELAY_PORT_TYPE && |
| 7106 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7107 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_TCP", |
| 7108 | GetIceCandidatePairCounter(local, remote), |
| 7109 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7110 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7111 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.CandidatePairType_UDP", |
| 7112 | GetIceCandidatePairCounter(local, remote), |
| 7113 | kIceCandidatePairMax); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7114 | } else { |
| 7115 | RTC_CHECK(0); |
| 7116 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7117 | |
| 7118 | // Increment the counter for IP type. |
| 7119 | if (local.address().family() == AF_INET) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7120 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7121 | kBestConnections_IPv4, |
| 7122 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7123 | } else if (local.address().family() == AF_INET6) { |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7124 | RTC_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", |
| 7125 | kBestConnections_IPv6, |
| 7126 | kPeerConnectionAddressFamilyCounter_Max); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7127 | } else { |
Qingsi Wang | 1dac6d8 | 2018-12-12 23:28:47 | [diff] [blame] | 7128 | RTC_CHECK(!local.address().hostname().empty() && |
| 7129 | local.address().IsUnresolvedIP()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7130 | } |
| 7131 | |
| 7132 | return; |
| 7133 | } |
| 7134 | } |
| 7135 | } |
| 7136 | |
| 7137 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7138 | const cricket::TransportStats& stats, |
| 7139 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7140 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 7141 | return; |
| 7142 | } |
| 7143 | |
| 7144 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 7145 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 7146 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 7147 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7148 | return; |
| 7149 | } |
| 7150 | |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7151 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 7152 | for (cricket::MediaType media_type : media_types) { |
| 7153 | switch (media_type) { |
| 7154 | case cricket::MEDIA_TYPE_AUDIO: |
| 7155 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7156 | "WebRTC.PeerConnection.SrtpCryptoSuite.Audio", srtp_crypto_suite, |
| 7157 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7158 | break; |
| 7159 | case cricket::MEDIA_TYPE_VIDEO: |
| 7160 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7161 | "WebRTC.PeerConnection.SrtpCryptoSuite.Video", srtp_crypto_suite, |
| 7162 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7163 | break; |
| 7164 | case cricket::MEDIA_TYPE_DATA: |
| 7165 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7166 | "WebRTC.PeerConnection.SrtpCryptoSuite.Data", srtp_crypto_suite, |
| 7167 | rtc::SRTP_CRYPTO_SUITE_MAX_VALUE); |
| 7168 | break; |
| 7169 | default: |
| 7170 | RTC_NOTREACHED(); |
| 7171 | continue; |
| 7172 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7173 | } |
Qingsi Wang | 7fc821d | 2018-07-12 19:54:53 | [diff] [blame] | 7174 | } |
| 7175 | |
| 7176 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 7177 | for (cricket::MediaType media_type : media_types) { |
| 7178 | switch (media_type) { |
| 7179 | case cricket::MEDIA_TYPE_AUDIO: |
| 7180 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7181 | "WebRTC.PeerConnection.SslCipherSuite.Audio", ssl_cipher_suite, |
| 7182 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7183 | break; |
| 7184 | case cricket::MEDIA_TYPE_VIDEO: |
| 7185 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7186 | "WebRTC.PeerConnection.SslCipherSuite.Video", ssl_cipher_suite, |
| 7187 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7188 | break; |
| 7189 | case cricket::MEDIA_TYPE_DATA: |
| 7190 | RTC_HISTOGRAM_ENUMERATION_SPARSE( |
| 7191 | "WebRTC.PeerConnection.SslCipherSuite.Data", ssl_cipher_suite, |
| 7192 | rtc::SSL_CIPHER_SUITE_MAX_VALUE); |
| 7193 | break; |
| 7194 | default: |
| 7195 | RTC_NOTREACHED(); |
| 7196 | continue; |
| 7197 | } |
Steve Anton | c7b964c | 2018-02-01 22:39:45 | [diff] [blame] | 7198 | } |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7199 | } |
| 7200 | } |
| 7201 | |
| 7202 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7203 | RTC_DCHECK_RUN_ON(worker_thread()); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7204 | RTC_DCHECK(call_); |
| 7205 | call_->OnSentPacket(sent_packet); |
| 7206 | } |
| 7207 | |
| 7208 | const std::string PeerConnection::GetTransportName( |
| 7209 | const std::string& content_name) { |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7210 | cricket::ChannelInterface* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7211 | if (channel) { |
| 7212 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7213 | } |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 7214 | if (data_channel_controller_.data_channel_transport()) { |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 7215 | RTC_DCHECK(sctp_mid_s_); |
| 7216 | if (content_name == *sctp_mid_s_) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7217 | return *sctp_transport_name(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7218 | } |
| 7219 | } |
| 7220 | // Return an empty string if failed to retrieve the transport name. |
| 7221 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7222 | } |
| 7223 | |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7224 | void PeerConnection::DestroyTransceiverChannel( |
| 7225 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 7226 | transceiver) { |
| 7227 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7228 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7229 | cricket::ChannelInterface* channel = transceiver->internal()->channel(); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7230 | if (channel) { |
| 7231 | transceiver->internal()->SetChannel(nullptr); |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7232 | DestroyChannelInterface(channel); |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7233 | } |
| 7234 | } |
| 7235 | |
Harald Alvestrand | 408cb4b | 2019-11-16 11:09:08 | [diff] [blame] | 7236 | void PeerConnection::DestroyDataChannelTransport() { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 7237 | if (data_channel_controller_.rtp_data_channel()) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 7238 | data_channel_controller_.OnTransportChannelClosed(); |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 7239 | DestroyChannelInterface(data_channel_controller_.rtp_data_channel()); |
| 7240 | data_channel_controller_.set_rtp_data_channel(nullptr); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7241 | } |
| 7242 | |
| 7243 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 7244 | // grab a reference to this PeerConnection. If this is called from the |
| 7245 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 7246 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 7247 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 7248 | |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 7249 | if (sctp_mid_s_) { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 7250 | data_channel_controller_.OnTransportChannelClosed(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7251 | network_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
| 7252 | RTC_DCHECK_RUN_ON(network_thread()); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7253 | TeardownDataChannelTransport_n(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7254 | }); |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 7255 | sctp_mid_s_.reset(); |
Bjorn Mellem | 175aa2e | 2018-11-08 19:23:22 | [diff] [blame] | 7256 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7257 | } |
| 7258 | |
Amit Hilbuch | dd9390c | 2018-11-14 00:26:05 | [diff] [blame] | 7259 | void PeerConnection::DestroyChannelInterface( |
| 7260 | cricket::ChannelInterface* channel) { |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7261 | RTC_DCHECK(channel); |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7262 | switch (channel->media_type()) { |
| 7263 | case cricket::MEDIA_TYPE_AUDIO: |
| 7264 | channel_manager()->DestroyVoiceChannel( |
| 7265 | static_cast<cricket::VoiceChannel*>(channel)); |
| 7266 | break; |
| 7267 | case cricket::MEDIA_TYPE_VIDEO: |
| 7268 | channel_manager()->DestroyVideoChannel( |
| 7269 | static_cast<cricket::VideoChannel*>(channel)); |
| 7270 | break; |
| 7271 | case cricket::MEDIA_TYPE_DATA: |
| 7272 | channel_manager()->DestroyRtpDataChannel( |
| 7273 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 7274 | break; |
| 7275 | default: |
| 7276 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 7277 | break; |
| 7278 | } |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7279 | } |
Steve Anton | 6fec880 | 2017-12-04 18:37:29 | [diff] [blame] | 7280 | |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7281 | bool PeerConnection::OnTransportChanged( |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7282 | const std::string& mid, |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7283 | RtpTransportInternal* rtp_transport, |
Harald Alvestrand | c85328f | 2019-02-28 06:51:00 | [diff] [blame] | 7284 | rtc::scoped_refptr<DtlsTransport> dtls_transport, |
Bjorn A Mellem | bc3eebc | 2019-09-23 21:53:54 | [diff] [blame] | 7285 | DataChannelTransportInterface* data_channel_transport) { |
Karl Wiberg | ac02589 | 2019-03-26 12:08:37 | [diff] [blame] | 7286 | RTC_DCHECK_RUN_ON(network_thread()); |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7287 | bool ret = true; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7288 | auto base_channel = GetChannel(mid); |
| 7289 | if (base_channel) { |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7290 | ret = base_channel->SetRtpTransport(rtp_transport); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 7291 | } |
Harald Alvestrand | 7a829a8 | 2020-02-12 06:38:21 | [diff] [blame] | 7292 | if (mid == sctp_mid_n_) { |
Harald Alvestrand | 00cf34c | 2019-12-02 08:56:02 | [diff] [blame] | 7293 | data_channel_controller_.OnTransportChanged(data_channel_transport); |
Bjorn A Mellem | b689af4 | 2019-08-21 17:44:59 | [diff] [blame] | 7294 | } |
Taylor Brandstetter | cbaa254 | 2018-04-16 23:42:14 | [diff] [blame] | 7295 | return ret; |
Steve Anton | 75737c0 | 2017-11-06 18:37:17 | [diff] [blame] | 7296 | } |
| 7297 | |
Guido Urdaneta | 1ff16c8 | 2019-05-20 17:31:53 | [diff] [blame] | 7298 | void PeerConnection::OnSetStreams() { |
| 7299 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7300 | if (IsUnifiedPlan()) |
| 7301 | UpdateNegotiationNeeded(); |
| 7302 | } |
| 7303 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7304 | PeerConnectionObserver* PeerConnection::Observer() const { |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 7305 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7306 | RTC_DCHECK(observer_); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7307 | return observer_; |
| 7308 | } |
| 7309 | |
Benjamin Wright | 8c27cca | 2018-10-25 17:16:44 | [diff] [blame] | 7310 | CryptoOptions PeerConnection::GetCryptoOptions() { |
| 7311 | // TODO(bugs.webrtc.org/9891) - Remove PeerConnectionFactory::CryptoOptions |
| 7312 | // after it has been removed. |
| 7313 | return configuration_.crypto_options.has_value() |
| 7314 | ? *configuration_.crypto_options |
| 7315 | : factory_->options().crypto_options; |
| 7316 | } |
| 7317 | |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7318 | void PeerConnection::ClearStatsCache() { |
Karl Wiberg | 6cab5c8 | 2019-03-26 08:57:01 | [diff] [blame] | 7319 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Harald Alvestrand | 8906187 | 2018-01-02 13:08:34 | [diff] [blame] | 7320 | if (stats_collector_) { |
| 7321 | stats_collector_->ClearCachedStatsReport(); |
| 7322 | } |
| 7323 | } |
| 7324 | |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7325 | void PeerConnection::RequestUsagePatternReportForTesting() { |
Steve Anton | ad18276 | 2018-09-05 20:22:40 | [diff] [blame] | 7326 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_REPORT_USAGE_PATTERN, |
| 7327 | nullptr); |
Harald Alvestrand | 7a1c7f7 | 2018-08-01 08:50:16 | [diff] [blame] | 7328 | } |
| 7329 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7330 | void PeerConnection::UpdateNegotiationNeeded() { |
| 7331 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7332 | if (!IsUnifiedPlan()) { |
| 7333 | Observer()->OnRenegotiationNeeded(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7334 | GenerateNegotiationNeededEvent(); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7335 | return; |
| 7336 | } |
| 7337 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7338 | // In the spec, a task is queued here to run the following steps - this is |
| 7339 | // meant to ensure we do not fire onnegotiationneeded prematurely if multiple |
| 7340 | // changes are being made at once. In order to support Chromium's |
| 7341 | // implementation where the JavaScript representation of the PeerConnection |
| 7342 | // lives on a separate thread though, the queuing of a task is instead |
| 7343 | // performed by the PeerConnectionObserver posting from the signaling thread |
| 7344 | // to the JavaScript main thread that negotiation is needed. And because the |
| 7345 | // Operations Chain lives on the WebRTC signaling thread, |
| 7346 | // ShouldFireNegotiationNeededEvent() must be called before firing the event |
| 7347 | // to ensure the Operations Chain is still empty and the event has not been |
| 7348 | // invalidated. |
| 7349 | |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7350 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7351 | if (IsClosed()) |
| 7352 | return; |
| 7353 | |
| 7354 | // If connection's signaling state is not "stable", abort these steps. |
| 7355 | if (signaling_state() != kStable) |
| 7356 | return; |
| 7357 | |
| 7358 | // NOTE |
| 7359 | // The negotiation-needed flag will be updated once the state transitions to |
| 7360 | // "stable", as part of the steps for setting an RTCSessionDescription. |
| 7361 | |
| 7362 | // If the result of checking if negotiation is needed is false, clear the |
| 7363 | // negotiation-needed flag by setting connection's [[NegotiationNeeded]] slot |
| 7364 | // to false, and abort these steps. |
| 7365 | bool is_negotiation_needed = CheckIfNegotiationIsNeeded(); |
| 7366 | if (!is_negotiation_needed) { |
| 7367 | is_negotiation_needed_ = false; |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7368 | // Invalidate any negotiation needed event that may previosuly have been |
| 7369 | // generated. |
| 7370 | ++negotiation_needed_event_id_; |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7371 | return; |
| 7372 | } |
| 7373 | |
| 7374 | // If connection's [[NegotiationNeeded]] slot is already true, abort these |
| 7375 | // steps. |
| 7376 | if (is_negotiation_needed_) |
| 7377 | return; |
| 7378 | |
| 7379 | // Set connection's [[NegotiationNeeded]] slot to true. |
| 7380 | is_negotiation_needed_ = true; |
| 7381 | |
| 7382 | // Queue a task that runs the following steps: |
| 7383 | // If connection's [[IsClosed]] slot is true, abort these steps. |
| 7384 | // If connection's [[NegotiationNeeded]] slot is false, abort these steps. |
| 7385 | // Fire an event named negotiationneeded at connection. |
| 7386 | Observer()->OnRenegotiationNeeded(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7387 | // Fire the spec-compliant version; when ShouldFireNegotiationNeededEvent() is |
| 7388 | // used in the task queued by the observer, this event will only fire when the |
| 7389 | // chain is empty. |
| 7390 | GenerateNegotiationNeededEvent(); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7391 | } |
| 7392 | |
| 7393 | bool PeerConnection::CheckIfNegotiationIsNeeded() { |
| 7394 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7395 | // 1. If any implementation-specific negotiation is required, as described at |
| 7396 | // the start of this section, return true. |
| 7397 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7398 | // 2. If connection.[[LocalIceCredentialsToReplace]] is not empty, return |
| 7399 | // true. |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7400 | if (local_ice_credentials_to_replace_->HasIceCredentials()) { |
| 7401 | return true; |
| 7402 | } |
| 7403 | |
| 7404 | // 3. Let description be connection.[[CurrentLocalDescription]]. |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7405 | const SessionDescriptionInterface* description = current_local_description(); |
| 7406 | if (!description) |
| 7407 | return true; |
| 7408 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7409 | // 4. If connection has created any RTCDataChannels, and no m= section in |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7410 | // description has been negotiated yet for data, return true. |
Harald Alvestrand | 05e4d08 | 2019-12-03 13:04:21 | [diff] [blame] | 7411 | if (data_channel_controller_.HasSctpDataChannels()) { |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7412 | if (!cricket::GetFirstDataContent(description->description()->contents())) |
| 7413 | return true; |
| 7414 | } |
| 7415 | |
Henrik Boström | 79b6980 | 2019-07-18 09:16:56 | [diff] [blame] | 7416 | // 5. For each transceiver in connection's set of transceivers, perform the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7417 | // following checks: |
| 7418 | for (const auto& transceiver : transceivers_) { |
| 7419 | const ContentInfo* current_local_msection = |
| 7420 | FindTransceiverMSection(transceiver.get(), description); |
| 7421 | |
| 7422 | const ContentInfo* current_remote_msection = FindTransceiverMSection( |
| 7423 | transceiver.get(), current_remote_description()); |
| 7424 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7425 | // 5.4 If transceiver is stopped and is associated with an m= section, |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7426 | // but the associated m= section is not yet rejected in |
| 7427 | // connection.[[CurrentLocalDescription]] or |
| 7428 | // connection.[[CurrentRemoteDescription]], return true. |
| 7429 | if (transceiver->stopped()) { |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7430 | RTC_DCHECK(transceiver->stopping()); |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7431 | if (current_local_msection && !current_local_msection->rejected && |
| 7432 | ((current_remote_msection && !current_remote_msection->rejected) || |
| 7433 | !current_remote_msection)) { |
| 7434 | return true; |
| 7435 | } |
| 7436 | continue; |
| 7437 | } |
| 7438 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7439 | // 5.1 If transceiver.[[Stopping]] is true and transceiver.[[Stopped]] is |
| 7440 | // false, return true. |
| 7441 | if (transceiver->stopping() && !transceiver->stopped()) |
| 7442 | return true; |
| 7443 | |
| 7444 | // 5.2 If transceiver isn't stopped and isn't yet associated with an m= |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7445 | // section in description, return true. |
| 7446 | if (!current_local_msection) |
| 7447 | return true; |
| 7448 | |
| 7449 | const MediaContentDescription* current_local_media_description = |
| 7450 | current_local_msection->media_description(); |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7451 | // 5.3 If transceiver isn't stopped and is associated with an m= section |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7452 | // in description then perform the following checks: |
| 7453 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7454 | // 5.3.1 If transceiver.[[Direction]] is "sendrecv" or "sendonly", and the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7455 | // associated m= section in description either doesn't contain a single |
| 7456 | // "a=msid" line, or the number of MSIDs from the "a=msid" lines in this |
| 7457 | // m= section, or the MSID values themselves, differ from what is in |
| 7458 | // transceiver.sender.[[AssociatedMediaStreamIds]], return true. |
| 7459 | if (RtpTransceiverDirectionHasSend(transceiver->direction())) { |
| 7460 | if (current_local_media_description->streams().size() == 0) |
| 7461 | return true; |
| 7462 | |
| 7463 | std::vector<std::string> msection_msids; |
| 7464 | for (const auto& stream : current_local_media_description->streams()) { |
| 7465 | for (const std::string& msid : stream.stream_ids()) |
| 7466 | msection_msids.push_back(msid); |
| 7467 | } |
| 7468 | |
| 7469 | std::vector<std::string> transceiver_msids = |
| 7470 | transceiver->sender()->stream_ids(); |
| 7471 | if (msection_msids.size() != transceiver_msids.size()) |
| 7472 | return true; |
| 7473 | |
| 7474 | absl::c_sort(transceiver_msids); |
| 7475 | absl::c_sort(msection_msids); |
| 7476 | if (transceiver_msids != msection_msids) |
| 7477 | return true; |
| 7478 | } |
| 7479 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7480 | // 5.3.2 If description is of type "offer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7481 | // associated m= section in neither connection.[[CurrentLocalDescription]] |
| 7482 | // nor connection.[[CurrentRemoteDescription]] matches |
| 7483 | // transceiver.[[Direction]], return true. |
| 7484 | if (description->GetType() == SdpType::kOffer) { |
| 7485 | if (!current_remote_description()) |
| 7486 | return true; |
| 7487 | |
| 7488 | if (!current_remote_msection) |
| 7489 | return true; |
| 7490 | |
| 7491 | RtpTransceiverDirection current_local_direction = |
| 7492 | current_local_media_description->direction(); |
| 7493 | RtpTransceiverDirection current_remote_direction = |
| 7494 | current_remote_msection->media_description()->direction(); |
| 7495 | if (transceiver->direction() != current_local_direction && |
| 7496 | transceiver->direction() != |
| 7497 | RtpTransceiverDirectionReversed(current_remote_direction)) { |
| 7498 | return true; |
| 7499 | } |
| 7500 | } |
| 7501 | |
Harald Alvestrand | 6060df5 | 2020-08-11 07:54:02 | [diff] [blame] | 7502 | // 5.3.3 If description is of type "answer", and the direction of the |
Guido Urdaneta | 70c2db1 | 2019-04-16 10:24:14 | [diff] [blame] | 7503 | // associated m= section in the description does not match |
| 7504 | // transceiver.[[Direction]] intersected with the offered direction (as |
| 7505 | // described in [JSEP] (section 5.3.1.)), return true. |
| 7506 | if (description->GetType() == SdpType::kAnswer) { |
| 7507 | if (!remote_description()) |
| 7508 | return true; |
| 7509 | |
| 7510 | const ContentInfo* offered_remote_msection = |
| 7511 | FindTransceiverMSection(transceiver.get(), remote_description()); |
| 7512 | |
| 7513 | RtpTransceiverDirection offered_direction = |
| 7514 | offered_remote_msection |
| 7515 | ? offered_remote_msection->media_description()->direction() |
| 7516 | : RtpTransceiverDirection::kInactive; |
| 7517 | |
| 7518 | if (current_local_media_description->direction() != |
| 7519 | (RtpTransceiverDirectionIntersection( |
| 7520 | transceiver->direction(), |
| 7521 | RtpTransceiverDirectionReversed(offered_direction)))) { |
| 7522 | return true; |
| 7523 | } |
| 7524 | } |
| 7525 | } |
| 7526 | |
| 7527 | // If all the preceding checks were performed and true was not returned, |
| 7528 | // nothing remains to be negotiated; return false. |
| 7529 | return false; |
| 7530 | } |
| 7531 | |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7532 | void PeerConnection::OnOperationsChainEmpty() { |
| 7533 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7534 | if (IsClosed() || !update_negotiation_needed_on_empty_chain_) |
| 7535 | return; |
| 7536 | update_negotiation_needed_on_empty_chain_ = false; |
| 7537 | // Firing when chain is empty is only supported in Unified Plan to avoid Plan |
| 7538 | // B regressions. (In Plan B, onnegotiationneeded is already broken anyway, so |
| 7539 | // firing it even more might just be confusing.) |
| 7540 | if (IsUnifiedPlan()) { |
| 7541 | UpdateNegotiationNeeded(); |
| 7542 | } |
| 7543 | } |
| 7544 | |
| 7545 | bool PeerConnection::ShouldFireNegotiationNeededEvent(uint32_t event_id) { |
| 7546 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7547 | // Plan B? Always fire to conform with useless legacy behavior. |
| 7548 | if (!IsUnifiedPlan()) { |
| 7549 | return true; |
| 7550 | } |
| 7551 | // The event ID has been invalidated. Either negotiation is no longer needed |
| 7552 | // or a newer negotiation needed event has been generated. |
| 7553 | if (event_id != negotiation_needed_event_id_) { |
| 7554 | return false; |
| 7555 | } |
| 7556 | // The chain is no longer empty, update negotiation needed when it becomes |
| 7557 | // empty. This should generate a newer negotiation needed event, making this |
| 7558 | // one obsolete. |
| 7559 | if (!operations_chain_->IsEmpty()) { |
| 7560 | // Since we just suppressed an event that would have been fired, if |
| 7561 | // negotiation is still needed by the time the chain becomes empty again, we |
| 7562 | // must make sure to generate another event if negotiation is needed then. |
| 7563 | // This happens when |is_negotiation_needed_| goes from false to true, so we |
| 7564 | // set it to false until UpdateNegotiationNeeded() is called. |
| 7565 | is_negotiation_needed_ = false; |
| 7566 | update_negotiation_needed_on_empty_chain_ = true; |
| 7567 | return false; |
| 7568 | } |
| 7569 | // We must not fire if the signaling state is no longer "stable". If |
| 7570 | // negotiation is still needed when we return to "stable", a new negotiation |
| 7571 | // needed event will be generated, so this one can safely be suppressed. |
| 7572 | if (signaling_state_ != PeerConnectionInterface::kStable) { |
| 7573 | return false; |
| 7574 | } |
| 7575 | // All checks have passed - please fire "negotiationneeded" now! |
| 7576 | return true; |
| 7577 | } |
| 7578 | |
| 7579 | void PeerConnection::GenerateNegotiationNeededEvent() { |
| 7580 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7581 | ++negotiation_needed_event_id_; |
| 7582 | Observer()->OnNegotiationNeededEvent(negotiation_needed_event_id_); |
| 7583 | } |
| 7584 | |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7585 | RTCError PeerConnection::Rollback(SdpType sdp_type) { |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7586 | auto state = signaling_state(); |
| 7587 | if (state != PeerConnectionInterface::kHaveLocalOffer && |
| 7588 | state != PeerConnectionInterface::kHaveRemoteOffer) { |
| 7589 | return RTCError(RTCErrorType::INVALID_STATE, |
| 7590 | "Called in wrong signalingState: " + |
| 7591 | GetSignalingStateString(signaling_state())); |
| 7592 | } |
| 7593 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 7594 | RTC_DCHECK(IsUnifiedPlan()); |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 7595 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_added_streams; |
| 7596 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> all_removed_streams; |
| 7597 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> removed_receivers; |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7598 | |
| 7599 | for (auto&& transceivers_stable_state_pair : |
| 7600 | transceiver_stable_states_by_transceivers_) { |
| 7601 | auto transceiver = transceivers_stable_state_pair.first; |
| 7602 | auto state = transceivers_stable_state_pair.second; |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 7603 | |
| 7604 | if (state.remote_stream_ids()) { |
| 7605 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
| 7606 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> removed_streams; |
| 7607 | SetAssociatedRemoteStreams(transceiver->internal()->receiver_internal(), |
| 7608 | state.remote_stream_ids().value(), |
| 7609 | &added_streams, &removed_streams); |
| 7610 | all_added_streams.insert(all_added_streams.end(), added_streams.begin(), |
| 7611 | added_streams.end()); |
| 7612 | all_removed_streams.insert(all_removed_streams.end(), |
| 7613 | removed_streams.begin(), |
| 7614 | removed_streams.end()); |
| 7615 | if (!state.has_m_section() && !state.newly_created()) { |
| 7616 | continue; |
| 7617 | } |
| 7618 | } |
| 7619 | |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7620 | RTC_DCHECK(transceiver->internal()->mid().has_value()); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7621 | DestroyTransceiverChannel(transceiver); |
| 7622 | |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7623 | if (signaling_state() == PeerConnectionInterface::kHaveRemoteOffer && |
| 7624 | transceiver->receiver()) { |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 7625 | removed_receivers.push_back(transceiver->receiver()); |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7626 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7627 | if (state.newly_created()) { |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 7628 | if (transceiver->internal()->reused_for_addtrack()) { |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7629 | transceiver->internal()->set_created_by_addtrack(true); |
| 7630 | } else { |
| 7631 | int remaining_transceiver_count = 0; |
| 7632 | for (auto&& t : transceivers_) { |
| 7633 | if (t != transceiver) { |
| 7634 | transceivers_[remaining_transceiver_count++] = t; |
| 7635 | } |
| 7636 | } |
| 7637 | transceivers_.resize(remaining_transceiver_count); |
| 7638 | } |
| 7639 | } |
| 7640 | transceiver->internal()->sender_internal()->set_transport(nullptr); |
| 7641 | transceiver->internal()->receiver_internal()->set_transport(nullptr); |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7642 | transceiver->internal()->set_mid(state.mid()); |
| 7643 | transceiver->internal()->set_mline_index(state.mline_index()); |
| 7644 | } |
Eldar Rello | d85ea75 | 2020-02-19 18:41:07 | [diff] [blame] | 7645 | transport_controller_->RollbackTransports(); |
Eldar Rello | d9ebe01 | 2020-03-18 18:41:45 | [diff] [blame] | 7646 | if (have_pending_rtp_data_channel_) { |
| 7647 | DestroyDataChannelTransport(); |
| 7648 | have_pending_rtp_data_channel_ = false; |
| 7649 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7650 | transceiver_stable_states_by_transceivers_.clear(); |
| 7651 | pending_local_description_.reset(); |
| 7652 | pending_remote_description_.reset(); |
| 7653 | ChangeSignalingState(PeerConnectionInterface::kStable); |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7654 | |
Eldar Rello | 353a718 | 2019-11-25 16:49:44 | [diff] [blame] | 7655 | // Once all processing has finished, fire off callbacks. |
| 7656 | for (const auto& receiver : removed_receivers) { |
| 7657 | Observer()->OnRemoveTrack(receiver); |
| 7658 | } |
| 7659 | for (const auto& stream : all_added_streams) { |
| 7660 | Observer()->OnAddStream(stream); |
| 7661 | } |
| 7662 | for (const auto& stream : all_removed_streams) { |
| 7663 | Observer()->OnRemoveStream(stream); |
| 7664 | } |
| 7665 | |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7666 | // The assumption is that in case of implicit rollback UpdateNegotiationNeeded |
| 7667 | // gets called in SetRemoteDescription. |
| 7668 | if (sdp_type == SdpType::kRollback) { |
| 7669 | UpdateNegotiationNeeded(); |
| 7670 | if (is_negotiation_needed_) { |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7671 | // Legacy version. |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7672 | Observer()->OnRenegotiationNeeded(); |
Henrik Boström | e574a31 | 2020-08-25 08:20:11 | [diff] [blame] | 7673 | // Spec-compliant version; the event may get invalidated before firing. |
| 7674 | GenerateNegotiationNeededEvent(); |
Eldar Rello | ead0ec9 | 2019-10-21 20:01:31 | [diff] [blame] | 7675 | } |
| 7676 | } |
Eldar Rello | 5ab79e6 | 2019-10-09 15:29:44 | [diff] [blame] | 7677 | return RTCError::OK(); |
| 7678 | } |
| 7679 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 | [diff] [blame] | 7680 | } // namespace webrtc |