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; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 639 | rtc::Optional<int> stun_candidate_keepalive_interval; |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 640 | rtc::Optional<rtc::IntervalRange> ice_regather_interval_range; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 641 | webrtc::TurnCustomizer* turn_customizer; |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 642 | SdpSemantics sdp_semantics; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 643 | rtc::Optional<rtc::AdapterType> network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 644 | }; |
| 645 | static_assert(sizeof(stuff_being_tested_for_equality) == sizeof(*this), |
| 646 | "Did you add something to RTCConfiguration and forget to " |
| 647 | "update operator==?"); |
| 648 | return type == o.type && servers == o.servers && |
| 649 | bundle_policy == o.bundle_policy && |
| 650 | rtcp_mux_policy == o.rtcp_mux_policy && |
| 651 | tcp_candidate_policy == o.tcp_candidate_policy && |
| 652 | candidate_network_policy == o.candidate_network_policy && |
| 653 | audio_jitter_buffer_max_packets == o.audio_jitter_buffer_max_packets && |
| 654 | audio_jitter_buffer_fast_accelerate == |
| 655 | o.audio_jitter_buffer_fast_accelerate && |
| 656 | ice_connection_receiving_timeout == |
| 657 | o.ice_connection_receiving_timeout && |
| 658 | ice_backup_candidate_pair_ping_interval == |
| 659 | o.ice_backup_candidate_pair_ping_interval && |
| 660 | continual_gathering_policy == o.continual_gathering_policy && |
| 661 | certificates == o.certificates && |
| 662 | prioritize_most_likely_ice_candidate_pairs == |
| 663 | o.prioritize_most_likely_ice_candidate_pairs && |
| 664 | media_config == o.media_config && disable_ipv6 == o.disable_ipv6 && |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 665 | disable_ipv6_on_wifi == o.disable_ipv6_on_wifi && |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 666 | max_ipv6_networks == o.max_ipv6_networks && |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 667 | disable_link_local_networks == o.disable_link_local_networks && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 668 | enable_rtp_data_channel == o.enable_rtp_data_channel && |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 669 | screencast_min_bitrate == o.screencast_min_bitrate && |
| 670 | combined_audio_video_bwe == o.combined_audio_video_bwe && |
| 671 | enable_dtls_srtp == o.enable_dtls_srtp && |
| 672 | ice_candidate_pool_size == o.ice_candidate_pool_size && |
| 673 | prune_turn_ports == o.prune_turn_ports && |
| 674 | presume_writable_when_fully_relayed == |
| 675 | o.presume_writable_when_fully_relayed && |
| 676 | enable_ice_renomination == o.enable_ice_renomination && |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 677 | redetermine_role_on_ice_restart == o.redetermine_role_on_ice_restart && |
Steve Anton | 300bf8e | 2017-07-14 10:13:10 -0700 | [diff] [blame] | 678 | ice_check_min_interval == o.ice_check_min_interval && |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 679 | stun_candidate_keepalive_interval == |
| 680 | o.stun_candidate_keepalive_interval && |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 681 | ice_regather_interval_range == o.ice_regather_interval_range && |
Steve Anton | 79e7960 | 2017-11-20 10:25:56 -0800 | [diff] [blame] | 682 | turn_customizer == o.turn_customizer && |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 683 | sdp_semantics == o.sdp_semantics && |
| 684 | network_preference == o.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | bool PeerConnectionInterface::RTCConfiguration::operator!=( |
| 688 | const PeerConnectionInterface::RTCConfiguration& o) const { |
| 689 | return !(*this == o); |
deadbeef | 3edec7c | 2016-12-10 11:44:26 -0800 | [diff] [blame] | 690 | } |
| 691 | |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 692 | // Generate a RTCP CNAME when a PeerConnection is created. |
| 693 | std::string GenerateRtcpCname() { |
| 694 | std::string cname; |
| 695 | if (!rtc::CreateRandomString(kRtcpCnameLength, &cname)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 696 | RTC_LOG(LS_ERROR) << "Failed to generate CNAME."; |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 697 | RTC_NOTREACHED(); |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 698 | } |
| 699 | return cname; |
| 700 | } |
| 701 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 702 | bool ValidateOfferAnswerOptions( |
| 703 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options) { |
| 704 | return IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_audio) && |
| 705 | IsValidOfferToReceiveMedia(rtc_options.offer_to_receive_video); |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 706 | } |
| 707 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 708 | // From |rtc_options|, fill parts of |session_options| shared by all generated |
| 709 | // m= sections (in other words, nothing that involves a map/array). |
| 710 | void ExtractSharedMediaSessionOptions( |
| 711 | const PeerConnectionInterface::RTCOfferAnswerOptions& rtc_options, |
| 712 | cricket::MediaSessionOptions* session_options) { |
| 713 | session_options->vad_enabled = rtc_options.voice_activity_detection; |
| 714 | session_options->bundle_enabled = rtc_options.use_rtp_mux; |
| 715 | } |
zhihuang | a77e6bb | 2017-08-14 18:17:48 -0700 | [diff] [blame] | 716 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 717 | bool ConvertConstraintsToOfferAnswerOptions( |
| 718 | const MediaConstraintsInterface* constraints, |
| 719 | PeerConnectionInterface::RTCOfferAnswerOptions* offer_answer_options) { |
olka | 3c74766 | 2017-08-17 06:50:32 -0700 | [diff] [blame] | 720 | if (!constraints) { |
| 721 | return true; |
| 722 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 723 | |
| 724 | bool value = false; |
| 725 | size_t mandatory_constraints_satisfied = 0; |
| 726 | |
| 727 | if (FindConstraint(constraints, |
| 728 | MediaConstraintsInterface::kOfferToReceiveAudio, &value, |
| 729 | &mandatory_constraints_satisfied)) { |
| 730 | offer_answer_options->offer_to_receive_audio = |
| 731 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 732 | kOfferToReceiveMediaTrue |
| 733 | : 0; |
| 734 | } |
| 735 | |
| 736 | if (FindConstraint(constraints, |
| 737 | MediaConstraintsInterface::kOfferToReceiveVideo, &value, |
| 738 | &mandatory_constraints_satisfied)) { |
| 739 | offer_answer_options->offer_to_receive_video = |
| 740 | value ? PeerConnectionInterface::RTCOfferAnswerOptions:: |
| 741 | kOfferToReceiveMediaTrue |
| 742 | : 0; |
| 743 | } |
| 744 | if (FindConstraint(constraints, |
| 745 | MediaConstraintsInterface::kVoiceActivityDetection, &value, |
| 746 | &mandatory_constraints_satisfied)) { |
| 747 | offer_answer_options->voice_activity_detection = value; |
| 748 | } |
| 749 | if (FindConstraint(constraints, MediaConstraintsInterface::kUseRtpMux, &value, |
| 750 | &mandatory_constraints_satisfied)) { |
| 751 | offer_answer_options->use_rtp_mux = value; |
| 752 | } |
| 753 | if (FindConstraint(constraints, MediaConstraintsInterface::kIceRestart, |
| 754 | &value, &mandatory_constraints_satisfied)) { |
| 755 | offer_answer_options->ice_restart = value; |
| 756 | } |
| 757 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 758 | return mandatory_constraints_satisfied == constraints->GetMandatory().size(); |
| 759 | } |
| 760 | |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 761 | PeerConnection::PeerConnection(PeerConnectionFactory* factory, |
| 762 | std::unique_ptr<RtcEventLog> event_log, |
| 763 | std::unique_ptr<Call> call) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 764 | : factory_(factory), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 765 | event_log_(std::move(event_log)), |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 766 | rtcp_cname_(GenerateRtcpCname()), |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 767 | local_streams_(StreamCollection::Create()), |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 768 | remote_streams_(StreamCollection::Create()), |
| 769 | call_(std::move(call)) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 770 | |
| 771 | PeerConnection::~PeerConnection() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 772 | TRACE_EVENT0("webrtc", "PeerConnection::~PeerConnection"); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 773 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 774 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 775 | // Need to stop transceivers before destroying the stats collector because |
| 776 | // AudioRtpSender has a reference to the StatsCollector it will update when |
| 777 | // stopping. |
| 778 | for (auto transceiver : transceivers_) { |
| 779 | transceiver->Stop(); |
| 780 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 781 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 782 | stats_.reset(nullptr); |
hbos | b78306a | 2016-12-19 05:06:57 -0800 | [diff] [blame] | 783 | if (stats_collector_) { |
| 784 | stats_collector_->WaitForPendingRequest(); |
| 785 | stats_collector_ = nullptr; |
| 786 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 787 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 788 | // Don't destroy BaseChannels until after stats has been cleaned up so that |
| 789 | // the last stats request can still read from the channels. |
| 790 | DestroyAllChannels(); |
| 791 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 792 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " is destroyed."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 793 | |
| 794 | webrtc_session_desc_factory_.reset(); |
| 795 | sctp_invoker_.reset(); |
| 796 | sctp_factory_.reset(); |
| 797 | transport_controller_.reset(); |
| 798 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 799 | // port_allocator_ lives on the network thread and should be destroyed there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 800 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 801 | [this] { port_allocator_.reset(); }); |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 802 | // call_ and event_log_ must be destroyed on the worker thread. |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 803 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 804 | call_.reset(); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 805 | // The event log must outlive call (and any other object that uses it). |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 806 | event_log_.reset(); |
| 807 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 810 | void PeerConnection::DestroyAllChannels() { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 811 | // Destroy video channels first since they may have a pointer to a voice |
| 812 | // channel. |
| 813 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 814 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 815 | DestroyTransceiverChannel(transceiver); |
| 816 | } |
| 817 | } |
| 818 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 819 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 820 | DestroyTransceiverChannel(transceiver); |
| 821 | } |
| 822 | } |
| 823 | DestroyDataChannel(); |
| 824 | } |
| 825 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 826 | bool PeerConnection::Initialize( |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 827 | const PeerConnectionInterface::RTCConfiguration& configuration, |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 828 | std::unique_ptr<cricket::PortAllocator> allocator, |
Henrik Boström | d03c23b | 2016-06-01 11:44:18 +0200 | [diff] [blame] | 829 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_generator, |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 830 | PeerConnectionObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 831 | TRACE_EVENT0("webrtc", "PeerConnection::Initialize"); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 832 | |
| 833 | RTCError config_error = ValidateConfiguration(configuration); |
| 834 | if (!config_error.ok()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 835 | RTC_LOG(LS_ERROR) << "Invalid configuration: " << config_error.message(); |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 836 | return false; |
| 837 | } |
| 838 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 839 | if (!allocator) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 840 | RTC_LOG(LS_ERROR) |
| 841 | << "PeerConnection initialized without a PortAllocator? " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 842 | "This shouldn't happen if using PeerConnectionFactory."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 843 | return false; |
| 844 | } |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 845 | |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 846 | if (!observer) { |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 847 | // TODO(deadbeef): Why do we do this? |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 848 | RTC_LOG(LS_ERROR) << "PeerConnection initialized without a " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 849 | "PeerConnectionObserver"; |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 850 | return false; |
| 851 | } |
pthatcher@webrtc.org | 877ac76 | 2015-02-04 22:03:09 +0000 | [diff] [blame] | 852 | observer_ = observer; |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 853 | port_allocator_ = std::move(allocator); |
deadbeef | 653b8e0 | 2015-11-11 12:55:10 -0800 | [diff] [blame] | 854 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 855 | // The port allocator lives on the network thread and should be initialized |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 856 | // there. |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 857 | if (!network_thread()->Invoke<bool>( |
| 858 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::InitializePortAllocator_n, |
| 859 | this, configuration))) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 860 | return false; |
| 861 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 862 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 863 | // RFC 3264: The numeric value of the session id and version in the |
| 864 | // o line MUST be representable with a "64 bit signed integer". |
| 865 | // Due to this constraint session id |session_id_| is max limited to |
| 866 | // LLONG_MAX. |
| 867 | session_id_ = rtc::ToString(rtc::CreateRandomId64() & LLONG_MAX); |
| 868 | transport_controller_.reset(factory_->CreateTransportController( |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 869 | port_allocator_.get(), configuration.redetermine_role_on_ice_restart, |
| 870 | event_log_.get())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 871 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 872 | transport_controller_->SignalConnectionState.connect( |
| 873 | this, &PeerConnection::OnTransportControllerConnectionState); |
| 874 | transport_controller_->SignalGatheringState.connect( |
| 875 | this, &PeerConnection::OnTransportControllerGatheringState); |
| 876 | transport_controller_->SignalCandidatesGathered.connect( |
| 877 | this, &PeerConnection::OnTransportControllerCandidatesGathered); |
| 878 | transport_controller_->SignalCandidatesRemoved.connect( |
| 879 | this, &PeerConnection::OnTransportControllerCandidatesRemoved); |
| 880 | transport_controller_->SignalDtlsHandshakeError.connect( |
| 881 | this, &PeerConnection::OnTransportControllerDtlsHandshakeError); |
| 882 | |
| 883 | sctp_factory_ = factory_->CreateSctpTransportInternalFactory(); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 884 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 885 | stats_.reset(new StatsCollector(this)); |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 886 | stats_collector_ = RTCStatsCollector::Create(this); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 887 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 888 | configuration_ = configuration; |
| 889 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 890 | const PeerConnectionFactoryInterface::Options& options = factory_->options(); |
| 891 | |
| 892 | transport_controller_->SetSslMaxProtocolVersion(options.ssl_max_version); |
| 893 | |
| 894 | // Obtain a certificate from RTCConfiguration if any were provided (optional). |
| 895 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 896 | if (!configuration.certificates.empty()) { |
| 897 | // TODO(hbos,torbjorng): Decide on certificate-selection strategy instead of |
| 898 | // just picking the first one. The decision should be made based on the DTLS |
| 899 | // handshake. The DTLS negotiations need to know about all certificates. |
| 900 | certificate = configuration.certificates[0]; |
| 901 | } |
| 902 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 903 | transport_controller_->SetIceConfig(ParseIceConfig(configuration)); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 904 | |
| 905 | if (options.disable_encryption) { |
| 906 | dtls_enabled_ = false; |
| 907 | } else { |
| 908 | // Enable DTLS by default if we have an identity store or a certificate. |
| 909 | dtls_enabled_ = (cert_generator || certificate); |
| 910 | // |configuration| can override the default |dtls_enabled_| value. |
| 911 | if (configuration.enable_dtls_srtp) { |
| 912 | dtls_enabled_ = *(configuration.enable_dtls_srtp); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // Enable creation of RTP data channels if the kEnableRtpDataChannels is set. |
| 917 | // It takes precendence over the disable_sctp_data_channels |
| 918 | // PeerConnectionFactoryInterface::Options. |
| 919 | if (configuration.enable_rtp_data_channel) { |
| 920 | data_channel_type_ = cricket::DCT_RTP; |
| 921 | } else { |
| 922 | // DTLS has to be enabled to use SCTP. |
| 923 | if (!options.disable_sctp_data_channels && dtls_enabled_) { |
| 924 | data_channel_type_ = cricket::DCT_SCTP; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | video_options_.screencast_min_bitrate_kbps = |
| 929 | configuration.screencast_min_bitrate; |
| 930 | audio_options_.combined_audio_video_bwe = |
| 931 | configuration.combined_audio_video_bwe; |
| 932 | |
| 933 | audio_options_.audio_jitter_buffer_max_packets = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 934 | configuration.audio_jitter_buffer_max_packets; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 935 | |
| 936 | audio_options_.audio_jitter_buffer_fast_accelerate = |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 937 | configuration.audio_jitter_buffer_fast_accelerate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 938 | |
| 939 | // Whether the certificate generator/certificate is null or not determines |
| 940 | // what PeerConnectionDescriptionFactory will do, so make sure that we give it |
| 941 | // the right instructions by clearing the variables if needed. |
| 942 | if (!dtls_enabled_) { |
| 943 | cert_generator.reset(); |
| 944 | certificate = nullptr; |
| 945 | } else if (certificate) { |
| 946 | // Favor generated certificate over the certificate generator. |
| 947 | cert_generator.reset(); |
| 948 | } |
| 949 | |
| 950 | webrtc_session_desc_factory_.reset(new WebRtcSessionDescriptionFactory( |
| 951 | signaling_thread(), channel_manager(), this, session_id(), |
| 952 | std::move(cert_generator), certificate)); |
| 953 | webrtc_session_desc_factory_->SignalCertificateReady.connect( |
| 954 | this, &PeerConnection::OnCertificateReady); |
| 955 | |
| 956 | if (options.disable_encryption) { |
| 957 | webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED); |
| 958 | } |
| 959 | |
| 960 | webrtc_session_desc_factory_->set_enable_encrypted_rtp_header_extensions( |
| 961 | options.crypto_options.enable_encrypted_rtp_header_extensions); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 962 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 963 | // Add default audio/video transceivers for Plan B SDP. |
| 964 | if (!IsUnifiedPlan()) { |
| 965 | transceivers_.push_back( |
| 966 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 967 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_AUDIO))); |
| 968 | transceivers_.push_back( |
| 969 | RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 970 | signaling_thread(), new RtpTransceiver(cricket::MEDIA_TYPE_VIDEO))); |
| 971 | } |
| 972 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 976 | RTCError PeerConnection::ValidateConfiguration( |
| 977 | const RTCConfiguration& config) const { |
| 978 | if (config.ice_regather_interval_range && |
| 979 | config.continual_gathering_policy == GATHER_ONCE) { |
| 980 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 981 | "ice_regather_interval_range specified but continual " |
| 982 | "gathering policy is GATHER_ONCE"); |
| 983 | } |
| 984 | return RTCError::OK(); |
| 985 | } |
| 986 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 987 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 988 | PeerConnection::local_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 989 | return local_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 990 | } |
| 991 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 992 | rtc::scoped_refptr<StreamCollectionInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 993 | PeerConnection::remote_streams() { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 994 | return remote_streams_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 995 | } |
| 996 | |
perkj@webrtc.org | c2dd5ee | 2014-11-04 11:31:29 +0000 | [diff] [blame] | 997 | bool PeerConnection::AddStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 998 | TRACE_EVENT0("webrtc", "PeerConnection::AddStream"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 999 | if (IsClosed()) { |
| 1000 | return false; |
| 1001 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1002 | if (!CanAddLocalMediaStream(local_streams_, local_stream)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1005 | |
| 1006 | local_streams_->AddStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1007 | MediaStreamObserver* observer = new MediaStreamObserver(local_stream); |
| 1008 | observer->SignalAudioTrackAdded.connect(this, |
| 1009 | &PeerConnection::OnAudioTrackAdded); |
| 1010 | observer->SignalAudioTrackRemoved.connect( |
| 1011 | this, &PeerConnection::OnAudioTrackRemoved); |
| 1012 | observer->SignalVideoTrackAdded.connect(this, |
| 1013 | &PeerConnection::OnVideoTrackAdded); |
| 1014 | observer->SignalVideoTrackRemoved.connect( |
| 1015 | this, &PeerConnection::OnVideoTrackRemoved); |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1016 | stream_observers_.push_back(std::unique_ptr<MediaStreamObserver>(observer)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1017 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1018 | for (const auto& track : local_stream->GetAudioTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1019 | AddAudioTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1020 | } |
| 1021 | for (const auto& track : local_stream->GetVideoTracks()) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1022 | AddVideoTrack(track.get(), local_stream); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1025 | stats_->AddStream(local_stream); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1026 | observer_->OnRenegotiationNeeded(); |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
| 1030 | void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1031 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveStream"); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 1032 | if (!IsClosed()) { |
| 1033 | for (const auto& track : local_stream->GetAudioTracks()) { |
| 1034 | RemoveAudioTrack(track.get(), local_stream); |
| 1035 | } |
| 1036 | for (const auto& track : local_stream->GetVideoTracks()) { |
| 1037 | RemoveVideoTrack(track.get(), local_stream); |
| 1038 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1039 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1040 | local_streams_->RemoveStream(local_stream); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1041 | stream_observers_.erase( |
| 1042 | std::remove_if( |
| 1043 | stream_observers_.begin(), stream_observers_.end(), |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1044 | [local_stream](const std::unique_ptr<MediaStreamObserver>& observer) { |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 1045 | return observer->stream()->label().compare(local_stream->label()) == |
| 1046 | 0; |
| 1047 | }), |
| 1048 | stream_observers_.end()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1049 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1050 | if (IsClosed()) { |
| 1051 | return; |
| 1052 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1053 | observer_->OnRenegotiationNeeded(); |
| 1054 | } |
| 1055 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1056 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::AddTrack( |
| 1057 | MediaStreamTrackInterface* track, |
| 1058 | std::vector<MediaStreamInterface*> streams) { |
| 1059 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1060 | std::vector<std::string> stream_labels; |
| 1061 | for (auto* stream : streams) { |
| 1062 | if (!stream) { |
| 1063 | RTC_LOG(LS_ERROR) << "Stream list has null element."; |
| 1064 | return nullptr; |
| 1065 | } |
| 1066 | stream_labels.push_back(stream->label()); |
| 1067 | } |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1068 | auto sender_or_error = AddTrack(track, stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1069 | if (!sender_or_error.ok()) { |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1070 | return nullptr; |
| 1071 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1072 | return sender_or_error.MoveValue(); |
| 1073 | } |
| 1074 | |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1075 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack( |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1076 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1077 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 2d6c76a | 2018-01-05 17:10:52 -0800 | [diff] [blame] | 1078 | TRACE_EVENT0("webrtc", "PeerConnection::AddTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1079 | if (!track) { |
| 1080 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null."); |
| 1081 | } |
| 1082 | if (!(track->kind() == MediaStreamTrackInterface::kAudioKind || |
| 1083 | track->kind() == MediaStreamTrackInterface::kVideoKind)) { |
| 1084 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1085 | "Track has invalid kind: " + track->kind()); |
| 1086 | } |
| 1087 | // TODO(bugs.webrtc.org/7932): Support adding a track to multiple streams. |
| 1088 | if (stream_labels.size() > 1u) { |
| 1089 | LOG_AND_RETURN_ERROR( |
| 1090 | RTCErrorType::UNSUPPORTED_OPERATION, |
| 1091 | "AddTrack with more than one stream is not currently supported."); |
| 1092 | } |
| 1093 | if (IsClosed()) { |
| 1094 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1095 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1096 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1097 | if (FindSenderForTrack(track)) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1098 | LOG_AND_RETURN_ERROR( |
| 1099 | RTCErrorType::INVALID_PARAMETER, |
| 1100 | "Sender already exists for track " + track->id() + "."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1101 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1102 | // TODO(bugs.webrtc.org/7933): MediaSession expects the sender to have exactly |
| 1103 | // one stream. AddTrackInternal will return an error if there is more than one |
| 1104 | // stream, but if the caller specifies none then we need to generate a random |
| 1105 | // stream label. |
| 1106 | std::vector<std::string> adjusted_stream_labels = stream_labels; |
| 1107 | if (stream_labels.empty()) { |
| 1108 | adjusted_stream_labels.push_back(rtc::CreateRandomUuid()); |
| 1109 | } |
| 1110 | RTC_DCHECK_EQ(1, adjusted_stream_labels.size()); |
| 1111 | auto sender_or_error = |
| 1112 | (IsUnifiedPlan() ? AddTrackUnifiedPlan(track, adjusted_stream_labels) |
| 1113 | : AddTrackPlanB(track, adjusted_stream_labels)); |
| 1114 | if (sender_or_error.ok()) { |
| 1115 | observer_->OnRenegotiationNeeded(); |
Steve Anton | 43a723a | 2018-01-04 15:48:17 -0800 | [diff] [blame] | 1116 | stats_->AddTrack(track); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1117 | } |
| 1118 | return sender_or_error; |
| 1119 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1120 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1121 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1122 | PeerConnection::AddTrackPlanB( |
| 1123 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1124 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1125 | cricket::MediaType media_type = |
| 1126 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1127 | ? cricket::MEDIA_TYPE_AUDIO |
| 1128 | : cricket::MEDIA_TYPE_VIDEO); |
| 1129 | auto new_sender = CreateSender(media_type, track, stream_labels); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1130 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1131 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1132 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1133 | const RtpSenderInfo* sender_info = |
| 1134 | FindSenderInfo(local_audio_sender_infos_, |
| 1135 | new_sender->internal()->stream_id(), track->id()); |
| 1136 | if (sender_info) { |
| 1137 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1138 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1139 | } else { |
| 1140 | RTC_DCHECK_EQ(MediaStreamTrackInterface::kVideoKind, track->kind()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1141 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1142 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1143 | const RtpSenderInfo* sender_info = |
| 1144 | FindSenderInfo(local_video_sender_infos_, |
| 1145 | new_sender->internal()->stream_id(), track->id()); |
| 1146 | if (sender_info) { |
| 1147 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1148 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1149 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1150 | return rtc::scoped_refptr<RtpSenderInterface>(new_sender); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1151 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1152 | |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1153 | RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> |
| 1154 | PeerConnection::AddTrackUnifiedPlan( |
| 1155 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1156 | const std::vector<std::string>& stream_labels) { |
| 1157 | auto transceiver = FindFirstTransceiverForAddedTrack(track); |
| 1158 | if (transceiver) { |
| 1159 | if (transceiver->direction() == RtpTransceiverDirection::kRecvOnly) { |
| 1160 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1161 | } else if (transceiver->direction() == RtpTransceiverDirection::kInactive) { |
| 1162 | transceiver->SetDirection(RtpTransceiverDirection::kSendOnly); |
| 1163 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1164 | transceiver->sender()->SetTrack(track); |
| 1165 | transceiver->internal()->sender_internal()->set_stream_ids(stream_labels); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1166 | } else { |
| 1167 | cricket::MediaType media_type = |
| 1168 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1169 | ? cricket::MEDIA_TYPE_AUDIO |
| 1170 | : cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1171 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1172 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1173 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1174 | transceiver->internal()->set_created_by_addtrack(true); |
| 1175 | transceiver->SetDirection(RtpTransceiverDirection::kSendRecv); |
| 1176 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1177 | return transceiver->sender(); |
| 1178 | } |
| 1179 | |
| 1180 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1181 | PeerConnection::FindFirstTransceiverForAddedTrack( |
| 1182 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1183 | RTC_DCHECK(track); |
| 1184 | for (auto transceiver : transceivers_) { |
| 1185 | if (!transceiver->sender()->track() && |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1186 | cricket::MediaTypeToString(transceiver->media_type()) == |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1187 | track->kind() && |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame^] | 1188 | !transceiver->internal()->has_ever_been_used_to_send() && |
| 1189 | !transceiver->stopped()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1190 | return transceiver; |
| 1191 | } |
| 1192 | } |
| 1193 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | bool PeerConnection::RemoveTrack(RtpSenderInterface* sender) { |
| 1197 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveTrack"); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1198 | return RemoveTrackInternal(sender).ok(); |
| 1199 | } |
| 1200 | |
| 1201 | RTCError PeerConnection::RemoveTrackInternal( |
| 1202 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1203 | if (!sender) { |
| 1204 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null."); |
| 1205 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1206 | if (IsClosed()) { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1207 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE, |
| 1208 | "PeerConnection is closed."); |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1209 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1210 | if (IsUnifiedPlan()) { |
| 1211 | auto transceiver = FindTransceiverBySender(sender); |
| 1212 | if (!transceiver || !sender->track()) { |
| 1213 | return RTCError::OK(); |
| 1214 | } |
| 1215 | sender->SetTrack(nullptr); |
| 1216 | if (transceiver->direction() == RtpTransceiverDirection::kSendRecv) { |
| 1217 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kRecvOnly); |
| 1218 | } else if (transceiver->direction() == RtpTransceiverDirection::kSendOnly) { |
| 1219 | transceiver->internal()->SetDirection(RtpTransceiverDirection::kInactive); |
| 1220 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1221 | } else { |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1222 | bool removed; |
| 1223 | if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 1224 | removed = GetAudioTransceiver()->internal()->RemoveSender(sender); |
| 1225 | } else { |
| 1226 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type()); |
| 1227 | removed = GetVideoTransceiver()->internal()->RemoveSender(sender); |
| 1228 | } |
| 1229 | if (!removed) { |
| 1230 | LOG_AND_RETURN_ERROR( |
| 1231 | RTCErrorType::INVALID_PARAMETER, |
| 1232 | "Couldn't find sender " + sender->id() + " to remove."); |
| 1233 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1234 | } |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1235 | observer_->OnRenegotiationNeeded(); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1236 | return RTCError::OK(); |
| 1237 | } |
| 1238 | |
| 1239 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1240 | PeerConnection::FindTransceiverBySender( |
| 1241 | rtc::scoped_refptr<RtpSenderInterface> sender) { |
| 1242 | for (auto transceiver : transceivers_) { |
| 1243 | if (transceiver->sender() == sender) { |
| 1244 | return transceiver; |
| 1245 | } |
| 1246 | } |
| 1247 | return nullptr; |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1250 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1251 | PeerConnection::AddTransceiver( |
| 1252 | rtc::scoped_refptr<MediaStreamTrackInterface> track) { |
| 1253 | return AddTransceiver(track, RtpTransceiverInit()); |
| 1254 | } |
| 1255 | |
| 1256 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1257 | PeerConnection::AddTransceiver( |
| 1258 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1259 | const RtpTransceiverInit& init) { |
| 1260 | if (!IsUnifiedPlan()) { |
| 1261 | LOG_AND_RETURN_ERROR( |
| 1262 | RTCErrorType::INTERNAL_ERROR, |
| 1263 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1264 | } |
| 1265 | if (!track) { |
| 1266 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "track is null"); |
| 1267 | } |
| 1268 | cricket::MediaType media_type; |
| 1269 | if (track->kind() == MediaStreamTrackInterface::kAudioKind) { |
| 1270 | media_type = cricket::MEDIA_TYPE_AUDIO; |
| 1271 | } else if (track->kind() == MediaStreamTrackInterface::kVideoKind) { |
| 1272 | media_type = cricket::MEDIA_TYPE_VIDEO; |
| 1273 | } else { |
| 1274 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1275 | "Track kind is not audio or video"); |
| 1276 | } |
| 1277 | return AddTransceiver(media_type, track, init); |
| 1278 | } |
| 1279 | |
| 1280 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1281 | PeerConnection::AddTransceiver(cricket::MediaType media_type) { |
| 1282 | return AddTransceiver(media_type, RtpTransceiverInit()); |
| 1283 | } |
| 1284 | |
| 1285 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1286 | PeerConnection::AddTransceiver(cricket::MediaType media_type, |
| 1287 | const RtpTransceiverInit& init) { |
| 1288 | if (!IsUnifiedPlan()) { |
| 1289 | LOG_AND_RETURN_ERROR( |
| 1290 | RTCErrorType::INTERNAL_ERROR, |
| 1291 | "AddTransceiver only supported when Unified Plan is enabled."); |
| 1292 | } |
| 1293 | if (!(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1294 | media_type == cricket::MEDIA_TYPE_VIDEO)) { |
| 1295 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 1296 | "media type is not audio or video"); |
| 1297 | } |
| 1298 | return AddTransceiver(media_type, nullptr, init); |
| 1299 | } |
| 1300 | |
| 1301 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1302 | PeerConnection::AddTransceiver( |
| 1303 | cricket::MediaType media_type, |
| 1304 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1305 | const RtpTransceiverInit& init, |
| 1306 | bool fire_callback) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1307 | RTC_DCHECK((media_type == cricket::MEDIA_TYPE_AUDIO || |
| 1308 | media_type == cricket::MEDIA_TYPE_VIDEO)); |
| 1309 | if (track) { |
| 1310 | RTC_DCHECK_EQ(media_type, |
| 1311 | (track->kind() == MediaStreamTrackInterface::kAudioKind |
| 1312 | ? cricket::MEDIA_TYPE_AUDIO |
| 1313 | : cricket::MEDIA_TYPE_VIDEO)); |
| 1314 | } |
| 1315 | |
| 1316 | // TODO(bugs.webrtc.org/7600): Verify init. |
| 1317 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1318 | if (init.stream_labels.size() > 1u) { |
| 1319 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1320 | "More than one stream is not yet supported."); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1323 | std::vector<std::string> stream_labels = {!init.stream_labels.empty() |
| 1324 | ? init.stream_labels[0] |
| 1325 | : rtc::CreateRandomUuid()}; |
| 1326 | |
| 1327 | auto sender = CreateSender(media_type, track, stream_labels); |
| 1328 | auto receiver = CreateReceiver(media_type, rtc::CreateRandomUuid()); |
| 1329 | auto transceiver = CreateAndAddTransceiver(sender, receiver); |
| 1330 | transceiver->internal()->set_direction(init.direction); |
| 1331 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1332 | if (fire_callback) { |
| 1333 | observer_->OnRenegotiationNeeded(); |
| 1334 | } |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1335 | |
| 1336 | return rtc::scoped_refptr<RtpTransceiverInterface>(transceiver); |
| 1337 | } |
| 1338 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1339 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 1340 | PeerConnection::CreateSender( |
| 1341 | cricket::MediaType media_type, |
| 1342 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 1343 | const std::vector<std::string>& stream_labels) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1344 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender; |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1345 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
| 1346 | RTC_DCHECK(!track || |
| 1347 | (track->kind() == MediaStreamTrackInterface::kAudioKind)); |
| 1348 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1349 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1350 | new AudioRtpSender(worker_thread(), |
| 1351 | static_cast<AudioTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1352 | stream_labels, stats_.get())); |
| 1353 | } else { |
| 1354 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
| 1355 | RTC_DCHECK(!track || |
| 1356 | (track->kind() == MediaStreamTrackInterface::kVideoKind)); |
| 1357 | sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
| 1358 | signaling_thread(), |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1359 | new VideoRtpSender(worker_thread(), |
| 1360 | static_cast<VideoTrackInterface*>(track.get()), |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1361 | stream_labels)); |
| 1362 | } |
| 1363 | sender->internal()->set_stream_ids(stream_labels); |
| 1364 | return sender; |
| 1365 | } |
| 1366 | |
| 1367 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1368 | PeerConnection::CreateReceiver(cricket::MediaType media_type, |
| 1369 | const std::string& receiver_id) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1370 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1371 | receiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1372 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1373 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
Steve Anton | 6077675 | 2018-01-10 11:51:34 -0800 | [diff] [blame] | 1374 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1375 | new AudioRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1376 | } else { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1377 | RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1378 | receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 1379 | signaling_thread(), |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1380 | new VideoRtpReceiver(worker_thread(), receiver_id, {})); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1381 | } |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1382 | return receiver; |
| 1383 | } |
| 1384 | |
| 1385 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 1386 | PeerConnection::CreateAndAddTransceiver( |
| 1387 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> sender, |
| 1388 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 1389 | receiver) { |
| 1390 | auto transceiver = RtpTransceiverProxyWithInternal<RtpTransceiver>::Create( |
| 1391 | signaling_thread(), new RtpTransceiver(sender, receiver)); |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1392 | transceivers_.push_back(transceiver); |
Steve Anton | f9381f0 | 2017-12-14 10:23:57 -0800 | [diff] [blame] | 1393 | return transceiver; |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1394 | } |
| 1395 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1396 | rtc::scoped_refptr<DtmfSenderInterface> PeerConnection::CreateDtmfSender( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1397 | AudioTrackInterface* track) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1398 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDtmfSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1399 | if (IsClosed()) { |
| 1400 | return nullptr; |
| 1401 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1402 | if (!track) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1403 | RTC_LOG(LS_ERROR) << "CreateDtmfSender - track is NULL."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1404 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1405 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1406 | auto track_sender = FindSenderForTrack(track); |
| 1407 | if (!track_sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1408 | RTC_LOG(LS_ERROR) << "CreateDtmfSender called with a non-added track."; |
deadbeef | 20cb0c1 | 2017-02-01 20:27:00 -0800 | [diff] [blame] | 1409 | return nullptr; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1412 | return track_sender->GetDtmfSender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1415 | rtc::scoped_refptr<RtpSenderInterface> PeerConnection::CreateSender( |
deadbeef | bd7d8f7 | 2015-12-18 16:58:44 -0800 | [diff] [blame] | 1416 | const std::string& kind, |
| 1417 | const std::string& stream_id) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1418 | TRACE_EVENT0("webrtc", "PeerConnection::CreateSender"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 1419 | if (IsClosed()) { |
| 1420 | return nullptr; |
| 1421 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1422 | |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1423 | std::vector<std::string> stream_labels; |
| 1424 | if (!stream_id.empty()) { |
| 1425 | stream_labels.push_back(stream_id); |
| 1426 | } |
| 1427 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1428 | // TODO(steveanton): Move construction of the RtpSenders to RtpTransceiver. |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1429 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1430 | if (kind == MediaStreamTrackInterface::kAudioKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1431 | auto* audio_sender = new AudioRtpSender(worker_thread(), nullptr, |
| 1432 | stream_labels, stats_.get()); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1433 | audio_sender->SetVoiceMediaChannel(voice_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1434 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1435 | signaling_thread(), audio_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1436 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1437 | } else if (kind == MediaStreamTrackInterface::kVideoKind) { |
Steve Anton | 47136dd | 2018-01-12 10:49:35 -0800 | [diff] [blame] | 1438 | auto* video_sender = |
| 1439 | new VideoRtpSender(worker_thread(), nullptr, stream_labels); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 1440 | video_sender->SetVideoMediaChannel(video_media_channel()); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1441 | new_sender = RtpSenderProxyWithInternal<RtpSenderInternal>::Create( |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 1442 | signaling_thread(), video_sender); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1443 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1444 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1445 | RTC_LOG(LS_ERROR) << "CreateSender called with invalid kind: " << kind; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1446 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1447 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1448 | |
deadbeef | e1f9d83 | 2016-01-14 15:35:42 -0800 | [diff] [blame] | 1449 | return new_sender; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1452 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::GetSenders() |
| 1453 | const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1454 | std::vector<rtc::scoped_refptr<RtpSenderInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1455 | for (auto sender : GetSendersInternal()) { |
| 1456 | ret.push_back(sender); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1457 | } |
| 1458 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1461 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1462 | PeerConnection::GetSendersInternal() const { |
| 1463 | std::vector<rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>> |
| 1464 | all_senders; |
| 1465 | for (auto transceiver : transceivers_) { |
| 1466 | auto senders = transceiver->internal()->senders(); |
| 1467 | all_senders.insert(all_senders.end(), senders.begin(), senders.end()); |
| 1468 | } |
| 1469 | return all_senders; |
| 1470 | } |
| 1471 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1472 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> |
| 1473 | PeerConnection::GetReceivers() const { |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1474 | std::vector<rtc::scoped_refptr<RtpReceiverInterface>> ret; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1475 | for (const auto& receiver : GetReceiversInternal()) { |
| 1476 | ret.push_back(receiver); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 1477 | } |
| 1478 | return ret; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 1481 | std::vector< |
| 1482 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1483 | PeerConnection::GetReceiversInternal() const { |
| 1484 | std::vector< |
| 1485 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>> |
| 1486 | all_receivers; |
| 1487 | for (auto transceiver : transceivers_) { |
| 1488 | auto receivers = transceiver->internal()->receivers(); |
| 1489 | all_receivers.insert(all_receivers.end(), receivers.begin(), |
| 1490 | receivers.end()); |
| 1491 | } |
| 1492 | return all_receivers; |
| 1493 | } |
| 1494 | |
Steve Anton | 9158ef6 | 2017-11-27 13:01:52 -0800 | [diff] [blame] | 1495 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 1496 | PeerConnection::GetTransceivers() const { |
| 1497 | RTC_DCHECK(IsUnifiedPlan()); |
| 1498 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> all_transceivers; |
| 1499 | for (auto transceiver : transceivers_) { |
| 1500 | all_transceivers.push_back(transceiver); |
| 1501 | } |
| 1502 | return all_transceivers; |
| 1503 | } |
| 1504 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1505 | bool PeerConnection::GetStats(StatsObserver* observer, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 1506 | MediaStreamTrackInterface* track, |
| 1507 | StatsOutputLevel level) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1508 | TRACE_EVENT0("webrtc", "PeerConnection::GetStats"); |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 1509 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1510 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1511 | RTC_LOG(LS_ERROR) << "GetStats - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1512 | return false; |
| 1513 | } |
| 1514 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1515 | stats_->UpdateStats(level); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1516 | // The StatsCollector is used to tell if a track is valid because it may |
| 1517 | // remember tracks that the PeerConnection previously removed. |
| 1518 | if (track && !stats_->IsValidTrack(track->id())) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1519 | RTC_LOG(LS_WARNING) << "GetStats is called with an invalid track: " |
| 1520 | << track->id(); |
zhihuang | e9e94c3 | 2016-11-04 11:38:15 -0700 | [diff] [blame] | 1521 | return false; |
| 1522 | } |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1523 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_GETSTATS, |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 1524 | new GetStatsMsg(observer, track)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1525 | return true; |
| 1526 | } |
| 1527 | |
hbos | 74e1a4f | 2016-09-15 23:33:01 -0700 | [diff] [blame] | 1528 | void PeerConnection::GetStats(RTCStatsCollectorCallback* callback) { |
| 1529 | RTC_DCHECK(stats_collector_); |
| 1530 | stats_collector_->GetStatsReport(callback); |
| 1531 | } |
| 1532 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1533 | PeerConnectionInterface::SignalingState PeerConnection::signaling_state() { |
| 1534 | return signaling_state_; |
| 1535 | } |
| 1536 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1537 | PeerConnectionInterface::IceConnectionState |
| 1538 | PeerConnection::ice_connection_state() { |
| 1539 | return ice_connection_state_; |
| 1540 | } |
| 1541 | |
| 1542 | PeerConnectionInterface::IceGatheringState |
| 1543 | PeerConnection::ice_gathering_state() { |
| 1544 | return ice_gathering_state_; |
| 1545 | } |
| 1546 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1547 | rtc::scoped_refptr<DataChannelInterface> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1548 | PeerConnection::CreateDataChannel( |
| 1549 | const std::string& label, |
| 1550 | const DataChannelInit* config) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1551 | TRACE_EVENT0("webrtc", "PeerConnection::CreateDataChannel"); |
zhihuang | 9763d56 | 2016-08-05 11:14:50 -0700 | [diff] [blame] | 1552 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1553 | bool first_datachannel = !HasDataChannels(); |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1554 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 1555 | std::unique_ptr<InternalDataChannelInit> internal_config; |
henrika@webrtc.org | aebb1ad | 2014-01-14 10:00:58 +0000 | [diff] [blame] | 1556 | if (config) { |
| 1557 | internal_config.reset(new InternalDataChannelInit(*config)); |
| 1558 | } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 1559 | rtc::scoped_refptr<DataChannelInterface> channel( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1560 | InternalCreateDataChannel(label, internal_config.get())); |
| 1561 | if (!channel.get()) { |
| 1562 | return nullptr; |
| 1563 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1564 | |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1565 | // Trigger the onRenegotiationNeeded event for every new RTP DataChannel, or |
| 1566 | // the first SCTP DataChannel. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1567 | if (data_channel_type() == cricket::DCT_RTP || first_datachannel) { |
jiayl@webrtc.org | 001fd2d | 2014-05-29 15:31:11 +0000 | [diff] [blame] | 1568 | observer_->OnRenegotiationNeeded(); |
| 1569 | } |
wu@webrtc.org | 91053e7 | 2013-08-10 07:18:04 +0000 | [diff] [blame] | 1570 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1571 | return DataChannelProxy::Create(signaling_thread(), channel.get()); |
| 1572 | } |
| 1573 | |
| 1574 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1575 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1576 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1577 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1578 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1579 | // Always create an offer even if |ConvertConstraintsToOfferAnswerOptions| |
| 1580 | // returns false for now. Because |ConvertConstraintsToOfferAnswerOptions| |
| 1581 | // compares the mandatory fields parsed with the mandatory fields added in the |
| 1582 | // |constraints| and some downstream applications might create offers with |
| 1583 | // mandatory fields which would not be parsed in the helper method. For |
| 1584 | // example, in Chromium/remoting, |kEnableDtlsSrtp| is added to the |
| 1585 | // |constraints| as a mandatory field but it is not parsed. |
| 1586 | ConvertConstraintsToOfferAnswerOptions(constraints, &offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1587 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1588 | CreateOffer(observer, offer_answer_options); |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | void PeerConnection::CreateOffer(CreateSessionDescriptionObserver* observer, |
| 1592 | const RTCOfferAnswerOptions& options) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1593 | TRACE_EVENT0("webrtc", "PeerConnection::CreateOffer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1594 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1595 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1596 | RTC_LOG(LS_ERROR) << "CreateOffer - observer is NULL."; |
jiayl@webrtc.org | b18bf5e | 2014-08-04 18:34:16 +0000 | [diff] [blame] | 1597 | return; |
| 1598 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1599 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1600 | if (IsClosed()) { |
| 1601 | std::string error = "CreateOffer called when PeerConnection is closed."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1602 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1603 | PostCreateSessionDescriptionFailure(observer, error); |
| 1604 | return; |
| 1605 | } |
| 1606 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1607 | if (!ValidateOfferAnswerOptions(options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1608 | std::string error = "CreateOffer called with invalid options."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1609 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1610 | PostCreateSessionDescriptionFailure(observer, error); |
| 1611 | return; |
| 1612 | } |
| 1613 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1614 | // Legacy handling for offer_to_receive_audio and offer_to_receive_video. |
| 1615 | // Specified in WebRTC section 4.4.3.2 "Legacy configuration extensions". |
| 1616 | if (IsUnifiedPlan()) { |
| 1617 | RTCError error = HandleLegacyOfferOptions(options); |
| 1618 | if (!error.ok()) { |
| 1619 | PostCreateSessionDescriptionFailure(observer, error.message()); |
| 1620 | return; |
| 1621 | } |
| 1622 | } |
| 1623 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1624 | cricket::MediaSessionOptions session_options; |
| 1625 | GetOptionsForOffer(options, &session_options); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1626 | webrtc_session_desc_factory_->CreateOffer(observer, options, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1629 | RTCError PeerConnection::HandleLegacyOfferOptions( |
| 1630 | const RTCOfferAnswerOptions& options) { |
| 1631 | RTC_DCHECK(IsUnifiedPlan()); |
| 1632 | |
| 1633 | if (options.offer_to_receive_audio == 0) { |
| 1634 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1635 | cricket::MEDIA_TYPE_AUDIO); |
| 1636 | } else if (options.offer_to_receive_audio == 1) { |
| 1637 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_AUDIO); |
| 1638 | } else if (options.offer_to_receive_audio > 1) { |
| 1639 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1640 | "offer_to_receive_audio > 1 is not supported."); |
| 1641 | } |
| 1642 | |
| 1643 | if (options.offer_to_receive_video == 0) { |
| 1644 | RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1645 | cricket::MEDIA_TYPE_VIDEO); |
| 1646 | } else if (options.offer_to_receive_video == 1) { |
| 1647 | AddUpToOneReceivingTransceiverOfType(cricket::MEDIA_TYPE_VIDEO); |
| 1648 | } else if (options.offer_to_receive_video > 1) { |
| 1649 | LOG_AND_RETURN_ERROR(RTCErrorType::UNSUPPORTED_PARAMETER, |
| 1650 | "offer_to_receive_video > 1 is not supported."); |
| 1651 | } |
| 1652 | |
| 1653 | return RTCError::OK(); |
| 1654 | } |
| 1655 | |
| 1656 | void PeerConnection::RemoveRecvDirectionFromReceivingTransceiversOfType( |
| 1657 | cricket::MediaType media_type) { |
| 1658 | for (auto transceiver : GetReceivingTransceiversOfType(media_type)) { |
| 1659 | transceiver->internal()->set_direction( |
| 1660 | RtpTransceiverDirectionWithRecvSet(transceiver->direction(), false)); |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | void PeerConnection::AddUpToOneReceivingTransceiverOfType( |
| 1665 | cricket::MediaType media_type) { |
| 1666 | if (GetReceivingTransceiversOfType(media_type).empty()) { |
| 1667 | RtpTransceiverInit init; |
| 1668 | init.direction = RtpTransceiverDirection::kRecvOnly; |
| 1669 | AddTransceiver(media_type, nullptr, init, /*fire_callback=*/false); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | std::vector<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1674 | PeerConnection::GetReceivingTransceiversOfType(cricket::MediaType media_type) { |
| 1675 | std::vector< |
| 1676 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 1677 | receiving_transceivers; |
| 1678 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 1679 | if (!transceiver->stopped() && transceiver->media_type() == media_type && |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1680 | RtpTransceiverDirectionHasRecv(transceiver->direction())) { |
| 1681 | receiving_transceivers.push_back(transceiver); |
| 1682 | } |
| 1683 | } |
| 1684 | return receiving_transceivers; |
| 1685 | } |
| 1686 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1687 | void PeerConnection::CreateAnswer( |
| 1688 | CreateSessionDescriptionObserver* observer, |
| 1689 | const MediaConstraintsInterface* constraints) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1690 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1691 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1692 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1693 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1696 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1697 | PeerConnectionInterface::RTCOfferAnswerOptions offer_answer_options; |
| 1698 | if (!ConvertConstraintsToOfferAnswerOptions(constraints, |
| 1699 | &offer_answer_options)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1700 | std::string error = "CreateAnswer called with invalid constraints."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1701 | RTC_LOG(LS_ERROR) << error; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1702 | PostCreateSessionDescriptionFailure(observer, error); |
| 1703 | return; |
| 1704 | } |
| 1705 | |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1706 | CreateAnswer(observer, offer_answer_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | void PeerConnection::CreateAnswer(CreateSessionDescriptionObserver* observer, |
| 1710 | const RTCOfferAnswerOptions& options) { |
| 1711 | TRACE_EVENT0("webrtc", "PeerConnection::CreateAnswer"); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1712 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1713 | RTC_LOG(LS_ERROR) << "CreateAnswer - observer is NULL."; |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1717 | if (!(signaling_state_ == kHaveRemoteOffer || |
| 1718 | signaling_state_ == kHaveLocalPrAnswer)) { |
| 1719 | std::string error = |
| 1720 | "PeerConnection cannot create an answer in a state other than " |
| 1721 | "have-remote-offer or have-local-pranswer."; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1722 | RTC_LOG(LS_ERROR) << error; |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1723 | PostCreateSessionDescriptionFailure(observer, error); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 1727 | // The remote description should be set if we're in the right state. |
| 1728 | RTC_DCHECK(remote_description()); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1729 | |
Steve Anton | 22da89f | 2018-01-25 13:58:07 -0800 | [diff] [blame] | 1730 | if (IsUnifiedPlan()) { |
| 1731 | if (options.offer_to_receive_audio != RTCOfferAnswerOptions::kUndefined) { |
| 1732 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_audio is not " |
| 1733 | "supported with Unified Plan semantics. Use the " |
| 1734 | "RtpTransceiver API instead."; |
| 1735 | } |
| 1736 | if (options.offer_to_receive_video != RTCOfferAnswerOptions::kUndefined) { |
| 1737 | RTC_LOG(LS_WARNING) << "CreateAnswer: offer_to_receive_video is not " |
| 1738 | "supported with Unified Plan semantics. Use the " |
| 1739 | "RtpTransceiver API instead."; |
| 1740 | } |
| 1741 | } |
| 1742 | |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1743 | cricket::MediaSessionOptions session_options; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 1744 | GetOptionsForAnswer(options, &session_options); |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 1745 | |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 1746 | webrtc_session_desc_factory_->CreateAnswer(observer, session_options); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | void PeerConnection::SetLocalDescription( |
| 1750 | SetSessionDescriptionObserver* observer, |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1751 | SessionDescriptionInterface* desc_ptr) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 1752 | TRACE_EVENT0("webrtc", "PeerConnection::SetLocalDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1753 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1754 | // The SetLocalDescription contract is that we take ownership of the session |
| 1755 | // description regardless of the outcome, so wrap it in a unique_ptr right |
| 1756 | // away. Ideally, SetLocalDescription's signature will be changed to take the |
| 1757 | // description as a unique_ptr argument to formalize this agreement. |
| 1758 | std::unique_ptr<SessionDescriptionInterface> desc(desc_ptr); |
| 1759 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 1760 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1761 | RTC_LOG(LS_ERROR) << "SetLocalDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1762 | return; |
| 1763 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1764 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1765 | if (!desc) { |
| 1766 | PostSetSessionDescriptionFailure(observer, "SessionDescription is NULL."); |
| 1767 | return; |
| 1768 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1769 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1770 | // If a session error has occurred the PeerConnection is in a possibly |
| 1771 | // inconsistent state so fail right away. |
| 1772 | if (session_error() != SessionError::kNone) { |
| 1773 | std::string error_message = GetSessionErrorMsg(); |
| 1774 | RTC_LOG(LS_ERROR) << "SetLocalDescription: " << error_message; |
| 1775 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1776 | return; |
| 1777 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1778 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1779 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_LOCAL); |
| 1780 | if (!error.ok()) { |
| 1781 | std::string error_message = GetSetDescriptionErrorMessage( |
| 1782 | cricket::CS_LOCAL, desc->GetType(), error); |
| 1783 | RTC_LOG(LS_ERROR) << error_message; |
| 1784 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
| 1785 | return; |
| 1786 | } |
| 1787 | |
| 1788 | // Grab the description type before moving ownership to ApplyLocalDescription, |
| 1789 | // which may destroy it before returning. |
| 1790 | const SdpType type = desc->GetType(); |
| 1791 | |
| 1792 | error = ApplyLocalDescription(std::move(desc)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1793 | // |desc| may be destroyed at this point. |
| 1794 | |
| 1795 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1796 | // If ApplyLocalDescription fails, the PeerConnection could be in an |
| 1797 | // inconsistent state, so act conservatively here and set the session error |
| 1798 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 1799 | SetSessionError(SessionError::kContent, error.message()); |
| 1800 | std::string error_message = |
| 1801 | GetSetDescriptionErrorMessage(cricket::CS_LOCAL, type, error); |
| 1802 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1803 | PostSetSessionDescriptionFailure(observer, std::move(error_message)); |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 1804 | return; |
| 1805 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1806 | RTC_DCHECK(local_description()); |
| 1807 | |
| 1808 | PostSetSessionDescriptionSuccess(observer); |
| 1809 | |
| 1810 | // According to JSEP, after setLocalDescription, changing the candidate pool |
| 1811 | // size is not allowed, and changing the set of ICE servers will not result |
| 1812 | // in new candidates being gathered. |
| 1813 | port_allocator_->FreezeCandidatePool(); |
| 1814 | |
| 1815 | // MaybeStartGathering needs to be called after posting |
| 1816 | // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates |
| 1817 | // before signaling that SetLocalDescription completed. |
| 1818 | transport_controller_->MaybeStartGathering(); |
| 1819 | |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 1820 | if (local_description()->GetType() == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1821 | // TODO(deadbeef): We already had to hop to the network thread for |
| 1822 | // MaybeStartGathering... |
| 1823 | network_thread()->Invoke<void>( |
| 1824 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 1825 | port_allocator_.get())); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | RTCError PeerConnection::ApplyLocalDescription( |
| 1830 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 1831 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 1832 | RTC_DCHECK(desc); |
| 1833 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1834 | // Update stats here so that we have the most recent stats for tracks and |
| 1835 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 1836 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1837 | |
| 1838 | // Update the initial_offerer flag if this session is the initial_offerer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1839 | SdpType type = desc->GetType(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1840 | if (!initial_offerer_.has_value()) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1841 | initial_offerer_.emplace(type == SdpType::kOffer); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1842 | if (*initial_offerer_) { |
| 1843 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLING); |
| 1844 | } else { |
| 1845 | transport_controller_->SetIceRole(cricket::ICEROLE_CONTROLLED); |
| 1846 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1847 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1848 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1849 | // Take a reference to the old local description since it's used below to |
| 1850 | // compare against the new local description. When setting the new local |
| 1851 | // description, grab ownership of the replaced session description in case it |
| 1852 | // is the same as |old_local_description|, to keep it alive for the duration |
| 1853 | // of the method. |
| 1854 | const SessionDescriptionInterface* old_local_description = |
| 1855 | local_description(); |
| 1856 | std::unique_ptr<SessionDescriptionInterface> replaced_local_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 1857 | if (type == SdpType::kAnswer) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1858 | replaced_local_description = pending_local_description_ |
| 1859 | ? std::move(pending_local_description_) |
| 1860 | : std::move(current_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1861 | current_local_description_ = std::move(desc); |
| 1862 | pending_local_description_ = nullptr; |
| 1863 | current_remote_description_ = std::move(pending_remote_description_); |
| 1864 | } else { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1865 | replaced_local_description = std::move(pending_local_description_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1866 | pending_local_description_ = std::move(desc); |
| 1867 | } |
| 1868 | // The session description to apply now must be accessed by |
| 1869 | // |local_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1870 | RTC_DCHECK(local_description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1871 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1872 | if (IsUnifiedPlan()) { |
| 1873 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 1874 | cricket::CS_LOCAL, *local_description(), old_local_description, |
| 1875 | remote_description()); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1876 | if (!error.ok()) { |
| 1877 | return error; |
| 1878 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1879 | for (auto transceiver : transceivers_) { |
| 1880 | const ContentInfo* content = |
| 1881 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1882 | if (!content) { |
| 1883 | continue; |
| 1884 | } |
| 1885 | const MediaContentDescription* media_desc = content->media_description(); |
| 1886 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 1887 | transceiver->internal()->set_current_direction(media_desc->direction()); |
| 1888 | } |
| 1889 | if (content->rejected && !transceiver->stopped()) { |
| 1890 | transceiver->Stop(); |
| 1891 | } |
| 1892 | } |
| 1893 | } else { |
| 1894 | // Transport and Media channels will be created only when offer is set. |
| 1895 | if (type == SdpType::kOffer) { |
| 1896 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 1897 | // description is applied. Restore back to old description. |
| 1898 | RTCError error = CreateChannels(*local_description()->description()); |
| 1899 | if (!error.ok()) { |
| 1900 | return error; |
| 1901 | } |
| 1902 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1903 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1904 | // Remove unused channels if MediaContentDescription is rejected. |
| 1905 | RemoveUnusedChannels(local_description()->description()); |
| 1906 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1907 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 1908 | RTCError error = UpdateSessionState(type, cricket::CS_LOCAL); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1909 | if (!error.ok()) { |
| 1910 | return error; |
| 1911 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1912 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1913 | if (remote_description()) { |
| 1914 | // Now that we have a local description, we can push down remote candidates. |
| 1915 | UseCandidatesInSessionDescription(remote_description()); |
| 1916 | } |
| 1917 | |
| 1918 | pending_ice_restarts_.clear(); |
| 1919 | if (session_error() != SessionError::kNone) { |
| 1920 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 1921 | } |
| 1922 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1923 | // If setting the description decided our SSL role, allocate any necessary |
| 1924 | // SCTP sids. |
| 1925 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 1926 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1927 | AllocateSctpSids(role); |
| 1928 | } |
| 1929 | |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1930 | if (IsUnifiedPlan()) { |
| 1931 | for (auto transceiver : transceivers_) { |
| 1932 | const ContentInfo* content = |
| 1933 | FindMediaSectionForTransceiver(transceiver, local_description()); |
| 1934 | if (!content) { |
| 1935 | continue; |
| 1936 | } |
| 1937 | if (content->rejected && !transceiver->stopped()) { |
| 1938 | transceiver->Stop(); |
| 1939 | } |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1940 | const auto& streams = content->media_description()->streams(); |
| 1941 | if (!content->rejected && !streams.empty()) { |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1942 | transceiver->internal()->sender_internal()->set_stream_ids( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1943 | {streams[0].sync_label}); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1944 | transceiver->internal()->sender_internal()->SetSsrc( |
Steve Anton | 74255ff | 2018-01-24 18:32:57 -0800 | [diff] [blame] | 1945 | streams[0].first_ssrc()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 1946 | } |
| 1947 | } |
| 1948 | } else { |
| 1949 | // Plan B semantics. |
| 1950 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1951 | // Update state and SSRC of local MediaStreams and DataChannels based on the |
| 1952 | // local session description. |
| 1953 | const cricket::ContentInfo* audio_content = |
| 1954 | GetFirstAudioContent(local_description()->description()); |
| 1955 | if (audio_content) { |
| 1956 | if (audio_content->rejected) { |
| 1957 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 1958 | } else { |
| 1959 | const cricket::AudioContentDescription* audio_desc = |
| 1960 | audio_content->media_description()->as_audio(); |
| 1961 | UpdateLocalSenders(audio_desc->streams(), audio_desc->type()); |
| 1962 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1963 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1964 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 1965 | const cricket::ContentInfo* video_content = |
| 1966 | GetFirstVideoContent(local_description()->description()); |
| 1967 | if (video_content) { |
| 1968 | if (video_content->rejected) { |
| 1969 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 1970 | } else { |
| 1971 | const cricket::VideoContentDescription* video_desc = |
| 1972 | video_content->media_description()->as_video(); |
| 1973 | UpdateLocalSenders(video_desc->streams(), video_desc->type()); |
| 1974 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 1975 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | const cricket::ContentInfo* data_content = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 1979 | GetFirstDataContent(local_description()->description()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1980 | if (data_content) { |
| 1981 | const cricket::DataContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 1982 | data_content->media_description()->as_data(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 1983 | if (rtc::starts_with(data_desc->protocol().data(), |
| 1984 | cricket::kMediaProtocolRtpPrefix)) { |
| 1985 | UpdateLocalRtpDataChannels(data_desc->streams()); |
| 1986 | } |
| 1987 | } |
| 1988 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 1989 | return RTCError::OK(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | void PeerConnection::SetRemoteDescription( |
Henrik Boström | a4ecf55 | 2017-11-23 14:17:07 +0000 | [diff] [blame] | 1993 | SetSessionDescriptionObserver* observer, |
| 1994 | SessionDescriptionInterface* desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 1995 | SetRemoteDescription( |
| 1996 | std::unique_ptr<SessionDescriptionInterface>(desc), |
| 1997 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface>( |
| 1998 | new SetRemoteDescriptionObserverAdapter(this, observer))); |
| 1999 | } |
| 2000 | |
| 2001 | void PeerConnection::SetRemoteDescription( |
| 2002 | std::unique_ptr<SessionDescriptionInterface> desc, |
| 2003 | rtc::scoped_refptr<SetRemoteDescriptionObserverInterface> observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2004 | TRACE_EVENT0("webrtc", "PeerConnection::SetRemoteDescription"); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2005 | |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 2006 | if (!observer) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2007 | RTC_LOG(LS_ERROR) << "SetRemoteDescription - observer is NULL."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2010 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2011 | if (!desc) { |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 2012 | observer->OnSetRemoteDescriptionComplete(RTCError( |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2013 | RTCErrorType::INVALID_PARAMETER, "SessionDescription is NULL.")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2014 | return; |
| 2015 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2016 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2017 | // If a session error has occurred the PeerConnection is in a possibly |
| 2018 | // inconsistent state so fail right away. |
| 2019 | if (session_error() != SessionError::kNone) { |
| 2020 | std::string error_message = GetSessionErrorMsg(); |
| 2021 | RTC_LOG(LS_ERROR) << "SetRemoteDescription: " << error_message; |
| 2022 | observer->OnSetRemoteDescriptionComplete( |
| 2023 | RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message))); |
| 2024 | return; |
| 2025 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2026 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2027 | RTCError error = ValidateSessionDescription(desc.get(), cricket::CS_REMOTE); |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2028 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2029 | std::string error_message = GetSetDescriptionErrorMessage( |
| 2030 | cricket::CS_REMOTE, desc->GetType(), error); |
| 2031 | RTC_LOG(LS_ERROR) << error_message; |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2032 | observer->OnSetRemoteDescriptionComplete( |
| 2033 | RTCError(error.type(), std::move(error_message))); |
| 2034 | return; |
| 2035 | } |
Steve Anton | 71439a6 | 2018-02-15 11:53:06 -0800 | [diff] [blame] | 2036 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2037 | // Grab the description type before moving ownership to |
| 2038 | // ApplyRemoteDescription, which may destroy it before returning. |
| 2039 | const SdpType type = desc->GetType(); |
| 2040 | |
| 2041 | error = ApplyRemoteDescription(std::move(desc)); |
| 2042 | // |desc| may be destroyed at this point. |
| 2043 | |
| 2044 | if (!error.ok()) { |
| 2045 | // If ApplyRemoteDescription fails, the PeerConnection could be in an |
| 2046 | // inconsistent state, so act conservatively here and set the session error |
| 2047 | // so that future calls to SetLocalDescription/SetRemoteDescription fail. |
| 2048 | SetSessionError(SessionError::kContent, error.message()); |
| 2049 | std::string error_message = |
| 2050 | GetSetDescriptionErrorMessage(cricket::CS_REMOTE, type, error); |
| 2051 | RTC_LOG(LS_ERROR) << error_message; |
| 2052 | observer->OnSetRemoteDescriptionComplete( |
| 2053 | RTCError(error.type(), std::move(error_message))); |
| 2054 | return; |
| 2055 | } |
| 2056 | RTC_DCHECK(remote_description()); |
| 2057 | |
| 2058 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2059 | // TODO(deadbeef): We already had to hop to the network thread for |
| 2060 | // MaybeStartGathering... |
| 2061 | network_thread()->Invoke<void>( |
| 2062 | RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 2063 | port_allocator_.get())); |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2064 | // Make UMA notes about what was agreed to. |
| 2065 | if (uma_observer_) { |
| 2066 | switch (remote_description()->description()->msid_signaling()) { |
| 2067 | case 0: |
| 2068 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2069 | kSdpSemanticNegotiatedNone, |
| 2070 | kSdpSemanticNegotiatedMax); |
| 2071 | break; |
| 2072 | case cricket::kMsidSignalingMediaSection: |
| 2073 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2074 | kSdpSemanticNegotiatedUnifiedPlan, |
| 2075 | kSdpSemanticNegotiatedMax); |
| 2076 | break; |
| 2077 | case cricket::kMsidSignalingSsrcAttribute: |
| 2078 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2079 | kSdpSemanticNegotiatedPlanB, |
| 2080 | kSdpSemanticNegotiatedMax); |
| 2081 | break; |
| 2082 | case cricket::kMsidSignalingMediaSection | |
| 2083 | cricket::kMsidSignalingSsrcAttribute: |
| 2084 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated, |
| 2085 | kSdpSemanticNegotiatedMixed, |
| 2086 | kSdpSemanticNegotiatedMax); |
| 2087 | break; |
| 2088 | default: |
| 2089 | RTC_NOTREACHED(); |
| 2090 | } |
| 2091 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | observer->OnSetRemoteDescriptionComplete(RTCError::OK()); |
| 2095 | } |
| 2096 | |
| 2097 | RTCError PeerConnection::ApplyRemoteDescription( |
| 2098 | std::unique_ptr<SessionDescriptionInterface> desc) { |
| 2099 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 2100 | RTC_DCHECK(desc); |
| 2101 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2102 | // Update stats here so that we have the most recent stats for tracks and |
| 2103 | // streams that might be removed by updating the session description. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 2104 | stats_->UpdateStats(kStatsOutputLevelStandard); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2105 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2106 | // Take a reference to the old remote description since it's used below to |
| 2107 | // compare against the new remote description. When setting the new remote |
| 2108 | // description, grab ownership of the replaced session description in case it |
| 2109 | // is the same as |old_remote_description|, to keep it alive for the duration |
| 2110 | // of the method. |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2111 | const SessionDescriptionInterface* old_remote_description = |
| 2112 | remote_description(); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2113 | std::unique_ptr<SessionDescriptionInterface> replaced_remote_description; |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2114 | SdpType type = desc->GetType(); |
| 2115 | if (type == SdpType::kAnswer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2116 | replaced_remote_description = pending_remote_description_ |
| 2117 | ? std::move(pending_remote_description_) |
| 2118 | : std::move(current_remote_description_); |
| 2119 | current_remote_description_ = std::move(desc); |
| 2120 | pending_remote_description_ = nullptr; |
| 2121 | current_local_description_ = std::move(pending_local_description_); |
| 2122 | } else { |
| 2123 | replaced_remote_description = std::move(pending_remote_description_); |
| 2124 | pending_remote_description_ = std::move(desc); |
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 | // The session description to apply now must be accessed by |
| 2127 | // |remote_description()|. |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2128 | RTC_DCHECK(remote_description()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2129 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2130 | // Transport and Media channels will be created only when offer is set. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2131 | if (IsUnifiedPlan()) { |
| 2132 | RTCError error = UpdateTransceiversAndDataChannels( |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2133 | cricket::CS_REMOTE, *remote_description(), local_description(), |
| 2134 | old_remote_description); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2135 | if (!error.ok()) { |
| 2136 | return error; |
| 2137 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2138 | } else { |
| 2139 | if (type == SdpType::kOffer) { |
| 2140 | // TODO(bugs.webrtc.org/4676) - Handle CreateChannel failure, as new local |
| 2141 | // description is applied. Restore back to old description. |
| 2142 | RTCError error = CreateChannels(*remote_description()->description()); |
| 2143 | if (!error.ok()) { |
| 2144 | return error; |
| 2145 | } |
| 2146 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2147 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2148 | // Remove unused channels if MediaContentDescription is rejected. |
| 2149 | RemoveUnusedChannels(remote_description()->description()); |
| 2150 | } |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2151 | |
| 2152 | // NOTE: Candidates allocation will be initiated only when SetLocalDescription |
| 2153 | // is called. |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 2154 | RTCError error = UpdateSessionState(type, cricket::CS_REMOTE); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2155 | if (!error.ok()) { |
| 2156 | return error; |
| 2157 | } |
| 2158 | |
| 2159 | if (local_description() && |
| 2160 | !UseCandidatesInSessionDescription(remote_description())) { |
| 2161 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidCandidates); |
| 2162 | } |
| 2163 | |
| 2164 | if (old_remote_description) { |
| 2165 | for (const cricket::ContentInfo& content : |
| 2166 | old_remote_description->description()->contents()) { |
| 2167 | // Check if this new SessionDescription contains new ICE ufrag and |
| 2168 | // password that indicates the remote peer requests an ICE restart. |
| 2169 | // TODO(deadbeef): When we start storing both the current and pending |
| 2170 | // remote description, this should reset pending_ice_restarts and compare |
| 2171 | // against the current description. |
| 2172 | if (CheckForRemoteIceRestart(old_remote_description, remote_description(), |
| 2173 | content.name)) { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 2174 | if (type == SdpType::kOffer) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2175 | pending_ice_restarts_.insert(content.name); |
| 2176 | } |
| 2177 | } else { |
| 2178 | // We retain all received candidates only if ICE is not restarted. |
| 2179 | // When ICE is restarted, all previous candidates belong to an old |
| 2180 | // generation and should not be kept. |
| 2181 | // TODO(deadbeef): This goes against the W3C spec which says the remote |
| 2182 | // description should only contain candidates from the last set remote |
| 2183 | // description plus any candidates added since then. We should remove |
| 2184 | // this once we're sure it won't break anything. |
| 2185 | WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription( |
| 2186 | old_remote_description, content.name, mutable_remote_description()); |
| 2187 | } |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | if (session_error() != SessionError::kNone) { |
| 2192 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
| 2193 | } |
| 2194 | |
| 2195 | // Set the the ICE connection state to connecting since the connection may |
| 2196 | // become writable with peer reflexive candidates before any remote candidate |
| 2197 | // is signaled. |
| 2198 | // TODO(pthatcher): This is a short-term solution for crbug/446908. A real fix |
| 2199 | // is to have a new signal the indicates a change in checking state from the |
| 2200 | // transport and expose a new checking() member from transport that can be |
| 2201 | // read to determine the current checking state. The existing SignalConnecting |
| 2202 | // actually means "gathering candidates", so cannot be be used here. |
Steve Anton | a3a92c2 | 2017-12-07 10:27:41 -0800 | [diff] [blame] | 2203 | if (remote_description()->GetType() != SdpType::kOffer && |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2204 | ice_connection_state() == PeerConnectionInterface::kIceConnectionNew) { |
| 2205 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 2206 | } |
| 2207 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2208 | // If setting the description decided our SSL role, allocate any necessary |
| 2209 | // SCTP sids. |
| 2210 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2211 | if (data_channel_type() == cricket::DCT_SCTP && GetSctpSslRole(&role)) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2212 | AllocateSctpSids(role); |
| 2213 | } |
| 2214 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2215 | if (IsUnifiedPlan()) { |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2216 | std::vector<rtc::scoped_refptr<RtpTransceiverInterface>> |
| 2217 | receiving_transceivers; |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2218 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> added_streams; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2219 | for (auto transceiver : transceivers_) { |
| 2220 | const ContentInfo* content = |
| 2221 | FindMediaSectionForTransceiver(transceiver, remote_description()); |
| 2222 | if (!content) { |
| 2223 | continue; |
| 2224 | } |
| 2225 | const MediaContentDescription* media_desc = content->media_description(); |
| 2226 | RtpTransceiverDirection local_direction = |
| 2227 | RtpTransceiverDirectionReversed(media_desc->direction()); |
| 2228 | // From the WebRTC specification, steps 2.2.8.5/6 of section 4.4.1.6 "Set |
| 2229 | // the RTCSessionDescription: If direction is sendrecv or recvonly, and |
| 2230 | // transceiver's current direction is neither sendrecv nor recvonly, |
| 2231 | // process the addition of a remote track for the media description. |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame^] | 2232 | // |
| 2233 | // Create a sync label in the case of an unsignalled msid. |
| 2234 | std::string sync_label = rtc::CreateRandomUuid(); |
| 2235 | if (!media_desc->streams().empty()) { |
| 2236 | sync_label = media_desc->streams()[0].sync_label; |
| 2237 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2238 | if (RtpTransceiverDirectionHasRecv(local_direction) && |
| 2239 | (!transceiver->current_direction() || |
| 2240 | !RtpTransceiverDirectionHasRecv( |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame^] | 2241 | *transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2242 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 2243 | remote_streams_->find(sync_label); |
| 2244 | if (!stream) { |
| 2245 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 2246 | MediaStream::Create(sync_label)); |
| 2247 | remote_streams_->AddStream(stream); |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2248 | added_streams.push_back(stream); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2249 | } |
| 2250 | transceiver->internal()->receiver_internal()->SetStreams({stream}); |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2251 | receiving_transceivers.push_back(transceiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2252 | } |
| 2253 | // If direction is sendonly or inactive, and transceiver's current |
| 2254 | // direction is neither sendonly nor inactive, process the removal of a |
| 2255 | // remote track for the media description. |
| 2256 | if (!RtpTransceiverDirectionHasRecv(local_direction) && |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2257 | (transceiver->current_direction() && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2258 | RtpTransceiverDirectionHasRecv(*transceiver->current_direction()))) { |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2259 | transceiver->internal()->receiver_internal()->SetStreams({}); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2260 | } |
| 2261 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 2262 | transceiver->internal()->set_current_direction(local_direction); |
| 2263 | } |
| 2264 | if (content->rejected && !transceiver->stopped()) { |
| 2265 | transceiver->Stop(); |
| 2266 | } |
Seth Hampson | 2f0d702 | 2018-02-20 11:54:42 -0800 | [diff] [blame^] | 2267 | if (!content->rejected && |
| 2268 | RtpTransceiverDirectionHasRecv(local_direction)) { |
| 2269 | // Set ssrc to 0 in the case of an unsignalled ssrc. |
| 2270 | uint32_t ssrc = 0; |
| 2271 | if (!media_desc->streams().empty()) { |
| 2272 | ssrc = media_desc->streams()[0].first_ssrc(); |
| 2273 | } |
| 2274 | transceiver->internal()->receiver_internal()->SetupMediaChannel(ssrc); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 2275 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2276 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2277 | // Once all processing has finished, fire off callbacks. |
Steve Anton | 8b815cd | 2018-02-16 16:14:42 -0800 | [diff] [blame] | 2278 | for (auto transceiver : receiving_transceivers) { |
| 2279 | observer_->OnTrack(transceiver); |
| 2280 | observer_->OnAddTrack(transceiver->receiver(), |
| 2281 | transceiver->receiver()->streams()); |
Steve Anton | ef65ef1 | 2018-01-10 17:15:20 -0800 | [diff] [blame] | 2282 | } |
Steve Anton | c49bcd9 | 2018-02-14 14:28:13 -0800 | [diff] [blame] | 2283 | for (auto stream : added_streams) { |
| 2284 | observer_->OnAddStream(stream); |
| 2285 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2286 | } |
| 2287 | |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2288 | const cricket::ContentInfo* audio_content = |
| 2289 | GetFirstAudioContent(remote_description()->description()); |
| 2290 | const cricket::ContentInfo* video_content = |
| 2291 | GetFirstVideoContent(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2292 | const cricket::AudioContentDescription* audio_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2293 | GetFirstAudioContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2294 | const cricket::VideoContentDescription* video_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2295 | GetFirstVideoContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2296 | const cricket::DataContentDescription* data_desc = |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2297 | GetFirstDataContentDescription(remote_description()->description()); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2298 | |
| 2299 | // Check if the descriptions include streams, just in case the peer supports |
| 2300 | // MSID, but doesn't indicate so with "a=msid-semantic". |
Henrik Boström | fdb9201 | 2017-11-09 19:55:44 +0100 | [diff] [blame] | 2301 | if (remote_description()->description()->msid_supported() || |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 2302 | (audio_desc && !audio_desc->streams().empty()) || |
| 2303 | (video_desc && !video_desc->streams().empty())) { |
| 2304 | remote_peer_supports_msid_ = true; |
| 2305 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2306 | |
| 2307 | // We wait to signal new streams until we finish processing the description, |
| 2308 | // since only at that point will new streams have all their tracks. |
| 2309 | rtc::scoped_refptr<StreamCollection> new_streams(StreamCollection::Create()); |
| 2310 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2311 | if (!IsUnifiedPlan()) { |
| 2312 | // TODO(steveanton): When removing RTP senders/receivers in response to a |
| 2313 | // rejected media section, there is some cleanup logic that expects the |
| 2314 | // voice/ video channel to still be set. But in this method the voice/video |
| 2315 | // channel would have been destroyed by the SetRemoteDescription caller |
| 2316 | // above so the cleanup that relies on them fails to run. The RemoveSenders |
| 2317 | // calls should be moved to right before the DestroyChannel calls to fix |
| 2318 | // this. |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 2319 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2320 | // Find all audio rtp streams and create corresponding remote AudioTracks |
| 2321 | // and MediaStreams. |
| 2322 | if (audio_content) { |
| 2323 | if (audio_content->rejected) { |
| 2324 | RemoveSenders(cricket::MEDIA_TYPE_AUDIO); |
| 2325 | } else { |
| 2326 | bool default_audio_track_needed = |
| 2327 | !remote_peer_supports_msid_ && |
| 2328 | RtpTransceiverDirectionHasSend(audio_desc->direction()); |
| 2329 | UpdateRemoteSendersList(GetActiveStreams(audio_desc), |
| 2330 | default_audio_track_needed, audio_desc->type(), |
| 2331 | new_streams); |
| 2332 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2333 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2334 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2335 | // Find all video rtp streams and create corresponding remote VideoTracks |
| 2336 | // and MediaStreams. |
| 2337 | if (video_content) { |
| 2338 | if (video_content->rejected) { |
| 2339 | RemoveSenders(cricket::MEDIA_TYPE_VIDEO); |
| 2340 | } else { |
| 2341 | bool default_video_track_needed = |
| 2342 | !remote_peer_supports_msid_ && |
| 2343 | RtpTransceiverDirectionHasSend(video_desc->direction()); |
| 2344 | UpdateRemoteSendersList(GetActiveStreams(video_desc), |
| 2345 | default_video_track_needed, video_desc->type(), |
| 2346 | new_streams); |
| 2347 | } |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 2348 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2349 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2350 | // Update the DataChannels with the information from the remote peer. |
| 2351 | if (data_desc) { |
| 2352 | if (rtc::starts_with(data_desc->protocol().data(), |
| 2353 | cricket::kMediaProtocolRtpPrefix)) { |
| 2354 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2355 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2356 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2357 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2358 | // Iterate new_streams and notify the observer about new MediaStreams. |
| 2359 | for (size_t i = 0; i < new_streams->count(); ++i) { |
| 2360 | MediaStreamInterface* new_stream = new_streams->at(i); |
| 2361 | stats_->AddStream(new_stream); |
| 2362 | observer_->OnAddStream( |
| 2363 | rtc::scoped_refptr<MediaStreamInterface>(new_stream)); |
| 2364 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2365 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2366 | UpdateEndedRemoteMediaStreams(); |
| 2367 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 2368 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 2369 | return RTCError::OK(); |
deadbeef | fc648b6 | 2015-10-13 16:42:33 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2372 | RTCError PeerConnection::UpdateTransceiversAndDataChannels( |
| 2373 | cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2374 | const SessionDescriptionInterface& new_session, |
| 2375 | const SessionDescriptionInterface* old_local_description, |
| 2376 | const SessionDescriptionInterface* old_remote_description) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2377 | RTC_DCHECK(IsUnifiedPlan()); |
| 2378 | |
Steve Anton | 7464fca | 2018-01-19 11:10:37 -0800 | [diff] [blame] | 2379 | const cricket::ContentGroup* bundle_group = nullptr; |
| 2380 | if (new_session.GetType() == SdpType::kOffer) { |
| 2381 | auto bundle_group_or_error = |
| 2382 | GetEarlyBundleGroup(*new_session.description()); |
| 2383 | if (!bundle_group_or_error.ok()) { |
| 2384 | return bundle_group_or_error.MoveError(); |
| 2385 | } |
| 2386 | bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2387 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2388 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2389 | const ContentInfos& new_contents = new_session.description()->contents(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2390 | for (size_t i = 0; i < new_contents.size(); ++i) { |
| 2391 | const cricket::ContentInfo& new_content = new_contents[i]; |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2392 | cricket::MediaType media_type = new_content.media_description()->type(); |
| 2393 | seen_mids_.insert(new_content.name); |
| 2394 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 2395 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2396 | const cricket::ContentInfo* old_local_content = nullptr; |
| 2397 | if (old_local_description && |
| 2398 | i < old_local_description->description()->contents().size()) { |
| 2399 | old_local_content = |
| 2400 | &old_local_description->description()->contents()[i]; |
| 2401 | } |
| 2402 | const cricket::ContentInfo* old_remote_content = nullptr; |
| 2403 | if (old_remote_description && |
| 2404 | i < old_remote_description->description()->contents().size()) { |
| 2405 | old_remote_content = |
| 2406 | &old_remote_description->description()->contents()[i]; |
| 2407 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2408 | auto transceiver_or_error = |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2409 | AssociateTransceiver(source, new_session.GetType(), i, new_content, |
| 2410 | old_local_content, old_remote_content); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2411 | if (!transceiver_or_error.ok()) { |
| 2412 | return transceiver_or_error.MoveError(); |
| 2413 | } |
| 2414 | auto transceiver = transceiver_or_error.MoveValue(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2415 | RTCError error = |
| 2416 | UpdateTransceiverChannel(transceiver, new_content, bundle_group); |
| 2417 | if (!error.ok()) { |
| 2418 | return error; |
| 2419 | } |
| 2420 | } else if (media_type == cricket::MEDIA_TYPE_DATA) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2421 | if (GetDataMid() && new_content.name != *GetDataMid()) { |
| 2422 | // Ignore all but the first data section. |
| 2423 | continue; |
| 2424 | } |
| 2425 | RTCError error = UpdateDataChannel(source, new_content, bundle_group); |
| 2426 | if (!error.ok()) { |
| 2427 | return error; |
| 2428 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2429 | } else { |
| 2430 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2431 | "Unknown section type."); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return RTCError::OK(); |
| 2436 | } |
| 2437 | |
| 2438 | RTCError PeerConnection::UpdateTransceiverChannel( |
| 2439 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2440 | transceiver, |
| 2441 | const cricket::ContentInfo& content, |
| 2442 | const cricket::ContentGroup* bundle_group) { |
| 2443 | RTC_DCHECK(IsUnifiedPlan()); |
| 2444 | RTC_DCHECK(transceiver); |
| 2445 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 2446 | if (content.rejected) { |
| 2447 | if (channel) { |
| 2448 | transceiver->internal()->SetChannel(nullptr); |
| 2449 | DestroyBaseChannel(channel); |
| 2450 | } |
| 2451 | } else { |
| 2452 | if (!channel) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2453 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2454 | channel = CreateVoiceChannel( |
| 2455 | content.name, |
| 2456 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2457 | } else { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2458 | RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, transceiver->media_type()); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2459 | channel = CreateVideoChannel( |
| 2460 | content.name, |
| 2461 | GetTransportNameForMediaSection(content.name, bundle_group)); |
| 2462 | } |
| 2463 | if (!channel) { |
| 2464 | LOG_AND_RETURN_ERROR( |
| 2465 | RTCErrorType::INTERNAL_ERROR, |
| 2466 | "Failed to create channel for mid=" + content.name); |
| 2467 | } |
| 2468 | transceiver->internal()->SetChannel(channel); |
| 2469 | } |
| 2470 | } |
| 2471 | return RTCError::OK(); |
| 2472 | } |
| 2473 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2474 | RTCError PeerConnection::UpdateDataChannel( |
| 2475 | cricket::ContentSource source, |
| 2476 | const cricket::ContentInfo& content, |
| 2477 | const cricket::ContentGroup* bundle_group) { |
| 2478 | if (data_channel_type_ == cricket::DCT_NONE) { |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 2479 | // If data channels are disabled, ignore this media section. CreateAnswer |
| 2480 | // will take care of rejecting it. |
| 2481 | return RTCError::OK(); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 2482 | } |
| 2483 | if (content.rejected) { |
| 2484 | DestroyDataChannel(); |
| 2485 | } else { |
| 2486 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 2487 | if (!CreateDataChannel(content.name, GetTransportNameForMediaSection( |
| 2488 | content.name, bundle_group))) { |
| 2489 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 2490 | "Failed to create data channel."); |
| 2491 | } |
| 2492 | } |
| 2493 | if (source == cricket::CS_REMOTE) { |
| 2494 | const MediaContentDescription* data_desc = content.media_description(); |
| 2495 | if (data_desc && cricket::IsRtpProtocol(data_desc->protocol())) { |
| 2496 | UpdateRemoteRtpDataChannels(GetActiveStreams(data_desc)); |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | return RTCError::OK(); |
| 2501 | } |
| 2502 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2503 | RTCErrorOr<rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>>> |
| 2504 | PeerConnection::AssociateTransceiver(cricket::ContentSource source, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2505 | SdpType type, |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2506 | size_t mline_index, |
| 2507 | const ContentInfo& content, |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2508 | const ContentInfo* old_local_content, |
| 2509 | const ContentInfo* old_remote_content) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2510 | RTC_DCHECK(IsUnifiedPlan()); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 2511 | // If this is an offer then the m= section might be recycled. If the m= |
| 2512 | // section is being recycled (defined as: rejected in the current local or |
| 2513 | // remote description and not rejected in new description), dissociate the |
| 2514 | // currently associated RtpTransceiver by setting its mid property to null, |
| 2515 | // and discard the mapping between the transceiver and its m= section index. |
| 2516 | if (IsMediaSectionBeingRecycled(type, content, old_local_content, |
| 2517 | old_remote_content)) { |
| 2518 | // We want to dissociate the transceiver that has the rejected mid. |
| 2519 | const std::string& old_mid = |
| 2520 | (old_local_content && old_local_content->rejected) |
| 2521 | ? old_local_content->name |
| 2522 | : old_remote_content->name; |
| 2523 | auto old_transceiver = GetAssociatedTransceiver(old_mid); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2524 | if (old_transceiver) { |
| 2525 | old_transceiver->internal()->set_mid(rtc::nullopt); |
| 2526 | old_transceiver->internal()->set_mline_index(rtc::nullopt); |
| 2527 | } |
| 2528 | } |
| 2529 | const MediaContentDescription* media_desc = content.media_description(); |
| 2530 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 2531 | if (source == cricket::CS_LOCAL) { |
| 2532 | // Find the RtpTransceiver that corresponds to this m= section, using the |
| 2533 | // mapping between transceivers and m= section indices established when |
| 2534 | // creating the offer. |
| 2535 | if (!transceiver) { |
| 2536 | transceiver = GetTransceiverByMLineIndex(mline_index); |
| 2537 | } |
| 2538 | if (!transceiver) { |
| 2539 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2540 | "Unknown transceiver"); |
| 2541 | } |
| 2542 | } else { |
| 2543 | RTC_DCHECK_EQ(source, cricket::CS_REMOTE); |
| 2544 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers |
| 2545 | // of the same type... |
| 2546 | if (!transceiver && |
| 2547 | RtpTransceiverDirectionHasRecv(media_desc->direction())) { |
| 2548 | transceiver = FindAvailableTransceiverToReceive(media_desc->type()); |
| 2549 | } |
| 2550 | // If no RtpTransceiver was found in the previous step, create one with a |
| 2551 | // recvonly direction. |
| 2552 | if (!transceiver) { |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2553 | auto sender = |
| 2554 | CreateSender(media_desc->type(), nullptr, {rtc::CreateRandomUuid()}); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 2555 | std::string receiver_id; |
| 2556 | if (!media_desc->streams().empty()) { |
| 2557 | receiver_id = media_desc->streams()[0].id; |
| 2558 | } else { |
| 2559 | receiver_id = rtc::CreateRandomUuid(); |
| 2560 | } |
| 2561 | auto receiver = CreateReceiver(media_desc->type(), receiver_id); |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 2562 | transceiver = CreateAndAddTransceiver(sender, receiver); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2563 | transceiver->internal()->set_direction( |
| 2564 | RtpTransceiverDirection::kRecvOnly); |
| 2565 | } |
| 2566 | } |
| 2567 | RTC_DCHECK(transceiver); |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2568 | if (transceiver->media_type() != media_desc->type()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2569 | LOG_AND_RETURN_ERROR( |
| 2570 | RTCErrorType::INVALID_PARAMETER, |
| 2571 | "Transceiver type does not match media description type."); |
| 2572 | } |
| 2573 | // Associate the found or created RtpTransceiver with the m= section by |
| 2574 | // setting the value of the RtpTransceiver's mid property to the MID of the m= |
| 2575 | // section, and establish a mapping between the transceiver and the index of |
| 2576 | // the m= section. |
| 2577 | transceiver->internal()->set_mid(content.name); |
| 2578 | transceiver->internal()->set_mline_index(mline_index); |
| 2579 | return std::move(transceiver); |
| 2580 | } |
| 2581 | |
| 2582 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2583 | PeerConnection::GetAssociatedTransceiver(const std::string& mid) const { |
| 2584 | RTC_DCHECK(IsUnifiedPlan()); |
| 2585 | for (auto transceiver : transceivers_) { |
| 2586 | if (transceiver->mid() == mid) { |
| 2587 | return transceiver; |
| 2588 | } |
| 2589 | } |
| 2590 | return nullptr; |
| 2591 | } |
| 2592 | |
| 2593 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2594 | PeerConnection::GetTransceiverByMLineIndex(size_t mline_index) const { |
| 2595 | RTC_DCHECK(IsUnifiedPlan()); |
| 2596 | for (auto transceiver : transceivers_) { |
| 2597 | if (transceiver->internal()->mline_index() == mline_index) { |
| 2598 | return transceiver; |
| 2599 | } |
| 2600 | } |
| 2601 | return nullptr; |
| 2602 | } |
| 2603 | |
| 2604 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2605 | PeerConnection::FindAvailableTransceiverToReceive( |
| 2606 | cricket::MediaType media_type) const { |
| 2607 | RTC_DCHECK(IsUnifiedPlan()); |
| 2608 | // From JSEP section 5.10 (Applying a Remote Description): |
| 2609 | // If the m= section is sendrecv or recvonly, and there are RtpTransceivers of |
| 2610 | // the same type that were added to the PeerConnection by addTrack and are not |
| 2611 | // associated with any m= section and are not stopped, find the first such |
| 2612 | // RtpTransceiver. |
| 2613 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2614 | if (transceiver->media_type() == media_type && |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 2615 | transceiver->internal()->created_by_addtrack() && !transceiver->mid() && |
| 2616 | !transceiver->stopped()) { |
| 2617 | return transceiver; |
| 2618 | } |
| 2619 | } |
| 2620 | return nullptr; |
| 2621 | } |
| 2622 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2623 | const cricket::ContentInfo* PeerConnection::FindMediaSectionForTransceiver( |
| 2624 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2625 | transceiver, |
| 2626 | const SessionDescriptionInterface* sdesc) const { |
| 2627 | RTC_DCHECK(transceiver); |
| 2628 | RTC_DCHECK(sdesc); |
| 2629 | if (IsUnifiedPlan()) { |
| 2630 | if (!transceiver->internal()->mid()) { |
| 2631 | // This transceiver is not associated with a media section yet. |
| 2632 | return nullptr; |
| 2633 | } |
| 2634 | return sdesc->description()->GetContentByName( |
| 2635 | *transceiver->internal()->mid()); |
| 2636 | } else { |
| 2637 | // Plan B only allows at most one audio and one video section, so use the |
| 2638 | // first media section of that type. |
| 2639 | return cricket::GetFirstMediaContent(sdesc->description()->contents(), |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 2640 | transceiver->media_type()); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 2641 | } |
| 2642 | } |
| 2643 | |
deadbeef | 46c7389 | 2016-11-16 19:42:04 -0800 | [diff] [blame] | 2644 | PeerConnectionInterface::RTCConfiguration PeerConnection::GetConfiguration() { |
| 2645 | return configuration_; |
| 2646 | } |
| 2647 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2648 | bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration, |
| 2649 | RTCError* error) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2650 | TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration"); |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 2651 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2652 | if (local_description() && configuration.ice_candidate_pool_size != |
| 2653 | configuration_.ice_candidate_pool_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2654 | RTC_LOG(LS_ERROR) << "Can't change candidate pool size after calling " |
| 2655 | "SetLocalDescription."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2656 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2657 | } |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 2658 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2659 | // The simplest (and most future-compatible) way to tell if the config was |
| 2660 | // modified in an invalid way is to copy each property we do support |
| 2661 | // modifying, then use operator==. There are far more properties we don't |
| 2662 | // support modifying than those we do, and more could be added. |
| 2663 | RTCConfiguration modified_config = configuration_; |
| 2664 | modified_config.servers = configuration.servers; |
| 2665 | modified_config.type = configuration.type; |
| 2666 | modified_config.ice_candidate_pool_size = |
| 2667 | configuration.ice_candidate_pool_size; |
| 2668 | modified_config.prune_turn_ports = configuration.prune_turn_ports; |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2669 | modified_config.ice_check_min_interval = configuration.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2670 | modified_config.stun_candidate_keepalive_interval = |
| 2671 | configuration.stun_candidate_keepalive_interval; |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2672 | modified_config.turn_customizer = configuration.turn_customizer; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 2673 | modified_config.network_preference = configuration.network_preference; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2674 | if (configuration != modified_config) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2675 | RTC_LOG(LS_ERROR) << "Modifying the configuration in an unsupported way."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2676 | return SafeSetError(RTCErrorType::INVALID_MODIFICATION, error); |
| 2677 | } |
| 2678 | |
Steve Anton | 038834f | 2017-07-14 15:59:59 -0700 | [diff] [blame] | 2679 | // Validate the modified configuration. |
| 2680 | RTCError validate_error = ValidateConfiguration(modified_config); |
| 2681 | if (!validate_error.ok()) { |
| 2682 | return SafeSetError(std::move(validate_error), error); |
| 2683 | } |
| 2684 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2685 | // Note that this isn't possible through chromium, since it's an unsigned |
| 2686 | // short in WebIDL. |
| 2687 | if (configuration.ice_candidate_pool_size < 0 || |
| 2688 | configuration.ice_candidate_pool_size > UINT16_MAX) { |
| 2689 | return SafeSetError(RTCErrorType::INVALID_RANGE, error); |
| 2690 | } |
| 2691 | |
| 2692 | // Parse ICE servers before hopping to network thread. |
| 2693 | cricket::ServerAddresses stun_servers; |
| 2694 | std::vector<cricket::RelayServerConfig> turn_servers; |
| 2695 | RTCErrorType parse_error = |
| 2696 | ParseIceServers(configuration.servers, &stun_servers, &turn_servers); |
| 2697 | if (parse_error != RTCErrorType::NONE) { |
| 2698 | return SafeSetError(parse_error, error); |
| 2699 | } |
| 2700 | |
| 2701 | // In theory this shouldn't fail. |
| 2702 | if (!network_thread()->Invoke<bool>( |
| 2703 | RTC_FROM_HERE, |
| 2704 | rtc::Bind(&PeerConnection::ReconfigurePortAllocator_n, this, |
| 2705 | stun_servers, turn_servers, modified_config.type, |
| 2706 | modified_config.ice_candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 2707 | modified_config.prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 2708 | modified_config.turn_customizer, |
| 2709 | modified_config.stun_candidate_keepalive_interval))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2710 | RTC_LOG(LS_ERROR) << "Failed to apply configuration to PortAllocator."; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2711 | return SafeSetError(RTCErrorType::INTERNAL_ERROR, error); |
| 2712 | } |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 2713 | |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2714 | // As described in JSEP, calling setConfiguration with new ICE servers or |
| 2715 | // candidate policy must set a "needs-ice-restart" bit so that the next offer |
| 2716 | // triggers an ICE restart which will pick up the changes. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2717 | if (modified_config.servers != configuration_.servers || |
| 2718 | modified_config.type != configuration_.type || |
| 2719 | modified_config.prune_turn_ports != configuration_.prune_turn_ports) { |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2720 | transport_controller_->SetNeedsIceRestartFlag(); |
deadbeef | d1a38b5 | 2016-12-10 13:15:33 -0800 | [diff] [blame] | 2721 | } |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2722 | |
Qingsi Wang | 9c98f0c | 2018-02-15 15:10:59 -0800 | [diff] [blame] | 2723 | transport_controller_->SetIceConfig(ParseIceConfig(modified_config)); |
skvlad | d1f5fda | 2017-02-03 16:54:05 -0800 | [diff] [blame] | 2724 | |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2725 | configuration_ = modified_config; |
| 2726 | return SafeSetError(RTCErrorType::NONE, error); |
buildbot@webrtc.org | 41451d4 | 2014-05-03 05:39:45 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2729 | bool PeerConnection::AddIceCandidate( |
| 2730 | const IceCandidateInterface* ice_candidate) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2731 | TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate"); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 2732 | if (IsClosed()) { |
| 2733 | return false; |
| 2734 | } |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2735 | |
| 2736 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2737 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: ICE candidates can't be added " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2738 | "without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | if (!ice_candidate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2743 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate is NULL."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2744 | return false; |
| 2745 | } |
| 2746 | |
| 2747 | bool valid = false; |
| 2748 | bool ready = ReadyToUseRemoteCandidate(ice_candidate, nullptr, &valid); |
| 2749 | if (!valid) { |
| 2750 | return false; |
| 2751 | } |
| 2752 | |
| 2753 | // Add this candidate to the remote session description. |
| 2754 | if (!mutable_remote_description()->AddCandidate(ice_candidate)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2755 | RTC_LOG(LS_ERROR) << "ProcessIceMessage: Candidate cannot be used."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2756 | return false; |
| 2757 | } |
| 2758 | |
| 2759 | if (ready) { |
| 2760 | return UseCandidate(ice_candidate); |
| 2761 | } else { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2762 | RTC_LOG(LS_INFO) << "ProcessIceMessage: Not ready to use candidate."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2763 | return true; |
| 2764 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2767 | bool PeerConnection::RemoveIceCandidates( |
| 2768 | const std::vector<cricket::Candidate>& candidates) { |
| 2769 | TRACE_EVENT0("webrtc", "PeerConnection::RemoveIceCandidates"); |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2770 | if (!remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2771 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: ICE candidates can't be " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2772 | "removed without any remote session description."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2773 | return false; |
| 2774 | } |
| 2775 | |
| 2776 | if (candidates.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2777 | RTC_LOG(LS_ERROR) << "RemoveRemoteIceCandidates: candidates are empty."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2778 | return false; |
| 2779 | } |
| 2780 | |
| 2781 | size_t number_removed = |
| 2782 | mutable_remote_description()->RemoveCandidates(candidates); |
| 2783 | if (number_removed != candidates.size()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2784 | RTC_LOG(LS_ERROR) |
| 2785 | << "RemoveRemoteIceCandidates: Failed to remove candidates. " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 2786 | "Requested " |
| 2787 | << candidates.size() << " but only " << number_removed |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2788 | << " are removed."; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | // Remove the candidates from the transport controller. |
| 2792 | std::string error; |
| 2793 | bool res = transport_controller_->RemoveRemoteCandidates(candidates, &error); |
| 2794 | if (!res && !error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 2795 | RTC_LOG(LS_ERROR) << "Error when removing remote candidates: " << error; |
Steve Anton | d25da37 | 2017-11-06 14:50:29 -0800 | [diff] [blame] | 2796 | } |
| 2797 | return true; |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2800 | void PeerConnection::RegisterUMAObserver(UMAObserver* observer) { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 2801 | TRACE_EVENT0("webrtc", "PeerConnection::RegisterUmaObserver"); |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2802 | uma_observer_ = observer; |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2803 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2804 | if (transport_controller()) { |
| 2805 | transport_controller()->SetMetricsObserver(uma_observer_); |
guoweis@webrtc.org | 7169afd | 2014-12-04 17:59:29 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2808 | // Send information about IPv4/IPv6 status. |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 2809 | if (uma_observer_) { |
Honghai Zhang | d93f50c | 2016-10-05 11:47:22 -0700 | [diff] [blame] | 2810 | port_allocator_->SetMetricsObserver(uma_observer_); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2811 | if (port_allocator_->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6) { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2812 | uma_observer_->IncrementEnumCounter( |
| 2813 | kEnumCounterAddressFamily, kPeerConnection_IPv6, |
| 2814 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | b445f26 | 2014-05-23 22:19:37 +0000 | [diff] [blame] | 2815 | } else { |
Guo-wei Shieh | dfbe679 | 2015-09-03 17:12:07 -0700 | [diff] [blame] | 2816 | uma_observer_->IncrementEnumCounter( |
| 2817 | kEnumCounterAddressFamily, kPeerConnection_IPv4, |
| 2818 | kPeerConnectionAddressFamilyCounter_Max); |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2819 | } |
Harald Alvestrand | 5dbb586 | 2018-02-13 23:48:00 +0100 | [diff] [blame] | 2820 | // Send information about the requested SDP semantics. |
| 2821 | switch (configuration_.sdp_semantics) { |
| 2822 | case SdpSemantics::kDefault: |
| 2823 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2824 | kSdpSemanticRequestDefault, |
| 2825 | kSdpSemanticRequestMax); |
| 2826 | |
| 2827 | break; |
| 2828 | case SdpSemantics::kPlanB: |
| 2829 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2830 | kSdpSemanticRequestPlanB, |
| 2831 | kSdpSemanticRequestMax); |
| 2832 | break; |
| 2833 | case SdpSemantics::kUnifiedPlan: |
| 2834 | uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested, |
| 2835 | kSdpSemanticRequestUnifiedPlan, |
| 2836 | kSdpSemanticRequestMax); |
| 2837 | break; |
| 2838 | default: |
| 2839 | RTC_NOTREACHED(); |
| 2840 | } |
mallinath@webrtc.org | d37bcfa | 2014-05-12 23:10:18 +0000 | [diff] [blame] | 2841 | } |
buildbot@webrtc.org | 1567b8c | 2014-05-08 19:54:16 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2844 | RTCError PeerConnection::SetBitrate(const BitrateParameters& bitrate) { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2845 | if (!worker_thread()->IsCurrent()) { |
| 2846 | return worker_thread()->Invoke<RTCError>( |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2847 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::SetBitrate, this, bitrate)); |
| 2848 | } |
| 2849 | |
| 2850 | const bool has_min = static_cast<bool>(bitrate.min_bitrate_bps); |
| 2851 | const bool has_current = static_cast<bool>(bitrate.current_bitrate_bps); |
| 2852 | const bool has_max = static_cast<bool>(bitrate.max_bitrate_bps); |
| 2853 | if (has_min && *bitrate.min_bitrate_bps < 0) { |
| 2854 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2855 | "min_bitrate_bps <= 0"); |
| 2856 | } |
| 2857 | if (has_current) { |
| 2858 | if (has_min && *bitrate.current_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2859 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2860 | "current_bitrate_bps < min_bitrate_bps"); |
| 2861 | } else if (*bitrate.current_bitrate_bps < 0) { |
| 2862 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2863 | "curent_bitrate_bps < 0"); |
| 2864 | } |
| 2865 | } |
| 2866 | if (has_max) { |
| 2867 | if (has_current && |
| 2868 | *bitrate.max_bitrate_bps < *bitrate.current_bitrate_bps) { |
| 2869 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2870 | "max_bitrate_bps < current_bitrate_bps"); |
| 2871 | } else if (has_min && *bitrate.max_bitrate_bps < *bitrate.min_bitrate_bps) { |
| 2872 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2873 | "max_bitrate_bps < min_bitrate_bps"); |
| 2874 | } else if (*bitrate.max_bitrate_bps < 0) { |
| 2875 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 2876 | "max_bitrate_bps < 0"); |
| 2877 | } |
| 2878 | } |
| 2879 | |
Lu Liu | e4bf600 | 2018-02-20 19:16:26 +0000 | [diff] [blame] | 2880 | Call::Config::BitrateConfigMask mask; |
zstein | 4b97980 | 2017-06-02 14:37:37 -0700 | [diff] [blame] | 2881 | mask.min_bitrate_bps = bitrate.min_bitrate_bps; |
| 2882 | mask.start_bitrate_bps = bitrate.current_bitrate_bps; |
| 2883 | mask.max_bitrate_bps = bitrate.max_bitrate_bps; |
| 2884 | |
| 2885 | RTC_DCHECK(call_.get()); |
| 2886 | call_->SetBitrateConfigMask(mask); |
| 2887 | |
| 2888 | return RTCError::OK(); |
| 2889 | } |
| 2890 | |
Alex Narest | 78609d5 | 2017-10-20 10:37:47 +0200 | [diff] [blame] | 2891 | void PeerConnection::SetBitrateAllocationStrategy( |
| 2892 | std::unique_ptr<rtc::BitrateAllocationStrategy> |
| 2893 | bitrate_allocation_strategy) { |
| 2894 | rtc::Thread* worker_thread = factory_->worker_thread(); |
| 2895 | if (!worker_thread->IsCurrent()) { |
| 2896 | rtc::BitrateAllocationStrategy* strategy_raw = |
| 2897 | bitrate_allocation_strategy.release(); |
| 2898 | auto functor = [this, strategy_raw]() { |
| 2899 | call_->SetBitrateAllocationStrategy( |
| 2900 | rtc::WrapUnique<rtc::BitrateAllocationStrategy>(strategy_raw)); |
| 2901 | }; |
| 2902 | worker_thread->Invoke<void>(RTC_FROM_HERE, functor); |
| 2903 | return; |
| 2904 | } |
| 2905 | RTC_DCHECK(call_.get()); |
| 2906 | call_->SetBitrateAllocationStrategy(std::move(bitrate_allocation_strategy)); |
| 2907 | } |
| 2908 | |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 2909 | void PeerConnection::SetAudioPlayout(bool playout) { |
| 2910 | if (!worker_thread()->IsCurrent()) { |
| 2911 | worker_thread()->Invoke<void>( |
| 2912 | RTC_FROM_HERE, |
| 2913 | rtc::Bind(&PeerConnection::SetAudioPlayout, this, playout)); |
| 2914 | return; |
| 2915 | } |
| 2916 | auto audio_state = |
| 2917 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2918 | audio_state->SetPlayout(playout); |
| 2919 | } |
| 2920 | |
| 2921 | void PeerConnection::SetAudioRecording(bool recording) { |
| 2922 | if (!worker_thread()->IsCurrent()) { |
| 2923 | worker_thread()->Invoke<void>( |
| 2924 | RTC_FROM_HERE, |
| 2925 | rtc::Bind(&PeerConnection::SetAudioRecording, this, recording)); |
| 2926 | return; |
| 2927 | } |
| 2928 | auto audio_state = |
| 2929 | factory_->channel_manager()->media_engine()->GetAudioState(); |
| 2930 | audio_state->SetRecording(recording); |
| 2931 | } |
| 2932 | |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2933 | std::unique_ptr<rtc::SSLCertificate> |
| 2934 | PeerConnection::GetRemoteAudioSSLCertificate() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2935 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 2936 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2937 | return nullptr; |
| 2938 | } |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2939 | return GetRemoteSSLCertificate( |
| 2940 | audio_transceiver->internal()->channel()->transport_name()); |
Steve Anton | 8c0f7a7 | 2017-10-03 10:03:10 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2943 | std::unique_ptr<rtc::SSLCertChain> |
| 2944 | PeerConnection::GetRemoteAudioSSLCertChain() { |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2945 | auto audio_transceiver = GetFirstAudioTransceiver(); |
| 2946 | if (!audio_transceiver || !audio_transceiver->internal()->channel()) { |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2947 | return nullptr; |
| 2948 | } |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2949 | return transport_controller_->GetRemoteSSLCertChain( |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 2950 | audio_transceiver->internal()->channel()->transport_name()); |
| 2951 | } |
| 2952 | |
| 2953 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 2954 | PeerConnection::GetFirstAudioTransceiver() const { |
| 2955 | for (auto transceiver : transceivers_) { |
| 2956 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 2957 | return transceiver; |
| 2958 | } |
| 2959 | } |
| 2960 | return nullptr; |
Zhi Huang | 70b820f | 2018-01-27 14:16:15 -0800 | [diff] [blame] | 2961 | } |
| 2962 | |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2963 | bool PeerConnection::StartRtcEventLog(rtc::PlatformFile file, |
| 2964 | int64_t max_size_bytes) { |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2965 | // TODO(eladalon): It would be better to not allow negative values into PC. |
| 2966 | const size_t max_size = (max_size_bytes < 0) |
| 2967 | ? RtcEventLog::kUnlimitedOutput |
| 2968 | : rtc::saturated_cast<size_t>(max_size_bytes); |
| 2969 | return StartRtcEventLog( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2970 | rtc::MakeUnique<RtcEventLogOutputFile>(file, max_size), |
| 2971 | webrtc::RtcEventLog::kImmediateOutput); |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2972 | } |
| 2973 | |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2974 | bool PeerConnection::StartRtcEventLog(std::unique_ptr<RtcEventLogOutput> output, |
| 2975 | int64_t output_period_ms) { |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2976 | // TODO(eladalon): In C++14, this can be done with a lambda. |
| 2977 | struct Functor { |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2978 | bool operator()() { |
| 2979 | return pc->StartRtcEventLog_w(std::move(output), output_period_ms); |
| 2980 | } |
Karl Wiberg | d6b4819 | 2017-10-16 23:01:06 +0200 | [diff] [blame] | 2981 | PeerConnection* const pc; |
| 2982 | std::unique_ptr<RtcEventLogOutput> output; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2983 | const int64_t output_period_ms; |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 2984 | }; |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 2985 | return worker_thread()->Invoke<bool>( |
| 2986 | RTC_FROM_HERE, Functor{this, std::move(output), output_period_ms}); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | void PeerConnection::StopRtcEventLog() { |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 2990 | worker_thread()->Invoke<void>( |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 2991 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::StopRtcEventLog_w, this)); |
| 2992 | } |
| 2993 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2994 | const SessionDescriptionInterface* PeerConnection::local_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 2995 | return pending_local_description_ ? pending_local_description_.get() |
| 2996 | : current_local_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | const SessionDescriptionInterface* PeerConnection::remote_description() const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3000 | return pending_remote_description_ ? pending_remote_description_.get() |
| 3001 | : current_remote_description_.get(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3004 | const SessionDescriptionInterface* PeerConnection::current_local_description() |
| 3005 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3006 | return current_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | const SessionDescriptionInterface* PeerConnection::current_remote_description() |
| 3010 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3011 | return current_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | const SessionDescriptionInterface* PeerConnection::pending_local_description() |
| 3015 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3016 | return pending_local_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | const SessionDescriptionInterface* PeerConnection::pending_remote_description() |
| 3020 | const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3021 | return pending_remote_description_.get(); |
deadbeef | fe4a8a4 | 2016-12-20 17:56:17 -0800 | [diff] [blame] | 3022 | } |
| 3023 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3024 | void PeerConnection::Close() { |
Peter Boström | 1a9d615 | 2015-12-08 22:15:17 +0100 | [diff] [blame] | 3025 | TRACE_EVENT0("webrtc", "PeerConnection::Close"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3026 | // Update stats here so that we have the most recent stats for tracks and |
| 3027 | // streams before the channels are closed. |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 3028 | stats_->UpdateStats(kStatsOutputLevelStandard); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3029 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3030 | ChangeSignalingState(PeerConnectionInterface::kClosed); |
Steve Anton | 3fe1b15 | 2017-12-12 10:20:08 -0800 | [diff] [blame] | 3031 | |
Steve Anton | 8af2186 | 2017-12-15 11:20:13 -0800 | [diff] [blame] | 3032 | for (auto transceiver : transceivers_) { |
| 3033 | transceiver->Stop(); |
| 3034 | } |
| 3035 | DestroyAllChannels(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3036 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 3037 | // The event log is used in the transport controller, which must be outlived |
| 3038 | // by the former. CreateOffer by the peer connection is implemented |
| 3039 | // asynchronously and if the peer connection is closed without resetting the |
| 3040 | // WebRTC session description factory, the session description factory would |
| 3041 | // call the transport controller. |
| 3042 | webrtc_session_desc_factory_.reset(); |
| 3043 | transport_controller_.reset(); |
| 3044 | |
deadbeef | 42a4263 | 2017-03-10 15:18:00 -0800 | [diff] [blame] | 3045 | network_thread()->Invoke<void>( |
| 3046 | RTC_FROM_HERE, |
| 3047 | rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool, |
| 3048 | port_allocator_.get())); |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 3049 | |
Steve Anton | 978b876 | 2017-09-29 12:15:02 -0700 | [diff] [blame] | 3050 | worker_thread()->Invoke<void>(RTC_FROM_HERE, [this] { |
eladalon | 248fd4f | 2017-09-06 05:18:15 -0700 | [diff] [blame] | 3051 | call_.reset(); |
| 3052 | // The event log must outlive call (and any other object that uses it). |
| 3053 | event_log_.reset(); |
| 3054 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 3057 | void PeerConnection::OnMessage(rtc::Message* msg) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3058 | switch (msg->message_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3059 | case MSG_SET_SESSIONDESCRIPTION_SUCCESS: { |
| 3060 | SetSessionDescriptionMsg* param = |
| 3061 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3062 | param->observer->OnSuccess(); |
| 3063 | delete param; |
| 3064 | break; |
| 3065 | } |
| 3066 | case MSG_SET_SESSIONDESCRIPTION_FAILED: { |
| 3067 | SetSessionDescriptionMsg* param = |
| 3068 | static_cast<SetSessionDescriptionMsg*>(msg->pdata); |
| 3069 | param->observer->OnFailure(param->error); |
| 3070 | delete param; |
| 3071 | break; |
| 3072 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3073 | case MSG_CREATE_SESSIONDESCRIPTION_FAILED: { |
| 3074 | CreateSessionDescriptionMsg* param = |
| 3075 | static_cast<CreateSessionDescriptionMsg*>(msg->pdata); |
| 3076 | param->observer->OnFailure(param->error); |
| 3077 | delete param; |
| 3078 | break; |
| 3079 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3080 | case MSG_GETSTATS: { |
| 3081 | GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata); |
nisse | e8abe3e | 2017-01-18 05:00:34 -0800 | [diff] [blame] | 3082 | StatsReports reports; |
| 3083 | stats_->GetStats(param->track, &reports); |
| 3084 | param->observer->OnComplete(reports); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3085 | delete param; |
| 3086 | break; |
| 3087 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 3088 | case MSG_FREE_DATACHANNELS: { |
| 3089 | sctp_data_channels_to_free_.clear(); |
| 3090 | break; |
| 3091 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3092 | default: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3093 | RTC_NOTREACHED() << "Not implemented"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3094 | break; |
| 3095 | } |
| 3096 | } |
| 3097 | |
Steve Anton | afb0bb7 | 2018-02-20 11:35:37 -0800 | [diff] [blame] | 3098 | cricket::VoiceMediaChannel* PeerConnection::voice_media_channel() const { |
| 3099 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3100 | auto* voice_channel = static_cast<cricket::VoiceChannel*>( |
| 3101 | GetAudioTransceiver()->internal()->channel()); |
| 3102 | if (voice_channel) { |
| 3103 | return voice_channel->media_channel(); |
| 3104 | } else { |
| 3105 | return nullptr; |
| 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | cricket::VideoMediaChannel* PeerConnection::video_media_channel() const { |
| 3110 | RTC_DCHECK(!IsUnifiedPlan()); |
| 3111 | auto* video_channel = static_cast<cricket::VideoChannel*>( |
| 3112 | GetVideoTransceiver()->internal()->channel()); |
| 3113 | if (video_channel) { |
| 3114 | return video_channel->media_channel(); |
| 3115 | } else { |
| 3116 | return nullptr; |
| 3117 | } |
| 3118 | } |
| 3119 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3120 | void PeerConnection::CreateAudioReceiver( |
| 3121 | MediaStreamInterface* stream, |
| 3122 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3123 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3124 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3125 | auto* audio_receiver = new AudioRtpReceiver( |
| 3126 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3127 | audio_receiver->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3128 | audio_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3129 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3130 | signaling_thread(), audio_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3131 | GetAudioTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3132 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3135 | void PeerConnection::CreateVideoReceiver( |
| 3136 | MediaStreamInterface* stream, |
| 3137 | const RtpSenderInfo& remote_sender_info) { |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3138 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams; |
| 3139 | streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream)); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3140 | auto* video_receiver = new VideoRtpReceiver( |
| 3141 | worker_thread(), remote_sender_info.sender_id, streams); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3142 | video_receiver->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | d367921 | 2018-01-17 17:41:02 -0800 | [diff] [blame] | 3143 | video_receiver->SetupMediaChannel(remote_sender_info.first_ssrc); |
| 3144 | auto receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create( |
| 3145 | signaling_thread(), video_receiver); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3146 | GetVideoTransceiver()->internal()->AddReceiver(receiver); |
Henrik Boström | 9e6fd2b | 2017-11-21 13:41:51 +0100 | [diff] [blame] | 3147 | observer_->OnAddTrack(receiver, std::move(streams)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3150 | // TODO(deadbeef): Keep RtpReceivers around even if track goes away in remote |
| 3151 | // description. |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3152 | rtc::scoped_refptr<RtpReceiverInterface> PeerConnection::RemoveAndStopReceiver( |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3153 | const RtpSenderInfo& remote_sender_info) { |
| 3154 | auto receiver = FindReceiverById(remote_sender_info.sender_id); |
| 3155 | if (!receiver) { |
| 3156 | RTC_LOG(LS_WARNING) << "RtpReceiver for track with id " |
| 3157 | << remote_sender_info.sender_id << " doesn't exist."; |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3158 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 3159 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3160 | if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 3161 | GetAudioTransceiver()->internal()->RemoveReceiver(receiver); |
| 3162 | } else { |
| 3163 | GetVideoTransceiver()->internal()->RemoveReceiver(receiver); |
| 3164 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3165 | return receiver; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3168 | void PeerConnection::AddAudioTrack(AudioTrackInterface* track, |
| 3169 | MediaStreamInterface* stream) { |
| 3170 | RTC_DCHECK(!IsClosed()); |
| 3171 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3172 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3173 | // We already have a sender for this track, so just change the stream_id |
| 3174 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3175 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
| 3179 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3180 | auto new_sender = |
| 3181 | CreateSender(cricket::MEDIA_TYPE_AUDIO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3182 | new_sender->internal()->SetVoiceMediaChannel(voice_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3183 | GetAudioTransceiver()->internal()->AddSender(new_sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3184 | // If the sender has already been configured in SDP, we call SetSsrc, |
| 3185 | // which will connect the sender to the underlying transport. This can |
| 3186 | // occur if a local session description that contains the ID of the sender |
| 3187 | // is set before AddStream is called. It can also occur if the local |
| 3188 | // session description is not changed and RemoveStream is called, and |
| 3189 | // later AddStream is called again with the same stream. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3190 | const RtpSenderInfo* sender_info = |
| 3191 | FindSenderInfo(local_audio_sender_infos_, stream->label(), track->id()); |
| 3192 | if (sender_info) { |
| 3193 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | // TODO(deadbeef): Don't destroy RtpSenders here; they should be kept around |
| 3198 | // indefinitely, when we have unified plan SDP. |
| 3199 | void PeerConnection::RemoveAudioTrack(AudioTrackInterface* track, |
| 3200 | MediaStreamInterface* stream) { |
| 3201 | RTC_DCHECK(!IsClosed()); |
| 3202 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3203 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3204 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3205 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3206 | return; |
| 3207 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3208 | GetAudioTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | void PeerConnection::AddVideoTrack(VideoTrackInterface* track, |
| 3212 | MediaStreamInterface* stream) { |
| 3213 | RTC_DCHECK(!IsClosed()); |
| 3214 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3215 | if (sender) { |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3216 | // We already have a sender for this track, so just change the stream_id |
| 3217 | // so that it's correct in the next call to CreateOffer. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3218 | sender->internal()->set_stream_id(stream->label()); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3219 | return; |
| 3220 | } |
| 3221 | |
| 3222 | // Normal case; we've never seen this track before. |
Steve Anton | 02ee47c | 2018-01-10 16:26:06 -0800 | [diff] [blame] | 3223 | auto new_sender = |
| 3224 | CreateSender(cricket::MEDIA_TYPE_VIDEO, track, {stream->label()}); |
Steve Anton | 57858b3 | 2018-02-15 15:19:50 -0800 | [diff] [blame] | 3225 | new_sender->internal()->SetVideoMediaChannel(video_media_channel()); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3226 | GetVideoTransceiver()->internal()->AddSender(new_sender); |
| 3227 | const RtpSenderInfo* sender_info = |
| 3228 | FindSenderInfo(local_video_sender_infos_, stream->label(), track->id()); |
| 3229 | if (sender_info) { |
| 3230 | new_sender->internal()->SetSsrc(sender_info->first_ssrc); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | void PeerConnection::RemoveVideoTrack(VideoTrackInterface* track, |
| 3235 | MediaStreamInterface* stream) { |
| 3236 | RTC_DCHECK(!IsClosed()); |
| 3237 | auto sender = FindSenderForTrack(track); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3238 | if (!sender) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3239 | RTC_LOG(LS_WARNING) << "RtpSender for track with id " << track->id() |
| 3240 | << " doesn't exist."; |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3241 | return; |
| 3242 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3243 | GetVideoTransceiver()->internal()->RemoveSender(sender); |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3246 | void PeerConnection::SetIceConnectionState(IceConnectionState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3247 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3248 | if (ice_connection_state_ == new_state) { |
| 3249 | return; |
| 3250 | } |
| 3251 | |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3252 | // After transitioning to "closed", ignore any additional states from |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3253 | // TransportController (such as "disconnected"). |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3254 | if (IsClosed()) { |
deadbeef | cbecd35 | 2015-09-23 11:50:27 -0700 | [diff] [blame] | 3255 | return; |
| 3256 | } |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3257 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3258 | RTC_LOG(LS_INFO) << "Changing IceConnectionState " << ice_connection_state_ |
| 3259 | << " => " << new_state; |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3260 | RTC_DCHECK(ice_connection_state_ != |
| 3261 | PeerConnectionInterface::kIceConnectionClosed); |
| 3262 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3263 | ice_connection_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3264 | observer_->OnIceConnectionChange(ice_connection_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | void PeerConnection::OnIceGatheringChange( |
| 3268 | PeerConnectionInterface::IceGatheringState new_state) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3269 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3270 | if (IsClosed()) { |
| 3271 | return; |
| 3272 | } |
| 3273 | ice_gathering_state_ = new_state; |
mallinath@webrtc.org | d3dc424 | 2014-03-01 00:05:52 +0000 | [diff] [blame] | 3274 | observer_->OnIceGatheringChange(ice_gathering_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3277 | void PeerConnection::OnIceCandidate( |
| 3278 | std::unique_ptr<IceCandidateInterface> candidate) { |
deadbeef | 0a6c4ca | 2015-10-06 11:38:28 -0700 | [diff] [blame] | 3279 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3280 | if (IsClosed()) { |
| 3281 | return; |
| 3282 | } |
jbauch | 81bf7b0 | 2017-03-25 08:31:12 -0700 | [diff] [blame] | 3283 | observer_->OnIceCandidate(candidate.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3286 | void PeerConnection::OnIceCandidatesRemoved( |
| 3287 | const std::vector<cricket::Candidate>& candidates) { |
| 3288 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3289 | if (IsClosed()) { |
| 3290 | return; |
| 3291 | } |
Honghai Zhang | 7fb69db | 2016-03-14 11:59:18 -0700 | [diff] [blame] | 3292 | observer_->OnIceCandidatesRemoved(candidates); |
| 3293 | } |
| 3294 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3295 | void PeerConnection::ChangeSignalingState( |
| 3296 | PeerConnectionInterface::SignalingState signaling_state) { |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 3297 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 3298 | if (signaling_state_ == signaling_state) { |
| 3299 | return; |
| 3300 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 3301 | RTC_LOG(LS_INFO) << "Session: " << session_id() << " Old state: " |
| 3302 | << GetSignalingStateString(signaling_state_) |
| 3303 | << " New state: " |
| 3304 | << GetSignalingStateString(signaling_state); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3305 | signaling_state_ = signaling_state; |
| 3306 | if (signaling_state == kClosed) { |
| 3307 | ice_connection_state_ = kIceConnectionClosed; |
| 3308 | observer_->OnIceConnectionChange(ice_connection_state_); |
| 3309 | if (ice_gathering_state_ != kIceGatheringComplete) { |
| 3310 | ice_gathering_state_ = kIceGatheringComplete; |
| 3311 | observer_->OnIceGatheringChange(ice_gathering_state_); |
| 3312 | } |
| 3313 | } |
| 3314 | observer_->OnSignalingChange(signaling_state_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3317 | void PeerConnection::OnAudioTrackAdded(AudioTrackInterface* track, |
| 3318 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3319 | if (IsClosed()) { |
| 3320 | return; |
| 3321 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3322 | AddAudioTrack(track, stream); |
| 3323 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3324 | } |
| 3325 | |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3326 | void PeerConnection::OnAudioTrackRemoved(AudioTrackInterface* track, |
| 3327 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3328 | if (IsClosed()) { |
| 3329 | return; |
| 3330 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3331 | RemoveAudioTrack(track, stream); |
| 3332 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | void PeerConnection::OnVideoTrackAdded(VideoTrackInterface* track, |
| 3336 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3337 | if (IsClosed()) { |
| 3338 | return; |
| 3339 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3340 | AddVideoTrack(track, stream); |
| 3341 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | void PeerConnection::OnVideoTrackRemoved(VideoTrackInterface* track, |
| 3345 | MediaStreamInterface* stream) { |
zhihuang | 29ff844 | 2016-07-27 11:07:25 -0700 | [diff] [blame] | 3346 | if (IsClosed()) { |
| 3347 | return; |
| 3348 | } |
korniltsev.anatoly | ec390b5 | 2017-07-24 17:00:25 -0700 | [diff] [blame] | 3349 | RemoveVideoTrack(track, stream); |
| 3350 | observer_->OnRenegotiationNeeded(); |
deadbeef | eb45981 | 2015-12-15 19:24:43 -0800 | [diff] [blame] | 3351 | } |
| 3352 | |
Henrik Boström | 3163867 | 2017-11-23 17:48:32 +0100 | [diff] [blame] | 3353 | void PeerConnection::PostSetSessionDescriptionSuccess( |
| 3354 | SetSessionDescriptionObserver* observer) { |
| 3355 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3356 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3357 | MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg); |
| 3358 | } |
| 3359 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3360 | void PeerConnection::PostSetSessionDescriptionFailure( |
| 3361 | SetSessionDescriptionObserver* observer, |
| 3362 | const std::string& error) { |
| 3363 | SetSessionDescriptionMsg* msg = new SetSessionDescriptionMsg(observer); |
| 3364 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3365 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3366 | MSG_SET_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | void PeerConnection::PostCreateSessionDescriptionFailure( |
| 3370 | CreateSessionDescriptionObserver* observer, |
| 3371 | const std::string& error) { |
| 3372 | CreateSessionDescriptionMsg* msg = new CreateSessionDescriptionMsg(observer); |
| 3373 | msg->error = error; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 3374 | signaling_thread()->Post(RTC_FROM_HERE, this, |
| 3375 | MSG_CREATE_SESSIONDESCRIPTION_FAILED, msg); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3376 | } |
| 3377 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3378 | void PeerConnection::GetOptionsForOffer( |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3379 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3380 | cricket::MediaSessionOptions* session_options) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3381 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3382 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3383 | if (IsUnifiedPlan()) { |
| 3384 | GetOptionsForUnifiedPlanOffer(offer_answer_options, session_options); |
| 3385 | } else { |
| 3386 | GetOptionsForPlanBOffer(offer_answer_options, session_options); |
| 3387 | } |
| 3388 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3389 | // Intentionally unset the data channel type for RTP data channel with the |
| 3390 | // second condition. Otherwise the RTP data channels would be successfully |
| 3391 | // negotiated by default and the unit tests in WebRtcDataBrowserTest will fail |
| 3392 | // when building with chromium. We want to leave RTP data channels broken, so |
| 3393 | // people won't try to use them. |
| 3394 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3395 | session_options->data_channel_type = data_channel_type(); |
| 3396 | } |
| 3397 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3398 | // Apply ICE restart flag and renomination flag. |
| 3399 | for (auto& options : session_options->media_description_options) { |
| 3400 | options.transport_options.ice_restart = offer_answer_options.ice_restart; |
| 3401 | options.transport_options.enable_ice_renomination = |
| 3402 | configuration_.enable_ice_renomination; |
| 3403 | } |
| 3404 | |
| 3405 | session_options->rtcp_cname = rtcp_cname_; |
| 3406 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3407 | session_options->is_unified_plan = IsUnifiedPlan(); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | void PeerConnection::GetOptionsForPlanBOffer( |
| 3411 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3412 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3413 | // Figure out transceiver directional preferences. |
| 3414 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3415 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3416 | |
| 3417 | // By default, generate sendrecv/recvonly m= sections. |
| 3418 | bool recv_audio = true; |
| 3419 | bool recv_video = true; |
| 3420 | |
| 3421 | // By default, only offer a new m= section if we have media to send with it. |
| 3422 | bool offer_new_audio_description = send_audio; |
| 3423 | bool offer_new_video_description = send_video; |
| 3424 | bool offer_new_data_description = HasDataChannels(); |
| 3425 | |
| 3426 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3427 | if (offer_answer_options.offer_to_receive_audio != |
| 3428 | RTCOfferAnswerOptions::kUndefined) { |
| 3429 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3430 | offer_new_audio_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3431 | offer_new_audio_description || |
| 3432 | (offer_answer_options.offer_to_receive_audio > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3433 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3434 | if (offer_answer_options.offer_to_receive_video != |
| 3435 | RTCOfferAnswerOptions::kUndefined) { |
| 3436 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3437 | offer_new_video_description = |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3438 | offer_new_video_description || |
| 3439 | (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | rtc::Optional<size_t> audio_index; |
| 3443 | rtc::Optional<size_t> video_index; |
| 3444 | rtc::Optional<size_t> data_index; |
| 3445 | // If a current description exists, generate m= sections in the same order, |
| 3446 | // using the first audio/video/data section that appears and rejecting |
| 3447 | // extraneous ones. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3448 | if (local_description()) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3449 | GenerateMediaDescriptionOptions( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 3450 | local_description(), |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3451 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3452 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3453 | &audio_index, &video_index, &data_index, session_options); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3456 | // Add audio/video/data m= sections to the end if needed. |
| 3457 | if (!audio_index && offer_new_audio_description) { |
| 3458 | session_options->media_description_options.push_back( |
| 3459 | cricket::MediaDescriptionOptions( |
| 3460 | cricket::MEDIA_TYPE_AUDIO, cricket::CN_AUDIO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3461 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3462 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3463 | audio_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3464 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3465 | if (!video_index && offer_new_video_description) { |
| 3466 | session_options->media_description_options.push_back( |
| 3467 | cricket::MediaDescriptionOptions( |
| 3468 | cricket::MEDIA_TYPE_VIDEO, cricket::CN_VIDEO, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3469 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), |
| 3470 | false)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3471 | video_index = session_options->media_description_options.size() - 1; |
deadbeef | c80741f | 2015-10-22 13:14:45 -0700 | [diff] [blame] | 3472 | } |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3473 | if (!data_index && offer_new_data_description) { |
| 3474 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3475 | GetMediaDescriptionOptionsForActiveData(cricket::CN_DATA)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3476 | data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3480 | !audio_index ? nullptr |
| 3481 | : &session_options->media_description_options[*audio_index]; |
| 3482 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3483 | !video_index ? nullptr |
| 3484 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3485 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3486 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3487 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | // Find a new MID that is not already in |used_mids|, then add it to |used_mids| |
| 3491 | // and return a reference to it. |
| 3492 | // Generated MIDs should be no more than 3 bytes long to take up less space in |
| 3493 | // the RTP packet. |
| 3494 | static const std::string& AllocateMid(std::set<std::string>* used_mids) { |
| 3495 | RTC_DCHECK(used_mids); |
| 3496 | // We're boring: just generate MIDs 0, 1, 2, ... |
| 3497 | size_t i = 0; |
| 3498 | std::set<std::string>::iterator it; |
| 3499 | bool inserted; |
| 3500 | do { |
| 3501 | std::string mid = rtc::ToString(i++); |
| 3502 | auto insert_result = used_mids->insert(mid); |
| 3503 | it = insert_result.first; |
| 3504 | inserted = insert_result.second; |
| 3505 | } while (!inserted); |
| 3506 | return *it; |
| 3507 | } |
| 3508 | |
| 3509 | static cricket::MediaDescriptionOptions |
| 3510 | GetMediaDescriptionOptionsForTransceiver( |
| 3511 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 3512 | transceiver, |
| 3513 | const std::string& mid) { |
| 3514 | cricket::MediaDescriptionOptions media_description_options( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3515 | transceiver->media_type(), mid, transceiver->direction(), |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3516 | transceiver->stopped()); |
Steve Anton | 5f94aa2 | 2018-02-01 10:58:30 -0800 | [diff] [blame] | 3517 | // This behavior is specified in JSEP. The gist is that: |
| 3518 | // 1. The MSID is included if the RtpTransceiver's direction is sendonly or |
| 3519 | // sendrecv. |
| 3520 | // 2. If the MSID is included, then it must be included in any subsequent |
| 3521 | // offer/answer exactly the same until the RtpTransceiver is stopped. |
| 3522 | if (!transceiver->stopped() && |
| 3523 | (RtpTransceiverDirectionHasSend(transceiver->direction()) || |
| 3524 | transceiver->internal()->has_ever_been_used_to_send())) { |
| 3525 | cricket::SenderOptions sender_options; |
| 3526 | sender_options.track_id = transceiver->sender()->id(); |
| 3527 | sender_options.stream_ids = transceiver->sender()->stream_ids(); |
| 3528 | // TODO(bugs.webrtc.org/7600): Set num_sim_layers to the number of encodings |
| 3529 | // set in the RTP parameters when the transceiver was added. |
| 3530 | sender_options.num_sim_layers = 1; |
| 3531 | media_description_options.sender_options.push_back(sender_options); |
| 3532 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3533 | return media_description_options; |
| 3534 | } |
| 3535 | |
| 3536 | void PeerConnection::GetOptionsForUnifiedPlanOffer( |
| 3537 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3538 | cricket::MediaSessionOptions* session_options) { |
| 3539 | // Rules for generating an offer are dictated by JSEP sections 5.2.1 (Initial |
| 3540 | // Offers) and 5.2.2 (Subsequent Offers). |
| 3541 | RTC_DCHECK_EQ(session_options->media_description_options.size(), 0); |
| 3542 | const ContentInfos& local_contents = |
| 3543 | (local_description() ? local_description()->description()->contents() |
| 3544 | : ContentInfos()); |
| 3545 | const ContentInfos& remote_contents = |
| 3546 | (remote_description() ? remote_description()->description()->contents() |
| 3547 | : ContentInfos()); |
| 3548 | // The mline indices that can be recycled. New transceivers should reuse these |
| 3549 | // slots first. |
| 3550 | std::queue<size_t> recycleable_mline_indices; |
| 3551 | // Track the MIDs used in previous offer/answer exchanges and the current |
| 3552 | // offer so that new, unique MIDs are generated. |
| 3553 | std::set<std::string> used_mids = seen_mids_; |
| 3554 | // First, go through each media section that exists in either the local or |
| 3555 | // remote description and generate a media section in this offer for the |
| 3556 | // associated transceiver. If a media section can be recycled, generate a |
| 3557 | // default, rejected media section here that can be later overwritten. |
| 3558 | for (size_t i = 0; |
| 3559 | i < std::max(local_contents.size(), remote_contents.size()); ++i) { |
| 3560 | // Either |local_content| or |remote_content| is non-null. |
| 3561 | const ContentInfo* local_content = |
| 3562 | (i < local_contents.size() ? &local_contents[i] : nullptr); |
| 3563 | const ContentInfo* remote_content = |
| 3564 | (i < remote_contents.size() ? &remote_contents[i] : nullptr); |
| 3565 | bool had_been_rejected = (local_content && local_content->rejected) || |
| 3566 | (remote_content && remote_content->rejected); |
| 3567 | const std::string& mid = |
| 3568 | (local_content ? local_content->name : remote_content->name); |
| 3569 | cricket::MediaType media_type = |
| 3570 | (local_content ? local_content->media_description()->type() |
| 3571 | : remote_content->media_description()->type()); |
| 3572 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3573 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3574 | auto transceiver = GetAssociatedTransceiver(mid); |
| 3575 | RTC_CHECK(transceiver); |
| 3576 | // A media section is considered eligible for recycling if it is marked as |
| 3577 | // rejected in either the local or remote description. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 3578 | if (had_been_rejected && transceiver->stopped()) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3579 | session_options->media_description_options.push_back( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 3580 | cricket::MediaDescriptionOptions(transceiver->media_type(), mid, |
| 3581 | RtpTransceiverDirection::kInactive, |
| 3582 | /*stopped=*/true)); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3583 | recycleable_mline_indices.push(i); |
| 3584 | } else { |
| 3585 | session_options->media_description_options.push_back( |
| 3586 | GetMediaDescriptionOptionsForTransceiver(transceiver, mid)); |
| 3587 | // CreateOffer shouldn't really cause any state changes in |
| 3588 | // PeerConnection, but we need a way to match new transceivers to new |
| 3589 | // media sections in SetLocalDescription and JSEP specifies this is done |
| 3590 | // by recording the index of the media section generated for the |
| 3591 | // transceiver in the offer. |
| 3592 | transceiver->internal()->set_mline_index(i); |
| 3593 | } |
| 3594 | } else { |
| 3595 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3596 | RTC_CHECK(GetDataMid()); |
| 3597 | if (had_been_rejected || mid != *GetDataMid()) { |
| 3598 | session_options->media_description_options.push_back( |
| 3599 | GetMediaDescriptionOptionsForRejectedData(mid)); |
| 3600 | } else { |
| 3601 | session_options->media_description_options.push_back( |
| 3602 | GetMediaDescriptionOptionsForActiveData(mid)); |
| 3603 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3604 | } |
| 3605 | } |
| 3606 | // Next, look for transceivers that are newly added (that is, are not stopped |
| 3607 | // and not associated). Reuse media sections marked as recyclable first, |
| 3608 | // otherwise append to the end of the offer. New media sections should be |
| 3609 | // added in the order they were added to the PeerConnection. |
| 3610 | for (auto transceiver : transceivers_) { |
| 3611 | if (transceiver->mid() || transceiver->stopped()) { |
| 3612 | continue; |
| 3613 | } |
| 3614 | size_t mline_index; |
| 3615 | if (!recycleable_mline_indices.empty()) { |
| 3616 | mline_index = recycleable_mline_indices.front(); |
| 3617 | recycleable_mline_indices.pop(); |
| 3618 | session_options->media_description_options[mline_index] = |
| 3619 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3620 | AllocateMid(&used_mids)); |
| 3621 | } else { |
| 3622 | mline_index = session_options->media_description_options.size(); |
| 3623 | session_options->media_description_options.push_back( |
| 3624 | GetMediaDescriptionOptionsForTransceiver(transceiver, |
| 3625 | AllocateMid(&used_mids))); |
| 3626 | } |
| 3627 | // See comment above for why CreateOffer changes the transceiver's state. |
| 3628 | transceiver->internal()->set_mline_index(mline_index); |
| 3629 | } |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3630 | // Lastly, add a m-section if we have local data channels and an m section |
| 3631 | // does not already exist. |
| 3632 | if (!GetDataMid() && HasDataChannels()) { |
| 3633 | session_options->media_description_options.push_back( |
| 3634 | GetMediaDescriptionOptionsForActiveData(AllocateMid(&used_mids))); |
| 3635 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | void PeerConnection::GetOptionsForAnswer( |
| 3639 | const RTCOfferAnswerOptions& offer_answer_options, |
| 3640 | cricket::MediaSessionOptions* session_options) { |
| 3641 | ExtractSharedMediaSessionOptions(offer_answer_options, session_options); |
| 3642 | |
| 3643 | if (IsUnifiedPlan()) { |
| 3644 | GetOptionsForUnifiedPlanAnswer(offer_answer_options, session_options); |
| 3645 | } else { |
| 3646 | GetOptionsForPlanBAnswer(offer_answer_options, session_options); |
| 3647 | } |
| 3648 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3649 | // Intentionally unset the data channel type for RTP data channel. Otherwise |
| 3650 | // the RTP data channels would be successfully negotiated by default and the |
| 3651 | // unit tests in WebRtcDataBrowserTest will fail when building with chromium. |
| 3652 | // We want to leave RTP data channels broken, so people won't try to use them. |
| 3653 | if (!rtp_data_channels_.empty() || data_channel_type() != cricket::DCT_RTP) { |
| 3654 | session_options->data_channel_type = data_channel_type(); |
| 3655 | } |
| 3656 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3657 | // Apply ICE renomination flag. |
| 3658 | for (auto& options : session_options->media_description_options) { |
| 3659 | options.transport_options.enable_ice_renomination = |
| 3660 | configuration_.enable_ice_renomination; |
| 3661 | } |
zhihuang | 8f65cdf | 2016-05-06 18:40:30 -0700 | [diff] [blame] | 3662 | |
| 3663 | session_options->rtcp_cname = rtcp_cname_; |
jbauch | cb56065 | 2016-08-04 05:20:32 -0700 | [diff] [blame] | 3664 | session_options->crypto_options = factory_->options().crypto_options; |
Steve Anton | e831b8c | 2018-02-01 12:22:16 -0800 | [diff] [blame] | 3665 | session_options->is_unified_plan = IsUnifiedPlan(); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3666 | } |
| 3667 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3668 | void PeerConnection::GetOptionsForPlanBAnswer( |
| 3669 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 3670 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3671 | // Figure out transceiver directional preferences. |
| 3672 | bool send_audio = HasRtpSender(cricket::MEDIA_TYPE_AUDIO); |
| 3673 | bool send_video = HasRtpSender(cricket::MEDIA_TYPE_VIDEO); |
| 3674 | |
| 3675 | // By default, generate sendrecv/recvonly m= sections. The direction is also |
| 3676 | // restricted by the direction in the offer. |
| 3677 | bool recv_audio = true; |
| 3678 | bool recv_video = true; |
| 3679 | |
| 3680 | // The "offer_to_receive_X" options allow those defaults to be overridden. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3681 | if (offer_answer_options.offer_to_receive_audio != |
| 3682 | RTCOfferAnswerOptions::kUndefined) { |
| 3683 | recv_audio = (offer_answer_options.offer_to_receive_audio > 0); |
deadbeef | 0ed85b2 | 2016-02-23 17:24:52 -0800 | [diff] [blame] | 3684 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3685 | if (offer_answer_options.offer_to_receive_video != |
| 3686 | RTCOfferAnswerOptions::kUndefined) { |
| 3687 | recv_video = (offer_answer_options.offer_to_receive_video > 0); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | rtc::Optional<size_t> audio_index; |
| 3691 | rtc::Optional<size_t> video_index; |
| 3692 | rtc::Optional<size_t> data_index; |
Steve Anton | dffead8 | 2018-02-06 10:31:29 -0800 | [diff] [blame] | 3693 | |
| 3694 | // Generate m= sections that match those in the offer. |
| 3695 | // Note that mediasession.cc will handle intersection our preferred |
| 3696 | // direction with the offered direction. |
| 3697 | GenerateMediaDescriptionOptions( |
| 3698 | remote_description(), |
| 3699 | RtpTransceiverDirectionFromSendRecv(send_audio, recv_audio), |
| 3700 | RtpTransceiverDirectionFromSendRecv(send_video, recv_video), &audio_index, |
| 3701 | &video_index, &data_index, session_options); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3702 | |
| 3703 | cricket::MediaDescriptionOptions* audio_media_description_options = |
| 3704 | !audio_index ? nullptr |
| 3705 | : &session_options->media_description_options[*audio_index]; |
| 3706 | cricket::MediaDescriptionOptions* video_media_description_options = |
| 3707 | !video_index ? nullptr |
| 3708 | : &session_options->media_description_options[*video_index]; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3709 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3710 | AddRtpSenderOptions(GetSendersInternal(), audio_media_description_options, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3711 | video_media_description_options); |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3712 | } |
zhihuang | af38847 | 2016-11-02 16:49:48 -0700 | [diff] [blame] | 3713 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3714 | void PeerConnection::GetOptionsForUnifiedPlanAnswer( |
| 3715 | const PeerConnectionInterface::RTCOfferAnswerOptions& offer_answer_options, |
| 3716 | cricket::MediaSessionOptions* session_options) { |
| 3717 | // Rules for generating an answer are dictated by JSEP sections 5.3.1 (Initial |
| 3718 | // Answers) and 5.3.2 (Subsequent Answers). |
| 3719 | RTC_DCHECK(remote_description()); |
| 3720 | RTC_DCHECK(remote_description()->GetType() == SdpType::kOffer); |
| 3721 | for (const ContentInfo& content : |
| 3722 | remote_description()->description()->contents()) { |
| 3723 | cricket::MediaType media_type = content.media_description()->type(); |
| 3724 | if (media_type == cricket::MEDIA_TYPE_AUDIO || |
| 3725 | media_type == cricket::MEDIA_TYPE_VIDEO) { |
| 3726 | auto transceiver = GetAssociatedTransceiver(content.name); |
| 3727 | RTC_CHECK(transceiver); |
| 3728 | session_options->media_description_options.push_back( |
| 3729 | GetMediaDescriptionOptionsForTransceiver(transceiver, content.name)); |
| 3730 | } else { |
| 3731 | RTC_CHECK_EQ(cricket::MEDIA_TYPE_DATA, media_type); |
Steve Anton | dbf9d03 | 2018-01-19 15:23:40 -0800 | [diff] [blame] | 3732 | // Reject all data sections if data channels are disabled. |
| 3733 | // Reject a data section if it has already been rejected. |
| 3734 | // Reject all data sections except for the first one. |
| 3735 | if (data_channel_type_ == cricket::DCT_NONE || content.rejected || |
| 3736 | content.name != *GetDataMid()) { |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3737 | session_options->media_description_options.push_back( |
| 3738 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
| 3739 | } else { |
| 3740 | session_options->media_description_options.push_back( |
| 3741 | GetMediaDescriptionOptionsForActiveData(content.name)); |
| 3742 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 3743 | } |
| 3744 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3745 | } |
| 3746 | |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3747 | void PeerConnection::GenerateMediaDescriptionOptions( |
| 3748 | const SessionDescriptionInterface* session_desc, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3749 | RtpTransceiverDirection audio_direction, |
| 3750 | RtpTransceiverDirection video_direction, |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3751 | rtc::Optional<size_t>* audio_index, |
| 3752 | rtc::Optional<size_t>* video_index, |
| 3753 | rtc::Optional<size_t>* data_index, |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3754 | cricket::MediaSessionOptions* session_options) { |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3755 | for (const cricket::ContentInfo& content : |
| 3756 | session_desc->description()->contents()) { |
| 3757 | if (IsAudioContent(&content)) { |
| 3758 | // If we already have an audio m= section, reject this extra one. |
| 3759 | if (*audio_index) { |
| 3760 | session_options->media_description_options.push_back( |
| 3761 | cricket::MediaDescriptionOptions( |
| 3762 | cricket::MEDIA_TYPE_AUDIO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3763 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3764 | } else { |
| 3765 | session_options->media_description_options.push_back( |
| 3766 | cricket::MediaDescriptionOptions( |
| 3767 | cricket::MEDIA_TYPE_AUDIO, content.name, audio_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3768 | audio_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3769 | *audio_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3770 | } |
| 3771 | } else if (IsVideoContent(&content)) { |
| 3772 | // If we already have an video m= section, reject this extra one. |
| 3773 | if (*video_index) { |
| 3774 | session_options->media_description_options.push_back( |
| 3775 | cricket::MediaDescriptionOptions( |
| 3776 | cricket::MEDIA_TYPE_VIDEO, content.name, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3777 | RtpTransceiverDirection::kInactive, true)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3778 | } else { |
| 3779 | session_options->media_description_options.push_back( |
| 3780 | cricket::MediaDescriptionOptions( |
| 3781 | cricket::MEDIA_TYPE_VIDEO, content.name, video_direction, |
Steve Anton | 1d03a75 | 2017-11-27 14:30:09 -0800 | [diff] [blame] | 3782 | video_direction == RtpTransceiverDirection::kInactive)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3783 | *video_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3784 | } |
| 3785 | } else { |
| 3786 | RTC_DCHECK(IsDataContent(&content)); |
| 3787 | // If we already have an data m= section, reject this extra one. |
| 3788 | if (*data_index) { |
| 3789 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3790 | GetMediaDescriptionOptionsForRejectedData(content.name)); |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3791 | } else { |
| 3792 | session_options->media_description_options.push_back( |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3793 | GetMediaDescriptionOptionsForActiveData(content.name)); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 3794 | *data_index = session_options->media_description_options.size() - 1; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 3795 | } |
| 3796 | } |
hta | a2a49d9 | 2016-03-04 02:51:39 -0800 | [diff] [blame] | 3797 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3798 | } |
| 3799 | |
Steve Anton | fa2260d | 2017-12-28 16:38:23 -0800 | [diff] [blame] | 3800 | cricket::MediaDescriptionOptions |
| 3801 | PeerConnection::GetMediaDescriptionOptionsForActiveData( |
| 3802 | const std::string& mid) const { |
| 3803 | // Direction for data sections is meaningless, but legacy endpoints might |
| 3804 | // expect sendrecv. |
| 3805 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3806 | RtpTransceiverDirection::kSendRecv, |
| 3807 | /*stopped=*/false); |
| 3808 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3809 | return options; |
| 3810 | } |
| 3811 | |
| 3812 | cricket::MediaDescriptionOptions |
| 3813 | PeerConnection::GetMediaDescriptionOptionsForRejectedData( |
| 3814 | const std::string& mid) const { |
| 3815 | cricket::MediaDescriptionOptions options(cricket::MEDIA_TYPE_DATA, mid, |
| 3816 | RtpTransceiverDirection::kInactive, |
| 3817 | /*stopped=*/true); |
| 3818 | AddRtpDataChannelOptions(rtp_data_channels_, &options); |
| 3819 | return options; |
| 3820 | } |
| 3821 | |
| 3822 | rtc::Optional<std::string> PeerConnection::GetDataMid() const { |
| 3823 | switch (data_channel_type_) { |
| 3824 | case cricket::DCT_RTP: |
| 3825 | if (!rtp_data_channel_) { |
| 3826 | return rtc::nullopt; |
| 3827 | } |
| 3828 | return rtp_data_channel_->content_name(); |
| 3829 | case cricket::DCT_SCTP: |
| 3830 | return sctp_content_name_; |
| 3831 | default: |
| 3832 | return rtc::nullopt; |
| 3833 | } |
| 3834 | } |
| 3835 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3836 | void PeerConnection::RemoveSenders(cricket::MediaType media_type) { |
| 3837 | UpdateLocalSenders(std::vector<cricket::StreamParams>(), media_type); |
| 3838 | UpdateRemoteSendersList(std::vector<cricket::StreamParams>(), false, |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3839 | media_type, nullptr); |
deadbeef | faac497 | 2015-11-12 15:33:07 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3842 | void PeerConnection::UpdateRemoteSendersList( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3843 | const cricket::StreamParamsVec& streams, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3844 | bool default_sender_needed, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3845 | cricket::MediaType media_type, |
| 3846 | StreamCollection* new_streams) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3847 | std::vector<RtpSenderInfo>* current_senders = |
| 3848 | GetRemoteSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3849 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3850 | // 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] | 3851 | // the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3852 | for (auto sender_it = current_senders->begin(); |
| 3853 | sender_it != current_senders->end(); |
| 3854 | /* incremented manually */) { |
| 3855 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3856 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3857 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3858 | bool sender_exists = params && params->id == info.sender_id; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3859 | // 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] | 3860 | if ((info.stream_label == kDefaultStreamLabel && default_sender_needed) || |
| 3861 | sender_exists) { |
| 3862 | ++sender_it; |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3863 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3864 | OnRemoteSenderRemoved(info, media_type); |
| 3865 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3869 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3870 | for (const cricket::StreamParams& params : streams) { |
| 3871 | // 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] | 3872 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3873 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3874 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3875 | uint32_t ssrc = params.first_ssrc(); |
| 3876 | |
| 3877 | rtc::scoped_refptr<MediaStreamInterface> stream = |
| 3878 | remote_streams_->find(stream_label); |
| 3879 | if (!stream) { |
| 3880 | // This is a new MediaStream. Create a new remote MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3881 | stream = MediaStreamProxy::Create(rtc::Thread::Current(), |
| 3882 | MediaStream::Create(stream_label)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3883 | remote_streams_->AddStream(stream); |
| 3884 | new_streams->AddStream(stream); |
| 3885 | } |
| 3886 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3887 | const RtpSenderInfo* sender_info = |
| 3888 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 3889 | if (!sender_info) { |
| 3890 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 3891 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3892 | } |
| 3893 | } |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3894 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3895 | // Add default sender if necessary. |
| 3896 | if (default_sender_needed) { |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3897 | rtc::scoped_refptr<MediaStreamInterface> default_stream = |
| 3898 | remote_streams_->find(kDefaultStreamLabel); |
| 3899 | if (!default_stream) { |
| 3900 | // Create the new default MediaStream. |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3901 | default_stream = MediaStreamProxy::Create( |
| 3902 | rtc::Thread::Current(), MediaStream::Create(kDefaultStreamLabel)); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3903 | remote_streams_->AddStream(default_stream); |
| 3904 | new_streams->AddStream(default_stream); |
| 3905 | } |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3906 | std::string default_sender_id = (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 3907 | ? kDefaultAudioSenderId |
| 3908 | : kDefaultVideoSenderId; |
| 3909 | const RtpSenderInfo* default_sender_info = FindSenderInfo( |
| 3910 | *current_senders, kDefaultStreamLabel, default_sender_id); |
| 3911 | if (!default_sender_info) { |
| 3912 | current_senders->push_back( |
| 3913 | RtpSenderInfo(kDefaultStreamLabel, default_sender_id, 0)); |
| 3914 | OnRemoteSenderAdded(current_senders->back(), media_type); |
deadbeef | bda7e0b | 2015-12-08 17:13:40 -0800 | [diff] [blame] | 3915 | } |
| 3916 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3919 | void PeerConnection::OnRemoteSenderAdded(const RtpSenderInfo& sender_info, |
| 3920 | cricket::MediaType media_type) { |
| 3921 | MediaStreamInterface* stream = |
| 3922 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3923 | |
| 3924 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3925 | CreateAudioReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3926 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3927 | CreateVideoReceiver(stream, sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3928 | } else { |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 3929 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3930 | } |
| 3931 | } |
| 3932 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3933 | void PeerConnection::OnRemoteSenderRemoved(const RtpSenderInfo& sender_info, |
| 3934 | cricket::MediaType media_type) { |
| 3935 | MediaStreamInterface* stream = |
| 3936 | remote_streams_->find(sender_info.stream_label); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3937 | |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3938 | rtc::scoped_refptr<RtpReceiverInterface> receiver; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3939 | if (media_type == cricket::MEDIA_TYPE_AUDIO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3940 | // When the MediaEngine audio channel is destroyed, the RemoteAudioSource |
| 3941 | // will be notified which will end the AudioRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3942 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3943 | rtc::scoped_refptr<AudioTrackInterface> audio_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3944 | stream->FindAudioTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3945 | if (audio_track) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3946 | stream->RemoveTrack(audio_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3947 | } |
| 3948 | } else if (media_type == cricket::MEDIA_TYPE_VIDEO) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3949 | // Stopping or destroying a VideoRtpReceiver will end the |
| 3950 | // VideoRtpReceiver::track(). |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3951 | receiver = RemoveAndStopReceiver(sender_info); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3952 | rtc::scoped_refptr<VideoTrackInterface> video_track = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3953 | stream->FindVideoTrack(sender_info.sender_id); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3954 | if (video_track) { |
perkj | d61bf80 | 2016-03-24 03:16:19 -0700 | [diff] [blame] | 3955 | // There's no guarantee the track is still available, e.g. the track may |
| 3956 | // have been removed from the stream by an application. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3957 | stream->RemoveTrack(video_track); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3958 | } |
| 3959 | } else { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 3960 | RTC_NOTREACHED() << "Invalid media type"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3961 | } |
Henrik Boström | 933d8b0 | 2017-10-10 10:05:16 -0700 | [diff] [blame] | 3962 | if (receiver) { |
| 3963 | observer_->OnRemoveTrack(receiver); |
| 3964 | } |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | void PeerConnection::UpdateEndedRemoteMediaStreams() { |
| 3968 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_to_remove; |
| 3969 | for (size_t i = 0; i < remote_streams_->count(); ++i) { |
| 3970 | MediaStreamInterface* stream = remote_streams_->at(i); |
| 3971 | if (stream->GetAudioTracks().empty() && stream->GetVideoTracks().empty()) { |
| 3972 | streams_to_remove.push_back(stream); |
| 3973 | } |
| 3974 | } |
| 3975 | |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3976 | for (auto& stream : streams_to_remove) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3977 | remote_streams_->RemoveStream(stream); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 3978 | observer_->OnRemoveStream(std::move(stream)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3982 | void PeerConnection::UpdateLocalSenders( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3983 | const std::vector<cricket::StreamParams>& streams, |
| 3984 | cricket::MediaType media_type) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3985 | std::vector<RtpSenderInfo>* current_senders = GetLocalSenderInfos(media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3986 | |
| 3987 | // Find removed tracks. I.e., tracks where the track id, stream label or ssrc |
| 3988 | // don't match the new StreamParam. |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3989 | for (auto sender_it = current_senders->begin(); |
| 3990 | sender_it != current_senders->end(); |
| 3991 | /* incremented manually */) { |
| 3992 | const RtpSenderInfo& info = *sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3993 | const cricket::StreamParams* params = |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3994 | cricket::GetStreamBySsrc(streams, info.first_ssrc); |
| 3995 | if (!params || params->id != info.sender_id || |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3996 | params->sync_label != info.stream_label) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 3997 | OnLocalSenderRemoved(info, media_type); |
| 3998 | sender_it = current_senders->erase(sender_it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 3999 | } else { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4000 | ++sender_it; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4001 | } |
| 4002 | } |
| 4003 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4004 | // Find new and active senders. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4005 | for (const cricket::StreamParams& params : streams) { |
| 4006 | // 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] | 4007 | // sender id. |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4008 | const std::string& stream_label = params.sync_label; |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4009 | const std::string& sender_id = params.id; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4010 | uint32_t ssrc = params.first_ssrc(); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4011 | const RtpSenderInfo* sender_info = |
| 4012 | FindSenderInfo(*current_senders, stream_label, sender_id); |
| 4013 | if (!sender_info) { |
| 4014 | current_senders->push_back(RtpSenderInfo(stream_label, sender_id, ssrc)); |
| 4015 | OnLocalSenderAdded(current_senders->back(), media_type); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4016 | } |
| 4017 | } |
| 4018 | } |
| 4019 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4020 | void PeerConnection::OnLocalSenderAdded(const RtpSenderInfo& sender_info, |
| 4021 | cricket::MediaType media_type) { |
| 4022 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4023 | if (!sender) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4024 | RTC_LOG(LS_WARNING) << "An unknown RtpSender with id " |
| 4025 | << sender_info.sender_id |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4026 | << " has been configured in the local description."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4027 | return; |
| 4028 | } |
| 4029 | |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4030 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4031 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4032 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4033 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4034 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4035 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4036 | sender->internal()->set_stream_id(sender_info.stream_label); |
| 4037 | sender->internal()->SetSsrc(sender_info.first_ssrc); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4038 | } |
| 4039 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4040 | void PeerConnection::OnLocalSenderRemoved(const RtpSenderInfo& sender_info, |
| 4041 | cricket::MediaType media_type) { |
| 4042 | auto sender = FindSenderById(sender_info.sender_id); |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4043 | if (!sender) { |
| 4044 | // This is the normal case. I.e., RemoveStream has been called and the |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4045 | // SessionDescriptions has been renegotiated. |
| 4046 | return; |
| 4047 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4048 | |
| 4049 | // A sender has been removed from the SessionDescription but it's still |
| 4050 | // associated with the PeerConnection. This only occurs if the SDP doesn't |
| 4051 | // match with the calls to CreateSender, AddStream and RemoveStream. |
| 4052 | if (sender->media_type() != media_type) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4053 | RTC_LOG(LS_WARNING) << "An RtpSender has been configured in the local" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4054 | " description with an unexpected media type."; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4055 | return; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4056 | } |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4057 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4058 | sender->internal()->SetSsrc(0); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | void PeerConnection::UpdateLocalRtpDataChannels( |
| 4062 | const cricket::StreamParamsVec& streams) { |
| 4063 | std::vector<std::string> existing_channels; |
| 4064 | |
| 4065 | // Find new and active data channels. |
| 4066 | for (const cricket::StreamParams& params : streams) { |
| 4067 | // |it->sync_label| is actually the data channel label. The reason is that |
| 4068 | // we use the same naming of data channels as we do for |
| 4069 | // MediaStreams and Tracks. |
| 4070 | // For MediaStreams, the sync_label is the MediaStream label and the |
| 4071 | // track label is the same as |streamid|. |
| 4072 | const std::string& channel_label = params.sync_label; |
| 4073 | auto data_channel_it = rtp_data_channels_.find(channel_label); |
nisse | 7ce109a | 2017-01-31 00:57:56 -0800 | [diff] [blame] | 4074 | if (data_channel_it == rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4075 | RTC_LOG(LS_ERROR) << "channel label not found"; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4076 | continue; |
| 4077 | } |
| 4078 | // Set the SSRC the data channel should use for sending. |
| 4079 | data_channel_it->second->SetSendSsrc(params.first_ssrc()); |
| 4080 | existing_channels.push_back(data_channel_it->first); |
| 4081 | } |
| 4082 | |
| 4083 | UpdateClosingRtpDataChannels(existing_channels, true); |
| 4084 | } |
| 4085 | |
| 4086 | void PeerConnection::UpdateRemoteRtpDataChannels( |
| 4087 | const cricket::StreamParamsVec& streams) { |
| 4088 | std::vector<std::string> existing_channels; |
| 4089 | |
| 4090 | // Find new and active data channels. |
| 4091 | for (const cricket::StreamParams& params : streams) { |
| 4092 | // The data channel label is either the mslabel or the SSRC if the mslabel |
| 4093 | // does not exist. Ex a=ssrc:444330170 mslabel:test1. |
| 4094 | std::string label = params.sync_label.empty() |
| 4095 | ? rtc::ToString(params.first_ssrc()) |
| 4096 | : params.sync_label; |
| 4097 | auto data_channel_it = rtp_data_channels_.find(label); |
| 4098 | if (data_channel_it == rtp_data_channels_.end()) { |
| 4099 | // This is a new data channel. |
| 4100 | CreateRemoteRtpDataChannel(label, params.first_ssrc()); |
| 4101 | } else { |
| 4102 | data_channel_it->second->SetReceiveSsrc(params.first_ssrc()); |
| 4103 | } |
| 4104 | existing_channels.push_back(label); |
| 4105 | } |
| 4106 | |
| 4107 | UpdateClosingRtpDataChannels(existing_channels, false); |
| 4108 | } |
| 4109 | |
| 4110 | void PeerConnection::UpdateClosingRtpDataChannels( |
| 4111 | const std::vector<std::string>& active_channels, |
| 4112 | bool is_local_update) { |
| 4113 | auto it = rtp_data_channels_.begin(); |
| 4114 | while (it != rtp_data_channels_.end()) { |
| 4115 | DataChannel* data_channel = it->second; |
| 4116 | if (std::find(active_channels.begin(), active_channels.end(), |
| 4117 | data_channel->label()) != active_channels.end()) { |
| 4118 | ++it; |
| 4119 | continue; |
| 4120 | } |
| 4121 | |
| 4122 | if (is_local_update) { |
| 4123 | data_channel->SetSendSsrc(0); |
| 4124 | } else { |
| 4125 | data_channel->RemotePeerRequestClose(); |
| 4126 | } |
| 4127 | |
| 4128 | if (data_channel->state() == DataChannel::kClosed) { |
| 4129 | rtp_data_channels_.erase(it); |
| 4130 | it = rtp_data_channels_.begin(); |
| 4131 | } else { |
| 4132 | ++it; |
| 4133 | } |
| 4134 | } |
| 4135 | } |
| 4136 | |
| 4137 | void PeerConnection::CreateRemoteRtpDataChannel(const std::string& label, |
| 4138 | uint32_t remote_ssrc) { |
| 4139 | rtc::scoped_refptr<DataChannel> channel( |
| 4140 | InternalCreateDataChannel(label, nullptr)); |
| 4141 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4142 | RTC_LOG(LS_WARNING) << "Remote peer requested a DataChannel but" |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4143 | "CreateDataChannel failed."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4144 | return; |
| 4145 | } |
| 4146 | channel->SetReceiveSsrc(remote_ssrc); |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4147 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4148 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4149 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | rtc::scoped_refptr<DataChannel> PeerConnection::InternalCreateDataChannel( |
| 4153 | const std::string& label, |
| 4154 | const InternalDataChannelInit* config) { |
| 4155 | if (IsClosed()) { |
| 4156 | return nullptr; |
| 4157 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4158 | if (data_channel_type() == cricket::DCT_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4159 | RTC_LOG(LS_ERROR) |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4160 | << "InternalCreateDataChannel: Data is not supported in this call."; |
| 4161 | return nullptr; |
| 4162 | } |
| 4163 | InternalDataChannelInit new_config = |
| 4164 | config ? (*config) : InternalDataChannelInit(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4165 | if (data_channel_type() == cricket::DCT_SCTP) { |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4166 | if (new_config.id < 0) { |
| 4167 | rtc::SSLRole role; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4168 | if ((GetSctpSslRole(&role)) && |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4169 | !sid_allocator_.AllocateSid(role, &new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4170 | RTC_LOG(LS_ERROR) |
| 4171 | << "No id can be allocated for the SCTP data channel."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4172 | return nullptr; |
| 4173 | } |
| 4174 | } else if (!sid_allocator_.ReserveSid(new_config.id)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4175 | RTC_LOG(LS_ERROR) << "Failed to create a SCTP data channel " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4176 | "because the id is already in use or out of range."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4177 | return nullptr; |
| 4178 | } |
| 4179 | } |
| 4180 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4181 | rtc::scoped_refptr<DataChannel> channel( |
| 4182 | DataChannel::Create(this, data_channel_type(), label, new_config)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4183 | if (!channel) { |
| 4184 | sid_allocator_.ReleaseSid(new_config.id); |
| 4185 | return nullptr; |
| 4186 | } |
| 4187 | |
| 4188 | if (channel->data_channel_type() == cricket::DCT_RTP) { |
| 4189 | if (rtp_data_channels_.find(channel->label()) != rtp_data_channels_.end()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4190 | RTC_LOG(LS_ERROR) << "DataChannel with label " << channel->label() |
| 4191 | << " already exists."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4192 | return nullptr; |
| 4193 | } |
| 4194 | rtp_data_channels_[channel->label()] = channel; |
| 4195 | } else { |
| 4196 | RTC_DCHECK(channel->data_channel_type() == cricket::DCT_SCTP); |
| 4197 | sctp_data_channels_.push_back(channel); |
| 4198 | channel->SignalClosed.connect(this, |
| 4199 | &PeerConnection::OnSctpDataChannelClosed); |
| 4200 | } |
| 4201 | |
Steve Anton | 2d8609c | 2018-01-23 16:38:46 -0800 | [diff] [blame] | 4202 | SignalDataChannelCreated_(channel.get()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4203 | return channel; |
| 4204 | } |
| 4205 | |
| 4206 | bool PeerConnection::HasDataChannels() const { |
| 4207 | return !rtp_data_channels_.empty() || !sctp_data_channels_.empty(); |
| 4208 | } |
| 4209 | |
| 4210 | void PeerConnection::AllocateSctpSids(rtc::SSLRole role) { |
| 4211 | for (const auto& channel : sctp_data_channels_) { |
| 4212 | if (channel->id() < 0) { |
| 4213 | int sid; |
| 4214 | if (!sid_allocator_.AllocateSid(role, &sid)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4215 | RTC_LOG(LS_ERROR) << "Failed to allocate SCTP sid."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4216 | continue; |
| 4217 | } |
| 4218 | channel->SetSctpSid(sid); |
| 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | void PeerConnection::OnSctpDataChannelClosed(DataChannel* channel) { |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4224 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4225 | for (auto it = sctp_data_channels_.begin(); it != sctp_data_channels_.end(); |
| 4226 | ++it) { |
| 4227 | if (it->get() == channel) { |
| 4228 | if (channel->id() >= 0) { |
| 4229 | sid_allocator_.ReleaseSid(channel->id()); |
| 4230 | } |
deadbeef | bd29246 | 2015-12-14 18:15:29 -0800 | [diff] [blame] | 4231 | // Since this method is triggered by a signal from the DataChannel, |
| 4232 | // we can't free it directly here; we need to free it asynchronously. |
| 4233 | sctp_data_channels_to_free_.push_back(*it); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4234 | sctp_data_channels_.erase(it); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 4235 | signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FREE_DATACHANNELS, |
| 4236 | nullptr); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4237 | return; |
| 4238 | } |
| 4239 | } |
| 4240 | } |
| 4241 | |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4242 | void PeerConnection::OnDataChannelDestroyed() { |
| 4243 | // Use a temporary copy of the RTP/SCTP DataChannel list because the |
| 4244 | // DataChannel may callback to us and try to modify the list. |
| 4245 | std::map<std::string, rtc::scoped_refptr<DataChannel>> temp_rtp_dcs; |
| 4246 | temp_rtp_dcs.swap(rtp_data_channels_); |
| 4247 | for (const auto& kv : temp_rtp_dcs) { |
| 4248 | kv.second->OnTransportChannelDestroyed(); |
| 4249 | } |
| 4250 | |
| 4251 | std::vector<rtc::scoped_refptr<DataChannel>> temp_sctp_dcs; |
| 4252 | temp_sctp_dcs.swap(sctp_data_channels_); |
| 4253 | for (const auto& channel : temp_sctp_dcs) { |
| 4254 | channel->OnTransportChannelDestroyed(); |
| 4255 | } |
| 4256 | } |
| 4257 | |
| 4258 | void PeerConnection::OnDataChannelOpenMessage( |
| 4259 | const std::string& label, |
| 4260 | const InternalDataChannelInit& config) { |
| 4261 | rtc::scoped_refptr<DataChannel> channel( |
| 4262 | InternalCreateDataChannel(label, &config)); |
| 4263 | if (!channel.get()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4264 | RTC_LOG(LS_ERROR) << "Failed to create DataChannel from the OPEN message."; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4265 | return; |
| 4266 | } |
| 4267 | |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 4268 | rtc::scoped_refptr<DataChannelInterface> proxy_channel = |
| 4269 | DataChannelProxy::Create(signaling_thread(), channel); |
Taylor Brandstetter | 98cde26 | 2016-05-31 13:02:21 -0700 | [diff] [blame] | 4270 | observer_->OnDataChannel(std::move(proxy_channel)); |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4273 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4274 | PeerConnection::GetAudioTransceiver() const { |
| 4275 | // This method only works with Plan B SDP, where there is a single |
| 4276 | // audio/video transceiver. |
| 4277 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4278 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4279 | if (transceiver->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4280 | return transceiver; |
| 4281 | } |
| 4282 | } |
| 4283 | RTC_NOTREACHED(); |
| 4284 | return nullptr; |
| 4285 | } |
| 4286 | |
| 4287 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 4288 | PeerConnection::GetVideoTransceiver() const { |
| 4289 | // This method only works with Plan B SDP, where there is a single |
| 4290 | // audio/video transceiver. |
| 4291 | RTC_DCHECK(!IsUnifiedPlan()); |
| 4292 | for (auto transceiver : transceivers_) { |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 4293 | if (transceiver->media_type() == cricket::MEDIA_TYPE_VIDEO) { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4294 | return transceiver; |
| 4295 | } |
| 4296 | } |
| 4297 | RTC_NOTREACHED(); |
| 4298 | return nullptr; |
| 4299 | } |
| 4300 | |
| 4301 | // TODO(bugs.webrtc.org/7600): Remove this when multiple transceivers with |
| 4302 | // individual transceiver directions are supported. |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4303 | bool PeerConnection::HasRtpSender(cricket::MediaType type) const { |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4304 | switch (type) { |
| 4305 | case cricket::MEDIA_TYPE_AUDIO: |
| 4306 | return !GetAudioTransceiver()->internal()->senders().empty(); |
| 4307 | case cricket::MEDIA_TYPE_VIDEO: |
| 4308 | return !GetVideoTransceiver()->internal()->senders().empty(); |
| 4309 | case cricket::MEDIA_TYPE_DATA: |
| 4310 | return false; |
| 4311 | } |
| 4312 | RTC_NOTREACHED(); |
| 4313 | return false; |
zhihuang | 1c378ed | 2017-08-17 14:10:50 -0700 | [diff] [blame] | 4314 | } |
| 4315 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4316 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4317 | PeerConnection::FindSenderForTrack(MediaStreamTrackInterface* track) const { |
| 4318 | for (auto transceiver : transceivers_) { |
| 4319 | for (auto sender : transceiver->internal()->senders()) { |
| 4320 | if (sender->track() == track) { |
| 4321 | return sender; |
| 4322 | } |
| 4323 | } |
| 4324 | } |
| 4325 | return nullptr; |
deadbeef | fac0655 | 2015-11-25 11:26:01 -0800 | [diff] [blame] | 4326 | } |
| 4327 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4328 | rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>> |
| 4329 | PeerConnection::FindSenderById(const std::string& sender_id) const { |
| 4330 | for (auto transceiver : transceivers_) { |
| 4331 | for (auto sender : transceiver->internal()->senders()) { |
| 4332 | if (sender->id() == sender_id) { |
| 4333 | return sender; |
| 4334 | } |
| 4335 | } |
| 4336 | } |
| 4337 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4338 | } |
| 4339 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4340 | rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>> |
| 4341 | PeerConnection::FindReceiverById(const std::string& receiver_id) const { |
| 4342 | for (auto transceiver : transceivers_) { |
| 4343 | for (auto receiver : transceiver->internal()->receivers()) { |
| 4344 | if (receiver->id() == receiver_id) { |
| 4345 | return receiver; |
| 4346 | } |
| 4347 | } |
| 4348 | } |
| 4349 | return nullptr; |
deadbeef | 70ab1a1 | 2015-09-28 16:53:55 -0700 | [diff] [blame] | 4350 | } |
| 4351 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4352 | std::vector<PeerConnection::RtpSenderInfo>* |
| 4353 | PeerConnection::GetRemoteSenderInfos(cricket::MediaType media_type) { |
| 4354 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4355 | media_type == cricket::MEDIA_TYPE_VIDEO); |
| 4356 | return (media_type == cricket::MEDIA_TYPE_AUDIO) |
| 4357 | ? &remote_audio_sender_infos_ |
| 4358 | : &remote_video_sender_infos_; |
| 4359 | } |
| 4360 | |
| 4361 | std::vector<PeerConnection::RtpSenderInfo>* PeerConnection::GetLocalSenderInfos( |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4362 | cricket::MediaType media_type) { |
| 4363 | RTC_DCHECK(media_type == cricket::MEDIA_TYPE_AUDIO || |
| 4364 | media_type == cricket::MEDIA_TYPE_VIDEO); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4365 | return (media_type == cricket::MEDIA_TYPE_AUDIO) ? &local_audio_sender_infos_ |
| 4366 | : &local_video_sender_infos_; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4369 | const PeerConnection::RtpSenderInfo* PeerConnection::FindSenderInfo( |
| 4370 | const std::vector<PeerConnection::RtpSenderInfo>& infos, |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4371 | const std::string& stream_label, |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 4372 | const std::string sender_id) const { |
| 4373 | for (const RtpSenderInfo& sender_info : infos) { |
| 4374 | if (sender_info.stream_label == stream_label && |
| 4375 | sender_info.sender_id == sender_id) { |
| 4376 | return &sender_info; |
deadbeef | ab9b2d1 | 2015-10-14 11:33:11 -0700 | [diff] [blame] | 4377 | } |
| 4378 | } |
| 4379 | return nullptr; |
| 4380 | } |
| 4381 | |
| 4382 | DataChannel* PeerConnection::FindDataChannelBySid(int sid) const { |
| 4383 | for (const auto& channel : sctp_data_channels_) { |
| 4384 | if (channel->id() == sid) { |
| 4385 | return channel; |
| 4386 | } |
| 4387 | } |
| 4388 | return nullptr; |
| 4389 | } |
| 4390 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4391 | bool PeerConnection::InitializePortAllocator_n( |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4392 | const RTCConfiguration& configuration) { |
| 4393 | cricket::ServerAddresses stun_servers; |
| 4394 | std::vector<cricket::RelayServerConfig> turn_servers; |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4395 | if (ParseIceServers(configuration.servers, &stun_servers, &turn_servers) != |
| 4396 | RTCErrorType::NONE) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4397 | return false; |
| 4398 | } |
| 4399 | |
Taylor Brandstetter | f8e6577 | 2016-06-27 17:20:15 -0700 | [diff] [blame] | 4400 | port_allocator_->Initialize(); |
| 4401 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4402 | // To handle both internal and externally created port allocator, we will |
| 4403 | // enable BUNDLE here. |
| 4404 | int portallocator_flags = port_allocator_->flags(); |
| 4405 | portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4406 | cricket::PORTALLOCATOR_ENABLE_IPV6 | |
| 4407 | cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4408 | // If the disable-IPv6 flag was specified, we'll not override it |
| 4409 | // by experiment. |
| 4410 | if (configuration.disable_ipv6) { |
| 4411 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
sprang | c1b57a1 | 2017-02-28 08:50:47 -0800 | [diff] [blame] | 4412 | } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") |
| 4413 | .find("Disabled") == 0) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4414 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6); |
| 4415 | } |
| 4416 | |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4417 | if (configuration.disable_ipv6_on_wifi) { |
| 4418 | portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6_ON_WIFI); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4419 | RTC_LOG(LS_INFO) << "IPv6 candidates on Wi-Fi are disabled."; |
zhihuang | b09b3f9 | 2017-03-07 14:40:51 -0800 | [diff] [blame] | 4420 | } |
| 4421 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4422 | if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) { |
| 4423 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4424 | RTC_LOG(LS_INFO) << "TCP candidates are disabled."; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4427 | if (configuration.candidate_network_policy == |
| 4428 | kCandidateNetworkPolicyLowCost) { |
| 4429 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_COSTLY_NETWORKS; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4430 | RTC_LOG(LS_INFO) << "Do not gather candidates on high-cost networks"; |
honghaiz | 6034705 | 2016-05-31 18:29:12 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
Daniel Lazarenko | 2870b0a | 2018-01-25 10:30:22 +0100 | [diff] [blame] | 4433 | if (configuration.disable_link_local_networks) { |
| 4434 | portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_LINK_LOCAL_NETWORKS; |
| 4435 | RTC_LOG(LS_INFO) << "Disable candidates on link-local network interfaces."; |
| 4436 | } |
| 4437 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4438 | port_allocator_->set_flags(portallocator_flags); |
| 4439 | // No step delay is used while allocating ports. |
| 4440 | port_allocator_->set_step_delay(cricket::kMinimumStepDelay); |
| 4441 | port_allocator_->set_candidate_filter( |
| 4442 | ConvertIceTransportTypeToCandidateFilter(configuration.type)); |
deadbeef | d21eab3 | 2017-07-26 16:50:11 -0700 | [diff] [blame] | 4443 | port_allocator_->set_max_ipv6_networks(configuration.max_ipv6_networks); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4444 | |
| 4445 | // Call this last since it may create pooled allocator sessions using the |
| 4446 | // properties set above. |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4447 | port_allocator_->SetConfiguration( |
| 4448 | stun_servers, turn_servers, configuration.ice_candidate_pool_size, |
| 4449 | configuration.prune_turn_ports, configuration.turn_customizer, |
| 4450 | configuration.stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4451 | return true; |
| 4452 | } |
| 4453 | |
deadbeef | 91dd567 | 2016-05-18 16:55:30 -0700 | [diff] [blame] | 4454 | bool PeerConnection::ReconfigurePortAllocator_n( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4455 | const cricket::ServerAddresses& stun_servers, |
| 4456 | const std::vector<cricket::RelayServerConfig>& turn_servers, |
| 4457 | IceTransportsType type, |
| 4458 | int candidate_pool_size, |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4459 | bool prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4460 | webrtc::TurnCustomizer* turn_customizer, |
| 4461 | rtc::Optional<int> stun_candidate_keepalive_interval) { |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4462 | port_allocator_->set_candidate_filter( |
deadbeef | 293e926 | 2017-01-11 12:28:30 -0800 | [diff] [blame] | 4463 | ConvertIceTransportTypeToCandidateFilter(type)); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4464 | // Call this last since it may create pooled allocator sessions using the |
| 4465 | // candidate filter set above. |
deadbeef | 6de92f9 | 2016-12-12 18:49:32 -0800 | [diff] [blame] | 4466 | return port_allocator_->SetConfiguration( |
Jonas Oreland | bdcee28 | 2017-10-10 14:01:40 +0200 | [diff] [blame] | 4467 | stun_servers, turn_servers, candidate_pool_size, prune_turn_ports, |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4468 | turn_customizer, stun_candidate_keepalive_interval); |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
Steve Anton | ba81867 | 2017-11-06 10:21:57 -0800 | [diff] [blame] | 4471 | cricket::ChannelManager* PeerConnection::channel_manager() const { |
| 4472 | return factory_->channel_manager(); |
| 4473 | } |
| 4474 | |
| 4475 | MetricsObserverInterface* PeerConnection::metrics_observer() const { |
| 4476 | return uma_observer_; |
| 4477 | } |
| 4478 | |
Elad Alon | 99c3fe5 | 2017-10-13 16:29:40 +0200 | [diff] [blame] | 4479 | bool PeerConnection::StartRtcEventLog_w( |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4480 | std::unique_ptr<RtcEventLogOutput> output, |
| 4481 | int64_t output_period_ms) { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4482 | if (!event_log_) { |
| 4483 | return false; |
| 4484 | } |
Bjorn Terelius | de93943 | 2017-11-20 17:38:14 +0100 | [diff] [blame] | 4485 | return event_log_->StartLogging(std::move(output), output_period_ms); |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | void PeerConnection::StopRtcEventLog_w() { |
zhihuang | 7798501 | 2017-02-07 15:45:16 -0800 | [diff] [blame] | 4489 | if (event_log_) { |
| 4490 | event_log_->StopLogging(); |
| 4491 | } |
ivoc | 14d5dbe | 2016-07-04 07:06:55 -0700 | [diff] [blame] | 4492 | } |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 4493 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4494 | cricket::BaseChannel* PeerConnection::GetChannel( |
| 4495 | const std::string& content_name) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4496 | for (auto transceiver : transceivers_) { |
| 4497 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4498 | if (channel && channel->content_name() == content_name) { |
| 4499 | return channel; |
| 4500 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4501 | } |
| 4502 | if (rtp_data_channel() && |
| 4503 | rtp_data_channel()->content_name() == content_name) { |
| 4504 | return rtp_data_channel(); |
| 4505 | } |
| 4506 | return nullptr; |
| 4507 | } |
| 4508 | |
| 4509 | bool PeerConnection::GetSctpSslRole(rtc::SSLRole* role) { |
| 4510 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4511 | RTC_LOG(LS_INFO) |
| 4512 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4513 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4514 | return false; |
| 4515 | } |
| 4516 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4517 | 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] | 4518 | "SSL Role of the SCTP transport."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4519 | return false; |
| 4520 | } |
| 4521 | |
| 4522 | return transport_controller_->GetSslRole(*sctp_transport_name_, role); |
| 4523 | } |
| 4524 | |
| 4525 | bool PeerConnection::GetSslRole(const std::string& content_name, |
| 4526 | rtc::SSLRole* role) { |
| 4527 | if (!local_description() || !remote_description()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4528 | RTC_LOG(LS_INFO) |
| 4529 | << "Local and Remote descriptions must be applied to get the " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4530 | "SSL Role of the session."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4531 | return false; |
| 4532 | } |
| 4533 | |
| 4534 | return transport_controller_->GetSslRole(GetTransportName(content_name), |
| 4535 | role); |
| 4536 | } |
| 4537 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4538 | void PeerConnection::SetSessionError(SessionError error, |
| 4539 | const std::string& error_desc) { |
| 4540 | RTC_DCHECK_RUN_ON(signaling_thread()); |
| 4541 | if (error != session_error_) { |
| 4542 | session_error_ = error; |
| 4543 | session_error_desc_ = error_desc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4544 | } |
| 4545 | } |
| 4546 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4547 | RTCError PeerConnection::UpdateSessionState(SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4548 | cricket::ContentSource source) { |
| 4549 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4550 | |
| 4551 | // If there's already a pending error then no state transition should happen. |
| 4552 | // But all call-sites should be verifying this before calling us! |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 4553 | RTC_DCHECK(session_error() == SessionError::kNone); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4554 | |
| 4555 | // If this is an answer then we know whether to BUNDLE or not. If both the |
| 4556 | // 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] | 4557 | if (type == SdpType::kAnswer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4558 | const cricket::ContentGroup* local_bundle = |
| 4559 | local_description()->description()->GetGroupByName( |
| 4560 | cricket::GROUP_TYPE_BUNDLE); |
| 4561 | const cricket::ContentGroup* remote_bundle = |
| 4562 | remote_description()->description()->GetGroupByName( |
| 4563 | cricket::GROUP_TYPE_BUNDLE); |
| 4564 | if (local_bundle && remote_bundle) { |
| 4565 | // The answerer decides the transport to bundle on. |
| 4566 | const cricket::ContentGroup* answer_bundle = |
| 4567 | (source == cricket::CS_LOCAL ? local_bundle : remote_bundle); |
| 4568 | if (!EnableBundle(*answer_bundle)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4569 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4570 | kEnableBundleFailed); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4571 | } |
| 4572 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4573 | } |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4574 | |
| 4575 | // Only push down the transport description after potentially enabling BUNDLE; |
| 4576 | // we don't want to push down a description on a transport about to be |
| 4577 | // destroyed. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4578 | RTCError error = PushdownTransportDescription(source, type); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4579 | if (!error.ok()) { |
| 4580 | return error; |
| 4581 | } |
| 4582 | |
| 4583 | // If this is answer-ish we're ready to let media flow. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4584 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4585 | EnableSending(); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | // Update the signaling state according to the specified state machine (see |
| 4589 | // https://w3c.github.io/webrtc-pc/#rtcsignalingstate-enum). |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4590 | if (type == SdpType::kOffer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4591 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4592 | ? PeerConnectionInterface::kHaveLocalOffer |
| 4593 | : PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4594 | } else if (type == SdpType::kPrAnswer) { |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4595 | ChangeSignalingState(source == cricket::CS_LOCAL |
| 4596 | ? PeerConnectionInterface::kHaveLocalPrAnswer |
| 4597 | : PeerConnectionInterface::kHaveRemotePrAnswer); |
| 4598 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4599 | RTC_DCHECK(type == SdpType::kAnswer); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4600 | ChangeSignalingState(PeerConnectionInterface::kStable); |
| 4601 | } |
| 4602 | |
| 4603 | // Update internal objects according to the session description's media |
| 4604 | // descriptions. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4605 | error = PushdownMediaDescription(type, source); |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4606 | if (!error.ok()) { |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 4607 | return error; |
Steve Anton | 6d6a2ae | 2017-12-04 17:19:47 -0800 | [diff] [blame] | 4608 | } |
| 4609 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4610 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4611 | } |
| 4612 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4613 | RTCError PeerConnection::PushdownMediaDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4614 | SdpType type, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4615 | cricket::ContentSource source) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4616 | const SessionDescriptionInterface* sdesc = |
| 4617 | (source == cricket::CS_LOCAL ? local_description() |
| 4618 | : remote_description()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4619 | RTC_DCHECK(sdesc); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4620 | |
| 4621 | // Push down the new SDP media section for each audio/video transceiver. |
| 4622 | for (auto transceiver : transceivers_) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4623 | const ContentInfo* content_info = |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4624 | FindMediaSectionForTransceiver(transceiver, sdesc); |
| 4625 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4626 | if (!channel || !content_info || content_info->rejected) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4627 | continue; |
| 4628 | } |
| 4629 | const MediaContentDescription* content_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4630 | content_info->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4631 | if (!content_desc) { |
| 4632 | continue; |
| 4633 | } |
| 4634 | std::string error; |
| 4635 | bool success = |
| 4636 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4637 | ? channel->SetLocalContent(content_desc, type, &error) |
| 4638 | : channel->SetRemoteContent(content_desc, type, &error); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4639 | if (!success) { |
| 4640 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, std::move(error)); |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | // If using the RtpDataChannel, push down the new SDP section for it too. |
| 4645 | if (rtp_data_channel_) { |
| 4646 | const ContentInfo* data_content = |
| 4647 | cricket::GetFirstDataContent(sdesc->description()); |
| 4648 | if (data_content && !data_content->rejected) { |
| 4649 | const MediaContentDescription* data_desc = |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 4650 | data_content->media_description(); |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4651 | if (data_desc) { |
| 4652 | std::string error; |
| 4653 | bool success = |
| 4654 | (source == cricket::CS_LOCAL) |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4655 | ? rtp_data_channel_->SetLocalContent(data_desc, type, &error) |
| 4656 | : rtp_data_channel_->SetRemoteContent(data_desc, type, |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4657 | &error); |
| 4658 | if (!success) { |
| 4659 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4660 | std::move(error)); |
| 4661 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4665 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4666 | // Need complete offer/answer with an SCTP m= section before starting SCTP, |
| 4667 | // according to https://tools.ietf.org/html/draft-ietf-mmusic-sctp-sdp-19 |
| 4668 | if (sctp_transport_ && local_description() && remote_description() && |
| 4669 | cricket::GetFirstDataContent(local_description()->description()) && |
| 4670 | cricket::GetFirstDataContent(remote_description()->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4671 | bool success = network_thread()->Invoke<bool>( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4672 | RTC_FROM_HERE, |
| 4673 | rtc::Bind(&PeerConnection::PushdownSctpParameters_n, this, source)); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4674 | if (!success) { |
| 4675 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 4676 | "Failed to push down SCTP parameters."); |
| 4677 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4678 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4679 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4680 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | bool PeerConnection::PushdownSctpParameters_n(cricket::ContentSource source) { |
| 4684 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 4685 | RTC_DCHECK(local_description()); |
| 4686 | RTC_DCHECK(remote_description()); |
| 4687 | // Apply the SCTP port (which is hidden inside a DataCodec structure...) |
| 4688 | // When we support "max-message-size", that would also be pushed down here. |
| 4689 | return sctp_transport_->Start( |
| 4690 | GetSctpPort(local_description()->description()), |
| 4691 | GetSctpPort(remote_description()->description())); |
| 4692 | } |
| 4693 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4694 | RTCError PeerConnection::PushdownTransportDescription( |
| 4695 | cricket::ContentSource source, |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4696 | SdpType type) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4697 | RTC_DCHECK_RUN_ON(signaling_thread()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4698 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4699 | const SessionDescriptionInterface* sdesc = |
| 4700 | (source == cricket::CS_LOCAL ? local_description() |
| 4701 | : remote_description()); |
| 4702 | RTC_DCHECK(sdesc); |
| 4703 | for (const cricket::TransportInfo& tinfo : |
| 4704 | sdesc->description()->transport_infos()) { |
| 4705 | std::string error; |
| 4706 | bool success; |
| 4707 | if (source == cricket::CS_LOCAL) { |
| 4708 | success = transport_controller_->SetLocalTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4709 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4710 | } else { |
| 4711 | success = transport_controller_->SetRemoteTransportDescription( |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 4712 | tinfo.content_name, tinfo.description, type, &error); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4713 | } |
| 4714 | if (!success) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 4715 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 4716 | "Failed to push down transport description for " + |
| 4717 | tinfo.content_name + ": " + error); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 4721 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4722 | } |
| 4723 | |
| 4724 | bool PeerConnection::GetTransportDescription( |
| 4725 | const SessionDescription* description, |
| 4726 | const std::string& content_name, |
| 4727 | cricket::TransportDescription* tdesc) { |
| 4728 | if (!description || !tdesc) { |
| 4729 | return false; |
| 4730 | } |
| 4731 | const TransportInfo* transport_info = |
| 4732 | description->GetTransportInfoByName(content_name); |
| 4733 | if (!transport_info) { |
| 4734 | return false; |
| 4735 | } |
| 4736 | *tdesc = transport_info->description; |
| 4737 | return true; |
| 4738 | } |
| 4739 | |
| 4740 | bool PeerConnection::EnableBundle(const cricket::ContentGroup& bundle) { |
| 4741 | const std::string* first_content_name = bundle.FirstContentName(); |
| 4742 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4743 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4744 | return false; |
| 4745 | } |
| 4746 | const std::string& transport_name = *first_content_name; |
| 4747 | |
| 4748 | auto maybe_set_transport = [this, bundle, |
| 4749 | transport_name](cricket::BaseChannel* ch) { |
| 4750 | if (!ch || !bundle.HasContentName(ch->content_name())) { |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4751 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | std::string old_transport_name = ch->transport_name(); |
| 4755 | if (old_transport_name == transport_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4756 | RTC_LOG(LS_INFO) << "BUNDLE already enabled for " << ch->content_name() |
| 4757 | << " on " << transport_name << "."; |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4758 | return; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4759 | } |
| 4760 | |
| 4761 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 4762 | transport_controller_->CreateDtlsTransport( |
| 4763 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4764 | bool need_rtcp = (ch->rtcp_dtls_transport() != nullptr); |
| 4765 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 4766 | if (need_rtcp) { |
| 4767 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 4768 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4769 | } |
| 4770 | |
| 4771 | ch->SetTransports(rtp_dtls_transport, rtcp_dtls_transport); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4772 | RTC_LOG(LS_INFO) << "Enabled BUNDLE for " << ch->content_name() << " on " |
| 4773 | << transport_name << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4774 | transport_controller_->DestroyDtlsTransport( |
| 4775 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 4776 | // If the channel needs rtcp, it means that the channel used to have a |
| 4777 | // rtcp transport which needs to be deleted now. |
| 4778 | if (need_rtcp) { |
| 4779 | transport_controller_->DestroyDtlsTransport( |
| 4780 | old_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 4781 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4782 | }; |
| 4783 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4784 | for (auto transceiver : transceivers_) { |
| 4785 | maybe_set_transport(transceiver->internal()->channel()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4786 | } |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 4787 | maybe_set_transport(rtp_data_channel_); |
| 4788 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4789 | // For SCTP, transport creation/deletion happens here instead of in the |
| 4790 | // object itself. |
| 4791 | if (sctp_transport_) { |
| 4792 | RTC_DCHECK(sctp_transport_name_); |
| 4793 | RTC_DCHECK(sctp_content_name_); |
| 4794 | if (transport_name != *sctp_transport_name_ && |
| 4795 | bundle.HasContentName(*sctp_content_name_)) { |
| 4796 | network_thread()->Invoke<void>( |
| 4797 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::ChangeSctpTransport_n, this, |
| 4798 | transport_name)); |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | return true; |
| 4803 | } |
| 4804 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4805 | cricket::IceConfig PeerConnection::ParseIceConfig( |
| 4806 | const PeerConnectionInterface::RTCConfiguration& config) const { |
| 4807 | cricket::ContinualGatheringPolicy gathering_policy; |
| 4808 | // TODO(honghaiz): Add the third continual gathering policy in |
| 4809 | // PeerConnectionInterface and map it to GATHER_CONTINUALLY_AND_RECOVER. |
| 4810 | switch (config.continual_gathering_policy) { |
| 4811 | case PeerConnectionInterface::GATHER_ONCE: |
| 4812 | gathering_policy = cricket::GATHER_ONCE; |
| 4813 | break; |
| 4814 | case PeerConnectionInterface::GATHER_CONTINUALLY: |
| 4815 | gathering_policy = cricket::GATHER_CONTINUALLY; |
| 4816 | break; |
| 4817 | default: |
| 4818 | RTC_NOTREACHED(); |
| 4819 | gathering_policy = cricket::GATHER_ONCE; |
| 4820 | } |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4821 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4822 | cricket::IceConfig ice_config; |
| 4823 | ice_config.receiving_timeout = config.ice_connection_receiving_timeout; |
| 4824 | ice_config.prioritize_most_likely_candidate_pairs = |
| 4825 | config.prioritize_most_likely_ice_candidate_pairs; |
| 4826 | ice_config.backup_connection_ping_interval = |
| 4827 | config.ice_backup_candidate_pair_ping_interval; |
| 4828 | ice_config.continual_gathering_policy = gathering_policy; |
| 4829 | ice_config.presume_writable_when_fully_relayed = |
| 4830 | config.presume_writable_when_fully_relayed; |
| 4831 | ice_config.ice_check_min_interval = config.ice_check_min_interval; |
Qingsi Wang | db53f8e | 2018-02-20 14:45:49 -0800 | [diff] [blame] | 4832 | ice_config.stun_keepalive_interval = config.stun_candidate_keepalive_interval; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4833 | ice_config.regather_all_networks_interval_range = |
| 4834 | config.ice_regather_interval_range; |
Qingsi Wang | 9a5c6f8 | 2018-02-01 10:38:40 -0800 | [diff] [blame] | 4835 | ice_config.network_preference = config.network_preference; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4836 | return ice_config; |
| 4837 | } |
| 4838 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4839 | bool PeerConnection::GetLocalTrackIdBySsrc(uint32_t ssrc, |
| 4840 | std::string* track_id) { |
| 4841 | if (!local_description()) { |
| 4842 | return false; |
| 4843 | } |
| 4844 | return webrtc::GetTrackIdBySsrc(local_description()->description(), ssrc, |
| 4845 | track_id); |
| 4846 | } |
| 4847 | |
| 4848 | bool PeerConnection::GetRemoteTrackIdBySsrc(uint32_t ssrc, |
| 4849 | std::string* track_id) { |
| 4850 | if (!remote_description()) { |
| 4851 | return false; |
| 4852 | } |
| 4853 | return webrtc::GetTrackIdBySsrc(remote_description()->description(), ssrc, |
| 4854 | track_id); |
| 4855 | } |
| 4856 | |
| 4857 | bool PeerConnection::SendData(const cricket::SendDataParams& params, |
| 4858 | const rtc::CopyOnWriteBuffer& payload, |
| 4859 | cricket::SendDataResult* result) { |
| 4860 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4861 | RTC_LOG(LS_ERROR) << "SendData called when rtp_data_channel_ " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4862 | "and sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4863 | return false; |
| 4864 | } |
| 4865 | return rtp_data_channel_ |
| 4866 | ? rtp_data_channel_->SendData(params, payload, result) |
| 4867 | : network_thread()->Invoke<bool>( |
| 4868 | RTC_FROM_HERE, |
| 4869 | Bind(&cricket::SctpTransportInternal::SendData, |
| 4870 | sctp_transport_.get(), params, payload, result)); |
| 4871 | } |
| 4872 | |
| 4873 | bool PeerConnection::ConnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4874 | if (!rtp_data_channel_ && !sctp_transport_) { |
| 4875 | // Don't log an error here, because DataChannels are expected to call |
| 4876 | // ConnectDataChannel in this state. It's the only way to initially tell |
| 4877 | // whether or not the underlying transport is ready. |
| 4878 | return false; |
| 4879 | } |
| 4880 | if (rtp_data_channel_) { |
| 4881 | rtp_data_channel_->SignalReadyToSendData.connect( |
| 4882 | webrtc_data_channel, &DataChannel::OnChannelReady); |
| 4883 | rtp_data_channel_->SignalDataReceived.connect(webrtc_data_channel, |
| 4884 | &DataChannel::OnDataReceived); |
| 4885 | } else { |
| 4886 | SignalSctpReadyToSendData.connect(webrtc_data_channel, |
| 4887 | &DataChannel::OnChannelReady); |
| 4888 | SignalSctpDataReceived.connect(webrtc_data_channel, |
| 4889 | &DataChannel::OnDataReceived); |
| 4890 | SignalSctpStreamClosedRemotely.connect( |
| 4891 | webrtc_data_channel, &DataChannel::OnStreamClosedRemotely); |
| 4892 | } |
| 4893 | return true; |
| 4894 | } |
| 4895 | |
| 4896 | void PeerConnection::DisconnectDataChannel(DataChannel* webrtc_data_channel) { |
| 4897 | if (!rtp_data_channel_ && !sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4898 | RTC_LOG(LS_ERROR) |
| 4899 | << "DisconnectDataChannel called when rtp_data_channel_ and " |
| 4900 | "sctp_transport_ are NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4901 | return; |
| 4902 | } |
| 4903 | if (rtp_data_channel_) { |
| 4904 | rtp_data_channel_->SignalReadyToSendData.disconnect(webrtc_data_channel); |
| 4905 | rtp_data_channel_->SignalDataReceived.disconnect(webrtc_data_channel); |
| 4906 | } else { |
| 4907 | SignalSctpReadyToSendData.disconnect(webrtc_data_channel); |
| 4908 | SignalSctpDataReceived.disconnect(webrtc_data_channel); |
| 4909 | SignalSctpStreamClosedRemotely.disconnect(webrtc_data_channel); |
| 4910 | } |
| 4911 | } |
| 4912 | |
| 4913 | void PeerConnection::AddSctpDataStream(int sid) { |
| 4914 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4915 | RTC_LOG(LS_ERROR) |
| 4916 | << "AddSctpDataStream called when sctp_transport_ is NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4917 | return; |
| 4918 | } |
| 4919 | network_thread()->Invoke<void>( |
| 4920 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::OpenStream, |
| 4921 | sctp_transport_.get(), sid)); |
| 4922 | } |
| 4923 | |
| 4924 | void PeerConnection::RemoveSctpDataStream(int sid) { |
| 4925 | if (!sctp_transport_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 4926 | RTC_LOG(LS_ERROR) << "RemoveSctpDataStream called when sctp_transport_ is " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 4927 | "NULL."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4928 | return; |
| 4929 | } |
| 4930 | network_thread()->Invoke<void>( |
| 4931 | RTC_FROM_HERE, rtc::Bind(&cricket::SctpTransportInternal::ResetStream, |
| 4932 | sctp_transport_.get(), sid)); |
| 4933 | } |
| 4934 | |
| 4935 | bool PeerConnection::ReadyToSendData() const { |
| 4936 | return (rtp_data_channel_ && rtp_data_channel_->ready_to_send_data()) || |
| 4937 | sctp_ready_to_send_data_; |
| 4938 | } |
| 4939 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4940 | std::map<std::string, std::string> PeerConnection::GetTransportNamesByMid() |
| 4941 | const { |
| 4942 | std::map<std::string, std::string> transport_names_by_mid; |
| 4943 | for (auto transceiver : transceivers_) { |
| 4944 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 4945 | if (channel) { |
| 4946 | transport_names_by_mid[channel->content_name()] = |
| 4947 | channel->transport_name(); |
| 4948 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4949 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4950 | if (rtp_data_channel_) { |
| 4951 | transport_names_by_mid[rtp_data_channel_->content_name()] = |
| 4952 | rtp_data_channel_->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4953 | } |
| 4954 | if (sctp_transport_) { |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4955 | transport_names_by_mid[*sctp_content_name_] = *sctp_transport_name_; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4956 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4957 | return transport_names_by_mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4958 | } |
| 4959 | |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4960 | std::map<std::string, cricket::TransportStats> |
| 4961 | PeerConnection::GetTransportStatsByNames( |
| 4962 | const std::set<std::string>& transport_names) { |
| 4963 | if (!network_thread()->IsCurrent()) { |
| 4964 | return network_thread() |
| 4965 | ->Invoke<std::map<std::string, cricket::TransportStats>>( |
| 4966 | RTC_FROM_HERE, |
| 4967 | [&] { return GetTransportStatsByNames(transport_names); }); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4968 | } |
Steve Anton | 5dfde18 | 2018-02-06 10:34:40 -0800 | [diff] [blame] | 4969 | std::map<std::string, cricket::TransportStats> transport_stats_by_name; |
| 4970 | for (const std::string& transport_name : transport_names) { |
| 4971 | cricket::TransportStats transport_stats; |
| 4972 | bool success = |
| 4973 | transport_controller_->GetStats(transport_name, &transport_stats); |
| 4974 | if (success) { |
| 4975 | transport_stats_by_name[transport_name] = std::move(transport_stats); |
| 4976 | } else { |
| 4977 | RTC_LOG(LS_ERROR) << "Failed to get transport stats for transport_name=" |
| 4978 | << transport_name; |
| 4979 | } |
| 4980 | } |
| 4981 | return transport_stats_by_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 4982 | } |
| 4983 | |
| 4984 | bool PeerConnection::GetLocalCertificate( |
| 4985 | const std::string& transport_name, |
| 4986 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 4987 | return transport_controller_->GetLocalCertificate(transport_name, |
| 4988 | certificate); |
| 4989 | } |
| 4990 | |
| 4991 | std::unique_ptr<rtc::SSLCertificate> PeerConnection::GetRemoteSSLCertificate( |
| 4992 | const std::string& transport_name) { |
| 4993 | return transport_controller_->GetRemoteSSLCertificate(transport_name); |
| 4994 | } |
| 4995 | |
| 4996 | cricket::DataChannelType PeerConnection::data_channel_type() const { |
| 4997 | return data_channel_type_; |
| 4998 | } |
| 4999 | |
| 5000 | bool PeerConnection::IceRestartPending(const std::string& content_name) const { |
| 5001 | return pending_ice_restarts_.find(content_name) != |
| 5002 | pending_ice_restarts_.end(); |
| 5003 | } |
| 5004 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5005 | bool PeerConnection::NeedsIceRestart(const std::string& content_name) const { |
| 5006 | return transport_controller_->NeedsIceRestart(content_name); |
| 5007 | } |
| 5008 | |
| 5009 | void PeerConnection::OnCertificateReady( |
| 5010 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 5011 | transport_controller_->SetLocalCertificate(certificate); |
| 5012 | } |
| 5013 | |
| 5014 | void PeerConnection::OnDtlsSrtpSetupFailure(cricket::BaseChannel*, bool rtcp) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5015 | SetSessionError(SessionError::kTransport, |
| 5016 | rtcp ? kDtlsSrtpSetupFailureRtcp : kDtlsSrtpSetupFailureRtp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | void PeerConnection::OnTransportControllerConnectionState( |
| 5020 | cricket::IceConnectionState state) { |
| 5021 | switch (state) { |
| 5022 | case cricket::kIceConnectionConnecting: |
| 5023 | // If the current state is Connected or Completed, then there were |
| 5024 | // writable channels but now there are not, so the next state must |
| 5025 | // be Disconnected. |
| 5026 | // kIceConnectionConnecting is currently used as the default, |
| 5027 | // un-connected state by the TransportController, so its only use is |
| 5028 | // detecting disconnections. |
| 5029 | if (ice_connection_state_ == |
| 5030 | PeerConnectionInterface::kIceConnectionConnected || |
| 5031 | ice_connection_state_ == |
| 5032 | PeerConnectionInterface::kIceConnectionCompleted) { |
| 5033 | SetIceConnectionState( |
| 5034 | PeerConnectionInterface::kIceConnectionDisconnected); |
| 5035 | } |
| 5036 | break; |
| 5037 | case cricket::kIceConnectionFailed: |
| 5038 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionFailed); |
| 5039 | break; |
| 5040 | case cricket::kIceConnectionConnected: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5041 | RTC_LOG(LS_INFO) << "Changing to ICE connected state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5042 | "all transports are writable."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5043 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5044 | break; |
| 5045 | case cricket::kIceConnectionCompleted: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5046 | RTC_LOG(LS_INFO) << "Changing to ICE completed state because " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5047 | "all transports are complete."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5048 | if (ice_connection_state_ != |
| 5049 | PeerConnectionInterface::kIceConnectionConnected) { |
| 5050 | // If jumping directly from "checking" to "connected", |
| 5051 | // signal "connected" first. |
| 5052 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionConnected); |
| 5053 | } |
| 5054 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionCompleted); |
| 5055 | if (metrics_observer()) { |
| 5056 | ReportTransportStats(); |
| 5057 | } |
| 5058 | break; |
| 5059 | default: |
| 5060 | RTC_NOTREACHED(); |
| 5061 | } |
| 5062 | } |
| 5063 | |
| 5064 | void PeerConnection::OnTransportControllerCandidatesGathered( |
| 5065 | const std::string& transport_name, |
| 5066 | const cricket::Candidates& candidates) { |
| 5067 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5068 | int sdp_mline_index; |
| 5069 | if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5070 | RTC_LOG(LS_ERROR) |
| 5071 | << "OnTransportControllerCandidatesGathered: content name " |
| 5072 | << transport_name << " not found"; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5073 | return; |
| 5074 | } |
| 5075 | |
| 5076 | for (cricket::Candidates::const_iterator citer = candidates.begin(); |
| 5077 | citer != candidates.end(); ++citer) { |
| 5078 | // Use transport_name as the candidate media id. |
| 5079 | std::unique_ptr<JsepIceCandidate> candidate( |
| 5080 | new JsepIceCandidate(transport_name, sdp_mline_index, *citer)); |
| 5081 | if (local_description()) { |
| 5082 | mutable_local_description()->AddCandidate(candidate.get()); |
| 5083 | } |
| 5084 | OnIceCandidate(std::move(candidate)); |
| 5085 | } |
| 5086 | } |
| 5087 | |
| 5088 | void PeerConnection::OnTransportControllerCandidatesRemoved( |
| 5089 | const std::vector<cricket::Candidate>& candidates) { |
| 5090 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5091 | // Sanity check. |
| 5092 | for (const cricket::Candidate& candidate : candidates) { |
| 5093 | if (candidate.transport_name().empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5094 | RTC_LOG(LS_ERROR) << "OnTransportControllerCandidatesRemoved: " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5095 | "empty content name in candidate " |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5096 | << candidate.ToString(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5097 | return; |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | if (local_description()) { |
| 5102 | mutable_local_description()->RemoveCandidates(candidates); |
| 5103 | } |
| 5104 | OnIceCandidatesRemoved(candidates); |
| 5105 | } |
| 5106 | |
| 5107 | void PeerConnection::OnTransportControllerDtlsHandshakeError( |
| 5108 | rtc::SSLHandshakeError error) { |
| 5109 | if (metrics_observer()) { |
| 5110 | metrics_observer()->IncrementEnumCounter( |
| 5111 | webrtc::kEnumCounterDtlsHandshakeError, static_cast<int>(error), |
| 5112 | static_cast<int>(rtc::SSLHandshakeError::MAX_VALUE)); |
| 5113 | } |
| 5114 | } |
| 5115 | |
Steve Anton | ed10bd9 | 2017-12-05 10:52:59 -0800 | [diff] [blame] | 5116 | void PeerConnection::EnableSending() { |
| 5117 | for (auto transceiver : transceivers_) { |
| 5118 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5119 | if (channel && !channel->enabled()) { |
| 5120 | channel->Enable(true); |
| 5121 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5122 | } |
| 5123 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5124 | if (rtp_data_channel_ && !rtp_data_channel_->enabled()) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5125 | rtp_data_channel_->Enable(true); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5126 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | // Returns the media index for a local ice candidate given the content name. |
| 5130 | bool PeerConnection::GetLocalCandidateMediaIndex( |
| 5131 | const std::string& content_name, |
| 5132 | int* sdp_mline_index) { |
| 5133 | if (!local_description() || !sdp_mline_index) { |
| 5134 | return false; |
| 5135 | } |
| 5136 | |
| 5137 | bool content_found = false; |
| 5138 | const ContentInfos& contents = local_description()->description()->contents(); |
| 5139 | for (size_t index = 0; index < contents.size(); ++index) { |
| 5140 | if (contents[index].name == content_name) { |
| 5141 | *sdp_mline_index = static_cast<int>(index); |
| 5142 | content_found = true; |
| 5143 | break; |
| 5144 | } |
| 5145 | } |
| 5146 | return content_found; |
| 5147 | } |
| 5148 | |
| 5149 | bool PeerConnection::UseCandidatesInSessionDescription( |
| 5150 | const SessionDescriptionInterface* remote_desc) { |
| 5151 | if (!remote_desc) { |
| 5152 | return true; |
| 5153 | } |
| 5154 | bool ret = true; |
| 5155 | |
| 5156 | for (size_t m = 0; m < remote_desc->number_of_mediasections(); ++m) { |
| 5157 | const IceCandidateCollection* candidates = remote_desc->candidates(m); |
| 5158 | for (size_t n = 0; n < candidates->count(); ++n) { |
| 5159 | const IceCandidateInterface* candidate = candidates->at(n); |
| 5160 | bool valid = false; |
| 5161 | if (!ReadyToUseRemoteCandidate(candidate, remote_desc, &valid)) { |
| 5162 | if (valid) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5163 | RTC_LOG(LS_INFO) |
| 5164 | << "UseCandidatesInSessionDescription: Not ready to use " |
Jonas Olsson | 45cc890 | 2018-02-13 10:37:07 +0100 | [diff] [blame] | 5165 | "candidate."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5166 | } |
| 5167 | continue; |
| 5168 | } |
| 5169 | ret = UseCandidate(candidate); |
| 5170 | if (!ret) { |
| 5171 | break; |
| 5172 | } |
| 5173 | } |
| 5174 | } |
| 5175 | return ret; |
| 5176 | } |
| 5177 | |
| 5178 | bool PeerConnection::UseCandidate(const IceCandidateInterface* candidate) { |
| 5179 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5180 | size_t remote_content_size = |
| 5181 | remote_description()->description()->contents().size(); |
| 5182 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5183 | RTC_LOG(LS_ERROR) << "UseCandidate: Invalid candidate media index."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5184 | return false; |
| 5185 | } |
| 5186 | |
| 5187 | cricket::ContentInfo content = |
| 5188 | remote_description()->description()->contents()[mediacontent_index]; |
| 5189 | std::vector<cricket::Candidate> candidates; |
| 5190 | candidates.push_back(candidate->candidate()); |
| 5191 | // Invoking BaseSession method to handle remote candidates. |
| 5192 | std::string error; |
| 5193 | if (transport_controller_->AddRemoteCandidates(content.name, candidates, |
| 5194 | &error)) { |
| 5195 | // Candidates successfully submitted for checking. |
| 5196 | if (ice_connection_state_ == PeerConnectionInterface::kIceConnectionNew || |
| 5197 | ice_connection_state_ == |
| 5198 | PeerConnectionInterface::kIceConnectionDisconnected) { |
| 5199 | // If state is New, then the session has just gotten its first remote ICE |
| 5200 | // candidates, so go to Checking. |
| 5201 | // If state is Disconnected, the session is re-using old candidates or |
| 5202 | // receiving additional ones, so go to Checking. |
| 5203 | // If state is Connected, stay Connected. |
| 5204 | // TODO(bemasc): If state is Connected, and the new candidates are for a |
| 5205 | // newly added transport, then the state actually _should_ move to |
| 5206 | // checking. Add a way to distinguish that case. |
| 5207 | SetIceConnectionState(PeerConnectionInterface::kIceConnectionChecking); |
| 5208 | } |
| 5209 | // TODO(bemasc): If state is Completed, go back to Connected. |
| 5210 | } else { |
| 5211 | if (!error.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5212 | RTC_LOG(LS_WARNING) << error; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5213 | } |
| 5214 | } |
| 5215 | return true; |
| 5216 | } |
| 5217 | |
| 5218 | void PeerConnection::RemoveUnusedChannels(const SessionDescription* desc) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5219 | // Destroy video channel first since it may have a pointer to the |
| 5220 | // voice channel. |
| 5221 | const cricket::ContentInfo* video_info = cricket::GetFirstVideoContent(desc); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5222 | if (!video_info || video_info->rejected) { |
| 5223 | DestroyTransceiverChannel(GetVideoTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5224 | } |
| 5225 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5226 | const cricket::ContentInfo* audio_info = cricket::GetFirstAudioContent(desc); |
| 5227 | if (!audio_info || audio_info->rejected) { |
| 5228 | DestroyTransceiverChannel(GetAudioTransceiver()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | const cricket::ContentInfo* data_info = cricket::GetFirstDataContent(desc); |
| 5232 | if (!data_info || data_info->rejected) { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5233 | DestroyDataChannel(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5234 | } |
| 5235 | } |
| 5236 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5237 | std::string PeerConnection::GetTransportNameForMediaSection( |
| 5238 | const std::string& mid, |
| 5239 | const cricket::ContentGroup* bundle_group) const { |
| 5240 | if (!bundle_group) { |
| 5241 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5242 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5243 | const std::string* first_content_name = bundle_group->FirstContentName(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5244 | if (!first_content_name) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5245 | RTC_LOG(LS_WARNING) << "Tried to BUNDLE with no contents."; |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5246 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5247 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5248 | if (!bundle_group->HasContentName(mid)) { |
| 5249 | RTC_LOG(LS_WARNING) << mid << " is not part of any bundle group"; |
| 5250 | return mid; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5251 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5252 | RTC_LOG(LS_INFO) << "Bundling " << mid << " on " << *first_content_name; |
| 5253 | return *first_content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5254 | } |
| 5255 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5256 | RTCErrorOr<const cricket::ContentGroup*> PeerConnection::GetEarlyBundleGroup( |
| 5257 | const SessionDescription& desc) const { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5258 | const cricket::ContentGroup* bundle_group = nullptr; |
| 5259 | if (configuration_.bundle_policy == |
| 5260 | PeerConnectionInterface::kBundlePolicyMaxBundle) { |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5261 | bundle_group = desc.GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5262 | if (!bundle_group) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5263 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5264 | "max-bundle configured but session description " |
| 5265 | "has no BUNDLE group"); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5266 | } |
| 5267 | } |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5268 | return std::move(bundle_group); |
| 5269 | } |
| 5270 | |
| 5271 | RTCError PeerConnection::CreateChannels(const SessionDescription& desc) { |
| 5272 | auto bundle_group_or_error = GetEarlyBundleGroup(desc); |
| 5273 | if (!bundle_group_or_error.ok()) { |
| 5274 | return bundle_group_or_error.MoveError(); |
| 5275 | } |
| 5276 | const cricket::ContentGroup* bundle_group = bundle_group_or_error.MoveValue(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5277 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5278 | // Creating the media channels and transport proxies. |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5279 | const cricket::ContentInfo* voice = cricket::GetFirstAudioContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5280 | if (voice && !voice->rejected && |
| 5281 | !GetAudioTransceiver()->internal()->channel()) { |
| 5282 | cricket::VoiceChannel* voice_channel = CreateVoiceChannel( |
| 5283 | voice->name, |
| 5284 | GetTransportNameForMediaSection(voice->name, bundle_group)); |
| 5285 | if (!voice_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5286 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5287 | "Failed to create voice channel."); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5288 | } |
| 5289 | GetAudioTransceiver()->internal()->SetChannel(voice_channel); |
| 5290 | } |
| 5291 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5292 | const cricket::ContentInfo* video = cricket::GetFirstVideoContent(&desc); |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5293 | if (video && !video->rejected && |
| 5294 | !GetVideoTransceiver()->internal()->channel()) { |
| 5295 | cricket::VideoChannel* video_channel = CreateVideoChannel( |
| 5296 | video->name, |
| 5297 | GetTransportNameForMediaSection(video->name, bundle_group)); |
| 5298 | if (!video_channel) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5299 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5300 | "Failed to create video channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5301 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5302 | GetVideoTransceiver()->internal()->SetChannel(video_channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5303 | } |
| 5304 | |
Steve Anton | dcc3c02 | 2017-12-22 16:02:54 -0800 | [diff] [blame] | 5305 | const cricket::ContentInfo* data = cricket::GetFirstDataContent(&desc); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5306 | if (data_channel_type_ != cricket::DCT_NONE && data && !data->rejected && |
| 5307 | !rtp_data_channel_ && !sctp_transport_) { |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5308 | if (!CreateDataChannel(data->name, GetTransportNameForMediaSection( |
| 5309 | data->name, bundle_group))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5310 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, |
| 5311 | "Failed to create data channel."); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5312 | } |
| 5313 | } |
| 5314 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5315 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5316 | } |
| 5317 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5318 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5319 | cricket::VoiceChannel* PeerConnection::CreateVoiceChannel( |
| 5320 | const std::string& mid, |
| 5321 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5322 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5323 | transport_controller_->CreateDtlsTransport( |
| 5324 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5325 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5326 | if (configuration_.rtcp_mux_policy != |
| 5327 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5328 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5329 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5330 | } |
| 5331 | |
| 5332 | cricket::VoiceChannel* voice_channel = channel_manager()->CreateVoiceChannel( |
| 5333 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5334 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5335 | audio_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5336 | if (!voice_channel) { |
| 5337 | transport_controller_->DestroyDtlsTransport( |
| 5338 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5339 | if (rtcp_dtls_transport) { |
| 5340 | transport_controller_->DestroyDtlsTransport( |
| 5341 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5342 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5343 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5344 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5345 | voice_channel->SignalRtcpMuxFullyActive.connect( |
| 5346 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5347 | voice_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5348 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5349 | voice_channel->SignalSentPacket.connect(this, |
| 5350 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5351 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5352 | return voice_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5353 | } |
| 5354 | |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5355 | // TODO(steveanton): Perhaps this should be managed by the RtpTransceiver. |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5356 | cricket::VideoChannel* PeerConnection::CreateVideoChannel( |
| 5357 | const std::string& mid, |
| 5358 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5359 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5360 | transport_controller_->CreateDtlsTransport( |
| 5361 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5362 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5363 | if (configuration_.rtcp_mux_policy != |
| 5364 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5365 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5366 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5367 | } |
| 5368 | |
| 5369 | cricket::VideoChannel* video_channel = channel_manager()->CreateVideoChannel( |
| 5370 | call_.get(), configuration_.media_config, rtp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5371 | rtcp_dtls_transport, signaling_thread(), mid, SrtpRequired(), |
| 5372 | video_options_); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5373 | |
| 5374 | if (!video_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 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5381 | return nullptr; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5382 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5383 | video_channel->SignalRtcpMuxFullyActive.connect( |
| 5384 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5385 | video_channel->SignalDtlsSrtpSetupFailure.connect( |
| 5386 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5387 | video_channel->SignalSentPacket.connect(this, |
| 5388 | &PeerConnection::OnSentPacket_w); |
Steve Anton | 4171afb | 2017-11-20 10:20:22 -0800 | [diff] [blame] | 5389 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5390 | return video_channel; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5391 | } |
| 5392 | |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5393 | bool PeerConnection::CreateDataChannel(const std::string& mid, |
| 5394 | const std::string& transport_name) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5395 | bool sctp = (data_channel_type_ == cricket::DCT_SCTP); |
| 5396 | if (sctp) { |
| 5397 | if (!sctp_factory_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5398 | RTC_LOG(LS_ERROR) |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5399 | << "Trying to create SCTP transport, but didn't compile with " |
| 5400 | "SCTP support (HAVE_SCTP)"; |
| 5401 | return false; |
| 5402 | } |
| 5403 | if (!network_thread()->Invoke<bool>( |
| 5404 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::CreateSctpTransport_n, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5405 | this, mid, transport_name))) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5406 | return false; |
| 5407 | } |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5408 | for (const auto& channel : sctp_data_channels_) { |
| 5409 | channel->OnTransportChannelCreated(); |
| 5410 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5411 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5412 | cricket::DtlsTransportInternal* rtp_dtls_transport = |
| 5413 | transport_controller_->CreateDtlsTransport( |
| 5414 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5415 | cricket::DtlsTransportInternal* rtcp_dtls_transport = nullptr; |
| 5416 | if (configuration_.rtcp_mux_policy != |
| 5417 | PeerConnectionInterface::kRtcpMuxPolicyRequire) { |
| 5418 | rtcp_dtls_transport = transport_controller_->CreateDtlsTransport( |
| 5419 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5420 | } |
| 5421 | |
| 5422 | rtp_data_channel_ = channel_manager()->CreateRtpDataChannel( |
| 5423 | configuration_.media_config, rtp_dtls_transport, rtcp_dtls_transport, |
Steve Anton | eda6ccd | 2017-12-04 10:21:55 -0800 | [diff] [blame] | 5424 | signaling_thread(), mid, SrtpRequired()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5425 | |
| 5426 | if (!rtp_data_channel_) { |
| 5427 | transport_controller_->DestroyDtlsTransport( |
| 5428 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5429 | if (rtcp_dtls_transport) { |
| 5430 | transport_controller_->DestroyDtlsTransport( |
| 5431 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5432 | } |
| 5433 | return false; |
| 5434 | } |
| 5435 | |
| 5436 | rtp_data_channel_->SignalRtcpMuxFullyActive.connect( |
| 5437 | this, &PeerConnection::DestroyRtcpTransport_n); |
| 5438 | rtp_data_channel_->SignalDtlsSrtpSetupFailure.connect( |
| 5439 | this, &PeerConnection::OnDtlsSrtpSetupFailure); |
| 5440 | rtp_data_channel_->SignalSentPacket.connect( |
| 5441 | this, &PeerConnection::OnSentPacket_w); |
| 5442 | } |
| 5443 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5444 | return true; |
| 5445 | } |
| 5446 | |
| 5447 | Call::Stats PeerConnection::GetCallStats() { |
| 5448 | if (!worker_thread()->IsCurrent()) { |
| 5449 | return worker_thread()->Invoke<Call::Stats>( |
| 5450 | RTC_FROM_HERE, rtc::Bind(&PeerConnection::GetCallStats, this)); |
| 5451 | } |
| 5452 | if (call_) { |
| 5453 | return call_->GetStats(); |
| 5454 | } else { |
| 5455 | return Call::Stats(); |
| 5456 | } |
| 5457 | } |
| 5458 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5459 | bool PeerConnection::CreateSctpTransport_n(const std::string& content_name, |
| 5460 | const std::string& transport_name) { |
| 5461 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5462 | RTC_DCHECK(sctp_factory_); |
| 5463 | cricket::DtlsTransportInternal* tc = |
| 5464 | transport_controller_->CreateDtlsTransport_n( |
| 5465 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5466 | sctp_transport_ = sctp_factory_->CreateSctpTransport(tc); |
| 5467 | RTC_DCHECK(sctp_transport_); |
| 5468 | sctp_invoker_.reset(new rtc::AsyncInvoker()); |
| 5469 | sctp_transport_->SignalReadyToSendData.connect( |
| 5470 | this, &PeerConnection::OnSctpTransportReadyToSendData_n); |
| 5471 | sctp_transport_->SignalDataReceived.connect( |
| 5472 | this, &PeerConnection::OnSctpTransportDataReceived_n); |
| 5473 | sctp_transport_->SignalStreamClosedRemotely.connect( |
| 5474 | this, &PeerConnection::OnSctpStreamClosedRemotely_n); |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5475 | sctp_transport_name_ = transport_name; |
| 5476 | sctp_content_name_ = content_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5477 | return true; |
| 5478 | } |
| 5479 | |
| 5480 | void PeerConnection::ChangeSctpTransport_n(const std::string& transport_name) { |
| 5481 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5482 | RTC_DCHECK(sctp_transport_); |
| 5483 | RTC_DCHECK(sctp_transport_name_); |
| 5484 | std::string old_sctp_transport_name = *sctp_transport_name_; |
Oskar Sundbom | 9b28a03 | 2017-11-16 10:53:30 +0100 | [diff] [blame] | 5485 | sctp_transport_name_ = transport_name; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5486 | cricket::DtlsTransportInternal* tc = |
| 5487 | transport_controller_->CreateDtlsTransport_n( |
| 5488 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5489 | sctp_transport_->SetTransportChannel(tc); |
| 5490 | transport_controller_->DestroyDtlsTransport_n( |
| 5491 | old_sctp_transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 5492 | } |
| 5493 | |
| 5494 | void PeerConnection::DestroySctpTransport_n() { |
| 5495 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5496 | sctp_transport_.reset(nullptr); |
Taylor Brandstetter | 389a97c | 2018-01-03 16:26:06 -0800 | [diff] [blame] | 5497 | transport_controller_->DestroyDtlsTransport_n( |
| 5498 | *sctp_transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5499 | sctp_content_name_.reset(); |
| 5500 | sctp_transport_name_.reset(); |
| 5501 | sctp_invoker_.reset(nullptr); |
| 5502 | sctp_ready_to_send_data_ = false; |
| 5503 | } |
| 5504 | |
| 5505 | void PeerConnection::OnSctpTransportReadyToSendData_n() { |
| 5506 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5507 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5508 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5509 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5510 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5511 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5512 | // pending tasks will be cleared. |
| 5513 | sctp_invoker_->AsyncInvoke<void>(RTC_FROM_HERE, signaling_thread(), [this] { |
| 5514 | OnSctpTransportReadyToSendData_s(true); |
| 5515 | }); |
| 5516 | } |
| 5517 | |
| 5518 | void PeerConnection::OnSctpTransportReadyToSendData_s(bool ready) { |
| 5519 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5520 | sctp_ready_to_send_data_ = ready; |
| 5521 | SignalSctpReadyToSendData(ready); |
| 5522 | } |
| 5523 | |
| 5524 | void PeerConnection::OnSctpTransportDataReceived_n( |
| 5525 | const cricket::ReceiveDataParams& params, |
| 5526 | const rtc::CopyOnWriteBuffer& payload) { |
| 5527 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5528 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5529 | // Note: Cannot use rtc::Bind here because it will grab a reference to |
| 5530 | // PeerConnection and potentially cause PeerConnection to live longer than |
| 5531 | // expected. It is safe not to grab a reference since the sctp_invoker_ will |
| 5532 | // be destroyed before PeerConnection is destroyed, and at that point all |
| 5533 | // pending tasks will be cleared. |
| 5534 | sctp_invoker_->AsyncInvoke<void>( |
| 5535 | RTC_FROM_HERE, signaling_thread(), [this, params, payload] { |
| 5536 | OnSctpTransportDataReceived_s(params, payload); |
| 5537 | }); |
| 5538 | } |
| 5539 | |
| 5540 | void PeerConnection::OnSctpTransportDataReceived_s( |
| 5541 | const cricket::ReceiveDataParams& params, |
| 5542 | const rtc::CopyOnWriteBuffer& payload) { |
| 5543 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5544 | if (params.type == cricket::DMT_CONTROL && IsOpenMessage(payload)) { |
| 5545 | // Received OPEN message; parse and signal that a new data channel should |
| 5546 | // be created. |
| 5547 | std::string label; |
| 5548 | InternalDataChannelInit config; |
| 5549 | config.id = params.ssrc; |
| 5550 | if (!ParseDataChannelOpenMessage(payload, &label, &config)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5551 | RTC_LOG(LS_WARNING) << "Failed to parse the OPEN message for sid " |
| 5552 | << params.ssrc; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5553 | return; |
| 5554 | } |
| 5555 | config.open_handshake_role = InternalDataChannelInit::kAcker; |
| 5556 | OnDataChannelOpenMessage(label, config); |
| 5557 | } else { |
| 5558 | // Otherwise just forward the signal. |
| 5559 | SignalSctpDataReceived(params, payload); |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | void PeerConnection::OnSctpStreamClosedRemotely_n(int sid) { |
| 5564 | RTC_DCHECK(data_channel_type_ == cricket::DCT_SCTP); |
| 5565 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5566 | sctp_invoker_->AsyncInvoke<void>( |
| 5567 | RTC_FROM_HERE, signaling_thread(), |
| 5568 | rtc::Bind(&sigslot::signal1<int>::operator(), |
| 5569 | &SignalSctpStreamClosedRemotely, sid)); |
| 5570 | } |
| 5571 | |
| 5572 | // Returns false if bundle is enabled and rtcp_mux is disabled. |
| 5573 | bool PeerConnection::ValidateBundleSettings(const SessionDescription* desc) { |
| 5574 | bool bundle_enabled = desc->HasGroup(cricket::GROUP_TYPE_BUNDLE); |
| 5575 | if (!bundle_enabled) |
| 5576 | return true; |
| 5577 | |
| 5578 | const cricket::ContentGroup* bundle_group = |
| 5579 | desc->GetGroupByName(cricket::GROUP_TYPE_BUNDLE); |
| 5580 | RTC_DCHECK(bundle_group != NULL); |
| 5581 | |
| 5582 | const cricket::ContentInfos& contents = desc->contents(); |
| 5583 | for (cricket::ContentInfos::const_iterator citer = contents.begin(); |
| 5584 | citer != contents.end(); ++citer) { |
| 5585 | const cricket::ContentInfo* content = (&*citer); |
| 5586 | RTC_DCHECK(content != NULL); |
| 5587 | if (bundle_group->HasContentName(content->name) && !content->rejected && |
Steve Anton | 5adfafd | 2017-12-20 16:34:00 -0800 | [diff] [blame] | 5588 | content->type == MediaProtocolType::kRtp) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5589 | if (!HasRtcpMuxEnabled(content)) |
| 5590 | return false; |
| 5591 | } |
| 5592 | } |
| 5593 | // RTCP-MUX is enabled in all the contents. |
| 5594 | return true; |
| 5595 | } |
| 5596 | |
| 5597 | bool PeerConnection::HasRtcpMuxEnabled(const cricket::ContentInfo* content) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 5598 | return content->media_description()->rtcp_mux(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5599 | } |
| 5600 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5601 | RTCError PeerConnection::ValidateSessionDescription( |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5602 | const SessionDescriptionInterface* sdesc, |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5603 | cricket::ContentSource source) { |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5604 | if (session_error() != SessionError::kNone) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5605 | LOG_AND_RETURN_ERROR(RTCErrorType::INTERNAL_ERROR, GetSessionErrorMsg()); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | if (!sdesc || !sdesc->description()) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5609 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, kInvalidSdp); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5610 | } |
| 5611 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5612 | SdpType type = sdesc->GetType(); |
| 5613 | if ((source == cricket::CS_LOCAL && !ExpectSetLocalDescription(type)) || |
| 5614 | (source == cricket::CS_REMOTE && !ExpectSetRemoteDescription(type))) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5615 | LOG_AND_RETURN_ERROR( |
| 5616 | RTCErrorType::INVALID_PARAMETER, |
| 5617 | "Called in wrong state: " + GetSignalingStateString(signaling_state())); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5618 | } |
| 5619 | |
| 5620 | // Verify crypto settings. |
| 5621 | std::string crypto_error; |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5622 | if (webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED || |
| 5623 | dtls_enabled_) { |
Harald Alvestrand | 194939b | 2018-01-24 16:04:13 +0100 | [diff] [blame] | 5624 | RTCError crypto_error = |
| 5625 | VerifyCrypto(sdesc->description(), dtls_enabled_, uma_observer_); |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5626 | if (!crypto_error.ok()) { |
| 5627 | return crypto_error; |
| 5628 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | // Verify ice-ufrag and ice-pwd. |
| 5632 | if (!VerifyIceUfragPwdPresent(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5633 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5634 | kSdpWithoutIceUfragPwd); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | if (!ValidateBundleSettings(sdesc->description())) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5638 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5639 | kBundleWithoutRtcpMux); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | // TODO(skvlad): When the local rtcp-mux policy is Require, reject any |
| 5643 | // m-lines that do not rtcp-mux enabled. |
| 5644 | |
| 5645 | // Verify m-lines in Answer when compared against Offer. |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5646 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5647 | // With an answer we want to compare the new answer session description with |
| 5648 | // the offer's session description from the current negotiation. |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5649 | const cricket::SessionDescription* offer_desc = |
| 5650 | (source == cricket::CS_LOCAL) ? remote_description()->description() |
| 5651 | : local_description()->description(); |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5652 | if (!MediaSectionsHaveSameCount(*offer_desc, *sdesc->description()) || |
| 5653 | !MediaSectionsInSameOrder(*offer_desc, nullptr, *sdesc->description(), |
| 5654 | type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5655 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5656 | kMlineMismatchInAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5657 | } |
| 5658 | } else { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5659 | // The re-offers should respect the order of m= sections in current |
| 5660 | // description. See RFC3264 Section 8 paragraph 4 for more details. |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5661 | // With a re-offer, either the current local or current remote descriptions |
| 5662 | // could be the most up to date, so we would like to check against both of |
| 5663 | // them if they exist. It could be the case that one of them has a 0 port |
| 5664 | // for a media section, but the other does not. This is important to check |
| 5665 | // against in the case that we are recycling an m= section. |
| 5666 | const cricket::SessionDescription* current_desc = nullptr; |
| 5667 | const cricket::SessionDescription* secondary_current_desc = nullptr; |
| 5668 | if (local_description()) { |
| 5669 | current_desc = local_description()->description(); |
| 5670 | if (remote_description()) { |
| 5671 | secondary_current_desc = remote_description()->description(); |
| 5672 | } |
| 5673 | } else if (remote_description()) { |
| 5674 | current_desc = remote_description()->description(); |
| 5675 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5676 | if (current_desc && |
Seth Hampson | ae8a90a | 2018-02-13 15:33:48 -0800 | [diff] [blame] | 5677 | !MediaSectionsInSameOrder(*current_desc, secondary_current_desc, |
| 5678 | *sdesc->description(), type)) { |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5679 | LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, |
| 5680 | kMlineMismatchInSubsequentOffer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5681 | } |
| 5682 | } |
| 5683 | |
Steve Anton | 8a00691 | 2017-12-04 15:25:56 -0800 | [diff] [blame] | 5684 | return RTCError::OK(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5685 | } |
| 5686 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5687 | bool PeerConnection::ExpectSetLocalDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5688 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5689 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5690 | return (state == PeerConnectionInterface::kStable) || |
| 5691 | (state == PeerConnectionInterface::kHaveLocalOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5692 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5693 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5694 | return (state == PeerConnectionInterface::kHaveRemoteOffer) || |
| 5695 | (state == PeerConnectionInterface::kHaveLocalPrAnswer); |
| 5696 | } |
| 5697 | } |
| 5698 | |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5699 | bool PeerConnection::ExpectSetRemoteDescription(SdpType type) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5700 | PeerConnectionInterface::SignalingState state = signaling_state(); |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5701 | if (type == SdpType::kOffer) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5702 | return (state == PeerConnectionInterface::kStable) || |
| 5703 | (state == PeerConnectionInterface::kHaveRemoteOffer); |
Steve Anton | 2039306 | 2017-12-04 16:24:52 -0800 | [diff] [blame] | 5704 | } else { |
Steve Anton | 3828c06 | 2017-12-06 10:34:51 -0800 | [diff] [blame] | 5705 | RTC_DCHECK(type == SdpType::kPrAnswer || type == SdpType::kAnswer); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5706 | return (state == PeerConnectionInterface::kHaveLocalOffer) || |
| 5707 | (state == PeerConnectionInterface::kHaveRemotePrAnswer); |
| 5708 | } |
| 5709 | } |
| 5710 | |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5711 | const char* PeerConnection::SessionErrorToString(SessionError error) const { |
| 5712 | switch (error) { |
| 5713 | case SessionError::kNone: |
| 5714 | return "ERROR_NONE"; |
| 5715 | case SessionError::kContent: |
| 5716 | return "ERROR_CONTENT"; |
| 5717 | case SessionError::kTransport: |
| 5718 | return "ERROR_TRANSPORT"; |
| 5719 | } |
| 5720 | RTC_NOTREACHED(); |
| 5721 | return ""; |
| 5722 | } |
| 5723 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5724 | std::string PeerConnection::GetSessionErrorMsg() { |
| 5725 | std::ostringstream desc; |
Steve Anton | f847081 | 2017-12-04 10:46:21 -0800 | [diff] [blame] | 5726 | desc << kSessionError << SessionErrorToString(session_error()) << ". "; |
| 5727 | desc << kSessionErrorDesc << session_error_desc() << "."; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5728 | return desc.str(); |
| 5729 | } |
| 5730 | |
| 5731 | // We need to check the local/remote description for the Transport instead of |
| 5732 | // the session, because a new Transport added during renegotiation may have |
| 5733 | // them unset while the session has them set from the previous negotiation. |
| 5734 | // Not doing so may trigger the auto generation of transport description and |
| 5735 | // mess up DTLS identity information, ICE credential, etc. |
| 5736 | bool PeerConnection::ReadyToUseRemoteCandidate( |
| 5737 | const IceCandidateInterface* candidate, |
| 5738 | const SessionDescriptionInterface* remote_desc, |
| 5739 | bool* valid) { |
| 5740 | *valid = true; |
| 5741 | |
| 5742 | const SessionDescriptionInterface* current_remote_desc = |
| 5743 | remote_desc ? remote_desc : remote_description(); |
| 5744 | |
| 5745 | if (!current_remote_desc) { |
| 5746 | return false; |
| 5747 | } |
| 5748 | |
| 5749 | size_t mediacontent_index = static_cast<size_t>(candidate->sdp_mline_index()); |
| 5750 | size_t remote_content_size = |
| 5751 | current_remote_desc->description()->contents().size(); |
| 5752 | if (mediacontent_index >= remote_content_size) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 5753 | RTC_LOG(LS_ERROR) |
| 5754 | << "ReadyToUseRemoteCandidate: Invalid candidate media index " |
| 5755 | << mediacontent_index; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5756 | |
| 5757 | *valid = false; |
| 5758 | return false; |
| 5759 | } |
| 5760 | |
| 5761 | cricket::ContentInfo content = |
| 5762 | current_remote_desc->description()->contents()[mediacontent_index]; |
| 5763 | |
| 5764 | const std::string transport_name = GetTransportName(content.name); |
| 5765 | if (transport_name.empty()) { |
| 5766 | return false; |
| 5767 | } |
| 5768 | return transport_controller_->ReadyForRemoteCandidates(transport_name); |
| 5769 | } |
| 5770 | |
| 5771 | bool PeerConnection::SrtpRequired() const { |
| 5772 | return dtls_enabled_ || |
| 5773 | webrtc_session_desc_factory_->SdesPolicy() == cricket::SEC_REQUIRED; |
| 5774 | } |
| 5775 | |
| 5776 | void PeerConnection::OnTransportControllerGatheringState( |
| 5777 | cricket::IceGatheringState state) { |
| 5778 | RTC_DCHECK(signaling_thread()->IsCurrent()); |
| 5779 | if (state == cricket::kIceGatheringGathering) { |
| 5780 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringGathering); |
| 5781 | } else if (state == cricket::kIceGatheringComplete) { |
| 5782 | OnIceGatheringChange(PeerConnectionInterface::kIceGatheringComplete); |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | void PeerConnection::ReportTransportStats() { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5787 | std::map<std::string, std::set<cricket::MediaType>> |
| 5788 | media_types_by_transport_name; |
| 5789 | for (auto transceiver : transceivers_) { |
| 5790 | if (transceiver->internal()->channel()) { |
| 5791 | const std::string& transport_name = |
| 5792 | transceiver->internal()->channel()->transport_name(); |
| 5793 | media_types_by_transport_name[transport_name].insert( |
Steve Anton | 6947025 | 2018-02-09 11:43:08 -0800 | [diff] [blame] | 5794 | transceiver->media_type()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5795 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5796 | } |
| 5797 | if (rtp_data_channel()) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5798 | media_types_by_transport_name[rtp_data_channel()->transport_name()].insert( |
| 5799 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5800 | } |
| 5801 | if (sctp_transport_name_) { |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5802 | media_types_by_transport_name[*sctp_transport_name_].insert( |
| 5803 | cricket::MEDIA_TYPE_DATA); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5804 | } |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5805 | for (const auto& entry : media_types_by_transport_name) { |
| 5806 | const std::string& transport_name = entry.first; |
| 5807 | const std::set<cricket::MediaType> media_types = entry.second; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5808 | cricket::TransportStats stats; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5809 | if (transport_controller_->GetStats(transport_name, &stats)) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5810 | ReportBestConnectionState(stats); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5811 | ReportNegotiatedCiphers(stats, media_types); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5812 | } |
| 5813 | } |
| 5814 | } |
| 5815 | // Walk through the ConnectionInfos to gather best connection usage |
| 5816 | // for IPv4 and IPv6. |
| 5817 | void PeerConnection::ReportBestConnectionState( |
| 5818 | const cricket::TransportStats& stats) { |
| 5819 | RTC_DCHECK(metrics_observer()); |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5820 | for (const cricket::TransportChannelStats& channel_stats : |
| 5821 | stats.channel_stats) { |
| 5822 | for (const cricket::ConnectionInfo& connection_info : |
| 5823 | channel_stats.connection_infos) { |
| 5824 | if (!connection_info.best_connection) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5825 | continue; |
| 5826 | } |
| 5827 | |
| 5828 | PeerConnectionEnumCounterType type = kPeerConnectionEnumCounterMax; |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5829 | const cricket::Candidate& local = connection_info.local_candidate; |
| 5830 | const cricket::Candidate& remote = connection_info.remote_candidate; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5831 | |
| 5832 | // Increment the counter for IceCandidatePairType. |
| 5833 | if (local.protocol() == cricket::TCP_PROTOCOL_NAME || |
| 5834 | (local.type() == RELAY_PORT_TYPE && |
| 5835 | local.relay_protocol() == cricket::TCP_PROTOCOL_NAME)) { |
| 5836 | type = kEnumCounterIceCandidatePairTypeTcp; |
| 5837 | } else if (local.protocol() == cricket::UDP_PROTOCOL_NAME) { |
| 5838 | type = kEnumCounterIceCandidatePairTypeUdp; |
| 5839 | } else { |
| 5840 | RTC_CHECK(0); |
| 5841 | } |
| 5842 | metrics_observer()->IncrementEnumCounter( |
| 5843 | type, GetIceCandidatePairCounter(local, remote), |
| 5844 | kIceCandidatePairMax); |
| 5845 | |
| 5846 | // Increment the counter for IP type. |
| 5847 | if (local.address().family() == AF_INET) { |
| 5848 | metrics_observer()->IncrementEnumCounter( |
| 5849 | kEnumCounterAddressFamily, kBestConnections_IPv4, |
| 5850 | kPeerConnectionAddressFamilyCounter_Max); |
| 5851 | |
| 5852 | } else if (local.address().family() == AF_INET6) { |
| 5853 | metrics_observer()->IncrementEnumCounter( |
| 5854 | kEnumCounterAddressFamily, kBestConnections_IPv6, |
| 5855 | kPeerConnectionAddressFamilyCounter_Max); |
| 5856 | } else { |
| 5857 | RTC_CHECK(0); |
| 5858 | } |
| 5859 | |
| 5860 | return; |
| 5861 | } |
| 5862 | } |
| 5863 | } |
| 5864 | |
| 5865 | void PeerConnection::ReportNegotiatedCiphers( |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5866 | const cricket::TransportStats& stats, |
| 5867 | const std::set<cricket::MediaType>& media_types) { |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5868 | RTC_DCHECK(metrics_observer()); |
| 5869 | if (!dtls_enabled_ || stats.channel_stats.empty()) { |
| 5870 | return; |
| 5871 | } |
| 5872 | |
| 5873 | int srtp_crypto_suite = stats.channel_stats[0].srtp_crypto_suite; |
| 5874 | int ssl_cipher_suite = stats.channel_stats[0].ssl_cipher_suite; |
| 5875 | if (srtp_crypto_suite == rtc::SRTP_INVALID_CRYPTO_SUITE && |
| 5876 | ssl_cipher_suite == rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5877 | return; |
| 5878 | } |
| 5879 | |
Steve Anton | c7b964c | 2018-02-01 14:39:45 -0800 | [diff] [blame] | 5880 | for (cricket::MediaType media_type : media_types) { |
| 5881 | PeerConnectionEnumCounterType srtp_counter_type; |
| 5882 | PeerConnectionEnumCounterType ssl_counter_type; |
| 5883 | switch (media_type) { |
| 5884 | case cricket::MEDIA_TYPE_AUDIO: |
| 5885 | srtp_counter_type = kEnumCounterAudioSrtpCipher; |
| 5886 | ssl_counter_type = kEnumCounterAudioSslCipher; |
| 5887 | break; |
| 5888 | case cricket::MEDIA_TYPE_VIDEO: |
| 5889 | srtp_counter_type = kEnumCounterVideoSrtpCipher; |
| 5890 | ssl_counter_type = kEnumCounterVideoSslCipher; |
| 5891 | break; |
| 5892 | case cricket::MEDIA_TYPE_DATA: |
| 5893 | srtp_counter_type = kEnumCounterDataSrtpCipher; |
| 5894 | ssl_counter_type = kEnumCounterDataSslCipher; |
| 5895 | break; |
| 5896 | default: |
| 5897 | RTC_NOTREACHED(); |
| 5898 | continue; |
| 5899 | } |
| 5900 | if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 5901 | metrics_observer()->IncrementSparseEnumCounter(srtp_counter_type, |
| 5902 | srtp_crypto_suite); |
| 5903 | } |
| 5904 | if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL) { |
| 5905 | metrics_observer()->IncrementSparseEnumCounter(ssl_counter_type, |
| 5906 | ssl_cipher_suite); |
| 5907 | } |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5908 | } |
| 5909 | } |
| 5910 | |
| 5911 | void PeerConnection::OnSentPacket_w(const rtc::SentPacket& sent_packet) { |
| 5912 | RTC_DCHECK(worker_thread()->IsCurrent()); |
| 5913 | RTC_DCHECK(call_); |
| 5914 | call_->OnSentPacket(sent_packet); |
| 5915 | } |
| 5916 | |
| 5917 | const std::string PeerConnection::GetTransportName( |
| 5918 | const std::string& content_name) { |
| 5919 | cricket::BaseChannel* channel = GetChannel(content_name); |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5920 | if (channel) { |
| 5921 | return channel->transport_name(); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5922 | } |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5923 | if (sctp_transport_) { |
| 5924 | RTC_DCHECK(sctp_content_name_); |
| 5925 | RTC_DCHECK(sctp_transport_name_); |
| 5926 | if (content_name == *sctp_content_name_) { |
| 5927 | return *sctp_transport_name_; |
| 5928 | } |
| 5929 | } |
| 5930 | // Return an empty string if failed to retrieve the transport name. |
| 5931 | return ""; |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5932 | } |
| 5933 | |
| 5934 | void PeerConnection::DestroyRtcpTransport_n(const std::string& transport_name) { |
| 5935 | RTC_DCHECK(network_thread()->IsCurrent()); |
| 5936 | transport_controller_->DestroyDtlsTransport_n( |
| 5937 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 5938 | } |
| 5939 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5940 | void PeerConnection::DestroyTransceiverChannel( |
| 5941 | rtc::scoped_refptr<RtpTransceiverProxyWithInternal<RtpTransceiver>> |
| 5942 | transceiver) { |
| 5943 | RTC_DCHECK(transceiver); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5944 | |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5945 | cricket::BaseChannel* channel = transceiver->internal()->channel(); |
| 5946 | if (channel) { |
| 5947 | transceiver->internal()->SetChannel(nullptr); |
| 5948 | DestroyBaseChannel(channel); |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 5949 | } |
| 5950 | } |
| 5951 | |
| 5952 | void PeerConnection::DestroyDataChannel() { |
Steve Anton | 6fec880 | 2017-12-04 10:37:29 -0800 | [diff] [blame] | 5953 | if (rtp_data_channel_) { |
| 5954 | OnDataChannelDestroyed(); |
| 5955 | DestroyBaseChannel(rtp_data_channel_); |
| 5956 | rtp_data_channel_ = nullptr; |
| 5957 | } |
| 5958 | |
| 5959 | // Note: Cannot use rtc::Bind to create a functor to invoke because it will |
| 5960 | // grab a reference to this PeerConnection. If this is called from the |
| 5961 | // PeerConnection destructor, the RefCountedObject vtable will have already |
| 5962 | // been destroyed (since it is a subclass of PeerConnection) and using |
| 5963 | // rtc::Bind will cause "Pure virtual function called" error to appear. |
| 5964 | |
| 5965 | if (sctp_transport_) { |
| 5966 | OnDataChannelDestroyed(); |
| 5967 | network_thread()->Invoke<void>(RTC_FROM_HERE, |
| 5968 | [this] { DestroySctpTransport_n(); }); |
| 5969 | } |
| 5970 | } |
| 5971 | |
| 5972 | void PeerConnection::DestroyBaseChannel(cricket::BaseChannel* channel) { |
| 5973 | RTC_DCHECK(channel); |
| 5974 | RTC_DCHECK(channel->rtp_dtls_transport()); |
| 5975 | |
| 5976 | // Need to cache these before destroying the base channel so that we do not |
| 5977 | // access uninitialized memory. |
| 5978 | const std::string transport_name = |
| 5979 | channel->rtp_dtls_transport()->transport_name(); |
| 5980 | const bool need_to_delete_rtcp = (channel->rtcp_dtls_transport() != nullptr); |
| 5981 | |
| 5982 | switch (channel->media_type()) { |
| 5983 | case cricket::MEDIA_TYPE_AUDIO: |
| 5984 | channel_manager()->DestroyVoiceChannel( |
| 5985 | static_cast<cricket::VoiceChannel*>(channel)); |
| 5986 | break; |
| 5987 | case cricket::MEDIA_TYPE_VIDEO: |
| 5988 | channel_manager()->DestroyVideoChannel( |
| 5989 | static_cast<cricket::VideoChannel*>(channel)); |
| 5990 | break; |
| 5991 | case cricket::MEDIA_TYPE_DATA: |
| 5992 | channel_manager()->DestroyRtpDataChannel( |
| 5993 | static_cast<cricket::RtpDataChannel*>(channel)); |
| 5994 | break; |
| 5995 | default: |
| 5996 | RTC_NOTREACHED() << "Unknown media type: " << channel->media_type(); |
| 5997 | break; |
| 5998 | } |
| 5999 | |
| 6000 | // |channel| can no longer be used. |
| 6001 | |
Steve Anton | 75737c0 | 2017-11-06 10:37:17 -0800 | [diff] [blame] | 6002 | transport_controller_->DestroyDtlsTransport( |
| 6003 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 6004 | if (need_to_delete_rtcp) { |
| 6005 | transport_controller_->DestroyDtlsTransport( |
| 6006 | transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 6007 | } |
| 6008 | } |
| 6009 | |
Harald Alvestrand | 8906187 | 2018-01-02 14:08:34 +0100 | [diff] [blame] | 6010 | void PeerConnection::ClearStatsCache() { |
| 6011 | if (stats_collector_) { |
| 6012 | stats_collector_->ClearCachedStatsReport(); |
| 6013 | } |
| 6014 | } |
| 6015 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6016 | } // namespace webrtc |