henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2012 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [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 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/peerconnection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 13 | #include <algorithm> |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 14 | #include <queue> |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 15 | #include <set> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | #include <utility> |
| 17 | #include <vector> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/jsepicecandidate.h" |
| 20 | #include "api/jsepsessiondescription.h" |
| 21 | #include "api/mediaconstraintsinterface.h" |
| 22 | #include "api/mediastreamproxy.h" |
| 23 | #include "api/mediastreamtrackproxy.h" |
| 24 | #include "call/call.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 25 | #include "logging/rtc_event_log/icelogger.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 26 | #include "logging/rtc_event_log/output/rtc_event_log_output_file.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "logging/rtc_event_log/rtc_event_log.h" |
| 28 | #include "media/sctp/sctptransport.h" |
| 29 | #include "pc/audiotrack.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 30 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "pc/channelmanager.h" |
| 32 | #include "pc/dtmfsender.h" |
| 33 | #include "pc/mediastream.h" |
| 34 | #include "pc/mediastreamobserver.h" |
| 35 | #include "pc/remoteaudiosource.h" |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 36 | #include "pc/rtpmediautils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 37 | #include "pc/rtpreceiver.h" |
| 38 | #include "pc/rtpsender.h" |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 39 | #include "pc/sctputils.h" |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 40 | #include "pc/sdputils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "pc/streamcollection.h" |
| 42 | #include "pc/videocapturertracksource.h" |
| 43 | #include "pc/videotrack.h" |
| 44 | #include "rtc_base/bind.h" |
| 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 47 | #include "rtc_base/numerics/safe_conversions.h" |
Elad Alon | 83ccca1 | 2017-10-04 13:18:26 +0200 | [diff] [blame] | 48 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 49 | #include "rtc_base/stringencode.h" |
| 50 | #include "rtc_base/stringutils.h" |
| 51 | #include "rtc_base/trace_event.h" |
| 52 | #include "system_wrappers/include/clock.h" |
| 53 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 55 | using cricket::ContentInfo; |
| 56 | using cricket::ContentInfos; |
| 57 | using cricket::MediaContentDescription; |
| 58 | using cricket::SessionDescription; |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 59 | using cricket::MediaProtocolType; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 60 | using cricket::TransportInfo; |
| 61 | |
| 62 | using cricket::LOCAL_PORT_TYPE; |
| 63 | using cricket::STUN_PORT_TYPE; |
| 64 | using cricket::RELAY_PORT_TYPE; |
| 65 | using cricket::PRFLX_PORT_TYPE; |
| 66 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 67 | namespace webrtc { |
| 68 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 69 | // Error messages |
| 70 | const char kBundleWithoutRtcpMux[] = |
| 71 | "rtcp-mux must be enabled when BUNDLE " |
| 72 | "is enabled."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 73 | const char kInvalidCandidates[] = "Description contains invalid candidates."; |
| 74 | const char kInvalidSdp[] = "Invalid session description."; |
| 75 | const char kMlineMismatchInAnswer[] = |
| 76 | "The order of m-lines in answer doesn't match order in offer. Rejecting " |
| 77 | "answer."; |
| 78 | const char kMlineMismatchInSubsequentOffer[] = |
| 79 | "The order of m-lines in subsequent offer doesn't match order from " |
| 80 | "previous offer/answer."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 81 | const char kSdpWithoutDtlsFingerprint[] = |
| 82 | "Called with SDP without DTLS fingerprint."; |
| 83 | const char kSdpWithoutSdesCrypto[] = "Called with SDP without SDES crypto."; |
| 84 | const char kSdpWithoutIceUfragPwd[] = |
| 85 | "Called with SDP without ice-ufrag and ice-pwd."; |
| 86 | const char kSessionError[] = "Session error code: "; |
| 87 | const char kSessionErrorDesc[] = "Session error description: "; |
| 88 | const char kDtlsSrtpSetupFailureRtp[] = |
| 89 | "Couldn't set up DTLS-SRTP on RTP channel."; |
| 90 | const char kDtlsSrtpSetupFailureRtcp[] = |
| 91 | "Couldn't set up DTLS-SRTP on RTCP channel."; |
| 92 | const char kEnableBundleFailed[] = "Failed to enable BUNDLE."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 94 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 96 | static const char kDefaultStreamLabel[] = "default"; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 97 | static const char kDefaultAudioSenderId[] = "defaulta0"; |
| 98 | static const char kDefaultVideoSenderId[] = "defaultv0"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 99 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 100 | // The length of RTCP CNAMEs. |
| 101 | static const int kRtcpCnameLength = 16; |
| 102 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | enum { |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 104 | MSG_SET_SESSIONDESCRIPTION_SUCCESS = 0, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | MSG_SET_SESSIONDESCRIPTION_FAILED, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 106 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 107 | MSG_GETSTATS, |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 108 | MSG_FREE_DATACHANNELS, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 111 | struct SetSessionDescriptionMsg : public rtc::MessageData { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 112 | explicit SetSessionDescriptionMsg( |
| 113 | webrtc::SetSessionDescriptionObserver* observer) |
| 114 | : observer(observer) { |
| 115 | } |
| 116 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 117 | rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | std::string error; |
| 119 | }; |
| 120 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 121 | struct CreateSessionDescriptionMsg : public rtc::MessageData { |
| 122 | explicit CreateSessionDescriptionMsg( |
| 123 | webrtc::CreateSessionDescriptionObserver* observer) |
| 124 | : observer(observer) {} |
| 125 | |
| 126 | rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer; |
| 127 | std::string error; |
| 128 | }; |
| 129 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 130 | struct GetStatsMsg : public rtc::MessageData { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 131 | GetStatsMsg(webrtc::StatsObserver* observer, |
| 132 | webrtc::MediaStreamTrackInterface* track) |
| 133 | : observer(observer), track(track) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 135 | rtc::scoped_refptr<webrtc::StatsObserver> observer; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 136 | rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 139 | // Check if we can send |new_stream| on a PeerConnection. |
| 140 | bool CanAddLocalMediaStream(webrtc::StreamCollectionInterface* current_streams, |
| 141 | webrtc::MediaStreamInterface* new_stream) { |
| 142 | if (!new_stream || !current_streams) { |
| 143 | return false; |
| 144 | } |
| 145 | if (current_streams->find(new_stream->label()) != nullptr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 146 | RTC_LOG(LS_ERROR) << "MediaStream with label " << new_stream->label() |
| 147 | << " is already added."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 153 | // If the direction is "recvonly" or "inactive", treat the description |
| 154 | // as containing no streams. |
| 155 | // See: https://code.google.com/p/webrtc/issues/detail?id=5054 |
| 156 | std::vector<cricket::StreamParams> GetActiveStreams( |
| 157 | const cricket::MediaContentDescription* desc) { |
Steve Anton | 4e70a72 | 2017-11-28 14:57:10 -0800 | [diff] [blame] | 158 | return RtpTransceiverDirectionHasSend(desc->direction()) |
deadbeef | 5e97fb5 | 2015-10-15 12:49:08 -0700 | [diff] [blame] | 159 | ? desc->streams() |
| 160 | : std::vector<cricket::StreamParams>(); |
| 161 | } |
| 162 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 163 | bool IsValidOfferToReceiveMedia(int value) { |
| 164 | typedef PeerConnectionInterface::RTCOfferAnswerOptions Options; |
| 165 | return (value >= Options::kUndefined) && |
| 166 | (value <= Options::kMaxOfferToReceiveMedia); |
| 167 | } |
| 168 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 169 | // Add options to |[audio/video]_media_description_options| from |senders|. |
| 170 | void AddRtpSenderOptions( |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 171 | const std::vector<rtc::scoped_refptr< |
| 172 | RtpSenderProxyWithInternal<RtpSenderInternal>>>& senders, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 173 | cricket::MediaDescriptionOptions* audio_media_description_options, |
| 174 | cricket::MediaDescriptionOptions* video_media_description_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 175 | for (const auto& sender : senders) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 176 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 177 | if (audio_media_description_options) { |
| 178 | audio_media_description_options->AddAudioSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 179 | sender->id(), sender->internal()->stream_ids()); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 180 | } |
| 181 | } else { |
| 182 | RTC_DCHECK(sender->media_type() == cricket::MEDIA_TYPE_VIDEO); |
| 183 | if (video_media_description_options) { |
| 184 | video_media_description_options->AddVideoSender( |
Steve Anton | 8ffb9c3 | 2017-08-31 15:45:38 -0700 | [diff] [blame] | 185 | sender->id(), sender->internal()->stream_ids(), 1); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 188 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 189 | } |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 190 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 191 | // Add options to |session_options| from |rtp_data_channels|. |
| 192 | void AddRtpDataChannelOptions( |
| 193 | const std::map<std::string, rtc::scoped_refptr<DataChannel>>& |
| 194 | rtp_data_channels, |
| 195 | cricket::MediaDescriptionOptions* data_media_description_options) { |
| 196 | if (!data_media_description_options) { |
| 197 | return; |
| 198 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 199 | // Check for data channels. |
| 200 | for (const auto& kv : rtp_data_channels) { |
| 201 | const DataChannel* channel = kv.second; |
| 202 | if (channel->state() == DataChannel::kConnecting || |
| 203 | channel->state() == DataChannel::kOpen) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 204 | // Legacy RTP data channels are signaled with the track/stream ID set to |
| 205 | // the data channel's label. |
| 206 | data_media_description_options->AddRtpDataChannel(channel->label(), |
| 207 | channel->label()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 212 | uint32_t ConvertIceTransportTypeToCandidateFilter( |
| 213 | PeerConnectionInterface::IceTransportsType type) { |
| 214 | switch (type) { |
| 215 | case PeerConnectionInterface::kNone: |
| 216 | return cricket::CF_NONE; |
| 217 | case PeerConnectionInterface::kRelay: |
| 218 | return cricket::CF_RELAY; |
| 219 | case PeerConnectionInterface::kNoHost: |
| 220 | return (cricket::CF_ALL & ~cricket::CF_HOST); |
| 221 | case PeerConnectionInterface::kAll: |
| 222 | return cricket::CF_ALL; |
| 223 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 224 | RTC_NOTREACHED(); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 225 | } |
| 226 | return cricket::CF_NONE; |
| 227 | } |
| 228 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 229 | // Helper to set an error and return from a method. |
| 230 | bool SafeSetError(webrtc::RTCErrorType type, webrtc::RTCError* error) { |
| 231 | if (error) { |
| 232 | error->set_type(type); |
| 233 | } |
| 234 | return type == webrtc::RTCErrorType::NONE; |
| 235 | } |
| 236 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 237 | bool SafeSetError(webrtc::RTCError error, webrtc::RTCError* error_out) { |
| 238 | if (error_out) { |
| 239 | *error_out = std::move(error); |
| 240 | } |
| 241 | return error.ok(); |
| 242 | } |
| 243 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 244 | std::string GetSignalingStateString( |
| 245 | PeerConnectionInterface::SignalingState state) { |
| 246 | switch (state) { |
| 247 | case PeerConnectionInterface::kStable: |
| 248 | return "kStable"; |
| 249 | case PeerConnectionInterface::kHaveLocalOffer: |
| 250 | return "kHaveLocalOffer"; |
| 251 | case PeerConnectionInterface::kHaveLocalPrAnswer: |
| 252 | return "kHavePrAnswer"; |
| 253 | case PeerConnectionInterface::kHaveRemoteOffer: |
| 254 | return "kHaveRemoteOffer"; |
| 255 | case PeerConnectionInterface::kHaveRemotePrAnswer: |
| 256 | return "kHaveRemotePrAnswer"; |
| 257 | case PeerConnectionInterface::kClosed: |
| 258 | return "kClosed"; |
| 259 | } |
| 260 | RTC_NOTREACHED(); |
| 261 | return ""; |
| 262 | } |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 263 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 264 | IceCandidatePairType GetIceCandidatePairCounter( |
| 265 | const cricket::Candidate& local, |
| 266 | const cricket::Candidate& remote) { |
| 267 | const auto& l = local.type(); |
| 268 | const auto& r = remote.type(); |
| 269 | const auto& host = LOCAL_PORT_TYPE; |
| 270 | const auto& srflx = STUN_PORT_TYPE; |
| 271 | const auto& relay = RELAY_PORT_TYPE; |
| 272 | const auto& prflx = PRFLX_PORT_TYPE; |
| 273 | if (l == host && r == host) { |
| 274 | bool local_private = IPIsPrivate(local.address().ipaddr()); |
| 275 | bool remote_private = IPIsPrivate(remote.address().ipaddr()); |
| 276 | if (local_private) { |
| 277 | if (remote_private) { |
| 278 | return kIceCandidatePairHostPrivateHostPrivate; |
| 279 | } else { |
| 280 | return kIceCandidatePairHostPrivateHostPublic; |
| 281 | } |
| 282 | } else { |
| 283 | if (remote_private) { |
| 284 | return kIceCandidatePairHostPublicHostPrivate; |
| 285 | } else { |
| 286 | return kIceCandidatePairHostPublicHostPublic; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | if (l == host && r == srflx) |
| 291 | return kIceCandidatePairHostSrflx; |
| 292 | if (l == host && r == relay) |
| 293 | return kIceCandidatePairHostRelay; |
| 294 | if (l == host && r == prflx) |
| 295 | return kIceCandidatePairHostPrflx; |
| 296 | if (l == srflx && r == host) |
| 297 | return kIceCandidatePairSrflxHost; |
| 298 | if (l == srflx && r == srflx) |
| 299 | return kIceCandidatePairSrflxSrflx; |
| 300 | if (l == srflx && r == relay) |
| 301 | return kIceCandidatePairSrflxRelay; |
| 302 | if (l == srflx && r == prflx) |
| 303 | return kIceCandidatePairSrflxPrflx; |
| 304 | if (l == relay && r == host) |
| 305 | return kIceCandidatePairRelayHost; |
| 306 | if (l == relay && r == srflx) |
| 307 | return kIceCandidatePairRelaySrflx; |
| 308 | if (l == relay && r == relay) |
| 309 | return kIceCandidatePairRelayRelay; |
| 310 | if (l == relay && r == prflx) |
| 311 | return kIceCandidatePairRelayPrflx; |
| 312 | if (l == prflx && r == host) |
| 313 | return kIceCandidatePairPrflxHost; |
| 314 | if (l == prflx && r == srflx) |
| 315 | return kIceCandidatePairPrflxSrflx; |
| 316 | if (l == prflx && r == relay) |
| 317 | return kIceCandidatePairPrflxRelay; |
| 318 | return kIceCandidatePairMax; |
| 319 | } |
| 320 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 321 | // Logic to decide if an m= section can be recycled. This means that the new |
| 322 | // m= section is not rejected, but the old local or remote m= section is |
| 323 | // rejected. |old_content_one| and |old_content_two| refer to the m= section |
| 324 | // of the old remote and old local descriptions in no particular order. |
| 325 | // We need to check both the old local and remote because either |
| 326 | // could be the most current from the latest negotation. |
| 327 | bool IsMediaSectionBeingRecycled(SdpType type, |
| 328 | const ContentInfo& content, |
| 329 | const ContentInfo* old_content_one, |
| 330 | const ContentInfo* old_content_two) { |
| 331 | return type == SdpType::kOffer && !content.rejected && |
| 332 | ((old_content_one && old_content_one->rejected) || |
| 333 | (old_content_two && old_content_two->rejected)); |
| 334 | } |
| 335 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 336 | // Verify that the order of media sections in |new_desc| matches |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 337 | // |current_desc|. The number of m= sections in |new_desc| should be no |
| 338 | // less than |current_desc|. In the case of checking an answer's |
| 339 | // |new_desc|, the |current_desc| is the last offer that was set as the |
| 340 | // local or remote. In the case of checking an offer's |new_desc| we |
| 341 | // check against the local and remote descriptions stored from the last |
| 342 | // negotiation, because either of these could be the most up to date for |
| 343 | // possible rejected m sections. These are the |current_desc| and |
| 344 | // |secondary_current_desc|. |
| 345 | bool MediaSectionsInSameOrder(const SessionDescription& current_desc, |
| 346 | const SessionDescription* secondary_current_desc, |
| 347 | const SessionDescription& new_desc, |
| 348 | const SdpType type) { |
| 349 | if (current_desc.contents().size() > new_desc.contents().size()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 350 | return false; |
| 351 | } |
| 352 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 353 | for (size_t i = 0; i < current_desc.contents().size(); ++i) { |
| 354 | const cricket::ContentInfo* secondary_content_info = nullptr; |
| 355 | if (secondary_current_desc && |
| 356 | i < secondary_current_desc->contents().size()) { |
| 357 | secondary_content_info = &secondary_current_desc->contents()[i]; |
| 358 | } |
| 359 | if (IsMediaSectionBeingRecycled(type, new_desc.contents()[i], |
| 360 | ¤t_desc.contents()[i], |
| 361 | secondary_content_info)) { |
| 362 | // For new offer descriptions, if the media section can be recycled, it's |
| 363 | // valid for the MID and media type to change. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 364 | continue; |
| 365 | } |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 366 | if (new_desc.contents()[i].name != current_desc.contents()[i].name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | const MediaContentDescription* new_desc_mdesc = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 370 | new_desc.contents()[i].media_description(); |
| 371 | const MediaContentDescription* current_desc_mdesc = |
| 372 | current_desc.contents()[i].media_description(); |
| 373 | if (new_desc_mdesc->type() != current_desc_mdesc->type()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 380 | bool MediaSectionsHaveSameCount(const SessionDescription& desc1, |
| 381 | const SessionDescription& desc2) { |
| 382 | return desc1.contents().size() == desc2.contents().size(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // Checks that each non-rejected content has SDES crypto keys or a DTLS |
| 386 | // fingerprint, unless it's in a BUNDLE group, in which case only the |
| 387 | // BUNDLE-tag section (first media section/description in the BUNDLE group) |
| 388 | // needs a ufrag and pwd. Mismatches, such as replying with a DTLS fingerprint |
| 389 | // to SDES keys, will be caught in JsepTransport negotiation, and backstopped |
| 390 | // by Channel's |srtp_required| check. |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 391 | RTCError VerifyCrypto(const SessionDescription* desc, |
| 392 | bool dtls_enabled, |
| 393 | rtc::scoped_refptr<webrtc::UMAObserver> uma_observer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 394 | const cricket::ContentGroup* bundle = |
| 395 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 396 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 397 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 398 | continue; |
| 399 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 400 | const std::string& mid = content_info.name; |
| 401 | if (bundle && bundle->HasContentName(mid) && |
| 402 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 403 | // This isn't the first media section in the BUNDLE group, so it's not |
| 404 | // required to have crypto attributes, since only the crypto attributes |
| 405 | // from the first section actually get used. |
| 406 | continue; |
| 407 | } |
| 408 | |
| 409 | // If the content isn't rejected or bundled into another m= section, crypto |
| 410 | // must be present. |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 411 | const MediaContentDescription* media = content_info.media_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 412 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 413 | if (!media || !tinfo) { |
| 414 | // Something is not right. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 415 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 416 | } |
| 417 | if (dtls_enabled) { |
| 418 | if (!tinfo->description.identity_fingerprint) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 419 | RTC_LOG(LS_WARNING) |
| 420 | << "Session description must have DTLS fingerprint if " |
| 421 | "DTLS enabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 422 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 423 | kSdpWithoutDtlsFingerprint); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 424 | } |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 425 | if (uma_observer) { |
| 426 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 427 | webrtc::kEnumCounterKeyProtocolDtls, |
| 428 | webrtc::kEnumCounterKeyProtocolMax); |
| 429 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 430 | } else { |
| 431 | if (media->cryptos().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 432 | RTC_LOG(LS_WARNING) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 433 | << "Session description must have SDES when DTLS disabled."; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 434 | return RTCError(RTCErrorType::INVALID_PARAMETER, kSdpWithoutSdesCrypto); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 435 | } |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 436 | if (uma_observer) { |
| 437 | uma_observer->IncrementEnumCounter(webrtc::kEnumCounterKeyProtocol, |
| 438 | webrtc::kEnumCounterKeyProtocolSdes, |
| 439 | webrtc::kEnumCounterKeyProtocolMax); |
| 440 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 441 | } |
| 442 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 443 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // Checks that each non-rejected content has ice-ufrag and ice-pwd set, unless |
| 447 | // it's in a BUNDLE group, in which case only the BUNDLE-tag section (first |
| 448 | // media section/description in the BUNDLE group) needs a ufrag and pwd. |
| 449 | bool VerifyIceUfragPwdPresent(const SessionDescription* desc) { |
| 450 | const cricket::ContentGroup* bundle = |
| 451 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 452 | for (const cricket::ContentInfo& content_info : desc->contents()) { |
| 453 | if (content_info.rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 454 | continue; |
| 455 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 456 | const std::string& mid = content_info.name; |
| 457 | if (bundle && bundle->HasContentName(mid) && |
| 458 | mid != *(bundle->FirstContentName())) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 459 | // This isn't the first media section in the BUNDLE group, so it's not |
| 460 | // required to have ufrag/password, since only the ufrag/password from |
| 461 | // the first section actually get used. |
| 462 | continue; |
| 463 | } |
| 464 | |
| 465 | // If the content isn't rejected or bundled into another m= section, |
| 466 | // ice-ufrag and ice-pwd must be present. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 467 | const TransportInfo* tinfo = desc->GetTransportInfoByName(mid); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 468 | if (!tinfo) { |
| 469 | // Something is not right. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 470 | RTC_LOG(LS_ERROR) << kInvalidSdp; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 471 | return false; |
| 472 | } |
| 473 | if (tinfo->description.ice_ufrag.empty() || |
| 474 | tinfo->description.ice_pwd.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 475 | RTC_LOG(LS_ERROR) << "Session description must have ice ufrag and pwd."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | return true; |
| 480 | } |
| 481 | |
| 482 | bool GetTrackIdBySsrc(const SessionDescription* session_description, |
| 483 | uint32_t ssrc, |
| 484 | std::string* track_id) { |
| 485 | RTC_DCHECK(track_id != NULL); |
| 486 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 487 | const cricket::AudioContentDescription* audio_desc = |
| 488 | cricket::GetFirstAudioContentDescription(session_description); |
| 489 | if (audio_desc) { |
| 490 | const auto* found = cricket::GetStreamBySsrc(audio_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 491 | if (found) { |
| 492 | *track_id = found->id; |
| 493 | return true; |
| 494 | } |
| 495 | } |
| 496 | |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 497 | const cricket::VideoContentDescription* video_desc = |
| 498 | cricket::GetFirstVideoContentDescription(session_description); |
| 499 | if (video_desc) { |
| 500 | const auto* found = cricket::GetStreamBySsrc(video_desc->streams(), ssrc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 501 | if (found) { |
| 502 | *track_id = found->id; |
| 503 | return true; |
| 504 | } |
| 505 | } |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | // Get the SCTP port out of a SessionDescription. |
| 510 | // Return -1 if not found. |
| 511 | int GetSctpPort(const SessionDescription* session_description) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 512 | const cricket::DataContentDescription* data_desc = |
| 513 | GetFirstDataContentDescription(session_description); |
| 514 | RTC_DCHECK(data_desc); |
| 515 | if (!data_desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 516 | return -1; |
| 517 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 518 | std::string value; |
| 519 | cricket::DataCodec match_pattern(cricket::kGoogleSctpDataCodecPlType, |
| 520 | cricket::kGoogleSctpDataCodecName); |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 521 | for (const cricket::DataCodec& codec : data_desc->codecs()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 522 | if (!codec.Matches(match_pattern)) { |
| 523 | continue; |
| 524 | } |
| 525 | if (codec.GetParam(cricket::kCodecParamPort, &value)) { |
| 526 | return rtc::FromString<int>(value); |
| 527 | } |
| 528 | } |
| 529 | return -1; |
| 530 | } |
| 531 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 532 | // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd). |
| 533 | bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc, |
| 534 | const SessionDescriptionInterface* new_desc, |
| 535 | const std::string& content_name) { |
| 536 | if (!old_desc) { |
| 537 | return false; |
| 538 | } |
| 539 | const SessionDescription* new_sd = new_desc->description(); |
| 540 | const SessionDescription* old_sd = old_desc->description(); |
| 541 | const ContentInfo* cinfo = new_sd->GetContentByName(content_name); |
| 542 | if (!cinfo || cinfo->rejected) { |
| 543 | return false; |
| 544 | } |
| 545 | // If the content isn't rejected, check if ufrag and password has changed. |
| 546 | const cricket::TransportDescription* new_transport_desc = |
| 547 | new_sd->GetTransportDescriptionByName(content_name); |
| 548 | const cricket::TransportDescription* old_transport_desc = |
| 549 | old_sd->GetTransportDescriptionByName(content_name); |
| 550 | if (!new_transport_desc || !old_transport_desc) { |
| 551 | // No transport description exists. This is not an ICE restart. |
| 552 | return false; |
| 553 | } |
| 554 | if (cricket::IceCredentialsChanged( |
| 555 | old_transport_desc->ice_ufrag, old_transport_desc->ice_pwd, |
| 556 | new_transport_desc->ice_ufrag, new_transport_desc->ice_pwd)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 557 | RTC_LOG(LS_INFO) << "Remote peer requests ICE restart for " << content_name |
| 558 | << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 559 | return true; |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 564 | // Generates a string error message for SetLocalDescription/SetRemoteDescription |
| 565 | // from an RTCError. |
| 566 | std::string GetSetDescriptionErrorMessage(cricket::ContentSource source, |
| 567 | SdpType type, |
| 568 | const RTCError& error) { |
| 569 | std::ostringstream oss; |
| 570 | oss << "Failed to set " << (source == cricket::CS_LOCAL ? "local" : "remote") |
| 571 | << " " << SdpTypeToString(type) << " sdp: " << error.message(); |
| 572 | return oss.str(); |
| 573 | } |
| 574 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 575 | } // namespace |
| 576 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 577 | // Upon completion, posts a task to execute the callback of the |
| 578 | // SetSessionDescriptionObserver asynchronously on the same thread. At this |
| 579 | // point, the state of the peer connection might no longer reflect the effects |
| 580 | // of the SetRemoteDescription operation, as the peer connection could have been |
| 581 | // modified during the post. |
| 582 | // TODO(hbos): Remove this class once we remove the version of |
| 583 | // PeerConnectionInterface::SetRemoteDescription() that takes a |
| 584 | // SetSessionDescriptionObserver as an argument. |
| 585 | class PeerConnection::SetRemoteDescriptionObserverAdapter |
| 586 | : public rtc::RefCountedObject<SetRemoteDescriptionObserverInterface> { |
| 587 | public: |
| 588 | SetRemoteDescriptionObserverAdapter( |
| 589 | rtc::scoped_refptr<PeerConnection> pc, |
| 590 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper) |
| 591 | : pc_(std::move(pc)), wrapper_(std::move(wrapper)) {} |
| 592 | |
| 593 | // SetRemoteDescriptionObserverInterface implementation. |
| 594 | void OnSetRemoteDescriptionComplete(RTCError error) override { |
| 595 | if (error.ok()) |
| 596 | pc_->PostSetSessionDescriptionSuccess(wrapper_); |
| 597 | else |
| 598 | pc_->PostSetSessionDescriptionFailure(wrapper_, error.message()); |
| 599 | } |
| 600 | |
| 601 | private: |
| 602 | rtc::scoped_refptr<PeerConnection> pc_; |
| 603 | rtc::scoped_refptr<SetSessionDescriptionObserver> wrapper_; |
| 604 | }; |
| 605 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 606 | bool PeerConnectionInterface::RTCConfiguration::operator==( |
| 607 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 608 | // This static_assert prevents us from accidentally breaking operator==. |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 609 | // Note: Order matters! Fields must be ordered the same as RTCConfiguration. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 610 | struct stuff_being_tested_for_equality { |
Magnus Jedvert | 3beb207 | 2017-07-14 14:23:56 +0000 | [diff] [blame] | 611 | IceServers servers; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 612 | IceTransportsType type; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 613 | BundlePolicy bundle_policy; |
| 614 | RtcpMuxPolicy rtcp_mux_policy; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 615 | std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates; |
| 616 | int ice_candidate_pool_size; |
| 617 | bool disable_ipv6; |
| 618 | bool disable_ipv6_on_wifi; |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 619 | int max_ipv6_networks; |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 620 | bool disable_link_local_networks; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 621 | bool enable_rtp_data_channel; |
| 622 | rtc::Optional<int> screencast_min_bitrate; |
| 623 | rtc::Optional<bool> combined_audio_video_bwe; |
| 624 | rtc::Optional<bool> enable_dtls_srtp; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 625 | TcpCandidatePolicy tcp_candidate_policy; |
| 626 | CandidateNetworkPolicy candidate_network_policy; |
| 627 | int audio_jitter_buffer_max_packets; |
| 628 | bool audio_jitter_buffer_fast_accelerate; |
| 629 | int ice_connection_receiving_timeout; |
| 630 | int ice_backup_candidate_pair_ping_interval; |
| 631 | ContinualGatheringPolicy continual_gathering_policy; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 632 | bool prioritize_most_likely_ice_candidate_pairs; |
| 633 | struct cricket::MediaConfig media_config; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 634 | bool prune_turn_ports; |
| 635 | bool presume_writable_when_fully_relayed; |
| 636 | bool enable_ice_renomination; |
| 637 | bool redetermine_role_on_ice_restart; |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 638 | rtc::Optional<int> ice_check_min_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 639 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 640 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 641 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 642 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 643 | }; |
| 644 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 645 | "Did you add something to RTCConfiguration and forget to " |
| 646 | "update operator==?"); |
| 647 | return type == o.type && servers == o.servers && |
| 648 | bundle_policy == o.bundle_policy && |
| 649 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 650 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 651 | candidate_network_policy == o.candidate_network_policy && |
| 652 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 653 | audio_jitter_buffer_fast_accelerate == |
| 654 | o.audio_jitter_buffer_fast_accelerate && |
| 655 | ice_connection_receiving_timeout == |
| 656 | o.ice_connection_receiving_timeout && |
| 657 | ice_backup_candidate_pair_ping_interval == |
| 658 | o.ice_backup_candidate_pair_ping_interval && |
| 659 | continual_gathering_policy == o.continual_gathering_policy && |
| 660 | certificates == o.certificates && |
| 661 | prioritize_most_likely_ice_candidate_pairs == |
| 662 | o.prioritize_most_likely_ice_candidate_pairs && |
| 663 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 664 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 665 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 666 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 667 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 668 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 669 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 670 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 671 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 672 | prune_turn_ports == o.prune_turn_ports && |
| 673 | presume_writable_when_fully_relayed == |
| 674 | o.presume_writable_when_fully_relayed && |
| 675 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 676 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 677 | ice_check_min_interval == o.ice_check_min_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 678 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 679 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 680 | sdp_semantics == o.sdp_semantics && |
| 681 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 685 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 686 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 687 | } |
| 688 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 689 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 690 | std::string GenerateRtcpCname() { |
| 691 | std::string cname; |
| 692 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 693 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 694 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 695 | } |
| 696 | return cname; |
| 697 | } |
| 698 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 699 | bool ValidateOfferAnswerOptions( |
| 700 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 701 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 702 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 703 | } |
| 704 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 705 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 706 | // m= sections (in other words, nothing that involves a map/array). |
| 707 | void ExtractSharedMediaSessionOptions( |
| 708 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 709 | cricket::MediaSessionOptions* session_options) { |
| 710 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 711 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 712 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 713 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 714 | bool ConvertConstraintsToOfferAnswerOptions( |
| 715 | const MediaConstraintsInterface* constraints, |
| 716 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 717 | if (!constraints) { |
| 718 | return true; |
| 719 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 720 | |
| 721 | bool value = false; |
| 722 | size_t mandatory_constraints_satisfied = 0; |
| 723 | |
| 724 | if (FindConstraint(constraints, |
| 725 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 726 | &mandatory_constraints_satisfied)) { |
| 727 | offer_answer_options->offer_to_receive_audio = |
| 728 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 729 | kOfferToReceiveMediaTrue |
| 730 | : 0; |
| 731 | } |
| 732 | |
| 733 | if (FindConstraint(constraints, |
| 734 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 735 | &mandatory_constraints_satisfied)) { |
| 736 | offer_answer_options->offer_to_receive_video = |
| 737 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 738 | kOfferToReceiveMediaTrue |
| 739 | : 0; |
| 740 | } |
| 741 | if (FindConstraint(constraints, |
| 742 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 743 | &mandatory_constraints_satisfied)) { |
| 744 | offer_answer_options->voice_activity_detection = value; |
| 745 | } |
| 746 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 747 | &mandatory_constraints_satisfied)) { |
| 748 | offer_answer_options->use_rtp_mux = value; |
| 749 | } |
| 750 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 751 | &value, &mandatory_constraints_satisfied)) { |
| 752 | offer_answer_options->ice_restart = value; |
| 753 | } |
| 754 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 755 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 756 | } |
| 757 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 758 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 759 | std::unique_ptr<RtcEventLog> event_log, |
| 760 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 761 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 762 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 763 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 764 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 765 | remote_streams_(StreamCollection::Create()), |
| 766 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 767 | |
| 768 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 769 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 770 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 771 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 772 | // Need to stop transceivers before destroying the stats collector because |
| 773 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 774 | // stopping. |
| 775 | for (auto transceiver : transceivers_) { |
| 776 | transceiver->Stop(); |
| 777 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 778 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 779 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 780 | if (stats_collector_) { |
| 781 | stats_collector_->WaitForPendingRequest(); |
| 782 | stats_collector_ = nullptr; |
| 783 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 784 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 785 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 786 | // the last stats request can still read from the channels. |
| 787 | DestroyAllChannels(); |
| 788 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 789 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 790 | |
| 791 | webrtc_session_desc_factory_.reset(); |
| 792 | sctp_invoker_.reset(); |
| 793 | sctp_factory_.reset(); |
| 794 | transport_controller_.reset(); |
| 795 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 796 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 797 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 798 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 799 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 800 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 801 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 802 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 803 | event_log_.reset(); |
| 804 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 807 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 808 | // Destroy video channels first since they may have a pointer to a voice |
| 809 | // channel. |
| 810 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 811 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 812 | DestroyTransceiverChannel(transceiver); |
| 813 | } |
| 814 | } |
| 815 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 816 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 817 | DestroyTransceiverChannel(transceiver); |
| 818 | } |
| 819 | } |
| 820 | DestroyDataChannel(); |
| 821 | } |
| 822 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 823 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 824 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 825 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 826 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 827 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 828 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 829 | |
| 830 | RTCError config_error = ValidateConfiguration(configuration); |
| 831 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 832 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 833 | return false; |
| 834 | } |
| 835 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 836 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 837 | RTC_LOG(LS_ERROR) |
| 838 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 839 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 840 | return false; |
| 841 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 842 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 843 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 844 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 845 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 846 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 847 | return false; |
| 848 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 849 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 850 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 851 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 852 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 853 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 854 | if (!network_thread()->Invoke<bool>( |
| 855 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 856 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 857 | return false; |
| 858 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 859 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 860 | // RFC 3264: The numeric value of the session id and version in the |
| 861 | // o line MUST be representable with a "64 bit signed integer". |
| 862 | // Due to this constraint session id |session_id_| is max limited to |
| 863 | // LLONG_MAX. |
| 864 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 865 | transport_controller_.reset(factory_->CreateTransportController( |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 866 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart, |
| 867 | event_log_.get())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 868 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 869 | transport_controller_->SignalConnectionState.connect( |
| 870 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 871 | transport_controller_->SignalGatheringState.connect( |
| 872 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 873 | transport_controller_->SignalCandidatesGathered.connect( |
| 874 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 875 | transport_controller_->SignalCandidatesRemoved.connect( |
| 876 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 877 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 878 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 879 | |
| 880 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 881 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 882 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 883 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 884 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 885 | configuration_ = configuration; |
| 886 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 887 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 888 | |
| 889 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 890 | |
| 891 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 892 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 893 | if (!configuration.certificates.empty()) { |
| 894 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 895 | // just picking the first one. The decision should be made based on the DTLS |
| 896 | // handshake. The DTLS negotiations need to know about all certificates. |
| 897 | certificate = configuration.certificates[0]; |
| 898 | } |
| 899 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 900 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 901 | |
| 902 | if (options.disable_encryption) { |
| 903 | dtls_enabled_ = false; |
| 904 | } else { |
| 905 | // Enable DTLS by default if we have an identity store or a certificate. |
| 906 | dtls_enabled_ = (cert_generator || certificate); |
| 907 | // |configuration| can override the default |dtls_enabled_| value. |
| 908 | if (configuration.enable_dtls_srtp) { |
| 909 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 914 | // It takes precendence over the disable_sctp_data_channels |
| 915 | // PeerConnectionFactoryInterface::Options. |
| 916 | if (configuration.enable_rtp_data_channel) { |
| 917 | data_channel_type_ = cricket::DCT_RTP; |
| 918 | } else { |
| 919 | // DTLS has to be enabled to use SCTP. |
| 920 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 921 | data_channel_type_ = cricket::DCT_SCTP; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | video_options_.screencast_min_bitrate_kbps = |
| 926 | configuration.screencast_min_bitrate; |
| 927 | audio_options_.combined_audio_video_bwe = |
| 928 | configuration.combined_audio_video_bwe; |
| 929 | |
| 930 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 931 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 932 | |
| 933 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 934 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 935 | |
| 936 | // Whether the certificate generator/certificate is null or not determines |
| 937 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 938 | // the right instructions by clearing the variables if needed. |
| 939 | if (!dtls_enabled_) { |
| 940 | cert_generator.reset(); |
| 941 | certificate = nullptr; |
| 942 | } else if (certificate) { |
| 943 | // Favor generated certificate over the certificate generator. |
| 944 | cert_generator.reset(); |
| 945 | } |
| 946 | |
| 947 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 948 | signaling_thread(), channel_manager(), this, session_id(), |
| 949 | std::move(cert_generator), certificate)); |
| 950 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 951 | this, &PeerConnection::OnCertificateReady); |
| 952 | |
| 953 | if (options.disable_encryption) { |
| 954 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 955 | } |
| 956 | |
| 957 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 958 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 959 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 960 | // Add default audio/video transceivers for Plan B SDP. |
| 961 | if (!IsUnifiedPlan()) { |
| 962 | transceivers_.push_back( |
| 963 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 964 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 965 | transceivers_.push_back( |
| 966 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 967 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 968 | } |
| 969 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 970 | return true; |
| 971 | } |
| 972 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 973 | RTCError PeerConnection::ValidateConfiguration( |
| 974 | const RTCConfiguration& config) const { |
| 975 | if (config.ice_regather_interval_range && |
| 976 | config.continual_gathering_policy == GATHER_ONCE) { |
| 977 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 978 | "ice_regather_interval_range specified but continual " |
| 979 | "gathering policy is GATHER_ONCE"); |
| 980 | } |
| 981 | return RTCError::OK(); |
| 982 | } |
| 983 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 984 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 985 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 986 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 987 | } |
| 988 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 989 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 990 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 991 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 992 | } |
| 993 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 994 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 995 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 996 | if (IsClosed()) { |
| 997 | return false; |
| 998 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 999 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1000 | return false; |
| 1001 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1002 | |
| 1003 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1004 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1005 | observer->SignalAudioTrackAdded.connect(this, |
| 1006 | &PeerConnection::OnAudioTrackAdded); |
| 1007 | observer->SignalAudioTrackRemoved.connect( |
| 1008 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1009 | observer->SignalVideoTrackAdded.connect(this, |
| 1010 | &PeerConnection::OnVideoTrackAdded); |
| 1011 | observer->SignalVideoTrackRemoved.connect( |
| 1012 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1013 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1014 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1015 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1016 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1017 | } |
| 1018 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1019 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1022 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1023 | observer_->OnRenegotiationNeeded(); |
| 1024 | return true; |
| 1025 | } |
| 1026 | |
| 1027 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1028 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1029 | if (!IsClosed()) { |
| 1030 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1031 | RemoveAudioTrack(track.get(), local_stream); |
| 1032 | } |
| 1033 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1034 | RemoveVideoTrack(track.get(), local_stream); |
| 1035 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1036 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1037 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1038 | stream_observers_.erase( |
| 1039 | std::remove_if( |
| 1040 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1041 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1042 | return observer->stream()->label().compare(local_stream->label()) == |
| 1043 | 0; |
| 1044 | }), |
| 1045 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1046 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1047 | if (IsClosed()) { |
| 1048 | return; |
| 1049 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1050 | observer_->OnRenegotiationNeeded(); |
| 1051 | } |
| 1052 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1053 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1054 | MediaStreamTrackInterface* track, |
| 1055 | std::vector<MediaStreamInterface*> streams) { |
| 1056 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1057 | std::vector<std::string> stream_labels; |
| 1058 | for (auto* stream : streams) { |
| 1059 | if (!stream) { |
| 1060 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1061 | return nullptr; |
| 1062 | } |
| 1063 | stream_labels.push_back(stream->label()); |
| 1064 | } |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1065 | auto sender_or_error = AddTrack(track, stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1066 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1067 | return nullptr; |
| 1068 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1069 | return sender_or_error.MoveValue(); |
| 1070 | } |
| 1071 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1072 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1073 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1074 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1075 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1076 | if (!track) { |
| 1077 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1078 | } |
| 1079 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1080 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1081 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1082 | "Track has invalid kind: " + track->kind()); |
| 1083 | } |
| 1084 | // TODO(bugs.webrtc.org/7932): Support adding a track to multiple streams. |
| 1085 | if (stream_labels.size() > 1u) { |
| 1086 | LOG_AND_RETURN_ERROR( |
| 1087 | RTCErrorType::UNSUPPORTED_OPERATION, |
| 1088 | "AddTrack with more than one stream is not currently supported."); |
| 1089 | } |
| 1090 | if (IsClosed()) { |
| 1091 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1092 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1093 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1094 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1095 | LOG_AND_RETURN_ERROR( |
| 1096 | RTCErrorType::INVALID_PARAMETER, |
| 1097 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1098 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1099 | // TODO(bugs.webrtc.org/7933): MediaSession expects the sender to have exactly |
| 1100 | // one stream. AddTrackInternal will return an error if there is more than one |
| 1101 | // stream, but if the caller specifies none then we need to generate a random |
| 1102 | // stream label. |
| 1103 | std::vector<std::string> adjusted_stream_labels = stream_labels; |
| 1104 | if (stream_labels.empty()) { |
| 1105 | adjusted_stream_labels.push_back(rtc::CreateRandomUuid()); |
| 1106 | } |
| 1107 | RTC_DCHECK_EQ(1, adjusted_stream_labels.size()); |
| 1108 | auto sender_or_error = |
| 1109 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, adjusted_stream_labels) |
| 1110 | : AddTrackPlanB(track, adjusted_stream_labels)); |
| 1111 | if (sender_or_error.ok()) { |
| 1112 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1113 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1114 | } |
| 1115 | return sender_or_error; |
| 1116 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1117 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1118 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1119 | PeerConnection::AddTrackPlanB( |
| 1120 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1121 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1122 | cricket::MediaType media_type = |
| 1123 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1124 | ? cricket::MEDIA_TYPE_AUDIO |
| 1125 | : cricket::MEDIA_TYPE_VIDEO); |
| 1126 | auto new_sender = CreateSender(media_type, track, stream_labels); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1127 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1128 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1129 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1130 | const RtpSenderInfo* sender_info = |
| 1131 | FindSenderInfo(local_audio_sender_infos_, |
| 1132 | new_sender->internal()->stream_id(), track->id()); |
| 1133 | if (sender_info) { |
| 1134 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1135 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1136 | } else { |
| 1137 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1138 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1139 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1140 | const RtpSenderInfo* sender_info = |
| 1141 | FindSenderInfo(local_video_sender_infos_, |
| 1142 | new_sender->internal()->stream_id(), track->id()); |
| 1143 | if (sender_info) { |
| 1144 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1145 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1146 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1147 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1148 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1149 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1150 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1151 | PeerConnection::AddTrackUnifiedPlan( |
| 1152 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1153 | const std::vector<std::string>& stream_labels) { |
| 1154 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1155 | if (transceiver) { |
| 1156 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
| 1157 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1158 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
| 1159 | transceiver->SetDirection(RtpTransceiverDirection::kSendOnly); |
| 1160 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1161 | transceiver->sender()->SetTrack(track); |
| 1162 | transceiver->internal()->sender_internal()->set_stream_ids(stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1163 | } else { |
| 1164 | cricket::MediaType media_type = |
| 1165 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1166 | ? cricket::MEDIA_TYPE_AUDIO |
| 1167 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1168 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1169 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1170 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1171 | transceiver->internal()->set_created_by_addtrack(true); |
| 1172 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1173 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1174 | return transceiver->sender(); |
| 1175 | } |
| 1176 | |
| 1177 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1178 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1179 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1180 | RTC_DCHECK(track); |
| 1181 | for (auto transceiver : transceivers_) { |
| 1182 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1183 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1184 | track->kind() && |
| 1185 | !transceiver->internal()->has_ever_been_used_to_send()) { |
| 1186 | return transceiver; |
| 1187 | } |
| 1188 | } |
| 1189 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1193 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1194 | return RemoveTrackInternal(sender).ok(); |
| 1195 | } |
| 1196 | |
| 1197 | RTCError PeerConnection::RemoveTrackInternal( |
| 1198 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1199 | if (!sender) { |
| 1200 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1201 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1202 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1203 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1204 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1205 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1206 | if (IsUnifiedPlan()) { |
| 1207 | auto transceiver = FindTransceiverBySender(sender); |
| 1208 | if (!transceiver || !sender->track()) { |
| 1209 | return RTCError::OK(); |
| 1210 | } |
| 1211 | sender->SetTrack(nullptr); |
| 1212 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
| 1213 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kRecvOnly); |
| 1214 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
| 1215 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kInactive); |
| 1216 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1217 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1218 | bool removed; |
| 1219 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1220 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1221 | } else { |
| 1222 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1223 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1224 | } |
| 1225 | if (!removed) { |
| 1226 | LOG_AND_RETURN_ERROR( |
| 1227 | RTCErrorType::INVALID_PARAMETER, |
| 1228 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1229 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1230 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1231 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1232 | return RTCError::OK(); |
| 1233 | } |
| 1234 | |
| 1235 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1236 | PeerConnection::FindTransceiverBySender( |
| 1237 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1238 | for (auto transceiver : transceivers_) { |
| 1239 | if (transceiver->sender() == sender) { |
| 1240 | return transceiver; |
| 1241 | } |
| 1242 | } |
| 1243 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1244 | } |
| 1245 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1246 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1247 | PeerConnection::AddTransceiver( |
| 1248 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1249 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1250 | } |
| 1251 | |
| 1252 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1253 | PeerConnection::AddTransceiver( |
| 1254 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1255 | const RtpTransceiverInit& init) { |
| 1256 | if (!IsUnifiedPlan()) { |
| 1257 | LOG_AND_RETURN_ERROR( |
| 1258 | RTCErrorType::INTERNAL_ERROR, |
| 1259 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1260 | } |
| 1261 | if (!track) { |
| 1262 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1263 | } |
| 1264 | cricket::MediaType media_type; |
| 1265 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1266 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1267 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1268 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1269 | } else { |
| 1270 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1271 | "Track kind is not audio or video"); |
| 1272 | } |
| 1273 | return AddTransceiver(media_type, track, init); |
| 1274 | } |
| 1275 | |
| 1276 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1277 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1278 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1279 | } |
| 1280 | |
| 1281 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1282 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1283 | const RtpTransceiverInit& init) { |
| 1284 | if (!IsUnifiedPlan()) { |
| 1285 | LOG_AND_RETURN_ERROR( |
| 1286 | RTCErrorType::INTERNAL_ERROR, |
| 1287 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1288 | } |
| 1289 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1290 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1291 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1292 | "media type is not audio or video"); |
| 1293 | } |
| 1294 | return AddTransceiver(media_type, nullptr, init); |
| 1295 | } |
| 1296 | |
| 1297 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1298 | PeerConnection::AddTransceiver( |
| 1299 | cricket::MediaType media_type, |
| 1300 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1301 | const RtpTransceiverInit& init, |
| 1302 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1303 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1304 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1305 | if (track) { |
| 1306 | RTC_DCHECK_EQ(media_type, |
| 1307 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1308 | ? cricket::MEDIA_TYPE_AUDIO |
| 1309 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1310 | } |
| 1311 | |
| 1312 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1313 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1314 | if (init.stream_labels.size() > 1u) { |
| 1315 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1316 | "More than one stream is not yet supported."); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1317 | } |
| 1318 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1319 | std::vector<std::string> stream_labels = {!init.stream_labels.empty() |
| 1320 | ? init.stream_labels[0] |
| 1321 | : rtc::CreateRandomUuid()}; |
| 1322 | |
| 1323 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1324 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1325 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1326 | transceiver->internal()->set_direction(init.direction); |
| 1327 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1328 | if (fire_callback) { |
| 1329 | observer_->OnRenegotiationNeeded(); |
| 1330 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1331 | |
| 1332 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1333 | } |
| 1334 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1335 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1336 | PeerConnection::CreateSender( |
| 1337 | cricket::MediaType media_type, |
| 1338 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1339 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1340 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1341 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1342 | RTC_DCHECK(!track || |
| 1343 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1344 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1345 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1346 | new AudioRtpSender(worker_thread(), |
| 1347 | static_cast<AudioTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1348 | stream_labels, stats_.get())); |
| 1349 | } else { |
| 1350 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1351 | RTC_DCHECK(!track || |
| 1352 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1353 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1354 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1355 | new VideoRtpSender(worker_thread(), |
| 1356 | static_cast<VideoTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1357 | stream_labels)); |
| 1358 | } |
| 1359 | sender->internal()->set_stream_ids(stream_labels); |
| 1360 | return sender; |
| 1361 | } |
| 1362 | |
| 1363 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1364 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1365 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1366 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1367 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1368 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1369 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1370 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1371 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1372 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1373 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1374 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1375 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1376 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1377 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1378 | return receiver; |
| 1379 | } |
| 1380 | |
| 1381 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1382 | PeerConnection::CreateAndAddTransceiver( |
| 1383 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1384 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1385 | receiver) { |
| 1386 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1387 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1388 | transceivers_.push_back(transceiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1389 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1392 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1393 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1394 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1395 | if (IsClosed()) { |
| 1396 | return nullptr; |
| 1397 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1398 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1399 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1400 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1401 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1402 | auto track_sender = FindSenderForTrack(track); |
| 1403 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1404 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1405 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1408 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1411 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1412 | const std::string& kind, |
| 1413 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1414 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1415 | if (IsClosed()) { |
| 1416 | return nullptr; |
| 1417 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1418 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1419 | std::vector<std::string> stream_labels; |
| 1420 | if (!stream_id.empty()) { |
| 1421 | stream_labels.push_back(stream_id); |
| 1422 | } |
| 1423 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1424 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1425 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1426 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1427 | auto* audio_sender = new AudioRtpSender(worker_thread(), nullptr, |
| 1428 | stream_labels, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1429 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1430 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1431 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1432 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1433 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1434 | auto* video_sender = |
| 1435 | new VideoRtpSender(worker_thread(), nullptr, stream_labels); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1436 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1437 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1438 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1439 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1440 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1441 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1442 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1443 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1444 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1445 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1448 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1449 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1450 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1451 | for (auto sender : GetSendersInternal()) { |
| 1452 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1453 | } |
| 1454 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1457 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1458 | PeerConnection::GetSendersInternal() const { |
| 1459 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1460 | all_senders; |
| 1461 | for (auto transceiver : transceivers_) { |
| 1462 | auto senders = transceiver->internal()->senders(); |
| 1463 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1464 | } |
| 1465 | return all_senders; |
| 1466 | } |
| 1467 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1468 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1469 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1470 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1471 | for (const auto& receiver : GetReceiversInternal()) { |
| 1472 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1473 | } |
| 1474 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1477 | std::vector< |
| 1478 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1479 | PeerConnection::GetReceiversInternal() const { |
| 1480 | std::vector< |
| 1481 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1482 | all_receivers; |
| 1483 | for (auto transceiver : transceivers_) { |
| 1484 | auto receivers = transceiver->internal()->receivers(); |
| 1485 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1486 | receivers.end()); |
| 1487 | } |
| 1488 | return all_receivers; |
| 1489 | } |
| 1490 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1491 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1492 | PeerConnection::GetTransceivers() const { |
| 1493 | RTC_DCHECK(IsUnifiedPlan()); |
| 1494 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1495 | for (auto transceiver : transceivers_) { |
| 1496 | all_transceivers.push_back(transceiver); |
| 1497 | } |
| 1498 | return all_transceivers; |
| 1499 | } |
| 1500 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1501 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1502 | MediaStreamTrackInterface* track, |
| 1503 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1504 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1505 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1506 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1507 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1508 | return false; |
| 1509 | } |
| 1510 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1511 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1512 | // The StatsCollector is used to tell if a track is valid because it may |
| 1513 | // remember tracks that the PeerConnection previously removed. |
| 1514 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1515 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1516 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1519 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1520 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1521 | return true; |
| 1522 | } |
| 1523 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1524 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 1525 | RTC_DCHECK(stats_collector_); |
| 1526 | stats_collector_->GetStatsReport(callback); |
| 1527 | } |
| 1528 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1529 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1530 | return signaling_state_; |
| 1531 | } |
| 1532 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1533 | PeerConnectionInterface::IceConnectionState |
| 1534 | PeerConnection::ice_connection_state() { |
| 1535 | return ice_connection_state_; |
| 1536 | } |
| 1537 | |
| 1538 | PeerConnectionInterface::IceGatheringState |
| 1539 | PeerConnection::ice_gathering_state() { |
| 1540 | return ice_gathering_state_; |
| 1541 | } |
| 1542 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1543 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1544 | PeerConnection::CreateDataChannel( |
| 1545 | const std::string& label, |
| 1546 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1547 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1548 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1549 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1550 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1551 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1552 | if (config) { |
| 1553 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1554 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1555 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1556 | InternalCreateDataChannel(label, internal_config.get())); |
| 1557 | if (!channel.get()) { |
| 1558 | return nullptr; |
| 1559 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1560 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1561 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1562 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1563 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1564 | observer_->OnRenegotiationNeeded(); |
| 1565 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1566 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1567 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1568 | } |
| 1569 | |
| 1570 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1571 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1572 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1573 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1574 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1575 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1576 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1577 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1578 | // |constraints| and some downstream applications might create offers with |
| 1579 | // mandatory fields which would not be parsed in the helper method. For |
| 1580 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1581 | // |constraints| as a mandatory field but it is not parsed. |
| 1582 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1583 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1584 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1588 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1589 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1590 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1591 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1592 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1593 | return; |
| 1594 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1595 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1596 | if (IsClosed()) { |
| 1597 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1598 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1599 | PostCreateSessionDescriptionFailure(observer, error); |
| 1600 | return; |
| 1601 | } |
| 1602 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1603 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1604 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1605 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1606 | PostCreateSessionDescriptionFailure(observer, error); |
| 1607 | return; |
| 1608 | } |
| 1609 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1610 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1611 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1612 | if (IsUnifiedPlan()) { |
| 1613 | RTCError error = HandleLegacyOfferOptions(options); |
| 1614 | if (!error.ok()) { |
| 1615 | PostCreateSessionDescriptionFailure(observer, error.message()); |
| 1616 | return; |
| 1617 | } |
| 1618 | } |
| 1619 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1620 | cricket::MediaSessionOptions session_options; |
| 1621 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1622 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1625 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1626 | const RTCOfferAnswerOptions& options) { |
| 1627 | RTC_DCHECK(IsUnifiedPlan()); |
| 1628 | |
| 1629 | if (options.offer_to_receive_audio == 0) { |
| 1630 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1631 | cricket::MEDIA_TYPE_AUDIO); |
| 1632 | } else if (options.offer_to_receive_audio == 1) { |
| 1633 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1634 | } else if (options.offer_to_receive_audio > 1) { |
| 1635 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1636 | "offer_to_receive_audio > 1 is not supported."); |
| 1637 | } |
| 1638 | |
| 1639 | if (options.offer_to_receive_video == 0) { |
| 1640 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1641 | cricket::MEDIA_TYPE_VIDEO); |
| 1642 | } else if (options.offer_to_receive_video == 1) { |
| 1643 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1644 | } else if (options.offer_to_receive_video > 1) { |
| 1645 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1646 | "offer_to_receive_video > 1 is not supported."); |
| 1647 | } |
| 1648 | |
| 1649 | return RTCError::OK(); |
| 1650 | } |
| 1651 | |
| 1652 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1653 | cricket::MediaType media_type) { |
| 1654 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 1655 | transceiver->internal()->set_direction( |
| 1656 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false)); |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1661 | cricket::MediaType media_type) { |
| 1662 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 1663 | RtpTransceiverInit init; |
| 1664 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1665 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1670 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1671 | std::vector< |
| 1672 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1673 | receiving_transceivers; |
| 1674 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1675 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1676 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1677 | receiving_transceivers.push_back(transceiver); |
| 1678 | } |
| 1679 | } |
| 1680 | return receiving_transceivers; |
| 1681 | } |
| 1682 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1683 | void PeerConnection::CreateAnswer( |
| 1684 | CreateSessionDescriptionObserver* observer, |
| 1685 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1686 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1687 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1688 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1689 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1690 | return; |
| 1691 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1692 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1693 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1694 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1695 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1696 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1697 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1698 | PostCreateSessionDescriptionFailure(observer, error); |
| 1699 | return; |
| 1700 | } |
| 1701 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1702 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1706 | const RTCOfferAnswerOptions& options) { |
| 1707 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1708 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1709 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1710 | return; |
| 1711 | } |
| 1712 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1713 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1714 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1715 | std::string error = |
| 1716 | "PeerConnection cannot create an answer in a state other than " |
| 1717 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1718 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1719 | PostCreateSessionDescriptionFailure(observer, error); |
| 1720 | return; |
| 1721 | } |
| 1722 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1723 | // The remote description should be set if we're in the right state. |
| 1724 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1725 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1726 | if (IsUnifiedPlan()) { |
| 1727 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1728 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1729 | "supported with Unified Plan semantics. Use the " |
| 1730 | "RtpTransceiver API instead."; |
| 1731 | } |
| 1732 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1733 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1734 | "supported with Unified Plan semantics. Use the " |
| 1735 | "RtpTransceiver API instead."; |
| 1736 | } |
| 1737 | } |
| 1738 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1739 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1740 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1741 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1742 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | void PeerConnection::SetLocalDescription( |
| 1746 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1747 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1748 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1749 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1750 | // The SetLocalDescription contract is that we take ownership of the session |
| 1751 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1752 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1753 | // description as a unique_ptr argument to formalize this agreement. |
| 1754 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1755 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1756 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1757 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1758 | return; |
| 1759 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1760 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1761 | if (!desc) { |
| 1762 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1763 | return; |
| 1764 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1765 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1766 | // If a session error has occurred the PeerConnection is in a possibly |
| 1767 | // inconsistent state so fail right away. |
| 1768 | if (session_error() != SessionError::kNone) { |
| 1769 | std::string error_message = GetSessionErrorMsg(); |
| 1770 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
| 1771 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1772 | return; |
| 1773 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1774 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1775 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1776 | if (!error.ok()) { |
| 1777 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1778 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1779 | RTC_LOG(LS_ERROR) << error_message; |
| 1780 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1785 | // which may destroy it before returning. |
| 1786 | const SdpType type = desc->GetType(); |
| 1787 | |
| 1788 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1789 | // |desc| may be destroyed at this point. |
| 1790 | |
| 1791 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1792 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1793 | // inconsistent state, so act conservatively here and set the session error |
| 1794 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1795 | SetSessionError(SessionError::kContent, error.message()); |
| 1796 | std::string error_message = |
| 1797 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1798 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1799 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1800 | return; |
| 1801 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1802 | RTC_DCHECK(local_description()); |
| 1803 | |
| 1804 | PostSetSessionDescriptionSuccess(observer); |
| 1805 | |
| 1806 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1807 | // size is not allowed, and changing the set of ICE servers will not result |
| 1808 | // in new candidates being gathered. |
| 1809 | port_allocator_->FreezeCandidatePool(); |
| 1810 | |
| 1811 | // MaybeStartGathering needs to be called after posting |
| 1812 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1813 | // before signaling that SetLocalDescription completed. |
| 1814 | transport_controller_->MaybeStartGathering(); |
| 1815 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1816 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1817 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1818 | // MaybeStartGathering... |
| 1819 | network_thread()->Invoke<void>( |
| 1820 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1821 | port_allocator_.get())); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | RTCError PeerConnection::ApplyLocalDescription( |
| 1826 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1827 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1828 | RTC_DCHECK(desc); |
| 1829 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1830 | // Update stats here so that we have the most recent stats for tracks and |
| 1831 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1832 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1833 | |
| 1834 | // Update the initial_offerer flag if this session is the initial_offerer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1835 | SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1836 | if (!initial_offerer_.has_value()) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1837 | initial_offerer_.emplace(type == SdpType::kOffer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1838 | if (*initial_offerer_) { |
| 1839 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1840 | } else { |
| 1841 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1842 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1843 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1844 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1845 | // Take a reference to the old local description since it's used below to |
| 1846 | // compare against the new local description. When setting the new local |
| 1847 | // description, grab ownership of the replaced session description in case it |
| 1848 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1849 | // of the method. |
| 1850 | const SessionDescriptionInterface* old_local_description = |
| 1851 | local_description(); |
| 1852 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1853 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1854 | replaced_local_description = pending_local_description_ |
| 1855 | ? std::move(pending_local_description_) |
| 1856 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1857 | current_local_description_ = std::move(desc); |
| 1858 | pending_local_description_ = nullptr; |
| 1859 | current_remote_description_ = std::move(pending_remote_description_); |
| 1860 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1861 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1862 | pending_local_description_ = std::move(desc); |
| 1863 | } |
| 1864 | // The session description to apply now must be accessed by |
| 1865 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1866 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1867 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1868 | if (IsUnifiedPlan()) { |
| 1869 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 1870 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 1871 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1872 | if (!error.ok()) { |
| 1873 | return error; |
| 1874 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1875 | for (auto transceiver : transceivers_) { |
| 1876 | const ContentInfo* content = |
| 1877 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1878 | if (!content) { |
| 1879 | continue; |
| 1880 | } |
| 1881 | const MediaContentDescription* media_desc = content->media_description(); |
| 1882 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1883 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 1884 | } |
| 1885 | if (content->rejected && !transceiver->stopped()) { |
| 1886 | transceiver->Stop(); |
| 1887 | } |
| 1888 | } |
| 1889 | } else { |
| 1890 | // Transport and Media channels will be created only when offer is set. |
| 1891 | if (type == SdpType::kOffer) { |
| 1892 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 1893 | // description is applied. Restore back to old description. |
| 1894 | RTCError error = CreateChannels(*local_description()->description()); |
| 1895 | if (!error.ok()) { |
| 1896 | return error; |
| 1897 | } |
| 1898 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1899 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1900 | // Remove unused channels if MediaContentDescription is rejected. |
| 1901 | RemoveUnusedChannels(local_description()->description()); |
| 1902 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1903 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 1904 | RTCError error = UpdateSessionState(type, cricket::CS_LOCAL); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1905 | if (!error.ok()) { |
| 1906 | return error; |
| 1907 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1908 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1909 | if (remote_description()) { |
| 1910 | // Now that we have a local description, we can push down remote candidates. |
| 1911 | UseCandidatesInSessionDescription(remote_description()); |
| 1912 | } |
| 1913 | |
| 1914 | pending_ice_restarts_.clear(); |
| 1915 | if (session_error() != SessionError::kNone) { |
| 1916 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 1917 | } |
| 1918 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1919 | // If setting the description decided our SSL role, allocate any necessary |
| 1920 | // SCTP sids. |
| 1921 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1922 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1923 | AllocateSctpSids(role); |
| 1924 | } |
| 1925 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1926 | if (IsUnifiedPlan()) { |
| 1927 | for (auto transceiver : transceivers_) { |
| 1928 | const ContentInfo* content = |
| 1929 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1930 | if (!content) { |
| 1931 | continue; |
| 1932 | } |
| 1933 | if (content->rejected && !transceiver->stopped()) { |
| 1934 | transceiver->Stop(); |
| 1935 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1936 | const auto& streams = content->media_description()->streams(); |
| 1937 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1938 | transceiver->internal()->sender_internal()->set_stream_ids( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1939 | {streams[0].sync_label}); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1940 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1941 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | } else { |
| 1945 | // Plan B semantics. |
| 1946 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1947 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1948 | // local session description. |
| 1949 | const cricket::ContentInfo* audio_content = |
| 1950 | GetFirstAudioContent(local_description()->description()); |
| 1951 | if (audio_content) { |
| 1952 | if (audio_content->rejected) { |
| 1953 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 1954 | } else { |
| 1955 | const cricket::AudioContentDescription* audio_desc = |
| 1956 | audio_content->media_description()->as_audio(); |
| 1957 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 1958 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1959 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1960 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1961 | const cricket::ContentInfo* video_content = |
| 1962 | GetFirstVideoContent(local_description()->description()); |
| 1963 | if (video_content) { |
| 1964 | if (video_content->rejected) { |
| 1965 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 1966 | } else { |
| 1967 | const cricket::VideoContentDescription* video_desc = |
| 1968 | video_content->media_description()->as_video(); |
| 1969 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 1970 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1971 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1975 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1976 | if (data_content) { |
| 1977 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1978 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1979 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1980 | cricket::kMediaProtocolRtpPrefix)) { |
| 1981 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1982 | } |
| 1983 | } |
| 1984 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1985 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 1989 | SetSessionDescriptionObserver* observer, |
| 1990 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 1991 | SetRemoteDescription( |
| 1992 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 1993 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 1994 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 1995 | } |
| 1996 | |
| 1997 | void PeerConnection::SetRemoteDescription( |
| 1998 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 1999 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2000 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2001 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2002 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2003 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2006 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2007 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2008 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2009 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2010 | return; |
| 2011 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2012 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 2013 | // If a session error has occurred the PeerConnection is in a possibly |
| 2014 | // inconsistent state so fail right away. |
| 2015 | if (session_error() != SessionError::kNone) { |
| 2016 | std::string error_message = GetSessionErrorMsg(); |
| 2017 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2018 | observer->OnSetRemoteDescriptionComplete( |
| 2019 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2020 | return; |
| 2021 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2022 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 2023 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2024 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 2025 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2026 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2027 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2028 | observer->OnSetRemoteDescriptionComplete( |
| 2029 | RTCError(error.type(), std::move(error_message))); |
| 2030 | return; |
| 2031 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2032 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 2033 | // Grab the description type before moving ownership to |
| 2034 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2035 | const SdpType type = desc->GetType(); |
| 2036 | |
| 2037 | error = ApplyRemoteDescription(std::move(desc)); |
| 2038 | // |desc| may be destroyed at this point. |
| 2039 | |
| 2040 | if (!error.ok()) { |
| 2041 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2042 | // inconsistent state, so act conservatively here and set the session error |
| 2043 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2044 | SetSessionError(SessionError::kContent, error.message()); |
| 2045 | std::string error_message = |
| 2046 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2047 | RTC_LOG(LS_ERROR) << error_message; |
| 2048 | observer->OnSetRemoteDescriptionComplete( |
| 2049 | RTCError(error.type(), std::move(error_message))); |
| 2050 | return; |
| 2051 | } |
| 2052 | RTC_DCHECK(remote_description()); |
| 2053 | |
| 2054 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2055 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2056 | // MaybeStartGathering... |
| 2057 | network_thread()->Invoke<void>( |
| 2058 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2059 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2060 | // Make UMA notes about what was agreed to. |
| 2061 | if (uma_observer_) { |
| 2062 | switch (remote_description()->description()->msid_signaling()) { |
| 2063 | case 0: |
| 2064 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2065 | kSdpSemanticNegotiatedNone, |
| 2066 | kSdpSemanticNegotiatedMax); |
| 2067 | break; |
| 2068 | case cricket::kMsidSignalingMediaSection: |
| 2069 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2070 | kSdpSemanticNegotiatedUnifiedPlan, |
| 2071 | kSdpSemanticNegotiatedMax); |
| 2072 | break; |
| 2073 | case cricket::kMsidSignalingSsrcAttribute: |
| 2074 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2075 | kSdpSemanticNegotiatedPlanB, |
| 2076 | kSdpSemanticNegotiatedMax); |
| 2077 | break; |
| 2078 | case cricket::kMsidSignalingMediaSection | |
| 2079 | cricket::kMsidSignalingSsrcAttribute: |
| 2080 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2081 | kSdpSemanticNegotiatedMixed, |
| 2082 | kSdpSemanticNegotiatedMax); |
| 2083 | break; |
| 2084 | default: |
| 2085 | RTC_NOTREACHED(); |
| 2086 | } |
| 2087 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2091 | } |
| 2092 | |
| 2093 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2094 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2095 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2096 | RTC_DCHECK(desc); |
| 2097 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2098 | // Update stats here so that we have the most recent stats for tracks and |
| 2099 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2100 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2101 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2102 | // Take a reference to the old remote description since it's used below to |
| 2103 | // compare against the new remote description. When setting the new remote |
| 2104 | // description, grab ownership of the replaced session description in case it |
| 2105 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2106 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2107 | const SessionDescriptionInterface* old_remote_description = |
| 2108 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2109 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2110 | SdpType type = desc->GetType(); |
| 2111 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2112 | replaced_remote_description = pending_remote_description_ |
| 2113 | ? std::move(pending_remote_description_) |
| 2114 | : std::move(current_remote_description_); |
| 2115 | current_remote_description_ = std::move(desc); |
| 2116 | pending_remote_description_ = nullptr; |
| 2117 | current_local_description_ = std::move(pending_local_description_); |
| 2118 | } else { |
| 2119 | replaced_remote_description = std::move(pending_remote_description_); |
| 2120 | pending_remote_description_ = std::move(desc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2121 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2122 | // The session description to apply now must be accessed by |
| 2123 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2124 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2125 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2126 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2127 | if (IsUnifiedPlan()) { |
| 2128 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2129 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2130 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2131 | if (!error.ok()) { |
| 2132 | return error; |
| 2133 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2134 | } else { |
| 2135 | if (type == SdpType::kOffer) { |
| 2136 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2137 | // description is applied. Restore back to old description. |
| 2138 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2139 | if (!error.ok()) { |
| 2140 | return error; |
| 2141 | } |
| 2142 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2143 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2144 | // Remove unused channels if MediaContentDescription is rejected. |
| 2145 | RemoveUnusedChannels(remote_description()->description()); |
| 2146 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2147 | |
| 2148 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 2149 | // is called. |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 2150 | RTCError error = UpdateSessionState(type, cricket::CS_REMOTE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2151 | if (!error.ok()) { |
| 2152 | return error; |
| 2153 | } |
| 2154 | |
| 2155 | if (local_description() && |
| 2156 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2157 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2158 | } |
| 2159 | |
| 2160 | if (old_remote_description) { |
| 2161 | for (const cricket::ContentInfo& content : |
| 2162 | old_remote_description->description()->contents()) { |
| 2163 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2164 | // password that indicates the remote peer requests an ICE restart. |
| 2165 | // TODO(deadbeef): When we start storing both the current and pending |
| 2166 | // remote description, this should reset pending_ice_restarts and compare |
| 2167 | // against the current description. |
| 2168 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2169 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2170 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2171 | pending_ice_restarts_.insert(content.name); |
| 2172 | } |
| 2173 | } else { |
| 2174 | // We retain all received candidates only if ICE is not restarted. |
| 2175 | // When ICE is restarted, all previous candidates belong to an old |
| 2176 | // generation and should not be kept. |
| 2177 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2178 | // description should only contain candidates from the last set remote |
| 2179 | // description plus any candidates added since then. We should remove |
| 2180 | // this once we're sure it won't break anything. |
| 2181 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2182 | old_remote_description, content.name, mutable_remote_description()); |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | if (session_error() != SessionError::kNone) { |
| 2188 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2189 | } |
| 2190 | |
| 2191 | // Set the the ICE connection state to connecting since the connection may |
| 2192 | // become writable with peer reflexive candidates before any remote candidate |
| 2193 | // is signaled. |
| 2194 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2195 | // is to have a new signal the indicates a change in checking state from the |
| 2196 | // transport and expose a new checking() member from transport that can be |
| 2197 | // read to determine the current checking state. The existing SignalConnecting |
| 2198 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2199 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2200 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2201 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2202 | } |
| 2203 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2204 | // If setting the description decided our SSL role, allocate any necessary |
| 2205 | // SCTP sids. |
| 2206 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2207 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2208 | AllocateSctpSids(role); |
| 2209 | } |
| 2210 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2211 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2212 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2213 | receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2214 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2215 | for (auto transceiver : transceivers_) { |
| 2216 | const ContentInfo* content = |
| 2217 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2218 | if (!content) { |
| 2219 | continue; |
| 2220 | } |
| 2221 | const MediaContentDescription* media_desc = content->media_description(); |
| 2222 | RtpTransceiverDirection local_direction = |
| 2223 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2224 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2225 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2226 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2227 | // process the addition of a remote track for the media description. |
| 2228 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2229 | (!transceiver->current_direction() || |
| 2230 | !RtpTransceiverDirectionHasRecv( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2231 | *transceiver->current_direction())) && |
| 2232 | !media_desc->streams().empty()) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2233 | const std::string& sync_label = media_desc->streams()[0].sync_label; |
| 2234 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2235 | remote_streams_->find(sync_label); |
| 2236 | if (!stream) { |
| 2237 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2238 | MediaStream::Create(sync_label)); |
| 2239 | remote_streams_->AddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2240 | added_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2241 | } |
| 2242 | transceiver->internal()->receiver_internal()->SetStreams({stream}); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2243 | receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2244 | } |
| 2245 | // If direction is sendonly or inactive, and transceiver's current |
| 2246 | // direction is neither sendonly nor inactive, process the removal of a |
| 2247 | // remote track for the media description. |
| 2248 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2249 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2250 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2251 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2252 | } |
| 2253 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2254 | transceiver->internal()->set_current_direction(local_direction); |
| 2255 | } |
| 2256 | if (content->rejected && !transceiver->stopped()) { |
| 2257 | transceiver->Stop(); |
| 2258 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 2259 | if (!content->rejected && !media_desc->streams().empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2260 | transceiver->internal()->receiver_internal()->SetupMediaChannel( |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2261 | media_desc->streams()[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2262 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2263 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2264 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2265 | for (auto transceiver : receiving_transceivers) { |
| 2266 | observer_->OnTrack(transceiver); |
| 2267 | observer_->OnAddTrack(transceiver->receiver(), |
| 2268 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2269 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2270 | for (auto stream : added_streams) { |
| 2271 | observer_->OnAddStream(stream); |
| 2272 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2273 | } |
| 2274 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2275 | const cricket::ContentInfo* audio_content = |
| 2276 | GetFirstAudioContent(remote_description()->description()); |
| 2277 | const cricket::ContentInfo* video_content = |
| 2278 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2279 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2280 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2281 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2282 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2283 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2284 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2285 | |
| 2286 | // Check if the descriptions include streams, just in case the peer supports |
| 2287 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2288 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2289 | (audio_desc && !audio_desc->streams().empty()) || |
| 2290 | (video_desc && !video_desc->streams().empty())) { |
| 2291 | remote_peer_supports_msid_ = true; |
| 2292 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2293 | |
| 2294 | // We wait to signal new streams until we finish processing the description, |
| 2295 | // since only at that point will new streams have all their tracks. |
| 2296 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2297 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2298 | if (!IsUnifiedPlan()) { |
| 2299 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2300 | // rejected media section, there is some cleanup logic that expects the |
| 2301 | // voice/ video channel to still be set. But in this method the voice/video |
| 2302 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2303 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2304 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2305 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2306 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2307 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2308 | // and MediaStreams. |
| 2309 | if (audio_content) { |
| 2310 | if (audio_content->rejected) { |
| 2311 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2312 | } else { |
| 2313 | bool default_audio_track_needed = |
| 2314 | !remote_peer_supports_msid_ && |
| 2315 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2316 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2317 | default_audio_track_needed, audio_desc->type(), |
| 2318 | new_streams); |
| 2319 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2320 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2321 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2322 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2323 | // and MediaStreams. |
| 2324 | if (video_content) { |
| 2325 | if (video_content->rejected) { |
| 2326 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2327 | } else { |
| 2328 | bool default_video_track_needed = |
| 2329 | !remote_peer_supports_msid_ && |
| 2330 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2331 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2332 | default_video_track_needed, video_desc->type(), |
| 2333 | new_streams); |
| 2334 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2335 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2336 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2337 | // Update the DataChannels with the information from the remote peer. |
| 2338 | if (data_desc) { |
| 2339 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2340 | cricket::kMediaProtocolRtpPrefix)) { |
| 2341 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2342 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2343 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2344 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2345 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2346 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2347 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2348 | stats_->AddStream(new_stream); |
| 2349 | observer_->OnAddStream( |
| 2350 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2351 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2352 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2353 | UpdateEndedRemoteMediaStreams(); |
| 2354 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2355 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2356 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2359 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2360 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2361 | const SessionDescriptionInterface& new_session, |
| 2362 | const SessionDescriptionInterface* old_local_description, |
| 2363 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2364 | RTC_DCHECK(IsUnifiedPlan()); |
| 2365 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2366 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2367 | if (new_session.GetType() == SdpType::kOffer) { |
| 2368 | auto bundle_group_or_error = |
| 2369 | GetEarlyBundleGroup(*new_session.description()); |
| 2370 | if (!bundle_group_or_error.ok()) { |
| 2371 | return bundle_group_or_error.MoveError(); |
| 2372 | } |
| 2373 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2374 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2375 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2376 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2377 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2378 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2379 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2380 | seen_mids_.insert(new_content.name); |
| 2381 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2382 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2383 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2384 | if (old_local_description && |
| 2385 | i < old_local_description->description()->contents().size()) { |
| 2386 | old_local_content = |
| 2387 | &old_local_description->description()->contents()[i]; |
| 2388 | } |
| 2389 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2390 | if (old_remote_description && |
| 2391 | i < old_remote_description->description()->contents().size()) { |
| 2392 | old_remote_content = |
| 2393 | &old_remote_description->description()->contents()[i]; |
| 2394 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2395 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2396 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2397 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2398 | if (!transceiver_or_error.ok()) { |
| 2399 | return transceiver_or_error.MoveError(); |
| 2400 | } |
| 2401 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2402 | RTCError error = |
| 2403 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2404 | if (!error.ok()) { |
| 2405 | return error; |
| 2406 | } |
| 2407 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2408 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2409 | // Ignore all but the first data section. |
| 2410 | continue; |
| 2411 | } |
| 2412 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2413 | if (!error.ok()) { |
| 2414 | return error; |
| 2415 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2416 | } else { |
| 2417 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2418 | "Unknown section type."); |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | return RTCError::OK(); |
| 2423 | } |
| 2424 | |
| 2425 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2426 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2427 | transceiver, |
| 2428 | const cricket::ContentInfo& content, |
| 2429 | const cricket::ContentGroup* bundle_group) { |
| 2430 | RTC_DCHECK(IsUnifiedPlan()); |
| 2431 | RTC_DCHECK(transceiver); |
| 2432 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2433 | if (content.rejected) { |
| 2434 | if (channel) { |
| 2435 | transceiver->internal()->SetChannel(nullptr); |
| 2436 | DestroyBaseChannel(channel); |
| 2437 | } |
| 2438 | } else { |
| 2439 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2440 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2441 | channel = CreateVoiceChannel( |
| 2442 | content.name, |
| 2443 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2444 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2445 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2446 | channel = CreateVideoChannel( |
| 2447 | content.name, |
| 2448 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2449 | } |
| 2450 | if (!channel) { |
| 2451 | LOG_AND_RETURN_ERROR( |
| 2452 | RTCErrorType::INTERNAL_ERROR, |
| 2453 | "Failed to create channel for mid=" + content.name); |
| 2454 | } |
| 2455 | transceiver->internal()->SetChannel(channel); |
| 2456 | } |
| 2457 | } |
| 2458 | return RTCError::OK(); |
| 2459 | } |
| 2460 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2461 | RTCError PeerConnection::UpdateDataChannel( |
| 2462 | cricket::ContentSource source, |
| 2463 | const cricket::ContentInfo& content, |
| 2464 | const cricket::ContentGroup* bundle_group) { |
| 2465 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2466 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2467 | // will take care of rejecting it. |
| 2468 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2469 | } |
| 2470 | if (content.rejected) { |
| 2471 | DestroyDataChannel(); |
| 2472 | } else { |
| 2473 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 2474 | if (!CreateDataChannel(content.name, GetTransportNameForMediaSection( |
| 2475 | content.name, bundle_group))) { |
| 2476 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2477 | "Failed to create data channel."); |
| 2478 | } |
| 2479 | } |
| 2480 | if (source == cricket::CS_REMOTE) { |
| 2481 | const MediaContentDescription* data_desc = content.media_description(); |
| 2482 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2483 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2484 | } |
| 2485 | } |
| 2486 | } |
| 2487 | return RTCError::OK(); |
| 2488 | } |
| 2489 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2490 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2491 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2492 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2493 | size_t mline_index, |
| 2494 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2495 | const ContentInfo* old_local_content, |
| 2496 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2497 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2498 | // If this is an offer then the m= section might be recycled. If the m= |
| 2499 | // section is being recycled (defined as: rejected in the current local or |
| 2500 | // remote description and not rejected in new description), dissociate the |
| 2501 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2502 | // and discard the mapping between the transceiver and its m= section index. |
| 2503 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2504 | old_remote_content)) { |
| 2505 | // We want to dissociate the transceiver that has the rejected mid. |
| 2506 | const std::string& old_mid = |
| 2507 | (old_local_content && old_local_content->rejected) |
| 2508 | ? old_local_content->name |
| 2509 | : old_remote_content->name; |
| 2510 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2511 | if (old_transceiver) { |
| 2512 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2513 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2514 | } |
| 2515 | } |
| 2516 | const MediaContentDescription* media_desc = content.media_description(); |
| 2517 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2518 | if (source == cricket::CS_LOCAL) { |
| 2519 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2520 | // mapping between transceivers and m= section indices established when |
| 2521 | // creating the offer. |
| 2522 | if (!transceiver) { |
| 2523 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2524 | } |
| 2525 | if (!transceiver) { |
| 2526 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2527 | "Unknown transceiver"); |
| 2528 | } |
| 2529 | } else { |
| 2530 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2531 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2532 | // of the same type... |
| 2533 | if (!transceiver && |
| 2534 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2535 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2536 | } |
| 2537 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2538 | // recvonly direction. |
| 2539 | if (!transceiver) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2540 | auto sender = |
| 2541 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2542 | std::string receiver_id; |
| 2543 | if (!media_desc->streams().empty()) { |
| 2544 | receiver_id = media_desc->streams()[0].id; |
| 2545 | } else { |
| 2546 | receiver_id = rtc::CreateRandomUuid(); |
| 2547 | } |
| 2548 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2549 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2550 | transceiver->internal()->set_direction( |
| 2551 | RtpTransceiverDirection::kRecvOnly); |
| 2552 | } |
| 2553 | } |
| 2554 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2555 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2556 | LOG_AND_RETURN_ERROR( |
| 2557 | RTCErrorType::INVALID_PARAMETER, |
| 2558 | "Transceiver type does not match media description type."); |
| 2559 | } |
| 2560 | // Associate the found or created RtpTransceiver with the m= section by |
| 2561 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2562 | // section, and establish a mapping between the transceiver and the index of |
| 2563 | // the m= section. |
| 2564 | transceiver->internal()->set_mid(content.name); |
| 2565 | transceiver->internal()->set_mline_index(mline_index); |
| 2566 | return std::move(transceiver); |
| 2567 | } |
| 2568 | |
| 2569 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2570 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2571 | RTC_DCHECK(IsUnifiedPlan()); |
| 2572 | for (auto transceiver : transceivers_) { |
| 2573 | if (transceiver->mid() == mid) { |
| 2574 | return transceiver; |
| 2575 | } |
| 2576 | } |
| 2577 | return nullptr; |
| 2578 | } |
| 2579 | |
| 2580 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2581 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2582 | RTC_DCHECK(IsUnifiedPlan()); |
| 2583 | for (auto transceiver : transceivers_) { |
| 2584 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2585 | return transceiver; |
| 2586 | } |
| 2587 | } |
| 2588 | return nullptr; |
| 2589 | } |
| 2590 | |
| 2591 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2592 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2593 | cricket::MediaType media_type) const { |
| 2594 | RTC_DCHECK(IsUnifiedPlan()); |
| 2595 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2596 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2597 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2598 | // associated with any m= section and are not stopped, find the first such |
| 2599 | // RtpTransceiver. |
| 2600 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2601 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2602 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2603 | !transceiver->stopped()) { |
| 2604 | return transceiver; |
| 2605 | } |
| 2606 | } |
| 2607 | return nullptr; |
| 2608 | } |
| 2609 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2610 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2611 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2612 | transceiver, |
| 2613 | const SessionDescriptionInterface* sdesc) const { |
| 2614 | RTC_DCHECK(transceiver); |
| 2615 | RTC_DCHECK(sdesc); |
| 2616 | if (IsUnifiedPlan()) { |
| 2617 | if (!transceiver->internal()->mid()) { |
| 2618 | // This transceiver is not associated with a media section yet. |
| 2619 | return nullptr; |
| 2620 | } |
| 2621 | return sdesc->description()->GetContentByName( |
| 2622 | *transceiver->internal()->mid()); |
| 2623 | } else { |
| 2624 | // Plan B only allows at most one audio and one video section, so use the |
| 2625 | // first media section of that type. |
| 2626 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2627 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2631 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2632 | return configuration_; |
| 2633 | } |
| 2634 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2635 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2636 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2637 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2638 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2639 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2640 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2641 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2642 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2643 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2644 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2645 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2646 | // The simplest (and most future-compatible) way to tell if the config was |
| 2647 | // modified in an invalid way is to copy each property we do support |
| 2648 | // modifying, then use operator==. There are far more properties we don't |
| 2649 | // support modifying than those we do, and more could be added. |
| 2650 | RTCConfiguration modified_config = configuration_; |
| 2651 | modified_config.servers = configuration.servers; |
| 2652 | modified_config.type = configuration.type; |
| 2653 | modified_config.ice_candidate_pool_size = |
| 2654 | configuration.ice_candidate_pool_size; |
| 2655 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2656 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2657 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2658 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2659 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2660 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2661 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2662 | } |
| 2663 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2664 | // Validate the modified configuration. |
| 2665 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2666 | if (!validate_error.ok()) { |
| 2667 | return SafeSetError(std::move(validate_error), error); |
| 2668 | } |
| 2669 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2670 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2671 | // short in WebIDL. |
| 2672 | if (configuration.ice_candidate_pool_size < 0 || |
| 2673 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2674 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2675 | } |
| 2676 | |
| 2677 | // Parse ICE servers before hopping to network thread. |
| 2678 | cricket::ServerAddresses stun_servers; |
| 2679 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2680 | RTCErrorType parse_error = |
| 2681 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2682 | if (parse_error != RTCErrorType::NONE) { |
| 2683 | return SafeSetError(parse_error, error); |
| 2684 | } |
| 2685 | |
| 2686 | // In theory this shouldn't fail. |
| 2687 | if (!network_thread()->Invoke<bool>( |
| 2688 | RTC_FROM_HERE, |
| 2689 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2690 | stun_servers, turn_servers, modified_config.type, |
| 2691 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2692 | modified_config.prune_turn_ports, |
| 2693 | modified_config.turn_customizer))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2694 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2695 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2696 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2697 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2698 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2699 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2700 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2701 | if (modified_config.servers != configuration_.servers || |
| 2702 | modified_config.type != configuration_.type || |
| 2703 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2704 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2705 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2706 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2707 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2708 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2709 | configuration_ = modified_config; |
| 2710 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2713 | bool PeerConnection::AddIceCandidate( |
| 2714 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2715 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2716 | if (IsClosed()) { |
| 2717 | return false; |
| 2718 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2719 | |
| 2720 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2721 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2722 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2727 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2728 | return false; |
| 2729 | } |
| 2730 | |
| 2731 | bool valid = false; |
| 2732 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2733 | if (!valid) { |
| 2734 | return false; |
| 2735 | } |
| 2736 | |
| 2737 | // Add this candidate to the remote session description. |
| 2738 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2739 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | if (ready) { |
| 2744 | return UseCandidate(ice_candidate); |
| 2745 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2746 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2747 | return true; |
| 2748 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2751 | bool PeerConnection::RemoveIceCandidates( |
| 2752 | const std::vector<cricket::Candidate>& candidates) { |
| 2753 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2754 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2755 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2756 | "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2757 | return false; |
| 2758 | } |
| 2759 | |
| 2760 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2761 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2762 | return false; |
| 2763 | } |
| 2764 | |
| 2765 | size_t number_removed = |
| 2766 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2767 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2768 | RTC_LOG(LS_ERROR) |
| 2769 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2770 | "Requested " |
| 2771 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2772 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | // Remove the candidates from the transport controller. |
| 2776 | std::string error; |
| 2777 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 2778 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2779 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2780 | } |
| 2781 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2784 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2785 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2786 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2787 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2788 | if (transport_controller()) { |
| 2789 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2792 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2793 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 2794 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2795 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2796 | uma_observer_->IncrementEnumCounter( |
| 2797 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 2798 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 2799 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2800 | uma_observer_->IncrementEnumCounter( |
| 2801 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 2802 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2803 | } |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2804 | // Send information about the requested SDP semantics. |
| 2805 | switch (configuration_.sdp_semantics) { |
| 2806 | case SdpSemantics::kDefault: |
| 2807 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2808 | kSdpSemanticRequestDefault, |
| 2809 | kSdpSemanticRequestMax); |
| 2810 | |
| 2811 | break; |
| 2812 | case SdpSemantics::kPlanB: |
| 2813 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2814 | kSdpSemanticRequestPlanB, |
| 2815 | kSdpSemanticRequestMax); |
| 2816 | break; |
| 2817 | case SdpSemantics::kUnifiedPlan: |
| 2818 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2819 | kSdpSemanticRequestUnifiedPlan, |
| 2820 | kSdpSemanticRequestMax); |
| 2821 | break; |
| 2822 | default: |
| 2823 | RTC_NOTREACHED(); |
| 2824 | } |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2825 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2828 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2829 | if (!worker_thread()->IsCurrent()) { |
| 2830 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2831 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 2832 | } |
| 2833 | |
| 2834 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 2835 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 2836 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 2837 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 2838 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2839 | "min_bitrate_bps <= 0"); |
| 2840 | } |
| 2841 | if (has_current) { |
| 2842 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2843 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2844 | "current_bitrate_bps < min_bitrate_bps"); |
| 2845 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 2846 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2847 | "curent_bitrate_bps < 0"); |
| 2848 | } |
| 2849 | } |
| 2850 | if (has_max) { |
| 2851 | if (has_current && |
| 2852 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 2853 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2854 | "max_bitrate_bps < current_bitrate_bps"); |
| 2855 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2856 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2857 | "max_bitrate_bps < min_bitrate_bps"); |
| 2858 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 2859 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2860 | "max_bitrate_bps < 0"); |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | Call::Config::BitrateConfigMask mask; |
| 2865 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 2866 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 2867 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 2868 | |
| 2869 | RTC_DCHECK(call_.get()); |
| 2870 | call_->SetBitrateConfigMask(mask); |
| 2871 | |
| 2872 | return RTCError::OK(); |
| 2873 | } |
| 2874 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 2875 | void PeerConnection::SetBitrateAllocationStrategy( |
| 2876 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 2877 | bitrate_allocation_strategy) { |
| 2878 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 2879 | if (!worker_thread->IsCurrent()) { |
| 2880 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 2881 | bitrate_allocation_strategy.release(); |
| 2882 | auto functor = [this, strategy_raw]() { |
| 2883 | call_->SetBitrateAllocationStrategy( |
| 2884 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 2885 | }; |
| 2886 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 2887 | return; |
| 2888 | } |
| 2889 | RTC_DCHECK(call_.get()); |
| 2890 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 2891 | } |
| 2892 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 2893 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 2894 | if (!worker_thread()->IsCurrent()) { |
| 2895 | worker_thread()->Invoke<void>( |
| 2896 | RTC_FROM_HERE, |
| 2897 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 2898 | return; |
| 2899 | } |
| 2900 | auto audio_state = |
| 2901 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2902 | audio_state->SetPlayout(playout); |
| 2903 | } |
| 2904 | |
| 2905 | void PeerConnection::SetAudioRecording(bool recording) { |
| 2906 | if (!worker_thread()->IsCurrent()) { |
| 2907 | worker_thread()->Invoke<void>( |
| 2908 | RTC_FROM_HERE, |
| 2909 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 2910 | return; |
| 2911 | } |
| 2912 | auto audio_state = |
| 2913 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2914 | audio_state->SetRecording(recording); |
| 2915 | } |
| 2916 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2917 | std::unique_ptr<rtc::SSLCertificate> |
| 2918 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2919 | if (!voice_channel()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2920 | return nullptr; |
| 2921 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2922 | return GetRemoteSSLCertificate(voice_channel()->transport_name()); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2923 | } |
| 2924 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2925 | std::unique_ptr<rtc::SSLCertChain> |
| 2926 | PeerConnection::GetRemoteAudioSSLCertChain() { |
| 2927 | if (!voice_channel()) { |
| 2928 | return nullptr; |
| 2929 | } |
| 2930 | |
| 2931 | return transport_controller_->GetRemoteSSLCertChain( |
| 2932 | voice_channel()->transport_name()); |
| 2933 | } |
| 2934 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2935 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 2936 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2937 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 2938 | const size_t max_size = (max_size_bytes < 0) |
| 2939 | ? RtcEventLog::kUnlimitedOutput |
| 2940 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 2941 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2942 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 2943 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2946 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 2947 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2948 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 2949 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2950 | bool operator()() { |
| 2951 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 2952 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2953 | PeerConnection* const pc; |
| 2954 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2955 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2956 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2957 | return worker_thread()->Invoke<bool>( |
| 2958 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2962 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2963 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 2964 | } |
| 2965 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2966 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2967 | return pending_local_description_ ? pending_local_description_.get() |
| 2968 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2972 | return pending_remote_description_ ? pending_remote_description_.get() |
| 2973 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2976 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 2977 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2978 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 2982 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2983 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 2987 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2988 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 2992 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2993 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 2994 | } |
| 2995 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2996 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2997 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2998 | // Update stats here so that we have the most recent stats for tracks and |
| 2999 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3000 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3001 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3002 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3003 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3004 | for (auto transceiver : transceivers_) { |
| 3005 | transceiver->Stop(); |
| 3006 | } |
| 3007 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3008 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3009 | // The event log is used in the transport controller, which must be outlived |
| 3010 | // by the former. CreateOffer by the peer connection is implemented |
| 3011 | // asynchronously and if the peer connection is closed without resetting the |
| 3012 | // WebRTC session description factory, the session description factory would |
| 3013 | // call the transport controller. |
| 3014 | webrtc_session_desc_factory_.reset(); |
| 3015 | transport_controller_.reset(); |
| 3016 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3017 | network_thread()->Invoke<void>( |
| 3018 | RTC_FROM_HERE, |
| 3019 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3020 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3021 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3022 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3023 | call_.reset(); |
| 3024 | // The event log must outlive call (and any other object that uses it). |
| 3025 | event_log_.reset(); |
| 3026 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3029 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3030 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3031 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3032 | SetSessionDescriptionMsg* param = |
| 3033 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3034 | param->observer->OnSuccess(); |
| 3035 | delete param; |
| 3036 | break; |
| 3037 | } |
| 3038 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3039 | SetSessionDescriptionMsg* param = |
| 3040 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3041 | param->observer->OnFailure(param->error); |
| 3042 | delete param; |
| 3043 | break; |
| 3044 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3045 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3046 | CreateSessionDescriptionMsg* param = |
| 3047 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3048 | param->observer->OnFailure(param->error); |
| 3049 | delete param; |
| 3050 | break; |
| 3051 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3052 | case MSG_GETSTATS: { |
| 3053 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3054 | StatsReports reports; |
| 3055 | stats_->GetStats(param->track, &reports); |
| 3056 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3057 | delete param; |
| 3058 | break; |
| 3059 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3060 | case MSG_FREE_DATACHANNELS: { |
| 3061 | sctp_data_channels_to_free_.clear(); |
| 3062 | break; |
| 3063 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3064 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3065 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3066 | break; |
| 3067 | } |
| 3068 | } |
| 3069 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3070 | void PeerConnection::CreateAudioReceiver( |
| 3071 | MediaStreamInterface* stream, |
| 3072 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3073 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3074 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3075 | auto* audio_receiver = new AudioRtpReceiver( |
| 3076 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3077 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3078 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3079 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3080 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3081 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3082 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3085 | void PeerConnection::CreateVideoReceiver( |
| 3086 | MediaStreamInterface* stream, |
| 3087 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3088 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3089 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3090 | auto* video_receiver = new VideoRtpReceiver( |
| 3091 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3092 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3093 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3094 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3095 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3096 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3097 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3100 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3101 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3102 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3103 | const RtpSenderInfo& remote_sender_info) { |
| 3104 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3105 | if (!receiver) { |
| 3106 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3107 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3108 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3109 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3110 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3111 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3112 | } else { |
| 3113 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3114 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3115 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3118 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3119 | MediaStreamInterface* stream) { |
| 3120 | RTC_DCHECK(!IsClosed()); |
| 3121 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3122 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3123 | // We already have a sender for this track, so just change the stream_id |
| 3124 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3125 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3126 | return; |
| 3127 | } |
| 3128 | |
| 3129 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3130 | auto new_sender = |
| 3131 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3132 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3133 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3134 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3135 | // which will connect the sender to the underlying transport. This can |
| 3136 | // occur if a local session description that contains the ID of the sender |
| 3137 | // is set before AddStream is called. It can also occur if the local |
| 3138 | // session description is not changed and RemoveStream is called, and |
| 3139 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3140 | const RtpSenderInfo* sender_info = |
| 3141 | FindSenderInfo(local_audio_sender_infos_, stream->label(), track->id()); |
| 3142 | if (sender_info) { |
| 3143 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3148 | // indefinitely, when we have unified plan SDP. |
| 3149 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3150 | MediaStreamInterface* stream) { |
| 3151 | RTC_DCHECK(!IsClosed()); |
| 3152 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3153 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3154 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3155 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3156 | return; |
| 3157 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3158 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3162 | MediaStreamInterface* stream) { |
| 3163 | RTC_DCHECK(!IsClosed()); |
| 3164 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3165 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3166 | // We already have a sender for this track, so just change the stream_id |
| 3167 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3168 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3169 | return; |
| 3170 | } |
| 3171 | |
| 3172 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3173 | auto new_sender = |
| 3174 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3175 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3176 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3177 | const RtpSenderInfo* sender_info = |
| 3178 | FindSenderInfo(local_video_sender_infos_, stream->label(), track->id()); |
| 3179 | if (sender_info) { |
| 3180 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3185 | MediaStreamInterface* stream) { |
| 3186 | RTC_DCHECK(!IsClosed()); |
| 3187 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3188 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3189 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3190 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3191 | return; |
| 3192 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3193 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3196 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3197 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3198 | if (ice_connection_state_ == new_state) { |
| 3199 | return; |
| 3200 | } |
| 3201 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3202 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3203 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3204 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3205 | return; |
| 3206 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3207 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3208 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3209 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3210 | RTC_DCHECK(ice_connection_state_ != |
| 3211 | PeerConnectionInterface::kIceConnectionClosed); |
| 3212 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3213 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3214 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | void PeerConnection::OnIceGatheringChange( |
| 3218 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3219 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3220 | if (IsClosed()) { |
| 3221 | return; |
| 3222 | } |
| 3223 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3224 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3227 | void PeerConnection::OnIceCandidate( |
| 3228 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3229 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3230 | if (IsClosed()) { |
| 3231 | return; |
| 3232 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3233 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3236 | void PeerConnection::OnIceCandidatesRemoved( |
| 3237 | const std::vector<cricket::Candidate>& candidates) { |
| 3238 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3239 | if (IsClosed()) { |
| 3240 | return; |
| 3241 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3242 | observer_->OnIceCandidatesRemoved(candidates); |
| 3243 | } |
| 3244 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3245 | void PeerConnection::ChangeSignalingState( |
| 3246 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3247 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3248 | if (signaling_state_ == signaling_state) { |
| 3249 | return; |
| 3250 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3251 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3252 | << GetSignalingStateString(signaling_state_) |
| 3253 | << " New state: " |
| 3254 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3255 | signaling_state_ = signaling_state; |
| 3256 | if (signaling_state == kClosed) { |
| 3257 | ice_connection_state_ = kIceConnectionClosed; |
| 3258 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3259 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3260 | ice_gathering_state_ = kIceGatheringComplete; |
| 3261 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3262 | } |
| 3263 | } |
| 3264 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3267 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3268 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3269 | if (IsClosed()) { |
| 3270 | return; |
| 3271 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3272 | AddAudioTrack(track, stream); |
| 3273 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3274 | } |
| 3275 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3276 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3277 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3278 | if (IsClosed()) { |
| 3279 | return; |
| 3280 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3281 | RemoveAudioTrack(track, stream); |
| 3282 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3283 | } |
| 3284 | |
| 3285 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3286 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3287 | if (IsClosed()) { |
| 3288 | return; |
| 3289 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3290 | AddVideoTrack(track, stream); |
| 3291 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3295 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3296 | if (IsClosed()) { |
| 3297 | return; |
| 3298 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3299 | RemoveVideoTrack(track, stream); |
| 3300 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3301 | } |
| 3302 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3303 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3304 | SetSessionDescriptionObserver* observer) { |
| 3305 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3306 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3307 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3308 | } |
| 3309 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3310 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3311 | SetSessionDescriptionObserver* observer, |
| 3312 | const std::string& error) { |
| 3313 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3314 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3315 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3316 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3320 | CreateSessionDescriptionObserver* observer, |
| 3321 | const std::string& error) { |
| 3322 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3323 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3324 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3325 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3326 | } |
| 3327 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3328 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3329 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3330 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3331 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3332 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3333 | if (IsUnifiedPlan()) { |
| 3334 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3335 | } else { |
| 3336 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3337 | } |
| 3338 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3339 | // Intentionally unset the data channel type for RTP data channel with the |
| 3340 | // second condition. Otherwise the RTP data channels would be successfully |
| 3341 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3342 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3343 | // people won't try to use them. |
| 3344 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3345 | session_options->data_channel_type = data_channel_type(); |
| 3346 | } |
| 3347 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3348 | // Apply ICE restart flag and renomination flag. |
| 3349 | for (auto& options : session_options->media_description_options) { |
| 3350 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3351 | options.transport_options.enable_ice_renomination = |
| 3352 | configuration_.enable_ice_renomination; |
| 3353 | } |
| 3354 | |
| 3355 | session_options->rtcp_cname = rtcp_cname_; |
| 3356 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3357 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3361 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3362 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3363 | // Figure out transceiver directional preferences. |
| 3364 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3365 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3366 | |
| 3367 | // By default, generate sendrecv/recvonly m= sections. |
| 3368 | bool recv_audio = true; |
| 3369 | bool recv_video = true; |
| 3370 | |
| 3371 | // By default, only offer a new m= section if we have media to send with it. |
| 3372 | bool offer_new_audio_description = send_audio; |
| 3373 | bool offer_new_video_description = send_video; |
| 3374 | bool offer_new_data_description = HasDataChannels(); |
| 3375 | |
| 3376 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3377 | if (offer_answer_options.offer_to_receive_audio != |
| 3378 | RTCOfferAnswerOptions::kUndefined) { |
| 3379 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3380 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3381 | offer_new_audio_description || |
| 3382 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3383 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3384 | if (offer_answer_options.offer_to_receive_video != |
| 3385 | RTCOfferAnswerOptions::kUndefined) { |
| 3386 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3387 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3388 | offer_new_video_description || |
| 3389 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | rtc::Optional<size_t> audio_index; |
| 3393 | rtc::Optional<size_t> video_index; |
| 3394 | rtc::Optional<size_t> data_index; |
| 3395 | // If a current description exists, generate m= sections in the same order, |
| 3396 | // using the first audio/video/data section that appears and rejecting |
| 3397 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3398 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3399 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3400 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3401 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3402 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3403 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3404 | } |
| 3405 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3406 | // Add audio/video/data m= sections to the end if needed. |
| 3407 | if (!audio_index && offer_new_audio_description) { |
| 3408 | session_options->media_description_options.push_back( |
| 3409 | cricket::MediaDescriptionOptions( |
| 3410 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3411 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3412 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3413 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3414 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3415 | if (!video_index && offer_new_video_description) { |
| 3416 | session_options->media_description_options.push_back( |
| 3417 | cricket::MediaDescriptionOptions( |
| 3418 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3419 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3420 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3421 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3422 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3423 | if (!data_index && offer_new_data_description) { |
| 3424 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3425 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3426 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3430 | !audio_index ? nullptr |
| 3431 | : &session_options->media_description_options[*audio_index]; |
| 3432 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3433 | !video_index ? nullptr |
| 3434 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3435 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3436 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3437 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3441 | // and return a reference to it. |
| 3442 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3443 | // the RTP packet. |
| 3444 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3445 | RTC_DCHECK(used_mids); |
| 3446 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3447 | size_t i = 0; |
| 3448 | std::set<std::string>::iterator it; |
| 3449 | bool inserted; |
| 3450 | do { |
| 3451 | std::string mid = rtc::ToString(i++); |
| 3452 | auto insert_result = used_mids->insert(mid); |
| 3453 | it = insert_result.first; |
| 3454 | inserted = insert_result.second; |
| 3455 | } while (!inserted); |
| 3456 | return *it; |
| 3457 | } |
| 3458 | |
| 3459 | static cricket::MediaDescriptionOptions |
| 3460 | GetMediaDescriptionOptionsForTransceiver( |
| 3461 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3462 | transceiver, |
| 3463 | const std::string& mid) { |
| 3464 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3465 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3466 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3467 | // This behavior is specified in JSEP. The gist is that: |
| 3468 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3469 | // sendrecv. |
| 3470 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3471 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3472 | if (!transceiver->stopped() && |
| 3473 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3474 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3475 | cricket::SenderOptions sender_options; |
| 3476 | sender_options.track_id = transceiver->sender()->id(); |
| 3477 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3478 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3479 | // set in the RTP parameters when the transceiver was added. |
| 3480 | sender_options.num_sim_layers = 1; |
| 3481 | media_description_options.sender_options.push_back(sender_options); |
| 3482 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3483 | return media_description_options; |
| 3484 | } |
| 3485 | |
| 3486 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3487 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3488 | cricket::MediaSessionOptions* session_options) { |
| 3489 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3490 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3491 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3492 | const ContentInfos& local_contents = |
| 3493 | (local_description() ? local_description()->description()->contents() |
| 3494 | : ContentInfos()); |
| 3495 | const ContentInfos& remote_contents = |
| 3496 | (remote_description() ? remote_description()->description()->contents() |
| 3497 | : ContentInfos()); |
| 3498 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3499 | // slots first. |
| 3500 | std::queue<size_t> recycleable_mline_indices; |
| 3501 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3502 | // offer so that new, unique MIDs are generated. |
| 3503 | std::set<std::string> used_mids = seen_mids_; |
| 3504 | // First, go through each media section that exists in either the local or |
| 3505 | // remote description and generate a media section in this offer for the |
| 3506 | // associated transceiver. If a media section can be recycled, generate a |
| 3507 | // default, rejected media section here that can be later overwritten. |
| 3508 | for (size_t i = 0; |
| 3509 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3510 | // Either |local_content| or |remote_content| is non-null. |
| 3511 | const ContentInfo* local_content = |
| 3512 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3513 | const ContentInfo* remote_content = |
| 3514 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3515 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3516 | (remote_content && remote_content->rejected); |
| 3517 | const std::string& mid = |
| 3518 | (local_content ? local_content->name : remote_content->name); |
| 3519 | cricket::MediaType media_type = |
| 3520 | (local_content ? local_content->media_description()->type() |
| 3521 | : remote_content->media_description()->type()); |
| 3522 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3523 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3524 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3525 | RTC_CHECK(transceiver); |
| 3526 | // A media section is considered eligible for recycling if it is marked as |
| 3527 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3528 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3529 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3530 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3531 | RtpTransceiverDirection::kInactive, |
| 3532 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3533 | recycleable_mline_indices.push(i); |
| 3534 | } else { |
| 3535 | session_options->media_description_options.push_back( |
| 3536 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3537 | // CreateOffer shouldn't really cause any state changes in |
| 3538 | // PeerConnection, but we need a way to match new transceivers to new |
| 3539 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3540 | // by recording the index of the media section generated for the |
| 3541 | // transceiver in the offer. |
| 3542 | transceiver->internal()->set_mline_index(i); |
| 3543 | } |
| 3544 | } else { |
| 3545 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3546 | RTC_CHECK(GetDataMid()); |
| 3547 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3548 | session_options->media_description_options.push_back( |
| 3549 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3550 | } else { |
| 3551 | session_options->media_description_options.push_back( |
| 3552 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3553 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3554 | } |
| 3555 | } |
| 3556 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3557 | // and not associated). Reuse media sections marked as recyclable first, |
| 3558 | // otherwise append to the end of the offer. New media sections should be |
| 3559 | // added in the order they were added to the PeerConnection. |
| 3560 | for (auto transceiver : transceivers_) { |
| 3561 | if (transceiver->mid() || transceiver->stopped()) { |
| 3562 | continue; |
| 3563 | } |
| 3564 | size_t mline_index; |
| 3565 | if (!recycleable_mline_indices.empty()) { |
| 3566 | mline_index = recycleable_mline_indices.front(); |
| 3567 | recycleable_mline_indices.pop(); |
| 3568 | session_options->media_description_options[mline_index] = |
| 3569 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3570 | AllocateMid(&used_mids)); |
| 3571 | } else { |
| 3572 | mline_index = session_options->media_description_options.size(); |
| 3573 | session_options->media_description_options.push_back( |
| 3574 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3575 | AllocateMid(&used_mids))); |
| 3576 | } |
| 3577 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3578 | transceiver->internal()->set_mline_index(mline_index); |
| 3579 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3580 | // Lastly, add a m-section if we have local data channels and an m section |
| 3581 | // does not already exist. |
| 3582 | if (!GetDataMid() && HasDataChannels()) { |
| 3583 | session_options->media_description_options.push_back( |
| 3584 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3585 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | void PeerConnection::GetOptionsForAnswer( |
| 3589 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3590 | cricket::MediaSessionOptions* session_options) { |
| 3591 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3592 | |
| 3593 | if (IsUnifiedPlan()) { |
| 3594 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3595 | } else { |
| 3596 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3597 | } |
| 3598 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3599 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3600 | // the RTP data channels would be successfully negotiated by default and the |
| 3601 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3602 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3603 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3604 | session_options->data_channel_type = data_channel_type(); |
| 3605 | } |
| 3606 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3607 | // Apply ICE renomination flag. |
| 3608 | for (auto& options : session_options->media_description_options) { |
| 3609 | options.transport_options.enable_ice_renomination = |
| 3610 | configuration_.enable_ice_renomination; |
| 3611 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3612 | |
| 3613 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3614 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3615 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3616 | } |
| 3617 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3618 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3619 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3620 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3621 | // Figure out transceiver directional preferences. |
| 3622 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3623 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3624 | |
| 3625 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3626 | // restricted by the direction in the offer. |
| 3627 | bool recv_audio = true; |
| 3628 | bool recv_video = true; |
| 3629 | |
| 3630 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3631 | if (offer_answer_options.offer_to_receive_audio != |
| 3632 | RTCOfferAnswerOptions::kUndefined) { |
| 3633 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3634 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3635 | if (offer_answer_options.offer_to_receive_video != |
| 3636 | RTCOfferAnswerOptions::kUndefined) { |
| 3637 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | rtc::Optional<size_t> audio_index; |
| 3641 | rtc::Optional<size_t> video_index; |
| 3642 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3643 | |
| 3644 | // Generate m= sections that match those in the offer. |
| 3645 | // Note that mediasession.cc will handle intersection our preferred |
| 3646 | // direction with the offered direction. |
| 3647 | GenerateMediaDescriptionOptions( |
| 3648 | remote_description(), |
| 3649 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3650 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3651 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3652 | |
| 3653 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3654 | !audio_index ? nullptr |
| 3655 | : &session_options->media_description_options[*audio_index]; |
| 3656 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3657 | !video_index ? nullptr |
| 3658 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3659 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3660 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3661 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3662 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3663 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3664 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3665 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3666 | cricket::MediaSessionOptions* session_options) { |
| 3667 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3668 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3669 | RTC_DCHECK(remote_description()); |
| 3670 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3671 | for (const ContentInfo& content : |
| 3672 | remote_description()->description()->contents()) { |
| 3673 | cricket::MediaType media_type = content.media_description()->type(); |
| 3674 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3675 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3676 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3677 | RTC_CHECK(transceiver); |
| 3678 | session_options->media_description_options.push_back( |
| 3679 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3680 | } else { |
| 3681 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3682 | // Reject all data sections if data channels are disabled. |
| 3683 | // Reject a data section if it has already been rejected. |
| 3684 | // Reject all data sections except for the first one. |
| 3685 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3686 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3687 | session_options->media_description_options.push_back( |
| 3688 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3689 | } else { |
| 3690 | session_options->media_description_options.push_back( |
| 3691 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3692 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3693 | } |
| 3694 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3695 | } |
| 3696 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3697 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3698 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3699 | RtpTransceiverDirection audio_direction, |
| 3700 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3701 | rtc::Optional<size_t>* audio_index, |
| 3702 | rtc::Optional<size_t>* video_index, |
| 3703 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3704 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3705 | for (const cricket::ContentInfo& content : |
| 3706 | session_desc->description()->contents()) { |
| 3707 | if (IsAudioContent(&content)) { |
| 3708 | // If we already have an audio m= section, reject this extra one. |
| 3709 | if (*audio_index) { |
| 3710 | session_options->media_description_options.push_back( |
| 3711 | cricket::MediaDescriptionOptions( |
| 3712 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3713 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3714 | } else { |
| 3715 | session_options->media_description_options.push_back( |
| 3716 | cricket::MediaDescriptionOptions( |
| 3717 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3718 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3719 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3720 | } |
| 3721 | } else if (IsVideoContent(&content)) { |
| 3722 | // If we already have an video m= section, reject this extra one. |
| 3723 | if (*video_index) { |
| 3724 | session_options->media_description_options.push_back( |
| 3725 | cricket::MediaDescriptionOptions( |
| 3726 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3727 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3728 | } else { |
| 3729 | session_options->media_description_options.push_back( |
| 3730 | cricket::MediaDescriptionOptions( |
| 3731 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3732 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3733 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3734 | } |
| 3735 | } else { |
| 3736 | RTC_DCHECK(IsDataContent(&content)); |
| 3737 | // If we already have an data m= section, reject this extra one. |
| 3738 | if (*data_index) { |
| 3739 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3740 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3741 | } else { |
| 3742 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3743 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3744 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3745 | } |
| 3746 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3747 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3750 | cricket::MediaDescriptionOptions |
| 3751 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3752 | const std::string& mid) const { |
| 3753 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3754 | // expect sendrecv. |
| 3755 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3756 | RtpTransceiverDirection::kSendRecv, |
| 3757 | /*stopped=*/false); |
| 3758 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3759 | return options; |
| 3760 | } |
| 3761 | |
| 3762 | cricket::MediaDescriptionOptions |
| 3763 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 3764 | const std::string& mid) const { |
| 3765 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3766 | RtpTransceiverDirection::kInactive, |
| 3767 | /*stopped=*/true); |
| 3768 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3769 | return options; |
| 3770 | } |
| 3771 | |
| 3772 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 3773 | switch (data_channel_type_) { |
| 3774 | case cricket::DCT_RTP: |
| 3775 | if (!rtp_data_channel_) { |
| 3776 | return rtc::nullopt; |
| 3777 | } |
| 3778 | return rtp_data_channel_->content_name(); |
| 3779 | case cricket::DCT_SCTP: |
| 3780 | return sctp_content_name_; |
| 3781 | default: |
| 3782 | return rtc::nullopt; |
| 3783 | } |
| 3784 | } |
| 3785 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3786 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 3787 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 3788 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3789 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3790 | } |
| 3791 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3792 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3793 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3794 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3795 | cricket::MediaType media_type, |
| 3796 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3797 | std::vector<RtpSenderInfo>* current_senders = |
| 3798 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3799 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3800 | // Find removed senders. I.e., senders where the sender id or ssrc don't match |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3801 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3802 | for (auto sender_it = current_senders->begin(); |
| 3803 | sender_it != current_senders->end(); |
| 3804 | /* incremented manually */) { |
| 3805 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3806 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3807 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3808 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3809 | // If this is a default track, and we still need it, don't remove it. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3810 | if ((info.stream_label == kDefaultStreamLabel && default_sender_needed) || |
| 3811 | sender_exists) { |
| 3812 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3813 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3814 | OnRemoteSenderRemoved(info, media_type); |
| 3815 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3816 | } |
| 3817 | } |
| 3818 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3819 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3820 | for (const cricket::StreamParams& params : streams) { |
| 3821 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3822 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3823 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3824 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3825 | uint32_t ssrc = params.first_ssrc(); |
| 3826 | |
| 3827 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3828 | remote_streams_->find(stream_label); |
| 3829 | if (!stream) { |
| 3830 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3831 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3832 | MediaStream::Create(stream_label)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3833 | remote_streams_->AddStream(stream); |
| 3834 | new_streams->AddStream(stream); |
| 3835 | } |
| 3836 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3837 | const RtpSenderInfo* sender_info = |
| 3838 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 3839 | if (!sender_info) { |
| 3840 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 3841 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3842 | } |
| 3843 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3844 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3845 | // Add default sender if necessary. |
| 3846 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3847 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 3848 | remote_streams_->find(kDefaultStreamLabel); |
| 3849 | if (!default_stream) { |
| 3850 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3851 | default_stream = MediaStreamProxy::Create( |
| 3852 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3853 | remote_streams_->AddStream(default_stream); |
| 3854 | new_streams->AddStream(default_stream); |
| 3855 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3856 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 3857 | ? kDefaultAudioSenderId |
| 3858 | : kDefaultVideoSenderId; |
| 3859 | const RtpSenderInfo* default_sender_info = FindSenderInfo( |
| 3860 | *current_senders, kDefaultStreamLabel, default_sender_id); |
| 3861 | if (!default_sender_info) { |
| 3862 | current_senders->push_back( |
| 3863 | RtpSenderInfo(kDefaultStreamLabel, default_sender_id, 0)); |
| 3864 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3865 | } |
| 3866 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3869 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 3870 | cricket::MediaType media_type) { |
| 3871 | MediaStreamInterface* stream = |
| 3872 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3873 | |
| 3874 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3875 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3876 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3877 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3878 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3879 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3883 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 3884 | cricket::MediaType media_type) { |
| 3885 | MediaStreamInterface* stream = |
| 3886 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3887 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3888 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3889 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3890 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 3891 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3892 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3893 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3894 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3895 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3896 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3897 | } |
| 3898 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3899 | // Stopping or destroying a VideoRtpReceiver will end the |
| 3900 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3901 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3902 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3903 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3904 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3905 | // There's no guarantee the track is still available, e.g. the track may |
| 3906 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3907 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3908 | } |
| 3909 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 3910 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3911 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3912 | if (receiver) { |
| 3913 | observer_->OnRemoveTrack(receiver); |
| 3914 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3915 | } |
| 3916 | |
| 3917 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 3918 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 3919 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 3920 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 3921 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 3922 | streams_to_remove.push_back(stream); |
| 3923 | } |
| 3924 | } |
| 3925 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3926 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3927 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3928 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3929 | } |
| 3930 | } |
| 3931 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3932 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3933 | const std::vector<cricket::StreamParams>& streams, |
| 3934 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3935 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3936 | |
| 3937 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 3938 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3939 | for (auto sender_it = current_senders->begin(); |
| 3940 | sender_it != current_senders->end(); |
| 3941 | /* incremented manually */) { |
| 3942 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3943 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3944 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3945 | if (!params || params->id != info.sender_id || |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3946 | params->sync_label != info.stream_label) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3947 | OnLocalSenderRemoved(info, media_type); |
| 3948 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3949 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3950 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3951 | } |
| 3952 | } |
| 3953 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3954 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3955 | for (const cricket::StreamParams& params : streams) { |
| 3956 | // The sync_label is the MediaStream label and the |stream.id| is the |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3957 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3958 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3959 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3960 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3961 | const RtpSenderInfo* sender_info = |
| 3962 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 3963 | if (!sender_info) { |
| 3964 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 3965 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | } |
| 3969 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3970 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 3971 | cricket::MediaType media_type) { |
| 3972 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3973 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3974 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 3975 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3976 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3977 | return; |
| 3978 | } |
| 3979 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3980 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3981 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 3982 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3983 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3984 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3985 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3986 | sender->internal()->set_stream_id(sender_info.stream_label); |
| 3987 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3990 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 3991 | cricket::MediaType media_type) { |
| 3992 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3993 | if (!sender) { |
| 3994 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3995 | // SessionDescriptions has been renegotiated. |
| 3996 | return; |
| 3997 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 3998 | |
| 3999 | // A sender has been removed from the SessionDescription but it's still |
| 4000 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4001 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4002 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4003 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4004 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4005 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4006 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4007 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4008 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4012 | const cricket::StreamParamsVec& streams) { |
| 4013 | std::vector<std::string> existing_channels; |
| 4014 | |
| 4015 | // Find new and active data channels. |
| 4016 | for (const cricket::StreamParams& params : streams) { |
| 4017 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4018 | // we use the same naming of data channels as we do for |
| 4019 | // MediaStreams and Tracks. |
| 4020 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4021 | // track label is the same as |streamid|. |
| 4022 | const std::string& channel_label = params.sync_label; |
| 4023 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4024 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4025 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4026 | continue; |
| 4027 | } |
| 4028 | // Set the SSRC the data channel should use for sending. |
| 4029 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4030 | existing_channels.push_back(data_channel_it->first); |
| 4031 | } |
| 4032 | |
| 4033 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4034 | } |
| 4035 | |
| 4036 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4037 | const cricket::StreamParamsVec& streams) { |
| 4038 | std::vector<std::string> existing_channels; |
| 4039 | |
| 4040 | // Find new and active data channels. |
| 4041 | for (const cricket::StreamParams& params : streams) { |
| 4042 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4043 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 4044 | std::string label = params.sync_label.empty() |
| 4045 | ? rtc::ToString(params.first_ssrc()) |
| 4046 | : params.sync_label; |
| 4047 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4048 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4049 | // This is a new data channel. |
| 4050 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4051 | } else { |
| 4052 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4053 | } |
| 4054 | existing_channels.push_back(label); |
| 4055 | } |
| 4056 | |
| 4057 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4058 | } |
| 4059 | |
| 4060 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4061 | const std::vector<std::string>& active_channels, |
| 4062 | bool is_local_update) { |
| 4063 | auto it = rtp_data_channels_.begin(); |
| 4064 | while (it != rtp_data_channels_.end()) { |
| 4065 | DataChannel* data_channel = it->second; |
| 4066 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4067 | data_channel->label()) != active_channels.end()) { |
| 4068 | ++it; |
| 4069 | continue; |
| 4070 | } |
| 4071 | |
| 4072 | if (is_local_update) { |
| 4073 | data_channel->SetSendSsrc(0); |
| 4074 | } else { |
| 4075 | data_channel->RemotePeerRequestClose(); |
| 4076 | } |
| 4077 | |
| 4078 | if (data_channel->state() == DataChannel::kClosed) { |
| 4079 | rtp_data_channels_.erase(it); |
| 4080 | it = rtp_data_channels_.begin(); |
| 4081 | } else { |
| 4082 | ++it; |
| 4083 | } |
| 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4088 | uint32_t remote_ssrc) { |
| 4089 | rtc::scoped_refptr<DataChannel> channel( |
| 4090 | InternalCreateDataChannel(label, nullptr)); |
| 4091 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4092 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4093 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4094 | return; |
| 4095 | } |
| 4096 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4097 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4098 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4099 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4103 | const std::string& label, |
| 4104 | const InternalDataChannelInit* config) { |
| 4105 | if (IsClosed()) { |
| 4106 | return nullptr; |
| 4107 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4108 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4109 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4110 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4111 | return nullptr; |
| 4112 | } |
| 4113 | InternalDataChannelInit new_config = |
| 4114 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4115 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4116 | if (new_config.id < 0) { |
| 4117 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4118 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4119 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4120 | RTC_LOG(LS_ERROR) |
| 4121 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4122 | return nullptr; |
| 4123 | } |
| 4124 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4125 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4126 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4127 | return nullptr; |
| 4128 | } |
| 4129 | } |
| 4130 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4131 | rtc::scoped_refptr<DataChannel> channel( |
| 4132 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4133 | if (!channel) { |
| 4134 | sid_allocator_.ReleaseSid(new_config.id); |
| 4135 | return nullptr; |
| 4136 | } |
| 4137 | |
| 4138 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4139 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4140 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4141 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4142 | return nullptr; |
| 4143 | } |
| 4144 | rtp_data_channels_[channel->label()] = channel; |
| 4145 | } else { |
| 4146 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4147 | sctp_data_channels_.push_back(channel); |
| 4148 | channel->SignalClosed.connect(this, |
| 4149 | &PeerConnection::OnSctpDataChannelClosed); |
| 4150 | } |
| 4151 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4152 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4153 | return channel; |
| 4154 | } |
| 4155 | |
| 4156 | bool PeerConnection::HasDataChannels() const { |
| 4157 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4158 | } |
| 4159 | |
| 4160 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4161 | for (const auto& channel : sctp_data_channels_) { |
| 4162 | if (channel->id() < 0) { |
| 4163 | int sid; |
| 4164 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4165 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4166 | continue; |
| 4167 | } |
| 4168 | channel->SetSctpSid(sid); |
| 4169 | } |
| 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4174 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4175 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4176 | ++it) { |
| 4177 | if (it->get() == channel) { |
| 4178 | if (channel->id() >= 0) { |
| 4179 | sid_allocator_.ReleaseSid(channel->id()); |
| 4180 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4181 | // Since this method is triggered by a signal from the DataChannel, |
| 4182 | // we can't free it directly here; we need to free it asynchronously. |
| 4183 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4184 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4185 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4186 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4187 | return; |
| 4188 | } |
| 4189 | } |
| 4190 | } |
| 4191 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4192 | void PeerConnection::OnDataChannelDestroyed() { |
| 4193 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4194 | // DataChannel may callback to us and try to modify the list. |
| 4195 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4196 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4197 | for (const auto& kv : temp_rtp_dcs) { |
| 4198 | kv.second->OnTransportChannelDestroyed(); |
| 4199 | } |
| 4200 | |
| 4201 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4202 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4203 | for (const auto& channel : temp_sctp_dcs) { |
| 4204 | channel->OnTransportChannelDestroyed(); |
| 4205 | } |
| 4206 | } |
| 4207 | |
| 4208 | void PeerConnection::OnDataChannelOpenMessage( |
| 4209 | const std::string& label, |
| 4210 | const InternalDataChannelInit& config) { |
| 4211 | rtc::scoped_refptr<DataChannel> channel( |
| 4212 | InternalCreateDataChannel(label, &config)); |
| 4213 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4214 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4215 | return; |
| 4216 | } |
| 4217 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4218 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4219 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4220 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4221 | } |
| 4222 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4223 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4224 | PeerConnection::GetAudioTransceiver() const { |
| 4225 | // This method only works with Plan B SDP, where there is a single |
| 4226 | // audio/video transceiver. |
| 4227 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4228 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4229 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4230 | return transceiver; |
| 4231 | } |
| 4232 | } |
| 4233 | RTC_NOTREACHED(); |
| 4234 | return nullptr; |
| 4235 | } |
| 4236 | |
| 4237 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4238 | PeerConnection::GetVideoTransceiver() const { |
| 4239 | // This method only works with Plan B SDP, where there is a single |
| 4240 | // audio/video transceiver. |
| 4241 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4242 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4243 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4244 | return transceiver; |
| 4245 | } |
| 4246 | } |
| 4247 | RTC_NOTREACHED(); |
| 4248 | return nullptr; |
| 4249 | } |
| 4250 | |
| 4251 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4252 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4253 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4254 | switch (type) { |
| 4255 | case cricket::MEDIA_TYPE_AUDIO: |
| 4256 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4257 | case cricket::MEDIA_TYPE_VIDEO: |
| 4258 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4259 | case cricket::MEDIA_TYPE_DATA: |
| 4260 | return false; |
| 4261 | } |
| 4262 | RTC_NOTREACHED(); |
| 4263 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4266 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4267 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4268 | for (auto transceiver : transceivers_) { |
| 4269 | for (auto sender : transceiver->internal()->senders()) { |
| 4270 | if (sender->track() == track) { |
| 4271 | return sender; |
| 4272 | } |
| 4273 | } |
| 4274 | } |
| 4275 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4276 | } |
| 4277 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4278 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4279 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4280 | for (auto transceiver : transceivers_) { |
| 4281 | for (auto sender : transceiver->internal()->senders()) { |
| 4282 | if (sender->id() == sender_id) { |
| 4283 | return sender; |
| 4284 | } |
| 4285 | } |
| 4286 | } |
| 4287 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4290 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4291 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4292 | for (auto transceiver : transceivers_) { |
| 4293 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4294 | if (receiver->id() == receiver_id) { |
| 4295 | return receiver; |
| 4296 | } |
| 4297 | } |
| 4298 | } |
| 4299 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4300 | } |
| 4301 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4302 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4303 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4304 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4305 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4306 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4307 | ? &remote_audio_sender_infos_ |
| 4308 | : &remote_video_sender_infos_; |
| 4309 | } |
| 4310 | |
| 4311 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4312 | cricket::MediaType media_type) { |
| 4313 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4314 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4315 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4316 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4317 | } |
| 4318 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4319 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4320 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4321 | const std::string& stream_label, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4322 | const std::string sender_id) const { |
| 4323 | for (const RtpSenderInfo& sender_info : infos) { |
| 4324 | if (sender_info.stream_label == stream_label && |
| 4325 | sender_info.sender_id == sender_id) { |
| 4326 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4327 | } |
| 4328 | } |
| 4329 | return nullptr; |
| 4330 | } |
| 4331 | |
| 4332 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4333 | for (const auto& channel : sctp_data_channels_) { |
| 4334 | if (channel->id() == sid) { |
| 4335 | return channel; |
| 4336 | } |
| 4337 | } |
| 4338 | return nullptr; |
| 4339 | } |
| 4340 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4341 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4342 | const RTCConfiguration& configuration) { |
| 4343 | cricket::ServerAddresses stun_servers; |
| 4344 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4345 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4346 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4347 | return false; |
| 4348 | } |
| 4349 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4350 | port_allocator_->Initialize(); |
| 4351 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4352 | // To handle both internal and externally created port allocator, we will |
| 4353 | // enable BUNDLE here. |
| 4354 | int portallocator_flags = port_allocator_->flags(); |
| 4355 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4356 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4357 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4358 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4359 | // by experiment. |
| 4360 | if (configuration.disable_ipv6) { |
| 4361 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4362 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4363 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4364 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4365 | } |
| 4366 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4367 | if (configuration.disable_ipv6_on_wifi) { |
| 4368 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4369 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4370 | } |
| 4371 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4372 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4373 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4374 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4377 | if (configuration.candidate_network_policy == |
| 4378 | kCandidateNetworkPolicyLowCost) { |
| 4379 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4380 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4381 | } |
| 4382 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4383 | if (configuration.disable_link_local_networks) { |
| 4384 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4385 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4386 | } |
| 4387 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4388 | port_allocator_->set_flags(portallocator_flags); |
| 4389 | // No step delay is used while allocating ports. |
| 4390 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4391 | port_allocator_->set_candidate_filter( |
| 4392 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4393 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4394 | |
| 4395 | // Call this last since it may create pooled allocator sessions using the |
| 4396 | // properties set above. |
| 4397 | port_allocator_->SetConfiguration(stun_servers, turn_servers, |
Honghai Zhang | b9e7b4a | 2016-06-30 20:52:02 -0700 | [diff] [blame] | 4398 | configuration.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4399 | configuration.prune_turn_ports, |
| 4400 | configuration.turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4401 | return true; |
| 4402 | } |
| 4403 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4404 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4405 | const cricket::ServerAddresses& stun_servers, |
| 4406 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4407 | IceTransportsType type, |
| 4408 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4409 | bool prune_turn_ports, |
| 4410 | webrtc::TurnCustomizer* turn_customizer) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4411 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4412 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4413 | // Call this last since it may create pooled allocator sessions using the |
| 4414 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4415 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4416 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
| 4417 | turn_customizer); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4420 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4421 | return factory_->channel_manager(); |
| 4422 | } |
| 4423 | |
| 4424 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4425 | return uma_observer_; |
| 4426 | } |
| 4427 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4428 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4429 | std::unique_ptr<RtcEventLogOutput> output, |
| 4430 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4431 | if (!event_log_) { |
| 4432 | return false; |
| 4433 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4434 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4438 | if (event_log_) { |
| 4439 | event_log_->StopLogging(); |
| 4440 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4441 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4442 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4443 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4444 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4445 | for (auto transceiver : transceivers_) { |
| 4446 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4447 | if (channel && channel->content_name() == content_name) { |
| 4448 | return channel; |
| 4449 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4450 | } |
| 4451 | if (rtp_data_channel() && |
| 4452 | rtp_data_channel()->content_name() == content_name) { |
| 4453 | return rtp_data_channel(); |
| 4454 | } |
| 4455 | return nullptr; |
| 4456 | } |
| 4457 | |
| 4458 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4459 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4460 | RTC_LOG(LS_INFO) |
| 4461 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4462 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4463 | return false; |
| 4464 | } |
| 4465 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4466 | RTC_LOG(LS_INFO) << "Non-rejected SCTP m= section is needed to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4467 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4468 | return false; |
| 4469 | } |
| 4470 | |
| 4471 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 4472 | } |
| 4473 | |
| 4474 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4475 | rtc::SSLRole* role) { |
| 4476 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4477 | RTC_LOG(LS_INFO) |
| 4478 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4479 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4480 | return false; |
| 4481 | } |
| 4482 | |
| 4483 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 4484 | role); |
| 4485 | } |
| 4486 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4487 | void PeerConnection::SetSessionError(SessionError error, |
| 4488 | const std::string& error_desc) { |
| 4489 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4490 | if (error != session_error_) { |
| 4491 | session_error_ = error; |
| 4492 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4493 | } |
| 4494 | } |
| 4495 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4496 | RTCError PeerConnection::UpdateSessionState(SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4497 | cricket::ContentSource source) { |
| 4498 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4499 | |
| 4500 | // If there's already a pending error then no state transition should happen. |
| 4501 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4502 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4503 | |
| 4504 | // If this is an answer then we know whether to BUNDLE or not. If both the |
| 4505 | // local and remote side have agreed to BUNDLE, go ahead and enable it. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4506 | if (type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4507 | const cricket::ContentGroup* local_bundle = |
| 4508 | local_description()->description()->GetGroupByName( |
| 4509 | cricket::GROUP_TYPE_BUNDLE); |
| 4510 | const cricket::ContentGroup* remote_bundle = |
| 4511 | remote_description()->description()->GetGroupByName( |
| 4512 | cricket::GROUP_TYPE_BUNDLE); |
| 4513 | if (local_bundle && remote_bundle) { |
| 4514 | // The answerer decides the transport to bundle on. |
| 4515 | const cricket::ContentGroup* answer_bundle = |
| 4516 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 4517 | if (!EnableBundle(*answer_bundle)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4518 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4519 | kEnableBundleFailed); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4520 | } |
| 4521 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4522 | } |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4523 | |
| 4524 | // Only push down the transport description after potentially enabling BUNDLE; |
| 4525 | // we don't want to push down a description on a transport about to be |
| 4526 | // destroyed. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4527 | RTCError error = PushdownTransportDescription(source, type); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4528 | if (!error.ok()) { |
| 4529 | return error; |
| 4530 | } |
| 4531 | |
| 4532 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4533 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4534 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
| 4537 | // Update the signaling state according to the specified state machine (see |
| 4538 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4539 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4540 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4541 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4542 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4543 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4544 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4545 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4546 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4547 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4548 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4549 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4550 | } |
| 4551 | |
| 4552 | // Update internal objects according to the session description's media |
| 4553 | // descriptions. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4554 | error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4555 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame^] | 4556 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4557 | } |
| 4558 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4559 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4562 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4563 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4564 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4565 | const SessionDescriptionInterface* sdesc = |
| 4566 | (source == cricket::CS_LOCAL ? local_description() |
| 4567 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4568 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4569 | |
| 4570 | // Push down the new SDP media section for each audio/video transceiver. |
| 4571 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4572 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4573 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4574 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4575 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4576 | continue; |
| 4577 | } |
| 4578 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4579 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4580 | if (!content_desc) { |
| 4581 | continue; |
| 4582 | } |
| 4583 | std::string error; |
| 4584 | bool success = |
| 4585 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4586 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4587 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4588 | if (!success) { |
| 4589 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4590 | } |
| 4591 | } |
| 4592 | |
| 4593 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4594 | if (rtp_data_channel_) { |
| 4595 | const ContentInfo* data_content = |
| 4596 | cricket::GetFirstDataContent(sdesc->description()); |
| 4597 | if (data_content && !data_content->rejected) { |
| 4598 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4599 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4600 | if (data_desc) { |
| 4601 | std::string error; |
| 4602 | bool success = |
| 4603 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4604 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4605 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4606 | &error); |
| 4607 | if (!success) { |
| 4608 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4609 | std::move(error)); |
| 4610 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4611 | } |
| 4612 | } |
| 4613 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4614 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4615 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4616 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4617 | if (sctp_transport_ && local_description() && remote_description() && |
| 4618 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4619 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4620 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4621 | RTC_FROM_HERE, |
| 4622 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4623 | if (!success) { |
| 4624 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4625 | "Failed to push down SCTP parameters."); |
| 4626 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4627 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4628 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4629 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4633 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4634 | RTC_DCHECK(local_description()); |
| 4635 | RTC_DCHECK(remote_description()); |
| 4636 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4637 | // When we support "max-message-size", that would also be pushed down here. |
| 4638 | return sctp_transport_->Start( |
| 4639 | GetSctpPort(local_description()->description()), |
| 4640 | GetSctpPort(remote_description()->description())); |
| 4641 | } |
| 4642 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4643 | RTCError PeerConnection::PushdownTransportDescription( |
| 4644 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4645 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4646 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4647 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4648 | const SessionDescriptionInterface* sdesc = |
| 4649 | (source == cricket::CS_LOCAL ? local_description() |
| 4650 | : remote_description()); |
| 4651 | RTC_DCHECK(sdesc); |
| 4652 | for (const cricket::TransportInfo& tinfo : |
| 4653 | sdesc->description()->transport_infos()) { |
| 4654 | std::string error; |
| 4655 | bool success; |
| 4656 | if (source == cricket::CS_LOCAL) { |
| 4657 | success = transport_controller_->SetLocalTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4658 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4659 | } else { |
| 4660 | success = transport_controller_->SetRemoteTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4661 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4662 | } |
| 4663 | if (!success) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4664 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4665 | "Failed to push down transport description for " + |
| 4666 | tinfo.content_name + ": " + error); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4667 | } |
| 4668 | } |
| 4669 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4670 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | bool PeerConnection::GetTransportDescription( |
| 4674 | const SessionDescription* description, |
| 4675 | const std::string& content_name, |
| 4676 | cricket::TransportDescription* tdesc) { |
| 4677 | if (!description || !tdesc) { |
| 4678 | return false; |
| 4679 | } |
| 4680 | const TransportInfo* transport_info = |
| 4681 | description->GetTransportInfoByName(content_name); |
| 4682 | if (!transport_info) { |
| 4683 | return false; |
| 4684 | } |
| 4685 | *tdesc = transport_info->description; |
| 4686 | return true; |
| 4687 | } |
| 4688 | |
| 4689 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 4690 | const std::string* first_content_name = bundle.FirstContentName(); |
| 4691 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4692 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4693 | return false; |
| 4694 | } |
| 4695 | const std::string& transport_name = *first_content_name; |
| 4696 | |
| 4697 | auto maybe_set_transport = [this, bundle, |
| 4698 | transport_name](cricket::BaseChannel* ch) { |
| 4699 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4700 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | std::string old_transport_name = ch->transport_name(); |
| 4704 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4705 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 4706 | << " on " << transport_name << "."; |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4707 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4711 | transport_controller_->CreateDtlsTransport( |
| 4712 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4713 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 4714 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4715 | if (need_rtcp) { |
| 4716 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4717 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4718 | } |
| 4719 | |
| 4720 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4721 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 4722 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4723 | transport_controller_->DestroyDtlsTransport( |
| 4724 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4725 | // If the channel needs rtcp, it means that the channel used to have a |
| 4726 | // rtcp transport which needs to be deleted now. |
| 4727 | if (need_rtcp) { |
| 4728 | transport_controller_->DestroyDtlsTransport( |
| 4729 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4730 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4731 | }; |
| 4732 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4733 | for (auto transceiver : transceivers_) { |
| 4734 | maybe_set_transport(transceiver->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4735 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4736 | maybe_set_transport(rtp_data_channel_); |
| 4737 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4738 | // For SCTP, transport creation/deletion happens here instead of in the |
| 4739 | // object itself. |
| 4740 | if (sctp_transport_) { |
| 4741 | RTC_DCHECK(sctp_transport_name_); |
| 4742 | RTC_DCHECK(sctp_content_name_); |
| 4743 | if (transport_name != *sctp_transport_name_ && |
| 4744 | bundle.HasContentName(*sctp_content_name_)) { |
| 4745 | network_thread()->Invoke<void>( |
| 4746 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 4747 | transport_name)); |
| 4748 | } |
| 4749 | } |
| 4750 | |
| 4751 | return true; |
| 4752 | } |
| 4753 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4754 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4755 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4756 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4757 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4758 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4759 | switch (config.continual_gathering_policy) { |
| 4760 | case PeerConnectionInterface::GATHER_ONCE: |
| 4761 | gathering_policy = cricket::GATHER_ONCE; |
| 4762 | break; |
| 4763 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4764 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4765 | break; |
| 4766 | default: |
| 4767 | RTC_NOTREACHED(); |
| 4768 | gathering_policy = cricket::GATHER_ONCE; |
| 4769 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4770 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4771 | cricket::IceConfig ice_config; |
| 4772 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
| 4773 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4774 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4775 | ice_config.backup_connection_ping_interval = |
| 4776 | config.ice_backup_candidate_pair_ping_interval; |
| 4777 | ice_config.continual_gathering_policy = gathering_policy; |
| 4778 | ice_config.presume_writable_when_fully_relayed = |
| 4779 | config.presume_writable_when_fully_relayed; |
| 4780 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
| 4781 | ice_config.regather_all_networks_interval_range = |
| 4782 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4783 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4784 | return ice_config; |
| 4785 | } |
| 4786 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4787 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4788 | std::string* track_id) { |
| 4789 | if (!local_description()) { |
| 4790 | return false; |
| 4791 | } |
| 4792 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4793 | track_id); |
| 4794 | } |
| 4795 | |
| 4796 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4797 | std::string* track_id) { |
| 4798 | if (!remote_description()) { |
| 4799 | return false; |
| 4800 | } |
| 4801 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4802 | track_id); |
| 4803 | } |
| 4804 | |
| 4805 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 4806 | const rtc::CopyOnWriteBuffer& payload, |
| 4807 | cricket::SendDataResult* result) { |
| 4808 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4809 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4810 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4811 | return false; |
| 4812 | } |
| 4813 | return rtp_data_channel_ |
| 4814 | ? rtp_data_channel_->SendData(params, payload, result) |
| 4815 | : network_thread()->Invoke<bool>( |
| 4816 | RTC_FROM_HERE, |
| 4817 | Bind(&cricket::SctpTransportInternal::SendData, |
| 4818 | sctp_transport_.get(), params, payload, result)); |
| 4819 | } |
| 4820 | |
| 4821 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4822 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 4823 | // Don't log an error here, because DataChannels are expected to call |
| 4824 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 4825 | // whether or not the underlying transport is ready. |
| 4826 | return false; |
| 4827 | } |
| 4828 | if (rtp_data_channel_) { |
| 4829 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 4830 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 4831 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 4832 | &DataChannel::OnDataReceived); |
| 4833 | } else { |
| 4834 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 4835 | &DataChannel::OnChannelReady); |
| 4836 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 4837 | &DataChannel::OnDataReceived); |
| 4838 | SignalSctpStreamClosedRemotely.connect( |
| 4839 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 4840 | } |
| 4841 | return true; |
| 4842 | } |
| 4843 | |
| 4844 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4845 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4846 | RTC_LOG(LS_ERROR) |
| 4847 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 4848 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4849 | return; |
| 4850 | } |
| 4851 | if (rtp_data_channel_) { |
| 4852 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 4853 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 4854 | } else { |
| 4855 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 4856 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 4857 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | void PeerConnection::AddSctpDataStream(int sid) { |
| 4862 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4863 | RTC_LOG(LS_ERROR) |
| 4864 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4865 | return; |
| 4866 | } |
| 4867 | network_thread()->Invoke<void>( |
| 4868 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 4869 | sctp_transport_.get(), sid)); |
| 4870 | } |
| 4871 | |
| 4872 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 4873 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4874 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4875 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4876 | return; |
| 4877 | } |
| 4878 | network_thread()->Invoke<void>( |
| 4879 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 4880 | sctp_transport_.get(), sid)); |
| 4881 | } |
| 4882 | |
| 4883 | bool PeerConnection::ReadyToSendData() const { |
| 4884 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 4885 | sctp_ready_to_send_data_; |
| 4886 | } |
| 4887 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4888 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 4889 | const { |
| 4890 | std::map<std::string, std::string> transport_names_by_mid; |
| 4891 | for (auto transceiver : transceivers_) { |
| 4892 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4893 | if (channel) { |
| 4894 | transport_names_by_mid[channel->content_name()] = |
| 4895 | channel->transport_name(); |
| 4896 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4897 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4898 | if (rtp_data_channel_) { |
| 4899 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 4900 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4901 | } |
| 4902 | if (sctp_transport_) { |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4903 | transport_names_by_mid[*sctp_content_name_] = *sctp_transport_name_; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4904 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4905 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4906 | } |
| 4907 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4908 | std::map<std::string, cricket::TransportStats> |
| 4909 | PeerConnection::GetTransportStatsByNames( |
| 4910 | const std::set<std::string>& transport_names) { |
| 4911 | if (!network_thread()->IsCurrent()) { |
| 4912 | return network_thread() |
| 4913 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 4914 | RTC_FROM_HERE, |
| 4915 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4916 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4917 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 4918 | for (const std::string& transport_name : transport_names) { |
| 4919 | cricket::TransportStats transport_stats; |
| 4920 | bool success = |
| 4921 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 4922 | if (success) { |
| 4923 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 4924 | } else { |
| 4925 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 4926 | << transport_name; |
| 4927 | } |
| 4928 | } |
| 4929 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | bool PeerConnection::GetLocalCertificate( |
| 4933 | const std::string& transport_name, |
| 4934 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 4935 | return transport_controller_->GetLocalCertificate(transport_name, |
| 4936 | certificate); |
| 4937 | } |
| 4938 | |
| 4939 | std::unique_ptr<rtc::SSLCertificate> PeerConnection::GetRemoteSSLCertificate( |
| 4940 | const std::string& transport_name) { |
| 4941 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
| 4942 | } |
| 4943 | |
| 4944 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 4945 | return data_channel_type_; |
| 4946 | } |
| 4947 | |
| 4948 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 4949 | return pending_ice_restarts_.find(content_name) != |
| 4950 | pending_ice_restarts_.end(); |
| 4951 | } |
| 4952 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4953 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 4954 | return transport_controller_->NeedsIceRestart(content_name); |
| 4955 | } |
| 4956 | |
| 4957 | void PeerConnection::OnCertificateReady( |
| 4958 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 4959 | transport_controller_->SetLocalCertificate(certificate); |
| 4960 | } |
| 4961 | |
| 4962 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4963 | SetSessionError(SessionError::kTransport, |
| 4964 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | void PeerConnection::OnTransportControllerConnectionState( |
| 4968 | cricket::IceConnectionState state) { |
| 4969 | switch (state) { |
| 4970 | case cricket::kIceConnectionConnecting: |
| 4971 | // If the current state is Connected or Completed, then there were |
| 4972 | // writable channels but now there are not, so the next state must |
| 4973 | // be Disconnected. |
| 4974 | // kIceConnectionConnecting is currently used as the default, |
| 4975 | // un-connected state by the TransportController, so its only use is |
| 4976 | // detecting disconnections. |
| 4977 | if (ice_connection_state_ == |
| 4978 | PeerConnectionInterface::kIceConnectionConnected || |
| 4979 | ice_connection_state_ == |
| 4980 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 4981 | SetIceConnectionState( |
| 4982 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 4983 | } |
| 4984 | break; |
| 4985 | case cricket::kIceConnectionFailed: |
| 4986 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 4987 | break; |
| 4988 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4989 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4990 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4991 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 4992 | break; |
| 4993 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4994 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4995 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4996 | if (ice_connection_state_ != |
| 4997 | PeerConnectionInterface::kIceConnectionConnected) { |
| 4998 | // If jumping directly from "checking" to "connected", |
| 4999 | // signal "connected" first. |
| 5000 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5001 | } |
| 5002 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5003 | if (metrics_observer()) { |
| 5004 | ReportTransportStats(); |
| 5005 | } |
| 5006 | break; |
| 5007 | default: |
| 5008 | RTC_NOTREACHED(); |
| 5009 | } |
| 5010 | } |
| 5011 | |
| 5012 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5013 | const std::string& transport_name, |
| 5014 | const cricket::Candidates& candidates) { |
| 5015 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5016 | int sdp_mline_index; |
| 5017 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5018 | RTC_LOG(LS_ERROR) |
| 5019 | << "OnTransportControllerCandidatesGathered: content name " |
| 5020 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5021 | return; |
| 5022 | } |
| 5023 | |
| 5024 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5025 | citer != candidates.end(); ++citer) { |
| 5026 | // Use transport_name as the candidate media id. |
| 5027 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5028 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5029 | if (local_description()) { |
| 5030 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5031 | } |
| 5032 | OnIceCandidate(std::move(candidate)); |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5037 | const std::vector<cricket::Candidate>& candidates) { |
| 5038 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5039 | // Sanity check. |
| 5040 | for (const cricket::Candidate& candidate : candidates) { |
| 5041 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5042 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5043 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5044 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5045 | return; |
| 5046 | } |
| 5047 | } |
| 5048 | |
| 5049 | if (local_description()) { |
| 5050 | mutable_local_description()->RemoveCandidates(candidates); |
| 5051 | } |
| 5052 | OnIceCandidatesRemoved(candidates); |
| 5053 | } |
| 5054 | |
| 5055 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5056 | rtc::SSLHandshakeError error) { |
| 5057 | if (metrics_observer()) { |
| 5058 | metrics_observer()->IncrementEnumCounter( |
| 5059 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5060 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5061 | } |
| 5062 | } |
| 5063 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5064 | void PeerConnection::EnableSending() { |
| 5065 | for (auto transceiver : transceivers_) { |
| 5066 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5067 | if (channel && !channel->enabled()) { |
| 5068 | channel->Enable(true); |
| 5069 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5070 | } |
| 5071 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5072 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5073 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5074 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | // Returns the media index for a local ice candidate given the content name. |
| 5078 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5079 | const std::string& content_name, |
| 5080 | int* sdp_mline_index) { |
| 5081 | if (!local_description() || !sdp_mline_index) { |
| 5082 | return false; |
| 5083 | } |
| 5084 | |
| 5085 | bool content_found = false; |
| 5086 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5087 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5088 | if (contents[index].name == content_name) { |
| 5089 | *sdp_mline_index = static_cast<int>(index); |
| 5090 | content_found = true; |
| 5091 | break; |
| 5092 | } |
| 5093 | } |
| 5094 | return content_found; |
| 5095 | } |
| 5096 | |
| 5097 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5098 | const SessionDescriptionInterface* remote_desc) { |
| 5099 | if (!remote_desc) { |
| 5100 | return true; |
| 5101 | } |
| 5102 | bool ret = true; |
| 5103 | |
| 5104 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5105 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5106 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5107 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5108 | bool valid = false; |
| 5109 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5110 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5111 | RTC_LOG(LS_INFO) |
| 5112 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5113 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5114 | } |
| 5115 | continue; |
| 5116 | } |
| 5117 | ret = UseCandidate(candidate); |
| 5118 | if (!ret) { |
| 5119 | break; |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | return ret; |
| 5124 | } |
| 5125 | |
| 5126 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5127 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5128 | size_t remote_content_size = |
| 5129 | remote_description()->description()->contents().size(); |
| 5130 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5131 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5132 | return false; |
| 5133 | } |
| 5134 | |
| 5135 | cricket::ContentInfo content = |
| 5136 | remote_description()->description()->contents()[mediacontent_index]; |
| 5137 | std::vector<cricket::Candidate> candidates; |
| 5138 | candidates.push_back(candidate->candidate()); |
| 5139 | // Invoking BaseSession method to handle remote candidates. |
| 5140 | std::string error; |
| 5141 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 5142 | &error)) { |
| 5143 | // Candidates successfully submitted for checking. |
| 5144 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5145 | ice_connection_state_ == |
| 5146 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5147 | // If state is New, then the session has just gotten its first remote ICE |
| 5148 | // candidates, so go to Checking. |
| 5149 | // If state is Disconnected, the session is re-using old candidates or |
| 5150 | // receiving additional ones, so go to Checking. |
| 5151 | // If state is Connected, stay Connected. |
| 5152 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5153 | // newly added transport, then the state actually _should_ move to |
| 5154 | // checking. Add a way to distinguish that case. |
| 5155 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5156 | } |
| 5157 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5158 | } else { |
| 5159 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5160 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | return true; |
| 5164 | } |
| 5165 | |
| 5166 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5167 | // Destroy video channel first since it may have a pointer to the |
| 5168 | // voice channel. |
| 5169 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5170 | if (!video_info || video_info->rejected) { |
| 5171 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5172 | } |
| 5173 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5174 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5175 | if (!audio_info || audio_info->rejected) { |
| 5176 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5180 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5181 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5182 | } |
| 5183 | } |
| 5184 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5185 | std::string PeerConnection::GetTransportNameForMediaSection( |
| 5186 | const std::string& mid, |
| 5187 | const cricket::ContentGroup* bundle_group) const { |
| 5188 | if (!bundle_group) { |
| 5189 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5190 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5191 | const std::string* first_content_name = bundle_group->FirstContentName(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5192 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5193 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5194 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5195 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5196 | if (!bundle_group->HasContentName(mid)) { |
| 5197 | RTC_LOG(LS_WARNING) << mid << " is not part of any bundle group"; |
| 5198 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5199 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5200 | RTC_LOG(LS_INFO) << "Bundling " << mid << " on " << *first_content_name; |
| 5201 | return *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5202 | } |
| 5203 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5204 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5205 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5206 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5207 | if (configuration_.bundle_policy == |
| 5208 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5209 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5210 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5211 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5212 | "max-bundle configured but session description " |
| 5213 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5214 | } |
| 5215 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5216 | return std::move(bundle_group); |
| 5217 | } |
| 5218 | |
| 5219 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
| 5220 | auto bundle_group_or_error = GetEarlyBundleGroup(desc); |
| 5221 | if (!bundle_group_or_error.ok()) { |
| 5222 | return bundle_group_or_error.MoveError(); |
| 5223 | } |
| 5224 | const cricket::ContentGroup* bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5225 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5226 | // Creating the media channels and transport proxies. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5227 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5228 | if (voice && !voice->rejected && |
| 5229 | !GetAudioTransceiver()->internal()->channel()) { |
| 5230 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel( |
| 5231 | voice->name, |
| 5232 | GetTransportNameForMediaSection(voice->name, bundle_group)); |
| 5233 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5234 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5235 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5236 | } |
| 5237 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5238 | } |
| 5239 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5240 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5241 | if (video && !video->rejected && |
| 5242 | !GetVideoTransceiver()->internal()->channel()) { |
| 5243 | cricket::VideoChannel* video_channel = CreateVideoChannel( |
| 5244 | video->name, |
| 5245 | GetTransportNameForMediaSection(video->name, bundle_group)); |
| 5246 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5247 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5248 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5249 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5250 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5251 | } |
| 5252 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5253 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5254 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5255 | !rtp_data_channel_ && !sctp_transport_) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5256 | if (!CreateDataChannel(data->name, GetTransportNameForMediaSection( |
| 5257 | data->name, bundle_group))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5258 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5259 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5260 | } |
| 5261 | } |
| 5262 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5263 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5264 | } |
| 5265 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5266 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5267 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
| 5268 | const std::string& mid, |
| 5269 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5270 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5271 | transport_controller_->CreateDtlsTransport( |
| 5272 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5273 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5274 | if (configuration_.rtcp_mux_policy != |
| 5275 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5276 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5277 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5278 | } |
| 5279 | |
| 5280 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 5281 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5282 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5283 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5284 | if (!voice_channel) { |
| 5285 | transport_controller_->DestroyDtlsTransport( |
| 5286 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5287 | if (rtcp_dtls_transport) { |
| 5288 | transport_controller_->DestroyDtlsTransport( |
| 5289 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5290 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5291 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5292 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5293 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 5294 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5295 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5296 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5297 | voice_channel->SignalSentPacket.connect(this, |
| 5298 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5299 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5300 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5301 | } |
| 5302 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5303 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5304 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
| 5305 | const std::string& mid, |
| 5306 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5307 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5308 | transport_controller_->CreateDtlsTransport( |
| 5309 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5310 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5311 | if (configuration_.rtcp_mux_policy != |
| 5312 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5313 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5314 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5315 | } |
| 5316 | |
| 5317 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 5318 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5319 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5320 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5321 | |
| 5322 | if (!video_channel) { |
| 5323 | transport_controller_->DestroyDtlsTransport( |
| 5324 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5325 | if (rtcp_dtls_transport) { |
| 5326 | transport_controller_->DestroyDtlsTransport( |
| 5327 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5328 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5329 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5330 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5331 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 5332 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5333 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5334 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5335 | video_channel->SignalSentPacket.connect(this, |
| 5336 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5337 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5338 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5339 | } |
| 5340 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5341 | bool PeerConnection::CreateDataChannel(const std::string& mid, |
| 5342 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5343 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5344 | if (sctp) { |
| 5345 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5346 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5347 | << "Trying to create SCTP transport, but didn't compile with " |
| 5348 | "SCTP support (HAVE_SCTP)"; |
| 5349 | return false; |
| 5350 | } |
| 5351 | if (!network_thread()->Invoke<bool>( |
| 5352 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5353 | this, mid, transport_name))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5354 | return false; |
| 5355 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5356 | for (const auto& channel : sctp_data_channels_) { |
| 5357 | channel->OnTransportChannelCreated(); |
| 5358 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5359 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5360 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5361 | transport_controller_->CreateDtlsTransport( |
| 5362 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5363 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5364 | if (configuration_.rtcp_mux_policy != |
| 5365 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5366 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5367 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5368 | } |
| 5369 | |
| 5370 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 5371 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5372 | signaling_thread(), mid, SrtpRequired()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5373 | |
| 5374 | if (!rtp_data_channel_) { |
| 5375 | transport_controller_->DestroyDtlsTransport( |
| 5376 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5377 | if (rtcp_dtls_transport) { |
| 5378 | transport_controller_->DestroyDtlsTransport( |
| 5379 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5380 | } |
| 5381 | return false; |
| 5382 | } |
| 5383 | |
| 5384 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 5385 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5386 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5387 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5388 | rtp_data_channel_->SignalSentPacket.connect( |
| 5389 | this, &PeerConnection::OnSentPacket_w); |
| 5390 | } |
| 5391 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5392 | return true; |
| 5393 | } |
| 5394 | |
| 5395 | Call::Stats PeerConnection::GetCallStats() { |
| 5396 | if (!worker_thread()->IsCurrent()) { |
| 5397 | return worker_thread()->Invoke<Call::Stats>( |
| 5398 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5399 | } |
| 5400 | if (call_) { |
| 5401 | return call_->GetStats(); |
| 5402 | } else { |
| 5403 | return Call::Stats(); |
| 5404 | } |
| 5405 | } |
| 5406 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5407 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 5408 | const std::string& transport_name) { |
| 5409 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5410 | RTC_DCHECK(sctp_factory_); |
| 5411 | cricket::DtlsTransportInternal* tc = |
| 5412 | transport_controller_->CreateDtlsTransport_n( |
| 5413 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5414 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5415 | RTC_DCHECK(sctp_transport_); |
| 5416 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5417 | sctp_transport_->SignalReadyToSendData.connect( |
| 5418 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5419 | sctp_transport_->SignalDataReceived.connect( |
| 5420 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5421 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5422 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5423 | sctp_transport_name_ = transport_name; |
| 5424 | sctp_content_name_ = content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5425 | return true; |
| 5426 | } |
| 5427 | |
| 5428 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 5429 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5430 | RTC_DCHECK(sctp_transport_); |
| 5431 | RTC_DCHECK(sctp_transport_name_); |
| 5432 | std::string old_sctp_transport_name = *sctp_transport_name_; |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5433 | sctp_transport_name_ = transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5434 | cricket::DtlsTransportInternal* tc = |
| 5435 | transport_controller_->CreateDtlsTransport_n( |
| 5436 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5437 | sctp_transport_->SetTransportChannel(tc); |
| 5438 | transport_controller_->DestroyDtlsTransport_n( |
| 5439 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5440 | } |
| 5441 | |
| 5442 | void PeerConnection::DestroySctpTransport_n() { |
| 5443 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5444 | sctp_transport_.reset(nullptr); |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 5445 | transport_controller_->DestroyDtlsTransport_n( |
| 5446 | *sctp_transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5447 | sctp_content_name_.reset(); |
| 5448 | sctp_transport_name_.reset(); |
| 5449 | sctp_invoker_.reset(nullptr); |
| 5450 | sctp_ready_to_send_data_ = false; |
| 5451 | } |
| 5452 | |
| 5453 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5454 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5455 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5456 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5457 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5458 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5459 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5460 | // pending tasks will be cleared. |
| 5461 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5462 | OnSctpTransportReadyToSendData_s(true); |
| 5463 | }); |
| 5464 | } |
| 5465 | |
| 5466 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5467 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5468 | sctp_ready_to_send_data_ = ready; |
| 5469 | SignalSctpReadyToSendData(ready); |
| 5470 | } |
| 5471 | |
| 5472 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5473 | const cricket::ReceiveDataParams& params, |
| 5474 | const rtc::CopyOnWriteBuffer& payload) { |
| 5475 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5476 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5477 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5478 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5479 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5480 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5481 | // pending tasks will be cleared. |
| 5482 | sctp_invoker_->AsyncInvoke<void>( |
| 5483 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5484 | OnSctpTransportDataReceived_s(params, payload); |
| 5485 | }); |
| 5486 | } |
| 5487 | |
| 5488 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5489 | const cricket::ReceiveDataParams& params, |
| 5490 | const rtc::CopyOnWriteBuffer& payload) { |
| 5491 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5492 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5493 | // Received OPEN message; parse and signal that a new data channel should |
| 5494 | // be created. |
| 5495 | std::string label; |
| 5496 | InternalDataChannelInit config; |
| 5497 | config.id = params.ssrc; |
| 5498 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5499 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5500 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5501 | return; |
| 5502 | } |
| 5503 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5504 | OnDataChannelOpenMessage(label, config); |
| 5505 | } else { |
| 5506 | // Otherwise just forward the signal. |
| 5507 | SignalSctpDataReceived(params, payload); |
| 5508 | } |
| 5509 | } |
| 5510 | |
| 5511 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5512 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5513 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5514 | sctp_invoker_->AsyncInvoke<void>( |
| 5515 | RTC_FROM_HERE, signaling_thread(), |
| 5516 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5517 | &SignalSctpStreamClosedRemotely, sid)); |
| 5518 | } |
| 5519 | |
| 5520 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5521 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5522 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5523 | if (!bundle_enabled) |
| 5524 | return true; |
| 5525 | |
| 5526 | const cricket::ContentGroup* bundle_group = |
| 5527 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5528 | RTC_DCHECK(bundle_group != NULL); |
| 5529 | |
| 5530 | const cricket::ContentInfos& contents = desc->contents(); |
| 5531 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5532 | citer != contents.end(); ++citer) { |
| 5533 | const cricket::ContentInfo* content = (&*citer); |
| 5534 | RTC_DCHECK(content != NULL); |
| 5535 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5536 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5537 | if (!HasRtcpMuxEnabled(content)) |
| 5538 | return false; |
| 5539 | } |
| 5540 | } |
| 5541 | // RTCP-MUX is enabled in all the contents. |
| 5542 | return true; |
| 5543 | } |
| 5544 | |
| 5545 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5546 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5547 | } |
| 5548 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5549 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5550 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5551 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5552 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5553 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5557 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5558 | } |
| 5559 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5560 | SdpType type = sdesc->GetType(); |
| 5561 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5562 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5563 | LOG_AND_RETURN_ERROR( |
| 5564 | RTCErrorType::INVALID_PARAMETER, |
| 5565 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | // Verify crypto settings. |
| 5569 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5570 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5571 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5572 | RTCError crypto_error = |
| 5573 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5574 | if (!crypto_error.ok()) { |
| 5575 | return crypto_error; |
| 5576 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5577 | } |
| 5578 | |
| 5579 | // Verify ice-ufrag and ice-pwd. |
| 5580 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5581 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5582 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5586 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5587 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5591 | // m-lines that do not rtcp-mux enabled. |
| 5592 | |
| 5593 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5594 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5595 | // With an answer we want to compare the new answer session description with |
| 5596 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5597 | const cricket::SessionDescription* offer_desc = |
| 5598 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5599 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5600 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5601 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5602 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5603 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5604 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5605 | } |
| 5606 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5607 | // The re-offers should respect the order of m= sections in current |
| 5608 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5609 | // With a re-offer, either the current local or current remote descriptions |
| 5610 | // could be the most up to date, so we would like to check against both of |
| 5611 | // them if they exist. It could be the case that one of them has a 0 port |
| 5612 | // for a media section, but the other does not. This is important to check |
| 5613 | // against in the case that we are recycling an m= section. |
| 5614 | const cricket::SessionDescription* current_desc = nullptr; |
| 5615 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5616 | if (local_description()) { |
| 5617 | current_desc = local_description()->description(); |
| 5618 | if (remote_description()) { |
| 5619 | secondary_current_desc = remote_description()->description(); |
| 5620 | } |
| 5621 | } else if (remote_description()) { |
| 5622 | current_desc = remote_description()->description(); |
| 5623 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5624 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5625 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5626 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5627 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5628 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5632 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5633 | } |
| 5634 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5635 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5636 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5637 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5638 | return (state == PeerConnectionInterface::kStable) || |
| 5639 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5640 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5641 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5642 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5643 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5644 | } |
| 5645 | } |
| 5646 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5647 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5648 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5649 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5650 | return (state == PeerConnectionInterface::kStable) || |
| 5651 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5652 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5653 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5654 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5655 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5656 | } |
| 5657 | } |
| 5658 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5659 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5660 | switch (error) { |
| 5661 | case SessionError::kNone: |
| 5662 | return "ERROR_NONE"; |
| 5663 | case SessionError::kContent: |
| 5664 | return "ERROR_CONTENT"; |
| 5665 | case SessionError::kTransport: |
| 5666 | return "ERROR_TRANSPORT"; |
| 5667 | } |
| 5668 | RTC_NOTREACHED(); |
| 5669 | return ""; |
| 5670 | } |
| 5671 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5672 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5673 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5674 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5675 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5676 | return desc.str(); |
| 5677 | } |
| 5678 | |
| 5679 | // We need to check the local/remote description for the Transport instead of |
| 5680 | // the session, because a new Transport added during renegotiation may have |
| 5681 | // them unset while the session has them set from the previous negotiation. |
| 5682 | // Not doing so may trigger the auto generation of transport description and |
| 5683 | // mess up DTLS identity information, ICE credential, etc. |
| 5684 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5685 | const IceCandidateInterface* candidate, |
| 5686 | const SessionDescriptionInterface* remote_desc, |
| 5687 | bool* valid) { |
| 5688 | *valid = true; |
| 5689 | |
| 5690 | const SessionDescriptionInterface* current_remote_desc = |
| 5691 | remote_desc ? remote_desc : remote_description(); |
| 5692 | |
| 5693 | if (!current_remote_desc) { |
| 5694 | return false; |
| 5695 | } |
| 5696 | |
| 5697 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5698 | size_t remote_content_size = |
| 5699 | current_remote_desc->description()->contents().size(); |
| 5700 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5701 | RTC_LOG(LS_ERROR) |
| 5702 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5703 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5704 | |
| 5705 | *valid = false; |
| 5706 | return false; |
| 5707 | } |
| 5708 | |
| 5709 | cricket::ContentInfo content = |
| 5710 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5711 | |
| 5712 | const std::string transport_name = GetTransportName(content.name); |
| 5713 | if (transport_name.empty()) { |
| 5714 | return false; |
| 5715 | } |
| 5716 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 5717 | } |
| 5718 | |
| 5719 | bool PeerConnection::SrtpRequired() const { |
| 5720 | return dtls_enabled_ || |
| 5721 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5722 | } |
| 5723 | |
| 5724 | void PeerConnection::OnTransportControllerGatheringState( |
| 5725 | cricket::IceGatheringState state) { |
| 5726 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5727 | if (state == cricket::kIceGatheringGathering) { |
| 5728 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5729 | } else if (state == cricket::kIceGatheringComplete) { |
| 5730 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5731 | } |
| 5732 | } |
| 5733 | |
| 5734 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5735 | std::map<std::string, std::set<cricket::MediaType>> |
| 5736 | media_types_by_transport_name; |
| 5737 | for (auto transceiver : transceivers_) { |
| 5738 | if (transceiver->internal()->channel()) { |
| 5739 | const std::string& transport_name = |
| 5740 | transceiver->internal()->channel()->transport_name(); |
| 5741 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5742 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5743 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5744 | } |
| 5745 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5746 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5747 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5748 | } |
| 5749 | if (sctp_transport_name_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5750 | media_types_by_transport_name[*sctp_transport_name_].insert( |
| 5751 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5752 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5753 | for (const auto& entry : media_types_by_transport_name) { |
| 5754 | const std::string& transport_name = entry.first; |
| 5755 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5756 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5757 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5758 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5759 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | // Walk through the ConnectionInfos to gather best connection usage |
| 5764 | // for IPv4 and IPv6. |
| 5765 | void PeerConnection::ReportBestConnectionState( |
| 5766 | const cricket::TransportStats& stats) { |
| 5767 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5768 | for (const cricket::TransportChannelStats& channel_stats : |
| 5769 | stats.channel_stats) { |
| 5770 | for (const cricket::ConnectionInfo& connection_info : |
| 5771 | channel_stats.connection_infos) { |
| 5772 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5773 | continue; |
| 5774 | } |
| 5775 | |
| 5776 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5777 | const cricket::Candidate& local = connection_info.local_candidate; |
| 5778 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5779 | |
| 5780 | // Increment the counter for IceCandidatePairType. |
| 5781 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 5782 | (local.type() == RELAY_PORT_TYPE && |
| 5783 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 5784 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 5785 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 5786 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 5787 | } else { |
| 5788 | RTC_CHECK(0); |
| 5789 | } |
| 5790 | metrics_observer()->IncrementEnumCounter( |
| 5791 | type, GetIceCandidatePairCounter(local, remote), |
| 5792 | kIceCandidatePairMax); |
| 5793 | |
| 5794 | // Increment the counter for IP type. |
| 5795 | if (local.address().family() == AF_INET) { |
| 5796 | metrics_observer()->IncrementEnumCounter( |
| 5797 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 5798 | kPeerConnectionAddressFamilyCounter_Max); |
| 5799 | |
| 5800 | } else if (local.address().family() == AF_INET6) { |
| 5801 | metrics_observer()->IncrementEnumCounter( |
| 5802 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 5803 | kPeerConnectionAddressFamilyCounter_Max); |
| 5804 | } else { |
| 5805 | RTC_CHECK(0); |
| 5806 | } |
| 5807 | |
| 5808 | return; |
| 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | |
| 5813 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5814 | const cricket::TransportStats& stats, |
| 5815 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5816 | RTC_DCHECK(metrics_observer()); |
| 5817 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 5818 | return; |
| 5819 | } |
| 5820 | |
| 5821 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 5822 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 5823 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 5824 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5825 | return; |
| 5826 | } |
| 5827 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5828 | for (cricket::MediaType media_type : media_types) { |
| 5829 | PeerConnectionEnumCounterType srtp_counter_type; |
| 5830 | PeerConnectionEnumCounterType ssl_counter_type; |
| 5831 | switch (media_type) { |
| 5832 | case cricket::MEDIA_TYPE_AUDIO: |
| 5833 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 5834 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 5835 | break; |
| 5836 | case cricket::MEDIA_TYPE_VIDEO: |
| 5837 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 5838 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 5839 | break; |
| 5840 | case cricket::MEDIA_TYPE_DATA: |
| 5841 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 5842 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 5843 | break; |
| 5844 | default: |
| 5845 | RTC_NOTREACHED(); |
| 5846 | continue; |
| 5847 | } |
| 5848 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 5849 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 5850 | srtp_crypto_suite); |
| 5851 | } |
| 5852 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5853 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 5854 | ssl_cipher_suite); |
| 5855 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | |
| 5859 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 5860 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 5861 | RTC_DCHECK(call_); |
| 5862 | call_->OnSentPacket(sent_packet); |
| 5863 | } |
| 5864 | |
| 5865 | const std::string PeerConnection::GetTransportName( |
| 5866 | const std::string& content_name) { |
| 5867 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5868 | if (channel) { |
| 5869 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5870 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5871 | if (sctp_transport_) { |
| 5872 | RTC_DCHECK(sctp_content_name_); |
| 5873 | RTC_DCHECK(sctp_transport_name_); |
| 5874 | if (content_name == *sctp_content_name_) { |
| 5875 | return *sctp_transport_name_; |
| 5876 | } |
| 5877 | } |
| 5878 | // Return an empty string if failed to retrieve the transport name. |
| 5879 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 5883 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5884 | transport_controller_->DestroyDtlsTransport_n( |
| 5885 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5886 | } |
| 5887 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5888 | void PeerConnection::DestroyTransceiverChannel( |
| 5889 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5890 | transceiver) { |
| 5891 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5892 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5893 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5894 | if (channel) { |
| 5895 | transceiver->internal()->SetChannel(nullptr); |
| 5896 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5897 | } |
| 5898 | } |
| 5899 | |
| 5900 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5901 | if (rtp_data_channel_) { |
| 5902 | OnDataChannelDestroyed(); |
| 5903 | DestroyBaseChannel(rtp_data_channel_); |
| 5904 | rtp_data_channel_ = nullptr; |
| 5905 | } |
| 5906 | |
| 5907 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 5908 | // grab a reference to this PeerConnection. If this is called from the |
| 5909 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 5910 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 5911 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 5912 | |
| 5913 | if (sctp_transport_) { |
| 5914 | OnDataChannelDestroyed(); |
| 5915 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 5916 | [this] { DestroySctpTransport_n(); }); |
| 5917 | } |
| 5918 | } |
| 5919 | |
| 5920 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 5921 | RTC_DCHECK(channel); |
| 5922 | RTC_DCHECK(channel->rtp_dtls_transport()); |
| 5923 | |
| 5924 | // Need to cache these before destroying the base channel so that we do not |
| 5925 | // access uninitialized memory. |
| 5926 | const std::string transport_name = |
| 5927 | channel->rtp_dtls_transport()->transport_name(); |
| 5928 | const bool need_to_delete_rtcp = (channel->rtcp_dtls_transport() != nullptr); |
| 5929 | |
| 5930 | switch (channel->media_type()) { |
| 5931 | case cricket::MEDIA_TYPE_AUDIO: |
| 5932 | channel_manager()->DestroyVoiceChannel( |
| 5933 | static_cast<cricket::VoiceChannel*>(channel)); |
| 5934 | break; |
| 5935 | case cricket::MEDIA_TYPE_VIDEO: |
| 5936 | channel_manager()->DestroyVideoChannel( |
| 5937 | static_cast<cricket::VideoChannel*>(channel)); |
| 5938 | break; |
| 5939 | case cricket::MEDIA_TYPE_DATA: |
| 5940 | channel_manager()->DestroyRtpDataChannel( |
| 5941 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 5942 | break; |
| 5943 | default: |
| 5944 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 5945 | break; |
| 5946 | } |
| 5947 | |
| 5948 | // |channel| can no longer be used. |
| 5949 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5950 | transport_controller_->DestroyDtlsTransport( |
| 5951 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5952 | if (need_to_delete_rtcp) { |
| 5953 | transport_controller_->DestroyDtlsTransport( |
| 5954 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5955 | } |
| 5956 | } |
| 5957 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 5958 | void PeerConnection::ClearStatsCache() { |
| 5959 | if (stats_collector_) { |
| 5960 | stats_collector_->ClearCachedStatsReport(); |
| 5961 | } |
| 5962 | } |
| 5963 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 5964 | } // namespace webrtc |